[RFC PATCH v12 10/26] mm: introduce APIs to check if the page allocation is tlb shootdownable

Byungchul Park
2025-02-20T05:36:03Z

Functionally, no change. This is a preparation for luf mechanism that
should indentify if tlb shootdown can be performed on page allocation.

In a context with irq disabled or non-task, tlb shootdown cannot be
performed because of deadlock issue. Thus, page allocator should work
being aware of whether tlb shootdown can be performed on returning page.

This patch introduced APIs that pcp or buddy page allocator can use to
delimit the critical sections taking off pages and indentify whether
tlb shootdown can be performed.

Signed-off-by: Byungchul Park <byungchul@sk.com>
---
include/linux/sched.h | 5 ++
mm/internal.h | 14 ++++
mm/page_alloc.c | 159 ++++++++++++++++++++++++++++++++++++++++++
mm/rmap.c | 2 +-
4 files changed, 179 insertions(+), 1 deletion(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 8e6e7a83332cf..c4ff83e1d5953 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1374,6 +1374,11 @@ struct task_struct {
struct callback_head cid_work;
#endif

+#if defined(CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH)
+ int luf_no_shootdown;
+ int luf_takeoff_started;
+#endif
+
struct tlbflush_unmap_batch tlb_ubc;
struct tlbflush_unmap_batch tlb_ubc_takeoff;

diff --git a/mm/internal.h b/mm/internal.h
index cbdebf8a02437..55bc8ca0d6118 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1583,6 +1583,20 @@ static inline void accept_page(struct page *page)
{
}
#endif /* CONFIG_UNACCEPTED_MEMORY */
+#if defined(CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH)
+extern struct luf_batch luf_batch[];
+bool luf_takeoff_start(void);
+void luf_takeoff_end(void);
+bool luf_takeoff_no_shootdown(void);
+bool luf_takeoff_check(struct page *page);
+bool luf_takeoff_check_and_fold(struct page *page);
+#else
+static inline bool luf_takeoff_start(void) { return false; }
+static inline void luf_takeoff_end(void) {}
+static inline bool luf_takeoff_no_shootdown(void) { return true; }
+static inline bool luf_takeoff_check(struct page *page) { return true; }
+static inline bool luf_takeoff_check_and_fold(struct page *page) { return true; }
+#endif

/* pagewalk.c */
int walk_page_range_mm(struct mm_struct *mm, unsigned long start,
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 244cb30496be5..cac2c95ca2430 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -622,6 +622,165 @@ compaction_capture(struct capture_control *capc, struct page *page,
}
#endif /* CONFIG_COMPACTION */

+#if defined(CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH)
+static bool no_shootdown_context(void)
+{
+ /*
+ * If it performs with irq disabled, that might cause a deadlock.
+ * Avoid tlb shootdown in this case.
+ */
+ return !(!irqs_disabled() && in_task());
+}
+
+/*
+ * Can be called with zone lock released and irq enabled.
+ */
+bool luf_takeoff_start(void)
+{
+ unsigned long flags;
+ bool no_shootdown = no_shootdown_context();
+
+ local_irq_save(flags);
+
+ /*
+ * It's the outmost luf_takeoff_start().
+ */
+ if (!current->luf_takeoff_started)
+ VM_WARN_ON(current->luf_no_shootdown);
+
+ /*
+ * current->luf_no_shootdown > 0 doesn't mean tlb shootdown is
+ * not allowed at all. However, it guarantees tlb shootdown is
+ * possible once current->luf_no_shootdown == 0. It might look
+ * too conservative but for now do this way for simplity.
+ */
+ if (no_shootdown || current->luf_no_shootdown)
+ current->luf_no_shootdown++;
+
+ current->luf_takeoff_started++;
+ local_irq_restore(flags);
+
+ return !no_shootdown;
+}
+
+/*
+ * Should be called within the same context of luf_takeoff_start().
+ */
+void luf_takeoff_end(void)
+{
+ unsigned long flags;
+ bool no_shootdown;
+ bool outmost = false;
+
+ local_irq_save(flags);
+ VM_WARN_ON(!current->luf_takeoff_started);
+
+ /*
+ * Assume the context and irq flags are same as those at
+ * luf_takeoff_start().
+ */
+ if (current->luf_no_shootdown)
+ current->luf_no_shootdown--;
+
+ no_shootdown = !!current->luf_no_shootdown;
+
+ current->luf_takeoff_started--;
+
+ /*
+ * It's the outmost luf_takeoff_end().
+ */
+ if (!current->luf_takeoff_started)
+ outmost = true;
+
+ local_irq_restore(flags);
+
+ if (no_shootdown)
+ goto out;
+
+ try_to_unmap_flush_takeoff();
+out:
+ if (outmost)
+ VM_WARN_ON(current->luf_no_shootdown);
+}
+
+/*
+ * Can be called with zone lock released and irq enabled.
+ */
+bool luf_takeoff_no_shootdown(void)
+{
+ bool no_shootdown = true;
+ unsigned long flags;
+
+ local_irq_save(flags);
+
+ /*
+ * No way. Delimit using luf_takeoff_{start,end}().
+ */
+ if (unlikely(!current->luf_takeoff_started)) {
+ VM_WARN_ON(1);
+ goto out;
+ }
+ no_shootdown = current->luf_no_shootdown;
+out:
+ local_irq_restore(flags);
+ return no_shootdown;
+}
+
+/*
+ * Should be called with either zone lock held and irq disabled or pcp
+ * lock held.
+ */
+bool luf_takeoff_check(struct page *page)
+{
+ unsigned short luf_key = page_luf_key(page);
+
+ /*
+ * No way. Delimit using luf_takeoff_{start,end}().
+ */
+ if (unlikely(!current->luf_takeoff_started)) {
+ VM_WARN_ON(1);
+ return false;
+ }
+
+ if (!luf_key)
+ return true;
+
+ return !current->luf_no_shootdown;
+}
+
+/*
+ * Should be called with either zone lock held and irq disabled or pcp
+ * lock held.
+ */
+bool luf_takeoff_check_and_fold(struct page *page)
+{
+ struct tlbflush_unmap_batch *tlb_ubc_takeoff = &current->tlb_ubc_takeoff;
+ unsigned short luf_key = page_luf_key(page);
+ struct luf_batch *lb;
+ unsigned long flags;
+
+ /*
+ * No way. Delimit using luf_takeoff_{start,end}().
+ */
+ if (unlikely(!current->luf_takeoff_started)) {
+ VM_WARN_ON(1);
+ return false;
+ }
+
+ if (!luf_key)
+ return true;
+
+ if (current->luf_no_shootdown)
+ return false;
+
+ lb = &luf_batch[luf_key];
+ read_lock_irqsave(&lb->lock, flags);
+ fold_batch(tlb_ubc_takeoff, &lb->batch, false);
+ read_unlock_irqrestore(&lb->lock, flags);
+ return true;
+}
+#endif
+
static inline void account_freepages(struct zone *zone, int nr_pages,
int migratetype)
{
diff --git a/mm/rmap.c b/mm/rmap.c
index 72c5e665e59a4..1581b1a00f974 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -693,7 +693,7 @@ void fold_batch(struct tlbflush_unmap_batch *dst,
/*
* Use 0th entry as accumulated batch.
*/
-static struct luf_batch luf_batch[NR_LUF_BATCH];
+struct luf_batch luf_batch[NR_LUF_BATCH];

static void luf_batch_init(struct luf_batch *lb)
{
--
2.17.1