Commit graph

221 commits

Author SHA1 Message Date
Wim Taymans
ffedc2d1e8 null-sink: add option to debug memory 2022-07-04 13:38:07 +02:00
Lucas Holt
6a15a02ec2 Add support for MidnightBSD
Fix build issue

Fix build issue
2022-06-19 18:22:47 +00:00
Barnabás Pőcze
479896279e spa: support: loop: handle cancellation better
Register a pthread cleanup handler to guarantee
that `spa_source::{priv, rmask}` are cleared even
if the thread is cancelled while the loop is dispatching.

This is necessary, otherwise `spa_source::priv` could point
to the stack of the cancelled thread, which will lead to
problems like this later:

  Program terminated with signal SIGSEGV, Segmentation fault.
  #0  0x00007f846b025be2 in detach_source (source=0x7f845f435f60) at ../spa/plugins/support/loop.c:144
  144      e->data = NULL;
  [Current thread is 1 (LWP 5274)]
  (gdb) p e
  $1 = (struct spa_poll_event *) 0x7f845e297820
  (gdb) bt
  #0  0x00007f846b025be2 in detach_source (source=0x7f845f435f60) at ../spa/plugins/support/loop.c:144
  #1  0x00007f846b0276ad in free_source (s=0x7f845f435f60) at ../spa/plugins/support/loop.c:359
  #2  0x00007f846b02a453 in loop_destroy_source (object=0x7f845f3af478, source=0x7f845f435f60) at ../spa/plugins/support/loop.c:786
  #3  0x00007f846b02a886 in impl_clear (handle=0x7f845f3af478) at ../spa/plugins/support/loop.c:859
  #4  0x00007f846b172f40 in unref_handle (handle=0x7f845f3af450) at ../src/pipewire/pipewire.c:211
  #5  0x00007f846b173579 in pw_unload_spa_handle (handle=0x7f845f3af478) at ../src/pipewire/pipewire.c:346
  #6  0x00007f846b15a761 in pw_loop_destroy (loop=0x7f845f434e30) at ../src/pipewire/loop.c:159
  #7  0x00007f846b135d8e in pw_data_loop_destroy (loop=0x7f845f434cb0) at ../src/pipewire/data-loop.c:166
  #8  0x00007f846b12c31c in pw_context_destroy (context=0x7f845f41c690) at ../src/pipewire/context.c:485
  #9  0x00007f846b3ddf9e in jack_client_close (client=0x7f845f3c1030) at ../pipewire-jack/src/pipewire-jack.c:3481
  ...
2022-06-02 00:24:24 +02:00
Wim Taymans
74da804e97 move some debug to fastpath 2022-03-28 16:25:00 +02:00
Wim Taymans
54933b67fd loop: clean polling flag when leaving the loop
When we leave the last recursive enter of the loop, clear the polling
flag.

It might be possible that it was not cleared because the loop might have
been killed with pthread_kill. In any case, the _leave calls need to be
made in this case as well.

This fixes issues when jack clients stop because it triggers and assert
because the polling flag is still active when the object is cleared.

See !1171
2022-03-07 10:27:22 +01:00
Barnabás Pőcze
b12f24efb9 spa: support: loop: don't initialize source->loop
There is no need to initialize the `loop`, `rmask`, or `priv`
members of a `spa_source` because `loop_add_source()` will
take care of that.
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
d6dfa93b40 spa: support: loop: assert source's loop when dispatching 2022-03-06 18:40:43 +00:00
Barnabás Pőcze
235b155b75 spa: support: loop: assert loop is not polling when destroyed
Assert that the loop is not polling when `impl_clear()` is called.
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
616519d704 spa: support: loop: assert loop is not polling when source is removed
`spa_source`s whose backing storage is not managed by the loop
cannot be safely removed while the loop is polling.
Assert that it does not happen.
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
60b9d9081b spa: only remove embedded source from data loop from within the loop
Use `spa_loop_invoke()` to invoke a callback on the data loop
to remove an embedded `spa_source` from the data loop.

