From patchwork Fri May 31 09:33:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13681426 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 983C9C25B75 for ; Fri, 31 May 2024 09:33:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 01A976B009B; Fri, 31 May 2024 05:33:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E224B6B0098; Fri, 31 May 2024 05:33:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC6BC6B0095; Fri, 31 May 2024 05:33:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 65BB86B0099 for ; Fri, 31 May 2024 05:33:44 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id EEEFD160740 for ; Fri, 31 May 2024 09:33:43 +0000 (UTC) X-FDA: 82178178726.06.42E954B Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf15.hostedemail.com (Postfix) with ESMTP id AB7D0A001C for ; Fri, 31 May 2024 09:33:41 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=JssftoR6; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b="/5hT1B4N"; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=JssftoR6; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b="/5hT1B4N"; 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=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=u2635+TpURoaMWiOVxYkY4gXrnKDk2yUsxy6mghZqBU=; b=rF8+vc6sF7tAw9TOaUi99Ia57rKwXDjw04BnrUB+0chUBH/7u0EftB4W7IDKpbkbUzc+yG xUwnKyaUXe92SMuRDebZ3M948yuI5ybEtQek9y41AZI+3RnQ9HHQczMorKj6//gl6HxVg5 5ChDdh8u8mxwEXc01skTXmbG47deNjY= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=JssftoR6; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b="/5hT1B4N"; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=JssftoR6; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b="/5hT1B4N"; 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=1717148022; a=rsa-sha256; cv=none; b=bZ3BqVybjn92rlqb9trdsNERKcXtkcmqunFBRZcnP7xGH02a5ZVBOw41kEYZm0ufoeaPOT KUG/bk8OsVQWaIaIxH1KKmXabKWyoG0qSV5wHiw2yXSlBYVJ6GEbMK9oJFYKRM2SBp32YJ mh3nTPq1To6tkU0rraVDNuRVXS90Wg0= 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 10AFD21B25; 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=u2635+TpURoaMWiOVxYkY4gXrnKDk2yUsxy6mghZqBU=; b=JssftoR6B7pLW9rYT+Tuv/kXK/+g2BXda/wOv7dsChTZxN/xGrJlQ1Jc6cmei7hmQSqNqP 6rqvF2BKD8laRmpvrMaXDERqb2Lu+k87pA+l3hoXVTU+jtV4440OtAWW1jJpaGRb1oLD33 LjQbmi/lydv2YxzuDTeIk5RFh3UnqbE= 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=u2635+TpURoaMWiOVxYkY4gXrnKDk2yUsxy6mghZqBU=; b=/5hT1B4N5thhpu2CPFQv8Dbg+mgJCzLOUi8MarugqWlJa+NX1EDAVtQEkknmpwp0aRhQxi itSLEgs1gbsRQ2DQ== 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=u2635+TpURoaMWiOVxYkY4gXrnKDk2yUsxy6mghZqBU=; b=JssftoR6B7pLW9rYT+Tuv/kXK/+g2BXda/wOv7dsChTZxN/xGrJlQ1Jc6cmei7hmQSqNqP 6rqvF2BKD8laRmpvrMaXDERqb2Lu+k87pA+l3hoXVTU+jtV4440OtAWW1jJpaGRb1oLD33 LjQbmi/lydv2YxzuDTeIk5RFh3UnqbE= 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=u2635+TpURoaMWiOVxYkY4gXrnKDk2yUsxy6mghZqBU=; b=/5hT1B4N5thhpu2CPFQv8Dbg+mgJCzLOUi8MarugqWlJa+NX1EDAVtQEkknmpwp0aRhQxi itSLEgs1gbsRQ2DQ== 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 E3E9B13A64; Fri, 31 May 2024 09:33:39 +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 eMBdN3OZWWZKHQAAD6G6ig (envelope-from ); Fri, 31 May 2024 09:33:39 +0000 From: Vlastimil Babka Date: Fri, 31 May 2024 11:33:32 +0200 Subject: [PATCH RFC 1/4] fault-inject: add support for static keys around fault injection sites MIME-Version: 1.0 Message-Id: <20240531-fault-injection-statickeys-v1-1-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: AB7D0A001C X-Stat-Signature: m8wixysif16413tmtrpjnnik7w3pgdq5 X-Rspam-User: X-HE-Tag: 1717148021-299478 X-HE-Meta: U2FsdGVkX1/31Xphb0TmrxPM1img/mLGKBpr5eeQlrF4+OA58iNYxq/SQiWKVRJ9+mS7cjXALQKt+M+lhwzMQkZbhY9qLn7Qyac+stmVMMpXBE7P9yeJx+Xp1INb9Jn86dYpX0QYgRdGs8C5YoQ47BXctzvhjsMDNZwqplxWGQQETkJ1uM6ulJN+o4zm1AdPN6XXan8YafSHuFgevonB10zYiYSgjW/ML3mQjBImQqr+OfhB7CY9rH8VORsXUmo0ftNnIAmYN7C99X+ZnWvP0SvvJlSt3ftY/Na3p+VR7MJHJdnKIfI7DTjPFubVNQqepVTuN3fLYV9FuwA9hDMc75e4auoH2NtriC+h/aTmhHQvL4ShzzWKFO76Cp5NL+W//61x9XU9XmJhBMoPdaVj0LSW5vHuO4UHkg3r1Lqp+yL0lXn51f9jTOfsf1keC1wYjY3feEj+sBn+BbnrlVeAcYfqrX51aiwws9/Goj9n46TiWEe40srlzY98tLi0MQzaZquhSzSFkKjHoXuAVF6VpC1biSw8XD/EPa0gOh3bSqqtff0IxpEwF5gz/5ZS38w+w/vvqhoudjyVmBcP1gQvy/6Txc/Mw6lgb28MwZKsCEbPYWtTFqkOW2Rbv7gUAd7gNSnN0FrCLwgEzOj8KQ9owDQUiJeBzgwWqzkY1tG4gSDCtARdp9NPV9lf1dJpDKl9thv3rfouK8REWuXKsFfv2O4ecx6dkM16DK00/UUyhByTewx22NK97oqEcc6zlB+XrVX6m/Y+a3BWud2KFdoZoFZK199cnFxFhUHvesKiT1rxbBYwCk9SaNGm2jKJuuHXVy2WKZSjrptW6GfX32e/o+DrqAPZAy9ckWco82ZYKbic7bsW0jts/GbfZxx8YMv8eo6sTcsym+jC8ooWbbjcsY7ZYvjvv1iOM3KIGlWvVXTcYf4PfpU66OYPG9IHsKYssNqCGy6Fq9Iq4oHoTT3 JwJ/Vp4s S7PT8Bip1VkmQ1gfCf50AvQutobDLTpbRKvL3/SEuEu8PIokQRzIUxIpWrttcvfteWcNeHQxaNWDc6Q0QXU9lu9ETfL0uHq7jad8xf1LnXgdldkiaHcigC6sG57YUmIZM4HsZm37QoFa2QA8IKacC0DOnYEDbKWtUOlkurJnoYvLG1YpoGGplfQJzqpj+7jjg89/bL+Qf6NYeJb+p5DWerR5PYQspRCP6+uj68yoTkmGfmdI3mOwhEeSEJG3SEeUqzqxGZqYSccc1W45JRFwN431wKu9wtdDsv1Ae4WW1XKEWPv8= 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: Some fault injection sites are placed in hotpaths and incur overhead even if not enabled, due to one or more function calls leading up to should_fail_ex() that returns false due to attr->probability == 0. This overhead can be eliminated if the outermost call into the checks is guarded with a static key, so add support for that. The framework should be told that such static key exist for a fault_attr, by initializing fault_attr->active with the static key address. When it's not NULL, enable the static key from setup_fault_attr() when the fault probability is non-zero. Also wire up writing into debugfs "probability" file to enable or disable the static key when transitioning between zero and non-zero probability. For now, do not add configfs interface support as the immediate plan is to leverage this for should_failslab() and should_fail_alloc_page() after other necessary preparatory changes, and none of the configfs based fault injection users. Signed-off-by: Vlastimil Babka --- include/linux/fault-inject.h | 7 ++++++- lib/fault-inject.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/include/linux/fault-inject.h b/include/linux/fault-inject.h index 6d5edef09d45..cfe75cc1bac4 100644 --- a/include/linux/fault-inject.h +++ b/include/linux/fault-inject.h @@ -9,6 +9,7 @@ #include #include #include +#include /* * For explanation of the elements of this struct, see @@ -30,13 +31,14 @@ struct fault_attr { unsigned long count; struct ratelimit_state ratelimit_state; struct dentry *dname; + struct static_key *active; }; enum fault_flags { FAULT_NOWARN = 1 << 0, }; -#define FAULT_ATTR_INITIALIZER { \ +#define FAULT_ATTR_INITIALIZER_KEY(_key) { \ .interval = 1, \ .times = ATOMIC_INIT(1), \ .require_end = ULONG_MAX, \ @@ -44,8 +46,11 @@ enum fault_flags { .ratelimit_state = RATELIMIT_STATE_INIT_DISABLED, \ .verbose = 2, \ .dname = NULL, \ + .active = (_key), \ } +#define FAULT_ATTR_INITIALIZER FAULT_ATTR_INITIALIZER_KEY(NULL) + #define DECLARE_FAULT_ATTR(name) struct fault_attr name = FAULT_ATTR_INITIALIZER int setup_fault_attr(struct fault_attr *attr, char *str); bool should_fail_ex(struct fault_attr *attr, ssize_t size, int flags); diff --git a/lib/fault-inject.c b/lib/fault-inject.c index d608f9b48c10..93c46d2ec106 100644 --- a/lib/fault-inject.c +++ b/lib/fault-inject.c @@ -35,6 +35,9 @@ int setup_fault_attr(struct fault_attr *attr, char *str) atomic_set(&attr->times, times); atomic_set(&attr->space, space); + if (probability != 0 && attr->active) + static_key_slow_inc(attr->active); + return 1; } EXPORT_SYMBOL_GPL(setup_fault_attr); @@ -166,6 +169,12 @@ EXPORT_SYMBOL_GPL(should_fail); #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS +/* + * Protect updating probability from debugfs as that may trigger static key + * changes when changing between zero and non-zero. + */ +static DEFINE_MUTEX(probability_mutex); + static int debugfs_ul_set(void *data, u64 val) { *(unsigned long *)data = val; @@ -186,6 +195,38 @@ static void debugfs_create_ul(const char *name, umode_t mode, debugfs_create_file(name, mode, parent, value, &fops_ul); } +static int debugfs_prob_set(void *data, u64 val) +{ + struct fault_attr *attr = data; + + mutex_lock(&probability_mutex); + + if (attr->active) { + if (attr->probability != 0 && val == 0) { + static_key_slow_dec(attr->active); + } else if (attr->probability == 0 && val != 0) { + static_key_slow_inc(attr->active); + } + } + + attr->probability = val; + + mutex_unlock(&probability_mutex); + + return 0; +} + +static int debugfs_prob_get(void *data, u64 *val) +{ + struct fault_attr *attr = data; + + *val = attr->probability; + + return 0; +} + +DEFINE_SIMPLE_ATTRIBUTE(fops_prob, debugfs_prob_get, debugfs_prob_set, "%llu\n"); + #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER static int debugfs_stacktrace_depth_set(void *data, u64 val) @@ -218,7 +259,7 @@ struct dentry *fault_create_debugfs_attr(const char *name, if (IS_ERR(dir)) return dir; - debugfs_create_ul("probability", mode, dir, &attr->probability); + debugfs_create_file("probability", mode, dir, &attr, &fops_prob); debugfs_create_ul("interval", mode, dir, &attr->interval); debugfs_create_atomic_t("times", mode, dir, &attr->times); debugfs_create_atomic_t("space", mode, dir, &attr->space);