From mboxrd@z Thu Jan 1 00:00:00 1970 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 4A589C27C4F for ; Thu, 13 Jun 2024 15:40:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3A966B00AB; Thu, 13 Jun 2024 11:39:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E39AA6B00B0; Thu, 13 Jun 2024 11:39:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC3746B00AC; Thu, 13 Jun 2024 11:39:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 871826B00AB for ; Thu, 13 Jun 2024 11:39:56 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3745312021D for ; Thu, 13 Jun 2024 15:39:56 +0000 (UTC) X-FDA: 82226275992.11.53FE9B3 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by imf30.hostedemail.com (Postfix) with ESMTP id 10A8580003 for ; Thu, 13 Jun 2024 15:39:53 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=qK+vecRb; spf=pass (imf30.hostedemail.com: domain of iii@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=iii@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718293193; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=EtpzWUsKPF6Xqg09UGXz7IHFcSozBdT63cYyYIkbG/E=; b=BzS030IsVPrIuvlCpwzldCyVWlAzMB80n2n+27WPCwc9KmFqwnc91cclmL5SkrDFvBgwSr JYzaCcCy07PAeia5F/sSV6L6USKG37RkkCqXLzEnAWkioaW4jl6AQIEPd411HprSr1D5cv ZFTfJyovCHxgoapvBqlZDb0RBKYszGw= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=qK+vecRb; spf=pass (imf30.hostedemail.com: domain of iii@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=iii@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718293193; a=rsa-sha256; cv=none; b=Py69v4nV/q5ks/ck98Z68mctnLE6z81GwvR4kn6CZvRKUtZiArcrH+aco5WutOTeQKRs6w qdTrb/9zlL9A7iVJ3uyqbbkBbcMn7FpgxTXu6P48PbxFjLf7dhnE5Twce4zibGvn6bTcm4 VZmsAnjc0PORIrQPBY+VAlkO87p/ulc= Received: from pps.filterd (m0353725.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45DFaxh5028926; Thu, 13 Jun 2024 15:39:46 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=pp1; bh=EtpzWUsKPF6Xq g09UGXz7IHFcSozBdT63cYyYIkbG/E=; b=qK+vecRbYYzCJkRsHBWubrva3REep w/L05qAPzmCKy04ck7vhAnXHPdQL7ZgY4AB0/HwhxaazpXScZzYfEI7dT2/HXWEQ sK6/3glkhSZDbHnN3sJeK1GUk6U8wd3J5meIeDstRJEMRWqT7aHpxfePPMVFag4G wT4UdP62uM8MtrSW2QWQa09IjK1EVvq3NzwKhJ+N3BQxJpkT9oBvYST3Ljo/8G7X LSyp1nWkVWc7Ywi81lW8IyUbgTH6UASVaM1n9CfRpdAUt0SefudhICkZlF1fUC63 gCQ2yuJj8651Sly70S2kImGowL4ZHICA2AxR/mgD8iysnBsEdRexIi7bA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3yr320r32t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 13 Jun 2024 15:39:46 +0000 (GMT) Received: from m0353725.ppops.net (m0353725.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 45DFdjpx002434; Thu, 13 Jun 2024 15:39:45 GMT Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3yr320r32q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 13 Jun 2024 15:39:45 +0000 (GMT) Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 45DEU7Sm008700; Thu, 13 Jun 2024 15:39:44 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 3yn4b3rk0u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 13 Jun 2024 15:39:44 +0000 Received: from smtpav07.fra02v.mail.ibm.com (smtpav07.fra02v.mail.ibm.com [10.20.54.106]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 45DFddjr52036052 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 13 Jun 2024 15:39:41 GMT Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id EB49C2005A; Thu, 13 Jun 2024 15:39:38 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 783B82006C; Thu, 13 Jun 2024 15:39:38 +0000 (GMT) Received: from black.boeblingen.de.ibm.com (unknown [9.155.200.166]) by smtpav07.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 13 Jun 2024 15:39:38 +0000 (GMT) From: Ilya Leoshkevich To: Alexander Gordeev , Alexander Potapenko , Andrew Morton , Christoph Lameter , David Rientjes , Heiko Carstens , Joonsoo Kim , Marco Elver , Masami Hiramatsu , Pekka Enberg , Steven Rostedt , Vasily Gorbik , Vlastimil Babka Cc: Christian Borntraeger , Dmitry Vyukov , Hyeonggon Yoo <42.hyeyoo@gmail.com>, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Mark Rutland , Roman Gushchin , Sven Schnelle , Ilya Leoshkevich Subject: [PATCH v4 11/35] kmsan: Allow disabling KMSAN checks for the current task Date: Thu, 13 Jun 2024 17:34:13 +0200 Message-ID: <20240613153924.961511-12-iii@linux.ibm.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613153924.961511-1-iii@linux.ibm.com> References: <20240613153924.961511-1-iii@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: LKpz0MVqEHZkt6RKhjF39DHCXdemLjnE X-Proofpoint-ORIG-GUID: 4diWKsJb6QpzSM8Rww7A3YFM9_vI0f-x X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-13_09,2024-06-13_02,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 impostorscore=0 mlxscore=0 adultscore=0 mlxlogscore=999 spamscore=0 suspectscore=0 phishscore=0 priorityscore=1501 clxscore=1015 lowpriorityscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2406130112 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 10A8580003 X-Stat-Signature: puqswqeoy4mehscc8onfps4f9utoqp9s X-HE-Tag: 1718293193-235186 X-HE-Meta: U2FsdGVkX1+x2WwHsVraqXHhaHdJre6I339qmIEJcawAvom6s2O0/FJWya76vRFzf/WfyQ24vHpWPzmAUbyjHMn0lGrDA59HmKTjKrBCgAx3x1+dnEB32fGsta4mV9PJjd+OjTmfiIM+V74twSiBelKjxJu/WsnVVMPiDT2cjLeZExSZZQq4gD7FlXv4RyIY8hw8oqUlrBW1ekUafcwGNoziDcOLMECTCWHzLTUbzM0/sWQ9g5myS2RicOu0YWagO8lvdZRFKgtJreb6OnM8x6dLfscVxfLII3pxrpl5bRliw9tdk1qYd9UXEZMDmCZLCm8ewAhOncIgrsHjn11V8NM/nH7mlFtSkYzdXG1LIxUN9WqtE5PuInrbCeqLWh41qBqfFKUQ4c5MHAZUymiUn9+0vW7QlMHJk4GeizI0t7ygxZfAnw1cAAL9n2uK8ehIOtxj2zhNSLLaCus5F2o7F3IERAhKMxPoAO/oJjZAnY9t/teU+njcisHsAX1dwu0lnA82DiVT63Sk+M6nyceJ9qg//nvhyZ6HB8yJbSeYe4d/5viom1eOk3X9HDcP53H62jh2cdx5X4nTQlt86A6MyUgrF6r68j5sjdcHhZVgzsvdF61xVAm0+3D2yC7S8T0eL9/nmfKEEuNNNpSGzy0axWr+PMeLHGUVTDOG7saS+bL37DB1G0lWET9UEiHrxCg1oUzRndwXRMRPZSL5wSxseJLbTaSPeFO+CZIMP2IXaDWI02QgqtB0cpV9y7Xo00Kr/vLp1pHPQEzikm6Edl4evYs2xkk4h5n1yv9V0T78hA/FFUWN/Xb4FbDf7/OH+DRp5ghL7PlrGBSP8TnbIEg35bDHXuAYbfA72zYB6W4SrPB/6z2QaSVjRGFhb7t1InRzFmVqMKDUfuayMlRXUIwNYnw+P6oF/XDzxrpuw0I4zPw463G/zSlVLDq6kZc4l1UZmuHkD0QkmQzq3ideAlz kl3gwgw2 uG/ONZh0mCrPlP89/eW0FRDAAl10zttEVBEGzkP0s6w0x6Tdwj9EhGRqrv2UFTMah1rdEnps2ZltkqsRV9Qz2brAET1a1tPkvfpDZgiQ4BhFogiTs7OhTrszxZQ5V7f7yWgsNem7sonqSiiOzs5z4QrRNbJm+U48DGnivCE0WJHub9PRvBRsprOhzp+Sfxgwq4ijFSm77oqwe2x0vgI3TNOdQPC/zcHeyc9R8XOYLPmT+JxPokhs6rMiaYvqjNtu6K8yk+7EiYoCqDv/WDzCtLnD5ZjzfC4RScyxA2kD/zSMMwUD6DpoLxePPaw== 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: Like for KASAN, it's useful to temporarily disable KMSAN checks around, e.g., redzone accesses. Introduce kmsan_disable_current() and kmsan_enable_current(), which are similar to their KASAN counterparts. Make them reentrant in order to handle memory allocations in interrupt context. Repurpose the allow_reporting field for this. Signed-off-by: Ilya Leoshkevich --- Documentation/dev-tools/kmsan.rst | 4 ++-- include/linux/kmsan.h | 24 ++++++++++++++++++++++++ include/linux/kmsan_types.h | 2 +- mm/kmsan/core.c | 1 - mm/kmsan/hooks.c | 18 +++++++++++++++--- mm/kmsan/report.c | 7 ++++--- tools/objtool/check.c | 2 ++ 7 files changed, 48 insertions(+), 10 deletions(-) diff --git a/Documentation/dev-tools/kmsan.rst b/Documentation/dev-tools/kmsan.rst index 323eedad53cd..022a823f5f1b 100644 --- a/Documentation/dev-tools/kmsan.rst +++ b/Documentation/dev-tools/kmsan.rst @@ -338,11 +338,11 @@ Per-task KMSAN state ~~~~~~~~~~~~~~~~~~~~ Every task_struct has an associated KMSAN task state that holds the KMSAN -context (see above) and a per-task flag disallowing KMSAN reports:: +context (see above) and a per-task counter disallowing KMSAN reports:: struct kmsan_context { ... - bool allow_reporting; + unsigned int depth; struct kmsan_context_state cstate; ... } diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index fe6c2212bdb1..23de1b3d6aee 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -239,6 +239,22 @@ void kmsan_unpoison_entry_regs(const struct pt_regs *regs); */ void *kmsan_get_metadata(void *addr, bool is_origin); +/* + * kmsan_enable_current(): Enable KMSAN for the current task. + * + * Each kmsan_enable_current() current call must be preceded by a + * kmsan_disable_current() call. These call pairs may be nested. + */ +void kmsan_enable_current(void); + +/* + * kmsan_disable_current(): Disable KMSAN for the current task. + * + * Each kmsan_disable_current() current call must be followed by a + * kmsan_enable_current() call. These call pairs may be nested. + */ +void kmsan_disable_current(void); + #else static inline void kmsan_init_shadow(void) @@ -338,6 +354,14 @@ static inline void kmsan_unpoison_entry_regs(const struct pt_regs *regs) { } +static inline void kmsan_enable_current(void) +{ +} + +static inline void kmsan_disable_current(void) +{ +} + #endif #endif /* _LINUX_KMSAN_H */ diff --git a/include/linux/kmsan_types.h b/include/linux/kmsan_types.h index 929287981afe..dfc59918b3c0 100644 --- a/include/linux/kmsan_types.h +++ b/include/linux/kmsan_types.h @@ -31,7 +31,7 @@ struct kmsan_context_state { struct kmsan_ctx { struct kmsan_context_state cstate; int kmsan_in_runtime; - bool allow_reporting; + unsigned int depth; }; #endif /* _LINUX_KMSAN_TYPES_H */ diff --git a/mm/kmsan/core.c b/mm/kmsan/core.c index 95f859e38c53..81b22220711a 100644 --- a/mm/kmsan/core.c +++ b/mm/kmsan/core.c @@ -43,7 +43,6 @@ void kmsan_internal_task_create(struct task_struct *task) struct thread_info *info = current_thread_info(); __memset(ctx, 0, sizeof(*ctx)); - ctx->allow_reporting = true; kmsan_internal_unpoison_memory(info, sizeof(*info), false); } diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index b408714f9ba3..267d0afa2e8b 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -39,12 +39,10 @@ void kmsan_task_create(struct task_struct *task) void kmsan_task_exit(struct task_struct *task) { - struct kmsan_ctx *ctx = &task->kmsan_ctx; - if (!kmsan_enabled || kmsan_in_runtime()) return; - ctx->allow_reporting = false; + kmsan_disable_current(); } void kmsan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags) @@ -424,3 +422,17 @@ void kmsan_check_memory(const void *addr, size_t size) REASON_ANY); } EXPORT_SYMBOL(kmsan_check_memory); + +void kmsan_enable_current(void) +{ + KMSAN_WARN_ON(current->kmsan_ctx.depth == 0); + current->kmsan_ctx.depth--; +} +EXPORT_SYMBOL(kmsan_enable_current); + +void kmsan_disable_current(void) +{ + current->kmsan_ctx.depth++; + KMSAN_WARN_ON(current->kmsan_ctx.depth == 0); +} +EXPORT_SYMBOL(kmsan_disable_current); diff --git a/mm/kmsan/report.c b/mm/kmsan/report.c index c79d3b0d2d0d..92e73ec61435 100644 --- a/mm/kmsan/report.c +++ b/mm/kmsan/report.c @@ -8,6 +8,7 @@ */ #include +#include #include #include #include @@ -158,12 +159,12 @@ void kmsan_report(depot_stack_handle_t origin, void *address, int size, if (!kmsan_enabled) return; - if (!current->kmsan_ctx.allow_reporting) + if (current->kmsan_ctx.depth) return; if (!origin) return; - current->kmsan_ctx.allow_reporting = false; + kmsan_disable_current(); ua_flags = user_access_save(); raw_spin_lock(&kmsan_report_lock); pr_err("=====================================================\n"); @@ -216,5 +217,5 @@ void kmsan_report(depot_stack_handle_t origin, void *address, int size, if (panic_on_kmsan) panic("kmsan.panic set ...\n"); user_access_restore(ua_flags); - current->kmsan_ctx.allow_reporting = true; + kmsan_enable_current(); } diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 0a33d9195b7a..01237d167223 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1202,6 +1202,8 @@ static const char *uaccess_safe_builtin[] = { "__sanitizer_cov_trace_switch", /* KMSAN */ "kmsan_copy_to_user", + "kmsan_disable_current", + "kmsan_enable_current", "kmsan_report", "kmsan_unpoison_entry_regs", "kmsan_unpoison_memory", -- 2.45.1