From nobody Wed Sep 10 11:03:51 2025 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 B3CF12C032E for ; Fri, 5 Sep 2025 19:15:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757099729; cv=none; b=hcepvBTQFLe3qhYNdeGFlcKvm8vvrvYhSvI5UXIjqXp8yeM5GplK9xuYUm/EHfJoV5K7oDWLtwjzoxM0Toy5nOWNiDli6NI7dy3g6bwAkD0lmbG6OmshHXPELH1QyW52TOzpFIvsBgYfpT3wrVxAea7NKOCjSr6u6PO1SX1iUFI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757099729; c=relaxed/simple; bh=9LB4pNawNDvYtH2LCllJebbjSodpn/+8Udl9dXMQuFE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QdRlkS3TM0GuLas1wU0IGGk0TbxPjBrs99ZwnuP7sVroDPxIv7il5xPt52RN6uO3zs8yEzpRLwa4eXxXTz74lLfj9MU+d6vcAXnKDTjI5IK1d0ou6phjQc/zBPYcnAmyyltjccE0VLo7UbnQp/N0f42dN5egLfNGRu8oLFzKMrk= 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=KHSSkgs/; arc=none smtp.client-ip=209.85.210.171 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="KHSSkgs/" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-77238cb3cbbso2679528b3a.0 for ; Fri, 05 Sep 2025 12:15:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1757099727; x=1757704527; 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=+lEkM5bIq6/Z03Z2MzIXOrrBa0ZEPgMosVL9NAQvMio=; b=KHSSkgs/HJFFSqcSrplnqURtRqeF9KwQqnYS7HtzrYAhHW5ouLKGyZZUvBVSJr7vTL juZYDb/HEU84T1dllyXFuUsBL+5sJKs0XrO48VdrhkIoxaRET06CVbhuHKWo8Km3ysc0 MCrsCdH1PSngXbfqA9h7XK8j87M1gOrmGyZPib42RWy2TP5QpLn0no4Otagg1GwsN9/t /FsqTVsa67JOIv28xSKTDd6ENPpUsVCGAaS+esZVH0S0hXYrcwQwS63Unhx5B5zRDOCi uf6DQByCFxDSn3RkWdOa99EoZv+m6rcAXDyPTbKg1XT7DnlKXs6Hjc2ksDK88JLgUk3w kXUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757099727; x=1757704527; 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=+lEkM5bIq6/Z03Z2MzIXOrrBa0ZEPgMosVL9NAQvMio=; b=OSx314Xza30i49VbQlH1TmqYULUWPlqWpeacVLxxzgzwBXCdHiJzr3JvmfZxaTa6my stneHYGobOWhWZAuMdlCZmNc8i1EJO31po+Sz9qscWXbsEqXTRLj5r9h2mDIa9mTqdqr 1rVJubx/MRVmTGfVUq5LgXI/TqW+cbS/HVfJVQPZ7B54jhdgYHBUd3032ar39EGxt3xu lOYHbxca1pAXUOte8X907blfsBjAeP8OrBDG+gNeFNzC4Sic5VICZpIGGqSaDGy2z/69 EwfndZKyP0qeJT1MJIpqxTL9YSlnVNfaMDZmV5Jn75SbpDBfyD9dRSW/U+Pz4rQsY+Np kz4w== X-Forwarded-Encrypted: i=1; AJvYcCUWUbF1Xr7yWZl4HJ+wqW9DkbnyZXs1NG1zo4DChSSvXsvDcWDGt9jVSxsotDV3/RrcouGV+W5XFmufSfg=@vger.kernel.org X-Gm-Message-State: AOJu0YxdGGM4rGu2/L2s8rduniu30mDZD3LUhdp2yxOZR1zChTB05Tdb T+kqU0MF7O1US1X4ee0XyZZmfLA4fdYBrNyYwkVq0qWAvUFGwUJaARZ5 X-Gm-Gg: ASbGncuJ+yo+zVYkYLiqNgPmujygpkglFqeGELDIJMPaW98HGMa7XilkhflJpyRNEI3 t/shxcYspXiVEYJ2pZcvB9IWbSph0eoJZf2kQ1vQcU5j5bwgiBYBxC/nKonbG7aP5IXh4qHR0CY 00jMZXsQ9HLBjxq3RUBhyWcnS48O0MnOzpw9O3v/XfBMFtk9IFzQBB2WwWFZKVOFBlzs0PzwakY 5oU2DZopFGfZgEtkWtVKP2Gj4JS+XaOrpKq1u13QjM5UKGo1uRMLJPPGHJK2SYRjPnToEtDnf7w JsT5/7+w/g31J/L+0kGpI6fk1j/lyBQ/j3B5MmdMSIiLGR740jSXEZheWh4kWmTTfElup+WprMa n2eGtlVoskDh4Wu8JcvN6T+2qw6G1LkiPEwhgcJmcOFOANtwsj+22MpbGKw== X-Google-Smtp-Source: AGHT+IFX1AsNYufNSGbWrppi99g9MPF4m602yXU0boYUFaYnzpDBnO4BP28Q0xPFgPU4tjs3kQ4Hwg== X-Received: by 2002:a05:6a00:3c89:b0:772:5a3f:7cb3 with SMTP id d2e1a72fcca58-7725a3f8497mr23849253b3a.1.1757099726844; Fri, 05 Sep 2025 12:15:26 -0700 (PDT) Received: from KASONG-MC4.tencent.com ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-77256a0f916sm15871442b3a.63.2025.09.05.12.15.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 05 Sep 2025 12:15:26 -0700 (PDT) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Matthew Wilcox , Hugh Dickins , Chris Li , Barry Song , Baoquan He , Nhat Pham , Kemeng Shi , Baolin Wang , Ying Huang , Johannes Weiner , David Hildenbrand , Yosry Ahmed , Lorenzo Stoakes , Zi Yan , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v2 14/15] mm, swap: implement dynamic allocation of swap table Date: Sat, 6 Sep 2025 03:13:56 +0800 Message-ID: <20250905191357.78298-15-ryncsn@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250905191357.78298-1-ryncsn@gmail.com> References: <20250905191357.78298-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 Now swap table is cluster based, which means free clusters can free its table since no one should modify it. There could be speculative readers, like swap cache look up, protect them by making them RCU protected. All swap table should be filled with null entries before free, so such readers will either see a NULL pointer or a null filled table being lazy freed. On allocation, allocate the table when a cluster is used by any order. This way, we can reduce the memory usage of large swap device significantly. This idea to dynamically release unused swap cluster data was initially suggested by Chris Li while proposing the cluster swap allocator and it suits the swap table idea very well. Co-developed-by: Chris Li Signed-off-by: Chris Li Signed-off-by: Kairui Song Acked-by: Chris Li --- mm/swap.h | 2 +- mm/swap_state.c | 9 +-- mm/swap_table.h | 37 ++++++++- mm/swapfile.c | 202 ++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 199 insertions(+), 51 deletions(-) diff --git a/mm/swap.h b/mm/swap.h index c4fb28845d77..caff4fe30fc5 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -36,7 +36,7 @@ struct swap_cluster_info { u16 count; u8 flags; u8 order; - atomic_long_t *table; /* Swap table entries, see mm/swap_table.h */ + atomic_long_t __rcu *table; /* Swap table entries, see mm/swap_table.h */ struct list_head list; }; =20 diff --git a/mm/swap_state.c b/mm/swap_state.c index 209d5e9e8d90..dfe8f42fc309 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -92,8 +92,8 @@ struct folio *swap_cache_get_folio(swp_entry_t entry) struct folio *folio; =20 for (;;) { - swp_tb =3D __swap_table_get(__swap_entry_to_cluster(entry), - swp_cluster_offset(entry)); + swp_tb =3D swap_table_get(__swap_entry_to_cluster(entry), + swp_cluster_offset(entry)); if (!swp_tb_is_folio(swp_tb)) return NULL; folio =3D swp_tb_to_folio(swp_tb); @@ -116,11 +116,10 @@ void *swap_cache_get_shadow(swp_entry_t entry) { unsigned long swp_tb; =20 - swp_tb =3D __swap_table_get(__swap_entry_to_cluster(entry), - swp_cluster_offset(entry)); + swp_tb =3D swap_table_get(__swap_entry_to_cluster(entry), + swp_cluster_offset(entry)); if (swp_tb_is_shadow(swp_tb)) return swp_tb_to_shadow(swp_tb); - return NULL; } =20 diff --git a/mm/swap_table.h b/mm/swap_table.h index e1f7cc009701..52254e455304 100644 --- a/mm/swap_table.h +++ b/mm/swap_table.h @@ -2,8 +2,15 @@ #ifndef _MM_SWAP_TABLE_H #define _MM_SWAP_TABLE_H =20 +#include +#include #include "swap.h" =20 +/* A typical flat array in each cluster as swap table */ +struct swap_table { + atomic_long_t entries[SWAPFILE_CLUSTER]; +}; + /* * A swap table entry represents the status of a swap slot on a swap * (physical or virtual) device. The swap table in each cluster is a @@ -76,22 +83,46 @@ static inline void *swp_tb_to_shadow(unsigned long swp_= tb) static inline void __swap_table_set(struct swap_cluster_info *ci, unsigned int off, unsigned long swp_tb) { + atomic_long_t *table =3D rcu_dereference_protected(ci->table, true); + + lockdep_assert_held(&ci->lock); VM_WARN_ON_ONCE(off >=3D SWAPFILE_CLUSTER); - atomic_long_set(&ci->table[off], swp_tb); + atomic_long_set(&table[off], swp_tb); } =20 static inline unsigned long __swap_table_xchg(struct swap_cluster_info *ci, unsigned int off, unsigned long swp_tb) { + atomic_long_t *table =3D rcu_dereference_protected(ci->table, true); + + lockdep_assert_held(&ci->lock); VM_WARN_ON_ONCE(off >=3D SWAPFILE_CLUSTER); /* Ordering is guaranteed by cluster lock, relax */ - return atomic_long_xchg_relaxed(&ci->table[off], swp_tb); + return atomic_long_xchg_relaxed(&table[off], swp_tb); } =20 static inline unsigned long __swap_table_get(struct swap_cluster_info *ci, unsigned int off) { + atomic_long_t *table; + VM_WARN_ON_ONCE(off >=3D SWAPFILE_CLUSTER); - return atomic_long_read(&ci->table[off]); + table =3D rcu_dereference_check(ci->table, lockdep_is_held(&ci->lock)); + + return atomic_long_read(&table[off]); +} + +static inline unsigned long swap_table_get(struct swap_cluster_info *ci, + unsigned int off) +{ + atomic_long_t *table; + unsigned long swp_tb; + + rcu_read_lock(); + table =3D rcu_dereference(ci->table); + swp_tb =3D table ? atomic_long_read(&table[off]) : null_to_swp_tb(); + rcu_read_unlock(); + + return swp_tb; } #endif diff --git a/mm/swapfile.c b/mm/swapfile.c index 6b3b35a7ddd9..49f93069faef 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -105,6 +105,8 @@ static DEFINE_SPINLOCK(swap_avail_lock); =20 struct swap_info_struct *swap_info[MAX_SWAPFILES]; =20 +static struct kmem_cache *swap_table_cachep; + static DEFINE_MUTEX(swapon_mutex); =20 static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait); @@ -400,10 +402,17 @@ static inline bool cluster_is_discard(struct swap_clu= ster_info *info) return info->flags =3D=3D CLUSTER_FLAG_DISCARD; } =20 +static inline bool cluster_table_is_alloced(struct swap_cluster_info *ci) +{ + return rcu_dereference_protected(ci->table, lockdep_is_held(&ci->lock)); +} + static inline bool cluster_is_usable(struct swap_cluster_info *ci, int ord= er) { if (unlikely(ci->flags > CLUSTER_FLAG_USABLE)) return false; + if (!cluster_table_is_alloced(ci)) + return false; if (!order) return true; return cluster_is_empty(ci) || order =3D=3D ci->order; @@ -421,32 +430,98 @@ static inline unsigned int cluster_offset(struct swap= _info_struct *si, return cluster_index(si, ci) * SWAPFILE_CLUSTER; } =20 -static int swap_table_alloc_table(struct swap_cluster_info *ci) +static void swap_cluster_free_table(struct swap_cluster_info *ci) { - WARN_ON(ci->table); - ci->table =3D kzalloc(sizeof(unsigned long) * SWAPFILE_CLUSTER, GFP_KERNE= L); - if (!ci->table) - return -ENOMEM; - return 0; + unsigned int ci_off; + struct swap_table *table; + + /* Only empty cluster's table is allow to be freed */ + lockdep_assert_held(&ci->lock); + VM_WARN_ON_ONCE(!cluster_is_empty(ci)); + for (ci_off =3D 0; ci_off < SWAPFILE_CLUSTER; ci_off++) + VM_WARN_ON_ONCE(!swp_tb_is_null(__swap_table_get(ci, ci_off))); + table =3D (void *)rcu_dereference_protected(ci->table, true); + rcu_assign_pointer(ci->table, NULL); + + kmem_cache_free(swap_table_cachep, table); } =20 -static void swap_cluster_free_table(struct swap_cluster_info *ci) +/* + * Allocate a swap table may need to sleep, which leads to migration, + * so attempt an atomic allocation first then fallback and handle + * potential race. + */ +static struct swap_cluster_info * +swap_cluster_alloc_table(struct swap_info_struct *si, + struct swap_cluster_info *ci, + int order) { - unsigned int ci_off; - unsigned long swp_tb; + struct swap_cluster_info *pcp_ci; + struct swap_table *table; + unsigned long offset; =20 - if (!ci->table) - return; + /* + * Only cluster isolation from the allocator does table allocation. + * Swap allocator uses a percpu cluster and holds the local lock. + */ + lockdep_assert_held(&ci->lock); + lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock); + + table =3D kmem_cache_zalloc(swap_table_cachep, + __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN); + if (table) { + rcu_assign_pointer(ci->table, table); + return ci; + } + + /* + * Try a sleep allocation. Each isolated free cluster may cause + * a sleep allocation, but there is a limited number of them, so + * the potential recursive allocation should be limited. + */ + spin_unlock(&ci->lock); + if (!(si->flags & SWP_SOLIDSTATE)) + spin_unlock(&si->global_cluster_lock); + local_unlock(&percpu_swap_cluster.lock); + table =3D kmem_cache_zalloc(swap_table_cachep, __GFP_HIGH | GFP_KERNEL); =20 - for (ci_off =3D 0; ci_off < SWAPFILE_CLUSTER; ci_off++) { - swp_tb =3D __swap_table_get(ci, ci_off); - if (!swp_tb_is_null(swp_tb)) - pr_err_once("swap: unclean swap space on swapoff: 0x%lx", - swp_tb); + local_lock(&percpu_swap_cluster.lock); + if (!(si->flags & SWP_SOLIDSTATE)) + spin_lock(&si->global_cluster_lock); + /* + * Back to atomic context. First, check if we migrated to a new + * CPU with a usable percpu cluster. If so, try using that instead. + * No need to check it for the spinning device, as swap is + * serialized by the global lock on them. + * + * The is_usable check is a bit rough, but ensures order 0 success. + */ + offset =3D this_cpu_read(percpu_swap_cluster.offset[order]); + if ((si->flags & SWP_SOLIDSTATE) && offset) { + pcp_ci =3D swap_cluster_lock(si, offset); + if (cluster_is_usable(pcp_ci, order) && + pcp_ci->count < SWAPFILE_CLUSTER) { + ci =3D pcp_ci; + goto free_table; + } + swap_cluster_unlock(pcp_ci); } =20 - kfree(ci->table); - ci->table =3D NULL; + if (!table) + return NULL; + + spin_lock(&ci->lock); + /* Nothing should have touched the dangling empty cluster. */ + if (WARN_ON_ONCE(cluster_table_is_alloced(ci))) + goto free_table; + + rcu_assign_pointer(ci->table, table); + return ci; + +free_table: + if (table) + kmem_cache_free(swap_table_cachep, table); + return ci; } =20 static void move_cluster(struct swap_info_struct *si, @@ -478,7 +553,7 @@ static void swap_cluster_schedule_discard(struct swap_i= nfo_struct *si, =20 static void __free_cluster(struct swap_info_struct *si, struct swap_cluste= r_info *ci) { - lockdep_assert_held(&ci->lock); + swap_cluster_free_table(ci); move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE); ci->order =3D 0; } @@ -493,15 +568,11 @@ static void __free_cluster(struct swap_info_struct *s= i, struct swap_cluster_info * this returns NULL for an non-empty list. */ static struct swap_cluster_info *isolate_lock_cluster( - struct swap_info_struct *si, struct list_head *list) + struct swap_info_struct *si, struct list_head *list, int order) { - struct swap_cluster_info *ci, *ret =3D NULL; + struct swap_cluster_info *ci, *found =3D NULL; =20 spin_lock(&si->lock); - - if (unlikely(!(si->flags & SWP_WRITEOK))) - goto out; - list_for_each_entry(ci, list, list) { if (!spin_trylock(&ci->lock)) continue; @@ -513,13 +584,19 @@ static struct swap_cluster_info *isolate_lock_cluster( =20 list_del(&ci->list); ci->flags =3D CLUSTER_FLAG_NONE; - ret =3D ci; + found =3D ci; break; } -out: spin_unlock(&si->lock); =20 - return ret; + if (found && !cluster_table_is_alloced(found)) { + /* Only an empty free cluster's swap table can be freed. */ + VM_WARN_ON_ONCE(list !=3D &si->free_clusters); + VM_WARN_ON_ONCE(!cluster_is_empty(found)); + return swap_cluster_alloc_table(si, found, order); + } + + return found; } =20 /* @@ -652,17 +729,27 @@ static void relocate_cluster(struct swap_info_struct = *si, * added to free cluster list and its usage counter will be increased by 1. * Only used for initialization. */ -static void inc_cluster_info_page(struct swap_info_struct *si, +static int inc_cluster_info_page(struct swap_info_struct *si, struct swap_cluster_info *cluster_info, unsigned long page_nr) { unsigned long idx =3D page_nr / SWAPFILE_CLUSTER; + struct swap_table *table; struct swap_cluster_info *ci; =20 ci =3D cluster_info + idx; + if (!ci->table) { + table =3D kmem_cache_zalloc(swap_table_cachep, GFP_KERNEL); + if (!table) + return -ENOMEM; + rcu_assign_pointer(ci->table, table); + } + ci->count++; =20 VM_BUG_ON(ci->count > SWAPFILE_CLUSTER); VM_BUG_ON(ci->flags); + + return 0; } =20 static bool cluster_reclaim_range(struct swap_info_struct *si, @@ -844,7 +931,7 @@ static unsigned int alloc_swap_scan_list(struct swap_in= fo_struct *si, unsigned int found =3D SWAP_ENTRY_INVALID; =20 do { - struct swap_cluster_info *ci =3D isolate_lock_cluster(si, list); + struct swap_cluster_info *ci =3D isolate_lock_cluster(si, list, order); unsigned long offset; =20 if (!ci) @@ -869,7 +956,7 @@ static void swap_reclaim_full_clusters(struct swap_info= _struct *si, bool force) if (force) to_scan =3D swap_usage_in_pages(si) / SWAPFILE_CLUSTER; =20 - while ((ci =3D isolate_lock_cluster(si, &si->full_clusters))) { + while ((ci =3D isolate_lock_cluster(si, &si->full_clusters, 0))) { offset =3D cluster_offset(si, ci); end =3D min(si->max, offset + SWAPFILE_CLUSTER); to_scan--; @@ -1017,6 +1104,7 @@ static unsigned long cluster_alloc_swap_entry(struct = swap_info_struct *si, int o done: if (!(si->flags & SWP_SOLIDSTATE)) spin_unlock(&si->global_cluster_lock); + return found; } =20 @@ -1884,7 +1972,13 @@ swp_entry_t get_swap_page_of_type(int type) /* This is called for allocating swap entry, not cache */ if (get_swap_device_info(si)) { if (si->flags & SWP_WRITEOK) { + /* + * Grab the local lock to be complaint + * with swap table allocation. + */ + local_lock(&percpu_swap_cluster.lock); offset =3D cluster_alloc_swap_entry(si, 0, 1); + local_unlock(&percpu_swap_cluster.lock); if (offset) { entry =3D swp_entry(si->type, offset); atomic_long_dec(&nr_swap_pages); @@ -2677,12 +2771,21 @@ static void wait_for_allocation(struct swap_info_st= ruct *si) static void free_cluster_info(struct swap_cluster_info *cluster_info, unsigned long maxpages) { + struct swap_cluster_info *ci; int i, nr_clusters =3D DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); =20 if (!cluster_info) return; - for (i =3D 0; i < nr_clusters; i++) - swap_cluster_free_table(&cluster_info[i]); + for (i =3D 0; i < nr_clusters; i++) { + ci =3D cluster_info + i; + /* Cluster with bad marks count will have a remaining table */ + spin_lock(&ci->lock); + if (rcu_dereference_protected(ci->table, true)) { + ci->count =3D 0; + swap_cluster_free_table(ci); + } + spin_unlock(&ci->lock); + } kvfree(cluster_info); } =20 @@ -2718,6 +2821,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) struct address_space *mapping; struct inode *inode; struct filename *pathname; + unsigned int maxpages; int err, found =3D 0; =20 if (!capable(CAP_SYS_ADMIN)) @@ -2824,8 +2928,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) p->swap_map =3D NULL; zeromap =3D p->zeromap; p->zeromap =3D NULL; + maxpages =3D p->max; cluster_info =3D p->cluster_info; - free_cluster_info(cluster_info, p->max); p->max =3D 0; p->cluster_info =3D NULL; spin_unlock(&p->lock); @@ -2837,6 +2941,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) p->global_cluster =3D NULL; vfree(swap_map); kvfree(zeromap); + free_cluster_info(cluster_info, maxpages); /* Destroy swap account information */ swap_cgroup_swapoff(p->type); =20 @@ -3215,11 +3320,8 @@ static struct swap_cluster_info *setup_clusters(stru= ct swap_info_struct *si, if (!cluster_info) goto err; =20 - for (i =3D 0; i < nr_clusters; i++) { + for (i =3D 0; i < nr_clusters; i++) spin_lock_init(&cluster_info[i].lock); - if (swap_table_alloc_table(&cluster_info[i])) - goto err_free; - } =20 if (!(si->flags & SWP_SOLIDSTATE)) { si->global_cluster =3D kmalloc(sizeof(*si->global_cluster), @@ -3238,16 +3340,23 @@ static struct swap_cluster_info *setup_clusters(str= uct swap_info_struct *si, * See setup_swap_map(): header page, bad pages, * and the EOF part of the last cluster. */ - inc_cluster_info_page(si, cluster_info, 0); + err =3D inc_cluster_info_page(si, cluster_info, 0); + if (err) + goto err; for (i =3D 0; i < swap_header->info.nr_badpages; i++) { unsigned int page_nr =3D swap_header->info.badpages[i]; =20 if (page_nr >=3D maxpages) continue; - inc_cluster_info_page(si, cluster_info, page_nr); + err =3D inc_cluster_info_page(si, cluster_info, page_nr); + if (err) + goto err; + } + for (i =3D maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) { + err =3D inc_cluster_info_page(si, cluster_info, i); + if (err) + goto err; } - for (i =3D maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) - inc_cluster_info_page(si, cluster_info, i); =20 INIT_LIST_HEAD(&si->free_clusters); INIT_LIST_HEAD(&si->full_clusters); @@ -3961,6 +4070,15 @@ static int __init swapfile_init(void) =20 swapfile_maximum_size =3D arch_max_swapfile_size(); =20 + /* + * Once a cluster is freed, it's swap table content is read + * only, and all swap cache readers (swap_cache_*) verifies + * the content before use. So it's safe to use RCU slab here. + */ + swap_table_cachep =3D kmem_cache_create("swap_table", + sizeof(struct swap_table), + 0, SLAB_PANIC | SLAB_TYPESAFE_BY_RCU, NULL); + #ifdef CONFIG_MIGRATION if (swapfile_maximum_size >=3D (1UL << SWP_MIG_TOTAL_BITS)) swap_migration_ad_supported =3D true; --=20 2.51.0