kdbus: the driver, original and non-working
[platform/kernel/linux-rpi.git] / ipc / kdbus / node.c
1 /*
2  * Copyright (C) 2013-2015 Kay Sievers
3  * Copyright (C) 2013-2015 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
4  * Copyright (C) 2013-2015 Daniel Mack <daniel@zonque.org>
5  * Copyright (C) 2013-2015 David Herrmann <dh.herrmann@gmail.com>
6  * Copyright (C) 2013-2015 Linux Foundation
7  *
8  * kdbus is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation; either version 2.1 of the License, or (at
11  * your option) any later version.
12  */
13
14 #include <linux/atomic.h>
15 #include <linux/fs.h>
16 #include <linux/idr.h>
17 #include <linux/kdev_t.h>
18 #include <linux/rbtree.h>
19 #include <linux/rwsem.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/wait.h>
23
24 #include "bus.h"
25 #include "domain.h"
26 #include "endpoint.h"
27 #include "fs.h"
28 #include "handle.h"
29 #include "node.h"
30 #include "util.h"
31
32 /**
33  * DOC: kdbus nodes
34  *
35  * Nodes unify lifetime management across exposed kdbus objects and provide a
36  * hierarchy. Each kdbus object, that might be exposed to user-space, has a
37  * kdbus_node object embedded and is linked into the hierarchy. Each node can
38  * have any number (0-n) of child nodes linked. Each child retains a reference
39  * to its parent node. For root-nodes, the parent is NULL.
40  *
41  * Each node object goes through a bunch of states during it's lifetime:
42  *     * NEW
43  *       * LINKED    (can be skipped by NEW->FREED transition)
44  *         * ACTIVE  (can be skipped by LINKED->INACTIVE transition)
45  *       * INACTIVE
46  *       * DRAINED
47  *     * FREED
48  *
49  * Each node is allocated by the caller and initialized via kdbus_node_init().
50  * This never fails and sets the object into state NEW. From now on, ref-counts
51  * on the node manage its lifetime. During init, the ref-count is set to 1. Once
52  * it drops to 0, the node goes to state FREED and the node->free_cb() callback
53  * is called to deallocate any memory.
54  *
55  * After initializing a node, you usually link it into the hierarchy. You need
56  * to provide a parent node and a name. The node will be linked as child to the
57  * parent and a globally unique ID is assigned to the child. The name of the
58  * child must be unique for all children of this parent. Otherwise, linking the
59  * child will fail with -EEXIST.
60  * Note that the child is not marked active, yet. Admittedly, it prevents any
61  * other node from being linked with the same name (thus, it reserves that
62  * name), but any child-lookup (via name or unique ID) will never return this
63  * child unless it has been marked active.
64  *
65  * Once successfully linked, you can use kdbus_node_activate() to activate a
66  * child. This will mark the child active. This state can be skipped by directly
67  * deactivating the child via kdbus_node_deactivate() (see below).
68  * By activating a child, you enable any lookups on this child to succeed from
69  * now on. Furthermore, any code that got its hands on a reference to the node,
70  * can from now on "acquire" the node.
71  *
72  *     Active References (or: 'acquiring' and 'releasing' a node)
73  *     Additionally to normal object references, nodes support something we call
74  *     "active references". An active reference can be acquired via
75  *     kdbus_node_acquire() and released via kdbus_node_release(). A caller
76  *     _must_ own a normal object reference whenever calling those functions.
77  *     Unlike object references, acquiring an active reference can fail (by
78  *     returning 'false' from kdbus_node_acquire()). An active reference can
79  *     only be acquired if the node is marked active. If it is not marked
80  *     active, yet, or if it was already deactivated, no more active references
81  *     can be acquired, ever!
82  *     Active references are used to track tasks working on a node. Whenever a
83  *     task enters kernel-space to perform an action on a node, it acquires an
84  *     active reference, performs the action and releases the reference again.
85  *     While holding an active reference, the node is guaranteed to stay active.
86  *     If the node is deactivated in parallel, the node is marked as
87  *     deactivated, then we wait for all active references to be dropped, before
88  *     we finally proceed with any cleanups. That is, if you hold an active
89  *     reference to a node, any resources that are bound to the "active" state
90  *     are guaranteed to stay accessible until you release your reference.
91  *
92  *     Active-references are very similar to rw-locks, where acquiring a node is
93  *     equal to try-read-lock and releasing to read-unlock. Deactivating a node
94  *     means write-lock and never releasing it again.
95  *     Unlike rw-locks, the 'active reference' concept is more versatile and
96  *     avoids unusual rw-lock usage (never releasing a write-lock..).
97  *
98  *     It is safe to acquire multiple active-references recursively. But you
99  *     need to check the return value of kdbus_node_acquire() on _each_ call. It
100  *     may stop granting references at _any_ time.
101  *
102  *     You're free to perform any operations you want while holding an active
103  *     reference, except sleeping for an indefinite period. Sleeping for a fixed
104  *     amount of time is fine, but you usually should not wait on wait-queues
105  *     without a timeout.
106  *     For example, if you wait for I/O to happen, you should gather all data
107  *     and schedule the I/O operation, then release your active reference and
108  *     wait for it to complete. Then try to acquire a new reference. If it
109  *     fails, perform any cleanup (the node is now dead). Otherwise, you can
110  *     finish your operation.
111  *
112  * All nodes can be deactivated via kdbus_node_deactivate() at any time. You can
113  * call this multiple times, even in parallel or on nodes that were never
114  * linked, and it will just work. The only restriction is, you must not hold an
115  * active reference when calling kdbus_node_deactivate().
116  * By deactivating a node, it is immediately marked inactive. Then, we wait for
117  * all active references to be released (called 'draining' the node). This
118  * shouldn't take very long as we don't perform long-lasting operations while
119  * holding an active reference. Note that once the node is marked inactive, no
120  * new active references can be acquired.
121  * Once all active references are dropped, the node is considered 'drained'. Now
122  * kdbus_node_deactivate() is called on each child of the node before we
123  * continue deactivating our node. That is, once all children are entirely
124  * deactivated, we call ->release_cb() of our node. ->release_cb() can release
125  * any resources on that node which are bound to the "active" state of a node.
126  * When done, we unlink the node from its parent rb-tree, mark it as
127  * 'released' and return.
128  * If kdbus_node_deactivate() is called multiple times (even in parallel), all
129  * but one caller will just wait until the node is fully deactivated. That is,
130  * one random caller of kdbus_node_deactivate() is selected to call
131  * ->release_cb() and cleanup the node. Only once all this is done, all other
132  * callers will return from kdbus_node_deactivate(). That is, it doesn't matter
133  * whether you're the selected caller or not, it will only return after
134  * everything is fully done.
135  *
136  * When a node is activated, we acquire a normal object reference to the node.
137  * This reference is dropped after deactivation is fully done (and only iff the
138  * node really was activated). This allows callers to link+activate a child node
139  * and then drop all refs. The node will be deactivated together with the
140  * parent, and then be freed when this reference is dropped.
141  *
142  * Currently, nodes provide a bunch of resources that external code can use
143  * directly. This includes:
144  *
145  *     * node->waitq: Each node has its own wait-queue that is used to manage
146  *                    the 'active' state. When a node is deactivated, we wait on
147  *                    this queue until all active refs are dropped. Analogously,
148  *                    when you release an active reference on a deactivated
149  *                    node, and the active ref-count drops to 0, we wake up a
150  *                    single thread on this queue. Furthermore, once the
151  *                    ->release_cb() callback finished, we wake up all waiters.
152  *                    The node-owner is free to re-use this wait-queue for other
153  *                    purposes. As node-management uses this queue only during
154  *                    deactivation, it is usually totally fine to re-use the
155  *                    queue for other, preferably low-overhead, use-cases.
156  *
157  *     * node->type: This field defines the type of the owner of this node. It
158  *                   must be set during node initialization and must remain
159  *                   constant. The node management never looks at this value,
160  *                   but external users might use to gain access to the owner
161  *                   object of a node.
162  *                   It is totally up to the owner of the node to define what
163  *                   their type means. Usually it means you can access the
164  *                   parent structure via container_of(), as long as you hold an
165  *                   active reference to the node.
166  *
167  *     * node->free_cb:    callback after all references are dropped
168  *       node->release_cb: callback during node deactivation
169  *                         These fields must be set by the node owner during
170  *                         node initialization. They must remain constant. If
171  *                         NULL, they're skipped.
172  *
173  *     * node->mode: filesystem access modes
174  *       node->uid:  filesystem owner uid
175  *       node->gid:  filesystem owner gid
176  *                   These fields must be set by the node owner during node
177  *                   initialization. They must remain constant and may be
178  *                   accessed by other callers to properly initialize
179  *                   filesystem nodes.
180  *
181  *     * node->id: This is an unsigned 32bit integer allocated by an IDA. It is
182  *                 always kept as small as possible during allocation and is
183  *                 globally unique across all nodes allocated by this module. 0
184  *                 is reserved as "not assigned" and is the default.
185  *                 The ID is assigned during kdbus_node_link() and is kept until
186  *                 the object is freed. Thus, the ID surpasses the active
187  *                 lifetime of a node. As long as you hold an object reference
188  *                 to a node (and the node was linked once), the ID is valid and
189  *                 unique.
190  *
191  *     * node->name: name of this node
192  *       node->hash: 31bit hash-value of @name (range [2..INT_MAX-1])
193  *                   These values follow the same lifetime rules as node->id.
194  *                   They're initialized when the node is linked and then remain
195  *                   constant until the last object reference is dropped.
196  *                   Unlike the id, the name is only unique across all siblings
197  *                   and only until the node is deactivated. Currently, the name
198  *                   is even unique if linked but not activated, yet. This might
199  *                   change in the future, though. Code should not rely on this.
200  *
201  *     * node->lock:     lock to protect node->children, node->rb, node->parent
202  *     * node->parent: Reference to parent node. This is set during LINK time
203  *                     and is dropped during destruction. You must not access
204  *                     it unless you hold an active reference to the node or if
205  *                     you know the node is dead.
206  *     * node->children: rb-tree of all linked children of this node. You must
207  *                       not access this directly, but use one of the iterator
208  *                       or lookup helpers.
209  */
210
211 /*
212  * Bias values track states of "active references". They're all negative. If a
213  * node is active, its active-ref-counter is >=0 and tracks all active
214  * references. Once a node is deactivaed, we subtract NODE_BIAS. This means, the
215  * counter is now negative but still counts the active references. Once it drops
216  * to exactly NODE_BIAS, we know all active references were dropped. Exactly one
217  * thread will change it to NODE_RELEASE now, perform cleanup and then put it
218  * into NODE_DRAINED. Once drained, all other threads that tried deactivating
219  * the node will now be woken up (thus, they wait until the node is fully done).
220  * The initial state during node-setup is NODE_NEW. If a node is directly
221  * deactivated without having ever been active, it is put into
222  * NODE_RELEASE_DIRECT instead of NODE_BIAS. This tracks this one-bit state
223  * across node-deactivation. The task putting it into NODE_RELEASE now knows
224  * whether the node was active before or not.
225  *
226  * Some archs implement atomic_sub(v) with atomic_add(-v), so reserve INT_MIN
227  * to avoid overflows if multiplied by -1.
228  */
229 #define KDBUS_NODE_BIAS                 (INT_MIN + 5)
230 #define KDBUS_NODE_RELEASE_DIRECT       (KDBUS_NODE_BIAS - 1)
231 #define KDBUS_NODE_RELEASE              (KDBUS_NODE_BIAS - 2)
232 #define KDBUS_NODE_DRAINED              (KDBUS_NODE_BIAS - 3)
233 #define KDBUS_NODE_NEW                  (KDBUS_NODE_BIAS - 4)
234
235 /* global unique ID mapping for kdbus nodes */
236 DEFINE_IDA(kdbus_node_ida);
237
238 /**
239  * kdbus_node_name_hash() - hash a name
240  * @name:       The string to hash
241  *
242  * This computes the hash of @name. It is guaranteed to be in the range
243  * [2..INT_MAX-1]. The values 1, 2 and INT_MAX are unused as they are reserved
244  * for the filesystem code.
245  *
246  * Return: hash value of the passed string
247  */
248 static unsigned int kdbus_node_name_hash(const char *name)
249 {
250         unsigned int hash;
251
252         /* reserve hash numbers 0, 1 and >=INT_MAX for magic directories */
253         hash = kdbus_strhash(name) & INT_MAX;
254         if (hash < 2)
255                 hash += 2;
256         if (hash >= INT_MAX)
257                 hash = INT_MAX - 1;
258
259         return hash;
260 }
261
262 /**
263  * kdbus_node_name_compare() - compare a name with a node's name
264  * @hash:       hash of the string to compare the node with
265  * @name:       name to compare the node with
266  * @node:       node to compare the name with
267  *
268  * Return: 0 if @name and @hash exactly match the information in @node, or
269  * an integer less than or greater than zero if @name is found, respectively,
270  * to be less than or be greater than the string stored in @node.
271  */
272 static int kdbus_node_name_compare(unsigned int hash, const char *name,
273                                    const struct kdbus_node *node)
274 {
275         if (hash != node->hash)
276                 return hash - node->hash;
277
278         return strcmp(name, node->name);
279 }
280
281 /**
282  * kdbus_node_init() - initialize a kdbus_node
283  * @node:       Pointer to the node to initialize
284  * @type:       The type the node will have (KDBUS_NODE_*)
285  *
286  * The caller is responsible of allocating @node and initializating it to zero.
287  * Once this call returns, you must use the node_ref() and node_unref()
288  * functions to manage this node.
289  */
290 void kdbus_node_init(struct kdbus_node *node, unsigned int type)
291 {
292         atomic_set(&node->refcnt, 1);
293         mutex_init(&node->lock);
294         node->id = 0;
295         node->type = type;
296         RB_CLEAR_NODE(&node->rb);
297         node->children = RB_ROOT;
298         init_waitqueue_head(&node->waitq);
299         atomic_set(&node->active, KDBUS_NODE_NEW);
300 }
301
302 /**
303  * kdbus_node_link() - link a node into the nodes system
304  * @node:       Pointer to the node to initialize
305  * @parent:     Pointer to a parent node, may be %NULL
306  * @name:       The name of the node (or NULL if root node)
307  *
308  * This links a node into the hierarchy. This must not be called multiple times.
309  * If @parent is NULL, the node becomes a new root node.
310  *
311  * This call will fail if @name is not unique across all its siblings or if no
312  * ID could be allocated. You must not activate a node if linking failed! It is
313  * safe to deactivate it, though.
314  *
315  * Once you linked a node, you must call kdbus_node_deactivate() before you drop
316  * the last reference (even if you never activate the node).
317  *
318  * Return: 0 on success. negative error otherwise.
319  */
320 int kdbus_node_link(struct kdbus_node *node, struct kdbus_node *parent,
321                     const char *name)
322 {
323         int ret;
324
325         if (WARN_ON(node->type != KDBUS_NODE_DOMAIN && !parent))
326                 return -EINVAL;
327
328         if (WARN_ON(parent && !name))
329                 return -EINVAL;
330
331         if (name) {
332                 node->name = kstrdup(name, GFP_KERNEL);
333                 if (!node->name)
334                         return -ENOMEM;
335
336                 node->hash = kdbus_node_name_hash(name);
337         }
338
339         ret = ida_simple_get(&kdbus_node_ida, 1, 0, GFP_KERNEL);
340         if (ret < 0)
341                 return ret;
342
343         node->id = ret;
344         ret = 0;
345
346         if (parent) {
347                 struct rb_node **n, *prev;
348
349                 if (!kdbus_node_acquire(parent))
350                         return -ESHUTDOWN;
351
352                 mutex_lock(&parent->lock);
353
354                 n = &parent->children.rb_node;
355                 prev = NULL;
356
357                 while (*n) {
358                         struct kdbus_node *pos;
359                         int result;
360
361                         pos = kdbus_node_from_rb(*n);
362                         prev = *n;
363                         result = kdbus_node_name_compare(node->hash,
364                                                          node->name,
365                                                          pos);
366                         if (result == 0) {
367                                 ret = -EEXIST;
368                                 goto exit_unlock;
369                         }
370
371                         if (result < 0)
372                                 n = &pos->rb.rb_left;
373                         else
374                                 n = &pos->rb.rb_right;
375                 }
376
377                 /* add new node and rebalance the tree */
378                 rb_link_node(&node->rb, prev, n);
379                 rb_insert_color(&node->rb, &parent->children);
380                 node->parent = kdbus_node_ref(parent);
381
382 exit_unlock:
383                 mutex_unlock(&parent->lock);
384                 kdbus_node_release(parent);
385         }
386
387         return ret;
388 }
389
390 /**
391  * kdbus_node_ref() - Acquire object reference
392  * @node:       node to acquire reference to (or NULL)
393  *
394  * This acquires a new reference to @node. You must already own a reference when
395  * calling this!
396  * If @node is NULL, this is a no-op.
397  *
398  * Return: @node is returned
399  */
400 struct kdbus_node *kdbus_node_ref(struct kdbus_node *node)
401 {
402         if (node)
403                 atomic_inc(&node->refcnt);
404         return node;
405 }
406
407 /**
408  * kdbus_node_unref() - Drop object reference
409  * @node:       node to drop reference to (or NULL)
410  *
411  * This drops an object reference to @node. You must not access the node if you
412  * no longer own a reference.
413  * If the ref-count drops to 0, the object will be destroyed (->free_cb will be
414  * called).
415  *
416  * If you linked or activated the node, you must deactivate the node before you
417  * drop your last reference! If you didn't link or activate the node, you can
418  * drop any reference you want.
419  *
420  * Note that this calls into ->free_cb() and thus _might_ sleep. The ->free_cb()
421  * callbacks must not acquire any outer locks, though. So you can safely drop
422  * references while holding locks.
423  *
424  * If @node is NULL, this is a no-op.
425  *
426  * Return: This always returns NULL
427  */
428 struct kdbus_node *kdbus_node_unref(struct kdbus_node *node)
429 {
430         if (node && atomic_dec_and_test(&node->refcnt)) {
431                 struct kdbus_node safe = *node;
432
433                 WARN_ON(atomic_read(&node->active) != KDBUS_NODE_DRAINED);
434                 WARN_ON(!RB_EMPTY_NODE(&node->rb));
435
436                 if (node->free_cb)
437                         node->free_cb(node);
438                 if (safe.id > 0)
439                         ida_simple_remove(&kdbus_node_ida, safe.id);
440
441                 kfree(safe.name);
442
443                 /*
444                  * kdbusfs relies on the parent to be available even after the
445                  * node was deactivated and unlinked. Therefore, we pin it
446                  * until a node is destroyed.
447                  */
448                 kdbus_node_unref(safe.parent);
449         }
450
451         return NULL;
452 }
453
454 /**
455  * kdbus_node_is_active() - test whether a node is active
456  * @node:       node to test
457  *
458  * This checks whether @node is active. That means, @node was linked and
459  * activated by the node owner and hasn't been deactivated, yet. If, and only
460  * if, a node is active, kdbus_node_acquire() will be able to acquire active
461  * references.
462  *
463  * Note that this function does not give any lifetime guarantees. After this
464  * call returns, the node might be deactivated immediately. Normally, what you
465  * want is to acquire a real active reference via kdbus_node_acquire().
466  *
467  * Return: true if @node is active, false otherwise
468  */
469 bool kdbus_node_is_active(struct kdbus_node *node)
470 {
471         return atomic_read(&node->active) >= 0;
472 }
473
474 /**
475  * kdbus_node_is_deactivated() - test whether a node was already deactivated
476  * @node:       node to test
477  *
478  * This checks whether kdbus_node_deactivate() was called on @node. Note that
479  * this might be true even if you never deactivated the node directly, but only
480  * one of its ancestors.
481  *
482  * Note that even if this returns 'false', the node might get deactivated
483  * immediately after the call returns.
484  *
485  * Return: true if @node was already deactivated, false if not
486  */
487 bool kdbus_node_is_deactivated(struct kdbus_node *node)
488 {
489         int v;
490
491         v = atomic_read(&node->active);
492         return v != KDBUS_NODE_NEW && v < 0;
493 }
494
495 /**
496  * kdbus_node_activate() - activate a node
497  * @node:       node to activate
498  *
499  * This marks @node as active if, and only if, the node wasn't activated nor
500  * deactivated, yet, and the parent is still active. Any but the first call to
501  * kdbus_node_activate() is a no-op.
502  * If you called kdbus_node_deactivate() before, then even the first call to
503  * kdbus_node_activate() will be a no-op.
504  *
505  * This call doesn't give any lifetime guarantees. The node might get
506  * deactivated immediately after this call returns. Or the parent might already
507  * be deactivated, which will make this call a no-op.
508  *
509  * If this call successfully activated a node, it will take an object reference
510  * to it. This reference is dropped after the node is deactivated. Therefore,
511  * the object owner can safely drop their reference to @node iff they know that
512  * its parent node will get deactivated at some point. Once the parent node is
513  * deactivated, it will deactivate all its child and thus drop this reference
514  * again.
515  *
516  * Return: True if this call successfully activated the node, otherwise false.
517  *         Note that this might return false, even if the node is still active
518  *         (eg., if you called this a second time).
519  */
520 bool kdbus_node_activate(struct kdbus_node *node)
521 {
522         bool res = false;
523
524         mutex_lock(&node->lock);
525         if (atomic_read(&node->active) == KDBUS_NODE_NEW) {
526                 atomic_sub(KDBUS_NODE_NEW, &node->active);
527                 /* activated nodes have ref +1 */
528                 kdbus_node_ref(node);
529                 res = true;
530         }
531         mutex_unlock(&node->lock);
532
533         return res;
534 }
535
536 /**
537  * kdbus_node_deactivate() - deactivate a node
538  * @node:       The node to deactivate.
539  *
540  * This function recursively deactivates this node and all its children. It
541  * returns only once all children and the node itself were recursively disabled
542  * (even if you call this function multiple times in parallel).
543  *
544  * It is safe to call this function on _any_ node that was initialized _any_
545  * number of times.
546  *
547  * This call may sleep, as it waits for all active references to be dropped.
548  */
549 void kdbus_node_deactivate(struct kdbus_node *node)
550 {
551         struct kdbus_node *pos, *child;
552         struct rb_node *rb;
553         int v_pre, v_post;
554
555         pos = node;
556
557         /*
558          * To avoid recursion, we perform back-tracking while deactivating
559          * nodes. For each node we enter, we first mark the active-counter as
560          * deactivated by adding BIAS. If the node as children, we set the first
561          * child as current position and start over. If the node has no
562          * children, we drain the node by waiting for all active refs to be
563          * dropped and then releasing the node.
564          *
565          * After the node is released, we set its parent as current position
566          * and start over. If the current position was the initial node, we're
567          * done.
568          *
569          * Note that this function can be called in parallel by multiple
570          * callers. We make sure that each node is only released once, and any
571          * racing caller will wait until the other thread fully released that
572          * node.
573          */
574
575         for (;;) {
576                 /*
577                  * Add BIAS to node->active to mark it as inactive. If it was
578                  * never active before, immediately mark it as RELEASE_INACTIVE
579                  * so we remember this state.
580                  * We cannot remember v_pre as we might iterate into the
581                  * children, overwriting v_pre, before we can release our node.
582                  */
583                 mutex_lock(&pos->lock);
584                 v_pre = atomic_read(&pos->active);
585                 if (v_pre >= 0)
586                         atomic_add_return(KDBUS_NODE_BIAS, &pos->active);
587                 else if (v_pre == KDBUS_NODE_NEW)
588                         atomic_set(&pos->active, KDBUS_NODE_RELEASE_DIRECT);
589                 mutex_unlock(&pos->lock);
590
591                 /* wait until all active references were dropped */
592                 wait_event(pos->waitq,
593                            atomic_read(&pos->active) <= KDBUS_NODE_BIAS);
594
595                 mutex_lock(&pos->lock);
596                 /* recurse into first child if any */
597                 rb = rb_first(&pos->children);
598                 if (rb) {
599                         child = kdbus_node_ref(kdbus_node_from_rb(rb));
600                         mutex_unlock(&pos->lock);
601                         pos = child;
602                         continue;
603                 }
604
605                 /* mark object as RELEASE */
606                 v_post = atomic_read(&pos->active);
607                 if (v_post == KDBUS_NODE_BIAS ||
608                     v_post == KDBUS_NODE_RELEASE_DIRECT)
609                         atomic_set(&pos->active, KDBUS_NODE_RELEASE);
610                 mutex_unlock(&pos->lock);
611
612                 /*
613                  * If this is the thread that marked the object as RELEASE, we
614                  * perform the actual release. Otherwise, we wait until the
615                  * release is done and the node is marked as DRAINED.
616                  */
617                 if (v_post == KDBUS_NODE_BIAS ||
618                     v_post == KDBUS_NODE_RELEASE_DIRECT) {
619                         if (pos->release_cb)
620                                 pos->release_cb(pos, v_post == KDBUS_NODE_BIAS);
621
622                         if (pos->parent) {
623                                 mutex_lock(&pos->parent->lock);
624                                 if (!RB_EMPTY_NODE(&pos->rb)) {
625                                         rb_erase(&pos->rb,
626                                                  &pos->parent->children);
627                                         RB_CLEAR_NODE(&pos->rb);
628                                 }
629                                 mutex_unlock(&pos->parent->lock);
630                         }
631
632                         /* mark as DRAINED */
633                         atomic_set(&pos->active, KDBUS_NODE_DRAINED);
634                         wake_up_all(&pos->waitq);
635
636                         /* drop VFS cache */
637                         kdbus_fs_flush(pos);
638
639                         /*
640                          * If the node was activated and someone subtracted BIAS
641                          * from it to deactivate it, we, and only us, are
642                          * responsible to release the extra ref-count that was
643                          * taken once in kdbus_node_activate().
644                          * If the node was never activated, no-one ever
645                          * subtracted BIAS, but instead skipped that state and
646                          * immediately went to NODE_RELEASE_DIRECT. In that case
647                          * we must not drop the reference.
648                          */
649                         if (v_post == KDBUS_NODE_BIAS)
650                                 kdbus_node_unref(pos);
651                 } else {
652                         /* wait until object is DRAINED */
653                         wait_event(pos->waitq,
654                             atomic_read(&pos->active) == KDBUS_NODE_DRAINED);
655                 }
656
657                 /*
658                  * We're done with the current node. Continue on its parent
659                  * again, which will try deactivating its next child, or itself
660                  * if no child is left.
661                  * If we've reached our initial node again, we are done and
662                  * can safely return.
663                  */
664                 if (pos == node)
665                         break;
666
667                 child = pos;
668                 pos = pos->parent;
669                 kdbus_node_unref(child);
670         }
671 }
672
673 /**
674  * kdbus_node_acquire() - Acquire an active ref on a node
675  * @node:       The node
676  *
677  * This acquires an active-reference to @node. This will only succeed if the
678  * node is active. You must release this active reference via
679  * kdbus_node_release() again.
680  *
681  * See the introduction to "active references" for more details.
682  *
683  * Return: %true if @node was non-NULL and active
684  */
685 bool kdbus_node_acquire(struct kdbus_node *node)
686 {
687         return node && atomic_inc_unless_negative(&node->active);
688 }
689
690 /**
691  * kdbus_node_release() - Release an active ref on a node
692  * @node:       The node
693  *
694  * This releases an active reference that was previously acquired via
695  * kdbus_node_acquire(). See kdbus_node_acquire() for details.
696  */
697 void kdbus_node_release(struct kdbus_node *node)
698 {
699         if (node && atomic_dec_return(&node->active) == KDBUS_NODE_BIAS)
700                 wake_up(&node->waitq);
701 }
702
703 /**
704  * kdbus_node_find_child() - Find child by name
705  * @node:       parent node to search through
706  * @name:       name of child node
707  *
708  * This searches through all children of @node for a child-node with name @name.
709  * If not found, or if the child is deactivated, NULL is returned. Otherwise,
710  * the child is acquired and a new reference is returned.
711  *
712  * If you're done with the child, you need to release it and drop your
713  * reference.
714  *
715  * This function does not acquire the parent node. However, if the parent was
716  * already deactivated, then kdbus_node_deactivate() will, at some point, also
717  * deactivate the child. Therefore, we can rely on the explicit ordering during
718  * deactivation.
719  *
720  * Return: Reference to acquired child node, or NULL if not found / not active.
721  */
722 struct kdbus_node *kdbus_node_find_child(struct kdbus_node *node,
723                                          const char *name)
724 {
725         struct kdbus_node *child;
726         struct rb_node *rb;
727         unsigned int hash;
728         int ret;
729
730         hash = kdbus_node_name_hash(name);
731
732         mutex_lock(&node->lock);
733         rb = node->children.rb_node;
734         while (rb) {
735                 child = kdbus_node_from_rb(rb);
736                 ret = kdbus_node_name_compare(hash, name, child);
737                 if (ret < 0)
738                         rb = rb->rb_left;
739                 else if (ret > 0)
740                         rb = rb->rb_right;
741                 else
742                         break;
743         }
744         if (rb && kdbus_node_acquire(child))
745                 kdbus_node_ref(child);
746         else
747                 child = NULL;
748         mutex_unlock(&node->lock);
749
750         return child;
751 }
752
753 static struct kdbus_node *node_find_closest_unlocked(struct kdbus_node *node,
754                                                      unsigned int hash,
755                                                      const char *name)
756 {
757         struct kdbus_node *n, *pos = NULL;
758         struct rb_node *rb;
759         int res;
760
761         /*
762          * Find the closest child with ``node->hash >= hash'', or, if @name is
763          * valid, ``node->name >= name'' (where '>=' is the lex. order).
764          */
765
766         rb = node->children.rb_node;
767         while (rb) {
768                 n = kdbus_node_from_rb(rb);
769
770                 if (name)
771                         res = kdbus_node_name_compare(hash, name, n);
772                 else
773                         res = hash - n->hash;
774
775                 if (res <= 0) {
776                         rb = rb->rb_left;
777                         pos = n;
778                 } else { /* ``hash > n->hash'', ``name > n->name'' */
779                         rb = rb->rb_right;
780                 }
781         }
782
783         return pos;
784 }
785
786 /**
787  * kdbus_node_find_closest() - Find closest child-match
788  * @node:       parent node to search through
789  * @hash:       hash value to find closest match for
790  *
791  * Find the closest child of @node with a hash greater than or equal to @hash.
792  * The closest match is the left-most child of @node with this property. Which
793  * means, it is the first child with that hash returned by
794  * kdbus_node_next_child(), if you'd iterate the whole parent node.
795  *
796  * Return: Reference to acquired child, or NULL if none found.
797  */
798 struct kdbus_node *kdbus_node_find_closest(struct kdbus_node *node,
799                                            unsigned int hash)
800 {
801         struct kdbus_node *child;
802         struct rb_node *rb;
803
804         mutex_lock(&node->lock);
805
806         child = node_find_closest_unlocked(node, hash, NULL);
807         while (child && !kdbus_node_acquire(child)) {
808                 rb = rb_next(&child->rb);
809                 if (rb)
810                         child = kdbus_node_from_rb(rb);
811                 else
812                         child = NULL;
813         }
814         kdbus_node_ref(child);
815
816         mutex_unlock(&node->lock);
817
818         return child;
819 }
820
821 /**
822  * kdbus_node_next_child() - Acquire next child
823  * @node:       parent node
824  * @prev:       previous child-node position or NULL
825  *
826  * This function returns a reference to the next active child of @node, after
827  * the passed position @prev. If @prev is NULL, a reference to the first active
828  * child is returned. If no more active children are found, NULL is returned.
829  *
830  * This function acquires the next child it returns. If you're done with the
831  * returned pointer, you need to release _and_ unref it.
832  *
833  * The passed in pointer @prev is not modified by this function, and it does
834  * *not* have to be active. If @prev was acquired via different means, or if it
835  * was unlinked from its parent before you pass it in, then this iterator will
836  * still return the next active child (it will have to search through the
837  * rb-tree based on the node-name, though).
838  * However, @prev must not be linked to a different parent than @node!
839  *
840  * Return: Reference to next acquired child, or NULL if at the end.
841  */
842 struct kdbus_node *kdbus_node_next_child(struct kdbus_node *node,
843                                          struct kdbus_node *prev)
844 {
845         struct kdbus_node *pos = NULL;
846         struct rb_node *rb;
847
848         mutex_lock(&node->lock);
849
850         if (!prev) {
851                 /*
852                  * New iteration; find first node in rb-tree and try to acquire
853                  * it. If we got it, directly return it as first element.
854                  * Otherwise, the loop below will find the next active node.
855                  */
856                 rb = rb_first(&node->children);
857                 if (!rb)
858                         goto exit;
859                 pos = kdbus_node_from_rb(rb);
860                 if (kdbus_node_acquire(pos))
861                         goto exit;
862         } else if (RB_EMPTY_NODE(&prev->rb)) {
863                 /*
864                  * The current iterator is no longer linked to the rb-tree. Use
865                  * its hash value and name to find the next _higher_ node and
866                  * acquire it. If we got it, return it as next element.
867                  * Otherwise, the loop below will find the next active node.
868                  */
869                 pos = node_find_closest_unlocked(node, prev->hash, prev->name);
870                 if (!pos)
871                         goto exit;
872                 if (kdbus_node_acquire(pos))
873                         goto exit;
874         } else {
875                 /*
876                  * The current iterator is still linked to the parent. Set it
877                  * as current position and use the loop below to find the next
878                  * active element.
879                  */
880                 pos = prev;
881         }
882
883         /* @pos was already returned or is inactive; find next active node */
884         do {
885                 rb = rb_next(&pos->rb);
886                 if (rb)
887                         pos = kdbus_node_from_rb(rb);
888                 else
889                         pos = NULL;
890         } while (pos && !kdbus_node_acquire(pos));
891
892 exit:
893         /* @pos is NULL or acquired. Take ref if non-NULL and return it */
894         kdbus_node_ref(pos);
895         mutex_unlock(&node->lock);
896         return pos;
897 }