Embedded `spa_source` objects cannot be safely removed
while the loop is polling without risking potential
use-after-frees.
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
7647ea7c83 spa: support: loop: fix use-after-free when loop is reentered
The core of the issue is the following: what happens if an
active source is destroyed before it could be dispatched?

For loop-managed sources (`struct source_impl`) this was addressed
by storing all destroyed sources in a list, and only freeing them
after dispatching has been finished. (0eb73f0f06)
This approach works for both strictly single-threaded
and `pw_thread_loop` loops assuming the loop is not
reentered.

However, if the loop is reentered, there can still be issues.
Assume that in one iteration sources A and B are active,
and returned from the system call, and source B is destroyed
before the loop starts dispatching. Consider what happens when
"A" is dispatched first, and it reenters the loop with timeout 0.
Imagine there are no new events, so `loop_iterate()` will immediately
return, but it will first destroy everything in the destroy list
(this is done at the end of `loop_iterate()`).
And herein lies the problem. In the previous iteration,
there exists a `spa_poll_event` object which points to source "B",
but that has just been destroyed at the end of the recursive
iteration. This will trigger a use-after-free once the previous
iteration inspects it.

Fix that by processing the destroy list right after first
processing the returned `spa_poll_event` objects, and
"detach" the source from the loop and its iterations
in `process_destroy()` before the source is destroyed.

See #2114 #2147
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
2eb36c00c1 spa: support: loop: add polling flag
Store whether or not the loop is currently polling, i.e.
calling `spa_system_pollfd_wait()`.
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
4ed0365976 spa: support: loop: assert source type 2022-03-06 18:40:43 +00:00
Barnabás Pőcze
cfc8510ce8 spa: support: loop: add some invariant assertions 2022-03-06 18:40:43 +00:00
Barnabás Pőcze
55ee5ec8b2 spa: support: loop: rename variables
It may be a little confusing that both the loop object
and the `source_impl` objects are referred to with variables
named `impl`. For this reason, rename all source_impl objects
named `impl` to `s`.
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
a4e7042176 spa: support: loop: do not return early in case of an error
It is expected that `nfds` is non-negative in the vast majority
of cases, so hopefully the runtime performance will not be
significantly affected by removing the check. This way
it is guaranteed that the destroy list is processed.
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
275e23a34d spa: support: loop: print previous mask when updating
Print the previous event mask in `loop_update_io()`
as well as the new one.
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
8941fc2866 spa: support: loop: get array size using macro 2022-03-06 18:40:43 +00:00
Barnabás Pőcze
cb8c2d8857 spa: support: loop: reset rmask after dispatch
Reset the `rmask` of the sources to zero after
dispatching the callbacks. This way the sources
are always as up-to-date as possible.
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
a22ce76dbf spa: support: loop: initialize rmask
Set `rmask` to zero when a source is added to,
or removed from the loop.
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
e2287f35db spa: support: loop: move struct members
Move the boolean members of `struct source_impl` to the end
of the struct. This changes the size of the struct from
104 bytes to 96 bytes on x86-64.

Before:

struct source_impl {
        struct spa_source          source;               /*     0    48 */
        struct impl *              impl;                 /*    48     8 */
        struct spa_list            link;                 /*    56    16 */
        /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
        _Bool                      close;                /*    72     1 */

        /* XXX 7 bytes hole, try to pack */

        union {
                spa_source_io_func_t io;                 /*    80     8 */
                spa_source_idle_func_t idle;             /*    80     8 */
                spa_source_event_func_t event;           /*    80     8 */
                spa_source_timer_func_t timer;           /*    80     8 */
                spa_source_signal_func_t signal;         /*    80     8 */
        } func;                                          /*    80     8 */
        _Bool                      enabled;              /*    88     1 */

        /* XXX 7 bytes hole, try to pack */

        struct spa_source *        fallback;             /*    96     8 */

        /* size: 104, cachelines: 2, members: 7 */
        /* sum members: 90, holes: 2, sum holes: 14 */
        /* last cacheline: 40 bytes */
};

After:

