linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jens Axboe <jens.axboe@oracle.com>
To: Linus Torvalds <torvalds@osdl.org>
Cc: Alistair John Strachan <s0348365@sms.ed.ac.uk>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Jeff Garzik <jeff@garzik.org>
Subject: Re: Linux 2.6.20-rc1
Date: Mon, 18 Dec 2006 19:41:17 +0100	[thread overview]
Message-ID: <20061218184117.GQ5010@kernel.dk> (raw)
In-Reply-To: <20061218183209.GP5010@kernel.dk>

On Mon, Dec 18 2006, Jens Axboe wrote:
> On Sat, Dec 16 2006, Linus Torvalds wrote:
> > That said: Jens - I think 0e75f906 was a mistake. "blk_rq_unmap()" really 
> > should be passed the "struct bio", not the "struct request *". Right now 
> > it does something _really_ strange with requests with linked bio's, and I 
> > don't think your and FUJITA's "leak fix" really works. What happens when 
> > the bio was a linked list on the request, and you put the old _head_ on 
> > the request with "rq->bio = bio"? What happens to the other parts of it?
> 
> I agree it's fishy and I did think about it. The design isn't exactly
> the prettiest, but it should be safe. The reason is that we don't
> actually unlink the individual bio from the list, even if we may set
> rq->bio to point somewhere further into the list. So as long as the bio
> is valid, the bi_next field is still valid as well. We need a reference
> on the bio to perform the unmap and blk_rq_unmap_user() drops this
> reference on its own, so the bio must be valid.
> 
> Taking a rq pointer when we really want a bio is nasty, though. I'll
> chance that at least.

Something like this. One alternative I did originally consider was to
add a rq->cbio (for lack of a better name) that points to the original
bio and isn't cleared on io completion, just to have the original bio
location stored. That makes the API symmetric and doesn't have any
hidden requirements, at the cost of an extra pointer in struct request.
So I think the included is the better patch, since it's still clear what
needs to be done and it doesn't add extra members to struct request.

-----

The blk_rq_unmap_user() API is not very nice. It expects the caller to
know that rq->bio has to be reset to the original bio, and it will
silently do nothing if that is not done. Instead make it explicit that
we need to pass in the first bio, by expecting a bio argument.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>

