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); } From patchwork Thu Apr 13 13:12:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13210282 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 27BECC77B61 for ; Thu, 13 Apr 2023 13:12:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C3263900006; Thu, 13 Apr 2023 09:12:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE3DE900002; Thu, 13 Apr 2023 09:12:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A8450900006; Thu, 13 Apr 2023 09:12:34 -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 9A188900002 for ; Thu, 13 Apr 2023 09:12:34 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6E9D81C5B34 for ; Thu, 13 Apr 2023 13:12:34 +0000 (UTC) X-FDA: 80676407028.12.B802642 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf04.hostedemail.com (Postfix) with ESMTP id 94B534001B for ; Thu, 13 Apr 2023 13:12:32 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=UWMmjgu4; spf=pass (imf04.hostedemail.com: domain of 3v_83ZAYKCMYsxupq3s00sxq.o0yxuz69-yyw7mow.03s@flex--glider.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3v_83ZAYKCMYsxupq3s00sxq.o0yxuz69-yyw7mow.03s@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=1681391552; 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=akRe6e4LluVT6nSisjA8EFH4R71lf+zwEBXmk0EhBro=; b=ivi8/mGvNPjTT4AhhgXN4uJ/CGWur3g0AOaLweUEB0pIHdNlkTX5qV/TaKiqShd67IFMlw hPpWAo+r6z0PnyyPSMG+BiO/e7rrfKlHuWSIgDE0YevxnehX38L2NQUvibIZGFZnb8/yN4 d8P/IuChmF9CBPxyA01ewkA+QBr7pFE= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=UWMmjgu4; spf=pass (imf04.hostedemail.com: domain of 3v_83ZAYKCMYsxupq3s00sxq.o0yxuz69-yyw7mow.03s@flex--glider.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3v_83ZAYKCMYsxupq3s00sxq.o0yxuz69-yyw7mow.03s@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681391552; a=rsa-sha256; cv=none; b=2QL4fmF/XPB7ewhI3zUMzihqXbK1L+LPzU2lpyogyPpVwwy+gBzexIjAdRYF/qKZZ5x7o2 JGKagFMJDvB/UWlpaIMegqiqZ0X5NhN1/58MZRX/lIPj/9pZAb1sPhnEZM3XBuy1TgjVTG qEsS1FXzTuGucfSWm4eHqkO0BllWlr8= Received: by mail-wm1-f74.google.com with SMTP id j15-20020a05600c1c0f00b003f0a83bf082so928328wms.8 for ; Thu, 13 Apr 2023 06:12:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681391551; x=1683983551; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=akRe6e4LluVT6nSisjA8EFH4R71lf+zwEBXmk0EhBro=; b=UWMmjgu4IOSqX5O5xqqvwkH7RA1XeATrgudE5npO4wFN2DVyqyLAVKX22J9okhAt/f WotKPxEQVCFGgD5uRAwrTZUlIEZ/IsbLrld0fd3ZNx6+fnGYgn6BFkHWg+tR5TuM3i/2 /QxA1eaUKLWaI7yosX5HLTbRHZPPs9eytyhZr7c+VI11gRjnyGympvKbf2DjnClcTAeZ jMHfSvmsDhUshFLa9aTwnqaCvBr5xvm37uTdVsG/JwEBAxP/7+CABtpGpFdswDI8yGf6 rlZFmxJpt+if/yKAmeqXtLu4so+aQiQ3pC2KmSbses/Xm3lEXWcn2I/t7g7IAXvH2OlP 1pwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681391551; x=1683983551; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=akRe6e4LluVT6nSisjA8EFH4R71lf+zwEBXmk0EhBro=; b=gEEyqJZ6PPiEZKKSAKUswtRPkA3aoqzPqiv7fUKm61ZR45o7zznrTCALSCVJRa+5CU 7wOEatvmpNUj2fJJMmJqQWneqKADYdlBV/k3iaiOtmuj+u1gsUufp5wqqn1ii5P8uPoB SuXHbEVMzzBtf0VWKlSlik6roZQO8Do0c0sge78wsGp3m8BiCK3I/qH6ljAv99MUEmtt GdDE8BftrieG6KL9K51Mj8q/3/Y+AsHlCTQCQHYnVCglCspGpK766iedl6K9xEKGiOuW 8fE9HG0yMMvJWfkPvVFoe08V0NbHbF97/13e1XJEYJ/dfbPD9HvQPQ1EQrgefskAEcKH 4axw== X-Gm-Message-State: AAQBX9fwBCbIMBI0J1iCOW3bm/SSzg16MXS+2+dv9Z2udSippKFo2P1Y 8DU1LzNPn99x9/hrDCxZp6nE4DyIBKM= X-Google-Smtp-Source: AKy350aXlH+JEH6G36TukQUXMTmo+3/5cJ7SYgjh19063ZjyiBg+1zGqVChNDLCI/LK5ak77r2MOdwY/dZk= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:eb2b:4d7d:1d7f:9316]) (user=glider job=sendgmr) by 2002:a5d:4a42:0:b0:2f2:7854:f419 with SMTP id v2-20020a5d4a42000000b002f27854f419mr325850wrs.13.1681391551064; Thu, 13 Apr 2023 06:12:31 -0700 (PDT) Date: Thu, 13 Apr 2023 15:12:21 +0200 In-Reply-To: <20230413131223.4135168-1-glider@google.com> Mime-Version: 1.0 References: <20230413131223.4135168-1-glider@google.com> X-Mailer: git-send-email 2.40.0.577.gac1e443424-goog Message-ID: <20230413131223.4135168-2-glider@google.com> Subject: [PATCH v2 2/4] mm: kmsan: handle alloc failures in kmsan_ioremap_page_range() 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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 94B534001B X-Rspam-User: X-Stat-Signature: ey9kdjg8nkidxtcekz4nts5r43endop5 X-HE-Tag: 1681391552-246331 X-HE-Meta: U2FsdGVkX19fa6HJKsLyrrcbUs+hKa3vCzJeKGdr2jFq4qg3t1G0hpdXyaHeXaYhyTwtuYBf4C/xWjhk+DVMLaryVvg9Id21YO1McFbpk5Y3D4s1Avw3jMUjv4ZaDj6sP3KLV8VGrUetBj/qKaOGkTIf8VbXCYH8ZJpIlMNg/6VDwxsIyngzwDBr4j0g85st7CrlSqWOhd0e6oiYBzV4RS16leQ6rbMUFeLn7yftB9eMF8t40iFm1QSLjv9sxFO4YQERnreuxMH4R7SIFBNeP4qk4jKeBYhLD9g4IPzp6eM6tgE2tangqxPuxMWoH2HGBBRs+AMBIiExQDUayLm+Mmm6+PH3vAHRFkIWX8R/SICZYV3LumO9tvbtO4MJcv8bpD8RT1byp200ak0r1jdKxmchlloL8dkhCKacCz2B0IJsUEyZh/Cg/QrR/V+t2/LMhOmXW2YLrud+/44wPLieiYyrF1gu//mnsEtx3v+h/pkqm88mDNqj/fJsHAAZjlKY7hJipMJRGk3Zaw2zdcCuAw3quN8DeDC+h3gk4tceWj0iKpIPMZ51ofIElINck2Ev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WFsT4Zbwf6p+o24wOvzv6OJ0dY+CX2NmNmHsw3Yv5OTFaIBoiQy2KXIvG3PXdhZPz2Jm/WX0p411RP4KMYzyKRCY38tTciHjF7EPPtxgYDNjhLXi9GLqU7RtHe9YG+qotG/y+51hnIvjmhGy8wX9Vp5Jc80E/jOW4BiX12ofcbxvhkEwPaVCogwcC1TNn5UPHp7+d56Y6cHEScEg== 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: Similarly to kmsan_vmap_pages_range_noflush(), kmsan_ioremap_page_range() must also properly handle allocation/mapping failures. In the case of such, it must clean up the already created metadata mappings and return an error code, so that the error can be propagated to ioremap_page_range(). Without doing so, KMSAN may silently fail to bring the metadata for the page range into a consistent state, which will result in user-visible crashes when trying to access them. 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: -- updated patch description as requested by Andrew Morton -- check the return value of __vmap_pages_range_noflush(), as suggested by Dipanjan Das -- return 0 from the inline version of kmsan_ioremap_page_range() (spotted by kernel test robot ) --- include/linux/kmsan.h | 19 ++++++++------- mm/kmsan/hooks.c | 55 ++++++++++++++++++++++++++++++++++++------- mm/vmalloc.c | 4 ++-- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index c7ff3aefc5a13..30b17647ce3c7 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -160,11 +160,12 @@ void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end); * @page_shift: page_shift argument passed to vmap_range_noflush(). * * KMSAN creates new metadata pages for the physical pages mapped into the - * virtual memory. + * virtual memory. Returns 0 on success, callers must check for non-zero return + * value. */ -void kmsan_ioremap_page_range(unsigned long addr, unsigned long end, - phys_addr_t phys_addr, pgprot_t prot, - unsigned int page_shift); +int kmsan_ioremap_page_range(unsigned long addr, unsigned long end, + phys_addr_t phys_addr, pgprot_t prot, + unsigned int page_shift); /** * kmsan_iounmap_page_range() - Notify KMSAN about a iounmap_page_range() call. @@ -296,12 +297,12 @@ static inline void kmsan_vunmap_range_noflush(unsigned long start, { } -static inline void kmsan_ioremap_page_range(unsigned long start, - unsigned long end, - phys_addr_t phys_addr, - pgprot_t prot, - unsigned int page_shift) +static inline int kmsan_ioremap_page_range(unsigned long start, + unsigned long end, + phys_addr_t phys_addr, pgprot_t prot, + unsigned int page_shift) { + return 0; } static inline void kmsan_iounmap_page_range(unsigned long start, diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 3807502766a3e..ec0da72e65aa0 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -148,35 +148,74 @@ void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end) * into the virtual memory. If those physical pages already had shadow/origin, * those are ignored. */ -void kmsan_ioremap_page_range(unsigned long start, unsigned long end, - phys_addr_t phys_addr, pgprot_t prot, - unsigned int page_shift) +int kmsan_ioremap_page_range(unsigned long start, unsigned long end, + phys_addr_t phys_addr, pgprot_t prot, + unsigned int page_shift) { gfp_t gfp_mask = GFP_KERNEL | __GFP_ZERO; struct page *shadow, *origin; unsigned long off = 0; - int nr; + int nr, err = 0, clean = 0, mapped; if (!kmsan_enabled || kmsan_in_runtime()) - return; + return 0; nr = (end - start) / PAGE_SIZE; kmsan_enter_runtime(); - for (int i = 0; i < nr; i++, off += PAGE_SIZE) { + for (int i = 0; i < nr; i++, off += PAGE_SIZE, clean = i) { shadow = alloc_pages(gfp_mask, 1); origin = alloc_pages(gfp_mask, 1); - __vmap_pages_range_noflush( + if (!shadow || !origin) { + err = -ENOMEM; + goto ret; + } + mapped = __vmap_pages_range_noflush( vmalloc_shadow(start + off), vmalloc_shadow(start + off + PAGE_SIZE), prot, &shadow, PAGE_SHIFT); - __vmap_pages_range_noflush( + if (mapped) { + err = mapped; + goto ret; + } + shadow = NULL; + mapped = __vmap_pages_range_noflush( vmalloc_origin(start + off), vmalloc_origin(start + off + PAGE_SIZE), prot, &origin, PAGE_SHIFT); + if (mapped) { + __vunmap_range_noflush( + vmalloc_shadow(start + off), + vmalloc_shadow(start + off + PAGE_SIZE)); + err = mapped; + goto ret; + } + origin = NULL; + } + /* Page mapping loop finished normally, nothing to clean up. */ + clean = 0; + +ret: + if (clean > 0) { + /* + * Something went wrong. Clean up shadow/origin pages allocated + * on the last loop iteration, then delete mappings created + * during the previous iterations. + */ + if (shadow) + __free_pages(shadow, 1); + if (origin) + __free_pages(origin, 1); + __vunmap_range_noflush( + vmalloc_shadow(start), + vmalloc_shadow(start + clean * PAGE_SIZE)); + __vunmap_range_noflush( + vmalloc_origin(start), + vmalloc_origin(start + clean * PAGE_SIZE)); } flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end)); flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end)); kmsan_leave_runtime(); + return err; } void kmsan_iounmap_page_range(unsigned long start, unsigned long end) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 1355d95cce1ca..31ff782d368b0 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -313,8 +313,8 @@ int ioremap_page_range(unsigned long addr, unsigned long end, ioremap_max_page_shift); flush_cache_vmap(addr, end); if (!err) - kmsan_ioremap_page_range(addr, end, phys_addr, prot, - ioremap_max_page_shift); + err = kmsan_ioremap_page_range(addr, end, phys_addr, prot, + ioremap_max_page_shift); return err; } From patchwork Thu Apr 13 13:12:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13210283 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 B33EDC77B61 for ; Thu, 13 Apr 2023 13:12:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 506BD900007; Thu, 13 Apr 2023 09:12:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B742900002; Thu, 13 Apr 2023 09:12:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 37E43900007; Thu, 13 Apr 2023 09:12:39 -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 296F6900002 for ; Thu, 13 Apr 2023 09:12:39 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C21F31C5C60 for ; Thu, 13 Apr 2023 13:12:38 +0000 (UTC) X-FDA: 80676407196.11.03A5F4A Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf03.hostedemail.com (Postfix) with ESMTP id C0B7620024 for ; Thu, 13 Apr 2023 13:12:35 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=D1+Gxpsm; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3wv83ZAYKCMkv0xst6v33v0t.r310x29C-11zAprz.36v@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3wv83ZAYKCMkv0xst6v33v0t.r310x29C-11zAprz.36v@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681391555; 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=BH4qKloTg3p5myDfS9qJ+XOa2IgLWTGcxSlYlXSqJNo=; b=fqXkMEK6b0GlRB4SZ1PJwlJKGNGBMBmMuOtMjGClryCBtV397KDMseZpydIlEzJ/OkzyXi +w7SnqqSfff+gOMSLL3+0KVcH7hEfDVjeiwegpH7IJ+Mnc5qgXSdMxk+oERODPyVsXraEz rYQs2n28rxDdd5cF1MTv+Icl3Ft7ORM= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=D1+Gxpsm; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3wv83ZAYKCMkv0xst6v33v0t.r310x29C-11zAprz.36v@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3wv83ZAYKCMkv0xst6v33v0t.r310x29C-11zAprz.36v@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681391555; a=rsa-sha256; cv=none; b=k30mo8MqynRYSoqHn0enY+FZv5mEM9ZLszkD7y04MCXObYXj5OWBe6AaKSqLPyHprgsQLn q8q665Z5uCxnJmdDioGfX/TUOxgvYhdOzbQshtasrkmWn2KcIN7p6vQcdTEkttVpqbGemg 94PuWDqXVRs/m2nOAhkbAAPZUi5M4wk= Received: by mail-ed1-f74.google.com with SMTP id 4fb4d7f45d1cf-50508242889so790667a12.1 for ; Thu, 13 Apr 2023 06:12:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681391554; x=1683983554; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BH4qKloTg3p5myDfS9qJ+XOa2IgLWTGcxSlYlXSqJNo=; b=D1+Gxpsma/kKehsYuehV5AlXbCxc9+6yPgVnS4mtrcrcUcfPbXHC2IVd4NdgcWvxKa sFORPx9k38EwNTdIx61YKgywtcophMGlm0SneHUBf4hypraueUI/TCDxI29jaZxY1vIy ltbilESRPv7Q0LzZkEMlnMV0LuOIkteNE9tSmGodmB8jIC9XLr/RqiX83LjxWJDm0fVC EzV7E9dIAd5BiJZP+J1mqdK7NdGn/rWTij/eOFuQ+J2dgfLFHk6TbzlAIkaBXBH7NQT0 efLIkeRRi5i07YY6nTPb/kS2kA4Hs5mxoljJv1OfBU3BsRfbtn2Ns7T6FwUjRXC1Rn46 5MNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681391554; x=1683983554; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BH4qKloTg3p5myDfS9qJ+XOa2IgLWTGcxSlYlXSqJNo=; b=GAkj4oPQfDEzjor4SbKtvPRtEJWW2ATpn06uwK6CRJ3OM3ZqkUqdgicuVJki9ODCAT yd+LhF6GXE+5ONKD9IEnL+PSkCRVXVNQXAOZvPIQy+sfs6QMqraQ9v5fHgn0cRVzn1L4 rJeVLX6DE+pZO5NMSMBoivXhLGSoej8TNBgwSeHeikp9FZXnhT2WPEJ7QdZ3aKYFhRqs 7lJ7AiqtC2NMvABUoM3hM/T27W5uuWzbc4+k6X1bL++k9/DufIsPj7IPwqOsz5ihTSuP 0L1MylckjD1Cn4KUdo7ghf6pZrNBVdR9QHhTBe5hcTCL42PJ4ssoIlL6yF3ymQjZoSuB +dOw== X-Gm-Message-State: AAQBX9cclG4rFGeKpGdczkM7qhAmyVv0hDLdlqa0VjKiy+NdE4HqXLFa 6HJdKtu+hBWarXcsL6o88g91bbl3GH0= X-Google-Smtp-Source: AKy350YjqcoIpDQnhokGemab6QINRaBafWRCSTgHRnGh9LmJmojNFRKF3kptG318ucQsrxRVaIe1NJVIppQ= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:eb2b:4d7d:1d7f:9316]) (user=glider job=sendgmr) by 2002:a50:9ee7:0:b0:505:842:37b0 with SMTP id a94-20020a509ee7000000b00505084237b0mr1236251edf.3.1681391554378; Thu, 13 Apr 2023 06:12:34 -0700 (PDT) Date: Thu, 13 Apr 2023 15:12:22 +0200 In-Reply-To: <20230413131223.4135168-1-glider@google.com> Mime-Version: 1.0 References: <20230413131223.4135168-1-glider@google.com> X-Mailer: git-send-email 2.40.0.577.gac1e443424-goog Message-ID: <20230413131223.4135168-3-glider@google.com> Subject: [PATCH v2 3/4] mm: kmsan: apply __must_check to non-void functions 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 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: C0B7620024 X-Stat-Signature: bmoxi99qybfsixbkqqjhjeq7a8bt1uf7 X-HE-Tag: 1681391555-917440 X-HE-Meta: U2FsdGVkX1/KAdiwdAJ/9P+Mf1I09tUMPkdWmHp9nmiqHlQ1woBCE4DfIYCF8Wjp0+OQGzV6zGx0ZqxPFyW7ud3rCHyNWBGOjcwleKB0fq8VSljOVFHf60ntouEpOvIFx3EscvAlEJXi+StDixkW1a+0KszWoB8QU3c5rHewSmJRrVef78MchZzuYLZoeMNZoUJ8EV0l5P8gFrkI/NaLaZkdZVPcFUnI460m4JhRZEHXa4fOgsu65q1ph2wsaip/L+TWsTlUBEl0m7IQtzKTcxcAVyu2pz97PNRWEq8y2Nt0ZrHPRWxX5MZRrdEY+uDh4GnA/Oxf60o1bZlaGUP1pLxtk4327tPAFI4c0zaCQcra1Mo4nV1mTXfQ55FSvcfgYCy7oyOLoJv67x8c5mISlvYVpYr7uvCEF+1j+eAK0xymfJ89CV+IpdqPN3BHcXjtXGWXUxiBYdLiHai7HaAhdoPm2ILBv49dYdnZceSsphniHIgH0Ga5zOCDWYpyNnJXyMHWqp7j7wuAuMHAYSJ+KZurWih2PjWdr1DRSD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cuNK++m1gkf3vbd0uP2EaNI/oukSr5kjSbwn3N5E= 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: Non-void KMSAN hooks may return error codes that indicate that KMSAN failed to reflect the changed memory state in the metadata (e.g. it could not create the necessary memory mappings). In such cases the callers should handle the errors to prevent the tool from using the inconsistent metadata in the future. We mark non-void hooks with __must_check so that error handling is not skipped. Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- include/linux/kmsan.h | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index 30b17647ce3c7..e0c23a32cdf01 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -54,7 +54,8 @@ void __init kmsan_init_runtime(void); * Freed pages are either returned to buddy allocator or held back to be used * as metadata pages. */ -bool __init kmsan_memblock_free_pages(struct page *page, unsigned int order); +bool __init __must_check kmsan_memblock_free_pages(struct page *page, + unsigned int order); /** * kmsan_alloc_page() - Notify KMSAN about an alloc_pages() call. @@ -137,9 +138,11 @@ void kmsan_kfree_large(const void *ptr); * vmalloc metadata address range. Returns 0 on success, callers must check * for non-zero return value. */ -int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, - pgprot_t prot, struct page **pages, - unsigned int page_shift); +int __must_check 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. @@ -163,9 +166,9 @@ void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end); * virtual memory. Returns 0 on success, callers must check for non-zero return * value. */ -int kmsan_ioremap_page_range(unsigned long addr, unsigned long end, - phys_addr_t phys_addr, pgprot_t prot, - unsigned int page_shift); +int __must_check kmsan_ioremap_page_range(unsigned long addr, unsigned long end, + phys_addr_t phys_addr, pgprot_t prot, + unsigned int page_shift); /** * kmsan_iounmap_page_range() - Notify KMSAN about a iounmap_page_range() call. @@ -237,8 +240,8 @@ static inline void kmsan_init_runtime(void) { } -static inline bool kmsan_memblock_free_pages(struct page *page, - unsigned int order) +static inline bool __must_check kmsan_memblock_free_pages(struct page *page, + unsigned int order) { return true; } @@ -251,10 +254,9 @@ 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) +static inline void kmsan_alloc_page(struct page *page, unsigned int order, + gfp_t flags) { - return 0; } static inline void kmsan_free_page(struct page *page, unsigned int order) @@ -283,11 +285,9 @@ static inline void kmsan_kfree_large(const void *ptr) { } -static inline int kmsan_vmap_pages_range_noflush(unsigned long start, - unsigned long end, - pgprot_t prot, - struct page **pages, - unsigned int page_shift) +static inline int __must_check kmsan_vmap_pages_range_noflush( + unsigned long start, unsigned long end, pgprot_t prot, + struct page **pages, unsigned int page_shift) { return 0; } @@ -297,10 +297,11 @@ static inline void kmsan_vunmap_range_noflush(unsigned long start, { } -static inline int kmsan_ioremap_page_range(unsigned long start, - unsigned long end, - phys_addr_t phys_addr, pgprot_t prot, - unsigned int page_shift) +static inline int __must_check kmsan_ioremap_page_range(unsigned long start, + unsigned long end, + phys_addr_t phys_addr, + pgprot_t prot, + unsigned int page_shift) { return 0; } From patchwork Thu Apr 13 13:12:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13210284 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 495D1C77B6F for ; Thu, 13 Apr 2023 13:12:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CCF63900008; Thu, 13 Apr 2023 09:12:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C7E83900002; Thu, 13 Apr 2023 09:12:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6DFE900008; Thu, 13 Apr 2023 09:12:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id AA10C900002 for ; Thu, 13 Apr 2023 09:12:41 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 54F18AAF44 for ; Thu, 13 Apr 2023 13:12:41 +0000 (UTC) X-FDA: 80676407322.05.6311C0D Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf20.hostedemail.com (Postfix) with ESMTP id 5FA781C0025 for ; Thu, 13 Apr 2023 13:12:39 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=3g3kcnsZ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3xf83ZAYKCMwy30vw9y66y3w.u64305CF-442Dsu2.69y@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3xf83ZAYKCMwy30vw9y66y3w.u64305CF-442Dsu2.69y@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681391559; 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=5hl8mLgZ8vfFyAYGSuilKL0D92tfM3tv959zGBlMPY8=; b=4t8fAjHeoKteXPujG3E/bHnQUaPDj2kk2oZ+2TZAmMDO/6WhBMjmIBPcg8B64X2kQcVdqV hQqszRelDc1L7n0djH2loiMmRFEZj3HFH/i2yJ+mVE/IP+YSvpCrkzHDB/fzkLNb9h+NQP SJDXkRQnaizwMu0wbOMa5AhTJSrNJAs= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=3g3kcnsZ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3xf83ZAYKCMwy30vw9y66y3w.u64305CF-442Dsu2.69y@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3xf83ZAYKCMwy30vw9y66y3w.u64305CF-442Dsu2.69y@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681391559; a=rsa-sha256; cv=none; b=QD6LDjDs5EPcJiJg4UNhsgEpDDs+t+f0muQ+0b1dY5f/f1a3vsAc3dI/FxIqGk4dqds5kT nE6+BD0P54KIKilWRNJCLGvEx17hN/pb7ntcUhQ/u3sdnpmmkI+EHrbGoQIiScm2r//IQB XhPZHylZDYqdxicVP/wM244nKvyzpB8= Received: by mail-ej1-f73.google.com with SMTP id tg11-20020a1709078dcb00b0093460dfa569so5425769ejc.13 for ; Thu, 13 Apr 2023 06:12:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681391558; x=1683983558; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5hl8mLgZ8vfFyAYGSuilKL0D92tfM3tv959zGBlMPY8=; b=3g3kcnsZVl5BwzCD7l2Xo768ttu3qPYULVrQMP5xF9enWn5TBU3XzrzUhG/7hwEHQA wPLn47/ryUIDarxbfs0tE7kinLwqMnxfwdQc9Q1dUN8JcWR+QRB1dpnx08/iFWkqR2Lk NNSidDbqp2G1GtnwG23rwxGljSRZFninLhww6HZAy1DTUk234ZDJBq5flP+4WWjmWQM+ 8Uix+/hEB8gXaepnyyxnUvHwpTRRtrP74mMwqCZz1CP5JVkB4+8uXG6l4d4BkgTOte8D 7KhEZEdSO9t3ZrKdlBLF64cImCAAa1EQIUDgwpJ1BLBeNAApg6yCW8UH9Z6PPH58aN5H kJPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681391558; x=1683983558; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5hl8mLgZ8vfFyAYGSuilKL0D92tfM3tv959zGBlMPY8=; b=E62gGOT5Riryrj8DCPsUrF/gXPFtB1INBl4luUTHycjDJKLI8enh3KM/OoI+SDEgsW w7+yt6sWaayuCFhsk5DJpawPWpREjwXMemI9L81mbjdz+GPC8a1rHTIA0kpWZY1tPJtu CluKXfgmzeLEpXoALkaEd06s63w/X/lGWxbLgSv8Ysnkiqu1WEuqAOYdurp3m+7m+sUM n5VyTGH4gMP07Q5iepT3n69eQq3/p9wq5m4GDnLeEoIj1BZhrq5+CCOcl2k2n5EZJRFJ HIsmFQ5/RgZOPnQ2nZ9D7HA6LiuNA8sSJHI2F02taDdpu5uc3EWBJbEtUrI2OJlTG4Ni Uibg== X-Gm-Message-State: AAQBX9c8266J52dyCbQWHqUT9QlTqFkgAuLM7kmjryTtLXf5QVuCnsiI 82t6sfTWQYbWIu11vFRA8YKcni3UVAY= X-Google-Smtp-Source: AKy350a9ouyXSsvTMFtTTTvt+/1HR6+68HAPE3D8GNHQDJB5FXfIXg3IfKHFWgMSoRb1WkwpEEN+s/qJiXc= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:eb2b:4d7d:1d7f:9316]) (user=glider job=sendgmr) by 2002:a17:906:3091:b0:94a:a6ac:8a2d with SMTP id 17-20020a170906309100b0094aa6ac8a2dmr1265450ejv.13.1681391557908; Thu, 13 Apr 2023 06:12:37 -0700 (PDT) Date: Thu, 13 Apr 2023 15:12:23 +0200 In-Reply-To: <20230413131223.4135168-1-glider@google.com> Mime-Version: 1.0 References: <20230413131223.4135168-1-glider@google.com> X-Mailer: git-send-email 2.40.0.577.gac1e443424-goog Message-ID: <20230413131223.4135168-4-glider@google.com> Subject: [PATCH v2 4/4] mm: apply __must_check to 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-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 5FA781C0025 X-Stat-Signature: bfegk3q9fhwh4jdc6331zngfyi8r5h4c X-HE-Tag: 1681391559-391615 X-HE-Meta: U2FsdGVkX1+AMDBHASgrVfjOnCCx8WnAjQNTHN/nR3eg2AqOn4SRs0hKWwC+qpfQfaYE9EvG66ad9lMyQh7V4HNJ0Ch7qpNYfhBr4V5VvZdvQDhbswZrah/iFUjt+wl6C6OuPMMh3ay8e0qwiSwpgYzjvtj8hVCZ5x36mkl/FMbIPSXLUyHqsivfwsVYDVCfDHsFAPF+EeQPIPa2sIODU+bA2EySxBlTan0EDL71eE6JbtNHG+2NsftQqULurvuu3yerGHafVM+jSd4IEoo1EMn8CX5lqOhqwYLg2zAvKl2nKEUY+FnAOau6JvolenGiZy6/C8ZVa5KS7EfCwonEbt26p+BoSnuRAOfwZKU4/fePTHyLzuvdf+doLnJSA7lZlWahQ7ZU3ByMMpmfHVbt9444zRbl6ZuJTqtfu6iYZfbRbCf4WG7H7AE/p2x7K28fvGEPgs5gSFflRd2aEblgaFndERalkpHyiKoi2Lz3QDbi7W4WKqA0GFJGDniexr91/vEWZdr6CGAbgpYMBquwInLEaknLuaZ55Q2AN3C5KH8ccEk+2Zxrwo3Exw+vqldTV1ElJsOxNoctnes9DOT9ogiCFPeHtN50f+aNG+U3Qkhd/BU5+B1oozFx9CEFoQZdxWEDE7fgVTe8+Fx+tH/zBjA+a2MPef71OlFApBomvc/HSTOv78uGLwaz2ln0nd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1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: To prevent errors when vmap_pages_range_noflush() or __vmap_pages_range_noflush() silently fail (see the link below for an example), annotate them with __must_check so that the callers do not unconditionally assume the mapping succeeded. Reported-by: Dipanjan Das Link: https://lore.kernel.org/linux-mm/CANX2M5ZRrRA64k0hOif02TjmY9kbbO2aCBPyq79es34RXZ=cAw@mail.gmail.com/ Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- mm/internal.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 7920a8b7982ec..a646cf7c41e8a 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -833,20 +833,20 @@ size_t splice_folio_into_pipe(struct pipe_inode_info *pipe, * mm/vmalloc.c */ #ifdef CONFIG_MMU -int vmap_pages_range_noflush(unsigned long addr, unsigned long end, - pgprot_t prot, struct page **pages, unsigned int page_shift); +int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end, + pgprot_t prot, struct page **pages, unsigned int page_shift); #else static inline -int vmap_pages_range_noflush(unsigned long addr, unsigned long end, - pgprot_t prot, struct page **pages, unsigned int page_shift) +int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end, + pgprot_t prot, struct page **pages, unsigned int page_shift) { return -EINVAL; } #endif -int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, - pgprot_t prot, struct page **pages, - unsigned int page_shift); +int __must_check __vmap_pages_range_noflush( + unsigned long addr, unsigned long end, pgprot_t prot, + struct page **pages, unsigned int page_shift); void vunmap_range_noflush(unsigned long start, unsigned long end);