Skip to contentSkip to conversation

Dirty balancing in the presence of mapped pages

PATCH In review Shantanu Goel 5 participants 10 messages May 5, 2004 – May 9, 2004

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Dirty balancing in the presence of mapped pages

Hi,

Presently the kernel does not collection information about the percentage of memory that processes have dirtied via mmap until reclamation. Nothing analogous to balance_dirty_pages() is being done for mmap'ed pages. The attached patch adds collection of dirty page information during kswapd() scans and initiation of background writeback by waking up bdflush.

Thanks,
Shantanu

__________________________________
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
http://hotjobs.sweepstakes.yahoo.com/careermakeover

include/linux/page-flags.h +1 -0
@@ -133,6 +133,7 @@
 	unsigned long allocstall;	/* direct reclaim calls */
 
 	unsigned long pgrotated;	/* pages rotated to tail of the LRU */
+	unsigned long bdflush;		/* bdflush wakeups due to dirty mapped pages */
 } ____cacheline_aligned;
 
 DECLARE_PER_CPU(struct page_state, page_states);
include/linux/rmap.h +1 -0
@@ -28,6 +28,7 @@
 struct pte_chain * fastcall
 	page_add_rmap(struct page *, pte_t *, struct pte_chain *);
 void fastcall page_remove_rmap(struct page *, pte_t *);
