Skip to content

Commit d618a27

Browse files
Mel GormanJiri Slaby
authored andcommitted
mm: non-atomically mark page accessed during page cache allocation where possible
commit 2457aec upstream. aops->write_begin may allocate a new page and make it visible only to have mark_page_accessed called almost immediately after. Once the page is visible the atomic operations are necessary which is noticable overhead when writing to an in-memory filesystem like tmpfs but should also be noticable with fast storage. The objective of the patch is to initialse the accessed information with non-atomic operations before the page is visible. The bulk of filesystems directly or indirectly use grab_cache_page_write_begin or find_or_create_page for the initial allocation of a page cache page. This patch adds an init_page_accessed() helper which behaves like the first call to mark_page_accessed() but may called before the page is visible and can be done non-atomically. The primary APIs of concern in this care are the following and are used by most filesystems. find_get_page find_lock_page find_or_create_page grab_cache_page_nowait grab_cache_page_write_begin All of them are very similar in detail to the patch creates a core helper pagecache_get_page() which takes a flags parameter that affects its behavior such as whether the page should be marked accessed or not. Then old API is preserved but is basically a thin wrapper around this core function. Each of the filesystems are then updated to avoid calling mark_page_accessed when it is known that the VM interfaces have already done the job. There is a slight snag in that the timing of the mark_page_accessed() has now changed so in rare cases it's possible a page gets to the end of the LRU as PageReferenced where as previously it might have been repromoted. This is expected to be rare but it's worth the filesystem people thinking about it in case they see a problem with the timing change. It is also the case that some filesystems may be marking pages accessed that previously did not but it makes sense that filesystems have consistent behaviour in this regard. The test case used to evaulate this is a simple dd of a large file done multiple times with the file deleted on each iterations. The size of the file is 1/10th physical memory to avoid dirty page balancing. In the async case it will be possible that the workload completes without even hitting the disk and will have variable results but highlight the impact of mark_page_accessed for async IO. The sync results are expected to be more stable. The exception is tmpfs where the normal case is for the "IO" to not hit the disk. The test machine was single socket and UMA to avoid any scheduling or NUMA artifacts. Throughput and wall times are presented for sync IO, only wall times are shown for async as the granularity reported by dd and the variability is unsuitable for comparison. As async results were variable do to writback timings, I'm only reporting the maximum figures. The sync results were stable enough to make the mean and stddev uninteresting. The performance results are reported based on a run with no profiling. Profile data is based on a separate run with oprofile running. async dd 3.15.0-rc3 3.15.0-rc3 vanilla accessed-v2 ext3 Max elapsed 13.9900 ( 0.00%) 11.5900 ( 17.16%) tmpfs Max elapsed 0.5100 ( 0.00%) 0.4900 ( 3.92%) btrfs Max elapsed 12.8100 ( 0.00%) 12.7800 ( 0.23%) ext4 Max elapsed 18.6000 ( 0.00%) 13.3400 ( 28.28%) xfs Max elapsed 12.5600 ( 0.00%) 2.0900 ( 83.36%) The XFS figure is a bit strange as it managed to avoid a worst case by sheer luck but the average figures looked reasonable. samples percentage ext3 86107 0.9783 vmlinux-3.15.0-rc4-vanilla mark_page_accessed ext3 23833 0.2710 vmlinux-3.15.0-rc4-accessed-v3r25 mark_page_accessed ext3 5036 0.0573 vmlinux-3.15.0-rc4-accessed-v3r25 init_page_accessed ext4 64566 0.8961 vmlinux-3.15.0-rc4-vanilla mark_page_accessed ext4 5322 0.0713 vmlinux-3.15.0-rc4-accessed-v3r25 mark_page_accessed ext4 2869 0.0384 vmlinux-3.15.0-rc4-accessed-v3r25 init_page_accessed xfs 62126 1.7675 vmlinux-3.15.0-rc4-vanilla mark_page_accessed xfs 1904 0.0554 vmlinux-3.15.0-rc4-accessed-v3r25 init_page_accessed xfs 103 0.0030 vmlinux-3.15.0-rc4-accessed-v3r25 mark_page_accessed btrfs 10655 0.1338 vmlinux-3.15.0-rc4-vanilla mark_page_accessed btrfs 2020 0.0273 vmlinux-3.15.0-rc4-accessed-v3r25 init_page_accessed btrfs 587 0.0079 vmlinux-3.15.0-rc4-accessed-v3r25 mark_page_accessed tmpfs 59562 3.2628 vmlinux-3.15.0-rc4-vanilla mark_page_accessed tmpfs 1210 0.0696 vmlinux-3.15.0-rc4-accessed-v3r25 init_page_accessed tmpfs 94 0.0054 vmlinux-3.15.0-rc4-accessed-v3r25 mark_page_accessed [akpm@linux-foundation.org: don't run init_page_accessed() against an uninitialised pointer] Signed-off-by: Mel Gorman <mgorman@suse.de> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Jan Kara <jack@suse.cz> Cc: Michal Hocko <mhocko@suse.cz> Cc: Hugh Dickins <hughd@google.com> Cc: Dave Hansen <dave.hansen@intel.com> Cc: Theodore Ts'o <tytso@mit.edu> Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Rik van Riel <riel@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Tested-by: Prabhakar Lad <prabhakar.csengg@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Mel Gorman <mgorman@suse.de> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent 967e642 commit d618a27

