From patchwork Wed Apr 12 14:52:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13209228 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 61F73C7619A for ; Wed, 12 Apr 2023 14:53:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CF0B96B0074; Wed, 12 Apr 2023 10:53:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CA0DA900003; Wed, 12 Apr 2023 10:53:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B40EB900002; Wed, 12 Apr 2023 10:53:08 -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 A47556B0074 for ; Wed, 12 Apr 2023 10:53:08 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 62123160141 for ; Wed, 12 Apr 2023 14:53:08 +0000 (UTC) X-FDA: 80673031656.28.AAD4EC5 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf03.hostedemail.com (Postfix) with ESMTP id 784F520024 for ; Wed, 12 Apr 2023 14:53:05 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=BGJ19UUT; spf=pass (imf03.hostedemail.com: domain of 30MU2ZAYKCF8DIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@flex--glider.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=30MU2ZAYKCF8DIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@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=1681311185; 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=CwA0wdGrULC2XXBzkQfVWleO9hyls9jS7CAlyZ8lvMo=; b=XZrc8fzY6mi57MvCzmbjxx2+7hsRCZIxpxn0breWMvnqSwcErYttEcwgYHHomFewmBOYM2 rQ2Pl7W7/w+A1j3cBR1IzKhEFuMMf5LPl0J95f+Cik1TJtH52J6ALMO9MUJ/N7azNSWE94 0q33YFp6+knGP5ztw+UGrE/dL5/njKo= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=BGJ19UUT; spf=pass (imf03.hostedemail.com: domain of 30MU2ZAYKCF8DIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@flex--glider.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=30MU2ZAYKCF8DIFABODLLDIB.9LJIFKRU-JJHS79H.LOD@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681311185; a=rsa-sha256; cv=none; b=RE+TU6lyB0l4NlPHlGNkIdmiqx//QXA/KiLeD+nxJHM6yhbo4A3p08wc5P2G6xf7VtWo22 2FqO66gdNv/La3OB0fHi1YWpB4OUv+vl37xpKpz1kxa61vjO/fAAWUoq0+sgFkpUgsHAjS cjp9DnxM3zZ3aHC2+kvZb83bNK7r6Bs= Received: by mail-yb1-f201.google.com with SMTP id p129-20020a254287000000b00aedd4305ff2so12604319yba.13 for ; Wed, 12 Apr 2023 07:53:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681311184; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=CwA0wdGrULC2XXBzkQfVWleO9hyls9jS7CAlyZ8lvMo=; b=BGJ19UUT/KQmXwBdHwvfufxanTrfOtkalEb3y2N+im3F2wdY+eRyJvh/ocOo8nqvpt uIcfLl18+AYnrHG4kmP2Uz8d6wIAZ9dx8yRd9Umw2JeCcoKxqj5pmjixgiZHtkC2BrgW oSHxZTZMW29EHRY01yVSztq5fRm8X1vjMchRqarRQjuPc/GNpryrcg5n7M2vJzY9DhIQ +w2LOGFSsfRD4VqKwBlOEX8NzX+CuE3pYgcQxQOePaJ2fG446CSGdzQ2QUOhRZ6jjgRB 5pEb2j+e+90dkmujhtCU8zi9mrzviRnvG+dSdZyM1nUjKupT6dPf7tEIjh3t5Qb4NvIu s4SA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681311184; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=CwA0wdGrULC2XXBzkQfVWleO9hyls9jS7CAlyZ8lvMo=; b=I983wvnmD37AKw0Vqfn6e6cisxxLOzw87kl2tRXdO7EuGx/64tW8ocp9/ts+z07X/+ xCr8yls8bl3HOIz2eB7DgRKXP6aEXFwUoqwpr/VaY3O7Fd+zabaFOtsepTun1Z3wz1Bc 3n7QJuHA1LTwforUKLljUyCpkkqp+KvAI2x02uYYj/wjs67MkanVD7l1KP/ynizFoRyZ HFrfitpK5aSSlxMGEeZznLl1ODEJNvGM+V3OGgTc6lrFYJLQFeoRsq5voXNqLxH6U9BX m6oLSv/dpbAW070ZD/c+EUEUMEjzKu7CZvbqnrMYx81O5kydk82GiDcJ5d8uqUOcn2wy fGBw== X-Gm-Message-State: AAQBX9dqAL502cumU1MFyhDbj1BC9PQyNP/CI/oa6YmCMb6iob6EXHeq c0I4NqPcIxNcqjOmGhrpV62ihTvn9GY= X-Google-Smtp-Source: AKy350Y9H8x8fkMOv72KbbGnFPvZ5mHfzvPfBgJCLqdyjWcNAGuZGxLlLfr/LwfWgzBFvM7gkBOLd4qRA1E= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:901c:7904:40a1:1b6c]) (user=glider job=sendgmr) by 2002:a25:c905:0:b0:b77:81f:42dc with SMTP id z5-20020a25c905000000b00b77081f42dcmr12063363ybf.1.1681311184657; Wed, 12 Apr 2023 07:53:04 -0700 (PDT) Date: Wed, 12 Apr 2023 16:52:59 +0200 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.577.gac1e443424-goog Message-ID: <20230412145300.3651840-1-glider@google.com> Subject: [PATCH 1/2] 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 784F520024 X-Stat-Signature: kao9o14owu14h8xfk9d9jarzyofk7j4q X-HE-Tag: 1681311185-394179 X-HE-Meta: U2FsdGVkX183XIunX1alv920AcWjuPpMvKgJXNlCAq1V7q48hegaq3rrOf6atDCiCLok01yoCSwpcbnruIi+vjNwUUuc94/re4epf5R+8JD+2mJdjjOzLTbPG9vv0nITlq6cGqqGQQmSS2k08bdhZR5JVZWDAfn4IxRpw2IVajEiHRfVzeyJKn0+BDaZ/HUocwKoaMUiC67aXVkbwWFxHZHuZB7mUtNN/TjrVxftR1RwMZ+pARlzQMh0P0BoIiAHGQFBaA31vaq+2kax/JQyUbig/fWpazzHAjBCEngFTMhKbJ4iWnIc4EipUJE9h97HPtQo1gFYJaH7Ipab52B3W6lFJtACLnAYfykI/0p5rRarNRuEmas5I8/vn7busWpVwTgiukzP3UAL9j0fhqkP8acCpIIqsr7p6dxinIKm53BHGLv8XSk+Yvv1QBPLzXZeZPTngJgZg1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KU0XMkiHIAH5EFJpGYj3LIhGKupmht88OpT76cP87W6W5sXUXjMjdRcSIPNUbz134UJytgnbCXSb4e+zwZqOQDwgN3vGRX1SyhdvZBWZ1IErmmjjyM8Ydt3Z+xhX0nj98Tt/gUwaCTnC0FwgBNmxcoJ8hSTiw2aRc+50FqqaqBpB9drKIY8clN3HphtYGxHEdefVBUHcrU12Shk+ucjVkHRtovojWFq4PHrvb+WkaB5ZXipJGZSKvSGkonxyjSjc5W5giFKOF8hrF+Q6oGK7ICE+2C3BuZ/gG7lG 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 --- include/linux/kmsan.h | 19 ++++++++++--------- mm/kmsan/shadow.c | 27 ++++++++++++++++++--------- mm/vmalloc.c | 6 +++++- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index e38ae3c346184..a0769d4aad1c8 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,11 +282,11 @@ 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) { } 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 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+9PiNbZcFfc28LaSPUPaUS3M6p1I8fBItFndCQN0gFLrOC2MjzbAAubQQD7obxCzLQyecw/HFSnc9BxJqLUGZLiDrQ8qL4OWQQV3pFzJrhausxnRvGF7qRaNE6s/ocZSq40bpDQmFvYfeEfaTXGTMlmfWFl/h3xKSrBE/oo8M7/KHY/Em6+SbPzFYVF4g5WDJZcBZAD9ffXFkZFSKuXB8qdzDu2sbm+EOmBt45ATeSnbYx4K+qB0u3Isgfdh/QR+HMaWnbh2Itmo7FkC+6JLxJFZRe8nJmZZeqT0xNo1eCZCaEXJxHxXTQ9xCafWckZ3Mco0dHEB2kKBMFoBOsjbGhPYsLtLBRfWVrLiO6sO8AUicsb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2soVcqFGFAzsaZux+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; }