From nobody Thu Dec 18 20:35:32 2025 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 939931C1F27 for ; Mon, 13 Jan 2025 17:59:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736791196; cv=none; b=SLOtN/1waA70UfxRcBd6nYo5XWFgmKL9cQPR5srQeavOCxWi5R8pSZmKCvrPrIlGoUuANKVG1+Lrc8SjppgNz3CwZuinMB7v4ZnYN/79Eqd1vpKze3brK3gdWPWu4SzpPh6b5OvdE8zma7UwQHgZOD3MA5E9fvno7+Ztzt7MeTI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736791196; c=relaxed/simple; bh=gh/4+Jxj/+y8cvOwuEPBuA7ZivYFEDiTwJlpVHum0Iw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TFw89aETqOSHAGrwR/2EqU/R6dsQqv/d9MHKsGypw52iZ5F67ke9EfuKeUFsJb03Gp6m/d2T70C7MnrUe1KAHGgr1owhHoGAlUO3ClSM97GPd+J/Q//q/UmMtnO36Ghp36r+TXULtNhPvoFoSyrfIvv0o2BiufSsRAY7OzL0eUs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QSopeymt; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QSopeymt" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2165448243fso96850365ad.1 for ; Mon, 13 Jan 2025 09:59:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736791194; x=1737395994; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=3bA8B6i+M3WoYB0+zo3p6m39muwwLLEWjZCQybnFwG0=; b=QSopeymt9Nv7/v8Z7p3O0M+KwOvSKYkQ5KrorUswncip8rkcZfIuAuB682rOOCaIlS ACXdVKRMokETOVpYpGB4P+LcHaw+b06BdcTmzg31VcDoj39PW5CUrAqspohItIK2CfaZ nrKtUbck3kVS1LrYeiIfrkIXefmxEGGQaWHIF94tmpphLCqNdZ1kMG/pCbNi17Fjfkl0 OJD7P2eVlv+LpGGY5LM7afOcWe8AdNTwfMV2GH40LsAEY+drUtqVWaU1zm4/uJ/zzerP I3KB+tMVrGBh2O8VlVS3/ll8ZLeudCiW3GsWEjcE0IC49AukDY2kjY4AOYvaj/GwJdkw BKrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736791194; x=1737395994; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=3bA8B6i+M3WoYB0+zo3p6m39muwwLLEWjZCQybnFwG0=; b=iJ/30cXMopV6oetZP1JeWvhSF7UZm3Hk2t4to5OMmY4wsEM6YgHsiaDU/iV2GqxKCB a4Tu6ud9pm6JTPmU00W5OCPXQ8fYsuI/yQSHpS/mqE+ohgL1nBN8X0p5KMJOCzZt5WFB iN6vfU2l0z/+JnRiUi9CSHH7z6fKMM/v1amHN+W/vxlthY3DJzzwRRyWg9coEJwrHDsJ 3cdTnF+dBiCx/G2VU0yFl6ICyQ7EegkdAn2zM8lb/IE+yElALPebdIebd8+gd13oQgar 4uiz/v+OWMS9IZF6brKVnNWjRCm8gNUyaLPxWpfjPushGE4FmwSgcDUxZXu731aDTbVn tMzQ== X-Forwarded-Encrypted: i=1; AJvYcCXe8MT/HTvvg8EDueFCjg/c7QJJYxi2RY8gk6aLFNW2Er2A1A87W+y7nLy2ACWPfTwVWzrGCTTuMThXcx0=@vger.kernel.org X-Gm-Message-State: AOJu0YzHNJ6H9OyrsNxED8HH6XxUUGIAsWpV/XbaEnA3eJyxsweTq7wo Z+ZY0HFEl36NliLphsWsxj6rOhcqezL/5o576/DyN1pV2GIPoC3yy114MB0/5wE= X-Gm-Gg: ASbGnct1nksD1JilCrWkjLMexFep68iXnHJYxeyKxk1C8CVxGTFFYcgX7nYywQuujQ2 nJ+OtozQKgf42vZYJ78RJy/m0KPVoEF4cWJBj6QrrmDJ02acwTZ1bn6/dzaXUz8c4GBkicV5jrE WdL6z9ByO+34Ky+3XZy+SqgtqLqGtE2y8loQITI26mgpBZV5NF1IzxdW2JyWFVwz225mCbjCPGp W6UR0Gfh3MTcJ0qCPOftBjH5qLEpejKRGcHc5qyVaiekLw3v7H1LMXXCCVGpPtae51qapNuhmPd UQ== X-Google-Smtp-Source: AGHT+IHG9J+kj0NwuV4dCCUaflW/hQEMT5mplvb1rxDF2wEH2kzs02TDJ+5TN5Jg0cDR5NohlVyXfw== X-Received: by 2002:a17:903:41c4:b0:212:5786:7bb6 with SMTP id d9443c01a7336-21a83f469b8mr301683785ad.3.1736791193793; Mon, 13 Jan 2025 09:59:53 -0800 (PST) Received: from KASONG-MC4.tencent.com ([115.171.41.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f21aba7sm57023635ad.113.2025.01.13.09.59.50 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 09:59:53 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Ryan Roberts , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Baoquan He , Nhat Pham , Johannes Weiner , Kalesh Singh , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v4 01/13] mm, swap: minor clean up for swap entry allocation Date: Tue, 14 Jan 2025 01:57:20 +0800 Message-ID: <20250113175732.48099-2-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250113175732.48099-1-ryncsn@gmail.com> References: <20250113175732.48099-1-ryncsn@gmail.com> Reply-To: Kairui Song Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kairui Song Direct reclaim can skip the whole folio after reclaimed a set of folio based slots. Also simplify the code for allocation, reduce indention. Signed-off-by: Kairui Song Reviewed-by: Baoquan He --- mm/swapfile.c | 59 +++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index b0a9071cfe1d..f8002f110104 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -604,23 +604,28 @@ static bool cluster_reclaim_range(struct swap_info_st= ruct *si, unsigned long start, unsigned long end) { unsigned char *map =3D si->swap_map; - unsigned long offset; + unsigned long offset =3D start; + int nr_reclaim; =20 spin_unlock(&ci->lock); spin_unlock(&si->lock); =20 - for (offset =3D start; offset < end; offset++) { + do { switch (READ_ONCE(map[offset])) { case 0: - continue; + offset++; + break; case SWAP_HAS_CACHE: - if (__try_to_reclaim_swap(si, offset, TTRS_ANYWAY | TTRS_DIRECT) > 0) - continue; - goto out; + nr_reclaim =3D __try_to_reclaim_swap(si, offset, TTRS_ANYWAY | TTRS_DIR= ECT); + if (nr_reclaim > 0) + offset +=3D nr_reclaim; + else + goto out; + break; default: goto out; } - } + } while (offset < end); out: spin_lock(&si->lock); spin_lock(&ci->lock); @@ -838,35 +843,30 @@ static unsigned long cluster_alloc_swap_entry(struct = swap_info_struct *si, int o &found, order, usage); frags++; if (found) - break; + goto done; } =20 - if (!found) { + /* + * Nonfull clusters are moved to frag tail if we reached + * here, count them too, don't over scan the frag list. + */ + while (frags < si->frag_cluster_nr[order]) { + ci =3D list_first_entry(&si->frag_clusters[order], + struct swap_cluster_info, list); /* - * Nonfull clusters are moved to frag tail if we reached - * here, count them too, don't over scan the frag list. + * Rotate the frag list to iterate, they were all failing + * high order allocation or moved here due to per-CPU usage, + * this help keeping usable cluster ahead. */ - while (frags < si->frag_cluster_nr[order]) { - ci =3D list_first_entry(&si->frag_clusters[order], - struct swap_cluster_info, list); - /* - * Rotate the frag list to iterate, they were all failing - * high order allocation or moved here due to per-CPU usage, - * this help keeping usable cluster ahead. - */ - list_move_tail(&ci->list, &si->frag_clusters[order]); - offset =3D alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); - frags++; - if (found) - break; - } + list_move_tail(&ci->list, &si->frag_clusters[order]); + offset =3D alloc_swap_scan_cluster(si, cluster_offset(si, ci), + &found, order, usage); + frags++; + if (found) + goto done; } } =20 - if (found) - goto done; - if (!list_empty(&si->discard_clusters)) { /* * we don't have free cluster but have some clusters in @@ -904,7 +904,6 @@ static unsigned long cluster_alloc_swap_entry(struct sw= ap_info_struct *si, int o goto done; } } - done: cluster->next[order] =3D offset; return found; --=20 2.47.1