Skip to contentSkip to conversation

net/smc: cancel event worker during device removal

PATCH Superseded Karsten Graul 2 participants 6 messages Mar 6, 2020 – Mar 9, 2020

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/smc: cancel event worker during device removal

During IB device removal, cancel the event worker before the device structure is freed. In the worker, check if the device is being terminated and do not proceed with the event work in that case.

Fixes a4cf0443c414 ("smc: introduce SMC as an IB-client")
Reported-by [email protected]
---
 net/smc/smc_ib.c | 4 ++++
 1 file changed, 4 insertions(+)
net/smc/smc_ib.c +4 -0
index d6ba186f67e2..5e4e64a9aa4b 100644
@@ -240,6 +240,9 @@ static void smc_ib_port_event_work(struct work_struct *work)
 		work, struct smc_ib_device, port_event_work);
 	u8 port_idx;
 
+	if (list_empty(&smcibdev->list))
+		return;
+
 	for_each_set_bit(port_idx, &smcibdev->port_event_mask, SMC_MAX_PORTS) {
 		smc_ib_remember_port_attr(smcibdev, port_idx + 1);
 		clear_bit(port_idx, &smcibdev->port_event_mask);
@@ -582,6 +585,7 @@ static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data)
 	smc_smcr_terminate_all(smcibdev);
 	smc_ib_cleanup_per_ibdev(smcibdev);
 	ib_unregister_event_handler(&smcibdev->event_handler);
+	cancel_work_sync(&smcibdev->port_event_work);
 	kfree(smcibdev);
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 17 lines · Karsten Graul

During IB device removal, cancel the event worker before the device structure is freed. In the worker, check if the device is being terminated and do not proceed with the event work in that case.

Fixes a4cf0443c414 ("smc: introduce SMC as an IB-client")
Reported-by [email protected]
---
 net/smc/smc_ib.c | 4 ++++
 1 file changed, 4 insertions(+)
net/smc/smc_ib.c +0 -0
index d6ba186f67e2..5e4e64a9aa4b 100644

net/smc/smc_ib.c static void smc_ib_port_event_work(struct work_struct *work) · line 245

Quoted 6 lines
net/smc/smc_ib.c +3 -0
@@ -240,6 +240,9 @@ static void smc_ib_port_event_work(struct work_struct *work)
 		work, struct smc_ib_device, port_event_work);
 	u8 port_idx;
 
+	if (list_empty(&smcibdev->list))
+		return;
+

How can it be true if you are not holding "smc_ib_devices.lock" during execution of smc_ib_port_event_work()?

Quoted 14 lines
net/smc/smc_ib.c +1 -1
 	for_each_set_bit(port_idx, &smcibdev->port_event_mask, SMC_MAX_PORTS) {
 		smc_ib_remember_port_attr(smcibdev, port_idx + 1);
 		clear_bit(port_idx, &smcibdev->port_event_mask);
@@ -582,6 +585,7 @@ static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data)
 	smc_smcr_terminate_all(smcibdev);
 	smc_ib_cleanup_per_ibdev(smcibdev);
 	ib_unregister_event_handler(&smcibdev->event_handler);
+	cancel_work_sync(&smcibdev->port_event_work);
 	kfree(smcibdev);
 }
 
--

2.17.1

#2 lore raw
Quoted 21 lines · Leon Romanovsky
Quoted 20 lines · Karsten Graul

During IB device removal, cancel the event worker before the device structure is freed. In the worker, check if the device is being terminated and do not proceed with the event work in that case.

Fixes a4cf0443c414 ("smc: introduce SMC as an IB-client")
Reported-by [email protected]
---
 net/smc/smc_ib.c | 4 ++++
 1 file changed, 4 insertions(+)
net/smc/smc_ib.c +0 -0
index d6ba186f67e2..5e4e64a9aa4b 100644
@@ -240,6 +240,9 @@ static void smc_ib_port_event_work(struct work_struct *work)
 		work, struct smc_ib_device, port_event_work);
 	u8 port_idx;
 
Quoted 7 lines

net/smc/smc_ib.c static void smc_ib_port_event_work(struct work_struct *work) · line 245

Quoted 3 lines
net/smc/smc_ib.c +3 -0
+	if (list_empty(&smcibdev->list))
+		return;
+

How can it be true if you are not holding "smc_ib_devices.lock" during execution of smc_ib_port_event_work()?

It is true when smc_ib_remove_dev() runs before the work actually started. Other than that its only a shortcut to return earlier, when the item is removed from the list after the check then the processing just takes a little bit longer...its still save.

