Skip to content

Commit bb799ca

Browse files
author
Jens Axboe
committed
bio: allow individual slabs in the bio_set
Instead of having a global bio slab cache, add a reference to one in each bio_set that is created. This allows for personalized slabs in each bio_set, so that they can have bios of different sizes. This means we can personalize the bios we return. File systems may want to embed the bio inside another structure, to avoid allocation more items (and stuffing them in ->bi_private) after the get a bio. Or we may want to embed a number of bio_vecs directly at the end of a bio, to avoid doing two allocations to return a bio. This is now possible. Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
1 parent 1b43449 commit bb799ca

6 files changed

Lines changed: 170 additions & 35 deletions

File tree

‎drivers/md/dm-crypt.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
10601060
goto bad_page_pool;
10611061
}
10621062

1063-
cc->bs = bioset_create(MIN_IOS, MIN_IOS);
1063+
cc->bs = bioset_create(MIN_IOS, 0);
10641064
if (!cc->bs) {
10651065
ti->error = "Cannot allocate crypt bioset";
10661066
goto bad_bs;

‎drivers/md/dm-io.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct dm_io_client *dm_io_client_create(unsigned num_pages)
5656
if (!client->pool)
5757
goto bad;
5858

59-
client->bios = bioset_create(16, 16);
59+
client->bios = bioset_create(16, 0);
6060
if (!client->bios)
6161
goto bad;
6262

‎drivers/md/dm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ static struct mapped_device *alloc_dev(int minor)
10931093
if (!md->tio_pool)
10941094
goto bad_tio_pool;
10951095

1096-
md->bs = bioset_create(16, 16);
1096+
md->bs = bioset_create(16, 0);
10971097
if (!md->bs)
10981098
goto bad_no_bioset;
10991099

‎fs/bio-integrity.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void bio_integrity_free(struct bio *bio, struct bio_set *bs)
111111
&& bip->bip_buf != NULL)
112112
kfree(bip->bip_buf);
113113

114-
mempool_free(bip->bip_vec, bs->bvec_pools[bip->bip_pool]);
114+
bvec_free_bs(bs, bip->bip_vec, bip->bip_pool);
115115
mempool_free(bip, bs->bio_integrity_pool);
116116

117117
bio->bi_integrity = NULL;

‎fs/bio.c‎

Lines changed: 161 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@
3131

3232
DEFINE_TRACE(block_split);
3333

34-
static struct kmem_cache *bio_slab __read_mostly;
35-
3634
static mempool_t *bio_split_pool __read_mostly;
3735

3836
/*
3937
* if you change this list, also change bvec_alloc or things will
4038
* break badly! cannot be bigger than what you can fit into an
4139
* unsigned short
4240
*/
43-
4441
#define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
45-
static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
42+
struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
4643
BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
4744
};
4845
#undef BV
@@ -53,11 +50,119 @@ static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
5350
*/
5451
struct bio_set *fs_bio_set;
5552

53+
/*
54+
* Our slab pool management
55+
*/
56+
struct bio_slab {
57+
struct kmem_cache *slab;
58+
unsigned int slab_ref;
59+
unsigned int slab_size;
60+
char name[8];
61+
};
62+
static DEFINE_MUTEX(bio_slab_lock);
63+
static struct bio_slab *bio_slabs;
64+
static unsigned int bio_slab_nr, bio_slab_max;
65+
66+
static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size)
67+
{
68+
unsigned int sz = sizeof(struct bio) + extra_size;
69+
struct kmem_cache *slab = NULL;
70+
struct bio_slab *bslab;
71+
unsigned int i, entry = -1;
72+
73+
mutex_lock(&bio_slab_lock);
74+
75+
i = 0;
76+
while (i < bio_slab_nr) {
77+
struct bio_slab *bslab = &bio_slabs[i];
78+
79+
if (!bslab->slab && entry == -1)
80+
entry = i;
81+
else if (bslab->slab_size == sz) {
82+
slab = bslab->slab;
83+
bslab->slab_ref++;
84+
break;
85+
}
86+
i++;
87+
}
88+
89+
if (slab)
90+
goto out_unlock;
91+
92+
if (bio_slab_nr == bio_slab_max && entry == -1) {
93+
bio_slab_max <<= 1;
94+
bio_slabs = krealloc(bio_slabs,
95+
bio_slab_max * sizeof(struct bio_slab),
96+
GFP_KERNEL);
97+
if (!bio_slabs)
98+
goto out_unlock;
99+
}
100+
if (entry == -1)
101+
entry = bio_slab_nr++;
102+
103+
bslab = &bio_slabs[entry];
104+
105+
snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry);
106+
slab = kmem_cache_create(bslab->name, sz, 0, SLAB_HWCACHE_ALIGN, NULL);
107+
if (!slab)
108+
goto out_unlock;
109+
110+
printk("bio: create slab <%s> at %d\n", bslab->name, entry);
111+
bslab->slab = slab;
112+
bslab->slab_ref = 1;
113+
bslab->slab_size = sz;
114+
out_unlock:
115+
mutex_unlock(&bio_slab_lock);
116+
return slab;
117+
}
118+
119+
static void bio_put_slab(struct bio_set *bs)
120+
{
121+
struct bio_slab *bslab = NULL;
122+
unsigned int i;
123+
124+
mutex_lock(&bio_slab_lock);
125+
126+
for (i = 0; i < bio_slab_nr; i++) {
127+
if (bs->bio_slab == bio_slabs[i].slab) {
128+
bslab = &bio_slabs[i];
129+
break;
130+
}
131+
}
132+
133+
if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
134+
goto out;
135+
136+
WARN_ON(!bslab->slab_ref);
137+
138+
if (--bslab->slab_ref)
139+
goto out;
140+
141+
kmem_cache_destroy(bslab->slab);
142+
bslab->slab = NULL;
143+
144+
out:
145+
mutex_unlock(&bio_slab_lock);
146+
}
147+
56148
unsigned int bvec_nr_vecs(unsigned short idx)
57149
{
58150
return bvec_slabs[idx].nr_vecs;
59151
}
60152

