libosmocore/src
Neels Hofmeyr 1f9cc01861 fsm: support graceful osmo_fsm_inst_term() cascades
Add global flag osmo_fsm_term_safely() -- if set to true, enable the following
behavior:

Detect osmo_fsm_inst_term() occuring within osmo_fsm_inst_term():
- collect deallocations until the outermost osmo_fsm_inst_term() is done.
- call osmo_fsm_inst_free() *after* dispatching the parent event.

If a struct osmo_fsm_inst enters osmo_fsm_inst_term() while another is already
within osmo_fsm_inst_term(), do not directly deallocate it, but talloc-reparent
it to a separate talloc context, to be deallocated with the outermost FSM inst.

The effect is that all osmo_fsm_inst freed within an osmo_fsm_inst_term()
cascade will stay allocated until all osmo_fsm_inst_term() are complete and all
of them will be deallocated at the same time.

Mark the deferred deallocation state as __thread in an attempt to make cascaded
deallocation handling threadsafe.  Keep the enable/disable flag separate, so
that it is global and not per-thread.

The feature is showcased by fsm_dealloc_test.c: with this feature, all of those
wild deallocation scenarios succeed.

Make fsm_dealloc_test a normal regression test in testsuite.at.

Rationale:

It is difficult to gracefully handle deallocations of groups of FSM instances
that reference each other. As soon as one child dispatching a cleanup event
causes its parent to deallocate before fsm.c was ready for it, deallocation
will hit a use-after-free. Before this patch, by using parent_term events and
distinct "terminating" FSM states, parent/child FSMs can be taught to wait for
all children to deallocate before deallocating the parent. But as soon as a
non-child / non-parent FSM instance is involved, or actually any other
cleanup() action that triggers parent FSMs or parent talloc contexts to become
unused, it is near impossible to think of all possible deallocation events
ricocheting, and to avoid running into freeing FSM instances that were still in
the middle of osmo_fsm_inst_term(), or FSM instances to enter
osmo_fsm_inst_term() more than once. This patch makes deallocation of "all
possible" setups of complex cross referencing FSM instances easy to handle
correctly, without running into use-after-free or double free situations, and,
notably, without changing calling code.

Change-Id: I8eda67540a1cd444491beb7856b9fcd0a3143b18
2019-04-11 05:36:36 +00:00
..
codec Bump version: 0.11.0.91-9d4a3-dirty → 0.12.0 2018-07-27 17:31:47 +02:00
coding coding: Always initialize bit counters in gsm0503_pdtch_egprs_decode(). 2018-09-05 16:08:34 +03:00
ctrl select: Rename BSC_FD_* constants to OSMO_FD_* 2019-03-21 16:02:01 +00:00
gb Add _c versions of functions that otherwise return static buffers 2019-04-10 22:42:32 +00:00
gsm gsm_utils.c: fix Doxygen description for gsm_get_octet_len() 2019-04-11 00:24:55 +00:00
pseudotalloc Fix embedded (arm-none-eabi) builds 2018-06-28 10:30:34 +02:00
sim Add _c versions of functions that otherwise return static buffers 2019-04-10 22:42:32 +00:00
vty vty/talloc_ctx_vty.c: use REG_NOSUB flag of regcomp() 2019-04-11 00:24:55 +00:00
Makefile.am add osmo_use_count API 2019-04-08 13:47:17 +00:00
application.c fix logging talloc ctx: add osmo_init_logging2() 2018-03-28 19:06:16 +02:00
backtrace.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
bitcomp.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
bits.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
bitvec.c Don't call abort() directly, always use osmo_panic() 2018-06-28 10:57:42 +02:00
conv.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
conv_acc.c conv_acc: Our code requires SSSE3, not just SSE3 2017-11-17 11:44:22 +01:00
conv_acc_generic.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
conv_acc_sse.c conv_acc: Our code requires SSSE3, not just SSE3 2017-11-17 11:44:22 +01:00
conv_acc_sse_avx.c conv_acc: Our code requires SSSE3, not just SSE3 2017-11-17 11:44:22 +01:00
conv_acc_sse_impl.h Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
counter.c counters: add osmo_counters_count() returns the amount of counters 2017-12-05 16:06:27 +01:00
crc16.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
crcXXgen.c.tpl Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
fsm.c fsm: support graceful osmo_fsm_inst_term() cascades 2019-04-11 05:36:36 +00:00
gsmtap_util.c src/gsmtap_util.c: fix possible memleak in gsmtap_source_init() 2019-03-28 10:07:59 +00:00
isdnhdlc.c isdnhdlc: Port from kernel to userspace 2018-05-11 21:57:46 +02:00
logging.c src/logging.c: fix syntax errors in Doxygen documentation 2019-03-27 08:56:21 +01:00
logging_gsmtap.c logging_gsmtap.c: document all params of log_target_create_gsmtap() 2019-03-27 08:56:21 +01:00
logging_syslog.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
loggingrb.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
macaddr.c osmo_get_macaddr: Fix buffer read out of bounds 2018-04-18 08:57:56 +00:00
msgb.c Add _c versions of functions that otherwise return static buffers 2019-04-10 22:42:32 +00:00
msgfile.c memleak: osmo_config_list_parse: getline() needs free also on error 2017-11-18 10:30:57 +00:00
panic.c osmo_panic(): Annotate as __attribute__ ((noreturn)) 2018-06-29 20:32:57 +02:00
plugin.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
prbs.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
prim.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
rate_ctr.c src/rate_ctr.c: drop some incorrect \ref references 2019-03-27 08:56:21 +01:00
rbtree.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
select.c select: Rename BSC_FD_* constants to OSMO_FD_* 2019-03-21 16:02:01 +00:00
sercomm.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
serial.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
signal.c Revert "signal.c: Make non-exported tall_sigh_ctx static" 2019-03-22 13:25:01 +01:00
sockaddr_str.c add osmo_sockaddr_str API 2019-04-08 13:47:17 +00:00
socket.c Add _c versions of functions that otherwise return static buffers 2019-04-10 22:42:32 +00:00
stat_item.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
stats.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
stats_statsd.c stats_statsd: Send all stat_items as gauges 2018-10-24 16:37:05 +02:00
strrb.c Fix/Update copyright notices; Add SPDX annotation 2017-11-13 01:35:12 +09:00
tdef.c fsm: add osmo_fsm_inst_state_chg_keep_or_start_timer() 2019-03-07 23:10:21 +01:00
timer.c Doxygen: fix documentation of osmo_timer_setup() 2019-03-27 08:56:21 +01:00
timer_clockgettime.c Fix embedded (arm-none-eabi) builds 2018-06-28 10:30:34 +02:00
timer_gettimeofday.c timer: Document osmo_gettimeofday API 2018-03-01 12:33:03 +00:00
use_count.c add osmo_use_count API 2019-04-08 13:47:17 +00:00
utils.c Add _c versions of functions that otherwise return static buffers 2019-04-10 22:42:32 +00:00
write_queue.c select: Rename BSC_FD_* constants to OSMO_FD_* 2019-03-21 16:02:01 +00:00