Skip to content

Commit 9c50823

Browse files
Andrew MortonLinus Torvalds
authored andcommitted
[PATCH] msync(): perform dirty page levelling
It seems sensible to perform dirty page throttling in msync: as the application dirties pages we can kick off pdflush early, or even force the msync() caller to perform writeout, or even throttle the msync() caller. The main effect of this is to start disk writeback earlier if we've just discovered that a large amount of pagecache has been dirtied. (Otherwise it wouldn't happen for up to five seconds, next time pdflush wakes up). It also will cause the page-dirtying process to get panalised for dirtying those pages rather than whacking someone else with the problem. We should do this for munmap() and possibly even exit(), too. We drop the mmap_sem while performing the dirty page balancing. It doesn't seem right to hold mmap_sem for that long. Note that this patch only affects MS_ASYNC. MS_SYNC will be syncing all the dirty pages anyway. We note that msync(MS_SYNC) does a full-file-sync inside mmap_sem, and always has. We can fix that up... The patch also tightens up the mmap_sem coverage in sys_msync(): no point in taking it while we perform the incoming arg checking. Cc: Hugh Dickins <hugh@veritas.com> Cc: Nick Piggin <nickpiggin@yahoo.com.au> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 4741c9f commit 9c50823

1 file changed

Lines changed: 61 additions & 32 deletions

File tree

‎mm/msync.c‎

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@
1212
#include <linux/mm.h>
1313
#include <linux/mman.h>
1414
#include <linux/hugetlb.h>
15+
#include <linux/writeback.h>
16+
#include <linux/file.h>
1517
#include <linux/syscalls.h>
1618

1719
#include <asm/pgtable.h>
1820
#include <asm/tlbflush.h>
1921

20-
static void msync_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
22+
static unsigned long msync_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
2123
unsigned long addr, unsigned long end)
2224
{
2325
pte_t *pte;
2426
spinlock_t *ptl;
2527
int progress = 0;
28+
unsigned long ret = 0;
2629

2730
again:
2831
pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
@@ -43,58 +46,64 @@ static void msync_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
4346
if (!page)
4447
continue;
4548
if (ptep_clear_flush_dirty(vma, addr, pte) ||
46-
page_test_and_clear_dirty(page))
47-
set_page_dirty(page);
49+
page_test_and_clear_dirty(page))
50+
ret += set_page_dirty(page);
4851
progress += 3;
4952
} while (pte++, addr += PAGE_SIZE, addr != end);
5053
pte_unmap_unlock(pte - 1, ptl);
5154
cond_resched();
5255
if (addr != end)
5356
goto again;
57+
return ret;
5458
}
5559

56-
static inline void msync_pmd_range(struct vm_area_struct *vma, pud_t *pud,
57-
unsigned long addr, unsigned long end)
60+
static inline unsigned long msync_pmd_range(struct vm_area_struct *vma,
61+
pud_t *pud, unsigned long addr, unsigned long end)
5862
{
5963
pmd_t *pmd;
6064
unsigned long next;
65+
unsigned long ret = 0;
6166

6267
pmd = pmd_offset(pud, addr);
6368
do {
6469
next = pmd_addr_end(addr, end);
6570
if (pmd_none_or_clear_bad(pmd))
6671
continue;
67-
msync_pte_range(vma, pmd, addr, next);
72+
ret += msync_pte_range(vma, pmd, addr, next);
6873
} while (pmd++, addr = next, addr != end);
74+
return ret;
6975
}
7076

71-
static inline void msync_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
72-
unsigned long addr, unsigned long end)
77+
static inline unsigned long msync_pud_range(struct vm_area_struct *vma,
78+
pgd_t *pgd, unsigned long addr, unsigned long end)
7379
{
7480
pud_t *pud;
7581
unsigned long next;
82+
unsigned long ret = 0;
7683

7784
pud = pud_offset(pgd, addr);
7885
do {
7986
next = pud_addr_end(addr, end);
8087
if (pud_none_or_clear_bad(pud))
8188
continue;
82-
msync_pmd_range(vma, pud, addr, next);
89+
ret += msync_pmd_range(vma, pud, addr, next);
8390
} while (pud++, addr = next, addr != end);
91+
return ret;
8492
}
8593

