linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Weiner <hannes@cmpxchg.org>
To: Roman Gushchin <roman.gushchin@linux.dev>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	cgroups@vger.kernel.org, Michal Hocko <mhocko@kernel.org>,
	Shakeel Butt <shakeelb@google.com>,
	Muchun Song <muchun.song@linux.dev>,
	Dennis Zhou <dennis@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	David Rientjes <rientjes@google.com>,
	Vlastimil Babka <vbabka@suse.cz>
Subject: Re: [PATCH v1 2/5] mm: kmem: add direct objcg pointer to task_struct
Date: Tue, 3 Oct 2023 12:59:19 -0400	[thread overview]
Message-ID: <20231003165919.GB20979@cmpxchg.org> (raw)
In-Reply-To: <20230929180056.1122002-3-roman.gushchin@linux.dev>

On Fri, Sep 29, 2023 at 11:00:52AM -0700, Roman Gushchin wrote:
> @@ -553,6 +553,16 @@ static inline bool folio_memcg_kmem(struct folio *folio)
>  	return folio->memcg_data & MEMCG_DATA_KMEM;
>  }
>  
> +static inline bool current_objcg_needs_update(struct obj_cgroup *objcg)
> +{
> +	return (struct obj_cgroup *)((unsigned long)objcg & 0x1);
> +}
> +
> +static inline struct obj_cgroup *
> +current_objcg_without_update_flag(struct obj_cgroup *objcg)
> +{
> +	return (struct obj_cgroup *)((unsigned long)objcg & ~0x1);
> +}

I would slightly prefer naming the bit with a define, and open-coding
the bitops in the current callsites. This makes it clearer that the
actual pointer bits are overloaded in the places where the pointer is
accessed.

> @@ -3001,6 +3001,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg)
>  	return objcg;
>  }
>  
> +static struct obj_cgroup *current_objcg_update(struct obj_cgroup *old)
> +{
> +	struct mem_cgroup *memcg;
> +	struct obj_cgroup *objcg = NULL, *tmp = old;
> +
> +	old = current_objcg_without_update_flag(old);
> +	if (old)
> +		obj_cgroup_put(old);
> +
> +	rcu_read_lock();
> +	do {
> +		/* Atomically drop the update bit, */
> +		WARN_ON_ONCE(cmpxchg(&current->objcg, tmp, 0) != tmp);
> +
> +		/* ...obtain the new objcg pointer */
> +		memcg = mem_cgroup_from_task(current);
> +		for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) {
> +			objcg = rcu_dereference(memcg->objcg);
> +			if (objcg && obj_cgroup_tryget(objcg))
> +				break;
> +			objcg = NULL;
> +		}

As per the other thread, it would be great to have a comment here
explaining the scenario(s) when the tryget could fail and we'd have to
defer to an ancestor.

> +
> +		/*
> +		 * ...and try atomically set up a new objcg pointer. If it
> +		 * fails, it means the update flag was set concurrently, so
> +		 * the whole procedure should be repeated.
> +		 */
> +		tmp = 0;
> +	} while (!try_cmpxchg(&current->objcg, &tmp, objcg));
> +	rcu_read_unlock();
> +
> +	return objcg;

Overall this looks great to me.

AFAICS the rcu_read_lock() is needed for the mem_cgroup_from_task()
and tryget(). Is it possible to localize it around these operations?
Or am I missing some other effect it has?

> @@ -6358,8 +6407,27 @@ static void mem_cgroup_move_task(void)
>  }
>  #endif
>  
> +#ifdef CONFIG_MEMCG_KMEM
> +static void mem_cgroup_fork(struct task_struct *task)
> +{
> +	/*
> +	 * Set the update flag to cause task->objcg to be initialized lazily
> +	 * on the first allocation.
> +	 */
> +	task->objcg = (struct obj_cgroup *)0x1;
> +}

I like this open-coding!

Should this mention why it doesn't need to be atomic? Task is in
fork(), no concurrent modifications from allocations or migrations
possible...

None of the feedback is a blocker, though.

Acked-by: Johannes Weiner <hannes@cmpxchg.org>


  reply	other threads:[~2023-10-03 16:59 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-29 18:00 [PATCH v1 0/5] mm: improve performance of accounted kernel memory allocations Roman Gushchin
2023-09-29 18:00 ` [PATCH v1 1/5] mm: kmem: optimize get_obj_cgroup_from_current() Roman Gushchin
2023-10-03 16:48   ` Johannes Weiner
2023-09-29 18:00 ` [PATCH v1 2/5] mm: kmem: add direct objcg pointer to task_struct Roman Gushchin
2023-10-03 16:59   ` Johannes Weiner [this message]
2023-09-29 18:00 ` [PATCH v1 3/5] mm: kmem: make memcg keep a reference to the original objcg Roman Gushchin
2023-09-29 18:00 ` [PATCH v1 4/5] mm: kmem: scoped objcg protection Roman Gushchin
2023-09-29 18:00 ` [PATCH v1 5/5] percpu: " Roman Gushchin
2023-10-04 18:32 ` [PATCH v1 0/5] mm: improve performance of accounted kernel memory allocations Michal Koutný
2023-10-04 19:02   ` Roman Gushchin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231003165919.GB20979@cmpxchg.org \
    --to=hannes@cmpxchg.org \
    --cc=akpm@linux-foundation.org \
    --cc=cgroups@vger.kernel.org \
    --cc=dennis@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeelb@google.com \
    --cc=vbabka@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox