[patch 3/3] use kref for bio

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Use kref for reference counter of bio.
This patch also removes BUG_ON() for freeing unreferenced bio.
But kref can detect it if CONFIG_DEBUG_SLAB is enabled.

Signed-off-by: Akinobu Mita <[email protected]>
CC: Jens Axboe <[email protected]>
CC: Greg KH <[email protected]>

 Documentation/block/biodoc.txt |    2 +-
 fs/bio.c                       |   17 ++++++++++-------
 fs/xfs/linux-2.6/xfs_aops.c    |    1 -
 include/linux/bio.h            |    4 ++--
 4 files changed, 13 insertions(+), 11 deletions(-)

Index: 2.6-git/Documentation/block/biodoc.txt
===================================================================
--- 2.6-git.orig/Documentation/block/biodoc.txt
+++ 2.6-git/Documentation/block/biodoc.txt
@@ -462,7 +462,7 @@ struct bio {
                                         used as index into pool */
        struct bio_vec   *bi_io_vec;  /* the actual vec list */
        bio_end_io_t	*bi_end_io;  /* bi_end_io (bio) */
-       atomic_t		bi_cnt;	     /* pin count: free when it hits zero */
+       struct kref	bi_kref;     /* pin count */
        void             *bi_private;
        bio_destructor_t *bi_destructor; /* bi_destructor (bio) */
 };
Index: 2.6-git/fs/bio.c
===================================================================
--- 2.6-git.orig/fs/bio.c
+++ 2.6-git/fs/bio.c
@@ -139,7 +139,7 @@ void bio_init(struct bio *bio)
 	bio->bi_size = 0;
 	bio->bi_max_vecs = 0;
 	bio->bi_end_io = NULL;
-	atomic_set(&bio->bi_cnt, 1);
+	kref_init(&bio->bi_kref);
 	bio->bi_private = NULL;
 }
 
@@ -208,6 +208,14 @@ void zero_fill_bio(struct bio *bio)
 }
 EXPORT_SYMBOL(zero_fill_bio);
 
+static void release_bio(struct kref *kref)
+{
+	struct bio *bio = container_of(kref, struct bio, bi_kref);
+
+	bio->bi_next = NULL;
+	bio->bi_destructor(bio);
+}
+
 /**
  * bio_put - release a reference to a bio
  * @bio:   bio to release reference to
@@ -218,15 +226,10 @@ EXPORT_SYMBOL(zero_fill_bio);
  **/
 void bio_put(struct bio *bio)
 {
-	BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
-
 	/*
 	 * last put frees it
 	 */
-	if (atomic_dec_and_test(&bio->bi_cnt)) {
-		bio->bi_next = NULL;
-		bio->bi_destructor(bio);
-	}
+	kref_put(&bio->bi_kref, release_bio);
 }
 
 inline int bio_phys_segments(request_queue_t *q, struct bio *bio)
Index: 2.6-git/fs/xfs/linux-2.6/xfs_aops.c
===================================================================
--- 2.6-git.orig/fs/xfs/linux-2.6/xfs_aops.c
+++ 2.6-git/fs/xfs/linux-2.6/xfs_aops.c
@@ -272,7 +272,6 @@ xfs_end_bio(
 		return 1;
 
 	ASSERT(ioend);
-	ASSERT(atomic_read(&bio->bi_cnt) >= 1);
 
 	/* Toss bio and pass work off to an xfsdatad thread */
 	if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
Index: 2.6-git/include/linux/bio.h
===================================================================
--- 2.6-git.orig/include/linux/bio.h
+++ 2.6-git/include/linux/bio.h
@@ -106,7 +106,7 @@ struct bio {
 	struct bio_vec		*bi_io_vec;	/* the actual vec list */
 
 	bio_end_io_t		*bi_end_io;
-	atomic_t		bi_cnt;		/* pin count */
+	struct kref		bi_kref;	/* pin count */
 
 	void			*bi_private;
 
@@ -249,7 +249,7 @@ struct bio {
  * returns. and then bio would be freed memory when if (bio->bi_flags ...)
  * runs
  */
-#define bio_get(bio)	atomic_inc(&(bio)->bi_cnt)
+#define bio_get(bio)	kref_get(&(bio)->bi_kref)
 
 
 /*

--
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[Index of Archives]     [Kernel Newbies]     [Netfilter]     [Bugtraq]     [Photo]     [Stuff]     [Gimp]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Video 4 Linux]     [Linux for the blind]     [Linux Resources]
  Powered by Linux