17 files changed

Lines changed: 219 additions & 162 deletions

File tree

‎fs/btrfs/extent_io.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4446,7 +4446,8 @@ static void check_buffer_tree_ref(struct extent_buffer *eb)
44464446
spin_unlock(&eb->refs_lock);
44474447
}
44484448

4449-
static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4449+
static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4450+
struct page *accessed)
44504451
{
44514452
unsigned long num_pages, i;
44524453

@@ -4455,7 +4456,8 @@ static void mark_extent_buffer_accessed(struct extent_buffer *eb)
44554456
num_pages = num_extent_pages(eb->start, eb->len);
44564457
for (i = 0; i < num_pages; i++) {
44574458
struct page *p = extent_buffer_page(eb, i);
4458-
mark_page_accessed(p);
4459+
if (p != accessed)
4460+
mark_page_accessed(p);
44594461
}
44604462
}
44614463

@@ -4476,7 +4478,7 @@ struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
44764478
eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
44774479
if (eb && atomic_inc_not_zero(&eb->refs)) {
44784480
rcu_read_unlock();
4479-
mark_extent_buffer_accessed(eb);
4481+
mark_extent_buffer_accessed(eb, NULL);
44804482
return eb;
44814483
}
44824484
rcu_read_unlock();
@@ -4504,7 +4506,7 @@ struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
45044506
spin_unlock(&mapping->private_lock);
45054507
unlock_page(p);
45064508
page_cache_release(p);
4507-
mark_extent_buffer_accessed(exists);
4509+
mark_extent_buffer_accessed(exists, p);
45084510
goto free_eb;
45094511
}
45104512

@@ -4519,7 +4521,6 @@ struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
45194521
attach_extent_buffer_page(eb, p);
45204522
spin_unlock(&mapping->private_lock);
45214523
WARN_ON(PageDirty(p));
4522-
mark_page_accessed(p);
45234524
eb->pages[i] = p;
45244525
if (!PageUptodate(p))
45254526
uptodate = 0;
@@ -4549,7 +4550,7 @@ struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
45494550
}
45504551
spin_unlock(&tree->buffer_lock);
45514552
radix_tree_preload_end();
4552-
mark_extent_buffer_accessed(exists);
4553+
mark_extent_buffer_accessed(exists, NULL);
45534554
goto free_eb;
45544555
}
45554556
/* add one reference for the tree */
@@ -4595,7 +4596,7 @@ struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
45954596
eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
45964597
if (eb && atomic_inc_not_zero(&eb->refs)) {
45974598
rcu_read_unlock();
4598-
mark_extent_buffer_accessed(eb);
4599+
mark_extent_buffer_accessed(eb, NULL);
45994600
return eb;
46004601
}
46014602
rcu_read_unlock();

‎fs/btrfs/file.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,12 @@ static void btrfs_drop_pages(struct page **pages, size_t num_pages)
471471
for (i = 0; i < num_pages; i++) {
472472
/* page checked is some magic around finding pages that
473473
* have been modified without going through btrfs_set_page_dirty
474-
* clear it here
474+
* clear it here. There should be no need to mark the pages
475+
* accessed as prepare_pages should have marked them accessed
476+
* in prepare_pages via find_or_create_page()
475477
*/
476478
ClearPageChecked(pages[i]);
477479
unlock_page(pages[i]);
478-
mark_page_accessed(pages[i]);
479480
page_cache_release(pages[i]);
480481
}
481482
}

‎fs/buffer.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ __find_get_block_slow(struct block_device *bdev, sector_t block)
227227
int all_mapped = 1;
228228

229229
index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
230-
page = find_get_page(bd_mapping, index);
230+
page = find_get_page_flags(bd_mapping, index, FGP_ACCESSED);
231231
if (!page)
232232
goto out;
233233

@@ -1366,12 +1366,13 @@ __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
13661366
struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
13671367

13681368
if (bh == NULL) {
1369+
/* __find_get_block_slow will mark the page accessed */
13691370
bh = __find_get_block_slow(bdev, block);
13701371
if (bh)
13711372
bh_lru_install(bh);
1372-
}
1373-
if (bh)
1373+
} else
13741374
touch_buffer(bh);
1375+
13751376
return bh;
13761377
}
13771378
EXPORT_SYMBOL(__find_get_block);

