From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 09F8A1BBBE5 for ; Mon, 30 Dec 2024 17:47:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735580841; cv=none; b=k+BmKmNrZ4ne4G5zl2SVZZLlCeC4hOsPwftrQJUkz0OcjGZl34kOrRlgEuZaCI5Yk0Ynw9MoEPDxE2HSoXWALh7bohH+M3EroM2rNJotDXGafxsgRaovU85Ms78CCHZsS0+pg3Bg9izilUgocea+xbw7q1g/5Dq9DypEQh3vhU8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735580841; c=relaxed/simple; bh=8n81DTXrxwkPk7XpWh0ieIWYhazwRFsd8unjobgQXqQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k54NK7FtFspjr2EKBBMrIV+AZJWH5N5A9emBsvYkzZof66SafsKPp/hof94eqEa2CcwvPlwapEneuc7zEY9A04tEu1XH/LwFVVsDr1hdlJBqdSDCtz74KN4RxTSD8Yib7WTENQ2+GYEn+Chmo2PkEH/JM6YXUxOBuYJInRkveFw= 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=Q+iJwEsk; arc=none smtp.client-ip=209.85.214.177 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="Q+iJwEsk" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2163bd70069so20738405ad.0 for ; Mon, 30 Dec 2024 09:47:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580839; x=1736185639; 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=2T/sNFK+FLf9Dy0NmIMHzxBPbHbk3WZ2pHgL/qbky98=; b=Q+iJwEsk41rbpU8CZ6mVXQ178CAyXfFL8bipdf5ixjWYaxVweQoMNNempgWrELnEV5 GeYvVi+ngHLMH4y0CHOiJ8LUVI+a/Jo42FybiUnCrJB6gpOSiO8W4/ELV9qW5EemNH2d Gs82raQhxGMSYdf0H5p9ljdJR/Zc3adGOawn+O7DhHqJ/orhbHxg7NptNKFPt1osxmC6 74inTD0YGNRPdwhf4KVKW/nwf2CFsPyAXqdkPEByYs9P+UyslpASLRzKeXISKdch/tSW MXGUtmEw+d9AsMDIBmAELacIsluAZ0qsY3yl++7kyFA4ADERnXpFV/oct1XXikDEvgKu DYFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580839; x=1736185639; 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=2T/sNFK+FLf9Dy0NmIMHzxBPbHbk3WZ2pHgL/qbky98=; b=to2Tg0iqxEGkH/TgZ3iQZnqZpqVzBK+67xX3AQCzOKcqV50VePmBUfC0HBIHjFkqwN JF54ENhKlzxxw9+Av8y4vWg0NviPBRGtnlS6683oti5kKApvrRtft7GFo5eHybHldoBh WpCP0aGcGvfPMWcsw105dGF0s7/cncl3C8sOIfZWYlxEQQpfBWThQD56eND0NBcprIra eiGH8VFUakIWtcAmSso3h1gjJoj8Caz6LnlVCg4xvTCyxZQGKttcWAsgut+0ncKRYTMS agkco0OadB5b2AcDuuu0QdXtnqnLMrqhIZyINE8glIQavX/XrlfE5i9+URGsLiwlSqTt A7Tw== X-Forwarded-Encrypted: i=1; AJvYcCXxhbDmZXx3YXIyyvaQ6NmaTdmiScT7xACym6Nsi9mVDfBZSVfVh6yVJ4PejpvWCSnCXZAo3VQ9GPK5VGU=@vger.kernel.org X-Gm-Message-State: AOJu0YyWIYeZ97AoSdm7sNSGKAsDmYGukJC2tW8IsUxDjsbZHW92YaRz e4kOgb7XY09RJ02bohKy5e6ponp25Q90SABu3n3wQrLt42Oon7Rs X-Gm-Gg: ASbGncsSCeDa8SCO4HEMOPBrra3rRIATfIYPYhInfkrOyVb+b0qprGVqL4JTt+Q/S68 8QuKsDtqzU0LvXRaOmLntEAONcK31aZH01sSKaQoEbMHPBYDvWHHO+JmIS6KLCEBqgMWigErK0K A/AgHidN5jXiThifLzVz11vpBUyiFTjgsR4w+vQENVuoNlqDDqkvB/VIKP5KXr72S+Vv/5x16JU 4DC+kp5RNcvR9gjJWFO06mqNDzZCSWxvhyD5v5jmHhp+vNBvgnCnshjun57o75zHu70ndCjkLPV X-Google-Smtp-Source: AGHT+IG1L+YlopKpf4F4WRcFOfSGe7YaoMXyGz516XDuI+LioGKwgnFwv7jBKhNwXjyeA8fR5b+skg== X-Received: by 2002:a17:903:2444:b0:215:e98c:c5d9 with SMTP id d9443c01a7336-219e6e9fb2emr458467365ad.18.1735580839480; Mon, 30 Dec 2024 09:47:19 -0800 (PST) Received: from KASONG-MC4.tencent.com ([1.203.117.231]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9cdf25sm180687695ad.118.2024.12.30.09.47.16 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:47:19 -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 v3 12/13] mm, swap: use a global swap cluster for non-rotation devices Date: Tue, 31 Dec 2024 01:46:20 +0800 Message-ID: <20241230174621.61185-13-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241230174621.61185-1-ryncsn@gmail.com> References: <20241230174621.61185-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: 8bit 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 157284maxresident)k 2901232inputs+0outputs (238877major+4227640minor)pagefaults After this commit: 113.90user 23.81system 38:11.77elapsed 6%CPU (0avgtext+0avgdata 157260maxresident)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 location */ + struct percpu_cluster *global_cluster; /* Use one global cluster for rotating 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 a3d1239d944b..e57e5453a25b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -814,7 +814,10 @@ static unsigned int alloc_swap_scan_cluster(struct swap_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] = next; return found; } @@ -875,9 +878,16 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o struct swap_cluster_info *ci; unsigned int offset, found = 0; - /* Fast path using per CPU cluster */ - local_lock(&si->percpu_cluster->lock); - offset = __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 = __this_cpu_read(si->percpu_cluster->next[order]); + } else { + /* Serialize HDD SWAP allocation for each device. */ + spin_lock(&si->global_cluster_lock); + offset = si->global_cluster->next[order]; + } + if (offset) { ci = lock_cluster(si, offset); /* Cluster could have been used by another order */ @@ -972,8 +982,10 @@ static unsigned long cluster_alloc_swap_entry(struct swap_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; } @@ -2778,6 +2790,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) mutex_unlock(&swapon_mutex); free_percpu(p->percpu_cluster); p->percpu_cluster = NULL; + kfree(p->global_cluster); + p->global_cluster = NULL; vfree(swap_map); kvfree(zeromap); kvfree(cluster_info); @@ -3183,17 +3197,24 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, for (i = 0; i < nr_clusters; i++) spin_lock_init(&cluster_info[i].lock); - si->percpu_cluster = alloc_percpu(struct percpu_cluster); - if (!si->percpu_cluster) - goto err_free; + if (si->flags & SWP_SOLIDSTATE) { + si->percpu_cluster = alloc_percpu(struct percpu_cluster); + if (!si->percpu_cluster) + goto err_free; - for_each_possible_cpu(cpu) { - struct percpu_cluster *cluster; + for_each_possible_cpu(cpu) { + struct percpu_cluster *cluster; - cluster = per_cpu_ptr(si->percpu_cluster, cpu); + cluster = per_cpu_ptr(si->percpu_cluster, cpu); + for (i = 0; i < SWAP_NR_ORDERS; i++) + cluster->next[i] = SWAP_ENTRY_INVALID; + local_lock_init(&cluster->lock); + } + } else { + si->global_cluster = kmalloc(sizeof(*si->global_cluster), GFP_KERNEL); for (i = 0; i < SWAP_NR_ORDERS; i++) - cluster->next[i] = SWAP_ENTRY_INVALID; - local_lock_init(&cluster->lock); + si->global_cluster->next[i] = SWAP_ENTRY_INVALID; + spin_lock_init(&si->global_cluster_lock); } /* @@ -3467,6 +3488,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) bad_swap: free_percpu(si->percpu_cluster); si->percpu_cluster = NULL; + kfree(si->global_cluster); + si->global_cluster = NULL; inode = NULL; destroy_swap_extents(si); swap_cgroup_swapoff(si->type); -- 2.47.1