From nobody Sun Feb 8 23:23:19 2026 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 A07661D935C for ; Tue, 24 Dec 2024 14:40:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735051221; cv=none; b=IHSQNPVp5OCUfob3Dxz4lwSrTS3+4bfHzOS/LrMEg7Ur9inpJs1k2NkYFZxjuCT/0S7C0NMxKY+pTtui0xobz1EaP+2q9vKwnsW8ao+VtU0bqZ8DuvWX80bBb0lkYS9WzKLO0p/BHM4oIosWTvgmuwrmJ5rGZp1D4bnToJuamwQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735051221; c=relaxed/simple; bh=GXi1K4qbSYhcgDmKK/QQKfoYN6tSZtpESJP+JqqBRBQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NlIlkEGraH3bSLB3D8C2fWzldt3Rn/B4uwHo1eo8mDL0A6PVC8XWUz1vXiIxCmQNcBWX8OLG7Z23/aFQWnpKEDzN7Fm8QRQPDAv3wP2neqtMsxFL2y4kRBforJa1LZd/RIGrqtBzAAHtiI+R3L4eMWjP3v5QIgWmDb8hzejEseE= 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=Dw4VbIgU; arc=none smtp.client-ip=209.85.214.176 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="Dw4VbIgU" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-2166f1e589cso62915035ad.3 for ; Tue, 24 Dec 2024 06:40:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735051219; x=1735656019; 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=grOS/bcMSihQT+oftYQ+fEn5xlcSkT6BHxL9XDq9XDs=; b=Dw4VbIgUZVWfW7aa0OuipQ3uOMk/2lI2XR6nEHV5SVZtPa5ms5brHRviPQfKORsHE0 pX5wYypBhqRU/BUptGK700k5xFFJXOFFhXkBZgFblk3oWjG8lyDLLtywrUg7/7KjHQPr G031bXBW8ues4m8KEHxkUBJtdz808rlvs/VwISDPEuctKHaM5JI4e7kD7cxsX2gnRjS0 h9/9Hb05i6ImL7oVdbIv03rTgjEoNOc5O7GuUnZK4jif4RzEt5zW5m277RBYv2O8GiWT sl6kn1h6TqPezUDX61uyBXSTaNOHB9DkVCHjFxPnIjsrfmBzgUeebREK2Z/Dxs7iqFnz 5uCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735051219; x=1735656019; 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=grOS/bcMSihQT+oftYQ+fEn5xlcSkT6BHxL9XDq9XDs=; b=kh6O1oakwAWuiBExixD5INqfmRAAdBPXc/VdxGcFjtS65hhlB+7FDau3dmnxWeUPQS 3qC/smjwQDfaAm5EPCyssi6Ws9XU6aXsHHY8IDyyXiC6VEdMgz/jPYeSlNLtwaRdJ4GW LiGCu/7nbkJiprqyPQnD5tJLGXS+WwRXMmsOpDur/Mst5jjyn+TyebGB0zTlqwlw1EPu R6jZV5U024XZEU6Qtb7DEgjvgP4EtxiXn+oOQCVcdz8RMihahGta+8cZm9q9K/5DrTwi IGk171T9Zo4sgBZyJ9ijwN2p5+4hMdMDTEhJ7CsMiOfc/Eec4+xz0B3KyZuKbQ0IVcHK BZhQ== X-Forwarded-Encrypted: i=1; AJvYcCVcv65Dz0JCOtxvpUiI1dzxf6u977ICET/r/IE8G96FlrnNUCk+m68ptkvii3M7Zim2fyhwEprrAEJpL/0=@vger.kernel.org X-Gm-Message-State: AOJu0YzUz5IWnJgT598xMyHXITv0tSN5RhvhEscmNdS+Xmpyz7JOaBpC 0XvOlbwc0X2N3BT6zT7DBiIXi+HNcp9nDPZuqf6iuMfJ48zZiuvA X-Gm-Gg: ASbGncvjNuoCaMxuypATmvYum4MKOW0bveuQQ7V4R2j92+aXSoXvt4InaAGRlyPpxqQ LAVmZDibyJUfBP5khruZIUQAQLG2322dBEb2sic2ECgL+oMpRxs1uogt0i7v8PdDARNTDABqGnZ 6ZA1+VBoR7rt8E7BnZj23W6KTHoIyBFmvbOZuJj4OuFzwBf38A4ynGVNwUZFphjxvCIOCImDV8K UkO4oXbuIsb7f1kHYCofoksXOH367aw3J19zsRUi8VpP1g8/HNy5NU87Xqb1hCX1dB+h/yR4Xh6 lg== X-Google-Smtp-Source: AGHT+IGc1mwapEXAYQJNjva8I5Jy3JbCFO3+Cko8mZMiFwBQtMcFjYL1OETSaeprQYaypdgO4WqVRg== X-Received: by 2002:a17:903:120d:b0:216:5af7:5a8e with SMTP id d9443c01a7336-219e6ebdc17mr247308385ad.26.1735051218961; Tue, 24 Dec 2024 06:40:18 -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.40.15 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 24 Dec 2024 06:40:18 -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 12/13] mm, swap: use a global swap cluster for non-rotation devices Date: Tue, 24 Dec 2024 22:38:10 +0800 Message-ID: <20241224143811.33462-13-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 Non-rotational devices (SSD / ZRAM) can tolerate fragmentation, so the goal of the SWAP allocator is to avoid contention for clusters. It uses a per-CPU cluster design, and each CPU will use a different cluster as much as possible. However, HDDs are very sensitive to fragmentation, contention is trivial in comparison. Therefore, we use one global cluster instead. This ensures that each order will be written to the same cluster as much as possible, which helps make the I/O more continuous. This ensures that the performance of the cluster allocator is as good as that of the old allocator. Tests after this commit compared to those before this series: Tested using 'make -j32' with tinyconfig, a 1G memcg limit, and HDD swap: make -j32 with tinyconfig, using 1G memcg limit and HDD swap: Before this series: 114.44user 29.11system 39:42.90elapsed 6%CPU (0avgtext+0avgdata 157284maxre= sident)k 2901232inputs+0outputs (238877major+4227640minor)pagefaults After this commit: 113.90user 23.81system 38:11.77elapsed 6%CPU (0avgtext+0avgdata 157260maxre= sident)k 2548728inputs+0outputs (235471major+4238110minor)pagefaults Suggested-by: Chris Li Signed-off-by: Kairui Song --- include/linux/swap.h | 2 ++ mm/swapfile.c | 51 ++++++++++++++++++++++++++++++++------------ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 4c1d2e69689f..b13b72645db3 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -318,6 +318,8 @@ struct swap_info_struct { unsigned int pages; /* total of usable pages of swap */ atomic_long_t inuse_pages; /* number of those currently in use */ struct percpu_cluster __percpu *percpu_cluster; /* per cpu's swap locatio= n */ + struct percpu_cluster *global_cluster; /* Use one global cluster for rota= ting device */ + spinlock_t global_cluster_lock; /* Serialize usage of global cluster */ struct rb_root swap_extent_root;/* root of the swap extent rbtree */ struct block_device *bdev; /* swap device or bdev of swap file */ struct file *swap_file; /* seldom referenced */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 0445a2db8492..482c531bdd8b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -814,7 +814,10 @@ static unsigned int alloc_swap_scan_cluster(struct swa= p_info_struct *si, out: relocate_cluster(si, ci); unlock_cluster(ci); - __this_cpu_write(si->percpu_cluster->next[order], next); + if (si->flags & SWP_SOLIDSTATE) + __this_cpu_write(si->percpu_cluster->next[order], next); + else + si->global_cluster->next[order] =3D next; return found; } =20 @@ -875,9 +878,16 @@ static unsigned long cluster_alloc_swap_entry(struct s= wap_info_struct *si, int o struct swap_cluster_info *ci; unsigned int offset, found =3D 0; =20 - /* Fast path using per CPU cluster */ - local_lock(&si->percpu_cluster->lock); - offset =3D __this_cpu_read(si->percpu_cluster->next[order]); + if (si->flags & SWP_SOLIDSTATE) { + /* Fast path using per CPU cluster */ + local_lock(&si->percpu_cluster->lock); + offset =3D __this_cpu_read(si->percpu_cluster->next[order]); + } else { + /* Serialize HDD SWAP allocation for each device. */ + spin_lock(&si->global_cluster_lock); + offset =3D si->global_cluster->next[order]; + } + if (offset) { ci =3D lock_cluster(si, offset); /* Cluster could have been used by another order */ @@ -972,8 +982,10 @@ static unsigned long cluster_alloc_swap_entry(struct s= wap_info_struct *si, int o } } done: - local_unlock(&si->percpu_cluster->lock); - + if (si->flags & SWP_SOLIDSTATE) + local_unlock(&si->percpu_cluster->lock); + else + spin_unlock(&si->global_cluster_lock); return found; } =20 @@ -2774,6 +2786,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) mutex_unlock(&swapon_mutex); free_percpu(p->percpu_cluster); p->percpu_cluster =3D NULL; + kfree(p->global_cluster); + p->global_cluster =3D NULL; vfree(swap_map); kvfree(zeromap); kvfree(cluster_info); @@ -3179,17 +3193,24 @@ static struct swap_cluster_info *setup_clusters(str= uct swap_info_struct *si, for (i =3D 0; i < nr_clusters; i++) spin_lock_init(&cluster_info[i].lock); =20 - si->percpu_cluster =3D alloc_percpu(struct percpu_cluster); - if (!si->percpu_cluster) - goto err_free; + if (si->flags & SWP_SOLIDSTATE) { + si->percpu_cluster =3D alloc_percpu(struct percpu_cluster); + if (!si->percpu_cluster) + goto err_free; =20 - for_each_possible_cpu(cpu) { - struct percpu_cluster *cluster; + for_each_possible_cpu(cpu) { + struct percpu_cluster *cluster; =20 - cluster =3D per_cpu_ptr(si->percpu_cluster, cpu); + cluster =3D per_cpu_ptr(si->percpu_cluster, cpu); + for (i =3D 0; i < SWAP_NR_ORDERS; i++) + cluster->next[i] =3D SWAP_ENTRY_INVALID; + local_lock_init(&cluster->lock); + } + } else { + si->global_cluster =3D kmalloc(sizeof(*si->global_cluster), GFP_KERNEL); for (i =3D 0; i < SWAP_NR_ORDERS; i++) - cluster->next[i] =3D SWAP_ENTRY_INVALID; - local_lock_init(&cluster->lock); + si->global_cluster->next[i] =3D SWAP_ENTRY_INVALID; + spin_lock_init(&si->global_cluster_lock); } =20 /* @@ -3463,6 +3484,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) bad_swap: free_percpu(si->percpu_cluster); si->percpu_cluster =3D NULL; + kfree(si->global_cluster); + si->global_cluster =3D NULL; inode =3D NULL; destroy_swap_extents(si); swap_cgroup_swapoff(si->type); --=20 2.47.1