diff --git a/block/ll_rw_blk.c b/block/ll_rw_blk.c
index fb40575..51c828e 100644
--- a/block/ll_rw_blk.c
+++ b/block/ll_rw_blk.c
@@ -2323,6 +2323,7 @@ int blk_rq_map_user(request_queue_t *q, struct request *rq, void __user *ubuf,
 		    unsigned long len)
 {
 	unsigned long bytes_read = 0;
+	struct bio *bio = NULL;
 	int ret;
 
 	if (len > (q->max_hw_sectors << 9))
@@ -2349,6 +2350,8 @@ int blk_rq_map_user(request_queue_t *q, struct request *rq, void __user *ubuf,
 		ret = __blk_rq_map_user(q, rq, ubuf, map_len);
 		if (ret < 0)
 			goto unmap_rq;
+		if (!bio)
+			bio = rq->bio;
 		bytes_read += ret;
 		ubuf += ret;
 	}
@@ -2356,7 +2359,7 @@ int blk_rq_map_user(request_queue_t *q, struct request *rq, void __user *ubuf,
 	rq->buffer = rq->data = NULL;
 	return 0;
 unmap_rq:
-	blk_rq_unmap_user(rq);
+	blk_rq_unmap_user(bio);
 	return ret;
 }
 
@@ -2413,26 +2416,28 @@ EXPORT_SYMBOL(blk_rq_map_user_iov);
 
 /**
  * blk_rq_unmap_user - unmap a request with user data
- * @rq:		rq to be unmapped
+ * @bio:		start of bio list
  *
  * Description:
- *    Unmap a rq previously mapped by blk_rq_map_user().
- *    rq->bio must be set to the original head of the request.
+ *    Unmap a rq previously mapped by blk_rq_map_user(). The caller must
+ *    supply the original rq->bio from the blk_rq_map_user() return, since
+ *    the io completion may have changed rq->bio.
  */
-int blk_rq_unmap_user(struct request *rq)
+int blk_rq_unmap_user(struct bio *bio)
 {
-	struct bio *bio, *mapped_bio;
+	struct bio *mapped_bio;
 
-	while ((bio = rq->bio)) {
-		if (bio_flagged(bio, BIO_BOUNCED))
+	while (bio) {
+		mapped_bio = bio;
+		if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
 			mapped_bio = bio->bi_private;
-		else
-			mapped_bio = bio;
 
 		__blk_rq_unmap_user(mapped_bio);
-		rq->bio = bio->bi_next;
-		bio_put(bio);
+		mapped_bio = bio;
+		bio = bio->bi_next;
+		bio_put(mapped_bio);
 	}
+
 	return 0;
 }
 
diff --git a/block/scsi_ioctl.c b/block/scsi_ioctl.c
index f322b6a..2528a0c 100644
--- a/block/scsi_ioctl.c
+++ b/block/scsi_ioctl.c
@@ -333,8 +333,7 @@ static int sg_io(struct file *file, request_queue_t *q,
 			hdr->sb_len_wr = len;
 	}
 
-	rq->bio = bio;
-	if (blk_rq_unmap_user(rq))
+	if (blk_rq_unmap_user(bio))
 		ret = -EFAULT;
 
 	/* may not have succeeded, but output values written to control
diff --git a/drivers/cdrom/cdrom.c b/drivers/cdrom/cdrom.c
index e4a2f8f..66d028d 100644
--- a/drivers/cdrom/cdrom.c
+++ b/drivers/cdrom/cdrom.c
@@ -2139,8 +2139,7 @@ static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
 			cdi->last_sense = s->sense_key;
 		}
 
-		rq->bio = bio;
-		if (blk_rq_unmap_user(rq))
+		if (blk_rq_unmap_user(bio))
 			ret = -EFAULT;
 
 		if (ret)
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 0a801cc..d93f8ea 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -710,7 +710,7 @@ extern void __blk_stop_queue(request_queue_t *q);
 extern void blk_run_queue(request_queue_t *);
 extern void blk_start_queueing(request_queue_t *);
 extern int blk_rq_map_user(request_queue_t *, struct request *, void __user *, unsigned long);
-extern int blk_rq_unmap_user(struct request *);
+extern int blk_rq_unmap_user(struct bio *);
 extern int blk_rq_map_kern(request_queue_t *, struct request *, void *, unsigned int, gfp_t);
 extern int blk_rq_map_user_iov(request_queue_t *, struct request *,
 			       struct sg_iovec *, int, unsigned int);

-- 
Jens Axboe


  reply	other threads:[~2006-12-18 18:39 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-14  2:06 Linux 2.6.20-rc1 Linus Torvalds
2006-12-14  2:46 ` Gene Heskett
2006-12-14  3:32   ` Linus Torvalds
2006-12-14  5:36     ` Gene Heskett
2006-12-14 17:48       ` ieee1394 in 2.6.20-rc1 (was Re: Linux 2.6.20-rc1) Stefan Richter
2006-12-14 19:08         ` Stefan Richter
2006-12-15  3:17         ` Gene Heskett
2006-12-17 18:11           ` Gene Heskett
2006-12-17 18:31             ` Stefan Richter
2006-12-17 19:04               ` Gene Heskett
2006-12-17 20:21                 ` Stefan Richter
2006-12-17 23:34                   ` Gene Heskett
2006-12-18  1:05                     ` Stefan Richter
2006-12-18  4:29                       ` Gene Heskett
2006-12-18 15:45                         ` Stefan Richter
2006-12-18 15:54                           ` Gene Heskett
2006-12-14 13:59 ` Linux 2.6.20-rc1 Alessandro Suardi
2006-12-14 14:18   ` Steve WIse
2006-12-14 15:48   ` Alan
2006-12-14 19:30 ` Alistair John Strachan
2006-12-14 19:57   ` Linus Torvalds
2006-12-14 20:05     ` Jeff Garzik
2006-12-14 20:26       ` [PATCH] support HDIO_GET_IDENTITY in libata Erik Andersen
2006-12-14 20:31         ` Jeff Garzik
2006-12-14 20:40           ` Erik Andersen
2006-12-16 16:34             ` Jeff Garzik
2006-12-15 18:45         ` Alexey Dobriyan
2006-12-16 16:34           ` Jeff Garzik
2006-12-14 20:16     ` Linux 2.6.20-rc1 Alistair John Strachan
2006-12-14 20:28       ` Jens Axboe
2006-12-14 20:33         ` Jeff Garzik
2006-12-14 20:36           ` Jens Axboe
2006-12-14 20:48         ` Jens Axboe
2006-12-14 21:13           ` Alistair John Strachan
2006-12-14 21:20             ` Jens Axboe
2006-12-15  0:48               ` Alistair John Strachan
2006-12-15  1:41                 ` Alistair John Strachan
2006-12-16 21:36                   ` Linus Torvalds
2006-12-16 22:28                     ` Alistair John Strachan
2006-12-16 22:31                       ` Jeff Garzik
2006-12-16 23:00                     ` Alistair John Strachan
2006-12-18 18:32                     ` Jens Axboe
2006-12-18 18:41                       ` Jens Axboe [this message]
2006-12-14 21:33             ` Jeff Garzik
2006-12-14 21:44               ` Alistair John Strachan
2006-12-14 21:50                 ` Jeff Garzik
2006-12-14 22:33                   ` Alistair John Strachan
2006-12-19 12:41                     ` Jens Axboe
2006-12-19 14:32                       ` Robert Hancock
2006-12-19 14:38                         ` Jens Axboe
2006-12-19 14:50                           ` Jens Axboe
2006-12-19 17:49                         ` Linus Torvalds
2006-12-14 21:53                 ` Jeff Garzik
2006-12-14 20:32   ` Nicolas Mailhot
2006-12-14 23:22     ` Jeff Garzik
2006-12-14 23:33       ` Nicolas Mailhot
2006-12-15 16:50 ` Bill Davidsen
2006-12-15 17:28   ` Alan
2006-12-18 21:57     ` Bill Davidsen
     [not found] ` <20061216174536.GA2753@melchior.yamamaya.is-a-geek.org>
2006-12-16 18:06   ` IO-APIC + timer doesn't work (was: Linux 2.6.20-rc1) Linus Torvalds
     [not found]     ` <20061216225338.GA2616@melchior.yamamaya.is-a-geek.org>
     [not found]       ` <20061216230605.GA2789@melchior.yamamaya.is-a-geek.org>
2006-12-16 23:36         ` Linus Torvalds
     [not found]           ` <20061216235513.GA2424@melchior.yamamaya.is-a-geek.org>
2006-12-17  0:04             ` IO-APIC + timer doesn't work Linus Torvalds
2006-12-17  5:16               ` Eric W. Biederman
2006-12-17  5:22               ` Eric W. Biederman
2006-12-18  6:16                 ` Len Brown
2006-12-17 13:10               ` Tobias Diedrich
2006-12-17 17:26                 ` Linus Torvalds
2006-12-17 14:57           ` IO-APIC + timer doesn't work (was: Linux 2.6.20-rc1) Tobias Diedrich
2006-12-18 13:14             ` Eric W. Biederman
2006-12-18 15:23               ` Tobias Diedrich
2006-12-18 15:34                 ` Tobias Diedrich
2006-12-18 15:43                 ` IO-APIC + timer doesn't work Eric W. Biederman
2006-12-19  8:00                   ` Yinghai Lu
2006-12-19 11:27                     ` Eric W. Biederman
2006-12-20  6:50                       ` Yinghai Lu
2006-12-21 19:15                         ` Tobias Diedrich
2006-12-21 20:46                         ` Eric W. Biederman
2006-12-31  8:29                           ` Yinghai Lu
     [not found] <fa.RIN4HRPnLGt7UFAh8INm8D0Re5k@ifi.uio.no>
     [not found] ` <fa.bn+19zl5p6JLw04wsJAH4QbLSps@ifi.uio.no>
     [not found]   ` <fa.hRBfOTtQdNUe6Lr4YfYDijpzP5g@ifi.uio.no>
     [not found]     ` <fa.p3mZcZJUV5vbz5aYUBbt4rJjr2A@ifi.uio.no>
2006-12-15  1:03       ` Linux 2.6.20-rc1 Robert Hancock

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20061218184117.GQ5010@kernel.dk \
    --to=jens.axboe@oracle.com \
    --cc=jeff@garzik.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=s0348365@sms.ed.ac.uk \
    --cc=torvalds@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).