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: 82523 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp403772vqo; Wed, 12 Apr 2023 08:14:39 -0700 (PDT) X-Google-Smtp-Source: AKy350aSIrat8oOpiq7YmNT6pzhjS938Wh6AxrJ843zrLChStKj/jQ2cPtdcOz/iYyTETnfM80r+ X-Received: by 2002:a17:90a:d34d:b0:246:7582:b76f with SMTP id i13-20020a17090ad34d00b002467582b76fmr3750527pjx.0.1681312479575; Wed, 12 Apr 2023 08:14:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681312479; cv=none; d=google.com; s=arc-20160816; b=dgx3v/M6PlKz/DspLHR1X6xFW1L//rW4ob7pSF2ULH24Ykx1BD0wfguTksUkGBwUNa A8PrRPH6oK/xLpRYGLlXWkWaaTKx1S4QQHwsqCPL9HVFpt2nfNSdXDN72FEsqOngHyHr fWUXIS9cEYBmpWPs7Jxg9EulwuX4L2nZ6T7kLsJdbLa+MXwCTut8pO52ItnqVsOvJ7ys I7PhqkzOUPecfaU87wmaLAFgPHfDISEOugg3L3G95bgUEgA+B7hTbhAbYtB7bW6Y0yJX Lh8+NqzaydKUHZLESrTG+1/GlGy79WoxNCRMvFlmwg3Eq8P0bfolJdfg+13hE2te9yjU VuAQ== 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=CwA0wdGrULC2XXBzkQfVWleO9hyls9jS7CAlyZ8lvMo=; b=Ij3YE9Sofq3bHA4amlBLxtwdVvw400eGUMVNOTiCirt7hUOqfl1Jdht9nYxG9P0D6i yy7jHHWhCqLeQkcXYPXgVUEVdOsE2ngEe4NFW/bpWTQG65Dee7PxzRIWAyvWeO4PF5J3 8CDVtjcdPzm4GuKPVukCW0knZkBmij9Adlr693zf2+5nZGmQl/IospYjq4xaXHhklmJK WvDcJvZcbxVVcp/oX1LVCI5LOpYdQqsxaRlUcfCkH1I6hge7p7FD0qNYsXtiL9BrzOqj bI1/R615iCrGe5WwmOgbLS0XwhfyJyZE/Cs6ZBDoosrRkaYo88LRlYu/NMZVgGU+11Ri 8wZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=BGJ19UUT; 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 h7-20020a17090aea8700b0023d1bcea88bsi2107998pjz.66.2023.04.12.08.14.26; Wed, 12 Apr 2023 08:14:39 -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=BGJ19UUT; 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 S229671AbjDLOxO (ORCPT + 99 others); Wed, 12 Apr 2023 10:53:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229626AbjDLOxN (ORCPT ); Wed, 12 Apr 2023 10:53:13 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BD543A90 for ; Wed, 12 Apr 2023 07:53:05 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id o19-20020a254113000000b00b8ed021361bso12702719yba.7 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=6ShHYmWmOJZfT+H4I2mueY5npBG84DwEMMUvefgPk6fRKnzXkxzWAMMOTE/a1wk9zw h9DO+EZDK+vbT8G8SeBHgCeuX31PeV/w7Y95NLOsYl6yvV3rz3Dot/jAw2u/VUqnfL2a Hzg1W+sZjNEiHpOxwMX+pW2xiVZ+faX3w0ZUsPt7okAJ3YbALvMh/JTSUQXloXGt3wd1 R2ANpDdSRKghIWM12nC7ym0CcHBtMvtk+uAMQBl3KLi0mRMeixErkrxn27aHZ+iBF0l2 nzHEe8zp5u4GFyvuWXL/n+9YBwgdKriVAZC/yIfNtzN6qmdmYN5UVykcdQDKeU6eD8Qr AWEA== X-Gm-Message-State: AAQBX9cEMFDdi7VnBgIXWhu+T9tOQZmg/Tp4ZYmNX3Yk84n1gAiKdtf7 5iGOWrs8ztItf/eSb/cj8hnudUPYh8I= 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-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?1762983914515294706?= X-GMAIL-MSGID: =?utf-8?q?1762983914515294706?= 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: 82520 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp389402vqo; Wed, 12 Apr 2023 07:58:19 -0700 (PDT) X-Google-Smtp-Source: AKy350ZNllj0oNNFOm6TAmnVx7uw8huzRRc81oksZ/VZJfjEvsMwKivILjqBXpZoFj49KsJrFEG5 X-Received: by 2002:aa7:d452:0:b0:504:8b9a:39fa with SMTP id q18-20020aa7d452000000b005048b9a39famr12316014edr.19.1681311499279; Wed, 12 Apr 2023 07:58:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681311499; cv=none; d=google.com; s=arc-20160816; b=WaZvm0R8Sv7LXoS+073lT17vtdqTuawf4GfbI0M1jXD0DncjyfdM/bBoQplZm4DQr7 vgzJgjNG+Jyz5QLWLO/JHZyby6Q82Ac5p34JxnbIlmr5Jhfkc4N/Q4zRtG847REstPQN uAE6HNdhZofkQqHQ/rY1c8LFD/ItN4Fz2tnerVfBvfPXrSKhqInnjM8VKAqeAotPxEQy 4FqdWo4WTiCOvpzQkeGQskVIqRA1M4Dl8jbSbw78HNJsV0ZfWtfEJToaBFQejz0S2Tth DZEBQ9tikT0K9y4VQ/kjle0dvk8e4z5NBhH+gORs7eCeictUc3GJ9j4GlfzOqxeoUgh2 GQOA== 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:references :mime-version:in-reply-to:date:dkim-signature; bh=GLtuEgzLkzzznhMQcB+RHI4zD53wmkIOG/wGgFhyqPw=; b=A9jYpDywxQDMaNCU7JwhsFGhxGAl91r6N+Hkvl/KvYeIHdNYMZPcEHueVDdVAVKKE7 r92Kpxmd9oh4IxFI1oQaoK0DjCEpvQLTRRb81Y+cEzCcANC2J8jgZvBUT7rnZ2GqMROn eviwV/1jzWlPQoFprKGONrGWf2qyAyrQHPr0vYS5gvlSBj660zOn1bqMFcDqsCv7Ib/R ADZkpQqvaptlnmHAx5ScF5kLLvoHVkGnmuXqjgb66BoUSuQbGGYi6/VJqWt4uJ5HxTJw lFmg0jORNmEVtREB9n9Ik+BKapTbiE2Y3F6fxCGwFYIet9+yfdDvhotjdBsIJ1J74GTk HsqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=utjKfmyg; 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 d8-20020aa7d688000000b00504b34f5002si5227164edr.342.2023.04.12.07.57.54; Wed, 12 Apr 2023 07:58:19 -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=utjKfmyg; 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 S229728AbjDLOxR (ORCPT + 99 others); Wed, 12 Apr 2023 10:53:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229845AbjDLOxP (ORCPT ); Wed, 12 Apr 2023 10:53:15 -0400 Received: from mail-ed1-x54a.google.com (mail-ed1-x54a.google.com [IPv6:2a00:1450:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DA5B1994 for ; Wed, 12 Apr 2023 07:53:10 -0700 (PDT) Received: by mail-ed1-x54a.google.com with SMTP id 4fb4d7f45d1cf-5048993067dso9188786a12.0 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=LertPRej0ySvjA2chjleNSMYUPCttro+GyAUmPGhSHTSHKrbc3um9zn/+9PPX8nL9y r9odt/k2ye2W9b7VDohtsD8hOj463HHBgBtkwOYu8HV6mJIqsKbtqUozllVGHo0VjI5o +x/ZJo8ndSILQz17VmZwTxKwfMBu8mCqtIO1c+5Xv+FJ/+coOK8gFSdGqNbDfPosRE8n ODqS3rfjYy2HuAglemYjiCHac4Gqc9vhJ09lIalQLdX5zU5Pg1nOs/+ErCmikQF4bwSy DKj/XeHTIcDsAq/cCon/ueDOn+FfJfAVuuV1gbmAl62BlVSIW5ZMw1o2y/KgyUQykowr t1TQ== X-Gm-Message-State: AAQBX9dmwX8U2PkVSmorh4wiUUsT0XMGtPcPCEqDEXuZZzhJG16ksbnw GQuQzZ26RYswib1Pmf64ddwW4+t4Ka4= 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-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?1762982886207250786?= X-GMAIL-MSGID: =?utf-8?q?1762982886207250786?= 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; }