Skip to contentSkip to conversation

libfc: Update rport reference counting

RFC PATCH Superseded Hannes Reinecke 2 participants 2 messages May 18, 2016 – May 23, 2016

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hannes Reinecke

author maintainer
#0 lore raw

RFC PATCH 1/1 libfc: Update rport reference counting

Originally libfc would just be initializing the refcount to '1', and using the disc_mutex to synchronize if and when the final put should be happening.
This has a race condition as the mutex might be delayed, causing other threads to access an invalid structure.
This patch updates the rport reference counting to increase the reference every time 'rport_lookup' is called, and decreases the reference correspondingly.
This removes the need to hold 'disc_mutex' when removing the structure, and avoids the above race condition.

---
 drivers/scsi/fcoe/fcoe_ctlr.c |  6 ------
 drivers/scsi/libfc/fc_lport.c | 19 ++++++++++-------
 drivers/scsi/libfc/fc_rport.c | 49 ++++++++++++++++++++++---------------------
 3 files changed, 37 insertions(+), 37 deletions(-)
drivers/scsi/fcoe/fcoe_ctlr.c +0 -6
index 3e83d48..59b8476 100644
@@ -2496,14 +2496,12 @@ static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
 	struct fcoe_rport *frport;
 	int ret = -1;
 
-	rcu_read_lock();
 	rdata = lport->tt.rport_lookup(lport, port_id);
 	if (rdata) {
 		frport = fcoe_ctlr_rport(rdata);
 		memcpy(mac, frport->enode_mac, ETH_ALEN);
 		ret = 0;
 	}
-	rcu_read_unlock();
 	return ret;
 }
 
@@ -2585,11 +2583,7 @@ static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
 		fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
 		return;
 	}
-	mutex_lock(&lport->disc.disc_mutex);
 	rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
-	if (rdata)
-		kref_get(&rdata->kref);
-	mutex_unlock(&lport->disc.disc_mutex);
 	if (rdata) {
 		if (rdata->ids.node_name == new->ids.node_name &&
 		    rdata->ids.port_name == new->ids.port_name) {
drivers/scsi/libfc/fc_lport.c +12 -7
index e01a298..b9b44da 100644
@@ -2090,7 +2090,7 @@ int fc_lport_bsg_request(struct fc_bsg_job *job)
 	struct fc_rport *rport;
 	struct fc_rport_priv *rdata;
 	int rc = -EINVAL;
-	u32 did;
+	u32 did, tov;
 
 	job->reply->reply_payload_rcv_len = 0;
 	if (rsp)
@@ -2121,15 +2121,20 @@ int fc_lport_bsg_request(struct fc_bsg_job *job)
 
 	case FC_BSG_HST_CT:
 		did = ntoh24(job->request->rqst_data.h_ct.port_id);
-		if (did == FC_FID_DIR_SERV)
+		if (did == FC_FID_DIR_SERV) {
 			rdata = lport->dns_rdata;
-		else
+			if (!rdata)
+				break;
+			tov = rdata->e_d_tov;
+		} else {
 			rdata = lport->tt.rport_lookup(lport, did);
+			if (!rdata)
+				break;
+			tov = rdata->e_d_tov;
+			kref_put(&rdata->kref, lport->tt.rport_destroy);
+		}
 
-		if (!rdata)
-			break;
-
-		rc = fc_lport_ct_request(job, lport, did, rdata->e_d_tov);
+		rc = fc_lport_ct_request(job, lport, did, tov);
 		break;
 
 	case FC_BSG_HST_ELS_NOLOGIN:
drivers/scsi/libfc/fc_rport.c +25 -24

153 lines not on this page · raw message

Signature · 1 line
1.8.5.6
#1 lore raw
Quoted 26 lines · Hannes Reinecke

Originally libfc would just be initializing the refcount to '1', and using the disc_mutex to synchronize if and when the final put should be happening.
This has a race condition as the mutex might be delayed, causing other threads to access an invalid structure.
This patch updates the rport reference counting to increase the reference every time 'rport_lookup' is called, and decreases the reference correspondingly.
This removes the need to hold 'disc_mutex' when removing the structure, and avoids the above race condition.

---
 drivers/scsi/fcoe/fcoe_ctlr.c |  6 ------
 drivers/scsi/libfc/fc_lport.c | 19 ++++++++++-------
 drivers/scsi/libfc/fc_rport.c | 49 ++++++++++++++++++++++---------------------
 3 files changed, 37 insertions(+), 37 deletions(-)
drivers/scsi/fcoe/fcoe_ctlr.c +0 -0
index 3e83d48..59b8476 100644
@@ -2496,14 +2496,12 @@ static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
 	struct fcoe_rport *frport;
 	int ret = -1;
 

drivers/scsi/fcoe/fcoe_ctlr.c static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac) · line 2506

Quoted 10 lines
drivers/scsi/fcoe/fcoe_ctlr.c +0 -2
-	rcu_read_lock();
 	rdata = lport->tt.rport_lookup(lport, port_id);
 	if (rdata) {
 		frport = fcoe_ctlr_rport(rdata);
 		memcpy(mac, frport->enode_mac, ETH_ALEN);
 		ret = 0;
 	}
-	rcu_read_unlock();
 	return ret;
 }

Hello Hannes,

Although I'm not very familiar with the SCSI FC code, the approach of this patch makes sense to me. But it's not clear to me which code drops the reference that has been obtained by the rport_lookup() call in the above code?

Bart.