From: David Hildenbrand <david@redhat.com>
To: Barry Song <21cnbao@gmail.com>,
akpm@linux-foundation.org, linux-mm@kvack.org
Cc: justinjiang@vivo.com, chrisl@kernel.org, hughd@google.com,
kaleshsingh@google.com, kasong@tencent.com,
linux-kernel@vger.kernel.org, ryan.roberts@arm.com,
v-songbaohua@oppo.com, ying.huang@intel.com
Subject: Re: [PATCH v2 2/2] mm: attempt to batch free swap entries for zap_pte_range()
Date: Wed, 7 Aug 2024 11:28:59 +0200 [thread overview]
Message-ID: <e50abade-c44e-41d4-b7bf-b9d54920e2a4@redhat.com> (raw)
In-Reply-To: <20240807082508.358322-3-21cnbao@gmail.com>
> mm/swapfile.c | 78 +++++++++++++++++++++++++++++++++++++++++++--------
> 1 file changed, 67 insertions(+), 11 deletions(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 35cb58373493..25c3f98fa8d5 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -156,6 +156,25 @@ static bool swap_is_has_cache(struct swap_info_struct *si,
> return true;
> }
>
> +static bool swap_is_last_map(struct swap_info_struct *si,
> + unsigned long offset, int nr_pages,
> + bool *has_cache)
Please use double tabs for indenting parameters on 2nd line on
new/changed code:
unsigned long offset, int nr_pages, bool *has_cache)
Results in less churn when renaming functions and we can frequently
avoid some lines.
> +{
> + unsigned char *map = si->swap_map + offset;
> + unsigned char *map_end = map + nr_pages;
> + bool cached = false;
> +
> + do {
> + if ((*map & ~SWAP_HAS_CACHE) != 1)
> + return false;
> + if (*map & SWAP_HAS_CACHE)
> + cached = true;
> + } while (++map < map_end);
> +
> + *has_cache = cached;
> + return true;
> +}
> +
> /*
> * returns number of pages in the folio that backs the swap entry. If positive,
> * the folio was reclaimed. If negative, the folio was not reclaimed. If 0, no
> @@ -1469,6 +1488,53 @@ static unsigned char __swap_entry_free(struct swap_info_struct *si,
> return usage;
> }
>
> +static bool __swap_entries_free(struct swap_info_struct *si,
> + swp_entry_t entry, int nr)
Dito.
> +{
> + unsigned long offset = swp_offset(entry);
> + unsigned int type = swp_type(entry);
> + struct swap_cluster_info *ci;
> + bool has_cache = false;
> + unsigned char count;
> + bool can_batch;
> + int i;
> +
> + if (nr <= 1 || swap_count(data_race(si->swap_map[offset])) != 1)
> + goto fallback;
> + /* cross into another cluster */
> + if (nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER)
> + goto fallback;
> +
> + ci = lock_cluster_or_swap_info(si, offset);
> + can_batch = swap_is_last_map(si, offset, nr, &has_cache);
> + if (can_batch) {
> + for (i = 0; i < nr; i++)
> + WRITE_ONCE(si->swap_map[offset + i], SWAP_HAS_CACHE);
> + }
> + unlock_cluster_or_swap_info(si, ci);
> +
> + if (!can_batch)
> + goto fallback;
I'd avoid "can_batch" and just do:
ci = lock_cluster_or_swap_info(si, offset);
if (!swap_is_last_map(si, offset, nr, &has_cache)) {
unlock_cluster_or_swap_info(si, ci);
goto fallback;
}
for (i = 0; i < nr; i++)
WRITE_ONCE(si->swap_map[offset + i], SWAP_HAS_CACHE);
unlock_cluster_or_swap_info(si, ci);
> + if (!has_cache) {
> + spin_lock(&si->lock);
I'm no expert on that code, but we might drop the cluster lock the take
the swap_info lock and then retake the cluster lock. I assume there are
no races we are worrying about here, right?
> + swap_entry_range_free(si, entry, nr);
> + spin_unlock(&si->lock);
> + }
> + return has_cache;
> +
> +fallback:
> + for (i = 0; i < nr; i++) {
One space too much before the "<".
> + if (data_race(si->swap_map[offset + i])) {
> + count = __swap_entry_free(si, swp_entry(type, offset + i));
> + if (count == SWAP_HAS_CACHE)
> + has_cache = true;
> + } else {
> + WARN_ON_ONCE(1);
> + }
> + }
> + return has_cache;
> +}
> +
--
Cheers,
David / dhildenb
next prev parent reply other threads:[~2024-08-07 9:29 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-07 8:25 [PATCH v2 0/2] mm: batch free swaps " Barry Song
2024-08-07 8:25 ` [PATCH v2 1/2] mm: rename instances of swap_info_struct to meaningful 'si' Barry Song
2024-08-07 9:11 ` David Hildenbrand
2024-08-07 8:25 ` [PATCH v2 2/2] mm: attempt to batch free swap entries for zap_pte_range() Barry Song
2024-08-07 9:28 ` David Hildenbrand [this message]
2024-08-07 9:48 ` Barry Song
2024-08-07 10:19 ` David Hildenbrand
2024-08-07 16:16 ` Kairui Song
2024-08-07 19:14 ` 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=e50abade-c44e-41d4-b7bf-b9d54920e2a4@redhat.com \
--to=david@redhat.com \
--cc=21cnbao@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=chrisl@kernel.org \
--cc=hughd@google.com \
--cc=justinjiang@vivo.com \
--cc=kaleshsingh@google.com \
--cc=kasong@tencent.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ryan.roberts@arm.com \
--cc=v-songbaohua@oppo.com \
--cc=ying.huang@intel.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
all inboxes | Powered by JetHome®