From d4bfc75ab4b2226b714f6cc85a93bd618ebe2a60 Mon Sep 17 00:00:00 2001 From: "yang.yang29@zte.com.cn" Date: Mon, 12 Jun 2023 15:42:24 +0800 Subject: [PATCH 1/7] eventfd: Make signal recursion protection a task bit commit 601bfccadc8f59c25eccb56f4d331aa2a1ab34af upstream. Upstream commit b542e383d8c005f06a131e2b40d5889b812f19c6 The recursion protection for eventfd_signal() is based on a per CPU variable and relies on the !RT semantics of spin_lock_irqsave() for protecting this per CPU variable. On RT kernels spin_lock_irqsave() neither disables preemption nor interrupts which allows the spin lock held section to be preempted. If the preempting task invokes eventfd_signal() as well, then the recursion warning triggers. Paolo suggested to protect the per CPU variable with a local lock, but that's heavyweight and actually not necessary. The goal of this protection is to prevent the task stack from overflowing, which can be achieved with a per task recursion protection as well. Replace the per CPU variable with a per task bit similar to other recursion protection bits like task_struct::in_page_owner. This works on both !RT and RT kernels and removes as a side effect the extra per CPU storage. No functional change for !RT kernels. Reported-by: Daniel Bristot de Oliveira Signed-off-by: Thomas Gleixner Tested-by: Daniel Bristot de Oliveira Acked-by: Jason Wang Cc: Al Viro Link: https://lore.kernel.org/r/87wnp9idso.ffs@tglx Signed-off-by: Sebastian Andrzej Siewior --- fs/aio.c | 2 +- fs/eventfd.c | 12 +++++------- include/linux/eventfd.h | 11 +++++------ include/linux/sched.h | 4 ++++ 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/fs/aio.c b/fs/aio.c index c72b2c51b446..f7d47c9ff6de 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -1761,7 +1761,7 @@ static int aio_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, list_del_init(&req->wait.entry); list_del(&iocb->ki_list); iocb->ki_res.res = mangle_poll(mask); - if (iocb->ki_eventfd && eventfd_signal_count()) { + if (iocb->ki_eventfd && eventfd_signal_allowed()) { iocb = NULL; INIT_WORK(&req->work, aio_poll_put_work); schedule_work(&req->work); diff --git a/fs/eventfd.c b/fs/eventfd.c index df466ef81ddd..9035ca60bfcf 100644 --- a/fs/eventfd.c +++ b/fs/eventfd.c @@ -25,8 +25,6 @@ #include #include -DEFINE_PER_CPU(int, eventfd_wake_count); - static DEFINE_IDA(eventfd_ida); struct eventfd_ctx { @@ -67,21 +65,21 @@ __u64 eventfd_signal(struct eventfd_ctx *ctx, __u64 n) * Deadlock or stack overflow issues can happen if we recurse here * through waitqueue wakeup handlers. If the caller users potentially * nested waitqueues with custom wakeup handlers, then it should - * check eventfd_signal_count() before calling this function. If - * it returns true, the eventfd_signal() call should be deferred to a + * check eventfd_signal_allowed() before calling this function. If + * it returns false, the eventfd_signal() call should be deferred to a * safe context. */ - if (WARN_ON_ONCE(this_cpu_read(eventfd_wake_count))) + if (WARN_ON_ONCE(current->in_eventfd_signal)) return 0; spin_lock_irqsave(&ctx->wqh.lock, flags); - this_cpu_inc(eventfd_wake_count); + current->in_eventfd_signal = 1; if (ULLONG_MAX - ctx->count < n) n = ULLONG_MAX - ctx->count; ctx->count += n; if (waitqueue_active(&ctx->wqh)) wake_up_locked_poll(&ctx->wqh, EPOLLIN); - this_cpu_dec(eventfd_wake_count); + current->in_eventfd_signal = 0; spin_unlock_irqrestore(&ctx->wqh.lock, flags); return n; diff --git a/include/linux/eventfd.h b/include/linux/eventfd.h index dc4fd8a6644d..836b4c021a0a 100644 --- a/include/linux/eventfd.h +++ b/include/linux/eventfd.h @@ -14,6 +14,7 @@ #include #include #include +#include /* * CAREFUL: Check include/uapi/asm-generic/fcntl.h when defining @@ -42,11 +43,9 @@ __u64 eventfd_signal(struct eventfd_ctx *ctx, __u64 n); int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, wait_queue_entry_t *wait, __u64 *cnt); -DECLARE_PER_CPU(int, eventfd_wake_count); - -static inline bool eventfd_signal_count(void) +static inline bool eventfd_signal_allowed(void) { - return this_cpu_read(eventfd_wake_count); + return !current->in_eventfd_signal; } #else /* CONFIG_EVENTFD */ @@ -77,9 +76,9 @@ static inline int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, return -ENOSYS; } -static inline bool eventfd_signal_count(void) +static inline bool eventfd_signal_allowed(void) { - return false; + return true; } #endif diff --git a/include/linux/sched.h b/include/linux/sched.h index d59aa4f198d9..d81141fb7598 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -836,6 +836,10 @@ struct task_struct { #ifdef CONFIG_BLK_DEV_IO_TRACE unsigned int btrace_seq; #endif +#ifdef CONFIG_EVENTFD + /* Recursion prevention for eventfd_signal() */ + unsigned in_eventfd_signal:1; +#endif unsigned int policy; int nr_cpus_allowed; -- Gitee From 216fe2a9142b84fd16ae58247a737e9845033eb8 Mon Sep 17 00:00:00 2001 From: "yang.yang29@zte.com.cn" Date: Mon, 12 Jun 2023 15:42:36 +0800 Subject: [PATCH 2/7] stop_machine: Remove this_cpu_ptr() from print_stop_info(). commit d3411853ece15811433eee77dd9210e793e5383b upstream. This aligns the patch ("stop_machine: Add function and caller debug info) with commit a8b62fd085050 ("stop_machine: Add function and caller debug info") that was merged upstream and is slightly different. Signed-off-by: Sebastian Andrzej Siewior --- kernel/stop_machine.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c index 725ee6710d25..eae5db7094e0 100644 --- a/kernel/stop_machine.c +++ b/kernel/stop_machine.c @@ -51,7 +51,11 @@ static bool stop_machine_initialized = false; void print_stop_info(const char *log_lvl, struct task_struct *task) { - struct cpu_stopper *stopper = this_cpu_ptr(&cpu_stopper); + /* + * If @task is a stopper task, it cannot migrate and task_cpu() is + * stable. + */ + struct cpu_stopper *stopper = per_cpu_ptr(&cpu_stopper, task_cpu(task)); if (task != stopper->thread) return; -- Gitee From 8d0e6c665b571cb09596dc50b458ffa1875d6daf Mon Sep 17 00:00:00 2001 From: "yang.yang29@zte.com.cn" Date: Mon, 12 Jun 2023 15:42:43 +0800 Subject: [PATCH 3/7] aio: Fix incorrect usage of eventfd_signal_allowed() commit dc0675004348410e888d4c73915a7110313f9d03 upstream. commit 4b3749865374899e115aa8c48681709b086fe6d3 upstream. We should defer eventfd_signal() to the workqueue when eventfd_signal_allowed() return false rather than return true. Fixes: b542e383d8c0 ("eventfd: Make signal recursion protection a task bit") Signed-off-by: Xie Yongji Link: https://lore.kernel.org/r/20210913111928.98-1-xieyongji@bytedance.com Reviewed-by: Eric Biggers Signed-off-by: Eric Biggers Signed-off-by: Luis Claudio R. Goncalves --- fs/aio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/aio.c b/fs/aio.c index f7d47c9ff6de..1a78979663dc 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -1761,7 +1761,7 @@ static int aio_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, list_del_init(&req->wait.entry); list_del(&iocb->ki_list); iocb->ki_res.res = mangle_poll(mask); - if (iocb->ki_eventfd && eventfd_signal_allowed()) { + if (iocb->ki_eventfd && !eventfd_signal_allowed()) { iocb = NULL; INIT_WORK(&req->work, aio_poll_put_work); schedule_work(&req->work); -- Gitee From 5956a79807a1a0361cedda5c38a8c72f75dab939 Mon Sep 17 00:00:00 2001 From: "yang.yang29@zte.com.cn" Date: Mon, 12 Jun 2023 15:42:51 +0800 Subject: [PATCH 4/7] Linux 5.10.111-rt66 REBASE commit 9dfb487760d4b527f107a3baed4d0308540e755d upstream. Signed-off-by: Luis Claudio R. Goncalves --- localversion-rt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/localversion-rt b/localversion-rt index 21988f9ad53f..d42c0971b041 100644 --- a/localversion-rt +++ b/localversion-rt @@ -1 +1 @@ --rt34 +-rt66 -- Gitee From cac96add3050269f775def1012fc23290770e71b Mon Sep 17 00:00:00 2001 From: "yang.yang29@zte.com.cn" Date: Mon, 12 Jun 2023 15:53:54 +0800 Subject: [PATCH 5/7] locking/rtmutex: switch to EXPORT_SYMBOL() for ww_mutex_lock{,_interruptible}() commit 1ec953ef935156419e365dc1cae379151fd9bffb upstream. We can use EXPORT_SYMBOL() instead of EXPORT_SYMBOL_GPL() in ww_mutex_lock_interruptible() and ww_mutex_lock(). That match ww_mutex_unlock() well. And also good for 3rd kernel modules. Link: https://lore.kernel.org/r/20220803062430.1307312-1-yajun.deng@linux.dev Signed-off-by: Yajun Deng Signed-off-by: Luis Claudio R. Goncalves --- kernel/locking/rtmutex.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c index 3356e0354013..5fabcfdfceba 100644 --- a/kernel/locking/rtmutex.c +++ b/kernel/locking/rtmutex.c @@ -2495,7 +2495,7 @@ ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) return ret; } -EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible); +EXPORT_SYMBOL(ww_mutex_lock_interruptible); int __sched ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) @@ -2515,7 +2515,7 @@ ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) return ret; } -EXPORT_SYMBOL_GPL(ww_mutex_lock); +EXPORT_SYMBOL(ww_mutex_lock); void __sched ww_mutex_unlock(struct ww_mutex *lock) { -- Gitee From d5bb2202851c03bb7d9bf922c79f03437590e23f Mon Sep 17 00:00:00 2001 From: "yang.yang29@zte.com.cn" Date: Mon, 12 Jun 2023 16:04:56 +0800 Subject: [PATCH 6/7] ftrace: Fix improper usage of __trace_stack() function. commit 5fe8e3e0f18e9314c71b8f3ed0801ea6cc0e6460 upstream. In kernel/trace/trace_events_trigger.c --> stacktrace_trigger() --> __trace_stack() is not defined as per the function definition. With commit edbaaa13a660 ("tracing: Merge irqflags + preemt counter, add RT bits") the irqflags(flags) and preemption counter(preempt_count()) are now should be evaluated early by tracing_gen_ctx(). This patch replaces the irqflags and preemption counter with tracing_gen_ctx(). Fixes: 5e8446e3820c ("tracing: Dump stacktrace trigger to the corresponding instance") Link: https://lore.kernel.org/r/20220723064943.16532-1-s.anandje1@gmail.com Signed-off-by: Anand Je Saipureddy Reviewed-by: Corey Minyard Signed-off-by: Luis Claudio R. Goncalves --- kernel/trace/trace_events_trigger.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/kernel/trace/trace_events_trigger.c b/kernel/trace/trace_events_trigger.c index fff2225847c4..3c6229f16e81 100644 --- a/kernel/trace/trace_events_trigger.c +++ b/kernel/trace/trace_events_trigger.c @@ -1220,12 +1220,10 @@ stacktrace_trigger(struct event_trigger_data *data, void *rec, struct ring_buffer_event *event) { struct trace_event_file *file = data->private_data; - unsigned long flags; - if (file) { - local_save_flags(flags); - __trace_stack(file->tr, flags, STACK_SKIP); - } else + if (file) + __trace_stack(file->tr, tracing_gen_ctx(), STACK_SKIP); + else trace_dump_stack(STACK_SKIP); } -- Gitee From 60332dad463a7d6a9f76974696840d9b4e642a6b Mon Sep 17 00:00:00 2001 From: "yang.yang29@zte.com.cn" Date: Mon, 12 Jun 2023 16:05:19 +0800 Subject: [PATCH 7/7] Linux 5.10.140-rt73 REBASE commit d47d63626cba1414d5fa74915f843d0e0d009e94 upstream. Signed-off-by: Luis Claudio R. Goncalves --- localversion-rt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/localversion-rt b/localversion-rt index d42c0971b041..e8ada8cdb471 100644 --- a/localversion-rt +++ b/localversion-rt @@ -1 +1 @@ --rt66 +-rt73 -- Gitee