Skip to contentSkip to conversation

bio-set patch on latest bk source tree

RFC PATCH In review Dave Olien 2 participants 3 messages Feb 1, 2005

Subsystems (3)
lore
Share & tools
lore

Dave Olien

author
#0 lore raw

RFC PATCH 1/1 bio-set patch on latest bk source tree

Hi, Jens,

I sent you a patch for review about a week ago. Here is that same patch, applied to the latest bk kernel. Thanks in advance for the review

Dave Olien

===== drivers/md/dm.c 1.61 vs edited =====

drivers/md/dm.c +9 -1
@@ -96,10 +96,16 @@ struct mapped_device {
 static kmem_cache_t *_io_cache;
 static kmem_cache_t *_tio_cache;
 
+static struct bio_set *dm_set;
+
 static int __init local_init(void)
 {
 	int r;
 
+	dm_set = bioset_create(512, 512, 1);
+	if (!dm_set)
+		return -ENOMEM;
+
 	/* allocate a slab for the dm_ios */
 	_io_cache = kmem_cache_create("dm_io",
 				      sizeof(struct dm_io), 0, 0, NULL, NULL);
@@ -133,6 +139,8 @@ static void local_exit(void)
 	kmem_cache_destroy(_tio_cache);
 	kmem_cache_destroy(_io_cache);
 
+	bioset_free(dm_set);
+	
 	if (unregister_blkdev(_major, _name) < 0)
 		DMERR("devfs_unregister_blkdev failed");
 
@@ -393,7 +401,7 @@ static struct bio *split_bvec(struct bio
 	struct bio *clone;
 	struct bio_vec *bv = bio->bi_io_vec + idx;
 
-	clone = bio_alloc(GFP_NOIO, 1);
+	clone = bio_alloc_bs(GFP_NOIO, 1, dm_set);
 	*clone->bi_io_vec = *bv;
 
 	clone->bi_sector = sector;

===== drivers/md/dm-io.c 1.4 vs edited =====

drivers/md/dm-io.c +9 -212

269 lines not on this page · raw message

===== drivers/md/dm-zero.c 1.3 vs edited =====

drivers/md/dm-zero.c +0 -17
@@ -24,23 +24,6 @@ static int zero_ctr(struct dm_target *ti
 }
 
 /*
- * Fills the bio pages with zeros
- */
-static void zero_fill_bio(struct bio *bio)
-{
-	unsigned long flags;
-	struct bio_vec *bv;
-	int i;
-
-	bio_for_each_segment(bv, bio, i) {
-		char *data = bvec_kmap_irq(bv, &flags);
-		memset(data, 0, bv->bv_len);
-		flush_dcache_page(bv->bv_page);
-		bvec_kunmap_irq(data, &flags);
-	}
-}
-
-/*
  * Return zeros only on reads
  */
 static int zero_map(struct dm_target *ti, struct bio *bio,

===== fs/bio.c 1.73 vs edited =====

fs/bio.c +147 -54

305 lines not on this page · raw message

===== include/linux/bio.h 1.43 vs edited =====

include/linux/bio.h +20 -0
@@ -59,6 +59,7 @@ struct bio_vec {
 	unsigned int	bv_offset;
 };
 
+struct bio_set;
 struct bio;
 typedef int (bio_end_io_t) (struct bio *, unsigned int, int);
 typedef void (bio_destructor_t) (struct bio *);
@@ -109,6 +110,7 @@ struct bio {
 	void			*bi_private;
 
 	bio_destructor_t	*bi_destructor;	/* destructor */
+	struct bio_set		*bi_set;	/* memory pools set */
 };
 
 /*
@@ -258,7 +260,11 @@ extern struct bio_pair *bio_split(struct
 extern mempool_t *bio_split_pool;
 extern void bio_pair_release(struct bio_pair *dbio);
 
+extern struct bio_set *bioset_create(int, int, int);
+extern void bioset_free(struct bio_set *);
+
 extern struct bio *bio_alloc(int, int);
+extern struct bio *bio_alloc_bs(int, int, struct bio_set *);
 extern void bio_put(struct bio *);
 
 extern void bio_endio(struct bio *, unsigned int, int);
@@ -328,5 +334,19 @@ extern inline char *__bio_kmap_irq(struc
 #define bio_kmap_irq(bio, flags) \
 	__bio_kmap_irq((bio), (bio)->bi_idx, (flags))
 #define bio_kunmap_irq(buf,flags)	__bio_kunmap_irq(buf, flags)
+
+extern inline void zero_fill_bio(struct bio *bio)
+{
+	unsigned long flags;
+	struct bio_vec *bv;
+	int i;
+
+	bio_for_each_segment(bv, bio, i) {
+		char *data = bvec_kmap_irq(bv, &flags);
+		memset(data, 0, bv->bv_len);
+		flush_dcache_page(bv->bv_page);
+		bvec_kunmap_irq(data, &flags);
+	}
+}
 
 #endif /* __LINUX_BIO_H */

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

#1 lore raw
Quoted 5 lines · Dave Olien

Hi, Jens,

I sent you a patch for review about a week ago. Here is that same patch, applied to the latest bk kernel. Thanks in advance for the review

Thanks Dave, I think we should queue this up with Andrew soonish. A few minor things need fixing, though:

Quoted 7 lines

===== drivers/md/dm.c 1.61 vs edited =====

drivers/md/dm.c +0 -0
@@ -96,10 +96,16 @@ struct mapped_device {
 static kmem_cache_t *_io_cache;
 static kmem_cache_t *_tio_cache;
 

drivers/md/dm.c struct mapped_device { · line 108

Quoted 10 lines
drivers/md/dm.c +6 -0
+static struct bio_set *dm_set;
+
 static int __init local_init(void)
 {
 	int r;
 
+	dm_set = bioset_create(512, 512, 1);
+	if (!dm_set)
+		return -ENOMEM;
+

You consistently use 512, that's a _lot_ of reserved bio's/vecs! For the full range of bio_vec pools, that will eat quite a large piece of memory per bio_set allocated. The idea with a mempool isn't to have a huge free number of elements, but rather just a few to maintain progress with memory pressure. I think this 512 should be heavily reduced.

Quoted 96 lines
drivers/md/dm.c +0 -0
 static int zero_map(struct dm_target *ti, struct bio *bio,

===== fs/bio.c 1.73 vs edited =====

fs/bio.c +28 -12
@@ -28,7 +28,6 @@
 
 #define BIO_POOL_SIZE 256
 
-static mempool_t *bio_pool;
 static kmem_cache_t *bio_slab;
 
 #define BIOVEC_NR_POOLS 6
@@ -40,11 +39,10 @@ static kmem_cache_t *bio_slab;
 #define BIO_SPLIT_ENTRIES 8	
 mempool_t *bio_split_pool;
 
-struct biovec_pool {
+struct biovec_slab {
 	int nr_vecs;
 	char *name; 
 	kmem_cache_t *slab;
-	mempool_t *pool;
 };
 
 /*
@@ -54,15 +52,32 @@ struct biovec_pool {
  */
 
 #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
-static struct biovec_pool bvec_array[BIOVEC_NR_POOLS] = {
+static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] = {
 	BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
 };
 #undef BV
 
-static inline struct bio_vec *bvec_alloc(int gfp_mask, int nr, unsigned long *idx)
+/*
+ * bio_set is used to allow other portions of the IO system to
+ * allocate their own private memory pools for bio and iovec structures.
+ * These memory pools in turn all allocate from the bio_slab
+ * and the bvec_slabs[].
+ */
+struct bio_set {
+	mempool_t *bio_pool;
+	mempool_t *bvec_pools[BIOVEC_NR_POOLS];
+};
+
+/*
+ * fs_bio_set is the bio_set containing bio and iovec memory pools used by
+ * IO code that does not need private memory pools.
+ */
+static struct bio_set *fs_bio_set;
+
+static inline struct bio_vec *bvec_alloc_bs(int gfp_mask, int nr, unsigned long *idx, struct bio_set *bs)
 {
-	struct biovec_pool *bp;
 	struct bio_vec *bvl;
+	struct biovec_slab *bp;
 
 	/*
 	 * see comment near bvec_array define!
@@ -80,26 +95,27 @@ static inline struct bio_vec *bvec_alloc
 	/*
 	 * idx now points to the pool we want to allocate from
 	 */
-	bp = bvec_array + *idx;
 
-	bvl = mempool_alloc(bp->pool, gfp_mask);
+	bp = bvec_slabs + *idx;
+	bvl = mempool_alloc(bs->bvec_pools[*idx], gfp_mask);
 	if (bvl)
 		memset(bvl, 0, bp->nr_vecs * sizeof(struct bio_vec));
+
 	return bvl;
 }
 
 /*
- * default destructor for a bio allocated with bio_alloc()
+ * default destructor for a bio allocated with bio_alloc_bs()
  */
 static void bio_destructor(struct bio *bio)
 {
 	const int pool_idx = BIO_POOL_IDX(bio);
-	struct biovec_pool *bp = bvec_array + pool_idx;
+	struct bio_set *bs = bio->bi_set;
 
 	BIO_BUG_ON(pool_idx >= BIOVEC_NR_POOLS);
 
-	mempool_free(bio->bi_io_vec, bp->pool);
-	mempool_free(bio, bio_pool);
+	mempool_free(bio->bi_io_vec, bs->bvec_pools[pool_idx]);
+	mempool_free(bio, bs->bio_pool);
 }
 
 inline void bio_init(struct bio *bio)

fs/bio.c inline void bio_init(struct bio *bio) · line 142

Quoted 7 lines
fs/bio.c +1 -1
@@ -121,18 +137,21 @@ inline void bio_init(struct bio *bio)
 }
 
 /**
- * bio_alloc - allocate a bio for I/O
+ * bio_alloc_bs - allocate a bio for I/O
  * @gfp_mask:   the GFP_ mask given to the slab allocator
  * @nr_iovecs:	number of iovecs to pre-allocate

bs is awefully close to 'block size' when it comes to acronyms in this area, I would prefer if you just called it bio_alloc_bioset() instead.

fs/bio.c static void bio_pair_free(void *bp, void · line 972

Quoted 4 lines
fs/bio.c +4 -0
+void bioset_free(struct bio_set *bs)
+{
+	if (!bs->bio_pool)
+		mempool_destroy(bs->bio_pool);

That check looks suspect, surely you mean to check for non-NULL.

Quoted 19 lines
fs/bio.c +19 -0
+
+	biovec_free_pools(bs);
+
+	kfree(bs);
+}
+
+struct bio_set *bioset_create(int bio_pool_size, int bvec_pool_size, int scale)
+{
+	struct bio_set *bs = kmalloc(sizeof(*bs), GFP_KERNEL);
+
+	if (!bs)
+		return NULL;
+
+	memset(bs, 0, sizeof(*bs));
+	bs->bio_pool = mempool_create(bio_pool_size, mempool_alloc_slab,
+			mempool_free_slab, bio_slab);
+
+	if (!bs->bio_pool) 
+		goto bad;

fs/bio.c static void bio_pair_free(void *bp, void · line 1001

Quoted 10 lines
fs/bio.c +10 -0
+	
+	if (biovec_create_pools(bs, bvec_pool_size, scale))
+		goto bad;
+
+	return bs;
+
+bad:
+	bioset_free(bs);
+	return NULL;
+}

Please drop the last goto

if (!biovec_create_pools(bs, bvec_pool_size, scale))
return bs;

is nicer.

Signature · 6 lines
Jens Axboe

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
author
#2 lore raw

Jens,

Thanks for the review! Your comments are quite right on all counts. Can't imagine how I slipped on that test for non-Null.

I'll reduce the 512 bioset allocation sizes to 16, give it a test see where that goes. I think I'll need to change the scale to 4 as well.

I'll apply your fixes, and submit to Andrew today.

Dave

Quoted 180 lines · Jens Axboe
Quoted 5 lines · Dave Olien

Hi, Jens,

I sent you a patch for review about a week ago. Here is that same patch, applied to the latest bk kernel. Thanks in advance for the review

Thanks Dave, I think we should queue this up with Andrew soonish. A few minor things need fixing, though:

Quoted 7 lines

===== drivers/md/dm.c 1.61 vs edited =====

drivers/md/dm.c +0 -0
@@ -96,10 +96,16 @@ struct mapped_device {
 static kmem_cache_t *_io_cache;
 static kmem_cache_t *_tio_cache;
 

drivers/md/dm.c struct mapped_device { · line 108

Quoted 10 lines
drivers/md/dm.c +6 -0
+static struct bio_set *dm_set;
+
 static int __init local_init(void)
 {
 	int r;
 
+	dm_set = bioset_create(512, 512, 1);
+	if (!dm_set)
+		return -ENOMEM;
+

You consistently use 512, that's a _lot_ of reserved bio's/vecs! For the full range of bio_vec pools, that will eat quite a large piece of memory per bio_set allocated. The idea with a mempool isn't to have a huge free number of elements, but rather just a few to maintain progress with memory pressure. I think this 512 should be heavily reduced.

Quoted 96 lines
drivers/md/dm.c +0 -0
 static int zero_map(struct dm_target *ti, struct bio *bio,

===== fs/bio.c 1.73 vs edited =====

fs/bio.c +28 -12
@@ -28,7 +28,6 @@
 
 #define BIO_POOL_SIZE 256
 
-static mempool_t *bio_pool;
 static kmem_cache_t *bio_slab;
 
 #define BIOVEC_NR_POOLS 6
@@ -40,11 +39,10 @@ static kmem_cache_t *bio_slab;
 #define BIO_SPLIT_ENTRIES 8	
 mempool_t *bio_split_pool;
 
-struct biovec_pool {
+struct biovec_slab {
 	int nr_vecs;
 	char *name; 
 	kmem_cache_t *slab;
-	mempool_t *pool;
 };
 
 /*
@@ -54,15 +52,32 @@ struct biovec_pool {
  */
 
 #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
-static struct biovec_pool bvec_array[BIOVEC_NR_POOLS] = {
+static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] = {
 	BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
 };
 #undef BV
 
-static inline struct bio_vec *bvec_alloc(int gfp_mask, int nr, unsigned long *idx)
+/*
+ * bio_set is used to allow other portions of the IO system to
+ * allocate their own private memory pools for bio and iovec structures.
+ * These memory pools in turn all allocate from the bio_slab
+ * and the bvec_slabs[].
+ */
+struct bio_set {
+	mempool_t *bio_pool;
+	mempool_t *bvec_pools[BIOVEC_NR_POOLS];
+};
+
+/*
+ * fs_bio_set is the bio_set containing bio and iovec memory pools used by
+ * IO code that does not need private memory pools.
+ */
+static struct bio_set *fs_bio_set;
+
+static inline struct bio_vec *bvec_alloc_bs(int gfp_mask, int nr, unsigned long *idx, struct bio_set *bs)
 {
-	struct biovec_pool *bp;
 	struct bio_vec *bvl;
+	struct biovec_slab *bp;
 
 	/*
 	 * see comment near bvec_array define!
@@ -80,26 +95,27 @@ static inline struct bio_vec *bvec_alloc
 	/*
 	 * idx now points to the pool we want to allocate from
 	 */
-	bp = bvec_array + *idx;
 
-	bvl = mempool_alloc(bp->pool, gfp_mask);
+	bp = bvec_slabs + *idx;
+	bvl = mempool_alloc(bs->bvec_pools[*idx], gfp_mask);
 	if (bvl)
 		memset(bvl, 0, bp->nr_vecs * sizeof(struct bio_vec));
+
 	return bvl;
 }
 
 /*
- * default destructor for a bio allocated with bio_alloc()
+ * default destructor for a bio allocated with bio_alloc_bs()
  */
 static void bio_destructor(struct bio *bio)
 {
 	const int pool_idx = BIO_POOL_IDX(bio);
-	struct biovec_pool *bp = bvec_array + pool_idx;
+	struct bio_set *bs = bio->bi_set;
 
 	BIO_BUG_ON(pool_idx >= BIOVEC_NR_POOLS);
 
-	mempool_free(bio->bi_io_vec, bp->pool);
-	mempool_free(bio, bio_pool);
+	mempool_free(bio->bi_io_vec, bs->bvec_pools[pool_idx]);
+	mempool_free(bio, bs->bio_pool);
 }
 
 inline void bio_init(struct bio *bio)

fs/bio.c inline void bio_init(struct bio *bio) · line 142

Quoted 7 lines
fs/bio.c +1 -1
@@ -121,18 +137,21 @@ inline void bio_init(struct bio *bio)
 }
 
 /**
- * bio_alloc - allocate a bio for I/O
+ * bio_alloc_bs - allocate a bio for I/O
  * @gfp_mask:   the GFP_ mask given to the slab allocator
  * @nr_iovecs:	number of iovecs to pre-allocate

bs is awefully close to 'block size' when it comes to acronyms in this area, I would prefer if you just called it bio_alloc_bioset() instead.

fs/bio.c static void bio_pair_free(void *bp, void · line 972

Quoted 4 lines
fs/bio.c +4 -0
+void bioset_free(struct bio_set *bs)
+{
+	if (!bs->bio_pool)
+		mempool_destroy(bs->bio_pool);

That check looks suspect, surely you mean to check for non-NULL.

Quoted 19 lines
fs/bio.c +19 -0
+
+	biovec_free_pools(bs);
+
+	kfree(bs);
+}
+
+struct bio_set *bioset_create(int bio_pool_size, int bvec_pool_size, int scale)
+{
+	struct bio_set *bs = kmalloc(sizeof(*bs), GFP_KERNEL);
+
+	if (!bs)
+		return NULL;
+
+	memset(bs, 0, sizeof(*bs));
+	bs->bio_pool = mempool_create(bio_pool_size, mempool_alloc_slab,
+			mempool_free_slab, bio_slab);
+
+	if (!bs->bio_pool) 
+		goto bad;

fs/bio.c static void bio_pair_free(void *bp, void · line 1001

Quoted 10 lines
fs/bio.c +10 -0
+	
+	if (biovec_create_pools(bs, bvec_pool_size, scale))
+		goto bad;
+
+	return bs;
+
+bad:
+	bioset_free(bs);
+	return NULL;
+}

Please drop the last goto

Quoted 12 lines

if (!biovec_create_pools(bs, bvec_pool_size, scale))
return bs;

is nicer.

Signature · 6 lines
Jens Axboe

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel