Skip to contentSkip to conversation

mm: add page preemption

RFC Superseded Hillf Danton 3 participants 4 messages Nov 3, 2019 – Nov 4, 2019

Subsystems (8)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

RFC v3 1/1 mm: add page preemption

The cpu preemption feature makes a task able to preempt other tasks of lower priorities for cpu. It has been around for a while.

This work introduces task prio into page reclaiming in order to add the page preemption feature that makes a task able to preempt other tasks of lower priorities for page.

No page will be reclaimed on behalf of tasks of lower priorities under pp, a two-edge feature that functions only under memory pressure, laying a barrier to pages flowing to lower prio, and the nice syscall is what users need to fiddle with it for instance if they have a bunch of workloads to run in datacenter, and some difficulty predicting the runtime working set size for every individual workload which is sensitive to jitters in lru pages.

Currently pages are reclaimed without prio taken into account; pages can be reclaimed from tasks of lower priorities on behalf of higher-prio tasks and vice versa.

s/and vice versa/only/ is what we need to make pp by definition, but it could not make a sense without prio introduced; otherwise we can simply skip deactivating the lru pages based on prio comprison, and work is done.

The introduction consists of two parts. On the page side, we have to store the page owner task's prio in page, which needs an extra room the size of the int type in the page struct. That room sounds impossible without inflating the page struct size, and is walked around by making pp depend on CONFIG_64BIT.

On the reclaimer side, kswapd's prio is set with the prio of its waker, and updated in the same manner as kswapd_order.

Another change is, because pp is a two-edge option, added in oom to avoid killing high-prio task in favor of low-prio task.

V3 is based on next-20191031.

Changes since v2
- page->prio depends on CONFIG_64BIT
- fix high-prio task killed in oom

Changes since v1
- page->prio depends on !LAST_CPUPID_NOT_IN_PAGE_FLAGS

Changes since v0
- s/page->nice/page->prio/
- drop the role of kswapd's reclaiming prioirty in prio comparison
- add pgdat->kswapd_prio

---

