linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Douglas Gilbert <dgilbert@interlog.com>
To: linux-scsi@vger.kernel.org
Cc: martin.petersen@oracle.com, jejb@linux.vnet.ibm.com, hare@suse.de
Subject: [PATCH v9 06/40] sg: make open count an atomic
Date: Tue, 21 Apr 2020 17:52:24 -0400	[thread overview]
Message-ID: <20200421215258.14348-7-dgilbert@interlog.com> (raw)
In-Reply-To: <20200421215258.14348-1-dgilbert@interlog.com>

Convert sg_device::open_cnt into an atomic. Also rename
sg_tablesize into the more descriptive max_sgat_elems.

**** Reviewed-by: Hannes Reinecke <hare@suse.com>

Signed-off-by: Douglas Gilbert <dgilbert@interlog.com>
---
 drivers/scsi/sg.c | 44 +++++++++++++++++++++++---------------------
 1 file changed, 23 insertions(+), 21 deletions(-)

diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 00fd672c20c6..a0efaf505fc1 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -162,9 +162,9 @@ struct sg_device { /* holds the state of each scsi generic device */
 	struct mutex open_rel_lock;     /* held when in open() or release() */
 	struct list_head sfds;
 	rwlock_t sfd_lock;      /* protect access to sfd list */
-	int sg_tablesize;	/* adapter's max scatter-gather table size */
+	int max_sgat_elems;	/* adapter's max sgat number of elements */
 	u32 index;		/* device index number */
-	int open_cnt;		/* count of opens (perhaps < num(sfds) ) */
+	atomic_t open_cnt;	/* count of opens (perhaps < num(sfds) ) */
 	unsigned long fdev_bm[1];	/* see SG_FDEV_* defines above */
 	struct gendisk *disk;
 	struct cdev * cdev;	/* char_dev [sysfs: /sys/cdev/major/sg<n>] */
@@ -276,11 +276,11 @@ sg_wait_open_event(struct sg_device *sdp, bool o_excl)
 	int retval = 0;
 
 	if (o_excl) {
-		while (sdp->open_cnt > 0) {
+		while (atomic_read(&sdp->open_cnt) > 0) {
 			mutex_unlock(&sdp->open_rel_lock);
 			retval = wait_event_interruptible(sdp->open_wait,
 					(SG_IS_DETACHING(sdp) ||
-					 !sdp->open_cnt));
+					 atomic_read(&sdp->open_cnt) == 0));
 			mutex_lock(&sdp->open_rel_lock);
 
 			if (retval) /* -ERESTARTSYS */
@@ -328,7 +328,7 @@ sg_open(struct inode *inode, struct file *filp)
 	o_excl = !!(op_flags & O_EXCL);
 	if (o_excl && ((op_flags & O_ACCMODE) == O_RDONLY))
 		return -EPERM; /* Can't lock it with read only access */
-	sdp = sg_get_dev(min_dev);
+	sdp = sg_get_dev(min_dev);	/* increments sdp->d_ref */
 	if (IS_ERR(sdp))
 		return PTR_ERR(sdp);
 
@@ -355,7 +355,7 @@ sg_open(struct inode *inode, struct file *filp)
 	mutex_lock(&sdp->open_rel_lock);
 	if (op_flags & O_NONBLOCK) {
 		if (o_excl) {
-			if (sdp->open_cnt > 0) {
+			if (atomic_read(&sdp->open_cnt) > 0) {
 				retval = -EBUSY;
 				goto error_mutex_locked;
 			}
@@ -375,27 +375,29 @@ sg_open(struct inode *inode, struct file *filp)
 	if (o_excl)
 		set_bit(SG_FDEV_EXCLUDE, sdp->fdev_bm);
 
-	if (sdp->open_cnt < 1) {  /* no existing opens */
+	if (atomic_read(&sdp->open_cnt) < 1) {  /* no existing opens */
 		clear_bit(SG_FDEV_LOG_SENSE, sdp->fdev_bm);
 		q = sdp->device->request_queue;
-		sdp->sg_tablesize = queue_max_segments(q);
+		sdp->max_sgat_elems = queue_max_segments(q);
 	}
-	sfp = sg_add_sfp(sdp);
+	sfp = sg_add_sfp(sdp);		/* increments sdp->d_ref */
 	if (IS_ERR(sfp)) {
 		retval = PTR_ERR(sfp);
 		goto out_undo;
 	}
 
 	filp->private_data = sfp;
-	sdp->open_cnt++;
+	atomic_inc(&sdp->open_cnt);
 	mutex_unlock(&sdp->open_rel_lock);
 	SG_LOG(3, sfp, "%s: minor=%d, op_flags=0x%x; %s count prior=%d%s\n",
-	       __func__, min_dev, op_flags, "device open", sdp->open_cnt,
+	       __func__, min_dev, op_flags, "device open",
+	       atomic_read(&sdp->open_cnt),
 	       ((op_flags & O_NONBLOCK) ? " O_NONBLOCK" : ""));
 
 	retval = 0;
 sg_put:
 	kref_put(&sdp->d_ref, sg_device_destroy);
+	/* if success, sdp->d_ref is incremented twice, decremented once */
 	return retval;
 
 out_undo:
@@ -423,20 +425,20 @@ sg_release(struct inode *inode, struct file *filp)
 	sfp = filp->private_data;
 	sdp = sfp->parentdp;
 	SG_LOG(3, sfp, "%s: device open count prior=%d\n", __func__,
-	       sdp->open_cnt);
+	       atomic_read(&sdp->open_cnt));
 	if (!sdp)
 		return -ENXIO;
 
 	mutex_lock(&sdp->open_rel_lock);
 	scsi_autopm_put_device(sdp->device);
 	kref_put(&sfp->f_ref, sg_remove_sfp);
-	sdp->open_cnt--;
+	atomic_dec(&sdp->open_cnt);
 
 	/* possibly many open()s waiting on exlude clearing, start many;
 	 * only open(O_EXCL)s wait on 0==open_cnt so only start one */
 	if (test_and_clear_bit(SG_FDEV_EXCLUDE, sdp->fdev_bm))
 		wake_up_interruptible_all(&sdp->open_wait);
-	else if (sdp->open_cnt == 0)
+	else if (atomic_read(&sdp->open_cnt) == 0)
 		wake_up_interruptible(&sdp->open_wait);
 	mutex_unlock(&sdp->open_rel_lock);
 	return 0;
@@ -1106,7 +1108,7 @@ sg_ioctl_common(struct file *filp, struct sg_device *sdp, struct sg_fd *sfp,
 		read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
 		return put_user(val, ip);
 	case SG_GET_SG_TABLESIZE:
-		return put_user(sdp->sg_tablesize, ip);
+		return put_user(sdp->max_sgat_elems, ip);
 	case SG_SET_RESERVED_SIZE:
 		result = get_user(val, ip);
 		if (result)
@@ -1585,7 +1587,7 @@ sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
 	init_waitqueue_head(&sdp->open_wait);
 	clear_bit(SG_FDEV_DETACHING, sdp->fdev_bm);
 	rwlock_init(&sdp->sfd_lock);
-	sdp->sg_tablesize = queue_max_segments(q);
+	sdp->max_sgat_elems = queue_max_segments(q);
 	sdp->index = k;
 	kref_init(&sdp->d_ref);
 	error = 0;
@@ -1987,7 +1989,7 @@ sg_build_indirect(struct sg_scatter_hold *schp, struct sg_fd *sfp,
 		  int buff_size)
 {
 	int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
-	int sg_tablesize = sfp->parentdp->sg_tablesize;
+	int max_sgat_elems = sfp->parentdp->max_sgat_elems;
 	int blk_size = buff_size, order;
 	gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | __GFP_ZERO;
 	struct sg_device *sdp = sfp->parentdp;
@@ -2002,7 +2004,7 @@ sg_build_indirect(struct sg_scatter_hold *schp, struct sg_fd *sfp,
 	       blk_size);
 
 	/* N.B. ret_sz carried into this block ... */
-	mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
+	mx_sc_elems = sg_build_sgat(schp, sfp, max_sgat_elems);
 	if (mx_sc_elems < 0)
 		return mx_sc_elems;	/* most likely -ENOMEM */
 
@@ -2690,9 +2692,9 @@ sg_proc_seq_show_debug(struct seq_file *s, void *v)
 				   scsidp->lun,
 				   scsidp->host->hostt->emulated);
 		}
-		seq_printf(s, " sg_tablesize=%d excl=%d open_cnt=%d\n",
-			   sdp->sg_tablesize, SG_HAVE_EXCLUDE(sdp),
-			   sdp->open_cnt);
+		seq_printf(s, " max_sgat_elems=%d excl=%d open_cnt=%d\n",
+			   sdp->max_sgat_elems, SG_HAVE_EXCLUDE(sdp),
+			   atomic_read(&sdp->open_cnt));
 		sg_proc_debug_helper(s, sdp);
 	}
 	read_unlock(&sdp->sfd_lock);
-- 
2.26.1


  parent reply	other threads:[~2020-04-21 21:53 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-21 21:52 [PATCH v9 00/40] sg: add v4 interface Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 01/40] sg: move functions around Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 02/40] sg: remove typedefs, type+formatting cleanup Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 03/40] sg: sg_log and is_enabled Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 04/40] sg: rework sg_poll(), minor changes Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 05/40] sg: bitops in sg_device Douglas Gilbert
