Make dirty throttling obey cpusets Currently dirty throttling does not work properly in a cpuset. If f.e a cpuset contains only 1/10th of available memory then all of the memory of a cpuset can be dirtied without any writes being triggered. If we are writing to a device that is mounted via NFS then the write operation may be terminated with OOM since NFS is not allowed to allocate more pages for writeout. If all of the cpusets memory is dirty then only 10% of total memory is dirty.i The background writeback threshold is usually set at 40% and the synchrononous threshold at 60%. So we are still below the global limits while the dirty ratio in the cpuset is 100%! F.e. NFS pages can fill up the complete cpuset. This patch makes dirty writeout cpuset aware. When determining the dirty limits in get_dirty_limits() we calculate values based on the nodes that are reachable from the current process (that has been dirtying the page). Then we can trigger writeout based on the dirty ratio of the memory in the cpuset. Determination of the dirty state is expensive since we have to scan over all the nodes and sum up the relevant values. We trigger writeout in a a cpuset specific way. We go through the dirty inodes and search for inodes that have dirty pages on the nodes of the active cpuset. If an inode fulfills that requirement then we begin writeout of the dirty pages of that inode. Christoph Lameter Index: linux-2.6.20-rc3/mm/page-writeback.c =================================================================== --- linux-2.6.20-rc3.orig/mm/page-writeback.c 2007-01-05 19:42:10.341674995 -0600 +++ linux-2.6.20-rc3/mm/page-writeback.c 2007-01-05 20:00:20.516089688 -0600 @@ -103,6 +103,14 @@ EXPORT_SYMBOL(laptop_mode); static void background_writeout(unsigned long _min_pages); +struct dirty_limits { + long thresh_background; + long thresh_dirty; + unsigned long nr_dirty; + unsigned long nr_unstable; + unsigned long nr_writeback; +}; + /* * Work out the current dirty-memory clamping and background writeout * thresholds. @@ -120,31 +128,73 @@ static void background_writeout(unsigned * We make sure that the background writeout level is below the adjusted * clamping level. */ -static void -get_dirty_limits(long *pbackground, long *pdirty, - struct address_space *mapping) +static int +get_dirty_limits(struct dirty_limits *dl, struct address_space *mapping) { int background_ratio; /* Percentages */ int dirty_ratio; int unmapped_ratio; + int in_cpuset = 0; long background; long dirty; - unsigned long available_memory = vm_total_pages; + unsigned long available_memory; + unsigned long high_memory; + unsigned long nr_mapped; struct task_struct *tsk; +#ifdef CONFIG_CPUSETS + /* + * Calculate the limits relative to the current cpuset if necessary. + */ + if (unlikely(!nodes_subset(node_online_map, + cpuset_current_mems_allowed))) { + int node; + + memset(dl, 0, sizeof(struct dirty_limits)); + available_memory = 0; + high_memory = 0; + nr_mapped = 0; + in_cpuset = 1; + for_each_node_mask(node, cpuset_current_mems_allowed) { + if (!node_online(node)) + continue; + dl->nr_dirty += node_page_state(node, NR_FILE_DIRTY); + dl->nr_unstable += + node_page_state(node, NR_UNSTABLE_NFS); + dl->nr_writeback += + node_page_state(node, NR_WRITEBACK); + available_memory += + NODE_DATA(node)->node_present_pages; +#ifdef CONFIG_HIGHMEM + high_memory += NODE_DATA(node) + ->node_zones[ZONE_HIGHMEM]->present_pages; +#endif + nr_mapped += node_page_state(node, NR_FILE_MAPPED) + + node_page_state(node, NR_ANON_PAGES); + } + } else +#endif + { + /* Global limits */ + dl->nr_dirty = global_page_state(NR_FILE_DIRTY); + dl->nr_unstable = global_page_state(NR_UNSTABLE_NFS); + dl->nr_writeback = global_page_state(NR_WRITEBACK); + available_memory = vm_total_pages; + high_memory = totalhigh_pages; + nr_mapped = global_page_state(NR_FILE_MAPPED) + + global_page_state(NR_ANON_PAGES); + } #ifdef CONFIG_HIGHMEM /* * If this mapping can only allocate from low memory, * we exclude high memory from our count. */ if (mapping && !(mapping_gfp_mask(mapping) & __GFP_HIGHMEM)) - available_memory -= totalhigh_pages; + available_memory -= high_memory; #endif - unmapped_ratio = 100 - ((global_page_state(NR_FILE_MAPPED) + - global_page_state(NR_ANON_PAGES)) * 100) / - vm_total_pages; + unmapped_ratio = 100 - (nr_mapped * 100) / available_memory; dirty_ratio = vm_dirty_ratio; if (dirty_ratio > unmapped_ratio / 2) @@ -164,8 +214,9 @@ get_dirty_limits(long *pbackground, long background += background / 4; dirty += dirty / 4; } - *pbackground = background; - *pdirty = dirty; + dl->thresh_background = background; + dl->thresh_dirty = dirty; + return in_cpuset; } /* @@ -178,8 +229,7 @@ get_dirty_limits(long *pbackground, long static void balance_dirty_pages(struct address_space *mapping) { long nr_reclaimable; - long background_thresh; - long dirty_thresh; + struct dirty_limits dl; unsigned long pages_written = 0; unsigned long write_chunk = sync_writeback_pages(); @@ -194,11 +244,11 @@ static void balance_dirty_pages(struct a .range_cyclic = 1, }; - get_dirty_limits(&background_thresh, &dirty_thresh, mapping); - nr_reclaimable = global_page_state(NR_FILE_DIRTY) + - global_page_state(NR_UNSTABLE_NFS); - if (nr_reclaimable + global_page_state(NR_WRITEBACK) <= - dirty_thresh) + if (get_dirty_limits(&dl, mapping)) + wbc.in_cpuset = 1; + nr_reclaimable = dl.nr_dirty + dl.nr_unstable; + if (nr_reclaimable + dl.nr_writeback <= + dl.thresh_dirty) break; if (!dirty_exceeded) @@ -212,13 +262,9 @@ static void balance_dirty_pages(struct a */ if (nr_reclaimable) { writeback_inodes(&wbc); - get_dirty_limits(&background_thresh, - &dirty_thresh, mapping); - nr_reclaimable = global_page_state(NR_FILE_DIRTY) + - global_page_state(NR_UNSTABLE_NFS); - if (nr_reclaimable + - global_page_state(NR_WRITEBACK) - <= dirty_thresh) + get_dirty_limits(&dl, mapping); + nr_reclaimable = dl.nr_dirty + dl.nr_unstable; + if (nr_reclaimable + dl.nr_writeback <= dl.thresh_dirty) break; pages_written += write_chunk - wbc.nr_to_write; if (pages_written >= write_chunk) @@ -227,8 +273,8 @@ static void balance_dirty_pages(struct a congestion_wait(WRITE, HZ/10); } - if (nr_reclaimable + global_page_state(NR_WRITEBACK) - <= dirty_thresh && dirty_exceeded) + if (nr_reclaimable + dl.nr_writeback + <= dl.thresh_dirty && dirty_exceeded) dirty_exceeded = 0; if (writeback_in_progress(bdi)) @@ -243,7 +289,7 @@ static void balance_dirty_pages(struct a * background_thresh, to keep the amount of dirty memory low. */ if ((laptop_mode && pages_written) || - (!laptop_mode && (nr_reclaimable > background_thresh))) + (!laptop_mode && (nr_reclaimable > dl.thresh_background))) pdflush_operation(background_writeout, 0); } @@ -301,21 +347,19 @@ EXPORT_SYMBOL(balance_dirty_pages_rateli void throttle_vm_writeout(void) { - long background_thresh; - long dirty_thresh; + struct dirty_limits dl; for ( ; ; ) { - get_dirty_limits(&background_thresh, &dirty_thresh, NULL); + get_dirty_limits(&dl, NULL); /* * Boost the allowable dirty threshold a bit for page * allocators so they don't get DoS'ed by heavy writers */ - dirty_thresh += dirty_thresh / 10; /* wheeee... */ + dl.thresh_dirty += dl.thresh_dirty / 10; /* wheeee... */ - if (global_page_state(NR_UNSTABLE_NFS) + - global_page_state(NR_WRITEBACK) <= dirty_thresh) - break; + if (dl.nr_unstable + dl.nr_writeback <= dl.thresh_dirty) + break; congestion_wait(WRITE, HZ/10); } } @@ -338,12 +382,11 @@ static void background_writeout(unsigned }; for ( ; ; ) { - long background_thresh; - long dirty_thresh; + struct dirty_limits dl; - get_dirty_limits(&background_thresh, &dirty_thresh, NULL); - if (global_page_state(NR_FILE_DIRTY) + - global_page_state(NR_UNSTABLE_NFS) < background_thresh + if (get_dirty_limits(&dl, NULL)) + wbc.in_cpuset = 1; + if (dl.nr_dirty + dl.nr_unstable < dl.thresh_background && min_pages <= 0) break; wbc.encountered_congestion = 0; Index: linux-2.6.20-rc3/include/linux/writeback.h =================================================================== --- linux-2.6.20-rc3.orig/include/linux/writeback.h 2007-01-05 19:42:07.268165383 -0600 +++ linux-2.6.20-rc3/include/linux/writeback.h 2007-01-05 19:56:26.608899551 -0600 @@ -59,6 +59,8 @@ struct writeback_control { unsigned for_reclaim:1; /* Invoked from the page allocator */ unsigned for_writepages:1; /* This is a writepages() call */ unsigned range_cyclic:1; /* range_start is cyclic */ + unsigned in_cpuset:1; /* Only write back inodes that have + pages on cpusets nodes */ }; /* Index: linux-2.6.20-rc3/fs/fs-writeback.c =================================================================== --- linux-2.6.20-rc3.orig/fs/fs-writeback.c 2007-01-05 19:42:10.288936031 -0600 +++ linux-2.6.20-rc3/fs/fs-writeback.c 2007-01-05 19:56:26.722190718 -0600 @@ -365,6 +365,11 @@ sync_sb_inodes(struct super_block *sb, s if (current_is_pdflush() && !writeback_acquire(bdi)) break; +#ifdef CONFIG_CPUSETS + if (wbc->in_cpuset && !nodes_intersects(mapping->dirty_nodes, + cpuset_current_mems_allowed)) + break; +#endif BUG_ON(inode->i_state & I_FREEING); __iget(inode); pages_skipped = wbc->pages_skipped;