Skip to contentSkip to conversation

mm/memory: drop PROCESS_PAGES_NON_PREEMPT_BATCH

RFC PATCH Superseded Shivank Garg 3 participants 3 messages Oct 22

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 mm/memory: drop PROCESS_PAGES_NON_PREEMPT_BATCH

clear_contig_highpages() clears the whole range at once under preemptible models. Otherwise it clears PROCESS_PAGES_NON_PREEMPT_BATCH pages at a time, with a cond_resched() in between. The batch is 32MB if the architecture defines clear_pages(), and one page otherwise.

Since commit 7dadeaa6e851 ("sched: Further restrict the preemption modes"), architectures that support lazy preemption only offer the full, lazy and RT preemption models, also with PREEMPT_DYNAMIC. x86 is one of them, and it is the only architecture that defines clear_pages(). So the 32MB batch is never used: whenever clear_contig_highpages() runs under a cooperative model, the batch is one page.

Make that explicit. Clear the whole range when CONFIG_PREEMPTION is set, and one page at a time with cond_resched() in between otherwise. Drop PROCESS_PAGES_NON_PREEMPT_BATCH, which has no other users.

PREEMPT_DYNAMIC kernels also have CONFIG_PREEMPTION set, and cond_resched() does nothing on any of these kernels. So a build-time CONFIG_PREEMPTION check is enough, and preempt_model_preemptible() is not needed.

No functional change intended.

---
This came up in review of the copy_mc_highpages() series [1]. Pedro pointed out that most architecture are full or lazy preempt, so mm does not need to check the preemption model at runtime or batch the copy for cond_resched(), and simple per-page fallback is enough for non-preemptible models. David suggested doing the same for the zeroing path, and this patch does that.

[1] https://lore.kernel.org/all/[email protected]

---
 include/linux/mm.h | 28 +---------------------------
 mm/memory.c        | 29 ++++++++++++++++-------------
 2 files changed, 17 insertions(+), 40 deletions(-)
include/linux/mm.h +1 -27
index 35f5fc43861a..1083d0645d63 100644
@@ -5269,7 +5269,7 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
  *
  * When running under preemptible models this is not a problem. Under
  * cooperatively scheduled models, however, the caller is expected to
- * limit @npages to no more than PROCESS_PAGES_NON_PREEMPT_BATCH.
+ * clear one page at a time and call cond_resched() in between.
  */
 static inline void clear_pages(void *addr, unsigned int npages)
 {
@@ -5280,32 +5280,6 @@ static inline void clear_pages(void *addr, unsigned int npages)
 }
 #endif
 
-#ifndef PROCESS_PAGES_NON_PREEMPT_BATCH
-#ifdef clear_pages
-/*
- * The architecture defines clear_pages(), and we assume that it is
- * generally "fast". So choose a batch size large enough to allow the processor
- * headroom for optimizing the operation and yet small enough that we see
- * reasonable preemption latency for when this optimization is not possible
- * (ex. slow microarchitectures, memory bandwidth saturation.)
- *
- * With a value of 32MB and assuming a memory bandwidth of ~10GBps, this should
- * result in worst case preemption latency of around 3ms when clearing pages.
- *
- * (See comment above clear_pages() for why preemption latency is a concern
- * here.)
- */
-#define PROCESS_PAGES_NON_PREEMPT_BATCH		(SZ_32M >> PAGE_SHIFT)
-#else /* !clear_pages */
-/*
- * The architecture does not provide a clear_pages() implementation. Assume
- * that clear_page() -- which clear_pages() will fallback to -- is relatively
- * slow and choose a small value for PROCESS_PAGES_NON_PREEMPT_BATCH.
- */
-#define PROCESS_PAGES_NON_PREEMPT_BATCH		1
-#endif
-#endif
-
 #ifdef __HAVE_ARCH_GATE_AREA
 extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
 extern int in_gate_area_no_mm(unsigned long addr);
