Linux Trace Kernel
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Xuewen Wang <wangxuewen@kylinos.cn>
Cc: hughd@google.com, baolin.wang@linux.alibaba.com,
	mhiramat@kernel.org, mathieu.desnoyers@efficios.com,
	corbet@lwn.net, skhan@linuxfoundation.org, rdunlap@infradead.org,
	akpm@linux-foundation.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	linux-doc@vger.kernel.org
Subject: Re: [PATCH 2/2] mm/shmem: trace folio allocation failures and fallbacks
Date: Thu, 29 Oct 2026 10:38:34 -0400	[thread overview]
Message-ID: <20261029103834.4bc0590e@gandalf.local.home> (raw)
In-Reply-To: <20261027094014.2432692-3-wangxuewen@kylinos.cn>

On Tue, 27 Oct 2026 17:40:14 +0800
Xuewen Wang <wangxuewen@kylinos.cn> wrote:

> index 7a0050a1686f..72b20d6b3caf 100644
> --- a/include/trace/events/shmem.h
> +++ b/include/trace/events/shmem.h
> @@ -80,6 +80,61 @@ TRACE_EVENT(shmem_swapin_folio,
>  		  __entry->fallback_error, __entry->error)
>  );
>  
> +#define SHMEM_ALLOC_ALLOC	0
> +#define SHMEM_ALLOC_CHARGE	1
> +#define SHMEM_ALLOC_CACHE		2
> +#define SHMEM_ALLOC_ACCOUNT	3
> +#define SHMEM_ALLOC_DONE		4
> +#define SHMEM_ALLOC_FILTER	5
> +
> +TRACE_EVENT(shmem_alloc_folio_finish,
> +
> +	TP_PROTO(struct inode *inode, pgoff_t index,
> +		 int requested_order, int suitable_order, int actual_order,
> +		 unsigned int stage, int error, int ret),
> +
> +	TP_ARGS(inode, index, requested_order, suitable_order, actual_order,
> +		stage, error, ret),
> +
> +	TP_STRUCT__entry(
> +		__field(dev_t, dev)
> +		__field(unsigned long, ino)
> +		__field(unsigned long, index)

Same here. Move dev to after index.

-- Steve


> +		__field(int, requested_order)
> +		__field(int, suitable_order)
> +		__field(int, actual_order)
> +		__field(unsigned int, stage)
> +		__field(int, error)
> +		__field(int, ret)
> +	),
> +
> +	TP_fast_assign(
> +		__entry->dev = inode->i_sb->s_dev;
> +		__entry->ino = inode->i_ino;
> +		__entry->index = index;
> +		__entry->requested_order = requested_order;
> +		__entry->suitable_order = suitable_order;
> +		__entry->actual_order = actual_order;
> +		__entry->stage = stage;
> +		__entry->error = error;
> +		__entry->ret = ret;
> +	),
> +
> +	TP_printk("dev=%d:%d ino=%lu index=%lu requested_order=%d suitable_order=%d actual_order=%d stage=%s error=%d ret=%d",
> +		  MAJOR(__entry->dev), MINOR(__entry->dev),
> +		  __entry->ino, __entry->index,
> +		  __entry->requested_order, __entry->suitable_order,
> +		  __entry->actual_order,
> +		  __print_symbolic(__entry->stage,
> +				   { SHMEM_ALLOC_ALLOC, "alloc" },
> +				   { SHMEM_ALLOC_CHARGE, "charge" },
> +				   { SHMEM_ALLOC_CACHE, "cache" },
> +				   { SHMEM_ALLOC_ACCOUNT, "account" },
> +				   { SHMEM_ALLOC_DONE, "done" },
> +				   { SHMEM_ALLOC_FILTER, "filter" }),
> +		  __entry->error, __entry->ret)
> +);
> +
>  #endif /* _TRACE_SHMEM_H */
>  
>  /* This part must be outside protection */
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 3b910aec5bcf..68321d61dfdb 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -2155,17 +2155,23 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf,
>  	unsigned long suitable_orders = 0;
>  	struct folio *folio = NULL;
>  	pgoff_t aligned_index;
> +	pgoff_t orig_index = index;
>  	long pages;
>  	int error, order;
> +	int requested_order, suitable_order;
> +	int charge_error = 0;
> +	unsigned int stage;
>  
>  	if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
>  		orders = 0;
>  
> +	requested_order = orders ? highest_order(orders) : 0;
>  	if (orders > 0) {
>  		suitable_orders = shmem_suitable_orders(inode, vmf,
>  							mapping, index, orders);
>  
>  		order = highest_order(suitable_orders);
> +		suitable_order = order;
>  		while (suitable_orders) {
>  			pages = 1UL << order;
>  			aligned_index = round_down(index, pages);
> @@ -2181,19 +2187,35 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf,
>  			order = next_order(&suitable_orders, order);
>  		}
>  	} else {
> +		suitable_order = 0;
>  		pages = 1;
>  		folio = shmem_alloc_folio(gfp, 0, info, index);
>  	}
> -	if (!folio)
> +	if (!folio) {
> +		if (suitable_order < 0)
> +			trace_shmem_alloc_folio_finish(inode, orig_index,
> +						       requested_order,
> +						       suitable_order, -1,
> +						       SHMEM_ALLOC_FILTER, 0,
> +						       -ENOMEM);
> +		else
> +			trace_shmem_alloc_folio_finish(inode, orig_index,
> +						       requested_order,
> +						       suitable_order, -1,
> +						       SHMEM_ALLOC_ALLOC, -ENOMEM,
> +						       -ENOMEM);
>  		return ERR_PTR(-ENOMEM);
> +	}
>  
>  allocated:
>  	__folio_set_locked(folio);
>  	__folio_set_swapbacked(folio);
>  
>  	gfp &= GFP_RECLAIM_MASK;
> +	stage = SHMEM_ALLOC_CHARGE;
>  	error = mem_cgroup_charge(folio, fault_mm, gfp);
>  	if (error) {
> +		charge_error = error;
>  		if (xa_find(&mapping->i_pages, &index,
>  				index + pages - 1, XA_PRESENT)) {
>  			error = -EEXIST;
> @@ -2208,10 +2230,12 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf,
>  		goto unlock;
>  	}
>  
> +	stage = SHMEM_ALLOC_CACHE;
>  	error = shmem_add_to_page_cache(folio, mapping, index, NULL, gfp);
>  	if (error)
>  		goto unlock;
>  
> +	stage = SHMEM_ALLOC_ACCOUNT;
>  	error = shmem_inode_acct_blocks(inode, pages);
>  	if (error) {
>  		struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
> @@ -2242,9 +2266,17 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf,
>  
>  	shmem_recalc_inode(inode, pages, 0);
>  	folio_add_lru(folio);
> +	if (folio_order(folio) < requested_order)
> +		trace_shmem_alloc_folio_finish(inode, orig_index,
> +					       requested_order, suitable_order,
> +					       folio_order(folio),
> +					       SHMEM_ALLOC_DONE, 0, 0);
>  	return folio;
>  
>  unlock:
> +	trace_shmem_alloc_folio_finish(inode, orig_index, requested_order,
> +				       suitable_order, folio_order(folio), stage,
> +				       charge_error ?: error, error);
>  	folio_unlock(folio);
>  	folio_put(folio);
>  	return ERR_PTR(error);


  reply	other threads:[~2026-10-29 14:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27  9:40 [PATCH 0/2] mm/shmem: trace swapin results and allocation fallbacks Xuewen Wang
2026-10-27  9:40 ` [PATCH 1/2] mm/shmem: trace swapin results and order fallbacks Xuewen Wang
2026-10-27 23:55   ` Randy Dunlap
2026-10-28  3:27     ` wangxuewen
2026-10-29 14:36   ` Steven Rostedt
2026-10-27  9:40 ` [PATCH 2/2] mm/shmem: trace folio allocation failures and fallbacks Xuewen Wang
2026-10-29 14:38   ` Steven Rostedt [this message]
2026-10-28  7:56 ` [PATCH 0/2] mm/shmem: trace swapin results and allocation fallbacks Baolin Wang

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=20261029103834.4bc0590e@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=corbet@lwn.net \
    --cc=hughd@google.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=skhan@linuxfoundation.org \
    --cc=wangxuewen@kylinos.cn \
    /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