From nobody Mon Feb 9 05:52:18 2026 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 AC8B31CCED2 for ; Tue, 24 Dec 2024 14:39:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735051174; cv=none; b=krLPjTc1pCpcvU//Xz1x1hAv2WnqvS6MJa6qQHZ6MoXyP3T3wzAzAY+rUe//novGDNuX9IoHd4iNqpmV1LtckwU+XaoKWUXieqHQr5NGMg9ObxL2BKMdxUzJoZfc/OO7e+zjJLPNcWXe2suWkNjgnOcCGRkn6SAobEDGT9GJtMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735051174; c=relaxed/simple; bh=0zwLEl1IX5lfCSiyPNTsbs+vpbZuqKYgJ688EZCFVa0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oeAbpvw+ay6wiloYgoMLqpuM3KeSYSLol+/lAWnZz0Lwsv9p1Pa8AWY3As8LxywNwlQ0/faI5CHtGTliDL/7f3dCcVmd2I+CDVNWiwJ0K6ube/to7pgn3myUYWvDKSK+Oyvh8qJK+5BNc8wGwxw+FovHcqlNyZuTz+JhlRSkUWo= 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=G/CAbwb3; arc=none smtp.client-ip=209.85.214.172 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="G/CAbwb3" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2165cb60719so54434365ad.0 for ; Tue, 24 Dec 2024 06:39:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735051172; x=1735655972; 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=ByQEC8FsvBO2uOH/tvc24OCSxXwsTLkJSMXrTAjB9ew=; b=G/CAbwb31STjfDzPruH1pnTubhRcfSSLZRMFNvAUEqp5AV0s3rwUFQz0qFPWsssnbj TiQJTbIucFP+vJJMIVme5CM7J5O6UORkc9EHVdSm73XWQ7AMHcL3pWObjFzztYMaFfky cRayHGU34vYJr/bBIDMfUnSdGYi+60LBpqM/CqGwn61m9frC9gg8awTRjbhJwYnpj6BP vxORAeTbvWALQIjc6YNiDq0fz/uVbLM8xecT01Q9Oxq/whdRdHW7S5f35R/5ynUlH2Fd irhihbvZudhmFz4HbYAYL4yMcofpwgmSRn/EJiuw8wP0aThxBFpo+Vq04kRXPYSm3pc3 S1Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735051172; x=1735655972; 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=ByQEC8FsvBO2uOH/tvc24OCSxXwsTLkJSMXrTAjB9ew=; b=CWYsxU7A38wcR7BsqKgoNOfsDi77Om1Tqk7uh4BN/QF9KoPH6bHzr+BwaWOwZzj0OY WHTgzSdJkNl/2lfPdRHzcqk9HQ1Nw310OBAZlVUgStoFFU8yiRXc1739CEIAjgPNvLxn 5qACr1NR8Jy76Bo5MO7APJjgZ8R6oyk42OdY/8a5byLHJoikwerbL4XKlqVTXOvony4r KlnZec7q7izeLWVrQK1yeZepr7Au7AMN4mP6tgvImWmwwsJxLGSGiA41YcfuGUcO3aO8 a3Cl/islAlU4xVyFXMWtvNyDurksf/FKBvAmTH7AJ5VUvgA3NTUsuXyc2oeHbQPVo/Si WESw== X-Forwarded-Encrypted: i=1; AJvYcCUYDNnVagy4JTulwA4g5RK6FrS19i993SsjWfpKuXCLAvCRScidTX8gl0U24O1VwTJog3flrnCU0zBgeaY=@vger.kernel.org X-Gm-Message-State: AOJu0Yygu7fpNRqBBuT9XnbBERB41mtT0U2joGY1ol4DfEkqLX0KDq1X /A7W436sJN+1onypVIgm/7jqCZjCn1NJNiQjfJSoiv8K3yOUAkTy X-Gm-Gg: ASbGnctTbhKao80p/fhurhOD8HytHf0bQQb4a+wd8elXz4OX4UHU6GzYvj3tH3FTmuy 8jOXbbZ9mBEfcRFXiIf+E4qB8ySiBv6dzlIrrd+EFRDETSvxiNzMe7Li9YVuSyn8zvLZ8XsN9SV 3IVFAsaXt23C1z7jTQUMwM3v3BeHM/V8/UoTKkZcf7E88er/gccErFTr2YnRnsuTtSByRAHUIv9 Ab11RCgzS6SayKSVOzAZzGxx0xWQ/sisKM0WtJhp0oixwztarNky2UyPySNP+rlCdBefLaMUXQK 7Q== X-Google-Smtp-Source: AGHT+IERM809iT3lGHUyfSmwzz85A7RBdI6+sHZNonAv7QDefMy2AtxZq5MCVgChbGhf0YMVvbHZ6g== X-Received: by 2002:a17:902:c407:b0:216:50c6:6b42 with SMTP id d9443c01a7336-219e6f25ddbmr196266805ad.56.1735051171988; Tue, 24 Dec 2024 06:39:31 -0800 (PST) Received: from KASONG-MC4.tencent.com ([115.171.41.189]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9cdec1sm90598735ad.136.2024.12.24.06.39.28 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 24 Dec 2024 06:39:31 -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" , Nhat Pham , Johannes Weiner , Kalesh Singh , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v2 01/13] mm, swap: minor clean up for swap entry allocation Date: Tue, 24 Dec 2024 22:37:59 +0800 Message-ID: <20241224143811.33462-2-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241224143811.33462-1-ryncsn@gmail.com> References: <20241224143811.33462-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 --- 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