]> Pileus Git - ~andy/linux/blobdiff - drivers/scsi/libfc/fc_exch.c
libfc: Protect ep->esb_stat changes via ex_lock
[~andy/linux] / drivers / scsi / libfc / fc_exch.c
index 587992952b3c390ecad450875b015fc3c45f6bd9..bc0aba4fabb4a44d47f53bb5ee4d1a23f48ccab4 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/slab.h>
 #include <linux/err.h>
 #include <linux/export.h>
+#include <linux/log2.h>
 
 #include <scsi/fc/fc_fc2.h>
 
@@ -303,10 +304,7 @@ static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
                fr_eof(fp) = FC_EOF_N;
        }
 
-       /*
-        * Initialize remainig fh fields
-        * from fc_fill_fc_hdr
-        */
+       /* Initialize remaining fh fields from fc_fill_fc_hdr */
        fh->fh_ox_id = htons(ep->oxid);
        fh->fh_rx_id = htons(ep->rxid);
        fh->fh_seq_id = ep->seq.id;
@@ -362,9 +360,10 @@ static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
 
        FC_EXCH_DBG(ep, "Exchange timer armed : %d msecs\n", timer_msec);
 
-       if (queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
-                              msecs_to_jiffies(timer_msec)))
-               fc_exch_hold(ep);               /* hold for timer */
+       fc_exch_hold(ep);               /* hold for timer */
+       if (!queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
+                               msecs_to_jiffies(timer_msec)))
+               fc_exch_release(ep);
 }
 
 /**
@@ -838,8 +837,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;
@@ -987,6 +988,7 @@ static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
                }
        }
 
+       spin_lock_bh(&ep->ex_lock);
        /*
         * At this point, we have the exchange held.
         * Find or create the sequence.
@@ -1014,11 +1016,11 @@ static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
                                 * sending RSP, hence write request on other
                                 * end never finishes.
                                 */
-                               spin_lock_bh(&ep->ex_lock);
                                sp->ssb_stat |= SSB_ST_RESP;
                                sp->id = fh->fh_seq_id;
-                               spin_unlock_bh(&ep->ex_lock);
                        } else {
+                               spin_unlock_bh(&ep->ex_lock);
+
                                /* sequence/exch should exist */
                                reject = FC_RJT_SEQ_ID;
                                goto rel;
@@ -1029,6 +1031,7 @@ static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
 
        if (f_ctl & FC_FC_SEQ_INIT)
                ep->esb_stat |= ESB_ST_SEQ_INIT;
+       spin_unlock_bh(&ep->ex_lock);
 
        fr_seq(fp) = sp;
 out:
@@ -1478,8 +1481,11 @@ static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
 
        f_ctl = ntoh24(fh->fh_f_ctl);
        fr_seq(fp) = sp;
+
+       spin_lock_bh(&ep->ex_lock);
        if (f_ctl & FC_FC_SEQ_INIT)
                ep->esb_stat |= ESB_ST_SEQ_INIT;
+       spin_unlock_bh(&ep->ex_lock);
 
        if (fc_sof_needs_ack(sof))
                fc_seq_send_ack(sp, fp);
@@ -1662,7 +1668,7 @@ static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
                        break;
                default:
                        if (ep)
-                               FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
+                               FC_EXCH_DBG(ep, "BLS rctl %x - %s received\n",
                                            fh->fh_r_ctl,
                                            fc_exch_rctl_name(fh->fh_r_ctl));
                        break;
@@ -1956,13 +1962,13 @@ static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
 
        switch (op) {
        case ELS_LS_RJT:
-               FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
+               FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ\n");
                /* fall through */
        case ELS_LS_ACC:
                goto cleanup;
        default:
-               FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
-                           "for RRQ", op);
+               FC_EXCH_DBG(aborted_ep, "unexpected response op %x for RRQ\n",
+                           op);
                return;
        }
 
@@ -2533,13 +2539,8 @@ int fc_setup_exch_mgr(void)
         * cpu on which exchange originated by simple bitwise
         * AND operation between fc_cpu_mask and exchange id.
         */
-       fc_cpu_mask = 1;
-       fc_cpu_order = 0;
-       while (fc_cpu_mask < nr_cpu_ids) {
-               fc_cpu_mask <<= 1;
-               fc_cpu_order++;
-       }
-       fc_cpu_mask--;
+       fc_cpu_order = ilog2(roundup_pow_of_two(nr_cpu_ids));
+       fc_cpu_mask = (1 << fc_cpu_order) - 1;
 
        fc_exch_workqueue = create_singlethread_workqueue("fc_exch_workqueue");
        if (!fc_exch_workqueue)