From patchwork Fri Jun 9 01:34:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 13273084 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 51C64C7EE29 for ; Fri, 9 Jun 2023 01:34:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E498E8E0003; Thu, 8 Jun 2023 21:34:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DFA278E0001; Thu, 8 Jun 2023 21:34:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CC28A8E0003; Thu, 8 Jun 2023 21:34:11 -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 BFAA68E0001 for ; Thu, 8 Jun 2023 21:34:11 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 494F6803AF for ; Fri, 9 Jun 2023 01:34:11 +0000 (UTC) X-FDA: 80881488702.21.37ACF34 Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) by imf30.hostedemail.com (Postfix) with ESMTP id 6395E80004 for ; Fri, 9 Jun 2023 01:34:08 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Oi7q9zQX; spf=pass (imf30.hostedemail.com: domain of hughd@google.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=hughd@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=1686274448; 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=wdvmzniCtM+nQioqQdrwu6IzqdoeozaKC+WtG0d7jzc=; b=CWBurM42/6GZon9eDX+WV+jKj0cBPriCuWp6JjmQCuc9I4w3v38BtAQME80PpZqRidLTW7 PB3HHTLpUpVKhTmlpFH2du2l8TGLggMKCpoDuQTMD8nk1W2zGVCmopf+exmVvM7F71Nn9+ YStWaWyIn+qOMY2EUxmSULlU1qLvtBA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686274448; a=rsa-sha256; cv=none; b=ldeHSPgNJPfoL1Xq8WPp6iXuiGVnwIvPgmBSITGOPJsPjgevZumV/WPZ3oToQJVyUGrgL4 yUw8C6DZektprf7XyH2eMnOdm/XWhMAkse3OzTjgzEFJyUm5UoplaX40ZYoE1G3hbNfcgX /CRH4hUiQn+NM5NQ2AHz+4C3vr8zu9U= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Oi7q9zQX; spf=pass (imf30.hostedemail.com: domain of hughd@google.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-565e6beb7aaso11623917b3.2 for ; Thu, 08 Jun 2023 18:34:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686274447; x=1688866447; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=wdvmzniCtM+nQioqQdrwu6IzqdoeozaKC+WtG0d7jzc=; b=Oi7q9zQXAnHzVKo/FhCPs8tzz9nacAj7GeJ1aGGBUrWEYASbA/COShPsGhE7TSY1ih bxVmRvKdrmFR5jXzZRTgemTvq61YU0XHRU70b1d3QdbaFwgY+kX5E2pxmuHyonISfka/ 2mIjcxl1BRTJLCdi9tDPNj4z8xlbHAAh2ERkE6DkidseXIGdbxnscT3F7ARVo5KkhPVF m5RsG7gDmqBonOEOuZYs+SmyuTOVvsJthOrxfrS4j3QsaT2MI33gyJZxrSrSJZSC8FPD R5ZnuYh4R1BXHnQy+G2iJxg5x1Uo6TM759fPwJnBzBOJAwDjI9gtM3K7QZ52HU2U68va CGEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686274447; x=1688866447; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wdvmzniCtM+nQioqQdrwu6IzqdoeozaKC+WtG0d7jzc=; b=kHXoRF5mHLigMV3pzHs+F+ZJJQwXXSTe8w6t3lWvbX1k8IEFMyDPc8cKc9KgeODGCN ZczHLXEUrZFIHG3a2I/rN4zHkNOtCaahX7zy2fJNPj0lxfb++fWUZtPN770U9jciTnsk w1ffcspi1hmCwwup6o/Ak2nmhBVFrRARbPA4KhBBDoIWkFBCivIZQrMn7wnv4ZwlTsNf 68TSR133173cK0DTno9bzpk1WaHFNOfgMz5+nEPSZWVcd4Kh2q2d6/RrohWoWXDRcjA+ Q4jaymgaHfn2TnEFzBPNOGRKYUjxBl6bz67v9CaugxGcnsY2iKpvNJeevFEAm4dKNV9B 87yg== X-Gm-Message-State: AC+VfDzaGmmhcYinzeJlHt7GyNPqS8K30cU4JASvzBhAOpaoeRDWawth rwbR6ZnrqEND8FkNCayHneGSUg== X-Google-Smtp-Source: ACHHUZ5Q/YHvHVewBNkmQMz1HqeMOeXDnHdCMp674SssIxi8mNPsiGfn7zCsWmM3qzcif+cYyekxpQ== X-Received: by 2002:a0d:cb50:0:b0:54d:ea34:c31 with SMTP id n77-20020a0dcb50000000b0054dea340c31mr1301109ywd.29.1686274447361; Thu, 08 Jun 2023 18:34:07 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id q67-20020a818046000000b0054f83731ad2sm314580ywf.0.2023.06.08.18.34.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 18:34:06 -0700 (PDT) Date: Thu, 8 Jun 2023 18:34:03 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Mike Kravetz , Mike Rapoport , "Kirill A. Shutemov" , Matthew Wilcox , David Hildenbrand , Suren Baghdasaryan , Qi Zheng , Yang Shi , Mel Gorman , Peter Xu , Peter Zijlstra , Will Deacon , Yu Zhao , Alistair Popple , Ralph Campbell , Ira Weiny , Steven Price , SeongJae Park , Lorenzo Stoakes , Huang Ying , Naoya Horiguchi , Christophe Leroy , Zack Rusin , Jason Gunthorpe , Axel Rasmussen , Anshuman Khandual , Pasha Tatashin , Miaohe Lin , Minchan Kim , Christoph Hellwig , Song Liu , Thomas Hellstrom , Ryan Roberts , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 20/32] mm/madvise: clean up pte_offset_map_lock() scans In-Reply-To: Message-ID: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 6395E80004 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: asfyxd7ce3nnwjqy6bu47cyguudjhyhr X-HE-Tag: 1686274448-215513 X-HE-Meta: U2FsdGVkX18ijAGWz9KVmVYZHpODzz91VlSBkD7j/ijsnsl9Igb63vRKWGI4yGOGSiXfzOabZSz1DZHGS2N09gJa6bbWt2RpsAVW5qe4JmMTSppRJR6+3oufJg1OoRGCRylnyS1K96uNHpFMpWx1O2nlJdGMYU0+in0zpaC2wb3LUeOzZofct0e7bCw7IoHab6w7Wj6xWnCJnEHxmgqRkaWwVMxMUMuUbfTgNx4s9MTFy+sEi4WxCLK8I6vSSYaCu6Xo/58B2vZiFWCJMcv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b9OQWwE4ynn9v/EED0a6dFvpjzyQt3/DRY06bm96ZOP+4HTxYufrSAGdCYYWtBRZk4ifF3Whbfohog7uYIQ== 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: Came here to make madvise's several pte_offset_map_lock() scans advance to next extent on failure, and remove superfluous pmd_trans_unstable() and pmd_none_or_trans_huge_or_clear_bad() calls. But also did some nearby cleanup. swapin_walk_pmd_entry(): don't name an address "index"; don't drop the lock after every pte, only when calling out to read_swap_cache_async(). madvise_cold_or_pageout_pte_range() and madvise_free_pte_range(): prefer "start_pte" for pointer, orig_pte usually denotes a saved pte value; leave lazy MMU mode before unlocking; merge the success and failure paths after split_folio(). Signed-off-by: Hugh Dickins --- mm/madvise.c | 122 ++++++++++++++++++++++++++++----------------------- 1 file changed, 68 insertions(+), 54 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index b5ffbaf616f5..0af64c4a8f82 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -188,37 +188,43 @@ static int madvise_update_vma(struct vm_area_struct *vma, #ifdef CONFIG_SWAP static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start, - unsigned long end, struct mm_walk *walk) + unsigned long end, struct mm_walk *walk) { struct vm_area_struct *vma = walk->private; - unsigned long index; struct swap_iocb *splug = NULL; + pte_t *ptep = NULL; + spinlock_t *ptl; + unsigned long addr; - if (pmd_none_or_trans_huge_or_clear_bad(pmd)) - return 0; - - for (index = start; index != end; index += PAGE_SIZE) { + for (addr = start; addr < end; addr += PAGE_SIZE) { pte_t pte; swp_entry_t entry; struct page *page; - spinlock_t *ptl; - pte_t *ptep; - ptep = pte_offset_map_lock(vma->vm_mm, pmd, index, &ptl); + if (!ptep++) { + ptep = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); + if (!ptep) + break; + } + pte = *ptep; - pte_unmap_unlock(ptep, ptl); - if (!is_swap_pte(pte)) continue; entry = pte_to_swp_entry(pte); if (unlikely(non_swap_entry(entry))) continue; + pte_unmap_unlock(ptep, ptl); + ptep = NULL; + page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE, - vma, index, false, &splug); + vma, addr, false, &splug); if (page) put_page(page); } + + if (ptep) + pte_unmap_unlock(ptep, ptl); swap_read_unplug(splug); cond_resched(); @@ -340,7 +346,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, bool pageout = private->pageout; struct mm_struct *mm = tlb->mm; struct vm_area_struct *vma = walk->vma; - pte_t *orig_pte, *pte, ptent; + pte_t *start_pte, *pte, ptent; spinlock_t *ptl; struct folio *folio = NULL; LIST_HEAD(folio_list); @@ -422,11 +428,11 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, } regular_folio: - if (pmd_trans_unstable(pmd)) - return 0; #endif tlb_change_page_size(tlb, PAGE_SIZE); - orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); + start_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); + if (!start_pte) + return 0; flush_tlb_batched_pending(mm); arch_enter_lazy_mmu_mode(); for (; addr < end; pte++, addr += PAGE_SIZE) { @@ -447,25 +453,28 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, * are sure it's worth. Split it if we are only owner. */ if (folio_test_large(folio)) { + int err; + if (folio_mapcount(folio) != 1) break; if (pageout_anon_only_filter && !folio_test_anon(folio)) break; + if (!folio_trylock(folio)) + break; folio_get(folio); - if (!folio_trylock(folio)) { - folio_put(folio); - break; - } - pte_unmap_unlock(orig_pte, ptl); - if (split_folio(folio)) { - folio_unlock(folio); - folio_put(folio); - orig_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); - break; - } + arch_leave_lazy_mmu_mode(); + pte_unmap_unlock(start_pte, ptl); + start_pte = NULL; + err = split_folio(folio); folio_unlock(folio); folio_put(folio); - orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); + if (err) + break; + start_pte = pte = + pte_offset_map_lock(mm, pmd, addr, &ptl); + if (!start_pte) + break; + arch_enter_lazy_mmu_mode(); pte--; addr -= PAGE_SIZE; continue; @@ -510,8 +519,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, folio_deactivate(folio); } - arch_leave_lazy_mmu_mode(); - pte_unmap_unlock(orig_pte, ptl); + if (start_pte) { + arch_leave_lazy_mmu_mode(); + pte_unmap_unlock(start_pte, ptl); + } if (pageout) reclaim_pages(&folio_list); cond_resched(); @@ -612,7 +623,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, struct mm_struct *mm = tlb->mm; struct vm_area_struct *vma = walk->vma; spinlock_t *ptl; - pte_t *orig_pte, *pte, ptent; + pte_t *start_pte, *pte, ptent; struct folio *folio; int nr_swap = 0; unsigned long next; @@ -620,13 +631,12 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, next = pmd_addr_end(addr, end); if (pmd_trans_huge(*pmd)) if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next)) - goto next; - - if (pmd_trans_unstable(pmd)) - return 0; + return 0; tlb_change_page_size(tlb, PAGE_SIZE); - orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); + start_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); + if (!start_pte) + return 0; flush_tlb_batched_pending(mm); arch_enter_lazy_mmu_mode(); for (; addr != end; pte++, addr += PAGE_SIZE) { @@ -664,23 +674,26 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, * deactivate all pages. */ if (folio_test_large(folio)) { + int err; + if (folio_mapcount(folio) != 1) - goto out; + break; + if (!folio_trylock(folio)) + break; folio_get(folio); - if (!folio_trylock(folio)) { - folio_put(folio); - goto out; - } - pte_unmap_unlock(orig_pte, ptl); - if (split_folio(folio)) { - folio_unlock(folio); - folio_put(folio); - orig_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); - goto out; - } + arch_leave_lazy_mmu_mode(); + pte_unmap_unlock(start_pte, ptl); + start_pte = NULL; + err = split_folio(folio); folio_unlock(folio); folio_put(folio); - orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); + if (err) + break; + start_pte = pte = + pte_offset_map_lock(mm, pmd, addr, &ptl); + if (!start_pte) + break; + arch_enter_lazy_mmu_mode(); pte--; addr -= PAGE_SIZE; continue; @@ -725,17 +738,18 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, } folio_mark_lazyfree(folio); } -out: + if (nr_swap) { if (current->mm == mm) sync_mm_rss(mm); - add_mm_counter(mm, MM_SWAPENTS, nr_swap); } - arch_leave_lazy_mmu_mode(); - pte_unmap_unlock(orig_pte, ptl); + if (start_pte) { + arch_leave_lazy_mmu_mode(); + pte_unmap_unlock(start_pte, ptl); + } cond_resched(); -next: + return 0; }