All of lore.kernel.org
 help / color / mirror / Atom feed
From: Waiman Long <longman@redhat.com>
To: Muchun Song <songmuchun@bytedance.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Linux Memory Management List <linux-mm@kvack.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>
Subject: Re: [PATCH-mm v3] mm/list_lru: Optimize memcg_reparent_list_lru_node()
Date: Tue, 22 Mar 2022 21:55:05 -0400	[thread overview]
Message-ID: <2263666d-5eef-b1fe-d5e3-b166a3185263@redhat.com> (raw)
In-Reply-To: <CAMZfGtWS581YW4Y8oNU=E_zPnpK=mMdYVSG1F3U3fJNAzBzc+g@mail.gmail.com>

On 3/22/22 21:06, Muchun Song wrote:
> On Wed, Mar 9, 2022 at 10:40 PM Waiman Long <longman@redhat.com> wrote:
>> Since commit 2c80cd57c743 ("mm/list_lru.c: fix list_lru_count_node()
>> to be race free"), we are tracking the total number of lru
>> entries in a list_lru_node in its nr_items field.  In the case of
>> memcg_reparent_list_lru_node(), there is nothing to be done if nr_items
>> is 0.  We don't even need to take the nlru->lock as no new lru entry
>> could be added by a racing list_lru_add() to the draining src_idx memcg
>> at this point.
> Hi Waiman,
>
> Sorry for the late reply.  Quick question: what if there is an inflight
> list_lru_add()?  How about the following race?
>
> CPU0:                               CPU1:
> list_lru_add()
>      spin_lock(&nlru->lock)
>      l = list_lru_from_kmem(memcg)
>                                      memcg_reparent_objcgs(memcg)
>                                      memcg_reparent_list_lrus(memcg)
>                                          memcg_reparent_list_lru()
>                                              memcg_reparent_list_lru_node()
>                                                  if (!READ_ONCE(nlru->nr_items))
>                                                      // Miss reparenting
>                                                      return
>      // Assume 0->1
>      l->nr_items++
>      // Assume 0->1
>      nlru->nr_items++
>
> IIUC, we use nlru->lock to serialise this scenario.

I guess this race is theoretically possible but very unlikely since it 
means a very long pause between list_lru_from_kmem() and the increment 
of nr_items.

How about the following changes to make sure that this race can't happen?

diff --git a/mm/list_lru.c b/mm/list_lru.c
index c669d87001a6..c31a0a8ad4e7 100644
--- a/mm/list_lru.c
+++ b/mm/list_lru.c
@@ -395,9 +395,10 @@ static void memcg_reparent_list_lru_node(struct 
list_lru *lru, int nid,
         struct list_lru_one *src, *dst;

         /*
-        * If there is no lru entry in this nlru, we can skip it 
immediately.
+        * If there is no lru entry in this nlru and the nlru->lock is free,
+        * we can skip it immediately.
          */
-       if (!READ_ONCE(nlru->nr_items))
+       if (!READ_ONCE(nlru->nr_items) && !spin_is_locked(&nlru->lock))
                 return;

Cheers,
Longman


  reply	other threads:[~2022-03-23  1:55 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-09 14:40 [PATCH-mm v3] mm/list_lru: Optimize memcg_reparent_list_lru_node() Waiman Long
2022-03-23  1:06 ` Muchun Song
2022-03-23  1:55   ` Waiman Long [this message]
2022-03-23  2:12     ` Muchun Song
2022-03-28  0:57       ` Waiman Long
2022-03-28 19:12         ` Roman Gushchin
2022-03-28 20:46           ` Waiman Long
2022-03-28 21:12             ` Roman Gushchin
2022-03-28 21:20               ` Waiman Long
2022-03-28 23:44                 ` Roman Gushchin
2022-03-29  1:15           ` Muchun Song
2022-03-29  2:30             ` Roman Gushchin
2022-03-29 21:53             ` Waiman Long
2022-03-30  6:38               ` Muchun Song
2022-03-30  7:20                 ` Muchun Song
2022-03-28 19:12   ` Roman Gushchin

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=2263666d-5eef-b1fe-d5e3-b166a3185263@redhat.com \
    --to=longman@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=roman.gushchin@linux.dev \
    --cc=songmuchun@bytedance.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.