From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by dpdk.org (Postfix) with ESMTP id 2AA661BB05 for ; Wed, 4 Apr 2018 04:58:53 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Apr 2018 19:58:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.48,404,1517904000"; d="scan'208";a="217343534" Received: from fmsmsx104.amr.corp.intel.com ([10.18.124.202]) by fmsmga006.fm.intel.com with ESMTP; 03 Apr 2018 19:58:52 -0700 Received: from fmsmsx123.amr.corp.intel.com (10.18.125.38) by fmsmsx104.amr.corp.intel.com (10.18.124.202) with Microsoft SMTP Server (TLS) id 14.3.319.2; Tue, 3 Apr 2018 19:58:52 -0700 Received: from shsmsx104.ccr.corp.intel.com (10.239.4.70) by fmsmsx123.amr.corp.intel.com (10.18.125.38) with Microsoft SMTP Server (TLS) id 14.3.319.2; Tue, 3 Apr 2018 19:58:52 -0700 Received: from shsmsx103.ccr.corp.intel.com ([169.254.4.241]) by SHSMSX104.ccr.corp.intel.com ([169.254.5.43]) with mapi id 14.03.0319.002; Wed, 4 Apr 2018 10:58:50 +0800 From: "Zhang, Qi Z" To: "Guo, Jia" , "stephen@networkplumber.org" , "Richardson, Bruce" , "Yigit, Ferruh" , "Ananyev, Konstantin" , "gaetan.rivet@6wind.com" , "Wu, Jingjing" , "thomas@monjalon.net" , "motih@mellanox.com" , "Van Haaren, Harry" , "Tan, Jianfeng" CC: "jblunck@infradead.org" , "shreyansh.jain@nxp.com" , "dev@dpdk.org" , "Guo, Jia" , "Zhang, Helin" Thread-Topic: [dpdk-dev] [PATCH V18 3/5] eal: add failure handler mechanism for hot plug Thread-Index: AQHTy3hLTd5ywP3Sm0O9Rcb5GywPAqPv6j5g Date: Wed, 4 Apr 2018 02:58:49 +0000 Message-ID: <039ED4275CED7440929022BC67E70611531810AE@SHSMSX103.ccr.corp.intel.com> References: <1522779443-1932-1-git-send-email-jia.guo@intel.com> <1522779443-1932-4-git-send-email-jia.guo@intel.com> In-Reply-To: <1522779443-1932-4-git-send-email-jia.guo@intel.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: dlp-product: dlpe-windows dlp-version: 11.0.0.116 dlp-reaction: no-action x-originating-ip: [10.239.127.40] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: [dpdk-dev] [PATCH V18 3/5] eal: add failure handler mechanism for hot plug X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Apr 2018 02:58:54 -0000 > -----Original Message----- > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Jeff Guo > Sent: Wednesday, April 4, 2018 2:17 AM > To: stephen@networkplumber.org; Richardson, Bruce > ; Yigit, Ferruh ; > Ananyev, Konstantin ; > gaetan.rivet@6wind.com; Wu, Jingjing ; > thomas@monjalon.net; motih@mellanox.com; Van Haaren, Harry > ; Tan, Jianfeng > Cc: jblunck@infradead.org; shreyansh.jain@nxp.com; dev@dpdk.org; Guo, Jia > ; Zhang, Helin > Subject: [dpdk-dev] [PATCH V18 3/5] eal: add failure handler mechanism fo= r > hot plug >=20 > This patch introduces an API (rte_dev_handle_hot_unplug) to handle device > hot unplug event. When device be hot plug out, the device resource become > invalid, if this resource is still be unexpected read/write, system will = crash. The > api let user register the hot unplug handler, when hot plug failure occur= , the > working thread will be block until the uevent mechanism successful recove= ry > the memory and guaranty the application keep running smoothly. >=20 > Signed-off-by: Jeff Guo > --- > v16->v15: > add document and signal bus handler > --- > doc/guides/rel_notes/release_18_05.rst | 6 ++ > lib/librte_eal/common/include/rte_dev.h | 19 +++++ > lib/librte_eal/linuxapp/eal/eal_dev.c | 134 > +++++++++++++++++++++++++++++- > lib/librte_eal/linuxapp/igb_uio/igb_uio.c | 4 + > lib/librte_eal/rte_eal_version.map | 1 + > 5 files changed, 163 insertions(+), 1 deletion(-) >=20 > diff --git a/doc/guides/rel_notes/release_18_05.rst > b/doc/guides/rel_notes/release_18_05.rst > index 37e00c4..3aacbf1 100644 > --- a/doc/guides/rel_notes/release_18_05.rst > +++ b/doc/guides/rel_notes/release_18_05.rst > @@ -51,6 +51,12 @@ New Features > * ``rte_dev_event_callback_register`` and > ``rte_dev_event_callback_unregister`` > are for the user's callbacks register and unregister. >=20 > +* **Added hot plug failure handler.** > + > + Added a failure handler machenism to handle hot plug removal. > + > + * ``rte_dev_handle_hot_unplug`` for handle hot plug removel failure. > + > API Changes > ----------- >=20 > diff --git a/lib/librte_eal/common/include/rte_dev.h > b/lib/librte_eal/common/include/rte_dev.h > index 4c78938..7075e56 100644 > --- a/lib/librte_eal/common/include/rte_dev.h > +++ b/lib/librte_eal/common/include/rte_dev.h > @@ -361,4 +361,23 @@ rte_dev_event_monitor_start(void); > */ > int __rte_experimental > rte_dev_event_monitor_stop(void); > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice > + * > + * It can be used to register the device signal bus handler, and save > +the > + * current environment of each thread, when signal bus error invoke, > +the > + * handler would restore the environment by long jmp to each working > + * thread, then block the thread to waiting until the memory recovery > + * and remapping be finished, that would guaranty the system not crash > + * when the device be hot unplug. > + * > + * @param none > + * @return > + * - On success, zero. > + * - On failure, a negative value. > + */ > +int __rte_experimental > +rte_dev_handle_hot_unplug(void); > #endif /* _RTE_DEV_H_ */ > diff --git a/lib/librte_eal/linuxapp/eal/eal_dev.c > b/lib/librte_eal/linuxapp/eal/eal_dev.c > index 9f2ee40..fabb37a 100644 > --- a/lib/librte_eal/linuxapp/eal/eal_dev.c > +++ b/lib/librte_eal/linuxapp/eal/eal_dev.c > @@ -4,6 +4,9 @@ >=20 > #include > #include > +#include > +#include > +#include > #include > #include >=20 > @@ -12,12 +15,17 @@ > #include > #include > #include > +#include > +#include >=20 > #include "eal_private.h" >=20 > static struct rte_intr_handle intr_handle =3D {.fd =3D -1 }; static boo= l > monitor_started; >=20 > +pthread_mutex_t failure_recovery_lock; > +pthread_cond_t failure_recovery_cond; > + > #define EAL_UEV_MSG_LEN 4096 > #define EAL_UEV_MSG_ELEM_LEN 128 >=20 > @@ -29,6 +37,22 @@ enum eal_dev_event_subsystem { > EAL_DEV_EVENT_SUBSYSTEM_MAX > }; >=20 > +static RTE_DEFINE_PER_LCORE(sigjmp_buf, unplug_longjmp_env); > + > +static void sigbus_handler(int signum __rte_unused) { > + RTE_LOG(DEBUG, EAL, "receive SIGBUS error!\n"); > + siglongjmp(RTE_PER_LCORE(unplug_longjmp_env), 1); } > + > +static int cmp_dev_name(const struct rte_device *dev, > + const void *_name) > +{ > + const char *name =3D _name; > + > + return strcmp(dev->name, name); > +} > + > static int > dev_uev_socket_fd_create(void) > { > @@ -135,16 +159,114 @@ dev_uev_receive(int fd, struct rte_dev_event > *uevent) > return 0; > } >=20 > +static int > +dev_uev_remove_handler(struct rte_device *dev) { > + struct rte_bus *bus =3D rte_bus_find_by_device_name(dev->name); > + int ret; > + > + if (!dev) > + return -1; > + > + if (bus->handle_hot_unplug) { > + /** > + * call bus ops to handle hot unplug. > + */ > + ret =3D bus->handle_hot_unplug(dev); > + if (ret) { > + RTE_LOG(ERR, EAL, > + "It cannot handle hot unplug for device (%s) " > + "on the bus.\n ", > + dev->name); > + return ret; > + } > + } > + return 0; > +} > + > static void > dev_uev_process(__rte_unused void *param) { > struct rte_dev_event uevent; > + struct rte_bus *bus; > + struct rte_device *dev; > + const char *busname; > + int ret =3D 0; >=20 > if (dev_uev_receive(intr_handle.fd, &uevent)) > return; >=20 > - if (uevent.devname) > + switch (uevent.subsystem) { > + case EAL_DEV_EVENT_SUBSYSTEM_PCI: > + case EAL_DEV_EVENT_SUBSYSTEM_UIO: > + busname =3D "pci"; > + break; > + default: > + break; > + } > + > + if (uevent.devname) { > + if (uevent.type =3D=3D RTE_DEV_EVENT_REMOVE) { > + bus =3D rte_bus_find_by_name(busname); > + if (bus =3D=3D NULL) { > + RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", > + uevent.devname); > + return; > + } > + dev =3D bus->find_device(NULL, cmp_dev_name, > + uevent.devname); > + if (dev =3D=3D NULL) { > + RTE_LOG(ERR, EAL, > + "Cannot find unplugged device (%s)\n", > + uevent.devname); > + return; > + } > + ret =3D dev_uev_remove_handler(dev); > + if (ret) { > + RTE_LOG(ERR, EAL, "Driver cannot remap the " > + "device (%s)\n", > + dev->name); > + return; > + } > + /* wake up all the threads */ > + pthread_cond_broadcast(&failure_recovery_cond); > + } > + > dev_callback_process(uevent.devname, uevent.type); > + } > +} > + > +int __rte_experimental > +rte_dev_handle_hot_unplug(void) > +{ > + struct sigaction act; > + sigset_t mask; > + > + /* set signal handlers */ > + memset(&act, 0x00, sizeof(struct sigaction)); > + act.sa_handler =3D sigbus_handler; > + sigemptyset(&act.sa_mask); > + act.sa_flags =3D SA_RESTART; > + sigaction(SIGBUS, &act, NULL); > + sigemptyset(&mask); > + sigaddset(&mask, SIGBUS); > + pthread_sigmask(SIG_UNBLOCK, &mask, NULL); > + > + if (sigsetjmp(RTE_PER_LCORE(unplug_longjmp_env), 1)) { > + /* > + * wait on condition variable > + * before failure recovery be finish. > + */ > + pthread_mutex_lock(&failure_recovery_lock); > + RTE_LOG(DEBUG, EAL, "begin waiting for the failure handler.\n"); > + pthread_cond_wait(&failure_recovery_cond, > + &failure_recovery_lock); > + RTE_LOG(DEBUG, EAL, > + "come back from waiting for failure handler.\n"); > + pthread_mutex_unlock(&failure_recovery_lock); > + } > + > + return 0; Should we return the value of sigsetjmp, so application can know "rewind" h= appened and can do some clean up? > } >=20 > int __rte_experimental > @@ -169,6 +291,12 @@ rte_dev_event_monitor_start(void) > return -1; > } >=20 > + /* initialize mutex and condition variable > + * to control failure recovery. > + */ > + pthread_mutex_init(&failure_recovery_lock, NULL); > + pthread_cond_init(&failure_recovery_cond, NULL); > + > monitor_started =3D true; >=20 > return 0; > @@ -192,5 +320,9 @@ rte_dev_event_monitor_stop(void) > close(intr_handle.fd); > intr_handle.fd =3D -1; > monitor_started =3D false; > + > + pthread_cond_destroy(&failure_recovery_cond); > + pthread_mutex_destroy(&failure_recovery_lock); > + > return 0; > } > diff --git a/lib/librte_eal/linuxapp/igb_uio/igb_uio.c > b/lib/librte_eal/linuxapp/igb_uio/igb_uio.c > index 4cae4dd..293c310 100644 > --- a/lib/librte_eal/linuxapp/igb_uio/igb_uio.c > +++ b/lib/librte_eal/linuxapp/igb_uio/igb_uio.c > @@ -344,6 +344,10 @@ igbuio_pci_release(struct uio_info *info, struct ino= de > *inode) > struct rte_uio_pci_dev *udev =3D info->priv; > struct pci_dev *dev =3D udev->pdev; >=20 > + /* check if device has been remove before release */ > + if ((&dev->dev.kobj)->state_remove_uevent_sent =3D=3D 1) > + return -1; > + > mutex_lock(&udev->lock); > if (--udev->refcnt > 0) { > mutex_unlock(&udev->lock); > diff --git a/lib/librte_eal/rte_eal_version.map > b/lib/librte_eal/rte_eal_version.map > index d23f491..d37dd29 100644 > --- a/lib/librte_eal/rte_eal_version.map > +++ b/lib/librte_eal/rte_eal_version.map > @@ -262,5 +262,6 @@ EXPERIMENTAL { >=20 > rte_dev_event_callback_register; > rte_dev_event_callback_unregister; > + rte_dev_handle_hot_unplug; >=20 > } DPDK_18.05; > -- > 2.7.4