From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94E04C433EF for ; Tue, 29 Mar 2022 21:53:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A9B498D0002; Tue, 29 Mar 2022 17:53:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A49F68D0001; Tue, 29 Mar 2022 17:53:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EAA58D0002; Tue, 29 Mar 2022 17:53:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0006.hostedemail.com [216.40.44.6]) by kanga.kvack.org (Postfix) with ESMTP id 7BC588D0001 for ; Tue, 29 Mar 2022 17:53:37 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id D467DA45CB for ; Tue, 29 Mar 2022 21:53:36 +0000 (UTC) X-FDA: 79298776032.27.AE48E7E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf03.hostedemail.com (Postfix) with ESMTP id F22D42000A for ; Tue, 29 Mar 2022 21:53:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1648590815; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=puBJtnI/eFwkwibbXKroPb7DsEqz6d/eI2XJNgk+jx4=; b=KmER3NaWMdx0T1uI/iCn854ktIO8OM8Eh+4HA/rzYVMySMOeOVSqWuNRy6pq2lsH3GtbvG pPRiOJQUP6XMnjbatvYFL6YidGmLK7iqjMWRdV1xM48f6L1z0HCFGQO0aKqY4YK+ctfDkr QA4DSxRzNopknoZUriahGlZ8TFgfj3U= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-137-AHPZ-0BLMtSOAcWUBzuaug-1; Tue, 29 Mar 2022 17:53:33 -0400 X-MC-Unique: AHPZ-0BLMtSOAcWUBzuaug-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7AE37801585; Tue, 29 Mar 2022 21:53:33 +0000 (UTC) Received: from [10.18.17.215] (dhcp-17-215.bos.redhat.com [10.18.17.215]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3547F2024CB7; Tue, 29 Mar 2022 21:53:33 +0000 (UTC) Message-ID: <07be89ad-e355-69b9-6e36-07beaebf2d8b@redhat.com> Date: Tue, 29 Mar 2022 17:53:33 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH-mm v3] mm/list_lru: Optimize memcg_reparent_list_lru_node() Content-Language: en-US To: Muchun Song , Roman Gushchin Cc: Andrew Morton , Linux Memory Management List , LKML References: <20220309144000.1470138-1-longman@redhat.com> <2263666d-5eef-b1fe-d5e3-b166a3185263@redhat.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Stat-Signature: gzq9ek89q6jykpobyahtbj7hfuz1xxgo Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KmER3NaW; spf=none (imf03.hostedemail.com: domain of longman@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: F22D42000A X-HE-Tag: 1648590815-252884 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 3/28/22 21:15, Muchun Song wrote: > On Tue, Mar 29, 2022 at 3:12 AM Roman Gushchin wrote: >> On Sun, Mar 27, 2022 at 08:57:15PM -0400, Waiman Long wrote: >>> On 3/22/22 22:12, Muchun Song wrote: >>>> On Wed, Mar 23, 2022 at 9:55 AM Waiman Long wro= te: >>>>> On 3/22/22 21:06, Muchun Song wrote: >>>>>> On Wed, Mar 9, 2022 at 10:40 PM Waiman Long w= rote: >>>>>>> Since commit 2c80cd57c743 ("mm/list_lru.c: fix list_lru_count_nod= e() >>>>>>> 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 en= try >>>>>>> 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 inf= light >>>>>> list_lru_add()? How about the following race? >>>>>> >>>>>> CPU0: CPU1: >>>>>> list_lru_add() >>>>>> spin_lock(&nlru->lock) >>>>>> l =3D list_lru_from_kmem(memcg) >>>>>> memcg_reparent_objcgs(memcg= ) >>>>>> memcg_reparent_list_lrus(me= mcg) >>>>>> memcg_reparent_list_lru= () >>>>>> memcg_reparent_list= _lru_node() >>>>>> if (!READ_ONCE(= nlru->nr_items)) >>>>>> // Miss rep= arenting >>>>>> 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 increm= ent >>>>> of nr_items. >>>> It is more possible in a VM. >>>> >>>>> How about the following changes to make sure that this race can't h= appen? >>>>> >>>>> 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(struc= t >>>>> 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->lo= ck)) >>>> I think we also should insert a smp_rmb() between those two loads. >>> Thinking about this some more, I believe that adding spin_is_locked()= check >>> will be enough for x86. However, that will likely not be enough for a= rches >>> with a more relaxed memory semantics. So the safest way to avoid this >>> possible race is to move the check to within the lock critical sectio= n, >>> though that comes with a slightly higher overhead for the 0 nr_items = case. I >>> will send out a patch to correct that. Thanks for bring this possible= race >>> to my attention. >> Yes, I think it's not enough: > I think it may be enough if we insert a smp_rmb() between those two loa= ds. > >> CPU0 CPU1 >> READ_ONCE(&nlru->nr_items) -> 0 >> spin_lock(&nlru->lock); >> nlru->nr_items++; > ^^^ > ||| > The nlr here is not the > same as the one in CPU0, > since CPU0 have done the > memcg reparting. Then > CPU0 will not miss nlru > reparting. If I am wrong, please > correct me. Thanks. >> spin_unlock(&nlru->lock); >> && !spin_is_locked(&nlru->lock) -> 0 I just realize that there is another lock/unlock pair in=20 memcg_reparent_objcgs(): memcg_reparent_objcgs() =C2=A0=C2=A0=C2=A0 spin_lock_irq() =C2=A0=C2=A0=C2=A0 memcg reparenting =C2=A0=C2=A0=C2=A0 spin_unlock_irq() =C2=A0=C2=A0=C2=A0 percpu_ref_kill() =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 spin_lock_irqsave() =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ... =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 spin_unlock_irqrestore() This lock/unlock pair in percpu_ref_kill() will stop the reordering of=20 read/write before the memcg reparenting. Now I think just adding a=20 spin_is_locked() check with smp_rmb() should be enough. However, I would=20 like to change the ordering like that: if (!spin_is_locked(&nlru->lock)) { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 smp_rmb(); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (!READ_ONCE(nlru->nr_items= )) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 return; } Otherwise, we will have the problem list_lru_add() =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 spin_lock(&nlru->lock) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 l =3D list_lru_from_kmem(memcg) READ_ONCE(nlru->nr_items); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // Assume 0->1 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 l->nr_items++ =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // Assume 0->1 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 nlru->nr_items++ =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 spin_unlock(&nlru->lock) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= spin_is_locked() If spin_is_locked() is before spin_lock() in list_lru_add(),=20 list_lru_from_kmem() is guarantee to get the reparented memcg and so=20 won't added to the reparented lru. Thanks, Longman