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=1796359905; b=f8XbiD0oOmDAS4+SZ/nPw2ZTCd15WRydIcvyPsG8cQ0DLfNaLebUctHHAab/DE3l5r1q IDoN/fsi5bnyqLGg1fui8kqXSVv1SF9bHo0/XD8sid9O4bW1syC7R/+8GBJQ12SXXnH7A kwX/m9oePFJFv0z4yxh4B6rr96muRatDKwYlmzKzkgRrpBegtkqSGdujN/1jXU4Xx7o47 pDuyE8C7KTzMGHxnPkJ5oK0yL81kpaZg2Uab+umOcuM+tpa+7NAzpkK5Kilc+lRQREAPL BhQqKgKAlXQ+/raGpol6z4TCxNXPJt0NDHb5ZkyqCPdBWMURvidykrF5U3QHz3vs3YA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796359905; 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=qneSmtOgEb1U41H009tfBvh0dJjzfLp8eCKiG/cyVtU=; b=QlMwsMya/Wkw4jDO8+1txkaWLS38wUBLLFOrW2GeXACo8DtKsyyVTltolvBNdlYDIEei 4flMPiLyFouMdEOg7yctu2uF8OTt1VhAb9dIScIbXwF0tDf10EqhgJAqaO30t5IluV5Ii VZDAE+V/s5fd5AKMgO/H2ObWGD9ybUE5ilaW0t6RE7P3Nez6J2bI9mHyE4DuriTCvN0GJ tsA1JsOBmbZ78tAGQsHNyVWFfA56gjAJz6MQQNPPYGARJdGlEDUk6h3Wx1n0H7n/opoYx bCS5BtjbimwssmxXGbK8j1XozePCdsGdSY85JvNjERMyqvOZ45Y1veBaxm3/Jfz6qow== 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=dheJ2Ixa; 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 054BC606A33A for ; Fri, 4 Dec 2026 04:51:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 054BC606A33A 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=dheJ2Ixa Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-386a0f79047so962315eec.1 for ; Thu, 03 Dec 2026 20:51:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796359904; x=1796964704; 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=qneSmtOgEb1U41H009tfBvh0dJjzfLp8eCKiG/cyVtU=; b=dheJ2IxazKOYI/Be1KJ2CPscahWd3KgQsddlXwfbeIfKIEHLVR0n3GzByFls/c9tFQ 5BeeLH5UZ5VevmUmip+iVYFou7QAa9xwMEDyaYnzCzA00YPVlPy3M2MAuIJVhnHin15M sl1EScidZNVnw57TIZ6BCT3HTNBWVsLk3zw6zJemp/GwZCNiE9Z7Q+StFCgHjyDMP7u3 LbB3AhwCyIgACnNHknhwvA6OWnafcZFbtP+yGJtJrR7SwqvIaQhyU14JTx8VyHUuy5A+ 4MJqqC/FZRaJrXxWqI9L73kfBVXls6ElC5lbrIl6ctufy2STR8lrgI/4tzofuvdMCCAX ByAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796359904; x=1796964704; 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=qneSmtOgEb1U41H009tfBvh0dJjzfLp8eCKiG/cyVtU=; b=h69OdlUrZj+yl7OwnWLoGYbwRbUZWxq6d+cyDm0HNPaL1rGO+KCS7SzUNEu9QI+h4e gwThCsqtEBMKqVixZ7YpaRSMPSXVACLiCU/eN+A5YuXnTwLCekX7K/25jGZ3rt0l1abN uczyt3MkdZWWRMck3mQlB/cAIFWGAPKwBh0zuMczlqRTYd2Zl7PoDPKHQF+OxTq6ulM7 PzVYLUJ+kqb5DRx23gghrxJ7UP4EelahYsrXh8LIW3EaK8FyaLdUssZ+/kLXZ5kcZFLD DjJnmBSgftpqj9lpcR1h912tOAWD1+gna9AX12lK55DwxCmPS4Ea+NfPaZBo0OdSAZDN NJ0Q== X-Forwarded-Encrypted: i=1; AL+hErFidT7RMIJV3JegqYp91k1OndTygTb7TX4986XCCWlCZ3BEco+DBikP1EG6MpYmnpLKRjzVU9WPJo0A/aU=@vger.kernel.org X-Gm-Message-State: APuU4Z/Z286SvBDbknVf2D4xobRvF3sR5+yX0NmLJ44l+Z9OlxDGQOSs RRS3tApV5u6QdqEawLwTBotmA+blRPirxTV3iiCD2RsG5NmhiG1+ELynSJ+TualwEqgpM7yazKh 5EpFFsQ== X-Received: from dychz24.prod.google.com ([2002:a05:7301:8618:b0:36e:f67b:23ab]) (user=chuyee job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:831a:b0:383:b90a:3535 with SMTP id 5a478bee46e88-387521b3227mr916763eec.24.1796359903901; Thu, 03 Dec 2026 20:51:43 -0800 (PST) Date: Fri, 4 Dec 2026 04:51:40 +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 2/3] 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, keeping the RCU rules of the list it replaces. Signed-off-by: "Zhu, Yi" --- kernel/livepatch/patch.c | 20 +++++++++++--------- kernel/livepatch/patch.h | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c index 7330a189d6d0..10b32edd4a9c 100644 --- a/kernel/livepatch/patch.c +++ b/kernel/livepatch/patch.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -21,19 +22,20 @@ #include "transition.h" /* - * All klp_ops. 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. + * 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. */ -static LIST_HEAD(klp_ops); +#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; - list_for_each_entry_rcu(ops, &klp_ops, node, - lockdep_is_held(&klp_mutex)) { + 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; } @@ -156,7 +158,7 @@ static void klp_unpatch_func(struct klp_func *func) * klp_find_ops() readers may still see it until a grace * period has elapsed, hence kfree_rcu(). */ - list_del_rcu(&ops->node); + hash_del_rcu(&ops->node); list_del_rcu(&func->stack_node); kfree_rcu(ops, rcu); } else { @@ -224,7 +226,7 @@ static int klp_patch_func(struct klp_func *func) * is fully set up, so that the error path above can free it * right away. */ - list_add_rcu(&ops->node, &klp_ops); + hash_add_rcu(klp_ops_table, &ops->node, (unsigned long)ops->old_func); } else { list_add_rcu(&func->stack_node, &ops->func_stack); } diff --git a/kernel/livepatch/patch.h b/kernel/livepatch/patch.h index bf3b9017910a..67570bbf02c6 100644 --- a/kernel/livepatch/patch.h +++ b/kernel/livepatch/patch.h @@ -15,14 +15,14 @@ * 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; -- 2.56.1.987.g2ec0ab4260-goog