From patchwork Fri Jul 1 14:22:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12903373 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 16236C43334 for ; Fri, 1 Jul 2022 14:24:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8D546B0089; Fri, 1 Jul 2022 10:24:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A3D676B008A; Fri, 1 Jul 2022 10:24:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8DDD96B008C; Fri, 1 Jul 2022 10:24:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 7B4536B0089 for ; Fri, 1 Jul 2022 10:24:02 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 5725335AEE for ; Fri, 1 Jul 2022 14:24:02 +0000 (UTC) X-FDA: 79638750324.21.F5B07B5 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf13.hostedemail.com (Postfix) with ESMTP id ACDDA20050 for ; Fri, 1 Jul 2022 14:24:01 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id y18-20020a056402441200b0043564cdf765so1891212eda.11 for ; Fri, 01 Jul 2022 07:24:01 -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=MuviuQokEzhvfg2Z0+25mg/6MRU0laLu6ns1H0kafhg=; b=RS/M11ViQBiSN+sd81NPLXtCXBjB0FGsgDbLZu3p8F4GXPk0D+7t81g6tlYzjC+YwJ 2W2SuOsDI6B6k+EbyeqLWQ5i6XoSdBB/+r1g2aJs+YxxhMsErmeKZJZA7jqnG5QhyArd kCQL6luWnGxlNT+6WLZVxu+1AO53PlCajPc408xi4cgug6IuYEuGFoJhtwpZh92pjpFi LUqCR9BfPmYNdTfDQrXf+Z9/vBRTPln7cJAJKvCI/ZLobPZOQV/2MT0+aDpNak9Etgjo y4ufCn34FXG2xJ2ki47OVbFCxLHBCiQiG9yCQJMgCp4FSLz2CJ4PyO19DAZvw0qWc0JD PPNw== 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=MuviuQokEzhvfg2Z0+25mg/6MRU0laLu6ns1H0kafhg=; b=NpxVugu9GyQ7lxli0o45Y8c39kHBgSebmaVD7hgdU7KquSc1AvHy4KR7CsBdADs7/p XY6Yid5Lugs8I609pkSqlaUAyUp5DaFp3E+VwhCm78DVABmA72zHKogRZ2rBCOnXC+v3 viKX4lFpK4/JClGeOX/a0GJjjaBpimDLsGmOynY6dJvK2j8ILN3t/6ImkOivQdsj9mIR GQTB9jt/xUjgf2baj6OEEtUt42eKmUBUNTIzR1ewwJEGRkY9IFHJtWVrkJ9xNEECf+hI 4/Ln9zTz7/jytw0tq38R+mvt5UE35LpI2awUeThhBDNieQaCnI0dkjH8SRyYh1REqy3L 2oyw== X-Gm-Message-State: AJIora87PHf3OW5DcaJPXMOw2IMRxhNL+aknMYFf0chi5xJXhHr7oxQf upO9nxc95ddigNStvzRlEla2+teGVQ4= X-Google-Smtp-Source: AGRyM1svvXk0uhgtL2lyHdhsMHh+dNlSgp0MiLj8ryXpF1AJlade1jJus8xhDzqNuinjfAclnUPFVASuizY= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:a6f5:f713:759c:abb6]) (user=glider job=sendgmr) by 2002:a05:6402:430a:b0:435:8ec9:31ec with SMTP id m10-20020a056402430a00b004358ec931ecmr19482900edc.248.1656685440344; Fri, 01 Jul 2022 07:24:00 -0700 (PDT) Date: Fri, 1 Jul 2022 16:22:41 +0200 In-Reply-To: <20220701142310.2188015-1-glider@google.com> Message-Id: <20220701142310.2188015-17-glider@google.com> Mime-Version: 1.0 References: <20220701142310.2188015-1-glider@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v4 16/45] kmsan: handle task creation and exiting From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1656685441; a=rsa-sha256; cv=none; b=R4vqrBdXgwISQbJ35nEJfpGxKgFQFXKgUEkKETn6coThDnYO3nqj5i6vsvA64xsT9LqGAa 8K2a/qriTx3VMyB5hqp5UBgQCOzlbinFcChBAqz+VYV6OCGkRleSp/xsiMig5XNP/X7gaw tktkWCmAeGcnw5QDzrabnwxSVZxUrwE= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="RS/M11Vi"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3gAO_YgYKCJ8FKHCDQFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3gAO_YgYKCJ8FKHCDQFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1656685441; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MuviuQokEzhvfg2Z0+25mg/6MRU0laLu6ns1H0kafhg=; b=I8ilOvnxqc0bk1UCwpPH5FhFth5zV5seQFmlDbNPce0RIAju09HO+rE3gSTIT9wGUJfI0y CcmuXnhILwwJNKMxBpfudA0vzwQEjsEHOWoM5213fJ4vqkAffqwR7w2wUKZgb/cHAOWe+9 xNvuJP4JrKIAp4pupO2c3X2BNqpmNGc= X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: ACDDA20050 Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="RS/M11Vi"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3gAO_YgYKCJ8FKHCDQFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3gAO_YgYKCJ8FKHCDQFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--glider.bounces.google.com X-Rspam-User: X-Stat-Signature: zeqm87iyjah77th3eqe577ipxhsizmun X-HE-Tag: 1656685441-172866 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 v4: -- change sizeof(type) to sizeof(*ptr) 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 fd76cea338878..b71e2032222e9 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -16,6 +16,7 @@ struct page; struct kmem_cache; +struct task_struct; #ifdef CONFIG_KMSAN @@ -42,6 +43,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(). @@ -163,6 +172,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 9d44f2d46c696..6dfca6f00ec82 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -1026,6 +1027,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 16fb8880a9c6d..7eabed03ed10b 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; + struct thread_info *info = current_thread_info(); + + __memset(ctx, 0, sizeof(*ctx)); + ctx->allow_reporting = true; + kmsan_internal_unpoison_memory(info, sizeof(*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 d3c400ca097ba..c7fb8666607e2 100644 --- a/mm/kmsan/kmsan.h +++ b/mm/kmsan/kmsan.h @@ -179,6 +179,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);