From patchwork Fri Jun 21 00:24:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Leoshkevich X-Patchwork-Id: 13706569 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 E2B2FC2BA18 for ; Fri, 21 Jun 2024 00:27:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60F136B0122; Thu, 20 Jun 2024 20:26:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5BD6B6B0125; Thu, 20 Jun 2024 20:26:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 411AE6B0122; Thu, 20 Jun 2024 20:26:52 -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 146626B00FF for ; Thu, 20 Jun 2024 20:26:52 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CCDF3A06C3 for ; Fri, 21 Jun 2024 00:26:51 +0000 (UTC) X-FDA: 82253005422.27.C1297F4 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by imf25.hostedemail.com (Postfix) with ESMTP id 83564A000E for ; Fri, 21 Jun 2024 00:26:49 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=MJukNpgb; dmarc=pass (policy=none) header.from=ibm.com; spf=pass (imf25.hostedemail.com: domain of iii@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=iii@linux.ibm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718929602; a=rsa-sha256; cv=none; b=b6+Fdz1ZFDrmRfFKAfwkFVi6kHmKZkOR9NIXDMGqHxKNGIJ/VmhB1jVJ0RK/ePsbLncjlR 3901Jvff/gydjRxkhBaliIrOhfEegbRJSfBmd++V/71gR6bJZyZ5BBRN/4nWegalC0aw8U npUTen2G/0FZAGUznRzH1VaAcEsQBow= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=MJukNpgb; dmarc=pass (policy=none) header.from=ibm.com; spf=pass (imf25.hostedemail.com: domain of iii@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=iii@linux.ibm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718929602; 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=JyCixd63UT10LcLHBLxXvC3vYkg4ad8S/LeVZYMb+Kc=; b=O0+GFpCi4QTG1eMnTj3EpCJgQ9bxxMPBmpgOvOEG4/71JlUMw5DiTqOhsWMZjme4+/JPzv qzSck+N9qRmv+1gAudJNa4ElDlL9IQvmaSi5BeSiSFb8+7Pvrj9LBDGGQox8wr0quZ+tG0 36jcfK/iXXo2t2lGnHSlRtnWfl5T+lE= Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45KN6ts3030154; Fri, 21 Jun 2024 00:26:41 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=JyCixd63UT10L cLHBLxXvC3vYkg4ad8S/LeVZYMb+Kc=; b=MJukNpgbMpWrVF59W4IW8brxQK6N/ BwwR05DugY4rSzfxJ0cm2odtgmDFVmO93seeGwj5PpnHTJOAdpRZqw76JlJePl+c TktrXlYUnkhQuSqsVWJGS8N1rcrY1UAGuxSh5YQjZ+SAfu0KJkgTnFmB5t/so+ji fZAxt4t8Z6/4UyC7jHtt5XO6OCks1SzIp17knHbxcY3Tzvybv97LGc7okU4zTqec 30Wjz4XHmMT30OgX4wJNkLItoFzKrOsz6iZdd1aY3jeSSV1SsAcKwlKlk86hj/Xp E+2BSlEArJcglqb8rtvvuoTokvytHKZiPqH5JAw9qUr96hQ/lXoSkoYRQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3yvw8c070d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 21 Jun 2024 00:26:41 +0000 (GMT) Received: from m0356517.ppops.net (m0356517.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 45L0Qepq022701; Fri, 21 Jun 2024 00:26:40 GMT Received: from ppma21.wdc07v.mail.ibm.com (5b.69.3da9.ip4.static.sl-reverse.com [169.61.105.91]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3yvw8c0707-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 21 Jun 2024 00:26:40 +0000 (GMT) Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 45L0PCOc019980; Fri, 21 Jun 2024 00:26:39 GMT Received: from smtprelay04.fra02v.mail.ibm.com ([9.218.2.228]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3yvrqujnws-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 21 Jun 2024 00:26:39 +0000 Received: from smtpav01.fra02v.mail.ibm.com (smtpav01.fra02v.mail.ibm.com [10.20.54.100]) by smtprelay04.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 45L0QX1N16253262 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 21 Jun 2024 00:26:35 GMT Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 74F5B2004D; Fri, 21 Jun 2024 00:26:33 +0000 (GMT) Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4673E20040; Fri, 21 Jun 2024 00:26:32 +0000 (GMT) Received: from heavy.ibm.com (unknown [9.171.10.44]) by smtpav01.fra02v.mail.ibm.com (Postfix) with ESMTP; Fri, 21 Jun 2024 00:26:32 +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 v6 11/39] kmsan: Allow disabling KMSAN checks for the current task Date: Fri, 21 Jun 2024 02:24:45 +0200 Message-ID: <20240621002616.40684-12-iii@linux.ibm.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240621002616.40684-1-iii@linux.ibm.com> References: <20240621002616.40684-1-iii@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: clVUn1IDbtacwO_0GWY7U5sOyrimaMLk X-Proofpoint-GUID: X4_a9bjV2cmY5PTGlnJ8u_RDQjykc-B4 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-20_11,2024-06-20_04,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 phishscore=0 mlxscore=0 bulkscore=0 priorityscore=1501 spamscore=0 impostorscore=0 clxscore=1015 adultscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2406140001 definitions=main-2406210001 X-Rspamd-Queue-Id: 83564A000E X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: u5o743rmw616pj1ckwwpwpyz8tiztcze X-HE-Tag: 1718929609-77092 X-HE-Meta: U2FsdGVkX19HFF6Ha0ud9HydVi1eNQJLuEiOXSeCLllKjzRTWWUKjw1J2xXy6yX+pHfzvh/CYDCVxfn7xumgX4dNuLsGkL3wyzs9pn+8QmjHbO7Wou6g+YygZU+jaMLjwpsJZbfE4yXILIfTn+kA4LLQCw57UJr5oxasLMW6gUZ/crRe50NhFokUdbUCh8Mlg3yZLmVgJfbppSx3iskFJcUzmFZoaJ0YwUlpyX8HNMuad30mHWDb0MMzUhjbmipPJtKwnAcP5GbXmXymy0lJk01rdTwLY/JMi2BiKPA2WROqcNo2IN7R/70CWVCd0s5cLl76FNgh254nyCtjsSDgfPtWcUGbqCeFysSyn0PxLic9EXCZhDhfxTK5mfekF9dn+zROLZihKd33F4MwSThfM66d2L9HzflZOgZL2K63A8YCBjl3psUM+8baCvx99LF9tnriUyrPzISecM8qPvx2gP5/9qJwIRV5U+PnYbiXf7b+ZkLugFgE20yQkGYe4K01FSU6jMA0Qv2bPONfqjUpqm0gYuoB8bohjLtFF7/p9TRZIEpa9UIBIIxBVKvrdWFoqHTVsFD6gLXkXqWhaQQNUoooa0oJED48BDIv9cUiEfcgyVXlw9me7EAt6o31vJBlCnadrXHAnGL7n0hOy6BIU8Z671bxtu+bOcLep4cUEjhiDtQ87fO4/trI+jBijF22IkTlEUoFwzH8X1UrDmwEzho2Swg/SG+7+EKHWGt6DpIzEv21g0JkSDAD8vdHzsCLtduxWtPm6aGLG7JYMMepSNxsVk63QANzkfCBkJao8okeSuFwaHjhta8SOsDeHMS0nQCwUIn9Se5grRh4i5UNQ/C1NUrGQ5RWej2b8kIlP7CNeBfHe3LXmoboQlZK1SuRBavACL4rmlApDUbe3Aop5MSw8rooumQxx6mxBmXo0lX3+JAmQI+uJd4KGP3oYpG0obpIIAVMnnzUBewOhKC tRuPd9Bf Yl0kjB1G0+7lwIt4KiOD86jJ2wKri0QqwN7gIIdEg0J55YZCHBNmOc0bVgymNrXjzoVMw0JBZtEajwHc+0mc7uu5YhO8+h+zC2PQZTUzKD0v6FmTwTGPVImP6XwMaPaoT3TSB1e0PPnBzgWjuv04wnIZcDM/DJavXIh+UDK3qNbIldIWNZP9osxpJixYZx13OXUbdRIyTjvddY+4CP2EXa9g3didwasovCjQPXHoEhktv1K393B85uJpgMQ== 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. Reviewed-by: Alexander Potapenko Signed-off-by: Ilya Leoshkevich --- Documentation/dev-tools/kmsan.rst | 11 +++++++++-- 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, 55 insertions(+), 10 deletions(-) diff --git a/Documentation/dev-tools/kmsan.rst b/Documentation/dev-tools/kmsan.rst index 323eedad53cd..6a48d96c5c85 100644 --- a/Documentation/dev-tools/kmsan.rst +++ b/Documentation/dev-tools/kmsan.rst @@ -110,6 +110,13 @@ in the Makefile. Think of this as applying ``__no_sanitize_memory`` to every function in the file or directory. Most users won't need KMSAN_SANITIZE, unless their code gets broken by KMSAN (e.g. runs at early boot time). +KMSAN checks can also be temporarily disabled for the current task using +``kmsan_disable_current()`` and ``kmsan_enable_current()`` calls. Each +``kmsan_enable_current()`` call must be preceded by a +``kmsan_disable_current()`` call; these call pairs may be nested. One needs to +be careful with these calls, keeping the regions short and preferring other +ways to disable instrumentation, where possible. + Support ======= @@ -338,11 +345,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..14b5ea6d3a43 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",