test: minor cleanups
test: actually run test-timeout

Probably a copy/paste typo. If we say we run timeout-tests, lets actually
run timeout tests, not synchronous message passing tests.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
connection: add _acquire() and _release() helpers

Kdbus objects might be disconnected asynchronously at any time. Whenever
an object is disabled, we want any ioctl to fail with ECONNRESET on it. So
far, we have to check for object->disconnected whenever we acquire an
object lock. This is tedious and error-prone, and also kinda inconsistent
with the error-codes we return.

This patch introduces active-refs for kdbus connections. Whenever anyone
holds an active-ref, any disconnection will be delayed until all
active-refs are dropped. To prevent kdbus_conn_disconnect() from stalling,
you must never hold active-refs for an indefinite period. The concept is
mostly copied from kernfs_node in fs/kernfs/dir.c which uses this to
protect sysfs-attribute execution from parallel removal.

So far, only the MATCH_ADD/REMOVE ioctls make use of this. Further users
will follow. This will eventually allow us to drop most of the
kdbus_conn_active() tests in our code.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
connection: add lockdep annotations to 'active'-refs

This is mostly copied from kernfs, which employs the same "active-ref"
counters on attributes than we do.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
test: add race-testing

This adds some rather trivial helpers to test for cross-thread races when
we run multiple ioctls in parallel. If allows you to define code-blocks
that run before/after the test-run, and one code-block for each thread.
The threads are started in random order and synchronized via spin-locks,
so the start time is as close as possible.

Furthermore, we allow you to run tests multiple times and examine all the
results that occurred. Since we randomize thread execution, and you can
add random usleep()-calls, we can try to verify all possible races were
exercised. This is, obviously, not reliable, so we only return TEST_SKIP
in case something didn't go as expected.

The tests so far are rather trivial. More tests will follow.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
test-connection: trivial fixups

Signed-off-by: Daniel Mack <daniel@zonque.org>
matchdb: account maximum number of matches per user

Signed-off-by: Daniel Mack <daniel@zonque.org>
kdbus.txt: fix spelling

Signed-off-by: Daniel Mack <daniel@zonque.org>
policy: consider KDBUS_POLICY_MAX_SIZE

Signed-off-by: Daniel Mack <daniel@zonque.org>
bus: reintroduce KDBUS_MAKE_POLICY_OPEN

Re-introduce KDBUS_MAKE_POLICY_OPEN flag in the bus make ioctl.

When not specified, an empty policy is created on the default endpoint of
the bus. As it is empty be default, this means that no messages may pass
unless policy holders are uploaded.

In the tests, set this flag by default.

Signed-off-by: Daniel Mack <daniel@zonque.org>
notify: fix race between notify-flush and bus-disconnect

bus->ep is the default endpoint of a bus. As it has a reference to the bus
itself, we have to drop it when disconnecting a bus. However, flushing the
notification queues is done async (as it needs the bus lock) on bus->ep.
Make sure we own a reference during the flush.

Note that if the bus is disconnected while we currently flush the queue,
then kdbus_conn_kmsg_send() will notice it. In the upper layer, we just
need to make sure the endpoint stays accessible.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
bus: lock inside of kdbus_bus_find_conn_by_id()

All callers of find_conn_by_id() lock bus->lock around this call. Move
this into the helper to reduce complexity. We should avoid locking
bus->lock outside of bus.c whenever possible. And in this case, we really
gain nothing by locking outside of bus.c so far.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
match: turn atomic entry-counter into normal integer

We lock db->lock whenever we add/remove entries. There's no reason to make
the entry-counter atomic, we can just modify it inside of the lock.
There's also no high lock-contention, so this should be just fine.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
policy: fix docs

Add _unlocked prefix to the Docbook header for policy-db.
policy: don't access policy-db while iterating items

This builds up all entries in a temporary list while iterating policy
items. Previously, we have been applying them directly, which requires us
to hold the lock the entire time. This patch prepares for further
simplifications by building up a temporary list and trying to append all
entries after we're done parsing.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
policy: avoid temporary allocations to retain policies

If we add new policies, we currently allocate a temporary list to retain
existing policies in case we fail and have to restore them. This is not
needed. We can just link all the entries on the stack.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
policy: simplify code by using hlist iterators

Lets use hlist_for_each_entry_safe() instead of hard-coding the list. This
safes us code and makes it a lot more readable.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
policy: properly release previous entries

We never release our temporary storage of previous entries. Fix this by
properly releasing them if we succeeded.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
policy: inline kdbus_policy_add_one()

We only use this helper twice, but one time we really don't want the
duplicate-checks. Inline this helper and speed up restore operations by
avoiding the duplicated duplicate-checks.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
policy: don't "break;" in hash_for_each()

hash_for_each() is a nested for-loop, so "break;" does not what you'd
expect. Fix this by jumping out of the loop instead.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>