BUG/MEDIUM: pattern: don't trim pools under lock in pat_ref_purge_range()
authorWilly Tarreau <w@1wt.eu>
Sat, 4 Nov 2023 06:55:37 +0000 (07:55 +0100)
committerWilly Tarreau <w@1wt.eu>
Sat, 4 Nov 2023 06:55:37 +0000 (07:55 +0100)
commit58185669d8a0891948caaa7b357d78775b0cecb3
treefefe23a0defca7df63e47312cb70ff01e3f0619b
parentce7501de79b035a03c52aa47c671aac2fcdfd631
BUG/MEDIUM: pattern: don't trim pools under lock in pat_ref_purge_range()

There's a subtle issue that results from pat_ref_purge_range() trying
to release memory. Since commit 0d93a8186 ("MINOR: pools: work around
possibly slow malloc_trim() during gc") that was backported to 2.3,
trim_all_pools() now protects itself against concurrent malloc() and
free() by isolating itself. The problem is that pat_ref_purge_range()
must be called under a lock, which is precisely what's done in
cli_io_handler_clear_map(). Thus during a clearing of a map, if
another thread tries to access or update an entry in the same map, it
will wait for the ref->lock to be released, and trim_all_pools() will
wait for all threads to be harmless, thus causing a deadlock. Note
that disabling memory trimming cannot work around the problem here
because it's tested only under isolation.

The solution here consists in moving the call to trim_all_pools() to
the caller, out of the lock.

This must be backported as far as 2.4.
src/map.c
src/pattern.c