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: 82954 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1029557vqo; Thu, 13 Apr 2023 06:20:11 -0700 (PDT) X-Google-Smtp-Source: AKy350bKk2peCo2GedoYkKVndaawRfcsvlEoxkNlIcIM8TE46d7zrEKlDIkx9G+K6S8Ox6vq78PM X-Received: by 2002:a4a:45c5:0:b0:53e:5698:c0d2 with SMTP id y188-20020a4a45c5000000b0053e5698c0d2mr888937ooa.7.1681392011182; Thu, 13 Apr 2023 06:20:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681392011; cv=none; d=google.com; s=arc-20160816; b=sEqJ9YeOoXPfbSSQK9NTzdS3zKOcwe2gMCNCaTRIzyHM3HKQ5wjqabH6yfig95qBS+ 4vPLnXCVHmPme/B/Jouzot7YavTnz4toG/L0IyUsU8p9tIwoyoTR4xQMBw1ySKyvX5Hs NGRkzhK9pvZi7qcdKQQWe5pMwDjh6QpCHeE+7ZOGQxaGerh1qTwHWpa1wAfcKv0/+VQz AbE9n0KtG6PhAmJG0dKgHXJWOlFOdgIUGUqAa6+sONCoXtG6o5ShGpS9Nm6zBGc1ZsjC E3sxbPcyAOfbnOFVVyXOvMhFxv1g+RuKXkLW3UVA7uA3iemEAKMAPRnE+OgBf0za7oUc DYuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=akxBw8gEREuaBtxzA9qIYFhL4FrjZkaJp5ikMOEw/lQ=; b=kWY3znjLeOuSzcxFpAPQe414hHvvD2ZNiRusUY5aStrhs2P1q4Nh8L7ajsXegFlv37 atnIQZ75ycB4RPHlf2A7KwQ70n75J3YdgbNz2ozP6J8sDJR+WHK5gq7jBcmAvMSFcdgM nPewiLd/RSNIPYHvtWODsKtF8Pd8MHWuXZRda2R8su9O5o0jDddjR5L3HQ9vePn5DATO 4wFBPFyVYPun3iKmPwlyDZCFKhg6S4K9H9e4nkLj9ISTHmzJxbyhH4I6miQD1hah1ZnW XJSqGNZBNO8BmphhzCjod+9lHv1N6NW7bIct6FYsulUpnuPBjtdnqoj7pW3i0kJWbQ/R j2KQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=eFVXlBPl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x137-20020a4a418f000000b005423695623asi530883ooa.80.2023.04.13.06.19.57; Thu, 13 Apr 2023 06:20:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=eFVXlBPl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230182AbjDMNMt (ORCPT + 99 others); Thu, 13 Apr 2023 09:12:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230298AbjDMNMr (ORCPT ); Thu, 13 Apr 2023 09:12:47 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 467E89ECC for ; Thu, 13 Apr 2023 06:12:28 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-54f8e31155bso53091537b3.11 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=iJrhHwiZV5eFXWxSRVSSGoWb4kbLl4cAglEFG1KAEVFYALRaruxvHOGBCPZo+13PIB FxQv70Z1H7mMjWzoIuAzBn8DzyqYQchumUoQrvmkzg6fptoL0BFSxDk9Y5WEskmbJ4NZ lZpxB+ypbuRehjEqsepOYzsdADpv0cJWzlFaw+be8mSSvGItAb9VX5H79zmSUBV+Wx2f R9FOuQskrTy1znTSFkvD0HkQAfBsJV3ChxgJ3/+ZhcF7/exAlIn0n32QKowe6AtqBWnb PD3TIKUIuf3LRVEQWxRv31ZTiPCxnEAbkyiip+/+zwiWPFBVS1WoXnGDeduwKimvwInz XO6g== X-Gm-Message-State: AAQBX9fzFhwly2CFsZOHqua92mYtMUHij9lUYtxY54vvLUDWy/R74GH5 k6nCnC8InK643vXhufvolRfTEZ4EFMA= 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-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763067309465788551?= X-GMAIL-MSGID: =?utf-8?q?1763067309465788551?= 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); }