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 0A03AC433EF for ; Mon, 28 Mar 2022 19:12:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 76B208D0002; Mon, 28 Mar 2022 15:12:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 71AB48D0001; Mon, 28 Mar 2022 15:12:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E3448D0002; Mon, 28 Mar 2022 15:12:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0235.hostedemail.com [216.40.44.235]) by kanga.kvack.org (Postfix) with ESMTP id 4D10D8D0001 for ; Mon, 28 Mar 2022 15:12:20 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 095668249980 for ; Mon, 28 Mar 2022 19:12:20 +0000 (UTC) X-FDA: 79294740840.30.0F7BFA3 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf16.hostedemail.com (Postfix) with ESMTP id 47B4F180034 for ; Mon, 28 Mar 2022 19:12:19 +0000 (UTC) Date: Mon, 28 Mar 2022 12:12:12 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1648494737; 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: in-reply-to:in-reply-to:references:references; bh=8pHkCo/gdRv4VedvapgXEYnE/62prPMyEb6hZYV3es8=; b=t9tc2STGox8vO6ih3HLbuu+28UG4g40sJCUn6N4HOSguqH4MwV/DLaXfxB80qMUo2nYNJT DyOpCDb6F4+8uAH0sU7MVsH+uBtYNh0C8yaocdC2D2CFOB2IMGrEaV4vOFuOoBJ9nYH5oz ijbV7LGHWOVXRK3qPgnpikPHX1x0HKI= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: Waiman Long Cc: Muchun Song , Andrew Morton , Linux Memory Management List , LKML Subject: Re: [PATCH-mm v3] mm/list_lru: Optimize memcg_reparent_list_lru_node() Message-ID: References: <20220309144000.1470138-1-longman@redhat.com> <2263666d-5eef-b1fe-d5e3-b166a3185263@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Stat-Signature: ci1sk1uu1c1m1gkkk1aow4ru96ksixhs X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 47B4F180034 Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=t9tc2STG; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf16.hostedemail.com: domain of roman.gushchin@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev X-Rspam-User: X-HE-Tag: 1648494739-680514 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 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 wrote: > > > On 3/22/22 21:06, Muchun Song wrote: > > > > On Wed, Mar 9, 2022 at 10:40 PM Waiman Long 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. > > It is more possible in a VM. > > > > > 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)) > > 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 arches > 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 section, > 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: CPU0 CPU1 READ_ONCE(&nlru->nr_items) -> 0 spin_lock(&nlru->lock); nlru->nr_items++; spin_unlock(&nlru->lock); && !spin_is_locked(&nlru->lock) -> 0 Getting back to the original patch, I wonder if instead we can batch reparenting of lrus so we don't have to grab and release nlru->lock for each reparenting lru. Thanks!