include/linux/mm_types.h +10 -0
@@ -197,8 +197,18 @@ struct page {
 	/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
 	atomic_t _refcount;
 
+	/* 32/56 bytes above */
+
 #ifdef CONFIG_MEMCG
 	struct mem_cgroup *mem_cgroup;
+#else
+#ifdef CONFIG_64BIT
+	union {
+		int prio;
+		void *__pad;
+	};
+#define CONFIG_PAGE_PREEMPTION PP
+#endif
 #endif
 
 	/*
include/linux/page_prio.h new file +55 -0

55 lines not on this page · raw message

include/linux/mm.h +1 -0
@@ -24,6 +24,7 @@
 #include <linux/page_ext.h>
 #include <linux/err.h>
 #include <linux/page_ref.h>
+#include <linux/page_prio.h>
 #include <linux/memremap.h>
 #include <linux/overflow.h>
 #include <linux/sizes.h>
mm/khugepaged.c +2 -0
@@ -671,6 +671,7 @@ static void __collapse_huge_page_copy(pt
 			}
 		} else {
 			src_page = pte_page(pteval);
+			copy_page_prio(page, src_page);
 			copy_user_highpage(page, src_page, address, vma);
 			VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
 			release_pte_page(src_page);
@@ -1735,6 +1736,7 @@ xa_unlocked:
 				clear_highpage(new_page + (index % HPAGE_PMD_NR));
 				index++;
 			}
+			copy_page_prio(new_page, page);
 			copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
 					page);
 			list_del(&page->lru);
mm/migrate.c +1 -0
@@ -647,6 +647,7 @@ void migrate_page_states(struct page *ne
 		end_page_writeback(newpage);
 
 	copy_page_owner(page, newpage);
+	copy_page_prio(newpage, page);
 
 	mem_cgroup_migrate(page, newpage);
 }
mm/oom_kill.c +5 -0
@@ -164,6 +164,11 @@ static bool oom_unkillable_task(struct t
 		return true;
 	if (p->flags & PF_KTHREAD)
 		return true;
+
+#ifdef CONFIG_PAGE_PREEMPTION
+	if (p->prio < current->prio)
+		return true;
+#endif
 	return false;
 }
 
mm/shmem.c +1 -0
@@ -1575,6 +1575,7 @@ static int shmem_replace_page(struct pag
 
 	get_page(newpage);
 	copy_highpage(newpage, oldpage);
+	copy_page_prio(newpage, oldpage);
 	flush_dcache_page(newpage);
 
 	__SetPageLocked(newpage);
mm/swap.c +1 -0
@@ -407,6 +407,7 @@ static void __lru_cache_add(struct page
 	struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
 
 	get_page(page);
+	set_page_prio(page, current->prio);
 	if (!pagevec_add(pvec, page) || PageCompound(page))
 		__pagevec_lru_add(pvec);
 	put_cpu_var(lru_add_pvec);
include/linux/mmzone.h +1 -0
@@ -738,6 +738,7 @@ typedef struct pglist_data {
 	int kswapd_order;
 	enum zone_type kswapd_classzone_idx;
 
+	int kswapd_prio;
 	int kswapd_failures;		/* Number of 'reclaimed == 0' runs */
 
 #ifdef CONFIG_COMPACTION
mm/vmscan.c +31 -1
@@ -110,6 +110,9 @@ struct scan_control {
 	/* The highest zone to isolate pages for reclaim from */
 	s8 reclaim_idx;
 
+	s8 __pad;
+	int reclaimer_prio;
+
 	/* This context's GFP mask */
 	gfp_t gfp_mask;
 
@@ -1707,11 +1710,17 @@ static unsigned long isolate_lru_pages(u
 		total_scan += nr_pages;
 
 		if (page_zonenum(page) > sc->reclaim_idx) {
+next_page:
 			list_move(&page->lru, &pages_skipped);
 			nr_skipped[page_zonenum(page)] += nr_pages;
 			continue;
 		}
 
+#ifdef CONFIG_PAGE_PREEMPTION
+		if (is_active_lru(lru) && global_reclaim(sc) &&
+		    page_prio_higher(page, sc->reclaimer_prio))
+			goto next_page;
+#endif
 		/*
 		 * Do not count skipped pages because that makes the function
 		 * return with no isolated pages if the LRU mostly contains
@@ -3257,6 +3266,7 @@ unsigned long try_to_free_pages(struct z
 	unsigned long nr_reclaimed;
 	struct scan_control sc = {
 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
+		.reclaimer_prio = current->prio,
 		.gfp_mask = current_gfp_context(gfp_mask),
 		.reclaim_idx = gfp_zone(gfp_mask),
 		.order = order,
@@ -3583,6 +3593,7 @@ static int balance_pgdat(pg_data_t *pgda
 	bool boosted;
 	struct zone *zone;
 	struct scan_control sc = {
+		.reclaimer_prio = pgdat->kswapd_prio,
 		.gfp_mask = GFP_KERNEL,
 		.order = order,
 		.may_unmap = 1,
@@ -3736,6 +3747,8 @@ restart:
 		if (nr_boost_reclaim && !nr_reclaimed)
 			break;
 
+		sc.reclaimer_prio = pgdat->kswapd_prio;
+
 		if (raise_priority || !nr_reclaimed)
 			sc.priority--;
 	} while (sc.priority >= 1);
@@ -3828,6 +3841,7 @@ static void kswapd_try_to_sleep(pg_data_
 		 */
 		wakeup_kcompactd(pgdat, alloc_order, classzone_idx);
 
+		pgdat->kswapd_prio = MAX_PRIO + 1;
 		remaining = schedule_timeout(HZ/10);
 
 		/*
@@ -3862,8 +3876,10 @@ static void kswapd_try_to_sleep(pg_data_
 		 */
 		set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
 
-		if (!kthread_should_stop())
+		if (!kthread_should_stop()) {
+			pgdat->kswapd_prio = MAX_PRIO + 1;
 			schedule();
+		}
 
 		set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
 	} else {
@@ -3914,6 +3930,7 @@ static int kswapd(void *p)
 	tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
 	set_freezable();
 
+	pgdat->kswapd_prio = MAX_PRIO + 1;
 	pgdat->kswapd_order = 0;
 	pgdat->kswapd_classzone_idx = MAX_NR_ZONES;
 	for ( ; ; ) {
@@ -3982,6 +3999,19 @@ void wakeup_kswapd(struct zone *zone, gf
 		return;
 	pgdat = zone->zone_pgdat;
 
+#ifdef CONFIG_PAGE_PREEMPTION
+	do {
+		int prio = current->prio;
+
+		if (pgdat->kswapd_prio < prio) {
+			smp_rmb();
+			return;
+		}
+		pgdat->kswapd_prio = prio;
+		smp_wmb();
+	} while (0);
+#endif
+
 	if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES)
 		pgdat->kswapd_classzone_idx = classzone_idx;
 	else

--

#1 lore raw
Quoted 24 lines · Hillf Danton

The cpu preemption feature makes a task able to preempt other tasks of lower priorities for cpu. It has been around for a while.

This work introduces task prio into page reclaiming in order to add the page preemption feature that makes a task able to preempt other tasks of lower priorities for page.

No page will be reclaimed on behalf of tasks of lower priorities under pp, a two-edge feature that functions only under memory pressure, laying a barrier to pages flowing to lower prio, and the nice syscall is what users need to fiddle with it for instance if they have a bunch of workloads to run in datacenter, and some difficulty predicting the runtime working set size for every individual workload which is sensitive to jitters in lru pages.

Currently pages are reclaimed without prio taken into account; pages can be reclaimed from tasks of lower priorities on behalf of higher-prio tasks and vice versa.

s/and vice versa/only/ is what we need to make pp by definition, but it could not make a sense without prio introduced; otherwise we can simply skip deactivating the lru pages based on prio comprison, and work is done.

Quoted 5 lines

The introduction consists of two parts. On the page side, we have to store the page owner task's prio in page, which needs an extra room the size of the int type in the page struct. That room sounds impossible without inflating the page struct size, and is walked around by making pp depend on CONFIG_64BIT.

... and !MEMCG. Which means that your work is uninteresting because all the distros turn on CONFIG_MEMCG.

You still haven't given us any numbers. Or a workload which actually benefits from this patch.

#2 lore raw
In reply to Hillf Danton
Quoted 25 lines · On Sun, 3 Nov 2019 09:06:49 -0800 Matthew Wilcox wrote:
Quoted 24 lines · Hillf Danton

The cpu preemption feature makes a task able to preempt other tasks of lower priorities for cpu. It has been around for a while.

This work introduces task prio into page reclaiming in order to add the page preemption feature that makes a task able to preempt other tasks of lower priorities for page.

No page will be reclaimed on behalf of tasks of lower priorities under pp, a two-edge feature that functions only under memory pressure, laying a barrier to pages flowing to lower prio, and the nice syscall is what users need to fiddle with it for instance if they have a bunch of workloads to run in datacenter, and some difficulty predicting the runtime working set size for every individual workload which is sensitive to jitters in lru pages.

Currently pages are reclaimed without prio taken into account; pages can be reclaimed from tasks of lower priorities on behalf of higher-prio tasks and vice versa.

s/and vice versa/only/ is what we need to make pp by definition, but it could not make a sense without prio introduced; otherwise we can simply skip deactivating the lru pages based on prio comprison, and work is done.

Quoted 8 lines
Quoted 5 lines

The introduction consists of two parts. On the page side, we have to store the page owner task's prio in page, which needs an extra room the size of the int type in the page struct. That room sounds impossible without inflating the page struct size, and is walked around by making pp depend on CONFIG_64BIT.

... and !MEMCG. Which means that your work is uninteresting because all the distros turn on CONFIG_MEMCG.

I have no idea which one they turn on by default, ext4, btrfs or xfs, and why, but I think they feel free to do the right, or the left. So do users to configure and build the kernel they need to power their machines, a 4-socket server or a TV-top box.

Quoted 2 lines

You still haven't given us any numbers. Or a workload which actually benefits from this patch.

Though I do not know why it is turned on by distros and for what workloads, I would like to try to run a couple of the workloads you may have interest in.

Thanks
Hillf

#3 lore raw
Quoted 41 lines · Hillf Danton
Quoted 25 lines · On Sun, 3 Nov 2019 09:06:49 -0800 Matthew Wilcox wrote:
Quoted 24 lines · Hillf Danton

The cpu preemption feature makes a task able to preempt other tasks of lower priorities for cpu. It has been around for a while.

This work introduces task prio into page reclaiming in order to add the page preemption feature that makes a task able to preempt other tasks of lower priorities for page.

No page will be reclaimed on behalf of tasks of lower priorities under pp, a two-edge feature that functions only under memory pressure, laying a barrier to pages flowing to lower prio, and the nice syscall is what users need to fiddle with it for instance if they have a bunch of workloads to run in datacenter, and some difficulty predicting the runtime working set size for every individual workload which is sensitive to jitters in lru pages.

Currently pages are reclaimed without prio taken into account; pages can be reclaimed from tasks of lower priorities on behalf of higher-prio tasks and vice versa.

s/and vice versa/only/ is what we need to make pp by definition, but it could not make a sense without prio introduced; otherwise we can simply skip deactivating the lru pages based on prio comprison, and work is done.

Quoted 8 lines
Quoted 5 lines

The introduction consists of two parts. On the page side, we have to store the page owner task's prio in page, which needs an extra room the size of the int type in the page struct. That room sounds impossible without inflating the page struct size, and is walked around by making pp depend on CONFIG_64BIT.

... and !MEMCG. Which means that your work is uninteresting because all the distros turn on CONFIG_MEMCG.

I have no idea which one they turn on by default, ext4, btrfs or xfs, and why, but I think they feel free to do the right, or the left. So do users to configure and build the kernel they need to power their machines, a 4-socket server or a TV-top box.

Quoted 7 lines
Quoted 2 lines

You still haven't given us any numbers. Or a workload which actually benefits from this patch.

Though I do not know why it is turned on by distros and for what workloads, I would like to try to run a couple of the workloads you may have interest in.

Why do you keep posting RFC if you don't care about feedback? That's not how upstream work works, really.

Please finally fix your mail client for good as already noted in:
https://www.spinics.net/lists/linux-mm/msg194683.html

Signature · 3 lines
Thanks,

David / dhildenb