From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796495236; b=OmLsu86oqOqendJ28num81xPx1aCVBEMAQkk9+XYYmUpV4kjoK7h4d+gAYeRbTiHYYU1 0QKC9PZyWgdI54oeC7fBf1lb8cLhez6jzfAsbq5frAbJTkrZXJQEy+3AATxI3pFqvP13b b/MY13zeUd4NixvwpUL5+t2QLo509Zy1mJjd2ndilhFH1M/T4SeT6hMTP2XNCnbnwffaz RZK8hxbXTl+kCEGqsCHbIeaJMlZC9mxKf88bgm/c7M3EH9s5gFdcfWChVygfuOoCSdzrO 2dHpu1XYIQf9ZJOCHFc5DQeAOSl9OxwQFdVkxxjRz88diKjJtcRMbKjvQ0t70BmWn8g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796495236; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=HGoOWBgSlrfBCixYJW+OgtyZgfRVKagmyMY8RaNxSeA=; b=YN7hYcjZKusQ3n1hyZdhCzlcI2FKB/sYkwr1/r4qqe3UdItmY2tpOjPEykx9FHnorURR plGPPF/+ggKkDsx6lXsbAWLlr4MHxlcpj10mSe2pk/uDjCCcoS4XRbylo691aof255O3z sGZty6gxJTySBldNDWen1fN9isMjTsjYVktQwkVmqzyfbUrrc7GFjmt/5tpSh19naKsvE 1loGtH+VEa1XLpZRkAMWV1Vwv3LurV/2PHpBtzfOe5GnEoaHnJW/S8plQH8pQTPV3NhTK YRTdNkQNgFHvDyaFcCSJZuzvpChRM6eoQDjTnLq5X3mLUkWoJzAkm5fv9074G2fuoww== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fREfDaTp; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9AE0A606A33F for ; Sat, 5 Dec 2026 18:27:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fREfDaTp Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2FA841F0092B; Sat, 5 Dec 2026 18:27:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796495236; bh=HGoOWBgSlrfBCixYJW+OgtyZgfRVKagmyMY8RaNxSeA=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=fREfDaTpLMJfUNlJ6uDdAu+gEFu3/MdoPdHliyiKx889K7sQo5Akhhi/dbg03IS6+ P+tNtjlpojJuyJy5/p9RngtG8qj9ClcEZrdJGw9bFYMVKh2ScMGSqET37xBF4U7BfG 8w5h9Wp+PzaqffHtuCZfgdDVp5sbMJT87Mi6V4mKhr9atS3HQPz4GyBKM6oz1PxHb2 7AE5lo/PtDnzB8e+/lMw2AuIjQcFLBXDUnV4E0UD+SdR7V+ldtkzX4kXZ+LShCxbVy H9tZcrjyFVdloq3GTIW8UN300Eynuq1eywzRhnwAbQOL0jLp7DHA3toBawgnjj5Anr MAHcXrt8gyEnA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v5 4/4] livepatch: Wait for exiting tasks around a transition Reply-To: sashiko-reviews@lists.linux.dev To: "Binoy Ravindran" , "Paul E. McKenney" , "Frederic Weisbecker" , "Neeraj Upadhyay" , "Joel Fernandes" , "Josh Triplett" , "Boqun Feng" , "Uladzislau Rezki" , "Steven Rostedt" , "Mathieu Desnoyers" , "Lai Jiangshan" , Zqiang , "Josh Poimboeuf" , "Miroslav Benes" , "Petr Mladek" , "Joe Lawrence" , "Song Liu" , "Ingo Molnar" , "Jiri Kosina" , "Ingo Molnar" , "Peter Zijlstra" , "Juri Lelli" , "Vincent Guittot" , "Dietmar Eggemann" , "Ben Segall" , "Mel Gorman" , "Valentin Schneider" , "K Prateek Nayak" Cc: live-patching@vger.kernel.org, "Yafang Shao" , "Jaidev Shastri" In-Reply-To: <20261205-klp-series-v3-v5-4-3d60c8ea9316@vt.edu> References: <20261205-klp-series-v3-v5-0-3d60c8ea9316@vt.edu> <20261205-klp-series-v3-v5-4-3d60c8ea9316@vt.edu> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 05 Dec 2026 18:27:14 +0000 X-Mailing-List: live-patching@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] livepatch: klp_wait_exiting_tasks() checks per-patch forced flag - [High] livepatch: AB-BA deadlock between klp_mutex and tasks_rcu_exit_srcu - [High] livepatch: unprivileged PID namespace can pin tasks_rcu_exit_srcu indefinitely - [Medium] livepatch: unbacked-off 10 ms exit polling keeps klp_sched_try_switch active -- commit fe2e50e0784ef2725574105892343b959dcc0320 Author: Binoy Ravindran livepatch: Wait for exiting tasks around a transition This commit waits for tasks in the do_exit() SRCU exit window before starting and before completing a livepatch transition, using a killable wait in klp_init_transition() and a polled wait in klp_try_complete_transition(). Link: https://lore.kernel.org/live-patching/CALOAHbA9WHPjeZKUcUkwULagQjTMfq= AdAg+akqPzbZ7Byc=3Dqrw@mail.gmail.com/ > diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c > index 33095e888e10c..fb033483d02dd 100644 > --- a/kernel/livepatch/transition.c > +++ b/kernel/livepatch/transition.c > @@ -426,6 +426,36 @@ static void klp_send_signals(void) > read_unlock(&tasklist_lock); > } > =20 > +/* > + * Tasks in the exit window, between exit_tasks_rcu_start() and > + * exit_tasks_rcu_finish(), are no longer on the task list, so the stack > + * checks above do not see them. They may still run code that the clean= up > + * is about to remove. Once every other task has switched, start an SRCU > + * grace period for the window and poll it. Tasks do not switch back, so > + * the retries only poll, every 10 ms, without walking the tasks again. > + * klp_mutex is not held across the wait, a task stuck in the window del= ays > + * the transition like any other task that cannot switch, and a forced > + * transition does not wait for it. > + * > + * Return true if the transition must wait; it has then been rescheduled. > + */ > +static unsigned long klp_exit_cookie; > +static bool klp_exit_cookie_valid; > + > +static bool klp_wait_exiting_tasks(void) > +{ > + if (klp_transition_patch->forced) > + return false; [Severity: High] Does checking klp_transition_patch->forced work for forced enabling or replace transitions? klp_force_transition() only sets klp_transition_patch->forced when klp_target_state =3D=3D KLP_TRANSITION_UNPATCHED. For KLP_TRANSITION_PATCH= ED transitions it stays false, so forcing an enable or replace transition still loops here every 10 ms forever if a task is stuck in the exit window. Conversely, klp_reverse_transition() leaves klp_transition_patch->forced set. Reversing a replace patch to unpatching, forcing it, and reversing back skips the wait while prior patches remain unforced, letting klp_free_patch_finish() cause a use-after-free on exiting tasks. Using a separate transition-scoped flag set in klp_force_transition() and cleared in klp_init_transition() and klp_reverse_transition() would avoid both problems. > + if (!klp_exit_cookie_valid) { > + klp_exit_cookie =3D start_poll_synchronize_rcu_tasks_exit(); > + klp_exit_cookie_valid =3D true; > + } > + if (poll_state_synchronize_rcu_tasks_exit(klp_exit_cookie)) > + return false; > + schedule_delayed_work(&klp_transition_work, msecs_to_jiffies(10)); > + return true; > +} [Severity: Medium] Should this polling back off instead of firing every 10 ms indefinitely? If a task lingers in the exit window, rescheduling klp_transition_work every unrounded 10 ms wakes a CPU and acquires klp_mutex 100 times per second, preventing tickless idle and deep C-states (unlike the existing round_jiffies_relative(HZ) retry). In addition, klp_resched_disable() is not called until exiting_done, which keeps klp_sched_try_switch_key enabled in __schedule() across all CPUs for the entire wait even though all live and idle tasks have already switched. [ ... ] > @@ -551,12 +606,52 @@ void klp_start_transition(void) > klp_signals_cnt =3D 0; > } > =20 > +/* > + * Wait for the tasks in the exit window to leave it. Return -EINTR if = the > + * current task gets a fatal signal first. > + */ > +static int klp_wait_exiting_tasks_killable(void) > +{ > + unsigned long cookie =3D start_poll_synchronize_rcu_tasks_exit(); > + > + while (!poll_state_synchronize_rcu_tasks_exit(cookie)) { > + if (fatal_signal_pending(current)) > + return -EINTR; > + schedule_timeout_killable(msecs_to_jiffies(10)); > + } > + return 0; > +} [Severity: High] Can an unprivileged user pin tasks_rcu_exit_srcu here indefinitely? With unshare(CLONE_NEWUSER | CLONE_NEWPID), a parent can fork PID 1 and PID 2 into the child namespace, let PID 1 exit, and never reap PID 2. do_exit() calls exit_tasks_rcu_start() before zap_pid_ns_processes(), so PID 1 blocks in TASK_IDLE while holding the tasks_rcu_exit_srcu read lock. Any host livepatch enable or disable then hangs here holding klp_mutex, blocking all module loads and unloads in klp_module_coming() and klp_module_going() until killed, and any active transition stalls indefinitely in klp_wait_exiting_tasks(). [ ... ] > @@ -597,6 +692,28 @@ void klp_init_transition(struct klp_patch *patch, in= t state) > task->patch_state =3D initial_state; > } > =20 > + /* > + * Tasks in the exit window were not visited above, so their patch_state > + * is stale. Once func->transition is set, klp_ftrace_handler() would > + * warn about them, or run the wrong version of a function for them. > + * Wait for them to leave the window. Tasks that enter it later were on > + * the task list above. The wait can be long, or never end if a task is > + * stuck in the window, so the caller can interrupt it with a fatal > + * signal. > + * > + * Tasks that already left the window run with preemption disabled until > + * they finally stop, and may still call functions such as __schedule(). > + * Make sure that they stopped running as well. synchronize_rcu() is n= ot > + * enough: __schedule() reports the RCU quiescent state before > + * switch_to(). > + */ > + if (klp_wait_exiting_tasks_killable()) { > + klp_undo_init_transition(); > + return -EINTR; > + } [Severity: High] Can waiting for tasks_rcu_exit_srcu here under klp_mutex cause an ABBA deadlock? Task A (root ns) Task I (child init) Task B (child ns) ---------------- ------------------- ----------------- lock(&klp_mutex) exit_tasks_rcu_start() zap_pid_ns_processes() SIGKILL -> Task B stack_order_show() lock(&klp_mutex) <-- uninterruptible wait for Task B klp_wait_exiting_tasks_killable() <-- waits for tasks_rcu_exit_srcu *** DEADLOCK *** Task B blocks uninterruptibly in stack_order_show() (world-readable sysfs) or klp_module_coming()/klp_module_going() and ignores Task I's SIGKILL. Task A is outside the child namespace so Task I never signals it; all three tasks deadlock and module loading stalls on klp_mutex until Task A is kille= d. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261205-klp-series= -v3-v5-0-3d60c8ea9316@vt.edu?part=3D4