Subject: Re: [PATCH v17 44/47] dept: introduce APIs to set page usage and use subclasses_evt for the usage

From: Byungchul Park <[email-masked]>
Date: Wed, 19 Nov 2025 19:53:12 +0900

On Thu, Oct 02, 2025 at 05:12:44PM +0900, Byungchul Park wrote:
> False positive reports have been observed since dept works with the
> assumption that all the pages have the same dept class, but the class
> should be split since the problematic call paths are different depending
> on what the page is used for.
> 
> At least, ones in block device's address_space and ones in regular
> file's address_space have exclusively different usages.
> 
> Thus, define usage candidates like:
> 
>    DEPT_PAGE_REGFILE_CACHE /* page in regular file's address_space */
>    DEPT_PAGE_BDEV_CACHE    /* page in block device's address_space */
>    DEPT_PAGE_DEFAULT       /* the others */

1. I'd like to annotate a page to DEPT_PAGE_REGFILE_CACHE when the page
   starts to be associated with a page cache for fs data.

2. And I'd like to annotate a page to DEPT_PAGE_BDEV_CACHE when the page
   starts to be associated with meta data of fs e.g. super block.

3. Lastly, I'd like to reset the annotated value if any, that has been
   set in the page, when the page ends the assoication with either page
   cache or meta block of fs e.g. freeing the page.

Can anyone suggest good places in code for the annotation 1, 2, 3?  It'd
be totally appreciated. :-)

	Byungchul

