Skip to content

Commit b85e0ef

Browse files
Hugh Dickinstorvalds
authored andcommitted
mm: consistent truncate and invalidate loops
Make the pagevec_lookup loops in truncate_inode_pages_range(), invalidate_mapping_pages() and invalidate_inode_pages2_range() more consistent with each other. They were relying upon page->index of an unlocked page, but apologizing for it: accept it, embrace it, add comments and WARN_ONs, and simplify the index handling. invalidate_inode_pages2_range() had special handling for a wrapped page->index + 1 = 0 case; but MAX_LFS_FILESIZE doesn't let us anywhere near there, and a corrupt page->index in the radix_tree could cause more trouble than that would catch. Remove that wrapped handling. invalidate_inode_pages2_range() uses min() to limit the pagevec_lookup when near the end of the range: copy that into the other two, although it's less useful than you might think (it limits the use of the buffer, rather than the indices looked up). Signed-off-by: Hugh Dickins <hughd@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8a549be commit b85e0ef

2 files changed

Lines changed: 49 additions & 63 deletions

File tree

‎mm/filemap.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ void __delete_from_page_cache(struct page *page)
128128

129129
radix_tree_delete(&mapping->page_tree, page->index);
130130
page->mapping = NULL;
131+
/* Leave page->index set: truncation lookup relies upon it */
131132
mapping->nrpages--;
132133
__dec_zone_page_state(page, NR_FILE_PAGES);
133134
if (PageSwapBacked(page))
@@ -483,6 +484,7 @@ int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
483484
spin_unlock_irq(&mapping->tree_lock);
484485
} else {
485486
page->mapping = NULL;
487+
/* Leave page->index set: truncation relies upon it */
486488
spin_unlock_irq(&mapping->tree_lock);
487489
mem_cgroup_uncharge_cache_page(page);
488490
page_cache_release(page);

‎mm/truncate.c‎

Lines changed: 47 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,6 @@ int invalidate_inode_page(struct page *page)
199199
* The first pass will remove most pages, so the search cost of the second pass
200200
* is low.
201201
*
202-
* When looking at page->index outside the page lock we need to be careful to
203-
* copy it into a local to avoid races (it could change at any time).
204-
*
205202
* We pass down the cache-hot hint to the page freeing code. Even if the
206203
* mapping is large, it is probably the case that the final pages are the most
207204
* recently touched, and freeing happens in ascending file offset order.
@@ -210,10 +207,10 @@ void truncate_inode_pages_range(struct address_space *mapping,
210207
loff_t lstart, loff_t lend)
211208
{
212209
const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
213-
pgoff_t end;
214210
const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
215211
struct pagevec pvec;
216-
pgoff_t next;
212+
pgoff_t index;
213+
pgoff_t end;
217214
int i;
218215

219216
cleancache_flush_inode(mapping);
@@ -224,24 +221,21 @@ void truncate_inode_pages_range(struct address_space *mapping,
224221
end = (lend >> PAGE_CACHE_SHIFT);
225222

226223
pagevec_init(&pvec, 0);
227-
next = start;
228-
while (next <= end &&
229-
pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
224+
index = start;
225+
while (index <= end && pagevec_lookup(&pvec, mapping, index,
226+
min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
230227
mem_cgroup_uncharge_start();
231228
for (i = 0; i < pagevec_count(&pvec); i++) {
232229
struct page *page = pvec.pages[i];
233-
pgoff_t page_index = page->index;
234230

235-
if (page_index > end) {
236-
next = page_index;
231+
/* We rely upon deletion not changing page->index */
232+
index = page->index;
233+
if (index > end)
237234
break;
238-
}
239235

240-
if (page_index > next)
241-
next = page_index;
242-
next++;
243236
if (!trylock_page(page))
244237
continue;
238+
WARN_ON(page->index != index);
245239
if (PageWriteback(page)) {
246240
unlock_page(page);
247241
continue;
@@ -252,6 +246,7 @@ void truncate_inode_pages_range(struct address_space *mapping,
252246
pagevec_release(&pvec);
253247
mem_cgroup_uncharge_end();
254248
cond_resched();
249+
index++;
255250
}
256251

257252
if (partial) {
@@ -264,13 +259,14 @@ void truncate_inode_pages_range(struct address_space *mapping,
264259
}
265260
}
266261

267-
next = start;
262+
index = start;
268263
for ( ; ; ) {
269264
cond_resched();
270-
if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
271-
if (next == start)
265+
if (!pagevec_lookup(&pvec, mapping, index,
266+
min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
267+
if (index == start)
272268
break;
273-
next = start;
269+
index = start;
274270
continue;
275271
}
276272
if (pvec.pages[0]->index > end) {
@@ -281,18 +277,20 @@ void truncate_inode_pages_range(struct address_space *mapping,
281277
for (i = 0; i < pagevec_count(&pvec); i++) {
282278
struct page *page = pvec.pages[i];
283279

284-
if (page->index > end)
280+
/* We rely upon deletion not changing page->index */
281+
index = page->index;
282+
if (index > end)
285283
break;
284+
286285
lock_page(page);
286+
WARN_ON(page->index != index);
287287
wait_on_page_writeback(page);
288288
truncate_inode_page(mapping, page);
289-
if (page->index > next)
290-
next = page->index;
291-
next++;
292289
unlock_page(page);
293290
}
294291
pagevec_release(&pvec);
295292
mem_cgroup_uncharge_end();
293+
index++;
296294
}
297295
cleancache_flush_inode(mapping);
298296
}
@@ -333,35 +331,26 @@ unsigned long invalidate_mapping_pages(struct address_space *mapping,
333331
pgoff_t start, pgoff_t end)
334332
{
335333
struct pagevec pvec;
336-
pgoff_t next = start;
334+
pgoff_t index = start;
337335
unsigned long ret;
338336
unsigned long count = 0;
339337
int i;
340338

341339
pagevec_init(&pvec, 0);
342-
while (next <= end &&
343-
pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
340+
while (index <= end && pagevec_lookup(&pvec, mapping, index,
341+
min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
344342
mem_cgroup_uncharge_start();
345343
for (i = 0; i < pagevec_count(&pvec); i++) {
346344
struct page *page = pvec.pages[i];
347-
pgoff_t index;
348-
int lock_failed;
349-
350-
lock_failed = !trylock_page(page);
351345

352-
/*
353-
* We really shouldn't be looking at the ->index of an
354-
* unlocked page. But we're not allowed to lock these
355-
* pages. So we rely upon nobody altering the ->index
356-
* of this (pinned-by-us) page.
357-
*/
346+
/* We rely upon deletion not changing page->index */
358347
index = page->index;
359-
if (index > next)
360-
next = index;
361-
next++;
362-
if (lock_failed)
363-
continue;
348+
if (index > end)
349+
break;
364350

351+
if (!trylock_page(page))
352+
continue;
353+
WARN_ON(page->index != index);
365354
ret = invalidate_inode_page(page);
366355
unlock_page(page);
367356
/*
@@ -371,12 +360,11 @@ unsigned long invalidate_mapping_pages(struct address_space *mapping,
371360
if (!ret)
372361
deactivate_page(page);
373362
count += ret;
374-
if (next > end)
375-
break;
376363
}
377364
pagevec_release(&pvec);
378365
mem_cgroup_uncharge_end();
379366
cond_resched();
367+
index++;
380368
}
381369
return count;
382370
}
@@ -442,56 +430,51 @@ int invalidate_inode_pages2_range(struct address_space *mapping,
442430
pgoff_t start, pgoff_t end)
443431
{
444432
struct pagevec pvec;
445-
pgoff_t next;
433+
pgoff_t index;
446434
int i;
447435
int ret = 0;
448436
int ret2 = 0;
449437
int did_range_unmap = 0;
450-
int wrapped = 0;
451438

452439
cleancache_flush_inode(mapping);
453440
pagevec_init(&pvec, 0);
454-
next = start;
455-
while (next <= end && !wrapped &&
456-
pagevec_lookup(&pvec, mapping, next,
457-
min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
441+
index = start;
442+
while (index <= end && pagevec_lookup(&pvec, mapping, index,
443+
min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
458444
mem_cgroup_uncharge_start();
459445
for (i = 0; i < pagevec_count(&pvec); i++) {
460446
struct page *page = pvec.pages[i];
461-
pgoff_t page_index;
447+
448+
/* We rely upon deletion not changing page->index */
449+
index = page->index;
450+
if (index > end)
451+
break;
462452

463453
lock_page(page);
454+
WARN_ON(page->index != index);
464455
if (page->mapping != mapping) {
465456
unlock_page(page);
466457
continue;
467458
}
468-
page_index = page->index;
469-
next = page_index + 1;
470-
if (next == 0)
471-
wrapped = 1;
472-
if (page_index > end) {
473-
unlock_page(page);
474-
break;
475-
}
476459
wait_on_page_writeback(page);
477460
if (page_mapped(page)) {
478461
if (!did_range_unmap) {
479462
/*
480463
* Zap the rest of the file in one hit.
481464
*/
482465
unmap_mapping_range(mapping,
483-
(loff_t)page_index<<PAGE_CACHE_SHIFT,
484-
(loff_t)(end - page_index + 1)
485-
<< PAGE_CACHE_SHIFT,
466+
(loff_t)index << PAGE_CACHE_SHIFT,
467+
(loff_t)(1 + end - index)
468+
<< PAGE_CACHE_SHIFT,
486469
0);
487470
did_range_unmap = 1;
488471
} else {
489472
/*
490473
* Just zap this page
491474
*/
492475
unmap_mapping_range(mapping,
493-
(loff_t)page_index<<PAGE_CACHE_SHIFT,
494-
PAGE_CACHE_SIZE, 0);
476+
(loff_t)index << PAGE_CACHE_SHIFT,
477+
PAGE_CACHE_SIZE, 0);
495478
}
496479
}
497480
BUG_ON(page_mapped(page));
@@ -507,6 +490,7 @@ int invalidate_inode_pages2_range(struct address_space *mapping,
507490
pagevec_release(&pvec);
508491
mem_cgroup_uncharge_end();
509492
cond_resched();
493+
index++;
510494
}
511495
cleancache_flush_inode(mapping);
512496
return ret;

0 commit comments

Comments
 (0)