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: 13329964 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 313D8C0015E for ; Thu, 27 Jul 2023 11:02:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD9AB6B0072; Thu, 27 Jul 2023 07:02:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A89446B0078; Thu, 27 Jul 2023 07:02:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 977D96B007B; Thu, 27 Jul 2023 07:02:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 88DFD6B0072 for ; Thu, 27 Jul 2023 07:02:38 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5D1CE161012 for ; Thu, 27 Jul 2023 11:02:38 +0000 (UTC) X-FDA: 81057103596.20.E02A769 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf09.hostedemail.com (Postfix) with ESMTP id 09EE5140015 for ; Thu, 27 Jul 2023 11:02:34 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf09.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690455755; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=ei3or1z9wzS0d961tsHrEkUjs0WJucw6hIc9jgD9iPE=; b=nUUZmwB2ITtzD823F0FUkIBjoD5fNb0WVdQ9jSeFe7rJ/NoY+P0ITACjjU9UZUZKPr/vUn nPTbnMFPOvGDYQaM0VAnw+6jsgA27prgJt3xJCZICy4+LR7b2Ooz4H9AJvTffTGyTjpJOA lUgmrAqb71YdgC03nfmEfQVMwqSfOis= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf09.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690455755; a=rsa-sha256; cv=none; b=Xof0Aq9EQkytKqt3vSKYaJo0cF6ZzhGDvX/uny50aIAL5T57SFZjPMxLmpltoHZog5a5zl 5Qj/6RM5d2a6UllHp6/LEu2cDw4yJsbr74MiSiJ18diRubtDZNMdoYMM9Np575H57qdyuq qZMZ3frjznCKM2dWLTBIfeZjQBxrAgc= 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-Rspamd-Queue-Id: 09EE5140015 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: dqbmpuk9ctw6izqd9otte3r4jb6wgowo X-HE-Tag: 1690455754-264036 X-HE-Meta: U2FsdGVkX1/o0woqC0wkBc67V/CXA3JY73M3dsFvZfzCb2iPjZG9cxBT5ETAvwh6aIEUjrNhRaZi/l5fHE723vFfnnwubxV8KXoST67EdX36ezzkBBWsxDeAliSkzue8J183Brmxjo6+aDCbFVyr+77VAdS53qZooCu6YNdVYfyRqCleFHlga7MI92liZTeWn1ojcGyw+Y4zilvd5BEacU7+b11Ve2vRiaRu4oKNKBORQxQqu4uKbv1rpFxCu3P5NEKJDrSuB1IDj1EuQMqXcbp6sxeXWCKi5/uyEylqN1vUMoE5tf+Mv8yvmvwtd0lHNwbNhGUYfxnyoiZjRkNUwBe9sBJd1OpC1XPTdfrJL0lmf/OGNL1qyfHSDK4rCYh5742Gz8AyeV723rHj7ndq2CHcobB3pNj3QZmD54G+y8zz4GZHolVvjJrrHvkq6Me922F80WTztIp9q6DI7bVwUw51h3ZcgwgNVVW68OLZHySLSuXjCFBkBSXSxWgWRhpMBejJWMnJMfne4yX+nAhjPA9958efhXesSi+j1V6pOKlwLWanVUmAWrB0XtroNECKvklQd8SObh3suWpeXj/IoPiLF18zVwYtIto7ntzrHmv32CeQN4P858OERTqifspCtdSoCrTQdvpRYkOuHD5vo+y+tIhvuq3RRPBhUup0Rf/RCU6aMxCqu77ipLFe0//LFl01OX6282Wsz7CB+jw+96pJWlorCOTshfUJvK2oU4AVuADleRnQj4JI7E8a0Fu82wSeO8cQtjc4i61qnpSLRxvG0WfaCUW5umxAS45GWjryqprg4nTSNNQW6IP4L8zE1YcEpQaVvkS1LU9+GyCp69hOH+InmBi11KsSpqfGfTSejIvTWLXf/amcEdNKzzLMr8RjEt3hnrgKHQGjyEfPxaEQ4C4PUVExVutdTJoyFUSvtr3tJwG1Z4j6b6ut6k/qv+UnlFh5MZ2KJ8BCIZn g+xGkNT9 I5cc3j8eZ0h56WSGzlaAUEcxIKhhenTb2/ty9sHdbxBlhzCUkWl5GaAwAEKfD6Kdm4Byu3yCQzQeTudQBpoNZFJoM9fMT6yQgvVjPZ8oEf7kTx769ukxZUBJ0ybP2ru5r0XRif1y1XQGLW5yJMhpgRGI5gX7GODP5/4nFW/wIhTeRuYKRkWlhr2AOx9aCu5NsFYWbAuZv/49gi0U2uYq9BniJfKMUk9WXqgkENyKaFfQhwQ6dM5tGnpA3FtA55iNfYq1f7l/TJT8TdQ0= 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: 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,