From patchwork Wed Jun 19 22:49:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13704697 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF64DC27C53 for ; Wed, 19 Jun 2024 22:50:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8C5278D0092; Wed, 19 Jun 2024 18:49:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 731518D0091; Wed, 19 Jun 2024 18:49:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1FCFF6B01F0; Wed, 19 Jun 2024 18:49:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D527C6B01B1 for ; Wed, 19 Jun 2024 18:49:48 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 88023A092C for ; Wed, 19 Jun 2024 22:49:48 +0000 (UTC) X-FDA: 82249132056.13.63DB78E Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf15.hostedemail.com (Postfix) with ESMTP id 3D532A0003 for ; Wed, 19 Jun 2024 22:49:46 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=OctDuuKS; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=j+2cW9Bf; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=njbQ6Amv; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=W6ttRn13; spf=pass (imf15.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718837382; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IKuT+N7+Lb5VqApn1kbaOItiOKwDSbQEIH+cYViP3f4=; b=IvuLaj+efRY1V6UJIo8M/9nFUT/5+3TfZIA0I1LUQR+YQyn0jjwEhcBwnFJIgQbxd95MPf BE+Y6k/cs75lXDU5gl4Fra5bu0s+xIY9i8LVL0Uv4GgEq7T5MD6/Wc5M5EXWn3wJlhdZnR ujinpNR968YevsUi3SQt7dWquJkC62Q= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=OctDuuKS; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=j+2cW9Bf; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=njbQ6Amv; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=W6ttRn13; spf=pass (imf15.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718837382; a=rsa-sha256; cv=none; b=2a8zrLhvw9+U/jule41aCzDtlnX7mlfdAXriIXXxq8P+b3AOUIndGLjEBcQczOCtSutiuz 67/Rhgmw+MtMO3yCQGI+rpP3yliHRRv2tPdiJvjV/3fzggqV1hIHyNCPuA6GrqIlimqVv6 NOcNbyulmvfGLVNN1RhUEK/UkqKrGJE= Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id B6C7121A61; Wed, 19 Jun 2024 22:49:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1718837385; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IKuT+N7+Lb5VqApn1kbaOItiOKwDSbQEIH+cYViP3f4=; b=OctDuuKSC4UF+wjKtoVyH0QvGLpF8XAqLyIbYfZfqUqDqbAwL2bcO3E1Pn+AnZ3eyibQfJ 6tYt02lob+xX6VBjZqZpNYNoAFLn6kK0OzL6sjX7j4tN4Djr5FmBiXSud2Nwi+ztUQq/Zn 90QlsQZrnKA+lNEaCE4uWXQHIrkgSYU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1718837385; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IKuT+N7+Lb5VqApn1kbaOItiOKwDSbQEIH+cYViP3f4=; b=j+2cW9Bfl+vq0dFzwR4Z8VJ/wS4ujClcEsbZnU5mUWOOkqMaN8pzA61v7a4cLcVPI6cou5 oo7W4FlMxtS0HUCw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1718837383; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IKuT+N7+Lb5VqApn1kbaOItiOKwDSbQEIH+cYViP3f4=; b=njbQ6AmvOqf/I3slNL642nz7dBav5v8cX1pr5wFiS4LsJeEcIXUKP4JeIGDMoYpMT1ZHst BoNAr/7ULUHsi0/LTIHKAQvyJs2XwZsVIxHGH1g8+c1W4/i2GGHXiON2JJcEy7S/UNuL8L xJNvP++tshk5rMe3z2U9GcJdCDP19Jw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1718837383; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IKuT+N7+Lb5VqApn1kbaOItiOKwDSbQEIH+cYViP3f4=; b=W6ttRn13nlD24fhIhJolQ8tSxHCj36d2kgYmU/rAvTHIozAnRoU5Z5qBqx+Xe4FxOmedZz 1dd53nZh0GPG6TCQ== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 972F913AAA; Wed, 19 Jun 2024 22:49:43 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id eNSiJIdgc2aFIAAAD6G6ig (envelope-from ); Wed, 19 Jun 2024 22:49:43 +0000 From: Vlastimil Babka Date: Thu, 20 Jun 2024 00:49:00 +0200 Subject: [PATCH v2 6/7] mm, slab: add static key for should_failslab() MIME-Version: 1.0 Message-Id: <20240620-fault-injection-statickeys-v2-6-e23947d3d84b@suse.cz> References: <20240620-fault-injection-statickeys-v2-0-e23947d3d84b@suse.cz> In-Reply-To: <20240620-fault-injection-statickeys-v2-0-e23947d3d84b@suse.cz> To: Akinobu Mita , Christoph Lameter , David Rientjes , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , "Naveen N. Rao" , Anil S Keshavamurthy , "David S. Miller" , Masami Hiramatsu , Steven Rostedt , Mark Rutland Cc: Jiri Olsa , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-kernel@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Vlastimil Babka X-Mailer: b4 0.14.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5370; i=vbabka@suse.cz; h=from:subject:message-id; bh=ldvIP2V3S2F517OMbSSDg7KW3uQS+0XOj9fkVOyN4sA=; b=owEBbQGS/pANAwAIAbvgsHXSRYiaAcsmYgBmc2CAyioCEBExL2f8WncWFmFMWNZmaTFD1Cae+ fsvxwAVOTSJATMEAAEIAB0WIQR7u8hBFZkjSJZITfG74LB10kWImgUCZnNggAAKCRC74LB10kWI moEGB/9t+BV2+IAq6SNJND2pIHnMkyYyiUiRMgkXxQEQLNZbKEmMr1y1PoT7g7IbtFutozCfC6P bWrARbt2jfDNJOj/yOgVfB0zY6h9vQlmoqe8jjatje4Lt3s25DwGW6BgQl8PwkCPUNdQLfBevS/ AYIXYRcHbUOYW/qvyFYFXpUaCDhSmv0MLe1twno9XP42wpIZW1tw1gBuz+4UY284502W7uAXa2g cySWbAiKUS9owip7X8JnR7HvnnWXngktOPjsSzDdpuHPNzMa+FjgMBXyHemCi0nrD5+CCZyLUBo WPwX6uGMzVlf+cD+P/EYDIixpxBcEuaOvo6PBJfQfQxFxkWP X-Developer-Key: i=vbabka@suse.cz; a=openpgp; fpr=A940D434992C2E8E99103D50224FA7E7CC82A664 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 3D532A0003 X-Stat-Signature: 8fguen5hhr5689co4eagiecet5war3xq X-HE-Tag: 1718837386-759694 X-HE-Meta: U2FsdGVkX1/T2KHXBJAA3g8PmZsbelnmaSJIs4vjAfNqDX7CVR0jQkdEXib61qt9d5xKYBcXx+4j7Ay6L/kYc7t75O+EFfkR3WN8A0cafV9SoClfNLCRpU68VLvX3lYvyx3EOLStpwCLK4KIbEKWxisddreFZCviDZGtMRrCXR593enEeA45ptEadD/OfCzPLoE9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V67sY+aqqdfQwFMQHnm7KUjfYAGoracfMeJzp4mjoUMtrYhE3SY+cv2ERQ7hmcUDD5qMvSkAvWaD7wPorYflI5233loE+rFjkvFgJM77IknkSjDPOensMaBJb0LaA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Since commit 4f6923fbb352 ("mm: make should_failslab always available for fault injection") should_failslab() is unconditionally a noinline function. This adds visible overhead to the slab allocation hotpath, even if the function is empty. With CONFIG_FAILSLAB=y there's additional overhead, even when the functionality is not activated by a boot parameter or via debugfs. The overhead can be eliminated with a static key around the callsite. Fault injection and error injection frameworks including bpf can now be told that this function has a static key associated, and are able to enable and disable it accordingly. Additionally, compile out all relevant code if neither CONFIG_FAILSLAB nor CONFIG_FUNCTION_ERROR_INJECTION is enabled. When only the latter is not enabled, make should_failslab() static inline instead of noinline. To demonstrate the reduced overhead of calling an empty should_failslab() function, a kernel build with CONFIG_FUNCTION_ERROR_INJECTION enabled but CONFIG_FAILSLAB disabled, and CPU mitigations enabled, was used in a qemu-kvm (virtme-ng) on AMD Ryzen 7 2700 machine, and execution of a program trying to open() a non-existent file was measured 3 times: for (int i = 0; i < 10000000; i++) { open("non_existent", O_RDONLY); } After this patch, the measured real time was 4.3% smaller. Using perf profiling it was verified that should_failslab was gone from the profile. With CONFIG_FAILSLAB also enabled, the patched kernel performace was unaffected, as expected, while unpatched kernel's performance was worse, resulting in the relative speedup being 10.5%. This means it no longer needs to be an option suitable only for debug kernel builds. Acked-by: Alexei Starovoitov Reviewed-by: Roman Gushchin Signed-off-by: Vlastimil Babka --- include/linux/fault-inject.h | 4 +++- mm/failslab.c | 2 +- mm/slab.h | 3 +++ mm/slub.c | 30 +++++++++++++++++++++++++++--- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/include/linux/fault-inject.h b/include/linux/fault-inject.h index cfe75cc1bac4..0d0fa94dc1c8 100644 --- a/include/linux/fault-inject.h +++ b/include/linux/fault-inject.h @@ -107,9 +107,11 @@ static inline bool __should_fail_alloc_page(gfp_t gfp_mask, unsigned int order) } #endif /* CONFIG_FAIL_PAGE_ALLOC */ +#ifdef CONFIG_FUNCTION_ERROR_INJECTION int should_failslab(struct kmem_cache *s, gfp_t gfpflags); +#endif #ifdef CONFIG_FAILSLAB -extern bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags); +bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags); #else static inline bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags) { diff --git a/mm/failslab.c b/mm/failslab.c index ffc420c0e767..878fd08e5dac 100644 --- a/mm/failslab.c +++ b/mm/failslab.c @@ -9,7 +9,7 @@ static struct { bool ignore_gfp_reclaim; bool cache_filter; } failslab = { - .attr = FAULT_ATTR_INITIALIZER, + .attr = FAULT_ATTR_INITIALIZER_KEY(&should_failslab_active.key), .ignore_gfp_reclaim = true, .cache_filter = false, }; diff --git a/mm/slab.h b/mm/slab.h index 5f8f47c5bee0..792e19cb37b8 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -11,6 +11,7 @@ #include #include #include +#include /* * Internal slab definitions @@ -160,6 +161,8 @@ static_assert(IS_ALIGNED(offsetof(struct slab, freelist), sizeof(freelist_aba_t) */ #define slab_page(s) folio_page(slab_folio(s), 0) +DECLARE_STATIC_KEY_FALSE(should_failslab_active); + /* * If network-based swap is enabled, sl*b must keep track of whether pages * were allocated from pfmemalloc reserves. diff --git a/mm/slub.c b/mm/slub.c index 0809760cf789..11980aa94631 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3874,13 +3874,37 @@ static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s, 0, sizeof(void *)); } -noinline int should_failslab(struct kmem_cache *s, gfp_t gfpflags) +#if defined(CONFIG_FUNCTION_ERROR_INJECTION) || defined(CONFIG_FAILSLAB) +DEFINE_STATIC_KEY_FALSE(should_failslab_active); + +#ifdef CONFIG_FUNCTION_ERROR_INJECTION +noinline +#else +static inline +#endif +int should_failslab(struct kmem_cache *s, gfp_t gfpflags) { if (__should_failslab(s, gfpflags)) return -ENOMEM; return 0; } -ALLOW_ERROR_INJECTION(should_failslab, ERRNO); +ALLOW_ERROR_INJECTION_KEY(should_failslab, ERRNO, &should_failslab_active); + +static __always_inline int should_failslab_wrapped(struct kmem_cache *s, + gfp_t gfp) +{ + if (static_branch_unlikely(&should_failslab_active)) + return should_failslab(s, gfp); + else + return 0; +} +#else +static __always_inline int should_failslab_wrapped(struct kmem_cache *s, + gfp_t gfp) +{ + return false; +} +#endif static __fastpath_inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags) @@ -3889,7 +3913,7 @@ struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags) might_alloc(flags); - if (unlikely(should_failslab(s, flags))) + if (should_failslab_wrapped(s, flags)) return NULL; return s;