1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/*
* Copyright (C) 2013-2015 Kay Sievers
* Copyright (C) 2013-2015 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
* Copyright (C) 2013-2015 Daniel Mack <daniel@zonque.org>
* Copyright (C) 2013-2015 David Herrmann <dh.herrmann@gmail.com>
* Copyright (C) 2013-2015 Linux Foundation
* Copyright (C) 2014-2015 Djalal Harouni <tixxdz@opendz.org>
*
* kdbus 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/audit.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/hashtable.h>
#include <linux/idr.h>
#include <linux/init.h>
#include <linux/math64.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/poll.h>
#include <linux/sched.h>
#include <linux/sizes.h>
#include <linux/slab.h>
#include <linux/syscalls.h>
#include <linux/uio.h>
#include "util.h"
#include "domain.h"
#include "connection.h"
#include "item.h"
#include "message.h"
#include "metadata.h"
#include "queue.h"
#include "reply.h"
/**
* kdbus_queue_init() - initialize data structure related to a queue
* @queue: The queue to initialize
*/
void kdbus_queue_init(struct kdbus_queue *queue)
{
INIT_LIST_HEAD(&queue->msg_list);
queue->msg_prio_queue = RB_ROOT;
}
/**
* kdbus_queue_peek() - Retrieves an entry from a queue
* @queue: The queue
* @priority: The minimum priority of the entry to peek
* @use_priority: Boolean flag whether or not to peek by priority
*
* Look for a entry in a queue, either by priority, or the oldest one (FIFO).
* The entry is not freed, put off the queue's lists or anything else.
*
* Return: the peeked queue entry on success, NULL if no suitable msg is found
*/
struct kdbus_queue_entry *kdbus_queue_peek(struct kdbus_queue *queue,
s64 priority, bool use_priority)
{
struct kdbus_queue_entry *e;
if (list_empty(&queue->msg_list))
return NULL;
if (use_priority) {
/* get next entry with highest priority */
e = rb_entry(queue->msg_prio_highest,
struct kdbus_queue_entry, prio_node);
/* no entry with the requested priority */
if (e->priority > priority)
return NULL;
} else {
/* ignore the priority, return the next entry in the entry */
e = list_first_entry(&queue->msg_list,
struct kdbus_queue_entry, entry);
}
return e;
}
static void kdbus_queue_entry_link(struct kdbus_queue_entry *entry)
{
struct kdbus_queue *queue = &entry->conn->queue;
struct rb_node **n, *pn = NULL;
bool highest = true;
lockdep_assert_held(&entry->conn->lock);
if (WARN_ON(!list_empty(&entry->entry)))
return;
/* sort into priority entry tree */
n = &queue->msg_prio_queue.rb_node;
while (*n) {
struct kdbus_queue_entry *e;
pn = *n;
e = rb_entry(pn, struct kdbus_queue_entry, prio_node);
/* existing node for this priority, add to its list */
if (likely(entry->priority == e->priority)) {
list_add_tail(&entry->prio_entry, &e->prio_entry);
goto prio_done;
}
if (entry->priority < e->priority) {
n = &pn->rb_left;
} else {
n = &pn->rb_right;
highest = false;
}
}
/* cache highest-priority entry */
if (highest)
queue->msg_prio_highest = &entry->prio_node;
/* new node for this priority */
rb_link_node(&entry->prio_node, pn, n);
rb_insert_color(&entry->prio_node, &queue->msg_prio_queue);
INIT_LIST_HEAD(&entry->prio_entry);
prio_done:
/* add to unsorted fifo list */
list_add_tail(&entry->entry, &queue->msg_list);
}
static void kdbus_queue_entry_unlink(struct kdbus_queue_entry *entry)
{
struct kdbus_queue *queue = &entry->conn->queue;
lockdep_assert_held(&entry->conn->lock);
if (list_empty(&entry->entry))
return;
list_del_init(&entry->entry);
if (list_empty(&entry->prio_entry)) {
/*
* Single entry for this priority, update cached
* highest-priority entry, remove the tree node.
*/
if (queue->msg_prio_highest == &entry->prio_node)
queue->msg_prio_highest = rb_next(&entry->prio_node);
rb_erase(&entry->prio_node, &queue->msg_prio_queue);
} else {
struct kdbus_queue_entry *q;
/*
* Multiple entries for this priority entry, get next one in
* the list. Update cached highest-priority entry, store the
* new one as the tree node.
*/
q = list_first_entry(&entry->prio_entry,
struct kdbus_queue_entry, prio_entry);
list_del(&entry->prio_entry);
if (queue->msg_prio_highest == &entry->prio_node)
queue->msg_prio_highest = &q->prio_node;
rb_replace_node(&entry->prio_node, &q->prio_node,
&queue->msg_prio_queue);
}
}
/**
* kdbus_queue_entry_new() - allocate a queue entry
* @src: source connection, or NULL
* @dst: destination connection
* @s: staging object carrying the message
*
* Allocates a queue entry based on a given msg and allocate space for
* the message payload and the requested metadata in the connection's pool.
* The entry is not actually added to the queue's lists at this point.
*
* Return: the allocated entry on success, or an ERR_PTR on failures.
*/
struct kdbus_queue_entry *kdbus_queue_entry_new(struct kdbus_conn *src,
struct kdbus_conn *dst,
struct kdbus_staging *s)
{
struct kdbus_queue_entry *entry;
int ret;
entry = kzalloc(sizeof(*entry), GFP_KERNEL);
if (!entry)
return ERR_PTR(-ENOMEM);
INIT_LIST_HEAD(&entry->entry);
entry->priority = s->msg->priority;
entry->conn = kdbus_conn_ref(dst);
entry->gaps = kdbus_gaps_ref(s->gaps);
entry->slice = kdbus_staging_emit(s, src, dst);
if (IS_ERR(entry->slice)) {
ret = PTR_ERR(entry->slice);
entry->slice = NULL;
goto error;
}
entry->user = src ? kdbus_user_ref(src->user) : NULL;
return entry;
error:
kdbus_queue_entry_free(entry);
return ERR_PTR(ret);
}
/**
* kdbus_queue_entry_free() - free resources of an entry
* @entry: The entry to free
*
* Removes resources allocated by a queue entry, along with the entry itself.
* Note that the entry's slice is not freed at this point.
*/
void kdbus_queue_entry_free(struct kdbus_queue_entry *entry)
{
if (!entry)
return;
lockdep_assert_held(&entry->conn->lock);
kdbus_queue_entry_unlink(entry);
kdbus_reply_unref(entry->reply);
if (entry->slice) {
kdbus_conn_quota_dec(entry->conn, entry->user,
kdbus_pool_slice_size(entry->slice),
entry->gaps ? entry->gaps->n_fds : 0);
kdbus_pool_slice_release(entry->slice);
}
kdbus_user_unref(entry->user);
kdbus_gaps_unref(entry->gaps);
kdbus_conn_unref(entry->conn);
kfree(entry);
}
/**
* kdbus_queue_entry_install() - install message components into the
* receiver's process
* @entry: The queue entry to install
* @return_flags: Pointer to store the return flags for userspace
* @install_fds: Whether or not to install associated file descriptors
*
* Return: 0 on success.
*/
int kdbus_queue_entry_install(struct kdbus_queue_entry *entry,
u64 *return_flags, bool install_fds)
{
bool incomplete_fds = false;
int ret;
lockdep_assert_held(&entry->conn->lock);
ret = kdbus_gaps_install(entry->gaps, entry->slice, &incomplete_fds);
if (ret < 0)
return ret;
if (incomplete_fds)
*return_flags |= KDBUS_RECV_RETURN_INCOMPLETE_FDS;
return 0;
}
/**
* kdbus_queue_entry_enqueue() - enqueue an entry
* @entry: entry to enqueue
* @reply: reply to link to this entry (or NULL if none)
*
* This enqueues an unqueued entry into the message queue of the linked
* connection. It also binds a reply object to the entry so we can remember it
* when the message is moved.
*
* Once this call returns (and the connection lock is released), this entry can
* be dequeued by the target connection. Note that the entry will not be removed
* from the queue until it is destroyed.
*/
void kdbus_queue_entry_enqueue(struct kdbus_queue_entry *entry,
struct kdbus_reply *reply)
{
lockdep_assert_held(&entry->conn->lock);
if (WARN_ON(entry->reply) || WARN_ON(!list_empty(&entry->entry)))
return;
entry->reply = kdbus_reply_ref(reply);
kdbus_queue_entry_link(entry);
}
/**
* kdbus_queue_entry_move() - move queue entry
* @e: queue entry to move
* @dst: destination connection to queue the entry on
*
* This moves a queue entry onto a different connection. It allocates a new
* slice on the target connection and copies the message over. If the copy
* succeeded, we move the entry from @src to @dst.
*
* On failure, the entry is left untouched.
*
* The queue entry must be queued right now, and after the call succeeds it will
* be queued on the destination, but no longer on the source.
*
* The caller must hold the connection lock of the source *and* destination.
*
* Return: 0 on success, negative error code on failure.
*/
int kdbus_queue_entry_move(struct kdbus_queue_entry *e,
struct kdbus_conn *dst)
{
struct kdbus_pool_slice *slice = NULL;
struct kdbus_conn *src = e->conn;
size_t size, fds;
int ret;
lockdep_assert_held(&src->lock);
lockdep_assert_held(&dst->lock);
if (WARN_ON(list_empty(&e->entry)))
return -EINVAL;
if (src == dst)
return 0;
size = kdbus_pool_slice_size(e->slice);
fds = e->gaps ? e->gaps->n_fds : 0;
ret = kdbus_conn_quota_inc(dst, e->user, size, fds);
if (ret < 0)
return ret;
slice = kdbus_pool_slice_alloc(dst->pool, size, true);
if (IS_ERR(slice)) {
ret = PTR_ERR(slice);
slice = NULL;
goto error;
}
ret = kdbus_pool_slice_copy(slice, e->slice);
if (ret < 0)
goto error;
kdbus_queue_entry_unlink(e);
kdbus_conn_quota_dec(src, e->user, size, fds);
kdbus_pool_slice_release(e->slice);
kdbus_conn_unref(e->conn);
e->slice = slice;
e->conn = kdbus_conn_ref(dst);
kdbus_queue_entry_link(e);
return 0;
error:
kdbus_pool_slice_release(slice);
kdbus_conn_quota_dec(dst, e->user, size, fds);
return ret;
}
|