+int fastcall page_is_dirty(struct page *);
 
 /*
  * Called from mm/vmscan.c to handle paging out
mm/page_alloc.c +1 -0
@@ -1681,6 +1681,7 @@
 	"allocstall",
 
 	"pgrotated",
+	"bdflush",
 };
 
 static void *vmstat_start(struct seq_file *m, loff_t *pos)
mm/rmap.c +37 -0
@@ -170,6 +170,43 @@
 }
 
 /**
+ * page_is_dirty - test if the page was modified
+ * @page: the page to test
+ *
+ * Quick test_and_clear_dirty for all mappings to a page,
+ * returns the number of processes which modified the page.
+ * Caller needs to hold the rmap lock.
+ */
+int fastcall page_is_dirty(struct page * page)
+{
+	struct pte_chain *pc;
+	int dirty = 0;
+
+	if (PageDirect(page)) {
+		pte_t *pte = rmap_ptep_map(page->pte.direct);
+		if (ptep_test_and_clear_dirty(pte))
+			dirty++;
+		rmap_ptep_unmap(pte);
+	} else {
+		/* Check all the page tables mapping this page. */
+		for (pc = page->pte.chain; pc; pc = pte_chain_next(pc)) {
+			int i;
+
+			for (i = pte_chain_idx(pc); i < NRPTE; i++) {
+				pte_addr_t pte_paddr = pc->ptes[i];
+				pte_t *p;
+
+				p = rmap_ptep_map(pte_paddr);
+				if (ptep_test_and_clear_dirty(p))
+					dirty++;
+				rmap_ptep_unmap(p);
+			}
+		}
+	}
+	return dirty;
+}
+
+/**
  * page_add_rmap - add reverse mapping entry to a page
  * @page: the page to add the mapping to
  * @ptep: the page table entry mapping this page
mm/vmscan.c +27 -15

127 lines not on this page · raw message

#1 lore raw
Quoted 8 lines · Shantanu Goel

Presently the kernel does not collection information about the percentage of memory that processes have dirtied via mmap until reclamation. Nothing analogous to balance_dirty_pages() is being done for mmap'ed pages. The attached patch adds collection of dirty page information during kswapd() scans and initiation of background writeback by waking up bdflush.

And what were the effects of this patch?

Did you consider propagating the pte dirtiness into the pageframe within page_referenced(), while we're there?

#2 lore raw
Quoted 11 lines · Andrew Morton
Quoted 7 lines · Shantanu Goel

Presently the kernel does not collection information about the percentage of memory that processes have dirtied via mmap until reclamation. Nothing analogous to balance_dirty_pages() is being done for mmap'ed pages. The attached patch adds collection of dirty page information during kswapd() scans and initiation of background writeback by waking up bdflush.

Quoted 2 lines

And what were the effects of this patch?

I havea modified patch from Nikita that does the if (ptep_test_and_clear_dirty) set_page_dirty from page_referenced, under the page_table_lock.

So it also picks up pages coming off the active list.

It doesn't do the wakeup_bdflush thing, but that sounds like a good idea. What does wakeup_bdflush(-1) mean?

#3 lore raw
Quoted 13 lines · Nick Piggin
Quoted 11 lines · Andrew Morton
Quoted 7 lines · Shantanu Goel

Presently the kernel does not collection information about the percentage of memory that processes have dirtied via mmap until reclamation. Nothing analogous to balance_dirty_pages() is being done for mmap'ed pages. The attached patch adds collection of dirty page information during kswapd() scans and initiation of background writeback by waking up bdflush.

Quoted 6 lines
Quoted 2 lines

And what were the effects of this patch?

I havea modified patch from Nikita that does the if (ptep_test_and_clear_dirty) set_page_dirty from page_referenced, under the page_table_lock.

Dude. I have lots of patches too. The question is: what use are they?

In this case, given that we have an actively mapped MAP_SHARED pagecache page, marking it dirty will cause it to be written by pdflush. Even though we're not about to reclaim it, and even though the process which is mapping the page may well modify it again. This patch will cause additional I/O.

So we need to understand why it was written, and what effects were observed, with what workload, and all that good stuff.

Quoted 2 lines

It doesn't do the wakeup_bdflush thing, but that sounds like a good idea. What does wakeup_bdflush(-1) mean?

It appears that it will cause pdflush to write out down to dirty_background_ratio.

#4 lore raw
Quoted 26 lines · Andrew Morton
Quoted 14 lines · Nick Piggin
Quoted 12 lines · Andrew Morton
Quoted 7 lines · Shantanu Goel

Presently the kernel does not collection information about the percentage of memory that processes have dirtied via mmap until reclamation. Nothing analogous to balance_dirty_pages() is being done for mmap'ed pages. The attached patch adds collection of dirty page information during kswapd() scans and initiation of background writeback by waking up bdflush.

Quoted 6 lines
Quoted 2 lines

And what were the effects of this patch?

I havea modified patch from Nikita that does the if (ptep_test_and_clear_dirty) set_page_dirty from page_referenced, under the page_table_lock.

Dude. I have lots of patches too. The question is: what use are they?

Quoted 8 lines

In this case, given that we have an actively mapped MAP_SHARED pagecache page, marking it dirty will cause it to be written by pdflush. Even though we're not about to reclaim it, and even though the process which is mapping the page may well modify it again. This patch will cause additional I/O.

So we need to understand why it was written, and what effects were observed, with what workload, and all that good stuff.

I guess it is an attempt to do somewhat better at dirty page accounting for mmap'ed pages. The balance_dirty_pages_ratelimited writeout path also has the same problem as you describe. Maybe usage patterns means this is less of an issue here?

But I suppose it wouldn't be nice to change without seeing some improvement somewhere.

Quoted 8 lines
Quoted 3 lines

It doesn't do the wakeup_bdflush thing, but that sounds like a good idea. What does wakeup_bdflush(-1) mean?

It appears that it will cause pdflush to write out down to dirty_background_ratio.

Yeah. So wakeup_bdflush(0) would be more consistent?

#5 lore raw
Quoted 10 lines · Nick Piggin
Quoted 8 lines · Andrew Morton
Quoted 2 lines · Nick Piggin

It doesn't do the wakeup_bdflush thing, but that sounds like a good idea. What does wakeup_bdflush(-1) mean?

Quoted 6 lines
Quoted 3 lines

It appears that it will cause pdflush to write out down to dirty_background_ratio.

Yeah. So wakeup_bdflush(0) would be more consistent?

No. Sorry, next time I'll actually read the code :|

#6 lore raw
In reply to Nick Piggin
Quoted 11 lines · Nick Piggin
Quoted 3 lines

So we need to understand why it was written, and what effects were observed, with what workload, and all that good stuff.

I guess it is an attempt to do somewhat better at dirty page accounting for mmap'ed pages. The balance_dirty_pages_ratelimited writeout path also has the same problem as you describe. Maybe usage patterns means this is less of an issue here?

But I suppose it wouldn't be nice to change without seeing some improvement somewhere.

Lots of issues here, writing in random blocks can lead to fragmentation if the data is newly allocated, but won't change fragmenting if the page mapped is alread allocated on the disk.

Is it practical or desirable to be writing mapped pages of allready allocated files back more readily, since it avoid all the allocation issues? But you still need to limit dirty pages, so at some point it will be necessary to do the allocation, preferably in an optimal way.

Quoted 12 lines
Quoted 10 lines
Quoted 4 lines

It doesn't do the wakeup_bdflush thing, but that sounds like a good idea. What does wakeup_bdflush(-1) mean?

It appears that it will cause pdflush to write out down to dirty_background_ratio.

Yeah. So wakeup_bdflush(0) would be more consistent?

Signature · 3 lines
bill davidsen <[email protected]>
  CTO TMR Associates, Inc
  Doing interesting things with small computers since 1979
#7 lore raw
In reply to Andrew Morton
Quoted 1 line · --- Andrew Morton
Quoted 1 line
Quoted 1 line

And what were the effects of this patch?

Below are some results of an iozone run on ext3 with ordered data mode. The machine is 2xXeon with HT and 1.25GB of memory and a 15000rpm SCSI disk.

iozone was run with the following parameters:
iozone -c -B -R -i 0 -r <record length> -s 1944978

The file size is 50% more than the amount of RAM.

2.6.6-rc3-bk5 stock (all KBytes):

        record        write        re-write
             4       110752           19143
             8       109818           17726
            16       112165           17053
            32       109824           17096

2.6.6-rc3-bk5 patched (all KBytes):

        record        write        re-write
             4       114284           17467
             8       117902           17149
            16       117835           18742
            32       118102           18961

Difference from stock (%):

        record        write        re-write
             4         +3.0             -8.7
             8         +7.3             -3.2
            16         +5.0             +9.9
            32         +7.5            +10.9

It seems this patch helps writes a bit but hurts re-writes for smaller record sizes. My guess is the larger block size enables this patch to reduce the # I/O requests. I'll investigate this further and also run the random write test when I get a chance.

Quoted 9 lines

In this case, given that we have an actively mapped MAP_SHARED pagecache
page, marking it dirty will cause it to be written by pdflush. Even though
we're not about to reclaim it, and even though the process which is mapping
the page may well modify it again. This patch will cause additional I/O.

True, but is that really very different from normal file I/O where we actively balance # dirty pages? Also, the I/O will only happen if the dirty thresholds are exceeded. It probably makes sense though to skip SwapCache pages to more closely mimic file I/O behaviour.

Quoted 5 lines

So we need to understand why it was written, and
what effects were
observed, with what workload, and all that good
stuff.

My motivation was the NFS/WRITEPAGE_ACTIVATE discussion and gobs of mmap'ed sequential writes. If we can detect dirty pages before they need to be reclaimed and submit them for writeback, the NFS layer will be hopefully be able to combine them into bigger requests thereby reducing # RPCs. This works well in the file I/O case so I figured it might work equally well in the mmap case. The results are still pending though. I posted the patch to get feedback on whether people see any fundamental flaw in this approach.

Quoted 8 lines
Quoted 1 line

It doesn't do the wakeup_bdflush thing, but that

sounds

Quoted 1 line

like a good idea. What does wakeup_bdflush(-1)

mean?

It appears that it will cause pdflush to write out down to
dirty_background_ratio.

Yup, the idea is to mimic the balance_dirty_pages() behaviour but not to force writes unless required by the dirty ratios.

Thanks,
Shantanu

__________________________________
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
http://hotjobs.sweepstakes.yahoo.com/careermakeover

#8 lore raw
Quoted 11 lines · Shantanu Goel
Quoted 10 lines

In this case, given that we have an actively mapped MAP_SHARED pagecache
page, marking it dirty will cause it to be written by pdflush. Even though
we're not about to reclaim it, and even though the process which is mapping
the page may well modify it again. This patch will cause additional I/O.

Quoted 6 lines

True, but is that really very different from normal file I/O where we actively balance # dirty pages? Also, the I/O will only happen if the dirty thresholds are exceeded. It probably makes sense though to skip SwapCache pages to more closely mimic file I/O behaviour.

We need to think about why real applications (as opposed to benchmarks) use MAP_SHARED. I suspect many of them will modify pages again and again and again. We really want to avoid writing these pages out until the application has truly finished with them.

I think it is probably the case that pages which were dirtied with write(2) are much less likely to be redirtied than pages which were dirtied via MAP_SHARED.

One thing you might like to look at is to give these pages another trip around the LRU after they have been unmapped from pagetables, and to give pdflush a poke. Add instrumentation to record how many pages end up getting written via vmscan's writepage versus via pdflush (use current_is_pdflush()).

mm/vmscan.c renamed from mm/vmscan.c~a +3 -1
diff -puN mm/vmscan.c~a mm/vmscan.c
@@ -318,7 +318,9 @@ shrink_list(struct list_head *page_list,
 				rmap_unlock(page);
 				goto keep_locked;
 			case SWAP_SUCCESS:
-				; /* try to free the page below */
+				if (PageDirty(page))
+					goto keep_locked;
+				break;
 			}
 		}
 		rmap_unlock(page);

