Message ID | 20240503005023.174597-2-21cnbao@gmail.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | large folios swap-in: handle refault cases first | expand |
On 03/05/2024 01:50, Barry Song wrote: > From: Chuanhua Han <hanchuanhua@oppo.com> > > While swapping in a large folio, we need to free swaps related to the whole > folio. To avoid frequently acquiring and releasing swap locks, it is better > to introduce an API for batched free. > Furthermore, this new function, swap_free_nr(), is designed to efficiently > handle various scenarios for releasing a specified number, nr, of swap > entries. > > Signed-off-by: Chuanhua Han <hanchuanhua@oppo.com> > Co-developed-by: Barry Song <v-songbaohua@oppo.com> > Signed-off-by: Barry Song <v-songbaohua@oppo.com> This looks much better! Reviewed-by: Ryan Roberts <ryan.roberts@arm.com> > --- > include/linux/swap.h | 5 +++++ > mm/swapfile.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 52 insertions(+) > > diff --git a/include/linux/swap.h b/include/linux/swap.h > index 11c53692f65f..d1d35e92d7e9 100644 > --- a/include/linux/swap.h > +++ b/include/linux/swap.h > @@ -483,6 +483,7 @@ extern void swap_shmem_alloc(swp_entry_t); > extern int swap_duplicate(swp_entry_t); > extern int swapcache_prepare(swp_entry_t); > extern void swap_free(swp_entry_t); > +extern void swap_free_nr(swp_entry_t entry, int nr_pages); > extern void swapcache_free_entries(swp_entry_t *entries, int n); > extern void free_swap_and_cache_nr(swp_entry_t entry, int nr); > int swap_type_of(dev_t device, sector_t offset); > @@ -564,6 +565,10 @@ static inline void swap_free(swp_entry_t swp) > { > } > > +static inline void swap_free_nr(swp_entry_t entry, int nr_pages) > +{ > +} > + > static inline void put_swap_folio(struct folio *folio, swp_entry_t swp) > { > } > diff --git a/mm/swapfile.c b/mm/swapfile.c > index f6ca215fb92f..ec12f2b9d229 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -1356,6 +1356,53 @@ void swap_free(swp_entry_t entry) > __swap_entry_free(p, entry); > } > > +static void cluster_swap_free_nr(struct swap_info_struct *sis, > + unsigned long offset, int nr_pages) > +{ > + struct swap_cluster_info *ci; > + DECLARE_BITMAP(to_free, BITS_PER_LONG) = { 0 }; > + int i, nr; > + > + ci = lock_cluster_or_swap_info(sis, offset); > + while (nr_pages) { > + nr = min(BITS_PER_LONG, nr_pages); > + for (i = 0; i < nr; i++) { > + if (!__swap_entry_free_locked(sis, offset + i, 1)) > + bitmap_set(to_free, i, 1); > + } > + if (!bitmap_empty(to_free, BITS_PER_LONG)) { > + unlock_cluster_or_swap_info(sis, ci); > + for_each_set_bit(i, to_free, BITS_PER_LONG) > + free_swap_slot(swp_entry(sis->type, offset + i)); > + if (nr == nr_pages) > + return; > + bitmap_clear(to_free, 0, BITS_PER_LONG); > + ci = lock_cluster_or_swap_info(sis, offset); > + } > + offset += nr; > + nr_pages -= nr; > + } > + unlock_cluster_or_swap_info(sis, ci); > +} > + > +void swap_free_nr(swp_entry_t entry, int nr_pages) > +{ > + int nr; > + struct swap_info_struct *sis; > + unsigned long offset = swp_offset(entry); > + > + sis = _swap_info_get(entry); > + if (!sis) > + return; > + > + while (nr_pages) { > + nr = min_t(int, nr_pages, SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); > + cluster_swap_free_nr(sis, offset, nr); > + offset += nr; > + nr_pages -= nr; > + } > +} > + > /* > * Called after dropping swapcache to decrease refcnt to swap entries. > */
Hi Barry, Looks good. Looking forward to the change to batch free to skipping the swap slot cache. All the entries are from the same swap device, it does not need the sort function. Currently it goes through a lot of locking and and unlocking inside the loop. Acked-by: Chris Li <chrisl@kernel.org> Chris On Thu, May 2, 2024 at 5:50 PM Barry Song <21cnbao@gmail.com> wrote: > > From: Chuanhua Han <hanchuanhua@oppo.com> > > While swapping in a large folio, we need to free swaps related to the whole > folio. To avoid frequently acquiring and releasing swap locks, it is better > to introduce an API for batched free. > Furthermore, this new function, swap_free_nr(), is designed to efficiently > handle various scenarios for releasing a specified number, nr, of swap > entries. > > Signed-off-by: Chuanhua Han <hanchuanhua@oppo.com> > Co-developed-by: Barry Song <v-songbaohua@oppo.com> > Signed-off-by: Barry Song <v-songbaohua@oppo.com> > --- > include/linux/swap.h | 5 +++++ > mm/swapfile.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 52 insertions(+) > > diff --git a/include/linux/swap.h b/include/linux/swap.h > index 11c53692f65f..d1d35e92d7e9 100644 > --- a/include/linux/swap.h > +++ b/include/linux/swap.h > @@ -483,6 +483,7 @@ extern void swap_shmem_alloc(swp_entry_t); > extern int swap_duplicate(swp_entry_t); > extern int swapcache_prepare(swp_entry_t); > extern void swap_free(swp_entry_t); > +extern void swap_free_nr(swp_entry_t entry, int nr_pages); > extern void swapcache_free_entries(swp_entry_t *entries, int n); > extern void free_swap_and_cache_nr(swp_entry_t entry, int nr); > int swap_type_of(dev_t device, sector_t offset); > @@ -564,6 +565,10 @@ static inline void swap_free(swp_entry_t swp) > { > } > > +static inline void swap_free_nr(swp_entry_t entry, int nr_pages) > +{ > +} > + > static inline void put_swap_folio(struct folio *folio, swp_entry_t swp) > { > } > diff --git a/mm/swapfile.c b/mm/swapfile.c > index f6ca215fb92f..ec12f2b9d229 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -1356,6 +1356,53 @@ void swap_free(swp_entry_t entry) > __swap_entry_free(p, entry); > } > > +static void cluster_swap_free_nr(struct swap_info_struct *sis, > + unsigned long offset, int nr_pages) > +{ > + struct swap_cluster_info *ci; > + DECLARE_BITMAP(to_free, BITS_PER_LONG) = { 0 }; > + int i, nr; > + > + ci = lock_cluster_or_swap_info(sis, offset); > + while (nr_pages) { > + nr = min(BITS_PER_LONG, nr_pages); > + for (i = 0; i < nr; i++) { > + if (!__swap_entry_free_locked(sis, offset + i, 1)) > + bitmap_set(to_free, i, 1); > + } > + if (!bitmap_empty(to_free, BITS_PER_LONG)) { > + unlock_cluster_or_swap_info(sis, ci); > + for_each_set_bit(i, to_free, BITS_PER_LONG) > + free_swap_slot(swp_entry(sis->type, offset + i)); > + if (nr == nr_pages) > + return; > + bitmap_clear(to_free, 0, BITS_PER_LONG); > + ci = lock_cluster_or_swap_info(sis, offset); > + } > + offset += nr; > + nr_pages -= nr; > + } > + unlock_cluster_or_swap_info(sis, ci); > +} > + > +void swap_free_nr(swp_entry_t entry, int nr_pages) > +{ > + int nr; > + struct swap_info_struct *sis; > + unsigned long offset = swp_offset(entry); > + > + sis = _swap_info_get(entry); > + if (!sis) > + return; > + > + while (nr_pages) { > + nr = min_t(int, nr_pages, SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); > + cluster_swap_free_nr(sis, offset, nr); > + offset += nr; > + nr_pages -= nr; > + } > +} > + > /* > * Called after dropping swapcache to decrease refcnt to swap entries. > */ > -- > 2.34.1 >
Barry Song <21cnbao@gmail.com> writes: > From: Chuanhua Han <hanchuanhua@oppo.com> > > While swapping in a large folio, we need to free swaps related to the whole > folio. To avoid frequently acquiring and releasing swap locks, it is better > to introduce an API for batched free. > Furthermore, this new function, swap_free_nr(), is designed to efficiently > handle various scenarios for releasing a specified number, nr, of swap > entries. > > Signed-off-by: Chuanhua Han <hanchuanhua@oppo.com> > Co-developed-by: Barry Song <v-songbaohua@oppo.com> > Signed-off-by: Barry Song <v-songbaohua@oppo.com> LGTM, Thanks! Reviewed-by: "Huang, Ying" <ying.huang@intel.com> > --- > include/linux/swap.h | 5 +++++ > mm/swapfile.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 52 insertions(+) > > diff --git a/include/linux/swap.h b/include/linux/swap.h > index 11c53692f65f..d1d35e92d7e9 100644 > --- a/include/linux/swap.h > +++ b/include/linux/swap.h > @@ -483,6 +483,7 @@ extern void swap_shmem_alloc(swp_entry_t); > extern int swap_duplicate(swp_entry_t); > extern int swapcache_prepare(swp_entry_t); > extern void swap_free(swp_entry_t); > +extern void swap_free_nr(swp_entry_t entry, int nr_pages); > extern void swapcache_free_entries(swp_entry_t *entries, int n); > extern void free_swap_and_cache_nr(swp_entry_t entry, int nr); > int swap_type_of(dev_t device, sector_t offset); > @@ -564,6 +565,10 @@ static inline void swap_free(swp_entry_t swp) > { > } > > +static inline void swap_free_nr(swp_entry_t entry, int nr_pages) > +{ > +} > + > static inline void put_swap_folio(struct folio *folio, swp_entry_t swp) > { > } > diff --git a/mm/swapfile.c b/mm/swapfile.c > index f6ca215fb92f..ec12f2b9d229 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -1356,6 +1356,53 @@ void swap_free(swp_entry_t entry) > __swap_entry_free(p, entry); > } > > +static void cluster_swap_free_nr(struct swap_info_struct *sis, > + unsigned long offset, int nr_pages) > +{ > + struct swap_cluster_info *ci; > + DECLARE_BITMAP(to_free, BITS_PER_LONG) = { 0 }; > + int i, nr; > + > + ci = lock_cluster_or_swap_info(sis, offset); > + while (nr_pages) { > + nr = min(BITS_PER_LONG, nr_pages); > + for (i = 0; i < nr; i++) { > + if (!__swap_entry_free_locked(sis, offset + i, 1)) > + bitmap_set(to_free, i, 1); > + } > + if (!bitmap_empty(to_free, BITS_PER_LONG)) { > + unlock_cluster_or_swap_info(sis, ci); > + for_each_set_bit(i, to_free, BITS_PER_LONG) > + free_swap_slot(swp_entry(sis->type, offset + i)); > + if (nr == nr_pages) > + return; > + bitmap_clear(to_free, 0, BITS_PER_LONG); > + ci = lock_cluster_or_swap_info(sis, offset); > + } > + offset += nr; > + nr_pages -= nr; > + } > + unlock_cluster_or_swap_info(sis, ci); > +} > + > +void swap_free_nr(swp_entry_t entry, int nr_pages) > +{ > + int nr; > + struct swap_info_struct *sis; > + unsigned long offset = swp_offset(entry); > + > + sis = _swap_info_get(entry); > + if (!sis) > + return; > + > + while (nr_pages) { > + nr = min_t(int, nr_pages, SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); > + cluster_swap_free_nr(sis, offset, nr); > + offset += nr; > + nr_pages -= nr; > + } > +} > + > /* > * Called after dropping swapcache to decrease refcnt to swap entries. > */
diff --git a/include/linux/swap.h b/include/linux/swap.h index 11c53692f65f..d1d35e92d7e9 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -483,6 +483,7 @@ extern void swap_shmem_alloc(swp_entry_t); extern int swap_duplicate(swp_entry_t); extern int swapcache_prepare(swp_entry_t); extern void swap_free(swp_entry_t); +extern void swap_free_nr(swp_entry_t entry, int nr_pages); extern void swapcache_free_entries(swp_entry_t *entries, int n); extern void free_swap_and_cache_nr(swp_entry_t entry, int nr); int swap_type_of(dev_t device, sector_t offset); @@ -564,6 +565,10 @@ static inline void swap_free(swp_entry_t swp) { } +static inline void swap_free_nr(swp_entry_t entry, int nr_pages) +{ +} + static inline void put_swap_folio(struct folio *folio, swp_entry_t swp) { } diff --git a/mm/swapfile.c b/mm/swapfile.c index f6ca215fb92f..ec12f2b9d229 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1356,6 +1356,53 @@ void swap_free(swp_entry_t entry) __swap_entry_free(p, entry); } +static void cluster_swap_free_nr(struct swap_info_struct *sis, + unsigned long offset, int nr_pages) +{ + struct swap_cluster_info *ci; + DECLARE_BITMAP(to_free, BITS_PER_LONG) = { 0 }; + int i, nr; + + ci = lock_cluster_or_swap_info(sis, offset); + while (nr_pages) { + nr = min(BITS_PER_LONG, nr_pages); + for (i = 0; i < nr; i++) { + if (!__swap_entry_free_locked(sis, offset + i, 1)) + bitmap_set(to_free, i, 1); + } + if (!bitmap_empty(to_free, BITS_PER_LONG)) { + unlock_cluster_or_swap_info(sis, ci); + for_each_set_bit(i, to_free, BITS_PER_LONG) + free_swap_slot(swp_entry(sis->type, offset + i)); + if (nr == nr_pages) + return; + bitmap_clear(to_free, 0, BITS_PER_LONG); + ci = lock_cluster_or_swap_info(sis, offset); + } + offset += nr; + nr_pages -= nr; + } + unlock_cluster_or_swap_info(sis, ci); +} + +void swap_free_nr(swp_entry_t entry, int nr_pages) +{ + int nr; + struct swap_info_struct *sis; + unsigned long offset = swp_offset(entry); + + sis = _swap_info_get(entry); + if (!sis) + return; + + while (nr_pages) { + nr = min_t(int, nr_pages, SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); + cluster_swap_free_nr(sis, offset, nr); + offset += nr; + nr_pages -= nr; + } +} + /* * Called after dropping swapcache to decrease refcnt to swap entries. */