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=1796345959; b=0SwrI8tJNBMuYXfdayfy+T5swxDH5WT1S8JM8lN5Wu5xP0wIimAiIWJysxfDdOWL7ueb DPx2RZCyePq0AtHYrg8d4hVK4/c8oVC/uY9hQnT0Nv0P1H57dt0uBhq+W6zcPhp1Izl26 4tmAHALW9htmpH+KvwrbWREGdi2pP9XFB1Hx9AS7kf5+3jTwi1VbFTEcd5j7a+TfeGZ/1 hcKpvKnCCy5XE/JS346ZO7j0H7Kk0Is72ld0rANdwXEYDeToC16F795hdNkSm3B25yfUv iWCDaZQwCaWL0urORovc3R81ILv/5ao4pBUZUOVRyxo2k8wUn4xncNE9DX0drJf1/hg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796345959; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=a5UVgFRrb3y96zMtJsT28+S62Lt7MD3gCNhjDkM9/Ho=; b=aUMDTPvjgAvnqr0bpun4F473zXHD/82DCt1RQ3sDRRqoiV6U7zgT5MkITT7vDf75SLmL wFFSY5bbqY9HKwQD3IPqrdToblwfig30zCPA/VwuTK4+aE+vmhBo2kXEbQ1+hLoSxO+mB CkpoPC2K/8Sxa4zX1CXcxFPqTGj2Zffe5/RE96RVB/JCRDDs9SYqCCeonAVnfFTPgivB7 0vg/OdrtMJJNsfkYPuxKBAFkCxyehH8WtSRky1O+QmEtmL3TvqE62xoNYzyngTe/xKdO9 kq9mHkItRoe1NxhOgPWCSms9EjSrAasjN1lIl1h87iy1frQpibjqz/Zc333PwteKgoQ== 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="GX/7xKr/"; 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 09A45606A686 for ; Fri, 4 Dec 2026 00:59:19 +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=GX/7xKr/ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FECA1F000CE; Fri, 4 Dec 2026 00:59:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796345958; bh=a5UVgFRrb3y96zMtJsT28+S62Lt7MD3gCNhjDkM9/Ho=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=GX/7xKr/Pn69KMxpT5kBJ1BjVUl+MSlMpEGcCcdILjHw02EtasJjDrG7Ni++EKGz9 925VlhIwtgWt28Ofal0P8bNE77Ig6ghBrYgdPRsBEGPzot+fAL15VBzFCm8OPP84zg 3sbsNLoMZqEu/I8PJXtswRvc57/2TOFhhRhTWRFoqprtqzna5VPN1WQmj21qZdjEHi M2b+qqieNV3mqeARWMFxUaFzmr+b6E8Q1bYCBOZzBYqW4sqy2ZTCRNxIJqJIM/Ctfm ahL9MTInZ8XRWmlqz4JLhXWifB3zb178Y2xa7e4Ys4Gmp34XKyV+wHG0zeP2EY6RER eAc+aCpLmirhQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 2/2] livepatch: check task stacks against a sorted range table Reply-To: sashiko-reviews@lists.linux.dev To: =?utf-8?b?Wmh1LCBZaQ==?= , "Josh Poimboeuf" , "Miroslav Benes" , "Petr Mladek" , "Joe Lawrence" , "Song Liu" , "Wilson Felipe Pereira" , "Dylan Hatch" Cc: live-patching@vger.kernel.org In-Reply-To: <4a186e29014b74d0bf480a70cb7e41b543758939.1796344045.git.chuyee@google.com> References: <4a186e29014b74d0bf480a70cb7e41b543758939.1796344045.git.chuyee@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 00:59:18 +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 3 potential issues to consider: - [Medium] livepatch: missing smp_wmb() after klp_update_stack_ranges() - [Medium] livepatch: unnecessary klp_update_stack_ranges() on module events - [Medium] livepatch: PROVE_RCU warning from rcu_dereference_sched() on PREEMPT_RT -- commit 0663b7e5bf136d2c344658ea79f62f99071f953d Author: Zhu, Yi livepatch: check task stacks against a sorted range table This commit builds an RCU-published sorted table of patched function address ranges so klp_check_stack() can look up stack entries with bsearch() instead of scanning every patched function for every task. > diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c > index b83715272e8c3..355ba21705544 100644 > --- a/kernel/livepatch/core.c > +++ b/kernel/livepatch/core.c > @@ -1366,6 +1366,10 @@ int klp_module_coming(struct module *mod) > } > } > =20 > + /* The transition patch may now patch functions in this module. */ > + if (klp_transition_patch) > + klp_update_stack_ranges(); > + > mutex_unlock(&klp_mutex); > =20 > return 0; > @@ -1380,6 +1384,8 @@ int klp_module_coming(struct module *mod) > mod->klp_alive =3D false; > obj->mod =3D NULL; > klp_cleanup_module_patches_limited(mod, patch); > + if (klp_transition_patch) > + klp_update_stack_ranges(); > mutex_unlock(&klp_mutex); > =20 > return ret; > @@ -1401,6 +1407,10 @@ void klp_module_going(struct module *mod) > =20 > klp_cleanup_module_patches_limited(mod, NULL); > =20 > + /* Drop this module's functions from the transition range table. */ > + if (klp_transition_patch) > + klp_update_stack_ranges(); > + > mutex_unlock(&klp_mutex); > } [Severity: Medium] Should klp_update_stack_ranges() only run when klp_transition_patch actually patched or unpatched an object for mod? Right now any unrelated module load or unload during an active transition allocates, rebuilds, and sorts the entire range table under klp_mutex. The call on the err: path in klp_module_coming() is also redundant, since klp_stack_ranges was never updated before the failure and klp_cleanup_module_patches_limited() restores the previous state. If kvmalloc() fails transiently during one of these unnecessary rebuilds, klp_update_stack_ranges() overwrites the valid table with NULL, falling back to the O(N * D) stack check for the rest of the transition. Tracking whether klp_transition_patch touched mod and dropping the err: call avoids both. > diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c > index ef70ff582da18..fc35e09eff692 100644 > --- a/kernel/livepatch/transition.c > +++ b/kernel/livepatch/transition.c [ ... ] > @@ -266,6 +420,11 @@ static int klp_check_stack(struct task_struct *task,= const char **oldname) > return -EINVAL; > nr_entries =3D ret; > =20 > + ranges =3D rcu_dereference_sched(klp_stack_ranges); > + if (ranges) > + return klp_check_stack_ranges(ranges, entries, nr_entries, > + oldname); > + > klp_for_each_object(klp_transition_patch, obj) { [Severity: Medium] Does rcu_dereference_sched(klp_stack_ranges) trigger a CONFIG_PROVE_RCU warning on CONFIG_PREEMPT_RT? When klp_try_complete_transition() checks task =3D=3D current, it holds klp_mutex and read_lock(&tasklist_lock) and calls klp_check_stack() directly without disabling preemption. On PREEMPT_RT, rt_read_lock() takes rcu_read_lock() and migrate_disable() while leaving preemption enabled, so rcu_read_lock_sched_held() returns false and fires a suspicious RCU usage splat. Using rcu_dereference_sched_check(klp_stack_ranges, lockdep_is_held(&klp_mutex)), as klp_find_ops() does, avoids the warning. [ ... ] > @@ -517,6 +676,13 @@ void klp_start_transition(void) > klp_transition_patch->mod->name, > klp_target_state =3D=3D KLP_TRANSITION_PATCHED ? "patching" : "unpat= ching"); > =20 > + /* > + * Build the range table for klp_check_stack() before any task can be > + * marked TIF_PATCH_PENDING and check its stack. This also covers a > + * reversed transition, whose target state just changed. > + */ > + klp_update_stack_ranges(); > + > /* > * Mark all normal tasks as needing a patch state update. They'll > * switch either in klp_try_complete_transition() or as they exit the [Severity: Medium] Is an smp_wmb() missing between klp_update_stack_ranges() and setting TIF_PATCH_PENDING? During klp_reverse_transition(), klp_sched_try_switch_key is already enabled, and its existing smp_wmb() runs before klp_start_transition(). Inside klp_update_stack_ranges(), rcu_replace_pointer() only uses smp_store_release(), which does not order the klp_stack_ranges store before the subsequent unordered set_tsk_thread_flag(..., TIF_PATCH_PENDING) writes. CPU0 CPU1 klp_reverse_transition() klp_target_state =3D ... smp_wmb() klp_start_transition() klp_update_stack_ranges() rcu_replace_pointer(...) set_tsk_thread_flag(...) __klp_sched_try_switch() klp_patch_pending(current) -> true smp_rmb() klp_try_switch_task(current) klp_check_stack() rcu_dereference_sched(...) <-- sees stale range table On a weakly-ordered architecture such as powerpc or arm64, CPU1 can observe TIF_PATCH_PENDING and the flipped klp_target_state with the pre-reversal range table. When reversing to unpatch, that checks old_func instead of new_func, allowing a task to switch while a patched function is still on its stack and potentially return into freed livepatch module text. Adding smp_wmb() after klp_update_stack_ranges() pairs with the smp_rmb() in __klp_sched_try_switch(). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/cover.1796344045.gi= t.chuyee@google.com?part=3D2