When kswapd is awoken due to reclaim by a running task, set the priority of kswapd to that of the calling task thus making memory reclaim cpu activity affected by nice level. -ck --- mm/vmscan.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) Index: linux-3.5-ck1/mm/vmscan.c =================================================================== --- linux-3.5-ck1.orig/mm/vmscan.c 2012-08-16 13:36:16.855888803 +1000 +++ linux-3.5-ck1/mm/vmscan.c 2012-08-16 13:36:16.992886603 +1000 @@ -1889,6 +1889,33 @@ static inline bool compaction_ready(stru } /* + * Helper functions to adjust nice level of kswapd, based on the priority of + * the task (p) that called it. If it is already higher priority we do not + * demote its nice level since it is still working on behalf of a higher + * priority task. With kernel threads we leave it at nice 0. + * + * We don't ever run kswapd real time, so if a real time task calls kswapd we + * set it to highest SCHED_NORMAL priority. + */ +static inline int effective_sc_prio(struct task_struct *p) +{ + if (likely(p->mm)) { + if (rt_task(p)) + return -20; + return task_nice(p); + } + return 0; +} + +static void set_kswapd_nice(struct task_struct *kswapd, int active) +{ + long nice = effective_sc_prio(current); + + if (task_nice(kswapd) > nice || !active) + set_user_nice(kswapd, nice); +} + +/* * This is the direct reclaim path, for page-allocating processes. We only * try to reclaim pages from zones which will satisfy the caller's allocation * request. @@ -2812,6 +2839,7 @@ static int kswapd(void *p) void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) { pg_data_t *pgdat; + int active; if (!populated_zone(zone)) return; @@ -2823,7 +2851,9 @@ void wakeup_kswapd(struct zone *zone, in pgdat->kswapd_max_order = order; pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx); } - if (!waitqueue_active(&pgdat->kswapd_wait)) + active = waitqueue_active(&pgdat->kswapd_wait); + set_kswapd_nice(pgdat->kswapd, active); + if (!active) return; if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0)) return;