From patchwork Fri Jul 1 14:22:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12903379 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 7E0B3C43334 for ; Fri, 1 Jul 2022 14:24:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 166826B0071; Fri, 1 Jul 2022 10:24:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 116A56B007E; Fri, 1 Jul 2022 10:24:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F1FAA6B008C; Fri, 1 Jul 2022 10:24:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E37EA6B0071 for ; Fri, 1 Jul 2022 10:24:18 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id C0BB280856 for ; Fri, 1 Jul 2022 14:24:18 +0000 (UTC) X-FDA: 79638750996.30.B8B2C75 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf17.hostedemail.com (Postfix) with ESMTP id 4ACBB40044 for ; Fri, 1 Jul 2022 14:24:18 +0000 (UTC) Received: by mail-ej1-f74.google.com with SMTP id hp8-20020a1709073e0800b0072629757566so852346ejc.0 for ; Fri, 01 Jul 2022 07:24:18 -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=q/n/OecEQokjv5vk7nJWmgO+y+lMcJIuX+yK+Jk36kg=; b=U5LnbAq/BfGg73b3hAPk1LGNT654UNP9cra9ckGKhN/P0fyT5dmbNa1GwGMC/VQSau kRRACO1kwseZjFl8pDjCJnqdw+CVsYmJalihZ0BfiiWO66sGbSWS/QGcsD8uZTf0fZVy 6eRMZPVngQrj635M05w5qDdJQQzaIckJ8YJ7a9RY76OdD8EzT31PanbT0T3p4KWXGYQF +qmU1PTyJ4IBn8XSLLL2PEetWgBrtk9fkaI3OUzV7+LCeZNWoyFZA/kpqxczFaWlH/25 jGYWS+j/JNFVmhC3ACDNxjLd8Gs1XS5BL6t/D7d5YG8xFOVszZWJNMj5ExZnAIg3/Kxj YuYA== 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=q/n/OecEQokjv5vk7nJWmgO+y+lMcJIuX+yK+Jk36kg=; b=WgIdUBshY52ypZ9GLgeKbSSiBeLa7KAiD4WppX06R8JAsoiW4NEB3ih+Fxdr5xefLd 2EJsT630ppxcYapptbZn2cXV7ymR6V8WVjVRodpqF/hL+EYbH2iryiug8rV9cGrv+v+9 uCHiOPfxgLKy4YWmFB7tUPn7CgJMKY1khUmZ1BXlhQznZrXEBgPDXyhDXzcITB8isPhH qH6hGvYDW/GzYKmGyk+dezMS1r4+Yh8vhsVp0z1pFUasMVPBjOmTJY8hDh9pWcWvYfJr HsHKhJGNcRtxBIRpxEtJNHb++XB1aTyX5g/eSbbEqpnRKIpP1YVxqwWBIs2ebvzpdFO/ veBg== X-Gm-Message-State: AJIora84JA9014VJ3U3kM40jORwkvXZlfaZRrfqv1JF3BqBe7PHQ7+EZ ym8UTXHuNM6Qmp00szdkNu5uQyX88pg= X-Google-Smtp-Source: AGRyM1vh1xuSpd10aw8ZdO/pImp9DeAVweQvga8OQDtjXKfbnui60mltRkNXb/YJK5XPxxAY6bzLEimCmls= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:a6f5:f713:759c:abb6]) (user=glider job=sendgmr) by 2002:a05:6402:f1b:b0:436:d3c4:aef2 with SMTP id i27-20020a0564020f1b00b00436d3c4aef2mr19579345eda.27.1656685457025; Fri, 01 Jul 2022 07:24:17 -0700 (PDT) Date: Fri, 1 Jul 2022 16:22:47 +0200 In-Reply-To: <20220701142310.2188015-1-glider@google.com> Message-Id: <20220701142310.2188015-23-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 22/45] dma: kmsan: unpoison DMA mappings 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-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="U5LnbAq/"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3kQO_YgYKCLAWbYTUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3kQO_YgYKCLAWbYTUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1656685458; a=rsa-sha256; cv=none; b=6w/8/770L2GDAZsUSDUsnnlEfuHl/SBEEwbXO43Tr2rB2ZNpBKrS0vZ2TJiwE3yZvGWL/r Tx9BQF3bcUP+bcgRMZezrQZP6doGH5a0/B7RKrIQvKbKShyok+AThP21PhTN/HLy5gjAjp y0XzYFfIc4cqlaojL61jP/AuicX6Lew= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1656685458; 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=q/n/OecEQokjv5vk7nJWmgO+y+lMcJIuX+yK+Jk36kg=; b=wSvPZU5q9SLrb609UeVFJRrZbFtvcLkm+ifraGrwzpUdL0ob89ZFdKrtL2ga+m0S1S15b+ Kz5EH0ztF9pTKZNsQRPyvWgkY44dgx7HxL6IgwH86icni8gLpQnHCRGrKSkOmvcgmdWLb+ KkomUBj8Fs6yc0W+jPS7XPOi3qlciJc= X-Stat-Signature: sebx1nxk7z6ofk45trz4mr8zn793b5m8 X-Rspamd-Queue-Id: 4ACBB40044 Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="U5LnbAq/"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3kQO_YgYKCLAWbYTUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--glider.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3kQO_YgYKCLAWbYTUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--glider.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1656685458-335563 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: KMSAN doesn't know about DMA memory writes performed by devices. We unpoison such memory when it's mapped to avoid false positive reports. Signed-off-by: Alexander Potapenko --- v2: -- move implementation of kmsan_handle_dma() and kmsan_handle_dma_sg() here v4: -- swap dma: and kmsan: int the subject Link: https://linux-review.googlesource.com/id/Ia162dc4c5a92e74d4686c1be32a4dfeffc5c32cd --- include/linux/kmsan.h | 41 +++++++++++++++++++++++++++++ kernel/dma/mapping.c | 9 ++++--- mm/kmsan/hooks.c | 61 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 3 deletions(-) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index 82fd564cc72e7..55fe673ee1e84 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -9,6 +9,7 @@ #ifndef _LINUX_KMSAN_H #define _LINUX_KMSAN_H +#include #include #include #include @@ -17,6 +18,7 @@ struct page; struct kmem_cache; struct task_struct; +struct scatterlist; #ifdef CONFIG_KMSAN @@ -204,6 +206,35 @@ void kmsan_ioremap_page_range(unsigned long addr, unsigned long end, */ void kmsan_iounmap_page_range(unsigned long start, unsigned long end); +/** + * kmsan_handle_dma() - Handle a DMA data transfer. + * @page: first page of the buffer. + * @offset: offset of the buffer within the first page. + * @size: buffer size. + * @dir: one of possible dma_data_direction values. + * + * Depending on @direction, KMSAN: + * * checks the buffer, if it is copied to device; + * * initializes the buffer, if it is copied from device; + * * does both, if this is a DMA_BIDIRECTIONAL transfer. + */ +void kmsan_handle_dma(struct page *page, size_t offset, size_t size, + enum dma_data_direction dir); + +/** + * kmsan_handle_dma_sg() - Handle a DMA transfer using scatterlist. + * @sg: scatterlist holding DMA buffers. + * @nents: number of scatterlist entries. + * @dir: one of possible dma_data_direction values. + * + * Depending on @direction, KMSAN: + * * checks the buffers in the scatterlist, if they are copied to device; + * * initializes the buffers, if they are copied from device; + * * does both, if this is a DMA_BIDIRECTIONAL transfer. + */ +void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, + enum dma_data_direction dir); + #else static inline void kmsan_init_shadow(void) @@ -286,6 +317,16 @@ static inline void kmsan_iounmap_page_range(unsigned long start, { } +static inline void kmsan_handle_dma(struct page *page, size_t offset, + size_t size, enum dma_data_direction dir) +{ +} + +static inline void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, + enum dma_data_direction dir) +{ +} + #endif #endif /* _LINUX_KMSAN_H */ diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index db7244291b745..5d17d5d62166b 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -156,6 +156,7 @@ dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page, addr = dma_direct_map_page(dev, page, offset, size, dir, attrs); else addr = ops->map_page(dev, page, offset, size, dir, attrs); + kmsan_handle_dma(page, offset, size, dir); debug_dma_map_page(dev, page, offset, size, dir, addr, attrs); return addr; @@ -194,11 +195,13 @@ static int __dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, else ents = ops->map_sg(dev, sg, nents, dir, attrs); - if (ents > 0) + if (ents > 0) { + kmsan_handle_dma_sg(sg, nents, dir); debug_dma_map_sg(dev, sg, nents, ents, dir, attrs); - else if (WARN_ON_ONCE(ents != -EINVAL && ents != -ENOMEM && - ents != -EIO)) + } else if (WARN_ON_ONCE(ents != -EINVAL && ents != -ENOMEM && + ents != -EIO)) { return -EIO; + } return ents; } diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 1cdb4420977f1..8a6947a2a2f22 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -10,9 +10,11 @@ */ #include +#include #include #include #include +#include #include #include @@ -250,6 +252,65 @@ void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy, } EXPORT_SYMBOL(kmsan_copy_to_user); +static void kmsan_handle_dma_page(const void *addr, size_t size, + enum dma_data_direction dir) +{ + switch (dir) { + case DMA_BIDIRECTIONAL: + kmsan_internal_check_memory((void *)addr, size, /*user_addr*/ 0, + REASON_ANY); + kmsan_internal_unpoison_memory((void *)addr, size, + /*checked*/ false); + break; + case DMA_TO_DEVICE: + kmsan_internal_check_memory((void *)addr, size, /*user_addr*/ 0, + REASON_ANY); + break; + case DMA_FROM_DEVICE: + kmsan_internal_unpoison_memory((void *)addr, size, + /*checked*/ false); + break; + case DMA_NONE: + break; + } +} + +/* Helper function to handle DMA data transfers. */ +void kmsan_handle_dma(struct page *page, size_t offset, size_t size, + enum dma_data_direction dir) +{ + u64 page_offset, to_go, addr; + + if (PageHighMem(page)) + return; + addr = (u64)page_address(page) + offset; + /* + * The kernel may occasionally give us adjacent DMA pages not belonging + * to the same allocation. Process them separately to avoid triggering + * internal KMSAN checks. + */ + while (size > 0) { + page_offset = addr % PAGE_SIZE; + to_go = min(PAGE_SIZE - page_offset, (u64)size); + kmsan_handle_dma_page((void *)addr, to_go, dir); + addr += to_go; + size -= to_go; + } +} +EXPORT_SYMBOL(kmsan_handle_dma); + +void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, + enum dma_data_direction dir) +{ + struct scatterlist *item; + int i; + + for_each_sg(sg, item, nents, i) + kmsan_handle_dma(sg_page(item), item->offset, item->length, + dir); +} +EXPORT_SYMBOL(kmsan_handle_dma_sg); + /* Functions from kmsan-checks.h follow. */ void kmsan_poison_memory(const void *address, size_t size, gfp_t flags) {