From patchwork Thu Jul 27 11:02:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 126926 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp1038420vqo; Thu, 27 Jul 2023 04:50:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlGFP2JQjOaCiN0TGrRRe2OmtOFYcO/ncFc8JVHtJtrlk8m6geZ3c9oM5VQVhwv0RriChr3+ X-Received: by 2002:a17:902:c943:b0:1b2:676d:1143 with SMTP id i3-20020a170902c94300b001b2676d1143mr3120766pla.15.1690458605377; Thu, 27 Jul 2023 04:50:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690458605; cv=none; d=google.com; s=arc-20160816; b=nIbbpSKyNyyUebYUHW9W5Yalo3gI7so64N7olLMfXU/ioTGByX7XiHfwYBfML5zw5C llDKbv/BoE65L5Rl4NNavlL0bJd+zh+jiPFT8bWllU5IEIiFiSYwHUM+1b/VWAZNR/cG ZWVWetzJRaN+K8aHCNY8q+irHFE3vwoGukqPocqg9wiQqGmdz1i6Dyj3hZK5nkV+cofX FpxrJJaMGyRReRpMc9rQWek+PEhz0dfzFh1V4rWKqoSrAiSPgPzfS1418315w8PmUsAE nYDq6kiFAgydWYBiY/w7noqWC7EdM5RwwM8fT40KVwcQZUzhUAtzcpcF5a6PZpPLVLLs HWtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=ei3or1z9wzS0d961tsHrEkUjs0WJucw6hIc9jgD9iPE=; fh=QvlGRRaCWQY6of6CL3viG7jjxnnYo6PGR+4iWsYZbUM=; b=wLj2nAR4Zj3lNMKde5vAagGI0JCSjBQqEAHr9/Ph/s/HEId35X+qyDG8F6GHf2HYVX htOJ7ppnBd5adzaLwErPKHEcXtjH00nP7QGWeUyt/kaAwobOR9RcytIKLezQhVMXWFOO yuY+ogJfHKPYenCGmEzVjez9rXf+OQqJdrFUCJ3RyJjmdH5obDmLS8W4dRG4vTBXY8DQ Kc+LduJLgmL4xJJF5SIlxy+ZNJErWhpS8qnhh+3yVCD9NdpEr2/FnXvG4MUY2XUEjbDg oddvp+I89GAW/AcHz/xNTwiSYEW8uKWgCNlIuIJibkVbPHECjhbjJzk4fHEi4XO/RROz gzxA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t14-20020a170902e84e00b001b521cf9578si1263923plg.443.2023.07.27.04.49.51; Thu, 27 Jul 2023 04:50:05 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233009AbjG0LDJ (ORCPT + 99 others); Thu, 27 Jul 2023 07:03:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233128AbjG0LCs (ORCPT ); Thu, 27 Jul 2023 07:02:48 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5871330CF for ; Thu, 27 Jul 2023 04:02:34 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 96136D75; Thu, 27 Jul 2023 04:03:16 -0700 (PDT) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.26]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id DAFA03F5A1; Thu, 27 Jul 2023 04:02:31 -0700 (PDT) From: Ryan Roberts To: Andrew Morton , Matthew Wilcox , Yin Fengwei , David Hildenbrand , Yu Zhao , Yang Shi , "Huang, Ying" , Zi Yan , Nathan Chancellor Cc: Ryan Roberts , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v1] mm: Fix use-after-free for MMU_GATHER_NO_GATHER Date: Thu, 27 Jul 2023 12:02:24 +0100 Message-Id: <20230727110224.3333682-1-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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: INBOX X-GMAIL-THRID: 1772574177155799706 X-GMAIL-MSGID: 1772574322438895021 The recent change to batch-zap anonymous ptes did not take into account that for platforms where MMU_GATHER_NO_GATHER is enabled (e.g. s390), __tlb_remove_page() drops a reference to the page. This means that the folio reference count can drop to zero while still in use (i.e. before folio_remove_rmap_range() is called). This does not happen on other platforms because the actual page freeing is deferred. Solve this by appropriately getting/putting the folio to guarrantee it does not get freed early. Given the new need to get/put the folio in the batch path, let's stick to the non-batched path if the folio is not large. In this case batching is not helpful since the batch size is 1. Signed-off-by: Ryan Roberts Fixes: 904d9713b3b0 ("mm: batch-zap large anonymous folio PTE mappings") Reported-by: Nathan Chancellor Link: https://lore.kernel.org/linux-mm/20230726161942.GA1123863@dev-arch.thelio-3990X/ --- Hi Andrew, This fixes patch 3 in the series at [1], which is currently in mm-unstable. I'm not sure whether you want to take the fix or whether I should re-post the entire series? Thanks, Ryan mm/memory.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) -- 2.25.1 diff --git a/mm/memory.c b/mm/memory.c index 2130bad76eb1..808f6408a570 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1456,6 +1456,9 @@ static unsigned long try_zap_anon_pte_range(struct mmu_gather *tlb, bool full; int i; + /* __tlb_remove_page drops a ref; prevent it going to 0 while in use. */ + folio_get(folio); + for (i = 0; i < nr_pages;) { ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); tlb_remove_tlb_entry(tlb, pte, addr); @@ -1476,6 +1479,8 @@ static unsigned long try_zap_anon_pte_range(struct mmu_gather *tlb, folio_remove_rmap_range(folio, page - i, i, vma); + folio_put(folio); + return i; } @@ -1526,26 +1531,33 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, */ if (page && PageAnon(page)) { struct folio *folio = page_folio(page); - int nr_pages_req, nr_pages; - nr_pages_req = folio_nr_pages_cont_mapped( - folio, page, pte, addr, end); + if (folio_test_large(folio)) { + int nr_pages_req, nr_pages; + int counter = mm_counter(page); - nr_pages = try_zap_anon_pte_range(tlb, vma, - folio, page, pte, addr, - nr_pages_req, details); + nr_pages_req = folio_nr_pages_cont_mapped( + folio, page, pte, addr, + end); - rss[mm_counter(page)] -= nr_pages; - nr_pages--; - pte += nr_pages; - addr += nr_pages << PAGE_SHIFT; + /* folio may be freed on return. */ + nr_pages = try_zap_anon_pte_range( + tlb, vma, folio, page, + pte, addr, nr_pages_req, + details); - if (unlikely(nr_pages < nr_pages_req)) { - force_flush = 1; - addr += PAGE_SIZE; - break; + rss[counter] -= nr_pages; + nr_pages--; + pte += nr_pages; + addr += nr_pages << PAGE_SHIFT; + + if (unlikely(nr_pages < nr_pages_req)) { + force_flush = 1; + addr += PAGE_SIZE; + break; + } + continue; } - continue; } ptent = ptep_get_and_clear_full(mm, addr, pte,