struct source_impl {
        struct spa_source          source;               /*     0    48 */
        struct impl *              impl;                 /*    48     8 */
        struct spa_list            link;                 /*    56    16 */
        /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
        union {
                spa_source_io_func_t io;                 /*    72     8 */
                spa_source_idle_func_t idle;             /*    72     8 */
                spa_source_event_func_t event;           /*    72     8 */
                spa_source_timer_func_t timer;           /*    72     8 */
                spa_source_signal_func_t signal;         /*    72     8 */
        } func;                                          /*    72     8 */
        struct spa_source *        fallback;             /*    80     8 */
        _Bool                      close;                /*    88     1 */
        _Bool                      enabled;              /*    89     1 */

        /* size: 96, cachelines: 2, members: 7 */
        /* padding: 6 */
        /* last cacheline: 32 bytes */
};
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
16f63a3c8f Revert "loop: remove destroy list"
This reverts commit c474846c42.
In addition, `s->loop` is also checked before dispatching a source.

The destroy list is needed in the presence of threads. The
issue is that a source may be destroyed between `epoll_wait()`
returning and thread loop lock being acquired. If this
source is active, then a use-after-free will be triggered
when the thread loop acquires the lock and starts dispatching
the sources.

  thread 1                       thread 2
 ----------                     ----------
                                loop_iterate
                                  spa_loop_control_hook_before
                                    // release lock

 pw_thread_loop_lock

                                  spa_system_pollfd_wait
                                    // assume it returns with source A

 pw_loop_destroy_source(..., A)
  // frees storage of A

 pw_thread_loop_unlock
                                  spa_loop_control_hook_after
                                    // acquire the lock

                                  for (...) {
                                    struct spa_source *s = ep[i].data;
                                    s->rmask = ep[i].events;
                                      // use-after-free if `s` refers to
                                      // the previously freed `A`

Fixes #2147
2022-02-18 20:31:14 +01:00
Barnabás Pőcze
bf886ba209 support: also protect against recursive invocations
Add an extra private field to the source to store the pollevent of
the current iteration. This changes ABI but it seems an embedded source
is not used outside of our own plugins and the unit test doesn't test
this ABI case.

Whenever a source is removed, we can set the data field of the
pollevent to NULL so that it won't be handled in any iteration anymore.

Avoid dispatching the same event multiple times when doing recursive
iterations.

Add some more unit tests for this.

Fixes #2114
2022-02-08 17:21:10 +01:00
Alexandre BIQUE
0b637c3291 support: loop_enter/leave hardening
This commit adds a counter for loop_enter/leave and checks:

 - consecutive enter are used on the same thread
 - leave is used on the same thread as enter
 - at destruction, the enter_count must be 0
2022-02-08 12:02:50 +01:00
Wim Taymans
c346ee5e90 support: close log file when we opened it 2022-02-08 11:30:39 +01:00
Wim Taymans
c474846c42 loop: remove destroy list
Now that sources can't be dispatched anymore after a _remove, we don't
need to keep the destroy_list anymore and we can free the source
immediately.

See #2114
2022-02-08 10:18:02 +01:00
Wim Taymans
45d911641b loop: handle remove while dispatching better
Keep the array of dispatched sources around in the loop. When a source
is removed while dispatching, set the data to NULL so that we don't try
to deref the source again or call its function.

Fixes #2114
2022-02-08 10:17:13 +01:00
Barnabás Pőcze
cc73053512 treewide: meson.build: use feature.allowed()
Since meson 0.59.0, a feature object has an `allowed()`
method which returns true when the feature is set to
'enabled' or 'auto'.

Utilize that instead of the previously used

  not feature.disabled()
2022-02-04 00:15:59 +01:00
Wim Taymans
6ece5d810c loop: invoke immediately when loop is not running
Or else we might never get our callback called or worse, block forever,
waiting for the response.
2022-01-18 20:03:01 +01:00
Wim Taymans
776b52749f Use configured quantum_limit instead of hardcoded value
Parse the quantum_limit parameters and use this to scale the buffers so
that they can contain the maximum allowed samples instead of the
hardcoded 8192 value.

See #1931
2022-01-12 17:50:12 +01:00
Wim Taymans
c46113faa3 json: spa_json_get_string() writes up to len chars or fail 2022-01-04 10:42:32 +01:00
Wim Taymans
35cbe4e939 buffers: make alignment optional
Make the alignment parameter optional when negotiating buffers.
Default to a 16 bytes alignment and adjust for the max cpu
alignment.
Remove the useless align buffer parameter in plugins, we always
set it to 16 anyway.
2022-01-03 12:32:26 +01:00
Wim Taymans
30982775d9 support: use defines for alignment 2022-01-03 11:14:15 +01:00
Barnabás Pőcze
2b110af366 treewide: meson.build: use dependency variable for SPA
Use `spa_dep` everywhere instead of `spa_inc`,
and remove `spa_inc` altogether.
2021-12-28 18:34:06 +01:00
Wim Taymans
d86008cf8f support: set clock name for null-audio-sink 2021-12-10 12:43:03 +01:00
Wim Taymans
53352c0c81 support: add property to configure clock name
Add a clock.name property on the driver node. By default this is
clock.system.monotonic for the monotonic system clock.
2021-12-10 11:22:23 +01:00
Wim Taymans
84ecebbd4e cpu: only run SSE code when supported
Check if we can run SSE instructions before executing the denormals
SSE code.

Fixes #1775
2021-11-02 20:12:35 +01:00
Wim Taymans
2cddd4a775 cpu: disable VFP asm when not available
Fixes #1746
2021-10-25 16:32:16 +02:00
Wim Taymans
651f0decea cpu: fix compilation on some architectures 2021-10-21 11:09:48 +02:00
Wim Taymans
54c0857d57 cpu: also pass the sse_args when compiling with SSE 2021-10-13 15:10:21 +02:00
Wim Taymans
14fd5425af cpu: use intrinsics to modify status register
For better portability. Also only use the code when we can compile with
SSE is available.
2021-10-13 15:00:09 +02:00
Peter Hutterer
c44d0a6bbc spa: declare a dependency for the dbus lib
When running meson test without a preceding ninja build, we end up with
missing libraries. This somehow happened to work until recently but now
it triggers an issue with libspa-dbus.so not being available during the
valgrind test run, causing a test case to fail.
2021-10-12 09:45:33 +10:00
Wim Taymans
85d5c8cd6c cpu: add zero_denormals method
Add a method to enable/disable the denormals flush-to-zero and
denormals-as-zero CPU options.

Add a config option to make it possible to disable this again.

Fixes high CPU usage when dealing with denormals, which can happen
in many DSP functions.

Fixes #1681
2021-10-11 14:57:02 +02:00
Wim Taymans
5f7c4dec34 meson: make it possible to compile without dbus
Make an option to disable dbus and all the code that depends on
it.

Fixes #1685
2021-10-09 15:00:04 +02:00
Wim Taymans
392b2f1f96 dbuf: use log topic 2021-10-03 12:27:20 +02:00
Wim Taymans
f8817b4394 cpu: fix compilation on ARM 2021-09-30 11:12:27 +02:00
Wim Taymans
013567524a support: res might be used uninitialized 2021-09-29 17:08:46 +02:00
Barnabás Pőcze
184e80d753 spa: support: use spa_list_consume()
Use `spa_list_consume()` when freeing entities in the pattern
list instead of `spa_list_for_each_safe()`.
2021-09-29 15:07:36 +00:00
Barnabás Pőcze
25a0e3beb4 spa: support: do not include C source file
Previously, `log-patterns.c` was included in two other source files
(`journal.c` and `logger.c`). It was also specified in the sources
list for the libspa-support library, which resulted in the unnecessary
independent compilation of the file, generating "defined but not used"
warnings.

Extract the function definitions into `log-patterns.h` and
use that in `journal.c` and `logger.c`, and remove the inclusion
of `log-patterns.c` from both.
2021-09-29 15:07:36 +00:00
Barnabás Pőcze
b292a71220 spa: support: use flexible array member
Use a flexible array member directly to store the pattern.
2021-09-29 15:07:36 +00:00