From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796344510; b=khp0wP7w9fqi8EBat7QayteieMQuH5Aj9R8Bjh2wRupaMsFc0bKqO/eYR5aag2fUd2D3 G4kB8JBgQhVQFRNnmSTL6+pwGCepJbC3JdQ2uGXsOun4JvFg2YtltdC6G0BjvKbMoYSef JeLSi6tUrBylkUxyrMCA8p1RKZsiwE8bjQ+Si+JVvP8kfIXOJXgqB0M6m6Dt8QGy2220l W38CvzX8OBEvDslT8UbkX2Af1msJWvxdEKYmHouxS4wVmL9A9Tv19gYceeJoxgTaZ/fKm hqqkCP8z0KVtz6m4DmQIjjebVUluMhpludw0xx3E3TABgGWat6Kkg2b+v2tFYFjfeSw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796344510; 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=mev5GdUaO0TUefkbJMR/ImdLJ2bzf5JHWPmLlVkV6KE=; b=QF5V2CNd9F60joeU37bmv6kdO8mVTx3Mfskb+lUvpjo+4vT5qDjeI7bI7vX9Na7mxyGt Od9Nk1PtinLIhbFARTzFBuhStWUtnaiv7K97BLrb5/MbXQDWzNIjKPkeKL5ENhR/jjbe2 aqgSa3dmPEKha71+Dq4A441Ku1/b20E8OCrYhwgTrO+XqSgw4oPD0/PFcjda8PQ6Cptls Gd39eAHntOm2/wdf8rmaHFgD3BnWknhrZcKCqWe+hB5PrWQxOYspwKeRMgJhu9PozR2Qz /P/U6/o4YFrRZ4z01fnygPXJJTKzwL+HHNXvP6kV6wg6jejT9tgJfoDCOcm37rn6f2w== 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=mB1bx6Jj; arc=none smtp.remote-ip=74.125.82.200 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (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 D41AF606A33F for ; Fri, 4 Dec 2026 00:35:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D41AF606A33F 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=mB1bx6Jj Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-341eff9e381so754319eec.1 for ; Thu, 03 Dec 2026 16:35:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796344509; x=1796949309; 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=mev5GdUaO0TUefkbJMR/ImdLJ2bzf5JHWPmLlVkV6KE=; b=mB1bx6JjKMMpnffdFs/9wfaHaDW9MLssR+tEmfrW9kzBr8FIJlPVokCpdHuMqX78d8 96kKBCj0efGZzmZUTcajw5CHPp0W8/XmkpesuMrwd0Y+k7+ziGz1PTQb4v/ZuT/aozjB L1m/x/n/pc5II4+mNMTCPLASEho/n6AS9D8/XDF/yTE/Z3StFg8XYVQPU7a6s3S85Uy+ SAEfdLMUWf2KA2QPE3fpHAwY7kPTUxXbLYSPRyjQKdbxE2DmhcbbcYZBiFTS3KWIBoQ6 tND6lcootitk+q1phLCKhHxTyklhKpfMIsdEc0n28bGVYdOLQNBCn80vLkStQxRbTas+ G6ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796344509; x=1796949309; 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=mev5GdUaO0TUefkbJMR/ImdLJ2bzf5JHWPmLlVkV6KE=; b=hqrcrswrfVQ+3BbJy0ogH4LZxHcIDrPJccAyWbaL7Phj1vbppFCLkihIWyQRdZtC9W jT7XikVb8G5/WI5Wna7cL+BUp/7kHQ2sUcGPy5nW79MVO3XaYfwlHMguiLvJdYQFLDkk VnAWKPKHgeyhaC23JW4Ilw8gzreHDpBZW3UwOJj4+suTpHtv1xEIOp3fQ1X6TePX8FuK sLz2UFb/BQSO9xvErz6uZWbeSm+0uT7ogGgDJBw4RRE2CWyMYGfzjYyCt9vgL6dbyTNM dzPVA+q9JlxuyE998b0RhFiBstCYluH2GopAHopNybrQFpKsZfu6PzT/a5Xrg7HfQix6 VwfQ== X-Forwarded-Encrypted: i=1; AL+hErE8y2Ulag7rYkPvImHU8sg+B1W7UnYQTgYhvxlrqUVxQIqnf0rehQXl/+ROy9mnMM0lD5/08Jyj/FpEhHo=@vger.kernel.org X-Gm-Message-State: APuU4Z99TZYEplmWrbwzOUL6CLoFy5xsxZgYFvDqejq6fRpzpDoGCKU/ WqEqmNKs8VEbp8O0qd3cbgpLqh66BDpneyXjatSK3jF41VO/p/eCpLgpVu4j6KG1fMQi+4IyrH/ BTGNQyg== X-Received: from dyu28.prod.google.com ([2002:a05:693c:811c:b0:386:9d73:d06d]) (user=chuyee job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:6f0b:b0:387:474a:c46c with SMTP id 5a478bee46e88-38752139e7dmr433327eec.12.1796344508717; Thu, 03 Dec 2026 16:35:08 -0800 (PST) Date: Fri, 4 Dec 2026 00:35:06 +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: <3f1027446e0fb57f76fca545840f99f19fa0237c.1796344045.git.chuyee@google.com> Subject: [PATCH 1/2] livepatch: look up klp_ops in a hash 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" klp_find_ops() walks a global list with one entry per patched function. It is called for every patched function when a function is patched or unpatched, and for every patched function of every task in klp_check_stack_func() during a patching transition. That makes the per-task stack check O(N^2) in the number of patched functions. With a synthetic 5,000-function livepatch and ~1,900 tasks, the first transition pass takes 156 seconds and triggers soft lockups. Key the klp_ops on the patched function in a hash table. Store old_func in struct klp_ops so a lookup no longer has to dereference the top of func_stack. The stack check can also run from __klp_sched_try_switch() with only preemption disabled, so use the RCU hash list primitives: publish a klp_ops only after its ftrace ops is registered, unpublish it before tearing it down, and free it with kfree_rcu(). Signed-off-by: "Zhu, Yi" --- kernel/livepatch/patch.c | 38 ++++++++++++++++++++++++++------------ kernel/livepatch/patch.h | 8 ++++++-- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c index 3f54a017bbf6..e93105187bc4 100644 --- a/kernel/livepatch/patch.c +++ b/kernel/livepatch/patch.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -20,17 +21,22 @@ #include "patch.h" #include "transition.h" -static LIST_HEAD(klp_ops); +/* + * All klp_ops, keyed on the patched function. Updates are serialized by + * klp_mutex. Lookups normally hold klp_mutex too, but the stack check can + * also run from __klp_sched_try_switch() with only preemption disabled, so + * entries are added and removed with the RCU list primitives. + */ +#define KLP_OPS_HASH_BITS 10 +static DEFINE_HASHTABLE(klp_ops_table, KLP_OPS_HASH_BITS); struct klp_ops *klp_find_ops(void *old_func) { struct klp_ops *ops; - struct klp_func *func; - list_for_each_entry(ops, &klp_ops, node) { - func = list_first_entry(&ops->func_stack, struct klp_func, - stack_node); - if (func->old_func == old_func) + hash_for_each_possible_rcu(klp_ops_table, ops, node, (unsigned long)old_func, + lockdep_is_held(&klp_mutex)) { + if (ops->old_func == old_func) return ops; } @@ -144,12 +150,17 @@ static void klp_unpatch_func(struct klp_func *func) if (WARN_ON(!ftrace_loc)) return; + /* + * Unpublish ops before tearing it down. Lockless + * klp_find_ops() readers may still see it until a grace + * period has elapsed, hence kfree_rcu(). + */ + hash_del_rcu(&ops->node); WARN_ON(unregister_ftrace_function(&ops->fops)); WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0)); list_del_rcu(&func->stack_node); - list_del(&ops->node); - kfree(ops); + kfree_rcu(ops, rcu); } else { list_del_rcu(&func->stack_node); } @@ -191,8 +202,7 @@ static int klp_patch_func(struct klp_func *func) FTRACE_OPS_FL_IPMODIFY | FTRACE_OPS_FL_PERMANENT; - list_add(&ops->node, &klp_ops); - + ops->old_func = func->old_func; INIT_LIST_HEAD(&ops->func_stack); list_add_rcu(&func->stack_node, &ops->func_stack); @@ -211,7 +221,12 @@ static int klp_patch_func(struct klp_func *func) goto err; } - + /* + * Publish ops for lockless klp_find_ops() readers only once it + * is fully set up, so that the error path above can free it + * right away. + */ + hash_add_rcu(klp_ops_table, &ops->node, (unsigned long)ops->old_func); } else { list_add_rcu(&func->stack_node, &ops->func_stack); } @@ -222,7 +237,6 @@ static int klp_patch_func(struct klp_func *func) err: list_del_rcu(&func->stack_node); - list_del(&ops->node); kfree(ops); return ret; } diff --git a/kernel/livepatch/patch.h b/kernel/livepatch/patch.h index d5f2fbe373e0..67570bbf02c6 100644 --- a/kernel/livepatch/patch.h +++ b/kernel/livepatch/patch.h @@ -15,14 +15,18 @@ * struct's func_stack list. The winner is the klp_func at the top of the * func_stack (front of the list). * - * @node: node for the global klp_ops list + * @node: node in the global klp_ops hash table, keyed on @old_func + * @old_func: the patched function shared by all klp_func's on @func_stack * @func_stack: list head for the stack of klp_func's (active func is on top) * @fops: registered ftrace ops struct + * @rcu: for freeing after lockless klp_find_ops() readers are done */ struct klp_ops { - struct list_head node; + struct hlist_node node; + void *old_func; struct list_head func_stack; struct ftrace_ops fops; + struct rcu_head rcu; }; struct klp_ops *klp_find_ops(void *old_func); -- 2.56.1.987.g2ec0ab4260-goog