Messages in this thread | | | From | John Ogness <> | Subject | Re: [PATCH next v3 1/2] dump_stack: move cpu lock to printk.c | Date | Wed, 16 Jun 2021 15:46:02 +0206 |
| |
On 2021-06-16, Petr Mladek <pmladek@suse.com> wrote: >> With this series version I moved the tracking into a global variable >> @printk_cpulock_nested, which is fine, except that a boolean is not >> capable of tracking more than 1 nesting. Which means that >> __printk_cpu_unlock() would release cpu lock ownership too soon. >> >> Doing this correctly is a simple change: >> >> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c >> index e67dc510fa1b..5376216e4f3d 100644 >> --- a/kernel/printk/printk.c >> +++ b/kernel/printk/printk.c >> @@ -3535,7 +3535,7 @@ EXPORT_SYMBOL_GPL(kmsg_dump_rewind); >> >> #ifdef CONFIG_SMP >> static atomic_t printk_cpulock_owner = ATOMIC_INIT(-1); >> -static bool printk_cpulock_nested; >> +static atomic_t printk_cpulock_nested = ATOMIC_INIT(0); >> >> /** >> * __printk_wait_on_cpu_lock() - Busy wait until the printk cpu-reentrant >> @@ -3596,7 +3598,7 @@ int __printk_cpu_trylock(void) >> >> } else if (old == cpu) { >> /* This CPU is already the owner. */ >> - printk_cpulock_nested = true; >> + atomic_inc(&printk_cpulock_nested); >> return 1; >> } >> >> @@ -3613,8 +3615,8 @@ EXPORT_SYMBOL(__printk_cpu_trylock); >> */ >> void __printk_cpu_unlock(void) >> { >> - if (printk_cpulock_nested) { >> - printk_cpulock_nested = false; >> + if (atomic_read(&printk_cpulock_nested)) { >> + atomic_dec(&printk_cpulock_nested); > > I think about handling printk_cpulock_nested with only one > atomic operation. Something like: > > if (atomic_dec_return(&printk_cpulock_level) == 0) > atomic_set_release(&printk_cpulock_owner, -1); > > It would require always incremanting the number in lock, e.g. > > old = atomic_cmpxchg(&printk_cpulock_owner, -1, cpu); > if (old == -1 || old == cpu) { > atomic_inc(&printk_cpulock_level); > return 1; > }
I actually implemented similar code during an internal draft. I later decided against it, mainly because I prefer to keep the old==-1 and old==cpu cases separate.
Also note that atomic_dec_return() introduces an unnecessary memory barrier. If we take your proposed implementation we would use atomic_dec_return_relaxed() instead.
> But I am not sure if it is really better. Feel free to keep > your variant.
*sigh* Frankly, I don't care much. My variant saves a few CPU instructions for the normal case (non-nested), but that probably is not much of an argument.
For v4 I will keep my variant because it explicitly handles the non-nested/nested cases separately, which helps when adding the memory barrier comments in the follow-up patch. In particular, the label LMM(__printk_cpu_trylock:B), which represents the first moment a new CPU begins to load/store data, only applies to the old==-1 condition.
John Ogness
| |