Skip to content

Commit

Permalink
usb: implement dedicated subsystem sysctl tables
Browse files Browse the repository at this point in the history
This moves the usb related sysctl knobs to an own usb local sysctl table
in order to clean up the global sysctl as well as allow the knob to be
exported and referenced appropriately when building the usb components
as dedicated modules.

Signed-off-by: Levente Polyak <[email protected]>
  • Loading branch information
anthraxx committed Dec 14, 2020
1 parent a92b383 commit 003c531
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
1 change: 1 addition & 0 deletions drivers/usb/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ usbcore-y += phy.o port.o
usbcore-$(CONFIG_OF) += of.o
usbcore-$(CONFIG_USB_PCI) += hcd-pci.o
usbcore-$(CONFIG_ACPI) += usb-acpi.o
usbcore-$(CONFIG_SYSCTL) += sysctl.o

obj-$(CONFIG_USB) += usbcore.o

Expand Down
3 changes: 0 additions & 3 deletions drivers/usb/core/hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -5054,9 +5054,6 @@ static int descriptors_changed(struct usb_device *udev,
return changed;
}

/* sysctl */
int deny_new_usb __read_mostly = 0;

static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
u16 portchange)
{
Expand Down
44 changes: 44 additions & 0 deletions drivers/usb/core/sysctl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/kmemleak.h>
#include <linux/sysctl.h>
#include <linux/usb.h>

static struct ctl_table usb_table[] = {
{
.procname = "deny_new_usb",
.data = &deny_new_usb,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec_minmax_sysadmin,
.extra1 = SYSCTL_ZERO,
.extra2 = SYSCTL_ONE,
},
{ }
};

static struct ctl_table usb_root_table[] = {
{ .procname = "kernel",
.mode = 0555,
.child = usb_table },
{ }
};

static struct ctl_table_header *usb_table_header;

int __init usb_init_sysctl(void)
{
usb_table_header = register_sysctl_table(usb_root_table);
if (!usb_table_header) {
pr_warn("usb: sysctl registration failed\n");
return -ENOMEM;
}

kmemleak_not_leak(usb_table_header);
return 0;
}

void usb_exit_sysctl(void)
{
unregister_sysctl_table(usb_table_header);
}
9 changes: 9 additions & 0 deletions drivers/usb/core/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
#define usb_autosuspend_delay 0
#endif

int deny_new_usb __read_mostly = 0;
EXPORT_SYMBOL(deny_new_usb);

static bool match_endpoint(struct usb_endpoint_descriptor *epd,
struct usb_endpoint_descriptor **bulk_in,
struct usb_endpoint_descriptor **bulk_out,
Expand Down Expand Up @@ -978,6 +981,9 @@ static int __init usb_init(void)
usb_debugfs_init();

usb_acpi_register();
retval = usb_init_sysctl();
if (retval)
goto sysctl_init_failed;
retval = bus_register(&usb_bus_type);
if (retval)
goto bus_register_failed;
Expand Down Expand Up @@ -1012,6 +1018,8 @@ static int __init usb_init(void)
bus_notifier_failed:
bus_unregister(&usb_bus_type);
bus_register_failed:
usb_exit_sysctl();
sysctl_init_failed:
usb_acpi_unregister();
usb_debugfs_cleanup();
out:
Expand All @@ -1035,6 +1043,7 @@ static void __exit usb_exit(void)
usb_hub_cleanup();
bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
bus_unregister(&usb_bus_type);
usb_exit_sysctl();
usb_acpi_unregister();
usb_debugfs_cleanup();
idr_destroy(&usb_bus_idr);
Expand Down
10 changes: 9 additions & 1 deletion include/linux/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -2035,8 +2035,16 @@ extern void usb_led_activity(enum usb_led_event ev);
static inline void usb_led_activity(enum usb_led_event ev) {}
#endif

/* sysctl */
/* sysctl.c */
extern int deny_new_usb;
#ifdef CONFIG_SYSCTL
extern int usb_init_sysctl(void);
extern void usb_exit_sysctl(void);
#else
static inline int usb_init_sysctl(void) { return 0; }
static inline void usb_exit_sysctl(void) { }
#endif /* CONFIG_SYSCTL */


#endif /* __KERNEL__ */

Expand Down
11 changes: 0 additions & 11 deletions kernel/sysctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2322,17 +2322,6 @@ static struct ctl_table kern_table[] = {
.extra1 = SYSCTL_ZERO,
.extra2 = SYSCTL_ONE,
},
#if IS_ENABLED(CONFIG_USB)
{
.procname = "deny_new_usb",
.data = &deny_new_usb,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec_minmax_sysadmin,
.extra1 = SYSCTL_ZERO,
.extra2 = SYSCTL_ONE,
},
#endif
{
.procname = "ngroups_max",
.data = &ngroups_max,
Expand Down

0 comments on commit 003c531

Please sign in to comment.