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

Byungchul Park
2025-10-02T08:14:08Z

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 */

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 <byungchul@sk.com>
---
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