Skip to contentSkip to conversation

staging: unisys: visorhba: Convert module from IDR to XArray

PATCH Superseded Fabio M. De Francesco 4 participants 7 messages May 4, 2021 Staging

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/1 staging: unisys: visorhba: Convert module from IDR to XArray

Converted visorhba from IDR to XArray. The abstract data type XArray is more memory-efficient, parallelizable and cache friendly. It takes advantage of RCU to perform lookups without locking. Furthermore, IDR is deprecated because XArray has a better (cleaner and more consistent) API.

---

Changes from v6; Added a call to xa_destroy() that I had forgotten. Fixed taking locks with interrupts disabled (use xa_erase_irq()). Replaced checking for success with checking for failure. Issues detected by Dan Carpenter and Matthew Wilcox.
Changes from v5: As suggested by Matthew Wilcox, reworded the commit message, modified setup_scsitaskmgmt_handles() to manage and return errors, used 'xa_limit_32b' as the range of indexes (because there is no need to use 0 as a marker of no allocation).
Changes from v4: Fixed some issues detected by Matthew Wilcox and Fabio Aiuto.
Changes from v3: Matthew Wilcox found that the XArray was not
initialized: now it is. Changed types handles from u64 to u32 because they can't work as arguments of xa_alloc_irq() and u32 is enough large for storing XArray indexes.
Changes from v2: Some residual errors from v1 were not fixed in v2. Now they have been removed.
Changes from v1: After a first review by Matthew Wilcox, who found a series of errors and gave suggestions on how to fix them, I rewrote a larger part of the patch.

 .../staging/unisys/visorhba/visorhba_main.c   | 102 ++++++++----------
 1 file changed, 43 insertions(+), 59 deletions(-)
drivers/staging/unisys/visorhba/visorhba_main.c +43 -59
index 4455d26f7c96..61b2b133ec33 100644
@@ -6,10 +6,10 @@
 
 #include <linux/debugfs.h>
 #include <linux/kthread.h>
-#include <linux/idr.h>
 #include <linux/module.h>
 #include <linux/seq_file.h>
 #include <linux/visorbus.h>
+#include <linux/xarray.h>
 #include <scsi/scsi.h>
 #include <scsi/scsi_host.h>
 #include <scsi/scsi_cmnd.h>
@@ -82,8 +82,7 @@ struct visorhba_devdata {
 	 * allows us to pass int handles back-and-forth between us and
 	 * iovm, instead of raw pointers
 	 */
-	struct idr idr;
-
+	struct xarray xa;
 	struct dentry *debugfs_dir;
 	struct dentry *debugfs_info;
 };
@@ -182,71 +181,49 @@ static struct uiscmdrsp *get_scsipending_cmdrsp(struct visorhba_devdata *ddata,
 	return NULL;
 }
 
-/*
- * simple_idr_get - Associate a provided pointer with an int value
- *		    1 <= value <= INT_MAX, and return this int value;
- *		    the pointer value can be obtained later by passing
- *		    this int value to idr_find()
- * @idrtable: The data object maintaining the pointer<-->int mappings
- * @p:	      The pointer value to be remembered
- * @lock:     A spinlock used when exclusive access to idrtable is needed
- *
- * Return: The id number mapped to pointer 'p', 0 on failure
- */
-static unsigned int simple_idr_get(struct idr *idrtable, void *p,
-				   spinlock_t *lock)
-{
-	int id;
-	unsigned long flags;
-
-	idr_preload(GFP_KERNEL);
-	spin_lock_irqsave(lock, flags);
-	id = idr_alloc(idrtable, p, 1, INT_MAX, GFP_NOWAIT);
-	spin_unlock_irqrestore(lock, flags);
-	idr_preload_end();
-	/* failure */
-	if (id < 0)
-		return 0;
-	/* idr_alloc() guarantees > 0 */
-	return (unsigned int)(id);
-}
-
 /*
  * setup_scsitaskmgmt_handles - Stash the necessary handles so that the
  *				completion processing logic for a taskmgmt
  *				cmd will be able to find who to wake up
  *				and where to stash the result
- * @idrtable: The data object maintaining the pointer<-->int mappings
- * @lock:     A spinlock used when exclusive access to idrtable is needed
+ * @xa:       The data object maintaining the pointer<-->int mappings
  * @cmdrsp:   Response from the IOVM
  * @event:    The event handle to associate with an id
  * @result:   The location to place the result of the event handle into
  */
