Re: [Outreachy kernel] [PATCH v5] staging: unisys: visorhba: Convert module from IDR to XArray

From: Matthew Wilcox
Date: Mon May 03 2021 - 14:27:27 EST


On Tue, Apr 27, 2021 at 05:07:19PM +0200, Fabio M. De Francesco wrote:
> Converted visorhba from IDR to XArray. The abstract data type XArray is
> more memory-efficient, parallelisable and cache friendly. It takes
> advantage of RCU to perform lookups without locking.

I think the commit message could use a little more work. The advantage
of the XArray over the IDR is that it has a better API (and the IDR
interface is deprecated).

> -static void setup_scsitaskmgmt_handles(struct idr *idrtable, spinlock_t *lock,
> - struct uiscmdrsp *cmdrsp,
> +static void 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(1, INT_MAX), GFP_KERNEL);
> + if (ret == 0)
> + cmdrsp->scsitaskmgmt.notify_handle = id;
> + ret = xa_alloc_irq(xa, &id, result, XA_LIMIT(1, INT_MAX), GFP_KERNEL);
> + if (ret == 0)
> + cmdrsp->scsitaskmgmt.notifyresult_handle = id;
> }

I think there's a preexisting bug here that you haven't fixed ;-)

Think through the failure case -- if we fail to allocate an ID, then we
can't send the command, because it won't be able to notify on completion.
So I'd start out by making this function return an int (0 on success,
errno on failure). Then if the first one succeeds and the second fails,
free the first one before returning an error.

> /*
> * 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);
> + xa_erase(xa, cmdrsp->scsitaskmgmt.notify_handle);
> if (cmdrsp->scsitaskmgmt.notifyresult_handle)
> - idr_remove(idrtable, cmdrsp->scsitaskmgmt.notifyresult_handle);
> + xa_erase(xa, cmdrsp->scsitaskmgmt.notifyresult_handle);
> }

These can then be unconditional, because cleanup_scsitaskmgmt_handles()
won't get called unless we sent the command.

> /*
> @@ -284,8 +257,7 @@ 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);
> + setup_scsitaskmgmt_handles(&devdata->xa, cmdrsp, &notifyevent, &notifyresult);

This needs to check the result from setup() and decline to send the
command if it fails.

> /* save destination */
> cmdrsp->scsitaskmgmt.tasktype = tasktype;
> @@ -311,14 +283,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);

... be sure not to call cleanup() on that path, though.

> return FAILED;
> }
>