153+
void bvec_free_bs(struct bio_set *bs, struct bio_vec *bv, unsigned int idx)
154+
{
155+
BIO_BUG_ON(idx >= BIOVEC_NR_POOLS);
156+
157+
if (idx == BIOVEC_MAX_IDX)
158+
mempool_free(bv, bs->bvec_pool);
159+
else {
160+
struct biovec_slab *bvs = bvec_slabs + idx;
161+
162+
kmem_cache_free(bvs->slab, bv);
163+
}
164+
}
165+
61166
struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx,
62167
struct bio_set *bs)
63168
{
@@ -134,24 +239,22 @@ struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx,
134239

135240
void bio_free(struct bio *bio, struct bio_set *bs)
136241
{
137-
if (bio->bi_io_vec) {
138-
const int pool_idx = BIO_POOL_IDX(bio);
242+
void *p;
139243

140-
BIO_BUG_ON(pool_idx >= BIOVEC_NR_POOLS);
141-
142-
if (pool_idx == BIOVEC_MAX_IDX)
143-
mempool_free(bio->bi_io_vec, bs->bvec_pool);
144-
else {
145-
struct biovec_slab *bvs = bvec_slabs + pool_idx;
146-
147-
kmem_cache_free(bvs->slab, bio->bi_io_vec);
148-
}
149-
}
244+
if (bio->bi_io_vec)
245+
bvec_free_bs(bs, bio->bi_io_vec, BIO_POOL_IDX(bio));
150246

151247
if (bio_integrity(bio))
152248
bio_integrity_free(bio, bs);
153249

154-
mempool_free(bio, bs->bio_pool);
250+
/*
251+
* If we have front padding, adjust the bio pointer before freeing
252+
*/
253+
p = bio;
254+
if (bs->front_pad)
255+
p -= bs->front_pad;
256+
257+
mempool_free(p, bs->bio_pool);
155258
}
156259

157260
/*
@@ -188,16 +291,20 @@ void bio_init(struct bio *bio)
188291
* for a &struct bio to become free. If a %NULL @bs is passed in, we will
189292
* fall back to just using @kmalloc to allocate the required memory.
190293
*
191-
* allocate bio and iovecs from the memory pools specified by the
192-
* bio_set structure, or @kmalloc if none given.
294+
* Note that the caller must set ->bi_destructor on succesful return
295+
* of a bio, to do the appropriate freeing of the bio once the reference
296+
* count drops to zero.
193297
**/
194298
struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
195299
{
196-
struct bio *bio;
300+
struct bio *bio = NULL;
301+
302+
if (bs) {
303+
void *p = mempool_alloc(bs->bio_pool, gfp_mask);
197304

198-
if (bs)
199-
bio = mempool_alloc(bs->bio_pool, gfp_mask);
200-
else
305+
if (p)
306+
bio = p + bs->front_pad;
307+
} else
201308
bio = kmalloc(sizeof(*bio), gfp_mask);
202309

203310
if (likely(bio)) {
@@ -1398,28 +1505,48 @@ void bioset_free(struct bio_set *bs)
13981505

13991506
bioset_integrity_free(bs);
14001507
biovec_free_pools(bs);
1508+
bio_put_slab(bs);
14011509

14021510
kfree(bs);
14031511
}
14041512

1405-
struct bio_set *bioset_create(int bio_pool_size, int bvec_pool_size)
1513+
/**
1514+
* bioset_create - Create a bio_set
1515+
* @pool_size: Number of bio and bio_vecs to cache in the mempool
1516+
* @front_pad: Number of bytes to allocate in front of the returned bio
1517+
*
1518+
* Description:
1519+
* Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
1520+
* to ask for a number of bytes to be allocated in front of the bio.
1521+
* Front pad allocation is useful for embedding the bio inside
1522+
* another structure, to avoid allocating extra data to go with the bio.
1523+
* Note that the bio must be embedded at the END of that structure always,
1524+
* or things will break badly.
1525+
*/
1526+
struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
14061527
{
14071528
struct bio_set *bs;
14081529

14091530
bs = kzalloc(sizeof(*bs), GFP_KERNEL);
14101531
if (!bs)
14111532
return NULL;
14121533

1413-
bs->bio_slab = bio_slab;
1534+
bs->front_pad = front_pad;
14141535

1415-
bs->bio_pool = mempool_create_slab_pool(bio_pool_size, bs->bio_slab);
1536+
bs->bio_slab = bio_find_or_create_slab(front_pad);
1537+
if (!bs->bio_slab) {
1538+
kfree(bs);
1539+
return NULL;
1540+
}
1541+
1542+
bs->bio_pool = mempool_create_slab_pool(pool_size, bs->bio_slab);
14161543
if (!bs->bio_pool)
14171544
goto bad;
14181545

1419-
if (bioset_integrity_create(bs, bio_pool_size))
1546+
if (bioset_integrity_create(bs, pool_size))
14201547
goto bad;
14211548

1422-
if (!biovec_create_pools(bs, bvec_pool_size))
1549+
if (!biovec_create_pools(bs, pool_size))
14231550
return bs;
14241551

14251552
bad:
@@ -1443,12 +1570,16 @@ static void __init biovec_init_slabs(void)
14431570

14441571
static int __init init_bio(void)
14451572
{
1446-
bio_slab = KMEM_CACHE(bio, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
1573+
bio_slab_max = 2;
1574+
bio_slab_nr = 0;
1575+
bio_slabs = kzalloc(bio_slab_max * sizeof(struct bio_slab), GFP_KERNEL);
1576+
if (!bio_slabs)
1577+
panic("bio: can't allocate bios\n");
14471578

14481579
bio_integrity_init_slab();
14491580
biovec_init_slabs();
14501581

1451-
fs_bio_set = bioset_create(BIO_POOL_SIZE, 2);
1582+
fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);
14521583
if (!fs_bio_set)
14531584
panic("bio: can't allocate bios\n");
14541585

‎include/linux/bio.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ struct bio_pair {
334334
extern struct bio_pair *bio_split(struct bio *bi, int first_sectors);
335335
extern void bio_pair_release(struct bio_pair *dbio);
336336

337-
extern struct bio_set *bioset_create(int, int);
337+
extern struct bio_set *bioset_create(unsigned int, unsigned int);
338338
extern void bioset_free(struct bio_set *);
339339

340340
extern struct bio *bio_alloc(gfp_t, int);
@@ -379,6 +379,7 @@ extern struct bio *bio_copy_user_iov(struct request_queue *,
379379
extern int bio_uncopy_user(struct bio *);
380380
void zero_fill_bio(struct bio *bio);
381381
extern struct bio_vec *bvec_alloc_bs(gfp_t, int, unsigned long *, struct bio_set *);
382+
extern void bvec_free_bs(struct bio_set *, struct bio_vec *, unsigned int);
382383
extern unsigned int bvec_nr_vecs(unsigned short idx);
383384

384385
/*
@@ -401,6 +402,8 @@ static inline void bio_set_completion_cpu(struct bio *bio, unsigned int cpu)
401402

402403
struct bio_set {
403404
struct kmem_cache *bio_slab;
405+
unsigned int front_pad;
406+
404407
mempool_t *bio_pool;
405408
#if defined(CONFIG_BLK_DEV_INTEGRITY)
406409
mempool_t *bio_integrity_pool;
@@ -415,6 +418,7 @@ struct biovec_slab {
415418
};
416419

417420
extern struct bio_set *fs_bio_set;
421+
extern struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly;
418422

419423
/*
420424
* a small number of entries is fine, not going to be performance critical.

0 commit comments

Comments
 (0)