From nobody Thu Dec 18 20:35:31 2025 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 31D7F1C4A20 for ; Mon, 13 Jan 2025 18:00:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736791204; cv=none; b=GVY3o0SFJlzY89nji9VIPoShztyV/lAod0ZcINcgdrvVeVqUkVNtrDPACVj0yuZwg4VOP8xsqXuqMFht37FMB05XV76R6p9NNhoUCyROoQ1QPcXOhJy7zl6aCFIeW8SU6MEwreOz+2HTG0Eh0qoGK+AUEqNDaxTg6o5JiJY6huo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736791204; c=relaxed/simple; bh=SZLMFCU6NCGPPuCVjV6RfRC7/B5x3lzZXtPUqGogpDI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZKU3p7w2AEvxGkGuQmGrDJxhye3/VCKmlkSBZg9lboILWr5gvoI+mGJkcgTHYQQEEjp6CaBHT9yIrEllHa5pkBCgggPDGZIl673fXOs5hC+3IFaIFB9pCObDWl60tOmGSHcFJcUUn3+GR2ZKhW+p0bhvRD9QexAfLQPN1ld0r48= 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=ef541Elp; arc=none smtp.client-ip=209.85.214.179 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="ef541Elp" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-219f8263ae0so75220005ad.0 for ; Mon, 13 Jan 2025 10:00:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736791201; x=1737396001; 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=1QSJCLnDWZKCsPmvpnvEILycrNkHAmJg2j2a6zzrUYg=; b=ef541ElpIsAJC3EvlsVuTLHvJ2RWL8K6GKJZdMHHPoSOSDiCGuTtySqwDkhdcu9h36 aKBCcZw3VMTNCsaCRaP1iGsunprNKQz4UCd8zQ8kbhCjv4shLT6daOTdi04aKgfJe8b/ 9JuxFo5o9V7tTlusayjWXvo2+n+R5paa8dJMLbeAkoOXTpYegF49L7B7JYuQdEEfmJTh RigKya0kVdKMaKazjjHoPswbFYvOaH1+8aZmClcJ1vQDxdA5XHua9c4ztwdRMSPpEAKK QZ/Wgbo7J5JT+k8lmhCbI6g6HdRw5JRrHm0DZkoM6DI2XM9SrEcqYdy6nA2deiFXihol m0/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736791201; x=1737396001; 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=1QSJCLnDWZKCsPmvpnvEILycrNkHAmJg2j2a6zzrUYg=; b=spFVGh661wIfXI5eI+0wk9ErFrHMvtd2X+W0pQGhdiFHzXWNHNfnNTxhEkxhGwkmHt HOvVTHIPNBmyOH/Ps42iGWfUyBAjbRysXrChGfyQ35mRUUZifGkyG3VEMQKo/L4MNt+f GnvjyGtqyRppStLqeTfw9uD3IvE9SDEuS4I+6nn+Yy5/BSaDIzYg8yy8Dg1/yn6G7hZZ K5dEq29e2AVS6U2HHDu9JAImOsg5DIvQNbosaebowErm5WtbYeeMjWYTBlfnztpx17LW m10aOs5y9C19C2M1CGSAaweo/MaUrS//E7PsirD4U1CPgZA6kT/buKFTY79JHEYuP+Hv JjOA== X-Forwarded-Encrypted: i=1; AJvYcCUQ1DWd1B2orjRgWQqUJBmqzG+/LsbEJ7sl1thGqS6RzSToMaVYsx8rGddCMmlBx5kN9/KllfxFR3m2kbg=@vger.kernel.org X-Gm-Message-State: AOJu0YwaFH0j+qmIoBkR+GzbkgqAlknSQmi7b+OzgQWkyp3Rw7ld2sHM W0VE6PDDghVrfJioBZjK/SHCcgVO9ZxAyw1Qip9QEUb6lWTZR30I X-Gm-Gg: ASbGncvGxYyr03sSnapvKCuYAWyHD+RsH3c1vSNcHYU2yBs4on2aSZFX1HyZwdN7bzC OA0XPF1TDDMjGwd4bEIl1ZFBpfhNXIeEZVKpDpYUBgji6P2GdHCyBAXkrLNdc42KPqj+uto1y+6 C4BN9OS2pb4as0MehhYTAlAMGNblfQMuf5xMlXBPKUR9PhsIp+T/MXkHsxWU7rY3AAr1JPJZdeG gO7ZQQkVtH9mC1Z90eiL8Vsslj8Au8cWsSVuKP2ZHP3dq+3lFSlbQln6urxEF77WMUnDw1L1PT1 hQ== X-Google-Smtp-Source: AGHT+IHsOcq0v8r/IsD/rTblP/e+H0X8Pne7rzRS4aDwWP++SL+VwES933KtHDTI+0HHkHetWhZy5w== X-Received: by 2002:a17:902:ccc2:b0:216:4165:c05e with SMTP id d9443c01a7336-21a83f67982mr398301215ad.24.1736791201254; Mon, 13 Jan 2025 10:00:01 -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.57 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 10:00:00 -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 03/13] mm, swap: remove old allocation path for HDD Date: Tue, 14 Jan 2025 01:57:22 +0800 Message-ID: <20250113175732.48099-4-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 We are currently using different swap allocation algorithm for HDD and non-HDD. This leads to the existence of a different set of locks, and the code path is heavily bloated, causing difficulties for further optimization and maintenance. This commit removes all HDD swap allocation and related dead code, and uses the cluster allocation algorithm instead. The performance may drop temporarily, but this should be negligible: The main advantage of the legacy HDD allocation algorithm is that it tends to use continuous slots, but swap device gets fragmented quickly anyway, and the attempt to use continuous slots will fail easily. This commit also enables mTHP swap on HDD, which is expected to be beneficial, and following commits will adapt and optimize the cluster allocator for HDD. Suggested-by: Chris Li Suggested-by: "Huang, Ying" Signed-off-by: Kairui Song Reviewed-by: Baoquan He --- include/linux/swap.h | 3 - mm/swapfile.c | 235 ++----------------------------------------- 2 files changed, 9 insertions(+), 229 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 187715eec3cb..0c681aa5cb98 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -310,9 +310,6 @@ struct swap_info_struct { unsigned int highest_bit; /* index of last free in swap_map */ unsigned int pages; /* total of usable pages of swap */ unsigned int inuse_pages; /* number of those currently in use */ - unsigned int cluster_next; /* likely index for next allocation */ - unsigned int cluster_nr; /* countdown to next cluster search */ - unsigned int __percpu *cluster_next_cpu; /*percpu index for next allocati= on */ struct percpu_cluster __percpu *percpu_cluster; /* per cpu's swap locatio= n */ struct rb_root swap_extent_root;/* root of the swap extent rbtree */ struct block_device *bdev; /* swap device or bdev of swap file */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 574059158627..fca58d43b836 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1001,49 +1001,6 @@ static void swap_range_free(struct swap_info_struct = *si, unsigned long offset, WRITE_ONCE(si->inuse_pages, si->inuse_pages - nr_entries); } =20 -static void set_cluster_next(struct swap_info_struct *si, unsigned long ne= xt) -{ - unsigned long prev; - - if (!(si->flags & SWP_SOLIDSTATE)) { - si->cluster_next =3D next; - return; - } - - prev =3D this_cpu_read(*si->cluster_next_cpu); - /* - * Cross the swap address space size aligned trunk, choose - * another trunk randomly to avoid lock contention on swap - * address space if possible. - */ - if ((prev >> SWAP_ADDRESS_SPACE_SHIFT) !=3D - (next >> SWAP_ADDRESS_SPACE_SHIFT)) { - /* No free swap slots available */ - if (si->highest_bit <=3D si->lowest_bit) - return; - next =3D get_random_u32_inclusive(si->lowest_bit, si->highest_bit); - next =3D ALIGN_DOWN(next, SWAP_ADDRESS_SPACE_PAGES); - next =3D max_t(unsigned int, next, si->lowest_bit); - } - this_cpu_write(*si->cluster_next_cpu, next); -} - -static bool swap_offset_available_and_locked(struct swap_info_struct *si, - unsigned long offset) -{ - if (data_race(!si->swap_map[offset])) { - spin_lock(&si->lock); - return true; - } - - if (vm_swap_full() && READ_ONCE(si->swap_map[offset]) =3D=3D SWAP_HAS_CAC= HE) { - spin_lock(&si->lock); - return true; - } - - return false; -} - static int cluster_alloc_swap(struct swap_info_struct *si, unsigned char usage, int nr, swp_entry_t slots[], int order) @@ -1071,13 +1028,7 @@ static int scan_swap_map_slots(struct swap_info_stru= ct *si, unsigned char usage, int nr, swp_entry_t slots[], int order) { - unsigned long offset; - unsigned long scan_base; - unsigned long last_in_cluster =3D 0; - int latency_ration =3D LATENCY_LIMIT; unsigned int nr_pages =3D 1 << order; - int n_ret =3D 0; - bool scanned_many =3D false; =20 /* * We try to cluster swap pages by allocating them sequentially @@ -1089,7 +1040,6 @@ static int scan_swap_map_slots(struct swap_info_struc= t *si, * But we do now try to find an empty cluster. -Andrea * And we let swap pages go all over an SSD partition. Hugh */ - if (order > 0) { /* * Should not even be attempting large allocations when huge @@ -1109,158 +1059,7 @@ static int scan_swap_map_slots(struct swap_info_str= uct *si, return 0; } =20 - if (si->cluster_info) - return cluster_alloc_swap(si, usage, nr, slots, order); - - si->flags +=3D SWP_SCANNING; - - /* For HDD, sequential access is more important. */ - scan_base =3D si->cluster_next; - offset =3D scan_base; - - if (unlikely(!si->cluster_nr--)) { - if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { - si->cluster_nr =3D SWAPFILE_CLUSTER - 1; - goto checks; - } - - spin_unlock(&si->lock); - - /* - * If seek is expensive, start searching for new cluster from - * start of partition, to minimize the span of allocated swap. - */ - scan_base =3D offset =3D si->lowest_bit; - last_in_cluster =3D offset + SWAPFILE_CLUSTER - 1; - - /* Locate the first empty (unaligned) cluster */ - for (; last_in_cluster <=3D READ_ONCE(si->highest_bit); offset++) { - if (si->swap_map[offset]) - last_in_cluster =3D offset + SWAPFILE_CLUSTER; - else if (offset =3D=3D last_in_cluster) { - spin_lock(&si->lock); - offset -=3D SWAPFILE_CLUSTER - 1; - si->cluster_next =3D offset; - si->cluster_nr =3D SWAPFILE_CLUSTER - 1; - goto checks; - } - if (unlikely(--latency_ration < 0)) { - cond_resched(); - latency_ration =3D LATENCY_LIMIT; - } - } - - offset =3D scan_base; - spin_lock(&si->lock); - si->cluster_nr =3D SWAPFILE_CLUSTER - 1; - } - -checks: - if (!(si->flags & SWP_WRITEOK)) - goto no_page; - if (!si->highest_bit) - goto no_page; - if (offset > si->highest_bit) - scan_base =3D offset =3D si->lowest_bit; - - /* reuse swap entry of cache-only swap if not busy. */ - if (vm_swap_full() && si->swap_map[offset] =3D=3D SWAP_HAS_CACHE) { - int swap_was_freed; - spin_unlock(&si->lock); - swap_was_freed =3D __try_to_reclaim_swap(si, offset, TTRS_ANYWAY | TTRS_= DIRECT); - spin_lock(&si->lock); - /* entry was freed successfully, try to use this again */ - if (swap_was_freed > 0) - goto checks; - goto scan; /* check next one */ - } - - if (si->swap_map[offset]) { - if (!n_ret) - goto scan; - else - goto done; - } - memset(si->swap_map + offset, usage, nr_pages); - - swap_range_alloc(si, offset, nr_pages); - slots[n_ret++] =3D swp_entry(si->type, offset); - - /* got enough slots or reach max slots? */ - if ((n_ret =3D=3D nr) || (offset >=3D si->highest_bit)) - goto done; - - /* search for next available slot */ - - /* time to take a break? */ - if (unlikely(--latency_ration < 0)) { - if (n_ret) - goto done; - spin_unlock(&si->lock); - cond_resched(); - spin_lock(&si->lock); - latency_ration =3D LATENCY_LIMIT; - } - - if (si->cluster_nr && !si->swap_map[++offset]) { - /* non-ssd case, still more slots in cluster? */ - --si->cluster_nr; - goto checks; - } - - /* - * Even if there's no free clusters available (fragmented), - * try to scan a little more quickly with lock held unless we - * have scanned too many slots already. - */ - if (!scanned_many) { - unsigned long scan_limit; - - if (offset < scan_base) - scan_limit =3D scan_base; - else - scan_limit =3D si->highest_bit; - for (; offset <=3D scan_limit && --latency_ration > 0; - offset++) { - if (!si->swap_map[offset]) - goto checks; - } - } - -done: - if (order =3D=3D 0) - set_cluster_next(si, offset + 1); - si->flags -=3D SWP_SCANNING; - return n_ret; - -scan: - VM_WARN_ON(order > 0); - spin_unlock(&si->lock); - while (++offset <=3D READ_ONCE(si->highest_bit)) { - if (unlikely(--latency_ration < 0)) { - cond_resched(); - latency_ration =3D LATENCY_LIMIT; - scanned_many =3D true; - } - if (swap_offset_available_and_locked(si, offset)) - goto checks; - } - offset =3D si->lowest_bit; - while (offset < scan_base) { - if (unlikely(--latency_ration < 0)) { - cond_resched(); - latency_ration =3D LATENCY_LIMIT; - scanned_many =3D true; - } - if (swap_offset_available_and_locked(si, offset)) - goto checks; - offset++; - } - spin_lock(&si->lock); - -no_page: - si->flags -=3D SWP_SCANNING; - return n_ret; + return cluster_alloc_swap(si, usage, nr, slots, order); } =20 int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) @@ -2871,8 +2670,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) mutex_unlock(&swapon_mutex); free_percpu(p->percpu_cluster); p->percpu_cluster =3D NULL; - free_percpu(p->cluster_next_cpu); - p->cluster_next_cpu =3D NULL; vfree(swap_map); kvfree(zeromap); kvfree(cluster_info); @@ -3184,8 +2981,6 @@ static unsigned long read_swap_header(struct swap_inf= o_struct *si, } =20 si->lowest_bit =3D 1; - si->cluster_next =3D 1; - si->cluster_nr =3D 0; =20 maxpages =3D swapfile_maximum_size; last_page =3D swap_header->info.last_page; @@ -3271,7 +3066,6 @@ static struct swap_cluster_info *setup_clusters(struc= t swap_info_struct *si, unsigned long maxpages) { unsigned long nr_clusters =3D DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); - unsigned long col =3D si->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_= COLS; struct swap_cluster_info *cluster_info; unsigned long i, j, k, idx; int cpu, err =3D -ENOMEM; @@ -3283,15 +3077,6 @@ static struct swap_cluster_info *setup_clusters(stru= ct swap_info_struct *si, for (i =3D 0; i < nr_clusters; i++) spin_lock_init(&cluster_info[i].lock); =20 - si->cluster_next_cpu =3D alloc_percpu(unsigned int); - if (!si->cluster_next_cpu) - goto err_free; - - /* Random start position to help with wear leveling */ - for_each_possible_cpu(cpu) - per_cpu(*si->cluster_next_cpu, cpu) =3D - get_random_u32_inclusive(1, si->highest_bit); - si->percpu_cluster =3D alloc_percpu(struct percpu_cluster); if (!si->percpu_cluster) goto err_free; @@ -3333,7 +3118,7 @@ static struct swap_cluster_info *setup_clusters(struc= t swap_info_struct *si, * sharing same address space. */ for (k =3D 0; k < SWAP_CLUSTER_COLS; k++) { - j =3D (k + col) % SWAP_CLUSTER_COLS; + j =3D k % SWAP_CLUSTER_COLS; for (i =3D 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) { struct swap_cluster_info *ci; idx =3D i * SWAP_CLUSTER_COLS + j; @@ -3483,18 +3268,18 @@ SYSCALL_DEFINE2(swapon, const char __user *, specia= lfile, int, swap_flags) =20 if (si->bdev && bdev_nonrot(si->bdev)) { si->flags |=3D SWP_SOLIDSTATE; - - cluster_info =3D setup_clusters(si, swap_header, maxpages); - if (IS_ERR(cluster_info)) { - error =3D PTR_ERR(cluster_info); - cluster_info =3D NULL; - goto bad_swap_unlock_inode; - } } else { atomic_inc(&nr_rotate_swap); inced_nr_rotate_swap =3D true; } =20 + cluster_info =3D setup_clusters(si, swap_header, maxpages); + if (IS_ERR(cluster_info)) { + error =3D PTR_ERR(cluster_info); + cluster_info =3D NULL; + goto bad_swap_unlock_inode; + } + if ((swap_flags & SWAP_FLAG_DISCARD) && si->bdev && bdev_max_discard_sectors(si->bdev)) { /* @@ -3575,8 +3360,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) bad_swap: free_percpu(si->percpu_cluster); si->percpu_cluster =3D NULL; - free_percpu(si->cluster_next_cpu); - si->cluster_next_cpu =3D NULL; inode =3D NULL; destroy_swap_extents(si); swap_cgroup_swapoff(si->type); --=20 2.47.1