Bus1 Kernel Message Bus
Subsystems (3)
cover Bus1 Kernel Message Bus
Hi
This proposal introduces bus1.ko, a kernel messaging bus. This is not a request for inclusion, yet. It is rather an initial draft and a Request For Comments.
While bus1 emerged out of the kdbus project, bus1 was started from scratch and the concepts have little in common. In a nutshell, bus1 provides a capability-based IPC system, similar in nature to Android Binder, Cap'n Proto, and seL4. The module is completely generic and does neither require nor mandate a user-space counter-part.
o Description
Bus1 is a local IPC system, which provides a decentralized infrastructure to share objects between local peers. The main building blocks are nodes and handles. Nodes represent objects of a local peer, while handles represent descriptors that point to a node. Nodes can be created and destroyed by any peer, and they will always remain owned by their respective creator. Handles on the other hand, are used to refer to nodes and can be passed around with messages as auxiliary data. Whenever a handle is transferred, the receiver will get its own handle allocated, pointing to the same node as the original handle.
Any peer can send messages directed at one of their handles. This will transfer the message to the owner of the node the handle points to. If a peer does not posess a handle to a given node, it will not be able to send a message to that node. That is, handles provide exclusive access management. Anyone that somehow acquired a handle to a node is privileged to further send this handle to other peers. As such, access management is transitive. Once a peer acquired a handle, it cannot be revoked again. However, a node owner can, at anytime, destroy a node. This will effectively unbind all existing handles to that node on any peer, notifying each one of the destruction.
Unlike nodes and handles, peers cannot be addressed directly. In fact, peers are completely disconnected entities. A peer is merely an anchor of a set of nodes and handles, including an incoming message queue for any of those. Whether multiple nodes are all part of the same peer, or part of different peers does not affect the remote view of those. Peers solely exist as management entity and command dispatcher to local processes.
The set of actors on a system is completely decentralized. There is no global component involved that provides a central registry or discovery mechanism. Furthermore, communication between peers only involves those peers, and does not affect any other peer in any way. No global communication lock is taken. However, any communication is still globally ordered, including unicasts, multicasts, and notifications.
o Prior Art
The concepts behind bus1 are almost identical to capability systems like Android Binder, Google Mojo, Cap'n Proto, seL4, and more. Bus1 differs from them by supporting Global Ordering, Multicasts, Resource Accounting, No Global Locking, No Global Context.
While the bus1 UAPI does not expose all features (like soft-references as supported by Binder), the in-kernel code includes support for it. Multiple UAPIs can be supported on top of the in-kernel bus1 code, including support for the Binder UAPI. Efforts on this are still on-going.
o Documentation
The first patch in this series provides the bus1(7) man-page. It explains all concepts in bus1 in more detail. Furthermore, it describes the API that is available on bus1 file descriptors. The pre-compiled man-page is available at:
There is also a great bunch of in-source documentation available. All cross-source-file APIs have KernelDoc annotations. Furthermore, we have an introduction for each subsystem, to be found in the header files. The total number in lines of code for bus1 is roughly ~4.5k. The remaining ~5k LOC are comments and documentation.
o Upstream
The upstream development repository is available on github:
It is an out-of-tree repository that allows easy and fast development of new bus1 features. The in-tree integration repository is available at:
o Conferences
Tom and I will be attending Linux Plumbers Conf next week. Please do not hesitate to contact us there in person. There will also be a presentation
[1] of bus1 on the last day of the conference.
Thanks
Tom & David
[1] https://www.linuxplumbersconf.org/2016/ocw/proposals/3819
Tom Gundersen (14):
bus1: add bus1(7) man-page
bus1: provide stub cdev /dev/bus1
bus1: util - active reference utility library
bus1: util - fixed list utility library
bus1: util - pool utility library
bus1: util - queue utility library
bus1: tracking user contexts
bus1: implement peer management context
bus1: provide transaction context for multicasts
bus1: add handle management
bus1: implement message transmission
bus1: hook up file-operations
bus1: limit and protect resources
bus1: basic user-space kselftests
Documentation/bus1/.gitignore | 2 + Documentation/bus1/Makefile | 41 + Documentation/bus1/bus1.xml | 833 +++++++++++++++++++++ Documentation/bus1/stylesheet.xsl | 16 + include/uapi/linux/bus1.h | 138 ++++ init/Kconfig | 17 + ipc/Makefile | 1 + ipc/bus1/Makefile | 16 + ipc/bus1/handle.c | 823 ++++++++++++++++++++ ipc/bus1/handle.h | 312 ++++++++ ipc/bus1/main.c | 146 ++++ ipc/bus1/main.h | 88 +++ ipc/bus1/message.c | 656 ++++++++++++++++ ipc/bus1/message.h | 171 +++++ ipc/bus1/peer.c | 1163 +++++++++++++++++++++++++++++ ipc/bus1/peer.h | 163 ++++ ipc/bus1/security.h | 45 ++ ipc/bus1/tests.c | 19 + ipc/bus1/tests.h | 32 + ipc/bus1/tx.c | 360 +++++++++ ipc/bus1/tx.h | 102 +++ ipc/bus1/user.c | 628 ++++++++++++++++ ipc/bus1/user.h | 140 ++++ ipc/bus1/util.c | 214 ++++++ ipc/bus1/util.h | 141 ++++ ipc/bus1/util/active.c | 419 +++++++++++ ipc/bus1/util/active.h | 154 ++++ ipc/bus1/util/flist.c | 116 +++ ipc/bus1/util/flist.h | 202 +++++ ipc/bus1/util/pool.c | 572 ++++++++++++++ ipc/bus1/util/pool.h | 164 ++++ ipc/bus1/util/queue.c | 445 +++++++++++ ipc/bus1/util/queue.h | 351 +++++++++ tools/testing/selftests/bus1/.gitignore | 2 + tools/testing/selftests/bus1/Makefile | 19 + tools/testing/selftests/bus1/bus1-ioctl.h | 111 +++ tools/testing/selftests/bus1/test-api.c | 532 +++++++++++++ tools/testing/selftests/bus1/test-io.c | 198 +++++ tools/testing/selftests/bus1/test.h | 114 +++ 39 files changed, 9666 insertions(+) create mode 100644 Documentation/bus1/.gitignore create mode 100644 Documentation/bus1/Makefile create mode 100644 Documentation/bus1/bus1.xml create mode 100644 Documentation/bus1/stylesheet.xsl create mode 100644 include/uapi/linux/bus1.h create mode 100644 ipc/bus1/Makefile create mode 100644 ipc/bus1/handle.c create mode 100644 ipc/bus1/handle.h create mode 100644 ipc/bus1/main.c create mode 100644 ipc/bus1/main.h create mode 100644 ipc/bus1/message.c create mode 100644 ipc/bus1/message.h create mode 100644 ipc/bus1/peer.c create mode 100644 ipc/bus1/peer.h create mode 100644 ipc/bus1/security.h create mode 100644 ipc/bus1/tests.c create mode 100644 ipc/bus1/tests.h create mode 100644 ipc/bus1/tx.c create mode 100644 ipc/bus1/tx.h create mode 100644 ipc/bus1/user.c create mode 100644 ipc/bus1/user.h create mode 100644 ipc/bus1/util.c create mode 100644 ipc/bus1/util.h create mode 100644 ipc/bus1/util/active.c create mode 100644 ipc/bus1/util/active.h create mode 100644 ipc/bus1/util/flist.c create mode 100644 ipc/bus1/util/flist.h create mode 100644 ipc/bus1/util/pool.c create mode 100644 ipc/bus1/util/pool.h create mode 100644 ipc/bus1/util/queue.c create mode 100644 ipc/bus1/util/queue.h create mode 100644 tools/testing/selftests/bus1/.gitignore create mode 100644 tools/testing/selftests/bus1/Makefile create mode 100644 tools/testing/selftests/bus1/bus1-ioctl.h create mode 100644 tools/testing/selftests/bus1/test-api.c create mode 100644 tools/testing/selftests/bus1/test-io.c create mode 100644 tools/testing/selftests/bus1/test.h
Signature · 1 line
2.10.1
RFC 1/14 bus1: add bus1(7) man-page
From: Tom Gundersen <[email protected]>
Add new directory ./Documentation/bus1/ and include DocBook scripts to build the bus1(7) man-page. This documents the bus1 character-device, including all the file-operations you can perform on it.
Furthermore, the man-page also introduces the core bus1 concepts and explains how they work.
Build the bus1-documentation via:
$ make -C Documentation/bus1/ mandocs
$ make -C Documentation/bus1/ htmldocs--- Documentation/bus1/.gitignore | 2 + Documentation/bus1/Makefile | 41 ++ Documentation/bus1/bus1.xml | 833 ++++++++++++++++++++++++++++++++++++++ Documentation/bus1/stylesheet.xsl | 16 + 4 files changed, 892 insertions(+) create mode 100644 Documentation/bus1/.gitignore create mode 100644 Documentation/bus1/Makefile create mode 100644 Documentation/bus1/bus1.xml create mode 100644 Documentation/bus1/stylesheet.xsl
Documentation/bus1/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +*.7 +*.html
Documentation/bus1/Makefile new file +41 -0
41 lines not on this page · raw message
Documentation/bus1/bus1.xml new file +833 -0
833 lines not on this page · raw message
Documentation/bus1/stylesheet.xsl new file +16 -0
@@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="UTF-8"?> +<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0"> + <param name="chunk.quietly">1</param> + <param name="funcsynopsis.style">ansi</param> + <param name="funcsynopsis.tabular.threshold">80</param> + <param name="callout.graphics">0</param> + <param name="paper.type">A4</param> + <param name="generate.section.toc.level">2</param> + <param name="use.id.as.filename">1</param> + <param name="citerefentry.link">1</param> + <strip-space elements="*"/> + <template name="generate.citerefentry.link"> + <value-of select="refentrytitle"/> + <text>.html</text> + </template> +</stylesheet>
Signature · 1 line
2.10.1
RFC 4/14 bus1: util - fixed list utility library
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
This implements a fixed-size list called bus1_flist. The size of the list must be constant over the lifetime of the list. The list can hold one arbitrary pointer per node.
Fixed lists are a combination of a linked list and a static array. That is, fixed lists behave like linked lists (no random access, but arbitrary size), but compare in speed with arrays (consequetive accesses are fast). Unlike fixed arrays, fixed lists can hold huge number of elements without requiring vmalloc, but solely relying on small-size kmalloc allocations.
Internally, fixed lists are a singly-linked list of static arrays. This guarantees that iterations behave almost like on an array, except when crossing a batch-border.
Fixed lists can replace fixed-size arrays whenever you need to support large number of elements, but don't need random access. Fixed lists have ALMOST the same memory requirements as fixed-size arrays, except one pointer of state per 'BUS1_FLIST_BATCH' elements. If only a small size (i.e., it only requires one batch) is stored in a fixed list, then its memory requirements and iteration time are equivalent to fixed-size arrays.
Fixed lists will be required by the upcoming bus1 message-transactions. They must support large auxiliary data transfers, in case users want to send their entire handle state via the bus.
--- ipc/bus1/Makefile | 3 +- ipc/bus1/util/flist.c | 116 +++++++++++++++++++++++++++++ ipc/bus1/util/flist.h | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 320 insertions(+), 1 deletion(-) create mode 100644 ipc/bus1/util/flist.c create mode 100644 ipc/bus1/util/flist.h
ipc/bus1/Makefile +2 -1
@@ -1,6 +1,7 @@ bus1-y := \ main.o \ - util/active.o + util/active.o \ + util/flist.o obj-$(CONFIG_BUS1) += bus1.o
ipc/bus1/util/flist.c new file +116 -0
116 lines not on this page · raw message
ipc/bus1/util/flist.h new file +202 -0
202 lines not on this page · raw message
Signature · 1 line
2.10.1
RFC 7/14 bus1: tracking user contexts
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
Different users can communicate via bus1, and many resources are shared between multiple users. The bus1_user object represents the UID of a user, like "struct user_struct" does in the kernel core. It is used to account global resources, apply limits, and calculate quotas if different UIDs communicate with each other.
All dynamic resources have global per-user limits, which cannot be exceeded by a user. They prevent a single user from exhausting local resources. Each peer that is created is always owned by the user that initialized it. All resources allocated on that peer are accounted on that pinned user. Additionally to global resources, there are local limits per peer, that can be controlled by each peer individually (e.g., specifying a maximum pool size). Those local limits allow a user to distribute the globally available resources across its peer instances.
Since bus1 allows communication across UID boundaries, any such transmission of resources must be properly accounted. Bus1 employs dynamic quotas to fairly distribute available resources. Those quotas make sure that available resources of a peer cannot be exhausted by remote UIDs, but are fairly divided among all communicating peers.
This only implements the user tracking, the resource limits will be added in follow-up patches.
--- ipc/bus1/Makefile | 1 + ipc/bus1/main.c | 3 ++ ipc/bus1/user.c | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ipc/bus1/user.h | 67 ++++++++++++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 ipc/bus1/user.c create mode 100644 ipc/bus1/user.h
ipc/bus1/Makefile +1 -0
@@ -1,5 +1,6 @@ bus1-y := \ main.o \ + user.o \ util/active.o \ util/flist.o \ util/pool.o \
ipc/bus1/main.c +3 -0
@@ -16,6 +16,7 @@#include <linux/module.h> #include "main.h" #include "tests.h" +#include "user.h" static int bus1_fop_open (struct inode *inode,struct file *file) {
@@ -64,6 +65,7 @@ static int __init bus1_modinit(void) error:debugfs_remove (bus1_debugdir); +bus1_user_modexit ();return r; }
@@ -71,6 +73,7 @@ static void __exit bus1_modexit(void) {misc_deregister (&bus1_misc);debugfs_remove (bus1_debugdir); +bus1_user_modexit ();pr_info ("unloaded\n" ); }
ipc/bus1/user.c new file +153 -0
153 lines not on this page · raw message
ipc/bus1/user.h new file +67 -0
67 lines not on this page · raw message
Signature · 1 line
2.10.1
RFC 6/14 bus1: util - queue utility library
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
(Please refer to 'Lamport Timestamps', the concept of
'happened-before', and 'causal ordering'. The queue implementation has its roots in Lamport Timestamps, treating a set of local CPUs as a distributed system to avoid any global synchronization.)
A bus1 message queue is a FIFO, i.e., messages are linearly ordered by the time they were sent. Moreover, atomic delivery of messages to multiple queues are supported, without any global synchronization, i.e., the order of message delivery is consistent across queues.
Messages can be destined for multiple queues, hence, we need to be careful that all queues get a consistent order of incoming messages. We define the concept of `global order' to provide a basic set of guarantees. This global order is a partial order on the set of all messages. The order is defined as:
1) If a message B was queued *after* a message A, then: A < B
2) If a message B was queued *after* a message A was dequeued,
then: A < B
3) If a message B was dequeued *after* a message A on the same queue,
then: A < B
(Note: Causality is honored. after' and before' do not refer to
the same task, nor the same queue, but rather any kind of synchronization between the two operations.)
The queue object implements this global order in a lockless fashion. It solely relies on a distributed clock on each queue. Each message to be sent causes a clock tick on the local clock and on all destination clocks. Furthermore, all clocks are synchronized, meaning they're fast-forwarded in case they're behind the highest of all participating peers. No global state tracking is involved.
During a message transaction, we first queue a message as 'staging' entry in each destination with a preliminary timestamp. This timestamp is explicitly odd numbered. Any odd numbered timestamp is considered 'staging' and causes *any* message ordered after it to be blocked until it is no longer staging. This allows us to queue the message in parallel with any racing multicast, and be guaranteed that all possible conflicts are blocked until we eventually commit a transaction. To commit a transaction (after all staging entries are queued), we choose the highest timestamp we have seen across all destinations and re-queue all our entries on each peer using that timestamp. Here we use a commit timestamp (even numbered).
With this in mind, we define that a client can only dequeue messages from its queue that have an even timestamp. Furthermore, if there is a message queued with an odd timestamp that is lower than the even timestamp of another message, then neither message can be dequeued. They're considered to be in-flight conflicts. This guarantees that two concurrent multicast messages can be queued without any *global* locks, but either can only be dequeued by a peer if their ordering has been established (via commit timestamps).
NOTE: A fully committed message is not guaranteed to be ready to be
dequeued as it may be blocked by a staging entry. This means that there is an arbitrary (though bounded) time from a message transaction completing when the queue may still appear to be empty. In other words, message transmission is not instantaneous. It would be possible to change this at the cost of shortly blocking each message transaction on all other conflicting tasks.
The queue implementation uses an rb-tree (ordered by timestamps and sender), with a cached pointer to the front of the queue. It will be embedded in every peer participating on the bus1 kernel message bus1.
--- ipc/bus1/Makefile | 3 +- ipc/bus1/util/queue.c | 445 ++++++++++++++++++++++++++++++++++++++++++++++++++ ipc/bus1/util/queue.h | 351 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 798 insertions(+), 1 deletion(-) create mode 100644 ipc/bus1/util/queue.c create mode 100644 ipc/bus1/util/queue.h
ipc/bus1/Makefile +2 -1
@@ -2,7 +2,8 @@ bus1-y := \ main.o \ util/active.o \ util/flist.o \ - util/pool.o + util/pool.o \ + util/queue.o obj-$(CONFIG_BUS1) += bus1.o
ipc/bus1/util/queue.c new file +445 -0
445 lines not on this page · raw message
ipc/bus1/util/queue.h new file +351 -0
351 lines not on this page · raw message
Signature · 1 line
2.10.1
RFC 5/14 bus1: util - pool utility library
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
A bus1-pool is a shmem-backed memory pool shared between userspace and the kernel. The pool is used to transfer memory from the kernel to userspace without requiring userspace to pre-allocate space.
The pool is managed in slices, which are published to userspace when they are ready to be read and must be released by userspace when userspace is done with them.
Userspace has read-only access to its pools and the kernel has read-write access, but published slices are not altered.
This pool implementation will be used by bus1 message transactions to support single-copy data transfers, directly from the sender's address space into the pool of the destination peer. The allocation algorithm is based on the Android Binder code and has served their needs well for many years now.
--- ipc/bus1/Makefile | 3 +- ipc/bus1/util/pool.c | 572 +++++++++++++++++++++++++++++++++++++++++++++++++++ ipc/bus1/util/pool.h | 164 +++++++++++++++ 3 files changed, 738 insertions(+), 1 deletion(-) create mode 100644 ipc/bus1/util/pool.c create mode 100644 ipc/bus1/util/pool.h
ipc/bus1/Makefile +2 -1
@@ -1,7 +1,8 @@ bus1-y := \ main.o \ util/active.o \ - util/flist.o + util/flist.o \ + util/pool.o obj-$(CONFIG_BUS1) += bus1.o
ipc/bus1/util/pool.c new file +572 -0
572 lines not on this page · raw message
ipc/bus1/util/pool.h new file +164 -0
164 lines not on this page · raw message
Signature · 1 line
2.10.1
RFC 12/14 bus1: hook up file-operations
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
This hooks up all the file-operations on a bus1-file-descriptor. It implements the ioctls as defined in the UAPI, as well as mmap() and poll() support.
--- ipc/bus1/main.c | 46 +++ ipc/bus1/peer.c | 934 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- ipc/bus1/peer.h | 8 + 3 files changed, 987 insertions(+), 1 deletion(-)
ipc/bus1/main.c +46 -0
@@ -14,10 +14,17 @@#include <linux/init.h> #include <linux/miscdevice.h> #include <linux/module.h> +#include <linux/poll.h> +#include <linux/seq_file.h> +#include <linux/uio.h> +#include <uapi/linux/bus1.h> #include "main.h" #include "peer.h" #include "tests.h" #include "user.h" +#include "util/active.h" +#include "util/pool.h" +#include "util/queue.h" static int bus1_fop_open (struct inode *inode,struct file *file) {
@@ -37,6 +44,41 @@ static int bus1_fop_release(struct inode *inode, struct file *file)return 0 ; } +static unsigned int bus1_fop_poll (struct file *file, +struct poll_table_struct *wait) +{ +struct bus1_peer *peer = file->private_data; +unsigned int mask; + +poll_wait (file, &peer->waitq, wait); + +/* access queue->front unlocked */ +rcu_read_lock (); +if (bus1_active_is_deactivated (&peer->active)) { + mask =POLLHUP ; + }else { + mask =POLLOUT |POLLWRNORM ; +if (bus1_queue_is_readable_rcu (&peer->data.queue)) + mask |=POLLIN |POLLRDNORM ; + } +rcu_read_unlock (); + +return mask; +} + +static int bus1_fop_mmap (struct file *file,struct vm_area_struct *vma) +{ +struct bus1_peer *peer = file->private_data; +int r; + +if (!bus1_peer_acquire (peer)) +return -ESHUTDOWN ; + + r =bus1_pool_mmap (&peer->data.pool, vma); +bus1_peer_release (peer); +return r; +} +static void bus1_fop_show_fdinfo (struct seq_file *m,struct file *file) {struct bus1_peer *peer = file->private_data;
@@ -48,7 +90,11 @@ const struct file_operations bus1_fops = { .owner = THIS_MODULE, .open = bus1_fop_open, .release = bus1_fop_release, + .poll = bus1_fop_poll, .llseek = noop_llseek, + .mmap = bus1_fop_mmap, + .unlocked_ioctl = bus1_peer_ioctl, + .compat_ioctl = bus1_peer_ioctl, .show_fdinfo = bus1_fop_show_fdinfo, };
ipc/bus1/peer.c +933 -1
981 lines not on this page · raw message
ipc/bus1/peer.h +8 -0
@@ -52,11 +52,13 @@#include <linux/rcupdate.h> #include <linux/rbtree.h> #include <linux/wait.h> +#include <uapi/linux/bus1.h> #include "user.h" #include "util/active.h" #include "util/pool.h" #include "util/queue.h" +struct bus1_message ;struct cred ;struct dentry ;struct pid_namespace ;
@@ -73,8 +75,12 @@ struct pid_namespace;* @active: active references * @debugdir: debugfs root of this peer, or NULL/ERR_PTR * @data.lock: data lock +* @data.pool: data pool * @data.queue: message queue * @local.lock: local peer runtime lock +* @local.seed: pinned seed message +* @local.map_handles: map of owned handles (by handle ID) +* @local.handle_ids: handle ID allocator */ struct bus1_peer {u64 id;
@@ -95,6 +101,7 @@ struct bus1_peer {struct {struct mutex lock; +struct bus1_message *seed;struct rb_root map_handles;u64 handle_ids; } local;
@@ -102,6 +109,7 @@ struct bus1_peer {struct bus1_peer *bus1_peer_new (void );struct bus1_peer *bus1_peer_free (struct bus1_peer *peer); +long bus1_peer_ioctl (struct file *file,unsigned int cmd,unsigned long arg);/** * bus1_peer_acquire() - acquire active reference to peer
Signature · 1 line
2.10.1
RFC 14/14 bus1: basic user-space kselftests
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
This adds kselftests integration and provides some basic API tests for bus1.
--- tools/testing/selftests/bus1/.gitignore | 2 + tools/testing/selftests/bus1/Makefile | 19 ++ tools/testing/selftests/bus1/bus1-ioctl.h | 111 +++++++ tools/testing/selftests/bus1/test-api.c | 532 ++++++++++++++++++++++++++++++ tools/testing/selftests/bus1/test-io.c | 198 +++++++++++ tools/testing/selftests/bus1/test.h | 114 +++++++ 6 files changed, 976 insertions(+) create mode 100644 tools/testing/selftests/bus1/.gitignore create mode 100644 tools/testing/selftests/bus1/Makefile create mode 100644 tools/testing/selftests/bus1/bus1-ioctl.h create mode 100644 tools/testing/selftests/bus1/test-api.c create mode 100644 tools/testing/selftests/bus1/test-io.c create mode 100644 tools/testing/selftests/bus1/test.h
tools/testing/selftests/bus1/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +test-api +test-io
tools/testing/selftests/bus1/Makefile new file +19 -0
@@ -0,0 +1,19 @@ +# Makefile for bus1 selftests + +CC = $(CROSS_COMPILE)gcc +CFLAGS += -D_FILE_OFFSET_BITS=64 -Wall -g -O2 +CFLAGS += -I../../../../include/uapi/ +CFLAGS += -I../../../../include/ +CFLAGS += -I../../../../usr/include/ + +TEST_PROGS := test-api test-io + +all: $(TEST_PROGS) + +%: %.c bus1-ioctl.h test.h ../../../../usr/include/linux/bus1.h + $(CC) $(CFLAGS) $< -o $@ + +include ../lib.mk + +clean: + $(RM) $(TEST_PROGS)
tools/testing/selftests/bus1/bus1-ioctl.h new file +111 -0
111 lines not on this page · raw message
tools/testing/selftests/bus1/test-api.c new file +532 -0
532 lines not on this page · raw message
tools/testing/selftests/bus1/test-io.c new file +198 -0
198 lines not on this page · raw message
tools/testing/selftests/bus1/test.h new file +114 -0
114 lines not on this page · raw message
Signature · 1 line
2.10.1
RFC 13/14 bus1: limit and protect resources
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
This adds resource-counters to peers and users. They limit the number of objects that a peer can operate on. The root limits per user can be configured as a module option. Everything else is just based on them.
This also adds LSM hooks. They are not integrated with ./security/, yet, but just provide the hooks as discussed with the SELinux maintainers. Since the operations on bus1 are very similar in nature to Binder, the hooks are the same as well (since the LSM people seemed pretty happy with the Binder hooks, anyway).
--- ipc/bus1/message.c | 47 +++++- ipc/bus1/peer.c | 95 ++++++++++- ipc/bus1/peer.h | 2 + ipc/bus1/security.h | 45 +++++ ipc/bus1/user.c | 475 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ipc/bus1/user.h | 75 ++++++++- 6 files changed, 728 insertions(+), 11 deletions(-) create mode 100644 ipc/bus1/security.h
ipc/bus1/message.c +45 -2
@@ -26,6 +26,7 @@#include "handle.h" #include "message.h" #include "peer.h" +#include "security.h" #include "tx.h" #include "user.h" #include "util.h"
@@ -242,9 +243,16 @@ int bus1_factory_seal(struct bus1_factory *f)struct bus1_handle *h;struct bus1_flist *e;size_t i; +int r;lockdep_assert_held (&f->peer->local.lock); + r =bus1_user_charge (&f->peer->user->limits.n_handles, + &f->peer->data.limits.n_handles, + f->n_handles_charge); +if (r <0 ) +return r; +for (i =0 , e = f->handles; i < f->n_handles; e =bus1_flist_next (e, &i)) {
@@ -291,11 +299,29 @@ struct bus1_message *bus1_factory_instantiate(struct bus1_factory *f, transmit_secctx = f->has_secctx && (READ_ONCE (peer->flags) &BUS1_PEER_FLAG_WANT_SECCTX ); + r =bus1_user_charge (&peer->user->limits.n_slices, + &peer->data.limits.n_slices,1 ); +if (r <0 ) +return ERR_PTR (r); + + r =bus1_user_charge (&peer->user->limits.n_handles, + &peer->data.limits.n_handles, f->n_handles); +if (r <0 ) { +bus1_user_discharge (&peer->user->limits.n_slices, + &peer->data.limits.n_slices,1 ); +return ERR_PTR (r); + } + size =sizeof (*m) +bus1_flist_inline_size (f->n_handles) + f->n_files *sizeof (struct file *); m =kmalloc (size,GFP_KERNEL ); -if (!m) +if (!m) { +bus1_user_discharge (&peer->user->limits.n_handles, + &peer->data.limits.n_handles, f->n_handles); +bus1_user_discharge (&peer->user->limits.n_slices, + &peer->data.limits.n_slices,1 );return ERR_PTR (-ENOMEM ); + }/* set to default first, so the destructor can be called anytime */ kref_init (&m->ref);
@@ -329,6 +355,8 @@ struct bus1_message *bus1_factory_instantiate(struct bus1_factory *f, m->slice =bus1_pool_alloc (&peer->data.pool, size);mutex_unlock (&peer->data.lock);if (IS_ERR (m->slice)) { +bus1_user_discharge (&peer->user->limits.n_slices, + &peer->data.limits.n_slices,1 ); r =PTR_ERR (m->slice); m->slice =NULL ; goto error;
@@ -376,6 +404,11 @@ struct bus1_message *bus1_factory_instantiate(struct bus1_factory *f,/* import files */ while (m->n_files < f->n_files) { + r =security_bus1_transfer_file (f->peer, peer, + f->files[m->n_files]); +if (r <0 ) + goto error; + m->files[m->n_files] =get_file (f->files[m->n_files]); ++m->n_files; }
@@ -436,10 +469,15 @@ void bus1_message_free(struct kref *k)bus1_handle_unref (e->ptr); } } +bus1_user_discharge (&peer->user->limits.n_handles, + &peer->data.limits.n_handles, m->n_handles_charge);bus1_flist_deinit (m->handles, m->n_handles);if (m->slice) {mutex_lock (&peer->data.lock); +if (!bus1_pool_slice_is_public (m->slice)) +bus1_user_discharge (&peer->user->limits.n_slices, + &peer->data.limits.n_slices,1 );bus1_pool_release_kernel (&peer->data.pool, m->slice);mutex_unlock (&peer->data.lock); }
@@ -575,7 +613,12 @@ int bus1_message_install(struct bus1_message *m, struct bus1_cmd_recv *param) }/* charge resources */ -if (!peek) { +if (peek) { + r =bus1_user_charge (&peer->user->limits.n_handles, + &peer->data.limits.n_handles, n_handles); +if (r <0 ) + goto exit; + }else {WARN_ON (n_handles < m->n_handles_charge); m->n_handles_charge -= n_handles; }
ipc/bus1/peer.c +87 -8
180 lines not on this page · raw message
ipc/bus1/peer.h +2 -0
@@ -77,6 +77,7 @@ struct pid_namespace;* @data.lock: data lock * @data.pool: data pool * @data.queue: message queue +* @data.limits: resource limit counter * @local.lock: local peer runtime lock * @local.seed: pinned seed message * @local.map_handles: map of owned handles (by handle ID)
@@ -97,6 +98,7 @@ struct bus1_peer {struct mutex lock;struct bus1_pool pool;struct bus1_queue queue; +struct bus1_user_limits limits; } data;struct {
ipc/bus1/security.h new file +45 -0
45 lines not on this page · raw message
ipc/bus1/user.c +475 -0
502 lines not on this page · raw message
ipc/bus1/user.h +74 -1
100 lines not on this page · raw message
Signature · 1 line
2.10.1
RFC 11/14 bus1: implement message transmission
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
While notifications already work and simply require linking bus1_handle objects into the destination queue, real messages require proper payloads. This implements two core objects: Message objects and factories.
The message factory is similar to transaction contexts, and lives completely on the stack. It is used to import the parameters given by user-space in a SEND ioctl. It parses and validates them. With this message factors we can now instantiate many messages, one for each destination of a multicast.
Messages need to carry a bunch of data, mainly:
- metadata: This just matches what Unix-sockets do (uid, gid, pid, tid, and secctx)
- payload: Random memory passed in as iovec-array by user-space
- files: Set of file-descriptors, very similar to SCM_RIGHTS
- handles: Set of local handles to transfer to the destination
--- ipc/bus1/Makefile | 1 + ipc/bus1/message.c | 613 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ipc/bus1/message.h | 171 +++++++++++++++ ipc/bus1/peer.c | 2 + ipc/bus1/peer.h | 2 + ipc/bus1/util.c | 162 ++++++++++++++ ipc/bus1/util.h | 7 + 7 files changed, 958 insertions(+) create mode 100644 ipc/bus1/message.c create mode 100644 ipc/bus1/message.h
ipc/bus1/Makefile +1 -0
@@ -1,6 +1,7 @@ bus1-y := \ handle.o \ main.o \ + message.o \ peer.o \ tx.o \ user.o \
ipc/bus1/message.c new file +613 -0
613 lines not on this page · raw message
ipc/bus1/message.h new file +171 -0
171 lines not on this page · raw message
ipc/bus1/peer.c +2 -0
@@ -70,6 +70,7 @@ struct bus1_peer *bus1_peer_new(void)/* initialize data section */ mutex_init (&peer->data.lock); + peer->data.pool =BUS1_POOL_NULL ;bus1_queue_init (&peer->data.queue);/* initialize peer-private section */
@@ -136,6 +137,7 @@ struct bus1_peer *bus1_peer_free(struct bus1_peer *peer)/* deinitialize data section */ bus1_queue_deinit (&peer->data.queue); +bus1_pool_deinit (&peer->data.pool);mutex_destroy (&peer->data.lock);/* deinitialize constant fields */
ipc/bus1/peer.h +2 -0
@@ -54,6 +54,7 @@#include <linux/wait.h> #include "user.h" #include "util/active.h" +#include "util/pool.h" #include "util/queue.h" struct cred ;
@@ -88,6 +89,7 @@ struct bus1_peer {struct {struct mutex lock; +struct bus1_pool pool;struct bus1_queue queue; } data;
ipc/bus1/util.c +162 -0
174 lines not on this page · raw message
ipc/bus1/util.h +7 -0
@@ -26,6 +26,7 @@#include <linux/types.h> struct dentry ; +struct iovec ;/** * BUS1_TAIL - tail pointer in singly-linked lists
@@ -37,6 +38,12 @@ struct dentry;*/ #define BUS1_TAIL ERR_PTR(-1) +int bus1_import_vecs (struct iovec *out_vecs, +size_t *out_length, +const void __user *vecs, +size_t n_vecs); +struct file *bus1_import_fd (int fd); +#if defined(CONFIG_DEBUG_FS ) struct dentry *
Signature · 1 line
2.10.1
RFC 10/14 bus1: add handle management
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
The object system on a bus is based on 'nodes' and 'handles'. Any peer can allocate new, local objects at any time. The creator automatically becomes the sole owner of the object. References to objects can be passed as payload of messages. The recipient will then gain their own reference to the object as well. Additionally, an object can be the destination of a message, in which case the message is always sent to the original creator (and thus the owner) of the object.
Internally, objects are called 'nodes'. A reference to an object is a 'handle'. Whenever a new node is created, the owner implicitly gains an handle as well. In fact, handles are the only way to refer to a node. The node itself is entirely hidden in the implementation, and visible in the API as an “anchor handle”.
Whenever a handle is passed as payload of a message, the target peer will gain a handle linked to the same underlying node. This works regardless of whether the sender is the owner of the underlying node, or not.
Each peer can identify all its handles (both owned and un-owned) by a 64-bit integer. The namespace is local to each peer, and the numbers cannot be compared with the numbers of other peers (in fact, they are very likely to clash, but might still have *different* underlying nodes). However, if a peer receives a reference to the same node multiple times, the resulting handle will be the same. The kernel keeps count of how often each peer owns a handle.
If a peer no longer requires a specific handle, it can release it. If the peer releases its last reference to a handle, the handle will be destroyed.
The owner of a node (and *only* the owner) can trigger the destruction of a node (even if other peers still own handles to it). In this case, all peers that own a handle are notified of this fact. Once all handles to a specific node have been released (except for the handle internally pinned in the node itself), the owner of the node is notified of this, so it can potentially destroy both any linked state and the node itself.
Node destruction is fully synchronized with any transaction. That is, a node and all its handles are valid in every message that is transmitted *before* the notification of its destruction. Furthermore, no message after this notification will carry the ID of such a destroyed node. Note that message transactions are asynchronous. That is, there is no unique point in time that a message is synchronized with another message. Hence, whether a specific handle passed with a message is still valid or not, cannot be predicted by the sender, but only by one of the receivers.
--- ipc/bus1/Makefile | 1 + ipc/bus1/handle.c | 823 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ipc/bus1/handle.h | 312 +++++++++++++++++++++ ipc/bus1/peer.c | 3 + ipc/bus1/peer.h | 2 + ipc/bus1/util.h | 83 ++++++ 6 files changed, 1224 insertions(+) create mode 100644 ipc/bus1/handle.c create mode 100644 ipc/bus1/handle.h
ipc/bus1/Makefile +1 -0
@@ -1,4 +1,5 @@ bus1-y := \ + handle.o \ main.o \ peer.o \ tx.o \
ipc/bus1/handle.c new file +823 -0
823 lines not on this page · raw message
ipc/bus1/handle.h new file +312 -0
312 lines not on this page · raw message
ipc/bus1/peer.c +3 -0
@@ -74,6 +74,8 @@ struct bus1_peer *bus1_peer_new(void)/* initialize peer-private section */ mutex_init (&peer->local.lock); + peer->local.map_handles =RB_ROOT ; + peer->local.handle_ids =0 ;if (!IS_ERR_OR_NULL (bus1_debugdir)) {char idstr[22 ];
@@ -129,6 +131,7 @@ struct bus1_peer *bus1_peer_free(struct bus1_peer *peer)bus1_peer_disconnect (peer);/* deinitialize peer-private section */ +WARN_ON (!RB_EMPTY_ROOT (&peer->local.map_handles));mutex_destroy (&peer->local.lock);/* deinitialize data section */
ipc/bus1/peer.h +2 -0
@@ -93,6 +93,8 @@ struct bus1_peer {struct {struct mutex lock; +struct rb_root map_handles; +u64 handle_ids; } local; };
ipc/bus1/util.h +83 -0
93 lines not on this page · raw message
Signature · 1 line
2.10.1
RFC 9/14 bus1: provide transaction context for multicasts
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
The transaction engine is an object that lives on the stack and is used to stage and commit multicasts properly. Unlike unicasts, a multicast cannot just be queued on each destination, but must be properly synchronized. This requires us to first stage each message on their respective destination, then sync and tick the clocks, and eventual commit all messages.
The transaction context implements this logic for both, unicasts and multicasts. It hides the timestamp handling and takes care to properly synchronize accesses to the peer queues.
--- ipc/bus1/Makefile | 1 + ipc/bus1/peer.c | 2 + ipc/bus1/peer.h | 3 + ipc/bus1/tx.c | 360 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ipc/bus1/tx.h | 102 ++++++++++++++++ 5 files changed, 468 insertions(+) create mode 100644 ipc/bus1/tx.c create mode 100644 ipc/bus1/tx.h
ipc/bus1/Makefile +1 -0
@@ -1,6 +1,7 @@ bus1-y := \ main.o \ peer.o \ + tx.o \ user.o \ util.o \ util/active.o \
ipc/bus1/peer.c +2 -0
@@ -70,6 +70,7 @@ struct bus1_peer *bus1_peer_new(void)/* initialize data section */ mutex_init (&peer->data.lock); +bus1_queue_init (&peer->data.queue);/* initialize peer-private section */ mutex_init (&peer->local.lock);
@@ -131,6 +132,7 @@ struct bus1_peer *bus1_peer_free(struct bus1_peer *peer)mutex_destroy (&peer->local.lock);/* deinitialize data section */ +bus1_queue_deinit (&peer->data.queue);mutex_destroy (&peer->data.lock);/* deinitialize constant fields */
ipc/bus1/peer.h +3 -0
@@ -54,6 +54,7 @@#include <linux/wait.h> #include "user.h" #include "util/active.h" +#include "util/queue.h" struct cred ;struct dentry ;
@@ -71,6 +72,7 @@ struct pid_namespace;* @active: active references * @debugdir: debugfs root of this peer, or NULL/ERR_PTR * @data.lock: data lock +* @data.queue: message queue * @local.lock: local peer runtime lock */ struct bus1_peer {
@@ -86,6 +88,7 @@ struct bus1_peer {struct {struct mutex lock; +struct bus1_queue queue; } data;struct {
ipc/bus1/tx.c new file +360 -0
360 lines not on this page · raw message
ipc/bus1/tx.h new file +102 -0
102 lines not on this page · raw message
Signature · 1 line
2.10.1
RFC 8/14 bus1: implement peer management context
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
A peer context provides access to the bus1 system. A peer itself is not a routable entity, but rather only a local anchor to serve as gateway to the bus. To participate on the bus, you need to allocate a peer. This peer manages all your state on the bus, including all allocated nodes, owned handles, incoming messages, and more.
A peer is split into 3 sections:
- A static section that is initialized at peer creation and never changes
- A peer-local section that is only ever accessed by ioctls done by the peer itself.
- A data section that might be accessed by remote peers when interacting with this peer.
All peers on the system operate on the same level. There is no context a peer is linked into. Hence, you can never lock multiple peers at the same time. Instead, peers provide active-references. Before performing an operation on a peer, an active reference must be acquired, and hold as long as the operation goes on. When done, the reference is released again. When a peer is disconnected, no more active references can be acquired, and any outstanding operation is waited for before the peer is destroyed.
Additionally to active-references, there are 2 locks: A peer-local lock and a data lock. The peer-local lock is used to synchronize operations done by the peer itself. It is never acquired by a remote peer. The data lock protects the data of the peer, which might be modified by remote peers. The data lock nests underneath the local-lock. Furthermore, the data-lock critical sections must be kept small and never block indefinitely. Remote peers might wait for data-locks, hence they must rely on not being DoSed. The local peer lock, however, is private to the peer itself. Not such restrictions apply. It is mostly used to give the impression of atomic operations (i.e., making the API appear consistent and coherent).
This only adds the peer context, the ioctls will be implemented in follow-up patches.
--- ipc/bus1/Makefile | 2 + ipc/bus1/main.c | 17 +++++++ ipc/bus1/main.h | 14 ++++++ ipc/bus1/peer.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ipc/bus1/peer.h | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ipc/bus1/util.c | 52 +++++++++++++++++++ ipc/bus1/util.h | 51 +++++++++++++++++++ 7 files changed, 427 insertions(+) create mode 100644 ipc/bus1/peer.c create mode 100644 ipc/bus1/peer.h create mode 100644 ipc/bus1/util.c create mode 100644 ipc/bus1/util.h
ipc/bus1/Makefile +2 -0
@@ -1,6 +1,8 @@ bus1-y := \ main.o \ + peer.o \ user.o \ + util.o \ util/active.o \ util/flist.o \ util/pool.o \
ipc/bus1/main.c +17 -0
@@ -15,24 +15,41 @@#include <linux/miscdevice.h> #include <linux/module.h> #include "main.h" +#include "peer.h" #include "tests.h" #include "user.h" static int bus1_fop_open (struct inode *inode,struct file *file) { +struct bus1_peer *peer; + + peer =bus1_peer_new (); +if (IS_ERR (peer)) +return PTR_ERR (peer); + + file->private_data = peer;return 0 ; }static int bus1_fop_release (struct inode *inode,struct file *file) { +bus1_peer_free (file->private_data);return 0 ; } +static void bus1_fop_show_fdinfo (struct seq_file *m,struct file *file) +{ +struct bus1_peer *peer = file->private_data; + +seq_printf (m,KBUILD_MODNAME "-peer:\t%16llx\n" , peer->id); +} +const struct file_operations bus1_fops = { .owner =THIS_MODULE , .open = bus1_fop_open, .release = bus1_fop_release, .llseek = noop_llseek, + .show_fdinfo = bus1_fop_show_fdinfo, };static struct miscdevice bus1_misc = {
ipc/bus1/main.h +14 -0
@@ -49,6 +49,20 @@* ordered, including unicasts, multicasts, and notifications. */ +/** +* Locking +* +* Most of the bus1 objects form a hierarchy, as such, their locks must be +* ordered. Not all orders are explicitly defined (e.g., they might define +* orthogonal hierarchies), but this list gives a rough overview: +* +* bus1_peer.active +* bus1_peer.local.lock +* bus1_peer.data.lock +* bus1_user.lock +* bus1_user_lock +*/ +struct dentry ;struct file_operations ;
ipc/bus1/peer.c new file +145 -0
145 lines not on this page · raw message
ipc/bus1/peer.h new file +146 -0
146 lines not on this page · raw message
ipc/bus1/util.c new file +52 -0
52 lines not on this page · raw message
ipc/bus1/util.h new file +51 -0
51 lines not on this page · raw message
Signature · 1 line
2.10.1
RFC 3/14 bus1: util - active reference utility library
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
The bus1_active object implements active references. They work similarly to plain object reference counters, but allow disabling any new references from being taken.
Each bus1_active object goes through a set of states:
NEW: Initial state, no active references can be acquired
ACTIVE: Live state, active references can be acquired
DRAINING: Deactivated but lingering, no active references
can be acquired
DRAINED: Deactivated and all active references were dropped
RELEASED: Fully drained and synchronously released
Initially, all bus1_active objects are in state NEW. As soon as they're activated, they enter ACTIVE and active references can be acquired. This is the normal, live state. Once the object is deactivated, it enters state DRAINING. No new active references can be acquired, but some threads might still own active references. Once all those are dropped, the object enters state DRAINED. Now the object can be released a *single* time, before it enters state RELEASED and is finished. It cannot be re-used anymore.
Active-references are very useful to track threads that invoke callbacks on an object. As long as a callback is running, an active reference is held, and as such the object is usually protected from being destroyed. The destructor of the object needs to deactivate *and* drain the object, before releasing resources.
Active references will be used heavy by the upcoming bus1_peer object. Whenever a peer operates on a remote peer, it must acquire and hold an active reference on that remote peer. This guarantees that the remote peer will wait for this operation to finish before possibly disconnecting from the bus.
In concept, active-references can be seen as rw-locks. However, they have much more strict state-transitions. Prior art can be seen in super-blocks ('atomic_t s_active'), and kernfs ('atomic_t active').
--- ipc/bus1/Makefile | 3 +- ipc/bus1/util/active.c | 419 +++++++++++++++++++++++++++++++++++++++++++++++++ ipc/bus1/util/active.h | 154 ++++++++++++++++++ 3 files changed, 575 insertions(+), 1 deletion(-) create mode 100644 ipc/bus1/util/active.c create mode 100644 ipc/bus1/util/active.h
ipc/bus1/Makefile +2 -1
@@ -1,5 +1,6 @@ bus1-y := \ - main.o + main.o \ + util/active.o obj-$(CONFIG_BUS1) += bus1.o
ipc/bus1/util/active.c new file +419 -0
419 lines not on this page · raw message
ipc/bus1/util/active.h new file +154 -0
154 lines not on this page · raw message
Signature · 1 line
2.10.1
RFC 2/14 bus1: provide stub cdev /dev/bus1
In reply to David HerrmannFrom: Tom Gundersen <[email protected]>
Add the CONFIG_BUS1 option to enable the bus1 kernel messaging bus. If enabled, provide the bus1.ko module with a stub cdev /dev/bus1. So far it does not expose any API, but the full intended uapi is provided in include/uapi/linux/bus1.h already.
--- include/uapi/linux/bus1.h | 138 ++++++++++++++++++++++++++++++++++++++++++++++ init/Kconfig | 17 ++++++ ipc/Makefile | 1 + ipc/bus1/Makefile | 6 ++ ipc/bus1/main.c | 80 +++++++++++++++++++++++++++ ipc/bus1/main.h | 74 +++++++++++++++++++++++++ ipc/bus1/tests.c | 19 +++++++ ipc/bus1/tests.h | 32 +++++++++++ 8 files changed, 367 insertions(+) create mode 100644 include/uapi/linux/bus1.h create mode 100644 ipc/bus1/Makefile create mode 100644 ipc/bus1/main.c create mode 100644 ipc/bus1/main.h create mode 100644 ipc/bus1/tests.c create mode 100644 ipc/bus1/tests.h
include/uapi/linux/bus1.h new file +138 -0
@@ -0,0 +1,138 @@ +#ifndef _UAPI_LINUX_BUS1_H +#define _UAPI_LINUX_BUS1_H + +/* +* Copyright (C) 2013-2016 Red Hat, Inc. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU Lesser General Public License as published by the +* Free Software Foundation; either version 2.1 of the License, or (at +* your option) any later version. +*/ + +#include <linux/ioctl.h> +#include <linux/types.h> + +#define BUS1_FD_MAX (256) + +#define BUS1_IOCTL_MAGIC 0x96 +#define BUS1_HANDLE_INVALID ((__u64)-1) +#define BUS1_OFFSET_INVALID ((__u64)-1) + +enum { +BUS1_HANDLE_FLAG_MANAGED =1ULL <<0 , +BUS1_HANDLE_FLAG_REMOTE =1ULL <<1 , +}; + +enum { +BUS1_PEER_FLAG_WANT_SECCTX =1ULL <<0 , +}; + +enum { +BUS1_PEER_RESET_FLAG_FLUSH =1ULL <<0 , +BUS1_PEER_RESET_FLAG_FLUSH_SEED =1ULL <<1 , +}; + +struct bus1_cmd_peer_reset { +__u64 flags; +__u64 peer_flags; +__u32 max_slices; +__u32 max_handles; +__u32 max_inflight_bytes; +__u32 max_inflight_fds; +} __attribute__((__aligned__ (8 ))); + +struct bus1_cmd_handle_transfer { +__u64 flags; +__u64 src_handle; +__u64 dst_fd; +__u64 dst_handle; +} __attribute__((__aligned__ (8 ))); + +enum { +BUS1_NODES_DESTROY_FLAG_RELEASE_HANDLES =1ULL <<0 , +}; + +struct bus1_cmd_nodes_destroy { +__u64 flags; +__u64 ptr_nodes; +__u64 n_nodes; +} __attribute__((__aligned__ (8 ))); + +enum { +BUS1_SEND_FLAG_CONTINUE =1ULL <<0 , +BUS1_SEND_FLAG_SEED =1ULL <<1 , +}; + +struct bus1_cmd_send { +__u64 flags; +__u64 ptr_destinations; +__u64 ptr_errors; +__u64 n_destinations; +__u64 ptr_vecs; +__u64 n_vecs; +__u64 ptr_handles; +__u64 n_handles; +__u64 ptr_fds; +__u64 n_fds; +} __attribute__((__aligned__ (8 ))); + +enum { +BUS1_RECV_FLAG_PEEK =1ULL <<0 , +BUS1_RECV_FLAG_SEED =1ULL <<1 , +BUS1_RECV_FLAG_INSTALL_FDS =1ULL <<2 , +}; + +enum { +BUS1_MSG_NONE , +BUS1_MSG_DATA , +BUS1_MSG_NODE_DESTROY , +BUS1_MSG_NODE_RELEASE , +}; + +enum { +BUS1_MSG_FLAG_HAS_SECCTX =1ULL <<0 , +BUS1_MSG_FLAG_CONTINUE =1ULL <<1 , +}; + +struct bus1_cmd_recv { +__u64 flags; +__u64 max_offset; +struct { +__u64 type; +__u64 flags; +__u64 destination; +__u32 uid; +__u32 gid; +__u32 pid; +__u32 tid; +__u64 offset; +__u64 n_bytes; +__u64 n_handles; +__u64 n_fds; +__u64 n_secctx; + } __attribute__((__aligned__ (8 ))) msg; +} __attribute__((__aligned__ (8 ))); + +enum { +BUS1_CMD_PEER_DISCONNECT =_IOWR (BUS1_IOCTL_MAGIC ,0x00 , + __u64), +BUS1_CMD_PEER_QUERY =_IOWR (BUS1_IOCTL_MAGIC ,0x01 , + struct bus1_cmd_peer_reset), +BUS1_CMD_PEER_RESET =_IOWR (BUS1_IOCTL_MAGIC ,0x02 , + struct bus1_cmd_peer_reset), +BUS1_CMD_HANDLE_RELEASE =_IOWR (BUS1_IOCTL_MAGIC ,0x10 , + __u64), +BUS1_CMD_HANDLE_TRANSFER =_IOWR (BUS1_IOCTL_MAGIC ,0x11 , + struct bus1_cmd_handle_transfer), +BUS1_CMD_NODES_DESTROY =_IOWR (BUS1_IOCTL_MAGIC ,0x20 , + struct bus1_cmd_nodes_destroy), +BUS1_CMD_SLICE_RELEASE =_IOWR (BUS1_IOCTL_MAGIC ,0x30 , + __u64), +BUS1_CMD_SEND =_IOWR (BUS1_IOCTL_MAGIC ,0x40 , + struct bus1_cmd_send), +BUS1_CMD_RECV =_IOWR (BUS1_IOCTL_MAGIC ,0x50 , + struct bus1_cmd_recv), +}; + +#endif /* _UAPI_LINUX_BUS1_H */
init/Kconfig +17 -0
@@ -273,6 +273,23 @@ config POSIX_MQUEUE_SYSCTL depends on SYSCTL default y +config BUS1 + tristate "Bus1 Kernel Message Bus" + help + The Bus1 Kernel Message Bus defines and implements a distributed + object model. It provides a capability-based IPC system for machine + local communication. + + The Bus1 IPC system is exposed via /dev/bus1. If debugfs is enabled, + bus1 exposes additional debug information there. + +config BUS1_TESTS + bool "Bus1 Self-Tests" + depends on BUS1 + help + Enable and run the bus1 self-tests before loading the module. The + overhead is minimal, so there is generally no harm in enabling it. + config CROSS_MEMORY_ATTACH bool "Enable process_vm_readv/writev syscalls" depends on MMU
ipc/Makefile +1 -0
@@ -9,4 +9,5 @@ obj_mq-$(CONFIG_COMPAT) += compat_mq.o obj-$(CONFIG_POSIX_MQUEUE) += mqueue.o msgutil.o $(obj_mq-y) obj-$(CONFIG_IPC_NS) += namespace.o obj-$(CONFIG_POSIX_MQUEUE_SYSCTL) += mq_sysctl.o +obj-$(CONFIG_BUS1) += bus1/
ipc/bus1/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +bus1-y := \ + main.o + +obj-$(CONFIG_BUS1) += bus1.o + +bus1-$(CONFIG_BUS1_TESTS) += tests.o
ipc/bus1/main.c new file +80 -0
80 lines not on this page · raw message
ipc/bus1/main.h new file +74 -0
74 lines not on this page · raw message
ipc/bus1/tests.c new file +19 -0
@@ -0,0 +1,19 @@ +/* +* Copyright (C) 2013-2016 Red Hat, Inc. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU Lesser General Public License as published by the +* Free Software Foundation; either version 2.1 of the License, or (at +* your option) any later version. +*/ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/err.h> +#include <linux/kernel.h> +#include "tests.h" + +int bus1_tests_run (void ) +{ +pr_info ("run selftests..\n" ); +return 0 ; +}
ipc/bus1/tests.h new file +32 -0
@@ -0,0 +1,32 @@ +#ifndef __BUS1_TESTS_H +#define __BUS1_TESTS_H + +/* +* Copyright (C) 2013-2016 Red Hat, Inc. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU Lesser General Public License as published by the +* Free Software Foundation; either version 2.1 of the License, or (at +* your option) any later version. +*/ + +/** +* Kernel Selftests +* +* These tests are built into the kernel module itself if, and only if, the +* required configuration is selected. On every module load, the selftests will +* be run. On production builds, this option should not be selected. +*/ + +#include <linux/kernel.h> + +#if IS_ENABLED (CONFIG_BUS1_TESTS ) +int bus1_tests_run (void ); +#else +static inline int bus1_tests_run (void ) +{ +return 0 ; +} +#endif + +#endif /* __BUS1_TESTS_H */
Signature · 1 line
2.10.1
So the thing that tends to worry me about these is resource management.
If I understood the documentation correctly, this has per-user resource management, which guarantees that at least the system won't run out of memory. Good. The act of sending a message transfers the resource to the receiver end. Fine.
However, the usual problem ends up being that a bad user can basically DoS a system agent, especially since for obvious performance reasons the send/receive has to be asynchronous.
So the usual DoS model is that some user just sends a lot of messages to a system agent, filling up the system agent resource quota, and basically killing the system. No, it didn't run out of memory, but the system agent may not be able to do anything more, since it is now out of resources.
Keeping the resource management with the sender doesn't solve the problem, it just reverses it: now the attack will be to send a lot of queries to the system agent, but then just refuse to listen to the replies - again causing the system agent to run out of resources.
Usually the way this is resolved this is by forcing a "request-and-reply" resource management model, where the person who sends out a request is not only the one who is accounted for the request, but also accounted for the reply buffer. That way the system agent never runs out of resources, because it's always the requesting party that has its resources accounted, never the system agent.
You may well have solved this, but can you describe what the solution is without forcing people to read the code and try to analyze it?
Linus
Hi
Quoted 29 lines · Linus Torvalds
So the thing that tends to worry me about these is resource management.
If I understood the documentation correctly, this has per-user resource management, which guarantees that at least the system won't run out of memory. Good. The act of sending a message transfers the resource to the receiver end. Fine.
However, the usual problem ends up being that a bad user can basically DoS a system agent, especially since for obvious performance reasons the send/receive has to be asynchronous.
So the usual DoS model is that some user just sends a lot of messages to a system agent, filling up the system agent resource quota, and basically killing the system. No, it didn't run out of memory, but the system agent may not be able to do anything more, since it is now out of resources.
Keeping the resource management with the sender doesn't solve the problem, it just reverses it: now the attack will be to send a lot of queries to the system agent, but then just refuse to listen to the replies - again causing the system agent to run out of resources.
Usually the way this is resolved this is by forcing a "request-and-reply" resource management model, where the person who sends out a request is not only the one who is accounted for the request, but also accounted for the reply buffer. That way the system agent never runs out of resources, because it's always the requesting party that has its resources accounted, never the system agent.
Quoted 2 lines
You may well have solved this, but can you describe what the solution is without forcing people to read the code and try to analyze it?
All accounting on bus1 is done on a UID-basis. This is the initial model that tries to match POSIX semantics. More advanced accounting is left as a future extension (like cgroup-based, etc.). So whenever we talk about "user accounting", we talk about the user-abstraction in bus1 that right now is based on UIDs, but could be extended for other schemes.
All bus1 resources are owned by a peer, and each peer has a user assigned (which right now corresponds to file->f_cred->uid). Whenever a peer allocates resources, it is accounted on its user. There are global limits per user which cannot be exceeded. Additionally, each peer can set its own per-peer limits, to further partition the per-user limits. Of course, per-user limits override per-peer limits, if necessary.
Now this is all trivial and obvious. It works like any resource accounting in the kernel. It becomes tricky when we try to transfer resources. Before SEND, a resource is always accounted on the sender. After RECV, a resource is accounted on the receiver. That is, resource ownership is transferred. In most cases this is obvious: memory is copied from one address-space into another, or file-descriptors are added into the file-table of another process, etc.
Lastly, when a resource is queued, we decided to go with receiver-accounting. This means, at the time of SEND resource ownership is transferred (unlike sender-accounting, which would transfer it at time of RECV). The reasons are manifold, but mainly we want RECV to not fail due to accounting, resource exhaustion, etc. We wanted SEND to do the heavy-lifting, and RECV to just dequeue. By avoiding sender-based accounting, we avoid attacks where a receiver does not dequeue messages and thus exhausts the sender's limits. The issue left is senders DoS'ing a target user. To mitigate this, we implemented a quota system. Whenever a sender wants to transfer resources to a receiver, it only gets access to a subset of the receivers resource limits. The inflight resources are accounted on a uid<->uid basis, and the current algorithm allows a receiver access to at most half the limit of the destination not currently used by anyone else.
Example:
Imagine a receiver with a limit of 1024 handles. A sender transmits a message to that receiver. It gets access to half the limit not used by anyone else, hence 512 handles. It does not matter how many senders there are, nor how many messages are sent, it will reach its quota at
512. As long as they all belong to the same user, they will share the quota and can queue at most 512 handles. If a second sending user comes into play, it gets half the remaining not used by anyone else, which ends up being 256. And so on... If the peer dequeues messages in between, the numbers get higher again. But if you do the math, the most you can get is 50% of the targets resources, if you're the only sender. In all other cases you get less (like intertwined transfers, etc).
We did look into sender-based inflight accounting, but the same set of issues arises. Sure, a Request+Reply model would make this easier to handle, but we want to explicitly support a Subscribe+Event{n} model. In this case there is more than one Reply to a message.
Long story short: We have uid<->uid quotas so far, which prevent DoS attacks, unless you get access to a ridiculous amount of local UIDs. Details on which resources are accounted can be found in the wiki [1].
Thanks
David
Re: [RFC v1 02/14] bus1: provide stub cdev /dev/bus1
In reply to David HerrmannQuoted 8 lines · David Herrmann
From: Tom Gundersen <[email protected]>
Add the
CONFIG_BUS1option to enable the bus1 kernel messaging bus. If enabled, provide the bus1.ko module with a stub cdev/dev/bus1. So far it does not expose any API, but the full intended uapi is provided ininclude/uapi/linux/bus1.halready.
This may have been covered elsewhere, but could this use syscalls instead?
Re: [RFC v1 02/14] bus1: provide stub cdev /dev/bus1
Quoted 1 line · Andy Lutomirski
This may have been covered elsewhere, but could this use syscalls instead?
Yes, syscalls would work essentially the same. For now, we are using a cdev as it makes it a lot more convenient to develop and test as an out-of-tree module, but that could be changed easily before the final submission, if that's what we want.
Cheers,
Tom
Re: [RFC v1 02/14] bus1: provide stub cdev /dev/bus1
Quoted 7 lines · CEST Tom Gundersen
Quoted 1 line · Andy Lutomirski
This may have been covered elsewhere, but could this use syscalls instead?
Yes, syscalls would work essentially the same. For now, we are using a cdev as it makes it a lot more convenient to develop and test as an out-of-tree module, but that could be changed easily before the final submission, if that's what we want.
Generally speaking, I think syscalls would be appropriate here, and put bus1 into a similar category as the other ipc interfaces (shm, msg, sem, mqueue, ...).
However, syscall API design is nontrivial, and will require a bit of work to come to a set of syscalls that is fairly compact but also extensible enough. I think it makes sense to go through the exercise of working out what the syscall interface would end up looking like, and then make a decision.
There is currently a set of file operations:
@@ -48,7 +90,11 @@ const struct file_operations bus1_fops = {
.owner = THIS_MODULE,
.open = bus1_fop_open,
.release = bus1_fop_release,
+ .poll = bus1_fop_poll,
.llseek = noop_llseek,
+ .mmap = bus1_fop_mmap,
+ .unlocked_ioctl = bus1_peer_ioctl,
+ .compat_ioctl = bus1_peer_ioctl,
.show_fdinfo = bus1_fop_show_fdinfo,};
and then another set of ioctls:
+enum {
+ BUS1_CMD_PEER_DISCONNECT = _IOWR(BUS1_IOCTL_MAGIC, 0x00, + __u64), + BUS1_CMD_PEER_QUERY = _IOWR(BUS1_IOCTL_MAGIC, 0x01, + struct bus1_cmd_peer_reset), + BUS1_CMD_PEER_RESET = _IOWR(BUS1_IOCTL_MAGIC, 0x02, + struct bus1_cmd_peer_reset), + BUS1_CMD_HANDLE_RELEASE = _IOWR(BUS1_IOCTL_MAGIC, 0x10, + __u64), + BUS1_CMD_HANDLE_TRANSFER = _IOWR(BUS1_IOCTL_MAGIC, 0x11, + struct bus1_cmd_handle_transfer), + BUS1_CMD_NODES_DESTROY = _IOWR(BUS1_IOCTL_MAGIC, 0x20, + struct bus1_cmd_nodes_destroy), + BUS1_CMD_SLICE_RELEASE = _IOWR(BUS1_IOCTL_MAGIC, 0x30, + __u64), + BUS1_CMD_SEND = _IOWR(BUS1_IOCTL_MAGIC, 0x40, + struct bus1_cmd_send), + BUS1_CMD_RECV = _IOWR(BUS1_IOCTL_MAGIC, 0x50, + struct bus1_cmd_recv),
+};
I think there is no alternative to having some sort of file descriptor with the basic operations you have above, but there is a question of how to get that file descriptor if the ioctls get changed to a syscall, the basic options being:
- Keep using a chardev. This works, but feels a little odd to me, and I can't think of any other interfaces combining syscalls with a chardev.
- Have one syscall that returns an open file descriptor, replacing the fops->open() function. One advantage is that you can pass additional arguments in that you can't have with open. An example for this would be mqueue_open().
- Have a mountable file system, and use open() on that to create connections. Advantages are that it's fairly easy to have one instance per fs-namespace, and you can have user-defined naming of objects in the file system.
For the other operations, the obvious translation would be to turn each ioctl command into one syscall, but that may not always be the best representation. One limitation is that you cannot generally have more than six 'long' arguments on a lot of architectures, and passing 'u64' arguments to syscalls is awkward.
For some of the commands, the transformation would be straightforward if we assume that the 'u64' arguments can actually be 'long', I guess like this:
+struct bus1_cmd_handle_transfer {
+ __u64 flags; + __u64 src_handle; + __u64 dst_fd; + __u64 dst_handle;
+} __attribute__((__aligned__(8)));
long bus1_handle_transfer(int fd, unsigned long handle,
int dst_fd, unsigned long *dst_handle, unsigned int flags);
+struct bus1_cmd_nodes_destroy {
+ __u64 flags; + __u64 ptr_nodes; + __u64 n_nodes;
+} __attribute__((__aligned__(8)));
long bus1_nodes_destroy(int fd, u64 *ptr_nodes,
long n_nodes, unsigned int flags);
However, the peer_reset would exceed the 6-argument limit when you count the initial file descriptor even if you assume that 'flags' can be made 32-bit:
+struct bus1_cmd_peer_reset {
+ __u64 flags; + __u64 peer_flags; + __u32 max_slices; + __u32 max_handles; + __u32 max_inflight_bytes; + __u32 max_inflight_fds;
+} __attribute__((__aligned__(8)));
maybe something slightly ugly like
long bus1_peer_reset(int fd, const struct bus1_peer_limits *param,
unsigned int flags);
a library might provide a wrapper that passes all the limits as separate arguments.
The receive function would be fairly straightforward again, as we just pass a pointer to the returned message, and all inputs can be arguments, but the send command with this structure
+struct bus1_cmd_send {
+ __u64 flags; + __u64 ptr_destinations; + __u64 ptr_errors; + __u64 n_destinations; + __u64 ptr_vecs; + __u64 n_vecs; + __u64 ptr_handles; + __u64 n_handles; + __u64 ptr_fds; + __u64 n_fds;
+} __attribute__((__aligned__(8)));
is really tricky, as it's such a central interface but it's also really complex, with its five indirect pointers to variable-length arrays, making a total of 11 arguments (including the first fd). Turning this into a syscall would probably make a more efficient interface, so maybe some of the arrays can be turned into a single argument and require the user to call it multiple times instead of the kernel looping around it.
The minimal version would be something like
long bus1_send(int fd, long dst, struct iovec *vecs, int n_vecs,
long handle, int dst_fd);
so you already get to six arguments with one destination, one handle and one fd but no flags. Replacing vecs/n_vecs with pointer and length doesn't help either, so I guess whatever we do here we have to use some indirect structure.
Arnd
[CC += linuux-api]@vger.kernel.org
Hi David,
Could you please CC linux-api@ on all future iterations of this patch!
Cheers,
Michael
Quoted 194 lines · David Herrmann
Hi
This proposal introduces bus1.ko, a kernel messaging bus. This is not a request for inclusion, yet. It is rather an initial draft and a Request For Comments.
While bus1 emerged out of the kdbus project, bus1 was started from scratch and the concepts have little in common. In a nutshell, bus1 provides a capability-based IPC system, similar in nature to Android Binder, Cap'n Proto, and seL4. The module is completely generic and does neither require nor mandate a user-space counter-part.
o Description
Bus1 is a local IPC system, which provides a decentralized infrastructure to share objects between local peers. The main building blocks are nodes and handles. Nodes represent objects of a local peer, while handles represent descriptors that point to a node. Nodes can be created and destroyed by any peer, and they will always remain owned by their respective creator. Handles on the other hand, are used to refer to nodes and can be passed around with messages as auxiliary data. Whenever a handle is transferred, the receiver will get its own handle allocated, pointing to the same node as the original handle.
Any peer can send messages directed at one of their handles. This will transfer the message to the owner of the node the handle points to. If a peer does not posess a handle to a given node, it will not be able to send a message to that node. That is, handles provide exclusive access management. Anyone that somehow acquired a handle to a node is privileged to further send this handle to other peers. As such, access management is transitive. Once a peer acquired a handle, it cannot be revoked again. However, a node owner can, at anytime, destroy a node. This will effectively unbind all existing handles to that node on any peer, notifying each one of the destruction.
Unlike nodes and handles, peers cannot be addressed directly. In fact, peers are completely disconnected entities. A peer is merely an anchor of a set of nodes and handles, including an incoming message queue for any of those. Whether multiple nodes are all part of the same peer, or part of different peers does not affect the remote view of those. Peers solely exist as management entity and command dispatcher to local processes.
The set of actors on a system is completely decentralized. There is no global component involved that provides a central registry or discovery mechanism. Furthermore, communication between peers only involves those peers, and does not affect any other peer in any way. No global communication lock is taken. However, any communication is still globally ordered, including unicasts, multicasts, and notifications.
o Prior Art
The concepts behind bus1 are almost identical to capability systems like Android Binder, Google Mojo, Cap'n Proto, seL4, and more. Bus1 differs from them by supporting Global Ordering, Multicasts, Resource Accounting, No Global Locking, No Global Context.
While the bus1 UAPI does not expose all features (like soft-references as supported by Binder), the in-kernel code includes support for it. Multiple UAPIs can be supported on top of the in-kernel bus1 code, including support for the Binder UAPI. Efforts on this are still on-going.
o Documentation
The first patch in this series provides the bus1(7) man-page. It explains all concepts in bus1 in more detail. Furthermore, it describes the API that is available on bus1 file descriptors. The pre-compiled man-page is available at:
There is also a great bunch of in-source documentation available. All cross-source-file APIs have KernelDoc annotations. Furthermore, we have an introduction for each subsystem, to be found in the header files. The total number in lines of code for bus1 is roughly ~4.5k. The remaining ~5k LOC are comments and documentation.
o Upstream
The upstream development repository is available on github:
It is an out-of-tree repository that allows easy and fast development of new bus1 features. The in-tree integration repository is available at:
o Conferences
Tom and I will be attending Linux Plumbers Conf next week. Please do not hesitate to contact us there in person. There will also be a presentation
[1] of bus1 on the last day of the conference.Thanks
Tom & David[1] https://www.linuxplumbersconf.org/2016/ocw/proposals/3819
Tom Gundersen (14):
bus1: add bus1(7) man-page
bus1: provide stub cdev/dev/bus1
bus1: util - active reference utility library
bus1: util - fixed list utility library
bus1: util - pool utility library
bus1: util - queue utility library
bus1: tracking user contexts
bus1: implement peer management context
bus1: provide transaction context for multicasts
bus1: add handle management
bus1: implement message transmission
bus1: hook up file-operations
bus1: limit and protect resources
bus1: basic user-space kselftestsDocumentation/bus1/.gitignore | 2 + Documentation/bus1/Makefile | 41 + Documentation/bus1/bus1.xml | 833 +++++++++++++++++++++ Documentation/bus1/stylesheet.xsl | 16 + include/uapi/linux/bus1.h | 138 ++++ init/Kconfig | 17 + ipc/Makefile | 1 + ipc/bus1/Makefile | 16 + ipc/bus1/handle.c | 823 ++++++++++++++++++++ ipc/bus1/handle.h | 312 ++++++++ ipc/bus1/main.c | 146 ++++ ipc/bus1/main.h | 88 +++ ipc/bus1/message.c | 656 ++++++++++++++++ ipc/bus1/message.h | 171 +++++ ipc/bus1/peer.c | 1163 +++++++++++++++++++++++++++++ ipc/bus1/peer.h | 163 ++++ ipc/bus1/security.h | 45 ++ ipc/bus1/tests.c | 19 + ipc/bus1/tests.h | 32 + ipc/bus1/tx.c | 360 +++++++++ ipc/bus1/tx.h | 102 +++ ipc/bus1/user.c | 628 ++++++++++++++++ ipc/bus1/user.h | 140 ++++ ipc/bus1/util.c | 214 ++++++ ipc/bus1/util.h | 141 ++++ ipc/bus1/util/active.c | 419 +++++++++++ ipc/bus1/util/active.h | 154 ++++ ipc/bus1/util/flist.c | 116 +++ ipc/bus1/util/flist.h | 202 +++++ ipc/bus1/util/pool.c | 572 ++++++++++++++ ipc/bus1/util/pool.h | 164 ++++ ipc/bus1/util/queue.c | 445 +++++++++++ ipc/bus1/util/queue.h | 351 +++++++++ tools/testing/selftests/bus1/.gitignore | 2 + tools/testing/selftests/bus1/Makefile | 19 + tools/testing/selftests/bus1/bus1-ioctl.h | 111 +++ tools/testing/selftests/bus1/test-api.c | 532 +++++++++++++ tools/testing/selftests/bus1/test-io.c | 198 +++++ tools/testing/selftests/bus1/test.h | 114 +++ 39 files changed, 9666 insertions(+) create mode 100644 Documentation/bus1/.gitignore create mode 100644 Documentation/bus1/Makefile create mode 100644 Documentation/bus1/bus1.xml create mode 100644 Documentation/bus1/stylesheet.xsl create mode 100644 include/uapi/linux/bus1.h create mode 100644 ipc/bus1/Makefile create mode 100644 ipc/bus1/handle.c create mode 100644 ipc/bus1/handle.h create mode 100644 ipc/bus1/main.c create mode 100644 ipc/bus1/main.h create mode 100644 ipc/bus1/message.c create mode 100644 ipc/bus1/message.h create mode 100644 ipc/bus1/peer.c create mode 100644 ipc/bus1/peer.h create mode 100644 ipc/bus1/security.h create mode 100644 ipc/bus1/tests.c create mode 100644 ipc/bus1/tests.h create mode 100644 ipc/bus1/tx.c create mode 100644 ipc/bus1/tx.h create mode 100644 ipc/bus1/user.c create mode 100644 ipc/bus1/user.h create mode 100644 ipc/bus1/util.c create mode 100644 ipc/bus1/util.h create mode 100644 ipc/bus1/util/active.c create mode 100644 ipc/bus1/util/active.h create mode 100644 ipc/bus1/util/flist.c create mode 100644 ipc/bus1/util/flist.h create mode 100644 ipc/bus1/util/pool.c create mode 100644 ipc/bus1/util/pool.h create mode 100644 ipc/bus1/util/queue.c create mode 100644 ipc/bus1/util/queue.h create mode 100644 tools/testing/selftests/bus1/.gitignore create mode 100644 tools/testing/selftests/bus1/Makefile create mode 100644 tools/testing/selftests/bus1/bus1-ioctl.h create mode 100644 tools/testing/selftests/bus1/test-api.c create mode 100644 tools/testing/selftests/bus1/test-io.c create mode 100644 tools/testing/selftests/bus1/test.h--
2.10.1
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Author of "The Linux Programming Interface", http://blog.man7.org/
Re: [RFC v1 04/14] bus1: util - fixed list utility library
In reply to Peter ZijlstraHi
Quoted 14 lines · Peter Zijlstra
ipc/bus1/util/flist.h line 52
Quoted 10 lines · David Herrmann
ipc/bus1/util/flist.h new file +8
-0+ e =kmalloc_array (sizeof (*e),BUS1_FLIST_BATCH +1 , gfp); +#define BUS1_FLIST_BATCH (1024) +struct bus1_flist { +union { +struct bus1_flist *next; +void *ptr; + }; +};So that's an allocation of 8*(1024+1), or slightly more than 2 pages.
Quoted 6 lines
kmalloc will round up to the next power of two, so you'll end up with an allocation of 16*1024, wasting a whopping 8184 bytes per such allocation in slack space.
Please consider using 1023 or something for your batch size, 511 would get you to exactly 1 page which would be even better.
Thanks for the hint! 511 looks like the obvious choice. Maybe even (PAGE_SIZE / sizeof(long) - 1). I will put a comment next to the definition.
Thanks
David
Re: [RFC v1 04/14] bus1: util - fixed list utility library
In reply to Arnd BergmannHi
Quoted 23 lines · Arnd Bergmann
Quoted 22 lines · CEST David Herrmann
Quoted 20 lines · Peter Zijlstra
ipc/bus1/util/flist.h line 52
Quoted 10 lines · David Herrmann
ipc/bus1/util/flist.h new file +8
-0+ e =kmalloc_array (sizeof (*e),BUS1_FLIST_BATCH +1 , gfp); +#define BUS1_FLIST_BATCH (1024) +struct bus1_flist { +union { +struct bus1_flist *next; +void *ptr; + }; +};So that's an allocation of 8*(1024+1), or slightly more than 2 pages.
kmalloc will round up to the next power of two, so you'll end up with an allocation of 16*1024, wasting a whopping 8184 bytes per such allocation in slack space.
Please consider using 1023 or something for your batch size, 511 would get you to exactly 1 page which would be even better.
Quoted 8 lines
Quoted 5 lines
Thanks for the hint! 511 looks like the obvious choice. Maybe even (
PAGE_SIZE/ sizeof(long) - 1). I will put a comment next to the definition.
PAGE_SIZEcan be up to 64KB though, so that might lead wasting a lot of memory.
The bus1-flist implementation never over-allocates. It is a fixed size list, so it only allocates as much memory as needed. The issue PeterZ pointed out is passing suitable sizes to kmalloc(), which internally over-allocates to power-of-2 bounds (or some similar bounds). So we only ever waste space here if kmalloc() internally rounds up. The code in bus1-flist allocates exactly the needed space.
Thanks
David
Re: [RFC v1 04/14] bus1: util - fixed list utility library
In reply to David HerrmannQuoted 19 lines · CEST David Herrmann
Quoted 18 lines · Peter Zijlstra
ipc/bus1/util/flist.h line 52
Quoted 10 lines · David Herrmann
ipc/bus1/util/flist.h new file +8
-0+ e =kmalloc_array (sizeof (*e),BUS1_FLIST_BATCH +1 , gfp); +#define BUS1_FLIST_BATCH (1024) +struct bus1_flist { +union { +struct bus1_flist *next; +void *ptr; + }; +};So that's an allocation of 8*(1024+1), or slightly more than 2 pages.
kmalloc will round up to the next power of two, so you'll end up with an allocation of 16*1024, wasting a whopping 8184 bytes per such allocation in slack space.
Quoted 8 lines
Quoted 2 lines
Please consider using 1023 or something for your batch size, 511 would get you to exactly 1 page which would be even better.
Thanks for the hint! 511 looks like the obvious choice. Maybe even (
PAGE_SIZE/ sizeof(long) - 1). I will put a comment next to the definition.
PAGE_SIZE can be up to 64KB though, so that might lead wasting a lot of memory.
Arnd
Re: [RFC v1 05/14] bus1: util - pool utility library
In reply to David HerrmannQuoted 10 lines · David Herrmann
ipc/bus1/util/pool.c new file +10
-0+/* insert slice into the free tree */ +static void bus1_pool_slice_link_free (struct bus1_pool_slice *slice, +struct bus1_pool *pool) +{ +struct rb_node **n, *prev =NULL ; +struct bus1_pool_slice *ps; + + n = &pool->slices_free.rb_node; +while (*n) { + prev = *n;
ipc/bus1/util/pool.c line 75
Quoted 10 lines
ipc/bus1/util/pool.c new file +10
-0+ ps = container_of(prev, struct bus1_pool_slice, rb); +if (slice->size < ps->size) + n = &prev->rb_left; +else + n = &prev->rb_right; + } + +rb_link_node (&slice->rb, prev, n); +rb_insert_color (&slice->rb, &pool->slices_free); +}
If you only sort free slices by size, how do you merge contiguous free slices?
Quoted 33 lines
ipc/bus1/util/pool.c new file +33
-0+/* find free slice big enough to hold @size bytes */ +static struct bus1_pool_slice * +bus1_pool_slice_find_by_size (struct bus1_pool *pool,size_t size) +{ +struct bus1_pool_slice *ps, *closest =NULL ; +struct rb_node *n; + + n = pool->slices_free.rb_node; +while (n) { + ps = container_of(n, struct bus1_pool_slice, rb); +if (size < ps->size) { + closest = ps; + n = n->rb_left; + }else if (size > ps->size) { + n = n->rb_right; + }else /* if (size == ps->size) */ { +return ps; + } + } + +return closest; +} + +/* find used slice with given offset */ +static struct bus1_pool_slice * +bus1_pool_slice_find_by_offset (struct bus1_pool *pool,size_t offset) +{ +struct bus1_pool_slice *ps; +struct rb_node *n; + + n = pool->slices_busy.rb_node; +while (n) { + ps = container_of(n, struct bus1_pool_slice, rb);
ipc/bus1/util/pool.c line 144
Quoted 10 lines
ipc/bus1/util/pool.c new file +10
-0+if (offset < ps->offset) + n = n->rb_left; +else if (offset > ps->offset) + n = n->rb_right; +else /* if (offset == ps->offset) */ +return ps; + } + +return NULL ; +}
I find these two function names misleading. They don't find_by_size or find_by_offset. They find_free_by_size and find_busy_by_offset. You could reduce that to find_free and find_busy and have the 'size' and 'offset' in the argument name.
Re: [RFC v1 05/14] bus1: util - pool utility library
In reply to David Herrmannipc/bus1/util/pool.c line 54
Quoted 10 lines · David Herrmann
ipc/bus1/util/pool.c new file +10
-0+static struct bus1_pool_slice * +bus1_pool_slice_free (struct bus1_pool_slice *slice) +{ +if (!slice) +return NULL ; + +kfree (slice); + +return NULL ; +}
The return value is never used. Which reduces the entire thing to:
kfree(slice);
since kfree() already accepts a NULL.
Quoted 3 lines
ipc/bus1/util/pool.c new file +3
-0+bus1_pool_slice_free (slice); +bus1_pool_slice_free (slice); +bus1_pool_slice_free (ps);
Re: [RFC v1 04/14] bus1: util - fixed list utility library
In reply to David Herrmannipc/bus1/util/flist.h line 52
Quoted 10 lines · David Herrmann
ipc/bus1/util/flist.h new file +8
-0+ e =kmalloc_array (sizeof (*e),BUS1_FLIST_BATCH +1 , gfp); +#define BUS1_FLIST_BATCH (1024) +struct bus1_flist { +union { +struct bus1_flist *next; +void *ptr; + }; +};
So that's an allocation of 8*(1024+1), or slightly more than 2 pages.
kmalloc will round up to the next power of two, so you'll end up with an allocation of 16*1024, wasting a whopping 8184 bytes per such allocation in slack space.
Please consider using 1023 or something for your batch size, 511 would get you to exactly 1 page which would be even better.
Re: [RFC v1 05/14] bus1: util - pool utility library
In reply to Peter ZijlstraQuoted 16 lines · Peter Zijlstra
Quoted 15 lines · David Herrmann
ipc/bus1/util/pool.c new file +15
-0+/* insert slice into the free tree */ +static void bus1_pool_slice_link_free (struct bus1_pool_slice *slice, +struct bus1_pool *pool) +{ +struct rb_node **n, *prev =NULL ; +struct bus1_pool_slice *ps; + + n = &pool->slices_free.rb_node; +while (*n) { + prev = *n; + ps = container_of(prev, struct bus1_pool_slice, rb); +if (slice->size < ps->size) + n = &prev->rb_left; +else + n = &prev->rb_right;
Quoted 8 lines
ipc/bus1/util/pool.c line 75
Quoted 5 lines
ipc/bus1/util/pool.c new file +5
-0+ } + +rb_link_node (&slice->rb, prev, n); +rb_insert_color (&slice->rb, &pool->slices_free); +}If you only sort free slices by size, how do you merge contiguous free slices?
Ah, I see, you also keep an ordered list of slices and use that one function up from here.
Re: [RFC v1 05/14] bus1: util - pool utility library
In reply to David HerrmannOn Wed, Oct 26, 2016 at 09:18:01PM +0200, David Herrmann wrote:
All small nits..
Quoted 4 lines
ipc/bus1/util/pool.c new file +4
-0+void bus1_pool_deinit (struct bus1_pool *pool) +{ +struct bus1_pool_slice *slice; +
ipc/bus1/util/pool.c line 233
Quoted 10 lines
ipc/bus1/util/pool.c new file +10
-0+if (!pool || !pool->f) +return ; + +while ((slice =list_first_entry_or_null (&pool->slices, + struct bus1_pool_slice, + entry))) { +WARN_ON (slice->ref_kernel); +list_del (&slice->entry); +bus1_pool_slice_free (slice); + }
I prefer to write that loop like:
while (!list_empty(&pool->slices)) {
slice = list_first_entry(&pool->slices, struct bus1_pool_slice, entry);
list_del(&slice->entry);
// ...
}Quoted 13 lines
ipc/bus1/util/pool.c new file +13
-0+static void bus1_pool_free (struct bus1_pool *pool, +struct bus1_pool_slice *slice) +{ +struct bus1_pool_slice *ps; + +/* don't free the slice if either has a reference */ +if (slice->ref_kernel || slice->ref_user ||WARN_ON (slice->free)) +return ; + +/* +* To release a pool-slice, we first drop it from the busy-tree, then +* merge it with possible previous/following free slices and re-add it +* to the free-tree.
ipc/bus1/util/pool.c line 327
Quoted 10 lines
ipc/bus1/util/pool.c new file +10
-0+*/ + +rb_erase (&slice->rb, &pool->slices_busy); + +if (!WARN_ON (slice->size > pool->allocated_size)) + pool->allocated_size -= slice->size; + +if (pool->slices.next != &slice->entry) { + ps = container_of(slice->entry.prev, struct bus1_pool_slice, + entry);
ps = list_prev_entry(slice, entry);
ipc/bus1/util/pool.c line 339
Quoted 12 lines
ipc/bus1/util/pool.c new file +12
-0+if (ps->free) { +rb_erase (&ps->rb, &pool->slices_free); +list_del (&slice->entry); + ps->size += slice->size; +bus1_pool_slice_free (slice); + slice = ps;/* switch to previous slice */ + } + } + +if (pool->slices.prev != &slice->entry) { + ps = container_of(slice->entry.next, struct bus1_pool_slice, + entry);
ps = list_next_entry(slice, entry);
Quoted 11 lines
ipc/bus1/util/pool.c new file +11
-0+if (ps->free) { +rb_erase (&ps->rb, &pool->slices_free); +list_del (&ps->entry); + slice->size += ps->size; +bus1_pool_slice_free (ps); + } + } + + slice->free = true; +bus1_pool_slice_link_free (slice, pool); +}
Re: [RFC v1 02/14] bus1: provide stub cdev /dev/bus1
In reply to Arnd BergmannQuoted 10 lines · Arnd Bergmann
Quoted 7 lines · CEST Tom Gundersen
Quoted 1 line · Andy Lutomirski
This may have been covered elsewhere, but could this use syscalls instead?
Yes, syscalls would work essentially the same. For now, we are using a cdev as it makes it a lot more convenient to develop and test as an out-of-tree module, but that could be changed easily before the final submission, if that's what we want.
Quoted 3 lines
Generally speaking, I think syscalls would be appropriate here, and put bus1 into a similar category as the other ipc interfaces (shm, msg, sem, mqueue, ...).
Could you elaborate on why you think syscalls would be more appropriate than ioctls?
Quoted 43 lines
However, syscall API design is nontrivial, and will require a bit of work to come to a set of syscalls that is fairly compact but also extensible enough. I think it makes sense to go through the exercise of working out what the syscall interface would end up looking like, and then make a decision.
There is currently a set of file operations:
(quoted diff) +4
-0@@ -48,7 +90,11 @@ const struct file_operations bus1_fops = { .owner = THIS_MODULE, .open = bus1_fop_open, .release = bus1_fop_release, + .poll = bus1_fop_poll, .llseek = noop_llseek, + .mmap = bus1_fop_mmap, + .unlocked_ioctl = bus1_peer_ioctl, + .compat_ioctl = bus1_peer_ioctl, .show_fdinfo = bus1_fop_show_fdinfo, };and then another set of ioctls:
include/uapi/linux/bus1.h new file +20
-0+enum { +BUS1_CMD_PEER_DISCONNECT =_IOWR (BUS1_IOCTL_MAGIC ,0x00 , + __u64), +BUS1_CMD_PEER_QUERY =_IOWR (BUS1_IOCTL_MAGIC ,0x01 , + struct bus1_cmd_peer_reset), +BUS1_CMD_PEER_RESET =_IOWR (BUS1_IOCTL_MAGIC ,0x02 , + struct bus1_cmd_peer_reset), +BUS1_CMD_HANDLE_RELEASE =_IOWR (BUS1_IOCTL_MAGIC ,0x10 , + __u64), +BUS1_CMD_HANDLE_TRANSFER =_IOWR (BUS1_IOCTL_MAGIC ,0x11 , + struct bus1_cmd_handle_transfer), +BUS1_CMD_NODES_DESTROY =_IOWR (BUS1_IOCTL_MAGIC ,0x20 , + struct bus1_cmd_nodes_destroy), +BUS1_CMD_SLICE_RELEASE =_IOWR (BUS1_IOCTL_MAGIC ,0x30 , + __u64), +BUS1_CMD_SEND =_IOWR (BUS1_IOCTL_MAGIC ,0x40 , + struct bus1_cmd_send), +BUS1_CMD_RECV =_IOWR (BUS1_IOCTL_MAGIC ,0x50 , + struct bus1_cmd_recv), +};
Quoted 4 lines
I think there is no alternative to having some sort of file descriptor with the basic operations you have above, but there is a question of how to get that file descriptor if the ioctls get changed to a syscall, the basic options being:
I could see the point of wanting a syscall to get the fd (your second option below), but as I said, not sure I see why we would want to use syscalls instead of ioctls.
Quoted 8 lines
- Keep using a chardev. This works, but feels a little odd to me, and I can't think of any other interfaces combining syscalls with a chardev.
- Have one syscall that returns an open file descriptor, replacing the
fops->open()function. One advantage is that you can pass additional arguments in that you can't have with open. An example for this would bemqueue_open().
If we are going to change it, this might makes sense to me. It would allow you to get the fd without having to have access to some character device.
Quoted 4 lines
- Have a mountable file system, and use
open()on that to create connections. Advantages are that it's fairly easy to have one instance per fs-namespace, and you can have user-defined naming of objects in the file system.
Note that currently we only have one object (/dev/bus1) and each fd is disconnected from anything else on creation, so not sure what benefits a filesystem (or several instances of it) would give?
Quoted 87 lines
For the other operations, the obvious translation would be to turn each ioctl command into one syscall, but that may not always be the best representation. One limitation is that you cannot generally have more than six 'long' arguments on a lot of architectures, and passing 'u64' arguments to syscalls is awkward.
For some of the commands, the transformation would be straightforward if we assume that the 'u64' arguments can actually be 'long', I guess like this:
include/uapi/linux/bus1.h new file +6
-0+struct bus1_cmd_handle_transfer { +__u64 flags; +__u64 src_handle; +__u64 dst_fd; +__u64 dst_handle; +} __attribute__((__aligned__ (8 )));long bus1_handle_transfer(int fd, unsigned long handle,
int dst_fd, unsigned long *dst_handle, unsigned int flags);include/uapi/linux/bus1.h new file +5
-0+struct bus1_cmd_nodes_destroy { +__u64 flags; +__u64 ptr_nodes; +__u64 n_nodes; +} __attribute__((__aligned__ (8 )));long bus1_nodes_destroy(int fd, u64
*ptr_nodes,long n_nodes, unsigned int flags);However, the
peer_resetwould exceed the 6-argument limit when you count the initial file descriptor even if you assume that 'flags' can be made 32-bit:include/uapi/linux/bus1.h new file +8
-0+struct bus1_cmd_peer_reset { +__u64 flags; +__u64 peer_flags; +__u32 max_slices; +__u32 max_handles; +__u32 max_inflight_bytes; +__u32 max_inflight_fds; +} __attribute__((__aligned__ (8 )));maybe something slightly ugly like
long bus1_peer_reset(int fd, const struct
bus1_peer_limits*param,unsigned int flags);a library might provide a wrapper that passes all the limits as separate arguments.
The receive function would be fairly straightforward again, as we just pass a pointer to the returned message, and all inputs can be arguments, but the send command with this structure
include/uapi/linux/bus1.h new file +12
-0+struct bus1_cmd_send { +__u64 flags; +__u64 ptr_destinations; +__u64 ptr_errors; +__u64 n_destinations; +__u64 ptr_vecs; +__u64 n_vecs; +__u64 ptr_handles; +__u64 n_handles; +__u64 ptr_fds; +__u64 n_fds; +} __attribute__((__aligned__ (8 )));is really tricky, as it's such a central interface but it's also really complex, with its five indirect pointers to variable-length arrays, making a total of 11 arguments (including the first fd). Turning this into a syscall would probably make a more efficient interface, so maybe some of the arrays can be turned into a single argument and require the user to call it multiple times instead of the kernel looping around it.
The minimal version would be something like
long bus1_send(int fd, long dst, struct iovec *vecs, int
n_vecs,long handle, int dst_fd);so you already get to six arguments with one destination, one handle and one fd but no flags. Replacing vecs/n_vecs with pointer and length doesn't help either, so I guess whatever we do here we have to use some indirect structure.
Arnd
Re: [RFC v1 06/14] bus1: util - queue utility library
In reply to David HerrmannQuoted 5 lines · David Herrmann
Messages can be destined for multiple queues, hence, we need to be careful that all queues get a consistent order of incoming messages. We define the concept of `global order' to provide a basic set of guarantees. This global order is a partial order on the set of all messages. The order is defined as:
Ah, ok. So it _is_ a partial order only. I got confused by earlier reports, and the term 'global order' in general, to think you did a total order. And then I wondered wth you needed total ordering for.
Maybe best to scrub 'global order' from the entire text. It's not helpful.
Re: [RFC v1 02/14] bus1: provide stub cdev /dev/bus1
In reply to Tom GundersenQuoted 3 lines · Tom Gundersen
Could you elaborate on why you think syscalls would be more appropriate than ioctls?
ioctl's tend to be a horrid mess both for things like compat.but also for things like system call tracing and filtering (ie BPF).
The compat mess is fixable by making sure you always use 64-bit fields rather than pointers everywhere and everything is aligned. The tracing and filtering one not so much.
Linus
Re: [RFC v1 02/14] bus1: provide stub cdev /dev/bus1
Quoted 10 lines · Linus Torvalds
Quoted 3 lines · Tom Gundersen
Could you elaborate on why you think syscalls would be more appropriate than ioctls?
ioctl's tend to be a horrid mess both for things like compat.but also for things like system call tracing and filtering (ie BPF).
The compat mess is fixable by making sure you always use 64-bit fields rather than pointers everywhere and everything is aligned.
This we do.
Quoted 2 lines
The
tracing and filtering one not so much.
Got it. Thanks.
Tom
Re: [RFC v1 06/14] bus1: util - queue utility library
In reply to David HerrmannQuoted 7 lines · David Herrmann
A bus1 message queue is a FIFO, i.e., messages are linearly ordered by the time they were sent. Moreover, atomic delivery of messages to multiple queues are supported, without any global synchronization, i.e., the order of message delivery is consistent across queues.
Messages can be destined for multiple queues, hence, we need to be careful that all queues get a consistent order of incoming messages.
So I read that to mean that if A and B both send a multi-cast message to C and D, the messages will appear in the same order for both C and D.
Why is this important? It seem that this multi-cast ordering generates much of the complexity of this patch while this Changelog fails to explain why this is a desired property.
Quoted 17 lines
We
define the concept of `global order' to provide a basic set of guarantees. This global order is a partial order on the set of all messages. The order is defined as:1) If a message B was queued *after* a message A, then: A < B
2) If a message B was queued *after* a message A was dequeued,
then: A < B3) If a message B was dequeued *after* a message A on the same queue,
then: A < B(Note: Causality is honored.
after' andbefore' do not refer to
the same task, nor the same queue, but rather any kind of synchronization between the two operations.)
Quoted 6 lines
The queue object implements this global order in a lockless fashion. It solely relies on a distributed clock on each queue. Each message to be sent causes a clock tick on the local clock and on all destination clocks. Furthermore, all clocks are synchronized, meaning they're fast-forwarded in case they're behind the highest of all participating peers. No global state tracking is involved.
Yet the code does compares on more than just timestamps. Why are these secondary (and even tertiary) ordering required?
Re: [RFC v1 06/14] bus1: util - queue utility library
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · David Herrmann
A bus1 message queue is a FIFO, i.e., messages are linearly ordered by the time they were sent. Moreover, atomic delivery of messages to multiple queues are supported, without any global synchronization, i.e., the order of message delivery is consistent across queues.
Quoted 5 lines
Quoted 2 lines
Messages can be destined for multiple queues, hence, we need to be careful that all queues get a consistent order of incoming messages.
So I read that to mean that if A and B both send a multi-cast message to C and D, the messages will appear in the same order for both C and D.
That is one of the ordering guarantees, yes.
Quoted 3 lines
Why is this important? It seem that this multi-cast ordering generates much of the complexity of this patch while this Changelog fails to explain why this is a desired property.
I don't think this is the case. The most important guarantee we give is causal ordering. To make this work with multicast, we must stage messages first, then commit on a second round. That is, we must find some way to iterate over all clocks before committing, but at the same time preventing any races. The multicast-stability as you just described we get for free by introducing the second-level ordering via sender-address.
Stability in multicasts without causal order is not necessarily a crucial feature. However, note that if this ordering is given, it allows reducing the number of round-trips in dependent systems. Imagine a daemon reacting to a set of events from different sources. If the actions of that daemon are solely defined by incoming events, someone else can deduce the actions the daemon took without requiring the daemon to send out events by itself. That is, you can just watch the events on the system, and validly deduce the state of such daemon.
Example: There is a configuration daemon that sends events when configuration is changed. And there is a hotplug daemon that sends events when devices are hotplugged. You get an event that the "default mute-state" for audio devices was changed, after it you get a hotplugged audio device. You can now rely on the audio daemon to get the events in the same order, and hence apply the new "default mute-state" to the new device. No need to query the audio daemon whether the new device is muted.
But as I said, the causal ordering is what we really want. Multicast-stability is just a nice side-effect.
It might also be note mentioning: Both Android Binder and Chromium Mojo make sure they provide causal ordering, since they run into real issues. Binder allows placing multiple messages under the same binder-lock, and Mojo provides Associated Interfaces [1]. DBus makes sure to provide those ordering guarantees as well.
Quoted 24 lines
Quoted 17 lines
We
define the concept of `global order' to provide a basic set of guarantees. This global order is a partial order on the set of all messages. The order is defined as:1) If a message B was queued *after* a message A, then: A < B
2) If a message B was queued *after* a message A was dequeued,
then: A < B3) If a message B was dequeued *after* a message A on the same queue,
then: A < B(Note: Causality is honored.
after' andbefore' do not refer to
the same task, nor the same queue, but rather any kind of synchronization between the two operations.)Quoted 6 lines
The queue object implements this global order in a lockless fashion. It solely relies on a distributed clock on each queue. Each message to be sent causes a clock tick on the local clock and on all destination clocks. Furthermore, all clocks are synchronized, meaning they're fast-forwarded in case they're behind the highest of all participating peers. No global state tracking is involved.
Quoted 2 lines
Yet the code does compares on more than just timestamps. Why are these secondary (and even tertiary) ordering required?
Lamport Timestamps are guaranteed to be unique per-sender, but a receiving queue can still contain messages with the same timestamp (from different senders). That is, if two multicasts overlap, they might end up with the same timestamp, if, and only if, they can have no causal relationship (i.e., the ioctls
are called concurrently). We want to extend this partial order, though. We want to provide a stable order in those cases (as described above), so we need a secondary order (we simply pick the memory address of the sender). This guarantees that all receivers get the same order of all messages (even if they have equal timestamps).
Note that equal timestamps only happen if entries block each other. Hence, we can use the memory address as secondary order, since we know it is unique in those cases (and cannot be re-used).
Cheers,
Tom
[1] https://docs.google.com/document/d/1ENDDzACX4hplfQ8cCHGo_rXd3IHTu5H4hEZ44Cu8KVs
Re: [RFC v1 08/14] bus1: implement peer management context
In reply to David HerrmannDavid, Tom,
Quoted 9 lines · David Herrmann
ipc/bus1/peer.c new file +9
-0+struct bus1_peer *bus1_peer_new (void ) +{ +static atomic64_t peer_ids =ATOMIC64_INIT (0 ); +const struct cred *cred =current_cred (); +struct bus1_peer *peer; +struct bus1_user *user; + + user =bus1_user_ref_by_uid (cred->uid); +if (IS_ERR (user))
ipc/bus1/peer.c line 62
Quoted 10 lines
ipc/bus1/peer.c new file +10
-0+return ERR_CAST (user); + + peer =kmalloc (sizeof (*peer),GFP_KERNEL ); +if (!peer) { +bus1_user_unref (user); +return ERR_PTR (-ENOMEM ); + } + +/* initialize constant fields */ + peer->id =atomic64_inc_return (&peer_ids);
What is the purpose of this id? Do other components depend on it and are they aware of possible overflows?
Since it is an 64bit integer overflowing it is hard but not impossible.
Signature · 2 lines
Thanks, //richard
Re: [RFC v1 08/14] bus1: implement peer management context
In reply to David HerrmannQuoted 7 lines · David Herrmann
ipc/bus1/peer.c new file +7
-0+/* initialize constant fields */ + peer->id =atomic64_inc_return (&peer_ids); + peer->flags =0 ; + peer->cred =get_cred (current_cred ()); + peer->pid_ns =get_pid_ns (task_active_pid_ns (current)); + peer->user = user; + peer->debugdir =NULL ;
ipc/bus1/peer.c line 77
Quoted 10 lines
ipc/bus1/peer.c new file +10
-0+init_waitqueue_head (&peer->waitq); +bus1_active_init (&peer->active); + +/* initialize data section */ +mutex_init (&peer->data.lock); + +/* initialize peer-private section */ +mutex_init (&peer->local.lock); + +if (!IS_ERR_OR_NULL (bus1_debugdir)) {
How can bus1_debugdir contain an error code? AFACT it is either a valid dentry or NULL.
Quoted 6 lines
ipc/bus1/peer.c new file +6
-0+char idstr[22 ]; + +snprintf (idstr,sizeof (idstr),"peer-%llx" , peer->id); + + peer->debugdir =debugfs_create_dir (idstr, bus1_debugdir); +if (!peer->debugdir) {
ipc/bus1/peer.c line 93
Quoted 10 lines
ipc/bus1/peer.c new file +10
-0+pr_err ("cannot create debugfs dir for peer %llx\n" , + peer->id); + }else if (!IS_ERR_OR_NULL (peer->debugdir)) { +bus1_debugfs_create_atomic_x ("active" ,S_IRUGO , + peer->debugdir, + &peer->active.count); + } + } + +bus1_active_activate (&peer->active);
This is a no-nop since bus1_active_init() set ->count to BUS1_ACTIVE_NEW.
Quoted 17 lines
ipc/bus1/peer.c new file +17
-0+return peer; +} + +static int bus1_peer_disconnect (struct bus1_peer *peer) +{ +bus1_active_deactivate (&peer->active); +bus1_active_drain (&peer->active, &peer->waitq); + +if (!bus1_active_cleanup (&peer->active, &peer->waitq, +NULL ,NULL )) +return -ESHUTDOWN ; + +return 0 ; +} + +/** +* bus1_peer_free() - destroy peer
ipc/bus1/peer.c line 120
Quoted 10 lines
ipc/bus1/peer.c new file +10
-0+* @peer: peer to destroy, or NULL +* +* Destroy a peer object that was previously allocated via bus1_peer_new(). +* This synchronously waits for any outstanding operations on this peer to +* finish, then releases all linked resources and deallocates the peer in an +* rcu-delayed manner. +* +* If NULL is passed, this is a no-op. +* +* Return: NULL is returned.
What about making the function of type void?
Quoted 24 lines
ipc/bus1/peer.c new file +24
-0+struct bus1_peer *bus1_peer_free (struct bus1_peer *peer) +{ +if (!peer) +return NULL ; + +/* disconnect from environment */ +bus1_peer_disconnect (peer); + +/* deinitialize peer-private section */ +mutex_destroy (&peer->local.lock); + +/* deinitialize data section */ +mutex_destroy (&peer->data.lock); + +/* deinitialize constant fields */ +debugfs_remove_recursive (peer->debugdir); +bus1_active_deinit (&peer->active); + peer->user =bus1_user_unref (peer->user); +put_pid_ns (peer->pid_ns); +put_cred (peer->cred); +kfree_rcu (peer, rcu); + +return NULL ; +}
Signature · 2 lines
Thanks, //richard
Quoted 10 lines · David Herrmann
Hi
This proposal introduces bus1.ko, a kernel messaging bus. This is not a request for inclusion, yet. It is rather an initial draft and a Request For Comments.
While bus1 emerged out of the kdbus project, bus1 was started from scratch and the concepts have little in common. In a nutshell, bus1 provides a capability-based IPC system, similar in nature to Android Binder, Cap'n Proto, and seL4. The module is completely generic and does neither require nor mandate a user-space counter-part.
One thing which is not so clear to me is the role of bus1 wrt. containers. Can a container A exchange messages with a container B?
If not, where is the boundary? I guess it is the pid namespace.
Signature · 2 lines
Thanks, //richard
Re: [RFC v1 08/14] bus1: implement peer management context
In reply to Richard WeinbergerQuoted 17 lines · Richard Weinberger
David, Tom,
Quoted 14 lines · David Herrmann
ipc/bus1/peer.c new file +14
-0+struct bus1_peer *bus1_peer_new (void ) +{ +static atomic64_t peer_ids =ATOMIC64_INIT (0 ); +const struct cred *cred =current_cred (); +struct bus1_peer *peer; +struct bus1_user *user; + + user =bus1_user_ref_by_uid (cred->uid); +if (IS_ERR (user)) +return ERR_CAST (user); + + peer =kmalloc (sizeof (*peer),GFP_KERNEL ); +if (!peer) { +bus1_user_unref (user);
Quoted 8 lines
ipc/bus1/peer.c line 62
Quoted 5 lines
ipc/bus1/peer.c new file +5
-0+return ERR_PTR (-ENOMEM ); + } + +/* initialize constant fields */ + peer->id =atomic64_inc_return (&peer_ids);What is the purpose of this id? Do other components depend on it and are they aware of possible overflows?
The id is used purely to give a name to the peer in debugfs.
Quoted 1 line
Since it is an 64bit integer overflowing it is hard but not impossible.
Hm, what scenario do you have in mind? I cannot see how this could happen (short of creating peers in a loop for hundreds of years).
Cheers,
Tom
Re: [RFC v1 08/14] bus1: implement peer management context
Quoted 28 lines · Tom Gundersen
Quoted 17 lines · Richard Weinberger
David, Tom,
Quoted 14 lines · David Herrmann
ipc/bus1/peer.c new file +14
-0+struct bus1_peer *bus1_peer_new (void ) +{ +static atomic64_t peer_ids =ATOMIC64_INIT (0 ); +const struct cred *cred =current_cred (); +struct bus1_peer *peer; +struct bus1_user *user; + + user =bus1_user_ref_by_uid (cred->uid); +if (IS_ERR (user)) +return ERR_CAST (user); + + peer =kmalloc (sizeof (*peer),GFP_KERNEL ); +if (!peer) { +bus1_user_unref (user);Quoted 8 lines
ipc/bus1/peer.c line 62
Quoted 5 lines
ipc/bus1/peer.c new file +5
-0+return ERR_PTR (-ENOMEM ); + } + +/* initialize constant fields */ + peer->id =atomic64_inc_return (&peer_ids);What is the purpose of this id? Do other components depend on it and are they aware of possible overflows?
Quoted 1 line
The id is used purely to give a name to the peer in debugfs.
Okay.
Quoted 4 lines
Quoted 1 line
Since it is an 64bit integer overflowing it is hard but not impossible.
Hm, what scenario do you have in mind? I cannot see how this could happen (short of creating peers in a loop for hundreds of years).
When it is purely for naming creating peers is slow enough it is no problem at all. That's why I was asking.
Thanks,
//richard
Re: [RFC v1 08/14] bus1: implement peer management context
In reply to Richard WeinbergerQuoted 13 lines · Richard Weinberger
Quoted 12 lines · David Herrmann
ipc/bus1/peer.c new file +12
-0+/* initialize constant fields */ + peer->id =atomic64_inc_return (&peer_ids); + peer->flags =0 ; + peer->cred =get_cred (current_cred ()); + peer->pid_ns =get_pid_ns (task_active_pid_ns (current)); + peer->user = user; + peer->debugdir =NULL ; +init_waitqueue_head (&peer->waitq); +bus1_active_init (&peer->active); + +/* initialize data section */ +mutex_init (&peer->data.lock);
Quoted 8 lines
ipc/bus1/peer.c line 77
Quoted 5 lines
ipc/bus1/peer.c new file +5
-0+ +/* initialize peer-private section */ +mutex_init (&peer->local.lock); + +if (!IS_ERR_OR_NULL (bus1_debugdir)) {How can
bus1_debugdircontain an error code? AFACT it is either a valid dentry or NULL.
If debugfs is not enabled it will be ERR_PTR(-ENODEV).
Quoted 10 lines
Quoted 10 lines
ipc/bus1/peer.c new file +10
-0+char idstr[22 ]; + +snprintf (idstr,sizeof (idstr),"peer-%llx" , peer->id); + + peer->debugdir =debugfs_create_dir (idstr, bus1_debugdir); +if (!peer->debugdir) { +pr_err ("cannot create debugfs dir for peer %llx\n" , + peer->id); + }else if (!IS_ERR_OR_NULL (peer->debugdir)) { + bus1_debugfs_create_atomic_x("active", S_IRUGO,
Quoted 8 lines
ipc/bus1/peer.c line 93
Quoted 6 lines
ipc/bus1/peer.c new file +6
-0+ peer->debugdir, + &peer->active.count); + } + } + +bus1_active_activate (&peer->active);This is a no-nop since
bus1_active_init()set ->count toBUS1_ACTIVE_NEW.
bus1_active_activate() changes count from BUS1_ACTIVE_NEW to 0.
Quoted 21 lines
Quoted 21 lines
ipc/bus1/peer.c new file +21
-0+return peer; +} + +static int bus1_peer_disconnect (struct bus1_peer *peer) +{ +bus1_active_deactivate (&peer->active); +bus1_active_drain (&peer->active, &peer->waitq); + +if (!bus1_active_cleanup (&peer->active, &peer->waitq, +NULL ,NULL )) +return -ESHUTDOWN ; + +return 0 ; +} + +/** +* bus1_peer_free() - destroy peer +* @peer: peer to destroy, or NULL +* +* Destroy a peer object that was previously allocated via bus1_peer_new(). +* This synchronously waits for any outstanding operations on this peer to
Quoted 8 lines
Quoted 6 lines
+ * finish, then releases all linked resources and deallocates the peer in an
+ * rcu-delayed manner.
+ *
+ * If NULL is passed, this is a no-op.
+ *
+ * Return: NULL is returned.What about making the function of type void?
We are consistently returning the type being freed so we can do
foo->bar = bar_free(bar);
Just a matter of style though.
Quoted 28 lines
Quoted 14 lines
ipc/bus1/peer.c new file +14
-0+struct bus1_peer *bus1_peer_free (struct bus1_peer *peer) +{ +if (!peer) +return NULL ; + +/* disconnect from environment */ +bus1_peer_disconnect (peer); + +/* deinitialize peer-private section */ +mutex_destroy (&peer->local.lock); + +/* deinitialize data section */ +mutex_destroy (&peer->data.lock); +ipc/bus1/peer.c line 145
Quoted 10 lines
ipc/bus1/peer.c new file +10
-0+/* deinitialize constant fields */ +debugfs_remove_recursive (peer->debugdir); +bus1_active_deinit (&peer->active); + peer->user =bus1_user_unref (peer->user); +put_pid_ns (peer->pid_ns); +put_cred (peer->cred); +kfree_rcu (peer, rcu); + +return NULL ; +}--
Thanks,
//richard
Re: [RFC v1 06/14] bus1: util - queue utility library
In reply to Tom GundersenQuoted 14 lines · Tom Gundersen
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · David Herrmann
A bus1 message queue is a FIFO, i.e., messages are linearly ordered by the time they were sent. Moreover, atomic delivery of messages to multiple queues are supported, without any global synchronization, i.e., the order of message delivery is consistent across queues.
Quoted 5 lines
Quoted 2 lines
Messages can be destined for multiple queues, hence, we need to be careful that all queues get a consistent order of incoming messages.
So I read that to mean that if A and B both send a multi-cast message to C and D, the messages will appear in the same order for both C and D.
Quoted 8 lines
That is one of the ordering guarantees, yes.
Quoted 3 lines
Why is this important? It seem that this multi-cast ordering generates much of the complexity of this patch while this Changelog fails to explain why this is a desired property.
I don't think this is the case. The most important guarantee we give is causal ordering.
C and D not observing the message in the same order is consistent with causality (and actual physics). The cause is A sending something the effect is C receiving something. These two events must be ordered (which yields the partial order). But there is no guarantee that different observers would observe the same order. Esp. since A and B do not share a clock and these events are not in fact ordered themselves.
When we go back to the example of special relativity, as per the paper, this is trivially observable if we put A and C together in a frame of reference and B and D in a different frame and have the two frames move (at a significant fraction of the speed of light) relative to one another. The signal, being an emission of light, would not arrive at both observers in the same order (if the signal was given sufficiently 'simultaneous')
Quoted 5 lines
To make this work with multicast, we must stage messages first, then commit on a second round. That is, we must find some way to iterate over all clocks before committing, but at the same time preventing any races. The multicast-stability as you just described we get for free by introducing the second-level ordering via sender-address.
And this, precisely, is what generates all the complexity found in this patch. You want to strictly provide more than causality, which does not, as per the argument above, provide this at all.
You're providing a semi-global ordering of things that are themselves not actually ordered.
Quoted 9 lines
Stability in multicasts without causal order is not necessarily a crucial feature. However, note that if this ordering is given, it allows reducing the number of round-trips in dependent systems. Imagine a daemon reacting to a set of events from different sources. If the actions of that daemon are solely defined by incoming events, someone else can deduce the actions the daemon took without requiring the daemon to send out events by itself. That is, you can just watch the events on the system, and validly deduce the state of such daemon.
Quoted 8 lines
Example: There is a configuration daemon that sends events when configuration is changed. And there is a hotplug daemon that sends events when devices are hotplugged. You get an event that the "default mute-state" for audio devices was changed, after it you get a hotplugged audio device. You can now rely on the audio daemon to get the events in the same order, and hence apply the new "default mute-state" to the new device. No need to query the audio daemon whether the new device is muted.
Which is cute; but is it worth the pain?
Quoted 2 lines
But as I said, the causal ordering is what we really want. Multicast-stability is just a nice side-effect.
I'm saying they're not the same thing and multi-cast stability isn't at all implied.
Quoted 12 lines · Richard Weinberger
Quoted 10 lines · David Herrmann
Hi
This proposal introduces bus1.ko, a kernel messaging bus. This is not a request for inclusion, yet. It is rather an initial draft and a Request For Comments.
While bus1 emerged out of the kdbus project, bus1 was started from scratch and the concepts have little in common. In a nutshell, bus1 provides a capability-based IPC system, similar in nature to Android Binder, Cap'n Proto, and seL4. The module is completely generic and does neither require nor mandate a user-space counter-part.
Quoted 3 lines
One thing which is not so clear to me is the role of bus1 wrt. containers. Can a container A exchange messages with a container B?
If not, where is the boundary? I guess it is the pid namespace.
There is no restriction with respect to containers. The metadata is translated between namespaces, obviously, but you can send messages to anyone you have a handle to.
Cheers,
Tom
Re: [RFC v1 06/14] bus1: util - queue utility library
In reply to Peter ZijlstraQuoted 45 lines · Peter Zijlstra
Quoted 14 lines · Tom Gundersen
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · David Herrmann
A bus1 message queue is a FIFO, i.e., messages are linearly ordered by the time they were sent. Moreover, atomic delivery of messages to multiple queues are supported, without any global synchronization, i.e., the order of message delivery is consistent across queues.
Quoted 5 lines
Quoted 2 lines
Messages can be destined for multiple queues, hence, we need to be careful that all queues get a consistent order of incoming messages.
So I read that to mean that if A and B both send a multi-cast message to C and D, the messages will appear in the same order for both C and D.
Quoted 8 lines
That is one of the ordering guarantees, yes.
Quoted 3 lines
Why is this important? It seem that this multi-cast ordering generates much of the complexity of this patch while this Changelog fails to explain why this is a desired property.
I don't think this is the case. The most important guarantee we give is causal ordering.
C and D not observing the message in the same order is consistent with causality (and actual physics). The cause is A sending something the effect is C receiving something. These two events must be ordered (which yields the partial order). But there is no guarantee that different observers would observe the same order. Esp. since A and B do not share a clock and these events are not in fact ordered themselves.
When we go back to the example of special relativity, as per the paper, this is trivially observable if we put A and C together in a frame of reference and B and D in a different frame and have the two frames move (at a significant fraction of the speed of light) relative to one another. The signal, being an emission of light, would not arrive at both observers in the same order (if the signal was given sufficiently 'simultaneous')
Quoted 5 lines
To make this work with multicast, we must stage messages first, then commit on a second round. That is, we must find some way to iterate over all clocks before committing, but at the same time preventing any races. The multicast-stability as you just described we get for free by introducing the second-level ordering via sender-address.
Quoted 6 lines
And this, precisely, is what generates all the complexity found in this patch. You want to strictly provide more than causality, which does not, as per the argument above, provide this at all.
You're providing a semi-global ordering of things that are themselves not actually ordered.
We are providing two things: causality (as in your physics example above), and consistency (which, I agree, is cute, but not necessarily crucial). However, the complexity comes from causality. Consistency is trivial. The only thing needed for consistency is to tag each message by its sender and use this to resolve conflicts in the ordering. The alternative would be to just let these entries order arbitrarily instead, but conceptually it would not be simpler and it would only save us a few lines of code.
Quoted 18 lines
Quoted 9 lines
Stability in multicasts without causal order is not necessarily a crucial feature. However, note that if this ordering is given, it allows reducing the number of round-trips in dependent systems. Imagine a daemon reacting to a set of events from different sources. If the actions of that daemon are solely defined by incoming events, someone else can deduce the actions the daemon took without requiring the daemon to send out events by itself. That is, you can just watch the events on the system, and validly deduce the state of such daemon.
Quoted 8 lines
Example: There is a configuration daemon that sends events when configuration is changed. And there is a hotplug daemon that sends events when devices are hotplugged. You get an event that the "default mute-state" for audio devices was changed, after it you get a hotplugged audio device. You can now rely on the audio daemon to get the events in the same order, and hence apply the new "default mute-state" to the new device. No need to query the audio daemon whether the new device is muted.
Quoted 7 lines
Which is cute; but is it worth the pain?
Quoted 2 lines
But as I said, the causal ordering is what we really want. Multicast-stability is just a nice side-effect.
I'm saying they're not the same thing and multi-cast stability isn't at all implied.
Yeah, we agree. These are orthogonal concepts. What I meant is that once we have causality, getting consistency as a side-effect is virtually free.
Re: [RFC v1 08/14] bus1: implement peer management context
In reply to Tom GundersenQuoted 24 lines · Tom Gundersen
Quoted 13 lines · Richard Weinberger
Quoted 12 lines · David Herrmann
ipc/bus1/peer.c new file +12
-0+/* initialize constant fields */ + peer->id =atomic64_inc_return (&peer_ids); + peer->flags =0 ; + peer->cred =get_cred (current_cred ()); + peer->pid_ns =get_pid_ns (task_active_pid_ns (current)); + peer->user = user; + peer->debugdir =NULL ; +init_waitqueue_head (&peer->waitq); +bus1_active_init (&peer->active); + +/* initialize data section */ +mutex_init (&peer->data.lock);Quoted 8 lines
ipc/bus1/peer.c line 77
Quoted 5 lines
ipc/bus1/peer.c new file +5
-0+ +/* initialize peer-private section */ +mutex_init (&peer->local.lock); + +if (!IS_ERR_OR_NULL (bus1_debugdir)) {How can
bus1_debugdircontain an error code? AFACT it is either a valid dentry or NULL.
Quoted 1 line
If debugfs is not enabled it will be
ERR_PTR(-ENODEV).
I thought you handle that earlier. But just figured that you check only for NULL after doing debugfs_create_dir(). This confused me.
Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
ipc/bus1/peer.c new file +10
-0+char idstr[22 ]; + +snprintf (idstr,sizeof (idstr),"peer-%llx" , peer->id); + + peer->debugdir =debugfs_create_dir (idstr, bus1_debugdir); +if (!peer->debugdir) { +pr_err ("cannot create debugfs dir for peer %llx\n" , + peer->id); + }else if (!IS_ERR_OR_NULL (peer->debugdir)) { + bus1_debugfs_create_atomic_x("active", S_IRUGO,Quoted 8 lines
ipc/bus1/peer.c line 93
Quoted 6 lines
ipc/bus1/peer.c new file +6
-0+ peer->debugdir, + &peer->active.count); + } + } + +bus1_active_activate (&peer->active);This is a no-nop since
bus1_active_init()set ->count toBUS1_ACTIVE_NEW.
Quoted 1 line
bus1_active_activate()changes count fromBUS1_ACTIVE_NEWto 0.
Too many "active" words. ;)
Now it makes sense. BUS1_ACTIVE_NEW is state "NEW" and the unnamed state "ready to use" is a counter >= 0.
Thanks,
//richard
Re: [RFC v1 06/14] bus1: util - queue utility library
In reply to Tom GundersenQuoted 18 lines · Tom Gundersen
Quoted 8 lines · Peter Zijlstra
On Fri, Oct 28, 2016 at 01:33:25PM +0200, Tom Gundersen wrote:
And this, precisely, is what generates all the complexity found in this patch. You want to strictly provide more than causality, which does not, as per the argument above, provide this at all.
You're providing a semi-global ordering of things that are themselves not actually ordered.
We are providing two things: causality (as in your physics example above), and consistency (which, I agree, is cute, but not necessarily crucial). However, the complexity comes from causality. Consistency is trivial. The only thing needed for consistency is to tag each message by its sender and use this to resolve conflicts in the ordering. The alternative would be to just let these entries order arbitrarily instead, but conceptually it would not be simpler and it would only save us a few lines of code.
Quoted 5 lines · Earlier you
Quoted 5 lines
Quoted 5 lines
To make this work with multicast, we must stage messages first, then commit on a second round. That is, we must find some way to iterate over all clocks before committing, but at the same time preventing any races. The multicast-stability as you just described we get for free by introducing the second-level ordering via sender-address.
But you don't need the two-pass thing at all for causality. The entire two-pass thing, and the serialization, is part of the consistency thing.
This is not virtually free.
For causality, all you need is a single iteration, delivering the message one after the other, only ever doing local clock movements. You do not need to find the max clock in the multicast set and avoid races etc..
Re: [RFC v1 06/14] bus1: util - queue utility library
Quoted 31 lines · Peter Zijlstra
Quoted 18 lines · Tom Gundersen
Quoted 8 lines · Peter Zijlstra
On Fri, Oct 28, 2016 at 01:33:25PM +0200, Tom Gundersen wrote:
And this, precisely, is what generates all the complexity found in this patch. You want to strictly provide more than causality, which does not, as per the argument above, provide this at all.
You're providing a semi-global ordering of things that are themselves not actually ordered.
We are providing two things: causality (as in your physics example above), and consistency (which, I agree, is cute, but not necessarily crucial). However, the complexity comes from causality. Consistency is trivial. The only thing needed for consistency is to tag each message by its sender and use this to resolve conflicts in the ordering. The alternative would be to just let these entries order arbitrarily instead, but conceptually it would not be simpler and it would only save us a few lines of code.
Quoted 5 lines · Earlier you
Quoted 5 lines
Quoted 5 lines
To make this work with multicast, we must stage messages first, then commit on a second round. That is, we must find some way to iterate over all clocks before committing, but at the same time preventing any races. The multicast-stability as you just described we get for free by introducing the second-level ordering via sender-address.
But you don't need the two-pass thing at all for causality. The entire two-pass thing, and the serialization, is part of the consistency thing.
Quoted 6 lines
This is not virtually free.
For causality, all you need is a single iteration, delivering the message one after the other, only ever doing local clock movements. You do not need to find the max clock in the multicast set and avoid races etc..
Ah, I see, we are talking past each other. The property we do want (which is not trivial) is that we do not want to observe the effect before the cause. If an event at A causes an event at B, then the two events should be guaranteed to be observed at C in that order. i.e., if you send a multi-cast message from A to B and C and as a result of receiving the message, B sends a message to C, we want to be guaranteed that C receives the latter after the former.
If this property is not wanted, then (repeated) unicast can in most cases be used instead of multi-cast (and a natural optimization, which we left out for now, would be to skip the staging round for unicast messages).
Cheers,
Tom
Re: [RFC v1 09/14] bus1: provide transaction context for multicasts
In reply to David HerrmannQuoted 13 lines · David Herrmann
From: Tom Gundersen <[email protected]>
The transaction engine is an object that lives on the stack and is used to stage and commit multicasts properly. Unlike unicasts, a multicast cannot just be queued on each destination, but must be properly synchronized. This requires us to first stage each message on their respective destination, then sync and tick the clocks, and eventual commit all messages.
The transaction context implements this logic for both, unicasts and multicasts. It hides the timestamp handling and takes care to properly synchronize accesses to the peer queues.
Quoted 8 lines
Signed-off-by Tom Gundersen <[email protected]>Signed-off-by David Herrmann <[email protected]>--- ipc/bus1/Makefile | 1 + ipc/bus1/peer.c | 2 + ipc/bus1/peer.h | 3 + ipc/bus1/tx.c | 360 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ipc/bus1/tx.h | 102 ++++++++++++++++
See, this is way more than 4 lines.
You don't need any of this for causality.
Re: [RFC v1 06/14] bus1: util - queue utility library
In reply to Tom GundersenQuoted 1 line · Tom Gundersen
Ah, I see, we are talking past each other.
Ah I see where my reasoning went wobbly, not sure how to fully express that yet. I think your solution is stronger than strictly required though, but I'm not sure there's a better one. I'll think on it.
Quoted 3 lines · David Herrmann
Long story short: We have uid<->uid quotas so far, which prevent DoS attacks, unless you get access to a ridiculous amount of local UIDs. Details on which resources are accounted can be found in the wiki [1].
Does only root user_ns uid count as separate or per-ns too?
In first case we will have vitually unbounded access to UIDs.
The second case can cap number of user namespaces a user can create while using bus1 inside.
Or am I missing something?
Signature · 1 line
Kirill A. Shutemov
Re: [RFC v1 01/14] bus1: add bus1(7) man-page
In reply to David HerrmannQuoted 1 line · David Herrmann
+To receive messag payloads, each peer has an associated shmem-backed
s/messag/message/
Signature · 1 line
Kirill A. Shutemov
Quoted 11 lines · Kirill A. Shutemov
Quoted 3 lines · David Herrmann
Long story short: We have uid<->uid quotas so far, which prevent DoS attacks, unless you get access to a ridiculous amount of local UIDs. Details on which resources are accounted can be found in the wiki [1].
Does only root
user_nsuid count as separate or per-ns too?In first case we will have vitually unbounded access to UIDs.
The second case can cap number of user namespaces a user can create while using bus1 inside.
That seems easy enough to solve. Make the uid<->uid quota use uids in the namespace of the side whose resources the operation uses. That way, if both sender and recipient live in a user namespace then you get quota per user in the namespace, but you can't use a user namespace to cheat and manufacture more users to get more quota when talking to something *outside* that namespace.
- Josh Triplett
Re: [RFC v1 02/14] bus1: provide stub cdev /dev/bus1
In reply to Tom GundersenQuoted 11 lines · Tom Gundersen
Quoted 10 lines · Arnd Bergmann
Quoted 7 lines · CEST Tom Gundersen
Quoted 1 line · Andy Lutomirski
This may have been covered elsewhere, but could this use syscalls instead?
Yes, syscalls would work essentially the same. For now, we are using a cdev as it makes it a lot more convenient to develop and test as an out-of-tree module, but that could be changed easily before the final submission, if that's what we want.
Quoted 6 lines
Quoted 3 lines
Generally speaking, I think syscalls would be appropriate here, and put bus1 into a similar category as the other ipc interfaces (shm, msg, sem, mqueue, ...).
Could you elaborate on why you think syscalls would be more appropriate than ioctls?
Linus already answered this, but I'd also add that core kernel features just make sense to be syscalls, rather than stuffing them in a random device driver.
Quoted 8 lines
Quoted 4 lines
- Have a mountable file system, and use
open()on that to create connections. Advantages are that it's fairly easy to have one instance per fs-namespace, and you can have user-defined naming of objects in the file system.Note that currently we only have one object (
/dev/bus1) and each fd is disconnected from anything else on creation, so not sure what benefits a filesystem (or several instances of it) would give?
I have not tried to understand some of the main concepts of bus1, so I simply assumed that there was some way of looking up handles of other instances. Using a file system gives you a natural way to look up resources by name the way we do e.g. for mq_open(), and it lets you easy decide whether containers should share a view of the same namespace by mounting the same instance of the file system into them or having separate instances.
If you don't ever need to look up a handle by name in bus1, using a mountable file system would not help you.
Arnd
Hi
Quoted 5 lines · Kirill A. Shutemov
Quoted 3 lines · David Herrmann
Long story short: We have uid<->uid quotas so far, which prevent DoS attacks, unless you get access to a ridiculous amount of local UIDs. Details on which resources are accounted can be found in the wiki [1].
Quoted 8 lines
Does only root
user_nsuid count as separate or per-ns too?In first case we will have vitually unbounded access to UIDs.
The second case can cap number of user namespaces a user can create while using bus1 inside.
Or am I missing something?
We use the exact same mechanism as "struct user_struct" (as defined in linux/sched.h). One instance corresponds to each kuid_t currently in use. This is analogous to task, epoll, inotify, fanotify, mqueue, pipes, keys, ... resource accounting.
Could you elaborate on what problem you see?
Thanks
David
Hi!
I'm a bit late to the party...
Quoted 19 lines
Example:
Imagine a receiver with a limit of 1024 handles. A sender transmits a message to that receiver. It gets access to half the limit not used by anyone else, hence 512 handles. It does not matter how many senders there are, nor how many messages are sent, it will reach its quota at
512. As long as they all belong to the same user, they will share the quota and can queue at most 512 handles. If a second sending user comes into play, it gets half the remaining not used by anyone else, which ends up being 256. And so on... If the peer dequeues messages in between, the numbers get higher again. But if you do the math, the most you can get is 50% of the targets resources, if you're the only sender. In all other cases you get less (like intertwined transfers, etc).We did look into sender-based inflight accounting, but the same set of issues arises. Sure, a Request+Reply model would make this easier to handle, but we want to explicitly support a Subscribe+Event{n} model. In this case there is more than one Reply to a message.
Quoted 4 lines
Long story short: We have uid<->uid quotas so far, which prevent DoS attacks, unless you get access to a ridiculous amount of local UIDs. Details on which resources are accounted can be found in the wiki [1].
So if there's limit of 1024 handles, all I need is 10 UIDs, right?
That might be a problem on multiuser unix machine, but on Android phones, each application gets its own UID. So all you need is 10 applications to bring the system down...
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html