‎fs/ext4/mballoc.c‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,8 @@ int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
10441044
* allocating. If we are looking at the buddy cache we would
10451045
* have taken a reference using ext4_mb_load_buddy and that
10461046
* would have pinned buddy page to page cache.
1047+
* The call to ext4_mb_get_buddy_page_lock will mark the
1048+
* page accessed.
10471049
*/
10481050
ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b);
10491051
if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
@@ -1062,7 +1064,6 @@ int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
10621064
ret = -EIO;
10631065
goto err;
10641066
}
1065-
mark_page_accessed(page);
10661067

10671068
if (e4b.bd_buddy_page == NULL) {
10681069
/*
@@ -1082,7 +1083,6 @@ int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
10821083
ret = -EIO;
10831084
goto err;
10841085
}
1085-
mark_page_accessed(page);
10861086
err:
10871087
ext4_mb_put_buddy_page_lock(&e4b);
10881088
return ret;
@@ -1141,7 +1141,7 @@ ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
11411141

11421142
/* we could use find_or_create_page(), but it locks page
11431143
* what we'd like to avoid in fast path ... */
1144-
page = find_get_page(inode->i_mapping, pnum);
1144+
page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
11451145
if (page == NULL || !PageUptodate(page)) {
11461146
if (page)
11471147
/*
@@ -1172,15 +1172,16 @@ ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
11721172
ret = -EIO;
11731173
goto err;
11741174
}
1175+
1176+
/* Pages marked accessed already */
11751177
e4b->bd_bitmap_page = page;
11761178
e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1177-
mark_page_accessed(page);
11781179

11791180
block++;
11801181
pnum = block / blocks_per_page;
11811182
poff = block % blocks_per_page;
11821183

1183-
page = find_get_page(inode->i_mapping, pnum);
1184+
page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
11841185
if (page == NULL || !PageUptodate(page)) {
11851186
if (page)
11861187
page_cache_release(page);
@@ -1201,9 +1202,10 @@ ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
12011202
ret = -EIO;
12021203
goto err;
12031204
}
1205+
1206+
/* Pages marked accessed already */
12041207
e4b->bd_buddy_page = page;
12051208
e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1206-
mark_page_accessed(page);
12071209

12081210
BUG_ON(e4b->bd_bitmap_page == NULL);
12091211
BUG_ON(e4b->bd_buddy_page == NULL);

‎fs/f2fs/checkpoint.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
7070
goto repeat;
7171
}
7272
out:
73-
mark_page_accessed(page);
7473
return page;
7574
}
7675

‎fs/f2fs/node.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,6 @@ struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
970970
}
971971
got_it:
972972
BUG_ON(nid != nid_of_node(page));
973-
mark_page_accessed(page);
974973
return page;
975974
}
976975

@@ -1026,7 +1025,6 @@ struct page *get_node_page_ra(struct page *parent, int start)
10261025
f2fs_put_page(page, 1);
10271026
return ERR_PTR(-EIO);
10281027
}
1029-
mark_page_accessed(page);
10301028
return page;
10311029
}
10321030

‎fs/fuse/file.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,6 @@ static ssize_t fuse_fill_write_pages(struct fuse_req *req,
988988
tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
989989
flush_dcache_page(page);
990990

991-
mark_page_accessed(page);
992-
993991
if (!tmp) {
994992
unlock_page(page);
995993
page_cache_release(page);

‎fs/gfs2/aops.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,6 @@ int gfs2_internal_read(struct gfs2_inode *ip, char *buf, loff_t *pos,
517517
p = kmap_atomic(page);
518518
memcpy(buf + copied, p + offset, amt);
519519
kunmap_atomic(p);
520-
mark_page_accessed(page);
521520
page_cache_release(page);
522521
copied += amt;
523522
index++;

‎fs/gfs2/meta_io.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ struct buffer_head *gfs2_getbuf(struct gfs2_glock *gl, u64 blkno, int create)
128128
yield();
129129
}
130130
} else {
131-
page = find_lock_page(mapping, index);
131+
page = find_get_page_flags(mapping, index,
132+
FGP_LOCK|FGP_ACCESSED);
132133
if (!page)
133134
return NULL;
134135
}
@@ -145,7 +146,6 @@ struct buffer_head *gfs2_getbuf(struct gfs2_glock *gl, u64 blkno, int create)
145146
map_bh(bh, sdp->sd_vfs, blkno);
146147

147148
unlock_page(page);
148-
mark_page_accessed(page);
149149
page_cache_release(page);
150150

151151
return bh;

‎fs/ntfs/attrib.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,6 @@ int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size)
17481748
if (page) {
17491749
set_page_dirty(page);
17501750
unlock_page(page);
1751-
mark_page_accessed(page);
17521751
page_cache_release(page);
17531752
}
17541753
ntfs_debug("Done.");

0 commit comments

Comments
 (0)