Patchwork [Open-FCoE,04/13] libfc: Fix fc_exch_find()

login
register
mail settings
Submitter Bart Van Assche
Date Aug. 17, 2013, 12:20 p.m.
Message ID <520F6A77.4050709@acm.org>
Download mbox | patch
Permalink /patch/101/
State Accepted
Headers show

Comments

Bart Van Assche - Aug. 17, 2013, 12:20 p.m.
On 08/14/13 09:34, Bart Van Assche wrote:
> Avoid that fc_exch_find() can return a non-zero exchange pointer if
> the exchange ID does not match. Found this by code inspection.
> 
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>
> Cc: Robert Love <robert.w.love@intel.com>
> Cc: Neil Horman <nhorman@tuxdriver.com>
> ---
>   drivers/scsi/libfc/fc_exch.c |    8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/scsi/libfc/fc_exch.c b/drivers/scsi/libfc/fc_exch.c
> index d0be52a..48f3293 100644
> --- a/drivers/scsi/libfc/fc_exch.c
> +++ b/drivers/scsi/libfc/fc_exch.c
> @@ -836,8 +836,12 @@ static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
>   		pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
>   		spin_lock_bh(&pool->lock);
>   		ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
> -		if (ep && ep->xid == xid)
> -			fc_exch_hold(ep);
> +		if (ep) {
> +			if (ep->xid == xid)
> +				fc_exch_hold(ep);
> +			else
> +				ep = NULL;
> +		}
>   		spin_unlock_bh(&pool->lock);
>   	}
>   	return ep;

(replying to my own e-mail)

After I had posted this patch I realized that the code in fc_exch.c guarantees that ep->xid == xid if ep != NULL. How about replacing the above patch with the patch below ?

[PATCH] libfc: Clarify fc_exch_find()

The condition ep != NULL && ep->xid != xid can never be met. Make
this explicit.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Cc: Robert Love <robert.w.love@intel.com>
Cc: Neil Horman <nhorman@tuxdriver.com>
---
 drivers/scsi/libfc/fc_exch.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Patch

diff --git a/drivers/scsi/libfc/fc_exch.c b/drivers/scsi/libfc/fc_exch.c
index d0be52a..f6bb0fb 100644
--- a/drivers/scsi/libfc/fc_exch.c
+++ b/drivers/scsi/libfc/fc_exch.c
@@ -836,8 +836,10 @@  static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
 		pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
 		spin_lock_bh(&pool->lock);
 		ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
-		if (ep && ep->xid == xid)
+		if (ep) {
+			WARN_ON(ep->xid != xid);
 			fc_exch_hold(ep);
+		}
 		spin_unlock_bh(&pool->lock);
 	}
 	return ep;