Skip to content

Commit cfd7323

Browse files
Alex Zhuravlevtytso
authored andcommitted
ext4: add prefetching for block allocation bitmaps
This should significantly improve bitmap loading, especially for flex groups as it tries to load all bitmaps within a flex.group instead of one by one synchronously. Prefetching is done in 8 * flex_bg groups, so it should be 8 read-ahead reads for a single allocating thread. At the end of allocation the thread waits for read-ahead completion and initializes buddy information so that read-aheads are not lost in case of memory pressure. At cr=0 the number of prefetching IOs is limited per allocation context to prevent a situation when mballoc loads thousands of bitmaps looking for a perfect group and ignoring groups with good chunks. Together with the patch "ext4: limit scanning of uninitialized groups" the mount time (which includes few tiny allocations) of a 1PB filesystem is reduced significantly: 0% full 50%-full unpatched patched mount time 33s 9279s 563s [ Restructured by tytso; removed the state flags in the allocation context, so it can be used to lazily prefetch the allocation bitmaps immediately after the file system is mounted. Skip prefetching block groups which are uninitialized. Finally pass in the REQ_RAHEAD flag to the block layer while prefetching. ] Signed-off-by: Alex Zhuravlev <bzzz@whamcloud.com> Reviewed-by: Andreas Dilger <adilger@whamcloud.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 273108f commit cfd7323

4 files changed

Lines changed: 153 additions & 6 deletions

File tree

‎fs/ext4/balloc.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ static int ext4_validate_block_bitmap(struct super_block *sb,
413413
* Return buffer_head on success or an ERR_PTR in case of failure.
414414
*/
415415
struct buffer_head *
416-
ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group)
416+
ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group,
417+
bool ignore_locked)
417418
{
418419
struct ext4_group_desc *desc;
419420
struct ext4_sb_info *sbi = EXT4_SB(sb);
@@ -441,6 +442,12 @@ ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group)
441442
return ERR_PTR(-ENOMEM);
442443
}
443444

445+
if (ignore_locked && buffer_locked(bh)) {
446+
/* buffer under IO already, return if called for prefetching */
447+
put_bh(bh);
448+
return NULL;
449+
}
450+
444451
if (bitmap_uptodate(bh))
445452
goto verify;
446453

@@ -490,7 +497,8 @@ ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group)
490497
trace_ext4_read_block_bitmap_load(sb, block_group);
491498
bh->b_end_io = ext4_end_bitmap_read;
492499
get_bh(bh);
493-
submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
500+
submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO |
501+
(ignore_locked ? REQ_RAHEAD : 0), bh);
494502
return bh;
495503
verify:
496504
err = ext4_validate_block_bitmap(sb, desc, block_group, bh);
@@ -534,7 +542,7 @@ ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
534542
struct buffer_head *bh;
535543
int err;
536544

537-
bh = ext4_read_block_bitmap_nowait(sb, block_group);
545+
bh = ext4_read_block_bitmap_nowait(sb, block_group, false);
538546
if (IS_ERR(bh))
539547
return bh;
540548
err = ext4_wait_block_bitmap(sb, block_group, bh);

