From: David Marchand <david.marchand@redhat.com>
To: dev@dpdk.org
Cc: probb@iol.unh.edu, Thomas Monjalon <thomas@monjalon.net>,
Ferruh Yigit <ferruh.yigit@amd.com>,
Andrew Rybchenko <andrew.rybchenko@oktetlabs.ru>
Subject: [PATCH v4 2/3] ethdev: avoid panicking in absence of ethdev shared data
Date: Wed, 27 Sep 2023 13:45:14 +0200 [thread overview]
Message-ID: <20230927114515.1245213-3-david.marchand@redhat.com> (raw)
In-Reply-To: <20230927114515.1245213-1-david.marchand@redhat.com>
This is a preparation step before freeing the ethdev shared data
memzone.
Previously, because the primary process never freed the memzone, a
secondary process could assume this memzone was present.
But in the next commit, this will change.
Make eth_dev_shared_data_prepare() report whether the memzone is
available so that upper level API can react accordingly.
Signed-off-by: David Marchand <david.marchand@redhat.com>
---
lib/ethdev/ethdev_driver.c | 23 ++++++++++++++-----
lib/ethdev/ethdev_private.c | 10 +++++---
lib/ethdev/ethdev_private.h | 2 +-
lib/ethdev/ethdev_trace.h | 6 +++--
lib/ethdev/rte_ethdev.c | 46 +++++++++++++++++++++++++------------
5 files changed, 60 insertions(+), 27 deletions(-)
diff --git a/lib/ethdev/ethdev_driver.c b/lib/ethdev/ethdev_driver.c
index c92cd4b947..b339e325a0 100644
--- a/lib/ethdev/ethdev_driver.c
+++ b/lib/ethdev/ethdev_driver.c
@@ -92,7 +92,8 @@ rte_eth_dev_allocate(const char *name)
/* Synchronize port creation between primary and secondary processes. */
rte_spinlock_lock(rte_mcfg_ethdev_get_lock());
- eth_dev_shared_data_prepare();
+ if (eth_dev_shared_data_prepare() == NULL)
+ goto unlock;
if (eth_dev_allocated(name) != NULL) {
RTE_ETHDEV_LOG(ERR,
@@ -128,9 +129,10 @@ rte_eth_dev_allocated(const char *name)
rte_spinlock_lock(rte_mcfg_ethdev_get_lock());
- eth_dev_shared_data_prepare();
-
- ethdev = eth_dev_allocated(name);
+ if (eth_dev_shared_data_prepare() != NULL)
+ ethdev = eth_dev_allocated(name);
+ else
+ ethdev = NULL;
rte_spinlock_unlock(rte_mcfg_ethdev_get_lock());
@@ -151,7 +153,8 @@ rte_eth_dev_attach_secondary(const char *name)
/* Synchronize port attachment to primary port creation and release. */
rte_spinlock_lock(rte_mcfg_ethdev_get_lock());
- eth_dev_shared_data_prepare();
+ if (eth_dev_shared_data_prepare() == NULL)
+ goto unlock;
for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
if (strcmp(eth_dev_shared_data->data[i].name, name) == 0)
@@ -166,6 +169,7 @@ rte_eth_dev_attach_secondary(const char *name)
RTE_ASSERT(eth_dev->data->port_id == i);
}
+unlock:
rte_spinlock_unlock(rte_mcfg_ethdev_get_lock());
return eth_dev;
}
@@ -219,12 +223,19 @@ rte_eth_dev_probing_finish(struct rte_eth_dev *dev)
int
rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
{
+ int ret;
+
if (eth_dev == NULL)
return -EINVAL;
rte_spinlock_lock(rte_mcfg_ethdev_get_lock());
- eth_dev_shared_data_prepare();
+ if (eth_dev_shared_data_prepare() == NULL)
+ ret = -EINVAL;
+ else
+ ret = 0;
rte_spinlock_unlock(rte_mcfg_ethdev_get_lock());
+ if (ret != 0)
+ return ret;
if (eth_dev->state != RTE_ETH_DEV_UNUSED)
rte_eth_dev_callback_process(eth_dev,
diff --git a/lib/ethdev/ethdev_private.c b/lib/ethdev/ethdev_private.c
index 6756625729..911de1e595 100644
--- a/lib/ethdev/ethdev_private.c
+++ b/lib/ethdev/ethdev_private.c
@@ -318,7 +318,7 @@ rte_eth_call_tx_callbacks(uint16_t port_id, uint16_t queue_id,
return nb_pkts;
}
-void
+void *
eth_dev_shared_data_prepare(void)
{
const unsigned int flags = 0;
@@ -332,8 +332,10 @@ eth_dev_shared_data_prepare(void)
rte_socket_id(), flags);
} else
mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
- if (mz == NULL)
- rte_panic("Cannot allocate ethdev shared data\n");
+ if (mz == NULL) {
+ RTE_ETHDEV_LOG(ERR, "Cannot allocate ethdev shared data\n");
+ goto out;
+ }
eth_dev_shared_data = mz->addr;
if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
@@ -343,6 +345,8 @@ eth_dev_shared_data_prepare(void)
sizeof(eth_dev_shared_data->data));
}
}
+out:
+ return eth_dev_shared_data;
}
void
diff --git a/lib/ethdev/ethdev_private.h b/lib/ethdev/ethdev_private.h
index f7706e6a95..1572da7b48 100644
--- a/lib/ethdev/ethdev_private.h
+++ b/lib/ethdev/ethdev_private.h
@@ -67,7 +67,7 @@ void eth_dev_fp_ops_setup(struct rte_eth_fp_ops *fpo,
const struct rte_eth_dev *dev);
-void eth_dev_shared_data_prepare(void)
+void *eth_dev_shared_data_prepare(void)
__rte_exclusive_locks_required(rte_mcfg_ethdev_get_lock());
void eth_dev_rxq_release(struct rte_eth_dev *dev, uint16_t qid);
diff --git a/lib/ethdev/ethdev_trace.h b/lib/ethdev/ethdev_trace.h
index 423e71236e..e367d29c3a 100644
--- a/lib/ethdev/ethdev_trace.h
+++ b/lib/ethdev/ethdev_trace.h
@@ -112,8 +112,9 @@ RTE_TRACE_POINT(
RTE_TRACE_POINT(
rte_ethdev_trace_owner_new,
- RTE_TRACE_POINT_ARGS(uint64_t owner_id),
+ RTE_TRACE_POINT_ARGS(uint64_t owner_id, int ret),
rte_trace_point_emit_u64(owner_id);
+ rte_trace_point_emit_int(ret);
)
RTE_TRACE_POINT(
@@ -377,10 +378,11 @@ RTE_TRACE_POINT(
RTE_TRACE_POINT(
rte_ethdev_trace_owner_get,
RTE_TRACE_POINT_ARGS(uint16_t port_id,
- const struct rte_eth_dev_owner *owner),
+ const struct rte_eth_dev_owner *owner, int ret),
rte_trace_point_emit_u16(port_id);
rte_trace_point_emit_u64(owner->id);
rte_trace_point_emit_string(owner->name);
+ rte_trace_point_emit_int(ret);
)
RTE_TRACE_POINT(
diff --git a/lib/ethdev/rte_ethdev.c b/lib/ethdev/rte_ethdev.c
index e0d22c27c6..5c9495ecfe 100644
--- a/lib/ethdev/rte_ethdev.c
+++ b/lib/ethdev/rte_ethdev.c
@@ -388,7 +388,7 @@ rte_eth_find_next_sibling(uint16_t port_id, uint16_t ref_port_id)
static bool
eth_dev_is_allocated(const struct rte_eth_dev *ethdev)
{
- return ethdev->data->name[0] != '\0';
+ return ethdev->data != NULL && ethdev->data->name[0] != '\0';
}
int
@@ -433,6 +433,8 @@ rte_eth_find_next_owned_by(uint16_t port_id, const uint64_t owner_id)
int
rte_eth_dev_owner_new(uint64_t *owner_id)
{
+ int ret;
+
if (owner_id == NULL) {
RTE_ETHDEV_LOG(ERR, "Cannot get new owner ID to NULL\n");
return -EINVAL;
@@ -440,14 +442,18 @@ rte_eth_dev_owner_new(uint64_t *owner_id)
rte_spinlock_lock(rte_mcfg_ethdev_get_lock());
- eth_dev_shared_data_prepare();
- *owner_id = eth_dev_shared_data->next_owner_id++;
+ if (eth_dev_shared_data_prepare() != NULL) {
+ *owner_id = eth_dev_shared_data->next_owner_id++;
+ ret = 0;
+ } else {
+ ret = -ENOMEM;
+ }
rte_spinlock_unlock(rte_mcfg_ethdev_get_lock());
- rte_ethdev_trace_owner_new(*owner_id);
+ rte_ethdev_trace_owner_new(*owner_id, ret);
- return 0;
+ return ret;
}
static int
@@ -506,8 +512,10 @@ rte_eth_dev_owner_set(const uint16_t port_id,
rte_spinlock_lock(rte_mcfg_ethdev_get_lock());
- eth_dev_shared_data_prepare();
- ret = eth_dev_owner_set(port_id, RTE_ETH_DEV_NO_OWNER, owner);
+ if (eth_dev_shared_data_prepare() != NULL)
+ ret = eth_dev_owner_set(port_id, RTE_ETH_DEV_NO_OWNER, owner);
+ else
+ ret = -ENOMEM;
rte_spinlock_unlock(rte_mcfg_ethdev_get_lock());
@@ -525,8 +533,10 @@ rte_eth_dev_owner_unset(const uint16_t port_id, const uint64_t owner_id)
rte_spinlock_lock(rte_mcfg_ethdev_get_lock());
- eth_dev_shared_data_prepare();
- ret = eth_dev_owner_set(port_id, owner_id, &new_owner);
+ if (eth_dev_shared_data_prepare() != NULL)
+ ret = eth_dev_owner_set(port_id, owner_id, &new_owner);
+ else
+ ret = -ENOMEM;
rte_spinlock_unlock(rte_mcfg_ethdev_get_lock());
@@ -543,8 +553,9 @@ rte_eth_dev_owner_delete(const uint64_t owner_id)
rte_spinlock_lock(rte_mcfg_ethdev_get_lock());
- eth_dev_shared_data_prepare();
- if (eth_is_valid_owner_id(owner_id)) {
+ if (eth_dev_shared_data_prepare() == NULL) {
+ ret = -ENOMEM;
+ } else if (eth_is_valid_owner_id(owner_id)) {
for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
struct rte_eth_dev_data *data =
rte_eth_devices[port_id].data;
@@ -573,6 +584,7 @@ int
rte_eth_dev_owner_get(const uint16_t port_id, struct rte_eth_dev_owner *owner)
{
struct rte_eth_dev *ethdev;
+ int ret;
RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
ethdev = &rte_eth_devices[port_id];
@@ -591,14 +603,18 @@ rte_eth_dev_owner_get(const uint16_t port_id, struct rte_eth_dev_owner *owner)
rte_spinlock_lock(rte_mcfg_ethdev_get_lock());
- eth_dev_shared_data_prepare();
- rte_memcpy(owner, ðdev->data->owner, sizeof(*owner));
+ if (eth_dev_shared_data_prepare() != NULL) {
+ rte_memcpy(owner, ðdev->data->owner, sizeof(*owner));
+ ret = 0;
+ } else {
+ ret = -ENOMEM;
+ }
rte_spinlock_unlock(rte_mcfg_ethdev_get_lock());
- rte_ethdev_trace_owner_get(port_id, owner);
+ rte_ethdev_trace_owner_get(port_id, owner, ret);
- return 0;
+ return ret;
}
int
--
2.41.0
next prev parent reply other threads:[~2023-09-27 11:45 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-18 9:13 [PATCH 0/2] Release ethdev shared memory on port cleanup David Marchand
2023-08-18 9:13 ` [PATCH 1/2] ethdev: protect shared memory accesses under one lock David Marchand
2023-08-18 9:13 ` [PATCH 2/2] ethdev: cleanup shared data with the last port David Marchand
2023-08-18 11:36 ` David Marchand
2023-08-18 10:18 ` [PATCH 0/2] Release ethdev shared memory on port cleanup Morten Brørup
2023-08-31 15:34 ` Thomas Monjalon
2023-08-18 13:41 ` [PATCH v2 " David Marchand
2023-08-18 13:41 ` [PATCH v2 1/2] ethdev: protect shared memory accesses under one lock David Marchand
2023-08-18 13:41 ` [PATCH v2 2/2] ethdev: cleanup shared data with the last port David Marchand
2023-08-21 8:58 ` [PATCH v3 0/3] Release ethdev shared memory on port cleanup David Marchand
2023-08-21 8:58 ` [PATCH v3 1/3] ethdev: protect shared memory accesses under one lock David Marchand
2023-08-21 8:58 ` [PATCH v3 2/3] ethdev: avoid panicking in absence of ethdev shared data David Marchand
2023-08-21 8:58 ` [PATCH v3 3/3] ethdev: cleanup shared data with the last port David Marchand
2023-08-31 16:05 ` [PATCH v3 0/3] Release ethdev shared memory on port cleanup Thomas Monjalon
2023-09-01 7:32 ` David Marchand
2023-09-27 11:45 ` [PATCH v4 " David Marchand
2023-09-27 11:45 ` [PATCH v4 1/3] ethdev: protect shared memory accesses under one lock David Marchand
2023-09-27 11:45 ` David Marchand [this message]
2023-09-27 11:45 ` [PATCH v4 3/3] ethdev: cleanup shared data with the last port David Marchand
2023-10-11 12:53 ` [PATCH v4 0/3] Release ethdev shared memory on port cleanup Thomas Monjalon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230927114515.1245213-3-david.marchand@redhat.com \
--to=david.marchand@redhat.com \
--cc=andrew.rybchenko@oktetlabs.ru \
--cc=dev@dpdk.org \
--cc=ferruh.yigit@amd.com \
--cc=probb@iol.unh.edu \
--cc=thomas@monjalon.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).