From 003c53150680304debf75ac6271542f052ca3d1a Mon Sep 17 00:00:00 2001 From: Levente Polyak Date: Sun, 6 Sep 2020 21:08:16 +0200 Subject: [PATCH] usb: implement dedicated subsystem sysctl tables 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 --- drivers/usb/core/Makefile | 1 + drivers/usb/core/hub.c | 3 --- drivers/usb/core/sysctl.c | 44 +++++++++++++++++++++++++++++++++++++++ drivers/usb/core/usb.c | 9 ++++++++ include/linux/usb.h | 10 ++++++++- kernel/sysctl.c | 11 ---------- 6 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 drivers/usb/core/sysctl.c diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile index 18e874b0441e3..fc7a3a9aa72af 100644 --- a/drivers/usb/core/Makefile +++ b/drivers/usb/core/Makefile @@ -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 diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index 9385c745d55ee..b62b3da81ac49 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c @@ -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) { diff --git a/drivers/usb/core/sysctl.c b/drivers/usb/core/sysctl.c new file mode 100644 index 0000000000000..3fa188ac8f67b --- /dev/null +++ b/drivers/usb/core/sysctl.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +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); +} diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c index 9b4ac4415f1a4..93b4b798bdccb 100644 --- a/drivers/usb/core/usb.c +++ b/drivers/usb/core/usb.c @@ -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, @@ -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; @@ -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: @@ -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); diff --git a/include/linux/usb.h b/include/linux/usb.h index 8e7549e3012ae..653265115e560 100644 --- a/include/linux/usb.h +++ b/include/linux/usb.h @@ -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__ */ diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 0435bd5c8ba7a..2d8b19cbb7717 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -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,