From patchwork Fri Aug 26 15:07:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956195 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 172EFECAAD4 for ; Fri, 26 Aug 2022 15:09:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A804494000D; Fri, 26 Aug 2022 11:09:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A0782940007; Fri, 26 Aug 2022 11:09:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8826394000D; Fri, 26 Aug 2022 11:09:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 763B8940007 for ; Fri, 26 Aug 2022 11:09:17 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 46545C0E50 for ; Fri, 26 Aug 2022 15:09:17 +0000 (UTC) X-FDA: 79842077154.16.91EAE7F Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf01.hostedemail.com (Postfix) with ESMTP id 711EF4001D for ; Fri, 26 Aug 2022 15:09:15 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id c14-20020a05640227ce00b0043e5df12e2cso1236774ede.15 for ; Fri, 26 Aug 2022 08:09:15 -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; bh=76jHGgwozZvM48bVgyafH0ePqqgPygQeeBv9QE8zFSs=; b=Ez/lNwvAaDDeJuJMLhCTbPEpOPlHhfNIq7VyjE0qAVyZCRontZv0oB0LtGJ+nv4bwC gZw/mTwPTBWyA0dCxVGdhKH7XEOHdENq7Cv0XhQdf86eksUUxOa2+VWsVcz6rEA4dWpu Zvs62QVT+xB2cN1avkjLXHEoEa+e1ekxcYkU9ofidPy0VrkaWYbMd4GmDWtb/KlvsLO2 yXZ6UmXxmwAic31Rzt9UwrAuioHtXeVlWsWj3qGlJA79GCkFWZNr2tEMTQpyVXsI1d7b 0KcOCYL7oQy7CjEb6pflswFigH5TXa8Rfml+4QXtapWs+MN75E6ySnIaQBQTkmU2kvfY TSxg== 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; bh=76jHGgwozZvM48bVgyafH0ePqqgPygQeeBv9QE8zFSs=; b=ePSeg4n8MmkI+KoSH6FMLLak0ULm8WMzP7GLDrYaScs6KUzv+YPnGxGLng6d6dVEbG WDKKiPESzsMiXz8Jn1GfRF4P4mNY3ZR12t2DZFF1VDqtqrspvGYsvzPcpL69OnOe6ocO 01LIKhp2X2ktKSjsKemxaKDe98/GzEP+WfTjgiHS1RrMXcbqSQ2tILPFYShPlBYw/Y/n ez/vwvZqK6F5CtI4trkQuLIEHD8u+2737LRoTbP8rRUxP/60LUDJuXK5o4AkFRbl9RtK lK5BM6vxexzDi6TWnMVO5ZdSzNzJEzJkQfVoNH6WMIF1MlxQ7DoebLt2jg8OHGrz6Cij UADA== X-Gm-Message-State: ACgBeo3VtCMVzioDOQ3OUlt7AknwXPFV4tp639OcIeSKu+IpeT5+Uqh4 qElFZe+f10TNhQIFBZ7DdvCUdf9Byyo= X-Google-Smtp-Source: AA6agR6bf5SCba3ePQHoJhycDuv14GdKQb0ZRH0Sro37EO5apbOGfCrAwn2LLTqoAO1tZW/O5CJdylcsL78= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:906:8458:b0:73d:d0e9:4b27 with SMTP id e24-20020a170906845800b0073dd0e94b27mr5257444ejy.766.1661526554281; Fri, 26 Aug 2022 08:09:14 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:45 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-23-glider@google.com> Subject: [PATCH v5 22/44] 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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526555; a=rsa-sha256; cv=none; b=pNCYRH7dxy2BDKkCLdf85Hq4zYK2w81RooSsHObLIu+gBXgkZCiQBrssGLEqjm5+60Wvou ErdAVGc07sr+UGVwb98tbeN6h/snIy4kbE3XITZeI88CsdpdW2l6ixloujannhzmMotC/N ZhigzoV1+Yuz/oJeI7ua9sckC6KJrFY= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="Ez/lNwvA"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3GuIIYwYKCCEDIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3GuIIYwYKCCEDIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526555; 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=76jHGgwozZvM48bVgyafH0ePqqgPygQeeBv9QE8zFSs=; b=EJ4GFxtGQ7+shJpsJNUYxLQyLXt1+oixIJRTZA9fO8RWVx6+VnyA1VlJDkRtuNA5c8d7Vw EHc5hveLjpl+MCqieQzOssB/FGX05V0xeZYqgVsyCLDWgfwwMZMCyEeAJOEdWXc0OrxDCA SAzT5nSNJCf2seom4kYgw0jIY2rVvN8= X-Rspamd-Queue-Id: 711EF4001D Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="Ez/lNwvA"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3GuIIYwYKCCEDIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3GuIIYwYKCCEDIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@flex--glider.bounces.google.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: 5jj4s8ys3dhroufoiitd316uiymuz3cb X-HE-Tag: 1661526555-581171 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 v5: -- do not export KMSAN hooks that are not called from modules Link: https://linux-review.googlesource.com/id/Ia162dc4c5a92e74d4686c1be32a4dfeffc5c32cd --- include/linux/kmsan.h | 41 ++++++++++++++++++++++++++++++ kernel/dma/mapping.c | 9 ++++--- mm/kmsan/hooks.c | 59 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index f056ba8a7a551..c6ae00e327e5e 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 @@ -196,6 +198,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) @@ -278,6 +309,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 49cbf3e33de71..48dfd11807be2 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 && ents != -EREMOTEIO)) + } else if (WARN_ON_ONCE(ents != -EINVAL && ents != -ENOMEM && + ents != -EIO && ents != -EREMOTEIO)) { return -EIO; + } return ents; } diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index a8a03f079a8a5..41b6b41e6183a 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -10,9 +10,11 @@ */ #include +#include #include #include #include +#include #include #include @@ -242,6 +244,63 @@ 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; + } +} + +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); +} + /* Functions from kmsan-checks.h follow. */ void kmsan_poison_memory(const void *address, size_t size, gfp_t flags) {