_

#9 lore raw
In reply to Andrew Morton
Quoted 14 lines · Andrew Morton
Quoted 13 lines · Nick Piggin
Quoted 11 lines · Andrew Morton
Quoted 7 lines · Shantanu Goel

Presently the kernel does not collection information about the percentage of memory that processes have dirtied via mmap until reclamation. Nothing analogous to balance_dirty_pages() is being done for mmap'ed pages. The attached patch adds collection of dirty page information during kswapd() scans and initiation of background writeback by waking up bdflush.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

And what were the effects of this patch?

I havea modified patch from Nikita that does the if (ptep_test_and_clear_dirty) set_page_dirty from page_referenced, under the page_table_lock.

Dude. I have lots of patches too. The question is: what use are they?

Learning patch-scripts? :)

Quoted 5 lines

In this case, given that we have an actively mapped MAP_SHARED pagecache page, marking it dirty will cause it to be written by pdflush. Even though we're not about to reclaim it, and even though the process which is mapping the page may well modify it again. This patch will cause additional I/O.

Dirty bit is transferred to the struct page when page is moved to the inactive list, where pages are not supposedly referenced/dirtied frequently. Besides, additional IO, if any, will be done through ->writepages() which is much more efficient than single-page pageout from tail of the inactive list.

Quoted 3 lines

So we need to understand why it was written, and what effects were observed, with what workload, and all that good stuff.

Another possible scenario where early transfer of dirty bit could be
useful: huge file consisting of single hole is mmapped, and user level starts dirtying all pages. Current VM thinks happily that all memory is clean, ->writepages() is not invoked. VM scanning starts, shrink_list() dirties page-at-a-time (shrink_list()->try_to_unmap()->set_page_dirty()), and calls ->writepage() that has to insert meta-data for the hole page (extent, indirect pointer, whatever), and to submit IO. As order of pages at the inactive list corresponds to the order of page faults (i.e., random), this will result in the horrible fragmentation.

Nikita.