Re: [PATCH-next v2] mm/memcg: Properly handle memcg_stock access for PREEMPT_RT

From: Sebastian Andrzej Siewior
Date: Fri Dec 10 2021 - 08:01:38 EST


On 2021-12-09 21:52:28 [-0500], Waiman Long wrote:

> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c

> @@ -2210,7 +2211,7 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
> struct memcg_stock_pcp *stock;
> unsigned long flags;
>
> - local_irq_save(flags);
> + local_lock_irqsave(&memcg_stock.lock, flags);

Why is this one using the lock? It isn't accessing irq_obj, right?

> stock = this_cpu_ptr(&memcg_stock);
> if (stock->cached != memcg) { /* reset if necessary */
> @@ -2779,29 +2780,28 @@ static struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg)
> * which is cheap in non-preempt kernel. The interrupt context object stock
> * can only be accessed after disabling interrupt. User context code can
> * access interrupt object stock, but not vice versa.
> + *
> + * This task and interrupt context optimization is disabled for PREEMPT_RT
> + * as there is no performance gain in this case.
> */
> static inline struct obj_stock *get_obj_stock(unsigned long *pflags)
> {
> - struct memcg_stock_pcp *stock;
> -
> - if (likely(in_task())) {
> + if (likely(in_task()) && !IS_ENABLED(CONFIG_PREEMPT_RT)) {
> *pflags = 0UL;
> preempt_disable();
> - stock = this_cpu_ptr(&memcg_stock);
> - return &stock->task_obj;
> + return this_cpu_ptr(&memcg_stock.task_obj);
> }

We usually add the local_lock_t to the object it protects, struct
obj_stock it this case.
That would give you two different locks (instead of one) so you wouldn't
have to use preempt_disable() to avoid lockdep's complains. Also it
would warn you if you happen to use that obj_stock in !in_task() which
is isn't possible now.
The only downside would be that drain_local_stock() needs to acquire two
locks.

>
> - local_irq_save(*pflags);
> - stock = this_cpu_ptr(&memcg_stock);
> - return &stock->irq_obj;
> + local_lock_irqsave(&memcg_stock.lock, *pflags);
> + return this_cpu_ptr(&memcg_stock.irq_obj);
> }
>
> static inline void put_obj_stock(unsigned long flags)
> {
> - if (likely(in_task()))
> + if (likely(in_task()) && !IS_ENABLED(CONFIG_PREEMPT_RT))
> preempt_enable();
> else
> - local_irq_restore(flags);
> + local_unlock_irqrestore(&memcg_stock.lock, flags);
> }
>
> /*

Sebastian