Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp280468ioo; Thu, 26 May 2022 03:37:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyfYp7whc31cAmNlMB4dNLvK7OND6iDvsNwBdfdF7gC0G80tao1k3k0dVKt20VJiY5JZg4 X-Received: by 2002:a62:a516:0:b0:505:722e:15d5 with SMTP id v22-20020a62a516000000b00505722e15d5mr38067277pfm.52.1653561439724; Thu, 26 May 2022 03:37:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653561439; cv=none; d=google.com; s=arc-20160816; b=Bdpy6GHZNB+VAKmsizSr0e+hQMP/dLu6yOIaJoP1fmpH3/DOHWDR5j6qfDwxH+irsC St7x8DsSk30f6MQRVyw5net3//6Y9PvAaGP1FJ/8HT2xJUqLcNiSpt3YKDkq+L911j2/ YE59UffCwzpjGDcO+tq7tjU/xh1kcRl2BblfbMCY2ShnyHHX9q60AFMzfRxyZDW8AW6N yDulhCUvO24hOrIktM0yfb6szuci5FZyQJs6vsTlOYIZ4CTv5dJq4bOVG66WpfC+pNKk snc3d38e4cmAfCWuno30DAQaWvLRq8RG63fRtsIpHdEEVFDSkgZ9c+VfWy2qnpBCY0cm XbEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=v7iWdvSl9xApudroXhJn9FrDy1CICqE653ZASePdCf0=; b=MIahsR8uFm6U0BV3BDslhQdSeA08/4YZOdyUkHPFG883qBXpRMl6MCbrLraSccRIQp EgVy/sphHOgwiMeD6/tbP3YYuFixwqBH8MojpJ6zxsfSK6+z8g9nQKTgD94E46lV7X17 3JaWGKDGT0IRJATMMkctcGy8RQFGqWNBeZ7x0Vq6TibqVXxuJxXdJRaDZVBeMtl+7eOt 6o6E1xD6wt4+A+ogO3ydDElDRDe2OLOMwYkrxJROb2CiGM8sJ77YHVKLvKQPcfqV90hD OOaUhZOwK7yo+v7Ehb8hUo8CoxY/bok2+PXRWW7wH5hkbcG+vhkrf/XbOyM/nhSce1iM B0ZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bg31M50F; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e193-20020a6369ca000000b003c67a501c74si2068715pgc.636.2022.05.26.03.37.08; Thu, 26 May 2022 03:37:19 -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=@redhat.com header.s=mimecast20190719 header.b=bg31M50F; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243849AbiEYPAN (ORCPT + 99 others); Wed, 25 May 2022 11:00:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242616AbiEYPAF (ORCPT ); Wed, 25 May 2022 11:00:05 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C73E96C0FE for ; Wed, 25 May 2022 08:00:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653490802; 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=v7iWdvSl9xApudroXhJn9FrDy1CICqE653ZASePdCf0=; b=bg31M50FeVZblI/tSCinRaTsY4jWXHJBx4IywM1+yDvUb8XUD2pyWT+MduwYqMbhTR3Y3r UF1RicUrw0EudE8Txkfa1e9VR5DS/IC61R9Qb2mh/W7SiM/uunWAYwjdnKPt7JtAhTt8dE /VD2t04+ATjzPYRo52jf773eScxibHA= 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-473-8h6aK_FZN4-QwsgJpwB8PQ-1; Wed, 25 May 2022 10:59:59 -0400 X-MC-Unique: 8h6aK_FZN4-QwsgJpwB8PQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C69DB85A5BC; Wed, 25 May 2022 14:59:58 +0000 (UTC) Received: from [10.22.34.172] (unknown [10.22.34.172]) by smtp.corp.redhat.com (Postfix) with ESMTP id 469ED8287E; Wed, 25 May 2022 14:59:58 +0000 (UTC) Message-ID: <0728780f-08e7-3d09-e859-aeec92a81550@redhat.com> Date: Wed, 25 May 2022 10:59:58 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Subject: Re: [PATCH v4 03/11] mm: memcontrol: make lruvec lock safe when LRU pages are reparented Content-Language: en-US To: Muchun Song Cc: hannes@cmpxchg.org, 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 References: <20220524060551.80037-1-songmuchun@bytedance.com> <20220524060551.80037-4-songmuchun@bytedance.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 5/25/22 06:20, Muchun Song wrote: > On Tue, May 24, 2022 at 03:23:11PM -0400, Waiman Long wrote: >> On 5/24/22 02:05, 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 >>> --- >>> include/linux/memcontrol.h | 18 +++----------- >>> mm/compaction.c | 10 +++++++- >>> mm/memcontrol.c | 62 +++++++++++++++++++++++++++++----------------- >>> mm/swap.c | 4 +++ >>> 4 files changed, 55 insertions(+), 39 deletions(-) >>> >>> diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h >>> index ff1c1dd7e762..4042e4d21fe2 100644 >>> --- a/include/linux/memcontrol.h >>> +++ b/include/linux/memcontrol.h >>> @@ -752,7 +752,9 @@ static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg, >>> * folio_lruvec - return lruvec for isolating/putting an LRU folio >>> * @folio: Pointer to the folio. >>> * >>> - * This function relies on folio->mem_cgroup being stable. >>> + * The lruvec can be changed to its parent lruvec when the page reparented. >>> + * The caller need to recheck if it cares about this changes (just like >>> + * folio_lruvec_lock() does). >>> */ >>> static inline struct lruvec *folio_lruvec(struct folio *folio) >>> { >>> @@ -771,15 +773,6 @@ struct lruvec *folio_lruvec_lock_irq(struct folio *folio); >>> struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, >>> unsigned long *flags); >>> -#ifdef CONFIG_DEBUG_VM >>> -void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio); >>> -#else >>> -static inline >>> -void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) >>> -{ >>> -} >>> -#endif >>> - >>> static inline >>> struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){ >>> return css ? container_of(css, struct mem_cgroup, css) : NULL; >>> @@ -1240,11 +1233,6 @@ static inline struct lruvec *folio_lruvec(struct folio *folio) >>> return &pgdat->__lruvec; >>> } >>> -static inline >>> -void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) >>> -{ >>> -} >>> - >>> static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) >>> { >>> return NULL; >>> diff --git a/mm/compaction.c b/mm/compaction.c >>> index 817098817302..1692b17db781 100644 >>> --- a/mm/compaction.c >>> +++ b/mm/compaction.c >>> @@ -515,6 +515,8 @@ compact_folio_lruvec_lock_irqsave(struct folio *folio, unsigned long *flags, >>> { >>> struct lruvec *lruvec; >>> + rcu_read_lock(); >>> +retry: >>> lruvec = folio_lruvec(folio); >>> /* Track if the lock is contended in async mode */ >>> @@ -527,7 +529,13 @@ compact_folio_lruvec_lock_irqsave(struct folio *folio, unsigned long *flags, >>> spin_lock_irqsave(&lruvec->lru_lock, *flags); >>> out: >>> - lruvec_memcg_debug(lruvec, folio); >>> + if (unlikely(lruvec_memcg(lruvec) != folio_memcg(folio))) { >>> + spin_unlock_irqrestore(&lruvec->lru_lock, *flags); >>> + goto retry; >>> + } >>> + >>> + /* See the comments in folio_lruvec_lock(). */ >>> + rcu_read_unlock(); >>> return lruvec; >>> } >>> diff --git a/mm/memcontrol.c b/mm/memcontrol.c >>> index 6de0d3e53eb1..b38a77f6696f 100644 >>> --- a/mm/memcontrol.c >>> +++ b/mm/memcontrol.c >>> @@ -1199,23 +1199,6 @@ int mem_cgroup_scan_tasks(struct mem_cgroup *memcg, >>> return ret; >>> } >>> -#ifdef CONFIG_DEBUG_VM >>> -void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) >>> -{ >>> - struct mem_cgroup *memcg; >>> - >>> - if (mem_cgroup_disabled()) >>> - return; >>> - >>> - memcg = folio_memcg(folio); >>> - >>> - if (!memcg) >>> - VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != root_mem_cgroup, folio); >>> - else >>> - VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != memcg, folio); >>> -} >>> -#endif >>> - >>> /** >>> * folio_lruvec_lock - Lock the lruvec for a folio. >>> * @folio: Pointer to the folio. >>> @@ -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. >>> + */ >> What is the point of this comment as preemption is not disabled for >> PREEMPT_RT kernel? >> > I'm not familar with PREEMPT_RT kernel. At least you are right, > preemption is not disabled in this case, I think I should drop > this assumption. Preemption is not disabled for PREEMPT_RT kernel but task migration to another cpu is disabled. So access to per-cpu variables are safe. RCU seems to have a special mode for PREEMPT_RT kernel but I am not familiar with the detail. Cheers, Longman