From patchwork Wed Apr 12 14:53:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13209229 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 DCDBCC77B6E for ; Wed, 12 Apr 2023 14:53:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 242CF6B0075; Wed, 12 Apr 2023 10:53:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F343900003; Wed, 12 Apr 2023 10:53:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0BA35900002; Wed, 12 Apr 2023 10:53:13 -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 F20A26B0075 for ; Wed, 12 Apr 2023 10:53:12 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B5CC4A00FF for ; Wed, 12 Apr 2023 14:53:12 +0000 (UTC) X-FDA: 80673031824.22.5EEF924 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf06.hostedemail.com (Postfix) with ESMTP id D6E58180007 for ; Wed, 12 Apr 2023 14:53:10 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=utjKfmyg; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 31cU2ZAYKCGQINKFGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=31cU2ZAYKCGQINKFGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681311190; 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=GLtuEgzLkzzznhMQcB+RHI4zD53wmkIOG/wGgFhyqPw=; b=WHqO7Fb5Goyw7Snb9ig1z53mecrF0AYuiv3faEwUD95DyKwmZOcRuG1sbKTyuyoJrRti8W mWu+smbwUXsamWpMM2vhIn9zmnXj6M7wEB8VgyCpP4k70Ys//DQxRaLNwnfE7YigV5Wk1E DpYGRxLE9zWc06SBa1sl1f+RIM8Fcx0= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=utjKfmyg; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 31cU2ZAYKCGQINKFGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=31cU2ZAYKCGQINKFGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681311190; a=rsa-sha256; cv=none; b=rjC3jmGOYsGML03qKpNQucaFdWNzo2tVhf6xWH1AxPegtVc7bxpDGCJeFFfVBqiiuUTSW/ AdMSDIjOBeFKH465Z9CRHlSiavP4Wpj1U/Rv0i2pL68P1+vqOS3eisB/83Nw5gqonQo5gV oNhwQ5KwIkz6qL1pGvccShbzpXrTch4= Received: by mail-ed1-f73.google.com with SMTP id 4fb4d7f45d1cf-505149e1a4eso752045a12.1 for ; Wed, 12 Apr 2023 07:53:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681311189; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GLtuEgzLkzzznhMQcB+RHI4zD53wmkIOG/wGgFhyqPw=; b=utjKfmygHn4kBdNPDo9Nu5744uCmHko/uzMzJes3gOweiYJk0TBjzaO2UChJ0JcFcJ lqin+1bee46zFqjL/hHX655dUQ2HRBBGkxuvYqqolwDWtcEg90ktwgp9D+O9oNn4ArOs EdSBg/WiVVR+gasXULzcLqxJc6U6K9DV/3j5Xq8xRQ1xRDb+DR/c2DSDFmp2olDBsYYd fSpGc3RDZVj/w4XGCSNFz5h1UfN7ktZUTSgAD+N8VJlJKA4ECg6MKy6b7XnsGoLwQ7mj aomTPIJ9aWSWMQ58tOu4ug/5EdiuE9yMeni+sxFDoZ6ywmtR3iYNqlt4mJEeZXBemZOV 16Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681311189; 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=GLtuEgzLkzzznhMQcB+RHI4zD53wmkIOG/wGgFhyqPw=; b=IxWhbF9muZYuCsXRoF19wTVcVOYbGwoMUEYrW43JJXU7XbSZf8fANFTJqjwmEd8HEm CQy5m63izOckOqbpyPEJH1w1ejN3IZkBJ54XsX124ibKshqOwzLxbWZd/111mTF3XCEz DOe8ILEV4OFESGL9cDJ1IXV5Ybite5ErwNdD2LG8XgHeoeJu8i7scR+5A6Ih+Hpd0rM+ FB099GJLvk+eK2WvKC919JLzUZpDnDUb1FFLQxeazUz/e7YoRnfhTtTuTtj938skpbcC gRRDQxV/qDnPLyOlY/ITApQhZ+vX0E9BMBJRuojHmOe2eLj7p+THphfLw8tU1IdrG5pN q41A== X-Gm-Message-State: AAQBX9cYPZ+X8KasHaTrMOh/xLBJUHGKHqjfyHWWmrv5E8hA8UGr1a6g fBHGSZXf5S/NeOVdwWrsB39nQe5Q0D4= X-Google-Smtp-Source: AKy350YtDxSb5Y0wl3Wqq51FjjIPmOzR3iiHinDz/kAPj7foWIm39W0DOvvC3nfps4wExlaQUWfESeTbnQY= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:901c:7904:40a1:1b6c]) (user=glider job=sendgmr) by 2002:a05:6402:550b:b0:4fb:e069:77ac with SMTP id fi11-20020a056402550b00b004fbe06977acmr2006910edb.0.1681311189113; Wed, 12 Apr 2023 07:53:09 -0700 (PDT) Date: Wed, 12 Apr 2023 16:53:00 +0200 In-Reply-To: <20230412145300.3651840-1-glider@google.com> Mime-Version: 1.0 References: <20230412145300.3651840-1-glider@google.com> X-Mailer: git-send-email 2.40.0.577.gac1e443424-goog Message-ID: <20230412145300.3651840-2-glider@google.com> Subject: [PATCH 2/2] 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-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: D6E58180007 X-Stat-Signature: 9obx6edkjzjb5bjy436qjesfexkc1nxn X-HE-Tag: 1681311190-33879 X-HE-Meta: U2FsdGVkX18RL9Vimt/poRuJqS13ngoPv5rCmVuYWDG23HAGwsKdDSTtiT7rgScJT265QaAGiCQVqXMRBTLDb7Fby5iX9FP7DPw3xi/Qdy7JZThyIPRbuVMO2yI2hR4LaLyUaLNzFknihIQISfYWiVm/gfUf4MdMn7BmVgUNjJtQHPLl906Qz2CtyeJ4HPD3K+218Z84adxvSeG4PO3DJY/3/PCosDB2sI+tfN28kRQK9mhjybQ5o22FFXVmK+J1SAfyuHnVAcIsRy25F/caW9oMm73uc9zMlpMZ7giM9hoLv4LhHWGnE+2UcQxqsh6PqI+9PiNbZcFfc28LaSPUPaUS3M6p1I8fBItFndCQN0gFLrOC2Mjz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nNBD1xkRU3TVY8tkxMQwKs6k3L6fVoYXMOjBqp4hJ59BhVXtOG32h4wyLe95Oznu+XV3yzgk4NcGphF23PwASoUpk0fM4sgz8NeSTzKezwrZYtWmmgqgOdZYMq0GoIW9DAgzgaP7badXSHh5LjJVlDGSe4Erd0jSWMGN3i7Hyyv1krdSSf7yL+BHPOadRe7V/oKDG3txETTvDq2wWNhkvtoTGV4OEP5+ecW/pG8HF7bJ0RUcCdyvFNMp2TiZyyz8ncEdsEkGZUEkXr/xv/fQFGF85LcyWeBxAKZJ3i1pYRAPGjik1wY8ATpeSBb9WLd2o2soVcqFGFAzsaZux+tyIW7TdN4KAa+Bb0wCntJhdmASxrwbSTRWBSKIPY5ywM9WbOBVw 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 failure can be propagated to ioremap_page_range(). 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 --- include/linux/kmsan.h | 18 +++++++-------- mm/kmsan/hooks.c | 53 +++++++++++++++++++++++++++++++++++++------ mm/vmalloc.c | 4 ++-- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index a0769d4aad1c8..fa5a4705ea379 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. @@ -295,11 +296,10 @@ 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) { } diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 3807502766a3e..02c17b7cb6ddd 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); + if (mapped) { + err = mapped; + goto ret; + } + shadow = NULL; __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; }