make signal handlers async-signal-safe
cover make signal handlers async-signal-safe
Signal handlers may only call the functions listed in signal-safety(7). Many DPDK examples ignored this and did printf() a "preparing to exit" message before setting the quit flag. A signal during another printf call can deadlock. And some of the programs did even more unsafe things.
GCC 14 -fanalyzer reports these:
warning: call to 'printf' from within signal handler [CWE-479]
[-Wanalyzer-unsafe-call-within-signal-handler]
The fix is the same throughout: the handler only sets the existing volatile flag, and any real work moves to the main loop or to main() after the lcores are joined. The message is dropped; the user pressed ^C and knows a signal was sent.
Patches 1-2 only delete the printf(). Patches 3-13 also relocate work that was being done in the handler.
Behaviour changes worth review:
- examples/ntb: SIGINT used to printf(), restore SIG_DFL and re-raise, killing the process without stopping the forwarding lcores or closing the devices. It now sets the per-lcore stopped flag, so SIGINT stops forwarding and returns to the ntb> prompt; quit does the teardown.
- examples/vdpa: the teardown moved to main() now also closes the vDPA devices when leaving interactive mode, which was missing before.
- examples/eventdev_pipeline: the second-signal escape hatch becomes _exit() instead of rte_exit(), the --dump-dev exit dump moves to main() (and uses the real dev_id rather than a hardcoded 0), and SIGTSTP now sets cdata.dump_dev_signal, which schedule_devices() already drained but nothing ever set. The SIGTSTP dump therefore requires a scheduler lcore.
Only examples/ethtool and examples/vmdq_dcb carry a Fixes: tag; the rest remove an unsafe call that has not been seen to deadlock in practice and are cleanups rather than backport material.
Applications outside the analyzer's reach likely have the same pattern; this covers what GCC flagged, plus eventdev_pipeline found by inspection.
Build tested with GCC 14 -fanalyzer; the warnings are gone for the files touched.
Stephen Hemminger (13):
graph: do not call printf in signal
examples: remove printf from signal handler examples/vmdq: do not print from signal handler examples/symmetric_mp: do not print or exit in handler examples/vdpa: make signal handler safe examples/vhost: make signal handler safe examples/vhost_blk: do not tear down from signal handler examples/ntb: do not print and re-raise from signal handler examples/ipsecgw: do not print from signal handler examples/l2fwd-macsec: remove print in signal handler examples/ethtool: fix exit flag and unchecked cmdline examples/vmdq_dcb: allow exit on signal examples/eventdev_pipeline: make signal handler safe
app/graph/main.c | 4 +-- examples/distributor/main.c | 3 +-- examples/dma/dmafwd.c | 2 -- examples/ethtool/ethtool-app/ethapp.c | 5 ++++ examples/ethtool/ethtool-app/main.c | 2 +- examples/eventdev_pipeline/main.c | 18 ++++++-------- examples/eventdev_pipeline/pipeline_common.h | 3 ++- examples/flow_filtering/main.c | 5 +--- examples/ipsec-secgw/ipsec-secgw.c | 5 +--- examples/l2fwd-event/main.c | 5 +--- examples/l2fwd-macsec/main.c | 5 +--- examples/l2fwd/main.c | 5 +--- examples/l3fwd-graph/main.c | 5 +--- examples/multi_process/symmetric_mp/main.c | 26 ++++++++++++++------ examples/ntb/ntb_fwd.c | 16 +++++++----- examples/vdpa/main.c | 16 ++++++------ examples/vhost/main.c | 14 ++++++----- examples/vhost_blk/vhost_blk.c | 26 +++++++++++--------- examples/vmdq/main.c | 20 ++++++++++++--- examples/vmdq_dcb/main.c | 20 ++++++++++++++- 20 files changed, 118 insertions(+), 87 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/13 graph: do not call printf in signal
Printf is not signal safe and calling it in a signal handler get flagged as an error by GCC analyzer.
--- app/graph/main.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
app/graph/main.c +1 -3
@@ -49,10 +49,8 @@ static struct app_params {static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } } static int
Signature · 1 line
2.53.0
PATCH 2/13 examples: remove printf from signal handler
In reply to Stephen HemmingerPrintf is not a signal safe function, remove useless printf's that cause warnings in GCC analyzer.
--- examples/distributor/main.c | 3 +-- examples/dma/dmafwd.c | 2 -- examples/eventdev_pipeline/main.c | 2 -- examples/flow_filtering/main.c | 5 +---- examples/l2fwd-event/main.c | 5 +---- examples/l2fwd/main.c | 5 +---- examples/l3fwd-graph/main.c | 5 +---- 7 files changed, 5 insertions(+), 22 deletions(-)
examples/distributor/main.c +1 -2
@@ -529,9 +529,8 @@ lcore_tx(struct rte_ring *in_r) }static void -int_handler (int sig_num) +int_handler(int sig_num __rte_unused) { -printf ("Exiting on signal %d\n" , sig_num);/* set quit flag for rx thread to exit */ quit_signal_rx =1 ; }
examples/dma/dmafwd.c +0 -2
@@ -998,8 +998,6 @@ static void signal_handler(int signum) {if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); force_quit = true; }else if (signum ==SIGUSR1 ) {dmadev_dump ();
examples/eventdev_pipeline/main.c +0 -2
@@ -319,8 +319,6 @@ signal_handler(int signum)if (fdata->done)rte_exit (1 ,"Exiting on signal %d\n" , signum);if ((signum ==SIGINT || signum ==SIGTERM ) && !once) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum);if (cdata.dump_dev)rte_event_dev_dump (0 , stdout); once =1 ;
examples/flow_filtering/main.c +1 -4
@@ -266,11 +266,8 @@ init_port(void)static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } }/* Parse the argument given in the command line of the application */
examples/l2fwd-event/main.c +1 -4
@@ -582,11 +582,8 @@ static void signal_handler(int signum) {struct l2fwd_resources *rsrc =l2fwd_get_rsrc (); -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) rsrc->force_quit = true; - } }int
examples/l2fwd/main.c +1 -4
@@ -638,11 +638,8 @@ check_all_ports_link_status(uint32_t port_mask)static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } }int
examples/l3fwd-graph/main.c +1 -4
@@ -753,11 +753,8 @@ check_all_ports_link_status(uint32_t port_mask)static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } } static void
Signature · 1 line
2.53.0
PATCH 3/13 examples/vmdq: do not print from signal handler
In reply to Stephen HemmingerThe SIGHUP handler calls printf() which is not async-signal-safe.
Set a flag instead and print the stats from the main lcore in the forwarding loop.
--- examples/vmdq/main.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
examples/vmdq/main.c +17 -3
@@ -465,18 +465,27 @@ update_mac_address(struct rte_mbuf *m, unsigned dst_port)rte_ether_addr_copy (&vmdq_ports_eth_addr[dst_port], ð->src_addr); } -/* When we receive a HUP signal, print out our stats */ +/* Set by the SIGHUP handler, consumed by the main lcore. */ +static volatile sig_atomic_t stats_requested; + +static void +sighup_handler(__rte_unused int signum) +{ + stats_requested =1 ; +} +static void -sighup_handler (int signum) +print_stats (void ) {unsigned int q = vmdq_queue_base; +for (; q < num_queues; q++) {if ((q - vmdq_queue_base) % (num_vmdq_queues / num_pools) ==0 )printf ("\nPool %u: " , (q - vmdq_queue_base) / (num_vmdq_queues / num_pools));printf ("%lu " , rxPackets[q]); } -printf ("\nFinished handling signal %d\n" , signum); +putchar ('\n' ); }/*
@@ -534,6 +543,11 @@ lcore_main(__rte_unused void *dummy)struct rte_mbuf *buf[MAX_PKT_BURST ];const uint16_t buf_size =RTE_DIM (buf); +if (stats_requested && lcore_id ==rte_get_main_lcore ()) { + stats_requested =0 ; +print_stats (); + } +for (p =0 ; p < num_ports; p++) {const uint8_t sport = ports[p];/* 0 <-> 1, 2 <-> 3 etc */
Signature · 1 line
2.53.0
PATCH 4/13 examples/symmetric_mp: do not print or exit in handler
In reply to Stephen HemmingerThe SIGINT and SIGTERM handler called printf() and exit() which are not async-signal-safe.
Set a flag, let the forwarding loops return, and print the stats from main() after joining the lcores.
--- examples/multi_process/symmetric_mp/main.c | 26 ++++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-)
examples/multi_process/symmetric_mp/main.c +19 -7
@@ -92,18 +92,25 @@ smp_usage(const char *prgname, const char *errmsg) } -/* signal handler configured for SIGTERM and SIGINT to print stats on exit */ +/* Set by SIGTERM and SIGINT handler to stop the forwarding loops. */ +static volatile sig_atomic_t quit; + +static void +signal_handler(__rte_unused int signum) +{ + quit =1 ; +} +static void -print_stats (int signum) +print_stats (void ) {unsigned i; -printf ("\nExiting on signal %d\n\n" , signum); +for (i =0 ; i < num_ports; i++){const uint8_t p_num = ports[i];printf ("Port %u: RX - %u, TX - %u, Drop - %u\n" , (unsigned )p_num, pstats[p_num].rx, pstats[p_num].tx, pstats[p_num].drop); } -exit (0 ); }/* Parse the argument given in the command line of the application */
@@ -344,7 +351,7 @@ lcore_main(void *arg __rte_unused)* queue number corresponding to our process number (not lcore id) */ -for (;;) { +while (!quit) {struct rte_mbuf *buf[PKT_BURST ];for (p = start_port; p < end_port; p++) {
@@ -364,6 +371,8 @@ lcore_main(void *arg __rte_unused) } } } + +return 0 ; }/* Check the link status of all ports in up to 9s, and print them finally */
@@ -440,8 +449,8 @@ main(int argc, char **argv)struct rte_mempool *mp;/* set up signal handlers to print stats on exit */ -signal (SIGINT , print_stats); -signal (SIGTERM , print_stats); +signal (SIGINT , signal_handler); +signal (SIGTERM , signal_handler);/* initialise the EAL for all */ ret =rte_eal_init (argc, argv);
@@ -484,6 +493,9 @@ main(int argc, char **argv)RTE_LOG (INFO ,APP ,"Finished Process Init.\n" );rte_eal_mp_remote_launch (lcore_main,NULL ,CALL_MAIN ); +rte_eal_mp_wait_lcore (); + +print_stats ();/* clean up the EAL */ rte_eal_cleanup ();
Signature · 1 line
2.53.0
PATCH 5/13 examples/vdpa: make signal handler safe
In reply to Stephen HemmingerThe handler called printf(), the vhost library teardown path and exit(), none of which are async-signal-safe.
Set a flag and do the teardown in main() once the input loop returns. This also closes the vDPA devices on exit from interactive mode, which was missing before.
--- examples/vdpa/main.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
examples/vdpa/main.c +8 -8
@@ -288,14 +288,13 @@ vdpa_sample_quit(void) } } +/* Set by SIGINT and SIGTERM handler, consumed by main(). */ +static volatile sig_atomic_t quit; +static void -signal_handler (int signum) +signal_handler(__rte_unused int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\nSignal %d received, preparing to exit...\n" , signum); -vdpa_sample_quit (); -exit (0 ); - } + quit =1 ; }/* interactive cmd functions */
@@ -513,7 +512,7 @@ main(int argc, char *argv[]) }printf ("enter \'q\' to quit\n" ); -while (scanf ("%c" , &ch)) { +while (!quit &&scanf ("%c" , &ch)) {if (ch =='q' )break ;while (ch !='\n' ) {
@@ -522,9 +521,10 @@ main(int argc, char *argv[]) }printf ("enter \'q\' to quit\n" ); } -vdpa_sample_quit (); } +vdpa_sample_quit (); +/* clean up the EAL */ rte_eal_cleanup ();
Signature · 1 line
2.53.0
PATCH 6/13 examples/vhost: make signal handler safe
In reply to Stephen HemmingerThe SIGINT handler unregistered the vhost drivers and called exit(), neither of which is async-signal-safe.
Set a flag, let switch_worker() return, and unregister the drivers in main() after joining the lcores.
--- examples/vhost/main.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
examples/vhost/main.c +8 -6
@@ -79,6 +79,9 @@ static int dma_count;/* mask of enabled ports */ static uint32_t enabled_port_mask =0 ; +/* Set by the SIGINT handler to stop the worker loops. */ +static volatile sig_atomic_t quit; +/* Promiscuous mode */ static uint32_t promiscuous;
@@ -1494,7 +1497,7 @@ switch_worker(void *arg) } } -while (1 ) { +while (!quit) {drain_mbuf_table (tx_q);drain_vhost_table ();/*
@@ -1872,14 +1875,10 @@ unregister_drivers(int socket_num) } } -/* When we receive a INT signal, unregister vhost driver */ static void sigint_handler(__rte_unused int signum) { -/* Unregister vhost driver. */ -unregister_drivers (nb_sockets); - -exit (0 ); + quit =1 ; } static void
@@ -2070,6 +2069,9 @@ main(int argc, char *argv[])RTE_LCORE_FOREACH_WORKER (lcore_id)rte_eal_wait_lcore (lcore_id); +/* Unregister vhost driver. */ +unregister_drivers (nb_sockets); +for (i =0 ; i < dma_count; i++) {if (rte_vhost_async_dma_unconfigure (dmas_id[i],0 ) <0 ) { RTE_LOG(ERR, VHOST_PORT,
Signature · 1 line
2.53.0
PATCH 7/13 examples/vhost_blk: do not tear down from signal handler
In reply to Stephen HemmingerThe SIGINT handler destroyed the controller and called exit() from signal context.
Set a flag, break the sleep loop, and destroy the controller in main().
--- examples/vhost_blk/vhost_blk.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-)
examples/vhost_blk/vhost_blk.c +14 -12
@@ -850,24 +850,18 @@ vhost_blk_ctrlr_destroy(struct vhost_blk_ctrlr *ctrlr)rte_vhost_driver_unregister (dev_pathname); } +/* Set by the SIGINT handler to break the main loop. */ +static volatile sig_atomic_t quit; +static void signal_handler(__rte_unused int signum) { -struct vhost_blk_ctrlr *ctrlr; - - ctrlr =vhost_blk_ctrlr_find (dev_pathname); -if (ctrlr ==NULL ) -return ; - -if (ctrlr->started) -destroy_device (ctrlr->vid); - -vhost_blk_ctrlr_destroy (ctrlr); -exit (0 ); + quit =1 ; }int main (int argc,char *argv[]) { +struct vhost_blk_ctrlr *ctrlr;int ret;/* init EAL */
@@ -895,9 +889,17 @@ int main(int argc, char *argv[]) }/* loop for exit the application */ -while (1 ) +while (!quit)sleep (1 ); + ctrlr =vhost_blk_ctrlr_find (dev_pathname); +if (ctrlr !=NULL ) { +if (ctrlr->started) +destroy_device (ctrlr->vid); + +vhost_blk_ctrlr_destroy (ctrlr); + } +/* clean up the EAL */ rte_eal_cleanup ();
Signature · 1 line
2.53.0
PATCH 8/13 examples/ntb: do not print and re-raise from signal handler
In reply to Stephen HemmingerThe handler called printf(), reset the disposition to SIG_DFL and killed itself, so the process died without stopping the forwarding lcores or closing the devices.
Set the existing per-lcore stopped flag instead, and mark it volatile since it is now written from signal context.
SIGINT no longer terminates the application; it stops forwarding and returns to the ntb> prompt, where quit does the teardown.
--- examples/ntb/ntb_fwd.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
examples/ntb/ntb_fwd.c +10 -6
@@ -41,7 +41,7 @@ struct ntb_fwd_stream {struct ntb_fwd_lcore_conf {uint16_t stream_id;uint16_t nb_stream; -uint8_t stopped; +volatile uint8_t stopped; };enum ntb_fwd_mode {
@@ -947,12 +947,16 @@ prompt(void) }static void -signal_handler (int signum) +signal_handler(__rte_unused int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\nSignal %d received, preparing to exit...\n" , signum); -signal (signum,SIG_DFL ); -kill (getpid (), signum); +struct ntb_fwd_lcore_conf *conf; +uint32_t lcore_id; + +RTE_LCORE_FOREACH_WORKER (lcore_id) { + conf = &fwd_lcore_conf[lcore_id]; + +if (conf->nb_stream) + conf->stopped =1 ; } }
Signature · 1 line
2.53.0
PATCH 9/13 examples/ipsecgw: do not print from signal handler
In reply to Stephen HemmingerThe SIGINT handler calls printf which is not async-signal safe.
--- examples/ipsec-secgw/ipsec-secgw.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
examples/ipsec-secgw/ipsec-secgw.c +1 -4
@@ -2448,11 +2448,8 @@ create_default_ipsec_flow(uint16_t port_id, uint64_t rx_offloads)static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } } static void
Signature · 1 line
2.53.0
Re: [PATCH 09/13] examples/ipsecgw: do not print from signal handler
Quoted 14 lines · Stephen Hemminger
The SIGINT handler calls printf which is not async-signal safe.
Signed-off-by Stephen Hemminger <[email protected]>--- examples/ipsec-secgw/ipsec-secgw.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)examples/ipsec-secgw/ipsec-secgw.c +0
-0@@ -2448,11 +2448,8 @@ create_default_ipsec_flow(uint16_t port_id, uint64_t rx_offloads)static void signal_handler (int signum)
examples/ipsec-secgw/ipsec-secgw.c create_default_ipsec_flow(uint16_t port_id, uint64_t rx_offloads) · line 2455
Quoted 10 lines
examples/ipsec-secgw/ipsec-secgw.c +1
-4{ -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } } static void
I think the same applies to interrupt callbacks (e.g.rte_eth_dev_cb_fn ), many example apps are using printf in those too.
From what I see write() is required to be async signal safe and can be used instead, in signal handlers and event callbacks, but we will lose the va args / format specifiers.
PATCH 10/13 examples/l2fwd-macsec: remove print in signal handler
In reply to Stephen HemmingerCalling printf in signal handler is not async-signal-safe. The printf was unnecessary anyway so remove it. Found by GCC -fanalyzer.
--- examples/l2fwd-macsec/main.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
examples/l2fwd-macsec/main.c +1 -4
@@ -1247,11 +1247,8 @@ check_all_ports_link_status(uint32_t port_mask)static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } }int
Signature · 1 line
2.53.0
PATCH 11/13 examples/ethtool: fix exit flag and unchecked cmdline
In reply to Stephen HemmingerThe exit_now flag is polled by worker_main() on one lcore and set by main() on another, but is a plain int so the load can be hoisted out of the loop. Make it volatile.
Also check the cmdline_stdin_new() result before using it.
Found by gcc -fanalyzer.
--- examples/ethtool/ethtool-app/ethapp.c | 5 +++++ examples/ethtool/ethtool-app/main.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-)
examples/ethtool/ethtool-app/ethapp.c +5 -0
@@ -4,6 +4,8 @@#include <stdlib.h> +#include <rte_debug.h> +#include <cmdline_parse.h> #include <cmdline_parse_num.h> #include <cmdline_parse_string.h>
@@ -914,6 +916,9 @@ void ethapp_main(void)struct cmdline *ctx_cmdline; ctx_cmdline =cmdline_stdin_new (list_prompt_commands,"EthApp> " ); +if (ctx_cmdline ==NULL ) +rte_panic ("Cannot create cmdline instance\n" ); +cmdline_interact (ctx_cmdline);cmdline_stdin_exit (ctx_cmdline); }
examples/ethtool/ethtool-app/main.c +1 -1
@@ -44,7 +44,7 @@ struct app_port {struct app_config {struct app_port ports[MAX_PORTS ];int cnt_ports; -int exit_now; +volatile int exit_now; };
Signature · 1 line
2.53.0
PATCH 12/13 examples/vmdq_dcb: allow exit on signal
In reply to Stephen HemmingerThe forwarding loop never terminated, so the application could only be killed. There was also no wait for the worker lcores.
Add a SIGINT and SIGTERM handler that sets a flag, return from lcore_main() when it is set, and join the workers before cleanup.
--- examples/vmdq_dcb/main.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
examples/vmdq_dcb/main.c +19 -1
@@ -513,6 +513,15 @@ update_mac_address(struct rte_mbuf *m, unsigned dst_port)rte_ether_addr_copy (&vmdq_ports_eth_addr[dst_port], ð->src_addr); } +/* Set by the SIGINT and SIGTERM handler to stop the forwarding loops. */ +static volatile sig_atomic_t quit; + +static void +signal_handler(__rte_unused int signum) +{ + quit =1 ; +} +/* When we receive a HUP signal, print out our stats */ static void sighup_handler (int signum)
@@ -567,7 +576,7 @@ lcore_main(void *arg)return 0 ; } -for (;;) { +while (!quit) {struct rte_mbuf *buf[MAX_PKT_BURST ];const uint16_t buf_size =RTE_DIM (buf);for (p =0 ; p < num_ports; p++) {
@@ -598,6 +607,8 @@ lcore_main(void *arg) } } } + +return 0 ; }/*
@@ -640,6 +651,8 @@ main(int argc, char *argv[])uint16_t portid;signal (SIGHUP , sighup_handler); +signal (SIGINT , signal_handler); +signal (SIGTERM , signal_handler);/* init EAL */ ret =rte_eal_init (argc, argv);
@@ -697,6 +710,11 @@ main(int argc, char *argv[])/* call on main too */ (void )lcore_main ((void *)i); +RTE_LCORE_FOREACH_WORKER (lcore_id) { +if (rte_eal_wait_lcore (lcore_id) <0 ) +return -1 ; + } +/* clean up the EAL */ rte_eal_cleanup ();
Signature · 1 line
2.53.0
PATCH 13/13 examples/eventdev_pipeline: make signal handler safe
In reply to Stephen HemmingerThe handler called rte_exit(), rte_event_dev_dump() and (before the previous patch) printf(), none of which are async-signal-safe. rte_exit() in particular runs rte_eal_cleanup() and exit(), tearing down the EAL from signal context while the worker lcores are still running in it.
Set the done flag and let main() do the work:
- The second-signal path becomes _exit(), which is async-signal-safe. This keeps the "hit ^C twice to give up on a clean shutdown" escape hatch without running exit handlers from the handler.
- The --dump-dev dump on exit moves to main() after rte_eal_mp_wait_lcore(), where it also gets the real dev_id instead of the hardcoded 0.
- SIGTSTP sets cdata.dump_dev_signal, which schedule_devices() already checks and drains on the scheduler lcore. That mechanism was present but nothing ever set the flag. Note this makes the SIGTSTP dump depend on a scheduler lcore being configured; there is no dump in configurations without one.
dump_dev_signal is now written from signal context, so make it volatile sig_atomic_t.
Unlike the rest of this series this one is not reported by -fanalyzer: the handler is registered in main() after rte_eal_init(), and the analyzer does not connect the registration to the handler body there. Found by inspection while auditing the neighbours.
--- examples/eventdev_pipeline/main.c | 16 +++++++--------- examples/eventdev_pipeline/pipeline_common.h | 3 ++- 2 files changed, 9 insertions(+), 10 deletions(-)
examples/eventdev_pipeline/main.c +7 -9
@@ -314,18 +314,13 @@ do_capability_setup(uint8_t eventdev_id)static void signal_handler (int signum) { -static uint8_t once; - -if (fdata->done) -rte_exit (1 ,"Exiting on signal %d\n" , signum); -if ((signum ==SIGINT || signum ==SIGTERM ) && !once) { -if (cdata.dump_dev) -rte_event_dev_dump (0 , stdout); - once =1 ; +if (signum ==SIGINT || signum ==SIGTERM ) { +if (fdata->done) +_exit (1 ); fdata->done =1 ; }if (signum ==SIGTSTP ) -rte_event_dev_dump (0 , stdout); + cdata.dump_dev_signal =1 ; } static inline uint64_t
@@ -452,6 +447,9 @@ main(int argc, char **argv)rte_eal_mp_wait_lcore (); +if (cdata.dump_dev) +rte_event_dev_dump (dev_id, stdout); +if (!cdata.quiet && (port_stat (dev_id, worker_data[0 ].port_id) != (uint64_t )-ENOTSUP )) {printf ("\nPort Workload distribution:\n" );
examples/eventdev_pipeline/pipeline_common.h +2 -1
@@ -3,6 +3,7 @@* Copyright 2017 Cavium, Inc. */ +#include <signal.h> #include <stdbool.h> #include <rte_eal.h>
@@ -68,7 +69,7 @@ struct config_data {int enable_queue_priorities;int quiet;int dump_dev; -int dump_dev_signal; +volatile sig_atomic_t dump_dev_signal;int all_type_queues;unsigned int num_stages;unsigned int worker_cq_depth;
Signature · 1 line
2.53.0
Quoted 46 lines · Stephen Hemminger
Signal handlers may only call the functions listed in signal-safety(7). Many DPDK examples ignored this and did
printf()a "preparing to exit" message before setting the quit flag. A signal during another printf call can deadlock. And some of the programs did even more unsafe things.GCC 14 -fanalyzer reports these:
warning: call to 'printf' from within signal handler [CWE-479]
[-Wanalyzer-unsafe-call-within-signal-handler]The fix is the same throughout: the handler only sets the existing volatile flag, and any real work moves to the main loop or to
main()after the lcores are joined. The message is dropped; the user pressed ^C and knows a signal was sent.Patches 1-2 only delete the
printf(). Patches 3-13 also relocate work that was being done in the handler.Behaviour changes worth review:
- examples/ntb: SIGINT used to
printf(), restoreSIG_DFLand re-raise, killing the process without stopping the forwarding lcores or closing the devices. It now sets the per-lcore stopped flag, so SIGINT stops forwarding and returns to the ntb> prompt; quit does the teardown.- examples/vdpa: the teardown moved to
main()now also closes the vDPA devices when leaving interactive mode, which was missing before.- examples/eventdev_pipeline: the second-signal escape hatch becomes
_exit()instead ofrte_exit(), the --dump-dev exit dump moves tomain()(and uses the realdev_idrather than a hardcoded 0), and SIGTSTP now sets cdata.dump_dev_signal, whichschedule_devices()already drained but nothing ever set. The SIGTSTP dump therefore requires a scheduler lcore.Only examples/ethtool and examples/vmdq_dcb carry a Fixes: tag; the rest remove an unsafe call that has not been seen to deadlock in practice and are cleanups rather than backport material.
Applications outside the analyzer's reach likely have the same pattern; this covers what GCC flagged, plus
eventdev_pipelinefound by inspection.Build tested with GCC 14 -fanalyzer; the warnings are gone for the files touched.
Quoted 15 lines
Stephen Hemminger (13):
graph: do not call printf in signal
examples: remove printf from signal handler examples/vmdq: do not print from signal handler examples/symmetric_mp: do not print or exit in handler examples/vdpa: make signal handler safe examples/vhost: make signal handler safe examples/vhost_blk: do not tear down from signal handler examples/ntb: do not print and re-raise from signal handler examples/ipsecgw: do not print from signal handler examples/l2fwd-macsec: remove print in signal handler examples/ethtool: fix exit flag and unchecked cmdline examples/vmdq_dcb: allow exit on signal examples/eventdev_pipeline: make signal handler safe
v2 cover make signal handlers async-signal-safe
In reply to Stephen HemmingerSignal handlers may only call the functions listed in signal-safety(7). Many DPDK examples ignored this and did printf() a "preparing to exit" message before setting the quit flag. A signal during another printf call can deadlock. And some of the programs did even more unsafe things.
GCC 14 -fanalyzer reports these:
warning: call to 'printf' from within signal handler [CWE-479]
[-Wanalyzer-unsafe-call-within-signal-handler]
The fix is the same throughout: the handler only sets the existing volatile flag, and any real work moves to the main loop or to main() after the lcores are joined. The message is dropped; the user pressed ^C and knows a signal was sent.
Patches 1-2 only delete the printf(). Patches 3-13 also relocate work that was being done in the handler.
Behaviour changes worth review:
- examples/ntb: SIGINT used to printf(), restore SIG_DFL and re-raise, killing the process without stopping the forwarding lcores or closing the devices. It now sets the per-lcore stopped flag, so SIGINT stops forwarding and returns to the ntb> prompt; quit does the teardown.
- examples/vdpa: the teardown moved to main() now also closes the vDPA devices when leaving interactive mode, which was missing before.
- examples/eventdev_pipeline: the second-signal escape hatch becomes _exit() instead of rte_exit(), the --dump-dev exit dump moves to main() (and uses the real dev_id rather than a hardcoded 0), and SIGTSTP now sets cdata.dump_dev_signal, which schedule_devices() already drained but nothing ever set. The SIGTSTP dump therefore requires a scheduler lcore.
Only examples/ethtool and examples/vmdq_dcb carry a Fixes: tag; the rest remove an unsafe call that has not been seen to deadlock in practice and are cleanups rather than backport material.
Applications outside the analyzer's reach likely have the same pattern; this covers what GCC flagged, plus eventdev_pipeline found by inspection.
Build tested with GCC 14 -fanalyzer; the warnings are gone for the files touched.
v2 -- needed more work, the Claude version of examples_pipeline
was over complex and had build errors.
Stephen Hemminger (13):
graph: do not call printf in signal
examples: remove printf from signal handler examples/vmdq: do not print from signal handler examples/symmetric_mp: do not print or exit in handler examples/vdpa: make signal handler safe examples/vhost: make signal handler safe examples/vhost_blk: do not tear down from signal handler examples/ntb: do not print and re-raise from signal handler examples/ipsecgw: do not print from signal handler examples/l2fwd-macsec: remove print in signal handler examples/ethtool: fix exit flag and unchecked cmdline examples/vmdq_dcb: allow exit on signal examples/eventdev_pipeline: make signal handler safe
app/graph/main.c | 4 +- examples/distributor/main.c | 3 +- examples/dma/dmafwd.c | 2 - examples/ethtool/ethtool-app/ethapp.c | 5 +++ examples/ethtool/ethtool-app/main.c | 2 +- examples/eventdev_pipeline/main.c | 41 ++++++++++++-------- examples/eventdev_pipeline/pipeline_common.h | 13 ++++--- examples/flow_filtering/main.c | 5 +-- examples/ipsec-secgw/ipsec-secgw.c | 5 +-- examples/l2fwd-event/main.c | 5 +-- examples/l2fwd-macsec/main.c | 5 +-- examples/l2fwd/main.c | 5 +-- examples/l3fwd-graph/main.c | 5 +-- examples/multi_process/symmetric_mp/main.c | 26 +++++++++---- examples/ntb/ntb_fwd.c | 16 +++++--- examples/vdpa/main.c | 16 ++++---- examples/vhost/main.c | 14 ++++--- examples/vhost_blk/vhost_blk.c | 26 +++++++------ examples/vmdq/main.c | 20 ++++++++-- examples/vmdq_dcb/main.c | 20 +++++++++- 20 files changed, 140 insertions(+), 98 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/13 graph: do not call printf in signal
Printf is not signal safe and calling it in a signal handler get flagged as an error by GCC analyzer.
--- app/graph/main.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
app/graph/main.c +1 -3
@@ -49,10 +49,8 @@ static struct app_params {static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } } static int
Signature · 1 line
2.53.0
PATCH v2 2/13 examples: remove printf from signal handler
In reply to Stephen HemmingerPrintf is not a signal safe function, remove useless printf's that cause warnings in GCC analyzer.
--- examples/distributor/main.c | 3 +-- examples/dma/dmafwd.c | 2 -- examples/eventdev_pipeline/main.c | 2 -- examples/flow_filtering/main.c | 5 +---- examples/l2fwd-event/main.c | 5 +---- examples/l2fwd/main.c | 5 +---- examples/l3fwd-graph/main.c | 5 +---- 7 files changed, 5 insertions(+), 22 deletions(-)
examples/distributor/main.c +1 -2
@@ -529,9 +529,8 @@ lcore_tx(struct rte_ring *in_r) }static void -int_handler (int sig_num) +int_handler(int sig_num __rte_unused) { -printf ("Exiting on signal %d\n" , sig_num);/* set quit flag for rx thread to exit */ quit_signal_rx =1 ; }
examples/dma/dmafwd.c +0 -2
@@ -998,8 +998,6 @@ static void signal_handler(int signum) {if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); force_quit = true; }else if (signum ==SIGUSR1 ) {dmadev_dump ();
examples/eventdev_pipeline/main.c +0 -2
@@ -319,8 +319,6 @@ signal_handler(int signum)if (fdata->done)rte_exit (1 ,"Exiting on signal %d\n" , signum);if ((signum ==SIGINT || signum ==SIGTERM ) && !once) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum);if (cdata.dump_dev)rte_event_dev_dump (0 , stdout); once =1 ;
examples/flow_filtering/main.c +1 -4
@@ -266,11 +266,8 @@ init_port(void)static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } }/* Parse the argument given in the command line of the application */
examples/l2fwd-event/main.c +1 -4
@@ -582,11 +582,8 @@ static void signal_handler(int signum) {struct l2fwd_resources *rsrc =l2fwd_get_rsrc (); -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) rsrc->force_quit = true; - } }int
examples/l2fwd/main.c +1 -4
@@ -638,11 +638,8 @@ check_all_ports_link_status(uint32_t port_mask)static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } }int
examples/l3fwd-graph/main.c +1 -4
@@ -753,11 +753,8 @@ check_all_ports_link_status(uint32_t port_mask)static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } } static void
Signature · 1 line
2.53.0
PATCH v2 3/13 examples/vmdq: do not print from signal handler
In reply to Stephen HemmingerThe SIGHUP handler calls printf() which is not async-signal-safe.
Set a flag instead and print the stats from the main lcore in the forwarding loop.
--- examples/vmdq/main.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
examples/vmdq/main.c +17 -3
@@ -465,18 +465,27 @@ update_mac_address(struct rte_mbuf *m, unsigned dst_port)rte_ether_addr_copy (&vmdq_ports_eth_addr[dst_port], ð->src_addr); } -/* When we receive a HUP signal, print out our stats */ +/* Set by the SIGHUP handler, consumed by the main lcore. */ +static volatile sig_atomic_t stats_requested; + +static void +sighup_handler(__rte_unused int signum) +{ + stats_requested =1 ; +} +static void -sighup_handler (int signum) +print_stats (void ) {unsigned int q = vmdq_queue_base; +for (; q < num_queues; q++) {if ((q - vmdq_queue_base) % (num_vmdq_queues / num_pools) ==0 )printf ("\nPool %u: " , (q - vmdq_queue_base) / (num_vmdq_queues / num_pools));printf ("%lu " , rxPackets[q]); } -printf ("\nFinished handling signal %d\n" , signum); +putchar ('\n' ); }/*
@@ -534,6 +543,11 @@ lcore_main(__rte_unused void *dummy)struct rte_mbuf *buf[MAX_PKT_BURST ];const uint16_t buf_size =RTE_DIM (buf); +if (stats_requested && lcore_id ==rte_get_main_lcore ()) { + stats_requested =0 ; +print_stats (); + } +for (p =0 ; p < num_ports; p++) {const uint8_t sport = ports[p];/* 0 <-> 1, 2 <-> 3 etc */
Signature · 1 line
2.53.0
PATCH v2 4/13 examples/symmetric_mp: do not print or exit in handler
In reply to Stephen HemmingerThe SIGINT and SIGTERM handler called printf() and exit() which are not async-signal-safe.
Set a flag, let the forwarding loops return, and print the stats from main() after joining the lcores.
--- examples/multi_process/symmetric_mp/main.c | 26 ++++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-)
examples/multi_process/symmetric_mp/main.c +19 -7
@@ -92,18 +92,25 @@ smp_usage(const char *prgname, const char *errmsg) } -/* signal handler configured for SIGTERM and SIGINT to print stats on exit */ +/* Set by SIGTERM and SIGINT handler to stop the forwarding loops. */ +static volatile sig_atomic_t quit; + +static void +signal_handler(__rte_unused int signum) +{ + quit =1 ; +} +static void -print_stats (int signum) +print_stats (void ) {unsigned i; -printf ("\nExiting on signal %d\n\n" , signum); +for (i =0 ; i < num_ports; i++){const uint8_t p_num = ports[i];printf ("Port %u: RX - %u, TX - %u, Drop - %u\n" , (unsigned )p_num, pstats[p_num].rx, pstats[p_num].tx, pstats[p_num].drop); } -exit (0 ); }/* Parse the argument given in the command line of the application */
@@ -344,7 +351,7 @@ lcore_main(void *arg __rte_unused)* queue number corresponding to our process number (not lcore id) */ -for (;;) { +while (!quit) {struct rte_mbuf *buf[PKT_BURST ];for (p = start_port; p < end_port; p++) {
@@ -364,6 +371,8 @@ lcore_main(void *arg __rte_unused) } } } + +return 0 ; }/* Check the link status of all ports in up to 9s, and print them finally */
@@ -440,8 +449,8 @@ main(int argc, char **argv)struct rte_mempool *mp;/* set up signal handlers to print stats on exit */ -signal (SIGINT , print_stats); -signal (SIGTERM , print_stats); +signal (SIGINT , signal_handler); +signal (SIGTERM , signal_handler);/* initialise the EAL for all */ ret =rte_eal_init (argc, argv);
@@ -484,6 +493,9 @@ main(int argc, char **argv)RTE_LOG (INFO ,APP ,"Finished Process Init.\n" );rte_eal_mp_remote_launch (lcore_main,NULL ,CALL_MAIN ); +rte_eal_mp_wait_lcore (); + +print_stats ();/* clean up the EAL */ rte_eal_cleanup ();
Signature · 1 line
2.53.0
PATCH v2 5/13 examples/vdpa: make signal handler safe
In reply to Stephen HemmingerThe handler called printf(), the vhost library teardown path and exit(), none of which are async-signal-safe.
Set a flag and do the teardown in main() once the input loop returns. This also closes the vDPA devices on exit from interactive mode, which was missing before.
--- examples/vdpa/main.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
examples/vdpa/main.c +8 -8
@@ -288,14 +288,13 @@ vdpa_sample_quit(void) } } +/* Set by SIGINT and SIGTERM handler, consumed by main(). */ +static volatile sig_atomic_t quit; +static void -signal_handler (int signum) +signal_handler(__rte_unused int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\nSignal %d received, preparing to exit...\n" , signum); -vdpa_sample_quit (); -exit (0 ); - } + quit =1 ; }/* interactive cmd functions */
@@ -513,7 +512,7 @@ main(int argc, char *argv[]) }printf ("enter \'q\' to quit\n" ); -while (scanf ("%c" , &ch)) { +while (!quit &&scanf ("%c" , &ch)) {if (ch =='q' )break ;while (ch !='\n' ) {
@@ -522,9 +521,10 @@ main(int argc, char *argv[]) }printf ("enter \'q\' to quit\n" ); } -vdpa_sample_quit (); } +vdpa_sample_quit (); +/* clean up the EAL */ rte_eal_cleanup ();
Signature · 1 line
2.53.0
PATCH v2 6/13 examples/vhost: make signal handler safe
In reply to Stephen HemmingerThe SIGINT handler unregistered the vhost drivers and called exit(), neither of which is async-signal-safe.
Set a flag, let switch_worker() return, and unregister the drivers in main() after joining the lcores.
--- examples/vhost/main.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
examples/vhost/main.c +8 -6
@@ -79,6 +79,9 @@ static int dma_count;/* mask of enabled ports */ static uint32_t enabled_port_mask =0 ; +/* Set by the SIGINT handler to stop the worker loops. */ +static volatile sig_atomic_t quit; +/* Promiscuous mode */ static uint32_t promiscuous;
@@ -1494,7 +1497,7 @@ switch_worker(void *arg) } } -while (1 ) { +while (!quit) {drain_mbuf_table (tx_q);drain_vhost_table ();/*
@@ -1872,14 +1875,10 @@ unregister_drivers(int socket_num) } } -/* When we receive a INT signal, unregister vhost driver */ static void sigint_handler(__rte_unused int signum) { -/* Unregister vhost driver. */ -unregister_drivers (nb_sockets); - -exit (0 ); + quit =1 ; } static void
@@ -2070,6 +2069,9 @@ main(int argc, char *argv[])RTE_LCORE_FOREACH_WORKER (lcore_id)rte_eal_wait_lcore (lcore_id); +/* Unregister vhost driver. */ +unregister_drivers (nb_sockets); +for (i =0 ; i < dma_count; i++) {if (rte_vhost_async_dma_unconfigure (dmas_id[i],0 ) <0 ) { RTE_LOG(ERR, VHOST_PORT,
Signature · 1 line
2.53.0
PATCH v2 7/13 examples/vhost_blk: do not tear down from signal handler
In reply to Stephen HemmingerThe SIGINT handler destroyed the controller and called exit() from signal context.
Set a flag, break the sleep loop, and destroy the controller in main().
--- examples/vhost_blk/vhost_blk.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-)
examples/vhost_blk/vhost_blk.c +14 -12
@@ -850,24 +850,18 @@ vhost_blk_ctrlr_destroy(struct vhost_blk_ctrlr *ctrlr)rte_vhost_driver_unregister (dev_pathname); } +/* Set by the SIGINT handler to break the main loop. */ +static volatile sig_atomic_t quit; +static void signal_handler(__rte_unused int signum) { -struct vhost_blk_ctrlr *ctrlr; - - ctrlr =vhost_blk_ctrlr_find (dev_pathname); -if (ctrlr ==NULL ) -return ; - -if (ctrlr->started) -destroy_device (ctrlr->vid); - -vhost_blk_ctrlr_destroy (ctrlr); -exit (0 ); + quit =1 ; }int main (int argc,char *argv[]) { +struct vhost_blk_ctrlr *ctrlr;int ret;/* init EAL */
@@ -895,9 +889,17 @@ int main(int argc, char *argv[]) }/* loop for exit the application */ -while (1 ) +while (!quit)sleep (1 ); + ctrlr =vhost_blk_ctrlr_find (dev_pathname); +if (ctrlr !=NULL ) { +if (ctrlr->started) +destroy_device (ctrlr->vid); + +vhost_blk_ctrlr_destroy (ctrlr); + } +/* clean up the EAL */ rte_eal_cleanup ();
Signature · 1 line
2.53.0
PATCH v2 8/13 examples/ntb: do not print and re-raise from signal handler
In reply to Stephen HemmingerThe handler called printf(), reset the disposition to SIG_DFL and killed itself, so the process died without stopping the forwarding lcores or closing the devices.
Set the existing per-lcore stopped flag instead, and mark it volatile since it is now written from signal context.
SIGINT no longer terminates the application; it stops forwarding and returns to the ntb> prompt, where quit does the teardown.
--- examples/ntb/ntb_fwd.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
examples/ntb/ntb_fwd.c +10 -6
@@ -41,7 +41,7 @@ struct ntb_fwd_stream {struct ntb_fwd_lcore_conf {uint16_t stream_id;uint16_t nb_stream; -uint8_t stopped; +volatile uint8_t stopped; };enum ntb_fwd_mode {
@@ -947,12 +947,16 @@ prompt(void) }static void -signal_handler (int signum) +signal_handler(__rte_unused int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\nSignal %d received, preparing to exit...\n" , signum); -signal (signum,SIG_DFL ); -kill (getpid (), signum); +struct ntb_fwd_lcore_conf *conf; +uint32_t lcore_id; + +RTE_LCORE_FOREACH_WORKER (lcore_id) { + conf = &fwd_lcore_conf[lcore_id]; + +if (conf->nb_stream) + conf->stopped =1 ; } }
Signature · 1 line
2.53.0
PATCH v2 9/13 examples/ipsecgw: do not print from signal handler
In reply to Stephen HemmingerThe SIGINT handler calls printf which is not async-signal safe.
--- examples/ipsec-secgw/ipsec-secgw.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
examples/ipsec-secgw/ipsec-secgw.c +1 -4
@@ -2448,11 +2448,8 @@ create_default_ipsec_flow(uint16_t port_id, uint64_t rx_offloads)static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } } static void
Signature · 1 line
2.53.0
PATCH v2 10/13 examples/l2fwd-macsec: remove print in signal handler
In reply to Stephen HemmingerCalling printf in signal handler is not async-signal-safe. The printf was unnecessary anyway so remove it. Found by GCC -fanalyzer.
--- examples/l2fwd-macsec/main.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
examples/l2fwd-macsec/main.c +1 -4
@@ -1247,11 +1247,8 @@ check_all_ports_link_status(uint32_t port_mask)static void signal_handler (int signum) { -if (signum ==SIGINT || signum ==SIGTERM ) { -printf ("\n\nSignal %d received, preparing to exit...\n" , - signum); +if (signum ==SIGINT || signum ==SIGTERM ) force_quit = true; - } }int
Signature · 1 line
2.53.0
PATCH v2 11/13 examples/ethtool: fix exit flag and unchecked cmdline
In reply to Stephen HemmingerThe exit_now flag is polled by worker_main() on one lcore and set by main() on another, but is a plain int so the load can be hoisted out of the loop. Make it volatile.
Also check the cmdline_stdin_new() result before using it.
Found by gcc -fanalyzer.
--- examples/ethtool/ethtool-app/ethapp.c | 5 +++++ examples/ethtool/ethtool-app/main.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-)
examples/ethtool/ethtool-app/ethapp.c +5 -0
@@ -4,6 +4,8 @@#include <stdlib.h> +#include <rte_debug.h> +#include <cmdline_parse.h> #include <cmdline_parse_num.h> #include <cmdline_parse_string.h>
@@ -914,6 +916,9 @@ void ethapp_main(void)struct cmdline *ctx_cmdline; ctx_cmdline =cmdline_stdin_new (list_prompt_commands,"EthApp> " ); +if (ctx_cmdline ==NULL ) +rte_panic ("Cannot create cmdline instance\n" ); +cmdline_interact (ctx_cmdline);cmdline_stdin_exit (ctx_cmdline); }
examples/ethtool/ethtool-app/main.c +1 -1
@@ -44,7 +44,7 @@ struct app_port {struct app_config {struct app_port ports[MAX_PORTS ];int cnt_ports; -int exit_now; +volatile int exit_now; };
Signature · 1 line
2.53.0
PATCH v2 12/13 examples/vmdq_dcb: allow exit on signal
In reply to Stephen HemmingerThe forwarding loop never terminated, so the application could only be killed. There was also no wait for the worker lcores.
Add a SIGINT and SIGTERM handler that sets a flag, return from lcore_main() when it is set, and join the workers before cleanup.
--- examples/vmdq_dcb/main.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
examples/vmdq_dcb/main.c +19 -1
@@ -513,6 +513,15 @@ update_mac_address(struct rte_mbuf *m, unsigned dst_port)rte_ether_addr_copy (&vmdq_ports_eth_addr[dst_port], ð->src_addr); } +/* Set by the SIGINT and SIGTERM handler to stop the forwarding loops. */ +static volatile sig_atomic_t quit; + +static void +signal_handler(__rte_unused int signum) +{ + quit =1 ; +} +/* When we receive a HUP signal, print out our stats */ static void sighup_handler (int signum)
@@ -567,7 +576,7 @@ lcore_main(void *arg)return 0 ; } -for (;;) { +while (!quit) {struct rte_mbuf *buf[MAX_PKT_BURST ];const uint16_t buf_size =RTE_DIM (buf);for (p =0 ; p < num_ports; p++) {
@@ -598,6 +607,8 @@ lcore_main(void *arg) } } } + +return 0 ; }/*
@@ -640,6 +651,8 @@ main(int argc, char *argv[])uint16_t portid;signal (SIGHUP , sighup_handler); +signal (SIGINT , signal_handler); +signal (SIGTERM , signal_handler);/* init EAL */ ret =rte_eal_init (argc, argv);
@@ -697,6 +710,11 @@ main(int argc, char *argv[])/* call on main too */ (void )lcore_main ((void *)i); +RTE_LCORE_FOREACH_WORKER (lcore_id) { +if (rte_eal_wait_lcore (lcore_id) <0 ) +return -1 ; + } +/* clean up the EAL */ rte_eal_cleanup ();
Signature · 1 line
2.53.0
PATCH v2 13/13 examples/eventdev_pipeline: make signal handler safe
In reply to Stephen HemmingerThis example was calling functions like rte_exit and printing from a signal handler which is not async signal safe.
Revise to spit the two signal actions here:
- The handler for SIGINT and SIGTERM set a flag which causes main loop to exit and proceed with normal clean shutdown. If a second SIGINT is received the process will get default signal handler (i.e process exit).
- The dump flag handler is changed form SIGTSTP (^Z) to use SIGUSR1 since that is more standard practice in Linux.
--- examples/eventdev_pipeline/main.c | 39 ++++++++++++-------- examples/eventdev_pipeline/pipeline_common.h | 13 ++++--- 2 files changed, 31 insertions(+), 21 deletions(-)
examples/eventdev_pipeline/main.c +24 -15
@@ -312,20 +312,16 @@ do_capability_setup(uint8_t eventdev_id) }static void -signal_handler (int signum) +signal_quit(int signum __rte_unused) { -static uint8_t once; - -if (fdata->done) -rte_exit (1 ,"Exiting on signal %d\n" , signum); -if ((signum ==SIGINT || signum ==SIGTERM ) && !once) { -if (cdata.dump_dev) -rte_event_dev_dump (0 , stdout); - once =1 ; +if (fdata !=NULL ) fdata->done =1 ; - } -if (signum ==SIGTSTP ) -rte_event_dev_dump (0 , stdout); +} + +static void +signal_dump(int signum __rte_unused) +{ + cdata.dump_dev_signal =1 ; } static inline uint64_t
@@ -345,9 +341,19 @@ main(int argc, char **argv)int lcore_id;int err; -signal (SIGINT , signal_handler); -signal (SIGTERM , signal_handler); -signal (SIGTSTP , signal_handler); +/* second SIGINT/SIGTERM takes the default action */ +struct sigaction sa = { + .sa_handler = signal_quit, + .sa_flags =SA_RESETHAND , + }; +sigemptyset (&sa.sa_mask); +sigaction (SIGINT , &sa,NULL ); +sigaction (SIGTERM , &sa,NULL ); + +/* allow repeated dump signals */ + sa.sa_handler = signal_dump; + sa.sa_flags =0 ; +sigaction (SIGUSR1 , &sa,NULL ); err =rte_eal_init (argc, argv);if (err <0 )
@@ -452,6 +458,9 @@ main(int argc, char **argv)rte_eal_mp_wait_lcore (); +if (cdata.dump_dev) +rte_event_dev_dump (0 , stdout); +if (!cdata.quiet && (port_stat (dev_id, worker_data[0 ].port_id) != (uint64_t )-ENOTSUP )) {printf ("\nPort Workload distribution:\n" );
examples/eventdev_pipeline/pipeline_common.h +7 -6
@@ -3,6 +3,7 @@* Copyright 2017 Cavium, Inc. */ +#include <signal.h> #include <stdbool.h> #include <rte_eal.h>
@@ -43,7 +44,7 @@ struct setup_data { };struct __rte_cache_aligned fastpath_data { -volatile int done; +volatile sig_atomic_t done;uint32_t evdev_service_id;uint32_t rxadptr_service_id;uint32_t txadptr_service_id;
@@ -68,7 +69,7 @@ struct config_data {int enable_queue_priorities;int quiet;int dump_dev; -int dump_dev_signal; +volatile sig_atomic_t dump_dev_signal;int all_type_queues;unsigned int num_stages;unsigned int worker_cq_depth;
@@ -127,16 +128,16 @@ schedule_devices(unsigned int lcore_id)if (fdata->sched_core[lcore_id]) {rte_service_run_iter_on_app_lcore (fdata->evdev_service_id, !fdata->sched_single); -if (cdata.dump_dev_signal) { -rte_event_dev_dump (0 , stdout); - cdata.dump_dev_signal =0 ; - } }if (fdata->tx_core[lcore_id]) {rte_service_run_iter_on_app_lcore (fdata->txadptr_service_id, !fdata->tx_single); } +if (cdata.dump_dev_signal) { +rte_event_dev_dump (0 , stdout); + cdata.dump_dev_signal =0 ; + } } static void
Signature · 1 line
2.53.0
Quoted 6 lines
Stephen Hemminger (13):
graph: do not call printf in signal
examples: remove printf from signal handler examples/vmdq: do not print from signal handler examples/symmetric_mp: do not print or exit in handler examples/vdpa: make signal handler safe
Quoted 8 lines
examples/vhost: make signal handler safe examples/vhost_blk: do not tear down from signal handler examples/ntb: do not print and re-raise from signal handler examples/ipsecgw: do not print from signal handler examples/l2fwd-macsec: remove print in signal handler examples/ethtool: fix exit flag and unchecked cmdline examples/vmdq_dcb: allow exit on signal examples/eventdev_pipeline: make signal handler safe
Applied with a bit of squashing for useless printf, thanks.