block: Create bip slabs with embedded integrity vectors

This patch restores stacking ability to the block layer integrity
infrastructure by creating a set of dedicated bip slabs.  Each bip slab
has an embedded bio_vec array at the end.  This cuts down on memory
allocations and also simplifies the code compared to the original bvec
version.  Only the largest bip slab is backed by a mempool.  The pool is
contained in the bio_set so stacking drivers can ensure forward
progress.

Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Jens Axboe <axboe@carl.(none)>
This commit is contained in:
Martin K. Petersen 2009-06-26 15:37:49 +02:00 committed by Jens Axboe
parent 6118b70b3a
commit 7878cba9f0
5 changed files with 162 additions and 67 deletions

View file

@ -2365,7 +2365,7 @@ int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
__bio_clone(bio, bio_src); __bio_clone(bio, bio_src);
if (bio_integrity(bio_src) && if (bio_integrity(bio_src) &&
bio_integrity_clone(bio, bio_src, gfp_mask)) bio_integrity_clone(bio, bio_src, gfp_mask, bs))
goto free_and_out; goto free_and_out;
if (bio_ctr && bio_ctr(bio, bio_src, data)) if (bio_ctr && bio_ctr(bio, bio_src, data))

View file

@ -1017,7 +1017,7 @@ static struct bio *split_bvec(struct bio *bio, sector_t sector,
clone->bi_flags |= 1 << BIO_CLONED; clone->bi_flags |= 1 << BIO_CLONED;
if (bio_integrity(bio)) { if (bio_integrity(bio)) {
bio_integrity_clone(clone, bio, GFP_NOIO); bio_integrity_clone(clone, bio, GFP_NOIO, bs);
bio_integrity_trim(clone, bio_integrity_trim(clone,
bio_sector_offset(bio, idx, offset), len); bio_sector_offset(bio, idx, offset), len);
} }
@ -1045,7 +1045,7 @@ static struct bio *clone_bio(struct bio *bio, sector_t sector,
clone->bi_flags &= ~(1 << BIO_SEG_VALID); clone->bi_flags &= ~(1 << BIO_SEG_VALID);
if (bio_integrity(bio)) { if (bio_integrity(bio)) {
bio_integrity_clone(clone, bio, GFP_NOIO); bio_integrity_clone(clone, bio, GFP_NOIO, bs);
if (idx != bio->bi_idx || clone->bi_size < bio->bi_size) if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
bio_integrity_trim(clone, bio_integrity_trim(clone,

View file

@ -1,7 +1,7 @@
/* /*
* bio-integrity.c - bio data integrity extensions * bio-integrity.c - bio data integrity extensions
* *
* Copyright (C) 2007, 2008 Oracle Corporation * Copyright (C) 2007, 2008, 2009 Oracle Corporation
* Written by: Martin K. Petersen <martin.petersen@oracle.com> * Written by: Martin K. Petersen <martin.petersen@oracle.com>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -25,11 +25,94 @@
#include <linux/bio.h> #include <linux/bio.h>
#include <linux/workqueue.h> #include <linux/workqueue.h>
static struct kmem_cache *bio_integrity_slab __read_mostly; struct integrity_slab {
static mempool_t *bio_integrity_pool; struct kmem_cache *slab;
static struct bio_set *integrity_bio_set; unsigned short nr_vecs;
char name[8];
};
#define IS(x) { .nr_vecs = x, .name = "bip-"__stringify(x) }
struct integrity_slab bip_slab[BIOVEC_NR_POOLS] __read_mostly = {
IS(1), IS(4), IS(16), IS(64), IS(128), IS(BIO_MAX_PAGES),
};
#undef IS
static struct workqueue_struct *kintegrityd_wq; static struct workqueue_struct *kintegrityd_wq;
static inline unsigned int vecs_to_idx(unsigned int nr)
{
switch (nr) {
case 1:
return 0;
case 2 ... 4:
return 1;
case 5 ... 16:
return 2;
case 17 ... 64:
return 3;
case 65 ... 128:
return 4;
case 129 ... BIO_MAX_PAGES:
return 5;
default:
BUG();
}
}
static inline int use_bip_pool(unsigned int idx)
{
if (idx == BIOVEC_NR_POOLS)
return 1;
return 0;
}
/**
* bio_integrity_alloc_bioset - Allocate integrity payload and attach it to bio
* @bio: bio to attach integrity metadata to
* @gfp_mask: Memory allocation mask
* @nr_vecs: Number of integrity metadata scatter-gather elements
* @bs: bio_set to allocate from
*
* Description: This function prepares a bio for attaching integrity
* metadata. nr_vecs specifies the maximum number of pages containing
* integrity metadata that can be attached.
*/
struct bio_integrity_payload *bio_integrity_alloc_bioset(struct bio *bio,
gfp_t gfp_mask,
unsigned int nr_vecs,
struct bio_set *bs)
{
struct bio_integrity_payload *bip;
unsigned int idx = vecs_to_idx(nr_vecs);
BUG_ON(bio == NULL);
bip = NULL;
/* Lower order allocations come straight from slab */
if (!use_bip_pool(idx))
bip = kmem_cache_alloc(bip_slab[idx].slab, gfp_mask);
/* Use mempool if lower order alloc failed or max vecs were requested */
if (bip == NULL) {
bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask);
if (unlikely(bip == NULL)) {
printk(KERN_ERR "%s: could not alloc bip\n", __func__);
return NULL;
}
}
memset(bip, 0, sizeof(*bip));
bip->bip_slab = idx;
bip->bip_bio = bio;
bio->bi_integrity = bip;
return bip;
}
EXPORT_SYMBOL(bio_integrity_alloc_bioset);
/** /**
* bio_integrity_alloc - Allocate integrity payload and attach it to bio * bio_integrity_alloc - Allocate integrity payload and attach it to bio
* @bio: bio to attach integrity metadata to * @bio: bio to attach integrity metadata to
@ -44,44 +127,19 @@ struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
gfp_t gfp_mask, gfp_t gfp_mask,
unsigned int nr_vecs) unsigned int nr_vecs)
{ {
struct bio_integrity_payload *bip; return bio_integrity_alloc_bioset(bio, gfp_mask, nr_vecs, fs_bio_set);
struct bio_vec *iv;
unsigned long idx;
BUG_ON(bio == NULL);
bip = mempool_alloc(bio_integrity_pool, gfp_mask);
if (unlikely(bip == NULL)) {
printk(KERN_ERR "%s: could not alloc bip\n", __func__);
return NULL;
}
memset(bip, 0, sizeof(*bip));
iv = bvec_alloc_bs(gfp_mask, nr_vecs, &idx, integrity_bio_set);
if (unlikely(iv == NULL)) {
printk(KERN_ERR "%s: could not alloc bip_vec\n", __func__);
mempool_free(bip, bio_integrity_pool);
return NULL;
}
bip->bip_pool = idx;
bip->bip_vec = iv;
bip->bip_bio = bio;
bio->bi_integrity = bip;
return bip;
} }
EXPORT_SYMBOL(bio_integrity_alloc); EXPORT_SYMBOL(bio_integrity_alloc);
/** /**
* bio_integrity_free - Free bio integrity payload * bio_integrity_free - Free bio integrity payload
* @bio: bio containing bip to be freed * @bio: bio containing bip to be freed
* @bs: bio_set this bio was allocated from
* *
* Description: Used to free the integrity portion of a bio. Usually * Description: Used to free the integrity portion of a bio. Usually
* called from bio_free(). * called from bio_free().
*/ */
void bio_integrity_free(struct bio *bio) void bio_integrity_free(struct bio *bio, struct bio_set *bs)
{ {
struct bio_integrity_payload *bip = bio->bi_integrity; struct bio_integrity_payload *bip = bio->bi_integrity;
@ -92,8 +150,10 @@ void bio_integrity_free(struct bio *bio)
&& bip->bip_buf != NULL) && bip->bip_buf != NULL)
kfree(bip->bip_buf); kfree(bip->bip_buf);
bvec_free_bs(integrity_bio_set, bip->bip_vec, bip->bip_pool); if (use_bip_pool(bip->bip_slab))
mempool_free(bip, bio_integrity_pool); mempool_free(bip, bs->bio_integrity_pool);
else
kmem_cache_free(bip_slab[bip->bip_slab].slab, bip);
bio->bi_integrity = NULL; bio->bi_integrity = NULL;
} }
@ -114,7 +174,7 @@ int bio_integrity_add_page(struct bio *bio, struct page *page,
struct bio_integrity_payload *bip = bio->bi_integrity; struct bio_integrity_payload *bip = bio->bi_integrity;
struct bio_vec *iv; struct bio_vec *iv;
if (bip->bip_vcnt >= bvec_nr_vecs(bip->bip_pool)) { if (bip->bip_vcnt >= bvec_nr_vecs(bip->bip_slab)) {
printk(KERN_ERR "%s: bip_vec full\n", __func__); printk(KERN_ERR "%s: bip_vec full\n", __func__);
return 0; return 0;
} }
@ -647,8 +707,8 @@ void bio_integrity_split(struct bio *bio, struct bio_pair *bp, int sectors)
bp->iv1 = bip->bip_vec[0]; bp->iv1 = bip->bip_vec[0];
bp->iv2 = bip->bip_vec[0]; bp->iv2 = bip->bip_vec[0];
bp->bip1.bip_vec = &bp->iv1; bp->bip1.bip_vec[0] = bp->iv1;
bp->bip2.bip_vec = &bp->iv2; bp->bip2.bip_vec[0] = bp->iv2;
bp->iv1.bv_len = sectors * bi->tuple_size; bp->iv1.bv_len = sectors * bi->tuple_size;
bp->iv2.bv_offset += sectors * bi->tuple_size; bp->iv2.bv_offset += sectors * bi->tuple_size;
@ -667,17 +727,19 @@ EXPORT_SYMBOL(bio_integrity_split);
* @bio: New bio * @bio: New bio
* @bio_src: Original bio * @bio_src: Original bio
* @gfp_mask: Memory allocation mask * @gfp_mask: Memory allocation mask
* @bs: bio_set to allocate bip from
* *
* Description: Called to allocate a bip when cloning a bio * Description: Called to allocate a bip when cloning a bio
*/ */
int bio_integrity_clone(struct bio *bio, struct bio *bio_src, gfp_t gfp_mask) int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
gfp_t gfp_mask, struct bio_set *bs)
{ {
struct bio_integrity_payload *bip_src = bio_src->bi_integrity; struct bio_integrity_payload *bip_src = bio_src->bi_integrity;
struct bio_integrity_payload *bip; struct bio_integrity_payload *bip;
BUG_ON(bip_src == NULL); BUG_ON(bip_src == NULL);
bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt); bip = bio_integrity_alloc_bioset(bio, gfp_mask, bip_src->bip_vcnt, bs);
if (bip == NULL) if (bip == NULL)
return -EIO; return -EIO;
@ -693,25 +755,43 @@ int bio_integrity_clone(struct bio *bio, struct bio *bio_src, gfp_t gfp_mask)
} }
EXPORT_SYMBOL(bio_integrity_clone); EXPORT_SYMBOL(bio_integrity_clone);
static int __init bio_integrity_init(void) int bioset_integrity_create(struct bio_set *bs, int pool_size)
{ {
kintegrityd_wq = create_workqueue("kintegrityd"); unsigned int max_slab = vecs_to_idx(BIO_MAX_PAGES);
if (!kintegrityd_wq) bs->bio_integrity_pool =
panic("Failed to create kintegrityd\n"); mempool_create_slab_pool(pool_size, bip_slab[max_slab].slab);
bio_integrity_slab = KMEM_CACHE(bio_integrity_payload, if (!bs->bio_integrity_pool)
SLAB_HWCACHE_ALIGN|SLAB_PANIC); return -1;
bio_integrity_pool = mempool_create_slab_pool(BIO_POOL_SIZE,
bio_integrity_slab);
if (!bio_integrity_pool)
panic("bio_integrity: can't allocate bip pool\n");
integrity_bio_set = bioset_create(BIO_POOL_SIZE, 0);
if (!integrity_bio_set)
panic("bio_integrity: can't allocate bio_set\n");
return 0; return 0;
} }
subsys_initcall(bio_integrity_init); EXPORT_SYMBOL(bioset_integrity_create);
void bioset_integrity_free(struct bio_set *bs)
{
if (bs->bio_integrity_pool)
mempool_destroy(bs->bio_integrity_pool);
}
EXPORT_SYMBOL(bioset_integrity_free);
void __init bio_integrity_init(void)
{
unsigned int i;
kintegrityd_wq = create_workqueue("kintegrityd");
if (!kintegrityd_wq)
panic("Failed to create kintegrityd\n");
for (i = 0 ; i < BIOVEC_NR_POOLS ; i++) {
unsigned int size;
size = sizeof(struct bio_integrity_payload)
+ bip_slab[i].nr_vecs * sizeof(struct bio_vec);
bip_slab[i].slab =
kmem_cache_create(bip_slab[i].name, size, 0,
SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
}
}

View file

@ -238,7 +238,7 @@ void bio_free(struct bio *bio, struct bio_set *bs)
bvec_free_bs(bs, bio->bi_io_vec, BIO_POOL_IDX(bio)); bvec_free_bs(bs, bio->bi_io_vec, BIO_POOL_IDX(bio));
if (bio_integrity(bio)) if (bio_integrity(bio))
bio_integrity_free(bio); bio_integrity_free(bio, bs);
/* /*
* If we have front padding, adjust the bio pointer before freeing * If we have front padding, adjust the bio pointer before freeing
@ -341,7 +341,7 @@ struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs)
static void bio_kmalloc_destructor(struct bio *bio) static void bio_kmalloc_destructor(struct bio *bio)
{ {
if (bio_integrity(bio)) if (bio_integrity(bio))
bio_integrity_free(bio); bio_integrity_free(bio, fs_bio_set);
kfree(bio); kfree(bio);
} }
@ -472,7 +472,7 @@ struct bio *bio_clone(struct bio *bio, gfp_t gfp_mask)
if (bio_integrity(bio)) { if (bio_integrity(bio)) {
int ret; int ret;
ret = bio_integrity_clone(b, bio, gfp_mask); ret = bio_integrity_clone(b, bio, gfp_mask, fs_bio_set);
if (ret < 0) { if (ret < 0) {
bio_put(b); bio_put(b);
@ -1539,6 +1539,7 @@ void bioset_free(struct bio_set *bs)
if (bs->bio_pool) if (bs->bio_pool)
mempool_destroy(bs->bio_pool); mempool_destroy(bs->bio_pool);
bioset_integrity_free(bs);
biovec_free_pools(bs); biovec_free_pools(bs);
bio_put_slab(bs); bio_put_slab(bs);
@ -1579,6 +1580,9 @@ struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
if (!bs->bio_pool) if (!bs->bio_pool)
goto bad; goto bad;
if (bioset_integrity_create(bs, pool_size))
goto bad;
if (!biovec_create_pools(bs, pool_size)) if (!biovec_create_pools(bs, pool_size))
return bs; return bs;
@ -1616,6 +1620,7 @@ static int __init init_bio(void)
if (!bio_slabs) if (!bio_slabs)
panic("bio: can't allocate bios\n"); panic("bio: can't allocate bios\n");
bio_integrity_init();
biovec_init_slabs(); biovec_init_slabs();
fs_bio_set = bioset_create(BIO_POOL_SIZE, 0); fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);

View file

@ -319,7 +319,6 @@ static inline int bio_has_allocated_vec(struct bio *bio)
*/ */
struct bio_integrity_payload { struct bio_integrity_payload {
struct bio *bip_bio; /* parent bio */ struct bio *bip_bio; /* parent bio */
struct bio_vec *bip_vec; /* integrity data vector */
sector_t bip_sector; /* virtual start sector */ sector_t bip_sector; /* virtual start sector */
@ -328,11 +327,12 @@ struct bio_integrity_payload {
unsigned int bip_size; unsigned int bip_size;
unsigned short bip_pool; /* pool the ivec came from */ unsigned short bip_slab; /* slab the bip came from */
unsigned short bip_vcnt; /* # of integrity bio_vecs */ unsigned short bip_vcnt; /* # of integrity bio_vecs */
unsigned short bip_idx; /* current bip_vec index */ unsigned short bip_idx; /* current bip_vec index */
struct work_struct bip_work; /* I/O completion */ struct work_struct bip_work; /* I/O completion */
struct bio_vec bip_vec[0]; /* embedded bvec array */
}; };
#endif /* CONFIG_BLK_DEV_INTEGRITY */ #endif /* CONFIG_BLK_DEV_INTEGRITY */
@ -430,6 +430,9 @@ struct bio_set {
unsigned int front_pad; unsigned int front_pad;
mempool_t *bio_pool; mempool_t *bio_pool;
#if defined(CONFIG_BLK_DEV_INTEGRITY)
mempool_t *bio_integrity_pool;
#endif
mempool_t *bvec_pool; mempool_t *bvec_pool;
}; };
@ -634,8 +637,9 @@ static inline struct bio *bio_list_get(struct bio_list *bl)
#define bio_integrity(bio) (bio->bi_integrity != NULL) #define bio_integrity(bio) (bio->bi_integrity != NULL)
extern struct bio_integrity_payload *bio_integrity_alloc_bioset(struct bio *, gfp_t, unsigned int, struct bio_set *);
extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int); extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
extern void bio_integrity_free(struct bio *); extern void bio_integrity_free(struct bio *, struct bio_set *);
extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int); extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
extern int bio_integrity_enabled(struct bio *bio); extern int bio_integrity_enabled(struct bio *bio);
extern int bio_integrity_set_tag(struct bio *, void *, unsigned int); extern int bio_integrity_set_tag(struct bio *, void *, unsigned int);
@ -645,21 +649,27 @@ extern void bio_integrity_endio(struct bio *, int);
extern void bio_integrity_advance(struct bio *, unsigned int); extern void bio_integrity_advance(struct bio *, unsigned int);
extern void bio_integrity_trim(struct bio *, unsigned int, unsigned int); extern void bio_integrity_trim(struct bio *, unsigned int, unsigned int);
extern void bio_integrity_split(struct bio *, struct bio_pair *, int); extern void bio_integrity_split(struct bio *, struct bio_pair *, int);
extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t); extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t, struct bio_set *);
extern int bioset_integrity_create(struct bio_set *, int);
extern void bioset_integrity_free(struct bio_set *);
extern void bio_integrity_init(void);
#else /* CONFIG_BLK_DEV_INTEGRITY */ #else /* CONFIG_BLK_DEV_INTEGRITY */
#define bio_integrity(a) (0) #define bio_integrity(a) (0)
#define bioset_integrity_create(a, b) (0)
#define bio_integrity_prep(a) (0) #define bio_integrity_prep(a) (0)
#define bio_integrity_enabled(a) (0) #define bio_integrity_enabled(a) (0)
#define bio_integrity_clone(a, b, c) (0) #define bio_integrity_clone(a, b, c, d) (0)
#define bio_integrity_free(a) do { } while (0) #define bioset_integrity_free(a) do { } while (0)
#define bio_integrity_free(a, b) do { } while (0)
#define bio_integrity_endio(a, b) do { } while (0) #define bio_integrity_endio(a, b) do { } while (0)
#define bio_integrity_advance(a, b) do { } while (0) #define bio_integrity_advance(a, b) do { } while (0)
#define bio_integrity_trim(a, b, c) do { } while (0) #define bio_integrity_trim(a, b, c) do { } while (0)
#define bio_integrity_split(a, b, c) do { } while (0) #define bio_integrity_split(a, b, c) do { } while (0)
#define bio_integrity_set_tag(a, b, c) do { } while (0) #define bio_integrity_set_tag(a, b, c) do { } while (0)
#define bio_integrity_get_tag(a, b, c) do { } while (0) #define bio_integrity_get_tag(a, b, c) do { } while (0)
#define bio_integrity_init(a) do { } while (0)
#endif /* CONFIG_BLK_DEV_INTEGRITY */ #endif /* CONFIG_BLK_DEV_INTEGRITY */