2020-04-21 21:52 ` Douglas Gilbert [this message]
2020-04-21 21:52 ` [PATCH v9 07/40] sg: move header to uapi section Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 08/40] sg: speed sg_poll and sg_get_num_waiting Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 09/40] sg: sg_allow_if_err_recovery and renames Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 10/40] sg: improve naming Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 11/40] sg: change rwlock to spinlock Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 12/40] sg: ioctl handling Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 13/40] sg: split sg_read Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 14/40] sg: sg_common_write add structure for arguments Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 15/40] sg: rework sg_vma_fault Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 16/40] sg: rework sg_mmap Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 17/40] sg: replace sg_allow_access Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 18/40] sg: rework scatter gather handling Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 19/40] sg: introduce request state machine Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 20/40] sg: sg_find_srp_by_id Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 21/40] sg: sg_fill_request_element Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 22/40] sg: printk change %p to %pK Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 23/40] sg: xarray for fds in device Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 24/40] sg: xarray for reqs in fd Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 25/40] sg: replace rq array with lists Douglas Gilbert
2020-04-24 15:21   ` kbuild test robot
2020-04-21 21:52 ` [PATCH v9 26/40] sg: sense buffer rework Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 27/40] sg: add sg v4 interface support Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 28/40] sg: rework debug info Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 29/40] sg: add 8 byte SCSI LUN to sg_scsi_id Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 30/40] sg: expand sg_comm_wr_t Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 31/40] sg: add sg_iosubmit_v3 and sg_ioreceive_v3 ioctls Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 32/40] sg: add some __must_hold macros Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 33/40] sg: move procfs objects to avoid forward decls Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 34/40] sg: protect multiple receivers Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 35/40] sg: first debugfs support Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 36/40] sg: rework mmap support Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 37/40] sg: defang allow_dio Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 38/40] sg: warn v3 write system call users Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 39/40] sg: add mmap_sz tracking Douglas Gilbert
2020-04-21 21:52 ` [PATCH v9 40/40] sg: bump version to 4.0.09 Douglas Gilbert

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=20200421215258.14348-7-dgilbert@interlog.com \
    --to=dgilbert@interlog.com \
    --cc=hare@suse.de \
    --cc=jejb@linux.vnet.ibm.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /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).