From patchwork Fri May 31 09:33:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13681428 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 6CB74C27C4F for ; Fri, 31 May 2024 09:33:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A3866B0098; Fri, 31 May 2024 05:33:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 262B06B009C; Fri, 31 May 2024 05:33:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE83B6B0095; Fri, 31 May 2024 05:33:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id A41856B00A0 for ; Fri, 31 May 2024 05:33:44 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4B39314018A for ; Fri, 31 May 2024 09:33:44 +0000 (UTC) X-FDA: 82178178768.29.6502DE9 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf01.hostedemail.com (Postfix) with ESMTP id 0156F4001D for ; Fri, 31 May 2024 09:33:41 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=NLOye7sb; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=egjcXQtp; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=NLOye7sb; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=egjcXQtp; spf=pass (imf01.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=1717148022; 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=t8Mu1INP306CXVJLbtkEYc46ROCJCSbEjjUhioLXt6I=; b=fg/mcdmUrzjwikVzD4czr23joUeEzH9QeIsF9YIZgS0YiA6XIneaE8MhGLKpOK3LGmhcEk plnB9fw6sCTsZPTeIqMfareb0fEhDUbCSYb94DL53l1MlBHWktp77w3SLCGsRVIlY6vMZp f1aO1E/IBhECdZTilILW1K88jgOfeXQ= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=NLOye7sb; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=egjcXQtp; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=NLOye7sb; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=egjcXQtp; spf=pass (imf01.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=1717148022; a=rsa-sha256; cv=none; b=WDZ2NX21JqPyF+jIgniC6A1N1doutynwI6/9Iv2Q5I6GpJwGhGwbGR0Yt5g80ZD/J/CZdS DQgeSpLIY+nf3bYXj1jS9A6dfOR3GvUUoXXBunP5f/d+zzvGWgfiD6bD/y45FgdEftpanR zSehqxFLa4qXxNdi0qKg8gT6VvTnKFg= 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 306CE21B29; Fri, 31 May 2024 09:33:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1717148020; 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=t8Mu1INP306CXVJLbtkEYc46ROCJCSbEjjUhioLXt6I=; b=NLOye7sbpB3u1184zrlaGLIfhjypv/L8wYQ7PttPtS19iOcn1heYNY/JhSZeqtmHmbGxHu OUeoUpvWM/WYj4FpaEZxsX90CaKbPaXZNHqaRpvzqIkQkwAcdTgH89ltA29ELTOr3hXNSA lCa6qtULc7LPWZeIOKoGPqhNDLgZNC8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1717148020; 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=t8Mu1INP306CXVJLbtkEYc46ROCJCSbEjjUhioLXt6I=; b=egjcXQtpYNPE0TganvgfvmKJWiJQ/5fLjMR5R9KMtaSX1BeJ1Ih1v57ryPctvr1Dv1VIM8 kBCA6IwOGoLvXhAw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1717148020; 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=t8Mu1INP306CXVJLbtkEYc46ROCJCSbEjjUhioLXt6I=; b=NLOye7sbpB3u1184zrlaGLIfhjypv/L8wYQ7PttPtS19iOcn1heYNY/JhSZeqtmHmbGxHu OUeoUpvWM/WYj4FpaEZxsX90CaKbPaXZNHqaRpvzqIkQkwAcdTgH89ltA29ELTOr3hXNSA lCa6qtULc7LPWZeIOKoGPqhNDLgZNC8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1717148020; 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=t8Mu1INP306CXVJLbtkEYc46ROCJCSbEjjUhioLXt6I=; b=egjcXQtpYNPE0TganvgfvmKJWiJQ/5fLjMR5R9KMtaSX1BeJ1Ih1v57ryPctvr1Dv1VIM8 kBCA6IwOGoLvXhAw== 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 0B90913A90; Fri, 31 May 2024 09:33:40 +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 YGCIAnSZWWZKHQAAD6G6ig (envelope-from ); Fri, 31 May 2024 09:33:40 +0000 From: Vlastimil Babka Date: Fri, 31 May 2024 11:33:33 +0200 Subject: [PATCH RFC 2/4] error-injection: support static keys around injectable functions MIME-Version: 1.0 Message-Id: <20240531-fault-injection-statickeys-v1-2-a513fd0a9614@suse.cz> References: <20240531-fault-injection-statickeys-v1-0-a513fd0a9614@suse.cz> In-Reply-To: <20240531-fault-injection-statickeys-v1-0-a513fd0a9614@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.13.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 0156F4001D X-Stat-Signature: e1bj78r85kbaa34gttswstgr5sqymip3 X-Rspam-User: X-HE-Tag: 1717148021-625925 X-HE-Meta: U2FsdGVkX18iNVOe3eRvh712U4S/A0fwgxvWx/qe0x5KIQxAm2mNS5uA7q+QmYi2CUfrcSmVkUGrVX4FR7RktP3nkmlJ6lvl4zZdVuKPJ19O7TT6MUy48ImKNYWbRiPzGptKmm8tF05GYLWLQB52l9ATvsUL3fiS9TvI3kDvRRKyboKz426IC1ZI+PjnZfd2o+eMyiH1eHuBBsrJxxN3MBXMlBvdrPoMXXz1P0bUqMntDnSTjtPswPFZ45ylSN7gKlXLuAjpzP6agDxTbLjKQdvSShlMhE6HniLcSjYXyMUkR0wL86bYeRkdn+wCFnyeWub6Lowq/RIURCQ0CslpnqgIbork+7yiNQu3bp1aeNojPqQEo2S04ZyULaqC8hfyWSzEMFJSlZvD/AwOGWvOtqUte1wT7g7AMI+8feVQOBRxNas+2dRqhX5RafwwyvEE2DSisWZdu5UihKxh+UGe56OQqw2cbskUlrkoiegYCHLAnam70hZzpt6nBSESFnMjC/2GJY//XbnlJftPTc9tAzST0sIr7HE6euU1ReemkogKthYCkz9iadOC9YDILoYYwiasIB+R/4WtYm9JcalEOVmepBUjLsP8S1mndQdTeWiUE7ndyfFQCtYTKk8fIdImIREd4d+Jti481ALeMflgfqgoENyLA30EaYapXyh2qP4hr6voKxg/VazfW6TLL5l/tnBXELCBWXXiirHaipadpFZ3ME2N+xZLowiMlfR5n+jxLNOPyF1X2ZNfmPHJkgTcBTNTXrf8YbjaXbIvrbDnCnazPbZISlX9aWYg8hRjb/S6y8w3MthoyyCU5Hi4XaQEUyeeCDxRhxp6acUG0Rhag1BIosJRmuANSHjYGvn1JTZp6jJkZjWHN5hx5/DfiAyLgGQml90RtwNHdbOX9aElSKgNpAEvtI6w6fAVE9hLIk2A7mJyBY+pfAq2VsjYzH2G0FopshqFq3CqbwjI2BD cxHBn/M7 gkfiDSa9i1lIDsNQNuDHpBqnNvFhDTWoP687nUPkj+DBLZkXvAHEVXRlCF1u4in4zUqaF0ntOQ/NYIJopKfLXURTYM4ALZ7HUWWT2xwuhJ8tNDji7euRlDg8AcPpc9UMQO5EP+r7uVqLaMSxT14hPd7uHqKSBiX9gQp3HpDqkaSU0qfO8S0QsiaEE6zeM7NREi9J/26iW+SovPSNiJ8arZtx7QjXvZLiBqvEvpAbq7hS2y9NCBCsTwAm2Is4PjZfB7xdhZxKKa6xsRg8VdxB1WrbCtQta+89GSSaSlNXEl6tTXAo= 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: Error injectable functions cannot be inlined and since some are called from hot paths, this incurrs overhead even if no error injection is enabled for them. To remove this overhead when disabled, allow the callsites of error injectable functions to put the calls behind a static key, which the framework can control when error injection is enabled or disabled for the function. Introduce a new ALLOW_ERROR_INJECTION_KEY() macro that adds a parameter with the static key's address, and store it in struct error_injection_entry. This new field has caused a mismatch when populating the injection list from the _error_injection_whitelist section with the current STRUCT_ALIGN(), so change the alignment to 8. During the population, copy the key's address also to struct ei_entry, and make it possible to retrieve it along with the error type by get_injectable_error_type(). Finally, make the processing of writes to the debugfs inject file enable the static key when the function is added to the injection list, and disable when removed. Signed-off-by: Vlastimil Babka --- include/asm-generic/error-injection.h | 13 ++++++++++++- include/asm-generic/vmlinux.lds.h | 2 +- include/linux/error-injection.h | 9 ++++++--- kernel/fail_function.c | 22 +++++++++++++++++++--- lib/error-inject.c | 6 +++++- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/include/asm-generic/error-injection.h b/include/asm-generic/error-injection.h index b05253f68eaa..eed2731f3820 100644 --- a/include/asm-generic/error-injection.h +++ b/include/asm-generic/error-injection.h @@ -12,6 +12,7 @@ enum { struct error_injection_entry { unsigned long addr; + unsigned long static_key_addr; int etype; }; @@ -25,16 +26,26 @@ struct pt_regs; * 'Error Injectable Functions' section. */ #define ALLOW_ERROR_INJECTION(fname, _etype) \ -static struct error_injection_entry __used \ +static struct error_injection_entry __used __aligned(8) \ __section("_error_injection_whitelist") \ _eil_addr_##fname = { \ .addr = (unsigned long)fname, \ .etype = EI_ETYPE_##_etype, \ } +#define ALLOW_ERROR_INJECTION_KEY(fname, _etype, key) \ +static struct error_injection_entry __used __aligned(8) \ + __section("_error_injection_whitelist") \ + _eil_addr_##fname = { \ + .addr = (unsigned long)fname, \ + .static_key_addr = (unsigned long)key, \ + .etype = EI_ETYPE_##_etype, \ + } + void override_function_with_return(struct pt_regs *regs); #else #define ALLOW_ERROR_INJECTION(fname, _etype) +#define ALLOW_ERROR_INJECTION_KEY(fname, _etype, key) static inline void override_function_with_return(struct pt_regs *regs) { } #endif diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index 5703526d6ebf..1b15a0af2a00 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -248,7 +248,7 @@ #ifdef CONFIG_FUNCTION_ERROR_INJECTION #define ERROR_INJECT_WHITELIST() \ - STRUCT_ALIGN(); \ + . = ALIGN(8); \ BOUNDED_SECTION(_error_injection_whitelist) #else #define ERROR_INJECT_WHITELIST() diff --git a/include/linux/error-injection.h b/include/linux/error-injection.h index 20e738f4eae8..bec81b57a9d5 100644 --- a/include/linux/error-injection.h +++ b/include/linux/error-injection.h @@ -6,10 +6,12 @@ #include #include +struct static_key; + #ifdef CONFIG_FUNCTION_ERROR_INJECTION -extern bool within_error_injection_list(unsigned long addr); -extern int get_injectable_error_type(unsigned long addr); +bool within_error_injection_list(unsigned long addr); +int get_injectable_error_type(unsigned long addr, struct static_key **key_addr); #else /* !CONFIG_FUNCTION_ERROR_INJECTION */ @@ -18,7 +20,8 @@ static inline bool within_error_injection_list(unsigned long addr) return false; } -static inline int get_injectable_error_type(unsigned long addr) +static inline int get_injectable_error_type(unsigned long addr, + struct static_key **key_addr) { return -EOPNOTSUPP; } diff --git a/kernel/fail_function.c b/kernel/fail_function.c index d971a0189319..9240eb137e00 100644 --- a/kernel/fail_function.c +++ b/kernel/fail_function.c @@ -27,15 +27,16 @@ struct fei_attr { struct list_head list; struct kprobe kp; unsigned long retval; + struct static_key *key; }; static DEFINE_MUTEX(fei_lock); static LIST_HEAD(fei_attr_list); static DECLARE_FAULT_ATTR(fei_fault_attr); static struct dentry *fei_debugfs_dir; -static unsigned long adjust_error_retval(unsigned long addr, unsigned long retv) +static unsigned long __adjust_error_retval(int type, unsigned long retv) { - switch (get_injectable_error_type(addr)) { + switch (type) { case EI_ETYPE_NULL: return 0; case EI_ETYPE_ERRNO: @@ -53,9 +54,17 @@ static unsigned long adjust_error_retval(unsigned long addr, unsigned long retv) return retv; } +static unsigned long adjust_error_retval(unsigned long addr, unsigned long retv) +{ + int type = get_injectable_error_type(addr, NULL); + + return __adjust_error_retval(type, retv); +} + static struct fei_attr *fei_attr_new(const char *sym, unsigned long addr) { struct fei_attr *attr; + int type; attr = kzalloc(sizeof(*attr), GFP_KERNEL); if (attr) { @@ -66,7 +75,10 @@ static struct fei_attr *fei_attr_new(const char *sym, unsigned long addr) } attr->kp.pre_handler = fei_kprobe_handler; attr->kp.post_handler = fei_post_handler; - attr->retval = adjust_error_retval(addr, 0); + + type = get_injectable_error_type(addr, &attr->key); + attr->retval = __adjust_error_retval(type, 0); + INIT_LIST_HEAD(&attr->list); } return attr; @@ -218,6 +230,8 @@ static int fei_open(struct inode *inode, struct file *file) static void fei_attr_remove(struct fei_attr *attr) { + if (attr->key) + static_key_slow_dec(attr->key); fei_debugfs_remove_attr(attr); unregister_kprobe(&attr->kp); list_del(&attr->list); @@ -295,6 +309,8 @@ static ssize_t fei_write(struct file *file, const char __user *buffer, fei_attr_free(attr); goto out; } + if (attr->key) + static_key_slow_inc(attr->key); fei_debugfs_add_attr(attr); list_add_tail(&attr->list, &fei_attr_list); ret = count; diff --git a/lib/error-inject.c b/lib/error-inject.c index 887acd9a6ea6..e5f3b63f0dbb 100644 --- a/lib/error-inject.c +++ b/lib/error-inject.c @@ -17,6 +17,7 @@ struct ei_entry { struct list_head list; unsigned long start_addr; unsigned long end_addr; + struct static_key *key; int etype; void *priv; }; @@ -37,7 +38,7 @@ bool within_error_injection_list(unsigned long addr) return ret; } -int get_injectable_error_type(unsigned long addr) +int get_injectable_error_type(unsigned long addr, struct static_key **key_addr) { struct ei_entry *ent; int ei_type = -EINVAL; @@ -46,6 +47,8 @@ int get_injectable_error_type(unsigned long addr) list_for_each_entry(ent, &error_injection_list, list) { if (addr >= ent->start_addr && addr < ent->end_addr) { ei_type = ent->etype; + if (key_addr) + *key_addr = ent->key; break; } } @@ -86,6 +89,7 @@ static void populate_error_injection_list(struct error_injection_entry *start, ent->start_addr = entry; ent->end_addr = entry + size; ent->etype = iter->etype; + ent->key = (struct static_key *) iter->static_key_addr; ent->priv = priv; INIT_LIST_HEAD(&ent->list); list_add_tail(&ent->list, &error_injection_list);