‎fs/ext4/ext4.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,8 @@ struct ext4_sb_info {
15211521
/* where last allocation was done - for stream allocation */
15221522
unsigned long s_mb_last_group;
15231523
unsigned long s_mb_last_start;
1524+
unsigned int s_mb_prefetch;
1525+
unsigned int s_mb_prefetch_limit;
15241526

15251527
/* stats for buddy allocator */
15261528
atomic_t s_bal_reqs; /* number of reqs with len > 1 */
@@ -2462,7 +2464,8 @@ extern struct ext4_group_desc * ext4_get_group_desc(struct super_block * sb,
24622464
extern int ext4_should_retry_alloc(struct super_block *sb, int *retries);
24632465

24642466
extern struct buffer_head *ext4_read_block_bitmap_nowait(struct super_block *sb,
2465-
ext4_group_t block_group);
2467+
ext4_group_t block_group,
2468+
bool ignore_locked);
24662469
extern int ext4_wait_block_bitmap(struct super_block *sb,
24672470
ext4_group_t block_group,
24682471
struct buffer_head *bh);
@@ -3161,6 +3164,7 @@ struct ext4_group_info {
31613164
(1 << EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT)
31623165
#define EXT4_GROUP_INFO_IBITMAP_CORRUPT \
31633166
(1 << EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT)
3167+
#define EXT4_GROUP_INFO_BBITMAP_READ_BIT 4
31643168

31653169
#define EXT4_MB_GRP_NEED_INIT(grp) \
31663170
(test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
@@ -3175,6 +3179,8 @@ struct ext4_group_info {
31753179
(set_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
31763180
#define EXT4_MB_GRP_CLEAR_TRIMMED(grp) \
31773181
(clear_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
3182+
#define EXT4_MB_GRP_TEST_AND_SET_READ(grp) \
3183+
(test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_READ_BIT, &((grp)->bb_state)))
31783184

31793185
#define EXT4_MAX_CONTENTION 8
31803186
#define EXT4_CONTENTION_THRESHOLD 2

‎fs/ext4/mballoc.c‎

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ static int ext4_mb_init_cache(struct page *page, char *incore, gfp_t gfp)
922922
bh[i] = NULL;
923923
continue;
924924
}
925-
bh[i] = ext4_read_block_bitmap_nowait(sb, group);
925+
bh[i] = ext4_read_block_bitmap_nowait(sb, group, false);
926926
if (IS_ERR(bh[i])) {
927927
err = PTR_ERR(bh[i]);
928928
bh[i] = NULL;
@@ -2209,12 +2209,93 @@ static int ext4_mb_good_group_nolock(struct ext4_allocation_context *ac,
22092209
return ret;
22102210
}
22112211

2212+
/*
2213+
* Start prefetching @nr block bitmaps starting at @group.
2214+
* Return the next group which needs to be prefetched.
2215+
*/
2216+
static ext4_group_t
2217+
ext4_mb_prefetch(struct super_block *sb, ext4_group_t group,
2218+
unsigned int nr, int *cnt)
2219+
{
2220+
ext4_group_t ngroups = ext4_get_groups_count(sb);
2221+
struct buffer_head *bh;
2222+
struct blk_plug plug;
2223+
2224+
blk_start_plug(&plug);
2225+
while (nr-- > 0) {
2226+
struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group,
2227+
NULL);
2228+
struct ext4_group_info *grp = ext4_get_group_info(sb, group);
2229+
2230+
/*
2231+
* Prefetch block groups with free blocks; but don't
2232+
* bother if it is marked uninitialized on disk, since
2233+
* it won't require I/O to read. Also only try to
2234+
* prefetch once, so we avoid getblk() call, which can
2235+
* be expensive.
2236+
*/
2237+
if (!EXT4_MB_GRP_TEST_AND_SET_READ(grp) &&
2238+
EXT4_MB_GRP_NEED_INIT(grp) &&
2239+
ext4_free_group_clusters(sb, gdp) > 0 &&
2240+
!(ext4_has_group_desc_csum(sb) &&
2241+
(gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))) {
2242+
bh = ext4_read_block_bitmap_nowait(sb, group, true);
2243+
if (bh && !IS_ERR(bh)) {
2244+
if (!buffer_uptodate(bh) && cnt)
2245+
(*cnt)++;
2246+
brelse(bh);
2247+
}
2248+
}
2249+
if (++group >= ngroups)
2250+
group = 0;
2251+
}
2252+
blk_finish_plug(&plug);
2253+
return group;
2254+
}
2255+
2256+
/*
2257+
* Prefetching reads the block bitmap into the buffer cache; but we
2258+
* need to make sure that the buddy bitmap in the page cache has been
2259+
* initialized. Note that ext4_mb_init_group() will block if the I/O
2260+
* is not yet completed, or indeed if it was not initiated by
2261+
* ext4_mb_prefetch did not start the I/O.
2262+
*
2263+
* TODO: We should actually kick off the buddy bitmap setup in a work
2264+
* queue when the buffer I/O is completed, so that we don't block
2265+
* waiting for the block allocation bitmap read to finish when
2266+
* ext4_mb_prefetch_fini is called from ext4_mb_regular_allocator().
2267+
*/
2268+
static void
2269+
ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group,
2270+
unsigned int nr)
2271+
{
2272+
while (nr-- > 0) {
2273+
struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group,
2274+
NULL);
2275+
struct ext4_group_info *grp = ext4_get_group_info(sb, group);
2276+
2277+
if (!group)
2278+
group = ext4_get_groups_count(sb);
2279+
group--;
2280+
grp = ext4_get_group_info(sb, group);
2281+
2282+
if (EXT4_MB_GRP_NEED_INIT(grp) &&
2283+
ext4_free_group_clusters(sb, gdp) > 0 &&
2284+
!(ext4_has_group_desc_csum(sb) &&
2285+
(gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))) {
2286+
if (ext4_mb_init_group(sb, group, GFP_NOFS))
2287+
break;
2288+
}
2289+
}
2290+
}
2291+
22122292
static noinline_for_stack int
22132293
ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
22142294
{
2215-
ext4_group_t ngroups, group, i;
2295+
ext4_group_t prefetch_grp = 0, ngroups, group, i;
22162296
int cr = -1;
22172297
int err = 0, first_err = 0;
2298+
unsigned int nr = 0, prefetch_ios = 0;
22182299
struct ext4_sb_info *sbi;
22192300
struct super_block *sb;
22202301
struct ext4_buddy e4b;
@@ -2282,6 +2363,7 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
22822363
* from the goal value specified
22832364
*/
22842365
group = ac->ac_g_ex.fe_group;
2366+
prefetch_grp = group;
22852367

22862368
for (i = 0; i < ngroups; group++, i++) {
22872369
int ret = 0;
@@ -2293,6 +2375,29 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
22932375
if (group >= ngroups)
22942376
group = 0;
22952377

2378+
/*
2379+
* Batch reads of the block allocation bitmaps
2380+
* to get multiple READs in flight; limit
2381+
* prefetching at cr=0/1, otherwise mballoc can
2382+
* spend a lot of time loading imperfect groups
2383+
*/
2384+
if ((prefetch_grp == group) &&
2385+
(cr > 1 ||
2386+
prefetch_ios < sbi->s_mb_prefetch_limit)) {
2387+
unsigned int curr_ios = prefetch_ios;
2388+
2389+
nr = sbi->s_mb_prefetch;
2390+
if (ext4_has_feature_flex_bg(sb)) {
2391+
nr = (group / sbi->s_mb_prefetch) *
2392+
sbi->s_mb_prefetch;
2393+
nr = nr + sbi->s_mb_prefetch - group;
2394+
}
2395+
prefetch_grp = ext4_mb_prefetch(sb, group,
2396+
nr, &prefetch_ios);
2397+
if (prefetch_ios == curr_ios)
2398+
nr = 0;
2399+
}
2400+
22962401
/* This now checks without needing the buddy page */
22972402
ret = ext4_mb_good_group_nolock(ac, group, cr);
22982403
if (ret <= 0) {
@@ -2367,6 +2472,10 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
23672472
mb_debug(sb, "Best len %d, origin len %d, ac_status %u, ac_flags 0x%x, cr %d ret %d\n",
23682473
ac->ac_b_ex.fe_len, ac->ac_o_ex.fe_len, ac->ac_status,
23692474
ac->ac_flags, cr, err);
2475+
2476+
if (nr)
2477+
ext4_mb_prefetch_fini(sb, prefetch_grp, nr);
2478+
23702479
return err;
23712480
}
23722481

@@ -2613,6 +2722,26 @@ static int ext4_mb_init_backend(struct super_block *sb)
26132722
goto err_freebuddy;
26142723
}
26152724

2725+
if (ext4_has_feature_flex_bg(sb)) {
2726+
/* a single flex group is supposed to be read by a single IO */
2727+
sbi->s_mb_prefetch = 1 << sbi->s_es->s_log_groups_per_flex;
2728+
sbi->s_mb_prefetch *= 8; /* 8 prefetch IOs in flight at most */
2729+
} else {
2730+
sbi->s_mb_prefetch = 32;
2731+
}
2732+
if (sbi->s_mb_prefetch > ext4_get_groups_count(sb))
2733+
sbi->s_mb_prefetch = ext4_get_groups_count(sb);
2734+
/* now many real IOs to prefetch within a single allocation at cr=0
2735+
* given cr=0 is an CPU-related optimization we shouldn't try to
2736+
* load too many groups, at some point we should start to use what
2737+
* we've got in memory.
2738+
* with an average random access time 5ms, it'd take a second to get
2739+
* 200 groups (* N with flex_bg), so let's make this limit 4
2740+
*/
2741+
sbi->s_mb_prefetch_limit = sbi->s_mb_prefetch * 4;
2742+
if (sbi->s_mb_prefetch_limit > ext4_get_groups_count(sb))
2743+
sbi->s_mb_prefetch_limit = ext4_get_groups_count(sb);
2744+
26162745
return 0;
26172746

26182747
err_freebuddy:

‎fs/ext4/sysfs.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ EXT4_RO_ATTR_ES_STRING(last_error_func, s_last_error_func, 32);
240240
EXT4_ATTR(first_error_time, 0444, first_error_time);
241241
EXT4_ATTR(last_error_time, 0444, last_error_time);
242242
EXT4_ATTR(journal_task, 0444, journal_task);
243+
EXT4_RW_ATTR_SBI_UI(mb_prefetch, s_mb_prefetch);
244+
EXT4_RW_ATTR_SBI_UI(mb_prefetch_limit, s_mb_prefetch_limit);
243245

244246
static unsigned int old_bump_val = 128;
245247
EXT4_ATTR_PTR(max_writeback_mb_bump, 0444, pointer_ui, &old_bump_val);
@@ -283,6 +285,8 @@ static struct attribute *ext4_attrs[] = {
283285
#ifdef CONFIG_EXT4_DEBUG
284286
ATTR_LIST(simulate_fail),
285287
#endif
288+
ATTR_LIST(mb_prefetch),
289+
ATTR_LIST(mb_prefetch_limit),
286290
NULL,
287291
};
288292
ATTRIBUTE_GROUPS(ext4);

0 commit comments

Comments
 (0)