From patchwork Tue Mar 29 12:39:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12794769 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 1B10EC433F5 for ; Tue, 29 Mar 2022 12:41:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A4B8A8D000A; Tue, 29 Mar 2022 08:41:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9FAEF8D0009; Tue, 29 Mar 2022 08:41:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C2968D000A; Tue, 29 Mar 2022 08:41:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0036.hostedemail.com [216.40.44.36]) by kanga.kvack.org (Postfix) with ESMTP id 7E2618D0009 for ; Tue, 29 Mar 2022 08:41:23 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 362121828A44C for ; Tue, 29 Mar 2022 12:41:23 +0000 (UTC) X-FDA: 79297384446.30.7C40F3B Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf01.hostedemail.com (Postfix) with ESMTP id B95B940005 for ; Tue, 29 Mar 2022 12:41:22 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id v9-20020a509549000000b00418d7c2f62aso10947384eda.15 for ; Tue, 29 Mar 2022 05:41:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=KExSZOK6Ak71zPKr5IrSRQ27+lVgS6k5uhCxlphiTtg=; b=tC2AC8qcVBGGwy5YubyQq84+DV0rcKjoONhQrYq3TKU20eKqh0tvDqhPRf7gRqM4PT dJmEH+z3DLtHBAC40BR8tQ/MaqO0WsxWK2QMgj40Hw2S7T7BAU6mtiu18LqGVLCo5/Qo jIKc2qIgz1E97QzbvGKvXcUu6cxEFeypgm1jOEzMJ2+zTDI7QZ7lhF0Ldt76B+O9/+/p 4pJKclD9Afk4fMQo1aDljx1Ws5Se+KObPjS8eyzNIzamBK0W0f/R7pNdSE7W8atZml4H uCyfBqUv6W9GA2MHL83z28IXWM2iPHYU3cZnXPjc+0QxA/mGQCWJHKnuELSvEXq12m1S 7Z0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=KExSZOK6Ak71zPKr5IrSRQ27+lVgS6k5uhCxlphiTtg=; b=WIaOH7k7Qkeik2x2d7Dee79kwsp5v3+cM+uBX/5Orgrfp8/5NvWFa3KFoml6ntlipr kJheyBllvIHQLvSGtBpD3IjPQhdEhGPaJdhExSvjN62FJFObcGcQMWgg5NcJTX3Ln4Kv JnXmTcl3h7kfhtaNPu3SB5f9LZBlo+hjHAc3rGXGRZ9u3oFHzstQGwx+/CBjnC1RG2DE 418chLZyoqCOiFAzpQHPuoDcYQxK+ada1+M77Q948UGQy4iITnqligbBCpkJ2o1EuyqT uhTLBM0eHZ/YYpMe03NNfaV616U1PGNT1lp5DT9kfkazUmLm+driZvzFTl9yOYvg4jj/ HyYg== X-Gm-Message-State: AOAM530xWjAnuZij6NT9X9JrzDNZycfPkES12ZRqdRXE30M6DXHOXsOa YWsnfV2skaCr/mjT4go90xSnL72Weqw= X-Google-Smtp-Source: ABdhPJx++M+AfNEUSZZQiWR+2EsjU8nZXkRMWuKEK5h5tMt4uYSC+sCaPadi/60UsvXveKH0DEZLOUoc2uE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:36eb:759:798f:98c3]) (user=glider job=sendgmr) by 2002:a17:906:c092:b0:6cd:f3a1:a11e with SMTP id f18-20020a170906c09200b006cdf3a1a11emr33573510ejz.185.1648557680652; Tue, 29 Mar 2022 05:41:20 -0700 (PDT) Date: Tue, 29 Mar 2022 14:39:48 +0200 In-Reply-To: <20220329124017.737571-1-glider@google.com> Message-Id: <20220329124017.737571-20-glider@google.com> Mime-Version: 1.0 References: <20220329124017.737571-1-glider@google.com> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog Subject: [PATCH v2 19/48] kmsan: handle task creation and exiting From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Mark Rutland , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Steven Rostedt , Thomas Gleixner , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=tC2AC8qc; spf=pass (imf01.hostedemail.com: domain of 3cP5CYgYKCJE163yzC19916z.x97638FI-775Gvx5.9C1@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3cP5CYgYKCJE163yzC19916z.x97638FI-775Gvx5.9C1@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: po6xwmu8zigtdmcfpe8f8pduobe8ea7a X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: B95B940005 X-HE-Tag: 1648557682-697681 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: Tell KMSAN that a new task is created, so the tool creates a backing metadata structure for that task. Signed-off-by: Alexander Potapenko --- v2: -- move implementation of kmsan_task_create() and kmsan_task_exit() here Link: https://linux-review.googlesource.com/id/I0f41c3a1c7d66f7e14aabcfdfc7c69addb945805 --- include/linux/kmsan.h | 17 +++++++++++++++++ kernel/exit.c | 2 ++ kernel/fork.c | 2 ++ mm/kmsan/core.c | 10 ++++++++++ mm/kmsan/hooks.c | 19 +++++++++++++++++++ mm/kmsan/kmsan.h | 2 ++ 6 files changed, 52 insertions(+) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index ed3630068e2ef..dca42e0e91991 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -17,6 +17,7 @@ struct page; struct kmem_cache; +struct task_struct; #ifdef CONFIG_KMSAN @@ -43,6 +44,14 @@ struct kmsan_ctx { bool allow_reporting; }; +void kmsan_task_create(struct task_struct *task); + +/** + * kmsan_task_exit() - Notify KMSAN that a task has exited. + * @task: task about to finish. + */ +void kmsan_task_exit(struct task_struct *task); + /** * kmsan_alloc_page() - Notify KMSAN about an alloc_pages() call. * @page: struct page pointer returned by alloc_pages(). @@ -164,6 +173,14 @@ void kmsan_iounmap_page_range(unsigned long start, unsigned long end); #else +static inline void kmsan_task_create(struct task_struct *task) +{ +} + +static inline void kmsan_task_exit(struct task_struct *task) +{ +} + static inline int kmsan_alloc_page(struct page *page, unsigned int order, gfp_t flags) { diff --git a/kernel/exit.c b/kernel/exit.c index b00a25bb4ab93..15e1bf7fe1fa1 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -59,6 +59,7 @@ #include #include #include +#include #include #include #include @@ -752,6 +753,7 @@ void __noreturn do_exit(long code) force_uaccess_begin(); kcov_task_exit(tsk); + kmsan_task_exit(tsk); coredump_task_exit(tsk); ptrace_event(PTRACE_EVENT_EXIT, code); diff --git a/kernel/fork.c b/kernel/fork.c index f1e89007f2288..f62c51d9cbfb1 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -956,6 +957,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) account_kernel_stack(tsk, 1); kcov_task_init(tsk); + kmsan_task_create(tsk); kmap_local_fork(tsk); #ifdef CONFIG_FAULT_INJECTION diff --git a/mm/kmsan/core.c b/mm/kmsan/core.c index f4196f274e754..8e594361332c6 100644 --- a/mm/kmsan/core.c +++ b/mm/kmsan/core.c @@ -44,6 +44,16 @@ bool kmsan_enabled __read_mostly; */ DEFINE_PER_CPU(struct kmsan_ctx, kmsan_percpu_ctx); +void kmsan_internal_task_create(struct task_struct *task) +{ + struct kmsan_ctx *ctx = &task->kmsan_ctx; + + __memset(ctx, 0, sizeof(struct kmsan_ctx)); + ctx->allow_reporting = true; + kmsan_internal_unpoison_memory(current_thread_info(), + sizeof(struct thread_info), false); +} + void kmsan_internal_poison_memory(void *address, size_t size, gfp_t flags, unsigned int poison_flags) { diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index e7c3ff48ed5cd..a13e15ef2bfd5 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -26,6 +26,25 @@ * skipping effects of functions like memset() inside instrumented code. */ +void kmsan_task_create(struct task_struct *task) +{ + kmsan_enter_runtime(); + kmsan_internal_task_create(task); + kmsan_leave_runtime(); +} +EXPORT_SYMBOL(kmsan_task_create); + +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; +} +EXPORT_SYMBOL(kmsan_task_exit); + void kmsan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags) { if (unlikely(object == NULL)) diff --git a/mm/kmsan/kmsan.h b/mm/kmsan/kmsan.h index bfe38789950a6..a1b5900ffd97b 100644 --- a/mm/kmsan/kmsan.h +++ b/mm/kmsan/kmsan.h @@ -172,6 +172,8 @@ void kmsan_internal_set_shadow_origin(void *address, size_t size, int b, u32 origin, bool checked); depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id); +void kmsan_internal_task_create(struct task_struct *task); + bool kmsan_metadata_is_contiguous(void *addr, size_t size); void kmsan_internal_check_memory(void *addr, size_t size, const void *user_addr, int reason);