Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp3837699ioo; Wed, 25 May 2022 09:07:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyKktV1dfMtLm1kby06nNxQA5RbfN3un3Hy/QN3nRRYPoaAod9upybovM3UeJZ7kURYeZjC X-Received: by 2002:a17:902:c412:b0:161:af8b:f478 with SMTP id k18-20020a170902c41200b00161af8bf478mr33505630plk.67.1653494859820; Wed, 25 May 2022 09:07:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653494859; cv=none; d=google.com; s=arc-20160816; b=ilz51jrdih2PUp6m0wxnTRYAWBJ85Wgcgr24KX9hEO1+cIsXU95N8HNzpxaBW/sbUI qNpqult22eWBW+u/w9ZS6W5WwjJjwaHTOe1stJM3/fO7OMbMc6kuDYpdMkJNzo6yoqg6 MEU1JhiqJsqgDwRDvxBygrROuOe0diwVVX15q2HRuy6jbqbGHhDTWm82KRZHJfuHefQ/ HXOHQl2FHKmplBhxgBGKfqSs27yLrE/Q7EFRWsdSS71V0M6HWWypoe+lfOuroKDMnMXN xFp/tCw6DMCr5dSTJivPrmGorpcZhjSGee5NFJoxgfAQPa8ES/YZLbmTZBAxFuksU/QE oItw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ZwBT4g14vYXymE6X+0Ue3RPk57uheIJlCkFHpAJ6pRk=; b=FAJupoc4BEXSrjFbBmigQBhPQJsqINd9Tyi1Wv3YpcNGGywlcFI76ZfDznufO6HXQu LxEnBO9WQqVS7MhCFX2ppmTW/I09rjlvnPhE6RvjiZykyrxUmBi4uHzeid0qjhAsBxVw ULXOl+PFgTOuS1b9UgVVElE+jSm1QFO2E0f9cmkCXmBm3PF1T1iS5sCsPOilph9c1ZtR Qvag6KbF+jh48ZP+iPAJSugzKWrI6Tu4v+r6WRuL884mQC/fj+TxXaZQ+miH/4vWfE1F /c47K8IutpiF7KgtLrxKKZTMMqJD/mdMQwFnOYXWCrfDPVjP54iPM4Luk9IDogiOSlcx OG/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=ZidvWTWQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h63-20020a625342000000b0050d3f057ea9si20993462pfb.235.2022.05.25.09.07.27; Wed, 25 May 2022 09:07:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=ZidvWTWQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233801AbiEYNEP (ORCPT + 99 others); Wed, 25 May 2022 09:04:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243674AbiEYNEM (ORCPT ); Wed, 25 May 2022 09:04:12 -0400 Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A753A5A92 for ; Wed, 25 May 2022 06:04:11 -0700 (PDT) Received: by mail-pg1-x530.google.com with SMTP id a9so16825150pgv.12 for ; Wed, 25 May 2022 06:04:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ZwBT4g14vYXymE6X+0Ue3RPk57uheIJlCkFHpAJ6pRk=; b=ZidvWTWQLhs+qxK+czFWiUM2yn05AKjy33y9lcAXvSJqpWLzIj1uMe9jxplyskdO3I 5QRSBT/MJanWvJ7tylrvEqtFCJYb9VZsMpscxiot4GDbirdqvsmm2ArflQ894D2CjJqf XdATAsTOsEMMmq06GT0jDJ1C9MYCztghh/capKTccvYcJcoHtRhy4UmdTl7GUsblwRYm NnCEUyS+8i6ozvjD380Wke39KNyXUs14HB2/iXbngP6IcEo6QaGpB33D2GcsVIXgx6wl dlhevHqjO8CcPqo6R+KwbdSG79xYWpXYADNv35I0JXli2cHDSH0yMDPEHc8ikpGfoXX2 fyjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ZwBT4g14vYXymE6X+0Ue3RPk57uheIJlCkFHpAJ6pRk=; b=dRDKUr75KMQf+fza/fIFVLIpywRWe0rwGoVTsQR8uHZwMyGsrbSFbXIW70iwbKWC4+ ueQjUFKzq1IA24pklerTHydZ4xBLv5p5AFdF5Rh2z0Dmoia2K/Ms5pKtMHdbgEtqVKzi A1GVez8/KsUUD3QJ07rsVN5W/OswvMEmHYSUqFPnJ7QP+EqER/qWNa3GXOeNhZPbN5PW LBniiLQs8tkxr/EqICZXhq5XAoOqWJhYkeLhZBAl29NPmnGef0UQ2Jdmnu5cY64ZQ1uB QJVLVQClkKpHnE74af68xlfH+4FbZnlKqt8ZrvwU2fjf2O3NbRFHsGC9CvhlDtLAPIjL h1pw== X-Gm-Message-State: AOAM5320hkHNRqDOmMixBYNq0pNJTtqfTCZ4oyZmy/2euRceCRRlLZP/ TWxKFzix/Gq9bWYXwTr+f3OmMQ== X-Received: by 2002:a05:6a00:1411:b0:4fd:e594:fac0 with SMTP id l17-20020a056a00141100b004fde594fac0mr33594230pfu.79.1653483850683; Wed, 25 May 2022 06:04:10 -0700 (PDT) Received: from localhost ([2408:8207:18da:2310:c40f:7b5:4fa8:df3f]) by smtp.gmail.com with ESMTPSA id w198-20020a6282cf000000b005184c9c46dbsm11338684pfd.81.2022.05.25.06.04.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 May 2022 06:04:10 -0700 (PDT) Date: Wed, 25 May 2022 21:03:59 +0800 From: Muchun Song To: Johannes Weiner Cc: mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, longman@redhat.com Subject: Re: [PATCH v4 03/11] mm: memcontrol: make lruvec lock safe when LRU pages are reparented Message-ID: References: <20220524060551.80037-1-songmuchun@bytedance.com> <20220524060551.80037-4-songmuchun@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 25, 2022 at 08:30:15AM -0400, Johannes Weiner wrote: > On Wed, May 25, 2022 at 05:53:30PM +0800, Muchun Song wrote: > > On Tue, May 24, 2022 at 03:27:20PM -0400, Johannes Weiner wrote: > > > On Tue, May 24, 2022 at 02:05:43PM +0800, Muchun Song wrote: > > > > The diagram below shows how to make the folio lruvec lock safe when LRU > > > > pages are reparented. > > > > > > > > folio_lruvec_lock(folio) > > > > retry: > > > > lruvec = folio_lruvec(folio); > > > > > > > > // The folio is reparented at this time. > > > > spin_lock(&lruvec->lru_lock); > > > > > > > > if (unlikely(lruvec_memcg(lruvec) != folio_memcg(folio))) > > > > // Acquired the wrong lruvec lock and need to retry. > > > > // Because this folio is on the parent memcg lruvec list. > > > > goto retry; > > > > > > > > // If we reach here, it means that folio_memcg(folio) is stable. > > > > > > > > memcg_reparent_objcgs(memcg) > > > > // lruvec belongs to memcg and lruvec_parent belongs to parent memcg. > > > > spin_lock(&lruvec->lru_lock); > > > > spin_lock(&lruvec_parent->lru_lock); > > > > > > > > // Move all the pages from the lruvec list to the parent lruvec list. > > > > > > > > spin_unlock(&lruvec_parent->lru_lock); > > > > spin_unlock(&lruvec->lru_lock); > > > > > > > > After we acquire the lruvec lock, we need to check whether the folio is > > > > reparented. If so, we need to reacquire the new lruvec lock. On the > > > > routine of the LRU pages reparenting, we will also acquire the lruvec > > > > lock (will be implemented in the later patch). So folio_memcg() cannot > > > > be changed when we hold the lruvec lock. > > > > > > > > Since lruvec_memcg(lruvec) is always equal to folio_memcg(folio) after > > > > we hold the lruvec lock, lruvec_memcg_debug() check is pointless. So > > > > remove it. > > > > > > > > This is a preparation for reparenting the LRU pages. > > > > > > > > Signed-off-by: Muchun Song > > > > > > This looks good to me. Just one question: > > > > > > > @@ -1230,10 +1213,23 @@ void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) > > > > */ > > > > struct lruvec *folio_lruvec_lock(struct folio *folio) > > > > { > > > > - struct lruvec *lruvec = folio_lruvec(folio); > > > > + struct lruvec *lruvec; > > > > > > > > + rcu_read_lock(); > > > > +retry: > > > > + lruvec = folio_lruvec(folio); > > > > spin_lock(&lruvec->lru_lock); > > > > - lruvec_memcg_debug(lruvec, folio); > > > > + > > > > + if (unlikely(lruvec_memcg(lruvec) != folio_memcg(folio))) { > > > > + spin_unlock(&lruvec->lru_lock); > > > > + goto retry; > > > > + } > > > > + > > > > + /* > > > > + * Preemption is disabled in the internal of spin_lock, which can serve > > > > + * as RCU read-side critical sections. > > > > + */ > > > > + rcu_read_unlock(); > > > > > > The code looks right to me, but I don't understand the comment: why do > > > we care that the rcu read-side continues? With the lru_lock held, > > > reparenting is on hold and the lruvec cannot be rcu-freed anyway, no? > > > > > > > Right. We could hold rcu read lock until end of reparting. So you mean > > we do rcu_read_unlock in folio_lruvec_lock()? > > The comment seems to suggest that disabling preemption is what keeps > the lruvec alive. But it's the lru_lock that keeps it alive. The > cgroup destruction path tries to take the lru_lock long before it even > gets to synchronize_rcu(). Once you hold the lru_lock, having an > implied read-side critical section as well doesn't seem to matter. > Well, I thought that spinlocks have implicit read-side critical sections because it disables preemption (I learned from the comments above synchronize_rcu() that says interrupts, preemption, or softirqs have been disabled also serve as RCU read-side critical sections). So I have a question: is it still true in a PREEMPT_RT kernel (I am not familiar with this)? > Should the comment be deleted? > I think we could remove the comments. If the above question is false, seems like we should continue holding rcu read lock. Thanks.