From patchwork Thu Sep 15 15:03:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977534 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 5118BC6FA89 for ; Thu, 15 Sep 2022 15:05:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 737FF80012; Thu, 15 Sep 2022 11:05:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 712218000C; Thu, 15 Sep 2022 11:05:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5622080012; Thu, 15 Sep 2022 11:05:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 481798000C for ; Thu, 15 Sep 2022 11:05:24 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2D0421C610B for ; Thu, 15 Sep 2022 15:05:24 +0000 (UTC) X-FDA: 79914643368.18.FFD8874 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf18.hostedemail.com (Postfix) with ESMTP id C25751C00AC for ; Thu, 15 Sep 2022 15:05:23 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id w20-20020a05640234d400b00450f24c8ca6so13150490edc.13 for ; Thu, 15 Sep 2022 08:05:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=uEBXmIOJ/jVJ62g3MgOZ1PWj/PPm6Zng9ZXCGF8oCGQ=; b=d96TG3Hxv6sIG0D9GCl0RiC+uFhT+JLVK6sevFTTg2cGiOmsNKa0agzQa6C6xzYpgB KrER5gjEWJxrUVaZFexycRUvOwztcaivd+5Ql+JyY3mBj0Ia9jZkUxSEK4PULX7m/EV6 XWACNEeJZDhm241z1k7FPfy66U38FvMA/gORPAoDPIW41/YFRIqK1uT63SaSHtWW0R0x q4TO7avKYTsOs8uhvI820+846g4n5hZBN5ENyr4KQ8lx22/A9EtRnuyzFUnVUEdfkMx/ sfWQuT3tK6ZVt0bBZuCbUoQf0lkYauyM3gSJyZgZsuanpkHtxDrS16OPGAfrdP3wp1IT m86g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=uEBXmIOJ/jVJ62g3MgOZ1PWj/PPm6Zng9ZXCGF8oCGQ=; b=Dg0MX8Sntxbd8NSCirPPKMGfbloajvAaRXsNBOd7JvClVfLzfxdxtbuh6jJqMoaBFP Aro5Zwss7US2SkX88M7oighjn4qH6RxhK9gr7J98z6Ahn+1/HjrqRnxbH9dnJMtMtcHc zlSO1cNbCTB0jMvB3/IaD6ftN6eeLQMsUVm5J+zuE8SJC8JMvdyca5gltxyAWTj73ZNo dlgoEjwY8CEI/nuOhN7+DiTMJQlahYMESMt4JC6A2lP/SsdCfKW+c6SashMEnlf0mm/l r3eMA6hvS2rsRRuI6hL1bNqqlW9yrOsXk56QrEvYFpdo/zKR0THBKXE1PF6m2EMlP0k5 lNZQ== X-Gm-Message-State: ACrzQf1Oz/YtRd9c6EhDAbn8Hu4dUrkA2TJ6SFr3gXS5lmZBl1QYzSEz RHosKt/mpn6ZMmz3mSx8si2QHGN/DuA= X-Google-Smtp-Source: AMsMyM4p9q4a8kwSPLQHupWtxmE8sSfLUWhBeeePGlfGtCaz1sm2ZmFR06/jzES7F9GL5jloyYk/aerrGvE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:907:7215:b0:780:3153:cca2 with SMTP id dr21-20020a170907721500b007803153cca2mr279565ejc.427.1663254322378; Thu, 15 Sep 2022 08:05:22 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:50 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-17-glider@google.com> Subject: [PATCH v7 16/43] 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 Biggers , 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 , Stephen Rothwell , 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-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=d96TG3Hx; spf=pass (imf18.hostedemail.com: domain of 3Mj8jYwYKCF0BGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Mj8jYwYKCF0BGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254323; a=rsa-sha256; cv=none; b=DWNFL3TMH3/BxqZJ8RZST314PO0a9WMMuUpZhw0SBe+vWnhEzdXCubnqoj8esDL6qCrMYj wlCMtSfiOvQO1B/1KfFSXeJfzoZZ/BItCosjEfUvH+BCrM7RBqlpNBO9MNU4yHVTfz9uvJ Z5zgODeZPQ8MdMZMtQuAdYvrokauFR8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254323; 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=uEBXmIOJ/jVJ62g3MgOZ1PWj/PPm6Zng9ZXCGF8oCGQ=; b=kOcVGwSLF84IZoexldh5eAdb9AGf7SQ6sTlJNZ6Jb3zbJK01mGSagMBiwVbt8yQBGvXpdC aNEJNedaEUWi/eX3o9UMmTn7o6sVf7FAtaOd3iDN2cVRb0cqaxZWcRD3UWz1QJb9Spx1Ha gJDfq3b6sK4OBoNlhT145Uo0BMGS4xo= X-Rspam-User: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C25751C00AC Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=d96TG3Hx; spf=pass (imf18.hostedemail.com: domain of 3Mj8jYwYKCF0BGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3Mj8jYwYKCF0BGD89MBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: pxmygck85efohj6cjciypr7adg1r7955 X-HE-Tag: 1663254323-318034 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) v5: -- do not export KMSAN hooks that are not called from modules -- minor comment fix Link: https://linux-review.googlesource.com/id/I0f41c3a1c7d66f7e14aabcfdfc7c69addb945805 --- include/linux/kmsan.h | 21 +++++++++++++++++++++ kernel/exit.c | 2 ++ kernel/fork.c | 2 ++ mm/kmsan/core.c | 10 ++++++++++ mm/kmsan/hooks.c | 17 +++++++++++++++++ mm/kmsan/kmsan.h | 2 ++ 6 files changed, 54 insertions(+) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index 5c4e0079054e6..354aee6f7b1a2 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -15,9 +15,22 @@ struct page; struct kmem_cache; +struct task_struct; #ifdef CONFIG_KMSAN +/** + * kmsan_task_create() - Initialize KMSAN state for the task. + * @task: task to initialize. + */ +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(). @@ -139,6 +152,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 84021b24f79e3..f5d620c315662 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 8a9e92068b150..a438f5ee3aed5 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 5330138fda5bc..112dce135c7f6 100644 --- a/mm/kmsan/core.c +++ b/mm/kmsan/core.c @@ -37,6 +37,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 000703c563a4d..6f3e64b0b61f8 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -27,6 +27,23 @@ * 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(); +} + +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; +} + 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 97d48b45dba58..77ee068c04ae9 100644 --- a/mm/kmsan/kmsan.h +++ b/mm/kmsan/kmsan.h @@ -180,6 +180,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);