From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796359903; b=lvXWoRKfv4lf8JAOuT4UnPpThGj4jowRjVtrEAq4Gwe+PC1w4awqJEd+TsC8+Oc/aIxB aBk51yq1EKS+1/fyVRVsKHhsw+Q6e+dMQlyjysWx08PRwLdKN4CFYDyPwCh+xFKPVzG5n ESSb6lZ9Ter6ATqE29MeyzV8wfDhvoFSYxuebyxo6q5Gratqx9YWPzm4jxAneCYz46vZv Ot7L8Rrce+0Gma924vTai09vS4OYADYR6FBrncA2wwBRgAjf92GDGvHrikUNHw/r1Msfq rSILudSFw7k3MlJpDiKK7/uPVbxWsYnLZvSkDWd3xdUCSS4cZ+siYZQVXM8sho5iEfg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796359903; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=ljTO/bNrSyBjmAnORIkLUMYRFLNtvB7Lyg3KYpuNWug=; b=dIT4M2FhBYY5j0WZ1h0gNknrdQym6eG75j+L7BY+vzri1FT66C5Eo+bB8HRD3Kl0bn9y z8K7x+hrz8oxDaDKz5hempfbSuFSak7ruJgNHnmErR/+yzbSXWB6D8gpDjuJ+1XNNLY+J lNIN5WTlNRGbeDkR5VQaRRX5bBw4a8DCplmPArjqpo6RmN21DqCpeFcKSwMJif5G1PK2C CkPUohMQ/g/uAX132AKO3UAaocVOh1GP8/81gchBjav58XPUMv8EJLVoajEfngviSjl82 DjPb9eCKUwexORr6iufrhui2msST4kQbrQ+9vdsDSNavUV3GGz0X5+Lsjcx8KBkaFBA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--chuyee.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Lx8kCSEr; arc=none smtp.remote-ip=74.125.82.70 Received: from mail-dl1-f70.google.com (mail-dl1-f70.google.com [74.125.82.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 881FD606A330 for ; Fri, 4 Dec 2026 04:51:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 881FD606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--chuyee.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Lx8kCSEr Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-1b5d5f81059so478000c88.1 for ; Thu, 03 Dec 2026 20:51:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796359903; x=1796964703; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ljTO/bNrSyBjmAnORIkLUMYRFLNtvB7Lyg3KYpuNWug=; b=Lx8kCSErEOlI87uXTYASrQF8GoaVr/4YnkY+wGzS+iQJfB9jjaZJtlSViYrM0+6tE+ Vo+6uonVrpIR98bFNQ58fYtdOcixjCsJARKAQZP0Dz8d0ql+7JRs1SXLEDxtpxbHObvq SAiFftV8UOkdVLnygK1sAzz94t6mxz8DpgYnwmqDjmDU7MhY239R+qAiF9UBNfi3Hkif Kt3pzv5xajwZiLzt0r+ci6AxWQD+kHq77J/SSC527MUN8lUVpufrng5fr/+kPKU5X+W7 CPjth8a+/xkjH/4zLTTh/mo6jHuthX7cRDjH/qJVdlXt2VnC1Di3J0U4RIbY+1EoVmyf UFvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796359903; x=1796964703; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ljTO/bNrSyBjmAnORIkLUMYRFLNtvB7Lyg3KYpuNWug=; b=xHDKhPqea/6qhlV2Fk6RxH+ik8m+BXUha7VZK760IiY7HSDMPxw4P9PDCWZQp2CgBS OEzjmMo4o0PKqv0EnZPIkd7XC5c/+6XpiTIT79q7C1fN1n2EhomAA7uq3NV7+sb2TNPb gU1auydb6SSyL/NRbxkx7a5Bdclu2NZCRIAMOrstABamvP4TkcBcGQHGYWcND8pknQrz LsqNwIbZfLqJxdeut2ZdA/dxUuyskqaytzYxcRmhScA/CBrkrz0tc9dwrzYqW33B03BI zXrTimgRcrb3nDUle9DRI0uKF/RrG2FDN119NVB9M4eeewwYKpMt6aqXsxtNCSCNCyAI KITQ== X-Forwarded-Encrypted: i=1; AL+hErHOQIg70EXf4kRk3zgLAHTJfiLRjMpPzxVFK+HxlJFHRlpNxtCWpwrm9kDTb68YEhZIEyL5bA15YG7zLBg=@vger.kernel.org X-Gm-Message-State: APuU4Z+Bk1lMXLbKAXiEehtzUjfU3UXAK6/2WuoufG3VpspABX2MEjO/ Cl3ANUq/3j/sSEN9Gw18kQ1+kl/iGVE+5Os9muPPcekuZpMHdf9yFs5UnUduxPoHV1zvPkNHs4Y yacmbxg== X-Received: from dlfz4-n2.prod.google.com ([2002:a05:701a:8444:20b0:1b5:aa96:54c7]) (user=chuyee job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:150a:b0:1b0:430d:d81c with SMTP id a92af1059eb24-1b5d7139cc3mr1141519c88.25.1796359902405; Thu, 03 Dec 2026 20:51:42 -0800 (PST) Date: Fri, 4 Dec 2026 04:51:38 +0000 In-Reply-To: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.1.987.g2ec0ab4260-goog Message-ID: Subject: [PATCH v2 0/3] livepatch: make the transition stack check scale with patch size From: "Zhu, Yi" To: Josh Poimboeuf , Miroslav Benes , Petr Mladek , Joe Lawrence , Song Liu , Wilson Felipe Pereira , Dylan Hatch Cc: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" During a patching transition, every task's stack is checked against every patched function. Two things make that expensive for large cumulative livepatches: - klp_check_stack_func() calls klp_find_ops(), a walk of a global list with one entry per patched function, for every patched function. The per-task check is therefore O(N^2) in the number of patched functions N. - Even without that lookup, each task repeats the same O(N * D) scan (D = stack depth), although the forbidden address ranges are the same for all tasks. The first transition pass runs under read_lock(&tasklist_lock), so on large patches this stalls fork() and exit() machine-wide for seconds to minutes. Patch 1 fixes an existing race: since klp_check_stack() can run from __klp_sched_try_switch() without klp_mutex, its klp_ops lookup can race with klp_module_coming() and klp_module_going() patching and unpatching an object of the transition patch, and dereference a NULL or freed klp_ops or an emptied func_stack. It makes klp_ops RCU-managed and the lookup tolerate a missing klp_ops. Patches 2 and 3 build on it. Patch 2 keys klp_ops on the patched function in an RCU hash table. Patch 3 builds a sorted, RCU-published table of the patched address ranges once per transition state change, and looks up each stack entry with bsearch(). The per-task cost drops to O(D log N). If the table can't be built, the existing per-function check is used. Testing, on livepatching/for-next (2c3787447317) with and without the series, in a 32-vCPU QEMU/KVM guest (Intel Xeon @ 2.60GHz host, CONFIG_PREEMPT_LAZY): - livepatch kselftests, with lockdep, PROVE_RCU and DEBUG_LIST: 39 passed, 4 skipped (legacy replace attribute not present), 0 failed on both kernels; no lockdep, RCU or WARN splats. - With the same debug options, 300 load/unload cycles of a target module while a transition is held pending by test_klp_callbacks_busy, with 32 extra tasks scheduling continuously: no splats. - A synthetic target module with N noinline functions and a livepatch replacing all of them; idle threads added to reach the task counts below (v1 numbers; a v2 rerun at 1,000, 5,000 and 10,000 functions matched them). The first klp_try_complete_transition() pass, timed with a kprobe, and the time from insmod until the transition completes: first pass insmod -> patched N tasks base patched base patched 100 ~1.9k 19.6 ms 1.7 ms 1.9 s 1.9 s 1,000 ~1.9k 3.41 s 1.9 ms 4.9 s 2.9 s 2,000 ~1.9k 14.5 s 1.7 ms 17.9 s 4.8 s 3,000 ~1.9k 30.7 s 2.2 ms 36.1 s 6.9 s 5,000 ~1.9k 156 s 1.8 ms 167 s 11.8 s 10,000 ~1.9k 1,069 s 1.8 ms 1,096 s 27.2 s 2,000 ~3.4k 25.9 s 3.0 ms 29.3 s 5.0 s 2,000 ~50.4k - 35.5 ms - 5.5 s 10,000 ~50.4k - 37.5 ms - 27.8 s On base, the first pass triggers soft lockups from 3,000 functions on; with ~3.4k tasks, udev-worker tasks on other CPUs report soft lockups too. The patched kernel shows no soft lockups or RCU stalls in any run. Its remaining insmod time is module loading, symbol resolution and ftrace registration, which this series does not change. Disabling a large patch has a separate cost: two synchronize_rcu() calls per function in the ftrace unregister path. That will be addressed separately. Changes in v2: - New patch 1, fixing the klp_ops lookup race above. It predates this series, but v1 patch 1 widened the window by unpublishing a klp_ops before its sleeping ftrace unregistration (Sashiko). - Patch 2: keep the unregister-then-unpublish order; the RCU conversion and old_func now come from patch 1. - Patch 3: add smp_wmb() between the range table update and the TIF_PATCH_PENDING writes, for reversed transitions (Sashiko). - Patch 3: rebuild the table on module load/unload only if the transition patch applies to the module, and not on the klp_module_coming() error path, where it never changed (Sashiko). - Patch 3: use rcu_dereference_check() with klp_mutex or a sched RCU read-side section as the condition (Sashiko). - Patch 1: set obj->patched with smp_store_release() and read it with smp_load_acquire() in the stack check (Sashiko). - Patch 1: mark the lockless func->old_func/new_func accesses against klp_free_object_loaded() with READ_ONCE()/WRITE_ONCE(), and use the immutable ops->old_func after the lookup. v1: https://lore.kernel.org/all/cover.1796344045.git.chuyee@google.com/ Zhu, Yi (3): livepatch: fix racy klp_ops lookup in klp_check_stack_func() livepatch: look up klp_ops in a hash table livepatch: check task stacks against a sorted range table kernel/livepatch/core.c | 29 +++- kernel/livepatch/patch.c | 47 +++++-- kernel/livepatch/patch.h | 8 +- kernel/livepatch/transition.c | 240 +++++++++++++++++++++++++++++++--- kernel/livepatch/transition.h | 1 + 5 files changed, 288 insertions(+), 37 deletions(-) base-commit: 2c37874473176739f4107c70739a930501d619c8 -- 2.56.1.987.g2ec0ab4260-goog