linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Michal Hocko <mhocko@suse.com>
To: Zhiguo Jiang <justinjiang@vivo.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Uladzislau Rezki <urezki@gmail.com>,
	Christoph Hellwig <hch@infradead.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	opensource.kernel@vivo.com
Subject: Re: [PATCH] mm: fix vmalloc memcg accounting issue
Date: Fri, 30 Aug 2024 13:58:19 +0200	[thread overview]
Message-ID: <ZtGz28j4T5tTUEUQ@tiehlicka> (raw)
In-Reply-To: <20240830113956.1355-1-justinjiang@vivo.com>

On Fri 30-08-24 19:39:56, Zhiguo Jiang wrote:
> The oepration of adding 1 for the MEMCG_VMALLOC count value has a
> judgment "if (gfp_mask & __GFP_ACCOUNT)" in vmalloc(), but subtracting
> 1 does not have this judgment in vfree(), which leads to the non-aligned
> count value operation. This patch fixes this issue.

Are you really observing this or have you just concluded this from
reading the code?

AFAIR mod_memcg_page_state will not account if page doesn't have memcg
associated with it which means it is not charged.

> Signed-off-by: Zhiguo Jiang <justinjiang@vivo.com>
> ---
>  include/linux/vmalloc.h |  1 +
>  mm/vmalloc.c            | 13 ++++++++++---
>  2 files changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
> index ad2ce7a6ab7a..0253feec371f
> --- a/include/linux/vmalloc.h
> +++ b/include/linux/vmalloc.h
> @@ -38,6 +38,7 @@ struct iov_iter;		/* in uio.h */
>  #define VM_DEFER_KMEMLEAK	0
>  #endif
>  #define VM_SPARSE		0x00001000	/* sparse vm_area. not all pages are present. */
> +#define VM_MEMCG_ACCOUNT	0x00002000	/* mark vm pages alloced with __GFP_ACCOUNT for memcg accounting. */
>  
>  /* bits [20..32] reserved for arch specific ioremap internals */
>  
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index d977c280b1c4..aff1bf7a8798
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -3125,6 +3125,12 @@ static struct vm_struct *__get_vm_area_node(unsigned long size,
>  		size += PAGE_SIZE;
>  
>  	area->flags = flags;
> +	/*
> +	 * Set memcg accounting flag in vm_struct, used for
> +	 * vfree() align vmalloc here.
> +	 */
> +	if (gfp_mask & __GFP_ACCOUNT)
> +		area->flags |= VM_MEMCG_ACCOUNT;
>  	area->caller = caller;
>  
>  	va = alloc_vmap_area(size, align, start, end, node, gfp_mask, 0, area);
> @@ -3367,7 +3373,9 @@ void vfree(const void *addr)
>  		struct page *page = vm->pages[i];
>  
>  		BUG_ON(!page);
> -		mod_memcg_page_state(page, MEMCG_VMALLOC, -1);
> +
> +		if (vm->flags & VM_MEMCG_ACCOUNT)
> +			mod_memcg_page_state(page, MEMCG_VMALLOC, -1);
>  		/*
>  		 * High-order allocs for huge vmallocs are split, so
>  		 * can be freed as an array of order-0 allocations
> @@ -3662,7 +3670,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
>  		node, page_order, nr_small_pages, area->pages);
>  
>  	atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
> -	if (gfp_mask & __GFP_ACCOUNT) {
> +	if (area->flags & VM_MEMCG_ACCOUNT) {
>  		int i;
>  
>  		for (i = 0; i < area->nr_pages; i++)
> @@ -3813,7 +3821,6 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,
>  		}
>  		goto fail;
>  	}
> -
>  	/*
>  	 * Prepare arguments for __vmalloc_area_node() and
>  	 * kasan_unpoison_vmalloc().
> -- 
> 2.39.0
> 

-- 
Michal Hocko
SUSE Labs


      reply	other threads:[~2024-08-30 11:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-30 11:39 Zhiguo Jiang
2024-08-30 11:58 ` Michal Hocko [this message]

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=ZtGz28j4T5tTUEUQ@tiehlicka \
    --to=mhocko@suse.com \
    --cc=akpm@linux-foundation.org \
    --cc=hch@infradead.org \
    --cc=justinjiang@vivo.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=opensource.kernel@vivo.com \
    --cc=urezki@gmail.com \
    /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