From patchwork Fri May 31 08:12:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13681306 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 74378C27C4F for ; Fri, 31 May 2024 08:13:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 003866B0095; Fri, 31 May 2024 04:13:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF52E6B0098; Fri, 31 May 2024 04:13:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D96006B0099; Fri, 31 May 2024 04:13:10 -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 B7CA16B0095 for ; Fri, 31 May 2024 04:13:10 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 69588C0EEA for ; Fri, 31 May 2024 08:13:10 +0000 (UTC) X-FDA: 82177975740.12.2B9169D Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) by imf18.hostedemail.com (Postfix) with ESMTP id 4F81C1C0016 for ; Fri, 31 May 2024 08:13:08 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="R/9WTWu1"; spf=pass (imf18.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.10 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717143188; 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:in-reply-to:references:references:dkim-signature; bh=fa0mzB/D4ntX5+IbbLHXMCXZ+fi5APaZ9VEgBDgsZrk=; b=XDuZJqPF/0JjCin/kO1lUrx/IgnWDpK2vOHr+YnJufQEX6UM178rqFtAzhjktothvlzF/J SaXteOPosD8yKPH4nbTnpizTyJMMfNUFCZrAE4OLkplJWIv30usP5b4KDHv4nxnDGgJq2v PcXcEr5CUij9MlIvgcEB+Rlhv2NwCac= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717143188; a=rsa-sha256; cv=none; b=ODGjbqXQaV7pueLoDwYAMUDCXzdrP7a0JVp3AxRhJkdNyanCm98kzywdgL+x9bjf+Vqy1w 8I9lYb/Ec9Qi5Kd47T6B5uJwnAA/o5kigOkK396ECeNrkxQ+jOcJgti+YwI1rRfn84Z4BD pf9RXdkDl+dwrTApgmgquyhCeCLh5Ns= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="R/9WTWu1"; spf=pass (imf18.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.10 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1717143188; x=1748679188; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XtM1tmkue2R/RWq0rdfSHuRCjgaKpNa2YNSgCs9L/sc=; b=R/9WTWu1EzTlAB+QM7FQOXTvjBF/ATXu2UeYXda6FNFHmRZqgTFHUCPR zW7LoPb/t+NvNXj6u6GLSKvDbIU5H7OARImkLd2pkNETaqwoPOxHMV7aQ FiDHDLcnD2483hjOIIMgiVqI4pMq5PelCMYCpezng6FFgd6I7teTnCN1H SSs0vTagduQT/m2BuElsEumNITmFLcGauHaBqZblqXj16HWCK9axbj7/O rIRmXU9wjuW7dALPP0aJ3OkabLmMXyZoRzRRVkuZUL8vb2y3Nf2vtMvO0 lka+9rGYMyFUoxrfY5wtqTMCDciPU5cDksD9PJphv9/dT3hFosM+AtAZS w==; X-CSE-ConnectionGUID: E52KlOp9TOqN4bjxKVXyWw== X-CSE-MsgGUID: aefUvbj5ScmtiyYT2CombA== X-IronPort-AV: E=McAfee;i="6600,9927,11088"; a="25079778" X-IronPort-AV: E=Sophos;i="6.08,203,1712646000"; d="scan'208";a="25079778" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 01:13:08 -0700 X-CSE-ConnectionGUID: ObA5obvcTXa5rgNCCMgFCQ== X-CSE-MsgGUID: rJElwWo5T9qLZMt+cdpi/g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,203,1712646000"; d="scan'208";a="59270962" Received: from unknown (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.30.35]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 01:13:04 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Hugh Dickins , Alistair Popple , Anshuman Khandual , David Hildenbrand , Mel Gorman , Miaohe Lin , Minchan Kim , Ryan Roberts , Yang Shi , Yu Zhao , Kairui Song , Barry Song , Chris Li , Yosry Ahmed Subject: [PATCH 3/3] mm,swap: simplify VMA based swap readahead window calculation Date: Fri, 31 May 2024 16:12:30 +0800 Message-Id: <20240531081230.310128-4-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240531081230.310128-1-ying.huang@intel.com> References: <20240531081230.310128-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: ar455nxa3dwee89nb5arn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2hJu9JX1RsJuQuXV58QQL5IeoS5c48tt+z1xgBhk/JOCQ/SOw4bJVi1ca1/FEUbrXRPe9/R3sFC6Ly5zZyRToZ133P7gIKpalPN0Gt+YmuvG1X3+8O4KoUQKYW3f9S5Gb1kQjgcFuFsnHvLIfbb9IdwrjnpzbGe7tqCplHOKantNC82UFJRMjof2bcXkHNEzKn1raTXyWM6XoS7VxyXAxwmw7lIX+ze9f4VMSA3G6R0Mt2Iqk+nD3XT0g/hKC2wc8wDVvdhTP3pKg/jiP++uCa4sTW+zkSB5TZG0rAExKix6vVRqaYGNF7e0yLBmDkmQz0RXjlq5rxuklB4VPw1+1nxAsFw8GlFXaxAc6DhzwRKoEm+oXNFtRS+QxBrTR33TV2fT8lvxrD6qGk0tgFeel9OBnSMu1iPP4nwhOWOn64zshbpvFnWjT6Hxv2Rn8mPWR8VRHsSjPoD82SkKpIrcpHW1bM/TtOmYYyR//C9wcbd4MLZLhHzahbAbJStn8wiWnVvzKMPpazOsf+hBWhhBwdZi3QTOIc2636DBa0X31CZCTncDMh4nGoVr 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: List-Subscribe: List-Unsubscribe: Replace PFNs with addresses in readahead window calculation. This simplified the logic and reduce the code line number. No functionality change is expected. Signed-off-by: "Huang, Ying" Cc: Hugh Dickins Cc: Alistair Popple Cc: Anshuman Khandual Cc: David Hildenbrand Cc: Mel Gorman Cc: Miaohe Lin Cc: Minchan Kim Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao Cc: Kairui Song Cc: Barry Song Cc: Chris Li Cc: Yosry Ahmed --- mm/swap_state.c | 66 +++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index e1dac70198a6..d2adbd7b571b 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -740,54 +740,40 @@ void exit_swap_address_space(unsigned int type) swapper_spaces[type] = NULL; } -static unsigned short swap_vma_ra_win(struct vm_fault *vmf, - unsigned short *offset, - unsigned short *nr_pte) +static int swap_vma_ra_win(struct vm_fault *vmf, unsigned long *start, + unsigned long *end) { struct vm_area_struct *vma = vmf->vma; unsigned long ra_val; - unsigned long faddr, pfn, fpfn, lpfn, rpfn; - unsigned long start, end; + unsigned long faddr, prev_faddr, left, right; unsigned int max_win, hits, prev_win, win; - max_win = 1 << min_t(unsigned int, READ_ONCE(page_cluster), - SWAP_RA_ORDER_CEILING); + max_win = 1 << min(READ_ONCE(page_cluster), SWAP_RA_ORDER_CEILING); if (max_win == 1) return 1; faddr = vmf->address; - fpfn = PFN_DOWN(faddr); ra_val = GET_SWAP_RA_VAL(vma); - pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val)); + prev_faddr = SWAP_RA_ADDR(ra_val); prev_win = SWAP_RA_WIN(ra_val); hits = SWAP_RA_HITS(ra_val); - win = __swapin_nr_pages(pfn, fpfn, hits, max_win, prev_win); - atomic_long_set(&vma->swap_readahead_info, - SWAP_RA_VAL(faddr, win, 0)); + win = __swapin_nr_pages(PFN_DOWN(prev_faddr), PFN_DOWN(faddr), hits, + max_win, prev_win); + atomic_long_set(&vma->swap_readahead_info, SWAP_RA_VAL(faddr, win, 0)); if (win == 1) return 1; - if (fpfn == pfn + 1) { - lpfn = fpfn; - rpfn = fpfn + win; - } else if (pfn == fpfn + 1) { - lpfn = fpfn - win + 1; - rpfn = fpfn + 1; - } else { - unsigned int left = (win - 1) / 2; - - lpfn = fpfn - left; - rpfn = fpfn + win - left; - } - if ((long)lpfn < 0) - lpfn = 0; - start = max3(lpfn, PFN_DOWN(vma->vm_start), - PFN_DOWN(faddr & PMD_MASK)); - end = min3(rpfn, PFN_DOWN(vma->vm_end), - PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE)); - - *nr_pte = end - start; - *offset = fpfn - start; + if (faddr == prev_faddr + PAGE_SIZE) + left = faddr; + else if (prev_faddr == faddr + PAGE_SIZE) + left = faddr - (win << PAGE_SHIFT) + PAGE_SIZE; + else + left = faddr - (((win - 1) / 2) << PAGE_SHIFT); + right = left + (win << PAGE_SHIFT); + if ((long)left < 0) + left = 0; + *start = max3(left, vma->vm_start, faddr & PMD_MASK); + *end = min3(right, vma->vm_end, (faddr & PMD_MASK) + PMD_SIZE); return win; } @@ -815,22 +801,20 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, struct swap_iocb *splug = NULL; struct folio *folio; pte_t *pte = NULL, pentry; - unsigned long addr; + int win; + unsigned long start, end, addr; swp_entry_t entry; pgoff_t ilx; - unsigned int i; bool page_allocated; - unsigned short win, nr_pte, offset; - win = swap_vma_ra_win(vmf, &offset, &nr_pte); + win = swap_vma_ra_win(vmf, &start, &end); if (win == 1) goto skip; - addr = vmf->address - offset * PAGE_SIZE; - ilx = targ_ilx - offset; + ilx = targ_ilx - PFN_DOWN(vmf->address - start); blk_start_plug(&plug); - for (i = 0; i < nr_pte; i++, ilx++, addr += PAGE_SIZE) { + for (addr = start; addr < end; ilx++, addr += PAGE_SIZE) { if (!pte++) { pte = pte_offset_map(vmf->pmd, addr); if (!pte) @@ -850,7 +834,7 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, continue; if (page_allocated) { swap_read_folio(folio, false, &splug); - if (i != offset) { + if (addr != vmf->address) { folio_set_readahead(folio); count_vm_event(SWAP_RA); }