Skip to contentSkip to conversation

use C11 atomic builtins for libs

PATCH Superseded Phil Yang 6 participants 27 messages Sep 11, 2020 – Sep 25, 2020

lore
Share & tools
lore

Phil Yang

author
#0 lore raw

cover use C11 atomic builtins for libs

Since rte_atomicXX APIs are not allowed to be used[1][2], use C11 atomic builtins instead in eal, bbdev, power, and ethdev libs.

[1] http://code.dpdk.org/dpdk/latest/source/doc/guides/rel_notes/deprecation.rst#L87
[2] http://code.dpdk.org/dpdk/latest/source/devtools/checkpatches.sh#L80

Phil Yang (4):
eal: use C11 atomic builtins for already initialized check
bbdev: use C11 atomic builtins for device processing counter
power: use C11 atomic builtins for power in use state update
ethdev: use C11 atomic builtins for link status update

 lib/librte_bbdev/rte_bbdev.c            |  5 ++--
 lib/librte_bbdev/rte_bbdev.h            |  4 +--
 lib/librte_eal/freebsd/eal.c            | 18 +++++++------
 lib/librte_eal/linux/eal.c              | 20 ++++++++-------
 lib/librte_ethdev/rte_ethdev_driver.h   | 19 ++++----------
 lib/librte_power/power_acpi_cpufreq.c   | 45 +++++++++++++++++++++++++--------
 lib/librte_power/power_pstate_cpufreq.c | 45 +++++++++++++++++++++++++--------
 7 files changed, 100 insertions(+), 56 deletions(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH 1/4 eal: use C11 atomic builtins for already initialized check

Since rte_atomicXX APIs are not allowed to be used, use C11 builtins to check if EAL is already initialized.

---
 lib/librte_eal/freebsd/eal.c | 18 ++++++++++--------
 lib/librte_eal/linux/eal.c   | 20 +++++++++++---------
 2 files changed, 21 insertions(+), 17 deletions(-)
lib/librte_eal/freebsd/eal.c +10 -8
index 798add0..9f4c7bb 100644
@@ -665,7 +665,8 @@ rte_eal_init(int argc, char **argv)
 {
 	int i, fctret, ret;
 	pthread_t thread_id;
-	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+	static uint32_t run_once;
+	uint32_t has_run = 0;
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 	const struct rte_config *config = rte_eal_get_configuration();
@@ -679,7 +680,8 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	if (!rte_atomic32_test_and_set(&run_once)) {
+	if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+					__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
 		rte_eal_init_alert("already called initialization.");
 		rte_errno = EALREADY;
 		return -1;
@@ -705,7 +707,7 @@ rte_eal_init(int argc, char **argv)
 	if (fctret < 0) {
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -715,20 +717,20 @@ rte_eal_init(int argc, char **argv)
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_trace_init() < 0) {
 		rte_eal_init_alert("Cannot init trace");
 		rte_errno = EFAULT;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -762,7 +764,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_bus_scan()) {
 		rte_eal_init_alert("Cannot scan the buses for devices");
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -790,7 +792,7 @@ rte_eal_init(int argc, char **argv)
 		if (ret < 0) {
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
-			rte_atomic32_clear(&run_once);
+			__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 			return -1;
 		}
 	}
lib/librte_eal/linux/eal.c +11 -9
index 0960f01..82a73ed 100644
@@ -960,7 +960,8 @@ rte_eal_init(int argc, char **argv)
 {
 	int i, fctret, ret;
 	pthread_t thread_id;
-	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+	static uint32_t run_once;
+	uint32_t has_run = 0;
 	const char *p;
 	static char logid[PATH_MAX];
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
@@ -977,7 +978,8 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	if (!rte_atomic32_test_and_set(&run_once)) {
+	if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+					__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
 		rte_eal_init_alert("already called initialization.");
 		rte_errno = EALREADY;
 		return -1;
@@ -1005,14 +1007,14 @@ rte_eal_init(int argc, char **argv)
 	if (fctret < 0) {
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1024,7 +1026,7 @@ rte_eal_init(int argc, char **argv)
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1064,7 +1066,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_bus_scan()) {
 		rte_eal_init_alert("Cannot scan the buses for devices");
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1138,7 +1140,7 @@ rte_eal_init(int argc, char **argv)
 		if (ret < 0) {
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
-			rte_atomic32_clear(&run_once);
+			__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 			return -1;
 		}
 	}
@@ -1162,7 +1164,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_eal_log_init(logid, internal_conf->syslog_facility) < 0) {
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1170,7 +1172,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");
 		rte_errno = EAGAIN;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 #endif
Signature · 1 line
2.7.4
author
#2 lore raw

PATCH 2/4 bbdev: use C11 atomic builtins for device processing counter

In reply to Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for device processing counter.

---
 lib/librte_bbdev/rte_bbdev.c | 5 +++--
 lib/librte_bbdev/rte_bbdev.h | 4 +---
 2 files changed, 4 insertions(+), 5 deletions(-)
lib/librte_bbdev/rte_bbdev.c +3 -2
index a4fdb69..5ba891c 100644
@@ -210,7 +210,7 @@ rte_bbdev_allocate(const char *name)
 		return NULL;
 	}
 
-	rte_atomic16_inc(&bbdev->data->process_cnt);
+	__atomic_add_fetch(&bbdev->data->process_cnt, 1, __ATOMIC_RELAXED);
 	bbdev->data->dev_id = dev_id;
 	bbdev->state = RTE_BBDEV_INITIALIZED;
 
@@ -252,7 +252,8 @@ rte_bbdev_release(struct rte_bbdev *bbdev)
 	}
 
 	/* clear shared BBDev Data if no process is using the device anymore */
-	if (rte_atomic16_dec_and_test(&bbdev->data->process_cnt))
+	if (__atomic_sub_fetch(&bbdev->data->process_cnt, 1,
+			      __ATOMIC_RELAXED) == 0)
 		memset(bbdev->data, 0, sizeof(*bbdev->data));
 
 	memset(bbdev, 0, sizeof(*bbdev));
lib/librte_bbdev/rte_bbdev.h +1 -3
index 5729137..7017124 100644
@@ -33,7 +33,6 @@ extern "C" {
 #include <string.h>
 
 #include <rte_compat.h>
-#include <rte_atomic.h>
 #include <rte_bus.h>
 #include <rte_cpuflags.h>
 #include <rte_memory.h>
@@ -426,8 +425,7 @@ struct rte_bbdev_data {
 	uint16_t dev_id;  /**< Device ID */
 	int socket_id;  /**< NUMA socket that device is on */
 	bool started;  /**< Device run-time state */
-	/** Counter of processes using the device */
-	rte_atomic16_t process_cnt;
+	uint16_t process_cnt;  /** Counter of processes using the device */
 };
 
 /* Forward declarations */
Signature · 1 line
2.7.4
author
#3 lore raw

PATCH 3/4 power: use C11 atomic builtins for power in use state update

In reply to Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for power in use state update.

---
 lib/librte_power/power_acpi_cpufreq.c   | 45 +++++++++++++++++++++++++--------
 lib/librte_power/power_pstate_cpufreq.c | 45 +++++++++++++++++++++++++--------
 2 files changed, 70 insertions(+), 20 deletions(-)
lib/librte_power/power_acpi_cpufreq.c +35 -10
index 583815a..84a9d75 100644
@@ -12,7 +12,6 @@
 #include <signal.h>
 #include <limits.h>
 
-#include <rte_atomic.h>
 #include <rte_memcpy.h>
 #include <rte_memory.h>
 #include <rte_string_fns.h>
@@ -86,7 +85,7 @@ struct rte_power_info {
 	FILE *f;                             /**< FD of scaling_setspeed */
 	char governor_ori[32];               /**< Original governor name */
 	uint32_t curr_idx;                   /**< Freq index in freqs array */
-	volatile uint32_t state;             /**< Power in use state */
+	uint32_t state;                      /**< Power in use state */
 	uint16_t turbo_available;            /**< Turbo Boost available */
 	uint16_t turbo_enable;               /**< Turbo Boost enable/disable */
 } __rte_cache_aligned;
@@ -300,6 +299,7 @@ int
 power_acpi_cpufreq_init(unsigned int lcore_id)
 {
 	struct rte_power_info *pi;
+	uint32_t exp_state;
 
 	if (lcore_id >= RTE_MAX_LCORE) {
 		RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
@@ -308,8 +308,16 @@ power_acpi_cpufreq_init(unsigned int lcore_id)
 	}
 
 	pi = &lcore_power_info[lcore_id];
-	if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
-			== 0) {
+	exp_state = POWER_IDLE;
+	/* The power in use state works as a guard variable between
+	 * the CPU frequency control initialization and exit process.
+	 * The ACQUIRE memory ordering here pairs with the RELEASE
+	 * ordering below as lock to make sure the frequency operations
+	 * in the critical section are done under the correct state.
+	 */
+	if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
+					POWER_ONGOING, 0,
+					__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
 		RTE_LOG(INFO, POWER, "Power management of lcore %u is "
 				"in use\n", lcore_id);
 		return -1;
@@ -346,12 +354,16 @@ power_acpi_cpufreq_init(unsigned int lcore_id)
 
 	RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
 			"power management\n", lcore_id);
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_USED,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return 0;
 
 fail:
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return -1;
 }
@@ -408,6 +420,7 @@ int
 power_acpi_cpufreq_exit(unsigned int lcore_id)
 {
 	struct rte_power_info *pi;
+	uint32_t exp_state;
 
 	if (lcore_id >= RTE_MAX_LCORE) {
 		RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
@@ -415,8 +428,16 @@ power_acpi_cpufreq_exit(unsigned int lcore_id)
 		return -1;
 	}
 	pi = &lcore_power_info[lcore_id];
-	if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
-			== 0) {
+	exp_state = POWER_USED;
+	/* The power in use state works as a guard variable between
+	 * the CPU frequency control initialization and exit process.
+	 * The ACQUIRE memory ordering here pairs with the RELEASE
+	 * ordering below as lock to make sure the frequency operations
+	 * in the critical section are done under the correct state.
+	 */
+	if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
+					POWER_ONGOING, 0,
+					__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
 		RTE_LOG(INFO, POWER, "Power management of lcore %u is "
 				"not used\n", lcore_id);
 		return -1;
@@ -436,12 +457,16 @@ power_acpi_cpufreq_exit(unsigned int lcore_id)
 	RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
 			"'userspace' mode and been set back to the "
 			"original\n", lcore_id);
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_IDLE,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return 0;
 
 fail:
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return -1;
 }
lib/librte_power/power_pstate_cpufreq.c +35 -10

101 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#4 lore raw

PATCH 4/4 ethdev: use C11 atomic builtins for link status update

In reply to Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for link status update.

---
 lib/librte_ethdev/rte_ethdev_driver.h | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)
lib/librte_ethdev/rte_ethdev_driver.h +5 -14
index 13fd049..78590dd 100644
@@ -216,8 +216,7 @@ static inline int
 rte_eth_linkstatus_set(struct rte_eth_dev *dev,
 		       const struct rte_eth_link *new_link)
 {
-	volatile uint64_t *dev_link
-		 = (volatile uint64_t *)&(dev->data->dev_link);
+	uint64_t *dev_link = (uint64_t *)&(dev->data->dev_link);
 	union {
 		uint64_t val64;
 		struct rte_eth_link link;
@@ -225,8 +224,8 @@ rte_eth_linkstatus_set(struct rte_eth_dev *dev,
 
 	RTE_BUILD_BUG_ON(sizeof(*new_link) != sizeof(uint64_t));
 
-	orig.val64 = rte_atomic64_exchange(dev_link,
-					   *(const uint64_t *)new_link);
+	orig.val64 = __atomic_exchange_n(dev_link, (const uint64_t *)new_link,
+					__ATOMIC_SEQ_CST);
 
 	return (orig.link.link_status == new_link->link_status) ? -1 : 0;
 }
@@ -244,20 +243,12 @@ static inline void
 rte_eth_linkstatus_get(const struct rte_eth_dev *dev,
 		       struct rte_eth_link *link)
 {
-	volatile uint64_t *src = (uint64_t *)&(dev->data->dev_link);
+	uint64_t *src = (uint64_t *)&(dev->data->dev_link);
 	uint64_t *dst = (uint64_t *)link;
 
 	RTE_BUILD_BUG_ON(sizeof(*link) != sizeof(uint64_t));
 
-#ifdef __LP64__
-	/* if cpu arch has 64 bit unsigned lon then implicitly atomic */
-	*dst = *src;
-#else
-	/* can't use rte_atomic64_read because it returns signed int */
-	do {
-		*dst = *src;
-	} while (!rte_atomic64_cmpset(src, *dst, *dst));
-#endif
+	*dst = __atomic_load_n(src, __ATOMIC_SEQ_CST);
 }
 
 /**
Signature · 1 line
2.7.4
#5 lore raw
In reply to Phil Yang
Quoted 4 lines · Phil Yang

Since rte_atomicXX APIs are not allowed to be used[1][2], use C11 atomic builtins instead in eal, bbdev, power, and ethdev libs.

Quoted 8 lines

[1] http://code.dpdk.org/dpdk/latest/source/doc/guides/rel_notes/deprecation.rst#L87
[2] http://code.dpdk.org/dpdk/latest/source/devtools/checkpatches.sh#L80

Phil Yang (4):
eal: use C11 atomic builtins for already initialized check
bbdev: use C11 atomic builtins for device processing counter
power: use C11 atomic builtins for power in use state update
ethdev: use C11 atomic builtins for link status update

It breaks build with clang (Travis + FreeBSD vm at UNH).

Signature · 1 line
David Marchand
author
#6 lore raw
Quoted 13 lines · David Marchand

Subject: Re: [dpdk-dev] [PATCH 0/4] use C11 atomic builtins for libs

Quoted 5 lines · Phil Yang

Since rte_atomicXX APIs are not allowed to be used[1][2], use C11 atomic builtins instead in eal, bbdev, power, and ethdev libs.

[1]

http://code.dpdk.org/dpdk/latest/source/doc/guides/rel_notes/deprecatio
n.rst#L87

Quoted 1 line

[2]

http://code.dpdk.org/dpdk/latest/source/devtools/checkpatches.sh#L80

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

Phil Yang (4):
eal: use C11 atomic builtins for already initialized check
bbdev: use C11 atomic builtins for device processing counter
power: use C11 atomic builtins for power in use state update
ethdev: use C11 atomic builtins for link status update

It breaks build with clang (Travis + FreeBSD vm at UNH).

Yes. It is an 'int-conversion' warning in clang.
Problem resolved. Will update the patch soon.

Thanks,
Phil

author
#7 lore raw

v2 cover use C11 atomic builtins for libs

In reply to Phil Yang

Since rte_atomicXX APIs are not allowed to be used[1][2], use C11 atomic builtins instead in eal, bbdev, power, and ethdev libs.

[1] http://code.dpdk.org/dpdk/latest/source/doc/guides/rel_notes/deprecation.rst#L87
[2] http://code.dpdk.org/dpdk/latest/source/devtools/checkpatches.sh#L80

v2:
Fix Clang int-conversion warning.

v1:
Initial version.

Phil Yang (4):
eal: use C11 atomic builtins for already initialized check
bbdev: use C11 atomic builtins for device processing counter
power: use C11 atomic builtins for power in use state update
ethdev: use C11 atomic builtins for link status update

 lib/librte_bbdev/rte_bbdev.c            |  5 ++--
 lib/librte_bbdev/rte_bbdev.h            |  4 +--
 lib/librte_eal/freebsd/eal.c            | 18 +++++++------
 lib/librte_eal/linux/eal.c              | 20 ++++++++-------
 lib/librte_ethdev/rte_ethdev_driver.h   | 19 ++++----------
 lib/librte_power/power_acpi_cpufreq.c   | 45 +++++++++++++++++++++++++--------
 lib/librte_power/power_pstate_cpufreq.c | 45 +++++++++++++++++++++++++--------
 7 files changed, 100 insertions(+), 56 deletions(-)
Signature · 1 line
2.7.4
author
#8 lore raw

PATCH v2 1/4 eal: use C11 atomic builtins for already initialized check

Since rte_atomicXX APIs are not allowed to be used, use C11 builtins to check if EAL is already initialized.

---
 lib/librte_eal/freebsd/eal.c | 18 ++++++++++--------
 lib/librte_eal/linux/eal.c   | 20 +++++++++++---------
 2 files changed, 21 insertions(+), 17 deletions(-)
lib/librte_eal/freebsd/eal.c +10 -8
index 798add0..9f4c7bb 100644
@@ -665,7 +665,8 @@ rte_eal_init(int argc, char **argv)
 {
 	int i, fctret, ret;
 	pthread_t thread_id;
-	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+	static uint32_t run_once;
+	uint32_t has_run = 0;
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 	const struct rte_config *config = rte_eal_get_configuration();
@@ -679,7 +680,8 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	if (!rte_atomic32_test_and_set(&run_once)) {
+	if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+					__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
 		rte_eal_init_alert("already called initialization.");
 		rte_errno = EALREADY;
 		return -1;
@@ -705,7 +707,7 @@ rte_eal_init(int argc, char **argv)
 	if (fctret < 0) {
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -715,20 +717,20 @@ rte_eal_init(int argc, char **argv)
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_trace_init() < 0) {
 		rte_eal_init_alert("Cannot init trace");
 		rte_errno = EFAULT;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -762,7 +764,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_bus_scan()) {
 		rte_eal_init_alert("Cannot scan the buses for devices");
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -790,7 +792,7 @@ rte_eal_init(int argc, char **argv)
 		if (ret < 0) {
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
-			rte_atomic32_clear(&run_once);
+			__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 			return -1;
 		}
 	}
lib/librte_eal/linux/eal.c +11 -9
index 0960f01..82a73ed 100644
@@ -960,7 +960,8 @@ rte_eal_init(int argc, char **argv)
 {
 	int i, fctret, ret;
 	pthread_t thread_id;
-	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+	static uint32_t run_once;
+	uint32_t has_run = 0;
 	const char *p;
 	static char logid[PATH_MAX];
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
@@ -977,7 +978,8 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	if (!rte_atomic32_test_and_set(&run_once)) {
+	if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+					__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
 		rte_eal_init_alert("already called initialization.");
 		rte_errno = EALREADY;
 		return -1;
@@ -1005,14 +1007,14 @@ rte_eal_init(int argc, char **argv)
 	if (fctret < 0) {
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1024,7 +1026,7 @@ rte_eal_init(int argc, char **argv)
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1064,7 +1066,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_bus_scan()) {
 		rte_eal_init_alert("Cannot scan the buses for devices");
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1138,7 +1140,7 @@ rte_eal_init(int argc, char **argv)
 		if (ret < 0) {
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
-			rte_atomic32_clear(&run_once);
+			__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 			return -1;
 		}
 	}
@@ -1162,7 +1164,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_eal_log_init(logid, internal_conf->syslog_facility) < 0) {
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1170,7 +1172,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");
 		rte_errno = EAGAIN;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 #endif
Signature · 1 line
2.7.4
#9 lore raw

Re: [dpdk-dev] [PATCH v2 1/4] eal: use C11 atomic builtins for already initialized check

Quoted 174 lines · Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 builtins to check if EAL is already initialized.

---
 lib/librte_eal/freebsd/eal.c | 18 ++++++++++--------
 lib/librte_eal/linux/eal.c   | 20 +++++++++++---------
 2 files changed, 21 insertions(+), 17 deletions(-)
lib/librte_eal/freebsd/eal.c +10 -8
index 798add0..9f4c7bb 100644
@@ -665,7 +665,8 @@ rte_eal_init(int argc, char **argv)
 {
        int i, fctret, ret;
        pthread_t thread_id;
-       static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+       static uint32_t run_once;
+       uint32_t has_run = 0;
        char cpuset[RTE_CPU_AFFINITY_STR_LEN];
        char thread_name[RTE_MAX_THREAD_NAME_LEN];
        const struct rte_config *config = rte_eal_get_configuration();
@@ -679,7 +680,8 @@ rte_eal_init(int argc, char **argv)
                return -1;
        }
 
-       if (!rte_atomic32_test_and_set(&run_once)) {
+       if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+                                       __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
                rte_eal_init_alert("already called initialization.");
                rte_errno = EALREADY;
                return -1;
@@ -705,7 +707,7 @@ rte_eal_init(int argc, char **argv)
        if (fctret < 0) {
                rte_eal_init_alert("Invalid 'command line' arguments.");
                rte_errno = EINVAL;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -715,20 +717,20 @@ rte_eal_init(int argc, char **argv)
        if (eal_plugins_init() < 0) {
                rte_eal_init_alert("Cannot init plugins");
                rte_errno = EINVAL;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
        if (eal_trace_init() < 0) {
                rte_eal_init_alert("Cannot init trace");
                rte_errno = EFAULT;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
        if (eal_option_device_parse()) {
                rte_errno = ENODEV;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -762,7 +764,7 @@ rte_eal_init(int argc, char **argv)
        if (rte_bus_scan()) {
                rte_eal_init_alert("Cannot scan the buses for devices");
                rte_errno = ENODEV;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -790,7 +792,7 @@ rte_eal_init(int argc, char **argv)
                if (ret < 0) {
                        rte_eal_init_alert("Cannot get hugepage information.");
                        rte_errno = EACCES;
-                       rte_atomic32_clear(&run_once);
+                       __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                        return -1;
                }
        }
lib/librte_eal/linux/eal.c +11 -10
index 0960f01..82a73ed 100644
@@ -960,7 +960,8 @@ rte_eal_init(int argc, char **argv)
 {
        int i, fctret, ret;
        pthread_t thread_id;
-       static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+       static uint32_t run_once;
+       uint32_t has_run = 0;
        const char *p;
        static char logid[PATH_MAX];
        char cpuset[RTE_CPU_AFFINITY_STR_LEN];
@@ -977,7 +978,8 @@ rte_eal_init(int argc, char **argv)
                return -1;
        }
 
-       if (!rte_atomic32_test_and_set(&run_once)) {
+       if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+                                       __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
                rte_eal_init_alert("already called initialization.");
                rte_errno = EALREADY;
                return -1;
@@ -1005,14 +1007,14 @@ rte_eal_init(int argc, char **argv)
        if (fctret < 0) {
                rte_eal_init_alert("Invalid 'command line' arguments.");
                rte_errno = EINVAL;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
        if (eal_plugins_init() < 0) {
                rte_eal_init_alert("Cannot init plugins");
                rte_errno = EINVAL;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -1024,7 +1026,7 @@ rte_eal_init(int argc, char **argv)
 
        if (eal_option_device_parse()) {
                rte_errno = ENODEV;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -1064,7 +1066,7 @@ rte_eal_init(int argc, char **argv)
        if (rte_bus_scan()) {
                rte_eal_init_alert("Cannot scan the buses for devices");
                rte_errno = ENODEV;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -1138,7 +1140,7 @@ rte_eal_init(int argc, char **argv)
                if (ret < 0) {
                        rte_eal_init_alert("Cannot get hugepage information.");
                        rte_errno = EACCES;
-                       rte_atomic32_clear(&run_once);
+                       __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                        return -1;
                }
        }
@@ -1162,7 +1164,7 @@ rte_eal_init(int argc, char **argv)
        if (rte_eal_log_init(logid, internal_conf->syslog_facility) < 0) {
                rte_eal_init_alert("Cannot init logging.");
                rte_errno = ENOMEM;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -1170,7 +1172,7 @@ rte_eal_init(int argc, char **argv)
        if (rte_eal_vfio_setup() < 0) {
                rte_eal_init_alert("Cannot init VFIO");
                rte_errno = EAGAIN;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 #endif
--
Quoted 2 lines

2.7.4

I see no reason to include rte_atomic.h in those files after this patch. Did I miss something?

Signature · 1 line
David Marchand
author
#10 lore raw

Re: [dpdk-dev] [PATCH v2 1/4] eal: use C11 atomic builtins for already initialized check

Quoted 168 lines · David Marchand

Subject: Re: [PATCH v2 1/4] eal: use C11 atomic builtins for already initialized check

Quoted 164 lines · Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 builtins to check if EAL is already initialized.

---
 lib/librte_eal/freebsd/eal.c | 18 ++++++++++--------
 lib/librte_eal/linux/eal.c   | 20 +++++++++++---------
 2 files changed, 21 insertions(+), 17 deletions(-)
lib/librte_eal/freebsd/eal.c +10 -8
index 798add0..9f4c7bb 100644
@@ -665,7 +665,8 @@ rte_eal_init(int argc, char **argv)
 {
        int i, fctret, ret;
        pthread_t thread_id;
-       static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+       static uint32_t run_once;
+       uint32_t has_run = 0;
        char cpuset[RTE_CPU_AFFINITY_STR_LEN];
        char thread_name[RTE_MAX_THREAD_NAME_LEN];
        const struct rte_config *config = rte_eal_get_configuration();
@@ -679,7 +680,8 @@ rte_eal_init(int argc, char **argv)
                return -1;
        }
 
-       if (!rte_atomic32_test_and_set(&run_once)) {
+       if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+                                       __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
                rte_eal_init_alert("already called initialization.");
                rte_errno = EALREADY;
                return -1;
@@ -705,7 +707,7 @@ rte_eal_init(int argc, char **argv)
        if (fctret < 0) {
                rte_eal_init_alert("Invalid 'command line' arguments.");
                rte_errno = EINVAL;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -715,20 +717,20 @@ rte_eal_init(int argc, char **argv)
        if (eal_plugins_init() < 0) {
                rte_eal_init_alert("Cannot init plugins");
                rte_errno = EINVAL;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
        if (eal_trace_init() < 0) {
                rte_eal_init_alert("Cannot init trace");
                rte_errno = EFAULT;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
        if (eal_option_device_parse()) {
                rte_errno = ENODEV;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -762,7 +764,7 @@ rte_eal_init(int argc, char **argv)
        if (rte_bus_scan()) {
                rte_eal_init_alert("Cannot scan the buses for devices");
                rte_errno = ENODEV;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -790,7 +792,7 @@ rte_eal_init(int argc, char **argv)
                if (ret < 0) {
                        rte_eal_init_alert("Cannot get hugepage information.");
                        rte_errno = EACCES;
-                       rte_atomic32_clear(&run_once);
+                       __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                        return -1;
                }
        }
lib/librte_eal/linux/eal.c +10 -8
index 0960f01..82a73ed 100644
@@ -960,7 +960,8 @@ rte_eal_init(int argc, char **argv)
 {
        int i, fctret, ret;
        pthread_t thread_id;
-       static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+       static uint32_t run_once;
+       uint32_t has_run = 0;
        const char *p;
        static char logid[PATH_MAX];
        char cpuset[RTE_CPU_AFFINITY_STR_LEN];
@@ -977,7 +978,8 @@ rte_eal_init(int argc, char **argv)
                return -1;
        }
 
-       if (!rte_atomic32_test_and_set(&run_once)) {
+       if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+                                       __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
                rte_eal_init_alert("already called initialization.");
                rte_errno = EALREADY;
                return -1;
@@ -1005,14 +1007,14 @@ rte_eal_init(int argc, char **argv)
        if (fctret < 0) {
                rte_eal_init_alert("Invalid 'command line' arguments.");
                rte_errno = EINVAL;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
        if (eal_plugins_init() < 0) {
                rte_eal_init_alert("Cannot init plugins");
                rte_errno = EINVAL;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -1024,7 +1026,7 @@ rte_eal_init(int argc, char **argv)
 
        if (eal_option_device_parse()) {
                rte_errno = ENODEV;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -1064,7 +1066,7 @@ rte_eal_init(int argc, char **argv)
        if (rte_bus_scan()) {
                rte_eal_init_alert("Cannot scan the buses for devices");
                rte_errno = ENODEV;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
@@ -1138,7 +1140,7 @@ rte_eal_init(int argc, char **argv)
                if (ret < 0) {
                        rte_eal_init_alert("Cannot get hugepage information.");
                        rte_errno = EACCES;
-                       rte_atomic32_clear(&run_once);
+                       __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                        return -1;
                }
        }
@@ -1162,7 +1164,7 @@ rte_eal_init(int argc, char **argv)
        if (rte_eal_log_init(logid, internal_conf->syslog_facility) < 0) {
                rte_eal_init_alert("Cannot init logging.");
                rte_errno = ENOMEM;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 
Quoted 15 lines
Quoted 10 lines
lib/librte_eal/linux/eal.c +1 -2
@@ -1170,7 +1172,7 @@ rte_eal_init(int argc, char **argv)
        if (rte_eal_vfio_setup() < 0) {
                rte_eal_init_alert("Cannot init VFIO");
                rte_errno = EAGAIN;
-               rte_atomic32_clear(&run_once);
+               __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
                return -1;
        }
 #endif
--
Quoted 2 lines

2.7.4

I see no reason to include rte_atomic.h in those files after this patch. Did I miss something?

No, it is not needed anymore.
Will remove them.

Thanks,
Phil

Quoted 4 lines

--
David Marchand

author
#11 lore raw

PATCH v2 2/4 bbdev: use C11 atomic builtins for device processing counter

In reply to Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for device processing counter.

---
 lib/librte_bbdev/rte_bbdev.c | 5 +++--
 lib/librte_bbdev/rte_bbdev.h | 4 +---
 2 files changed, 4 insertions(+), 5 deletions(-)
lib/librte_bbdev/rte_bbdev.c +3 -2
index a4fdb69..5ba891c 100644
@@ -210,7 +210,7 @@ rte_bbdev_allocate(const char *name)
 		return NULL;
 	}
 
-	rte_atomic16_inc(&bbdev->data->process_cnt);
+	__atomic_add_fetch(&bbdev->data->process_cnt, 1, __ATOMIC_RELAXED);
 	bbdev->data->dev_id = dev_id;
 	bbdev->state = RTE_BBDEV_INITIALIZED;
 
@@ -252,7 +252,8 @@ rte_bbdev_release(struct rte_bbdev *bbdev)
 	}
 
 	/* clear shared BBDev Data if no process is using the device anymore */
-	if (rte_atomic16_dec_and_test(&bbdev->data->process_cnt))
+	if (__atomic_sub_fetch(&bbdev->data->process_cnt, 1,
+			      __ATOMIC_RELAXED) == 0)
 		memset(bbdev->data, 0, sizeof(*bbdev->data));
 
 	memset(bbdev, 0, sizeof(*bbdev));
lib/librte_bbdev/rte_bbdev.h +1 -3
index 5729137..7017124 100644
@@ -33,7 +33,6 @@ extern "C" {
 #include <string.h>
 
 #include <rte_compat.h>
-#include <rte_atomic.h>
 #include <rte_bus.h>
 #include <rte_cpuflags.h>
 #include <rte_memory.h>
@@ -426,8 +425,7 @@ struct rte_bbdev_data {
 	uint16_t dev_id;  /**< Device ID */
 	int socket_id;  /**< NUMA socket that device is on */
 	bool started;  /**< Device run-time state */
-	/** Counter of processes using the device */
-	rte_atomic16_t process_cnt;
+	uint16_t process_cnt;  /** Counter of processes using the device */
 };
 
 /* Forward declarations */
Signature · 1 line
2.7.4
author
#12 lore raw

PATCH v2 3/4 power: use C11 atomic builtins for power in use state update

In reply to Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for power in use state update.

---
 lib/librte_power/power_acpi_cpufreq.c   | 45 +++++++++++++++++++++++++--------
 lib/librte_power/power_pstate_cpufreq.c | 45 +++++++++++++++++++++++++--------
 2 files changed, 70 insertions(+), 20 deletions(-)
lib/librte_power/power_acpi_cpufreq.c +35 -10
index 583815a..84a9d75 100644
@@ -12,7 +12,6 @@
 #include <signal.h>
 #include <limits.h>
 
-#include <rte_atomic.h>
 #include <rte_memcpy.h>
 #include <rte_memory.h>
 #include <rte_string_fns.h>
@@ -86,7 +85,7 @@ struct rte_power_info {
 	FILE *f;                             /**< FD of scaling_setspeed */
 	char governor_ori[32];               /**< Original governor name */
 	uint32_t curr_idx;                   /**< Freq index in freqs array */
-	volatile uint32_t state;             /**< Power in use state */
+	uint32_t state;                      /**< Power in use state */
 	uint16_t turbo_available;            /**< Turbo Boost available */
 	uint16_t turbo_enable;               /**< Turbo Boost enable/disable */
 } __rte_cache_aligned;
@@ -300,6 +299,7 @@ int
 power_acpi_cpufreq_init(unsigned int lcore_id)
 {
 	struct rte_power_info *pi;
+	uint32_t exp_state;
 
 	if (lcore_id >= RTE_MAX_LCORE) {
 		RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
@@ -308,8 +308,16 @@ power_acpi_cpufreq_init(unsigned int lcore_id)
 	}
 
 	pi = &lcore_power_info[lcore_id];
-	if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
-			== 0) {
+	exp_state = POWER_IDLE;
+	/* The power in use state works as a guard variable between
+	 * the CPU frequency control initialization and exit process.
+	 * The ACQUIRE memory ordering here pairs with the RELEASE
+	 * ordering below as lock to make sure the frequency operations
+	 * in the critical section are done under the correct state.
+	 */
+	if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
+					POWER_ONGOING, 0,
+					__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
 		RTE_LOG(INFO, POWER, "Power management of lcore %u is "
 				"in use\n", lcore_id);
 		return -1;
@@ -346,12 +354,16 @@ power_acpi_cpufreq_init(unsigned int lcore_id)
 
 	RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
 			"power management\n", lcore_id);
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_USED,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return 0;
 
 fail:
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return -1;
 }
@@ -408,6 +420,7 @@ int
 power_acpi_cpufreq_exit(unsigned int lcore_id)
 {
 	struct rte_power_info *pi;
+	uint32_t exp_state;
 
 	if (lcore_id >= RTE_MAX_LCORE) {
 		RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
@@ -415,8 +428,16 @@ power_acpi_cpufreq_exit(unsigned int lcore_id)
 		return -1;
 	}
 	pi = &lcore_power_info[lcore_id];
-	if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
-			== 0) {
+	exp_state = POWER_USED;
+	/* The power in use state works as a guard variable between
+	 * the CPU frequency control initialization and exit process.
+	 * The ACQUIRE memory ordering here pairs with the RELEASE
+	 * ordering below as lock to make sure the frequency operations
+	 * in the critical section are done under the correct state.
+	 */
+	if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
+					POWER_ONGOING, 0,
+					__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
 		RTE_LOG(INFO, POWER, "Power management of lcore %u is "
 				"not used\n", lcore_id);
 		return -1;
@@ -436,12 +457,16 @@ power_acpi_cpufreq_exit(unsigned int lcore_id)
 	RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
 			"'userspace' mode and been set back to the "
 			"original\n", lcore_id);
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_IDLE,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return 0;
 
 fail:
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return -1;
 }
lib/librte_power/power_pstate_cpufreq.c +35 -10

101 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#13 lore raw

PATCH v2 4/4 ethdev: use C11 atomic builtins for link status update

In reply to Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for link status update.

---
 lib/librte_ethdev/rte_ethdev_driver.h | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)
lib/librte_ethdev/rte_ethdev_driver.h +5 -14
index 13fd049..ebaf6da 100644
@@ -216,8 +216,7 @@ static inline int
 rte_eth_linkstatus_set(struct rte_eth_dev *dev,
 		       const struct rte_eth_link *new_link)
 {
-	volatile uint64_t *dev_link
-		 = (volatile uint64_t *)&(dev->data->dev_link);
+	uint64_t *dev_link = (uint64_t *)&(dev->data->dev_link);
 	union {
 		uint64_t val64;
 		struct rte_eth_link link;
@@ -225,8 +224,8 @@ rte_eth_linkstatus_set(struct rte_eth_dev *dev,
 
 	RTE_BUILD_BUG_ON(sizeof(*new_link) != sizeof(uint64_t));
 
-	orig.val64 = rte_atomic64_exchange(dev_link,
-					   *(const uint64_t *)new_link);
+	orig.val64 = __atomic_exchange_n(dev_link, *(const uint64_t *)new_link,
+					__ATOMIC_SEQ_CST);
 
 	return (orig.link.link_status == new_link->link_status) ? -1 : 0;
 }
@@ -244,20 +243,12 @@ static inline void
 rte_eth_linkstatus_get(const struct rte_eth_dev *dev,
 		       struct rte_eth_link *link)
 {
-	volatile uint64_t *src = (uint64_t *)&(dev->data->dev_link);
+	uint64_t *src = (uint64_t *)&(dev->data->dev_link);
 	uint64_t *dst = (uint64_t *)link;
 
 	RTE_BUILD_BUG_ON(sizeof(*link) != sizeof(uint64_t));
 
-#ifdef __LP64__
-	/* if cpu arch has 64 bit unsigned lon then implicitly atomic */
-	*dst = *src;
-#else
-	/* can't use rte_atomic64_read because it returns signed int */
-	do {
-		*dst = *src;
-	} while (!rte_atomic64_cmpset(src, *dst, *dst));
-#endif
+	*dst = __atomic_load_n(src, __ATOMIC_SEQ_CST);
 }
 
 /**
Signature · 1 line
2.7.4
#15 lore raw
In reply to Phil Yang
Quoted 10 lines · Phil Yang

Since rte_atomicXX APIs are not allowed to be used[1][2], use C11 atomic builtins instead in eal, bbdev, power, and ethdev libs.

[1] http://code.dpdk.org/dpdk/latest/source/doc/guides/rel_notes/deprecation.rst#L87
[2] http://code.dpdk.org/dpdk/latest/source/devtools/checkpatches.sh#L80

v2:
Fix Clang int-conversion warning.

Quoted 8 lines

v1:
Initial version.

Phil Yang (4):
eal: use C11 atomic builtins for already initialized check
bbdev: use C11 atomic builtins for device processing counter
power: use C11 atomic builtins for power in use state update
ethdev: use C11 atomic builtins for link status update

Thanks Phil.
Just a small comment on the first patch, the rest lgtm.

Nicolas, David H., could you have a look at (resp.) patch 2, 3?

Signature · 1 line
David Marchand
author
#16 lore raw
Quoted 25 lines · David Marchand

Subject: Re: [PATCH v2 0/4] use C11 atomic builtins for libs

Quoted 5 lines · Phil Yang

Since rte_atomicXX APIs are not allowed to be used[1][2], use C11 atomic builtins instead in eal, bbdev, power, and ethdev libs.

[1]

http://code.dpdk.org/dpdk/latest/source/doc/guides/rel_notes/deprecatio
n.rst#L87

Quoted 1 line

[2]

http://code.dpdk.org/dpdk/latest/source/devtools/checkpatches.sh#L80

Quoted 4 lines

v2:
Fix Clang int-conversion warning.

Quoted 8 lines

v1:
Initial version.

Phil Yang (4):
eal: use C11 atomic builtins for already initialized check
bbdev: use C11 atomic builtins for device processing counter
power: use C11 atomic builtins for power in use state update
ethdev: use C11 atomic builtins for link status update

Quoted 4 lines

Thanks Phil.
Just a small comment on the first patch, the rest lgtm.

Nicolas, David H., could you have a look at (resp.) patch 2, 3?

Thanks for your comments.
I will address them in the next version.

Thanks,
Phil

Quoted 4 lines

--
David Marchand

author
#17 lore raw

v3 cover use C11 atomic builtins for libs

In reply to Phil Yang

Since rte_atomicXX APIs are not allowed to be used[1][2], use C11 atomic builtins instead in eal, bbdev, power, and ethdev libs.

[1] http://code.dpdk.org/dpdk/latest/source/doc/guides/rel_notes/deprecation.rst#L87
[2] http://code.dpdk.org/dpdk/latest/source/devtools/checkpatches.sh#L80

v3:
remove unnecessary rte_atomic.h headers. (David)

v2:
Fix Clang int-conversion warning.

v1:
Initial version.

Phil Yang (4):
eal: use C11 atomic builtins for already initialized check
bbdev: use C11 atomic builtins for device processing counter
power: use C11 atomic builtins for power in use state update
ethdev: use C11 atomic builtins for link status update

 lib/librte_bbdev/rte_bbdev.c            |  5 ++--
 lib/librte_bbdev/rte_bbdev.h            |  4 +--
 lib/librte_eal/freebsd/eal.c            | 19 +++++++-------
 lib/librte_eal/linux/eal.c              | 21 +++++++--------
 lib/librte_ethdev/rte_ethdev_driver.h   | 19 ++++----------
 lib/librte_power/power_acpi_cpufreq.c   | 45 +++++++++++++++++++++++++--------
 lib/librte_power/power_pstate_cpufreq.c | 45 +++++++++++++++++++++++++--------
 7 files changed, 100 insertions(+), 58 deletions(-)
Signature · 1 line
2.7.4
author
#18 lore raw

PATCH v3 1/4 eal: use C11 atomic builtins for already initialized check

Since rte_atomicXX APIs are not allowed to be used, use C11 builtins to check if EAL is already initialized.

---
 lib/librte_eal/freebsd/eal.c | 19 ++++++++++---------
 lib/librte_eal/linux/eal.c   | 21 +++++++++++----------
 2 files changed, 21 insertions(+), 19 deletions(-)
lib/librte_eal/freebsd/eal.c +10 -9
index 798add0..ccea60a 100644
@@ -41,7 +41,6 @@
 #include <rte_devargs.h>
 #include <rte_version.h>
 #include <rte_vfio.h>
-#include <rte_atomic.h>
 #include <malloc_heap.h>
 #include <rte_telemetry.h>
 
@@ -665,7 +664,8 @@ rte_eal_init(int argc, char **argv)
 {
 	int i, fctret, ret;
 	pthread_t thread_id;
-	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+	static uint32_t run_once;
+	uint32_t has_run = 0;
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 	const struct rte_config *config = rte_eal_get_configuration();
@@ -679,7 +679,8 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	if (!rte_atomic32_test_and_set(&run_once)) {
+	if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+					__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
 		rte_eal_init_alert("already called initialization.");
 		rte_errno = EALREADY;
 		return -1;
@@ -705,7 +706,7 @@ rte_eal_init(int argc, char **argv)
 	if (fctret < 0) {
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -715,20 +716,20 @@ rte_eal_init(int argc, char **argv)
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_trace_init() < 0) {
 		rte_eal_init_alert("Cannot init trace");
 		rte_errno = EFAULT;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -762,7 +763,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_bus_scan()) {
 		rte_eal_init_alert("Cannot scan the buses for devices");
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -790,7 +791,7 @@ rte_eal_init(int argc, char **argv)
 		if (ret < 0) {
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
-			rte_atomic32_clear(&run_once);
+			__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 			return -1;
 		}
 	}
lib/librte_eal/linux/eal.c +11 -10
index 0960f01..9cf0e2e 100644
@@ -47,7 +47,6 @@
 #include <rte_dev.h>
 #include <rte_devargs.h>
 #include <rte_version.h>
-#include <rte_atomic.h>
 #include <malloc_heap.h>
 #include <rte_vfio.h>
 #include <rte_telemetry.h>
@@ -960,7 +959,8 @@ rte_eal_init(int argc, char **argv)
 {
 	int i, fctret, ret;
 	pthread_t thread_id;
-	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+	static uint32_t run_once;
+	uint32_t has_run = 0;
 	const char *p;
 	static char logid[PATH_MAX];
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
@@ -977,7 +977,8 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	if (!rte_atomic32_test_and_set(&run_once)) {
+	if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+					__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
 		rte_eal_init_alert("already called initialization.");
 		rte_errno = EALREADY;
 		return -1;
@@ -1005,14 +1006,14 @@ rte_eal_init(int argc, char **argv)
 	if (fctret < 0) {
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1024,7 +1025,7 @@ rte_eal_init(int argc, char **argv)
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1064,7 +1065,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_bus_scan()) {
 		rte_eal_init_alert("Cannot scan the buses for devices");
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1138,7 +1139,7 @@ rte_eal_init(int argc, char **argv)
 		if (ret < 0) {
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
-			rte_atomic32_clear(&run_once);
+			__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 			return -1;
 		}
 	}
@@ -1162,7 +1163,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_eal_log_init(logid, internal_conf->syslog_facility) < 0) {
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1170,7 +1171,7 @@ rte_eal_init(int argc, char **argv)
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");
 		rte_errno = EAGAIN;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 #endif
Signature · 1 line
2.7.4
author
#19 lore raw

PATCH v3 2/4 bbdev: use C11 atomic builtins for device processing counter

In reply to Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for device processing counter.

---
 lib/librte_bbdev/rte_bbdev.c | 5 +++--
 lib/librte_bbdev/rte_bbdev.h | 4 +---
 2 files changed, 4 insertions(+), 5 deletions(-)
lib/librte_bbdev/rte_bbdev.c +3 -2
index a4fdb69..5ba891c 100644
@@ -210,7 +210,7 @@ rte_bbdev_allocate(const char *name)
 		return NULL;
 	}
 
-	rte_atomic16_inc(&bbdev->data->process_cnt);
+	__atomic_add_fetch(&bbdev->data->process_cnt, 1, __ATOMIC_RELAXED);
 	bbdev->data->dev_id = dev_id;
 	bbdev->state = RTE_BBDEV_INITIALIZED;
 
@@ -252,7 +252,8 @@ rte_bbdev_release(struct rte_bbdev *bbdev)
 	}
 
 	/* clear shared BBDev Data if no process is using the device anymore */
-	if (rte_atomic16_dec_and_test(&bbdev->data->process_cnt))
+	if (__atomic_sub_fetch(&bbdev->data->process_cnt, 1,
+			      __ATOMIC_RELAXED) == 0)
 		memset(bbdev->data, 0, sizeof(*bbdev->data));
 
 	memset(bbdev, 0, sizeof(*bbdev));
lib/librte_bbdev/rte_bbdev.h +1 -3
index 5729137..7017124 100644
@@ -33,7 +33,6 @@ extern "C" {
 #include <string.h>
 
 #include <rte_compat.h>
-#include <rte_atomic.h>
 #include <rte_bus.h>
 #include <rte_cpuflags.h>
 #include <rte_memory.h>
@@ -426,8 +425,7 @@ struct rte_bbdev_data {
 	uint16_t dev_id;  /**< Device ID */
 	int socket_id;  /**< NUMA socket that device is on */
 	bool started;  /**< Device run-time state */
-	/** Counter of processes using the device */
-	rte_atomic16_t process_cnt;
+	uint16_t process_cnt;  /** Counter of processes using the device */
 };
 
 /* Forward declarations */
Signature · 1 line
2.7.4
#20 lore raw

Re: [dpdk-dev] [PATCH v3 2/4] bbdev: use C11 atomic builtins for device processing counter

Hi Phil,
Naïve question but the deprecation document was stating that "DPDK will adopt C11 atomic operations semantics and provide wrappers using C11 atomic built-ins." Here you are using directly the C11 atomic built-ins and not providing and using a DPDK wrapper.
Wasn't the intent to have a new rte_... wrapper here? Ie. the same way as the __sync_fetch_and_add() were called before behind the rte_atomicNN_XX wrapper.

Thanks
Nic

Quoted 70 lines

-----Original Message-----
From: Phil Yang <[email protected]>
Sent: Wednesday, September 23, 2020 10:39 PM
To: [email protected]; [email protected]; Chautru, Nicolas <[email protected]>; Hunt, David <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: [PATCH v3 2/4] bbdev: use C11 atomic builtins for device processing counter

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for device processing counter.

---
 lib/librte_bbdev/rte_bbdev.c | 5 +++--
 lib/librte_bbdev/rte_bbdev.h | 4 +---
 2 files changed, 4 insertions(+), 5 deletions(-)
lib/librte_bbdev/rte_bbdev.c +3 -2
index a4fdb69..5ba891c 100644
@@ -210,7 +210,7 @@ rte_bbdev_allocate(const char *name)
 		return NULL;
 	}
 
-	rte_atomic16_inc(&bbdev->data->process_cnt);
+	__atomic_add_fetch(&bbdev->data->process_cnt, 1,
 __ATOMIC_RELAXED);
 	bbdev->data->dev_id = dev_id;
 	bbdev->state = RTE_BBDEV_INITIALIZED;
@@ -252,7 +252,8 @@ rte_bbdev_release(struct rte_bbdev *bbdev)
 	}
 
 	/* clear shared BBDev Data if no process is using the device anymore
 */
-	if (rte_atomic16_dec_and_test(&bbdev->data->process_cnt))
+	if (__atomic_sub_fetch(&bbdev->data->process_cnt, 1,
+			      __ATOMIC_RELAXED) == 0)
 		memset(bbdev->data, 0, sizeof(*bbdev->data));
 
lib/librte_bbdev/rte_bbdev.c +0 -0
 	memset(bbdev, 0, sizeof(*bbdev));
lib/librte_bbdev/rte_bbdev.h +1 -4
index 5729137..7017124 100644
@@ -33,7 +33,6 @@ extern "C" {
 #include <string.h>
 
 #include <rte_compat.h>
-#include <rte_atomic.h>
 #include <rte_bus.h>
 #include <rte_cpuflags.h>
 #include <rte_memory.h>
@@ -426,8 +425,7 @@ struct rte_bbdev_data {
 	uint16_t dev_id;  /**< Device ID */
 	int socket_id;  /**< NUMA socket that device is on */
 	bool started;  /**< Device run-time state */
-	/** Counter of processes using the device */
-	rte_atomic16_t process_cnt;
+	uint16_t process_cnt;  /** Counter of processes using the device */
 };
 
 /* Forward declarations */
--

2.7.4

#21 lore raw

Re: [dpdk-dev] [PATCH v3 2/4] bbdev: use C11 atomic builtins for device processing counter

<snip>

Quoted 5 lines

Hi Phil,
Naïve question but the deprecation document was stating that "DPDK will adopt C11 atomic operations semantics and provide wrappers using C11 atomic built-ins."

At the time of writing the deprecation notice, that was the thinking. However, through further discussions [1] in the community, it was decided to use atomic built-ins.

[1] http://mails.dpdk.org/archives/dev/2020-May/167416.html

Quoted 80 lines

Here you are using directly the C11 atomic built-ins and not providing and using a DPDK wrapper.
Wasn't the intent to have a new rte_... wrapper here? Ie. the same way as the __sync_fetch_and_add() were called before behind the rte_atomicNN_XX wrapper.

Thanks
Nic

Quoted 6 lines

-----Original Message-----
From: Phil Yang <[email protected]>
Sent: Wednesday, September 23, 2020 10:39 PM
To: [email protected]; [email protected]; Chautru, Nicolas <[email protected]>; Hunt, David <[email protected]>
Cc: [email protected]; [email protected];

[email protected]

Quoted 63 lines

Subject: [PATCH v3 2/4] bbdev: use C11 atomic builtins for device processing counter

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for device processing counter.

---
lib/librte_bbdev/rte_bbdev.c | 5 +++-- lib/librte_bbdev/rte_bbdev.h
| 4 +---

 2 files changed, 4 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/lib/librte_bbdev/rte_bbdev.c index a4fdb69..5ba891c 100644

lib/librte_bbdev/rte_bbdev.c +3 -2
@@ -210,7 +210,7 @@ rte_bbdev_allocate(const char *name)
 		return NULL;
 	}
 
-	rte_atomic16_inc(&bbdev->data->process_cnt);
+	__atomic_add_fetch(&bbdev->data->process_cnt, 1,
 __ATOMIC_RELAXED);
 	bbdev->data->dev_id = dev_id;
 	bbdev->state = RTE_BBDEV_INITIALIZED;
@@ -252,7 +252,8 @@ rte_bbdev_release(struct rte_bbdev *bbdev)
 	}
 
 	/* clear shared BBDev Data if no process is using the device anymore
 */
-	if (rte_atomic16_dec_and_test(&bbdev->data->process_cnt))
+	if (__atomic_sub_fetch(&bbdev->data->process_cnt, 1,
+			      __ATOMIC_RELAXED) == 0)
 		memset(bbdev->data, 0, sizeof(*bbdev->data));
 
lib/librte_bbdev/rte_bbdev.c +0 -0
 	memset(bbdev, 0, sizeof(*bbdev));
(quoted diff) +0 -0

b/lib/librte_bbdev/rte_bbdev.h index 5729137..7017124 100644

lib/librte_bbdev/rte_bbdev.h +1 -4
@@ -33,7 +33,6 @@ extern "C" {
 #include <string.h>
 
 #include <rte_compat.h>
-#include <rte_atomic.h>
 #include <rte_bus.h>
 #include <rte_cpuflags.h>
 #include <rte_memory.h>
@@ -426,8 +425,7 @@ struct rte_bbdev_data {
 	uint16_t dev_id;  /**< Device ID */
 	int socket_id;  /**< NUMA socket that device is on */
 	bool started;  /**< Device run-time state */
-	/** Counter of processes using the device */
-	rte_atomic16_t process_cnt;
+	uint16_t process_cnt;  /** Counter of processes using the device */
 };
 
 /* Forward declarations */
--

2.7.4

#22 lore raw

Re: [dpdk-dev] [PATCH v3 2/4] bbdev: use C11 atomic builtins for device processing counter

Thanks Honnappa,

Quoted 107 lines

-----Original Message-----
From: Honnappa Nagarahalli <[email protected]>
Sent: Thursday, September 24, 2020 3:45 PM
To: Chautru, Nicolas <[email protected]>; Phil Yang <[email protected]>; [email protected]; [email protected]; Hunt, David <[email protected]>
Cc: Ruifeng Wang <[email protected]>; nd <[email protected]>; Honnappa Nagarahalli <[email protected]>; nd <[email protected]>
Subject: RE: [PATCH v3 2/4] bbdev: use C11 atomic builtins for device processing counter

<snip>

Quoted 5 lines

Hi Phil,
Naïve question but the deprecation document was stating that "DPDK will adopt C11 atomic operations semantics and provide wrappers using C11 atomic built-ins."

At the time of writing the deprecation notice, that was the thinking. However, through further discussions [1] in the community, it was decided to use atomic built-ins.

[1] http://mails.dpdk.org/archives/dev/2020-May/167416.html

Quoted 82 lines

Here you are using directly the C11 atomic built-ins and not providing and using a DPDK wrapper.
Wasn't the intent to have a new rte_... wrapper here? Ie. the same way as the __sync_fetch_and_add() were called before behind the rte_atomicNN_XX wrapper.

Thanks
Nic

Quoted 6 lines

-----Original Message-----
From: Phil Yang <[email protected]>
Sent: Wednesday, September 23, 2020 10:39 PM
To: [email protected]; [email protected]; Chautru, Nicolas <[email protected]>; Hunt, David <[email protected]>
Cc: [email protected]; [email protected];

[email protected]

Quoted 65 lines

Subject: [PATCH v3 2/4] bbdev: use C11 atomic builtins for device processing counter

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for device processing counter.

---
 lib/librte_bbdev/rte_bbdev.c | 5 +++--
lib/librte_bbdev/rte_bbdev.h
| 4 +---
 2 files changed, 4 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/lib/librte_bbdev/rte_bbdev.c index a4fdb69..5ba891c 100644

lib/librte_bbdev/rte_bbdev.c +1 -1
@@ -210,7 +210,7 @@ rte_bbdev_allocate(const char *name)
 		return NULL;
 	}
 
-	rte_atomic16_inc(&bbdev->data->process_cnt);
+	__atomic_add_fetch(&bbdev->data->process_cnt, 1,
 __ATOMIC_RELAXED);
 	bbdev->data->dev_id = dev_id;
 	bbdev->state = RTE_BBDEV_INITIALIZED;
@@ -252,7 +252,8 @@ rte_bbdev_release(struct rte_bbdev *bbdev)
 	}
 
 	/* clear shared BBDev Data if no process is using the device

anymore */

