From: Hannes Reinecke <hare@suse.de>
To: "Martin K. Petersen" <martin.petersen@oracle.com>
Cc: Christoph Hellwig <hch@lst.de>,
James Bottomley <james.bottomley@hansenpartnership.com>,
Johannes Thumshirn <jth@kernel.org>,
Doug Gilberg <dgilbert@interlog.com>,
linux-scsi@vger.kernel.org, Hannes Reinecke <hare@suse.de>,
stable@vger.kernel.org, Hannes Reinecke <hare@suse.com>
Subject: [PATCH 2/4] sg: protect access to to 'reserved' page array
Date: Fri, 3 Feb 2017 09:54:49 +0100 [thread overview]
Message-ID: <1486112091-68470-3-git-send-email-hare@suse.de> (raw)
In-Reply-To: <1486112091-68470-1-git-send-email-hare@suse.de>
The 'reserved' page array is used as a short-cut for mapping
data, saving us to allocate pages per request.
However, the 'reserved' array is only capable of holding one
request, so we need to protect it against concurrent accesses.
Cc: stable@vger.kernel.org
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Link: http://www.spinics.net/lists/linux-scsi/msg104326.html
Signed-off-by: Hannes Reinecke <hare@suse.com>
Tested-by: Johannes Thumshirn <jth@kernel.org>
---
drivers/scsi/sg.c | 30 ++++++++++++------------------
1 file changed, 12 insertions(+), 18 deletions(-)
diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 8a959b2..c29962c 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -154,6 +154,8 @@
unsigned char next_cmd_len; /* 0: automatic, >0: use on next write() */
char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
char mmap_called; /* 0 -> mmap() never called on this fd */
+ unsigned long flags;
+#define SG_RESERVED_IN_USE 1
struct kref f_ref;
struct execute_work ew;
} Sg_fd;
@@ -197,7 +199,6 @@ static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
static Sg_request *sg_add_request(Sg_fd * sfp);
static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
-static int sg_res_in_use(Sg_fd * sfp);
static Sg_device *sg_get_dev(int dev);
static void sg_device_destroy(struct kref *kref);
@@ -720,7 +721,7 @@ static int sg_allow_access(struct file *filp, unsigned char *cmd)
sg_remove_request(sfp, srp);
return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
}
- if (sg_res_in_use(sfp)) {
+ if (test_bit(SG_RESERVED_IN_USE, &sfp->flags)) {
sg_remove_request(sfp, srp);
return -EBUSY; /* reserve buffer already being used */
}
@@ -952,10 +953,14 @@ static int max_sectors_bytes(struct request_queue *q)
val = min_t(int, val,
max_sectors_bytes(sdp->device->request_queue));
if (val != sfp->reserve.bufflen) {
- if (sg_res_in_use(sfp) || sfp->mmap_called)
+ if (sfp->mmap_called)
+ return -EBUSY;
+ if (test_and_set_bit(SG_RESERVED_IN_USE, &sfp->flags))
return -EBUSY;
+
sg_remove_scat(sfp, &sfp->reserve);
sg_build_reserve(sfp, val);
+ clear_bit(SG_RESERVED_IN_USE, &sfp->flags);
}
return 0;
case SG_GET_RESERVED_SIZE:
@@ -1709,7 +1714,9 @@ static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned lon
md = &map_data;
if (md) {
- if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
+ if (dxfer_len <= rsv_schp->bufflen &&
+ test_and_set_bit(SG_RESERVED_IN_USE,
+ &sfp->flags) == 0)
sg_link_reserve(sfp, srp, dxfer_len);
else {
res = sg_build_indirect(req_schp, sfp, dxfer_len);
@@ -2003,6 +2010,7 @@ static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned lon
req_schp->sglist_len = 0;
sfp->save_scat_len = 0;
srp->res_used = 0;
+ clear_bit(SG_RESERVED_IN_USE, &sfp->flags);
}
static Sg_request *
@@ -2187,20 +2195,6 @@ static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned lon
schedule_work(&sfp->ew.work);
}
-static int
-sg_res_in_use(Sg_fd * sfp)
-{
- const Sg_request *srp;
- unsigned long iflags;
-
- read_lock_irqsave(&sfp->rq_list_lock, iflags);
- for (srp = sfp->headrp; srp; srp = srp->nextrp)
- if (srp->res_used)
- break;
- read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
- return srp ? 1 : 0;
-}
-
#ifdef CONFIG_SCSI_PROC_FS
static int
sg_idr_max_id(int id, void *p, void *data)
--
1.8.5.6
next prev parent reply other threads:[~2017-02-03 8:54 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-03 8:54 [PATCH 0/4] sanitize sg Hannes Reinecke
2017-02-03 8:54 ` [PATCH 1/4] sg: disable SET_FORCE_LOW_DMA Hannes Reinecke
2017-02-03 9:32 ` Johannes Thumshirn
2017-02-03 10:16 ` Hannes Reinecke
2017-02-03 10:23 ` Christoph Hellwig
2017-02-03 8:54 ` Hannes Reinecke [this message]
2017-02-03 9:34 ` [PATCH 2/4] sg: protect access to to 'reserved' page array Johannes Thumshirn
2017-02-03 10:24 ` Christoph Hellwig
2017-02-03 10:45 ` Hannes Reinecke
2017-02-03 8:54 ` [PATCH 3/4] sg: check for valid direction before starting the request Hannes Reinecke
2017-02-03 10:28 ` Christoph Hellwig
2017-02-03 10:50 ` Hannes Reinecke
2017-02-03 11:46 ` Hannes Reinecke
2017-02-03 8:54 ` [PATCH 4/4] sg: use standard lists for sg_requests Hannes Reinecke
2017-02-03 9:38 ` Johannes Thumshirn
2017-02-03 10:43 ` Christoph Hellwig
2017-02-03 10:48 ` Hannes Reinecke
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=1486112091-68470-3-git-send-email-hare@suse.de \
--to=hare@suse.de \
--cc=dgilbert@interlog.com \
--cc=hare@suse.com \
--cc=hch@lst.de \
--cc=james.bottomley@hansenpartnership.com \
--cc=jth@kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=stable@vger.kernel.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).