Quoted 14 lines
Quoted 14 lines
net/smc/smc_ib.c +1 -1
 	for_each_set_bit(port_idx, &smcibdev->port_event_mask, SMC_MAX_PORTS) {
 		smc_ib_remember_port_attr(smcibdev, port_idx + 1);
 		clear_bit(port_idx, &smcibdev->port_event_mask);
@@ -582,6 +585,7 @@ static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data)
 	smc_smcr_terminate_all(smcibdev);
 	smc_ib_cleanup_per_ibdev(smcibdev);
 	ib_unregister_event_handler(&smcibdev->event_handler);
+	cancel_work_sync(&smcibdev->port_event_work);
 	kfree(smcibdev);
 }
 
--

2.17.1

Signature · 3 lines
Karsten

(I'm a dude)
#3 lore raw
Quoted 30 lines · Karsten Graul
Quoted 21 lines · Leon Romanovsky
Quoted 20 lines · Karsten Graul

During IB device removal, cancel the event worker before the device structure is freed. In the worker, check if the device is being terminated and do not proceed with the event work in that case.

Fixes a4cf0443c414 ("smc: introduce SMC as an IB-client")
Reported-by [email protected]
---
 net/smc/smc_ib.c | 4 ++++
 1 file changed, 4 insertions(+)
net/smc/smc_ib.c +0 -0
index d6ba186f67e2..5e4e64a9aa4b 100644
@@ -240,6 +240,9 @@ static void smc_ib_port_event_work(struct work_struct *work)
 		work, struct smc_ib_device, port_event_work);
 	u8 port_idx;
 
Quoted 7 lines

net/smc/smc_ib.c static void smc_ib_port_event_work(struct work_struct *work) · line 245

Quoted 3 lines
net/smc/smc_ib.c +3 -0
+	if (list_empty(&smcibdev->list))
+		return;
+

How can it be true if you are not holding "smc_ib_devices.lock" during execution of smc_ib_port_event_work()?

Quoted 4 lines

It is true when smc_ib_remove_dev() runs before the work actually started. Other than that its only a shortcut to return earlier, when the item is removed from the list after the check then the processing just takes a little bit longer...its still save.

The check itself maybe safe, but it can't fix syzkaller bug reported above. As you said, the smc_ib_remove_dev() can be called immediately after your list_empty() check and we return to original behavior.

The correct design will be to ensure that smc_ib_port_event_work() is executed only smcibdev->list is not empty.

Thanks

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
net/smc/smc_ib.c +1 -0
 	for_each_set_bit(port_idx, &smcibdev->port_event_mask, SMC_MAX_PORTS) {
 		smc_ib_remember_port_attr(smcibdev, port_idx + 1);
 		clear_bit(port_idx, &smcibdev->port_event_mask);
@@ -582,6 +585,7 @@ static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data)
 	smc_smcr_terminate_all(smcibdev);
 	smc_ib_cleanup_per_ibdev(smcibdev);
 	ib_unregister_event_handler(&smcibdev->event_handler);
+	cancel_work_sync(&smcibdev->port_event_work);
 	kfree(smcibdev);
 }
 
Quoted 3 lines
Quoted 3 lines

--
2.17.1

--
Karsten

(I'm a dude)

#4 lore raw
Quoted 36 lines · Leon Romanovsky
Quoted 30 lines · Karsten Graul
Quoted 21 lines · Leon Romanovsky
Quoted 20 lines · Karsten Graul

During IB device removal, cancel the event worker before the device structure is freed. In the worker, check if the device is being terminated and do not proceed with the event work in that case.

Fixes a4cf0443c414 ("smc: introduce SMC as an IB-client")
Reported-by [email protected]
---
 net/smc/smc_ib.c | 4 ++++
 1 file changed, 4 insertions(+)
net/smc/smc_ib.c +0 -0
index d6ba186f67e2..5e4e64a9aa4b 100644
@@ -240,6 +240,9 @@ static void smc_ib_port_event_work(struct work_struct *work)
 		work, struct smc_ib_device, port_event_work);
 	u8 port_idx;
 
Quoted 7 lines

net/smc/smc_ib.c static void smc_ib_port_event_work(struct work_struct *work) · line 245

Quoted 3 lines
net/smc/smc_ib.c +3 -0
+	if (list_empty(&smcibdev->list))
+		return;
+

How can it be true if you are not holding "smc_ib_devices.lock" during execution of smc_ib_port_event_work()?

Quoted 4 lines