lib/librte_bbdev/rte_bbdev.c +2 -1
-	if (rte_atomic16_dec_and_test(&bbdev->data->process_cnt))
+	if (__atomic_sub_fetch(&bbdev->data->process_cnt, 1,
+			      __ATOMIC_RELAXED) == 0)
 		memset(bbdev->data, 0, sizeof(*bbdev->data));
 
 	memset(bbdev, 0, sizeof(*bbdev));
(quoted diff) +0 -0

b/lib/librte_bbdev/rte_bbdev.h index 5729137..7017124 100644

lib/librte_bbdev/rte_bbdev.h +2 -4
@@ -33,7 +33,6 @@ extern "C" {
 #include <string.h>
 
 #include <rte_compat.h>
-#include <rte_atomic.h>
 #include <rte_bus.h>
 #include <rte_cpuflags.h>
 #include <rte_memory.h>
@@ -426,8 +425,7 @@ struct rte_bbdev_data {
 	uint16_t dev_id;  /**< Device ID */
 	int socket_id;  /**< NUMA socket that device is on */
 	bool started;  /**< Device run-time state */
-	/** Counter of processes using the device */
-	rte_atomic16_t process_cnt;
+	uint16_t process_cnt;  /** Counter of processes using the device
+*/
 };
 
 /* Forward declarations */
--

2.7.4

author
#23 lore raw

PATCH v3 3/4 power: use C11 atomic builtins for power in use state update

In reply to Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for power in use state update.

---
 lib/librte_power/power_acpi_cpufreq.c   | 45 +++++++++++++++++++++++++--------
 lib/librte_power/power_pstate_cpufreq.c | 45 +++++++++++++++++++++++++--------
 2 files changed, 70 insertions(+), 20 deletions(-)
lib/librte_power/power_acpi_cpufreq.c +35 -10
index 583815a..84a9d75 100644
@@ -12,7 +12,6 @@
 #include <signal.h>
 #include <limits.h>
 
-#include <rte_atomic.h>
 #include <rte_memcpy.h>
 #include <rte_memory.h>
 #include <rte_string_fns.h>
@@ -86,7 +85,7 @@ struct rte_power_info {
 	FILE *f;                             /**< FD of scaling_setspeed */
 	char governor_ori[32];               /**< Original governor name */
 	uint32_t curr_idx;                   /**< Freq index in freqs array */
-	volatile uint32_t state;             /**< Power in use state */
+	uint32_t state;                      /**< Power in use state */
 	uint16_t turbo_available;            /**< Turbo Boost available */
 	uint16_t turbo_enable;               /**< Turbo Boost enable/disable */
 } __rte_cache_aligned;
@@ -300,6 +299,7 @@ int
 power_acpi_cpufreq_init(unsigned int lcore_id)
 {
 	struct rte_power_info *pi;
+	uint32_t exp_state;
 
 	if (lcore_id >= RTE_MAX_LCORE) {
 		RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
@@ -308,8 +308,16 @@ power_acpi_cpufreq_init(unsigned int lcore_id)
 	}
 
 	pi = &lcore_power_info[lcore_id];
-	if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
-			== 0) {
+	exp_state = POWER_IDLE;
+	/* The power in use state works as a guard variable between
+	 * the CPU frequency control initialization and exit process.
+	 * The ACQUIRE memory ordering here pairs with the RELEASE
+	 * ordering below as lock to make sure the frequency operations
+	 * in the critical section are done under the correct state.
+	 */
+	if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
+					POWER_ONGOING, 0,
+					__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
 		RTE_LOG(INFO, POWER, "Power management of lcore %u is "
 				"in use\n", lcore_id);
 		return -1;
@@ -346,12 +354,16 @@ power_acpi_cpufreq_init(unsigned int lcore_id)
 
 	RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
 			"power management\n", lcore_id);
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_USED,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return 0;
 
 fail:
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return -1;
 }
