From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796359906; b=hfzjru777ZHF8LAbzCb6/rRhfXr6e5OLaz7YJTrury2Q0c3244TLL1U+TbKhqIBkFCjs Ys1fWIjyt3jNpkKYzgsozHCHXXzfLDr8IdscTduWdDSYyMibH1o6Cvnwh7Jxdx5TXcCLL u36GmJv9Ui4LWX8tuH/dk6QQJk+8VtacyUFyF4ToEgXK5yZYRX6G+NJaVcMkYRAWwsW3D 3UTythKD1DxWCXcGARRIdAEb9TCU5VlMo8MdznMrIC7Pf8dfp1/cVy1ZbgsMP4xwX1tDI i1yJgwQ0vcnOhoMFoF9uU2lKiWlfWeDT4uT8X3Uz5cdfSJ8Ybi3MuXz58TZfgIXW32Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796359906; 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=+5aFPvNfZnOSh4RTpus+dKcy4UqYfPWxq08j2sMEP1M=; b=lBuux2CmqQJF6UEww+JiDaKikrSnNw0HpJkyZuU3gLBniIF3xtdee+lC1OANLPuBXYpY J9D6FwC0p3S5uK/36P4h+fDjFwDKRwSPK130huxbdYtGpbjzsIz/bojCUZyKe44sHki71 UdP4icAGHFd00rgeBu01NOdBNpfoIT214qaff7ASzNcEKxfCuHnsxeZJQaYgtipUbkSj6 OW/KzkzpGIpQt4zGRQoAINvn5CCjBCf7Mt1HEXG9+OOTOv9eIpOAY1VJ1ttwGrBuWvLlH oVgZxeDTWW2rXsabDwxDdy2f1fNdHmcF0x+3BOEkjgwDF0Wf6jfA2B3pSmmRIm+txCQ== 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=HrX8ZxDO; arc=none smtp.remote-ip=74.125.82.71 Received: from mail-dl1-f71.google.com (mail-dl1-f71.google.com [74.125.82.71]) (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 B229D606A684 for ; Fri, 4 Dec 2026 04:51:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B229D606A684 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=HrX8ZxDO Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-1b588e4bd7fso620453c88.0 for ; Thu, 03 Dec 2026 20:51:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796359905; x=1796964705; 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=+5aFPvNfZnOSh4RTpus+dKcy4UqYfPWxq08j2sMEP1M=; b=HrX8ZxDOucksxwT0MJ0W8Yu9cg4sNiYUD94v+DWqlaMtQe7Sl4rxxzuoTVryJ17l/J +LeYd56n7kBwVfTZiTOxnHNDcPTd3EQbQ3F2/bgUdwgNxC0RWeDIBXa87M9Swy5RG03u mOqlWrvoRSVwC6Z8Lit7sUR3S8PS8wD71+oBvjL+LR7ZvcwEt5bweeNeU6WieQe9itz7 64QfOOxNojwohk5i3i3NJnKkR1AZVj+omlwZj37U6NoKddWOH1DLUiVwh4pDC2Dl43Ng 6aIpCteOvEYf/azqmoEkwDeAnPAkk9cvUuNiVp1bGn9XO+uMz9S6PgsKW8vEnnUFVLhD mP6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796359905; x=1796964705; 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=+5aFPvNfZnOSh4RTpus+dKcy4UqYfPWxq08j2sMEP1M=; b=AOgBC7N+uBNExIhXan+R/oZLhBfg/gJVBgJnjx9Lv/EkBwTrt9k/QFSeDp/iS0t5yS /y3O+anZlNRHhRlEyLXFrc21UxZaE1shFkDU6FImFTSSRVhmqJ5dFDGOx8DkuL3kDeW3 EXa8QIsISewz+6P9eV3uYjn/BMgOZNtN38rlzmg884C+Ot5W10rXAs4n+dmBMeBTZLoS GSVbNn7VLHxuH4IpWQqFRJJYbhEOGy+hJ1CAM63wXkVmmgwx/b8vo/hyIBwMG3YQZ4XK QaX4C4MoHsDwHjG12aRpf4aI6TpBCq34J2ydoQ0wzHB1SOdDzMiwZzrudyzq5g23qwJK V4jQ== X-Forwarded-Encrypted: i=1; AL+hErF/dLS9KedPNAOU/4fFP3J74dtnu/guO/Hwc5uKVZEA5KnPjcYgdTk2LIwtxIzlKLkZQVMJiyUwh075lFU=@vger.kernel.org X-Gm-Message-State: APuU4Z9SYmd6K2vE+nsbSpPvrTD+y0Nr4ce+uWMjA7KR0ibrDuTQosZ1 P3vlyYKKLsYzYnELLKr+QbnAgj4uIUwbIQLWPIFdxW3onOljwUzzKIsBcrExgbIJW1ktFiUs5eq 7J1auMA== X-Received: from dleb8-n1.prod.google.com ([2002:a05:701b:4248:10b0:1b2:48fb:f4d0]) (user=chuyee job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:2203:b0:1b3:a89c:c456 with SMTP id a92af1059eb24-1b5d7105494mr932975c88.6.1796359904657; Thu, 03 Dec 2026 20:51:44 -0800 (PST) Date: Fri, 4 Dec 2026 04:51:41 +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: <370addaa39777621da6828eea8e1d0b17bc26b2e.1796357900.git.chuyee@google.com> Subject: [PATCH v2 3/3] livepatch: check task stacks against a sorted range table 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 transition, klp_check_stack() computes, for every task and every patched function, the code range that must not be on the task's stack, and scans the whole stack for it. The ranges are the same for every task, so this repeats the same work T times and costs O(N * D) per task even without the old O(N) klp_find_ops() walk. With thousands of patched functions and thousands of tasks, the first transition pass runs for seconds to minutes under read_lock(&tasklist_lock), which stalls fork() and exit() machine-wide. Build a table of those ranges, sorted by start address, and look up each stack entry with bsearch(), making the per-task cost O(D log N). klp_check_stack() also runs from __klp_sched_try_switch() on any CPU without klp_mutex, so the table is never changed in place. A new table is built under klp_mutex whenever the patched functions or the target state change while a transition is in progress: when the transition starts (including a reversed one), when a module that the transition patch applies to is loaded or unloaded, and when it completes. It's published with RCU and the old one is freed with kvfree_rcu(). Readers already run with preemption disabled. A write barrier orders the update before the TIF_PATCH_PENDING writes, pairing with the read barrier in __klp_sched_try_switch(). If the table can't be allocated, or two ranges overlap, the existing per-function check is used. The table may briefly be stale around module load and unload. A module that is being loaded has not run yet, so no task can be in one of its functions; a module that is going away only leaves extra ranges behind, which makes the check more conservative. Signed-off-by: "Zhu, Yi" --- kernel/livepatch/core.c | 24 ++++ kernel/livepatch/transition.c | 213 +++++++++++++++++++++++++++++++--- kernel/livepatch/transition.h | 1 + 3 files changed, 221 insertions(+), 17 deletions(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 92fbb524165a..1246ad28526e 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -1305,6 +1305,22 @@ static void klp_cleanup_module_patches_limited(struct module *mod, } } +/* Does the transition patch, if any, patch the given module? */ +static bool klp_transition_patch_has_module(struct module *mod) +{ + struct klp_object *obj; + + if (!klp_transition_patch) + return false; + + klp_for_each_object(klp_transition_patch, obj) { + if (klp_is_module(obj) && !strcmp(obj->name, mod->name)) + return true; + } + + return false; +} + int klp_module_coming(struct module *mod) { int ret; @@ -1367,6 +1383,10 @@ int klp_module_coming(struct module *mod) } } + /* The transition patch may now patch functions in this module. */ + if (klp_transition_patch_has_module(mod)) + klp_update_stack_ranges(); + mutex_unlock(&klp_mutex); return 0; @@ -1402,6 +1422,10 @@ void klp_module_going(struct module *mod) klp_cleanup_module_patches_limited(mod, NULL); + /* Drop this module's functions from the transition range table. */ + if (klp_transition_patch_has_module(mod)) + klp_update_stack_ranges(); + mutex_unlock(&klp_mutex); } diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c index 07e786c2d812..6756d5ccdea8 100644 --- a/kernel/livepatch/transition.c +++ b/kernel/livepatch/transition.c @@ -7,7 +7,11 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +#include #include +#include +#include +#include #include #include #include "core.h" @@ -27,6 +31,31 @@ static int klp_target_state = KLP_TRANSITION_IDLE; static unsigned int klp_signals_cnt; +/* + * The code ranges which must not be on a task's stack for the task to switch + * to klp_target_state, one per patched function of klp_transition_patch, + * sorted by start address so that each stack entry needs one bsearch(). + * + * klp_check_stack() reads the table with preemption disabled, both from + * transition passes (under klp_mutex) and from __klp_sched_try_switch() + * (without klp_mutex). So the table is never modified in place: a new one is + * built under klp_mutex whenever the set of patched functions or the target + * state changes, and published with RCU. A NULL table means the slow path. + */ +struct klp_stack_range { + unsigned long start; + unsigned long end; + const char *name; +}; + +struct klp_stack_ranges { + struct rcu_head rcu; + unsigned int nr; + struct klp_stack_range range[] __counted_by(nr); +}; + +static struct klp_stack_ranges __rcu *klp_stack_ranges; + /* * When a livepatch is in progress, enable klp stack checking in * schedule(). This helps CPU-bound kthreads get patched. @@ -145,6 +174,8 @@ static void klp_complete_transition(void) klp_target_state = KLP_TRANSITION_IDLE; klp_transition_patch = NULL; + + klp_update_stack_ranges(); } /* @@ -199,23 +230,23 @@ void klp_update_patch_state(struct task_struct *task) } /* - * Determine whether the given stack trace includes any references to a - * to-be-patched or to-be-unpatched function. + * Get the code range of a to-be-patched or to-be-unpatched function, which a + * task must not be running or sleeping in to switch to klp_target_state. A + * size of zero means there is nothing to check. */ -static int klp_check_stack_func(struct klp_func *func, unsigned long *entries, - unsigned int nr_entries) +static void klp_func_stack_range(struct klp_func *func, + unsigned long *func_addr, + unsigned long *func_size) { - unsigned long func_addr, func_size, address; struct klp_ops *ops; - int i; if (klp_target_state == KLP_TRANSITION_UNPATCHED) { /* * Check for the to-be-unpatched function * (the func itself). */ - func_addr = (unsigned long)READ_ONCE(func->new_func); - func_size = func->new_size; + *func_addr = (unsigned long)READ_ONCE(func->new_func); + *func_size = func->new_size; } else { struct list_head *next; @@ -241,23 +272,118 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries, * previous func or to the func_stack head, so use it rather than * list_is_singular(), which an emptied func_stack would defeat. */ - if (!ops) - return 0; + if (!ops) { + *func_addr = 0; + *func_size = 0; + return; + } next = READ_ONCE(func->stack_node.next); if (next == &ops->func_stack) { /* original function */ - func_addr = (unsigned long)ops->old_func; - func_size = func->old_size; + *func_addr = (unsigned long)ops->old_func; + *func_size = func->old_size; } else { /* previously patched function */ struct klp_func *prev; prev = list_entry(next, struct klp_func, stack_node); - func_addr = (unsigned long)READ_ONCE(prev->new_func); - func_size = prev->new_size; + *func_addr = (unsigned long)READ_ONCE(prev->new_func); + *func_size = prev->new_size; } } +} + +static int klp_stack_range_cmp(const void *a, const void *b) +{ + const struct klp_stack_range *ra = a, *rb = b; + + if (ra->start < rb->start) + return -1; + return ra->start > rb->start; +} + +/* + * Rebuild the sorted range table for the patched functions of + * klp_transition_patch, or clear it if there is no transition. Must be called + * whenever the set of patched functions or klp_target_state changes while a + * transition is in progress. If the table can't be built, it's cleared and + * klp_check_stack() falls back to checking each function. + */ +void klp_update_stack_ranges(void) +{ + struct klp_stack_ranges *new = NULL, *old; + struct klp_object *obj; + struct klp_func *func; + unsigned int nr = 0, i = 0; + + lockdep_assert_held(&klp_mutex); + + if (!klp_transition_patch) + goto publish; + + klp_for_each_object(klp_transition_patch, obj) { + if (!obj->patched) + continue; + klp_for_each_func(obj, func) + nr++; + } + + new = kvmalloc(struct_size(new, range, nr), GFP_KERNEL); + if (!new) + goto publish; + new->nr = nr; + + klp_for_each_object(klp_transition_patch, obj) { + if (!obj->patched) + continue; + klp_for_each_func(obj, func) { + unsigned long func_addr, func_size; + + klp_func_stack_range(func, &func_addr, &func_size); + if (!func_size) + continue; + new->range[i].start = func_addr; + new->range[i].end = func_addr + func_size; + new->range[i].name = func->old_name; + i++; + } + } + new->nr = i; + + sort(new->range, new->nr, sizeof(new->range[0]), klp_stack_range_cmp, + NULL); + + /* klp_stack_range_match() needs disjoint ranges. */ + for (i = 1; i < new->nr; i++) { + if (new->range[i].start < new->range[i - 1].end) { + pr_debug("'%s': overlapping functions '%s' and '%s', not using range table\n", + klp_transition_patch->mod->name, + new->range[i - 1].name, new->range[i].name); + kvfree(new); + new = NULL; + goto publish; + } + } + +publish: + old = rcu_replace_pointer(klp_stack_ranges, new, + lockdep_is_held(&klp_mutex)); + if (old) + kvfree_rcu(old, rcu); +} + +/* + * Determine whether the given stack trace includes any references to a + * to-be-patched or to-be-unpatched function. + */ +static int klp_check_stack_func(struct klp_func *func, unsigned long *entries, + unsigned int nr_entries) +{ + unsigned long func_addr, func_size, address; + int i; + + klp_func_stack_range(func, &func_addr, &func_size); for (i = 0; i < nr_entries; i++) { address = entries[i]; @@ -269,6 +395,37 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries, return 0; } +/* bsearch() comparator: is the address before, inside or after the range? */ +static int klp_stack_range_match(const void *key, const void *elt) +{ + unsigned long address = *(const unsigned long *)key; + const struct klp_stack_range *range = elt; + + if (address < range->start) + return -1; + return address >= range->end; +} + +/* Look up each stack entry in the sorted, disjoint range table. */ +static int klp_check_stack_ranges(const struct klp_stack_ranges *ranges, + const unsigned long *entries, + unsigned int nr_entries, const char **oldname) +{ + const struct klp_stack_range *range; + unsigned int i; + + for (i = 0; i < nr_entries; i++) { + range = bsearch(&entries[i], ranges->range, ranges->nr, + sizeof(ranges->range[0]), klp_stack_range_match); + if (range) { + *oldname = range->name; + return -EADDRINUSE; + } + } + + return 0; +} + /* * Determine whether it's safe to transition the task to the target patch state * by looking for any to-be-patched or to-be-unpatched functions on its stack. @@ -276,6 +433,7 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries, static int klp_check_stack(struct task_struct *task, const char **oldname) { unsigned long *entries = this_cpu_ptr(klp_stack_entries); + const struct klp_stack_ranges *ranges; struct klp_object *obj; struct klp_func *func; int ret, nr_entries; @@ -288,6 +446,13 @@ static int klp_check_stack(struct task_struct *task, const char **oldname) return -EINVAL; nr_entries = ret; + ranges = rcu_dereference_check(klp_stack_ranges, + lockdep_is_held(&klp_mutex) || + rcu_read_lock_sched_held()); + if (ranges) + return klp_check_stack_ranges(ranges, entries, nr_entries, + oldname); + klp_for_each_object(klp_transition_patch, obj) { /* Pairs with smp_store_release() in klp_patch_object(). */ if (!smp_load_acquire(&obj->patched)) @@ -394,9 +559,9 @@ void __klp_sched_try_switch(void) /* * Enforce the order of the TIF_PATCH_PENDING read above and the - * klp_target_state read in klp_try_switch_task(). The corresponding - * write barriers are in klp_init_transition() and - * klp_reverse_transition(). + * klp_target_state and klp_stack_ranges reads in klp_try_switch_task(). + * The corresponding write barriers are in klp_init_transition(), + * klp_reverse_transition() and klp_start_transition(). */ smp_rmb(); @@ -540,6 +705,20 @@ void klp_start_transition(void) klp_transition_patch->mod->name, klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching"); + /* + * 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(); + + /* + * Enforce the order of the table update and the TIF_PATCH_PENDING + * writes below, so that __klp_sched_try_switch() doesn't check a task + * against the table of the reversed transition. + */ + smp_wmb(); + /* * Mark all normal tasks as needing a patch state update. They'll * switch either in klp_try_complete_transition() or as they exit the diff --git a/kernel/livepatch/transition.h b/kernel/livepatch/transition.h index 322db16233de..49f8fd44c9bc 100644 --- a/kernel/livepatch/transition.h +++ b/kernel/livepatch/transition.h @@ -9,6 +9,7 @@ extern struct klp_patch *klp_transition_patch; void klp_init_transition(struct klp_patch *patch, int state); void klp_cancel_transition(void); void klp_start_transition(void); +void klp_update_stack_ranges(void); void klp_try_complete_transition(void); void klp_reverse_transition(void); void klp_force_transition(void); -- 2.56.1.987.g2ec0ab4260-goog