Skip to contentSkip to conversation

common: add safe version of foreach-list to Linux

PATCH In review Hamza Khan 5 participants 19 messages Jun 5, 2022 – Oct 5, 2022

lore
Share & tools
lore

Hamza Khan

author
#0 lore raw

PATCH 1/2 common: add safe version of foreach-list to Linux

Linux EAL does not have the LIST_FOREACH_SAFE version of the iterator macros. Add it.

---
 lib/eal/linux/include/rte_os.h | 7 +++++++
 1 file changed, 7 insertions(+)
lib/eal/linux/include/rte_os.h +7 -0
index c72bf5b7e6..00d7714181 100644
@@ -26,6 +26,13 @@ extern "C" {
 #define RTE_STAILQ_HEAD(name, type) STAILQ_HEAD(name, type)
 #define RTE_STAILQ_ENTRY(type) STAILQ_ENTRY(type)
 
+#ifndef LIST_FOREACH_SAFE
+#define	LIST_FOREACH_SAFE(var, head, field, tvar)			\
+	for ((var) = LIST_FIRST((head));				\
+	    (var) && ((tvar) = LIST_NEXT((var), field), 1);		\
+	    (var) = (tvar))
+#endif
+
 #ifdef CPU_SETSIZE /* may require _GNU_SOURCE */
 typedef cpu_set_t rte_cpuset_t;
 #define RTE_HAS_CPUSET
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/2 examples/vm_power_manager: use safe version of list iterator

Currently, when vm_power_manager exits, we are using a LIST_FOREACH macro to iterate over VM info structures while freeing them. This leads to use-after-free error. To address this, use the newly added LIST_FOREACH_SAFE macro.

Fixes e8ae9b662506 ("examples/vm_power: channel manager and monitor in host")
---
 examples/vm_power_manager/channel_manager.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
examples/vm_power_manager/channel_manager.c +2 -2
index 838465ab4b..bc95cec8d6 100644
@@ -1005,9 +1005,9 @@ channel_manager_exit(void)
 {
 	unsigned i;
 	char mask[RTE_MAX_LCORE];
-	struct virtual_machine_info *vm_info;
+	struct virtual_machine_info *vm_info, *tmp;
 
-	LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
+	LIST_FOREACH_SAFE(vm_info, &vm_list_head, vms_info, tmp) {
 
 		rte_spinlock_lock(&(vm_info->config_spinlock));
 
Signature · 1 line
2.25.1
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] examples/vm_power_manager: use safe version of list iterator

Quoted 20 lines · Hamza Khan

Currently, when vm_power_manager exits, we are using a LIST_FOREACH macro to iterate over VM info structures while freeing them. This leads to use-after-free error. To address this, use the newly added LIST_FOREACH_SAFE macro.

Fixes e8ae9b662506 ("examples/vm_power: channel manager and monitor in host")
---
  examples/vm_power_manager/channel_manager.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
examples/vm_power_manager/channel_manager.c +0 -0
index 838465ab4b..bc95cec8d6 100644
@@ -1005,9 +1005,9 @@ channel_manager_exit(void)
  {

examples/vm_power_manager/channel_manager.c channel_manager_exit(void) · line 1013

Quoted 10 lines
examples/vm_power_manager/channel_manager.c +2 -2
  	unsigned i;
  	char mask[RTE_MAX_LCORE];
-	struct virtual_machine_info *vm_info;
+	struct virtual_machine_info *vm_info, *tmp;
 
-	LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
+	LIST_FOREACH_SAFE(vm_info, &vm_list_head, vms_info, tmp) {
 
  		rte_spinlock_lock(&(vm_info->config_spinlock));
 
Tested-by
#3 lore raw

RE: [PATCH 2/2] examples/vm_power_manager: use safe version of list iterator

Quoted 40 lines

-----Original Message-----
From: Hunt, David <[email protected]>
Sent: Monday, July 4, 2022 8:32 PM
To: Khan, Hamza <[email protected]>; Carew, Alan <[email protected]>; De Lara Guarch, Pablo <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH 2/2] examples/vm_power_manager: use safe version of list iterator

Quoted 6 lines · Hamza Khan

Currently, when vm_power_manager exits, we are using a LIST_FOREACH macro to iterate over VM info structures while freeing them. This leads to use-after-free error. To address this, use the newly added LIST_FOREACH_SAFE macro.

Fixes e8ae9b662506 ("examples/vm_power: channel manager and

monitor

Quoted 21 lines

in host")
Cc: [email protected]
Cc: [email protected]

---
  examples/vm_power_manager/channel_manager.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
examples/vm_power_manager/channel_manager.c +1 -1
index 838465ab4b..bc95cec8d6 100644
@@ -1005,9 +1005,9 @@ channel_manager_exit(void)
  {
  	unsigned i;
  	char mask[RTE_MAX_LCORE];
-	struct virtual_machine_info *vm_info;
+	struct virtual_machine_info *vm_info, *tmp;
 
Quoted 9 lines

examples/vm_power_manager/channel_manager.c channel_manager_exit(void) · line 1013

Quoted 5 lines
examples/vm_power_manager/channel_manager.c +1 -1
-	LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
+	LIST_FOREACH_SAFE(vm_info, &vm_list_head, vms_info, tmp) {
 
  		rte_spinlock_lock(&(vm_info->config_spinlock));
 
#4 lore raw
In reply to Hamza Khan

01/06/2022 12:54, Hamza Khan:

Quoted 13 lines

Linux EAL does not have the LIST_FOREACH_SAFE version of the iterator macros. Add it.

---
 lib/eal/linux/include/rte_os.h | 7 +++++++
 1 file changed, 7 insertions(+)
lib/eal/linux/include/rte_os.h +0 -0
index c72bf5b7e6..00d7714181 100644

lib/eal/linux/include/rte_os.h extern "C" { · line 34

Quoted 9 lines
lib/eal/linux/include/rte_os.h +6 -0
@@ -26,6 +26,13 @@ extern "C" {
 #define RTE_STAILQ_HEAD(name, type) STAILQ_HEAD(name, type)
 #define RTE_STAILQ_ENTRY(type) STAILQ_ENTRY(type)
 
+#ifndef LIST_FOREACH_SAFE
+#define	LIST_FOREACH_SAFE(var, head, field, tvar)			\
+	for ((var) = LIST_FIRST((head));				\
+	    (var) && ((tvar) = LIST_NEXT((var), field), 1);		\
+	    (var) = (tvar))
+#endif

I'm not sure we want to add such thing without a RTE_ prefix. And we should not need LIST_*, we have RTE_TAILQ_*.

#5 lore raw
Quoted 30 lines

-----Original Message-----
From: Thomas Monjalon <[email protected]>
Sent: Tuesday 5 July 2022 17:16
To: Khan, Hamza <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH 1/2] common: add safe version of foreach-list to Linux

01/06/2022 12:54, Hamza Khan:

Quoted 13 lines

Linux EAL does not have the LIST_FOREACH_SAFE version of the iterator macros. Add it.

---
 lib/eal/linux/include/rte_os.h | 7 +++++++
 1 file changed, 7 insertions(+)
(quoted diff) +0 -0

b/lib/eal/linux/include/rte_os.h index c72bf5b7e6..00d7714181 100644

lib/eal/linux/include/rte_os.h extern "C" { · line 26

Quoted 1 line
lib/eal/linux/include/rte_os.h +0 -0
@@ -26,6 +26,13 @@ extern "C" {
 #define RTE_STAILQ_HEAD(name, type) STAILQ_HEAD(name, type)

#define

lib/eal/linux/include/rte_os.h extern "C" {

Quoted 4 lines
lib/eal/linux/include/rte_os.h +2 -0
 RTE_STAILQ_ENTRY(type) STAILQ_ENTRY(type)
 
+#ifndef LIST_FOREACH_SAFE
+#define	LIST_FOREACH_SAFE(var, head, field, tvar)
	\
Quoted 9 lines

lib/eal/linux/include/rte_os.h extern "C" { · line 34

Quoted 4 lines
lib/eal/linux/include/rte_os.h +4 -0
+	for ((var) = LIST_FIRST((head));				\
+	    (var) && ((tvar) = LIST_NEXT((var), field), 1);		\
+	    (var) = (tvar))
+#endif

I'm not sure we want to add such thing without a RTE_ prefix. And we should not need LIST_*, we have RTE_TAILQ_*.

I have sent v2 patch with the aforementioned fix.
However Is being held until the list moderator can review it for approval

#6 lore raw

07/07/2022 17:59, Khan, Hamza:

Quoted 38 lines
Quoted 29 lines

-----Original Message-----
From: Thomas Monjalon <[email protected]>
Sent: Tuesday 5 July 2022 17:16
To: Khan, Hamza <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH 1/2] common: add safe version of foreach-list to Linux

01/06/2022 12:54, Hamza Khan:

Quoted 13 lines

Linux EAL does not have the LIST_FOREACH_SAFE version of the iterator macros. Add it.

---
 lib/eal/linux/include/rte_os.h | 7 +++++++
 1 file changed, 7 insertions(+)
(quoted diff) +0 -0

b/lib/eal/linux/include/rte_os.h index c72bf5b7e6..00d7714181 100644

lib/eal/linux/include/rte_os.h extern "C" { · line 26

Quoted 1 line
lib/eal/linux/include/rte_os.h +0 -0
@@ -26,6 +26,13 @@ extern "C" {
 #define RTE_STAILQ_HEAD(name, type) STAILQ_HEAD(name, type)

#define

lib/eal/linux/include/rte_os.h extern "C" {

Quoted 4 lines
lib/eal/linux/include/rte_os.h +2 -0
 RTE_STAILQ_ENTRY(type) STAILQ_ENTRY(type)
 
+#ifndef LIST_FOREACH_SAFE
+#define	LIST_FOREACH_SAFE(var, head, field, tvar)
	\
Quoted 9 lines

lib/eal/linux/include/rte_os.h extern "C" { · line 34

Quoted 4 lines
lib/eal/linux/include/rte_os.h +4 -0
+	for ((var) = LIST_FIRST((head));				\
+	    (var) && ((tvar) = LIST_NEXT((var), field), 1);		\
+	    (var) = (tvar))
+#endif

I'm not sure we want to add such thing without a RTE_ prefix. And we should not need LIST_*, we have RTE_TAILQ_*.

Quoted 2 lines

I have sent v2 patch with the aforementioned fix.
However Is being held until the list moderator can review it for approval

I've unblocked it.
This is blocked because you are not registered in the mailing list, so it is considered as spam.

#7 lore raw
Quoted 50 lines

-----Original Message-----
From: Thomas Monjalon <[email protected]>
Sent: Thursday 7 July 2022 20:10
To: Khan, Hamza <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH 1/2] common: add safe version of foreach-list to Linux

07/07/2022 17:59, Khan, Hamza:

Quoted 40 lines
Quoted 40 lines

-----Original Message-----
From: Thomas Monjalon <[email protected]>
Sent: Tuesday 5 July 2022 17:16
To: Khan, Hamza <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH 1/2] common: add safe version of foreach-list to Linux

01/06/2022 12:54, Hamza Khan:

Quoted 14 lines

Linux EAL does not have the LIST_FOREACH_SAFE version of the iterator macros. Add it.

---
 lib/eal/linux/include/rte_os.h | 7 +++++++
 1 file changed, 7 insertions(+)
(quoted diff) +0 -0

b/lib/eal/linux/include/rte_os.h index c72bf5b7e6..00d7714181 100644

lib/eal/linux/include/rte_os.h extern "C" { · line 26

Quoted 1 line
lib/eal/linux/include/rte_os.h +0 -0
@@ -26,6 +26,13 @@ extern "C" {
 #define RTE_STAILQ_HEAD(name, type) STAILQ_HEAD(name, type)

#define

lib/eal/linux/include/rte_os.h extern "C" {

Quoted 4 lines
lib/eal/linux/include/rte_os.h +2 -0
 RTE_STAILQ_ENTRY(type) STAILQ_ENTRY(type)
 
+#ifndef LIST_FOREACH_SAFE
+#define	LIST_FOREACH_SAFE(var, head, field, tvar)
	\

lib/eal/linux/include/rte_os.h extern "C" { · line 34

Quoted 4 lines
lib/eal/linux/include/rte_os.h +4 -0
+	for ((var) = LIST_FIRST((head));				\
+	    (var) && ((tvar) = LIST_NEXT((var), field), 1);		\
+	    (var) = (tvar))
+#endif

I'm not sure we want to add such thing without a RTE_ prefix. And we should not need LIST_*, we have RTE_TAILQ_*.

Quoted 8 lines
Quoted 3 lines

I have sent v2 patch with the aforementioned fix. However Is being held until the list moderator can review it for approval

I've unblocked it.
This is blocked because you are not registered in the mailing list, so it is considered as spam.

The maintainers will only accept fixes into RC4 if they can be confident in the patch. So I have edited the Commit Message and created a V3
I registered with the mailing list yesterday but it seems that it is being held again. Do you have any suggestions on how to avoid this for future patches ? Thanks
Hamza

#8 lore raw

08/07/2022 10:56, Khan, Hamza:

Quoted 6 lines

From: Thomas Monjalon <[email protected]>

Quoted 5 lines

07/07/2022 17:59, Khan, Hamza:

Quoted 3 lines

I have sent v2 patch with the aforementioned fix. However Is being held until the list moderator can review it for approval

Quoted 8 lines
Quoted 4 lines

I've unblocked it.
This is blocked because you are not registered in the mailing list, so it is considered as spam.

The maintainers will only accept fixes into RC4 if they can be confident in the patch. So I have edited the Commit Message and created a V3
I registered with the mailing list yesterday but it seems that it is being held again. Do you have any suggestions on how to avoid this for future patches ?

Once you register, you receive an email with a confirmation link. Look in your spam folder.

#9 lore raw
Quoted 20 lines

-----Original Message-----
From: Thomas Monjalon <[email protected]>
Sent: Friday 8 July 2022 10:25
To: Khan, Hamza <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH 1/2] common: add safe version of foreach-list to Linux

08/07/2022 10:56, Khan, Hamza:

Quoted 10 lines

From: Thomas Monjalon <[email protected]>

Quoted 9 lines

07/07/2022 17:59, Khan, Hamza:

Quoted 3 lines

I have sent v2 patch with the aforementioned fix. However Is being held until the list moderator can review it for approval

I've unblocked it.
This is blocked because you are not registered in the mailing list, so it is considered as spam.

Quoted 9 lines
Quoted 1 line

The maintainers will only accept fixes into RC4 if they can be confident in

the patch.

Quoted 3 lines

So I have edited the Commit Message and created a V3 I registered with the mailing list yesterday but it seems that it is being held again. Do you have any suggestions on how to avoid this for future patches ?

Once you register, you receive an email with a confirmation link. Look in your spam folder.

Yes it was found in the junk folder
Thank You

#10 lore raw
In reply to Hamza Khan
Quoted 8 lines

-----Original Message-----
From: Khan, Hamza <[email protected]>
Sent: Wednesday 1 June 2022 11:55
Cc: [email protected]; Khan, Hamza <[email protected]>
Subject: [PATCH 1/2] common: add safe version of foreach-list to Linux

Quoted 4 lines

Linux EAL does not have the LIST_FOREACH_SAFE version of the iterator macros. Add it.

-- Snipped --

Request for review and Ack on this patch
Also Please merge the patch [2/2] examples/vm_power_manager: use safe version of list iterator ( https://patches.dpdk.org/project/dpdk/patch/[email protected]/ )

author
#11 lore raw

PATCH v2 1/1 examples/vm_power_manager: use safe version of list iterator

In reply to Hamza Khan

Currently, when vm_power_manager exits, we are using a LIST_FOREACH macro to iterate over VM info structures while freeing them. This leads to use-after-free error. To address this, replace all usages of LIST_* with TAILQ_* macros, and use the RTE_TAILQ_FOREACH_SAFE macro to iterate and delete VM info structures.

Fixes e8ae9b662506 ("examples/vm_power: channel manager and monitor in host")
---
 examples/vm_power_manager/channel_manager.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
examples/vm_power_manager/channel_manager.c +11 -9
index 838465ab4b..e82c26ddca 100644
@@ -29,6 +29,8 @@
 #include "channel_monitor.h"
 #include "power_manager.h"
 
+#include "rte_tailq.h"
+
 
 #define RTE_LOGTYPE_CHANNEL_MANAGER RTE_LOGTYPE_USER1
 
@@ -58,16 +60,16 @@ struct virtual_machine_info {
 	virDomainInfo info;
 	rte_spinlock_t config_spinlock;
 	int allow_query;
-	LIST_ENTRY(virtual_machine_info) vms_info;
+	RTE_TAILQ_ENTRY(virtual_machine_info) vms_info;
 };
 
-LIST_HEAD(, virtual_machine_info) vm_list_head;
+RTE_TAILQ_HEAD(, virtual_machine_info) vm_list_head;
 
 static struct virtual_machine_info *
 find_domain_by_name(const char *name)
 {
 	struct virtual_machine_info *info;
-	LIST_FOREACH(info, &vm_list_head, vms_info) {
+	RTE_TAILQ_FOREACH(info, &vm_list_head, vms_info) {
 		if (!strncmp(info->name, name, CHANNEL_MGR_MAX_NAME_LEN-1))
 			return info;
 	}
@@ -878,7 +880,7 @@ add_vm(const char *vm_name)
 
 	new_domain->allow_query = 0;
 	rte_spinlock_init(&(new_domain->config_spinlock));
-	LIST_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
+	TAILQ_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
 	return 0;
 }
 
@@ -900,7 +902,7 @@ remove_vm(const char *vm_name)
 		rte_spinlock_unlock(&vm_info->config_spinlock);
 		return -1;
 	}
-	LIST_REMOVE(vm_info, vms_info);
+	TAILQ_REMOVE(&vm_list_head, vm_info, vms_info);
 	rte_spinlock_unlock(&vm_info->config_spinlock);
 	rte_free(vm_info);
 	return 0;
@@ -953,7 +955,7 @@ channel_manager_init(const char *path __rte_unused)
 {
 	virNodeInfo info;
 
-	LIST_INIT(&vm_list_head);
+	TAILQ_INIT(&vm_list_head);
 	if (connect_hypervisor(path) < 0) {
 		global_n_host_cpus = 64;
 		global_hypervisor_available = 0;
@@ -1005,9 +1007,9 @@ channel_manager_exit(void)
 {
 	unsigned i;
 	char mask[RTE_MAX_LCORE];
-	struct virtual_machine_info *vm_info;
+	struct virtual_machine_info *vm_info, *tmp;
 
-	LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
+	RTE_TAILQ_FOREACH_SAFE(vm_info, &vm_list_head, vms_info, tmp) {
 
 		rte_spinlock_lock(&(vm_info->config_spinlock));
 
@@ -1022,7 +1024,7 @@ channel_manager_exit(void)
 		}
 		rte_spinlock_unlock(&(vm_info->config_spinlock));
 
-		LIST_REMOVE(vm_info, vms_info);
+		TAILQ_REMOVE(&vm_list_head, vm_info, vms_info);
 		rte_free(vm_info);
 	}
 
Signature · 1 line
2.25.1
author
#12 lore raw

PATCH v3 1/1 examples/vm_power_manager: use safe version of list iterator

In reply to Hamza Khan

Currently, when vm_power_manager exits, we are using a LIST_FOREACH macro to iterate over VM info structures while freeing them. This leads to use-after-free error. To address this, replace all usages of LIST_* with TAILQ_* macros, and use the RTE_TAILQ_FOREACH_SAFE macro to iterate and delete VM info structures.

* The change is small and doesn’t affect other code
* Testing was performed on the patch

Fixes e8ae9b662506 ("examples/vm_power: channel manager and monitor in host")

---
V3: Update commit message
V2: Use RTE_TAILQ_* marcos

---
 examples/vm_power_manager/channel_manager.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
examples/vm_power_manager/channel_manager.c +11 -9
index 838465ab4b..e82c26ddca 100644
@@ -29,6 +29,8 @@
 #include "channel_monitor.h"
 #include "power_manager.h"
 
+#include "rte_tailq.h"
+
 
 #define RTE_LOGTYPE_CHANNEL_MANAGER RTE_LOGTYPE_USER1
 
@@ -58,16 +60,16 @@ struct virtual_machine_info {
 	virDomainInfo info;
 	rte_spinlock_t config_spinlock;
 	int allow_query;
-	LIST_ENTRY(virtual_machine_info) vms_info;
+	RTE_TAILQ_ENTRY(virtual_machine_info) vms_info;
 };
 
-LIST_HEAD(, virtual_machine_info) vm_list_head;
+RTE_TAILQ_HEAD(, virtual_machine_info) vm_list_head;
 
 static struct virtual_machine_info *
 find_domain_by_name(const char *name)
 {
 	struct virtual_machine_info *info;
-	LIST_FOREACH(info, &vm_list_head, vms_info) {
+	RTE_TAILQ_FOREACH(info, &vm_list_head, vms_info) {
 		if (!strncmp(info->name, name, CHANNEL_MGR_MAX_NAME_LEN-1))
 			return info;
 	}
@@ -878,7 +880,7 @@ add_vm(const char *vm_name)
 
 	new_domain->allow_query = 0;
 	rte_spinlock_init(&(new_domain->config_spinlock));
-	LIST_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
+	TAILQ_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
 	return 0;
 }
 
@@ -900,7 +902,7 @@ remove_vm(const char *vm_name)
 		rte_spinlock_unlock(&vm_info->config_spinlock);
 		return -1;
 	}
-	LIST_REMOVE(vm_info, vms_info);
+	TAILQ_REMOVE(&vm_list_head, vm_info, vms_info);
 	rte_spinlock_unlock(&vm_info->config_spinlock);
 	rte_free(vm_info);
 	return 0;
@@ -953,7 +955,7 @@ channel_manager_init(const char *path __rte_unused)
 {
 	virNodeInfo info;
 
-	LIST_INIT(&vm_list_head);
+	TAILQ_INIT(&vm_list_head);
 	if (connect_hypervisor(path) < 0) {
 		global_n_host_cpus = 64;
 		global_hypervisor_available = 0;
@@ -1005,9 +1007,9 @@ channel_manager_exit(void)
 {
 	unsigned i;
 	char mask[RTE_MAX_LCORE];
-	struct virtual_machine_info *vm_info;
+	struct virtual_machine_info *vm_info, *tmp;
 
-	LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
+	RTE_TAILQ_FOREACH_SAFE(vm_info, &vm_list_head, vms_info, tmp) {
 
 		rte_spinlock_lock(&(vm_info->config_spinlock));
 
@@ -1022,7 +1024,7 @@ channel_manager_exit(void)
 		}
 		rte_spinlock_unlock(&(vm_info->config_spinlock));
 
-		LIST_REMOVE(vm_info, vms_info);
+		TAILQ_REMOVE(&vm_list_head, vm_info, vms_info);
 		rte_free(vm_info);
 	}
 
Signature · 1 line
2.25.1
Acked-by on v3 1/1
#13 lore raw

Re: [PATCH v3] examples/vm_power_manager: use safe version of list iterator

Quoted 95 lines · Hamza Khan

Currently, when vm_power_manager exits, we are using a LIST_FOREACH macro to iterate over VM info structures while freeing them. This leads to use-after-free error. To address this, replace all usages of LIST_* with TAILQ_* macros, and use the RTE_TAILQ_FOREACH_SAFE macro to iterate and delete VM info structures.

* The change is small and doesn’t affect other code
* Testing was performed on the patch

Fixes e8ae9b662506 ("examples/vm_power: channel manager and monitor in host")

---
V3: Update commit message
V2: Use RTE_TAILQ_* marcos

---
  examples/vm_power_manager/channel_manager.c | 20 +++++++++++---------
  1 file changed, 11 insertions(+), 9 deletions(-)
examples/vm_power_manager/channel_manager.c +10 -8
index 838465ab4b..e82c26ddca 100644
@@ -29,6 +29,8 @@
  #include "channel_monitor.h"
  #include "power_manager.h"
 
+#include "rte_tailq.h"
+
 
  #define RTE_LOGTYPE_CHANNEL_MANAGER RTE_LOGTYPE_USER1
 
@@ -58,16 +60,16 @@ struct virtual_machine_info {
  	virDomainInfo info;
  	rte_spinlock_t config_spinlock;
  	int allow_query;
-	LIST_ENTRY(virtual_machine_info) vms_info;
+	RTE_TAILQ_ENTRY(virtual_machine_info) vms_info;
  };
 
-LIST_HEAD(, virtual_machine_info) vm_list_head;
+RTE_TAILQ_HEAD(, virtual_machine_info) vm_list_head;
 
  static struct virtual_machine_info *
  find_domain_by_name(const char *name)
  {
  	struct virtual_machine_info *info;
-	LIST_FOREACH(info, &vm_list_head, vms_info) {
+	RTE_TAILQ_FOREACH(info, &vm_list_head, vms_info) {
  		if (!strncmp(info->name, name, CHANNEL_MGR_MAX_NAME_LEN-1))
  			return info;
  	}
@@ -878,7 +880,7 @@ add_vm(const char *vm_name)
 
  	new_domain->allow_query = 0;
  	rte_spinlock_init(&(new_domain->config_spinlock));
-	LIST_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
+	TAILQ_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
  	return 0;
  }
 
@@ -900,7 +902,7 @@ remove_vm(const char *vm_name)
  		rte_spinlock_unlock(&vm_info->config_spinlock);
  		return -1;
  	}
-	LIST_REMOVE(vm_info, vms_info);
+	TAILQ_REMOVE(&vm_list_head, vm_info, vms_info);
  	rte_spinlock_unlock(&vm_info->config_spinlock);
  	rte_free(vm_info);
  	return 0;
@@ -953,7 +955,7 @@ channel_manager_init(const char *path __rte_unused)
  {
  	virNodeInfo info;
 
-	LIST_INIT(&vm_list_head);
+	TAILQ_INIT(&vm_list_head);
  	if (connect_hypervisor(path) < 0) {
  		global_n_host_cpus = 64;
  		global_hypervisor_available = 0;
@@ -1005,9 +1007,9 @@ channel_manager_exit(void)
  {
  	unsigned i;
  	char mask[RTE_MAX_LCORE];
-	struct virtual_machine_info *vm_info;
+	struct virtual_machine_info *vm_info, *tmp;
 
-	LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
+	RTE_TAILQ_FOREACH_SAFE(vm_info, &vm_list_head, vms_info, tmp) {
 
  		rte_spinlock_lock(&(vm_info->config_spinlock));
 

examples/vm_power_manager/channel_manager.c channel_manager_exit(void) · line 1030

Quoted 8 lines
examples/vm_power_manager/channel_manager.c +1 -1
@@ -1022,7 +1024,7 @@ channel_manager_exit(void)
  		}
  		rte_spinlock_unlock(&(vm_info->config_spinlock));
 
-		LIST_REMOVE(vm_info, vms_info);
+		TAILQ_REMOVE(&vm_list_head, vm_info, vms_info);
  		rte_free(vm_info);
  	}
 
Reviewed-by Acked-by Reshma Pattan on v3 1/1
#14 lore raw

RE: [PATCH v3] examples/vm_power_manager: use safe version of list iterator

In reply to Hamza Khan
Quoted 4 lines

-----Original Message-----
From: Hamza Khan <[email protected]>

<snip>

Quoted 1 line

+#include "rte_tailq.h"

Should include this as #include <rte_tailq.h>

Otherwise looks good. If you are sending next version, include my Ack in the patch.

#15 lore raw

PATCH v4 1/1 examples/vm_power_manager: use safe version of list iterator

In reply to Hamza Khan

From: Hamza Khan <[email protected]>

Currently, when vm_power_manager exits, we are using a LIST_FOREACH macro to iterate over VM info structures while freeing them. This leads to use-after-free error. To address this, replace all usages of LIST_* with TAILQ_* macros, and use the RTE_TAILQ_FOREACH_SAFE macro to iterate and delete VM info structures.

* The change is small and doesn’t affect other code
* Testing was performed on the patch

Fixes e8ae9b662506 ("examples/vm_power: channel manager and monitor in host")

---
v4: fix header file inclusion

---
 examples/vm_power_manager/channel_manager.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
examples/vm_power_manager/channel_manager.c +11 -9
index 838465ab4b..cb872ad2d5 100644
@@ -22,6 +22,7 @@
 #include <rte_mempool.h>
 #include <rte_log.h>
 #include <rte_spinlock.h>
+#include <rte_tailq.h>
 
 #include <libvirt/libvirt.h>
 
@@ -30,6 +31,7 @@
 #include "power_manager.h"
 
 
+
 #define RTE_LOGTYPE_CHANNEL_MANAGER RTE_LOGTYPE_USER1
 
 struct libvirt_vm_info lvm_info[MAX_CLIENTS];
@@ -58,16 +60,16 @@ struct virtual_machine_info {
 	virDomainInfo info;
 	rte_spinlock_t config_spinlock;
 	int allow_query;
-	LIST_ENTRY(virtual_machine_info) vms_info;
+	RTE_TAILQ_ENTRY(virtual_machine_info) vms_info;
 };
 
-LIST_HEAD(, virtual_machine_info) vm_list_head;
+RTE_TAILQ_HEAD(, virtual_machine_info) vm_list_head;
 
 static struct virtual_machine_info *
 find_domain_by_name(const char *name)
 {
 	struct virtual_machine_info *info;
-	LIST_FOREACH(info, &vm_list_head, vms_info) {
+	RTE_TAILQ_FOREACH(info, &vm_list_head, vms_info) {
 		if (!strncmp(info->name, name, CHANNEL_MGR_MAX_NAME_LEN-1))
 			return info;
 	}
@@ -878,7 +880,7 @@ add_vm(const char *vm_name)
 
 	new_domain->allow_query = 0;
 	rte_spinlock_init(&(new_domain->config_spinlock));
-	LIST_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
+	TAILQ_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
 	return 0;
 }
 
@@ -900,7 +902,7 @@ remove_vm(const char *vm_name)
 		rte_spinlock_unlock(&vm_info->config_spinlock);
 		return -1;
 	}
-	LIST_REMOVE(vm_info, vms_info);
+	TAILQ_REMOVE(&vm_list_head, vm_info, vms_info);
 	rte_spinlock_unlock(&vm_info->config_spinlock);
 	rte_free(vm_info);
 	return 0;
@@ -953,7 +955,7 @@ channel_manager_init(const char *path __rte_unused)
 {
 	virNodeInfo info;
 
-	LIST_INIT(&vm_list_head);
+	TAILQ_INIT(&vm_list_head);
 	if (connect_hypervisor(path) < 0) {
 		global_n_host_cpus = 64;
 		global_hypervisor_available = 0;
@@ -1005,9 +1007,9 @@ channel_manager_exit(void)
 {
 	unsigned i;
 	char mask[RTE_MAX_LCORE];
-	struct virtual_machine_info *vm_info;
+	struct virtual_machine_info *vm_info, *tmp;
 
-	LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
+	RTE_TAILQ_FOREACH_SAFE(vm_info, &vm_list_head, vms_info, tmp) {
 
 		rte_spinlock_lock(&(vm_info->config_spinlock));
 
@@ -1022,7 +1024,7 @@ channel_manager_exit(void)
 		}
 		rte_spinlock_unlock(&(vm_info->config_spinlock));
 
-		LIST_REMOVE(vm_info, vms_info);
+		TAILQ_REMOVE(&vm_list_head, vm_info, vms_info);
 		rte_free(vm_info);
 	}
 
Signature · 1 line
2.25.1
Acked-by on v4 1/1
#16 lore raw

Re: [PATCH v4] examples/vm_power_manager: use safe version of list iterator

Quoted 15 lines · Reshma Pattan

From: Hamza Khan <[email protected]>

Currently, when vm_power_manager exits, we are using a LIST_FOREACH macro to iterate over VM info structures while freeing them. This leads to use-after-free error. To address this, replace all usages of LIST_* with TAILQ_* macros, and use the RTE_TAILQ_FOREACH_SAFE macro to iterate and delete VM info structures.

* The change is small and doesn’t affect other code
* Testing was performed on the patch

Fixes e8ae9b662506 ("examples/vm_power: channel manager and monitor in host")
Quoted 7 lines

---
v4: fix header file inclusion
---

--snip--

#17 lore raw

PATCH v5 1/1 examples/vm_power_manager: use safe version of list iterator

In reply to Reshma Pattan

From: Hamza Khan <[email protected]>

Currently, when vm_power_manager exits, we are using a LIST_FOREACH macro to iterate over VM info structures while freeing them. This leads to use-after-free error. To address this, replace all usages of LIST_* with TAILQ_* macros, and use the RTE_TAILQ_FOREACH_SAFE macro to iterate and delete VM info structures.

Fixes e8ae9b662506 ("examples/vm_power: channel manager and monitor in host")
---
 examples/vm_power_manager/channel_manager.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
examples/vm_power_manager/channel_manager.c +11 -9
index 838465ab4b..cb872ad2d5 100644
@@ -22,6 +22,7 @@
 #include <rte_mempool.h>
 #include <rte_log.h>
 #include <rte_spinlock.h>
+#include <rte_tailq.h>
 
 #include <libvirt/libvirt.h>
 
@@ -30,6 +31,7 @@
 #include "power_manager.h"
 
 
+
 #define RTE_LOGTYPE_CHANNEL_MANAGER RTE_LOGTYPE_USER1
 
 struct libvirt_vm_info lvm_info[MAX_CLIENTS];
@@ -58,16 +60,16 @@ struct virtual_machine_info {
 	virDomainInfo info;
 	rte_spinlock_t config_spinlock;
 	int allow_query;
-	LIST_ENTRY(virtual_machine_info) vms_info;
+	RTE_TAILQ_ENTRY(virtual_machine_info) vms_info;
 };
 
-LIST_HEAD(, virtual_machine_info) vm_list_head;
+RTE_TAILQ_HEAD(, virtual_machine_info) vm_list_head;
 
 static struct virtual_machine_info *
 find_domain_by_name(const char *name)
 {
 	struct virtual_machine_info *info;
-	LIST_FOREACH(info, &vm_list_head, vms_info) {
+	RTE_TAILQ_FOREACH(info, &vm_list_head, vms_info) {
 		if (!strncmp(info->name, name, CHANNEL_MGR_MAX_NAME_LEN-1))
 			return info;
 	}
@@ -878,7 +880,7 @@ add_vm(const char *vm_name)
 
 	new_domain->allow_query = 0;
 	rte_spinlock_init(&(new_domain->config_spinlock));
-	LIST_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
+	TAILQ_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
 	return 0;
 }
 
@@ -900,7 +902,7 @@ remove_vm(const char *vm_name)
 		rte_spinlock_unlock(&vm_info->config_spinlock);
 		return -1;
 	}
-	LIST_REMOVE(vm_info, vms_info);
+	TAILQ_REMOVE(&vm_list_head, vm_info, vms_info);
 	rte_spinlock_unlock(&vm_info->config_spinlock);
 	rte_free(vm_info);
 	return 0;
@@ -953,7 +955,7 @@ channel_manager_init(const char *path __rte_unused)
 {
 	virNodeInfo info;
 
-	LIST_INIT(&vm_list_head);
+	TAILQ_INIT(&vm_list_head);
 	if (connect_hypervisor(path) < 0) {
 		global_n_host_cpus = 64;
 		global_hypervisor_available = 0;
@@ -1005,9 +1007,9 @@ channel_manager_exit(void)
 {
 	unsigned i;
 	char mask[RTE_MAX_LCORE];
-	struct virtual_machine_info *vm_info;
+	struct virtual_machine_info *vm_info, *tmp;
 
-	LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
+	RTE_TAILQ_FOREACH_SAFE(vm_info, &vm_list_head, vms_info, tmp) {
 
 		rte_spinlock_lock(&(vm_info->config_spinlock));
 
@@ -1022,7 +1024,7 @@ channel_manager_exit(void)
 		}
 		rte_spinlock_unlock(&(vm_info->config_spinlock));
 
-		LIST_REMOVE(vm_info, vms_info);
+		TAILQ_REMOVE(&vm_list_head, vm_info, vms_info);
 		rte_free(vm_info);
 	}
 
Signature · 1 line
2.31.1
#18 lore raw

Re: [PATCH v5] examples/vm_power_manager: use safe version of list iterator

05/10/2022 00:09, Reshma Pattan:

Quoted 32 lines

From: Hamza Khan <[email protected]>

Currently, when vm_power_manager exits, we are using a LIST_FOREACH macro to iterate over VM info structures while freeing them. This leads to use-after-free error. To address this, replace all usages of LIST_* with TAILQ_* macros, and use the RTE_TAILQ_FOREACH_SAFE macro to iterate and delete VM info structures.

Fixes e8ae9b662506 ("examples/vm_power: channel manager and monitor in host")
---
 examples/vm_power_manager/channel_manager.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
examples/vm_power_manager/channel_manager.c +1 -0
index 838465ab4b..cb872ad2d5 100644
@@ -22,6 +22,7 @@
 #include <rte_mempool.h>
 #include <rte_log.h>
 #include <rte_spinlock.h>
+#include <rte_tailq.h>
 
 #include <libvirt/libvirt.h>
 

examples/vm_power_manager/channel_manager.c line 35

Quoted 5 lines
examples/vm_power_manager/channel_manager.c +1 -0
@@ -30,6 +31,7 @@
 #include "power_manager.h"
 
 
+
 #define RTE_LOGTYPE_CHANNEL_MANAGER RTE_LOGTYPE_USER1

I suppose we don't need a third blank line here.
Removing.

[...]

examples/vm_power_manager/channel_manager.c channel_manager_exit(void) · line 1012

Quoted 2 lines
examples/vm_power_manager/channel_manager.c +1 -1
-	LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
+	RTE_TAILQ_FOREACH_SAFE(vm_info, &vm_list_head, vms_info, tmp) {

Applied, thanks.