@@ -408,6 +420,7 @@ int
 power_acpi_cpufreq_exit(unsigned int lcore_id)
 {
 	struct rte_power_info *pi;
+	uint32_t exp_state;
 
 	if (lcore_id >= RTE_MAX_LCORE) {
 		RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
@@ -415,8 +428,16 @@ power_acpi_cpufreq_exit(unsigned int lcore_id)
 		return -1;
 	}
 	pi = &lcore_power_info[lcore_id];
-	if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
-			== 0) {
+	exp_state = POWER_USED;
+	/* The power in use state works as a guard variable between
+	 * the CPU frequency control initialization and exit process.
+	 * The ACQUIRE memory ordering here pairs with the RELEASE
+	 * ordering below as lock to make sure the frequency operations
+	 * in the critical section are done under the correct state.
+	 */
+	if (!__atomic_compare_exchange_n(&(pi->state), &exp_state,
+					POWER_ONGOING, 0,
+					__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
 		RTE_LOG(INFO, POWER, "Power management of lcore %u is "
 				"not used\n", lcore_id);
 		return -1;
@@ -436,12 +457,16 @@ power_acpi_cpufreq_exit(unsigned int lcore_id)
 	RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
 			"'userspace' mode and been set back to the "
 			"original\n", lcore_id);
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_IDLE,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return 0;
 
 fail:
-	rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
+	exp_state = POWER_ONGOING;
+	__atomic_compare_exchange_n(&(pi->state), &exp_state, POWER_UNKNOWN,
+				    0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
 
 	return -1;
 }
lib/librte_power/power_pstate_cpufreq.c +35 -10

101 lines not on this page · raw message

Signature · 1 line
2.7.4
Acked-by on v3 3/4
#24 lore raw

Re: [dpdk-dev] [PATCH v3 3/4] power: use C11 atomic builtins for power in use state update

Hi Phil,

Quoted 11 lines · Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for power in use state update.

---
  lib/librte_power/power_acpi_cpufreq.c   | 45 +++++++++++++++++++++++++--------
  lib/librte_power/power_pstate_cpufreq.c | 45 +++++++++++++++++++++++++--------
  2 files changed, 70 insertions(+), 20 deletions(-)

Looks good to me. Code looks good, and I applied it locally to have a test, and it's entering and exiting power manangemnt state fine on my systems here. Thanks.

author
#25 lore raw

PATCH v3 4/4 ethdev: use C11 atomic builtins for link status update

In reply to Phil Yang

Since rte_atomicXX APIs are not allowed to be used, use C11 atomic builtins for link status update.

---
 lib/librte_ethdev/rte_ethdev_driver.h | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)
lib/librte_ethdev/rte_ethdev_driver.h +5 -14
index 23cc1e0..04ac8e9 100644
@@ -920,8 +920,7 @@ static inline int
 rte_eth_linkstatus_set(struct rte_eth_dev *dev,
 		       const struct rte_eth_link *new_link)
 {
-	volatile uint64_t *dev_link
-		 = (volatile uint64_t *)&(dev->data->dev_link);
+	uint64_t *dev_link = (uint64_t *)&(dev->data->dev_link);
 	union {
 		uint64_t val64;
 		struct rte_eth_link link;
@@ -929,8 +928,8 @@ rte_eth_linkstatus_set(struct rte_eth_dev *dev,
 
 	RTE_BUILD_BUG_ON(sizeof(*new_link) != sizeof(uint64_t));
 
-	orig.val64 = rte_atomic64_exchange(dev_link,
-					   *(const uint64_t *)new_link);
+	orig.val64 = __atomic_exchange_n(dev_link, *(const uint64_t *)new_link,
+					__ATOMIC_SEQ_CST);
 
 	return (orig.link.link_status == new_link->link_status) ? -1 : 0;
 }
@@ -948,20 +947,12 @@ static inline void
 rte_eth_linkstatus_get(const struct rte_eth_dev *dev,
 		       struct rte_eth_link *link)
 {
-	volatile uint64_t *src = (uint64_t *)&(dev->data->dev_link);
+	uint64_t *src = (uint64_t *)&(dev->data->dev_link);
 	uint64_t *dst = (uint64_t *)link;
 
 	RTE_BUILD_BUG_ON(sizeof(*link) != sizeof(uint64_t));
 
-#ifdef __LP64__
-	/* if cpu arch has 64 bit unsigned lon then implicitly atomic */
-	*dst = *src;
-#else
-	/* can't use rte_atomic64_read because it returns signed int */
-	do {
-		*dst = *src;
-	} while (!rte_atomic64_cmpset(src, *dst, *dst));
-#endif
+	*dst = __atomic_load_n(src, __ATOMIC_SEQ_CST);
 }
 
 /**
Signature · 1 line
2.7.4
#26 lore raw
In reply to Phil Yang
Quoted 22 lines · Phil Yang

Since rte_atomicXX APIs are not allowed to be used[1][2], use C11 atomic builtins instead in eal, bbdev, power, and ethdev libs.

[1] http://code.dpdk.org/dpdk/latest/source/doc/guides/rel_notes/deprecation.rst#L87
[2] http://code.dpdk.org/dpdk/latest/source/devtools/checkpatches.sh#L80

v3:
remove unnecessary rte_atomic.h headers. (David)

v2:
Fix Clang int-conversion warning.

v1:
Initial version.

Phil Yang (4):
eal: use C11 atomic builtins for already initialized check
bbdev: use C11 atomic builtins for device processing counter
power: use C11 atomic builtins for power in use state update
ethdev: use C11 atomic builtins for link status update

Quoted 9 lines
 lib/librte_bbdev/rte_bbdev.c            |  5 ++--
 lib/librte_bbdev/rte_bbdev.h            |  4 +--
 lib/librte_eal/freebsd/eal.c            | 19 +++++++-------
 lib/librte_eal/linux/eal.c              | 21 +++++++--------
 lib/librte_ethdev/rte_ethdev_driver.h   | 19 ++++----------
 lib/librte_power/power_acpi_cpufreq.c   | 45 +++++++++++++++++++++++++--------
 lib/librte_power/power_pstate_cpufreq.c | 45 +++++++++++++++++++++++++--------
 7 files changed, 100 insertions(+), 58 deletions(-)

Series applied, thanks Phil.

Signature · 1 line
David Marchand