Linux-mm Archive mirror
 help / color / mirror / Atom feed
From: Chris Li <chrisl@kernel.org>
To: Barry Song <21cnbao@gmail.com>
Cc: akpm@linux-foundation.org, linux-mm@kvack.org,
	 baolin.wang@linux.alibaba.com, david@redhat.com,
	hanchuanhua@oppo.com,  hannes@cmpxchg.org, hughd@google.com,
	kasong@tencent.com,  linux-kernel@vger.kernel.org,
	ryan.roberts@arm.com, surenb@google.com,  v-songbaohua@oppo.com,
	willy@infradead.org, xiang@kernel.org,  ying.huang@intel.com,
	yosryahmed@google.com, yuzhao@google.com,  ziy@nvidia.com
Subject: Re: [PATCH v3 1/6] mm: swap: introduce swap_free_nr() for batched swap_free()
Date: Fri, 3 May 2024 13:25:22 -0700	[thread overview]
Message-ID: <CANeU7Qkqjxp4hHceK3f5ZAxRiidRN_mpYpTc44oBZ9T=hESMSg@mail.gmail.com> (raw)
In-Reply-To: <20240503005023.174597-2-21cnbao@gmail.com>

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
>


  parent reply	other threads:[~2024-05-03 20:25 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-03  0:50 [PATCH v3 0/6] large folios swap-in: handle refault cases first Barry Song
2024-05-03  0:50 ` [PATCH v3 1/6] mm: swap: introduce swap_free_nr() for batched swap_free() Barry Song
2024-05-03  9:26   ` Ryan Roberts
2024-05-03 20:25   ` Chris Li [this message]
2024-05-08  7:35   ` Huang, Ying
2024-05-03  0:50 ` [PATCH v3 2/6] mm: remove swap_free() and always use swap_free_nr() Barry Song
2024-05-03  9:31   ` Ryan Roberts
2024-05-03 20:37     ` Chris Li
2024-05-04  4:03       ` Christoph Hellwig
2024-05-04  4:27         ` Barry Song
2024-05-04  4:28           ` Christoph Hellwig
2024-05-04  4:47             ` Barry Song
2024-05-08  7:56     ` Huang, Ying
2024-05-08  8:30       ` Barry Song
2024-05-08  9:10         ` Ryan Roberts
2024-05-03  0:50 ` [PATCH v3 3/6] mm: introduce pte_move_swp_offset() helper which can move offset bidirectionally Barry Song
2024-05-03  9:41   ` Ryan Roberts
2024-05-03 23:40     ` Barry Song
2024-05-06  8:06       ` David Hildenbrand
2024-05-06  8:20         ` Barry Song
2024-05-06  8:31           ` David Hildenbrand
2024-05-07  8:14             ` Ryan Roberts
2024-05-07  8:24               ` Barry Song
2024-05-07  9:39                 ` Ryan Roberts
2024-05-03 20:51   ` Chris Li
2024-05-03 23:07     ` Barry Song
2024-05-08  8:08   ` Huang, Ying
2024-05-03  0:50 ` [PATCH v3 4/6] mm: introduce arch_do_swap_page_nr() which allows restore metadata for nr pages Barry Song
2024-05-03 10:02   ` Ryan Roberts
2024-05-06 16:51   ` Khalid Aziz
2024-05-03  0:50 ` [PATCH v3 5/6] mm: swap: make should_try_to_free_swap() support large-folio Barry Song
2024-05-03  0:50 ` [PATCH v3 6/6] mm: swap: entirely map large folios found in swapcache Barry Song
2024-05-03 10:50   ` Ryan Roberts
2024-05-03 23:23     ` Barry Song
2024-05-06 12:07       ` David Hildenbrand
2024-05-06 12:38         ` Barry Song
2024-05-06 12:58           ` Barry Song
2024-05-06 13:16             ` David Hildenbrand
2024-05-06 22:58               ` Barry Song
2024-05-07  8:24                 ` David Hildenbrand
2024-05-07  8:43                   ` Barry Song
2024-05-07  8:59                     ` David Hildenbrand
2024-05-07  9:24                       ` Barry Song
2024-05-07 10:39                         ` David Hildenbrand
2024-05-07 10:48                           ` Barry Song
2024-05-07  8:17       ` Ryan Roberts
2024-05-06 12:05   ` David Hildenbrand
2024-05-06 12:27     ` Barry Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CANeU7Qkqjxp4hHceK3f5ZAxRiidRN_mpYpTc44oBZ9T=hESMSg@mail.gmail.com' \
    --to=chrisl@kernel.org \
    --cc=21cnbao@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@redhat.com \
    --cc=hanchuanhua@oppo.com \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=kasong@tencent.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ryan.roberts@arm.com \
    --cc=surenb@google.com \
    --cc=v-songbaohua@oppo.com \
    --cc=willy@infradead.org \
    --cc=xiang@kernel.org \
    --cc=ying.huang@intel.com \
    --cc=yosryahmed@google.com \
    --cc=yuzhao@google.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).