Add hot plug event in rte eal interrupt and inplement it in i40e driver.
Subsystems (2)
Re: [PATCH V5 5/7] bus: add helper to handle sigbus
In reply to He, Shaopenghi, shaopeng
thanks for your review.
Quoted 16 lines · Shaopeng
Quoted 7 lines
-----Original Message-----
From: Guo, Jia
Sent: Thursday, July 5, 2018 3:39 PMThis patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
[...]
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 272
Quoted 6 lines
lib/librte_eal/common/eal_common_bus.c +6
-0+ bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* failed to handle the sigbus, pass the new errno. */ +if (bus && rte_errno ==-1 ) +return -1 ; +else if (!bus) + ret =1 ;Change the compare order, code will be a little bit shorter?
Quoted 6 lines
if (!bus) ret = 1 else if (rte_errno == -1) return -1;[...]
make sense.
Quoted 2 lines
Acked-by Shaopeng He <[email protected]>
Re: [PATCH V5 5/7] bus: add helper to handle sigbus
In reply to Jeff GuoQuoted 25 lines · Jeff Guo
This patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
Signed-off-by Jeff Guo <[email protected]>---
v5->v4:
refine the errno restore logic--- lib/librte_eal/common/eal_common_bus.c | 36 +++++++++++++++++++++++++++++++++- lib/librte_eal/common/eal_private.h | 12 ++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-)lib/librte_eal/common/eal_common_bus.c +1
-0@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
lib/librte_eal/common/eal_common_bus.c rte_bus_find_by_device_name(const char *str) · line 223
Quoted 4 lines
lib/librte_eal/common/eal_common_bus.c +0
-1@@ -220,7 +221,6 @@ rte_bus_find_by_device_name(const char *str)return rte_bus_find (NULL , bus_can_parse, name); } -
Unrelated change.
Quoted 5 lines
lib/librte_eal/common/eal_common_bus.c +0
-0/* * Get iommu class of devices on the bus. */ @@ -242,3 +242,37 @@ rte_bus_get_iommu_class(void) }
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 252
Quoted 10 lines
lib/librte_eal/common/eal_common_bus.c +8
-0return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + + ret = bus->sigbus_handler (failure_addr);
Shouldn't bus->sigbus_handler be checked here against NULL? It looks like not all buses implement it.
Quoted 36 lines
lib/librte_eal/common/eal_common_bus.c +26
-0+ rte_errno = ret; + +return !(bus->sigbus_handler && ret <=0 ); +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* failed to handle the sigbus, pass the new errno. */ +if (bus && rte_errno ==-1 ) +return -1 ; +else if (!bus) + ret =1 ; + +/* otherwise restore the old errno. */ + rte_errno = old_errno; + +return ret; +}lib/librte_eal/common/eal_private.h +2
-0@@ -258,4 +258,16 @@ int rte_mp_channel_init(void);*/ void dev_callback_process (char *device_name,enum rte_dev_event_type event); + +/**
lib/librte_eal/common/eal_private.h int rte_mp_channel_init(void); · line 272
Quoted 10 lines
lib/librte_eal/common/eal_private.h +10
-0+* Iterate all buses to find the corresponding bus, to handle the sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr);
Empty line is missing after the function.
Quoted 1 line
lib/librte_eal/common/eal_private.h +0
-0#endif /* _EAL_PRIVATE_H_ */
Re: [PATCH V5 5/7] bus: add helper to handle sigbus
hi, andrew
Thanks for your reviewing.
Quoted 14 lines · Andrew Rybchenko
Quoted 13 lines · Jeff Guo
This patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
Signed-off-by Jeff Guo <[email protected]>---
v5->v4:
refine the errno restore logic--- lib/librte_eal/common/eal_common_bus.c | 36 +++++++++++++++++++++++++++++++++- lib/librte_eal/common/eal_private.h | 12 ++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-)
Quoted 17 lines
Quoted 12 lines
lib/librte_eal/common/eal_common_bus.c +1
-0@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h" @@ -220,7 +221,6 @@ rte_bus_find_by_device_name(const char *str) return rte_bus_find(NULL, bus_can_parse, name);Quoted 2 lines
} -Unrelated change.
ok. I am fine to let it left to other specific patch.
Quoted 10 lines
Quoted 10 lines
/*
* Get iommu class of devices on the bus.
*/lib/librte_eal/common/eal_common_bus.c +3
-0@@ -242,3 +242,37 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus(const struct rte_bus *bus,
Quoted 9 lines
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 252
Quoted 5 lines
lib/librte_eal/common/eal_common_bus.c +5
-0+ const void *failure_addr) +{ +int ret; + + ret = bus->sigbus_handler (failure_addr);Shouldn't
bus->sigbus_handlerbe checked here against NULL? It looks like not all buses implement it.
should be like what you said.
Quoted 41 lines
Quoted 41 lines
lib/librte_eal/common/eal_common_bus.c +26
-0+ rte_errno = ret; + +return !(bus->sigbus_handler && ret <=0 ); +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* failed to handle the sigbus, pass the new errno. */ +if (bus && rte_errno ==-1 ) +return -1 ; +else if (!bus) + ret =1 ; + +/* otherwise restore the old errno. */ + rte_errno = old_errno; + +return ret; +}lib/librte_eal/common/eal_private.h +2
-0@@ -258,4 +258,16 @@ int rte_mp_channel_init(void);*/ void dev_callback_process (char *device_name,enum rte_dev_event_type event); + +/** +* Iterate all buses to find the corresponding bus, to handle the sigbus error.
+ * @paramfailure_addr
+ * Pointer of the fault address of the sigbus error.
+ *
Quoted 9 lines
Quoted 6 lines
+ * @return
+ * 0 success to handle the sigbus.
+ * -1 failed to handle the sigbus
+ * 1 no bus can handler the sigbus
+ */
+intrte_bus_sigbus_handler(const void *failure_addr);Empty line is missing after the function.
ok.
Quoted 2 lines
Quoted 1 line
#endif /* _EAL_PRIVATE_H_ */
PATCH v5 6/7 eal: add failure handle mechanism for hotplug
In reply to Jeff GuoThis patch introduces a failure handler mechanism to handle device hot plug removal event.
First register sigbus handler, once sigbus error be captured, will check the failure address and accordingly remap the invalid memory for the corresponding device. Bese on this mechanism, it could guaranty the application not to be crash when hotplug out device.
---v5->v4:
add sigbus old handler recover.
--- lib/librte_eal/linuxapp/eal/eal_dev.c | 111 +++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-)
lib/librte_eal/linuxapp/eal/eal_dev.c +110 -1
@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h>
@@ -14,15 +16,28 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h> +#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h" static struct rte_intr_handle intr_handle = {.fd =-1 };static bool monitor_started; +extern struct rte_bus_list rte_bus_list; +#define EAL_UEV_MSG_LEN 4096#define EAL_UEV_MSG_ELEM_LEN 128 +/* spinlock for device failure process */ +static rte_spinlock_t dev_failure_lock =RTE_SPINLOCK_INITIALIZER ; + +static struct sigaction sigbus_action_old; + +static int sigbus_need_recover; + static void dev_uev_handler(__rte_unused void *param);/* identify the system layer which reports this event. */
@@ -33,6 +48,49 @@ enum eal_dev_event_subsystem {EAL_DEV_EVENT_SUBSYSTEM_MAX }; +static void +sigbus_action_recover (void ) +{ +if (sigbus_need_recover) { +sigaction (SIGBUS , &sigbus_action_old,NULL ); + sigbus_need_recover =0 ; + } +} + +static void sigbus_handler (int signum,siginfo_t *info, + void *ctx __rte_unused) +{ +int ret; + +RTE_LOG (INFO ,EAL ,"Thread[%d] catch SIGBUS, fault address:%p\n" , + (int )pthread_self (), info->si_addr); + +rte_spinlock_lock (&dev_failure_lock); + ret =rte_bus_sigbus_handler (info->si_addr); +rte_spinlock_unlock (&dev_failure_lock); +if (ret ==-1 ) { +rte_exit (EXIT_FAILURE , +"Failed to handle SIGBUS for hotplug, " +"(rte_errno: %s)!" ,strerror (rte_errno)); + }else if (ret ==1 ) { +if (sigbus_action_old.sa_handler) + (*(sigbus_action_old.sa_handler))(signum); +else +rte_exit (EXIT_FAILURE , +"Failed to handle generic SIGBUS!" ); + } + +RTE_LOG (INFO ,EAL ,"Success to handle SIGBUS for hotplug!\n" ); +} + +static int cmp_dev_name (const struct rte_device *dev, +const void *_name) +{ +const char *name = _name; + +return strcmp (dev->name, name); +} +static int dev_uev_socket_fd_create (void ) {
@@ -147,6 +205,9 @@ dev_uev_handler(__rte_unused void *param)struct rte_dev_event uevent;int ret;char buf[EAL_UEV_MSG_LEN ]; +struct rte_bus *bus; +struct rte_device *dev; +const char *busname;memset (&uevent,0 ,sizeof (struct rte_dev_event ));memset (buf,0 ,EAL_UEV_MSG_LEN );
@@ -171,13 +232,50 @@ dev_uev_handler(__rte_unused void *param)RTE_LOG (DEBUG ,EAL ,"receive uevent(name:%s, type:%d, subsystem:%d)\n" , uevent.devname, uevent.type, uevent.subsystem); -if (uevent.devname) +switch (uevent.subsystem) { +case EAL_DEV_EVENT_SUBSYSTEM_PCI : +case EAL_DEV_EVENT_SUBSYSTEM_UIO : + busname ="pci" ; +break ; +default : +break ; + } + +if (uevent.devname) { +if (uevent.type ==RTE_DEV_EVENT_REMOVE ) { +rte_spinlock_lock (&dev_failure_lock); + bus =rte_bus_find_by_name (busname); +if (bus ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find bus (%s)\n" , + busname); +return ; + } + + dev = bus->find_device (NULL , cmp_dev_name, + uevent.devname); +if (dev ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find device (%s) on " +"bus (%s)\n" , uevent.devname, busname); +return ; + } + + ret = bus->hotplug_failure_handler (dev); +rte_spinlock_unlock (&dev_failure_lock); +if (ret) { +RTE_LOG (ERR ,EAL ,"Can not handle hotplug for " +"device (%s)\n" , dev->name); +return ; + } + }dev_callback_process (uevent.devname, uevent.type); + } } int __rte_experimentalrte_dev_event_monitor_start (void) { +sigset_t mask; +struct sigaction action;int ret;if (monitor_started)
@@ -197,6 +295,14 @@ rte_dev_event_monitor_start(void)return -1 ; } +/* register sigbus handler */ +sigemptyset (&mask); +sigaddset (&mask,SIGBUS ); + action.sa_flags =SA_SIGINFO ; + action.sa_mask = mask; + action.sa_sigaction = sigbus_handler; + sigbus_need_recover = !sigaction (SIGBUS , &action, &sigbus_action_old); + monitor_started = true;return 0 ;
@@ -217,8 +323,11 @@ rte_dev_event_monitor_stop(void)return ret; } +sigbus_action_recover (); +close (intr_handle.fd); intr_handle.fd =-1 ; monitor_started = false; +return 0 ; }
Signature · 1 line
2.7.4
Re: [PATCH V5 6/7] eal: add failure handle mechanism for hotplug
Quoted 11 lines
-----Original Message-----
From: Guo, Jia
Sent: Thursday, July 5, 2018 3:39 PMThis patch introduces a failure handler mechanism to handle device hot plug removal event.
First register sigbus handler, once sigbus error be captured, will check the failure address and accordingly remap the invalid memory for the corresponding device. Bese on this mechanism, it could
" Besed on this mechanism "?
Quoted 3 lines
guaranty the application not to be crash when hotplug out device.
Signed-off-by Jeff Guo <[email protected]>
Re: [PATCH V5 6/7] eal: add failure handle mechanism for hotplug
In reply to Jeff GuoQuoted 35 lines · Jeff Guo
This patch introduces a failure handler mechanism to handle device hot plug removal event.
First register sigbus handler, once sigbus error be captured, will check the failure address and accordingly remap the invalid memory for the corresponding device. Bese on this mechanism, it could guaranty the application not to be crash when hotplug out device.
Signed-off-by Jeff Guo <[email protected]>---
v5->v4:
add sigbus old handler recover.--- lib/librte_eal/linuxapp/eal/eal_dev.c | 111 +++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-)lib/librte_eal/linuxapp/eal/eal_dev.c +4
-0@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h> @@ -14,15 +16,28 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h>
lib/librte_eal/linuxapp/eal/eal_dev.c line 30
Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +4
-0+#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h" static struct rte_intr_handle intr_handle = {.fd =-1 };static bool monitor_started; +extern struct rte_bus_list rte_bus_list; +
Shouldn't rte_bus.h provide it?
lib/librte_eal/linuxapp/eal/eal_dev.c line 35
Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +2
-0#define EAL_UEV_MSG_LEN 4096#define EAL_UEV_MSG_ELEM_LEN 128 +/* spinlock for device failure process */ +static rte_spinlock_t dev_failure_lock =RTE_SPINLOCK_INITIALIZER ;
It would be useful to explain why the lock is needed and when it should be obtained/released. Which resources are protected by the lock?
Quoted 76 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +54
-1+ +static struct sigaction sigbus_action_old; + +static int sigbus_need_recover; + static void dev_uev_handler(__rte_unused void *param);/* identify the system layer which reports this event. */ @@ -33,6 +48,49 @@ enum eal_dev_event_subsystem {EAL_DEV_EVENT_SUBSYSTEM_MAX }; +static void +sigbus_action_recover (void ) +{ +if (sigbus_need_recover) { +sigaction (SIGBUS , &sigbus_action_old,NULL ); + sigbus_need_recover =0 ; + } +} + +static void sigbus_handler (int signum,siginfo_t *info, + void *ctx __rte_unused) +{ +int ret; + +RTE_LOG (INFO ,EAL ,"Thread[%d] catch SIGBUS, fault address:%p\n" , + (int )pthread_self (), info->si_addr); + +rte_spinlock_lock (&dev_failure_lock); + ret =rte_bus_sigbus_handler (info->si_addr); +rte_spinlock_unlock (&dev_failure_lock); +if (ret ==-1 ) { +rte_exit (EXIT_FAILURE , +"Failed to handle SIGBUS for hotplug, " +"(rte_errno: %s)!" ,strerror (rte_errno)); + }else if (ret ==1 ) { +if (sigbus_action_old.sa_handler) + (*(sigbus_action_old.sa_handler))(signum); +else +rte_exit (EXIT_FAILURE , +"Failed to handle generic SIGBUS!" ); + } + +RTE_LOG (INFO ,EAL ,"Success to handle SIGBUS for hotplug!\n" ); +} + +static int cmp_dev_name (const struct rte_device *dev, +const void *_name) +{ +const char *name = _name; + +return strcmp (dev->name, name); +} +static int dev_uev_socket_fd_create (void ) {@@ -147,6 +205,9 @@ dev_uev_handler(__rte_unused void *param)struct rte_dev_event uevent;int ret;char buf[EAL_UEV_MSG_LEN ]; +struct rte_bus *bus; +struct rte_device *dev; +const char *busname;memset (&uevent,0 ,sizeof (struct rte_dev_event ));memset (buf,0 ,EAL_UEV_MSG_LEN );@@ -171,13 +232,50 @@ dev_uev_handler(__rte_unused void *param)RTE_LOG (DEBUG ,EAL ,"receive uevent(name:%s, type:%d, subsystem:%d)\n" , uevent.devname, uevent.type, uevent.subsystem); -if (uevent.devname) +switch (uevent.subsystem) { +case EAL_DEV_EVENT_SUBSYSTEM_PCI : +case EAL_DEV_EVENT_SUBSYSTEM_UIO :
lib/librte_eal/linuxapp/eal/eal_dev.c dev_uev_handler(__rte_unused void *param) · line 247
Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +10
-0+ busname ="pci" ; +break ; +default : +break ; + } + +if (uevent.devname) { +if (uevent.type ==RTE_DEV_EVENT_REMOVE ) { +rte_spinlock_lock (&dev_failure_lock); + bus =rte_bus_find_by_name (busname);
It looks like busname could be uninitialized here.
Quoted 61 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +36
-0+if (bus ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find bus (%s)\n" , + busname); +return ; + } + + dev = bus->find_device (NULL , cmp_dev_name, + uevent.devname); +if (dev ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find device (%s) on " +"bus (%s)\n" , uevent.devname, busname); +return ; + } + + ret = bus->hotplug_failure_handler (dev); +rte_spinlock_unlock (&dev_failure_lock); +if (ret) { +RTE_LOG (ERR ,EAL ,"Can not handle hotplug for " +"device (%s)\n" , dev->name); +return ; + } + }dev_callback_process (uevent.devname, uevent.type); + } } int __rte_experimentalrte_dev_event_monitor_start (void) { +sigset_t mask; +struct sigaction action;int ret;if (monitor_started)@@ -197,6 +295,14 @@ rte_dev_event_monitor_start(void)return -1 ; } +/* register sigbus handler */ +sigemptyset (&mask); +sigaddset (&mask,SIGBUS ); + action.sa_flags =SA_SIGINFO ; + action.sa_mask = mask; + action.sa_sigaction = sigbus_handler; + sigbus_need_recover = !sigaction (SIGBUS , &action, &sigbus_action_old); + monitor_started = true;return 0 ;@@ -217,8 +323,11 @@ rte_dev_event_monitor_stop(void)return ret; } +sigbus_action_recover (); +close (intr_handle.fd); intr_handle.fd =-1 ; monitor_started = false; +return 0 ; }
Re: [PATCH V5 6/7] eal: add failure handle mechanism for hotplug
Quoted 36 lines · Andrew Rybchenko
Quoted 35 lines · Jeff Guo
This patch introduces a failure handler mechanism to handle device hot plug removal event.
First register sigbus handler, once sigbus error be captured, will check the failure address and accordingly remap the invalid memory for the corresponding device. Bese on this mechanism, it could guaranty the application not to be crash when hotplug out device.
Signed-off-by Jeff Guo <[email protected]>---
v5->v4:
add sigbus old handler recover.--- lib/librte_eal/linuxapp/eal/eal_dev.c | 111 +++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-)lib/librte_eal/linuxapp/eal/eal_dev.c +2
-0@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h> @@ -14,15 +16,28 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h>lib/librte_eal/linuxapp/eal/eal_dev.c +3
-0+#include <rte_bus.h> +#include <rte_eal.h> +#include <rte_spinlock.h>
Quoted 9 lines
lib/librte_eal/linuxapp/eal/eal_dev.c line 30
Quoted 6 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +2
-0+#include <rte_errno.h> #include "eal_private.h" static struct rte_intr_handle intr_handle = {.fd =-1 };static bool monitor_started; +extern struct rte_bus_list rte_bus_list; +Shouldn't
rte_bus.hprovide it?
I think rte_bus.h provide the rte_bus_list structure, and then announcement a variable in eal_common_bus.c, then i use it by extern in eal_dev.c.
Quoted 9 lines
Quoted 4 lines
#define
EAL_UEV_MSG_LEN4096
#defineEAL_UEV_MSG_ELEM_LEN128
+/* spinlock for device failure process */
+staticrte_spinlock_tdev_failure_lock=RTE_SPINLOCK_INITIALIZER;It would be useful to explain why the lock is needed and when it should be obtained/released. Which resources are protected by the lock?
make sense, this locker should be use both bus and device access protection. Will explicit to let it to be more readable.
Quoted 77 lines
Quoted 77 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +57
-0+ +static struct sigaction sigbus_action_old; + +static int sigbus_need_recover; + static void dev_uev_handler(__rte_unused void *param);/* identify the system layer which reports this event. */ @@ -33,6 +48,49 @@ enum eal_dev_event_subsystem {EAL_DEV_EVENT_SUBSYSTEM_MAX }; +static void +sigbus_action_recover (void ) +{ +if (sigbus_need_recover) { +sigaction (SIGBUS , &sigbus_action_old,NULL ); + sigbus_need_recover =0 ; + } +} + +static void sigbus_handler (int signum,siginfo_t *info, + void *ctx __rte_unused) +{ +int ret; + +RTE_LOG (INFO ,EAL ,"Thread[%d] catch SIGBUS, fault address:%p\n" , + (int )pthread_self (), info->si_addr); + +rte_spinlock_lock (&dev_failure_lock); + ret =rte_bus_sigbus_handler (info->si_addr); +rte_spinlock_unlock (&dev_failure_lock); +if (ret ==-1 ) { +rte_exit (EXIT_FAILURE , +"Failed to handle SIGBUS for hotplug, " +"(rte_errno: %s)!" ,strerror (rte_errno)); + }else if (ret ==1 ) { +if (sigbus_action_old.sa_handler) + (*(sigbus_action_old.sa_handler))(signum); +else +rte_exit (EXIT_FAILURE , +"Failed to handle generic SIGBUS!" ); + } + +RTE_LOG (INFO ,EAL ,"Success to handle SIGBUS for hotplug!\n" ); +} + +static int cmp_dev_name (const struct rte_device *dev, +const void *_name) +{ +const char *name = _name; + +return strcmp (dev->name, name); +} +static int dev_uev_socket_fd_create (void ) {@@ -147,6 +205,9 @@ dev_uev_handler(__rte_unused void *param)struct rte_dev_event uevent;int ret;char buf[EAL_UEV_MSG_LEN ]; +struct rte_bus *bus; +struct rte_device *dev; +const char *busname;memset (&uevent,0 ,sizeof (struct rte_dev_event ));memset (buf,0 ,EAL_UEV_MSG_LEN );@@ -171,13 +232,50 @@ dev_uev_handler(__rte_unused void *param)RTE_LOG (DEBUG ,EAL ,"receive uevent(name:%s, type:%d, subsystem:%d)\n", uevent.devname, uevent.type, uevent.subsystem); - if (uevent.devname) +switch (uevent.subsystem) { +case EAL_DEV_EVENT_SUBSYSTEM_PCI : +case EAL_DEV_EVENT_SUBSYSTEM_UIO : + busname ="pci" ; +break ; +default : +break ;
Quoted 9 lines
lib/librte_eal/linuxapp/eal/eal_dev.c dev_uev_handler(__rte_unused void *param) · line 247
Quoted 6 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +6
-0+ } + +if (uevent.devname) { +if (uevent.type ==RTE_DEV_EVENT_REMOVE ) { +rte_spinlock_lock (&dev_failure_lock); + bus =rte_bus_find_by_name (busname);It looks like busname could be uninitialized here.
you are correct i think.
Quoted 58 lines
Quoted 57 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +34
-0+if (bus ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find bus (%s)\n" , + busname); +return ; + } + + dev = bus->find_device (NULL , cmp_dev_name, + uevent.devname); +if (dev ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find device (%s) on " +"bus (%s)\n" , uevent.devname, busname); +return ; + } + + ret = bus->hotplug_failure_handler (dev); +rte_spinlock_unlock (&dev_failure_lock); +if (ret) { +RTE_LOG (ERR ,EAL ,"Can not handle hotplug for " +"device (%s)\n" , dev->name); +return ; + } + }dev_callback_process (uevent.devname, uevent.type); + } } int __rte_experimentalrte_dev_event_monitor_start (void) { +sigset_t mask; +struct sigaction action;int ret;if (monitor_started)@@ -197,6 +295,14 @@ rte_dev_event_monitor_start(void)return -1 ; } +/* register sigbus handler */ +sigemptyset (&mask); +sigaddset (&mask,SIGBUS ); + action.sa_flags =SA_SIGINFO ; + action.sa_mask = mask; + action.sa_sigaction = sigbus_handler; + sigbus_need_recover = !sigaction (SIGBUS , &action, &sigbus_action_old); + monitor_started = true;return 0 ;@@ -217,8 +323,11 @@ rte_dev_event_monitor_stop(void)return ret; } +sigbus_action_recover (); +close (intr_handle.fd); intr_handle.fd =-1 ; monitor_started = false; +return 0 ; }
v5 cover hot plug failure handle mechanism
In reply to Jeff GuoAs we know, hot plug is an importance feature, either use for the datacenter device’s fail-safe, or use for SRIOV Live Migration in SDN/NFV. It could bring the higher flexibility and continuality to the networking services in multiple use cases in industry. So let we see, dpdk as an importance networking framework, what can it help to implement hot plug solution for users.
We already have a general device event detect mechanism, failsafe driver, bonding driver and hot plug/unplug api in framework, app could use these to develop their hot plug solution.
let’s see the case of hot unplug, it can happen when a hardware device is be removed physically, or when the software disables it. App need to call ether dev API to detach the device, to unplug the device at the bus level and make access to the device invalid. But the problem is that, the removal of the device from the software lists is not going to be instantaneous, at this time if the data(fast) path still read/write the device, it will cause MMIO error and result of the app crash out.
Seems that we have got fail-safe driver(or app) + RTE_ETH_EVENT_INTR_RMV + kernel core driver solution to handle it, but still not have failsafe driver (or app) + RTE_DEV_EVENT_REMOVE + PCIe pmd driver failure handle solution. So there is an absence in dpdk hot plug solution right now.
Also, we know that kernel only guaranty hot plug on the kernel side, but not for the user mode side. Firstly we can hardly have a gatekeeper for any MMIO for multiple PMD driver. Secondly, no more specific 3rd tools such as udev/driverctl have especially cover these hot plug failure processing. Third, the feasibility of app’s implement for multiple user mode PMD driver is still a problem. Here, a general hot plug failure handle mechanism in dpdk framework would be proposed, it aim to guaranty that, when hot unplug occur, the system will not crash and app will not be break out, and user space can normally stop and release any relevant resources, then unplug of the device at the bus level cleanly.
The mechanism should be come across as bellow:
Firstly, app enabled the device event monitor and register the hot plug event’s callback before running data path. Once the hot unplug behave occur, the mechanism will detect the removal event and then accordingly do the failure handle. In order to do that, below functional will be bring in.
- Add a new bus ops “handle_hot_unplug” to handle bus read/write error, it is bus-specific and each kind of bus can implement its own logic.
- Implement pci bus specific ops “pci_handle_hot_unplug”. It will base on the failure address to remap memory for the corresponding device that unplugged.
For the data path or other unexpected control from the control path when hot unplug occur.
- Implement a new sigbus handler, it is registered when start device even monitoring. The handler is per process. Base on the signal event principle, control path thread and data path thread will randomly receive the sigbus error, but will go to the common sigbus handler. Once the MMIO sigbus error exposure, it will trigger the above hot unplug operation. The sigbus will be check if it is cause of the hot unplug or not, if not will info exception as the original sigbus handler. If yes, will do memory remapping.
For the control path and the igb uio release:
- When hot unplug device, the kernel will release the device resource in the kernel side, such as the fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash.
On the other hand, something like interrupt disable do not automatically process in kernel side. If not handler it, this redundancy and dirty thing will affect the interrupt resource be used by other device.
So the igb_uio driver have to check the hot plug status and corresponding process should be taken in igb uio deriver.
This patch propose to add structure of rte_udev_state into rte_uio_pci_dev of igb_uio kernel driver, which will record the state of uio device, such as probed/opened/released/removed/unplug. When detect the unexpected removal which cause of hot unplug behavior, it will corresponding disable interrupt resource, while for the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer kernel crash issue.
The mechanism could be use for fail-safe driver and app which want to use hot plug solution. let testpmd for example:
- Enable device event monitor->device unplug->failure handle->stop forwarding-> stop port->close port->detach port.
This process will not breaking the app/fail-safe running, and will not break other irrelevance device. And app could plug in the device and restart the date path again by below.
- Device plug in->bind igb_uio driver ->attached device->start port-> start forwarding.
patchset history:v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name
refine the sigbus handle logic
add lock for udev state in igb uio driver
v4->v3:
split patches to be small and clear
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug
v3->v2:
change bus ops name to bus_hotplug_handler. add new API and bus ops of bus_signal_handler distingush handle generic sigbus and hotplug sigbus
v2->v1(v21):
refine some doc and commit log
fix igb uio kernel issue for control path failure
rebase testpmd code
Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:v21->v20:
split function in hot unplug ops
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code
refine the remap logic for multiple device.
remove the auto binding
v19->18:
note for limitation of multiple hotplug,fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.
the prior patch history please check the patch set "add device event monitor framework"
Jeff Guo (7):
bus: add hotplug failure handler
bus/pci: implement hotplug failure handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler operation
bus: add helper to handle sigbus
eal: add failure handle mechanism for hotplug
igb_uio: fix uio release issue when hot unplug
drivers/bus/pci/pci_common.c | 77 ++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 ++++++++++ drivers/bus/pci/private.h | 12 ++++ kernel/linux/igb_uio/igb_uio.c | 51 ++++++++++++++- lib/librte_eal/common/eal_common_bus.c | 36 ++++++++++- lib/librte_eal/common/eal_private.h | 12 ++++ lib/librte_eal/common/include/rte_bus.h | 31 +++++++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 111 +++++++++++++++++++++++++++++++- 8 files changed, 358 insertions(+), 5 deletions(-)
Signature · 1 line
2.7.4
PATCH v5 7/7 igb_uio: fix uio release issue when hot unplug
When hotplug out device, the kernel will release the device resource in the kernel side, such as the fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash. On the other hand, something like interrupt disabling do not automatically process in kernel side. If not handler it, this redundancy and dirty thing will affect the interrupt resource be used by other device. So the igb_uio driver have to check the hotplug status, and the corresponding process should be taken in igb uio driver.
This patch propose to add structure of rte_udev_state into rte_uio_pci_dev of igb_uio kernel driver, which will record the state of uio device, such as probed/opened/released/removed/unplug. When detect the unexpected removal which cause of hotplug out behavior, it will corresponding disable interrupt resource, while for the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer kernel crash issue.
---v5->v4:
add lock for udev state
--- kernel/linux/igb_uio/igb_uio.c | 51 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-)
kernel/linux/igb_uio/igb_uio.c +48 -3
@@ -19,6 +19,15 @@#include "compat.h" +/* uio pci device state */ +enum rte_udev_state { +RTE_UDEV_PROBED , +RTE_UDEV_OPENNED , +RTE_UDEV_RELEASED , +RTE_UDEV_REMOVED , +RTE_UDEV_UNPLUG +}; +/** * A structure describing the private information for a uio device. */
@@ -28,6 +37,7 @@ struct rte_uio_pci_dev {enum rte_intr_mode mode;struct mutex lock;int refcnt; +enum rte_udev_state state; };static int wc_activate;
@@ -195,12 +205,22 @@ igbuio_pci_irqhandler(int irq, void *dev_id) {struct rte_uio_pci_dev *udev = (struct rte_uio_pci_dev *)dev_id;struct uio_info *info = &udev->info; +struct pci_dev *pdev = udev->pdev;/* Legacy mode need to mask in hardware */ if (udev->mode ==RTE_INTR_MODE_LEGACY && !pci_check_and_mask_intx (udev->pdev))return IRQ_NONE ; +mutex_lock (&udev->lock); +/* check the uevent of the kobj */ +if ((&pdev->dev.kobj)->state_remove_uevent_sent ==1 ) { +dev_notice (&pdev->dev,"device:%s, sent remove uevent!\n" , + (&pdev->dev.kobj)->name); + udev->state =RTE_UDEV_UNPLUG ; + } +mutex_unlock (&udev->lock); +uio_event_notify (info);/* Message signal mode, no share IRQ and automasked */
@@ -309,7 +329,6 @@ igbuio_pci_disable_interrupts(struct rte_uio_pci_dev *udev)#endif } -/** * This gets called while opening uio device file. */
@@ -331,20 +350,29 @@ igbuio_pci_open(struct uio_info *info, struct inode *inode)/* enable interrupts */ err =igbuio_pci_enable_interrupts (udev); -mutex_unlock (&udev->lock);if (err) {dev_err (&dev->dev,"Enable interrupt fails\n" ); +pci_clear_master (dev); +mutex_unlock (&udev->lock);return err; } + udev->state =RTE_UDEV_OPENNED ; +mutex_unlock (&udev->lock);return 0 ; } +/** +* This gets called while closing uio device file. +*/ static int igbuio_pci_release (struct uio_info *info,struct inode *inode) {struct rte_uio_pci_dev *udev = info->priv;struct pci_dev *dev = udev->pdev; +if (udev->state ==RTE_UDEV_REMOVED ) +return 0 ; +mutex_lock (&udev->lock);if (--udev->refcnt >0 ) {mutex_unlock (&udev->lock);
@@ -356,7 +384,7 @@ igbuio_pci_release(struct uio_info *info, struct inode *inode)/* stop the device from further DMA */ pci_clear_master (dev); - + udev->state =RTE_UDEV_RELEASED ;mutex_unlock (&udev->lock);return 0 ; }
@@ -562,6 +590,9 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) (unsigned long long)map_dma_addr, map_addr); } +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_PROBED ; +mutex_unlock (&udev->lock);return 0 ; fail_remove_group:
@@ -579,6 +610,20 @@ static void igbuio_pci_remove(struct pci_dev *dev) {struct rte_uio_pci_dev *udev =pci_get_drvdata (dev); +int ret; + +/* handler hot unplug */ +if (udev->state ==RTE_UDEV_OPENNED || + udev->state ==RTE_UDEV_UNPLUG ) { +dev_notice (&dev->dev,"Unexpected removal!\n" ); + ret =igbuio_pci_release (&udev->info,NULL ); +if (ret) +return ; +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_REMOVED ; +mutex_unlock (&udev->lock); +return ; + }mutex_destroy (&udev->lock);sysfs_remove_group (&dev->dev.kobj, &dev_attr_grp);
Signature · 1 line
2.7.4
v6 cover hotplug failure handle mechanism
In reply to Jeff GuoAs we know, hot plug is an importance feature, either use for the datacenter device’s fail-safe, or use for SRIOV Live Migration in SDN/NFV. It could bring the higher flexibility and continuality to the networking services in multiple use cases in industry. So let we see, dpdk as an importance networking framework, what can it help to implement hot plug solution for users.
We already have a general device event detect mechanism, failsafe driver, bonding driver and hot plug/unplug api in framework, app could use these to develop their hot plug solution.
let’s see the case of hot unplug, it can happen when a hardware device is be removed physically, or when the software disables it. App need to call ether dev API to detach the device, to unplug the device at the bus level and make access to the device invalid. But the problem is that, the removal of the device from the software lists is not going to be instantaneous, at this time if the data(fast) path still read/write the device, it will cause MMIO error and result of the app crash out.
Seems that we have got fail-safe driver(or app) + RTE_ETH_EVENT_INTR_RMV + kernel core driver solution to handle it, but still not have failsafe driver (or app) + RTE_DEV_EVENT_REMOVE + PCIe pmd driver failure handle solution. So there is an absence in dpdk hot plug solution right now.
Also, we know that kernel only guaranty hot plug on the kernel side, but not for the user mode side. Firstly we can hardly have a gatekeeper for any MMIO for multiple PMD driver. Secondly, no more specific 3rd tools such as udev/driverctl have especially cover these hot plug failure processing. Third, the feasibility of app’s implement for multiple user mode PMD driver is still a problem. Here, a general hot plug failure handle mechanism in dpdk framework would be proposed, it aim to guaranty that, when hot unplug occur, the system will not crash and app will not be break out, and user space can normally stop and release any relevant resources, then unplug of the device at the bus level cleanly.
The mechanism should be come across as bellow:
Firstly, app enabled the device event monitor and register the hot plug event’s callback before running data path. Once the hot unplug behave occur, the mechanism will detect the removal event and then accordingly do the failure handle. In order to do that, below functional will be bring in.
- Add a new bus ops “handle_hot_unplug” to handle bus read/write error, it is bus-specific and each kind of bus can implement its own logic.
- Implement pci bus specific ops “pci_handle_hot_unplug”. It will base on the failure address to remap memory for the corresponding device that unplugged.
For the data path or other unexpected control from the control path when hot unplug occur.
- Implement a new sigbus handler, it is registered when start device even monitoring. The handler is per process. Base on the signal event principle, control path thread and data path thread will randomly receive the sigbus error, but will go to the common sigbus handler. Once the MMIO sigbus error exposure, it will trigger the above hot unplug operation. The sigbus will be check if it is cause of the hot unplug or not, if not will info exception as the original sigbus handler. If yes, will do memory remapping.
For the control path and the igb uio release:
- When hot unplug device, the kernel will release the device resource in the kernel side, such as the fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash.
On the other hand, something like interrupt disable do not automatically process in kernel side. If not handler it, this redundancy and dirty thing will affect the interrupt resource be used by other device.
So the igb_uio driver have to check the hot plug status and corresponding process should be taken in igb uio deriver.
This patch propose to add structure of rte_udev_state into rte_uio_pci_dev of igb_uio kernel driver, which will record the state of uio device, such as probed/opened/released/removed/unplug. When detect the unexpected removal which cause of hot unplug behavior, it will corresponding disable interrupt resource, while for the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer kernel crash issue.
The mechanism could be use for fail-safe driver and app which want to use hot plug solution. let testpmd for example:
- Enable device event monitor->device unplug->failure handle->stop forwarding-> stop port->close port->detach port.
This process will not breaking the app/fail-safe running, and will not break other irrelevance device. And app could plug in the device and restart the date path again by below.
- Device plug in->bind igb_uio driver ->attached device->start port-> start forwarding.
patchset history:v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name
refine the sigbus handle logic
add lock for udev state in igb uio driver
v4->v3:
split patches to be small and clear
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug
v3->v2:
change bus ops name to bus_hotplug_handler. add new API and bus ops of bus_signal_handler distingush handle generic sigbus and hotplug sigbus
v2->v1(v21):
refine some doc and commit log
fix igb uio kernel issue for control path failure
rebase testpmd code
Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:v21->v20:
split function in hot unplug ops
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code
refine the remap logic for multiple device.
remove the auto binding
v19->18:
note for limitation of multiple hotplug,fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.
the prior patch history please check the patch set "add device event monitor framework"
Jeff Guo (7):
bus: add hotplug failure handler
bus/pci: implement hotplug failure handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler operation
bus: add helper to handle sigbus
eal: add failure handle mechanism for hotplug
igb_uio: fix uio release issue when hot unplug
drivers/bus/pci/pci_common.c | 77 +++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++++ drivers/bus/pci/private.h | 12 ++++ kernel/linux/igb_uio/igb_uio.c | 51 +++++++++++++- lib/librte_eal/common/eal_common_bus.c | 42 ++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++ lib/librte_eal/common/include/rte_bus.h | 33 +++++++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 114 +++++++++++++++++++++++++++++++- 8 files changed, 370 insertions(+), 4 deletions(-)
Signature · 1 line
2.7.4
PATCH v6 1/7 bus: add hotplug failure handler
When device be hotplug out, if app still continue to access device by mmio, it will cause of memory failure and result the system crash.
This patch introduces a bus ops to handle device hotplug failure, it is a bus specific behavior, so each kind of bus can implement its own logic case by case.
---v6->v5:
refine some description of bus ops
--- lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);typedef int (*rte_bus_parse_t )(const char *name,void *addr);/** +* Implementation a specific hotplug failure handler, which is responsible +* for handle the failure when the device be hotplug out from the bus. When +* hotplug removal event be detected, it could call this function to handle +* failure and guaranty the system would not crash in the case. +* @param dev +* Pointer of the device structure. +* +* @return +* 0 on success. +* !0 on error. +*/ +typedef int (*rte_bus_hotplug_failure_handler_t )(struct rte_device *dev); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -211,6 +225,8 @@ struct rte_bus {rte_bus_parse_t parse;/**< Parse a device name */ struct rte_bus_conf conf;/**< Bus configuration */ rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ +rte_bus_hotplug_failure_handler_t hotplug_failure_handler; +/**< handle hotplug failure on bus */ };/**
Signature · 1 line
2.7.4
PATCH v6 2/7 bus/pci: implement hotplug failure handler ops
In reply to Jeff GuoThis patch implements the ops of hotplug failure handler for PCI bus, it is functional to remap a new dummy memory which overlap to the failure memory to avoid MMIO read/write error.
---v6->v5:
refine some typo
--- drivers/bus/pci/pci_common.c | 28 ++++++++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++ drivers/bus/pci/private.h | 12 ++++++++++++ 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
@@ -408,6 +408,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, }static int +pci_hotplug_failure_handler (struct rte_device *dev) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =RTE_DEV_TO_PCI (dev); +if (!pdev) +return -1 ; + +switch (pdev->kdrv) { +case RTE_KDRV_IGB_UIO : +case RTE_KDRV_UIO_GENERIC : +case RTE_KDRV_NIC_UIO : +/* mmio resource is invalid, remap it to be safe. */ + ret =pci_uio_remap_resource (pdev); +break ; +default : +RTE_LOG (DEBUG ,EAL , +"Not managed by a supported kernel driver, skipped\n" ); + ret =-1 ; +break ; + } + +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -437,6 +464,7 @@ struct rte_pci_bus rte_pci_bus = { .unplug = pci_unplug, .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, + .hotplug_failure_handler = pci_hotplug_failure_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res) } } +/* remap the PCI resource of a PCI device in anonymous virtual memory */ +int +pci_uio_remap_resource (struct rte_pci_device *dev) +{ +int i; +void *map_address; + +if (dev ==NULL ) +return -1 ; + +/* Remap all BARs */ +for (i =0 ; i !=PCI_MAX_RESOURCE ; i++) { +/* skip empty BAR */ +if (dev->mem_resource[i].phys_addr ==0 ) +continue ; + map_address =mmap (dev->mem_resource[i].addr, + (size_t )dev->mem_resource[i].len, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ); +if (map_address ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL , +"Cannot remap resource for device %s\n" , + dev->name); +return -1 ; + } +RTE_LOG (INFO ,EAL , +"Successful remap resource for device %s\n" , + dev->name); + } + +return 0 ; +} +static struct mapped_pci_resource *pci_uio_find_resource (struct rte_pci_device *dev) {
drivers/bus/pci/private.h +12 -0
@@ -123,6 +123,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev, struct mapped_pci_resource *uio_res);/** +* Remap the PCI resource of a PCI device in anonymous virtual memory. +* +* @param dev +* Point to the struct rte pci device. +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +pci_uio_remap_resource (struct rte_pci_device *dev); + +/** * Map device memory to uio resource * * This function is private to EAL.
Signature · 1 line
2.7.4
PATCH v6 3/7 bus: add sigbus handler
In reply to Jeff GuoWhen device be hotplug out, if data path still read/write device, the sigbus error will occur, this error need to be handled. So a handler need to be here to capture the signal and handle it correspondingly.
This patch introduces a bus ops to handle sigbus error, it is a bus specific behavior, so that each kind of bus can implement its own logic case by case.
---v6->v5:
refine some description of bus ops
--- lib/librte_eal/common/include/rte_bus.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
lib/librte_eal/common/include/rte_bus.h +17 -0
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);typedef int (*rte_bus_hotplug_failure_handler_t )(struct rte_device *dev);/** +* Implementation a specific sigbus handler, which is responsible for handle +* the sigbus error which is either original memory error, or specific memory +* error that caused of hot unplug. When sigbus error be captured, it could +* call this function to handle sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 for success handle the sigbus. +* 1 for no bus handle the sigbus. +* -1 for failed to handle the sigbus +*/ +typedef int (*rte_bus_sigbus_handler_t )(const void *failure_addr); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -227,6 +242,8 @@ struct rte_bus {rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ rte_bus_hotplug_failure_handler_t hotplug_failure_handler;/**< handle hotplug failure on bus */ +rte_bus_sigbus_handler_t sigbus_handler;/**< handle sigbus error */ + };/**
Signature · 1 line
2.7.4
PATCH v6 4/7 bus/pci: implement sigbus handler operation
In reply to Jeff GuoThis patch implements the ops of sigbus handler for PCI bus, it is functional to find the corresponding pci device which is been hotplug out, and then call the bus ops of hotplug failure handler to handle the failure for the device.
---v6->v5:
refine some typo
--- drivers/bus/pci/pci_common.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
drivers/bus/pci/pci_common.c +49 -0
@@ -407,6 +407,32 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,return NULL ; } +/* check the failure address belongs to which device. */ +static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int i; + +FOREACH_DEVICE_ON_PCIBUS (pdev) { +for (i =0 ; i !=RTE_DIM (pdev->mem_resource); i++) { +if ((uint64_t )(uintptr_t )failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t )(uintptr_t )failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) { +RTE_LOG (INFO ,EAL ,"Failure address " +"%16.16" PRIx64" belongs to " +"device %s!\n" , + (uint64_t )(uintptr_t )failure_addr, + pdev->device.name); +return pdev; + } + } + } +return NULL ; +} +static int pci_hotplug_failure_handler (struct rte_device *dev) {
@@ -435,6 +461,28 @@ pci_hotplug_failure_handler(struct rte_device *dev) }static int +pci_sigbus_handler (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =pci_find_device_by_addr (failure_addr); +if (!pdev) { +/* It is a generic sigbus error, no bus would handle it. */ + ret =1 ; + }else { +/* The sigbus error is caused of hot removal. */ + ret =pci_hotplug_failure_handler (&pdev->device); +if (ret) { +RTE_LOG (ERR ,EAL ,"Failed to handle hot plug for " +"device %s" , pdev->name); + ret =-1 ; + } + } +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -465,6 +513,7 @@ struct rte_pci_bus rte_pci_bus = { .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, .hotplug_failure_handler = pci_hotplug_failure_handler, + .sigbus_handler = pci_sigbus_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
PATCH v6 5/7 bus: add helper to handle sigbus
In reply to Jeff GuoThis patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
---v6->v5:
refine some coding style.
--- lib/librte_eal/common/eal_common_bus.c | 42 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++++++++ 2 files changed, 54 insertions(+)
lib/librte_eal/common/eal_common_bus.c +42 -0
@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -242,3 +243,44 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) { +RTE_LOG (ERR ,EAL ,"Function sigbus_handler not supported by " +"bus (%s)\n" , bus->name); +return -1 ; + } + + ret = bus->sigbus_handler (failure_addr); + rte_errno = ret; + +return !(bus->sigbus_handler && ret <=0 ); +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* failed to handle the sigbus, pass the new errno. */ +if (!bus) + ret =1 ; +else if (rte_errno ==-1 ) +return -1 ; + +/* otherwise restore the old errno. */ + rte_errno = old_errno; + +return ret; +}
lib/librte_eal/common/eal_private.h +12 -0
@@ -258,4 +258,16 @@ int rte_mp_channel_init(void);*/ void dev_callback_process (char *device_name,enum rte_dev_event_type event); +/** +* Iterate all buses to find the corresponding bus, to handle the sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
PATCH v6 6/7 eal: add failure handle mechanism for hotplug
In reply to Jeff GuoThis patch introduces a failure handle mechanism to handle device hotplug removal event.
First it can register sigbus handler when enable device event monitor. Once sigbus error be captured, it will check the failure address and accordingly remap the invalid memory for the corresponding device. Besed on this mechanism, it could guaranty the application not crash when the device be hotplug out.
---v6->v5:
refine some doc and coding style
--- lib/librte_eal/linuxapp/eal/eal_dev.c | 114 +++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-)
lib/librte_eal/linuxapp/eal/eal_dev.c +113 -1
@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h>
@@ -14,15 +16,31 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h> +#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h" static struct rte_intr_handle intr_handle = {.fd =-1 };static bool monitor_started; +extern struct rte_bus_list rte_bus_list; +#define EAL_UEV_MSG_LEN 4096#define EAL_UEV_MSG_ELEM_LEN 128 +/* +* spinlock for device failure process, protect the bus and the device +* to avoid race condition. +*/ +static rte_spinlock_t dev_failure_lock =RTE_SPINLOCK_INITIALIZER ; + +static struct sigaction sigbus_action_old; + +static int sigbus_need_recover; + static void dev_uev_handler(__rte_unused void *param);/* identify the system layer which reports this event. */
@@ -33,6 +51,49 @@ enum eal_dev_event_subsystem {EAL_DEV_EVENT_SUBSYSTEM_MAX }; +static void +sigbus_action_recover (void ) +{ +if (sigbus_need_recover) { +sigaction (SIGBUS , &sigbus_action_old,NULL ); + sigbus_need_recover =0 ; + } +} + +static void sigbus_handler (int signum,siginfo_t *info, + void *ctx __rte_unused) +{ +int ret; + +RTE_LOG (INFO ,EAL ,"Thread[%d] catch SIGBUS, fault address:%p\n" , + (int )pthread_self (), info->si_addr); + +rte_spinlock_lock (&dev_failure_lock); + ret =rte_bus_sigbus_handler (info->si_addr); +rte_spinlock_unlock (&dev_failure_lock); +if (ret ==-1 ) { +rte_exit (EXIT_FAILURE , +"Failed to handle SIGBUS for hotplug, " +"(rte_errno: %s)!" ,strerror (rte_errno)); + }else if (ret ==1 ) { +if (sigbus_action_old.sa_handler) + (*(sigbus_action_old.sa_handler))(signum); +else +rte_exit (EXIT_FAILURE , +"Failed to handle generic SIGBUS!" ); + } + +RTE_LOG (INFO ,EAL ,"Success to handle SIGBUS for hotplug!\n" ); +} + +static int cmp_dev_name (const struct rte_device *dev, +const void *_name) +{ +const char *name = _name; + +return strcmp (dev->name, name); +} +static int dev_uev_socket_fd_create (void ) {
@@ -147,6 +208,9 @@ dev_uev_handler(__rte_unused void *param)struct rte_dev_event uevent;int ret;char buf[EAL_UEV_MSG_LEN ]; +struct rte_bus *bus; +struct rte_device *dev; +const char *busname ="" ;memset (&uevent,0 ,sizeof (struct rte_dev_event ));memset (buf,0 ,EAL_UEV_MSG_LEN );
@@ -171,13 +235,50 @@ dev_uev_handler(__rte_unused void *param)RTE_LOG (DEBUG ,EAL ,"receive uevent(name:%s, type:%d, subsystem:%d)\n" , uevent.devname, uevent.type, uevent.subsystem); -if (uevent.devname) +switch (uevent.subsystem) { +case EAL_DEV_EVENT_SUBSYSTEM_PCI : +case EAL_DEV_EVENT_SUBSYSTEM_UIO : + busname ="pci" ; +break ; +default : +break ; + } + +if (uevent.devname) { +if (uevent.type ==RTE_DEV_EVENT_REMOVE ) { +rte_spinlock_lock (&dev_failure_lock); + bus =rte_bus_find_by_name (busname); +if (bus ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find bus (%s)\n" , + busname); +return ; + } + + dev = bus->find_device (NULL , cmp_dev_name, + uevent.devname); +if (dev ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find device (%s) on " +"bus (%s)\n" , uevent.devname, busname); +return ; + } + + ret = bus->hotplug_failure_handler (dev); +rte_spinlock_unlock (&dev_failure_lock); +if (ret) { +RTE_LOG (ERR ,EAL ,"Can not handle hotplug for " +"device (%s)\n" , dev->name); +return ; + } + }dev_callback_process (uevent.devname, uevent.type); + } } int __rte_experimentalrte_dev_event_monitor_start (void) { +sigset_t mask; +struct sigaction action;int ret;if (monitor_started)
@@ -197,6 +298,14 @@ rte_dev_event_monitor_start(void)return -1 ; } +/* register sigbus handler */ +sigemptyset (&mask); +sigaddset (&mask,SIGBUS ); + action.sa_flags =SA_SIGINFO ; + action.sa_mask = mask; + action.sa_sigaction = sigbus_handler; + sigbus_need_recover = !sigaction (SIGBUS , &action, &sigbus_action_old); + monitor_started = true;return 0 ;
@@ -217,8 +326,11 @@ rte_dev_event_monitor_stop(void)return ret; } +sigbus_action_recover (); +close (intr_handle.fd); intr_handle.fd =-1 ; monitor_started = false; +return 0 ; }
Signature · 1 line
2.7.4
Re: [PATCH v6 6/7] eal: add failure handle mechanism for hotplug
Hi Jeff,
Quoted 37 lines · Jeff Guo
This patch introduces a failure handle mechanism to handle device hotplug removal event.
First it can register sigbus handler when enable device event monitor. Once sigbus error be captured, it will check the failure address and accordingly remap the invalid memory for the corresponding device. Besed on this mechanism, it could guaranty the application not crash when the device be hotplug out.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v6->v5:
refine some doc and coding style--- lib/librte_eal/linuxapp/eal/eal_dev.c | 114 +++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-)lib/librte_eal/linuxapp/eal/eal_dev.c +4
-0@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h> @@ -14,15 +16,31 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h>
lib/librte_eal/linuxapp/eal/eal_dev.c line 30
Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +4
-0+#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h" static struct rte_intr_handle intr_handle = {.fd =-1 };static bool monitor_started; +extern struct rte_bus_list rte_bus_list; +
Where do you use the rte_bus_list? It seems the reference is a remnant from a previous version.
You do not seem to need a direct access on rte_bus_list, as you call rte_bus_find instead.
Why do you need this extern? I think its absence is motivated: to keep the bus list private and force users to access it through standard exposed ways.
Regards,
Signature · 2 lines
Gaëtan Rivet 6WIND
Re: [PATCH v6 6/7] eal: add failure handle mechanism for hotplug
hi, gaetan
Quoted 53 lines · Gaëtan Rivet
Hi Jeff,
Quoted 37 lines · Jeff Guo
This patch introduces a failure handle mechanism to handle device hotplug removal event.
First it can register sigbus handler when enable device event monitor. Once sigbus error be captured, it will check the failure address and accordingly remap the invalid memory for the corresponding device. Besed on this mechanism, it could guaranty the application not crash when the device be hotplug out.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v6->v5:
refine some doc and coding style--- lib/librte_eal/linuxapp/eal/eal_dev.c | 114 +++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-)lib/librte_eal/linuxapp/eal/eal_dev.c +4
-0@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h> @@ -14,15 +16,31 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h> lib/librte_eal/linuxapp/eal/eal_dev.c line 30
Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +4
-0+#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h" static struct rte_intr_handle intr_handle = {.fd =-1 };static bool monitor_started; +extern struct rte_bus_list rte_bus_list; +Where do you use the
rte_bus_list? It seems the reference is a remnant from a previous version.
Quoted 7 lines
You do not seem to need a direct access on
rte_bus_list, as you callrte_bus_findinstead.Why do you need this extern? I think its absence is motivated: to keep the bus list private and force users to access it through standard exposed ways.
Regards,
i think that is my missing here. Will delete it. Thanks for your info.
PATCH v6 7/7 igb_uio: fix uio release issue when hot unplug
In reply to Jeff GuoWhen hotplug out device, the kernel will release the device resource in the kernel side, such as the fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash. On the other hand, something like interrupt disabling do not automatically process in kernel side. If not handler it, this redundancy and dirty thing will affect the interrupt resource be used by other device. So the igb_uio driver have to check the hotplug status, and the corresponding process should be taken in igb uio driver.
This patch propose to add structure of rte_udev_state into rte_uio_pci_dev of igb_uio kernel driver, which will record the state of uio device, such as probed/opened/released/removed/unplug. When detect the unexpected removal which cause of hotplug out behavior, it will corresponding disable interrupt resource, while for the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer kernel crash issue.
---v6->v5:
no change
--- kernel/linux/igb_uio/igb_uio.c | 51 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-)
kernel/linux/igb_uio/igb_uio.c +48 -3
@@ -19,6 +19,15 @@#include "compat.h" +/* uio pci device state */ +enum rte_udev_state { +RTE_UDEV_PROBED , +RTE_UDEV_OPENNED , +RTE_UDEV_RELEASED , +RTE_UDEV_REMOVED , +RTE_UDEV_UNPLUG +}; +/** * A structure describing the private information for a uio device. */
@@ -28,6 +37,7 @@ struct rte_uio_pci_dev {enum rte_intr_mode mode;struct mutex lock;int refcnt; +enum rte_udev_state state; };static int wc_activate;
@@ -195,12 +205,22 @@ igbuio_pci_irqhandler(int irq, void *dev_id) {struct rte_uio_pci_dev *udev = (struct rte_uio_pci_dev *)dev_id;struct uio_info *info = &udev->info; +struct pci_dev *pdev = udev->pdev;/* Legacy mode need to mask in hardware */ if (udev->mode ==RTE_INTR_MODE_LEGACY && !pci_check_and_mask_intx (udev->pdev))return IRQ_NONE ; +mutex_lock (&udev->lock); +/* check the uevent of the kobj */ +if ((&pdev->dev.kobj)->state_remove_uevent_sent ==1 ) { +dev_notice (&pdev->dev,"device:%s, sent remove uevent!\n" , + (&pdev->dev.kobj)->name); + udev->state =RTE_UDEV_UNPLUG ; + } +mutex_unlock (&udev->lock); +uio_event_notify (info);/* Message signal mode, no share IRQ and automasked */
@@ -309,7 +329,6 @@ igbuio_pci_disable_interrupts(struct rte_uio_pci_dev *udev)#endif } -/** * This gets called while opening uio device file. */
@@ -331,20 +350,29 @@ igbuio_pci_open(struct uio_info *info, struct inode *inode)/* enable interrupts */ err =igbuio_pci_enable_interrupts (udev); -mutex_unlock (&udev->lock);if (err) {dev_err (&dev->dev,"Enable interrupt fails\n" ); +pci_clear_master (dev); +mutex_unlock (&udev->lock);return err; } + udev->state =RTE_UDEV_OPENNED ; +mutex_unlock (&udev->lock);return 0 ; } +/** +* This gets called while closing uio device file. +*/ static int igbuio_pci_release (struct uio_info *info,struct inode *inode) {struct rte_uio_pci_dev *udev = info->priv;struct pci_dev *dev = udev->pdev; +if (udev->state ==RTE_UDEV_REMOVED ) +return 0 ; +mutex_lock (&udev->lock);if (--udev->refcnt >0 ) {mutex_unlock (&udev->lock);
@@ -356,7 +384,7 @@ igbuio_pci_release(struct uio_info *info, struct inode *inode)/* stop the device from further DMA */ pci_clear_master (dev); - + udev->state =RTE_UDEV_RELEASED ;mutex_unlock (&udev->lock);return 0 ; }
@@ -562,6 +590,9 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) (unsigned long long)map_dma_addr, map_addr); } +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_PROBED ; +mutex_unlock (&udev->lock);return 0 ; fail_remove_group:
@@ -579,6 +610,20 @@ static void igbuio_pci_remove(struct pci_dev *dev) {struct rte_uio_pci_dev *udev =pci_get_drvdata (dev); +int ret; + +/* handler hot unplug */ +if (udev->state ==RTE_UDEV_OPENNED || + udev->state ==RTE_UDEV_UNPLUG ) { +dev_notice (&dev->dev,"Unexpected removal!\n" ); + ret =igbuio_pci_release (&udev->info,NULL ); +if (ret) +return ; +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_REMOVED ; +mutex_unlock (&udev->lock); +return ; + }mutex_destroy (&udev->lock);sysfs_remove_group (&dev->dev.kobj, &dev_attr_grp);
Signature · 1 line
2.7.4
v7 cover hotplug failure handle mechanism
In reply to Jeff GuoAs we know, hot plug is an importance feature, either use for the datacenter device’s fail-safe, or use for SRIOV Live Migration in SDN/NFV. It could bring the higher flexibility and continuality to the networking services in multiple use cases in industry. So let we see, dpdk as an importance networking framework, what can it help to implement hot plug solution for users.
We already have a general device event detect mechanism, failsafe driver, bonding driver and hot plug/unplug api in framework, app could use these to develop their hot plug solution.
let’s see the case of hot unplug, it can happen when a hardware device is be removed physically, or when the software disables it. App need to call ether dev API to detach the device, to unplug the device at the bus level and make access to the device invalid. But the problem is that, the removal of the device from the software lists is not going to be instantaneous, at this time if the data(fast) path still read/write the device, it will cause MMIO error and result of the app crash out.
Seems that we have got fail-safe driver(or app) + RTE_ETH_EVENT_INTR_RMV + kernel core driver solution to handle it, but still not have failsafe driver (or app) + RTE_DEV_EVENT_REMOVE + PCIe pmd driver failure handle solution. So there is an absence in dpdk hot plug solution right now.
Also, we know that kernel only guaranty hot plug on the kernel side, but not for the user mode side. Firstly we can hardly have a gatekeeper for any MMIO for multiple PMD driver. Secondly, no more specific 3rd tools such as udev/driverctl have especially cover these hot plug failure processing. Third, the feasibility of app’s implement for multiple user mode PMD driver is still a problem. Here, a general hot plug failure handle mechanism in dpdk framework would be proposed, it aim to guaranty that, when hot unplug occur, the system will not crash and app will not be break out, and user space can normally stop and release any relevant resources, then unplug of the device at the bus level cleanly.
The mechanism should be come across as bellow:
Firstly, app enabled the device event monitor and register the hot plug event’s callback before running data path. Once the hot unplug behave occur, the mechanism will detect the removal event and then accordingly do the failure handle. In order to do that, below functional will be bring in.
- Add a new bus ops “handle_hot_unplug” to handle bus read/write error, it is bus-specific and each kind of bus can implement its own logic.
- Implement pci bus specific ops “pci_handle_hot_unplug”. It will base on the failure address to remap memory for the corresponding device that unplugged.
For the data path or other unexpected control from the control path when hot unplug occur.
- Implement a new sigbus handler, it is registered when start device even monitoring. The handler is per process. Base on the signal event principle, control path thread and data path thread will randomly receive the sigbus error, but will go to the common sigbus handler. Once the MMIO sigbus error exposure, it will trigger the above hot unplug operation. The sigbus will be check if it is cause of the hot unplug or not, if not will info exception as the original sigbus handler. If yes, will do memory remapping.
For the control path and the igb uio release:
- When hot unplug device, the kernel will release the device resource in the kernel side, such as the fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash.
On the other hand, something like interrupt disable do not automatically process in kernel side. If not handler it, this redundancy and dirty thing will affect the interrupt resource be used by other device.
So the igb_uio driver have to check the hot plug status and corresponding process should be taken in igb uio deriver.
This patch propose to add structure of rte_udev_state into rte_uio_pci_dev of igb_uio kernel driver, which will record the state of uio device, such as probed/opened/released/removed/unplug. When detect the unexpected removal which cause of hot unplug behavior, it will corresponding disable interrupt resource, while for the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer kernel crash issue.
The mechanism could be use for fail-safe driver and app which want to use hot plug solution. let testpmd for example:
- Enable device event monitor->device unplug->failure handle->stop forwarding-> stop port->close port->detach port.
This process will not breaking the app/fail-safe running, and will not break other irrelevance device. And app could plug in the device and restart the date path again by below.
- Device plug in->bind igb_uio driver ->attached device->start port-> start forwarding.
patchset history:v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name
refine the sigbus handle logic
add lock for udev state in igb uio driver
v4->v3:
split patches to be small and clear
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug
v3->v2:
change bus ops name to bus_hotplug_handler. add new API and bus ops of bus_signal_handler distingush handle generic sigbus and hotplug sigbus
v2->v1(v21):
refine some doc and commit log
fix igb uio kernel issue for control path failure
rebase testpmd code
Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:v21->v20:
split function in hot unplug ops
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code
refine the remap logic for multiple device.
remove the auto binding
v19->18:
note for limitation of multiple hotplug,fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.
the prior patch history please check the patch set "add device event monitor framework"
Jeff Guo (7):
bus: add hotplug failure handler
bus/pci: implement hotplug failure handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler operation
bus: add helper to handle sigbus
eal: add failure handle mechanism for hotplug
igb_uio: fix uio release issue when hot unplug
drivers/bus/pci/pci_common.c | 77 ++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 ++++++++++ drivers/bus/pci/private.h | 12 ++++ kernel/linux/igb_uio/igb_uio.c | 51 ++++++++++++++- lib/librte_eal/common/eal_common_bus.c | 42 ++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++ lib/librte_eal/common/include/rte_bus.h | 33 ++++++++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 112 +++++++++++++++++++++++++++++++- 8 files changed, 368 insertions(+), 4 deletions(-)
Signature · 1 line
2.7.4
PATCH v7 1/7 bus: add hotplug failure handler
When device be hotplug out, if app still continue to access device by mmio, it will cause of memory failure and result the system crash.
This patch introduces a bus ops to handle device hotplug failure, it is a bus specific behavior, so each kind of bus can implement its own logic case by case.
--- lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);typedef int (*rte_bus_parse_t )(const char *name,void *addr);/** +* Implementation a specific hotplug failure handler, which is responsible +* for handle the failure when the device be hotplug out from the bus. When +* hotplug removal event be detected, it could call this function to handle +* failure and guaranty the system would not crash in the case. +* @param dev +* Pointer of the device structure. +* +* @return +* 0 on success. +* !0 on error. +*/ +typedef int (*rte_bus_hotplug_failure_handler_t )(struct rte_device *dev); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -211,6 +225,8 @@ struct rte_bus {rte_bus_parse_t parse;/**< Parse a device name */ struct rte_bus_conf conf;/**< Bus configuration */ rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ +rte_bus_hotplug_failure_handler_t hotplug_failure_handler; +/**< handle hotplug failure on bus */ };/**
Signature · 1 line
2.7.4
PATCH v7 2/7 bus/pci: implement hotplug failure handler ops
In reply to Jeff GuoThis patch implements the ops of hotplug failure handler for PCI bus, it is functional to remap a new dummy memory which overlap to the failure memory to avoid MMIO read/write error.
--- drivers/bus/pci/pci_common.c | 28 ++++++++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++ drivers/bus/pci/private.h | 12 ++++++++++++ 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
@@ -408,6 +408,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, }static int +pci_hotplug_failure_handler (struct rte_device *dev) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =RTE_DEV_TO_PCI (dev); +if (!pdev) +return -1 ; + +switch (pdev->kdrv) { +case RTE_KDRV_IGB_UIO : +case RTE_KDRV_UIO_GENERIC : +case RTE_KDRV_NIC_UIO : +/* mmio resource is invalid, remap it to be safe. */ + ret =pci_uio_remap_resource (pdev); +break ; +default : +RTE_LOG (DEBUG ,EAL , +"Not managed by a supported kernel driver, skipped\n" ); + ret =-1 ; +break ; + } + +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -437,6 +464,7 @@ struct rte_pci_bus rte_pci_bus = { .unplug = pci_unplug, .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, + .hotplug_failure_handler = pci_hotplug_failure_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res) } } +/* remap the PCI resource of a PCI device in anonymous virtual memory */ +int +pci_uio_remap_resource (struct rte_pci_device *dev) +{ +int i; +void *map_address; + +if (dev ==NULL ) +return -1 ; + +/* Remap all BARs */ +for (i =0 ; i !=PCI_MAX_RESOURCE ; i++) { +/* skip empty BAR */ +if (dev->mem_resource[i].phys_addr ==0 ) +continue ; + map_address =mmap (dev->mem_resource[i].addr, + (size_t )dev->mem_resource[i].len, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ); +if (map_address ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL , +"Cannot remap resource for device %s\n" , + dev->name); +return -1 ; + } +RTE_LOG (INFO ,EAL , +"Successful remap resource for device %s\n" , + dev->name); + } + +return 0 ; +} +static struct mapped_pci_resource *pci_uio_find_resource (struct rte_pci_device *dev) {
drivers/bus/pci/private.h +12 -0
@@ -123,6 +123,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev, struct mapped_pci_resource *uio_res);/** +* Remap the PCI resource of a PCI device in anonymous virtual memory. +* +* @param dev +* Point to the struct rte pci device. +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +pci_uio_remap_resource (struct rte_pci_device *dev); + +/** * Map device memory to uio resource * * This function is private to EAL.
Signature · 1 line
2.7.4
PATCH v7 3/7 bus: add sigbus handler
In reply to Jeff GuoWhen device be hotplug out, if data path still read/write device, the sigbus error will occur, this error need to be handled. So a handler need to be here to capture the signal and handle it correspondingly.
This patch introduces a bus ops to handle sigbus error, it is a bus specific behavior, so that each kind of bus can implement its own logic case by case.
--- lib/librte_eal/common/include/rte_bus.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
lib/librte_eal/common/include/rte_bus.h +17 -0
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);typedef int (*rte_bus_hotplug_failure_handler_t )(struct rte_device *dev);/** +* Implementation a specific sigbus handler, which is responsible for handle +* the sigbus error which is either original memory error, or specific memory +* error that caused of hot unplug. When sigbus error be captured, it could +* call this function to handle sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 for success handle the sigbus. +* 1 for no bus handle the sigbus. +* -1 for failed to handle the sigbus +*/ +typedef int (*rte_bus_sigbus_handler_t )(const void *failure_addr); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -227,6 +242,8 @@ struct rte_bus {rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ rte_bus_hotplug_failure_handler_t hotplug_failure_handler;/**< handle hotplug failure on bus */ +rte_bus_sigbus_handler_t sigbus_handler;/**< handle sigbus error */ + };/**
Signature · 1 line
2.7.4
PATCH v7 4/7 bus/pci: implement sigbus handler operation
In reply to Jeff GuoThis patch implements the ops of sigbus handler for PCI bus, it is functional to find the corresponding pci device which is been hotplug out, and then call the bus ops of hotplug failure handler to handle the failure for the device.
--- drivers/bus/pci/pci_common.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
drivers/bus/pci/pci_common.c +49 -0
@@ -407,6 +407,32 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,return NULL ; } +/* check the failure address belongs to which device. */ +static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int i; + +FOREACH_DEVICE_ON_PCIBUS (pdev) { +for (i =0 ; i !=RTE_DIM (pdev->mem_resource); i++) { +if ((uint64_t )(uintptr_t )failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t )(uintptr_t )failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) { +RTE_LOG (INFO ,EAL ,"Failure address " +"%16.16" PRIx64" belongs to " +"device %s!\n" , + (uint64_t )(uintptr_t )failure_addr, + pdev->device.name); +return pdev; + } + } + } +return NULL ; +} +static int pci_hotplug_failure_handler (struct rte_device *dev) {
@@ -435,6 +461,28 @@ pci_hotplug_failure_handler(struct rte_device *dev) }static int +pci_sigbus_handler (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =pci_find_device_by_addr (failure_addr); +if (!pdev) { +/* It is a generic sigbus error, no bus would handle it. */ + ret =1 ; + }else { +/* The sigbus error is caused of hot removal. */ + ret =pci_hotplug_failure_handler (&pdev->device); +if (ret) { +RTE_LOG (ERR ,EAL ,"Failed to handle hot plug for " +"device %s" , pdev->name); + ret =-1 ; + } + } +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -465,6 +513,7 @@ struct rte_pci_bus rte_pci_bus = { .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, .hotplug_failure_handler = pci_hotplug_failure_handler, + .sigbus_handler = pci_sigbus_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
PATCH v7 5/7 bus: add helper to handle sigbus
In reply to Jeff GuoThis patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
--- lib/librte_eal/common/eal_common_bus.c | 42 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++++++++ 2 files changed, 54 insertions(+)
lib/librte_eal/common/eal_common_bus.c +42 -0
@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -242,3 +243,44 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) { +RTE_LOG (ERR ,EAL ,"Function sigbus_handler not supported by " +"bus (%s)\n" , bus->name); +return -1 ; + } + + ret = bus->sigbus_handler (failure_addr); + rte_errno = ret; + +return !(bus->sigbus_handler && ret <=0 ); +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* failed to handle the sigbus, pass the new errno. */ +if (!bus) + ret =1 ; +else if (rte_errno ==-1 ) +return -1 ; + +/* otherwise restore the old errno. */ + rte_errno = old_errno; + +return ret; +}
lib/librte_eal/common/eal_private.h +12 -0
@@ -258,4 +258,16 @@ int rte_mp_channel_init(void);*/ void dev_callback_process (char *device_name,enum rte_dev_event_type event); +/** +* Iterate all buses to find the corresponding bus, to handle the sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
PATCH v7 6/7 eal: add failure handle mechanism for hotplug
In reply to Jeff GuoThis patch introduces a failure handle mechanism to handle device hotplug removal event.
First it can register sigbus handler when enable device event monitor. Once sigbus error be captured, it will check the failure address and accordingly remap the invalid memory for the corresponding device. Besed on this mechanism, it could guaranty the application not crash when the device be hotplug out.
---v7->v6:
delete some unused part.
--- lib/librte_eal/linuxapp/eal/eal_dev.c | 112 +++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-)
lib/librte_eal/linuxapp/eal/eal_dev.c +111 -1
@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h>
@@ -14,6 +16,10 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h> +#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -23,6 +29,16 @@ static bool monitor_started;#define EAL_UEV_MSG_LEN 4096#define EAL_UEV_MSG_ELEM_LEN 128 +/* +* spinlock for device failure process, protect the bus and the device +* to avoid race condition. +*/ +static rte_spinlock_t dev_failure_lock =RTE_SPINLOCK_INITIALIZER ; + +static struct sigaction sigbus_action_old; + +static int sigbus_need_recover; + static void dev_uev_handler(__rte_unused void *param);/* identify the system layer which reports this event. */
@@ -33,6 +49,49 @@ enum eal_dev_event_subsystem {EAL_DEV_EVENT_SUBSYSTEM_MAX }; +static void +sigbus_action_recover (void ) +{ +if (sigbus_need_recover) { +sigaction (SIGBUS , &sigbus_action_old,NULL ); + sigbus_need_recover =0 ; + } +} + +static void sigbus_handler (int signum,siginfo_t *info, + void *ctx __rte_unused) +{ +int ret; + +RTE_LOG (INFO ,EAL ,"Thread[%d] catch SIGBUS, fault address:%p\n" , + (int )pthread_self (), info->si_addr); + +rte_spinlock_lock (&dev_failure_lock); + ret =rte_bus_sigbus_handler (info->si_addr); +rte_spinlock_unlock (&dev_failure_lock); +if (ret ==-1 ) { +rte_exit (EXIT_FAILURE , +"Failed to handle SIGBUS for hotplug, " +"(rte_errno: %s)!" ,strerror (rte_errno)); + }else if (ret ==1 ) { +if (sigbus_action_old.sa_handler) + (*(sigbus_action_old.sa_handler))(signum); +else +rte_exit (EXIT_FAILURE , +"Failed to handle generic SIGBUS!" ); + } + +RTE_LOG (INFO ,EAL ,"Success to handle SIGBUS for hotplug!\n" ); +} + +static int cmp_dev_name (const struct rte_device *dev, +const void *_name) +{ +const char *name = _name; + +return strcmp (dev->name, name); +} +static int dev_uev_socket_fd_create (void ) {
@@ -147,6 +206,9 @@ dev_uev_handler(__rte_unused void *param)struct rte_dev_event uevent;int ret;char buf[EAL_UEV_MSG_LEN ]; +struct rte_bus *bus; +struct rte_device *dev; +const char *busname ="" ;memset (&uevent,0 ,sizeof (struct rte_dev_event ));memset (buf,0 ,EAL_UEV_MSG_LEN );
@@ -171,13 +233,50 @@ dev_uev_handler(__rte_unused void *param)RTE_LOG (DEBUG ,EAL ,"receive uevent(name:%s, type:%d, subsystem:%d)\n" , uevent.devname, uevent.type, uevent.subsystem); -if (uevent.devname) +switch (uevent.subsystem) { +case EAL_DEV_EVENT_SUBSYSTEM_PCI : +case EAL_DEV_EVENT_SUBSYSTEM_UIO : + busname ="pci" ; +break ; +default : +break ; + } + +if (uevent.devname) { +if (uevent.type ==RTE_DEV_EVENT_REMOVE ) { +rte_spinlock_lock (&dev_failure_lock); + bus =rte_bus_find_by_name (busname); +if (bus ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find bus (%s)\n" , + busname); +return ; + } + + dev = bus->find_device (NULL , cmp_dev_name, + uevent.devname); +if (dev ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find device (%s) on " +"bus (%s)\n" , uevent.devname, busname); +return ; + } + + ret = bus->hotplug_failure_handler (dev); +rte_spinlock_unlock (&dev_failure_lock); +if (ret) { +RTE_LOG (ERR ,EAL ,"Can not handle hotplug for " +"device (%s)\n" , dev->name); +return ; + } + }dev_callback_process (uevent.devname, uevent.type); + } } int __rte_experimentalrte_dev_event_monitor_start (void) { +sigset_t mask; +struct sigaction action;int ret;if (monitor_started)
@@ -197,6 +296,14 @@ rte_dev_event_monitor_start(void)return -1 ; } +/* register sigbus handler */ +sigemptyset (&mask); +sigaddset (&mask,SIGBUS ); + action.sa_flags =SA_SIGINFO ; + action.sa_mask = mask; + action.sa_sigaction = sigbus_handler; + sigbus_need_recover = !sigaction (SIGBUS , &action, &sigbus_action_old); + monitor_started = true;return 0 ;
@@ -217,8 +324,11 @@ rte_dev_event_monitor_stop(void)return ret; } +sigbus_action_recover (); +close (intr_handle.fd); intr_handle.fd =-1 ; monitor_started = false; +return 0 ; }
Signature · 1 line
2.7.4
PATCH v7 7/7 igb_uio: fix uio release issue when hot unplug
In reply to Jeff GuoWhen hotplug out device, the kernel will release the device resource in the kernel side, such as the fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash. On the other hand, something like interrupt disabling do not automatically process in kernel side. If not handler it, this redundancy and dirty thing will affect the interrupt resource be used by other device. So the igb_uio driver have to check the hotplug status, and the corresponding process should be taken in igb uio driver.
This patch propose to add structure of rte_udev_state into rte_uio_pci_dev of igb_uio kernel driver, which will record the state of uio device, such as probed/opened/released/removed/unplug. When detect the unexpected removal which cause of hotplug out behavior, it will corresponding disable interrupt resource, while for the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer kernel crash issue.
--- kernel/linux/igb_uio/igb_uio.c | 51 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-)
kernel/linux/igb_uio/igb_uio.c +48 -3
@@ -19,6 +19,15 @@#include "compat.h" +/* uio pci device state */ +enum rte_udev_state { +RTE_UDEV_PROBED , +RTE_UDEV_OPENNED , +RTE_UDEV_RELEASED , +RTE_UDEV_REMOVED , +RTE_UDEV_UNPLUG +}; +/** * A structure describing the private information for a uio device. */
@@ -28,6 +37,7 @@ struct rte_uio_pci_dev {enum rte_intr_mode mode;struct mutex lock;int refcnt; +enum rte_udev_state state; };static int wc_activate;
@@ -195,12 +205,22 @@ igbuio_pci_irqhandler(int irq, void *dev_id) {struct rte_uio_pci_dev *udev = (struct rte_uio_pci_dev *)dev_id;struct uio_info *info = &udev->info; +struct pci_dev *pdev = udev->pdev;/* Legacy mode need to mask in hardware */ if (udev->mode ==RTE_INTR_MODE_LEGACY && !pci_check_and_mask_intx (udev->pdev))return IRQ_NONE ; +mutex_lock (&udev->lock); +/* check the uevent of the kobj */ +if ((&pdev->dev.kobj)->state_remove_uevent_sent ==1 ) { +dev_notice (&pdev->dev,"device:%s, sent remove uevent!\n" , + (&pdev->dev.kobj)->name); + udev->state =RTE_UDEV_UNPLUG ; + } +mutex_unlock (&udev->lock); +uio_event_notify (info);/* Message signal mode, no share IRQ and automasked */
@@ -309,7 +329,6 @@ igbuio_pci_disable_interrupts(struct rte_uio_pci_dev *udev)#endif } -/** * This gets called while opening uio device file. */
@@ -331,20 +350,29 @@ igbuio_pci_open(struct uio_info *info, struct inode *inode)/* enable interrupts */ err =igbuio_pci_enable_interrupts (udev); -mutex_unlock (&udev->lock);if (err) {dev_err (&dev->dev,"Enable interrupt fails\n" ); +pci_clear_master (dev); +mutex_unlock (&udev->lock);return err; } + udev->state =RTE_UDEV_OPENNED ; +mutex_unlock (&udev->lock);return 0 ; } +/** +* This gets called while closing uio device file. +*/ static int igbuio_pci_release (struct uio_info *info,struct inode *inode) {struct rte_uio_pci_dev *udev = info->priv;struct pci_dev *dev = udev->pdev; +if (udev->state ==RTE_UDEV_REMOVED ) +return 0 ; +mutex_lock (&udev->lock);if (--udev->refcnt >0 ) {mutex_unlock (&udev->lock);
@@ -356,7 +384,7 @@ igbuio_pci_release(struct uio_info *info, struct inode *inode)/* stop the device from further DMA */ pci_clear_master (dev); - + udev->state =RTE_UDEV_RELEASED ;mutex_unlock (&udev->lock);return 0 ; }
@@ -562,6 +590,9 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) (unsigned long long)map_dma_addr, map_addr); } +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_PROBED ; +mutex_unlock (&udev->lock);return 0 ; fail_remove_group:
@@ -579,6 +610,20 @@ static void igbuio_pci_remove(struct pci_dev *dev) {struct rte_uio_pci_dev *udev =pci_get_drvdata (dev); +int ret; + +/* handler hot unplug */ +if (udev->state ==RTE_UDEV_OPENNED || + udev->state ==RTE_UDEV_UNPLUG ) { +dev_notice (&dev->dev,"Unexpected removal!\n" ); + ret =igbuio_pci_release (&udev->info,NULL ); +if (ret) +return ; +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_REMOVED ; +mutex_unlock (&udev->lock); +return ; + }mutex_destroy (&udev->lock);sysfs_remove_group (&dev->dev.kobj, &dev_attr_grp);
Signature · 1 line
2.7.4
v7 cover hotplug failure handle mechanism
In reply to Jeff GuoAs we know, hot plug is an importance feature, either use for the datacenter device’s fail-safe, or use for SRIOV Live Migration in SDN/NFV. It could bring the higher flexibility and continuality to the networking services in multiple use cases in industry. So let we see, dpdk as an importance networking framework, what can it help to implement hot plug solution for users.
We already have a general device event detect mechanism, failsafe driver, bonding driver and hot plug/unplug api in framework, app could use these to develop their hot plug solution.
let’s see the case of hot unplug, it can happen when a hardware device is be removed physically, or when the software disables it. App need to call ether dev API to detach the device, to unplug the device at the bus level and make access to the device invalid. But the problem is that, the removal of the device from the software lists is not going to be instantaneous, at this time if the data(fast) path still read/write the device, it will cause MMIO error and result of the app crash out.
Seems that we have got fail-safe driver(or app) + RTE_ETH_EVENT_INTR_RMV + kernel core driver solution to handle it, but still not have failsafe driver (or app) + RTE_DEV_EVENT_REMOVE + PCIe pmd driver failure handle solution. So there is an absence in dpdk hot plug solution right now.
Also, we know that kernel only guaranty hot plug on the kernel side, but not for the user mode side. Firstly we can hardly have a gatekeeper for any MMIO for multiple PMD driver. Secondly, no more specific 3rd tools such as udev/driverctl have especially cover these hot plug failure processing. Third, the feasibility of app’s implement for multiple user mode PMD driver is still a problem. Here, a general hot plug failure handle mechanism in dpdk framework would be proposed, it aim to guaranty that, when hot unplug occur, the system will not crash and app will not be break out, and user space can normally stop and release any relevant resources, then unplug of the device at the bus level cleanly.
The mechanism should be come across as bellow:
Firstly, app enabled the device event monitor and register the hot plug event’s callback before running data path. Once the hot unplug behave occur, the mechanism will detect the removal event and then accordingly do the failure handle. In order to do that, below functional will be bring in.
- Add a new bus ops “handle_hot_unplug” to handle bus read/write error, it is bus-specific and each kind of bus can implement its own logic.
- Implement pci bus specific ops “pci_handle_hot_unplug”. It will base on the failure address to remap memory for the corresponding device that unplugged.
For the data path or other unexpected control from the control path when hot unplug occur.
- Implement a new sigbus handler, it is registered when start device even monitoring. The handler is per process. Base on the signal event principle, control path thread and data path thread will randomly receive the sigbus error, but will go to the common sigbus handler. Once the MMIO sigbus error exposure, it will trigger the above hot unplug operation. The sigbus will be check if it is cause of the hot unplug or not, if not will info exception as the original sigbus handler. If yes, will do memory remapping.
For the control path and the igb uio release:
- When hot unplug device, the kernel will release the device resource in the kernel side, such as the fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash.
On the other hand, something like interrupt disable do not automatically process in kernel side. If not handler it, this redundancy and dirty thing will affect the interrupt resource be used by other device.
So the igb_uio driver have to check the hot plug status and corresponding process should be taken in igb uio deriver.
This patch propose to add structure of rte_udev_state into rte_uio_pci_dev of igb_uio kernel driver, which will record the state of uio device, such as probed/opened/released/removed/unplug. When detect the unexpected removal which cause of hot unplug behavior, it will corresponding disable interrupt resource, while for the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer kernel crash issue.
The mechanism could be use for fail-safe driver and app which want to use hot plug solution. let testpmd for example:
- Enable device event monitor->device unplug->failure handle->stop forwarding-> stop port->close port->detach port.
This process will not breaking the app/fail-safe running, and will not break other irrelevance device. And app could plug in the device and restart the date path again by below.
- Device plug in->bind igb_uio driver ->attached device->start port-> start forwarding.
patchset history:v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name
refine the sigbus handle logic
add lock for udev state in igb uio driver
v4->v3:
split patches to be small and clear
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug
v3->v2:
change bus ops name to bus_hotplug_handler. add new API and bus ops of bus_signal_handler distingush handle generic sigbus and hotplug sigbus
v2->v1(v21):
refine some doc and commit log
fix igb uio kernel issue for control path failure
rebase testpmd code
Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:v21->v20:
split function in hot unplug ops
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code
refine the remap logic for multiple device.
remove the auto binding
v19->18:
note for limitation of multiple hotplug,fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.
the prior patch history please check the patch set "add device event monitor framework"
Jeff Guo (7):
bus: add hotplug failure handler
bus/pci: implement hotplug failure handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler operation
bus: add helper to handle sigbus
eal: add failure handle mechanism for hotplug
igb_uio: fix uio release issue when hot unplug
drivers/bus/pci/pci_common.c | 77 ++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 ++++++++++ drivers/bus/pci/private.h | 12 ++++ kernel/linux/igb_uio/igb_uio.c | 51 ++++++++++++++- lib/librte_eal/common/eal_common_bus.c | 42 ++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++ lib/librte_eal/common/include/rte_bus.h | 33 ++++++++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 112 +++++++++++++++++++++++++++++++- 8 files changed, 368 insertions(+), 4 deletions(-)
Signature · 1 line
2.7.4
PATCH v7 1/7 bus: add hotplug failure handler
When device be hotplug out, if app still continue to access device by mmio, it will cause of memory failure and result the system crash.
This patch introduces a bus ops to handle device hotplug failure, it is a bus specific behavior, so each kind of bus can implement its own logic case by case.
---v7->v6:
no change
--- lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);typedef int (*rte_bus_parse_t )(const char *name,void *addr);/** +* Implementation a specific hotplug failure handler, which is responsible +* for handle the failure when the device be hotplug out from the bus. When +* hotplug removal event be detected, it could call this function to handle +* failure and guaranty the system would not crash in the case. +* @param dev +* Pointer of the device structure. +* +* @return +* 0 on success. +* !0 on error. +*/ +typedef int (*rte_bus_hotplug_failure_handler_t )(struct rte_device *dev); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -211,6 +225,8 @@ struct rte_bus {rte_bus_parse_t parse;/**< Parse a device name */ struct rte_bus_conf conf;/**< Bus configuration */ rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ +rte_bus_hotplug_failure_handler_t hotplug_failure_handler; +/**< handle hotplug failure on bus */ };/**
Signature · 1 line
2.7.4
PATCH v7 2/7 bus/pci: implement hotplug failure handler ops
In reply to Jeff GuoThis patch implements the ops of hotplug failure handler for PCI bus, it is functional to remap a new dummy memory which overlap to the failure memory to avoid MMIO read/write error.
---v7->v6:
no change
--- drivers/bus/pci/pci_common.c | 28 ++++++++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++ drivers/bus/pci/private.h | 12 ++++++++++++ 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
@@ -408,6 +408,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, }static int +pci_hotplug_failure_handler (struct rte_device *dev) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =RTE_DEV_TO_PCI (dev); +if (!pdev) +return -1 ; + +switch (pdev->kdrv) { +case RTE_KDRV_IGB_UIO : +case RTE_KDRV_UIO_GENERIC : +case RTE_KDRV_NIC_UIO : +/* mmio resource is invalid, remap it to be safe. */ + ret =pci_uio_remap_resource (pdev); +break ; +default : +RTE_LOG (DEBUG ,EAL , +"Not managed by a supported kernel driver, skipped\n" ); + ret =-1 ; +break ; + } + +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -437,6 +464,7 @@ struct rte_pci_bus rte_pci_bus = { .unplug = pci_unplug, .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, + .hotplug_failure_handler = pci_hotplug_failure_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res) } } +/* remap the PCI resource of a PCI device in anonymous virtual memory */ +int +pci_uio_remap_resource (struct rte_pci_device *dev) +{ +int i; +void *map_address; + +if (dev ==NULL ) +return -1 ; + +/* Remap all BARs */ +for (i =0 ; i !=PCI_MAX_RESOURCE ; i++) { +/* skip empty BAR */ +if (dev->mem_resource[i].phys_addr ==0 ) +continue ; + map_address =mmap (dev->mem_resource[i].addr, + (size_t )dev->mem_resource[i].len, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ); +if (map_address ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL , +"Cannot remap resource for device %s\n" , + dev->name); +return -1 ; + } +RTE_LOG (INFO ,EAL , +"Successful remap resource for device %s\n" , + dev->name); + } + +return 0 ; +} +static struct mapped_pci_resource *pci_uio_find_resource (struct rte_pci_device *dev) {
drivers/bus/pci/private.h +12 -0
@@ -123,6 +123,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev, struct mapped_pci_resource *uio_res);/** +* Remap the PCI resource of a PCI device in anonymous virtual memory. +* +* @param dev +* Point to the struct rte pci device. +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +pci_uio_remap_resource (struct rte_pci_device *dev); + +/** * Map device memory to uio resource * * This function is private to EAL.
Signature · 1 line
2.7.4
PATCH v7 3/7 bus: add sigbus handler
In reply to Jeff GuoWhen device be hotplug out, if data path still read/write device, the sigbus error will occur, this error need to be handled. So a handler need to be here to capture the signal and handle it correspondingly.
This patch introduces a bus ops to handle sigbus error, it is a bus specific behavior, so that each kind of bus can implement its own logic case by case.
---v7->v6:
no change
--- lib/librte_eal/common/include/rte_bus.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
lib/librte_eal/common/include/rte_bus.h +17 -0
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);typedef int (*rte_bus_hotplug_failure_handler_t )(struct rte_device *dev);/** +* Implementation a specific sigbus handler, which is responsible for handle +* the sigbus error which is either original memory error, or specific memory +* error that caused of hot unplug. When sigbus error be captured, it could +* call this function to handle sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 for success handle the sigbus. +* 1 for no bus handle the sigbus. +* -1 for failed to handle the sigbus +*/ +typedef int (*rte_bus_sigbus_handler_t )(const void *failure_addr); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -227,6 +242,8 @@ struct rte_bus {rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ rte_bus_hotplug_failure_handler_t hotplug_failure_handler;/**< handle hotplug failure on bus */ +rte_bus_sigbus_handler_t sigbus_handler;/**< handle sigbus error */ + };/**
Signature · 1 line
2.7.4
PATCH v7 4/7 bus/pci: implement sigbus handler operation
In reply to Jeff GuoThis patch implements the ops of sigbus handler for PCI bus, it is functional to find the corresponding pci device which is been hotplug out, and then call the bus ops of hotplug failure handler to handle the failure for the device.
---v7->v6:
no change
--- drivers/bus/pci/pci_common.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
drivers/bus/pci/pci_common.c +49 -0
@@ -407,6 +407,32 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,return NULL ; } +/* check the failure address belongs to which device. */ +static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int i; + +FOREACH_DEVICE_ON_PCIBUS (pdev) { +for (i =0 ; i !=RTE_DIM (pdev->mem_resource); i++) { +if ((uint64_t )(uintptr_t )failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t )(uintptr_t )failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) { +RTE_LOG (INFO ,EAL ,"Failure address " +"%16.16" PRIx64" belongs to " +"device %s!\n" , + (uint64_t )(uintptr_t )failure_addr, + pdev->device.name); +return pdev; + } + } + } +return NULL ; +} +static int pci_hotplug_failure_handler (struct rte_device *dev) {
@@ -435,6 +461,28 @@ pci_hotplug_failure_handler(struct rte_device *dev) }static int +pci_sigbus_handler (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =pci_find_device_by_addr (failure_addr); +if (!pdev) { +/* It is a generic sigbus error, no bus would handle it. */ + ret =1 ; + }else { +/* The sigbus error is caused of hot removal. */ + ret =pci_hotplug_failure_handler (&pdev->device); +if (ret) { +RTE_LOG (ERR ,EAL ,"Failed to handle hot plug for " +"device %s" , pdev->name); + ret =-1 ; + } + } +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -465,6 +513,7 @@ struct rte_pci_bus rte_pci_bus = { .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, .hotplug_failure_handler = pci_hotplug_failure_handler, + .sigbus_handler = pci_sigbus_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
PATCH v7 5/7 bus: add helper to handle sigbus
In reply to Jeff GuoThis patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
---v7->v6:
no change
--- lib/librte_eal/common/eal_common_bus.c | 42 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++++++++ 2 files changed, 54 insertions(+)
lib/librte_eal/common/eal_common_bus.c +42 -0
@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -242,3 +243,44 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) { +RTE_LOG (ERR ,EAL ,"Function sigbus_handler not supported by " +"bus (%s)\n" , bus->name); +return -1 ; + } + + ret = bus->sigbus_handler (failure_addr); + rte_errno = ret; + +return !(bus->sigbus_handler && ret <=0 ); +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* failed to handle the sigbus, pass the new errno. */ +if (!bus) + ret =1 ; +else if (rte_errno ==-1 ) +return -1 ; + +/* otherwise restore the old errno. */ + rte_errno = old_errno; + +return ret; +}
lib/librte_eal/common/eal_private.h +12 -0
@@ -258,4 +258,16 @@ int rte_mp_channel_init(void);*/ void dev_callback_process (char *device_name,enum rte_dev_event_type event); +/** +* Iterate all buses to find the corresponding bus, to handle the sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
Re: [PATCH v7 5/7] bus: add helper to handle sigbus
Quoted 29 lines · Jeff Guo
This patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v7->v6:
no change--- lib/librte_eal/common/eal_common_bus.c | 42 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++++++++ 2 files changed, 54 insertions(+)lib/librte_eal/common/eal_common_bus.c +1
-0@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h" @@ -242,3 +243,44 @@ rte_bus_get_iommu_class(void) }return mode; }
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 255
Quoted 10 lines
lib/librte_eal/common/eal_common_bus.c +10
-0+ +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) { +RTE_LOG (ERR ,EAL ,"Function sigbus_handler not supported by " +"bus (%s)\n" , bus->name);
It is not an error. It is OK that some buses cannot handle SIGBUS.
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 262
Quoted 7 lines
lib/librte_eal/common/eal_common_bus.c +7
-0+return -1 ; + } + + ret = bus->sigbus_handler (failure_addr); + rte_errno = ret; + +return !(bus->sigbus_handler && ret <=0 );
There is no point to check bus->sigbus_handler here. It is already checked above. So, it should be just:
return ret > 0;
I.e. we should continue search if the address is not handled by any device on the bus (we should stop if it is handled (ret==0) or failed to to handle (ret < 0)).
Quoted 7 lines
lib/librte_eal/common/eal_common_bus.c +7
-0+} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; +
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 279
Quoted 10 lines
lib/librte_eal/common/eal_common_bus.c +10
-0+int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* failed to handle the sigbus, pass the new errno. */ +if (!bus) + ret =1 ; +else if (rte_errno ==-1 )
I'm still thinking it is bad to keep negative value in rte_errno here.
Quoted 28 lines
lib/librte_eal/common/eal_common_bus.c +7
-0+return -1 ; + +/* otherwise restore the old errno. */ + rte_errno = old_errno; + +return ret; +}lib/librte_eal/common/eal_private.h +12
-0@@ -258,4 +258,16 @@ int rte_mp_channel_init(void);*/ void dev_callback_process (char *device_name,enum rte_dev_event_type event); +/** +* Iterate all buses to find the corresponding bus, to handle the sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Re: [PATCH v7 5/7] bus: add helper to handle sigbus
Quoted 33 lines · Andrew Rybchenko
Quoted 32 lines · Jeff Guo
This patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v7->v6:
no change--- lib/librte_eal/common/eal_common_bus.c | 42 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++++++++ 2 files changed, 54 insertions(+)lib/librte_eal/common/eal_common_bus.c +1
-0@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h" @@ -242,3 +243,44 @@ rte_bus_get_iommu_class(void) }return mode;}
lib/librte_eal/common/eal_common_bus.c +4
-0+ +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr)
Quoted 9 lines
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 255
Quoted 6 lines
lib/librte_eal/common/eal_common_bus.c +6
-0+{ +int ret; + +if (!bus->sigbus_handler) { +RTE_LOG (ERR ,EAL ,"Function sigbus_handler not supported by " +"bus (%s)\n" , bus->name);It is not an error. It is OK that some buses cannot handle SIGBUS.
yes, it is.
Quoted 10 lines
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 262
Quoted 7 lines
lib/librte_eal/common/eal_common_bus.c +7
-0+return -1 ; + } + + ret = bus->sigbus_handler (failure_addr); + rte_errno = ret; + +return !(bus->sigbus_handler && ret <=0 );There is no point to check
bus->sigbus_handlerhere. It is already checked above. So, it should be just:
Quoted 5 lines
return ret > 0;I.e. we should continue search if the address is not handled by any device on the bus (we should stop if it is handled (ret==0) or failed to to handle (ret < 0)).
i will modify it, thanks.
Quoted 11 lines
Quoted 11 lines
lib/librte_eal/common/eal_common_bus.c +11
-0+} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ;
Quoted 9 lines
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 279
Quoted 6 lines
lib/librte_eal/common/eal_common_bus.c +6
-0+ + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* failed to handle the sigbus, pass the new errno. */ +if (!bus) + ret =1 ; +else if (rte_errno ==-1 )I'm still thinking it is bad to keep negative value in
rte_errnohere.
i think the rte_errno just no used for the caller if return -1. Since if find bus but process failed, will use rte_exit to process whatever the rte_errno value. Only return 1 means use the origin sigbus handler that will care about the errno.
Quoted 30 lines
Quoted 29 lines
lib/librte_eal/common/eal_common_bus.c +7
-0+return -1 ; + +/* otherwise restore the old errno. */ + rte_errno = old_errno; + +return ret; +}lib/librte_eal/common/eal_private.h +1
-0@@ -258,4 +258,16 @@ int rte_mp_channel_init(void);*/ void dev_callback_process (char *device_name,enum rte_dev_event_type event); +/** +* Iterate all buses to find the corresponding bus, to handle the sigbus error.
+ * @paramfailure_addr
+ * Pointer of the fault address of the sigbus error.
+ *
+ * @return
+ * 0 success to handle the sigbus.
+ * -1 failed to handle the sigbus
+ * 1 no bus can handler the sigbus
+ */lib/librte_eal/common/eal_private.h +2
-0+int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Re: [PATCH v7 5/7] bus: add helper to handle sigbus
Quoted 68 lines · Jeff Guo
Quoted 34 lines · Andrew Rybchenko
Quoted 33 lines · Jeff Guo
This patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v7->v6:
no change--- lib/librte_eal/common/eal_common_bus.c | 42 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++++++++ 2 files changed, 54 insertions(+)lib/librte_eal/common/eal_common_bus.c +1
-0@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h" @@ -242,3 +243,44 @@ rte_bus_get_iommu_class(void) }return mode;}
lib/librte_eal/common/eal_common_bus.c +4
-0+ +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr)Quoted 9 lines
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 255
Quoted 6 lines
lib/librte_eal/common/eal_common_bus.c +6
-0+{ +int ret; + +if (!bus->sigbus_handler) { +RTE_LOG (ERR ,EAL ,"Function sigbus_handler not supported by " +"bus (%s)\n" , bus->name);It is not an error. It is OK that some buses cannot handle SIGBUS.
yes, it is.
Quoted 11 lines
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 262
Quoted 7 lines
lib/librte_eal/common/eal_common_bus.c +7
-0+return -1 ; + } + + ret = bus->sigbus_handler (failure_addr); + rte_errno = ret; + +return !(bus->sigbus_handler && ret <=0 );There is no point to check
bus->sigbus_handlerhere. It is already checked above.
So, it should be just:Quoted 7 lines
return ret > 0;I.e. we should continue search if the address is not handled by any device
on the bus (we should stop if it is handled (ret==0) or failed to to handle
(ret < 0)).
Quoted 2 lines
i will modify it, thanks.
Why is rte_errno set here?rte_errno is meant by the bus dev to be set on error. You do not have to modify it.
ret would already be <0 on error.
At most, you could do something like:
if (ret < 0 && rte_errno == 0)
rte_errno = ENOTSUP;
Or something akin, with a non-descriptive error hinting that the developper didn't seem to care about setting errno to something meaningful (so only partially respecting the API).
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
lib/librte_eal/common/eal_common_bus.c +11
-0+} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ;Quoted 9 lines
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 279
Quoted 6 lines
lib/librte_eal/common/eal_common_bus.c +6
-0+ + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* failed to handle the sigbus, pass the new errno. */ +if (!bus) + ret =1 ; +else if (rte_errno ==-1 )I'm still thinking it is bad to keep negative value in
rte_errnohere.
Quoted 5 lines
i think the
rte_errnojust no used for the caller if return -1. Since if find bus but process failed, will userte_exitto process whatever therte_errnovalue. Only return 1 means use the origin sigbus handler that will care about the errno.
With the changes above, the check should be something like:
if (bus == NULL)
return 1;
else if (rte_errno != 0)
return -rte_errno;
rte_errno = old_errno;
return 0;Which would avoid resetting rte_errno on top of whichever value a dev would have used, and having it set to a negative non-errno value.
(Please do not just use this as-is, if you think this is not a good idea just tell us why or how you would prefer to do it. I'm only proposing a way that I think would work.)
Regards,
Signature · 2 lines
Gaëtan Rivet 6WIND
Re: [PATCH v7 5/7] bus: add helper to handle sigbus
Quoted 71 lines · Gaëtan Rivet
Quoted 63 lines · Jeff Guo
Quoted 33 lines · Andrew Rybchenko
Quoted 32 lines · Jeff Guo
This patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v7->v6:
no change--- lib/librte_eal/common/eal_common_bus.c | 42 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++++++++ 2 files changed, 54 insertions(+)lib/librte_eal/common/eal_common_bus.c +1
-0@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h" @@ -242,3 +243,44 @@ rte_bus_get_iommu_class(void) }return mode;}
lib/librte_eal/common/eal_common_bus.c +3
-0+ +static int +bus_handle_sigbus(const struct rte_bus *bus,Quoted 9 lines
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 255
Quoted 7 lines
lib/librte_eal/common/eal_common_bus.c +7
-0+ const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) { +RTE_LOG (ERR ,EAL ,"Function sigbus_handler not supported by " +"bus (%s)\n" , bus->name);It is not an error. It is OK that some buses cannot handle SIGBUS.
yes, it is.
Quoted 10 lines
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 262
Quoted 7 lines
lib/librte_eal/common/eal_common_bus.c +7
-0+return -1 ; + } + + ret = bus->sigbus_handler (failure_addr); + rte_errno = ret; + +return !(bus->sigbus_handler && ret <=0 );There is no point to check
bus->sigbus_handlerhere. It is already checked above.
So, it should be just:Quoted 7 lines
return ret > 0;I.e. we should continue search if the address is not handled by any device
on the bus (we should stop if it is handled (ret==0) or failed to to handle
(ret < 0)).Quoted 2 lines
i will modify it, thanks.
Why is
rte_errnoset here?rte_errnois meant by the bus dev to be set on error. You do not have to modify it.
ret would already be <0 on error.
Quoted 8 lines
At most, you could do something like:
if (ret < 0 &&
rte_errno== 0)rte_errno = ENOTSUP;Or something akin, with a non-descriptive error hinting that the developper didn't seem to care about setting errno to something meaningful (so only partially respecting the API).
the purpose to set rte_errno here is because of the status of the
handle need to pass though to the function caller "rte_bus_sigbus_handler", it could give a chance to check the searching status.
Quoted 34 lines
Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
lib/librte_eal/common/eal_common_bus.c +10
-0+} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; +Quoted 9 lines
lib/librte_eal/common/eal_common_bus.c rte_bus_get_iommu_class(void) · line 279
Quoted 7 lines
lib/librte_eal/common/eal_common_bus.c +7
-0+ rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* failed to handle the sigbus, pass the new errno. */ +if (!bus) + ret =1 ; +else if (rte_errno ==-1 )I'm still thinking it is bad to keep negative value in
rte_errnohere.Quoted 5 lines
i think the
rte_errnojust no used for the caller if return -1. Since if find bus but process failed, will userte_exitto process whatever therte_errnovalue. Only return 1 means use the origin sigbus handler that will care about the errno.With the changes above, the check should be something like:
if (bus == NULL) return 1; else if (rte_errno != 0) return -rte_errno; rte_errno = old_errno; return 0;
Quoted 8 lines
Which would avoid resetting
rte_errnoon top of whichever value a dev would have used, and having it set to a negative non-errno value.(Please do not just use this as-is, if you think this is not a good idea just tell us why or how you would prefer to do it. I'm only proposing a way that I think would work.)
Regards,
i think that is the problem to find a better way, i agree to maximum to keep the rte_errno should be make sense.
PATCH v7 6/7 eal: add failure handle mechanism for hotplug
In reply to Jeff GuoThis patch introduces a failure handle mechanism to handle device hotplug removal event.
First it can register sigbus handler when enable device event monitor. Once sigbus error be captured, it will check the failure address and accordingly remap the invalid memory for the corresponding device. Besed on this mechanism, it could guaranty the application not crash when the device be hotplug out.
---v7->v6:
delete some unused part.
--- lib/librte_eal/linuxapp/eal/eal_dev.c | 112 +++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-)
lib/librte_eal/linuxapp/eal/eal_dev.c +111 -1
@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h>
@@ -14,6 +16,10 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h> +#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -23,6 +29,16 @@ static bool monitor_started;#define EAL_UEV_MSG_LEN 4096#define EAL_UEV_MSG_ELEM_LEN 128 +/* +* spinlock for device failure process, protect the bus and the device +* to avoid race condition. +*/ +static rte_spinlock_t dev_failure_lock =RTE_SPINLOCK_INITIALIZER ; + +static struct sigaction sigbus_action_old; + +static int sigbus_need_recover; + static void dev_uev_handler(__rte_unused void *param);/* identify the system layer which reports this event. */
@@ -33,6 +49,49 @@ enum eal_dev_event_subsystem {EAL_DEV_EVENT_SUBSYSTEM_MAX }; +static void +sigbus_action_recover (void ) +{ +if (sigbus_need_recover) { +sigaction (SIGBUS , &sigbus_action_old,NULL ); + sigbus_need_recover =0 ; + } +} + +static void sigbus_handler (int signum,siginfo_t *info, + void *ctx __rte_unused) +{ +int ret; + +RTE_LOG (INFO ,EAL ,"Thread[%d] catch SIGBUS, fault address:%p\n" , + (int )pthread_self (), info->si_addr); + +rte_spinlock_lock (&dev_failure_lock); + ret =rte_bus_sigbus_handler (info->si_addr); +rte_spinlock_unlock (&dev_failure_lock); +if (ret ==-1 ) { +rte_exit (EXIT_FAILURE , +"Failed to handle SIGBUS for hotplug, " +"(rte_errno: %s)!" ,strerror (rte_errno)); + }else if (ret ==1 ) { +if (sigbus_action_old.sa_handler) + (*(sigbus_action_old.sa_handler))(signum); +else +rte_exit (EXIT_FAILURE , +"Failed to handle generic SIGBUS!" ); + } + +RTE_LOG (INFO ,EAL ,"Success to handle SIGBUS for hotplug!\n" ); +} + +static int cmp_dev_name (const struct rte_device *dev, +const void *_name) +{ +const char *name = _name; + +return strcmp (dev->name, name); +} +static int dev_uev_socket_fd_create (void ) {
@@ -147,6 +206,9 @@ dev_uev_handler(__rte_unused void *param)struct rte_dev_event uevent;int ret;char buf[EAL_UEV_MSG_LEN ]; +struct rte_bus *bus; +struct rte_device *dev; +const char *busname ="" ;memset (&uevent,0 ,sizeof (struct rte_dev_event ));memset (buf,0 ,EAL_UEV_MSG_LEN );
@@ -171,13 +233,50 @@ dev_uev_handler(__rte_unused void *param)RTE_LOG (DEBUG ,EAL ,"receive uevent(name:%s, type:%d, subsystem:%d)\n" , uevent.devname, uevent.type, uevent.subsystem); -if (uevent.devname) +switch (uevent.subsystem) { +case EAL_DEV_EVENT_SUBSYSTEM_PCI : +case EAL_DEV_EVENT_SUBSYSTEM_UIO : + busname ="pci" ; +break ; +default : +break ; + } + +if (uevent.devname) { +if (uevent.type ==RTE_DEV_EVENT_REMOVE ) { +rte_spinlock_lock (&dev_failure_lock); + bus =rte_bus_find_by_name (busname); +if (bus ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find bus (%s)\n" , + busname); +return ; + } + + dev = bus->find_device (NULL , cmp_dev_name, + uevent.devname); +if (dev ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find device (%s) on " +"bus (%s)\n" , uevent.devname, busname); +return ; + } + + ret = bus->hotplug_failure_handler (dev); +rte_spinlock_unlock (&dev_failure_lock); +if (ret) { +RTE_LOG (ERR ,EAL ,"Can not handle hotplug for " +"device (%s)\n" , dev->name); +return ; + } + }dev_callback_process (uevent.devname, uevent.type); + } } int __rte_experimentalrte_dev_event_monitor_start (void) { +sigset_t mask; +struct sigaction action;int ret;if (monitor_started)
@@ -197,6 +296,14 @@ rte_dev_event_monitor_start(void)return -1 ; } +/* register sigbus handler */ +sigemptyset (&mask); +sigaddset (&mask,SIGBUS ); + action.sa_flags =SA_SIGINFO ; + action.sa_mask = mask; + action.sa_sigaction = sigbus_handler; + sigbus_need_recover = !sigaction (SIGBUS , &action, &sigbus_action_old); + monitor_started = true;return 0 ;
@@ -217,8 +324,11 @@ rte_dev_event_monitor_stop(void)return ret; } +sigbus_action_recover (); +close (intr_handle.fd); intr_handle.fd =-1 ; monitor_started = false; +return 0 ; }
Signature · 1 line
2.7.4
Re: [PATCH v7 6/7] eal: add failure handle mechanism for hotplug
Quoted 43 lines · Jeff Guo
This patch introduces a failure handle mechanism to handle device hotplug removal event.
First it can register sigbus handler when enable device event monitor. Once sigbus error be captured, it will check the failure address and accordingly remap the invalid memory for the corresponding device. Besed on this mechanism, it could guaranty the application not crash when the device be hotplug out.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v7->v6:
delete some unused part.--- lib/librte_eal/linuxapp/eal/eal_dev.c | 112 +++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-)lib/librte_eal/linuxapp/eal/eal_dev.c +6
-0@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h> @@ -14,6 +16,10 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h> +#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h"
lib/librte_eal/linuxapp/eal/eal_dev.c static bool monitor_started; · line 36
Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +5
-0@@ -23,6 +29,16 @@ static bool monitor_started;#define EAL_UEV_MSG_LEN 4096#define EAL_UEV_MSG_ELEM_LEN 128 +/* +* spinlock for device failure process, protect the bus and the device +* to avoid race condition. +*/ +static rte_spinlock_t dev_failure_lock =RTE_SPINLOCK_INITIALIZER ;
Sorry, it is still too vague why the lock is required. It is just generic words. Please, add details and describe circumstance when it is required.
Re: [PATCH v7 6/7] eal: add failure handle mechanism for hotplug
Quoted 44 lines · Andrew Rybchenko
Quoted 43 lines · Jeff Guo
This patch introduces a failure handle mechanism to handle device hotplug removal event.
First it can register sigbus handler when enable device event monitor. Once sigbus error be captured, it will check the failure address and accordingly remap the invalid memory for the corresponding device. Besed on this mechanism, it could guaranty the application not crash when the device be hotplug out.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v7->v6:
delete some unused part.--- lib/librte_eal/linuxapp/eal/eal_dev.c | 112 +++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-)lib/librte_eal/linuxapp/eal/eal_dev.c +2
-0@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h> @@ -14,6 +16,10 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h>lib/librte_eal/linuxapp/eal/eal_dev.c +4
-0+#include <rte_bus.h> +#include <rte_eal.h> +#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h" @@ -23,6 +29,16 @@ static bool monitor_started;#define EAL_UEV_MSG_LEN 4096#define EAL_UEV_MSG_ELEM_LEN 128 +/*
Quoted 8 lines
Quoted 4 lines
+ * spinlock for device failure process, protect the bus and the device
+ * to avoid race condition.
+ */
+staticrte_spinlock_tdev_failure_lock=RTE_SPINLOCK_INITIALIZER;Sorry, it is still too vague why the lock is required. It is just generic words. Please, add details and describe circumstance when it is required.
let me check if i can do more for that.
PATCH v7 7/7 igb_uio: fix uio release issue when hot unplug
In reply to Jeff GuoWhen hotplug out device, the kernel will release the device resource in the kernel side, such as the fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash. On the other hand, something like interrupt disabling do not automatically process in kernel side. If not handler it, this redundancy and dirty thing will affect the interrupt resource be used by other device. So the igb_uio driver have to check the hotplug status, and the corresponding process should be taken in igb uio driver.
This patch propose to add structure of rte_udev_state into rte_uio_pci_dev of igb_uio kernel driver, which will record the state of uio device, such as probed/opened/released/removed/unplug. When detect the unexpected removal which cause of hotplug out behavior, it will corresponding disable interrupt resource, while for the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer kernel crash issue.
--- kernel/linux/igb_uio/igb_uio.c | 51 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-)
kernel/linux/igb_uio/igb_uio.c +48 -3
@@ -19,6 +19,15 @@#include "compat.h" +/* uio pci device state */ +enum rte_udev_state { +RTE_UDEV_PROBED , +RTE_UDEV_OPENNED , +RTE_UDEV_RELEASED , +RTE_UDEV_REMOVED , +RTE_UDEV_UNPLUG +}; +/** * A structure describing the private information for a uio device. */
@@ -28,6 +37,7 @@ struct rte_uio_pci_dev {enum rte_intr_mode mode;struct mutex lock;int refcnt; +enum rte_udev_state state; };static int wc_activate;
@@ -195,12 +205,22 @@ igbuio_pci_irqhandler(int irq, void *dev_id) {struct rte_uio_pci_dev *udev = (struct rte_uio_pci_dev *)dev_id;struct uio_info *info = &udev->info; +struct pci_dev *pdev = udev->pdev;/* Legacy mode need to mask in hardware */ if (udev->mode ==RTE_INTR_MODE_LEGACY && !pci_check_and_mask_intx (udev->pdev))return IRQ_NONE ; +mutex_lock (&udev->lock); +/* check the uevent of the kobj */ +if ((&pdev->dev.kobj)->state_remove_uevent_sent ==1 ) { +dev_notice (&pdev->dev,"device:%s, sent remove uevent!\n" , + (&pdev->dev.kobj)->name); + udev->state =RTE_UDEV_UNPLUG ; + } +mutex_unlock (&udev->lock); +uio_event_notify (info);/* Message signal mode, no share IRQ and automasked */
@@ -309,7 +329,6 @@ igbuio_pci_disable_interrupts(struct rte_uio_pci_dev *udev)#endif } -/** * This gets called while opening uio device file. */
@@ -331,20 +350,29 @@ igbuio_pci_open(struct uio_info *info, struct inode *inode)/* enable interrupts */ err =igbuio_pci_enable_interrupts (udev); -mutex_unlock (&udev->lock);if (err) {dev_err (&dev->dev,"Enable interrupt fails\n" ); +pci_clear_master (dev); +mutex_unlock (&udev->lock);return err; } + udev->state =RTE_UDEV_OPENNED ; +mutex_unlock (&udev->lock);return 0 ; } +/** +* This gets called while closing uio device file. +*/ static int igbuio_pci_release (struct uio_info *info,struct inode *inode) {struct rte_uio_pci_dev *udev = info->priv;struct pci_dev *dev = udev->pdev; +if (udev->state ==RTE_UDEV_REMOVED ) +return 0 ; +mutex_lock (&udev->lock);if (--udev->refcnt >0 ) {mutex_unlock (&udev->lock);
@@ -356,7 +384,7 @@ igbuio_pci_release(struct uio_info *info, struct inode *inode)/* stop the device from further DMA */ pci_clear_master (dev); - + udev->state =RTE_UDEV_RELEASED ;mutex_unlock (&udev->lock);return 0 ; }
@@ -562,6 +590,9 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) (unsigned long long)map_dma_addr, map_addr); } +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_PROBED ; +mutex_unlock (&udev->lock);return 0 ; fail_remove_group:
@@ -579,6 +610,20 @@ static void igbuio_pci_remove(struct pci_dev *dev) {struct rte_uio_pci_dev *udev =pci_get_drvdata (dev); +int ret; + +/* handler hot unplug */ +if (udev->state ==RTE_UDEV_OPENNED || + udev->state ==RTE_UDEV_UNPLUG ) { +dev_notice (&dev->dev,"Unexpected removal!\n" ); + ret =igbuio_pci_release (&udev->info,NULL ); +if (ret) +return ; +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_REMOVED ; +mutex_unlock (&udev->lock); +return ; + }mutex_destroy (&udev->lock);sysfs_remove_group (&dev->dev.kobj, &dev_attr_grp);
Signature · 1 line
2.7.4
Re: [PATCH v7 7/7] igb_uio: fix uio release issue when hot unplug
Quoted 9 lines · On Mon, 9 Jul 2018 20:01:06 +0800 Jeff Guo <[email protected]> wrote:
kernel/linux/igb_uio/igb_uio.c +1
-0@@ -195,12 +205,22 @@ igbuio_pci_irqhandler(int irq, void *dev_id) {struct rte_uio_pci_dev *udev = (struct rte_uio_pci_dev *)dev_id;struct uio_info *info = &udev->info; +struct pci_dev *pdev = udev->pdev;/* Legacy mode need to mask in hardware */ if (udev->mode ==RTE_INTR_MODE_LEGACY && !pci_check_and_mask_intx (udev->pdev))
kernel/linux/igb_uio/igb_uio.c igbuio_pci_irqhandler(int irq, void *dev_id) · line 222
Quoted 10 lines
kernel/linux/igb_uio/igb_uio.c +8
-0return IRQ_NONE ; +mutex_lock (&udev->lock); +/* check the uevent of the kobj */ +if ((&pdev->dev.kobj)->state_remove_uevent_sent ==1 ) { +dev_notice (&pdev->dev,"device:%s, sent remove uevent!\n" , + (&pdev->dev.kobj)->name); + udev->state =RTE_UDEV_UNPLUG ; + } +mutex_unlock (&udev->lock);
Did you run with lockdep?
I don't think you can safely acquire a mutex (would sleep) in IRQ context.
Re: [PATCH v7 7/7] igb_uio: fix uio release issue when hot unplug
hi, stephen
Thanks for your review.
Quoted 14 lines · Stephen Hemminger
Quoted 11 lines · On Mon, 9 Jul 2018 20:01:06 +0800 Jeff Guo <[email protected]> wrote:
kernel/linux/igb_uio/igb_uio.c +1
-0@@ -195,12 +205,22 @@ igbuio_pci_irqhandler(int irq, void *dev_id) {struct rte_uio_pci_dev *udev = (struct rte_uio_pci_dev *)dev_id;struct uio_info *info = &udev->info; +struct pci_dev *pdev = udev->pdev;/* Legacy mode need to mask in hardware */ if (udev->mode ==RTE_INTR_MODE_LEGACY && !pci_check_and_mask_intx (udev->pdev))return IRQ_NONE ;
Quoted 10 lines
kernel/linux/igb_uio/igb_uio.c igbuio_pci_irqhandler(int irq, void *dev_id) · line 222
Quoted 8 lines
kernel/linux/igb_uio/igb_uio.c +8
-0+mutex_lock (&udev->lock); +/* check the uevent of the kobj */ +if ((&pdev->dev.kobj)->state_remove_uevent_sent ==1 ) { +dev_notice (&pdev->dev,"device:%s, sent remove uevent!\n" , + (&pdev->dev.kobj)->name); + udev->state =RTE_UDEV_UNPLUG ; + } +mutex_unlock (&udev->lock);Did you run with lockdep?
I don't think you can safely acquire a mutex (would sleep) in IRQ context.
I think lockdep could do me a favor about that, but i think only the uio remove function will check the unplug status, so i think i could let this check in the uio remove function, no need to let it in irq handler anymore, since like what you said acquire a mutex in IRQ context is no safe.
v8 cover hotplug failure handle mechanism
In reply to Jeff GuoAs we know, hot plug is an importance feature, either use for the datacenter device’s fail-safe, or use for SRIOV Live Migration in SDN/NFV. It could bring the higher flexibility and continuality to the networking services in multiple use cases in industry. So let we see, dpdk as an importance networking framework, what can it help to implement hot plug solution for users.
We already have a general device event detect mechanism, failsafe driver, bonding driver and hot plug/unplug api in framework, app could use these to develop their hot plug solution.
let’s see the case of hot unplug, it can happen when a hardware device is be removed physically, or when the software disables it. App need to call ether dev API to detach the device, to unplug the device at the bus level and make access to the device invalid. But the problem is that, the removal of the device from the software lists is not going to be instantaneous, at this time if the data(fast) path still read/write the device, it will cause MMIO error and result of the app crash out.
Seems that we have got fail-safe driver(or app) + RTE_ETH_EVENT_INTR_RMV + kernel core driver solution to handle it, but still not have failsafe driver (or app) + RTE_DEV_EVENT_REMOVE + PCIe pmd driver failure handle solution. So there is an absence in dpdk hot plug solution right now.
Also, we know that kernel only guaranty hot plug on the kernel side, but not for the user mode side. Firstly we can hardly have a gatekeeper for any MMIO for multiple PMD driver. Secondly, no more specific 3rd tools such as udev/driverctl have especially cover these hot plug failure processing. Third, the feasibility of app’s implement for multiple user mode PMD driver is still a problem. Here, a general hot plug failure handle mechanism in dpdk framework would be proposed, it aim to guaranty that, when hot unplug occur, the system will not crash and app will not be break out, and user space can normally stop and release any relevant resources, then unplug of the device at the bus level cleanly.
The mechanism should be come across as bellow:
Firstly, app enabled the device event monitor and register the hot plug event’s callback before running data path. Once the hot unplug behave occur, the mechanism will detect the removal event and then accordingly do the failure handle. In order to do that, below functional will be bring in.
- Add a new bus ops “handle_hot_unplug” to handle bus read/write error, it is bus-specific and each kind of bus can implement its own logic.
- Implement pci bus specific ops “pci_handle_hot_unplug”. It will base on the failure address to remap memory for the corresponding device that unplugged.
For the data path or other unexpected control from the control path when hot unplug occur.
- Implement a new sigbus handler, it is registered when start device even monitoring. The handler is per process. Base on the signal event principle, control path thread and data path thread will randomly receive the sigbus error, but will go to the common sigbus handler. Once the MMIO sigbus error exposure, it will trigger the above hot unplug operation. The sigbus will be check if it is cause of the hot unplug or not, if not will info exception as the original sigbus handler. If yes, will do memory remapping.
For the control path and the igb uio release:
- When hot unplug device, the kernel will release the device resource in the kernel side, such as the fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash.
On the other hand, something like interrupt disable do not automatically process in kernel side. If not handler it, this redundancy and dirty thing will affect the interrupt resource be used by other device.
So the igb_uio driver have to check the hot plug status and corresponding process should be taken in igb uio deriver.
This patch propose to add structure of rte_udev_state into rte_uio_pci_dev of igb_uio kernel driver, which will record the state of uio device, such as probed/opened/released/removed/unplug. When detect the unexpected removal which cause of hot unplug behavior, it will corresponding disable interrupt resource, while for the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer kernel crash issue.
The mechanism could be use for fail-safe driver and app which want to use hot plug solution. let testpmd for example:
- Enable device event monitor->device unplug->failure handle->stop forwarding-> stop port->close port->detach port.
This process will not breaking the app/fail-safe running, and will not break other irrelevance device. And app could plug in the device and restart the date path again by below.
- Device plug in->bind igb_uio driver ->attached device->start port-> start forwarding.
patchset history:v8->v7:
refine errno process in sigbus handler.
refine igb uio release process
v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name
refine the sigbus handle logic
add lock for udev state in igb uio driver
v4->v3:
split patches to be small and clear
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug
v3->v2:
change bus ops name to bus_hotplug_handler. add new API and bus ops of bus_signal_handler distingush handle generic sigbus and hotplug sigbus
v2->v1(v21):
refine some doc and commit log
fix igb uio kernel issue for control path failure
rebase testpmd code
Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:v21->v20:
split function in hot unplug ops
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code
refine the remap logic for multiple device.
remove the auto binding
v19->18:
note for limitation of multiple hotplug,fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.
the prior patch history please check the patch set "add device event monitor framework".
Jeff Guo (7):
bus: add hotplug failure handler
bus/pci: implement hotplug failure handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler operation
bus: add helper to handle sigbus
eal: add failure handle mechanism for hotplug
igb_uio: fix uio release issue for hotplug
drivers/bus/pci/pci_common.c | 77 ++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 ++++++++++ drivers/bus/pci/private.h | 12 ++++ kernel/linux/igb_uio/igb_uio.c | 69 +++++++++++++++---- lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++ lib/librte_eal/common/include/rte_bus.h | 33 ++++++++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 113 +++++++++++++++++++++++++++++++- 8 files changed, 377 insertions(+), 15 deletions(-)
Signature · 1 line
2.7.4
PATCH v8 1/7 bus: add hotplug failure handler
When device be hotplug out, if app still continue to access device by mmio, it will cause of memory failure and result the system crash.
This patch introduces a bus ops to handle device hotplug failure, it is a bus specific behavior, so each kind of bus can implement its own logic case by case.
---v8->v7:
no change
--- lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);typedef int (*rte_bus_parse_t )(const char *name,void *addr);/** +* Implementation a specific hotplug failure handler, which is responsible +* for handle the failure when the device be hotplug out from the bus. When +* hotplug removal event be detected, it could call this function to handle +* failure and guaranty the system would not crash in the case. +* @param dev +* Pointer of the device structure. +* +* @return +* 0 on success. +* !0 on error. +*/ +typedef int (*rte_bus_hotplug_failure_handler_t )(struct rte_device *dev); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -211,6 +225,8 @@ struct rte_bus {rte_bus_parse_t parse;/**< Parse a device name */ struct rte_bus_conf conf;/**< Bus configuration */ rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ +rte_bus_hotplug_failure_handler_t hotplug_failure_handler; +/**< handle hotplug failure on bus */ };/**
Signature · 1 line
2.7.4
PATCH v8 2/7 bus/pci: implement hotplug failure handler ops
In reply to Jeff GuoThis patch implements the ops of hotplug failure handler for PCI bus, it is functional to remap a new dummy memory which overlap to the failure memory to avoid MMIO read/write error.
---v8->v7:
no change
--- drivers/bus/pci/pci_common.c | 28 ++++++++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++ drivers/bus/pci/private.h | 12 ++++++++++++ 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
@@ -408,6 +408,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, }static int +pci_hotplug_failure_handler (struct rte_device *dev) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =RTE_DEV_TO_PCI (dev); +if (!pdev) +return -1 ; + +switch (pdev->kdrv) { +case RTE_KDRV_IGB_UIO : +case RTE_KDRV_UIO_GENERIC : +case RTE_KDRV_NIC_UIO : +/* mmio resource is invalid, remap it to be safe. */ + ret =pci_uio_remap_resource (pdev); +break ; +default : +RTE_LOG (DEBUG ,EAL , +"Not managed by a supported kernel driver, skipped\n" ); + ret =-1 ; +break ; + } + +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -437,6 +464,7 @@ struct rte_pci_bus rte_pci_bus = { .unplug = pci_unplug, .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, + .hotplug_failure_handler = pci_hotplug_failure_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res) } } +/* remap the PCI resource of a PCI device in anonymous virtual memory */ +int +pci_uio_remap_resource (struct rte_pci_device *dev) +{ +int i; +void *map_address; + +if (dev ==NULL ) +return -1 ; + +/* Remap all BARs */ +for (i =0 ; i !=PCI_MAX_RESOURCE ; i++) { +/* skip empty BAR */ +if (dev->mem_resource[i].phys_addr ==0 ) +continue ; + map_address =mmap (dev->mem_resource[i].addr, + (size_t )dev->mem_resource[i].len, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ); +if (map_address ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL , +"Cannot remap resource for device %s\n" , + dev->name); +return -1 ; + } +RTE_LOG (INFO ,EAL , +"Successful remap resource for device %s\n" , + dev->name); + } + +return 0 ; +} +static struct mapped_pci_resource *pci_uio_find_resource (struct rte_pci_device *dev) {
drivers/bus/pci/private.h +12 -0
@@ -123,6 +123,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev, struct mapped_pci_resource *uio_res);/** +* Remap the PCI resource of a PCI device in anonymous virtual memory. +* +* @param dev +* Point to the struct rte pci device. +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +pci_uio_remap_resource (struct rte_pci_device *dev); + +/** * Map device memory to uio resource * * This function is private to EAL.
Signature · 1 line
2.7.4
PATCH v8 3/7 bus: add sigbus handler
In reply to Jeff GuoWhen device be hotplug out, if data path still read/write device, the sigbus error will occur, this error need to be handled. So a handler need to be here to capture the signal and handle it correspondingly.
This patch introduces a bus ops to handle sigbus error, it is a bus specific behavior, so that each kind of bus can implement its own logic case by case.
---v8->v7:
no change
--- lib/librte_eal/common/include/rte_bus.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
lib/librte_eal/common/include/rte_bus.h +17 -0
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);typedef int (*rte_bus_hotplug_failure_handler_t )(struct rte_device *dev);/** +* Implementation a specific sigbus handler, which is responsible for handle +* the sigbus error which is either original memory error, or specific memory +* error that caused of hot unplug. When sigbus error be captured, it could +* call this function to handle sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 for success handle the sigbus. +* 1 for no bus handle the sigbus. +* -1 for failed to handle the sigbus +*/ +typedef int (*rte_bus_sigbus_handler_t )(const void *failure_addr); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -227,6 +242,8 @@ struct rte_bus {rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ rte_bus_hotplug_failure_handler_t hotplug_failure_handler;/**< handle hotplug failure on bus */ +rte_bus_sigbus_handler_t sigbus_handler;/**< handle sigbus error */ + };/**
Signature · 1 line
2.7.4
PATCH v8 4/7 bus/pci: implement sigbus handler operation
In reply to Jeff GuoThis patch implements the ops of sigbus handler for PCI bus, it is functional to find the corresponding pci device which is been hotplug out, and then call the bus ops of hotplug failure handler to handle the failure for the device.
---v8->v7:
no change
--- drivers/bus/pci/pci_common.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
drivers/bus/pci/pci_common.c +49 -0
@@ -407,6 +407,32 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,return NULL ; } +/* check the failure address belongs to which device. */ +static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int i; + +FOREACH_DEVICE_ON_PCIBUS (pdev) { +for (i =0 ; i !=RTE_DIM (pdev->mem_resource); i++) { +if ((uint64_t )(uintptr_t )failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t )(uintptr_t )failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) { +RTE_LOG (INFO ,EAL ,"Failure address " +"%16.16" PRIx64" belongs to " +"device %s!\n" , + (uint64_t )(uintptr_t )failure_addr, + pdev->device.name); +return pdev; + } + } + } +return NULL ; +} +static int pci_hotplug_failure_handler (struct rte_device *dev) {
@@ -435,6 +461,28 @@ pci_hotplug_failure_handler(struct rte_device *dev) }static int +pci_sigbus_handler (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =pci_find_device_by_addr (failure_addr); +if (!pdev) { +/* It is a generic sigbus error, no bus would handle it. */ + ret =1 ; + }else { +/* The sigbus error is caused of hot removal. */ + ret =pci_hotplug_failure_handler (&pdev->device); +if (ret) { +RTE_LOG (ERR ,EAL ,"Failed to handle hot plug for " +"device %s" , pdev->name); + ret =-1 ; + } + } +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -465,6 +513,7 @@ struct rte_pci_bus rte_pci_bus = { .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, .hotplug_failure_handler = pci_hotplug_failure_handler, + .sigbus_handler = pci_sigbus_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
PATCH v8 5/7 bus: add helper to handle sigbus
In reply to Jeff GuoThis patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
---v8->v7:
refine errno process in sigbus handler
--- lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++++++++ 2 files changed, 55 insertions(+)
lib/librte_eal/common/eal_common_bus.c +43 -0
@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) +return -1 ; + + ret = bus->sigbus_handler (failure_addr); + +/* find bus but handle failed, keep the errno be set. */ +if (ret <0 && rte_errno ==0 ) + rte_errno =ENOTSUP ; + +return ret >0 ; +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* can not find bus. */ +if (!bus) +return 1 ; +/* find bus but handle failed, pass on the new errno. */ +else if (rte_errno !=0 ) +return -1 ; + +/* restore the old errno. */ + rte_errno = old_errno; + +return ret; +}
lib/librte_eal/common/eal_private.h +12 -0
@@ -258,4 +258,16 @@ int rte_mp_channel_init(void);*/ void dev_callback_process (char *device_name,enum rte_dev_event_type event); +/** +* Iterate all buses to find the corresponding bus, to handle the sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
PATCH v8 6/7 eal: add failure handle mechanism for hotplug
In reply to Jeff GuoThis patch introduces a failure handle mechanism to handle device hotplug removal event.
First it can register sigbus handler when enable device event monitor. Once sigbus error be captured, it will check the failure address and accordingly remap the invalid memory for the corresponding device. Besed on this mechanism, it could guaranty the application not crash when the device be hotplug out.
---v8->v7:
refine sigbus handle
--- lib/librte_eal/linuxapp/eal/eal_dev.c | 113 +++++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 1 deletion(-)
lib/librte_eal/linuxapp/eal/eal_dev.c +112 -1
@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h>
@@ -14,6 +16,10 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h> +#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -23,6 +29,17 @@ static bool monitor_started;#define EAL_UEV_MSG_LEN 4096#define EAL_UEV_MSG_ELEM_LEN 128 +/* +* spinlock for device failure handle, if try to access bus or device, +* such as handle sigbus on bus or handle memory failure for device just use +* this lock. It could protect the bus and the device to avoid race condition. +*/ +static rte_spinlock_t failure_handle_lock =RTE_SPINLOCK_INITIALIZER ; + +static struct sigaction sigbus_action_old; + +static int sigbus_need_recover; + static void dev_uev_handler(__rte_unused void *param);/* identify the system layer which reports this event. */
@@ -33,6 +50,49 @@ enum eal_dev_event_subsystem {EAL_DEV_EVENT_SUBSYSTEM_MAX }; +static void +sigbus_action_recover (void ) +{ +if (sigbus_need_recover) { +sigaction (SIGBUS , &sigbus_action_old,NULL ); + sigbus_need_recover =0 ; + } +} + +static void sigbus_handler (int signum,siginfo_t *info, + void *ctx __rte_unused) +{ +int ret; + +RTE_LOG (INFO ,EAL ,"Thread[%d] catch SIGBUS, fault address:%p\n" , + (int )pthread_self (), info->si_addr); + +rte_spinlock_lock (&failure_handle_lock); + ret =rte_bus_sigbus_handler (info->si_addr); +rte_spinlock_unlock (&failure_handle_lock); +if (ret ==-1 ) { +rte_exit (EXIT_FAILURE , +"Failed to handle SIGBUS for hotplug, " +"(rte_errno: %s)!" ,strerror (rte_errno)); + }else if (ret ==1 ) { +if (sigbus_action_old.sa_handler) + (*(sigbus_action_old.sa_handler))(signum); +else +rte_exit (EXIT_FAILURE , +"Failed to handle generic SIGBUS!" ); + } + +RTE_LOG (INFO ,EAL ,"Success to handle SIGBUS for hotplug!\n" ); +} + +static int cmp_dev_name (const struct rte_device *dev, +const void *_name) +{ +const char *name = _name; + +return strcmp (dev->name, name); +} +static int dev_uev_socket_fd_create (void ) {
@@ -147,6 +207,9 @@ dev_uev_handler(__rte_unused void *param)struct rte_dev_event uevent;int ret;char buf[EAL_UEV_MSG_LEN ]; +struct rte_bus *bus; +struct rte_device *dev; +const char *busname ="" ;memset (&uevent,0 ,sizeof (struct rte_dev_event ));memset (buf,0 ,EAL_UEV_MSG_LEN );
@@ -171,13 +234,50 @@ dev_uev_handler(__rte_unused void *param)RTE_LOG (DEBUG ,EAL ,"receive uevent(name:%s, type:%d, subsystem:%d)\n" , uevent.devname, uevent.type, uevent.subsystem); -if (uevent.devname) +switch (uevent.subsystem) { +case EAL_DEV_EVENT_SUBSYSTEM_PCI : +case EAL_DEV_EVENT_SUBSYSTEM_UIO : + busname ="pci" ; +break ; +default : +break ; + } + +if (uevent.devname) { +if (uevent.type ==RTE_DEV_EVENT_REMOVE ) { +rte_spinlock_lock (&failure_handle_lock); + bus =rte_bus_find_by_name (busname); +if (bus ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find bus (%s)\n" , + busname); +return ; + } + + dev = bus->find_device (NULL , cmp_dev_name, + uevent.devname); +if (dev ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find device (%s) on " +"bus (%s)\n" , uevent.devname, busname); +return ; + } + + ret = bus->hotplug_failure_handler (dev); +rte_spinlock_unlock (&failure_handle_lock); +if (ret) { +RTE_LOG (ERR ,EAL ,"Can not handle hotplug for " +"device (%s)\n" , dev->name); +return ; + } + }dev_callback_process (uevent.devname, uevent.type); + } } int __rte_experimentalrte_dev_event_monitor_start (void) { +sigset_t mask; +struct sigaction action;int ret;if (monitor_started)
@@ -197,6 +297,14 @@ rte_dev_event_monitor_start(void)return -1 ; } +/* register sigbus handler */ +sigemptyset (&mask); +sigaddset (&mask,SIGBUS ); + action.sa_flags =SA_SIGINFO ; + action.sa_mask = mask; + action.sa_sigaction = sigbus_handler; + sigbus_need_recover = !sigaction (SIGBUS , &action, &sigbus_action_old); + monitor_started = true;return 0 ;
@@ -217,8 +325,11 @@ rte_dev_event_monitor_stop(void)return ret; } +sigbus_action_recover (); +close (intr_handle.fd); intr_handle.fd =-1 ; monitor_started = false; +return 0 ; }
Signature · 1 line
2.7.4
PATCH v8 7/7 igb_uio: fix uio release issue for hotplug
In reply to Jeff GuoWhen hotplug out device, the device resource will be released in kernel. The fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash. On the other hand, interrupt disabling do not automatically be processed in kernel. If not handle it, this redundancy and dirty thing will affect the interrupt resource be used by other device. So the igb_uio driver have to check the hotplug status, and the corresponding process should be taken in igb uio driver.
This patch propose to add enum rte_udev_state into struct rte_uio_pci_dev of igb uio driver, which will record the state of uio device, such as probed/opened/released/removed. When detect the unexpected removal which cause of hotplug out behavior, it will corresponding disable interrupt resource. For the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer crash issue.
---v8->v7:
change enum of udev state, refine code to release udev resource
--- kernel/linux/igb_uio/igb_uio.c | 69 +++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 14 deletions(-)
kernel/linux/igb_uio/igb_uio.c +55 -14
@@ -19,6 +19,14 @@#include "compat.h" +/* uio pci device state */ +enum rte_udev_state { +RTE_UDEV_PROBED , +RTE_UDEV_OPENNED , +RTE_UDEV_RELEASED , +RTE_UDEV_REMOVED , +}; +/** * A structure describing the private information for a uio device. */
@@ -28,6 +36,7 @@ struct rte_uio_pci_dev {enum rte_intr_mode mode;struct mutex lock;int refcnt; +enum rte_udev_state state; };static int wc_activate;
@@ -309,6 +318,17 @@ igbuio_pci_disable_interrupts(struct rte_uio_pci_dev *udev)#endif } +/* Unmap previously ioremap'd resources */ +static void +igbuio_pci_release_iomem (struct uio_info *info) +{ +int i; + +for (i =0 ; i <MAX_UIO_MAPS ; i++) { +if (info->mem[i].internal_addr) +iounmap (info->mem[i].internal_addr); + } +}/** * This gets called while opening uio device file.
@@ -331,20 +351,35 @@ igbuio_pci_open(struct uio_info *info, struct inode *inode)/* enable interrupts */ err =igbuio_pci_enable_interrupts (udev); -mutex_unlock (&udev->lock);if (err) {dev_err (&dev->dev,"Enable interrupt fails\n" ); +pci_clear_master (dev); +mutex_unlock (&udev->lock);return err; } + udev->state =RTE_UDEV_OPENNED ; +mutex_unlock (&udev->lock);return 0 ; } +/** +* This gets called while closing uio device file. +*/ static int igbuio_pci_release (struct uio_info *info,struct inode *inode) {struct rte_uio_pci_dev *udev = info->priv;struct pci_dev *dev = udev->pdev; +if (udev->state ==RTE_UDEV_REMOVED ) { +mutex_destroy (&udev->lock); +igbuio_pci_release_iomem (&udev->info); +pci_disable_device (dev); +pci_set_drvdata (dev,NULL ); +kfree (udev); +return 0 ; + } +mutex_lock (&udev->lock);if (--udev->refcnt >0 ) {mutex_unlock (&udev->lock);
@@ -356,7 +391,7 @@ igbuio_pci_release(struct uio_info *info, struct inode *inode)/* stop the device from further DMA */ pci_clear_master (dev); - + udev->state =RTE_UDEV_RELEASED ;mutex_unlock (&udev->lock);return 0 ; }
@@ -414,18 +449,6 @@ igbuio_pci_setup_ioport(struct pci_dev *dev, struct uio_info *info,return 0 ; } -/* Unmap previously ioremap'd resources */ -static void -igbuio_pci_release_iomem (struct uio_info *info) -{ -int i; - -for (i =0 ; i <MAX_UIO_MAPS ; i++) { -if (info->mem[i].internal_addr) -iounmap (info->mem[i].internal_addr); - } -} -static int igbuio_setup_bars (struct pci_dev *dev,struct uio_info *info) {
@@ -562,6 +585,9 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) (unsigned long long)map_dma_addr, map_addr); } +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_PROBED ; +mutex_unlock (&udev->lock);return 0 ; fail_remove_group:
@@ -579,6 +605,21 @@ static void igbuio_pci_remove(struct pci_dev *dev) {struct rte_uio_pci_dev *udev =pci_get_drvdata (dev); +struct pci_dev *pdev = udev->pdev; +int ret; + +/* handle unexpected removal */ +if (udev->state ==RTE_UDEV_OPENNED || + (&pdev->dev.kobj)->state_remove_uevent_sent ==1 ) { +dev_notice (&dev->dev,"Unexpected removal!\n" ); + ret =igbuio_pci_release (&udev->info,NULL ); +if (ret) +return ; +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_REMOVED ; +mutex_unlock (&udev->lock); +return ; + }mutex_destroy (&udev->lock);sysfs_remove_group (&dev->dev.kobj, &dev_attr_grp);
Signature · 1 line
2.7.4
Re: [PATCH v8 7/7] igb_uio: fix uio release issue for hotplug
kernel/linux/igb_uio/igb_uio.c line 29
Quoted 9 lines · On Tue, 10 Jul 2018 19:03:27 +0800 Jeff Guo <[email protected]> wrote:
kernel/linux/igb_uio/igb_uio.c +8
-0+/* uio pci device state */ +enum rte_udev_state { +RTE_UDEV_PROBED , +RTE_UDEV_OPENNED , +RTE_UDEV_RELEASED , +RTE_UDEV_REMOVED , +}; +
The states here are a little confusing. especially since pci_release seems to take different actions based on the state. And there is nothing preventing races between unexpected removal (PCI), and removing the device from being used by igb_uio.
Would it be possible to only use state variable from the kernel PCI layer where the value is consistent?
Also there is refcounting in PCI layer (and locking). Could that be used instead?
Re: [PATCH v8 7/7] igb_uio: fix uio release issue for hotplug
Quoted 4 lines · Stephen Hemminger
Quoted 1 line · On Tue, 10 Jul 2018 19:03:27 +0800 Jeff Guo <[email protected]> wrote:
Quoted 12 lines
kernel/linux/igb_uio/igb_uio.c line 29
Quoted 8 lines
kernel/linux/igb_uio/igb_uio.c +8
-0+/* uio pci device state */ +enum rte_udev_state { +RTE_UDEV_PROBED , +RTE_UDEV_OPENNED , +RTE_UDEV_RELEASED , +RTE_UDEV_REMOVED , +}; +The states here are a little confusing. especially since
pci_releaseseems to take different actions based on the state. And there is nothing preventing races between unexpected removal (PCI), and removing the device from being used byigb_uio.
The states here just manage in igb uio, and only restore the status of igb uio.
And only the RTE_UDEV_REMOVED that the key status might be a highlight and process, it is means pci have been removed, then directly come to igb uio remove without go igb uio release at first, the status is for hotplug out, need to do specific process. It will no affect the normal process, and for normal igb uio remove, udev be release, no any status need to restore.
Quoted 2 lines
Would it be possible to only use state variable from the kernel PCI layer where the value is consistent?
The state which i only care here is hot remove state, i check that kobj->state_remove_uevent_sent could be use in igb uio, except that still can not find a specific state of kernel pci to identify it. If we can find it, it should be best. what's other comment from guys?
Quoted 2 lines
Also there is refcounting in PCI layer (and locking). Could that be used instead?
It might be, but if state is enough here we might not considerate to use it. If i am missing anything, let me know.
Re: [PATCH v8 7/7] igb_uio: fix uio release issue for hotplug
In reply to Jeff GuoQuoted 9 lines · On Tue, 10 Jul 2018 19:03:27 +0800 Jeff Guo <[email protected]> wrote:
When hotplug out device, the device resource will be released in kernel. The fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash. On the other hand, interrupt disabling do not automatically be processed in kernel. If not handle it, this redundancy and dirty thing will affect the interrupt resource be used by other device. So the
igb_uiodriver have to check the hotplug status, and the corresponding process should be taken in igb uio driver.
Quoted 8 lines
This patch propose to add enum
rte_udev_stateinto structrte_uio_pci_devof igb uio driver, which will record the state of uio device, such as probed/opened/released/removed. When detect the unexpected removal which cause of hotplug out behavior, it will corresponding disable interrupt resource. For the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer crash issue.Signed-off-by Jeff Guo <[email protected]>
The PCI hotplug management is an important and potentially error prone error of DPDK.
I realize that English is not your native language, but the commit messages for this are hard to read. Perhaps you can get a volunteer or other person in the community to reword them. The commit logs and comments contain important information about the documentation of the code.
How does VFIO handle hotplug? We should direct all users to use VFIO since it is supported and secure. Igb uio has always been a slightly dangerous (as they say "running with scissors") way of accessing devices.
Re: [PATCH v8 7/7] igb_uio: fix uio release issue for hotplug
Quoted 19 lines · Stephen Hemminger
Quoted 16 lines · On Tue, 10 Jul 2018 19:03:27 +0800 Jeff Guo <[email protected]> wrote:
When hotplug out device, the device resource will be released in kernel. The fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash. On the other hand, interrupt disabling do not automatically be processed in kernel. If not handle it, this redundancy and dirty thing will affect the interrupt resource be used by other device. So the
igb_uiodriver have to check the hotplug status, and the corresponding process should be taken in igb uio driver.This patch propose to add enum
rte_udev_stateinto structrte_uio_pci_devof igb uio driver, which will record the state of uio device, such as probed/opened/released/removed. When detect the unexpected removal which cause of hotplug out behavior, it will corresponding disable interrupt resource. For the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer crash issue.
Quoted 8 lines
Quoted 1 line
Signed-off-by Jeff Guo <[email protected]>The PCI hotplug management is an important and potentially error prone error of DPDK.
I realize that English is not your native language, but the commit messages for this are hard to read. Perhaps you can get a volunteer or other person in the community to reword them. The commit logs and comments contain important information about the documentation of the code.
yes, i think that it might not be the whole thing to let you confused except something specific. But definitely it is my task to let reviewer most easily know what i want to propose before they will ack it, especial for some complex case. let's try my best for check my word. I am also planning to go to more native English country and great conference study, anyway to improve my word : )
Quoted 3 lines
How does VFIO handle hotplug? We should direct all users to use VFIO since it is supported and secure. Igb uio has always been a slightly dangerous (as they say "running with scissors") way of accessing devices.
You exposure VFIO here to replace igo uio, maybe we should check if it is optional or not. If we can fix all igb_uio issue it should be optional, if only vfio show stable we should go to vfio only. What other else guy comment here?
Re: [PATCH v8 7/7] igb_uio: fix uio release issue for hotplug
Quoted 34 lines · Jeff Guo
Quoted 19 lines · Stephen Hemminger
Quoted 16 lines · On Tue, 10 Jul 2018 19:03:27 +0800 Jeff Guo <[email protected]> wrote:
When hotplug out device, the device resource will be released in kernel. The fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash. On the other hand, interrupt disabling do not automatically be processed in kernel. If not handle it, this redundancy and dirty thing will affect the interrupt resource be used by other device. So the
igb_uiodriver have to check the hotplug status, and the corresponding process should be taken in igb uio driver.This patch propose to add enum
rte_udev_stateinto structrte_uio_pci_devof igb uio driver, which will record the state of uio device, such as probed/opened/released/removed. When detect the unexpected removal which cause of hotplug out behavior, it will corresponding disable interrupt resource. For the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer crash issue.Quoted 8 lines
Quoted 1 line
Signed-off-by Jeff Guo <[email protected]>The PCI hotplug management is an important and potentially error prone error of DPDK.
I realize that English is not your native language, but the commit messages for this are hard to read. Perhaps you can get a volunteer or other person in the community to reword them. The commit logs and comments contain important information about the documentation of the code.
yes, i think that it might not be the whole thing to let you confused except something specific. But definitely it is my task to let reviewer most easily know what i want to propose before they will ack it, especial for some complex case. let's try my best for check my word. I am also planning to go to more native English country and great conference study, anyway to improve my word : )
Quoted 7 lines
Quoted 3 lines
How does VFIO handle hotplug? We should direct all users to use VFIO since it is supported and secure. Igb uio has always been a slightly dangerous (as they say "running with scissors") way of accessing devices.
You exposure VFIO here to replace igo uio, maybe we should check if it is optional or not. If we can fix all
igb_uioissue it should be optional, if only vfio show stable we should go to vfio only. What other else guy comment here?
Plus, the pci vfio hotplug enabling is still on the next plan, since the different framework between vifo and uio for uevent process. Per vfio, when user space control it will holding the resource so the uevent will be blocked to sent out from kernel. Anyway that should be another story. We hope this patch set just fix hotplug failure issue for igb uio case.
v9 cover hotplug failure handle mechanism
In reply to Jeff GuoAs we know, hot plug is an importance feature, either use for the datacenter device’s fail-safe, or use for SRIOV Live Migration in SDN/NFV. It could bring the higher flexibility and continuality to the networking services in multiple use cases in industry. So let we see, dpdk as an importance networking framework, what can it help to implement hot plug solution for users.
We already have a general device event detect mechanism, failsafe driver, bonding driver and hot plug/unplug api in framework, app could use these to develop their hot plug solution.
let’s see the case of hot unplug, it can happen when a hardware device is be removed physically, or when the software disables it. App need to call ether dev API to detach the device, to unplug the device at the bus level and make access to the device invalid. But the problem is that, the removal of the device from the software lists is not going to be instantaneous, at this time if the data(fast) path still read/write the device, it will cause MMIO error and result of the app crash out.
Seems that we have got fail-safe driver(or app) + RTE_ETH_EVENT_INTR_RMV + kernel core driver solution to handle it, but still not have failsafe driver (or app) + RTE_DEV_EVENT_REMOVE + PCIe pmd driver failure handle solution. So there is an absence in dpdk hot plug solution right now.
Also, we know that kernel only guaranty hot plug on the kernel side, but not for the user mode side. Firstly we can hardly have a gatekeeper for any MMIO for multiple PMD driver. Secondly, no more specific 3rd tools such as udev/driverctl have especially cover these hot plug failure processing. Third, the feasibility of app’s implement for multiple user mode PMD driver is still a problem. Here, a general hot plug failure handle mechanism in dpdk framework would be proposed, it aim to guaranty that, when hot unplug occur, the system will not crash and app will not be break out, and user space can normally stop and release any relevant resources, then unplug of the device at the bus level cleanly.
The mechanism should be come across as bellow:
Firstly, app enabled the device event monitor and register the hot plug event’s callback before running data path. Once the hot unplug behave occur, the mechanism will detect the removal event and then accordingly do the failure handle. In order to do that, below functional will be bring in.
- Add a new bus ops “handle_hot_unplug” to handle bus read/write error, it is bus-specific and each kind of bus can implement its own logic.
- Implement pci bus specific ops “pci_handle_hot_unplug”. It will base on the failure address to remap memory for the corresponding device that unplugged.
For the data path or other unexpected control from the control path when hot unplug occur.
- Implement a new sigbus handler, it is registered when start device even monitoring. The handler is per process. Base on the signal event principle, control path thread and data path thread will randomly receive the sigbus error, but will go to the common sigbus handler. Once the MMIO sigbus error exposure, it will trigger the above hot unplug operation. The sigbus will be check if it is cause of the hot unplug or not, if not will info exception as the original sigbus handler. If yes, will do memory remapping.
For the control path and the igb uio release:
- When hot unplug device, the kernel will release the device resource in the kernel side, such as the fd sys file will disappear, and the irq will be released. At this time, if igb uio driver still try to release this resource, it will cause kernel crash.
On the other hand, something like interrupt disable do not automatically process in kernel side. If not handler it, this redundancy and dirty thing will affect the interrupt resource be used by other device.
So the igb_uio driver have to check the hot plug status and corresponding process should be taken in igb uio deriver.
This patch propose to add structure of rte_udev_state into rte_uio_pci_dev of igb_uio kernel driver, which will record the state of uio device, such as probed/opened/released/removed/unplug. When detect the unexpected removal which cause of hot unplug behavior, it will corresponding disable interrupt resource, while for the part of releasement which kernel have already handle, just skip it to avoid double free or null pointer kernel crash issue.
The mechanism could be use for fail-safe driver and app which want to use hot plug solution. let testpmd for example:
- Enable device event monitor->device unplug->failure handle->stop forwarding-> stop port->close port->detach port.
This process will not breaking the app/fail-safe running, and will not break other irrelevance device. And app could plug in the device and restart the date path again by below.
- Device plug in->bind igb_uio driver ->attached device->start port-> start forwarding.
patchset history:v9->v8:
refine commit log to be more readable.
v8->v7:
refine errno process in sigbus handler.
refine igb uio release process
v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name
refine the sigbus handle logic
add lock for udev state in igb uio driver
v4->v3:
split patches to be small and clear
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug
v3->v2:
change bus ops name to bus_hotplug_handler. add new API and bus ops of bus_signal_handler distingush handle generic sigbus and hotplug sigbus
v2->v1(v21):
refine some doc and commit log
fix igb uio kernel issue for control path failure
rebase testpmd code
Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:v21->v20:
split function in hot unplug ops
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code
refine the remap logic for multiple device.
remove the auto binding
v19->18:
note for limitation of multiple hotplug,fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.
the prior patch history please check the patch set "add device event monitor framework".
Jeff Guo (7):
bus: add hotplug failure handler bus/pci: implement hotplug failure handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler operation
bus: add helper to handle sigbus
eal: add failure handle mechanism for hotplug
igb_uio: fix unexpected remove issue for hotplug
drivers/bus/pci/pci_common.c | 77 ++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 ++++++++++ drivers/bus/pci/private.h | 12 ++++ kernel/linux/igb_uio/igb_uio.c | 69 +++++++++++++++---- lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++ lib/librte_eal/common/include/rte_bus.h | 33 ++++++++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 113 +++++++++++++++++++++++++++++++- 8 files changed, 377 insertions(+), 15 deletions(-)
Signature · 1 line
2.7.4
PATCH v9 1/7 bus: add hotplug failure handler
When device be hotplug out, if app still continue to access device by mmio, it will cause of memory failure and result the system crash.
This patch introduces a bus ops to handle device hotplug failure, it is a bus specific behavior, so each kind of bus can implement its own logic case by case.
---v9->v8:
no change
--- lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);typedef int (*rte_bus_parse_t )(const char *name,void *addr);/** +* Implementation a specific hotplug failure handler, which is responsible +* for handle the failure when the device be hotplug out from the bus. When +* hotplug removal event be detected, it could call this function to handle +* failure and guaranty the system would not crash in the case. +* @param dev +* Pointer of the device structure. +* +* @return +* 0 on success. +* !0 on error. +*/ +typedef int (*rte_bus_hotplug_failure_handler_t )(struct rte_device *dev); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -211,6 +225,8 @@ struct rte_bus {rte_bus_parse_t parse;/**< Parse a device name */ struct rte_bus_conf conf;/**< Bus configuration */ rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ +rte_bus_hotplug_failure_handler_t hotplug_failure_handler; +/**< handle hotplug failure on bus */ };/**
Signature · 1 line
2.7.4
PATCH v9 2/7 bus/pci: implement hotplug failure handler ops
In reply to Jeff GuoThis patch implements the ops of hotplug failure handler for PCI bus, it is functional to remap a new dummy memory which overlap to the failure memory to avoid MMIO read/write error.
---v9->v8:
no change
--- drivers/bus/pci/pci_common.c | 28 ++++++++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++ drivers/bus/pci/private.h | 12 ++++++++++++ 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
@@ -408,6 +408,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, }static int +pci_hotplug_failure_handler (struct rte_device *dev) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =RTE_DEV_TO_PCI (dev); +if (!pdev) +return -1 ; + +switch (pdev->kdrv) { +case RTE_KDRV_IGB_UIO : +case RTE_KDRV_UIO_GENERIC : +case RTE_KDRV_NIC_UIO : +/* mmio resource is invalid, remap it to be safe. */ + ret =pci_uio_remap_resource (pdev); +break ; +default : +RTE_LOG (DEBUG ,EAL , +"Not managed by a supported kernel driver, skipped\n" ); + ret =-1 ; +break ; + } + +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -437,6 +464,7 @@ struct rte_pci_bus rte_pci_bus = { .unplug = pci_unplug, .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, + .hotplug_failure_handler = pci_hotplug_failure_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res) } } +/* remap the PCI resource of a PCI device in anonymous virtual memory */ +int +pci_uio_remap_resource (struct rte_pci_device *dev) +{ +int i; +void *map_address; + +if (dev ==NULL ) +return -1 ; + +/* Remap all BARs */ +for (i =0 ; i !=PCI_MAX_RESOURCE ; i++) { +/* skip empty BAR */ +if (dev->mem_resource[i].phys_addr ==0 ) +continue ; + map_address =mmap (dev->mem_resource[i].addr, + (size_t )dev->mem_resource[i].len, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ); +if (map_address ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL , +"Cannot remap resource for device %s\n" , + dev->name); +return -1 ; + } +RTE_LOG (INFO ,EAL , +"Successful remap resource for device %s\n" , + dev->name); + } + +return 0 ; +} +static struct mapped_pci_resource *pci_uio_find_resource (struct rte_pci_device *dev) {
drivers/bus/pci/private.h +12 -0
@@ -123,6 +123,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev, struct mapped_pci_resource *uio_res);/** +* Remap the PCI resource of a PCI device in anonymous virtual memory. +* +* @param dev +* Point to the struct rte pci device. +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +pci_uio_remap_resource (struct rte_pci_device *dev); + +/** * Map device memory to uio resource * * This function is private to EAL.
Signature · 1 line
2.7.4
PATCH v9 3/7 bus: add sigbus handler
In reply to Jeff GuoWhen device be hotplug out, if data path still read/write device, the sigbus error will occur, this error need to be handled. So a handler need to be here to capture the signal and handle it correspondingly.
This patch introduces a bus ops to handle sigbus error, it is a bus specific behavior, so that each kind of bus can implement its own logic case by case.
---v9->v8:
no change
--- lib/librte_eal/common/include/rte_bus.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
lib/librte_eal/common/include/rte_bus.h +17 -0
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);typedef int (*rte_bus_hotplug_failure_handler_t )(struct rte_device *dev);/** +* Implementation a specific sigbus handler, which is responsible for handle +* the sigbus error which is either original memory error, or specific memory +* error that caused of hot unplug. When sigbus error be captured, it could +* call this function to handle sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 for success handle the sigbus. +* 1 for no bus handle the sigbus. +* -1 for failed to handle the sigbus +*/ +typedef int (*rte_bus_sigbus_handler_t )(const void *failure_addr); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -227,6 +242,8 @@ struct rte_bus {rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ rte_bus_hotplug_failure_handler_t hotplug_failure_handler;/**< handle hotplug failure on bus */ +rte_bus_sigbus_handler_t sigbus_handler;/**< handle sigbus error */ + };/**
Signature · 1 line
2.7.4
PATCH v9 4/7 bus/pci: implement sigbus handler operation
In reply to Jeff GuoThis patch implements the ops of sigbus handler for PCI bus, it is functional to find the corresponding pci device which is been hotplug out, and then call the bus ops of hotplug failure handler to handle the failure for the device.
---v9->v8:
no change
--- drivers/bus/pci/pci_common.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
drivers/bus/pci/pci_common.c +49 -0
@@ -407,6 +407,32 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,return NULL ; } +/* check the failure address belongs to which device. */ +static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int i; + +FOREACH_DEVICE_ON_PCIBUS (pdev) { +for (i =0 ; i !=RTE_DIM (pdev->mem_resource); i++) { +if ((uint64_t )(uintptr_t )failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t )(uintptr_t )failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) { +RTE_LOG (INFO ,EAL ,"Failure address " +"%16.16" PRIx64" belongs to " +"device %s!\n" , + (uint64_t )(uintptr_t )failure_addr, + pdev->device.name); +return pdev; + } + } + } +return NULL ; +} +static int pci_hotplug_failure_handler (struct rte_device *dev) {
@@ -435,6 +461,28 @@ pci_hotplug_failure_handler(struct rte_device *dev) }static int +pci_sigbus_handler (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =pci_find_device_by_addr (failure_addr); +if (!pdev) { +/* It is a generic sigbus error, no bus would handle it. */ + ret =1 ; + }else { +/* The sigbus error is caused of hot removal. */ + ret =pci_hotplug_failure_handler (&pdev->device); +if (ret) { +RTE_LOG (ERR ,EAL ,"Failed to handle hot plug for " +"device %s" , pdev->name); + ret =-1 ; + } + } +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -465,6 +513,7 @@ struct rte_pci_bus rte_pci_bus = { .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, .hotplug_failure_handler = pci_hotplug_failure_handler, + .sigbus_handler = pci_sigbus_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
PATCH v9 5/7 bus: add helper to handle sigbus
In reply to Jeff GuoThis patch aim to add a helper to iterate all buses to find the corresponding bus to handle the sigbus error.
---v9->v8:
no change
--- lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++++++++ 2 files changed, 55 insertions(+)
lib/librte_eal/common/eal_common_bus.c +43 -0
@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) +return -1 ; + + ret = bus->sigbus_handler (failure_addr); + +/* find bus but handle failed, keep the errno be set. */ +if (ret <0 && rte_errno ==0 ) + rte_errno =ENOTSUP ; + +return ret >0 ; +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* can not find bus. */ +if (!bus) +return 1 ; +/* find bus but handle failed, pass on the new errno. */ +else if (rte_errno !=0 ) +return -1 ; + +/* restore the old errno. */ + rte_errno = old_errno; + +return ret; +}
lib/librte_eal/common/eal_private.h +12 -0
@@ -258,4 +258,16 @@ int rte_mp_channel_init(void);*/ void dev_callback_process (char *device_name,enum rte_dev_event_type event); +/** +* Iterate all buses to find the corresponding bus, to handle the sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
PATCH v9 6/7 eal: add failure handle mechanism for hotplug
In reply to Jeff GuoThis patch introduces a failure handle mechanism to handle device hotplug removal event.
First it can register sigbus handler when enable device event monitor. Once sigbus error be captured, it will check the failure address and accordingly remap the invalid memory for the corresponding device. Besed on this mechanism, it could guaranty the application not crash when the device be hotplug out.
---v9->v8:
no change
--- lib/librte_eal/linuxapp/eal/eal_dev.c | 113 +++++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 1 deletion(-)
lib/librte_eal/linuxapp/eal/eal_dev.c +112 -1
@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h>
@@ -14,6 +16,10 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h> +#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -23,6 +29,17 @@ static bool monitor_started;#define EAL_UEV_MSG_LEN 4096#define EAL_UEV_MSG_ELEM_LEN 128 +/* +* spinlock for device failure handle, if try to access bus or device, +* such as handle sigbus on bus or handle memory failure for device just use +* this lock. It could protect the bus and the device to avoid race condition. +*/ +static rte_spinlock_t failure_handle_lock =RTE_SPINLOCK_INITIALIZER ; + +static struct sigaction sigbus_action_old; + +static int sigbus_need_recover; + static void dev_uev_handler(__rte_unused void *param);/* identify the system layer which reports this event. */
@@ -33,6 +50,49 @@ enum eal_dev_event_subsystem {EAL_DEV_EVENT_SUBSYSTEM_MAX }; +static void +sigbus_action_recover (void ) +{ +if (sigbus_need_recover) { +sigaction (SIGBUS , &sigbus_action_old,NULL ); + sigbus_need_recover =0 ; + } +} + +static void sigbus_handler (int signum,siginfo_t *info, + void *ctx __rte_unused) +{ +int ret; + +RTE_LOG (INFO ,EAL ,"Thread[%d] catch SIGBUS, fault address:%p\n" , + (int )pthread_self (), info->si_addr); + +rte_spinlock_lock (&failure_handle_lock); + ret =rte_bus_sigbus_handler (info->si_addr); +rte_spinlock_unlock (&failure_handle_lock); +if (ret ==-1 ) { +rte_exit (EXIT_FAILURE , +"Failed to handle SIGBUS for hotplug, " +"(rte_errno: %s)!" ,strerror (rte_errno)); + }else if (ret ==1 ) { +if (sigbus_action_old.sa_handler) + (*(sigbus_action_old.sa_handler))(signum); +else +rte_exit (EXIT_FAILURE , +"Failed to handle generic SIGBUS!" ); + } + +RTE_LOG (INFO ,EAL ,"Success to handle SIGBUS for hotplug!\n" ); +} + +static int cmp_dev_name (const struct rte_device *dev, +const void *_name) +{ +const char *name = _name; + +return strcmp (dev->name, name); +} +static int dev_uev_socket_fd_create (void ) {
@@ -147,6 +207,9 @@ dev_uev_handler(__rte_unused void *param)struct rte_dev_event uevent;int ret;char buf[EAL_UEV_MSG_LEN ]; +struct rte_bus *bus; +struct rte_device *dev; +const char *busname ="" ;memset (&uevent,0 ,sizeof (struct rte_dev_event ));memset (buf,0 ,EAL_UEV_MSG_LEN );
@@ -171,13 +234,50 @@ dev_uev_handler(__rte_unused void *param)RTE_LOG (DEBUG ,EAL ,"receive uevent(name:%s, type:%d, subsystem:%d)\n" , uevent.devname, uevent.type, uevent.subsystem); -if (uevent.devname) +switch (uevent.subsystem) { +case EAL_DEV_EVENT_SUBSYSTEM_PCI : +case EAL_DEV_EVENT_SUBSYSTEM_UIO : + busname ="pci" ; +break ; +default : +break ; + } + +if (uevent.devname) { +if (uevent.type ==RTE_DEV_EVENT_REMOVE ) { +rte_spinlock_lock (&failure_handle_lock); + bus =rte_bus_find_by_name (busname); +if (bus ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find bus (%s)\n" , + busname); +return ; + } + + dev = bus->find_device (NULL , cmp_dev_name, + uevent.devname); +if (dev ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find device (%s) on " +"bus (%s)\n" , uevent.devname, busname); +return ; + } + + ret = bus->hotplug_failure_handler (dev); +rte_spinlock_unlock (&failure_handle_lock); +if (ret) { +RTE_LOG (ERR ,EAL ,"Can not handle hotplug for " +"device (%s)\n" , dev->name); +return ; + } + }dev_callback_process (uevent.devname, uevent.type); + } } int __rte_experimentalrte_dev_event_monitor_start (void) { +sigset_t mask; +struct sigaction action;int ret;if (monitor_started)
@@ -197,6 +297,14 @@ rte_dev_event_monitor_start(void)return -1 ; } +/* register sigbus handler */ +sigemptyset (&mask); +sigaddset (&mask,SIGBUS ); + action.sa_flags =SA_SIGINFO ; + action.sa_mask = mask; + action.sa_sigaction = sigbus_handler; + sigbus_need_recover = !sigaction (SIGBUS , &action, &sigbus_action_old); + monitor_started = true;return 0 ;
@@ -217,8 +325,11 @@ rte_dev_event_monitor_stop(void)return ret; } +sigbus_action_recover (); +close (intr_handle.fd); intr_handle.fd =-1 ; monitor_started = false; +return 0 ; }
Signature · 1 line
2.7.4
PATCH v9 7/7 igb_uio: fix unexpected remove issue for hotplug
In reply to Jeff GuoWhen device be hotplug out, the pci resource will be released in kernel, the uio fd will disappear, and the irq will be released. At this time, if igb uio driver still try to access or release these resource, it will cause kernel crash.
On the other hand, uio_remove will be called unexpectedly before uio_release. The uio_remove procedure will free resources which are needed by uio_release. So there is no chance to disable interrupt which is defined inside uio_release procedure. This will affect later usage of interrupt.
So the case of unexpectedly removal by hot unplug should be identify and correspondingly processed.
This patch propose to add enum rte_udev_state in struct rte_uio_pci_dev, that will keep the state of uio device as probed/opened/released/removed.
This patch also checks kobject’s remove_uevent_sent state to detect the unexpectedly removal status which means hot unplug. Once hot unplug be detected, it will call uio_release as soon as possible and set the uio status to be “removed”. After that, uio will check this status in uio_release function, if uio have already been removed, it will only free the dirty uio resource.
---v9->v8:
refine commit log to be more readable.
--- kernel/linux/igb_uio/igb_uio.c | 69 +++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 14 deletions(-)
kernel/linux/igb_uio/igb_uio.c +55 -14
@@ -19,6 +19,14 @@#include "compat.h" +/* uio pci device state */ +enum rte_udev_state { +RTE_UDEV_PROBED , +RTE_UDEV_OPENNED , +RTE_UDEV_RELEASED , +RTE_UDEV_REMOVED , +}; +/** * A structure describing the private information for a uio device. */
@@ -28,6 +36,7 @@ struct rte_uio_pci_dev {enum rte_intr_mode mode;struct mutex lock;int refcnt; +enum rte_udev_state state; };static int wc_activate;
@@ -309,6 +318,17 @@ igbuio_pci_disable_interrupts(struct rte_uio_pci_dev *udev)#endif } +/* Unmap previously ioremap'd resources */ +static void +igbuio_pci_release_iomem (struct uio_info *info) +{ +int i; + +for (i =0 ; i <MAX_UIO_MAPS ; i++) { +if (info->mem[i].internal_addr) +iounmap (info->mem[i].internal_addr); + } +}/** * This gets called while opening uio device file.
@@ -331,20 +351,35 @@ igbuio_pci_open(struct uio_info *info, struct inode *inode)/* enable interrupts */ err =igbuio_pci_enable_interrupts (udev); -mutex_unlock (&udev->lock);if (err) {dev_err (&dev->dev,"Enable interrupt fails\n" ); +pci_clear_master (dev); +mutex_unlock (&udev->lock);return err; } + udev->state =RTE_UDEV_OPENNED ; +mutex_unlock (&udev->lock);return 0 ; } +/** +* This gets called while closing uio device file. +*/ static int igbuio_pci_release (struct uio_info *info,struct inode *inode) {struct rte_uio_pci_dev *udev = info->priv;struct pci_dev *dev = udev->pdev; +if (udev->state ==RTE_UDEV_REMOVED ) { +mutex_destroy (&udev->lock); +igbuio_pci_release_iomem (&udev->info); +pci_disable_device (dev); +pci_set_drvdata (dev,NULL ); +kfree (udev); +return 0 ; + } +mutex_lock (&udev->lock);if (--udev->refcnt >0 ) {mutex_unlock (&udev->lock);
@@ -356,7 +391,7 @@ igbuio_pci_release(struct uio_info *info, struct inode *inode)/* stop the device from further DMA */ pci_clear_master (dev); - + udev->state =RTE_UDEV_RELEASED ;mutex_unlock (&udev->lock);return 0 ; }
@@ -414,18 +449,6 @@ igbuio_pci_setup_ioport(struct pci_dev *dev, struct uio_info *info,return 0 ; } -/* Unmap previously ioremap'd resources */ -static void -igbuio_pci_release_iomem (struct uio_info *info) -{ -int i; - -for (i =0 ; i <MAX_UIO_MAPS ; i++) { -if (info->mem[i].internal_addr) -iounmap (info->mem[i].internal_addr); - } -} -static int igbuio_setup_bars (struct pci_dev *dev,struct uio_info *info) {
@@ -562,6 +585,9 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) (unsigned long long)map_dma_addr, map_addr); } +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_PROBED ; +mutex_unlock (&udev->lock);return 0 ; fail_remove_group:
@@ -579,6 +605,21 @@ static void igbuio_pci_remove(struct pci_dev *dev) {struct rte_uio_pci_dev *udev =pci_get_drvdata (dev); +struct pci_dev *pdev = udev->pdev; +int ret; + +/* handle unexpected removal */ +if (udev->state ==RTE_UDEV_OPENNED || + (&pdev->dev.kobj)->state_remove_uevent_sent ==1 ) { +dev_notice (&dev->dev,"Unexpected removal!\n" ); + ret =igbuio_pci_release (&udev->info,NULL ); +if (ret) +return ; +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_REMOVED ; +mutex_unlock (&udev->lock); +return ; + }mutex_destroy (&udev->lock);sysfs_remove_group (&dev->dev.kobj, &dev_attr_grp);
Signature · 1 line
2.7.4
Re: [PATCH v9 7/7] igb_uio: fix unexpected remove issue for hotplug
Quoted 21 lines
-----Original Message-----
From: Guo, Jia
Sent: Wednesday, July 11, 2018 6:42 PMWhen device be hotplug out, the pci resource will be released in kernel, the uio fd will disappear, and the irq will be released. At this time, if igb uio driver still try to access or release these resource, it will cause kernel crash.
On the other hand,
uio_removewill be called unexpectedly beforeuio_release. Theuio_removeprocedure will free resources which are needed byuio_release. So there is no chance to disable interrupt which is defined insideuio_releaseprocedure. This will affect later usage of interrupt.So the case of unexpectedly removal by hot unplug should be identify and correspondingly processed.
This patch propose to add enum
rte_udev_statein structrte_uio_pci_dev, that will keep the state of uio device as probed/opened/released/removed.
Quoted 8 lines
This patch also checks kobject’s
remove_uevent_sentstate to detect the unexpectedly removal status which means hot unplug. Once hot unplug be detected, it will calluio_releaseas soon as possible and set the uio status to be “removed”. After that, uio will check this status inuio_releasefunction, if uio have already been removed, it will only free the dirty uio resource.Signed-off-by Jeff Guo <[email protected]>
Even though we prefer vfio than igb_uio as vfio is safer and more standard way of accessing devices, it's still good to have this bug-fixing to avoiding kernel crash and memory leak.
Later on, we might further enhance igb_uio by introducing similar mechanism which vfio-pci currently uses (send event to up-layer application in the middle of pci remove process), so up-layer application can close this device more gracefully. Or, we can suggest to use vfio, and leave igb_uio as it is.
Thanks,
--Shaopeng
Re: [PATCH v9 0/7] hotplug failure handle mechanism
In reply to Jeff GuoQuoted 9 lines · On Wed, 11 Jul 2018 18:41:50 +0800 Jeff Guo <[email protected]> wrote:
As we know, hot plug is an importance feature, either use for the datacenter device’s fail-safe, or use for SRIOV Live Migration in SDN/NFV. It could bring the higher flexibility and continuality to the networking services in multiple use cases in industry. So let we see, dpdk as an importance networking framework, what can it help to implement hot plug solution for users.
We already have a general device event detect mechanism, failsafe driver, bonding driver and hot plug/unplug api in framework, app could use these to develop their hot plug solution.
I like seeing a better solution to hot plug. But it is worth mentioning that for the Hyper-V netvsc driver this is mostly unnecessary. The Hyper-V host notifies the network driver directly about availability of SRIOV device, and the netvsc device driver can use that to do its own VF management. It doesn't really need (or want) to be using a general PCI solution.
Re: [PATCH v9 0/7] hotplug failure handle mechanism
Quoted 12 lines · Stephen Hemminger
Quoted 9 lines · On Wed, 11 Jul 2018 18:41:50 +0800 Jeff Guo <[email protected]> wrote:
As we know, hot plug is an importance feature, either use for the datacenter device’s fail-safe, or use for SRIOV Live Migration in SDN/NFV. It could bring the higher flexibility and continuality to the networking services in multiple use cases in industry. So let we see, dpdk as an importance networking framework, what can it help to implement hot plug solution for users.
We already have a general device event detect mechanism, failsafe driver, bonding driver and hot plug/unplug api in framework, app could use these to develop their hot plug solution.
Quoted 6 lines
I like seeing a better solution to hot plug. But it is worth mentioning that for the Hyper-V netvsc driver this is mostly unnecessary. The Hyper-V host notifies the network driver directly about availability of SRIOV device, and the netvsc device driver can use that to do its own VF management. It doesn't really need (or want) to be using a general PCI solution.
I am not sure about Hyper-v netvsc driver, but i am very interesting to deep analyze it to find what could let we find a best solution for hotplug. But since some customer, who like me still not have chance to use Hyper-v, they will encounter the hotplug issue when use kvm or other platform. so let we said that the topic about here is aim to fix the issue for that part of customer or developer. Just let hotplug be used more in diversity scenario.
v10 cover hotplug failure handle mechanism
In reply to Jeff GuoHotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.
We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio and vfio.
In the case of hotplug for igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.
In this patch set, we propose a PCIe bus failure handler mechanism for hotplug in igb_uio. It aims to guarantee that, when a hot unplug occurs, the system will not crash and the application will not break out.
The mechanism should work as below:
First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “memory_failure_handler” to handle bus read/write errors.
- Implement pci bus specific ops “pci_memory_failure_handler”. It will be based on the failure address to remap memory for the corresponding device that unplugged.
For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate all device on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_diable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.
The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device event monitor->Enable hotplug handle->Register event callback ->attach port->start port->start forwarding->Device unplug->failure handle ->stop forwarding->stop port->close port->detach port.
This patch set would not cover hotplug insert and binding, and it is only implement the igb_uio failure handler, the vfio hotplug failure handler will be in next coming patch set.
patchset history:v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log
v9->v8:
refine commit log to be more readable.
v8->v7:
refine errno process in sigbus handler.
refine igb uio release process
v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.
v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.
v3->v2:
change bus ops name to bus_hotplug_handler.
add new API and bus ops of bus_signal_handler distingush handle generic. sigbus and hotplug sigbus.
v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.
Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.
v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.
the prior patch history please check the patch set "add device event monitor framework".
Jeff Guo (8):
bus: add memory failure handler
bus/pci: implement memory failure handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hotplug
igb_uio: fix unexpected remove issue for hotplug
testpmd: use hotplug failure handle mechanism
app/test-pmd/testpmd.c | 27 +++++- doc/guides/rel_notes/release_18_08.rst | 5 + drivers/bus/pci/pci_common.c | 81 ++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++ drivers/bus/pci/private.h | 12 +++ kernel/linux/igb_uio/igb_uio.c | 69 +++++++++++--- lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_common_bus.c | 43 +++++++++ lib/librte_eal/common/eal_private.h | 38 ++++++++ lib/librte_eal/common/include/rte_bus.h | 34 +++++++ lib/librte_eal/common/include/rte_dev.h | 26 ++++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 159 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 13 files changed, 523 insertions(+), 20 deletions(-)
Signature · 1 line
2.7.4
PATCH v10 1/8 bus: add memory failure handler
A memory failure and system crash can be caused if a device is hotplugged out but the application can still access the device by MMIO.
This patch introduces bus ops to handle memory failures of illegal access, especially for hotplug. Each bus can implement its own case-dependent logic to handle the memory failures.
---v10->v9:
modify bus ops name
--- lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);typedef int (*rte_bus_parse_t )(const char *name,void *addr);/** +* Implement a specific memory failure handler, which is responsible for +* handle the failure of memory illegal access, especially for hotplug. When +* the event of hotplug-out be detected, it could call this function to handle +* the memory failure and avoid system crash. +* @param dev +* Pointer of the device structure. +* +* @return +* 0 on success. +* !0 on error. +*/ +typedef int (*rte_bus_memory_failure_handler_t )(struct rte_device *dev); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -212,6 +226,8 @@ struct rte_bus {struct rte_bus_conf conf;/**< Bus configuration */ rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ rte_dev_iterate_t dev_iterate;/**< Device iterator. */ +rte_bus_memory_failure_handler_t memory_failure_handler; +/**< handle memory failure on the bus */ };/**
Signature · 1 line
2.7.4
PATCH v10 2/8 bus/pci: implement memory failure handler ops
In reply to Jeff GuoThis patch implements the ops to handle memory failures on the PCI bus. It avoids MMIO read/write errors by creating a new dummy memory to remap the memory where the failure is.
---v10->v9:
change pci bus ops name
--- drivers/bus/pci/pci_common.c | 28 ++++++++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++ drivers/bus/pci/private.h | 12 ++++++++++++ 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
@@ -406,6 +406,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, }static int +pci_memory_failure_handler (struct rte_device *dev) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =RTE_DEV_TO_PCI (dev); +if (!pdev) +return -1 ; + +switch (pdev->kdrv) { +case RTE_KDRV_IGB_UIO : +case RTE_KDRV_UIO_GENERIC : +case RTE_KDRV_NIC_UIO : +/* mmio resource is invalid, remap it to be safe. */ + ret =pci_uio_remap_resource (pdev); +break ; +default : +RTE_LOG (DEBUG ,EAL , +"Not managed by a supported kernel driver, skipped\n" ); + ret =-1 ; +break ; + } + +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -435,6 +462,7 @@ struct rte_pci_bus rte_pci_bus = { .unplug = pci_unplug, .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, + .memory_failure_handler = pci_memory_failure_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res) } } +/* remap the PCI resource of a PCI device in anonymous virtual memory */ +int +pci_uio_remap_resource (struct rte_pci_device *dev) +{ +int i; +void *map_address; + +if (dev ==NULL ) +return -1 ; + +/* Remap all BARs */ +for (i =0 ; i !=PCI_MAX_RESOURCE ; i++) { +/* skip empty BAR */ +if (dev->mem_resource[i].phys_addr ==0 ) +continue ; + map_address =mmap (dev->mem_resource[i].addr, + (size_t )dev->mem_resource[i].len, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ); +if (map_address ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL , +"Cannot remap resource for device %s\n" , + dev->name); +return -1 ; + } +RTE_LOG (INFO ,EAL , +"Successful remap resource for device %s\n" , + dev->name); + } + +return 0 ; +} +static struct mapped_pci_resource *pci_uio_find_resource (struct rte_pci_device *dev) {
drivers/bus/pci/private.h +12 -0
@@ -123,6 +123,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev, struct mapped_pci_resource *uio_res);/** +* Remap the PCI resource of a PCI device in anonymous virtual memory. +* +* @param dev +* Point to the struct rte pci device. +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +pci_uio_remap_resource (struct rte_pci_device *dev); + +/** * Map device memory to uio resource * * This function is private to EAL.
Signature · 1 line
2.7.4
PATCH v10 3/8 bus: add sigbus handler
In reply to Jeff GuoWhen a device is hotplugged out, a sigbus error will occur of the datapath can still read/write to the device. A handler is required here to capture the sigbus signal and handle it appropriately.
This patch introduces bus ops to handle sigbus errors. Each bus can implement its own case-dependent logic to handle the sigbus errors.
---v10->v9:
refine commit log
--- lib/librte_eal/common/include/rte_bus.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
lib/librte_eal/common/include/rte_bus.h +18 -0
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);typedef int (*rte_bus_memory_failure_handler_t )(struct rte_device *dev);/** +* Implement a specific sigbus handler, which is responsible for handle +* the sigbus error which is either original memory error, or specific memory +* error that caused of device be hotplug-out. When sigbus error be captured, +* it could call this function to handle sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 for success handle the sigbus. +* 1 for no bus handle the sigbus. +* -1 for failed to handle the sigbus +*/ +typedef int (*rte_bus_sigbus_handler_t )(const void *failure_addr); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -228,6 +243,9 @@ struct rte_bus {rte_dev_iterate_t dev_iterate;/**< Device iterator. */ rte_bus_memory_failure_handler_t memory_failure_handler;/**< handle memory failure on the bus */ +rte_bus_sigbus_handler_t sigbus_handler; +/**< handle sigbus error on the bus */ + };/**
Signature · 1 line
2.7.4
PATCH v10 4/8 bus/pci: implement sigbus handler ops
In reply to Jeff GuoThis patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hotplugged out and calls the relevant ops of the memory failure handler to handle the failure of the device.
---v10->v9:
refine doc.
--- drivers/bus/pci/pci_common.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
drivers/bus/pci/pci_common.c +53 -0
@@ -405,6 +405,36 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,return NULL ; } +/** +* find the device which encounter the failure, by iterate all device on +* PCI bus to check if the memory failure address is located in the range +* of the BARs of any device. +*/ +static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int i; + +FOREACH_DEVICE_ON_PCIBUS (pdev) { +for (i =0 ; i !=RTE_DIM (pdev->mem_resource); i++) { +if ((uint64_t )(uintptr_t )failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t )(uintptr_t )failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) { +RTE_LOG (INFO ,EAL ,"Failure address " +"%16.16" PRIx64" belongs to " +"device %s!\n" , + (uint64_t )(uintptr_t )failure_addr, + pdev->device.name); +return pdev; + } + } + } +return NULL ; +} +static int pci_memory_failure_handler (struct rte_device *dev) {
@@ -433,6 +463,28 @@ pci_memory_failure_handler(struct rte_device *dev) }static int +pci_sigbus_handler (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =pci_find_device_by_addr (failure_addr); +if (!pdev) { +/* It is a generic sigbus error, no bus would handle it. */ + ret =1 ; + }else { +/* The sigbus error is caused of hotplug-out. */ + ret =pci_memory_failure_handler (&pdev->device); +if (ret) { +RTE_LOG (ERR ,EAL ,"Failed to handle failure for " +"device %s" , pdev->name); + ret =-1 ; + } + } +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -463,6 +515,7 @@ struct rte_pci_bus rte_pci_bus = { .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, .memory_failure_handler = pci_memory_failure_handler, + .sigbus_handler = pci_sigbus_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
PATCH v10 5/8 bus: add helper to handle sigbus
In reply to Jeff GuoThis patch aims to add a helper to iterate through all buses to find the relevant bus to handle the sigbus error.
---v10->v9:
refine commit log
--- lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++++++++ 2 files changed, 55 insertions(+)
lib/librte_eal/common/eal_common_bus.c +43 -0
@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) +return -1 ; + + ret = bus->sigbus_handler (failure_addr); + +/* find bus but handle failed, keep the errno be set. */ +if (ret <0 && rte_errno ==0 ) + rte_errno =ENOTSUP ; + +return ret >0 ; +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* can not find bus. */ +if (!bus) +return 1 ; +/* find bus but handle failed, pass on the new errno. */ +else if (rte_errno !=0 ) +return -1 ; + +/* restore the old errno. */ + rte_errno = old_errno; + +return ret; +}
lib/librte_eal/common/eal_private.h +12 -0
@@ -304,4 +304,16 @@ int rte_devargs_layers_parse(struct rte_devargs *devargs, const char *devstr); +/** +* Iterate all buses to find the corresponding bus to handle the sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
PATCH v10 5/8 bus: add helper to handle sigbus
In reply to Jeff GuoThis patch aims to add a helper to iterate through all buses to find the relevant bus to handle the sigbus error.
---v10->v9:
refine commit log
--- lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 12 ++++++++++ 2 files changed, 55 insertions(+)
lib/librte_eal/common/eal_common_bus.c +43 -0
@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) +return -1 ; + + ret = bus->sigbus_handler (failure_addr); + +/* find bus but handle failed, keep the errno be set. */ +if (ret <0 && rte_errno ==0 ) + rte_errno =ENOTSUP ; + +return ret >0 ; +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* can not find bus. */ +if (!bus) +return 1 ; +/* find bus but handle failed, pass on the new errno. */ +else if (rte_errno !=0 ) +return -1 ; + +/* restore the old errno. */ + rte_errno = old_errno; + +return ret; +}
lib/librte_eal/common/eal_private.h +12 -0
@@ -304,4 +304,16 @@ int rte_devargs_layers_parse(struct rte_devargs *devargs, const char *devstr); +/** +* Iterate all buses to find the corresponding bus to handle the sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
PATCH v10 6/8 eal: add failure handle mechanism for hotplug
In reply to Jeff GuoThe mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus error is captured, the mechanism will check the failure address and accordingly remap the invalid memory for the corresponding device. It could prevent the application from crashing when a device is hotplugged out.
By this patch, users could call below new added APIs to enable/disable the device failure handle mechanism:
- rte_dev_hotplug_handle_enable
- rte_dev_hotplug_handle_disable
---v10->v9:
add new APIs to enable/disable hotplug handling
--- doc/guides/rel_notes/release_18_08.rst | 5 + lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_private.h | 26 ++++++ lib/librte_eal/common/include/rte_dev.h | 26 ++++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 159 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 6 files changed, 231 insertions(+), 1 deletion(-)
doc/guides/rel_notes/release_18_08.rst +5 -0
@@ -117,6 +117,11 @@ New Features Added support for chained mbufs (input and output). +* **Added failure handle mechanism for hotplug.** + + ``rte_dev_hotplug_handle_enable`` and ``rte_dev_hotplug_handle_disable`` are + for enable or disable failure handle mechanism for hotplug. + API Changes -----------
lib/librte_eal/bsdapp/eal/eal_dev.c +14 -0
@@ -19,3 +19,17 @@ rte_dev_event_monitor_stop(void)RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" );return -1 ; } + +int +rte_dev_hotplug_handle_enable (void ) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +} + +int +rte_dev_hotplug_handle_disable (void ) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +}
lib/librte_eal/common/eal_private.h +26 -0
@@ -316,4 +316,30 @@ rte_devargs_layers_parse(struct rte_devargs *devargs,*/ int rte_bus_sigbus_handler (const void *failure_addr); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Register the sigbus hander. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_register (void ); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Unregister the sigbus hander. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_unregister (void ); +#endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/include/rte_dev.h +26 -0
@@ -460,4 +460,30 @@ rte_dev_event_monitor_start(void); int __rte_experimentalrte_dev_event_monitor_stop (void); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Enable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_enable (void); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Disable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_disable (void); +#endif /* _RTE_DEV_H_ */
lib/librte_eal/linuxapp/eal/eal_dev.c +158 -1
204 lines not on this page · raw message
lib/librte_eal/rte_eal_version.map +2 -0
@@ -274,6 +274,8 @@ EXPERIMENTAL { rte_dev_event_callback_unregister; rte_dev_event_monitor_start; rte_dev_event_monitor_stop; + rte_dev_hotplug_handle_enable; + rte_dev_hotplug_handle_disable; rte_dev_iterator_init; rte_dev_iterator_next; rte_devargs_add;
Signature · 1 line
2.7.4
PATCH v10 7/8 igb_uio: fix unexpected remove issue for hotplug
In reply to Jeff GuoWhen a device is hotplugged out, the PCI resource is released in the kernel, the UIO file descriptor will disappear and the irq will be released. After this, a kernel crash will be caused if the igb uio driver tries to access or release these resources.
And more, uio_remove will be called unexpectedly before uio_release when device be hotpluggged out, the uio_remove procedure will free resources that are required by uio_release. This will later affect the usage of interrupt as there is no way to disable the interrupt which is defined in uio_release.
To prevent this, the hotplug removal needs to be identified and processed accordingly in igb uio driver.
This patch proposes the addition of enum rte_udev_state in the rte_uio_pci_dev struct. This will store the state of the uio device as one of the following: probed/opened/released/removed.
This patch also checks the kobject's remove_uevent_sent state to detect if the removal status is hotplug-out. Once a hotplug-out is detected, it will call uio_release and set the uio status to "removed". After that, uio will check the status in the uio_release function. If uio has already been removed, it will only free the dirty uio resource.
---v10->v9:
refine commmit log.
--- kernel/linux/igb_uio/igb_uio.c | 69 +++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 14 deletions(-)
kernel/linux/igb_uio/igb_uio.c +55 -14
@@ -19,6 +19,14 @@#include "compat.h" +/* uio pci device state */ +enum rte_udev_state { +RTE_UDEV_PROBED , +RTE_UDEV_OPENNED , +RTE_UDEV_RELEASED , +RTE_UDEV_REMOVED , +}; +/** * A structure describing the private information for a uio device. */
@@ -28,6 +36,7 @@ struct rte_uio_pci_dev {enum rte_intr_mode mode;struct mutex lock;int refcnt; +enum rte_udev_state state; };static int wc_activate;
@@ -309,6 +318,17 @@ igbuio_pci_disable_interrupts(struct rte_uio_pci_dev *udev)#endif } +/* Unmap previously ioremap'd resources */ +static void +igbuio_pci_release_iomem (struct uio_info *info) +{ +int i; + +for (i =0 ; i <MAX_UIO_MAPS ; i++) { +if (info->mem[i].internal_addr) +iounmap (info->mem[i].internal_addr); + } +}/** * This gets called while opening uio device file.
@@ -331,20 +351,35 @@ igbuio_pci_open(struct uio_info *info, struct inode *inode)/* enable interrupts */ err =igbuio_pci_enable_interrupts (udev); -mutex_unlock (&udev->lock);if (err) {dev_err (&dev->dev,"Enable interrupt fails\n" ); +pci_clear_master (dev); +mutex_unlock (&udev->lock);return err; } + udev->state =RTE_UDEV_OPENNED ; +mutex_unlock (&udev->lock);return 0 ; } +/** +* This gets called while closing uio device file. +*/ static int igbuio_pci_release (struct uio_info *info,struct inode *inode) {struct rte_uio_pci_dev *udev = info->priv;struct pci_dev *dev = udev->pdev; +if (udev->state ==RTE_UDEV_REMOVED ) { +mutex_destroy (&udev->lock); +igbuio_pci_release_iomem (&udev->info); +pci_disable_device (dev); +pci_set_drvdata (dev,NULL ); +kfree (udev); +return 0 ; + } +mutex_lock (&udev->lock);if (--udev->refcnt >0 ) {mutex_unlock (&udev->lock);
@@ -356,7 +391,7 @@ igbuio_pci_release(struct uio_info *info, struct inode *inode)/* stop the device from further DMA */ pci_clear_master (dev); - + udev->state =RTE_UDEV_RELEASED ;mutex_unlock (&udev->lock);return 0 ; }
@@ -414,18 +449,6 @@ igbuio_pci_setup_ioport(struct pci_dev *dev, struct uio_info *info,return 0 ; } -/* Unmap previously ioremap'd resources */ -static void -igbuio_pci_release_iomem (struct uio_info *info) -{ -int i; - -for (i =0 ; i <MAX_UIO_MAPS ; i++) { -if (info->mem[i].internal_addr) -iounmap (info->mem[i].internal_addr); - } -} -static int igbuio_setup_bars (struct pci_dev *dev,struct uio_info *info) {
@@ -562,6 +585,9 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) (unsigned long long)map_dma_addr, map_addr); } +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_PROBED ; +mutex_unlock (&udev->lock);return 0 ; fail_remove_group:
@@ -579,6 +605,21 @@ static void igbuio_pci_remove(struct pci_dev *dev) {struct rte_uio_pci_dev *udev =pci_get_drvdata (dev); +struct pci_dev *pdev = udev->pdev; +int ret; + +/* handle unexpected removal */ +if (udev->state ==RTE_UDEV_OPENNED || + (&pdev->dev.kobj)->state_remove_uevent_sent ==1 ) { +dev_notice (&dev->dev,"Unexpected removal!\n" ); + ret =igbuio_pci_release (&udev->info,NULL ); +if (ret) +return ; +mutex_lock (&udev->lock); + udev->state =RTE_UDEV_REMOVED ; +mutex_unlock (&udev->lock); +return ; + }mutex_destroy (&udev->lock);sysfs_remove_group (&dev->dev.kobj, &dev_attr_grp);
Signature · 1 line
2.7.4
Re: [PATCH v10 7/8] igb_uio: fix unexpected remove issue for hotplug
Quoted 26 lines · Jeff Guo
When a device is hotplugged out, the PCI resource is released in the kernel, the UIO file descriptor will disappear and the irq will be released. After this, a kernel crash will be caused if the igb uio driver tries to access or release these resources.
And more,
uio_removewill be called unexpectedly beforeuio_releasewhen device be hotpluggged out, theuio_removeprocedure will free resources that are required byuio_release. This will later affect the usage of interrupt as there is no way to disable the interrupt which is defined inuio_release.To prevent this, the hotplug removal needs to be identified and processed accordingly in igb uio driver.
This patch proposes the addition of enum
rte_udev_statein therte_uio_pci_devstruct. This will store the state of the uio device as one of the following: probed/opened/released/removed.This patch also checks the kobject's
remove_uevent_sentstate to detect if the removal status is hotplug-out. Once a hotplug-out is detected, it will calluio_releaseand set the uio status to "removed". After that, uio will check the status in theuio_releasefunction. If uio has already been removed, it will only free the dirty uio resource.Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>
<...>
kernel/linux/igb_uio/igb_uio.c igbuio_pci_open(struct uio_info *info, struct inode *inode) · line 356
Quoted 8 lines
kernel/linux/igb_uio/igb_uio.c +1
-1@@ -331,20 +351,35 @@ igbuio_pci_open(struct uio_info *info, struct inode *inode)/* enable interrupts */ err =igbuio_pci_enable_interrupts (udev); -mutex_unlock (&udev->lock);if (err) {dev_err (&dev->dev,"Enable interrupt fails\n" ); +pci_clear_master (dev);
Why pci_clear_master required here.
btw, some part of this patch conflicts with [1], which removes mutes and use atomic refcnt operations, but introducing state seems needs mutex.
[1]igb_uio: fix refcount if open returns error
https://patches.dpdk.org/patch/44732/
Quoted 14 lines
kernel/linux/igb_uio/igb_uio.c +6
-0+mutex_unlock (&udev->lock);return err; } + udev->state =RTE_UDEV_OPENNED ; +mutex_unlock (&udev->lock);return 0 ; } +/** +* This gets called while closing uio device file. +*/ static int igbuio_pci_release (struct uio_info *info,struct inode *inode) {
kernel/linux/igb_uio/igb_uio.c igbuio_pci_open(struct uio_info *info, struct inode *inode) · line 380
Quoted 10 lines
kernel/linux/igb_uio/igb_uio.c +7
-0struct rte_uio_pci_dev *udev = info->priv;struct pci_dev *dev = udev->pdev; +if (udev->state ==RTE_UDEV_REMOVED ) { +mutex_destroy (&udev->lock); +igbuio_pci_release_iomem (&udev->info); +pci_disable_device (dev); +pci_set_drvdata (dev,NULL ); +kfree (udev); +return 0 ;
This branch taken when pci_remove called before pci_release.
- At this stage is "dev" valid, since pci_remove() called?
- In this path uio_unregister_device() is missing, who unregisters uio?
- sysfs_remove_group() also missing, it is not clear if it is forgotten or left out, what do you think move common part of pci_remove into new function and call both in pci_remove and here?
And as a logic, can we make pci_remove clear everything, instead of doing some cleanup here. Like:pci_remove:
- calls pci_release
- instead of return keeps doing pci_remove work
- set state to REMOVED
pci_release:
- if state is REMOVED, return without doing nothing
btw, even after uio_unregister_device() how pci_release called?
It can help to share crash backtrace in commit log, to describe problem in more detail.
Re: [PATCH v10 7/8] igb_uio: fix unexpected remove issue for hotplug
hi, ferruh
Quoted 31 lines · Ferruh Yigit
Quoted 26 lines · Jeff Guo
When a device is hotplugged out, the PCI resource is released in the kernel, the UIO file descriptor will disappear and the irq will be released. After this, a kernel crash will be caused if the igb uio driver tries to access or release these resources.
And more,
uio_removewill be called unexpectedly beforeuio_releasewhen device be hotpluggged out, theuio_removeprocedure will free resources that are required byuio_release. This will later affect the usage of interrupt as there is no way to disable the interrupt which is defined inuio_release.To prevent this, the hotplug removal needs to be identified and processed accordingly in igb uio driver.
This patch proposes the addition of enum
rte_udev_statein therte_uio_pci_devstruct. This will store the state of the uio device as one of the following: probed/opened/released/removed.This patch also checks the kobject's
remove_uevent_sentstate to detect if the removal status is hotplug-out. Once a hotplug-out is detected, it will calluio_releaseand set the uio status to "removed". After that, uio will check the status in theuio_releasefunction. If uio has already been removed, it will only free the dirty uio resource.Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]><...>
Quoted 2 lines
(quoted diff) +0
-0@@ -331,20 +351,35 @@ igbuio_pci_open(struct uio_info *info, struct inode *inode)
Quoted 7 lines
kernel/linux/igb_uio/igb_uio.c igbuio_pci_open(struct uio_info *info, struct inode *inode) · line 356
Quoted 6 lines
kernel/linux/igb_uio/igb_uio.c +1
-1/* enable interrupts */ err =igbuio_pci_enable_interrupts (udev); -mutex_unlock (&udev->lock);if (err) {dev_err (&dev->dev,"Enable interrupt fails\n" ); +pci_clear_master (dev);Why
pci_clear_masterrequired here.
It is because set master is before interrupt enabling, if enable interrupt fails should clear master before return i think.
Anyway it is not belong to this patch perspective, it could be separated to another one.
Quoted 6 lines
btw, some part of this patch conflicts with [1], which removes mutes and use atomic refcnt operations, but introducing state seems needs mutex.
[1]
igb_uio: fix refcount if open returns error
https://patches.dpdk.org/patch/44732/
yes, i see and will rework for that if need.
Quoted 17 lines
Quoted 17 lines
kernel/linux/igb_uio/igb_uio.c +6
-0+mutex_unlock (&udev->lock);return err; } + udev->state =RTE_UDEV_OPENNED ; +mutex_unlock (&udev->lock);return 0 ; } +/** +* This gets called while closing uio device file. +*/ static int igbuio_pci_release (struct uio_info *info,struct inode *inode) {struct rte_uio_pci_dev *udev = info->priv;struct pci_dev *dev = udev->pdev;
Quoted 13 lines
kernel/linux/igb_uio/igb_uio.c igbuio_pci_open(struct uio_info *info, struct inode *inode) · line 380
Quoted 7 lines
kernel/linux/igb_uio/igb_uio.c +7
-0+if (udev->state ==RTE_UDEV_REMOVED ) { +mutex_destroy (&udev->lock); +igbuio_pci_release_iomem (&udev->info); +pci_disable_device (dev); +pci_set_drvdata (dev,NULL ); +kfree (udev); +return 0 ;This branch taken when
pci_removecalled beforepci_release.
- At this stage is "dev" valid, sincepci_remove()called?
- In this pathuio_unregister_device()is missing, who unregisters uio?
-sysfs_remove_group()also missing, it is not clear if it is forgotten or left out, what do you think move common part ofpci_removeinto new function and call both inpci_removeand here?
It is not forgotten but specific left out, since the if uio remove before uio release it will cause kernel error, which is double free the
already-free irq issue when uio unregister device.
Quoted 9 lines
And as a logic, can we make
pci_removeclear everything, instead of doing some cleanup here. Like:pci_remove:
- callspci_release
- instead of return keeps doingpci_removework
- set state to REMOVED
pci_release:
- if state is REMOVED, return without doing nothing
I think the logic you said here is make sense, just make release and remove more focus their own work.
Quoted 2 lines
btw, even after
uio_unregister_device()howpci_releasecalled?
The consequence of igb uio removal is that, igb uio remove be called, then igb uio release be called when detaching device, then if quit app it will call pci remove.
Quoted 3 lines
It can help to share crash backtrace in commit log, to describe problem in more detail.
I will do that. And i think the 2 thing need to fix is that, one is the double free irq issue, the other one is give the chance to disable interrupt when uio remove be called before uio release. I check again and find that just add release before remove could both fix these
issues, so please review my coming update patch. Thanks.
PATCH 1/1 igb_uio: fix unexpected removal for hot-unplug
In reply to Jeff GuoWhen a device is hot-unplugged, pci_remove will be invoked unexpectedly before pci_release, it will caused kernel hung issue which will throw the error info of "Trying to free already-free IRQ XXX". And on the other hand, if pci_remove before pci_release, the interrupt will not got chance to be disabled. So this patch aim to fix this issue by adding pci_release call in pci_remove, it will gurranty that all pci clean up will be done before pci removal.
--- kernel/linux/igb_uio/igb_uio.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/linux/igb_uio/igb_uio.c +2 -0
@@ -570,6 +570,8 @@ igbuio_pci_remove(struct pci_dev *dev) {struct rte_uio_pci_dev *udev =pci_get_drvdata (dev); +igbuio_pci_release (&udev->info,NULL ); +sysfs_remove_group (&dev->dev.kobj, &dev_attr_grp);uio_unregister_device (&udev->info);igbuio_pci_release_iomem (&udev->info);
Signature · 1 line
2.7.4
Re: [PATCH v1] igb_uio: fix unexpected removal for hot-unplug
Quoted 18 lines · Jeff Guo
When a device is hot-unplugged,
pci_removewill be invoked unexpectedly beforepci_release, it will caused kernel hung issue which will throw the error info of "Trying to free already-free IRQ XXX". And on the other hand, ifpci_removebeforepci_release, the interrupt will not got chance to be disabled. So this patch aim to fix this issue by addingpci_releasecall inpci_remove, it will gurranty that all pci clean up will be done before pci removal.Signed-off-by Jeff Guo <[email protected]>--- kernel/linux/igb_uio/igb_uio.c | 2 ++ 1 file changed, 2 insertions(+)kernel/linux/igb_uio/igb_uio.c +0
-0
kernel/linux/igb_uio/igb_uio.c igbuio_pci_remove(struct pci_dev *dev) · line 574
Quoted 5 lines
kernel/linux/igb_uio/igb_uio.c +2
-0@@ -570,6 +570,8 @@ igbuio_pci_remove(struct pci_dev *dev) {struct rte_uio_pci_dev *udev =pci_get_drvdata (dev); +igbuio_pci_release (&udev->info,NULL ); +
Hi Jeff,
This is simpler approach comparing to previous version.
And do you know if igbuio_pci_release() won't be called after igbuio_pci_remove() because that will also cause crash, and indeed it will cause a crash in the uio too.
The flow as far as I can see:
when uioN device opened by application, igbuio_pci_open() is called.
If device removed, I expect driver remove() function called, which has a call stack like below:
igbuio_pci_remove()
uio_unregister_device()
uio_device_release()
kfree(struct uio_device)
After this point udev is freed and igbuio_pci_release() shouldn't be called, so I assume uioN device closed before this point but I couldn't find where, if not closed, closing it later will crash.
I can't test the hotplug case, can you please confirm above patch fixing crashes you observed for your use cases?
And for regular usecase this change shouldn't cause any problem, so at worst it may not be fixing all hotplug issues, which looks safe to get.
Re: [PATCH v1] igb_uio: fix unexpected removal for hot-unplug
Quoted 38 lines · Ferruh Yigit
Quoted 18 lines · Jeff Guo
When a device is hot-unplugged,
pci_removewill be invoked unexpectedly beforepci_release, it will caused kernel hung issue which will throw the error info of "Trying to free already-free IRQ XXX". And on the other hand, ifpci_removebeforepci_release, the interrupt will not got chance to be disabled. So this patch aim to fix this issue by addingpci_releasecall inpci_remove, it will gurranty that all pci clean up will be done before pci removal.Signed-off-by Jeff Guo <[email protected]>--- kernel/linux/igb_uio/igb_uio.c | 2 ++ 1 file changed, 2 insertions(+)kernel/linux/igb_uio/igb_uio.c +0
-0kernel/linux/igb_uio/igb_uio.c igbuio_pci_remove(struct pci_dev *dev) · line 574
Quoted 5 lines
kernel/linux/igb_uio/igb_uio.c +2
-0@@ -570,6 +570,8 @@ igbuio_pci_remove(struct pci_dev *dev) {struct rte_uio_pci_dev *udev =pci_get_drvdata (dev); +igbuio_pci_release (&udev->info,NULL ); +Hi Jeff,
This is simpler approach comparing to previous version.
And do you know if
igbuio_pci_release()won't be called afterigbuio_pci_remove()because that will also cause crash, and indeed it will cause a crash in the uio too.The flow as far as I can see:
when uioN device opened by application,igbuio_pci_open()is called.If device removed, I expect driver
remove()function called, which has a call stack like below:
Quoted 8 lines
igbuio_pci_remove()
uio_unregister_device()
uio_device_release()
kfree(struct uio_device)After this point udev is freed and
igbuio_pci_release()shouldn't be called, so I assume uioN device closed before this point but I couldn't find where, if not closed, closing it later will crash.
What i saw is that after igb_uio remove , if detach the device the pci release will be called, so the
igbuo_pci_release should be called again.
Quoted 2 lines
I can't test the hotplug case, can you please confirm above patch fixing crashes you observed for your use cases?
yes, it could be fix the crashed i observed right now.
Quoted 3 lines
And for regular usecase this change shouldn't cause any problem, so at worst it may not be fixing all hotplug issues, which looks safe to get.
I think it would fix this hung issue that caused of double free irq and would not have side effect anyway.
Ferruh Yigit
Acked-byRe: [PATCH v1] igb_uio: fix unexpected removal for hot-unplug
Quoted 11 lines · Jeff Guo
Quoted 9 lines · Ferruh Yigit
Quoted 8 lines · Jeff Guo
When a device is hot-unplugged,
pci_removewill be invoked unexpectedly beforepci_release, it will caused kernel hung issue which will throw the error info of "Trying to free already-free IRQ XXX". And on the other hand, ifpci_removebeforepci_release, the interrupt will not got chance to be disabled. So this patch aim to fix this issue by addingpci_releasecall inpci_remove, it will gurranty that all pci clean up will be done before pci removal.
Quoted 1 line
Quoted 1 line
Quoted 1 line
Signed-off-by Jeff Guo <[email protected]>
Thomas Monjalon
AppliedRe: [PATCH v1] igb_uio: fix unexpected removal for hot-unplug
22/10/2018 13:13, Ferruh Yigit:
Quoted 12 lines · Jeff Guo
Quoted 11 lines · Ferruh Yigit
Quoted 9 lines · Jeff Guo
Quoted 8 lines
When a device is hot-unplugged,
pci_removewill be invoked unexpectedly beforepci_release, it will caused kernel hung issue which will throw the error info of "Trying to free already-free IRQ XXX". And on the other hand, ifpci_removebeforepci_release, the interrupt will not got chance to be disabled. So this patch aim to fix this issue by addingpci_releasecall inpci_remove, it will gurranty that all pci clean up will be done before pci removal.
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Signed-off-by Jeff Guo <[email protected]>Acked-by Ferruh Yigit <[email protected]>
Applied, thanks
PATCH v10 8/8 testpmd: use hotplug failure handle mechanism
In reply to Jeff GuoThis patch use testpmd for example to show how to use failure handle mechanism for hotplug in app.
---v10->v9:
use new APIs to manage hotplug handling.
--- app/test-pmd/testpmd.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-)
app/test-pmd/testpmd.c +22 -5
@@ -2098,14 +2098,22 @@ pmd_test_exit(void)if (hot_plug) { ret =rte_dev_event_monitor_stop (); -if (ret) +if (ret) {RTE_LOG (ERR ,EAL ,"fail to stop device event monitor." ); +return ; + } ret =eth_dev_event_callback_unregister ();if (ret) +return ; + + ret =rte_dev_hotplug_handle_disable (); +if (ret) {RTE_LOG (ERR ,EAL , -"fail to unregister all event callbacks." ); +"fail to disable hotplug handling." ); +return ; + } }printf ("\nBye...\n" );
@@ -2784,14 +2792,23 @@ main(int argc, char** argv)init_config ();if (hot_plug) { -/* enable hot plug monitoring */ + ret =rte_dev_hotplug_handle_enable (); +if (ret) { +RTE_LOG (ERR ,EAL , +"fail to enable hotplug handling." ); +return -1 ; + } + ret =rte_dev_event_monitor_start ();if (ret) { - rte_errno =EINVAL ; +RTE_LOG (ERR ,EAL , +"fail to start device event monitoring." );return -1 ; } -eth_dev_event_callback_register (); + ret =eth_dev_event_callback_register (); +if (ret) +return -1 ; }if (start_port (RTE_PORT_ALL ) !=0 )
Signature · 1 line
2.7.4
v11 cover hot-unplug failure handle mechanism
In reply to Jeff GuoHotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.
We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.
In the case of hotplug for igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.
In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.
The mechanism should work as below:
First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could seperate implement case by case.
For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate all device on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_diable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.
The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device event monitor->Enable hotplug handle->Register event callback ->attach port->start port->start forwarding->Device unplug->failure handle ->stop forwarding->stop port->close port->detach port.
This patch set would not cover hotplug insert and binding, and it is only implement the igb_uio failure handler, the vfio hotplug failure handler will be in next coming patch set.
patchset history:v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. since we plan to abandon RTE_ETH_EVENT_INTR_RMV, change to use RTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move the igb_uio fixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.
v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log
v9->v8:
refine commit log to be more readable.
v8->v7:
refine errno process in sigbus handler.
refine igb uio release process
v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.
v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.
v3->v2:
change bus ops name to bus_hotplug_handler.
add new API and bus ops of bus_signal_handler distingush handle generic. sigbus and hotplug sigbus.
v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.
Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.
v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.
the prior patch history please check the patch set "add device event monitor framework".
Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug
testpmd: use hot-unplug failure handle mechanism
app/test-pmd/testpmd.c | 39 ++++++-- doc/guides/rel_notes/release_18_08.rst | 5 + drivers/bus/pci/pci_common.c | 81 ++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++ drivers/bus/pci/private.h | 12 +++ lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_common_bus.c | 43 +++++++++ lib/librte_eal/common/eal_private.h | 39 ++++++++ lib/librte_eal/common/include/rte_bus.h | 34 +++++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 162 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 12 files changed, 481 insertions(+), 9 deletions(-)
Signature · 1 line
2.7.4
PATCH v11 1/7 bus: add hot-unplug handler
A hot-unplug failure and app crash can be caused, when a device is hot-unplugged but the application still try to access the device by reading or writing from the BARs, which is already invalid but still not timely be unmap or released.
This patch introduces bus ops to handle hot-unplug failures. Each bus can implement its own case-dependent logic to handle the failures.
---v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops.
--- lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);typedef int (*rte_bus_parse_t )(const char *name,void *addr);/** +* Implement a specific hot-unplug handler, which is responsible for +* handle the failure when device be hot-unplugged. When the event of +* hot-unplug be detected, it could call this function to handle +* the hot-unplug failure and avoid app crash. +* @param dev +* Pointer of the device structure. +* +* @return +* 0 on success. +* !0 on error. +*/ +typedef int (*rte_bus_hot_unplug_handler_t )(struct rte_device *dev); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -212,6 +226,8 @@ struct rte_bus {struct rte_bus_conf conf;/**< Bus configuration */ rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ rte_dev_iterate_t dev_iterate;/**< Device iterator. */ +rte_bus_hot_unplug_handler_t hot_unplug_handler; +/**< handle hot-unplug failure on the bus */ };/**
Signature · 1 line
2.7.4
PATCH v11 2/7 bus/pci: implement hot-unplug handler ops
In reply to Jeff GuoThis patch implements the ops to handle hot-unplug on the PCI bus. For UIO PCI, it could avoids BARs read/write errors by creating a new dummy memory to remap the memory where the failure is. For VFIO or other kernel driver, it could specific implement function to handle hot-unplug case by case.
---v11->v10:
change the ops name
--- drivers/bus/pci/pci_common.c | 28 ++++++++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++ drivers/bus/pci/private.h | 12 ++++++++++++ 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
@@ -406,6 +406,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, }static int +pci_hot_unplug_handler (struct rte_device *dev) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =RTE_DEV_TO_PCI (dev); +if (!pdev) +return -1 ; + +switch (pdev->kdrv) { +case RTE_KDRV_IGB_UIO : +case RTE_KDRV_UIO_GENERIC : +case RTE_KDRV_NIC_UIO : +/* BARs resource is invalid, remap it to be safe. */ + ret =pci_uio_remap_resource (pdev); +break ; +default : +RTE_LOG (DEBUG ,EAL , +"Not managed by a supported kernel driver, skipped\n" ); + ret =-1 ; +break ; + } + +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -435,6 +462,7 @@ struct rte_pci_bus rte_pci_bus = { .unplug = pci_unplug, .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, + .hot_unplug_handler = pci_hot_unplug_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res) } } +/* remap the PCI resource of a PCI device in anonymous virtual memory */ +int +pci_uio_remap_resource (struct rte_pci_device *dev) +{ +int i; +void *map_address; + +if (dev ==NULL ) +return -1 ; + +/* Remap all BARs */ +for (i =0 ; i !=PCI_MAX_RESOURCE ; i++) { +/* skip empty BAR */ +if (dev->mem_resource[i].phys_addr ==0 ) +continue ; + map_address =mmap (dev->mem_resource[i].addr, + (size_t )dev->mem_resource[i].len, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ); +if (map_address ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL , +"Cannot remap resource for device %s\n" , + dev->name); +return -1 ; + } +RTE_LOG (INFO ,EAL , +"Successful remap resource for device %s\n" , + dev->name); + } + +return 0 ; +} +static struct mapped_pci_resource *pci_uio_find_resource (struct rte_pci_device *dev) {
drivers/bus/pci/private.h +12 -0
@@ -123,6 +123,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev, struct mapped_pci_resource *uio_res);/** +* Remap the PCI resource of a PCI device in anonymous virtual memory. +* +* @param dev +* Point to the struct rte pci device. +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +pci_uio_remap_resource (struct rte_pci_device *dev); + +/** * Map device memory to uio resource * * This function is private to EAL.
Signature · 1 line
2.7.4
PATCH v11 3/7 bus: add sigbus handler
In reply to Jeff GuoWhen a device is hot-unplugged, a sigbus error will occur of the datapath can still read/write to the device. A handler is required here to capture the sigbus signal and handle it appropriately.
This patch introduces a bus ops to handle sigbus errors. Each bus can implement its own case-dependent logic to handle the sigbus errors.
---v11->v10:
change some commit log
--- lib/librte_eal/common/include/rte_bus.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
lib/librte_eal/common/include/rte_bus.h +18 -0
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);typedef int (*rte_bus_hot_unplug_handler_t )(struct rte_device *dev);/** +* Implement a specific sigbus handler, which is responsible for handling +* the sigbus error which is either original memory error, or specific memory +* error that caused of device be hot-unplugged. When sigbus error be captured, +* it could call this function to handle sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 for success handle the sigbus. +* 1 for no bus handle the sigbus. +* -1 for failed to handle the sigbus +*/ +typedef int (*rte_bus_sigbus_handler_t )(const void *failure_addr); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -228,6 +243,9 @@ struct rte_bus {rte_dev_iterate_t dev_iterate;/**< Device iterator. */ rte_bus_hot_unplug_handler_t hot_unplug_handler;/**< handle hot-unplug failure on the bus */ +rte_bus_sigbus_handler_t sigbus_handler; +/**< handle sigbus error on the bus */ + };/**
Signature · 1 line
2.7.4
PATCH v11 4/7 bus/pci: implement sigbus handler ops
In reply to Jeff GuoThis patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hot-unplugged and calls the relevant ops of the hot-unplug handler to handle the hot-unplug failure of the device.
---v11->v10:
change commit log.
--- drivers/bus/pci/pci_common.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
drivers/bus/pci/pci_common.c +53 -0
@@ -405,6 +405,36 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,return NULL ; } +/** +* find the device which encounter the failure, by iterate over all device on +* PCI bus to check if the memory failure address is located in the range +* of the BARs of the device. +*/ +static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int i; + +FOREACH_DEVICE_ON_PCIBUS (pdev) { +for (i =0 ; i !=RTE_DIM (pdev->mem_resource); i++) { +if ((uint64_t )(uintptr_t )failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t )(uintptr_t )failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) { +RTE_LOG (INFO ,EAL ,"Failure address " +"%16.16" PRIx64" belongs to " +"device %s!\n" , + (uint64_t )(uintptr_t )failure_addr, + pdev->device.name); +return pdev; + } + } + } +return NULL ; +} +static int pci_hot_unplug_handler (struct rte_device *dev) {
@@ -433,6 +463,28 @@ pci_hot_unplug_handler(struct rte_device *dev) }static int +pci_sigbus_handler (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =pci_find_device_by_addr (failure_addr); +if (!pdev) { +/* It is a generic sigbus error, no bus would handle it. */ + ret =1 ; + }else { +/* The sigbus error is caused of hot-unplug. */ + ret =pci_hot_unplug_handler (&pdev->device); +if (ret) { +RTE_LOG (ERR ,EAL ,"Failed to handle hot-unplug for " +"device %s" , pdev->name); + ret =-1 ; + } + } +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -463,6 +515,7 @@ struct rte_pci_bus rte_pci_bus = { .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, .hot_unplug_handler = pci_hot_unplug_handler, + .sigbus_handler = pci_sigbus_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
PATCH v11 5/7 bus: add helper to handle sigbus
In reply to Jeff GuoThis patch aims to add a helper to iterate over all buses to find the relevant bus to handle the sigbus error.
---v11->v10:
change some words.
--- lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 13 ++++++++++ 2 files changed, 56 insertions(+)
lib/librte_eal/common/eal_common_bus.c +43 -0
@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) +return -1 ; + + ret = bus->sigbus_handler (failure_addr); + +/* find bus but handle failed, keep the errno be set. */ +if (ret <0 && rte_errno ==0 ) + rte_errno =ENOTSUP ; + +return ret >0 ; +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* can not find bus. */ +if (!bus) +return 1 ; +/* find bus but handle failed, pass on the new errno. */ +else if (rte_errno !=0 ) +return -1 ; + +/* restore the old errno. */ + rte_errno = old_errno; + +return ret; +}
lib/librte_eal/common/eal_private.h +13 -0
@@ -304,4 +304,17 @@ int rte_devargs_layers_parse(struct rte_devargs *devargs, const char *devstr); +/** +* Iterate over all buses to find the corresponding bus to handle the sigbus +* error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
PATCH v11 6/7 eal: add failure handle mechanism for hot-unplug
In reply to Jeff GuoThe mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.
By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
- rte_dev_hotplug_handle_enable
- rte_dev_hotplug_handle_disable
---v11->v10:
change some words and change the invoked func name.
--- doc/guides/rel_notes/release_18_08.rst | 5 + lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_private.h | 26 +++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 162 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 6 files changed, 234 insertions(+), 1 deletion(-)
doc/guides/rel_notes/release_18_08.rst +5 -0
@@ -117,6 +117,11 @@ New Features Added support for chained mbufs (input and output). +* **Added hot-unplug handle mechanism.** + + ``rte_dev_hotplug_handle_enable`` and ``rte_dev_hotplug_handle_disable`` are + for enabling or disabling hotplug handle mechanism. + API Changes -----------
lib/librte_eal/bsdapp/eal/eal_dev.c +14 -0
@@ -19,3 +19,17 @@ rte_dev_event_monitor_stop(void)RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" );return -1 ; } + +int +rte_dev_hotplug_handle_enable (void ) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +} + +int +rte_dev_hotplug_handle_disable (void ) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +}
lib/librte_eal/common/eal_private.h +26 -0
@@ -317,4 +317,30 @@ rte_devargs_layers_parse(struct rte_devargs *devargs,*/ int rte_bus_sigbus_handler (const void *failure_addr); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Register the sigbus handler. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_register (void ); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Unregister the sigbus handler. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_unregister (void ); +#endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/include/rte_dev.h +26 -0
@@ -460,4 +460,30 @@ rte_dev_event_monitor_start(void); int __rte_experimentalrte_dev_event_monitor_stop (void); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Enable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_enable (void); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Disable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_disable (void); +#endif /* _RTE_DEV_H_ */
lib/librte_eal/linuxapp/eal/eal_dev.c +161 -1
207 lines not on this page · raw message
lib/librte_eal/rte_eal_version.map +2 -0
@@ -281,6 +281,8 @@ EXPERIMENTAL { rte_dev_event_callback_unregister; rte_dev_event_monitor_start; rte_dev_event_monitor_stop; + rte_dev_hotplug_handle_enable; + rte_dev_hotplug_handle_disable; rte_dev_iterator_init; rte_dev_iterator_next; rte_devargs_add;
Signature · 1 line
2.7.4
PATCH v11 7/7 testpmd: use hot-unplug failure handle mechanism
In reply to Jeff GuoThis patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot-unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.
---v11->v10:
since we plan to abandon RTE_ETH_EVENT_INTR_RMV, change to use RTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage.
--- app/test-pmd/testpmd.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-)
app/test-pmd/testpmd.c +31 -8
@@ -2093,14 +2093,22 @@ pmd_test_exit(void)if (hot_plug) { ret =rte_dev_event_monitor_stop (); -if (ret) +if (ret) {RTE_LOG (ERR ,EAL ,"fail to stop device event monitor." ); +return ; + } ret =eth_dev_event_callback_unregister ();if (ret) +return ; + + ret =rte_dev_hotplug_handle_disable (); +if (ret) {RTE_LOG (ERR ,EAL , -"fail to unregister all event callbacks." ); +"fail to disable hotplug handling." ); +return ; + } }printf ("\nBye...\n" );
@@ -2244,6 +2252,9 @@ static void eth_dev_event_callback(char *device_name, enum rte_dev_event_type type, __rte_unused void *arg) { +uint16_t port_id; +int ret; +if (type >=RTE_DEV_EVENT_MAX ) {fprintf (stderr,"%s called upon invalid event %d\n" , __func__, type);
@@ -2254,9 +2265,12 @@ eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,case RTE_DEV_EVENT_REMOVE :RTE_LOG (ERR ,EAL ,"The device: %s has been removed!\n" , device_name); -/* TODO: After finish failure handle, begin to stop -* packet forward, stop port, close port, detach port. -*/ + ret =rte_eth_dev_get_port_by_name (device_name, &port_id); +if (ret) { +printf ("can not get port by device %s!\n" , device_name); +return ; + } +rmv_event_callback ((void *)(intptr_t )port_id);break ;case RTE_DEV_EVENT_ADD : RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
@@ -2779,14 +2793,23 @@ main(int argc, char** argv)init_config ();if (hot_plug) { -/* enable hot plug monitoring */ + ret =rte_dev_hotplug_handle_enable (); +if (ret) { +RTE_LOG (ERR ,EAL , +"fail to enable hotplug handling." ); +return -1 ; + } + ret =rte_dev_event_monitor_start ();if (ret) { - rte_errno =EINVAL ; +RTE_LOG (ERR ,EAL , +"fail to start device event monitoring." );return -1 ; } -eth_dev_event_callback_register (); + ret =eth_dev_event_callback_register (); +if (ret) +return -1 ; }if (start_port (RTE_PORT_ALL ) !=0 )
Signature · 1 line
2.7.4
v11 cover hot-unplug failure handle mechanism
In reply to Jeff GuoHotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.
We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.
In the case of hotplug for igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.
In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.
The mechanism should work as below:
First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could seperate implement case by case.
For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate all device on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_diable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.
The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device event monitor->Enable hotplug handle->Register event callback ->attach port->start port->start forwarding->Device unplug->failure handle ->stop forwarding->stop port->close port->detach port.
This patch set would not cover hotplug insert and binding, and it is only implement the igb_uio failure handler, the vfio hotplug failure handler will be in next coming patch set.
patchset history:v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. add experimental tag.
since we plan to abandon RTE_ETH_EVENT_INTR_RMV, change to use RTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move the igb_uio fixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.
v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log
v9->v8:
refine commit log to be more readable.
v8->v7:
refine errno process in sigbus handler.
refine igb uio release process
v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.
v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.
v3->v2:
change bus ops name to bus_hotplug_handler.
add new API and bus ops of bus_signal_handler distingush handle generic. sigbus and hotplug sigbus.
v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.
Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.
v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.
the prior patch history please check the patch set "add device event monitor framework".
Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug
testpmd: use hot-unplug failure handle mechanism
app/test-pmd/testpmd.c | 39 ++++++-- doc/guides/rel_notes/release_18_08.rst | 5 + drivers/bus/pci/pci_common.c | 81 ++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++ drivers/bus/pci/private.h | 12 +++ lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_common_bus.c | 43 +++++++++ lib/librte_eal/common/eal_private.h | 39 ++++++++ lib/librte_eal/common/include/rte_bus.h | 34 +++++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 162 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 12 files changed, 481 insertions(+), 9 deletions(-)
Signature · 1 line
2.7.4
PATCH v11 1/7 bus: add hot-unplug handler
A hot-unplug failure and app crash can be caused, when a device is hot-unplugged but the application still try to access the device by reading or writing from the BARs, which is already invalid but still not timely be unmap or released.
This patch introduces bus ops to handle hot-unplug failures. Each bus can implement its own case-dependent logic to handle the failures.
---v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops.
--- lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);typedef int (*rte_bus_parse_t )(const char *name,void *addr);/** +* Implement a specific hot-unplug handler, which is responsible for +* handle the failure when device be hot-unplugged. When the event of +* hot-unplug be detected, it could call this function to handle +* the hot-unplug failure and avoid app crash. +* @param dev +* Pointer of the device structure. +* +* @return +* 0 on success. +* !0 on error. +*/ +typedef int (*rte_bus_hot_unplug_handler_t )(struct rte_device *dev); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -212,6 +226,8 @@ struct rte_bus {struct rte_bus_conf conf;/**< Bus configuration */ rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ rte_dev_iterate_t dev_iterate;/**< Device iterator. */ +rte_bus_hot_unplug_handler_t hot_unplug_handler; +/**< handle hot-unplug failure on the bus */ };/**
Signature · 1 line
2.7.4
PATCH v11 2/7 bus/pci: implement hot-unplug handler ops
In reply to Jeff GuoThis patch implements the ops to handle hot-unplug on the PCI bus. For UIO PCI, it could avoids BARs read/write errors by creating a new dummy memory to remap the memory where the failure is. For VFIO or other kernel driver, it could specific implement function to handle hot-unplug case by case.
---v11->v10:
change the ops name
--- drivers/bus/pci/pci_common.c | 28 ++++++++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++ drivers/bus/pci/private.h | 12 ++++++++++++ 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
@@ -406,6 +406,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, }static int +pci_hot_unplug_handler (struct rte_device *dev) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =RTE_DEV_TO_PCI (dev); +if (!pdev) +return -1 ; + +switch (pdev->kdrv) { +case RTE_KDRV_IGB_UIO : +case RTE_KDRV_UIO_GENERIC : +case RTE_KDRV_NIC_UIO : +/* BARs resource is invalid, remap it to be safe. */ + ret =pci_uio_remap_resource (pdev); +break ; +default : +RTE_LOG (DEBUG ,EAL , +"Not managed by a supported kernel driver, skipped\n" ); + ret =-1 ; +break ; + } + +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -435,6 +462,7 @@ struct rte_pci_bus rte_pci_bus = { .unplug = pci_unplug, .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, + .hot_unplug_handler = pci_hot_unplug_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res) } } +/* remap the PCI resource of a PCI device in anonymous virtual memory */ +int +pci_uio_remap_resource (struct rte_pci_device *dev) +{ +int i; +void *map_address; + +if (dev ==NULL ) +return -1 ; + +/* Remap all BARs */ +for (i =0 ; i !=PCI_MAX_RESOURCE ; i++) { +/* skip empty BAR */ +if (dev->mem_resource[i].phys_addr ==0 ) +continue ; + map_address =mmap (dev->mem_resource[i].addr, + (size_t )dev->mem_resource[i].len, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ); +if (map_address ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL , +"Cannot remap resource for device %s\n" , + dev->name); +return -1 ; + } +RTE_LOG (INFO ,EAL , +"Successful remap resource for device %s\n" , + dev->name); + } + +return 0 ; +} +static struct mapped_pci_resource *pci_uio_find_resource (struct rte_pci_device *dev) {
drivers/bus/pci/private.h +12 -0
@@ -123,6 +123,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev, struct mapped_pci_resource *uio_res);/** +* Remap the PCI resource of a PCI device in anonymous virtual memory. +* +* @param dev +* Point to the struct rte pci device. +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +pci_uio_remap_resource (struct rte_pci_device *dev); + +/** * Map device memory to uio resource * * This function is private to EAL.
Signature · 1 line
2.7.4
PATCH v11 3/7 bus: add sigbus handler
In reply to Jeff GuoWhen a device is hot-unplugged, a sigbus error will occur of the datapath can still read/write to the device. A handler is required here to capture the sigbus signal and handle it appropriately.
This patch introduces a bus ops to handle sigbus errors. Each bus can implement its own case-dependent logic to handle the sigbus errors.
---v11->v10:
change some commit log
--- lib/librte_eal/common/include/rte_bus.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
lib/librte_eal/common/include/rte_bus.h +18 -0
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);typedef int (*rte_bus_hot_unplug_handler_t )(struct rte_device *dev);/** +* Implement a specific sigbus handler, which is responsible for handling +* the sigbus error which is either original memory error, or specific memory +* error that caused of device be hot-unplugged. When sigbus error be captured, +* it could call this function to handle sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 for success handle the sigbus. +* 1 for no bus handle the sigbus. +* -1 for failed to handle the sigbus +*/ +typedef int (*rte_bus_sigbus_handler_t )(const void *failure_addr); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -228,6 +243,9 @@ struct rte_bus {rte_dev_iterate_t dev_iterate;/**< Device iterator. */ rte_bus_hot_unplug_handler_t hot_unplug_handler;/**< handle hot-unplug failure on the bus */ +rte_bus_sigbus_handler_t sigbus_handler; +/**< handle sigbus error on the bus */ + };/**
Signature · 1 line
2.7.4
PATCH v11 4/7 bus/pci: implement sigbus handler ops
In reply to Jeff GuoThis patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hot-unplugged and calls the relevant ops of the hot-unplug handler to handle the hot-unplug failure of the device.
---v11->v10:
change commit log.
--- drivers/bus/pci/pci_common.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
drivers/bus/pci/pci_common.c +53 -0
@@ -405,6 +405,36 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,return NULL ; } +/** +* find the device which encounter the failure, by iterate over all device on +* PCI bus to check if the memory failure address is located in the range +* of the BARs of the device. +*/ +static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int i; + +FOREACH_DEVICE_ON_PCIBUS (pdev) { +for (i =0 ; i !=RTE_DIM (pdev->mem_resource); i++) { +if ((uint64_t )(uintptr_t )failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t )(uintptr_t )failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) { +RTE_LOG (INFO ,EAL ,"Failure address " +"%16.16" PRIx64" belongs to " +"device %s!\n" , + (uint64_t )(uintptr_t )failure_addr, + pdev->device.name); +return pdev; + } + } + } +return NULL ; +} +static int pci_hot_unplug_handler (struct rte_device *dev) {
@@ -433,6 +463,28 @@ pci_hot_unplug_handler(struct rte_device *dev) }static int +pci_sigbus_handler (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =pci_find_device_by_addr (failure_addr); +if (!pdev) { +/* It is a generic sigbus error, no bus would handle it. */ + ret =1 ; + }else { +/* The sigbus error is caused of hot-unplug. */ + ret =pci_hot_unplug_handler (&pdev->device); +if (ret) { +RTE_LOG (ERR ,EAL ,"Failed to handle hot-unplug for " +"device %s" , pdev->name); + ret =-1 ; + } + } +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -463,6 +515,7 @@ struct rte_pci_bus rte_pci_bus = { .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, .hot_unplug_handler = pci_hot_unplug_handler, + .sigbus_handler = pci_sigbus_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
PATCH v11 5/7 bus: add helper to handle sigbus
In reply to Jeff GuoThis patch aims to add a helper to iterate over all buses to find the relevant bus to handle the sigbus error.
---v11->v10:
change some words.
--- lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 13 ++++++++++ 2 files changed, 56 insertions(+)
lib/librte_eal/common/eal_common_bus.c +43 -0
@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) +return -1 ; + + ret = bus->sigbus_handler (failure_addr); + +/* find bus but handle failed, keep the errno be set. */ +if (ret <0 && rte_errno ==0 ) + rte_errno =ENOTSUP ; + +return ret >0 ; +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* can not find bus. */ +if (!bus) +return 1 ; +/* find bus but handle failed, pass on the new errno. */ +else if (rte_errno !=0 ) +return -1 ; + +/* restore the old errno. */ + rte_errno = old_errno; + +return ret; +}
lib/librte_eal/common/eal_private.h +13 -0
@@ -304,4 +304,17 @@ int rte_devargs_layers_parse(struct rte_devargs *devargs, const char *devstr); +/** +* Iterate over all buses to find the corresponding bus to handle the sigbus +* error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
PATCH v11 6/7 eal: add failure handle mechanism for hot-unplug
In reply to Jeff GuoThe mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.
By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
- rte_dev_hotplug_handle_enable
- rte_dev_hotplug_handle_disable
---v11->v10:
change some words and change the invoked func name. add experimental tag
--- doc/guides/rel_notes/release_18_08.rst | 5 + lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_private.h | 26 +++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 162 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 6 files changed, 234 insertions(+), 1 deletion(-)
doc/guides/rel_notes/release_18_08.rst +5 -0
@@ -117,6 +117,11 @@ New Features Added support for chained mbufs (input and output). +* **Added hot-unplug handle mechanism.** + + ``rte_dev_hotplug_handle_enable`` and ``rte_dev_hotplug_handle_disable`` are + for enabling or disabling hotplug handle mechanism. + API Changes -----------
lib/librte_eal/bsdapp/eal/eal_dev.c +14 -0
@@ -19,3 +19,17 @@ rte_dev_event_monitor_stop(void)RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" );return -1 ; } + +int __rte_experimental +rte_dev_hotplug_handle_enable (void) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +} + +int __rte_experimental +rte_dev_hotplug_handle_disable (void) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +}
lib/librte_eal/common/eal_private.h +26 -0
@@ -317,4 +317,30 @@ rte_devargs_layers_parse(struct rte_devargs *devargs,*/ int rte_bus_sigbus_handler (const void *failure_addr); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Register the sigbus handler. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_register (void ); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Unregister the sigbus handler. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_unregister (void ); +#endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/include/rte_dev.h +26 -0
@@ -460,4 +460,30 @@ rte_dev_event_monitor_start(void); int __rte_experimentalrte_dev_event_monitor_stop (void); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Enable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_enable (void); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Disable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_disable (void); +#endif /* _RTE_DEV_H_ */
lib/librte_eal/linuxapp/eal/eal_dev.c +161 -1
207 lines not on this page · raw message
lib/librte_eal/rte_eal_version.map +2 -0
@@ -281,6 +281,8 @@ EXPERIMENTAL { rte_dev_event_callback_unregister; rte_dev_event_monitor_start; rte_dev_event_monitor_stop; + rte_dev_hotplug_handle_enable; + rte_dev_hotplug_handle_disable; rte_dev_iterator_init; rte_dev_iterator_next; rte_devargs_add;
Signature · 1 line
2.7.4
Re: [PATCH v11 6/7] eal: add failure handle mechanism for hot-unplug
Hi Jeff,
Quoted 294 lines
The mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.
By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
-rte_dev_hotplug_handle_enable
-rte_dev_hotplug_handle_disableSigned-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v11->v10:
change some words and change the invoked func name. add experimental tag--- doc/guides/rel_notes/release_18_08.rst | 5 + lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_private.h | 26 +++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 162 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 6 files changed, 234 insertions(+), 1 deletion(-)doc/guides/rel_notes/release_18_08.rst +5
-0@@ -117,6 +117,11 @@ New Features Added support for chained mbufs (input and output). +* **Added hot-unplug handle mechanism.** + + ``rte_dev_hotplug_handle_enable`` and ``rte_dev_hotplug_handle_disable`` are + for enabling or disabling hotplug handle mechanism. + API Changes -----------lib/librte_eal/bsdapp/eal/eal_dev.c +14
-0@@ -19,3 +19,17 @@ rte_dev_event_monitor_stop(void)RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" );return -1 ; } + +int __rte_experimental +rte_dev_hotplug_handle_enable (void) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +} + +int __rte_experimental +rte_dev_hotplug_handle_disable (void) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +}lib/librte_eal/common/eal_private.h +26
-0@@ -317,4 +317,30 @@ rte_devargs_layers_parse(struct rte_devargs *devargs,*/ int rte_bus_sigbus_handler (const void *failure_addr); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Register the sigbus handler. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_register (void ); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Unregister the sigbus handler. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_unregister (void ); +#endif /* _EAL_PRIVATE_H_ */ lib/librte_eal/common/include/rte_dev.h +26
-0@@ -460,4 +460,30 @@ rte_dev_event_monitor_start(void); int __rte_experimentalrte_dev_event_monitor_stop (void); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Enable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_enable (void); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Disable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_disable (void); +#endif /* _RTE_DEV_H_ */ lib/librte_eal/linuxapp/eal/eal_dev.c +102
-1@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h> @@ -14,15 +16,32 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h> +#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h" static struct rte_intr_handle intr_handle = {.fd =-1 };static bool monitor_started; +static bool hotplug_handle;#define EAL_UEV_MSG_LEN 4096#define EAL_UEV_MSG_ELEM_LEN 128 +/* +* spinlock for device hot-unplug failure handling. If it try to access bus or +* device, such as handle sigbus on bus or handle memory failure for device +* just need to use this lock. It could protect the bus and the device to avoid +* race condition. +*/ +static rte_spinlock_t failure_handle_lock =RTE_SPINLOCK_INITIALIZER ; + +static struct sigaction sigbus_action_old; + +static int sigbus_need_recover; + static void dev_uev_handler(__rte_unused void *param);/* identify the system layer which reports this event. */ @@ -33,6 +52,49 @@ enum eal_dev_event_subsystem {EAL_DEV_EVENT_SUBSYSTEM_MAX }; +static void +sigbus_action_recover (void ) +{ +if (sigbus_need_recover) { +sigaction (SIGBUS , &sigbus_action_old,NULL ); + sigbus_need_recover =0 ; + } +} + +static void sigbus_handler (int signum,siginfo_t *info, + void *ctx __rte_unused) +{ +int ret; + +RTE_LOG (INFO ,EAL ,"Thread[%d] catch SIGBUS, fault address:%p\n" , + (int )pthread_self (), info->si_addr); + +rte_spinlock_lock (&failure_handle_lock); + ret =rte_bus_sigbus_handler (info->si_addr); +rte_spinlock_unlock (&failure_handle_lock); +if (ret ==-1 ) { +rte_exit (EXIT_FAILURE , +"Failed to handle SIGBUS for hot-unplug, " +"(rte_errno: %s)!" ,strerror (rte_errno)); + }else if (ret ==1 ) { +if (sigbus_action_old.sa_handler) + (*(sigbus_action_old.sa_handler))(signum); +else +rte_exit (EXIT_FAILURE , +"Failed to handle generic SIGBUS!" ); + } + +RTE_LOG (INFO ,EAL ,"Success to handle SIGBUS for hot-unplug!\n" ); +} + +static int cmp_dev_name (const struct rte_device *dev, +const void *_name) +{ +const char *name = _name; + +return strcmp (dev->name, name); +} +static int dev_uev_socket_fd_create (void ) {@@ -147,6 +209,9 @@ dev_uev_handler(__rte_unused void *param)struct rte_dev_event uevent;int ret;char buf[EAL_UEV_MSG_LEN ]; +struct rte_bus *bus; +struct rte_device *dev; +const char *busname ="" ;memset (&uevent,0 ,sizeof (struct rte_dev_event ));memset (buf,0 ,EAL_UEV_MSG_LEN );@@ -171,8 +236,43 @@ dev_uev_handler(__rte_unused void *param)RTE_LOG (DEBUG ,EAL ,"receive uevent(name:%s, type:%d, subsystem:%d)\n" , uevent.devname, uevent.type, uevent.subsystem); -if (uevent.devname) +switch (uevent.subsystem) { +case EAL_DEV_EVENT_SUBSYSTEM_PCI : +case EAL_DEV_EVENT_SUBSYSTEM_UIO : + busname ="pci" ; +break ; +default : +break ; + } + +if (uevent.devname) { +if (uevent.type ==RTE_DEV_EVENT_REMOVE && hotplug_handle) { +rte_spinlock_lock (&failure_handle_lock); + bus =rte_bus_find_by_name (busname); +if (bus ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find bus (%s)\n" , + busname); +return ; + } + + dev = bus->find_device (NULL , cmp_dev_name, + uevent.devname); +if (dev ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find device (%s) on " +"bus (%s)\n" , uevent.devname, busname); +return ; + } + + ret = bus->hot_unplug_handler (dev); +rte_spinlock_unlock (&failure_handle_lock); +if (ret) { +RTE_LOG (ERR ,EAL ,"Can not handle hot-unplug " +"for device (%s)\n" , dev->name); +return ; + } + }dev_callback_process (uevent.devname, uevent.type); + } }int __rte_experimental@@ -220,5 +320,65 @@ rte_dev_event_monitor_stop(void)close (intr_handle.fd); intr_handle.fd =-1 ; monitor_started = false; +return 0 ;
lib/librte_eal/linuxapp/eal/eal_dev.c rte_dev_event_monitor_stop(void) · line 334
Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +9
-0} + +int __rte_experimental +rte_dev_sigbus_handler_register (void) +{ +sigset_t mask; +struct sigaction action; + + rte_errno =0 ; +
Shouldn't you call sigaction only if sigbus_need_recover == 0?
Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +5
-0+sigemptyset (&mask); +sigaddset (&mask,SIGBUS ); + action.sa_flags =SA_SIGINFO ; + action.sa_mask = mask; + action.sa_sigaction = sigbus_handler;
lib/librte_eal/linuxapp/eal/eal_dev.c rte_dev_event_monitor_stop(void) · line 349
Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +10
-0+ sigbus_need_recover = !sigaction (SIGBUS , &action, &sigbus_action_old); + +return rte_errno; +} + +int __rte_experimental +rte_dev_sigbus_handler_unregister (void) +{ + rte_errno =0 ; + sigbus_need_recover =1 ;
Hmm, why to set sugbus_need_recover to 1 here?
If user called rte_dev_sigbus_handler_register() before, and it was successful, it already would be 1. In other cases, you probably don't have to do anything.
Konstantin
Quoted 50 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +35
-0+ +sigbus_action_recover (); + +return rte_errno; +} + +int __rte_experimental +rte_dev_hotplug_handle_enable (void) +{ +int ret =0 ; + + ret =rte_dev_sigbus_handler_register (); +if (ret <0 ) +RTE_LOG (ERR ,EAL ,"fail to register sigbus handler for " +"devices.\n" ); + + hotplug_handle = true; + +return ret; +} + +int __rte_experimental +rte_dev_hotplug_handle_disable (void) +{ +int ret =0 ; + + ret =rte_dev_sigbus_handler_unregister (); +if (ret <0 ) +RTE_LOG (ERR ,EAL ,"fail to unregister sigbus handler for " +"devices.\n" ); + + hotplug_handle = false; + +return ret; +}lib/librte_eal/rte_eal_version.map +2
-1@@ -281,6 +281,8 @@ EXPERIMENTAL { rte_dev_event_callback_unregister; rte_dev_event_monitor_start; rte_dev_event_monitor_stop; + rte_dev_hotplug_handle_enable; + rte_dev_hotplug_handle_disable; rte_dev_iterator_init; rte_dev_iterator_next; rte_devargs_add; --2.7.4
Re: [PATCH v11 6/7] eal: add failure handle mechanism for hot-unplug
hi, constantin
Quoted 298 lines · Konstantin
Hi Jeff,
Quoted 296 lines
The mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.
By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
-rte_dev_hotplug_handle_enable
-rte_dev_hotplug_handle_disableSigned-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v11->v10:
change some words and change the invoked func name. add experimental tag--- doc/guides/rel_notes/release_18_08.rst | 5 + lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_private.h | 26 +++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 162 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 6 files changed, 234 insertions(+), 1 deletion(-)doc/guides/rel_notes/release_18_08.rst +5
-0@@ -117,6 +117,11 @@ New Features Added support for chained mbufs (input and output). +* **Added hot-unplug handle mechanism.** + + ``rte_dev_hotplug_handle_enable`` and ``rte_dev_hotplug_handle_disable`` are + for enabling or disabling hotplug handle mechanism. + API Changes -----------lib/librte_eal/bsdapp/eal/eal_dev.c +14
-0@@ -19,3 +19,17 @@ rte_dev_event_monitor_stop(void)RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" );return -1 ; } + +int __rte_experimental +rte_dev_hotplug_handle_enable (void) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +} + +int __rte_experimental +rte_dev_hotplug_handle_disable (void) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +}lib/librte_eal/common/eal_private.h +26
-0@@ -317,4 +317,30 @@ rte_devargs_layers_parse(struct rte_devargs *devargs,*/ int rte_bus_sigbus_handler (const void *failure_addr); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Register the sigbus handler. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_register (void ); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Unregister the sigbus handler. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_unregister (void ); +#endif /* _EAL_PRIVATE_H_ */ lib/librte_eal/common/include/rte_dev.h +26
-0@@ -460,4 +460,30 @@ rte_dev_event_monitor_start(void); int __rte_experimentalrte_dev_event_monitor_stop (void); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Enable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_enable (void); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Disable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_disable (void); +#endif /* _RTE_DEV_H_ */ lib/librte_eal/linuxapp/eal/eal_dev.c +104
-1@@ -4,6 +4,8 @@#include <string.h> #include <unistd.h> +#include <fcntl.h> +#include <signal.h> #include <sys/socket.h> #include <linux/netlink.h> @@ -14,15 +16,32 @@#include <rte_malloc.h> #include <rte_interrupts.h> #include <rte_alarm.h> +#include <rte_bus.h> +#include <rte_eal.h> +#include <rte_spinlock.h> +#include <rte_errno.h> #include "eal_private.h" static struct rte_intr_handle intr_handle = {.fd =-1 };static bool monitor_started; +static bool hotplug_handle;#define EAL_UEV_MSG_LEN 4096#define EAL_UEV_MSG_ELEM_LEN 128 +/* +* spinlock for device hot-unplug failure handling. If it try to access bus or +* device, such as handle sigbus on bus or handle memory failure for device +* just need to use this lock. It could protect the bus and the device to avoid +* race condition. +*/ +static rte_spinlock_t failure_handle_lock =RTE_SPINLOCK_INITIALIZER ; + +static struct sigaction sigbus_action_old; + +static int sigbus_need_recover; + static void dev_uev_handler(__rte_unused void *param);/* identify the system layer which reports this event. */ @@ -33,6 +52,49 @@ enum eal_dev_event_subsystem {EAL_DEV_EVENT_SUBSYSTEM_MAX }; +static void +sigbus_action_recover (void ) +{ +if (sigbus_need_recover) { +sigaction (SIGBUS , &sigbus_action_old,NULL ); + sigbus_need_recover =0 ; + } +} + +static void sigbus_handler (int signum,siginfo_t *info, + void *ctx __rte_unused) +{ +int ret; + +RTE_LOG (INFO ,EAL ,"Thread[%d] catch SIGBUS, fault address:%p\n" , + (int )pthread_self (), info->si_addr); + +rte_spinlock_lock (&failure_handle_lock); + ret =rte_bus_sigbus_handler (info->si_addr); +rte_spinlock_unlock (&failure_handle_lock); +if (ret ==-1 ) { +rte_exit (EXIT_FAILURE , +"Failed to handle SIGBUS for hot-unplug, " +"(rte_errno: %s)!" ,strerror (rte_errno)); + }else if (ret ==1 ) { +if (sigbus_action_old.sa_handler) + (*(sigbus_action_old.sa_handler))(signum); +else +rte_exit (EXIT_FAILURE , +"Failed to handle generic SIGBUS!" ); + } + +RTE_LOG (INFO ,EAL ,"Success to handle SIGBUS for hot-unplug!\n" ); +} + +static int cmp_dev_name (const struct rte_device *dev, +const void *_name) +{ +const char *name = _name; + +return strcmp (dev->name, name); +} +static int dev_uev_socket_fd_create (void ) {@@ -147,6 +209,9 @@ dev_uev_handler(__rte_unused void *param)struct rte_dev_event uevent;int ret;char buf[EAL_UEV_MSG_LEN ]; +struct rte_bus *bus; +struct rte_device *dev; +const char *busname ="" ;memset (&uevent,0 ,sizeof (struct rte_dev_event ));memset (buf,0 ,EAL_UEV_MSG_LEN );@@ -171,8 +236,43 @@ dev_uev_handler(__rte_unused void *param)RTE_LOG (DEBUG ,EAL ,"receive uevent(name:%s, type:%d, subsystem:%d)\n" , uevent.devname, uevent.type, uevent.subsystem); -if (uevent.devname) +switch (uevent.subsystem) { +case EAL_DEV_EVENT_SUBSYSTEM_PCI : +case EAL_DEV_EVENT_SUBSYSTEM_UIO : + busname ="pci" ; +break ; +default : +break ; + } + +if (uevent.devname) { +if (uevent.type ==RTE_DEV_EVENT_REMOVE && hotplug_handle) { +rte_spinlock_lock (&failure_handle_lock); + bus =rte_bus_find_by_name (busname); +if (bus ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find bus (%s)\n" , + busname); +return ; + } + + dev = bus->find_device (NULL , cmp_dev_name, + uevent.devname); +if (dev ==NULL ) { +RTE_LOG (ERR ,EAL ,"Cannot find device (%s) on " +"bus (%s)\n" , uevent.devname, busname); +return ; + } + + ret = bus->hot_unplug_handler (dev); +rte_spinlock_unlock (&failure_handle_lock); +if (ret) { +RTE_LOG (ERR ,EAL ,"Can not handle hot-unplug " +"for device (%s)\n" , dev->name); +return ; + } + }dev_callback_process (uevent.devname, uevent.type); + } }int __rte_experimental@@ -220,5 +320,65 @@ rte_dev_event_monitor_stop(void)close (intr_handle.fd); intr_handle.fd =-1 ; monitor_started = false; +return 0 ; } + +int __rte_experimental
Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal_dev.c rte_dev_event_monitor_stop(void) · line 334
Quoted 7 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +7
-0+rte_dev_sigbus_handler_register (void) +{ +sigset_t mask; +struct sigaction action; + + rte_errno =0 ; +Shouldn't you call sigaction only if
sigbus_need_recover== 0?
i guess what you mean is that the sigbus_need_recover is need check before call sigaction, because the register could be call many times, if so, i think it is make sense to check it every time.
Quoted 11 lines
Quoted 11 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +11
-0+sigemptyset (&mask); +sigaddset (&mask,SIGBUS ); + action.sa_flags =SA_SIGINFO ; + action.sa_mask = mask; + action.sa_sigaction = sigbus_handler; + sigbus_need_recover = !sigaction (SIGBUS , &action, &sigbus_action_old); + +return rte_errno; +} + +int __rte_experimental
Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal_dev.c rte_dev_event_monitor_stop(void) · line 349
Quoted 4 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +4
-0+rte_dev_sigbus_handler_unregister (void) +{ + rte_errno =0 ; + sigbus_need_recover =1 ;Hmm, why to set
sugbus_need_recoverto 1 here?
If user calledrte_dev_sigbus_handler_register()before, and it was successful, it already would be 1. In other cases, you probably don't have to do anything.
Konstantin
you are right, it should let each sigaction calling to manage this macro but no other more place, thanks.
Quoted 50 lines
Quoted 50 lines
lib/librte_eal/linuxapp/eal/eal_dev.c +35
-0+ +sigbus_action_recover (); + +return rte_errno; +} + +int __rte_experimental +rte_dev_hotplug_handle_enable (void) +{ +int ret =0 ; + + ret =rte_dev_sigbus_handler_register (); +if (ret <0 ) +RTE_LOG (ERR ,EAL ,"fail to register sigbus handler for " +"devices.\n" ); + + hotplug_handle = true; + +return ret; +} + +int __rte_experimental +rte_dev_hotplug_handle_disable (void) +{ +int ret =0 ; + + ret =rte_dev_sigbus_handler_unregister (); +if (ret <0 ) +RTE_LOG (ERR ,EAL ,"fail to unregister sigbus handler for " +"devices.\n" ); + + hotplug_handle = false; + +return ret; +}lib/librte_eal/rte_eal_version.map +2
-1@@ -281,6 +281,8 @@ EXPERIMENTAL { rte_dev_event_callback_unregister; rte_dev_event_monitor_start; rte_dev_event_monitor_stop; + rte_dev_hotplug_handle_enable; + rte_dev_hotplug_handle_disable; rte_dev_iterator_init; rte_dev_iterator_next; rte_devargs_add; --2.7.4
PATCH v11 7/7 testpmd: use hot-unplug failure handle mechanism
In reply to Jeff GuoThis patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot-unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.
---v11->v10:
since we plan to abandon RTE_ETH_EVENT_INTR_RMV, change to use RTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage.
--- app/test-pmd/testpmd.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-)
app/test-pmd/testpmd.c +31 -8
@@ -2093,14 +2093,22 @@ pmd_test_exit(void)if (hot_plug) { ret =rte_dev_event_monitor_stop (); -if (ret) +if (ret) {RTE_LOG (ERR ,EAL ,"fail to stop device event monitor." ); +return ; + } ret =eth_dev_event_callback_unregister ();if (ret) +return ; + + ret =rte_dev_hotplug_handle_disable (); +if (ret) {RTE_LOG (ERR ,EAL , -"fail to unregister all event callbacks." ); +"fail to disable hotplug handling." ); +return ; + } }printf ("\nBye...\n" );
@@ -2244,6 +2252,9 @@ static void eth_dev_event_callback(char *device_name, enum rte_dev_event_type type, __rte_unused void *arg) { +uint16_t port_id; +int ret; +if (type >=RTE_DEV_EVENT_MAX ) {fprintf (stderr,"%s called upon invalid event %d\n" , __func__, type);
@@ -2254,9 +2265,12 @@ eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,case RTE_DEV_EVENT_REMOVE :RTE_LOG (ERR ,EAL ,"The device: %s has been removed!\n" , device_name); -/* TODO: After finish failure handle, begin to stop -* packet forward, stop port, close port, detach port. -*/ + ret =rte_eth_dev_get_port_by_name (device_name, &port_id); +if (ret) { +printf ("can not get port by device %s!\n" , device_name); +return ; + } +rmv_event_callback ((void *)(intptr_t )port_id);break ;case RTE_DEV_EVENT_ADD : RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
@@ -2779,14 +2793,23 @@ main(int argc, char** argv)init_config ();if (hot_plug) { -/* enable hot plug monitoring */ + ret =rte_dev_hotplug_handle_enable (); +if (ret) { +RTE_LOG (ERR ,EAL , +"fail to enable hotplug handling." ); +return -1 ; + } + ret =rte_dev_event_monitor_start ();if (ret) { - rte_errno =EINVAL ; +RTE_LOG (ERR ,EAL , +"fail to start device event monitoring." );return -1 ; } -eth_dev_event_callback_register (); + ret =eth_dev_event_callback_register (); +if (ret) +return -1 ; }if (start_port (RTE_PORT_ALL ) !=0 )
Signature · 1 line
2.7.4
Re: [PATCH v11 0/7] hot-unplug failure handle mechanism
In reply to Jeff GuoQuoted 149 lines · On Sun, 30 Sep 2018 19:29:56 +0800 Jeff Guo <[email protected]> wrote:
Hotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.
We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.
In the case of hotplug for
igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in
igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.The mechanism should work as below:
First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could seperate implement case by case.For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate all device on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_diable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device eventmonitor->Enablehotplughandle->Registerevent callback ->attachport->startport->startforwarding->Deviceunplug->failurehandle ->stopforwarding->stopport->closeport->detachport.This patch set would not cover hotplug insert and binding, and it is only implement the
igb_uiofailure handler, the vfio hotplug failure handler will be in next coming patch set.patchset history:
v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. add experimental tag.
since we plan to abandonRTE_ETH_EVENT_INTR_RMV, change to useRTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move theigb_uiofixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.
v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log
v9->v8:
refine commit log to be more readable.
v8->v7:
refine errno process in sigbus handler.
refine igb uio release process
v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.
v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.
v3->v2:
change bus ops name tobus_hotplug_handler.
add new API and bus ops ofbus_signal_handlerdistingush handle generic. sigbus and hotplug sigbus.
v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:
v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.the prior patch history please check the patch set "add device event monitor framework".
Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug
testpmd: use hot-unplug failure handle mechanism
Quoted 14 lines
app/test-pmd/testpmd.c | 39 ++++++-- doc/guides/rel_notes/release_18_08.rst | 5 + drivers/bus/pci/pci_common.c | 81 ++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++ drivers/bus/pci/private.h | 12 +++ lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_common_bus.c | 43 +++++++++ lib/librte_eal/common/eal_private.h | 39 ++++++++ lib/librte_eal/common/include/rte_bus.h | 34 +++++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 162 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 12 files changed, 481 insertions(+), 9 deletions(-)
I am glad to see this, hotplug is needed. But have a somewhat controversial point of view. The DPDK project needs to do more to force users to go to more modern kernels and API's; there has been too much effort already to support new DPDK on older kernels and distributions. This leads to higher testing burden, technical debt and multiple API's.
To take the extreme point of view.
* igb_uio should be deprecated and all new work only use vfio and vfio-ionommu only
* kni should be deprecated and replaced by virtio
When there are N ways of doing things against X kernel versions,
and Y distributions, and multiple device vendors; the combinational explosion of cases means that interfaces don't get the depth of testing they deserve.
That means why not support hotplug on VFIO only?
Re: [PATCH v11 0/7] hot-unplug failure handle mechanism
-----Original Message-----
Quoted 187 lines
Date: Mon, 1 Oct 2018 11:00:12 +0200
From: Stephen Hemminger <[email protected]>
To: Jeff Guo <[email protected]>
Cc: [email protected], [email protected],
[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Subject: Re: [dpdk-dev] [PATCH v11 0/7] hot-unplug failure handle mechanismQuoted 149 lines · On Sun, 30 Sep 2018 19:29:56 +0800 Jeff Guo <[email protected]> wrote:
Hotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.
We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.
In the case of hotplug for
igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in
igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.The mechanism should work as below:
First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could seperate implement case by case.For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate all device on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_diable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device eventmonitor->Enablehotplughandle->Registerevent callback ->attachport->startport->startforwarding->Deviceunplug->failurehandle ->stopforwarding->stopport->closeport->detachport.This patch set would not cover hotplug insert and binding, and it is only implement the
igb_uiofailure handler, the vfio hotplug failure handler will be in next coming patch set.patchset history:
v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. add experimental tag.
since we plan to abandonRTE_ETH_EVENT_INTR_RMV, change to useRTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move theigb_uiofixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.
v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log
v9->v8:
refine commit log to be more readable.
v8->v7:
refine errno process in sigbus handler.
refine igb uio release process
v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.
v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.
v3->v2:
change bus ops name tobus_hotplug_handler.
add new API and bus ops ofbus_signal_handlerdistingush handle generic. sigbus and hotplug sigbus.
v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:
v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.the prior patch history please check the patch set "add device event monitor framework".
Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug
testpmd: use hot-unplug failure handle mechanismQuoted 14 lines
app/test-pmd/testpmd.c | 39 ++++++-- doc/guides/rel_notes/release_18_08.rst | 5 + drivers/bus/pci/pci_common.c | 81 ++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++ drivers/bus/pci/private.h | 12 +++ lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_common_bus.c | 43 +++++++++ lib/librte_eal/common/eal_private.h | 39 ++++++++ lib/librte_eal/common/include/rte_bus.h | 34 +++++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 162 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 12 files changed, 481 insertions(+), 9 deletions(-)I am glad to see this, hotplug is needed. But have a somewhat controversial point of view. The DPDK project needs to do more to force users to go to more modern kernels and API's; there has been too much effort already to support new DPDK on older kernels and distributions. This leads to higher testing burden, technical debt and multiple API's.
Quoted 3 lines
To take the extreme point of view.
*igb_uioshould be deprecated and all new work only use vfio and vfio-ionommu only
* kni should be deprecated and replaced by virtio
+1
I think, The only feature missing in upstream kernel for DPDK may be to enable SRIOV on PF VFIO devices controlled by DPDK PMD.
I think, Binding a PF device to DPDK along with VFs(VF can be bound to netdev or DPDK PMDs, Though binding VF to netdev considered as security breach) will be useful for
a) rte_flow actions like redirecting the traffic to PF or VF on the given pattern
b) Some NICs can support promiscuous mode only on PF
c) Enable Switch representation devices
https://doc.dpdk.org/guides/prog_guide/switch_representation.html
I think, igb_uio mainly used as the backdoor for this use case.
I think, there was some work in this area but it is not upstreamed due to various reasons.
https://lkml.org/lkml/2018/3/8/1122
Quoted 7 lines
When there are N ways of doing things against X kernel versions,
and Y distributions, and multiple device vendors; the combinational explosion of cases means that interfaces don't get the depth of testing they deserve.That means why not support hotplug on VFIO only?
Re: [PATCH v11 0/7] hot-unplug failure handle mechanism
hi, jerin
Thanks for your comment and reply as below.
Quoted 201 lines · Jerin Jacob
-----Original Message-----
Quoted 186 lines
Date: Mon, 1 Oct 2018 11:00:12 +0200
From: Stephen Hemminger <[email protected]>
To: Jeff Guo <[email protected]>
Cc: [email protected], [email protected],
[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Subject: Re: [dpdk-dev] [PATCH v11 0/7] hot-unplug failure handle mechanismQuoted 149 lines · On Sun, 30 Sep 2018 19:29:56 +0800 Jeff Guo <[email protected]> wrote:
Hotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.
We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.
In the case of hotplug for
igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in
igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.The mechanism should work as below:
First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could seperate implement case by case.For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate all device on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_diable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device eventmonitor->Enablehotplughandle->Registerevent callback->attach port->start port->start forwarding->Device unplug->failure handle ->stop forwarding->stop port->close port->detach port.This patch set would not cover hotplug insert and binding, and it is only implement the
igb_uiofailure handler, the vfio hotplug failure handler will be in next coming patch set.patchset history:
v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. add experimental tag.
since we plan to abandonRTE_ETH_EVENT_INTR_RMV, change to useRTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move theigb_uiofixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.
v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log
v9->v8:
refine commit log to be more readable.
v8->v7:
refine errno process in sigbus handler.
refine igb uio release process
v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.
v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.
v3->v2:
change bus ops name tobus_hotplug_handler.
add new API and bus ops ofbus_signal_handlerdistingush handle generic. sigbus and hotplug sigbus.
v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:
v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.the prior patch history please check the patch set "add device event monitor framework".
Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug
testpmd: use hot-unplug failure handle mechanismQuoted 14 lines
app/test-pmd/testpmd.c | 39 ++++++-- doc/guides/rel_notes/release_18_08.rst | 5 + drivers/bus/pci/pci_common.c | 81 ++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++ drivers/bus/pci/private.h | 12 +++ lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_common_bus.c | 43 +++++++++ lib/librte_eal/common/eal_private.h | 39 ++++++++ lib/librte_eal/common/include/rte_bus.h | 34 +++++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 162 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 12 files changed, 481 insertions(+), 9 deletions(-)I am glad to see this, hotplug is needed. But have a somewhat controversial point of view. The DPDK project needs to do more to force users to go to more modern kernels and API's; there has been too much effort already to support new DPDK on older kernels and distributions. This leads to higher testing burden, technical debt and multiple API's.
Quoted 3 lines
To take the extreme point of view.
*igb_uioshould be deprecated and all new work only use vfio and vfio-ionommu only
* kni should be deprecated and replaced by virtio+1
I think, The only feature missing in upstream kernel for DPDK may be to enable SRIOV on PF VFIO devices controlled by DPDK PMD.
I think, Binding a PF device to DPDK along withVFs(VF can be bound to netdev or DPDK PMDs, Though binding VF to netdev considered as security breach)will be useful for
a)rte_flowactions like redirecting the traffic to PF or VF on the given pattern
b) Some NICs can support promiscuous mode only on PF
c) Enable Switch representation devices
https://doc.dpdk.org/guides/prog_guide/switch_representation.html
Quoted 5 lines
I think,
igb_uiomainly used as the backdoor for this use case.I think, there was some work in this area but it is not upstreamed due to various reasons.
https://lkml.org/lkml/2018/3/8/1122
I think you definite raise some meaningful of the hotplug for SRIOV. The igb_uio still is using now, its usage need to highlight to discuss.
Quoted 6 lines
Quoted 6 lines
When there are N ways of doing things against X kernel versions,
and Y distributions, and multiple device vendors; the combinational explosion of cases means that interfaces don't get the depth of testing they deserve.That means why not support hotplug on VFIO only?
Re: [PATCH v11 0/7] hot-unplug failure handle mechanism
In reply to Stephen Hemmingerhi, stephen
Thanks for your review, my answer as below.
Quoted 176 lines · Stephen Hemminger
Quoted 149 lines · On Sun, 30 Sep 2018 19:29:56 +0800 Jeff Guo <[email protected]> wrote:
Hotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.
We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.
In the case of hotplug for
igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in
igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.The mechanism should work as below:
First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could seperate implement case by case.For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate all device on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_diable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device eventmonitor->Enablehotplughandle->Registerevent callback->attach port->start port->start forwarding->Device unplug->failure handle ->stop forwarding->stop port->close port->detach port.This patch set would not cover hotplug insert and binding, and it is only implement the
igb_uiofailure handler, the vfio hotplug failure handler will be in next coming patch set.patchset history:
v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. add experimental tag.
since we plan to abandonRTE_ETH_EVENT_INTR_RMV, change to useRTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move theigb_uiofixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.
v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log
v9->v8:
refine commit log to be more readable.
v8->v7:
refine errno process in sigbus handler.
refine igb uio release process
v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.
v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.
v3->v2:
change bus ops name tobus_hotplug_handler.
add new API and bus ops ofbus_signal_handlerdistingush handle generic. sigbus and hotplug sigbus.
v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:
v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.the prior patch history please check the patch set "add device event monitor framework".
Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug
testpmd: use hot-unplug failure handle mechanismQuoted 14 lines
app/test-pmd/testpmd.c | 39 ++++++-- doc/guides/rel_notes/release_18_08.rst | 5 + drivers/bus/pci/pci_common.c | 81 ++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++ drivers/bus/pci/private.h | 12 +++ lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_common_bus.c | 43 +++++++++ lib/librte_eal/common/eal_private.h | 39 ++++++++ lib/librte_eal/common/include/rte_bus.h | 34 +++++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 162 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 12 files changed, 481 insertions(+), 9 deletions(-)I am glad to see this, hotplug is needed. But have a somewhat controversial point of view. The DPDK project needs to do more to force users to go to more modern kernels and API's; there has been too much effort already to support new DPDK on older kernels and distributions. This leads to higher testing burden, technical debt and multiple API's.
To take the extreme point of view.
*igb_uioshould be deprecated and all new work only use vfio and vfio-ionommu only
* kni should be deprecated and replaced by virtio
Quoted 5 lines
When there are N ways of doing things against X kernel versions,
and Y distributions, and multiple device vendors; the combinational explosion of cases means that interfaces don't get the depth of testing they deserve.That means why not support hotplug on VFIO only?
I think you gave a very constructive suggestion, but i am not 100% sure for that, at least something need we considerate and discuss here.
1)Is it announced to all dpdk user that igb_uio will be deprecated, and is it plan the time slot when will be?
2)At next, the subject of considerate should be the cost of the transaction. During the transaction, what is better way, to set experimental and implement and testing it to benefit for igb_uio user and customer, or just let this part to be vacation and avoid any unnecessary combinational cost of implementation and testing .
I think we can fulfill hotplug for our dpdk user and customer, if we could find a better way to handle 1) and 2).
v12 cover hot-unplug failure handle mechanism
In reply to Jeff GuoHotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.
We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.
In the case of hotplug for igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.
In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.
The mechanism should work as below:
First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could seperate implement case by case.
For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate all device on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_diable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.
The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device event monitor->Enable hotplug handle->Register event callback ->attach port->start port->start forwarding->Device unplug->failure handle ->stop forwarding->stop port->close port->detach port.
This patch set would not cover hotplug insert and binding, and it is only implement the igb_uio failure handler, the vfio hotplug failure handler will be in next coming patch set.
patchset history:v12->v11:
add and delete some checking about sigbus recover.
v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. since we plan to abandon RTE_ETH_EVENT_INTR_RMV, change to use RTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move the igb_uio fixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.
v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log
v9->v8:
refine commit log to be more readable.
v8->v7:
refine errno process in sigbus handler.
refine igb uio release process
v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.
v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.
v3->v2:
change bus ops name to bus_hotplug_handler.
add new API and bus ops of bus_signal_handler distingush handle generic. sigbus and hotplug sigbus.
v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.
Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.
v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.
the prior patch history please check the patch set "add device event monitor framework".
Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug
testpmd: use hot-unplug failure handle mechanism
app/test-pmd/testpmd.c | 39 ++++++-- doc/guides/rel_notes/release_18_08.rst | 5 + drivers/bus/pci/pci_common.c | 81 ++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++ drivers/bus/pci/private.h | 12 +++ lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_common_bus.c | 43 +++++++++ lib/librte_eal/common/eal_private.h | 39 ++++++++ lib/librte_eal/common/include/rte_bus.h | 34 +++++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 164 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 12 files changed, 483 insertions(+), 9 deletions(-)
Signature · 1 line
2.7.4
PATCH v12 1/7 bus: add hot-unplug handler
A hot-unplug failure and app crash can be caused, when a device is hot-unplugged but the application still try to access the device by reading or writing from the BARs, which is already invalid but still not timely be unmap or released.
This patch introduces bus ops to handle hot-unplug failures. Each bus can implement its own case-dependent logic to handle the failures.
---v12->v11:
no change.
--- lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);typedef int (*rte_bus_parse_t )(const char *name,void *addr);/** +* Implement a specific hot-unplug handler, which is responsible for +* handle the failure when device be hot-unplugged. When the event of +* hot-unplug be detected, it could call this function to handle +* the hot-unplug failure and avoid app crash. +* @param dev +* Pointer of the device structure. +* +* @return +* 0 on success. +* !0 on error. +*/ +typedef int (*rte_bus_hot_unplug_handler_t )(struct rte_device *dev); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -212,6 +226,8 @@ struct rte_bus {struct rte_bus_conf conf;/**< Bus configuration */ rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ rte_dev_iterate_t dev_iterate;/**< Device iterator. */ +rte_bus_hot_unplug_handler_t hot_unplug_handler; +/**< handle hot-unplug failure on the bus */ };/**
Signature · 1 line
2.7.4
PATCH v12 2/7 bus/pci: implement hot-unplug handler ops
In reply to Jeff GuoThis patch implements the ops to handle hot-unplug on the PCI bus. For UIO PCI, it could avoids BARs read/write errors by creating a new dummy memory to remap the memory where the failure is. For VFIO or other kernel driver, it could specific implement function to handle hot-unplug case by case.
---v12->v11:
no change.
--- drivers/bus/pci/pci_common.c | 28 ++++++++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++ drivers/bus/pci/private.h | 12 ++++++++++++ 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
@@ -406,6 +406,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, }static int +pci_hot_unplug_handler (struct rte_device *dev) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =RTE_DEV_TO_PCI (dev); +if (!pdev) +return -1 ; + +switch (pdev->kdrv) { +case RTE_KDRV_IGB_UIO : +case RTE_KDRV_UIO_GENERIC : +case RTE_KDRV_NIC_UIO : +/* BARs resource is invalid, remap it to be safe. */ + ret =pci_uio_remap_resource (pdev); +break ; +default : +RTE_LOG (DEBUG ,EAL , +"Not managed by a supported kernel driver, skipped\n" ); + ret =-1 ; +break ; + } + +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -435,6 +462,7 @@ struct rte_pci_bus rte_pci_bus = { .unplug = pci_unplug, .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, + .hot_unplug_handler = pci_hot_unplug_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res) } } +/* remap the PCI resource of a PCI device in anonymous virtual memory */ +int +pci_uio_remap_resource (struct rte_pci_device *dev) +{ +int i; +void *map_address; + +if (dev ==NULL ) +return -1 ; + +/* Remap all BARs */ +for (i =0 ; i !=PCI_MAX_RESOURCE ; i++) { +/* skip empty BAR */ +if (dev->mem_resource[i].phys_addr ==0 ) +continue ; + map_address =mmap (dev->mem_resource[i].addr, + (size_t )dev->mem_resource[i].len, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ); +if (map_address ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL , +"Cannot remap resource for device %s\n" , + dev->name); +return -1 ; + } +RTE_LOG (INFO ,EAL , +"Successful remap resource for device %s\n" , + dev->name); + } + +return 0 ; +} +static struct mapped_pci_resource *pci_uio_find_resource (struct rte_pci_device *dev) {
drivers/bus/pci/private.h +12 -0
@@ -123,6 +123,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev, struct mapped_pci_resource *uio_res);/** +* Remap the PCI resource of a PCI device in anonymous virtual memory. +* +* @param dev +* Point to the struct rte pci device. +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +pci_uio_remap_resource (struct rte_pci_device *dev); + +/** * Map device memory to uio resource * * This function is private to EAL.
Signature · 1 line
2.7.4
PATCH v12 3/7 bus: add sigbus handler
In reply to Jeff GuoWhen a device is hot-unplugged, a sigbus error will occur of the datapath can still read/write to the device. A handler is required here to capture the sigbus signal and handle it appropriately.
This patch introduces a bus ops to handle sigbus errors. Each bus can implement its own case-dependent logic to handle the sigbus errors.
---v12->v11:
no change.
--- lib/librte_eal/common/include/rte_bus.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
lib/librte_eal/common/include/rte_bus.h +18 -0
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);typedef int (*rte_bus_hot_unplug_handler_t )(struct rte_device *dev);/** +* Implement a specific sigbus handler, which is responsible for handling +* the sigbus error which is either original memory error, or specific memory +* error that caused of device be hot-unplugged. When sigbus error be captured, +* it could call this function to handle sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 for success handle the sigbus. +* 1 for no bus handle the sigbus. +* -1 for failed to handle the sigbus +*/ +typedef int (*rte_bus_sigbus_handler_t )(const void *failure_addr); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -228,6 +243,9 @@ struct rte_bus {rte_dev_iterate_t dev_iterate;/**< Device iterator. */ rte_bus_hot_unplug_handler_t hot_unplug_handler;/**< handle hot-unplug failure on the bus */ +rte_bus_sigbus_handler_t sigbus_handler; +/**< handle sigbus error on the bus */ + };/**
Signature · 1 line
2.7.4
PATCH v12 4/7 bus/pci: implement sigbus handler ops
In reply to Jeff GuoThis patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hot-unplugged and calls the relevant ops of the hot-unplug handler to handle the hot-unplug failure of the device.
---v12->v11:
no change.
--- drivers/bus/pci/pci_common.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
drivers/bus/pci/pci_common.c +53 -0
@@ -405,6 +405,36 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,return NULL ; } +/** +* find the device which encounter the failure, by iterate over all device on +* PCI bus to check if the memory failure address is located in the range +* of the BARs of the device. +*/ +static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int i; + +FOREACH_DEVICE_ON_PCIBUS (pdev) { +for (i =0 ; i !=RTE_DIM (pdev->mem_resource); i++) { +if ((uint64_t )(uintptr_t )failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t )(uintptr_t )failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) { +RTE_LOG (INFO ,EAL ,"Failure address " +"%16.16" PRIx64" belongs to " +"device %s!\n" , + (uint64_t )(uintptr_t )failure_addr, + pdev->device.name); +return pdev; + } + } + } +return NULL ; +} +static int pci_hot_unplug_handler (struct rte_device *dev) {
@@ -433,6 +463,28 @@ pci_hot_unplug_handler(struct rte_device *dev) }static int +pci_sigbus_handler (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =pci_find_device_by_addr (failure_addr); +if (!pdev) { +/* It is a generic sigbus error, no bus would handle it. */ + ret =1 ; + }else { +/* The sigbus error is caused of hot-unplug. */ + ret =pci_hot_unplug_handler (&pdev->device); +if (ret) { +RTE_LOG (ERR ,EAL ,"Failed to handle hot-unplug for " +"device %s" , pdev->name); + ret =-1 ; + } + } +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -463,6 +515,7 @@ struct rte_pci_bus rte_pci_bus = { .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, .hot_unplug_handler = pci_hot_unplug_handler, + .sigbus_handler = pci_sigbus_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
PATCH v12 5/7 bus: add helper to handle sigbus
In reply to Jeff GuoThis patch aims to add a helper to iterate over all buses to find the relevant bus to handle the sigbus error.
---v12->v11:
no change.
--- lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 13 ++++++++++ 2 files changed, 56 insertions(+)
lib/librte_eal/common/eal_common_bus.c +43 -0
@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) +return -1 ; + + ret = bus->sigbus_handler (failure_addr); + +/* find bus but handle failed, keep the errno be set. */ +if (ret <0 && rte_errno ==0 ) + rte_errno =ENOTSUP ; + +return ret >0 ; +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* can not find bus. */ +if (!bus) +return 1 ; +/* find bus but handle failed, pass on the new errno. */ +else if (rte_errno !=0 ) +return -1 ; + +/* restore the old errno. */ + rte_errno = old_errno; + +return ret; +}
lib/librte_eal/common/eal_private.h +13 -0
@@ -304,4 +304,17 @@ int rte_devargs_layers_parse(struct rte_devargs *devargs, const char *devstr); +/** +* Iterate over all buses to find the corresponding bus to handle the sigbus +* error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4
PATCH v11 6/7 eal: add failure handle mechanism for hot-unplug
In reply to Jeff GuoThe mechanism can initially register the sigbus handler after the device event monitor is enabled. When a sigbus event is captured, it will check the failure address and accordingly handle the memory failure of the corresponding device by invoke the hot-unplug handler. It could prevent the application from crashing when a device is hot-unplugged.
By this patch, users could call below new added APIs to enable/disable the device hotplug handle mechanism. Note that it just implement the hot-unplug handler in these functions, the other handler of hotplug, such as handler for hotplug binding, could be add in the future if need:
- rte_dev_hotplug_handle_enable
- rte_dev_hotplug_handle_disable
---v12->v11:
add and delete some checking about sigbus recover.
--- doc/guides/rel_notes/release_18_08.rst | 5 + lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_private.h | 26 +++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 164 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 6 files changed, 236 insertions(+), 1 deletion(-)
doc/guides/rel_notes/release_18_08.rst +5 -0
@@ -117,6 +117,11 @@ New Features Added support for chained mbufs (input and output). +* **Added hot-unplug handle mechanism.** + + ``rte_dev_hotplug_handle_enable`` and ``rte_dev_hotplug_handle_disable`` are + for enabling or disabling hotplug handle mechanism. + API Changes -----------
lib/librte_eal/bsdapp/eal/eal_dev.c +14 -0
@@ -19,3 +19,17 @@ rte_dev_event_monitor_stop(void)RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" );return -1 ; } + +int __rte_experimental +rte_dev_hotplug_handle_enable (void) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +} + +int __rte_experimental +rte_dev_hotplug_handle_disable (void) +{ +RTE_LOG (ERR ,EAL ,"Device event is not supported for FreeBSD\n" ); +return -1 ; +}
lib/librte_eal/common/eal_private.h +26 -0
@@ -317,4 +317,30 @@ rte_devargs_layers_parse(struct rte_devargs *devargs,*/ int rte_bus_sigbus_handler (const void *failure_addr); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Register the sigbus handler. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_register (void ); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Unregister the sigbus handler. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +rte_dev_sigbus_handler_unregister (void ); +#endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/include/rte_dev.h +26 -0
@@ -460,4 +460,30 @@ rte_dev_event_monitor_start(void); int __rte_experimentalrte_dev_event_monitor_stop (void); +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Enable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_enable (void); + +/** +* @warning +* @b EXPERIMENTAL: this API may change without prior notice +* +* Disable hotplug handling for devices. +* +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int __rte_experimental +rte_dev_hotplug_handle_disable (void); +#endif /* _RTE_DEV_H_ */
lib/librte_eal/linuxapp/eal/eal_dev.c +163 -1
209 lines not on this page · raw message
lib/librte_eal/rte_eal_version.map +2 -0
@@ -281,6 +281,8 @@ EXPERIMENTAL { rte_dev_event_callback_unregister; rte_dev_event_monitor_start; rte_dev_event_monitor_stop; + rte_dev_hotplug_handle_enable; + rte_dev_hotplug_handle_disable; rte_dev_iterator_init; rte_dev_iterator_next; rte_devargs_add;
Signature · 1 line
2.7.4
PATCH v11 7/7 testpmd: use hot-unplug failure handle mechanism
In reply to Jeff GuoThis patch use testpmd for example, to show how an app smoothly handle failure when device be hot-unplug. Except app should enabled the device event monitor and register the hotplug event’s callback, it also need enable hotplug handle mechanism before running. Once app detect the removal event, the hot-unplug callback would be called. It will first stop the packet forwarding, then stop the port, close the port, and finally detach the port to clean the device and release the resources.
---v12->v11:
no change.
--- app/test-pmd/testpmd.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-)
app/test-pmd/testpmd.c +31 -8
@@ -2093,14 +2093,22 @@ pmd_test_exit(void)if (hot_plug) { ret =rte_dev_event_monitor_stop (); -if (ret) +if (ret) {RTE_LOG (ERR ,EAL ,"fail to stop device event monitor." ); +return ; + } ret =eth_dev_event_callback_unregister ();if (ret) +return ; + + ret =rte_dev_hotplug_handle_disable (); +if (ret) {RTE_LOG (ERR ,EAL , -"fail to unregister all event callbacks." ); +"fail to disable hotplug handling." ); +return ; + } }printf ("\nBye...\n" );
@@ -2244,6 +2252,9 @@ static void eth_dev_event_callback(char *device_name, enum rte_dev_event_type type, __rte_unused void *arg) { +uint16_t port_id; +int ret; +if (type >=RTE_DEV_EVENT_MAX ) {fprintf (stderr,"%s called upon invalid event %d\n" , __func__, type);
@@ -2254,9 +2265,12 @@ eth_dev_event_callback(char *device_name, enum rte_dev_event_type type,case RTE_DEV_EVENT_REMOVE :RTE_LOG (ERR ,EAL ,"The device: %s has been removed!\n" , device_name); -/* TODO: After finish failure handle, begin to stop -* packet forward, stop port, close port, detach port. -*/ + ret =rte_eth_dev_get_port_by_name (device_name, &port_id); +if (ret) { +printf ("can not get port by device %s!\n" , device_name); +return ; + } +rmv_event_callback ((void *)(intptr_t )port_id);break ;case RTE_DEV_EVENT_ADD : RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
@@ -2779,14 +2793,23 @@ main(int argc, char** argv)init_config ();if (hot_plug) { -/* enable hot plug monitoring */ + ret =rte_dev_hotplug_handle_enable (); +if (ret) { +RTE_LOG (ERR ,EAL , +"fail to enable hotplug handling." ); +return -1 ; + } + ret =rte_dev_event_monitor_start ();if (ret) { - rte_errno =EINVAL ; +RTE_LOG (ERR ,EAL , +"fail to start device event monitoring." );return -1 ; } -eth_dev_event_callback_register (); + ret =eth_dev_event_callback_register (); +if (ret) +return -1 ; }if (start_port (RTE_PORT_ALL ) !=0 )
Signature · 1 line
2.7.4
v12 cover hot-unplug failure handle mechanism
In reply to Jeff GuoHotplug is an important feature for use-cases like the datacenter device's fail-safe and for SRIOV Live Migration in SDN/NFV. It could bring higher flexibility and continuality to networking services in multiple use-cases in the industry. So let's see how DPDK can help users implement hotplug solutions.
We already have a general device-event monitor mechanism, failsafe driver, and hot plug/unplug API in DPDK. We have already got the solution of “ethdev event + kernel PMD hotplug handler + failsafe”, but we still not got “eal event + hotplug handler for pci PMD + failsafe” implement, and we need to considerate 2 different solutions between uio pci and vfio pci.
In the case of hotplug for igb_uio, when a hardware device be removed physically or disabled in software, the application needs to be notified and detach the device out of the bus, and then make the device invalidate. The problem is that, the removal of the device is not instantaneous in software. If the application data path tries to read/write to the device when removal is still in process, it will cause an MMIO error and application will crash.
In this patch set, we propose a PCIe bus failure handler mechanism for hot-unplug in igb_uio. It aims to guarantee that, when a hot-unplug occurs, the application will not crash.
The mechanism should work as below:
First, the application enables the device event monitor, registers the hotplug event’s callback and enable hotplug handling before running the data path. Once the hot-unplug occurs, the mechanism will detect the removal event and then accordingly do the failure handling. In order to do that, the below functionality will be required:
- Add a new bus ops “hot_unplug_handler” to handle hot-unplug failure.
- Implement pci bus specific ops “pci_hot_unplug_handler”. For uio pci, it will be based on the failure address to remap memory for the corresponding device that unplugged. For vfio pci, could seperate implement case by case.
For the data path or other unexpected behaviors from the control path when a hot unplug occurs:
- Add a new bus ops “sigbus_handler”, that is responsible for handling the sigbus error which is either an original memory error, or a specific memory error that is caused by a hot unplug. When a sigbus error is captured, it will call this function to handle sigbus error.
- Implement PCI bus specific ops “pci_sigbus_handler”. It will iterate all device on PCI bus to find which device encounter the failure.
- Implement a "rte_bus_sigbus_handler" to iterate all buses to find a bus to handle the failure.
- Add a couple of APIs “rte_dev_hotplug_handle_enable” and “rte_dev_hotplug_handle_diable” to enable/disable hotplug handling. It will monitor the sigbus error by a handler which is per-process. Based on the signal event principle, the control path thread and the data path thread will randomly receive the sigbus error, but will call the common sigbus process. When sigbus be captured, it will call the above API to find bus to handle it.
The mechanism could be used by app or PMDs. For example, the whole process of hotplug in testpmd is:
- Enable device event monitor->Enable hotplug handle->Register event callback ->attach port->start port->start forwarding->Device unplug->failure handle ->stop forwarding->stop port->close port->detach port.
This patch set would not cover hotplug insert and binding, and it is only implement the igb_uio failure handler, the vfio hotplug failure handler will be in next coming patch set.
patchset history:v12->v11:
add and delete some checking about sigbus recover.
v11->v10:
change the ops name, since both uio and vfio will use the hot-unplug ops. since we plan to abandon RTE_ETH_EVENT_INTR_RMV, change to use RTE_DEV_EVENT_REMOVE, so modify the hotplug event and callback usage. move the igb_uio fixing part, since it is random issue and should be considarate as kernel driver defect but not include as this failure handler mechanism.
v10->v9:
modify the api name and exposure out for public use. add hotplug handle enable/disable APIs refine commit log
v9->v8:
refine commit log to be more readable.
v8->v7:
refine errno process in sigbus handler.
refine igb uio release process
v7->v6:
delete some unused part
v6->v5:
refine some description about bus ops
refine commit log
add some entry check.
v5->v4:
split patches to focus on the failure handle, remove the event usage by testpmd to another patch.
change the hotplug failure handler name.
refine the sigbus handle logic.
add lock for udev state in igb uio driver.
v4->v3:
split patches to be small and clear.
change to use new parameter "--hotplug-mode" in testpmd to identify the eal hotplug and ethdev hotplug.
v3->v2:
change bus ops name to bus_hotplug_handler.
add new API and bus ops of bus_signal_handler distingush handle generic. sigbus and hotplug sigbus.
v2->v1(v21):
refine some doc and commit log.
fix igb uio kernel issue for control path failure rebase testpmd code.
Since the hot plug solution be discussed serval around in the public, the scope be changed and the patch set be split into many times. Coming to the recently RFC and feature design, it just focus on the hot unplug failure handler at this patch set, so in order let this topic more clear and focus, summarize privours patch set in history “v1(v21)”, the v2 here go ahead for further track.
"v1(21)" == v21 as below:v21->v20:
split function in hot unplug ops.
sync failure hanlde to fix multiple process issue fix attach port issue for multiple devices case. combind rmv callback function to be only one.
v20->v19:
clean the code.
refine the remap logic for multiple device.
remove the auto binding.
v19->18:
note for limitation of multiple hotplug, fix some typo, sqeeze patch.
v18->v15:
add document, add signal bus handler, refine the code to be more clear.
the prior patch history please check the patch set "add device event monitor framework".
Jeff Guo (7):
bus: add hot-unplug handler
bus/pci: implement hot-unplug handler ops
bus: add sigbus handler
bus/pci: implement sigbus handler ops
bus: add helper to handle sigbus
eal: add failure handle mechanism for hot-unplug
testpmd: use hot-unplug failure handle mechanism
app/test-pmd/testpmd.c | 39 ++++++-- doc/guides/rel_notes/release_18_08.rst | 5 + drivers/bus/pci/pci_common.c | 81 ++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++ drivers/bus/pci/private.h | 12 +++ lib/librte_eal/bsdapp/eal/eal_dev.c | 14 +++ lib/librte_eal/common/eal_common_bus.c | 43 +++++++++ lib/librte_eal/common/eal_private.h | 39 ++++++++ lib/librte_eal/common/include/rte_bus.h | 34 +++++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/linuxapp/eal/eal_dev.c | 164 +++++++++++++++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 2 + 12 files changed, 483 insertions(+), 9 deletions(-)
Signature · 1 line
2.7.4
PATCH v12 1/7 bus: add hot-unplug handler
A hot-unplug failure and app crash can be caused, when a device is hot-unplugged but the application still try to access the device by reading or writing from the BARs, which is already invalid but still not timely be unmap or released.
This patch introduces bus ops to handle hot-unplug failures. Each bus can implement its own case-dependent logic to handle the failures.
---v12->v11:
no change.
--- lib/librte_eal/common/include/rte_bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/librte_eal/common/include/rte_bus.h +16 -0
@@ -168,6 +168,20 @@ typedef int (*rte_bus_unplug_t)(struct rte_device *dev);typedef int (*rte_bus_parse_t )(const char *name,void *addr);/** +* Implement a specific hot-unplug handler, which is responsible for +* handle the failure when device be hot-unplugged. When the event of +* hot-unplug be detected, it could call this function to handle +* the hot-unplug failure and avoid app crash. +* @param dev +* Pointer of the device structure. +* +* @return +* 0 on success. +* !0 on error. +*/ +typedef int (*rte_bus_hot_unplug_handler_t )(struct rte_device *dev); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -212,6 +226,8 @@ struct rte_bus {struct rte_bus_conf conf;/**< Bus configuration */ rte_bus_get_iommu_class_t get_iommu_class;/**< Get iommu class */ rte_dev_iterate_t dev_iterate;/**< Device iterator. */ +rte_bus_hot_unplug_handler_t hot_unplug_handler; +/**< handle hot-unplug failure on the bus */ };/**
Signature · 1 line
2.7.4
PATCH v12 2/7 bus/pci: implement hot-unplug handler ops
In reply to Jeff GuoThis patch implements the ops to handle hot-unplug on the PCI bus. For UIO PCI, it could avoids BARs read/write errors by creating a new dummy memory to remap the memory where the failure is. For VFIO or other kernel driver, it could specific implement function to handle hot-unplug case by case.
---v12->v11:
no change.
--- drivers/bus/pci/pci_common.c | 28 ++++++++++++++++++++++++++++ drivers/bus/pci/pci_common_uio.c | 33 +++++++++++++++++++++++++++++++++ drivers/bus/pci/private.h | 12 ++++++++++++ 3 files changed, 73 insertions(+)
drivers/bus/pci/pci_common.c +28 -0
@@ -406,6 +406,33 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, }static int +pci_hot_unplug_handler (struct rte_device *dev) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =RTE_DEV_TO_PCI (dev); +if (!pdev) +return -1 ; + +switch (pdev->kdrv) { +case RTE_KDRV_IGB_UIO : +case RTE_KDRV_UIO_GENERIC : +case RTE_KDRV_NIC_UIO : +/* BARs resource is invalid, remap it to be safe. */ + ret =pci_uio_remap_resource (pdev); +break ; +default : +RTE_LOG (DEBUG ,EAL , +"Not managed by a supported kernel driver, skipped\n" ); + ret =-1 ; +break ; + } + +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -435,6 +462,7 @@ struct rte_pci_bus rte_pci_bus = { .unplug = pci_unplug, .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, + .hot_unplug_handler = pci_hot_unplug_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
drivers/bus/pci/pci_common_uio.c +33 -0
@@ -146,6 +146,39 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res) } } +/* remap the PCI resource of a PCI device in anonymous virtual memory */ +int +pci_uio_remap_resource (struct rte_pci_device *dev) +{ +int i; +void *map_address; + +if (dev ==NULL ) +return -1 ; + +/* Remap all BARs */ +for (i =0 ; i !=PCI_MAX_RESOURCE ; i++) { +/* skip empty BAR */ +if (dev->mem_resource[i].phys_addr ==0 ) +continue ; + map_address =mmap (dev->mem_resource[i].addr, + (size_t )dev->mem_resource[i].len, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED ,-1 ,0 ); +if (map_address ==MAP_FAILED ) { +RTE_LOG (ERR ,EAL , +"Cannot remap resource for device %s\n" , + dev->name); +return -1 ; + } +RTE_LOG (INFO ,EAL , +"Successful remap resource for device %s\n" , + dev->name); + } + +return 0 ; +} +static struct mapped_pci_resource *pci_uio_find_resource (struct rte_pci_device *dev) {
drivers/bus/pci/private.h +12 -0
@@ -123,6 +123,18 @@ void pci_uio_free_resource(struct rte_pci_device *dev, struct mapped_pci_resource *uio_res);/** +* Remap the PCI resource of a PCI device in anonymous virtual memory. +* +* @param dev +* Point to the struct rte pci device. +* @return +* - On success, zero. +* - On failure, a negative value. +*/ +int +pci_uio_remap_resource (struct rte_pci_device *dev); + +/** * Map device memory to uio resource * * This function is private to EAL.
Signature · 1 line
2.7.4
PATCH v12 3/7 bus: add sigbus handler
In reply to Jeff GuoWhen a device is hot-unplugged, a sigbus error will occur of the datapath can still read/write to the device. A handler is required here to capture the sigbus signal and handle it appropriately.
This patch introduces a bus ops to handle sigbus errors. Each bus can implement its own case-dependent logic to handle the sigbus errors.
---v12->v11:
no change.
--- lib/librte_eal/common/include/rte_bus.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
lib/librte_eal/common/include/rte_bus.h +18 -0
@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);typedef int (*rte_bus_hot_unplug_handler_t )(struct rte_device *dev);/** +* Implement a specific sigbus handler, which is responsible for handling +* the sigbus error which is either original memory error, or specific memory +* error that caused of device be hot-unplugged. When sigbus error be captured, +* it could call this function to handle sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 for success handle the sigbus. +* 1 for no bus handle the sigbus. +* -1 for failed to handle the sigbus +*/ +typedef int (*rte_bus_sigbus_handler_t )(const void *failure_addr); + +/** * Bus scan policies */ enum rte_bus_scan_mode {
@@ -228,6 +243,9 @@ struct rte_bus {rte_dev_iterate_t dev_iterate;/**< Device iterator. */ rte_bus_hot_unplug_handler_t hot_unplug_handler;/**< handle hot-unplug failure on the bus */ +rte_bus_sigbus_handler_t sigbus_handler; +/**< handle sigbus error on the bus */ + };/**
Signature · 1 line
2.7.4
Re: [PATCH v12 3/7] bus: add sigbus handler
Quoted 24 lines · Jeff Guo
When a device is hot-unplugged, a sigbus error will occur of the datapath can still read/write to the device. A handler is required here to capture the sigbus signal and handle it appropriately.
This patch introduces a bus ops to handle sigbus errors. Each bus can implement its own case-dependent logic to handle the sigbus errors.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v12->v11:
no change.--- lib/librte_eal/common/include/rte_bus.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)lib/librte_eal/common/include/rte_bus.h +0
-0@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr);typedef int (*rte_bus_hot_unplug_handler_t )(struct rte_device *dev);/**
lib/librte_eal/common/include/rte_bus.h typedef int (*rte_bus_parse_t)(const char *name, void *addr); · line 194
Quoted 10 lines
lib/librte_eal/common/include/rte_bus.h +10
-0+* Implement a specific sigbus handler, which is responsible for handling +* the sigbus error which is either original memory error, or specific memory +* error that caused of device be hot-unplugged. When sigbus error be captured, +* it could call this function to handle sigbus error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 for success handle the sigbus. +* 1 for no bus handle the sigbus.
I think the comment here should be reworded. I can't parse "no bus handle the sigbus" - what does that mean, and how is it different from an error?
Signature · 2 lines
Thanks, Anatoly
Re: [PATCH v12 3/7] bus: add sigbus handler
Quoted 32 lines · Anatoly
Quoted 31 lines · Jeff Guo
When a device is hot-unplugged, a sigbus error will occur of the datapath can still read/write to the device. A handler is required here to capture the sigbus signal and handle it appropriately.
This patch introduces a bus ops to handle sigbus errors. Each bus can implement its own case-dependent logic to handle the sigbus errors.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v12->v11:
no change.
---
lib/librte_eal/common/include/rte_bus.h| 18 ++++++++++++++++++1 file changed, 18 insertions(+)lib/librte_eal/common/include/rte_bus.h +1
-0@@ -182,6 +182,21 @@ typedef int (*rte_bus_parse_t)(const char *name, void *addr); typedef int (*rte_bus_hot_unplug_handler_t)(struct rte_device *dev); /** +* Implement a specific sigbus handler, which is responsible for handling
+ * the sigbus error which is either original memory error, or specific memory
+ * error that caused of device be hot-unplugged. When sigbus error be captured,
+ * it could call this function to handle sigbus error.
Quoted 9 lines
Quoted 6 lines
+ * @param
failure_addr
+ * Pointer of the fault address of the sigbus error.
+ *
+ * @return
+ * 0 for success handle the sigbus.
+ * 1 for no bus handle the sigbus.I think the comment here should be reworded. I can't parse "no bus handle the sigbus" - what does that mean, and how is it different from an error?
ok, let me detail more.
PATCH v12 4/7 bus/pci: implement sigbus handler ops
In reply to Jeff GuoThis patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hot-unplugged and calls the relevant ops of the hot-unplug handler to handle the hot-unplug failure of the device.
---v12->v11:
no change.
--- drivers/bus/pci/pci_common.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
drivers/bus/pci/pci_common.c +53 -0
@@ -405,6 +405,36 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,return NULL ; } +/** +* find the device which encounter the failure, by iterate over all device on +* PCI bus to check if the memory failure address is located in the range +* of the BARs of the device. +*/ +static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int i; + +FOREACH_DEVICE_ON_PCIBUS (pdev) { +for (i =0 ; i !=RTE_DIM (pdev->mem_resource); i++) { +if ((uint64_t )(uintptr_t )failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t )(uintptr_t )failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) { +RTE_LOG (INFO ,EAL ,"Failure address " +"%16.16" PRIx64" belongs to " +"device %s!\n" , + (uint64_t )(uintptr_t )failure_addr, + pdev->device.name); +return pdev; + } + } + } +return NULL ; +} +static int pci_hot_unplug_handler (struct rte_device *dev) {
@@ -433,6 +463,28 @@ pci_hot_unplug_handler(struct rte_device *dev) }static int +pci_sigbus_handler (const void *failure_addr) +{ +struct rte_pci_device *pdev =NULL ; +int ret =0 ; + + pdev =pci_find_device_by_addr (failure_addr); +if (!pdev) { +/* It is a generic sigbus error, no bus would handle it. */ + ret =1 ; + }else { +/* The sigbus error is caused of hot-unplug. */ + ret =pci_hot_unplug_handler (&pdev->device); +if (ret) { +RTE_LOG (ERR ,EAL ,"Failed to handle hot-unplug for " +"device %s" , pdev->name); + ret =-1 ; + } + } +return ret; +} + +static int pci_plug (struct rte_device *dev) {return pci_probe_all_drivers (RTE_DEV_TO_PCI (dev));
@@ -463,6 +515,7 @@ struct rte_pci_bus rte_pci_bus = { .parse = pci_parse, .get_iommu_class = rte_pci_get_iommu_class, .hot_unplug_handler = pci_hot_unplug_handler, + .sigbus_handler = pci_sigbus_handler, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
Signature · 1 line
2.7.4
Re: [PATCH v12 4/7] bus/pci: implement sigbus handler ops
Quoted 29 lines · Jeff Guo
This patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hot-unplugged and calls the relevant ops of the hot-unplug handler to handle the hot-unplug failure of the device.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v12->v11:
no change.--- drivers/bus/pci/pci_common.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)drivers/bus/pci/pci_common.c +8
-0@@ -405,6 +405,36 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,return NULL ; } +/** +* find the device which encounter the failure, by iterate over all device on +* PCI bus to check if the memory failure address is located in the range +* of the BARs of the device. +*/ +static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{
drivers/bus/pci/pci_common.c pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, · line 425
Quoted 10 lines
drivers/bus/pci/pci_common.c +10
-0+struct rte_pci_device *pdev =NULL ; +int i; + +FOREACH_DEVICE_ON_PCIBUS (pdev) { +for (i =0 ; i !=RTE_DIM (pdev->mem_resource); i++) { +if ((uint64_t )(uintptr_t )failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t )(uintptr_t )failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) {
You must *really* dislike local variables :) Suggested rewriting:
const void *start, *end;size_t len;
start = pdev->mem_resource[i].addr;
len = pdev->mem_resource[i].len;
end = RTE_PTR_ADD(start, len);
if (failure_addr >= start && failure_addr < end) {
...
}I think this is way clearer.
drivers/bus/pci/pci_common.c pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, · line 430
Quoted 5 lines
drivers/bus/pci/pci_common.c +5
-0+RTE_LOG (INFO ,EAL ,"Failure address " +"%16.16" PRIx64" belongs to " +"device %s!\n" , + (uint64_t )(uintptr_t )failure_addr, + pdev->device.name);
I feel like this should have DEBUG level, rather than INFO. Alternatively, if you really feel like this should be at level INFO, the message should be reworded because the word "failure" might give the wrong impression :)
(but really, i think this is info useful for debugging purposes but not interesting generally, so it should be under DEBUG IMO)
Signature · 2 lines
Thanks, Anatoly
Re: [PATCH v12 4/7] bus/pci: implement sigbus handler ops
Quoted 53 lines · Anatoly
Quoted 32 lines · Jeff Guo
This patch implements the ops for the PCI bus sigbus handler. It finds the PCI device that is being hot-unplugged and calls the relevant ops of the hot-unplug handler to handle the hot-unplug failure of the device.
Signed-off-by Jeff Guo <[email protected]>Acked-by Shaopeng He <[email protected]>---
v12->v11:
no change.
---
drivers/bus/pci/pci_common.c| 53
++++++++++++++++++++++++++++++++++++++++++++1 file changed, 53 insertions(+)drivers/bus/pci/pci_common.c +0
-0@@ -405,6 +405,36 @@ pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, return NULL; }+/**
+ * find the device which encounter the failure, by iterate over all device on
+ * PCI bus to check if the memory failure address is located in the range
+ * of the BARs of the device.
+ */drivers/bus/pci/pci_common.c +3
-0+static struct rte_pci_device * +pci_find_device_by_addr (const void *failure_addr) +{drivers/bus/pci/pci_common.c pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, · line 425
Quoted 10 lines
drivers/bus/pci/pci_common.c +10
-0+ struct rte_pci_device *pdev = NULL; + int i; + + FOREACH_DEVICE_ON_PCIBUS(pdev) { + for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) { + if ((uint64_t)(uintptr_t)failure_addr >= + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr && + (uint64_t)(uintptr_t)failure_addr < + (uint64_t )(uintptr_t )pdev->mem_resource[i].addr + + pdev->mem_resource[i].len) {You must *really* dislike local variables :) Suggested rewriting:
const void *start, *end;
size_tlen;start = pdev->mem_resource[i].addr;
len = pdev->mem_resource[i].len;
end =RTE_PTR_ADD(start, len);
Quoted 6 lines
if (
failure_addr>= start &&failure_addr< end) {
...
}I think this is way clearer.
good point, local variable might be good and helpful. Thanks.
Quoted 10 lines
Quoted 5 lines
+ RTE_LOG(INFO, EAL, "Failure address " + "%16.16"PRIx64" belongs to " + "device %s!\n",
+ (uint64_t)(uintptr_t)failure_addr, +pdev->device.name);I feel like this should have DEBUG level, rather than INFO. Alternatively, if you really feel like this should be at level INFO, the message should be reworded because the word "failure" might give the wrong impression :)
(but really, i think this is info useful for debugging purposes but not interesting generally, so it should be under DEBUG IMO)
ok, i accept it.
PATCH v12 5/7 bus: add helper to handle sigbus
In reply to Jeff GuoThis patch aims to add a helper to iterate over all buses to find the relevant bus to handle the sigbus error.
---v12->v11:
no change.
--- lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 13 ++++++++++ 2 files changed, 56 insertions(+)
lib/librte_eal/common/eal_common_bus.c +43 -0
@@ -37,6 +37,7 @@#include <rte_bus.h> #include <rte_debug.h> #include <rte_string_fns.h> +#include <rte_errno.h> #include "eal_private.h"
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void) }return mode; } + +static int +bus_handle_sigbus (const struct rte_bus *bus, +const void *failure_addr) +{ +int ret; + +if (!bus->sigbus_handler) +return -1 ; + + ret = bus->sigbus_handler (failure_addr); + +/* find bus but handle failed, keep the errno be set. */ +if (ret <0 && rte_errno ==0 ) + rte_errno =ENOTSUP ; + +return ret >0 ; +} + +int +rte_bus_sigbus_handler (const void *failure_addr) +{ +struct rte_bus *bus; + +int ret =0 ; +int old_errno = rte_errno; + + rte_errno =0 ; + + bus =rte_bus_find (NULL , bus_handle_sigbus, failure_addr); +/* can not find bus. */ +if (!bus) +return 1 ; +/* find bus but handle failed, pass on the new errno. */ +else if (rte_errno !=0 ) +return -1 ; + +/* restore the old errno. */ + rte_errno = old_errno; + +return ret; +}
lib/librte_eal/common/eal_private.h +13 -0
@@ -304,4 +304,17 @@ int rte_devargs_layers_parse(struct rte_devargs *devargs, const char *devstr); +/** +* Iterate over all buses to find the corresponding bus to handle the sigbus +* error. +* @param failure_addr +* Pointer of the fault address of the sigbus error. +* +* @return +* 0 success to handle the sigbus. +* -1 failed to handle the sigbus +* 1 no bus can handler the sigbus +*/ +int rte_bus_sigbus_handler (const void *failure_addr); +#endif /* _EAL_PRIVATE_H_ */
Signature · 1 line
2.7.4