Received: by 2002:a05:6500:2018:b0:1fb:9675:f89d with SMTP id t24csp294995lqh; Fri, 31 May 2024 01:14:28 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV8680WhNqdnEXfoCltYBnUiAURG97qBL2l1FyoId8IQX0deng720JIZ79f1CeIVPXKXehswZlmh9UxbqfeQpYqd1xtEAvKO+0JnnVnjQ== X-Google-Smtp-Source: AGHT+IGQdME6XBBopjvw7E1qCckW1j5pKHnwzO6bu9EXqrr7OBiErl2nILNFHs5zlpp4MNk07EKZ X-Received: by 2002:a50:9f61:0:b0:57a:33ff:ff1c with SMTP id 4fb4d7f45d1cf-57a3638f9dfmr1058079a12.21.1717143268424; Fri, 31 May 2024 01:14:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717143268; cv=pass; d=google.com; s=arc-20160816; b=g3Z4Ck9OC37kzaG6fITgghc+CnZ2MRShA3ur27yVQqnHMA/guW8p4CRZ3JtbKKc74D e10Iz/Ootm3DX52PFy/mLIUNHmnuQcGGjY1JUnHxXei4UrW/NQjhcbzSBrcpYzXAYfMu mgQOm5i/IF0KkkSXeLzjHl/eFGt9RUJsieIzIV6pmRO2nsIfzGoQ7PyoC1C+0HENociq n3WYJZcfZhU+N8ZXRBpiE22UgAa36fOVVV0fCdNIgVBD1FgDfHn3alHuimiA4/9vqYim Z3PP4VOI9m3H7Ze/NkKIm/2v6u09f+0w759oORycG93AquMYyeY7JuSCny82Kh6tgAlW xanw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=fa0mzB/D4ntX5+IbbLHXMCXZ+fi5APaZ9VEgBDgsZrk=; fh=gl8dYwiJrRKBnLBmPUcglhbW6QYEAY+pkPSbddZrANs=; b=FQxMcfXJZgMHiCVBw0GcM0C43A4rLDi1kq3PTzyPXELoIh5BaMBeRWMaYlNQveJsBy 9znyO54kBKCRtRowZ2mHC2H6GsEZ7NjjXadF82wOJhJipUeFkb1+tY7pGbiV+re4XCT1 nbpGIyXDm8sTZRta32dRFV2Be62szpcdHqM69zYiswAvnfzvUQ6P3ZUpxkFEmAZrR3b5 LyJMl6ldWUnsimKRAb6UgSdaHUkpiPDeYw493Yj9A/d3JcBkpzuBgPNbyaDv0tNURixv GVBfWnRO4wbyrBKabDwNEUMvYqExfYlu2aLmd1IDqINKkjIdO1SPoFlDsXbGB2CpwmQF evuA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="R/9WTWu1"; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-196451-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-196451-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-57a31ccc8fbsi727295a12.676.2024.05.31.01.14.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 May 2024 01:14:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-196451-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="R/9WTWu1"; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-196451-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-196451-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 9AC381F29486 for ; Fri, 31 May 2024 08:14:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C215B824BF; Fri, 31 May 2024 08:13:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="R/9WTWu1" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2624B824AF for ; Fri, 31 May 2024 08:13:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.10 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717143189; cv=none; b=qpcQdoBpfAOJ/UZFxy6VrX177Ttoj/PmmV0SQrr+zah0nxsPZY71GCK9YLSaTZLbO6hF+QeHhF1W5DaHY/zbHhbfMIaNxCVHUjIQABlOl7C4vr7u9+X9aiy4BDm133pkqKSDRyLdQdPVnLJgSguIH0jplFYkxbUheEqrRU61rlk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717143189; c=relaxed/simple; bh=XtM1tmkue2R/RWq0rdfSHuRCjgaKpNa2YNSgCs9L/sc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CTNmhWrcIZbADsljZdp37wqdipBJ0dVAF5K27uCicrZ/rS3zGKpyvMPVteEH718sOfOU2I+gjff+fxFlUYhXJaGSe0ILUkGBrD0/uXAPqNRXVNa4N2NbnLwQK7vIKMQSfMuVXZAciwZN6U1vjxeQE49xHpswsRGYRZJDskIQjoc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=R/9WTWu1; arc=none smtp.client-ip=192.198.163.10 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=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: t0cJpyHcTo+Ho/wogisORg== X-CSE-MsgGUID: G5IZg+DBTRO5BL4aSNvR3w== X-IronPort-AV: E=McAfee;i="6600,9927,11088"; a="25079776" X-IronPort-AV: E=Sophos;i="6.08,203,1712646000"; d="scan'208";a="25079776" 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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); } -- 2.39.2