| Message ID | 20260512053625.2950900-2-hch@lst.de |
|---|---|
| State | Not Applicable |
| Headers | show |
| Series | [01/12] swap: remove the maxpages variable in sys_swapon | expand |
On 5/12/26 14:35, Christoph Hellwig wrote: > Always use si->max which is updated setup_swap_extents instead of copying > into and out of maxpages. Checking mm/swapfile.c, I see s->max being set only in swapon(). Is this a typo or am I misunderstanding this sentence ? Looks good otherwise, but it would be nice to rename ->max to ->maxpages to make it clear what this is counting. > > Signed-off-by: Christoph Hellwig <hch@lst.de> > --- > mm/swapfile.c | 27 +++++++++++---------------- > 1 file changed, 11 insertions(+), 16 deletions(-) > > diff --git a/mm/swapfile.c b/mm/swapfile.c > index 9174f1eeffb0..f7ebd97e28a3 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -3350,10 +3350,9 @@ static unsigned long read_swap_header(struct swap_info_struct *si, > } > > static int setup_swap_clusters_info(struct swap_info_struct *si, > - union swap_header *swap_header, > - unsigned long maxpages) > + union swap_header *swap_header) > { > - unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); > + unsigned long nr_clusters = DIV_ROUND_UP(si->max, SWAPFILE_CLUSTER); > struct swap_cluster_info *cluster_info; > int err = -ENOMEM; > unsigned long i; > @@ -3395,7 +3394,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, > if (err) > goto err; > } > - for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) { > + for (i = si->max; i < round_up(si->max, SWAPFILE_CLUSTER); i++) { > err = swap_cluster_setup_bad_slot(si, cluster_info, i, true); > if (err) > goto err; > @@ -3425,7 +3424,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, > si->cluster_info = cluster_info; > return 0; > err: > - free_swap_cluster_info(cluster_info, maxpages); > + free_swap_cluster_info(cluster_info, si->max); > return err; > } > > @@ -3440,7 +3439,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > union swap_header *swap_header; > int nr_extents; > sector_t span; > - unsigned long maxpages; > struct folio *folio = NULL; > struct inode *inode = NULL; > bool inced_nr_rotate_swap = false; > @@ -3512,14 +3510,13 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > } > swap_header = kmap_local_folio(folio, 0); > > - maxpages = read_swap_header(si, swap_header, inode); > - if (unlikely(!maxpages)) { > + si->max = read_swap_header(si, swap_header, inode); > + if (unlikely(!si->max)) { > error = -EINVAL; > goto bad_swap_unlock_inode; > } > > - si->max = maxpages; > - si->pages = maxpages - 1; > + si->pages = si->max - 1; > nr_extents = setup_swap_extents(si, swap_file, &span); > if (nr_extents < 0) { > error = nr_extents; > @@ -3531,14 +3528,12 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > goto bad_swap_unlock_inode; > } > > - maxpages = si->max; > - > /* Set up the swap cluster info */ > - error = setup_swap_clusters_info(si, swap_header, maxpages); > + error = setup_swap_clusters_info(si, swap_header); > if (error) > goto bad_swap_unlock_inode; > > - error = swap_cgroup_swapon(si->type, maxpages); > + error = swap_cgroup_swapon(si->type, si->max); > if (error) > goto bad_swap_unlock_inode; > > @@ -3546,7 +3541,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might > * be above MAX_PAGE_ORDER incase of a large swap file. > */ > - si->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), > + si->zeromap = kvmalloc_array(BITS_TO_LONGS(si->max), sizeof(long), > GFP_KERNEL | __GFP_ZERO); > if (!si->zeromap) { > error = -ENOMEM; > @@ -3597,7 +3592,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > } > } > > - error = zswap_swapon(si->type, maxpages); > + error = zswap_swapon(si->type, si->max); > if (error) > goto bad_swap_unlock_inode; >
On Tue, May 12, 2026 at 04:08:35PM +0900, Damien Le Moal wrote: > On 5/12/26 14:35, Christoph Hellwig wrote: > > Always use si->max which is updated setup_swap_extents instead of copying > > into and out of maxpages. > > Checking mm/swapfile.c, I see s->max being set only in swapon(). Is this a typo > or am I misunderstanding this sentence ? It is updated by the file system methods or the generic implementation called by setup_swap_extents currently. So the above is a bit imprecise. The next patch then removes this confusing update.
On 5/12/26 07:35, Christoph Hellwig wrote: > Always use si->max which is updated setup_swap_extents instead of copying > into and out of maxpages. > > Signed-off-by: Christoph Hellwig <hch@lst.de> > --- > mm/swapfile.c | 27 +++++++++++---------------- > 1 file changed, 11 insertions(+), 16 deletions(-) > Reviewed-by: Hannes Reinecke <hare@kernel.org> Cheers, Hannes
On Tue, May 12, 2026 at 07:35:17AM +0200, Christoph Hellwig wrote: > Always use si->max which is updated setup_swap_extents instead of copying "...updated in setup_swap_extents..." > into and out of maxpages. > > Signed-off-by: Christoph Hellwig <hch@lst.de> But yes, it's much harder to track the data flows if we keep copying the value in and out of local variables. Reviewed-by: "Darrick J. Wong" <djwong@kernel.org> --D > --- > mm/swapfile.c | 27 +++++++++++---------------- > 1 file changed, 11 insertions(+), 16 deletions(-) > > diff --git a/mm/swapfile.c b/mm/swapfile.c > index 9174f1eeffb0..f7ebd97e28a3 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -3350,10 +3350,9 @@ static unsigned long read_swap_header(struct swap_info_struct *si, > } > > static int setup_swap_clusters_info(struct swap_info_struct *si, > - union swap_header *swap_header, > - unsigned long maxpages) > + union swap_header *swap_header) > { > - unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); > + unsigned long nr_clusters = DIV_ROUND_UP(si->max, SWAPFILE_CLUSTER); > struct swap_cluster_info *cluster_info; > int err = -ENOMEM; > unsigned long i; > @@ -3395,7 +3394,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, > if (err) > goto err; > } > - for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) { > + for (i = si->max; i < round_up(si->max, SWAPFILE_CLUSTER); i++) { > err = swap_cluster_setup_bad_slot(si, cluster_info, i, true); > if (err) > goto err; > @@ -3425,7 +3424,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, > si->cluster_info = cluster_info; > return 0; > err: > - free_swap_cluster_info(cluster_info, maxpages); > + free_swap_cluster_info(cluster_info, si->max); > return err; > } > > @@ -3440,7 +3439,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > union swap_header *swap_header; > int nr_extents; > sector_t span; > - unsigned long maxpages; > struct folio *folio = NULL; > struct inode *inode = NULL; > bool inced_nr_rotate_swap = false; > @@ -3512,14 +3510,13 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > } > swap_header = kmap_local_folio(folio, 0); > > - maxpages = read_swap_header(si, swap_header, inode); > - if (unlikely(!maxpages)) { > + si->max = read_swap_header(si, swap_header, inode); > + if (unlikely(!si->max)) { > error = -EINVAL; > goto bad_swap_unlock_inode; > } > > - si->max = maxpages; > - si->pages = maxpages - 1; > + si->pages = si->max - 1; > nr_extents = setup_swap_extents(si, swap_file, &span); > if (nr_extents < 0) { > error = nr_extents; > @@ -3531,14 +3528,12 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > goto bad_swap_unlock_inode; > } > > - maxpages = si->max; > - > /* Set up the swap cluster info */ > - error = setup_swap_clusters_info(si, swap_header, maxpages); > + error = setup_swap_clusters_info(si, swap_header); > if (error) > goto bad_swap_unlock_inode; > > - error = swap_cgroup_swapon(si->type, maxpages); > + error = swap_cgroup_swapon(si->type, si->max); > if (error) > goto bad_swap_unlock_inode; > > @@ -3546,7 +3541,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might > * be above MAX_PAGE_ORDER incase of a large swap file. > */ > - si->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), > + si->zeromap = kvmalloc_array(BITS_TO_LONGS(si->max), sizeof(long), > GFP_KERNEL | __GFP_ZERO); > if (!si->zeromap) { > error = -ENOMEM; > @@ -3597,7 +3592,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > } > } > > - error = zswap_swapon(si->type, maxpages); > + error = zswap_swapon(si->type, si->max); > if (error) > goto bad_swap_unlock_inode; > > -- > 2.53.0 > >
On Mon, May 11, 2026 at 10:36 PM Christoph Hellwig <hch@lst.de> wrote: > > Always use si->max which is updated setup_swap_extents instead of copying > into and out of maxpages. > > Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Chris Li <chrisl@kernel.org> > --- > mm/swapfile.c | 27 +++++++++++---------------- > 1 file changed, 11 insertions(+), 16 deletions(-) > > diff --git a/mm/swapfile.c b/mm/swapfile.c > index 9174f1eeffb0..f7ebd97e28a3 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -3350,10 +3350,9 @@ static unsigned long read_swap_header(struct swap_info_struct *si, > } > > static int setup_swap_clusters_info(struct swap_info_struct *si, > - union swap_header *swap_header, > - unsigned long maxpages) > + union swap_header *swap_header) > { > - unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); > + unsigned long nr_clusters = DIV_ROUND_UP(si->max, SWAPFILE_CLUSTER); > struct swap_cluster_info *cluster_info; > int err = -ENOMEM; > unsigned long i; > @@ -3395,7 +3394,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, > if (err) > goto err; > } > - for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) { > + for (i = si->max; i < round_up(si->max, SWAPFILE_CLUSTER); i++) { > err = swap_cluster_setup_bad_slot(si, cluster_info, i, true); Nitpick: I couldn't hlep but notice the si->max does not change between setup bad slots, so in theory you can cache the si->max value to a local variable for the loop. In real life, it will make no difference, so feel free to keep it as is. Chris
diff --git a/mm/swapfile.c b/mm/swapfile.c index 9174f1eeffb0..f7ebd97e28a3 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3350,10 +3350,9 @@ static unsigned long read_swap_header(struct swap_info_struct *si, } static int setup_swap_clusters_info(struct swap_info_struct *si, - union swap_header *swap_header, - unsigned long maxpages) + union swap_header *swap_header) { - unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); + unsigned long nr_clusters = DIV_ROUND_UP(si->max, SWAPFILE_CLUSTER); struct swap_cluster_info *cluster_info; int err = -ENOMEM; unsigned long i; @@ -3395,7 +3394,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, if (err) goto err; } - for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) { + for (i = si->max; i < round_up(si->max, SWAPFILE_CLUSTER); i++) { err = swap_cluster_setup_bad_slot(si, cluster_info, i, true); if (err) goto err; @@ -3425,7 +3424,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, si->cluster_info = cluster_info; return 0; err: - free_swap_cluster_info(cluster_info, maxpages); + free_swap_cluster_info(cluster_info, si->max); return err; } @@ -3440,7 +3439,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) union swap_header *swap_header; int nr_extents; sector_t span; - unsigned long maxpages; struct folio *folio = NULL; struct inode *inode = NULL; bool inced_nr_rotate_swap = false; @@ -3512,14 +3510,13 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) } swap_header = kmap_local_folio(folio, 0); - maxpages = read_swap_header(si, swap_header, inode); - if (unlikely(!maxpages)) { + si->max = read_swap_header(si, swap_header, inode); + if (unlikely(!si->max)) { error = -EINVAL; goto bad_swap_unlock_inode; } - si->max = maxpages; - si->pages = maxpages - 1; + si->pages = si->max - 1; nr_extents = setup_swap_extents(si, swap_file, &span); if (nr_extents < 0) { error = nr_extents; @@ -3531,14 +3528,12 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } - maxpages = si->max; - /* Set up the swap cluster info */ - error = setup_swap_clusters_info(si, swap_header, maxpages); + error = setup_swap_clusters_info(si, swap_header); if (error) goto bad_swap_unlock_inode; - error = swap_cgroup_swapon(si->type, maxpages); + error = swap_cgroup_swapon(si->type, si->max); if (error) goto bad_swap_unlock_inode; @@ -3546,7 +3541,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might * be above MAX_PAGE_ORDER incase of a large swap file. */ - si->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), + si->zeromap = kvmalloc_array(BITS_TO_LONGS(si->max), sizeof(long), GFP_KERNEL | __GFP_ZERO); if (!si->zeromap) { error = -ENOMEM; @@ -3597,7 +3592,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) } } - error = zswap_swapon(si->type, maxpages); + error = zswap_swapon(si->type, si->max); if (error) goto bad_swap_unlock_inode;
Always use si->max which is updated setup_swap_extents instead of copying into and out of maxpages. Signed-off-by: Christoph Hellwig <hch@lst.de> --- mm/swapfile.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-)