86-
static void msync_page_range(struct vm_area_struct *vma,
94+
static unsigned long msync_page_range(struct vm_area_struct *vma,
8795
unsigned long addr, unsigned long end)
8896
{
8997
pgd_t *pgd;
9098
unsigned long next;
99+
unsigned long ret = 0;
91100

92101
/* For hugepages we can't go walking the page table normally,
93102
* but that's ok, hugetlbfs is memory based, so we don't need
94103
* to do anything more on an msync().
95104
*/
96105
if (vma->vm_flags & VM_HUGETLB)
97-
return;
106+
return 0;
98107

99108
BUG_ON(addr >= end);
100109
pgd = pgd_offset(vma->vm_mm, addr);
@@ -103,8 +112,9 @@ static void msync_page_range(struct vm_area_struct *vma,
103112
next = pgd_addr_end(addr, end);
104113
if (pgd_none_or_clear_bad(pgd))
105114
continue;
106-
msync_pud_range(vma, pgd, addr, next);
115+
ret += msync_pud_range(vma, pgd, addr, next);
107116
} while (pgd++, addr = next, addr != end);
117+
return ret;
108118
}
109119

110120
/*
@@ -118,8 +128,9 @@ static void msync_page_range(struct vm_area_struct *vma,
118128
* So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
119129
* applications.
120130
*/
121-
static int msync_interval(struct vm_area_struct *vma,
122-
unsigned long addr, unsigned long end, int flags)
131+
static int msync_interval(struct vm_area_struct *vma, unsigned long addr,
132+
unsigned long end, int flags,
133+
unsigned long *nr_pages_dirtied)
123134
{
124135
int ret = 0;
125136
struct file *file = vma->vm_file;
@@ -128,7 +139,7 @@ static int msync_interval(struct vm_area_struct *vma,
128139
return -EBUSY;
129140

130141
if (file && (vma->vm_flags & VM_SHARED)) {
131-
msync_page_range(vma, addr, end);
142+
*nr_pages_dirtied = msync_page_range(vma, addr, end);
132143

133144
if (flags & MS_SYNC) {
134145
struct address_space *mapping = file->f_mapping;
@@ -157,11 +168,8 @@ asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
157168
unsigned long end;
158169
struct vm_area_struct *vma;
159170
int unmapped_error, error = -EINVAL;
171+
int done = 0;
160172

161-
if (flags & MS_SYNC)
162-
current->flags |= PF_SYNCWRITE;
163-
164-
down_read(&current->mm->mmap_sem);
165173
if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
166174
goto out;
167175
if (start & ~PAGE_MASK)
@@ -180,13 +188,19 @@ asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
180188
* If the interval [start,end) covers some unmapped address ranges,
181189
* just ignore them, but return -ENOMEM at the end.
182190
*/
191+
down_read(&current->mm->mmap_sem);
192+
if (flags & MS_SYNC)
193+
current->flags |= PF_SYNCWRITE;
183194
vma = find_vma(current->mm, start);
184195
unmapped_error = 0;
185-
for (;;) {
196+
do {
197+
unsigned long nr_pages_dirtied = 0;
198+
struct file *file;
199+
186200
/* Still start < end. */
187201
error = -ENOMEM;
188202
if (!vma)
189-
goto out;
203+
goto out_unlock;
190204
/* Here start < vma->vm_end. */
191205
if (start < vma->vm_start) {
192206
unmapped_error = -ENOMEM;
@@ -195,22 +209,37 @@ asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
195209
/* Here vma->vm_start <= start < vma->vm_end. */
196210
if (end <= vma->vm_end) {
197211
if (start < end) {
198-
error = msync_interval(vma, start, end, flags);
212+
error = msync_interval(vma, start, end, flags,
213+
&nr_pages_dirtied);
199214
if (error)
200-
goto out;
215+
goto out_unlock;
201216
}
202217
error = unmapped_error;
203-
goto out;
218+
done = 1;
219+
} else {
220+
/* Here vma->vm_start <= start < vma->vm_end < end. */
221+
error = msync_interval(vma, start, vma->vm_end, flags,
222+
&nr_pages_dirtied);
223+
if (error)
224+
goto out_unlock;
204225
}
205-
/* Here vma->vm_start <= start < vma->vm_end < end. */
206-
error = msync_interval(vma, start, vma->vm_end, flags);
207-
if (error)
208-
goto out;
226+
file = vma->vm_file;
209227
start = vma->vm_end;
210-
vma = vma->vm_next;
211-
}
212-
out:
213-
up_read(&current->mm->mmap_sem);
228+
if ((flags & MS_ASYNC) && file && nr_pages_dirtied) {
229+
get_file(file);
230+
up_read(&current->mm->mmap_sem);
231+
balance_dirty_pages_ratelimited_nr(file->f_mapping,
232+
nr_pages_dirtied);
233+
fput(file);
234+
down_read(&current->mm->mmap_sem);
235+
vma = find_vma(current->mm, start);
236+
} else {
237+
vma = vma->vm_next;
238+
}
239+
} while (!done);
240+
out_unlock:
214241
current->flags &= ~PF_SYNCWRITE;
242+
up_read(&current->mm->mmap_sem);
243+
out:
215244
return error;
216245
}

0 commit comments

Comments
 (0)