Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: trigger page reclaim in alloc_contig_range() to stabilise watermarks

alloc_contig_range() performs memory allocation so it also should keep
track on keeping the correct level of memory watermarks. This commit adds
a call to *_slowpath style reclaim to grab enough pages to make sure that
the final collection of contiguous pages from freelists will not starve
the system.

Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
CC: Michal Nazarewicz <mina86@mina86.com>
Tested-by: Rob Clark <rob.clark@linaro.org>
Tested-by: Ohad Ben-Cohen <ohad@wizery.com>
Tested-by: Benjamin Gaignard <benjamin.gaignard@linaro.org>
Tested-by: Robert Nelson <robertcnelson@gmail.com>
Tested-by: Barry Song <Baohua.Song@csr.com>

+69
+9
include/linux/mmzone.h
··· 63 63 64 64 #ifdef CONFIG_CMA 65 65 # define is_migrate_cma(migratetype) unlikely((migratetype) == MIGRATE_CMA) 66 + # define cma_wmark_pages(zone) zone->min_cma_pages 66 67 #else 67 68 # define is_migrate_cma(migratetype) false 69 + # define cma_wmark_pages(zone) 0 68 70 #endif 69 71 70 72 #define for_each_migratetype_order(order, type) \ ··· 372 370 #ifdef CONFIG_MEMORY_HOTPLUG 373 371 /* see spanned/present_pages for more description */ 374 372 seqlock_t span_seqlock; 373 + #endif 374 + #ifdef CONFIG_CMA 375 + /* 376 + * CMA needs to increase watermark levels during the allocation 377 + * process to make sure that the system is not starved. 378 + */ 379 + unsigned long min_cma_pages; 375 380 #endif 376 381 struct free_area free_area[MAX_ORDER]; 377 382
+60
mm/page_alloc.c
··· 5079 5079 5080 5080 zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + (tmp >> 2); 5081 5081 zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + (tmp >> 1); 5082 + 5083 + zone->watermark[WMARK_MIN] += cma_wmark_pages(zone); 5084 + zone->watermark[WMARK_LOW] += cma_wmark_pages(zone); 5085 + zone->watermark[WMARK_HIGH] += cma_wmark_pages(zone); 5086 + 5082 5087 setup_zone_migrate_reserve(zone); 5083 5088 spin_unlock_irqrestore(&zone->lock, flags); 5084 5089 } ··· 5689 5684 return ret > 0 ? 0 : ret; 5690 5685 } 5691 5686 5687 + /* 5688 + * Update zone's cma pages counter used for watermark level calculation. 5689 + */ 5690 + static inline void __update_cma_watermarks(struct zone *zone, int count) 5691 + { 5692 + unsigned long flags; 5693 + spin_lock_irqsave(&zone->lock, flags); 5694 + zone->min_cma_pages += count; 5695 + spin_unlock_irqrestore(&zone->lock, flags); 5696 + setup_per_zone_wmarks(); 5697 + } 5698 + 5699 + /* 5700 + * Trigger memory pressure bump to reclaim some pages in order to be able to 5701 + * allocate 'count' pages in single page units. Does similar work as 5702 + *__alloc_pages_slowpath() function. 5703 + */ 5704 + static int __reclaim_pages(struct zone *zone, gfp_t gfp_mask, int count) 5705 + { 5706 + enum zone_type high_zoneidx = gfp_zone(gfp_mask); 5707 + struct zonelist *zonelist = node_zonelist(0, gfp_mask); 5708 + int did_some_progress = 0; 5709 + int order = 1; 5710 + 5711 + /* 5712 + * Increase level of watermarks to force kswapd do his job 5713 + * to stabilise at new watermark level. 5714 + */ 5715 + __update_cma_watermarks(zone, count); 5716 + 5717 + /* Obey watermarks as if the page was being allocated */ 5718 + while (!zone_watermark_ok(zone, 0, low_wmark_pages(zone), 0, 0)) { 5719 + wake_all_kswapd(order, zonelist, high_zoneidx, zone_idx(zone)); 5720 + 5721 + did_some_progress = __perform_reclaim(gfp_mask, order, zonelist, 5722 + NULL); 5723 + if (!did_some_progress) { 5724 + /* Exhausted what can be done so it's blamo time */ 5725 + out_of_memory(zonelist, gfp_mask, order, NULL, false); 5726 + } 5727 + } 5728 + 5729 + /* Restore original watermark levels. */ 5730 + __update_cma_watermarks(zone, -count); 5731 + 5732 + return count; 5733 + } 5734 + 5692 5735 /** 5693 5736 * alloc_contig_range() -- tries to allocate given range of pages 5694 5737 * @start: start PFN to allocate ··· 5835 5782 goto done; 5836 5783 } 5837 5784 5785 + /* 5786 + * Reclaim enough pages to make sure that contiguous allocation 5787 + * will not starve the system. 5788 + */ 5789 + __reclaim_pages(zone, GFP_HIGHUSER_MOVABLE, end-start); 5790 + 5791 + /* Grab isolated pages from freelists. */ 5838 5792 outer_end = isolate_freepages_range(outer_start, end); 5839 5793 if (!outer_end) { 5840 5794 ret = -EBUSY;