It is true when smc_ib_remove_dev() runs before the work actually started. Other than that its only a shortcut to return earlier, when the item is removed from the list after the check then the processing just takes a little bit longer...its still save.

Quoted 9 lines

The check itself maybe safe, but it can't fix syzkaller bug reported above. As you said, the smc_ib_remove_dev() can be called immediately after your list_empty() check and we return to original behavior.

The correct design will be to ensure that smc_ib_port_event_work() is executed only smcibdev->list is not empty.

Thanks

The fix I had in mind was the

	cancel_work_sync(&smcibdev->port_event_work);

to wait for a running port_event_work to finish before smcibdev is freed. I can remove the list_empty() check if that is too confusing.

Quoted 21 lines
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
net/smc/smc_ib.c +1 -0
 	for_each_set_bit(port_idx, &smcibdev->port_event_mask, SMC_MAX_PORTS) {
 		smc_ib_remember_port_attr(smcibdev, port_idx + 1);
 		clear_bit(port_idx, &smcibdev->port_event_mask);
@@ -582,6 +585,7 @@ static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data)
 	smc_smcr_terminate_all(smcibdev);
 	smc_ib_cleanup_per_ibdev(smcibdev);
 	ib_unregister_event_handler(&smcibdev->event_handler);
+	cancel_work_sync(&smcibdev->port_event_work);
 	kfree(smcibdev);
 }
 
Quoted 3 lines
Quoted 3 lines

--
2.17.1

--
Karsten

(I'm a dude)

Signature · 3 lines
Karsten

(I'm a dude)
#5 lore raw
Quoted 47 lines · Karsten Graul
Quoted 36 lines · Leon Romanovsky
Quoted 30 lines · Karsten Graul
Quoted 21 lines · Leon Romanovsky
Quoted 20 lines · Karsten Graul

During IB device removal, cancel the event worker before the device structure is freed. In the worker, check if the device is being terminated and do not proceed with the event work in that case.

Fixes a4cf0443c414 ("smc: introduce SMC as an IB-client")
Reported-by [email protected]
---
 net/smc/smc_ib.c | 4 ++++
 1 file changed, 4 insertions(+)
net/smc/smc_ib.c +0 -0
index d6ba186f67e2..5e4e64a9aa4b 100644
@@ -240,6 +240,9 @@ static void smc_ib_port_event_work(struct work_struct *work)
 		work, struct smc_ib_device, port_event_work);
 	u8 port_idx;
 
Quoted 7 lines

net/smc/smc_ib.c static void smc_ib_port_event_work(struct work_struct *work) · line 245

Quoted 3 lines
net/smc/smc_ib.c +3 -0
+	if (list_empty(&smcibdev->list))
+		return;
+

How can it be true if you are not holding "smc_ib_devices.lock" during execution of smc_ib_port_event_work()?

Quoted 4 lines

It is true when smc_ib_remove_dev() runs before the work actually started. Other than that its only a shortcut to return earlier, when the item is removed from the list after the check then the processing just takes a little bit longer...its still save.

Quoted 9 lines

The check itself maybe safe, but it can't fix syzkaller bug reported above. As you said, the smc_ib_remove_dev() can be called immediately after your list_empty() check and we return to original behavior.

The correct design will be to ensure that smc_ib_port_event_work() is executed only smcibdev->list is not empty.

Thanks

Quoted 6 lines

The fix I had in mind was the

	cancel_work_sync(&smcibdev->port_event_work);

to wait for a running port_event_work to finish before smcibdev is freed. I can remove the list_empty() check if that is too confusing.

Yes, please.

Thanks

Quoted 28 lines
Quoted 20 lines
Quoted 20 lines
Quoted 13 lines
Quoted 13 lines
net/smc/smc_ib.c +1 -0
 	for_each_set_bit(port_idx, &smcibdev->port_event_mask, SMC_MAX_PORTS) {
 		smc_ib_remember_port_attr(smcibdev, port_idx + 1);
 		clear_bit(port_idx, &smcibdev->port_event_mask);
@@ -582,6 +585,7 @@ static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data)
 	smc_smcr_terminate_all(smcibdev);
 	smc_ib_cleanup_per_ibdev(smcibdev);
 	ib_unregister_event_handler(&smcibdev->event_handler);
+	cancel_work_sync(&smcibdev->port_event_work);
 	kfree(smcibdev);
 }
 
Quoted 3 lines
Quoted 3 lines

--
2.17.1

--
Karsten

Quoted 2 lines
Quoted 2 lines

(I'm a dude)

--
Karsten

(I'm a dude)