kmod: simplify with a semaphore
Subsystems (17)
Luis Chamberlain
maintainerv2 cover kmod: simplify with a semaphore
I split the semaphore simplification work out from my first patch series [0] because as although the changes came out of that effort, in the end this set of patches are slightly orthogonal to the goal behind that series and this ended up being mostly a cleanup with mild bike shedding exercise.
As revealed from the first series, there is some tribal knowledge around why some binary semaphores are not just mutexes, so we cannot just convert them all to mutex. So I've extended Peter's patch with some of that tribal knowledge.
Changes on this v2:
o split this series up into its own
o adopt Peter's patch and extend it with some documentation as to why some folks stick to binary semaphores over mutexes
o modify kmod.c to use the preferred declaration
This goes boot tested.
[0] https://lkml.kernel.org/r/[email protected]
Luis Chamberlain (1):
modules/kmod: replace implementation with a sempahore
Peter Zijlstra (1):
Change DEFINE_SEMAPHORE() to take a number argument
arch/mips/cavium-octeon/setup.c | 2 +- arch/x86/kernel/cpu/intel.c | 2 +- drivers/firmware/efi/runtime-wrappers.c | 2 +- drivers/firmware/efi/vars.c | 2 +- drivers/macintosh/adb.c | 2 +- .../net/ethernet/broadcom/bnx2x/bnx2x_main.c | 2 +- drivers/platform/x86/intel/ifs/sysfs.c | 2 +- drivers/scsi/esas2r/esas2r_ioctl.c | 2 +- .../interface/vchiq_arm/vchiq_arm.c | 2 +- include/linux/semaphore.h | 11 ++++++-- kernel/module/kmod.c | 26 +++++-------------- kernel/printk/printk.c | 2 +- net/rxrpc/call_object.c | 6 ++--- 13 files changed, 28 insertions(+), 35 deletions(-)
Signature · 1 line
2.39.2
Luis Chamberlain
maintainerPATCH v2 1/2 Change DEFINE_SEMAPHORE() to take a number argument
From: Peter Zijlstra <[email protected]>
Fundamentally semaphores are a counted primitive, but DEFINE_SEMAPHORE() does not expose this and explicitly creates a binary semaphore.
Change DEFINE_SEMAPHORE() to take a number argument and use that in the few places that open-coded it using __SEMAPHORE_INITIALIZER().
[mcgrof: add some tribal knowledge about why some folks prefer
binary sempahores over mutexes]
--- arch/mips/cavium-octeon/setup.c | 2 +- arch/x86/kernel/cpu/intel.c | 2 +- drivers/firmware/efi/runtime-wrappers.c | 2 +- drivers/firmware/efi/vars.c | 2 +- drivers/macintosh/adb.c | 2 +- drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c | 2 +- drivers/platform/x86/intel/ifs/sysfs.c | 2 +- drivers/scsi/esas2r/esas2r_ioctl.c | 2 +- .../vc04_services/interface/vchiq_arm/vchiq_arm.c | 2 +- include/linux/semaphore.h | 11 +++++++++-- kernel/printk/printk.c | 2 +- net/rxrpc/call_object.c | 6 ++---- 12 files changed, 21 insertions(+), 16 deletions(-)
arch/mips/cavium-octeon/setup.c +1 -1
@@ -72,7 +72,7 @@ extern void pci_console_init(const char *arg);static unsigned long long max_memory =ULLONG_MAX ;static unsigned long long reserve_low_mem; -DEFINE_SEMAPHORE (octeon_bootbus_sem); +DEFINE_SEMAPHORE (octeon_bootbus_sem,1 );EXPORT_SYMBOL (octeon_bootbus_sem);static struct octeon_boot_descriptor *octeon_boot_desc_ptr;
arch/x86/kernel/cpu/intel.c +1 -1
@@ -1177,7 +1177,7 @@ static const struct {static struct ratelimit_state bld_ratelimit;static unsigned int sysctl_sld_mitigate =1 ; -static DEFINE_SEMAPHORE (buslock_sem); +static DEFINE_SEMAPHORE(buslock_sem, 1);#ifdef CONFIG_PROC_SYSCTL static struct ctl_table sld_sysctls[] = {
drivers/firmware/efi/runtime-wrappers.c +1 -1
@@ -158,7 +158,7 @@ void efi_call_virt_check_flags(unsigned long flags, const char *call)* none of the remaining functions are actually ever called at runtime. * So let's just use a single lock to serialize all Runtime Services calls. */ -static DEFINE_SEMAPHORE (efi_runtime_lock); +static DEFINE_SEMAPHORE(efi_runtime_lock, 1);/* * Expose the EFI runtime lock to the UV platform
drivers/firmware/efi/vars.c +1 -1
@@ -21,7 +21,7 @@/* Private pointer to registered efivars */ static struct efivars *__efivars; -static DEFINE_SEMAPHORE (efivars_lock); +static DEFINE_SEMAPHORE(efivars_lock, 1);static efi_status_t check_var_size (bool nonblocking,u32 attributes,unsigned long size)
drivers/macintosh/adb.c +1 -1
@@ -80,7 +80,7 @@ static struct adb_driver *adb_controller;BLOCKING_NOTIFIER_HEAD (adb_client_list);static int adb_got_sleep;static int adb_inited; -static DEFINE_SEMAPHORE (adb_probe_mutex); +static DEFINE_SEMAPHORE(adb_probe_mutex, 1);static int sleepy_trackpad;static int autopoll_devs;int __adb_probe_sync;
drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c +1 -1
@@ -298,7 +298,7 @@ const u32 dmae_reg_go_c[] = {/* Global resources for unloading a previously loaded device */ #define BNX2X_PREV_WAIT_NEEDED 1 -static DEFINE_SEMAPHORE (bnx2x_prev_sem); +static DEFINE_SEMAPHORE(bnx2x_prev_sem, 1);static LIST_HEAD (bnx2x_prev_list);/* Forward declaration */
drivers/platform/x86/intel/ifs/sysfs.c +1 -1
@@ -13,7 +13,7 @@* Protects against simultaneous tests on multiple cores, or * reloading can file while a test is in progress */ -static DEFINE_SEMAPHORE (ifs_sem); +static DEFINE_SEMAPHORE(ifs_sem, 1);/* * The sysfs interface to check additional details of last test
drivers/scsi/esas2r/esas2r_ioctl.c +1 -1
@@ -56,7 +56,7 @@ dma_addr_t esas2r_buffered_ioctl_addr;u32 esas2r_buffered_ioctl_size;struct pci_dev *esas2r_buffered_ioctl_pcid; -static DEFINE_SEMAPHORE (buffered_ioctl_semaphore); +static DEFINE_SEMAPHORE(buffered_ioctl_semaphore, 1); typedef int (*BUFFERED_IOCTL_CALLBACK)(struct esas2r_adapter *, struct esas2r_request *, struct esas2r_sg_context *,
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c +1 -1
@@ -149,7 +149,7 @@ static char *g_fragments_base;static char *g_free_fragments;static struct semaphore g_free_fragments_sema; -static DEFINE_SEMAPHORE (g_free_fragments_mutex); +static DEFINE_SEMAPHORE(g_free_fragments_mutex, 1); static int vchiq_blocking_bulk_transfer(struct vchiq_instance *instance, unsigned int handle, void *data,
include/linux/semaphore.h +9 -2
@@ -25,8 +25,15 @@ struct semaphore { .wait_list = LIST_HEAD_INIT((name).wait_list), \ } -#define DEFINE_SEMAPHORE (name) \ - struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1) +/* +* There is a big difference between a binary semaphore and a mutex. +* You cannot call mutex_unlock() from IRQ context because it takes an +* internal mutex spin_lock in a non-IRQ-safe manner. Both try_lock() +* and unlock() can be called from IRQ context. A mutex must also be +* released in the same context that locked it. +*/ +#define DEFINE_SEMAPHORE (_name, _n) \ + struct semaphore _name = __SEMAPHORE_INITIALIZER(_name, _n)static inline void sema_init (struct semaphore *sem,int val) {
kernel/printk/printk.c +1 -1
@@ -89,7 +89,7 @@ static DEFINE_MUTEX(console_mutex);* console_sem protects updates to console->seq and console_suspended, * and also provides serialization for console printing. */ -static DEFINE_SEMAPHORE (console_sem); +static DEFINE_SEMAPHORE(console_sem, 1);HLIST_HEAD (console_list);EXPORT_SYMBOL_GPL (console_list);DEFINE_STATIC_SRCU (console_srcu);
net/rxrpc/call_object.c +2 -4
@@ -40,10 +40,8 @@ const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = {struct kmem_cache *rxrpc_call_jar; -static struct semaphore rxrpc_call_limiter = -__SEMAPHORE_INITIALIZER (rxrpc_call_limiter,1000 ); -static struct semaphore rxrpc_kernel_call_limiter = -__SEMAPHORE_INITIALIZER (rxrpc_kernel_call_limiter,1000 ); +static DEFINE_SEMAPHORE(rxrpc_call_limiter, 1000); +static DEFINE_SEMAPHORE(rxrpc_kernel_call_limiter, 1000);void rxrpc_poke_call (struct rxrpc_call *call,enum rxrpc_call_poke_trace what) {
Signature · 1 line
2.39.2
Luis Chamberlain
maintainerPATCH v2 2/2 modules/kmod: replace implementation with a sempahore
In reply to Luis ChamberlainSimplfy the concurrency delimiter we user for kmod with the semaphore. I had used the kmod strategy to try to implement a similar concurrency delimiter for the kernel_read*() calls from the finit_module() path so to reduce vmalloc() memory pressure. That effort didn't provid yet conclusive results, but one thing that did became clear is we can use the suggested alternative solution with semaphores which Linus hinted at instead of using the atomic / wait strategy.
I've stress tested this with kmod test 0008:
time /data/linux-next/tools/testing/selftests/kmod/kmod.sh -t 0008
And I get only a *slight* delay. That delay however is small, a few seconds for a full test loop run that runs 150 times, for about ~30-40 seconds. The small delay is worth the simplfication IMHO.
--- kernel/module/kmod.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-)
kernel/module/kmod.c +7 -19
@@ -40,8 +40,7 @@* effect. Systems like these are very unlikely if modules are enabled. */ #define MAX_KMOD_CONCURRENT 50 -static atomic_t kmod_concurrent_max =ATOMIC_INIT (MAX_KMOD_CONCURRENT ); -static DECLARE_WAIT_QUEUE_HEAD (kmod_wq); +static DEFINE_SEMAPHORE(kmod_concurrent_max, MAX_KMOD_CONCURRENT);/* * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads
@@ -148,29 +147,18 @@ int __request_module(bool wait, const char *fmt, ...)if (ret)return ret; -if (atomic_dec_if_positive (&kmod_concurrent_max) <0 ) { -pr_warn_ratelimited ("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling..." , -atomic_read (&kmod_concurrent_max), -MAX_KMOD_CONCURRENT , module_name); - ret =wait_event_killable_timeout (kmod_wq, -atomic_dec_if_positive (&kmod_concurrent_max) >=0 , -MAX_KMOD_ALL_BUSY_TIMEOUT *HZ ); -if (!ret) { -pr_warn_ratelimited ("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now" , - module_name,MAX_KMOD_CONCURRENT ,MAX_KMOD_ALL_BUSY_TIMEOUT ); -return -ETIME ; - }else if (ret == -ERESTARTSYS ) { -pr_warn_ratelimited ("request_module: sigkill sent for modprobe %s, giving up" , module_name); -return ret; - } + ret =down_timeout (&kmod_concurrent_max,MAX_KMOD_ALL_BUSY_TIMEOUT ); +if (ret) { +pr_warn_ratelimited ("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now" , + module_name,MAX_KMOD_CONCURRENT ,MAX_KMOD_ALL_BUSY_TIMEOUT ); +return ret; }trace_module_request (module_name, wait, _RET_IP_); ret =call_modprobe (module_name, wait ?UMH_WAIT_PROC :UMH_WAIT_EXEC ); -atomic_inc (&kmod_concurrent_max); -wake_up (&kmod_wq); +up (&kmod_concurrent_max);return ret; }
Signature · 1 line
2.39.2
Re: [PATCH v2 1/2] Change DEFINE_SEMAPHORE() to take a number argument
In reply to Luis ChamberlainQuoted 8 lines · Luis Chamberlain
From: Peter Zijlstra <[email protected]>
Fundamentally semaphores are a counted primitive, but
DEFINE_SEMAPHORE()does not expose this and explicitly creates a binary semaphore.Change
DEFINE_SEMAPHORE()to take a number argument and use that in the few places that open-coded it using__SEMAPHORE_INITIALIZER().
No big deal considering the changelog is small, but I would have expected just for __SEMAPHORE_INITIALIZER() to be used in the next patch, instead of changing DEFINE_SEMAPHORE, which users just need because of the mutex api restrictions.
Quoted 5 lines
Signed-off-by Peter Zijlstra (Intel) <[email protected]>[mcgrof: add some tribal knowledge about why some folks prefer binary sempahores over mutexes]
Signed-off-by Luis Chamberlain <[email protected]>
Quoted 187 lines
---
arch/mips/cavium-octeon/setup.c | 2 +- arch/x86/kernel/cpu/intel.c | 2 +- drivers/firmware/efi/runtime-wrappers.c | 2 +- drivers/firmware/efi/vars.c | 2 +- drivers/macintosh/adb.c | 2 +- drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c | 2 +- drivers/platform/x86/intel/ifs/sysfs.c | 2 +- drivers/scsi/esas2r/esas2r_ioctl.c | 2 +- .../vc04_services/interface/vchiq_arm/vchiq_arm.c | 2 +- include/linux/semaphore.h | 11 +++++++++-- kernel/printk/printk.c | 2 +- net/rxrpc/call_object.c | 6 ++---- 12 files changed, 21 insertions(+), 16 deletions(-)arch/mips/cavium-octeon/setup.c +1
-1@@ -72,7 +72,7 @@ extern void pci_console_init(const char *arg);static unsigned long long max_memory =ULLONG_MAX ;static unsigned long long reserve_low_mem; -DEFINE_SEMAPHORE (octeon_bootbus_sem); +DEFINE_SEMAPHORE (octeon_bootbus_sem,1 );EXPORT_SYMBOL (octeon_bootbus_sem);static struct octeon_boot_descriptor *octeon_boot_desc_ptr;arch/x86/kernel/cpu/intel.c +1
-1@@ -1177,7 +1177,7 @@ static const struct {static struct ratelimit_state bld_ratelimit;static unsigned int sysctl_sld_mitigate =1 ; -static DEFINE_SEMAPHORE (buslock_sem); +static DEFINE_SEMAPHORE(buslock_sem, 1);#ifdef CONFIG_PROC_SYSCTL static struct ctl_table sld_sysctls[] = {drivers/firmware/efi/runtime-wrappers.c +1
-1@@ -158,7 +158,7 @@ void efi_call_virt_check_flags(unsigned long flags, const char *call)* none of the remaining functions are actually ever called at runtime. * So let's just use a single lock to serialize all Runtime Services calls. */ -static DEFINE_SEMAPHORE (efi_runtime_lock); +static DEFINE_SEMAPHORE(efi_runtime_lock, 1);/* * Expose the EFI runtime lock to the UV platform drivers/firmware/efi/vars.c +1
-1@@ -21,7 +21,7 @@/* Private pointer to registered efivars */ static struct efivars *__efivars; -static DEFINE_SEMAPHORE (efivars_lock); +static DEFINE_SEMAPHORE(efivars_lock, 1);static efi_status_t check_var_size (bool nonblocking,u32 attributes,unsigned long size)drivers/macintosh/adb.c +1
-1@@ -80,7 +80,7 @@ static struct adb_driver *adb_controller;BLOCKING_NOTIFIER_HEAD (adb_client_list);static int adb_got_sleep;static int adb_inited; -static DEFINE_SEMAPHORE (adb_probe_mutex); +static DEFINE_SEMAPHORE(adb_probe_mutex, 1);static int sleepy_trackpad;static int autopoll_devs;int __adb_probe_sync;drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c +1
-1@@ -298,7 +298,7 @@ const u32 dmae_reg_go_c[] = {/* Global resources for unloading a previously loaded device */ #define BNX2X_PREV_WAIT_NEEDED 1 -static DEFINE_SEMAPHORE (bnx2x_prev_sem); +static DEFINE_SEMAPHORE(bnx2x_prev_sem, 1);static LIST_HEAD (bnx2x_prev_list);/* Forward declaration */ drivers/platform/x86/intel/ifs/sysfs.c +1
-1@@ -13,7 +13,7 @@* Protects against simultaneous tests on multiple cores, or * reloading can file while a test is in progress */ -static DEFINE_SEMAPHORE (ifs_sem); +static DEFINE_SEMAPHORE(ifs_sem, 1);/* * The sysfs interface to check additional details of last test drivers/scsi/esas2r/esas2r_ioctl.c +1
-1@@ -56,7 +56,7 @@ dma_addr_t esas2r_buffered_ioctl_addr;u32 esas2r_buffered_ioctl_size;struct pci_dev *esas2r_buffered_ioctl_pcid; -static DEFINE_SEMAPHORE (buffered_ioctl_semaphore); +static DEFINE_SEMAPHORE(buffered_ioctl_semaphore, 1); typedef int (*BUFFERED_IOCTL_CALLBACK)(struct esas2r_adapter *, struct esas2r_request *, struct esas2r_sg_context *,drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c +1
-1@@ -149,7 +149,7 @@ static char *g_fragments_base;static char *g_free_fragments;static struct semaphore g_free_fragments_sema; -static DEFINE_SEMAPHORE (g_free_fragments_mutex); +static DEFINE_SEMAPHORE(g_free_fragments_mutex, 1);static int
vchiq_blocking_bulk_transfer(structvchiq_instance*instance, unsigned int handle, void *data,include/linux/semaphore.h +9
-2@@ -25,8 +25,15 @@ struct semaphore { .wait_list = LIST_HEAD_INIT((name).wait_list), \ } -#define DEFINE_SEMAPHORE (name) \ - struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1) +/* +* There is a big difference between a binary semaphore and a mutex. +* You cannot call mutex_unlock() from IRQ context because it takes an +* internal mutex spin_lock in a non-IRQ-safe manner. Both try_lock() +* and unlock() can be called from IRQ context. A mutex must also be +* released in the same context that locked it. +*/ +#define DEFINE_SEMAPHORE (_name, _n) \ + struct semaphore _name = __SEMAPHORE_INITIALIZER(_name, _n)static inline void sema_init (struct semaphore *sem,int val) {kernel/printk/printk.c +1
-1@@ -89,7 +89,7 @@ static DEFINE_MUTEX(console_mutex);* console_sem protects updates to console->seq and console_suspended, * and also provides serialization for console printing. */ -static DEFINE_SEMAPHORE (console_sem); +static DEFINE_SEMAPHORE(console_sem, 1);HLIST_HEAD (console_list);EXPORT_SYMBOL_GPL (console_list);DEFINE_STATIC_SRCU (console_srcu);net/rxrpc/call_object.c +2
-5@@ -40,10 +40,8 @@ const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = {struct kmem_cache *rxrpc_call_jar; -static struct semaphore rxrpc_call_limiter = -__SEMAPHORE_INITIALIZER (rxrpc_call_limiter,1000 ); -static struct semaphore rxrpc_kernel_call_limiter = -__SEMAPHORE_INITIALIZER (rxrpc_kernel_call_limiter,1000 ); +static DEFINE_SEMAPHORE(rxrpc_call_limiter, 1000); +static DEFINE_SEMAPHORE(rxrpc_kernel_call_limiter, 1000);void rxrpc_poke_call (struct rxrpc_call *call,enum rxrpc_call_poke_trace what) { --2.39.2
Re: [PATCH v2 2/2] modules/kmod: replace implementation with a sempahore
In reply to Luis ChamberlainIn the title: s/sempahore/semaphore
Quoted 8 lines · Luis Chamberlain
Simplfy the concurrency delimiter we user for kmod with the semaphore. I had used the kmod strategy to try to implement a similar concurrency delimiter for the kernel_read*() calls from the
finit_module()path so to reducevmalloc()memory pressure. That effort didn't provid yet conclusive results, but one thing that did became clear is we can use the suggested alternative solution with semaphores which Linus hinted at instead of using the atomic / wait strategy.
Quoted 7 lines
I've stress tested this with kmod test 0008:
time /data/linux-next/tools/testing/selftests/kmod/kmod.sh -t 0008
And I get only a *slight* delay. That delay however is small, a few seconds for a full test loop run that runs 150 times, for about ~30-40 seconds. The small delay is worth the simplfication IMHO.
Yes, code looks a lot nicer.
Quoted 58 lines
Signed-off-by Luis Chamberlain <[email protected]>---
kernel/module/kmod.c| 26 +++++++-------------------1 file changed, 7 insertions(+), 19 deletions(-)kernel/module/kmod.c +7
-20@@ -40,8 +40,7 @@* effect. Systems like these are very unlikely if modules are enabled. */ #define MAX_KMOD_CONCURRENT 50 -static atomic_t kmod_concurrent_max =ATOMIC_INIT (MAX_KMOD_CONCURRENT ); -static DECLARE_WAIT_QUEUE_HEAD (kmod_wq); +static DEFINE_SEMAPHORE(kmod_concurrent_max, MAX_KMOD_CONCURRENT);/* * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads @@ -148,29 +147,18 @@ int __request_module(bool wait, const char *fmt, ...)if (ret)return ret; -if (atomic_dec_if_positive (&kmod_concurrent_max) <0 ) { -pr_warn_ratelimited ("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling..." , -atomic_read (&kmod_concurrent_max), -MAX_KMOD_CONCURRENT , module_name); - ret =wait_event_killable_timeout (kmod_wq, -atomic_dec_if_positive (&kmod_concurrent_max) >=0 , -MAX_KMOD_ALL_BUSY_TIMEOUT *HZ ); -if (!ret) { -pr_warn_ratelimited ("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now" , - module_name,MAX_KMOD_CONCURRENT ,MAX_KMOD_ALL_BUSY_TIMEOUT ); -return -ETIME ; - }else if (ret == -ERESTARTSYS ) { -pr_warn_ratelimited ("request_module: sigkill sent for modprobe %s, giving up" , module_name); -return ret; - } + ret =down_timeout (&kmod_concurrent_max,MAX_KMOD_ALL_BUSY_TIMEOUT ); +if (ret) { +pr_warn_ratelimited ("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now" , + module_name,MAX_KMOD_CONCURRENT ,MAX_KMOD_ALL_BUSY_TIMEOUT ); +return ret; }trace_module_request (module_name, wait, _RET_IP_); ret =call_modprobe (module_name, wait ?UMH_WAIT_PROC :UMH_WAIT_EXEC ); -atomic_inc (&kmod_concurrent_max); -wake_up (&kmod_wq); +up (&kmod_concurrent_max);return ret; } --2.39.2
Re: [PATCH v2 1/2] Change DEFINE_SEMAPHORE() to take a number argument
In reply to Luis ChamberlainQuoted 7 lines · Luis Chamberlain
include/linux/semaphore.h +0
-0@@ -25,8 +25,15 @@ struct semaphore { .wait_list = LIST_HEAD_INIT((name).wait_list), \ }
include/linux/semaphore.h struct semaphore { · line 34
Quoted 10 lines
include/linux/semaphore.h +7
-2-#define DEFINE_SEMAPHORE (name) \ - struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1) +/* +* There is a big difference between a binary semaphore and a mutex. +* You cannot call mutex_unlock() from IRQ context because it takes an +* internal mutex spin_lock in a non-IRQ-safe manner. Both try_lock() +* and unlock() can be called from IRQ context. A mutex must also be +* released in the same context that locked it. +*/
I think this confuses cause and effect. How about this:
/*
* Binary semaphores and mutexes differ in that mutexes have an owner
* so they cannot be used from interrupt context and cannot be passed
* from one thread to another. down_trylock() and up() can be called
* from interrupt context.
*/
Or this:
/*
* Unlike mutexes, binary semaphores do not have an owner, so up() can
* be called in a different thread from the one which called down().
* It is also safe to call down_trylock() and up() from interrupt
* context.
*/
I'd like to mention completions as an alternative to semaphores, but can't figure out a nice way to fit that in.
Re: [PATCH v2 1/2] Change DEFINE_SEMAPHORE() to take a number argument
Quoted 3 lines · Matthew Wilcox
I'd like to mention completions as an alternative to semaphores, but can't figure out a nice way to fit that in.
I'm personally a bit sorry completions ever became a thing.
There's a real reason for having them, but they have been used and mis-used in so many confusing ways that I am worried every time I see a completion. We've had some nasty use of 'init_completion()' in particular.
There are many obvious uses of completions, and they have nice strict semantics wrt last-use etc (so that you can put them on the stack and know that you're the last user when you return, which is not necessarily true of locking in general).
But there are several less-than-obvious uses too, and any use of reinit_completion() ends up just making me go "Uhh". The serialization needed for that to actually work right often means that you might as well have used a "wait_event()" with a "smp_store_release()" variable instead and made the code more obvious.
I dunno. I might have had a few bad experiences and it's just rare enough to be one of those things that I feel wasn't worth the abstraction cost.
And I can't even blame anybody else. I think I'm to blame for that horror.
Linus
Re: [PATCH v2 1/2] Change DEFINE_SEMAPHORE() to take a number argument
In reply to Luis Chamberlainkernel/printk/printk.c static DEFINE_MUTEX(console_mutex); · line 92
Quoted 7 lines · Luis Chamberlain
+++
b/kernel/printk/printk.ckernel/printk/printk.c +1
-1@@ -89,7 +89,7 @@ static DEFINE_MUTEX(console_mutex);* console_sem protects updates to console->seq and console_suspended, * and also provides serialization for console printing. */ -static DEFINE_SEMAPHORE (console_sem); +static DEFINE_SEMAPHORE(console_sem, 1);
FWIW,
Re: [PATCH v2 2/2] modules/kmod: replace implementation with a sempahore
In reply to Luis ChamberlainQuoted 61 lines · Luis Chamberlain
Simplfy the concurrency delimiter we user for kmod with the semaphore. I had used the kmod strategy to try to implement a similar concurrency delimiter for the kernel_read*() calls from the
finit_module()path so to reducevmalloc()memory pressure. That effort didn't provid yet conclusive results, but one thing that did became clear is we can use the suggested alternative solution with semaphores which Linus hinted at instead of using the atomic / wait strategy.I've stress tested this with kmod test 0008:
time /data/linux-next/tools/testing/selftests/kmod/kmod.sh -t 0008
And I get only a *slight* delay. That delay however is small, a few seconds for a full test loop run that runs 150 times, for about ~30-40 seconds. The small delay is worth the simplfication IMHO.
Signed-off-by Luis Chamberlain <[email protected]>--- kernel/module/kmod.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-)kernel/module/kmod.c +6
-17@@ -40,8 +40,7 @@* effect. Systems like these are very unlikely if modules are enabled. */ #define MAX_KMOD_CONCURRENT 50 -static atomic_t kmod_concurrent_max =ATOMIC_INIT (MAX_KMOD_CONCURRENT ); -static DECLARE_WAIT_QUEUE_HEAD (kmod_wq); +static DEFINE_SEMAPHORE(kmod_concurrent_max, MAX_KMOD_CONCURRENT);/* * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads @@ -148,29 +147,18 @@ int __request_module(bool wait, const char *fmt, ...)if (ret)return ret; -if (atomic_dec_if_positive (&kmod_concurrent_max) <0 ) { -pr_warn_ratelimited ("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling..." , -atomic_read (&kmod_concurrent_max), -MAX_KMOD_CONCURRENT , module_name); - ret =wait_event_killable_timeout (kmod_wq, -atomic_dec_if_positive (&kmod_concurrent_max) >=0 , -MAX_KMOD_ALL_BUSY_TIMEOUT *HZ ); -if (!ret) { -pr_warn_ratelimited ("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now" , - module_name,MAX_KMOD_CONCURRENT ,MAX_KMOD_ALL_BUSY_TIMEOUT ); -return -ETIME ; - }else if (ret == -ERESTARTSYS ) { -pr_warn_ratelimited ("request_module: sigkill sent for modprobe %s, giving up" , module_name); -return ret; - } + ret =down_timeout (&kmod_concurrent_max,MAX_KMOD_ALL_BUSY_TIMEOUT ); +if (ret) { +pr_warn_ratelimited ("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now" , + module_name,MAX_KMOD_CONCURRENT ,MAX_KMOD_ALL_BUSY_TIMEOUT ); +return ret; }
kernel/module/kmod.c int __request_module(bool wait, const char *fmt, ...) · line 164
Quoted 10 lines
kernel/module/kmod.c +1
-2trace_module_request (module_name, wait, _RET_IP_); ret =call_modprobe (module_name, wait ?UMH_WAIT_PROC :UMH_WAIT_EXEC ); -atomic_inc (&kmod_concurrent_max); -wake_up (&kmod_wq); +up (&kmod_concurrent_max);return ret; }
Much cleaner
Signature · 3 lines
Thanks, David / dhildenb
Luis Chamberlain
maintainerRe: [PATCH v2 1/2] Change DEFINE_SEMAPHORE() to take a number argument
In reply to Matthew WilcoxQuoted 8 lines · Matthew Wilcox
Or this:
/*
* Unlike mutexes, binary semaphores do not have an owner, soup()can
* be called in a different thread from the one which calleddown().
* It is also safe to calldown_trylock()andup()from interrupt
* context.
*/
I went with that. Thanks for helping me paint this shed!
Luis
Re: [PATCH v2 2/2] modules/kmod: replace implementation with a sempahore
In reply to Luis ChamberlainHi,
On Wed, 5 Apr 2023, Luis Chamberlain wrote:
s/sempahore/semaphore/ in the subject
Quoted 1 line
Simplfy the concurrency delimiter we user for kmod with the semaphore.
"Simplify the concurrency delimiter we use for kmod with the semaphore."
(two typos)
Quoted 3 lines
I had used the kmod strategy to try to implement a similar concurrency delimiter for the kernel_read*() calls from the
finit_module()path so to reducevmalloc()memory pressure. That effort didn't provid yet
s/provid/provide/
Quoted 1 line
conclusive results, but one thing that did became clear is we can use
s/did // (or s/became/become/)
Quoted 40 lines
the suggested alternative solution with semaphores which Linus hinted at instead of using the atomic / wait strategy.
I've stress tested this with kmod test 0008:
time /data/linux-next/tools/testing/selftests/kmod/kmod.sh -t 0008
And I get only a *slight* delay. That delay however is small, a few seconds for a full test loop run that runs 150 times, for about ~30-40 seconds. The small delay is worth the simplfication IMHO.
Signed-off-by Luis Chamberlain <[email protected]>--- kernel/module/kmod.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-)kernel/module/kmod.c +1
-8@@ -40,8 +40,7 @@* effect. Systems like these are very unlikely if modules are enabled. */ #define MAX_KMOD_CONCURRENT 50 -static atomic_t kmod_concurrent_max =ATOMIC_INIT (MAX_KMOD_CONCURRENT ); -static DECLARE_WAIT_QUEUE_HEAD (kmod_wq); +static DEFINE_SEMAPHORE(kmod_concurrent_max, MAX_KMOD_CONCURRENT);/* * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads @@ -148,29 +147,18 @@ int __request_module(bool wait, const char *fmt, ...)if (ret)return ret; -if (atomic_dec_if_positive (&kmod_concurrent_max) <0 ) { -pr_warn_ratelimited ("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling..." , -atomic_read (&kmod_concurrent_max), -MAX_KMOD_CONCURRENT , module_name); - ret = wait_event_killable_timeout(kmod_wq, - atomic_dec_if_positive(&kmod_concurrent_max) >= 0,
kernel/module/kmod.c int __request_module(bool wait, const char *fmt, ...) · line 150
Quoted 10 lines
kernel/module/kmod.c +1
-9- MAX_KMOD_ALL_BUSY_TIMEOUT * HZ); -if (!ret) { -pr_warn_ratelimited ("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now" , - module_name,MAX_KMOD_CONCURRENT ,MAX_KMOD_ALL_BUSY_TIMEOUT ); -return -ETIME ; - }else if (ret == -ERESTARTSYS ) { -pr_warn_ratelimited ("request_module: sigkill sent for modprobe %s, giving up" , module_name); -return ret; - } + ret =down_timeout (&kmod_concurrent_max,MAX_KMOD_ALL_BUSY_TIMEOUT );
MAX_KMOD_ALL_BUSY_TIMEOUT * HZ ?
The simplification is very nice.
Miroslav
Luis Chamberlain
maintainerRe: [PATCH v2 2/2] modules/kmod: replace implementation with a sempahore
Quoted 1 line · Miroslav Benes
Hi,
<spell checks>
Thanks, fixed!
Quoted 1 line
MAX_KMOD_ALL_BUSY_TIMEOUT* HZ ?
Yes! In the end schedule_timeout() is used so yup, thanks!
Luis