Show a patch.

GET /api/patches/40749/
Content-Type: application/json
Vary: Accept

    "id": 40749,
    "url": "",
    "web_url": "",
    "project": {
        "id": 1,
        "url": "",
        "name": "DPDK",
        "link_name": "dpdk",
        "list_id": "",
        "list_email": "",
        "web_url": "",
        "scm_url": "git://",
        "webscm_url": ""
    "msgid": "<>",
    "date": "2018-06-07T12:38:32",
    "name": "[dpdk-dev,05/22] ethdev: introduce device lock",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": true,
    "hash": "fe1f2310d0986ee867e1134ecec59a939ee5b71e",
    "submitter": {
        "id": 504,
        "url": "",
        "name": "Zhang, Qi Z",
        "email": ""
    "delegate": {
        "id": 1,
        "url": "",
        "username": "tmonjalo",
        "first_name": "Thomas",
        "last_name": "Monjalon",
        "email": ""
    "mbox": "",
    "series": [
            "id": 30,
            "url": "",
            "web_url": "",
            "date": "2018-06-07T12:38:27",
            "name": "enable hotplug on multi-process",
            "version": 1,
            "mbox": ""
    "comments": "",
    "check": "fail",
    "checks": "",
    "tags": {},
    "headers": {
        "X-Mailman-Version": "2.1.15",
        "X-ExtLoop1": "1",
        "Errors-To": "",
        "X-Amp-Result": "SKIPPED(no attachment in message)",
        "X-Mailer": "git-send-email 2.13.6",
        "Received": [
            "from [] (localhost [])\n\tby (Postfix) with ESMTP id 30C831B1E5;\n\tThu,  7 Jun 2018 14:38:36 +0200 (CEST)",
            "from ( [])\n\tby (Postfix) with ESMTP id 4DA3C1B1B5\n\tfor <>; Thu,  7 Jun 2018 14:38:32 +0200 (CEST)",
            "from ([])\n\tby with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384;\n\t07 Jun 2018 05:38:31 -0700",
            "from ([])\n\tby with ESMTP; 07 Jun 2018 05:38:30 -0700"
        "References": "<>",
        "X-Amp-File-Uploaded": "False",
        "X-BeenThere": "",
        "Message-Id": "<>",
        "X-IronPort-AV": "E=Sophos;i=\"5.49,486,1520924400\"; d=\"scan'208\";a=\"62605571\"",
        "List-Id": "DPDK patches and discussions <>",
        "Precedence": "list",
        "From": "Qi Zhang <>",
        "X-Original-To": "",
        "List-Post": "<>",
        "Return-Path": "<>",
        "Sender": "\"dev\" <>",
        "List-Help": "<>",
        "In-Reply-To": "<>",
        "List-Subscribe": "<>,\n\t<>",
        "To": ",\n\",
        "Delivered-To": "",
        "List-Unsubscribe": "<>,\n\t<>",
        "Date": "Thu,  7 Jun 2018 20:38:32 +0800",
        "Cc": ",,, \n\,,\n\, Qi Zhang <>",
        "List-Archive": "<>",
        "Subject": "[dpdk-dev] [PATCH 05/22] ethdev: introduce device lock"
    "content": "Introduce API rte_eth_dev_lock and rte_eth_dev_unlock to let\napplication lock or unlock on specific ethdev, a locked device\ncan't be detached, this help applicaiton to prevent unexpected\ndevice detaching, especially in multi-process envrionment.\n\nAslo the new API let application to register a callback function\nwhich will be invoked before a device is going to be detached,\nthe return value of the function will decide if device will continue\nbe detached or not, this support application to do condition check\nat runtime.\n\nSigned-off-by: Qi Zhang <>\n---\n lib/librte_ethdev/Makefile          |   1 +\n lib/librte_ethdev/rte_ethdev.c      |  41 ++++++++++++++-\n lib/librte_ethdev/rte_ethdev.h      |  64 ++++++++++++++++++++++\n lib/librte_ethdev/rte_ethdev_lock.c | 102 ++++++++++++++++++++++++++++++++++++\n lib/librte_ethdev/rte_ethdev_lock.h |  23 ++++++++\n lib/librte_ethdev/rte_ethdev_mp.c   |   3 +-\n 6 files changed, 232 insertions(+), 2 deletions(-)\n create mode 100644 lib/librte_ethdev/rte_ethdev_lock.c\n create mode 100644 lib/librte_ethdev/rte_ethdev_lock.h",
    "diff": "diff --git a/lib/librte_ethdev/Makefile b/lib/librte_ethdev/Makefile\nindex 04e93f337..5c4646469 100644\n--- a/lib/librte_ethdev/Makefile\n+++ b/lib/librte_ethdev/Makefile\n@@ -20,6 +20,7 @@ LIBABIVER := 9\n \n SRCS-y += rte_ethdev.c\n SRCS-y += rte_ethdev_mp.c\n+SRCS-y += rte_ethdev_lock.c\n SRCS-y += rte_flow.c\n SRCS-y += rte_tm.c\n SRCS-y += rte_mtr.c\ndiff --git a/lib/librte_ethdev/rte_ethdev.c b/lib/librte_ethdev/rte_ethdev.c\nindex 24360f522..6494e71a4 100644\n--- a/lib/librte_ethdev/rte_ethdev.c\n+++ b/lib/librte_ethdev/rte_ethdev.c\n@@ -42,6 +42,7 @@\n #include \"rte_ethdev_driver.h\"\n #include \"ethdev_profile.h\"\n #include \"rte_ethdev_mp.h\"\n+#include \"rte_ethdev_lock.h\"\n \n int ethdev_logtype;\n \n@@ -787,7 +788,6 @@ rte_eth_dev_attach(const char *devargs, uint16_t *port_id)\n int\n rte_eth_dev_attach_private(const char *devargs, uint16_t *port_id)\n {\n-\n \tif (rte_eal_process_type() == RTE_PROC_PRIMARY)\n \t\treturn -ENOTSUP;\n \n@@ -828,6 +828,10 @@ rte_eth_dev_detach(uint16_t port_id, char *name __rte_unused)\n \t\treturn req.result;\n \t}\n \n+\tret = process_lock_callbacks(port_id);\n+\tif (ret)\n+\t\treturn ret;\n+\n \t/* check pre_detach */\n \treq.t = REQ_TYPE_PRE_DETACH;\n \treq.port_id = port_id;\n@@ -870,6 +874,7 @@ int\n rte_eth_dev_detach_private(uint16_t port_id, char *name __rte_unused)\n {\n \tuint32_t dev_flags;\n+\tint ret;\n \n \tif (rte_eal_process_type() == RTE_PROC_PRIMARY)\n \t\treturn -ENOTSUP;\n@@ -883,6 +888,10 @@ rte_eth_dev_detach_private(uint16_t port_id, char *name __rte_unused)\n \t\treturn -ENOTSUP;\n \t}\n \n+\tret = process_lock_callbacks(port_id);\n+\tif (ret)\n+\t\treturn ret;\n+\n \treturn do_eth_dev_detach(port_id);\n }\n \n@@ -4683,6 +4692,36 @@ rte_eth_devargs_parse(const char *dargs, struct rte_eth_devargs *eth_da)\n \treturn result;\n }\n \n+static int\n+dev_is_busy(uint16_t port_id __rte_unused, void *user_args __rte_unused)\n+{\n+\treturn -EBUSY;\n+}\n+\n+int\n+rte_eth_dev_lock(uint16_t port_id, rte_eth_dev_lock_callback_t callback,\n+\t\t void *user_args)\n+{\n+\tRTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);\n+\n+\tif (callback == NULL)\n+\t\treturn register_lock_callback(port_id, dev_is_busy, NULL);\n+\telse\n+\t\treturn register_lock_callback(port_id, callback, user_args);\n+}\n+\n+int\n+rte_eth_dev_unlock(uint16_t port_id, rte_eth_dev_lock_callback_t callback,\n+\t\t   void *user_args)\n+{\n+\tRTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);\n+\n+\tif (callback == NULL)\n+\t\treturn unregister_lock_callback(port_id, dev_is_busy, NULL);\n+\telse\n+\t\treturn unregister_lock_callback(port_id, callback, user_args);\n+}\n+\n RTE_INIT(ethdev_init_log);\n static void\n ethdev_init_log(void)\ndiff --git a/lib/librte_ethdev/rte_ethdev.h b/lib/librte_ethdev/rte_ethdev.h\nindex bb03d613b..506b6acdd 100644\n--- a/lib/librte_ethdev/rte_ethdev.h\n+++ b/lib/librte_ethdev/rte_ethdev.h\n@@ -4356,6 +4356,70 @@ rte_eth_tx_buffer(uint16_t port_id, uint16_t queue_id,\n \treturn rte_eth_tx_buffer_flush(port_id, queue_id, buffer);\n }\n \n+/**\n+ * Callback function before device is detached.\n+ *\n+ * This type of function will be added into a function list, and will be\n+ * invoked before device be detached. Application can register a callback\n+ * function so it can be notified and do some cleanup before detach happen.\n+ * Also, any callback function return !0 value will prevent device be\n+ * detached(ref. rte_eth_dev_lock and rte_eth_dev_unlock).\n+ *\n+ * @param port_id\n+ *   The port identifier of the Ethernet device.\n+ * @param user_args\n+ *   This is parameter \"user_args\" be saved when callback function is\n+ *   registered(rte_dev_eth_lock).\n+ *\n+ * @return\n+ *   0  device is allowed be detached.\n+ *   !0 device is not allowed be detached.\n+ */\n+typedef int (*rte_eth_dev_lock_callback_t)(uint16_t port_id, void *user_args);\n+\n+/**\n+ * Lock an Ethernet Device directly or register a callback function\n+ * for condition check at runtime, this help application to prevent\n+ * a device be detached unexpectly.\n+ * NOTE: Lock a device mutliple times with same parmeter will increase\n+ * a ref_count, and coresponding unlock decrease the ref_count, the\n+ * device will be unlocked when ref_count reach 0.\n+ *\n+ * @param port_id\n+ *   The port identifier of the Ethernet device.\n+ * @param callback\n+ *   !NULL the callback function will be added into a pre-detach list,\n+ *         it will be invoked when a device is going to be detached. The\n+ *         return value will decide if continue detach the device or not.\n+ *   NULL  lock the device directly, basically this just regiter a empty\n+ *         callback function(dev_is_busy) that return -EBUSY, so we can\n+ *         handle the pre-detach check in unified way.\n+ * @param user_args\n+ *   parameter will be parsed to callback function, only valid when\n+ *   callback != NULL.\n+ * @return\n+ *   0 on success, negative on error.\n+ */\n+int rte_eth_dev_lock(uint16_t port_id, rte_eth_dev_lock_callback_t callback,\n+\t\t     void *user_args);\n+\n+/**\n+ * Reverse operation of rte_eth_dev_lock.\n+ *\n+ * @param port_id\n+ *   The port identifier of the Ethernet device.\n+ * @param callback\n+ *   NULL  decrease the ref_count of default callback function.\n+ *   !NULL decrease the ref_count of specific callback with matched\n+ *         user_args.\n+ * @param user_args\n+ *   parameter to match, only valid when callback != NULL.\n+ * @return\n+ *   0 on success, negative on error.\n+ */\n+int rte_eth_dev_unlock(uint16_t port_id, rte_eth_dev_lock_callback_t callback,\n+\t\t       void *user_args);\n+\n #ifdef __cplusplus\n }\n #endif\ndiff --git a/lib/librte_ethdev/rte_ethdev_lock.c b/lib/librte_ethdev/rte_ethdev_lock.c\nnew file mode 100644\nindex 000000000..688d1d70a\n--- /dev/null\n+++ b/lib/librte_ethdev/rte_ethdev_lock.c\n@@ -0,0 +1,102 @@\n+/* SPDX-License-Identifier: BSD-3-Clause\n+ * Copyright(c) 2018 Intel Corporation\n+ */\n+#include \"rte_ethdev_lock.h\"\n+\n+struct lock_entry {\n+\tTAILQ_ENTRY(lock_entry) next;\n+\trte_eth_dev_lock_callback_t callback;\n+\tuint16_t port_id;\n+\tvoid *user_args;\n+\tint ref_count;\n+};\n+\n+TAILQ_HEAD(lock_entry_list, lock_entry);\n+static struct lock_entry_list lock_entry_list =\n+\tTAILQ_HEAD_INITIALIZER(lock_entry_list);\n+static rte_spinlock_t lock_entry_lock = RTE_SPINLOCK_INITIALIZER;\n+\n+int\n+register_lock_callback(uint16_t port_id,\n+\t\t       rte_eth_dev_lock_callback_t callback,\n+\t\t       void *user_args)\n+{\n+\tstruct lock_entry *le;\n+\n+\trte_spinlock_lock(&lock_entry_lock);\n+\n+\tTAILQ_FOREACH(le, &lock_entry_list, next) {\n+\t\tif (le->port_id == port_id &&\n+\t\t    le->callback == callback &&\n+\t\t    le->user_args == user_args)\n+\t\t\tbreak;\n+\t}\n+\n+\tif (!le) {\n+\t\tle = calloc(1, sizeof(struct lock_entry));\n+\t\tif (!le) {\n+\t\t\trte_spinlock_unlock(&lock_entry_lock);\n+\t\t\treturn -ENOMEM;\n+\t\t}\n+\t\tle->callback = callback;\n+\t\tle->port_id = port_id;\n+\t\tle->user_args = user_args;\n+\t\tTAILQ_INSERT_TAIL(&lock_entry_list, le, next);\n+\t}\n+\tle->ref_count++;\n+\n+\trte_spinlock_unlock(&lock_entry_lock);\n+\treturn 0;\n+}\n+\n+int\n+unregister_lock_callback(uint16_t port_id,\n+\t\t\t rte_eth_dev_lock_callback_t callback,\n+\t\t\t void *user_args)\n+{\n+\tstruct lock_entry *le;\n+\tint ret = 0;\n+\n+\trte_spinlock_lock(&lock_entry_lock);\n+\n+\tTAILQ_FOREACH(le, &lock_entry_list, next) {\n+\t\tif (le->port_id == port_id &&\n+\t\t    le->callback == callback &&\n+\t\t    le->user_args == user_args)\n+\t\t\tbreak;\n+\t}\n+\n+\tif (le) {\n+\t\tle->ref_count--;\n+\t\tif (!le->ref_count) {\n+\t\t\tTAILQ_REMOVE(&lock_entry_list, le, next);\n+\t\t\tfree(le);\n+\t\t}\n+\t} else {\n+\t\tret = -ENOENT;\n+\t}\n+\n+\trte_spinlock_unlock(&lock_entry_lock);\n+\treturn ret;\n+}\n+\n+int\n+process_lock_callbacks(uint16_t port_id)\n+{\n+\tstruct lock_entry *le;\n+\n+\trte_spinlock_lock(&lock_entry_lock);\n+\n+\tTAILQ_FOREACH(le, &lock_entry_list, next) {\n+\t\tif (le->port_id != port_id)\n+\t\t\tcontinue;\n+\n+\t\tif (le->callback(port_id, le->user_args)) {\n+\t\t\trte_spinlock_unlock(&lock_entry_lock);\n+\t\t\treturn -EBUSY;\n+\t\t}\n+\t}\n+\n+\trte_spinlock_unlock(&lock_entry_lock);\n+\treturn 0;\n+}\ndiff --git a/lib/librte_ethdev/rte_ethdev_lock.h b/lib/librte_ethdev/rte_ethdev_lock.h\nnew file mode 100644\nindex 000000000..7b370c926\n--- /dev/null\n+++ b/lib/librte_ethdev/rte_ethdev_lock.h\n@@ -0,0 +1,23 @@\n+/* SPDX-License-Identifier: BSD-3-Clause\n+ * Copyright(c) 2018 Intel Corporation\n+ */\n+\n+#ifndef _RTE_ETHDEV_LOCK_H_\n+#define _RTE_ETHDEV_LOCK_H_\n+\n+#include \"rte_ethdev.h\"\n+\n+int\n+register_lock_callback(uint16_t port_id,\n+\t\t       rte_eth_dev_lock_callback_t callback,\n+\t\t       void *user_args);\n+\n+int\n+unregister_lock_callback(uint16_t port_id,\n+\t\t\t rte_eth_dev_lock_callback_t callback,\n+\t\t\t void *user_args);\n+\n+int\n+process_lock_callbacks(uint16_t port_id);\n+\n+#endif\ndiff --git a/lib/librte_ethdev/rte_ethdev_mp.c b/lib/librte_ethdev/rte_ethdev_mp.c\nindex 8ede8151d..e23c8b010 100644\n--- a/lib/librte_ethdev/rte_ethdev_mp.c\n+++ b/lib/librte_ethdev/rte_ethdev_mp.c\n@@ -4,6 +4,7 @@\n \n #include \"rte_ethdev_driver.h\"\n #include \"rte_ethdev_mp.h\"\n+#include \"rte_ethdev_lock.h\"\n \n static int detach_on_secondary(uint16_t port_id)\n {\n@@ -101,7 +102,7 @@ static int handle_primary_request(const struct rte_mp_msg *msg, const void *peer\n \t\tret = attach_on_secondary(req->devargs, req->port_id);\n \t\tbreak;\n \tcase REQ_TYPE_PRE_DETACH:\n-\t\tret = 0;\n+\t\tret = process_lock_callbacks(req->port_id);\n \t\tbreak;\n \tcase REQ_TYPE_DETACH:\n \tcase REQ_TYPE_ATTACH_ROLLBACK:\n",
    "prefixes": [