Skip to contentSkip to conversation

libfc: fix potential timer list corruption

PATCH In review Hannes Reinecke 2 participants 5 messages May 30, 2017 – May 31, 2017

Subsystems (2)
lore
Share & tools
lore

Hannes Reinecke

author maintainer
#0 lore raw

cover libfc: fix potential timer list corruption

Hi all,

we've seen reports for a crash with an invalid timer_list->function, which turned out to be an unsafe usage of libfc discovery callbacks. This patchset fixes up the problem

As usual, comments and reviews are welcome.

Hannes Reinecke (3):
libfc: move 'pending' and 'requested' setting
libfc: only restart discovery after timeout if not already running
libfc: fixup locking in fc_disc_stop()

 drivers/scsi/libfc/fc_disc.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
Signature · 1 line
1.8.5.6
author maintainer
#3 lore raw

PATCH 3/3 libfc: fixup locking in fc_disc_stop()

fc_disc_stop() calls fc_disc_stop_rports(), which requires the disc_mutex to be held.
And we need to ensure that no fc_disc_timeout functions are queued after this function, so set the callback to NULL and ensure that all functions terminate early when the callback is not set.

---
 drivers/scsi/libfc/fc_disc.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/scsi/libfc/fc_disc.c +10 -3
index 82b1c97..109174f 100644
@@ -283,6 +283,7 @@ static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
 {
 	struct fc_lport *lport = fc_disc_lport(disc);
 	struct fc_rport_priv *rdata;
+	void (*callback)(struct fc_lport *, enum fc_disc_event);
 
 	FC_DISC_DBG(disc, "Discovery complete\n");
 
@@ -311,8 +312,10 @@ static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
 		kref_put(&rdata->kref, fc_rport_destroy);
 	}
 	rcu_read_unlock();
+	callback = disc->disc_callback;
 	mutex_unlock(&disc->disc_mutex);
-	disc->disc_callback(lport, event);
+	if (callback)
+		callback(lport, event);
 	mutex_lock(&disc->disc_mutex);
 }
 
@@ -711,9 +714,13 @@ static void fc_disc_stop(struct fc_lport *lport)
 {
 	struct fc_disc *disc = &lport->disc;
 
-	if (disc->pending)
-		cancel_delayed_work_sync(&disc->disc_work);
+	mutex_lock(&disc->disc_mutex);
+	disc->disc_callback = NULL;
+	mutex_unlock(&disc->disc_mutex);
+	cancel_delayed_work_sync(&disc->disc_work);
+	mutex_lock(&disc->disc_mutex);
 	fc_disc_stop_rports(disc);
+	mutex_unlock(&disc->disc_mutex);
 }
 
 /**
Signature · 1 line
1.8.5.6
author maintainer
#1 lore raw

PATCH 1/3 libfc: move 'pending' and 'requested' setting

In reply to Hannes Reinecke

Move 'pending' and 'requested' setting out of fc_disc_gpn_fc_req() into the calling function.

No functional change.

---
 drivers/scsi/libfc/fc_disc.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/scsi/libfc/fc_disc.c +6 -3
index fd501f8..913beb5 100644
@@ -242,6 +242,9 @@ static void fc_disc_restart(struct fc_disc *disc)
 	 */
 	disc->disc_id = (disc->disc_id + 2) | 1;
 	disc->retry_count = 0;
+	disc->pending = 1;
+	disc->requested = 0;
+
 	fc_disc_gpn_ft_req(disc);
 }
 
@@ -371,9 +374,6 @@ static void fc_disc_gpn_ft_req(struct fc_disc *disc)
 
 	WARN_ON(!fc_lport_test_ready(lport));
 
-	disc->pending = 1;
-	disc->requested = 0;
-
 	disc->buf_len = 0;
 	disc->seq_count = 0;
 	fp = fc_frame_alloc(lport,
@@ -503,6 +503,9 @@ static void fc_disc_timeout(struct work_struct *work)
 					    struct fc_disc,
 					    disc_work.work);
 	mutex_lock(&disc->disc_mutex);
+	disc->pending = 1;
+	disc->requested = 0;
+
 	fc_disc_gpn_ft_req(disc);
 	mutex_unlock(&disc->disc_mutex);
 }
Signature · 1 line
1.8.5.6
author maintainer
#2 lore raw

PATCH 2/3 libfc: only restart discovery after timeout if not already running

In reply to Hannes Reinecke

If a discovery is already running (ie if the 'pending' flags is set) there is no need to restart discovery from the timeout handler.

---
 drivers/scsi/libfc/fc_disc.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/scsi/libfc/fc_disc.c +5 -3
index 913beb5..82b1c97 100644
@@ -503,10 +503,12 @@ static void fc_disc_timeout(struct work_struct *work)
 					    struct fc_disc,
 					    disc_work.work);
 	mutex_lock(&disc->disc_mutex);
-	disc->pending = 1;
-	disc->requested = 0;
+	if (!disc->pending) {
+		disc->pending = 1;
+		disc->requested = 0;
 
-	fc_disc_gpn_ft_req(disc);
+		fc_disc_gpn_ft_req(disc);
+	}
 	mutex_unlock(&disc->disc_mutex);
 }
 
Signature · 1 line
1.8.5.6
Acked-by on the v1 series
#4 lore raw
In reply to Hannes Reinecke
Quoted 7 lines · Hannes Reinecke

Hi all,

we've seen reports for a crash with an invalid timer_list->function, which turned out to be an unsafe usage of libfc discovery callbacks. This patchset fixes up the problem

As usual, comments and reviews are welcome.

The patches look good themselves, but I'd prefer a *why* in the commit message. For instance in patch 1 you have:

"Move 'pending' and 'requested' setting out of fc_disc_gpn_fc_req() into the calling function.

No functional change."

But why do you need to move the 'pending' and 'requested' settings?

With an a bit more verbose changelog

for the whole series

Signature · 6 lines
Johannes Thumshirn                                          Storage
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850