Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4438223ioa; Wed, 27 Apr 2022 04:01:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwMxEPIaOM0GHF7NtSpegNDbpTg2QQNQWsZRSwGjE/njMEqDaNmFYdrct6R1eYG5ZCjw0Rp X-Received: by 2002:a63:8b4a:0:b0:3ab:255c:ff8e with SMTP id j71-20020a638b4a000000b003ab255cff8emr13560113pge.285.1651057294332; Wed, 27 Apr 2022 04:01:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651057294; cv=none; d=google.com; s=arc-20160816; b=WYEid/8z26nJtKLex+82jTuY8vYvgBYGVcOqh/W6XlbUK+Og+7zSwoBUxXNGjEB2iU QTMUhF5W4w3ALhYsU3u6xUpOArHUrbtZbPAfkmJouHL2Kflm/fqF8fzlttdGx4rOP6+b 5+MZfv9DA51gR0xCsJdRY/cD/KPCaL0yCM2gcjtH8Tv8dISwH/X+Xrztjoq7nssEnLqu coz9Syg7NKfUAS55U74mLC0zip2Rpefe15vjuaQAsWUeBhnbVMUu57d+K08J83br6cS8 nmbAQzy66d2TROQsAp+rsksTxfPcYBbPYOtcLCde7/niMLgVs16PRUO2zEsYRoDgNGcA zr2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=d8pL6jrPvx0aomk5qPe/O8+76gWOKR1XrEif20OjnQY=; b=X+4gOW+Jf15hypEsfD2Yk4jHdccUBJuTZllK8qaE/vQj+Tsuzq/FN8mXQB1eybyIEZ 9MGcQG1/QeKagZi4db/xewTsArpOeYUcyeEtNOXdyj/asiDmQTV6HlxoDc+ThmOUV69u Y91DZVKGTmU3AJpl/2NDIT2sfzpc2GqVd+YfONo0iSdnoRZQdtetVFBAo7GtGclt+l+i j2MD6aHfiBJ/8TIBU1EPTGxEwT2m76UejKuT+/zYJqSQl5NVVuud5TUtXqzMpPZqj+30 udGmbErZJvEtEN6/o0wsYoNk0DNzBb/oCAG5vI5BrOiLMObBYnma3k8MHy+8sjlFUxIY cdGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qHuNE5uJ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id 1-20020a17090a198100b001d63970bc44si1453783pji.78.2022.04.27.04.01.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 04:01:34 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qHuNE5uJ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5AE58435BFB; Wed, 27 Apr 2022 03:10:23 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353329AbiDZQuS (ORCPT + 99 others); Tue, 26 Apr 2022 12:50:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353259AbiDZQs6 (ORCPT ); Tue, 26 Apr 2022 12:48:58 -0400 Received: from mail-ej1-x649.google.com (mail-ej1-x649.google.com [IPv6:2a00:1450:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 119EFDED3 for ; Tue, 26 Apr 2022 09:45:08 -0700 (PDT) Received: by mail-ej1-x649.google.com with SMTP id x2-20020a1709065ac200b006d9b316257fso9386592ejs.12 for ; Tue, 26 Apr 2022 09:45:08 -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=d8pL6jrPvx0aomk5qPe/O8+76gWOKR1XrEif20OjnQY=; b=qHuNE5uJ2FDzh5HoUwOExyBkIzzUGi7u3r8cb1fmNh9JoURq8b/0KZazqEqktkONuv RSLViVqt3oajP2cDiy1OzrTVw7OGCCwD1o48WWrtmf3dphbUmtfJDJYJSX5YCXSpNfdF aXfERsMSgciWjwFSSbUv2J0vUdBe6KCUA+6Uqtn6lrlmCctTBymZW6DYKyoqZXJTtacd rnvL/jsjRqjNWe6PKbeI0qf9Va3S9UXaomO1/Ti1Zz7QraDNV7xVEPjV7ex+essI4Lra yDFX1T5khoEdzXqF+In8TAeLE7luEASOtTb9QCfHpF9L7qw2pr9BQJoomZYDCxZL6tCZ 7FYg== 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=d8pL6jrPvx0aomk5qPe/O8+76gWOKR1XrEif20OjnQY=; b=HEuE5yOZIIImPe07BRznMWafyOsn9AFImX9YeJ2pUX6qsigJymBSgS12PaEUVw584I gLgxG9xEk80abHOzd4i+tnw0v+uJRPAeKnfi7BGg93JGt8IqXsv6YTPROVgvfRWKyUo1 G1siLEksMydiVWMCoSfYM/PTORukyjdcCSKuQjGVaVclkkOJ1m//qDQ1peEbt8o24/wv xx02gXm71VOn0uwrZIwaXb+1pigGvSG6k06lLkQXZUrszzcmFPJgOMlPV9wtPMqVi6WI ROhe7KpNqno3MbjtvZognuTL+i/DzQB0hSoDCjqOm0XwQXuPpg3MkEGTg5ssW4/tyc0w 65Lg== X-Gm-Message-State: AOAM5325cCAY0QN7ALHfuJfN8mZ4DSWDkWtaf4Y4U8R20Ndk9+qrbNlx 9GbsBJEfCxK7ir/F+3Nai7VkXzyYQ0s= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:d580:abeb:bf6d:5726]) (user=glider job=sendgmr) by 2002:a05:6402:486:b0:413:bd00:4f3f with SMTP id k6-20020a056402048600b00413bd004f3fmr26069921edv.103.1650991507458; Tue, 26 Apr 2022 09:45:07 -0700 (PDT) Date: Tue, 26 Apr 2022 18:42:47 +0200 In-Reply-To: <20220426164315.625149-1-glider@google.com> Message-Id: <20220426164315.625149-19-glider@google.com> Mime-Version: 1.0 References: <20220426164315.625149-1-glider@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v3 18/46] 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 , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 f072959fcab7f..1784b7a741ddd 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -60,6 +60,7 @@ #include #include #include +#include #include #include #include @@ -741,6 +742,7 @@ void __noreturn do_exit(long code) WARN_ON(tsk->plug); 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 9796897560ab1..a6178bd28c409 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -1027,6 +1028,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) tsk->worker_private = NULL; 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 933d864d9d467..4b405abbb6c03 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 052e17b7a717d..43a529569053d 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); -- 2.36.0.rc2.479.g8af0fa9b8e-goog