From patchwork Thu Apr 13 13:12:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13210281 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 30359C77B76 for ; Thu, 13 Apr 2023 13:12:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9634900005; Thu, 13 Apr 2023 09:12:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B462A900002; Thu, 13 Apr 2023 09:12:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A3553900005; Thu, 13 Apr 2023 09:12:30 -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 953CA900002 for ; Thu, 13 Apr 2023 09:12:30 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3AD65401DD for ; Thu, 13 Apr 2023 13:12:30 +0000 (UTC) X-FDA: 80676406860.10.35E6589 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf22.hostedemail.com (Postfix) with ESMTP id 5D9B0C0011 for ; Thu, 13 Apr 2023 13:12:28 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=eFVXlBPl; spf=pass (imf22.hostedemail.com: domain of 3u_83ZAYKCMIotqlmzowwotm.kwutqv25-uus3iks.wzo@flex--glider.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3u_83ZAYKCMIotqlmzowwotm.kwutqv25-uus3iks.wzo@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681391548; 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: references:dkim-signature; bh=akxBw8gEREuaBtxzA9qIYFhL4FrjZkaJp5ikMOEw/lQ=; b=GDSuu5LtpTrF06g/qY+0uKE3HNSHM8RO+XyD4W3nFhQ6My3p6nK4ilG/LI5/ZB99fR/iH1 ukGLMxBVFzTLY1BFl+jjGLLIJWQ+gfwlX877dybtrQ1Le7sBwjMuuyP8H+nYHCM6eitINo fsiGFZsk7P3XJg+tSDiRz1gwT6iyinI= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=eFVXlBPl; spf=pass (imf22.hostedemail.com: domain of 3u_83ZAYKCMIotqlmzowwotm.kwutqv25-uus3iks.wzo@flex--glider.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3u_83ZAYKCMIotqlmzowwotm.kwutqv25-uus3iks.wzo@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681391548; a=rsa-sha256; cv=none; b=40jebZoMU89OPBvpw/fXnc4VSarzFs1IHu4zM+g81xBVtlXZVx4mESANVRRQmTXuXSX6lE /ygrwrY7x1PxCxJJhu6Uv0qgpVOpKXBdBq72dLR4OwtnxPWoobyzVFp7IVn5jWksIJxS8P 9xyMHiMUXWYWBO166xN6PaUqqLJEmKA= Received: by mail-yb1-f201.google.com with SMTP id c193-20020a25c0ca000000b00b868826cdfeso33906557ybf.0 for ; Thu, 13 Apr 2023 06:12:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681391547; x=1683983547; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=akxBw8gEREuaBtxzA9qIYFhL4FrjZkaJp5ikMOEw/lQ=; b=eFVXlBPlk8KkE4XTi3JZp57snk+KevAS/0AAEO8KUQKSbtlezEhHzl7gM28PRSANsQ xFi584iPfUhEI0ZAWBsfzgePMg6oXen48McBe5VCcz3DplJ9+JeZD8wEHuwXfb7eY+aA THak+NvZaFjvTd+yrsI8BUdRjts/e+TlenEWbeB/fSl1rVQuPhafFd1hHa62VJ5VBcge QATR+oYNfLbD6vAsZk5H8AvFSiI2ZPTTeghUDlHrPmD8jGdiFgPB+XAQN5P+oGv4bkuy TaaeUlNPKKBSXPBJH748wRB30XE0ZCS7+rSKmlOZquvzOsUXj2WzRZH+HEGv0+px1Ego 21RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681391547; x=1683983547; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=akxBw8gEREuaBtxzA9qIYFhL4FrjZkaJp5ikMOEw/lQ=; b=XV9J9ads9IimxIyo9GNpdWOwslaT7BQ4WozYUvhVQUIKxNDJvpMXsIy7OXZxIrCIbT Asm6FD52o3Yek6x2i+x23fl0yLa/6SHxZb+wIJRZnoPv4Q7bSIEUfMx7esIeP+yB8kjW 3vq525Gv4+rIsey2EEMsB0fXCPRAwTnxwZuevKfEeKrgFv0UbFhUbNP2G6XZixD6sLZK OdKfvUa+7xLhdByTvo9rMn9bG6i8f1n7AwvgqxeQr11BVlg7uiVyX1w6GmWXxRhy9nll mVzBfYvc/li3gyaoWmiAT3nwJWcthgoI7axA2vA+hCoNOIes3pC/ND7HAURizepx670u cMFg== X-Gm-Message-State: AAQBX9eV4NerAg7I29hQPo8pe2AA6YA+Eh6OzTjHiSir0RsHjG+6UJDK oIUk4h8JBIaIOYHRvW4jSxZ3c/zRlCY= X-Google-Smtp-Source: AKy350YiiGBZ9G/MU1vaW002Sqg0JV5Azs5DqEu339HBwPrg/oAX3rcUTYgYybyELrzyBffUmUNxtdYNiGA= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:eb2b:4d7d:1d7f:9316]) (user=glider job=sendgmr) by 2002:a81:4319:0:b0:545:62cb:3bcf with SMTP id q25-20020a814319000000b0054562cb3bcfmr1362388ywa.2.1681391547580; Thu, 13 Apr 2023 06:12:27 -0700 (PDT) Date: Thu, 13 Apr 2023 15:12:20 +0200 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.577.gac1e443424-goog Message-ID: <20230413131223.4135168-1-glider@google.com> Subject: [PATCH v2 1/4] mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush() From: Alexander Potapenko To: glider@google.com Cc: urezki@gmail.com, hch@infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, elver@google.com, dvyukov@google.com, kasan-dev@googlegroups.com, Dipanjan Das X-Stat-Signature: puo7zop7ssg36nzmryjdubde8rfjm7n6 X-Rspam-User: X-Rspamd-Queue-Id: 5D9B0C0011 X-Rspamd-Server: rspam06 X-HE-Tag: 1681391548-573703 X-HE-Meta: U2FsdGVkX1/yIJ4QsPaB9T87BRP9ljIb1BuijLi0ovZtMEBZcmBq+r040WrK1xSmqKuG7++WPRShL/+wGr6Zjtcak7zgGB+A3E8upy12rG2rvBcf96eXz4xWNLSb3hfucdwSgS3nl1NI2Fx+XWThNXTsmWF1KV0pTQzoyH0oE3/UBVndNEen9TTydl/6XAq2PknoiAjW1vj/y7gE9nwZriumXi6AGFr4xV0JyVY0YKixFqbo+He0kWiygipWdR1tXTjv97nT9i5bQNZddvmXWlydnEmfeYxqQ/57aQ2q9wHOML1yw3IDgV1H0zjys/iSWpzghex5Pal10sb9ldmu8ygyCybsTMfEXJ5sQNg8BGetWGBAq+FcEQoJafufuUwiFfU26UqgGyBKw+ZQxdkRcBxYKRupYiwTTo7GgUCjXEtZ4FUaRnZbR8RcfYWF197ucWa2x1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gMk8FrufatWOJ+FZztOM2gqzkUmB6xBwOCezcgPX64FHtc1rbINnvsAiA42AnlNP1wJLMRXT57NfBbqgVwEs4ZRJ1p6x0RMy5Z81lGMq6ILBT7pfrJ47wjYnctpN1TtLFIDGryC5dOeoopNv8rUCdREdafBDXKs/FtsjdJaWmRhaqMymyYAwCZh6XIib0SycvF0Ch9B41Ozh2xRIAYdkU/5ZMvD9x2A4DyKj5U4kSKmSl25jQjfjRZYetfXp6RmBcq0zEa8SBleVwEp4OKbdXVDj6A5yg0CBjqDPDQMA1uitoJGIhMPSAt0g== 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: As reported by Dipanjan Das, when KMSAN is used together with kernel fault injection (or, generally, even without the latter), calls to kcalloc() or __vmap_pages_range_noflush() may fail, leaving the metadata mappings for the virtual mapping in an inconsistent state. When these metadata mappings are accessed later, the kernel crashes. To address the problem, we return a non-zero error code from kmsan_vmap_pages_range_noflush() in the case of any allocation/mapping failure inside it, and make vmap_pages_range_noflush() return an error if KMSAN fails to allocate the metadata. This patch also removes KMSAN_WARN_ON() from vmap_pages_range_noflush(), as these allocation failures are not fatal anymore. Reported-by: Dipanjan Das Link: https://lore.kernel.org/linux-mm/CANX2M5ZRrRA64k0hOif02TjmY9kbbO2aCBPyq79es34RXZ=cAw@mail.gmail.com/ Fixes: b073d7f8aee4 ("mm: kmsan: maintain KMSAN metadata for page operations") Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- v2: -- return 0 from the inline version of kmsan_vmap_pages_range_noflush() (spotted by kernel test robot ) --- include/linux/kmsan.h | 20 +++++++++++--------- mm/kmsan/shadow.c | 27 ++++++++++++++++++--------- mm/vmalloc.c | 6 +++++- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index e38ae3c346184..c7ff3aefc5a13 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -134,11 +134,12 @@ void kmsan_kfree_large(const void *ptr); * @page_shift: page_shift passed to vmap_range_noflush(). * * KMSAN maps shadow and origin pages of @pages into contiguous ranges in - * vmalloc metadata address range. + * vmalloc metadata address range. Returns 0 on success, callers must check + * for non-zero return value. */ -void kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, - pgprot_t prot, struct page **pages, - unsigned int page_shift); +int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, + pgprot_t prot, struct page **pages, + unsigned int page_shift); /** * kmsan_vunmap_kernel_range_noflush() - Notify KMSAN about a vunmap. @@ -281,12 +282,13 @@ static inline void kmsan_kfree_large(const void *ptr) { } -static inline void kmsan_vmap_pages_range_noflush(unsigned long start, - unsigned long end, - pgprot_t prot, - struct page **pages, - unsigned int page_shift) +static inline int kmsan_vmap_pages_range_noflush(unsigned long start, + unsigned long end, + pgprot_t prot, + struct page **pages, + unsigned int page_shift) { + return 0; } static inline void kmsan_vunmap_range_noflush(unsigned long start, diff --git a/mm/kmsan/shadow.c b/mm/kmsan/shadow.c index a787c04e9583c..b8bb95eea5e3d 100644 --- a/mm/kmsan/shadow.c +++ b/mm/kmsan/shadow.c @@ -216,27 +216,29 @@ void kmsan_free_page(struct page *page, unsigned int order) kmsan_leave_runtime(); } -void kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, - pgprot_t prot, struct page **pages, - unsigned int page_shift) +int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, + pgprot_t prot, struct page **pages, + unsigned int page_shift) { unsigned long shadow_start, origin_start, shadow_end, origin_end; struct page **s_pages, **o_pages; - int nr, mapped; + int nr, mapped, err = 0; if (!kmsan_enabled) - return; + return 0; shadow_start = vmalloc_meta((void *)start, KMSAN_META_SHADOW); shadow_end = vmalloc_meta((void *)end, KMSAN_META_SHADOW); if (!shadow_start) - return; + return 0; nr = (end - start) / PAGE_SIZE; s_pages = kcalloc(nr, sizeof(*s_pages), GFP_KERNEL); o_pages = kcalloc(nr, sizeof(*o_pages), GFP_KERNEL); - if (!s_pages || !o_pages) + if (!s_pages || !o_pages) { + err = -ENOMEM; goto ret; + } for (int i = 0; i < nr; i++) { s_pages[i] = shadow_page_for(pages[i]); o_pages[i] = origin_page_for(pages[i]); @@ -249,10 +251,16 @@ void kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, kmsan_enter_runtime(); mapped = __vmap_pages_range_noflush(shadow_start, shadow_end, prot, s_pages, page_shift); - KMSAN_WARN_ON(mapped); + if (mapped) { + err = mapped; + goto ret; + } mapped = __vmap_pages_range_noflush(origin_start, origin_end, prot, o_pages, page_shift); - KMSAN_WARN_ON(mapped); + if (mapped) { + err = mapped; + goto ret; + } kmsan_leave_runtime(); flush_tlb_kernel_range(shadow_start, shadow_end); flush_tlb_kernel_range(origin_start, origin_end); @@ -262,6 +270,7 @@ void kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, ret: kfree(s_pages); kfree(o_pages); + return err; } /* Allocate metadata for pages allocated at boot time. */ diff --git a/mm/vmalloc.c b/mm/vmalloc.c index a50072066221a..1355d95cce1ca 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -605,7 +605,11 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, int vmap_pages_range_noflush(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages, unsigned int page_shift) { - kmsan_vmap_pages_range_noflush(addr, end, prot, pages, page_shift); + int ret = kmsan_vmap_pages_range_noflush(addr, end, prot, pages, + page_shift); + + if (ret) + return ret; return __vmap_pages_range_noflush(addr, end, prot, pages, page_shift); }