connection, handle: fix a number of checkpatch warnings

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection, message: cosmetics

Just two tiny cleanups.

Signed-off-by: Daniel Mack <daniel@zonque.org>
test-fd: test send fd operations on connection that do not accept fds

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test-fd: add more passfd+close tests to fdpassing

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
kdbus.h, metadata: factor out capabilities (ABI break)

Move capabilites to its own struct inside kdbus_item. This also gives
us the ability to inform userspace of the highest caps bit the kernel
knows about.

Hence, if the capabilites grow in either the number of sets or the
number of rows, we can now detect and handle both from userspace.

Signed-off-by: Daniel Mack <daniel@zonque.org>
kdbus.h: strip down header

Copyright information in kdbus.h seems overkill, so remove it.

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: improve the connection do not accept fd passing check

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test-fd: check if we did receive the right number of passed fds

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
connection: introduce kdbus_conn_find_reply()

Introduce kdbus_conn_find_reply() and factor out the list walking
code from kdbus_cmd_msg_cancel() and kdbus_conn_check_access().
There will be one more user later.

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: conditional branch cleanups

Use an else branch for better readability.

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: add kdbus_conn_reply.interrupted

Add a new boolean flag to track reply objects affected by interrupted
system calls. Treat such object as non-sync in kdbus_conn_work().

This patch does not add any user that sets this flag yet.

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: rework interrupted system call implementation

Calls that end up in wait_event_interruptible_timeout() are subject
to be interrupted if the userspace task receives a signal. In such
cases, the function will return -ERESTARTSYS, and in case a signal
handler was installed with SA_RESTART, the syscall would be
automatically restarted.

However, in case of KDBUS_CMD_MSG_SEND, however, we have to avoid
sending the same message again in that case, which is why we
decided to return -EINPROGRESS before, and let userspace call into
a special ioctl to catch up on the 2nd half of the SEND syscall.

However, it turns out there's a much simpler solution to that:

 * If a system call is interrupted, we set .interrupted = true in
   the reply tracker object, so it will be cleaned up by the
   connection worker.

 * When KDBUS_CMD_MSG_SEND is calles in a synchronous fashion,
   try to find a reply tracking object in the destination connection.
   If it exists, and is marked as .interrupted == true, catch up on
   it, and go back to wait_event_interruptible_timeout() right away.

That way, we can explicitly support SA_RESTART now, and as timeouts
are absolute, a restarted syscall does the right thing.

Signed-off-by: Daniel Mack <daniel@zonque.org>
test/test-sync: we accept SA_RESTART now

Tweak the test case accordingly.

Signed-off-by: Daniel Mack <daniel@zonque.org>
connection: block only normal fds allwo sending memfds

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
benchmark: cache the kdbus msg and update only memfd item offset if needed

No need to create the kdbus msg every time, and if we are sending a
memfd, then just store its item offset, patch it later with the right
memfd fd+data, seal it, then send it.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
doc: fix a typo kdbus instead of kbus

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
item.c: #include <linux/fs.h>

This should fix tonight's build bot error.

Signed-off-by: Daniel Mack <daniel@zonque.org>
kdbus.h: Use #defines rather than an enum for ioctl definition

Arnd Bergmann writes:

I think in general, using enum is great, but for ioctl command numbers,
we probably want to have defines so the user space implementation can
use #ifdef to see if the kernel version that it is being built for
knows a particular command.

[...]

which might in fact help some tools that try to do automated parsing
of header files to find ioctl commands.

Signed-off-by: Daniel Mack <daniel@zonque.org>
convert *_new() functions to return the new object directly

Al Viro writes:

BTW, the calling conventions for your foo_new() are annoying - instead of
"return -E... or 0, storing the reference to new object in var parameter
passed as the last argument", could you please just return ERR_PTR(-E...)
on error, a pointer to new object on success and to hell with those
struct foo **foo in the argument lists?

Signed-off-by: Daniel Mack <daniel@zonque.org>
handle.c: rework pointer assignment logic

During the review on LKML, Thomas Gleixner stubled over the usage
of our 'p' variable assignment. Apparantly, the idea of assigning
memdup()'ed memory to a void pointer that is automatically freed
at the end of the function wasn't obvious to readers.

Let's fix this, and

a) make kdbus_memdup_user() return void* instead of int, so we can
   directly assign variables to the return value

b) assign the void* variable after the memdup, and call it 'free_ptr'
   to make clearer what's going on.

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