lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <YHXaJaMUtXF/Y2pD@carbon.dhcp.thefacebook.com>
Date:   Tue, 13 Apr 2021 10:51:33 -0700
From:   Roman Gushchin <guro@...com>
To:     Muchun Song <songmuchun@...edance.com>
CC:     <hannes@...xchg.org>, <mhocko@...nel.org>,
        <akpm@...ux-foundation.org>, <shakeelb@...gle.com>,
        <vdavydov.dev@...il.com>, <linux-kernel@...r.kernel.org>,
        <linux-mm@...ck.org>, <duanxiongchun@...edance.com>,
        <fam.zheng@...edance.com>
Subject: Re: [PATCH 5/7] mm: memcontrol: simplify the logic of objcg pinning
 memcg

On Tue, Apr 13, 2021 at 02:51:51PM +0800, Muchun Song wrote:
> The obj_cgroup_release() and memcg_reparent_objcgs() are serialized by
> the css_set_lock. We do not need to care about objcg->memcg being
> released in the process of obj_cgroup_release(). So there is no need
> to pin memcg before releasing objcg. Remove those pinning logic to
> simplfy the code.
> 
> There are only two places that modifies the objcg->memcg. One is the
> initialization to objcg->memcg in the memcg_online_kmem(), another
> is objcgs reparenting in the memcg_reparent_objcgs(). It is also
> impossible for the two to run in parallel. So xchg() is unnecessary
> and it is enough to use WRITE_ONCE().
> 
> Signed-off-by: Muchun Song <songmuchun@...edance.com>
> Acked-by: Johannes Weiner <hannes@...xchg.org>

It's a good one! It took me some time to realize that it's safe.
Thanks!

Acked-by: Roman Gushchin <guro@...com>

> ---
>  mm/memcontrol.c | 20 ++++++--------------
>  1 file changed, 6 insertions(+), 14 deletions(-)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 1f807448233e..42d8c0f4ab1d 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -261,7 +261,6 @@ static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
>  static void obj_cgroup_release(struct percpu_ref *ref)
>  {
>  	struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt);
> -	struct mem_cgroup *memcg;
>  	unsigned int nr_bytes;
>  	unsigned int nr_pages;
>  	unsigned long flags;
> @@ -291,11 +290,9 @@ static void obj_cgroup_release(struct percpu_ref *ref)
>  	nr_pages = nr_bytes >> PAGE_SHIFT;
>  
>  	spin_lock_irqsave(&css_set_lock, flags);
> -	memcg = obj_cgroup_memcg(objcg);
>  	if (nr_pages)
>  		obj_cgroup_uncharge_pages(objcg, nr_pages);
>  	list_del(&objcg->list);
> -	mem_cgroup_put(memcg);
>  	spin_unlock_irqrestore(&css_set_lock, flags);
>  
>  	percpu_ref_exit(ref);
> @@ -330,17 +327,12 @@ static void memcg_reparent_objcgs(struct mem_cgroup *memcg,
>  
>  	spin_lock_irq(&css_set_lock);
>  
> -	/* Move active objcg to the parent's list */
> -	xchg(&objcg->memcg, parent);
> -	css_get(&parent->css);
> -	list_add(&objcg->list, &parent->objcg_list);
> -
> -	/* Move already reparented objcgs to the parent's list */
> -	list_for_each_entry(iter, &memcg->objcg_list, list) {
> -		css_get(&parent->css);
> -		xchg(&iter->memcg, parent);
> -		css_put(&memcg->css);
> -	}
> +	/* 1) Ready to reparent active objcg. */
> +	list_add(&objcg->list, &memcg->objcg_list);
> +	/* 2) Reparent active objcg and already reparented objcgs to parent. */
> +	list_for_each_entry(iter, &memcg->objcg_list, list)
> +		WRITE_ONCE(iter->memcg, parent);
> +	/* 3) Move already reparented objcgs to the parent's list */
>  	list_splice(&memcg->objcg_list, &parent->objcg_list);
>  
>  	spin_unlock_irq(&css_set_lock);
> -- 
> 2.11.0
> 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