-static void setup_scsitaskmgmt_handles(struct idr *idrtable, spinlock_t *lock,
-				       struct uiscmdrsp *cmdrsp,
+static int setup_scsitaskmgmt_handles(struct xarray *xa, struct uiscmdrsp *cmdrsp,
 				       wait_queue_head_t *event, int *result)
 {
-	/* specify the event that has to be triggered when this */
-	/* cmd is complete */
-	cmdrsp->scsitaskmgmt.notify_handle =
-		simple_idr_get(idrtable, event, lock);
-	cmdrsp->scsitaskmgmt.notifyresult_handle =
-		simple_idr_get(idrtable, result, lock);
+	int ret;
+	u32 id;
+
+	/* specify the event that has to be triggered when this cmd is complete */
+	ret = xa_alloc_irq(xa, &id, event, xa_limit_32b, GFP_KERNEL);
+	if (ret) 
+		return ret;
+	else
+		cmdrsp->scsitaskmgmt.notify_handle = id;
+	ret = xa_alloc_irq(xa, &id, result, xa_limit_32b, GFP_KERNEL);
+	if (ret) {
+		xa_erase_irq(xa, cmdrsp->scsitaskmgmt.notify_handle);
+		return ret;
+	} else
+		cmdrsp->scsitaskmgmt.notifyresult_handle = id;
+
+	return 0;
 }
 
 /*
  * cleanup_scsitaskmgmt_handles - Forget handles created by
  *				  setup_scsitaskmgmt_handles()
- * @idrtable: The data object maintaining the pointer<-->int mappings
+ * @xa: The data object maintaining the pointer<-->int mappings
  * @cmdrsp:   Response from the IOVM
  */
-static void cleanup_scsitaskmgmt_handles(struct idr *idrtable,
+static void cleanup_scsitaskmgmt_handles(struct xarray *xa,
 					 struct uiscmdrsp *cmdrsp)
 {
-	if (cmdrsp->scsitaskmgmt.notify_handle)
-		idr_remove(idrtable, cmdrsp->scsitaskmgmt.notify_handle);
-	if (cmdrsp->scsitaskmgmt.notifyresult_handle)
-		idr_remove(idrtable, cmdrsp->scsitaskmgmt.notifyresult_handle);
+	xa_erase_irq(xa, cmdrsp->scsitaskmgmt.notify_handle);
+	xa_erase_irq(xa, cmdrsp->scsitaskmgmt.notifyresult_handle);
 }
 
 /*
@@ -269,6 +246,7 @@ static int forward_taskmgmt_command(enum task_mgmt_types tasktype,
 	int notifyresult = 0xffff;
 	wait_queue_head_t notifyevent;
 	int scsicmd_id;
+	int ret;
 
 	if (devdata->serverdown || devdata->serverchangingstate)
 		return FAILED;
@@ -284,8 +262,14 @@ static int forward_taskmgmt_command(enum task_mgmt_types tasktype,
 
 	/* issue TASK_MGMT_ABORT_TASK */
 	cmdrsp->cmdtype = CMD_SCSITASKMGMT_TYPE;
-	setup_scsitaskmgmt_handles(&devdata->idr, &devdata->privlock, cmdrsp,
-				   &notifyevent, &notifyresult);
+
+	ret = setup_scsitaskmgmt_handles(&devdata->xa, cmdrsp,
+					 &notifyevent, &notifyresult);
+	if (ret) {
+		dev_dbg(&scsidev->sdev_gendev,
+		        "visorhba: setup_scsitaskmgmt_handles returned %d\n", ret);
+		return FAILED;
+	}
 
 	/* save destination */
 	cmdrsp->scsitaskmgmt.tasktype = tasktype;
@@ -311,14 +295,14 @@ static int forward_taskmgmt_command(enum task_mgmt_types tasktype,
 	dev_dbg(&scsidev->sdev_gendev,
 		"visorhba: taskmgmt type=%d success; result=0x%x\n",
 		 tasktype, notifyresult);
-	cleanup_scsitaskmgmt_handles(&devdata->idr, cmdrsp);
+	cleanup_scsitaskmgmt_handles(&devdata->xa, cmdrsp);
 	return SUCCESS;
 
 err_del_scsipending_ent:
 	dev_dbg(&scsidev->sdev_gendev,
 		"visorhba: taskmgmt type=%d not executed\n", tasktype);
 	del_scsipending_ent(devdata, scsicmd_id);
-	cleanup_scsitaskmgmt_handles(&devdata->idr, cmdrsp);
+	cleanup_scsitaskmgmt_handles(&devdata->xa, cmdrsp);
 	return FAILED;
 }
 
@@ -654,13 +638,13 @@ DEFINE_SHOW_ATTRIBUTE(info_debugfs);
  * Service Partition returned the result of the task management
  * command. Wake up anyone waiting for it.
  */
-static void complete_taskmgmt_command(struct idr *idrtable,
+static void complete_taskmgmt_command(struct xarray *xa,
 				      struct uiscmdrsp *cmdrsp, int result)
 {
 	wait_queue_head_t *wq =
-		idr_find(idrtable, cmdrsp->scsitaskmgmt.notify_handle);
+		xa_load(xa, cmdrsp->scsitaskmgmt.notify_handle);
 	int *scsi_result_ptr =
-		idr_find(idrtable, cmdrsp->scsitaskmgmt.notifyresult_handle);
+		xa_load(xa, cmdrsp->scsitaskmgmt.notifyresult_handle);
 	if (unlikely(!(wq && scsi_result_ptr))) {
 		pr_err("visorhba: no completion context; cmd will time out\n");
 		return;
@@ -708,7 +692,7 @@ static void visorhba_serverdown_complete(struct visorhba_devdata *devdata)
 			break;
 		case CMD_SCSITASKMGMT_TYPE:
 			cmdrsp = pendingdel->sent;
-			complete_taskmgmt_command(&devdata->idr, cmdrsp,
+			complete_taskmgmt_command(&devdata->xa, cmdrsp,
 						  TASK_MGMT_FAILED);
 			break;
 		default:
@@ -905,7 +889,7 @@ static void drain_queue(struct uiscmdrsp *cmdrsp,
 			if (!del_scsipending_ent(devdata,
 						 cmdrsp->scsitaskmgmt.handle))
 				break;
-			complete_taskmgmt_command(&devdata->idr, cmdrsp,
+			complete_taskmgmt_command(&devdata->xa, cmdrsp,
 						  cmdrsp->scsitaskmgmt.result);
 		} else if (cmdrsp->cmdtype == CMD_NOTIFYGUEST_TYPE)
 			dev_err_once(&devdata->dev->device,
@@ -1053,7 +1037,7 @@ static int visorhba_probe(struct visor_device *dev)
 	if (err)
 		goto err_debugfs_info;
 
-	idr_init(&devdata->idr);
+	xa_init(&devdata->xa);
 
 	devdata->cmdrsp = kmalloc(sizeof(*devdata->cmdrsp), GFP_ATOMIC);
 	visorbus_enable_channel_interrupts(dev);
@@ -1096,7 +1080,7 @@ static void visorhba_remove(struct visor_device *dev)
 	scsi_remove_host(scsihost);
 	scsi_host_put(scsihost);
 
-	idr_destroy(&devdata->idr);
+	xa_destroy(&devdata->xa);
 
 	dev_set_drvdata(&dev->device, NULL);
 	debugfs_remove(devdata->debugfs_info);
Signature · 1 line
2.31.1
#1 lore raw
Quoted 1 line · Fabio M. De Francesco

Changes from v6; Added a call to xa_destroy() that I had forgotten.

What? No! Go back and re-read what I wrote about this previously.

Quoted 8 lines
drivers/staging/unisys/visorhba/visorhba_main.c +1 -5
+static int setup_scsitaskmgmt_handles(struct xarray *xa, struct uiscmdrsp *cmdrsp,
 				       wait_queue_head_t *event, int *result)
 {
-	/* specify the event that has to be triggered when this */
-	/* cmd is complete */
-	cmdrsp->scsitaskmgmt.notify_handle =
-		simple_idr_get(idrtable, event, lock);
-	cmdrsp->scsitaskmgmt.notifyresult_handle =

drivers/staging/unisys/visorhba/visorhba_main.c static struct uiscmdrsp *get_scsipending_cmdrsp(struct visorhba_devdata *ddata, · line 205

Quoted 10 lines
drivers/staging/unisys/visorhba/visorhba_main.c +9 -1
-		simple_idr_get(idrtable, result, lock);
+	int ret;
+	u32 id;
+
+	/* specify the event that has to be triggered when this cmd is complete */
+	ret = xa_alloc_irq(xa, &id, event, xa_limit_32b, GFP_KERNEL);
+	if (ret) 
+		return ret;
+	else
+		cmdrsp->scsitaskmgmt.notify_handle = id;

This 'else' is actively confusing.

drivers/staging/unisys/visorhba/visorhba_main.c static struct uiscmdrsp *get_scsipending_cmdrsp(struct visorhba_devdata *ddata, · line 211

Quoted 6 lines
drivers/staging/unisys/visorhba/visorhba_main.c +6 -0
+	ret = xa_alloc_irq(xa, &id, result, xa_limit_32b, GFP_KERNEL);
+	if (ret) {
+		xa_erase_irq(xa, cmdrsp->scsitaskmgmt.notify_handle);
+		return ret;
+	} else
+		cmdrsp->scsitaskmgmt.notifyresult_handle = id;

Ditto.

#2 lore raw
Quoted 5 lines · CEST Matthew Wilcox
Quoted 1 line · Fabio M. De Francesco

Changes from v6; Added a call to xa_destroy() that I had forgotten.

What? No! Go back and re-read what I wrote about this previously.

I remember that explanation you gave me some days ago for not using it. But I was mislead by a comment ("Do we not have to call xa_destroy()?") by Dan and your "Correct" soon after the above comment. So I thought that I had misunderstand and the put back that call to xa_destroy(). I lost something in following the flow of the reviews, I suppose.

Quoted 2 lines
Quoted 2 lines

+static int setup_scsitaskmgmt_handles(struct xarray *xa, struct uiscmdrsp

*cmdrsp,

Quoted 2 lines
Quoted 2 lines

wait_queue_head_t *event,

int *result)

Quoted 13 lines

drivers/staging/unisys/visorhba/visorhba_main.c static struct uiscmdrsp *get_scsipending_cmdrsp(struct visorhba_devdata *ddata,

Quoted 13 lines

{

drivers/staging/unisys/visorhba/visorhba_main.c +4 -6
-	/* specify the event that has to be triggered when this */
-	/* cmd is complete */
-	cmdrsp->scsitaskmgmt.notify_handle =
-		simple_idr_get(idrtable, event, lock);
-	cmdrsp->scsitaskmgmt.notifyresult_handle =
-		simple_idr_get(idrtable, result, lock);
+	int ret;
+	u32 id;
+
+	/* specify the event that has to be triggered when this cmd is 

complete */

Quoted 8 lines

drivers/staging/unisys/visorhba/visorhba_main.c static struct uiscmdrsp *get_scsipending_cmdrsp(struct visorhba_devdata *ddata, · line 205

Quoted 5 lines
drivers/staging/unisys/visorhba/visorhba_main.c +5 -0
+	ret = xa_alloc_irq(xa, &id, event, xa_limit_32b, GFP_KERNEL);
+	if (ret)
+		return ret;
+	else
+		cmdrsp->scsitaskmgmt.notify_handle = id;

This 'else' is actively confusing.

Unnecessary and redundant, yes.

Quoted 10 lines

drivers/staging/unisys/visorhba/visorhba_main.c static struct uiscmdrsp *get_scsipending_cmdrsp(struct visorhba_devdata *ddata, · line 211

Quoted 7 lines
drivers/staging/unisys/visorhba/visorhba_main.c +6 -0
+	ret = xa_alloc_irq(xa, &id, result, xa_limit_32b, GFP_KERNEL);
+	if (ret) {
+		xa_erase_irq(xa, cmdrsp->scsitaskmgmt.notify_handle);
+		return ret;
+	} else
+		cmdrsp->scsitaskmgmt.notifyresult_handle = id;

Ditto.

Redundant, again.

Thanks,

Fabio

#3 lore raw
Quoted 11 lines · Fabio M. De Francesco
Quoted 5 lines · CEST Matthew Wilcox
Quoted 1 line · Fabio M. De Francesco

Changes from v6; Added a call to xa_destroy() that I had forgotten.

What? No! Go back and re-read what I wrote about this previously.

I remember that explanation you gave me some days ago for not using it. But I was mislead by a comment ("Do we not have to call xa_destroy()?") by Dan and your "Correct" soon after the above comment. So I thought that I had misunderstand and the put back that call to xa_destroy(). I lost something in following the flow of the reviews, I suppose.

English doesn't have the equivalent of the French 'si' or German 'doch', unfortunately. I imagine Italian does.

#4 lore raw
Quoted 8 lines · CEST Matthew Wilcox
Quoted 7 lines · Fabio M. De Francesco
Quoted 4 lines · CEST Matthew Wilcox
Quoted 1 line · Fabio M. De Francesco

Changes from v6; Added a call to xa_destroy() that I had forgotten.

What? No! Go back and re-read what I wrote about this previously.

I remember that explanation you gave me some days ago for not using it.

But I

Quoted 1 line
Quoted 1 line

was mislead by a comment ("Do we not have to call xa_destroy()?") by Dan

and

Quoted 2 lines
Quoted 2 lines

your "Correct" soon after the above comment. So I thought that I had misunderstand and the put back that call to xa_destroy(). I lost something

in

Quoted 5 lines
Quoted 1 line

following the flow of the reviews, I suppose.

English doesn't have the equivalent of the French 'si' or German 'doch', unfortunately. I imagine Italian does.

Yes, Italian does :-) Despite this, I still think that English is less prone to give raise to misunderstandings (obviously, if_and_only_if one knows how to use the language).

Back to the style of the code... in particular to (1) avoid unnecessary 'else', and to (2) check for failure (i.e., not for success) (3) don't (always) trust checkpatch.pl because often people don't want their code changed according to its output.

Start with (3): Steven Rostedt refused to apply a patch that I did upon a checkpatch warning: "replace "unsigned" with "unsigned int". He says he's perfectly comfortable with 'unsigned' and that he _prefers_ 'unsigned'. That's OK, I think; no problem.

As far as (1) and (2) are regarded, I've been told that when one modifies code she/he should not diverge from the style of the subsystem/driver maintainer/ author. If you look at visorhba_main.c, you'll find a lot of unnecessary 'else' and 'if (success)'...

So what are the general rules one should follow when changing (trivial) Linux code? Please note that my question has no other (hidden) purposes than learning to work properly with the Linux community and to reduce the unnecessary noise consequential to submitting a high number of patch versions.

Thanks a lot for any help,

Fabio

#5 lore raw
Quoted 10 lines · Fabio M. De Francesco

As far as (1) and (2) are regarded, I've been told that when one modifies code she/he should not diverge from the style of the subsystem/driver maintainer/ author. If you look at visorhba_main.c, you'll find a lot of unnecessary 'else' and 'if (success)'...

So what are the general rules one should follow when changing (trivial) Linux code? Please note that my question has no other (hidden) purposes than learning to work properly with the Linux community and to reduce the unnecessary noise consequential to submitting a high number of patch versions.

"trivial" changes should only be done in subsystems that welcome it.

drivers/staging/ welcomes it, anything other than that, you need to ask the maintainer.

thanks,

greg k-h

#6 lore raw
In reply to Fabio M. De Francesco
Quoted 14 lines · Fabio M. De Francesco
drivers/staging/unisys/visorhba/visorhba_main.c +6 -6
+static int setup_scsitaskmgmt_handles(struct xarray *xa, struct uiscmdrsp *cmdrsp,
 				       wait_queue_head_t *event, int *result)
 {
-	/* specify the event that has to be triggered when this */
-	/* cmd is complete */
-	cmdrsp->scsitaskmgmt.notify_handle =
-		simple_idr_get(idrtable, event, lock);
-	cmdrsp->scsitaskmgmt.notifyresult_handle =
-		simple_idr_get(idrtable, result, lock);
+	int ret;
+	u32 id;
+
+	/* specify the event that has to be triggered when this cmd is complete */
+	ret = xa_alloc_irq(xa, &id, event, xa_limit_32b, GFP_KERNEL);

drivers/staging/unisys/visorhba/visorhba_main.c static struct uiscmdrsp *get_scsipending_cmdrsp(struct visorhba_devdata *ddata, · line 211

Quoted 10 lines
drivers/staging/unisys/visorhba/visorhba_main.c +10 -0
+	if (ret) 
+		return ret;
+	else
+		cmdrsp->scsitaskmgmt.notify_handle = id;
+	ret = xa_alloc_irq(xa, &id, result, xa_limit_32b, GFP_KERNEL);
+	if (ret) {
+		xa_erase_irq(xa, cmdrsp->scsitaskmgmt.notify_handle);
+		return ret;
+	} else
+		cmdrsp->scsitaskmgmt.notifyresult_handle = id;

This else statement is not required. Please use the checkpatch script.

	if (ret) {
		xa_erase_irq(xa, cmdrsp->scsitaskmgmt.notify_handle);
		return ret;
	}
	cmdrsp->scsitaskmgmt.notifyresult_handle = id;
Quoted 4 lines
drivers/staging/unisys/visorhba/visorhba_main.c +2 -0
+
+	return 0;
 }

[ snip ]

drivers/staging/unisys/visorhba/visorhba_main.c static void visorhba_remove(struct visor_device *dev) · line 1083

Quoted 6 lines
drivers/staging/unisys/visorhba/visorhba_main.c +1 -1
@@ -1096,7 +1080,7 @@ static void visorhba_remove(struct visor_device *dev)
 	scsi_remove_host(scsihost);
 	scsi_host_put(scsihost);
 
-	idr_destroy(&devdata->idr);
+	xa_destroy(&devdata->xa);

Don't add this. Matthew explained this earlier.

drivers/staging/unisys/visorhba/visorhba_main.c static void visorhba_remove(struct visor_device *dev) · line 1086

Quoted 3 lines
drivers/staging/unisys/visorhba/visorhba_main.c +0 -0
 	dev_set_drvdata(&dev->device, NULL);
 	debugfs_remove(devdata->debugfs_info);

regards,
dan carpenter