mm/memory.c +16 -13
index 976cd03954e3..e09a53ea5943 100644
@@ -7654,27 +7654,30 @@ static inline int process_huge_page(
 }
 
 static void clear_contig_highpages(struct page *page, unsigned long addr,
-				   unsigned int nr_pages)
+		unsigned int nr_pages)
 {
-	unsigned int i, count;
+	unsigned int i;
+
+	might_sleep();
+
 	/*
 	 * When clearing we want to operate on the largest extent possible to
 	 * allow for architecture specific extent based optimizations.
 	 *
-	 * However, since clear_user_highpages() (and primitives clear_user_pages(),
-	 * clear_pages()), do not call cond_resched(), limit the unit size when
-	 * running under non-preemptible scheduling models.
+	 * clear_user_highpages() (and primitives clear_user_pages(),
+	 * clear_pages()) do not call cond_resched(). That is fine on
+	 * preemptible kernels, where the scheduler can preempt the clearing
+	 * at any point, so clear the whole range at once. Otherwise, clear
+	 * one page at a time and reschedule in between.
 	 */
-	const unsigned int unit = preempt_model_preemptible() ?
-				   nr_pages : PROCESS_PAGES_NON_PREEMPT_BATCH;
-
-	might_sleep();
+	if (IS_ENABLED(CONFIG_PREEMPTION)) {
+		clear_user_highpages(page, addr, nr_pages);
+		return;
+	}
 
-	for (i = 0; i < nr_pages; i += count) {
+	for (i = 0; i < nr_pages; i++) {
 		cond_resched();
-
-		count = min(unit, nr_pages - i);
-		clear_user_highpages(page + i, addr + i * PAGE_SIZE, count);
+		clear_user_highpages(page + i, addr + i * PAGE_SIZE, 1);
 	}
 }
 

---
base-commit: fcec4f4bcc1101dbd7e3aff267856b3bbfe128c6
change-id: 20261021-clear-pages-batch-77f72ef808e4

Best regards,

Signature · 1 line
Shivank Garg <[email protected]>
#1 lore raw
Quoted 34 lines · Shivank Garg

clear_contig_highpages() clears the whole range at once under preemptible models. Otherwise it clears PROCESS_PAGES_NON_PREEMPT_BATCH pages at a time, with a cond_resched() in between. The batch is 32MB if the architecture defines clear_pages(), and one page otherwise.

Since commit 7dadeaa6e851 ("sched: Further restrict the preemption modes"), architectures that support lazy preemption only offer the full, lazy and RT preemption models, also with PREEMPT_DYNAMIC. x86 is one of them, and it is the only architecture that defines clear_pages(). So the 32MB batch is never used: whenever clear_contig_highpages() runs under a cooperative model, the batch is one page.

Make that explicit. Clear the whole range when CONFIG_PREEMPTION is set, and one page at a time with cond_resched() in between otherwise. Drop PROCESS_PAGES_NON_PREEMPT_BATCH, which has no other users.

PREEMPT_DYNAMIC kernels also have CONFIG_PREEMPTION set, and cond_resched() does nothing on any of these kernels. So a build-time CONFIG_PREEMPTION check is enough, and preempt_model_preemptible() is not needed.

No functional change intended.

---
This came up in review of the copy_mc_highpages() series [1]. Pedro pointed out that most architecture are full or lazy preempt, so mm does not need to check the preemption model at runtime or batch the copy for cond_resched(), and simple per-page fallback is enough for non-preemptible models. David suggested doing the same for the zeroing path, and this patch does that.

Quoted 5 lines

[1] https://lore.kernel.org/all/[email protected]

---
 include/linux/mm.h | 28 +---------------------------
 mm/memory.c        | 29 ++++++++++++++++-------------
 2 files changed, 17 insertions(+), 40 deletions(-)

Awesome!

include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 5272

Quoted 11 lines
include/linux/mm.h +1 -1
index 35f5fc43861a..1083d0645d63 100644
@@ -5269,7 +5269,7 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
  *
  * When running under preemptible models this is not a problem. Under
  * cooperatively scheduled models, however, the caller is expected to
- * limit @npages to no more than PROCESS_PAGES_NON_PREEMPT_BATCH.
+ * clear one page at a time and call cond_resched() in between.

Why can't clear_pages() itself do cond_resched()? Same question for clear_user_pages() and/or clear_user_highpages().

Signature · 1 line
Pedro
#2 lore raw
Quoted 42 lines · Pedro Falcato
Quoted 34 lines · Shivank Garg

clear_contig_highpages() clears the whole range at once under preemptible models. Otherwise it clears PROCESS_PAGES_NON_PREEMPT_BATCH pages at a time, with a cond_resched() in between. The batch is 32MB if the architecture defines clear_pages(), and one page otherwise.

Since commit 7dadeaa6e851 ("sched: Further restrict the preemption modes"), architectures that support lazy preemption only offer the full, lazy and RT preemption models, also with PREEMPT_DYNAMIC. x86 is one of them, and it is the only architecture that defines clear_pages(). So the 32MB batch is never used: whenever clear_contig_highpages() runs under a cooperative model, the batch is one page.

Make that explicit. Clear the whole range when CONFIG_PREEMPTION is set, and one page at a time with cond_resched() in between otherwise. Drop PROCESS_PAGES_NON_PREEMPT_BATCH, which has no other users.

PREEMPT_DYNAMIC kernels also have CONFIG_PREEMPTION set, and cond_resched() does nothing on any of these kernels. So a build-time CONFIG_PREEMPTION check is enough, and preempt_model_preemptible() is not needed.

No functional change intended.

---
This came up in review of the copy_mc_highpages() series [1]. Pedro pointed out that most architecture are full or lazy preempt, so mm does not need to check the preemption model at runtime or batch the copy for cond_resched(), and simple per-page fallback is enough for non-preemptible models. David suggested doing the same for the zeroing path, and this patch does that.

Quoted 5 lines

[1] https://lore.kernel.org/all/[email protected]

---
 include/linux/mm.h | 28 +---------------------------
 mm/memory.c        | 29 ++++++++++++++++-------------
 2 files changed, 17 insertions(+), 40 deletions(-)

Awesome!

Quoted 11 lines

include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 5272

Quoted 10 lines
include/linux/mm.h +1 -1
index 35f5fc43861a..1083d0645d63 100644
@@ -5269,7 +5269,7 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
  *
  * When running under preemptible models this is not a problem. Under
  * cooperatively scheduled models, however, the caller is expected to
- * limit @npages to no more than PROCESS_PAGES_NON_PREEMPT_BATCH.
+ * clear one page at a time and call cond_resched() in between.

Why can't clear_pages() itself do cond_resched()?

clear_pages() is also used from contexts that cannot sleep. The page allocator zeroes via clear_highpages_kasan_tagged(), which uses clear_pages() for __GFP_ZERO/init_on_alloc in post_alloc_hook() and for init_on_free in __free_pages_prepare(). Those run in whatever context the allocation or free happens in, so clear_pages() can't call cond_resched().

Regards,
Hrushikesh