> Introduce APIs to set each page's usage properly and make sure not to
> interact between at least between DEPT_PAGE_REGFILE_CACHE and
> DEPT_PAGE_BDEV_CACHE.  However, besides the exclusive usages, allow any
> other combinations to interact to the other for example:
> 
>    PG_locked for DEPT_PAGE_DEFAULT page can wait for PG_locked for
>    DEPT_PAGE_REGFILE_CACHE page and vice versa.
> 
>    PG_locked for DEPT_PAGE_DEFAULT page can wait for PG_locked for
>    DEPT_PAGE_BDEV_CACHE page and vice versa.
> 
>    PG_locked for DEPT_PAGE_DEFAULT page can wait for PG_locked for
>    DEPT_PAGE_DEFAULT page.
> 
> Signed-off-by: Byungchul Park <[email-masked]>
> ---
>  include/linux/dept.h       | 31 +++++++++++++++-
>  include/linux/mm_types.h   |  1 +
>  include/linux/page-flags.h | 76 +++++++++++++++++++++++++++++++++++++-
>  3 files changed, 104 insertions(+), 4 deletions(-)
> 
> diff --git a/include/linux/dept.h b/include/linux/dept.h
> index 0ac13129f308..fbbc41048fac 100644
> --- a/include/linux/dept.h
> +++ b/include/linux/dept.h
> @@ -21,8 +21,8 @@ struct task_struct;
>  #define DEPT_MAX_WAIT_HIST		64
>  #define DEPT_MAX_ECXT_HELD		48
>  
> -#define DEPT_MAX_SUBCLASSES		16
> -#define DEPT_MAX_SUBCLASSES_EVT		2
> +#define DEPT_MAX_SUBCLASSES		24
> +#define DEPT_MAX_SUBCLASSES_EVT		3
>  #define DEPT_MAX_SUBCLASSES_USR		(DEPT_MAX_SUBCLASSES / DEPT_MAX_SUBCLASSES_EVT)
>  #define DEPT_MAX_SUBCLASSES_CACHE	2
>  
> @@ -390,6 +390,32 @@ struct dept_ext_wgen {
>  	unsigned int wgen;
>  };
>  
> +enum {
> +	DEPT_PAGE_DEFAULT = 0,
> +	DEPT_PAGE_REGFILE_CACHE,	/* regular file page cache */
> +	DEPT_PAGE_BDEV_CACHE,		/* block device cache */
> +	DEPT_PAGE_USAGE_NR,		/* nr of usages options */
> +};
> +
> +#define DEPT_PAGE_USAGE_SHIFT 16
> +#define DEPT_PAGE_USAGE_MASK ((1U << DEPT_PAGE_USAGE_SHIFT) - 1)
> +#define DEPT_PAGE_USAGE_PENDING_MASK (DEPT_PAGE_USAGE_MASK << DEPT_PAGE_USAGE_SHIFT)
> +
> +/*
> + * Identify each page's usage type
> + */
> +struct dept_page_usage {
> +	/*
> +	 * low 16 bits  : the current usage type
> +	 * high 16 bits : usage type requested to be set
> +	 *
> +	 * Do not apply the type requested immediately but defer until
> +	 * after clearing PG_locked bit of the folio or page e.g. by
> +	 * folio_unlock().
> +	 */
> +	atomic_t type; /* Update and read atomically */
> +};
> +
>  struct dept_event_site {
>  	/*
>  	 * event site name
> @@ -562,6 +588,7 @@ extern void dept_hardirqs_off(void);
>  struct dept_key { };
>  struct dept_map { };
>  struct dept_ext_wgen { };
> +struct dept_page_usage { };
>  struct dept_event_site { };
>  
>  #define DEPT_MAP_INITIALIZER(n, k) { }
> diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
> index 5ebc565309af..8ccbb030500c 100644
> --- a/include/linux/mm_types.h
> +++ b/include/linux/mm_types.h
> @@ -224,6 +224,7 @@ struct page {
>  	struct page *kmsan_shadow;
>  	struct page *kmsan_origin;
>  #endif
> +	struct dept_page_usage usage;
>  	struct dept_ext_wgen pg_locked_wgen;
>  } _struct_page_alignment;
>  
> diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
> index d3c4954c4218..3fd3660ddc6f 100644
> --- a/include/linux/page-flags.h
> +++ b/include/linux/page-flags.h
> @@ -204,6 +204,68 @@ enum pageflags {
>  
>  extern struct dept_map pg_locked_map;
>  
> +static inline int dept_set_page_usage(struct page *p,
> +		unsigned int new_type)
> +{
> +	unsigned int type = atomic_read(&p->usage.type);
> +
> +	if (WARN_ON_ONCE(new_type >= DEPT_PAGE_USAGE_NR))
> +		return -1;
> +
> +	new_type <<= DEPT_PAGE_USAGE_SHIFT;
> +retry:
> +	new_type &= ~DEPT_PAGE_USAGE_MASK;
> +	new_type |= type & DEPT_PAGE_USAGE_MASK;
> +
> +	if (!atomic_try_cmpxchg(&p->usage.type, &type, new_type))
> +		goto retry;
> +
> +	return 0;
> +}
> +
> +static inline int dept_reset_page_usage(struct page *p)
> +{
> +	return dept_set_page_usage(p, DEPT_PAGE_DEFAULT);
> +}
> +
> +static inline void dept_update_page_usage(struct page *p)
> +{
> +	unsigned int type = atomic_read(&p->usage.type);
> +	unsigned int new_type;
> +
> +retry:
> +	new_type = type & DEPT_PAGE_USAGE_PENDING_MASK;
> +	new_type >>= DEPT_PAGE_USAGE_SHIFT;
> +	new_type |= type & DEPT_PAGE_USAGE_PENDING_MASK;
> +
> +	/*
> +	 * Already updated by others.
> +	 */
> +	if (type == new_type)
> +		return;
> +
> +	if (!atomic_try_cmpxchg(&p->usage.type, &type, new_type))
> +		goto retry;
> +}
> +
> +static inline unsigned long dept_event_flags(struct page *p, bool wait)
> +{
> +	unsigned int type;
> +
> +	type = atomic_read(&p->usage.type) & DEPT_PAGE_USAGE_MASK;
> +
> +	if (WARN_ON_ONCE(type >= DEPT_PAGE_USAGE_NR))
> +		return 0;
> +
> +	/*
> +	 * event
> +	 */
> +	if (!wait)
> +		return 1UL << type;
> +
> +	return (1UL << DEPT_PAGE_DEFAULT) | (1UL << type);
> +}
> +
>  /*
>   * Place the following annotations in its suitable point in code:
>   *
> @@ -214,20 +276,28 @@ extern struct dept_map pg_locked_map;
>  
>  static inline void dept_page_set_bit(struct page *p, int bit_nr)
>  {
> +	dept_update_page_usage(p);
>  	if (bit_nr == PG_locked)
>  		dept_request_event(&pg_locked_map, &p->pg_locked_wgen);
>  }
>  
>  static inline void dept_page_clear_bit(struct page *p, int bit_nr)
>  {
> +	unsigned long evt_f;
> +
> +	evt_f = dept_event_flags(p, false);
>  	if (bit_nr == PG_locked)
> -		dept_event(&pg_locked_map, 1UL, _RET_IP_, __func__, &p->pg_locked_wgen);
> +		dept_event(&pg_locked_map, evt_f, _RET_IP_, __func__, &p->pg_locked_wgen);
>  }
>  
>  static inline void dept_page_wait_on_bit(struct page *p, int bit_nr)
>  {
> +	unsigned long evt_f;
> +
> +	dept_update_page_usage(p);
> +	evt_f = dept_event_flags(p, true);
>  	if (bit_nr == PG_locked)
> -		dept_wait(&pg_locked_map, 1UL, _RET_IP_, __func__, 0, -1L);
> +		dept_wait(&pg_locked_map, evt_f, _RET_IP_, __func__, 0, -1L);
>  }
>  
>  static inline void dept_folio_set_bit(struct folio *f, int bit_nr)
> @@ -245,6 +315,8 @@ static inline void dept_folio_wait_on_bit(struct folio *f, int bit_nr)
>  	dept_page_wait_on_bit(&f->page, bit_nr);
>  }
>  #else
> +#define dept_set_page_usage(p, t)		do { } while (0)
> +#define dept_reset_page_usage(p)		do { } while (0)
>  #define dept_page_set_bit(p, bit_nr)		do { } while (0)
>  #define dept_page_clear_bit(p, bit_nr)		do { } while (0)
>  #define dept_page_wait_on_bit(p, bit_nr)	do { } while (0)
> -- 
> 2.17.1