blob: c103e34f870568ffb8a4ebf9b31dc0ce9703d3a5 [file] [log] [blame]
David Howellsc73be612020-01-14 17:07:11 +00001// SPDX-License-Identifier: GPL-2.0
2/* Watch queue and general notification mechanism, built on pipes
3 *
4 * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 *
7 * See Documentation/watch_queue.rst
8 */
9
10#define pr_fmt(fmt) "watchq: " fmt
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/sched.h>
14#include <linux/slab.h>
15#include <linux/printk.h>
16#include <linux/miscdevice.h>
17#include <linux/fs.h>
18#include <linux/mm.h>
19#include <linux/pagemap.h>
20#include <linux/poll.h>
21#include <linux/uaccess.h>
22#include <linux/vmalloc.h>
23#include <linux/file.h>
24#include <linux/security.h>
25#include <linux/cred.h>
26#include <linux/sched/signal.h>
27#include <linux/watch_queue.h>
28#include <linux/pipe_fs_i.h>
29
30MODULE_DESCRIPTION("Watch queue");
31MODULE_AUTHOR("Red Hat, Inc.");
32MODULE_LICENSE("GPL");
33
34#define WATCH_QUEUE_NOTE_SIZE 128
35#define WATCH_QUEUE_NOTES_PER_PAGE (PAGE_SIZE / WATCH_QUEUE_NOTE_SIZE)
36
37static void watch_queue_pipe_buf_release(struct pipe_inode_info *pipe,
38 struct pipe_buffer *buf)
39{
40 struct watch_queue *wqueue = (struct watch_queue *)buf->private;
41 struct page *page;
42 unsigned int bit;
43
44 /* We need to work out which note within the page this refers to, but
45 * the note might have been maximum size, so merely ANDing the offset
46 * off doesn't work. OTOH, the note must've been more than zero size.
47 */
48 bit = buf->offset + buf->len;
49 if ((bit & (WATCH_QUEUE_NOTE_SIZE - 1)) == 0)
50 bit -= WATCH_QUEUE_NOTE_SIZE;
51 bit /= WATCH_QUEUE_NOTE_SIZE;
52
53 page = buf->page;
54 bit += page->index;
55
56 set_bit(bit, wqueue->notes_bitmap);
57}
58
59static int watch_queue_pipe_buf_steal(struct pipe_inode_info *pipe,
60 struct pipe_buffer *buf)
61{
62 return -1; /* No. */
63}
64
65/* New data written to a pipe may be appended to a buffer with this type. */
66static const struct pipe_buf_operations watch_queue_pipe_buf_ops = {
67 .confirm = generic_pipe_buf_confirm,
68 .release = watch_queue_pipe_buf_release,
69 .steal = watch_queue_pipe_buf_steal,
70 .get = generic_pipe_buf_get,
71};
72
73/*
74 * Post a notification to a watch queue.
75 */
76static bool post_one_notification(struct watch_queue *wqueue,
77 struct watch_notification *n)
78{
79 void *p;
80 struct pipe_inode_info *pipe = wqueue->pipe;
81 struct pipe_buffer *buf;
82 struct page *page;
83 unsigned int head, tail, mask, note, offset, len;
84 bool done = false;
85
86 if (!pipe)
87 return false;
88
89 spin_lock_irq(&pipe->rd_wait.lock);
90
91 if (wqueue->defunct)
92 goto out;
93
94 mask = pipe->ring_size - 1;
95 head = pipe->head;
96 tail = pipe->tail;
97 if (pipe_full(head, tail, pipe->ring_size))
98 goto lost;
99
100 note = find_first_bit(wqueue->notes_bitmap, wqueue->nr_notes);
101 if (note >= wqueue->nr_notes)
102 goto lost;
103
104 page = wqueue->notes[note / WATCH_QUEUE_NOTES_PER_PAGE];
105 offset = note % WATCH_QUEUE_NOTES_PER_PAGE * WATCH_QUEUE_NOTE_SIZE;
106 get_page(page);
107 len = n->info & WATCH_INFO_LENGTH;
108 p = kmap_atomic(page);
109 memcpy(p + offset, n, len);
110 kunmap_atomic(p);
111
112 buf = &pipe->bufs[head & mask];
113 buf->page = page;
114 buf->private = (unsigned long)wqueue;
115 buf->ops = &watch_queue_pipe_buf_ops;
116 buf->offset = offset;
117 buf->len = len;
118 buf->flags = 0;
119 pipe->head = head + 1;
120
121 if (!test_and_clear_bit(note, wqueue->notes_bitmap)) {
122 spin_unlock_irq(&pipe->rd_wait.lock);
123 BUG();
124 }
125 wake_up_interruptible_sync_poll_locked(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
126 done = true;
127
128out:
129 spin_unlock_irq(&pipe->rd_wait.lock);
130 if (done)
131 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
132 return done;
133
134lost:
135 goto out;
136}
137
138/*
139 * Apply filter rules to a notification.
140 */
141static bool filter_watch_notification(const struct watch_filter *wf,
142 const struct watch_notification *n)
143{
144 const struct watch_type_filter *wt;
145 unsigned int st_bits = sizeof(wt->subtype_filter[0]) * 8;
146 unsigned int st_index = n->subtype / st_bits;
147 unsigned int st_bit = 1U << (n->subtype % st_bits);
148 int i;
149
150 if (!test_bit(n->type, wf->type_filter))
151 return false;
152
153 for (i = 0; i < wf->nr_filters; i++) {
154 wt = &wf->filters[i];
155 if (n->type == wt->type &&
156 (wt->subtype_filter[st_index] & st_bit) &&
157 (n->info & wt->info_mask) == wt->info_filter)
158 return true;
159 }
160
161 return false; /* If there is a filter, the default is to reject. */
162}
163
164/**
165 * __post_watch_notification - Post an event notification
166 * @wlist: The watch list to post the event to.
167 * @n: The notification record to post.
168 * @cred: The creds of the process that triggered the notification.
169 * @id: The ID to match on the watch.
170 *
171 * Post a notification of an event into a set of watch queues and let the users
172 * know.
173 *
174 * The size of the notification should be set in n->info & WATCH_INFO_LENGTH and
175 * should be in units of sizeof(*n).
176 */
177void __post_watch_notification(struct watch_list *wlist,
178 struct watch_notification *n,
179 const struct cred *cred,
180 u64 id)
181{
182 const struct watch_filter *wf;
183 struct watch_queue *wqueue;
184 struct watch *watch;
185
186 if (((n->info & WATCH_INFO_LENGTH) >> WATCH_INFO_LENGTH__SHIFT) == 0) {
187 WARN_ON(1);
188 return;
189 }
190
191 rcu_read_lock();
192
193 hlist_for_each_entry_rcu(watch, &wlist->watchers, list_node) {
194 if (watch->id != id)
195 continue;
196 n->info &= ~WATCH_INFO_ID;
197 n->info |= watch->info_id;
198
199 wqueue = rcu_dereference(watch->queue);
200 wf = rcu_dereference(wqueue->filter);
201 if (wf && !filter_watch_notification(wf, n))
202 continue;
203
204 if (security_post_notification(watch->cred, cred, n) < 0)
205 continue;
206
207 post_one_notification(wqueue, n);
208 }
209
210 rcu_read_unlock();
211}
212EXPORT_SYMBOL(__post_watch_notification);
213
214/*
215 * Allocate sufficient pages to preallocation for the requested number of
216 * notifications.
217 */
218long watch_queue_set_size(struct pipe_inode_info *pipe, unsigned int nr_notes)
219{
220 struct watch_queue *wqueue = pipe->watch_queue;
221 struct page **pages;
222 unsigned long *bitmap;
223 unsigned long user_bufs;
224 unsigned int bmsize;
225 int ret, i, nr_pages;
226
227 if (!wqueue)
228 return -ENODEV;
229 if (wqueue->notes)
230 return -EBUSY;
231
232 if (nr_notes < 1 ||
233 nr_notes > 512) /* TODO: choose a better hard limit */
234 return -EINVAL;
235
236 nr_pages = (nr_notes + WATCH_QUEUE_NOTES_PER_PAGE - 1);
237 nr_pages /= WATCH_QUEUE_NOTES_PER_PAGE;
238 user_bufs = account_pipe_buffers(pipe->user, pipe->nr_accounted, nr_pages);
239
240 if (nr_pages > pipe->max_usage &&
241 (too_many_pipe_buffers_hard(user_bufs) ||
242 too_many_pipe_buffers_soft(user_bufs)) &&
243 pipe_is_unprivileged_user()) {
244 ret = -EPERM;
245 goto error;
246 }
247
248 ret = pipe_resize_ring(pipe, nr_notes);
249 if (ret < 0)
250 goto error;
251
252 pages = kcalloc(sizeof(struct page *), nr_pages, GFP_KERNEL);
253 if (!pages)
254 goto error;
255
256 for (i = 0; i < nr_pages; i++) {
257 pages[i] = alloc_page(GFP_KERNEL);
258 if (!pages[i])
259 goto error_p;
260 pages[i]->index = i * WATCH_QUEUE_NOTES_PER_PAGE;
261 }
262
263 bmsize = (nr_notes + BITS_PER_LONG - 1) / BITS_PER_LONG;
264 bmsize *= sizeof(unsigned long);
265 bitmap = kmalloc(bmsize, GFP_KERNEL);
266 if (!bitmap)
267 goto error_p;
268
269 memset(bitmap, 0xff, bmsize);
270 wqueue->notes = pages;
271 wqueue->notes_bitmap = bitmap;
272 wqueue->nr_pages = nr_pages;
273 wqueue->nr_notes = nr_pages * WATCH_QUEUE_NOTES_PER_PAGE;
274 return 0;
275
276error_p:
277 for (i = 0; i < nr_pages; i++)
278 __free_page(pages[i]);
279 kfree(pages);
280error:
281 (void) account_pipe_buffers(pipe->user, nr_pages, pipe->nr_accounted);
282 return ret;
283}
284
285/*
286 * Set the filter on a watch queue.
287 */
288long watch_queue_set_filter(struct pipe_inode_info *pipe,
289 struct watch_notification_filter __user *_filter)
290{
291 struct watch_notification_type_filter *tf;
292 struct watch_notification_filter filter;
293 struct watch_type_filter *q;
294 struct watch_filter *wfilter;
295 struct watch_queue *wqueue = pipe->watch_queue;
296 int ret, nr_filter = 0, i;
297
298 if (!wqueue)
299 return -ENODEV;
300
301 if (!_filter) {
302 /* Remove the old filter */
303 wfilter = NULL;
304 goto set;
305 }
306
307 /* Grab the user's filter specification */
308 if (copy_from_user(&filter, _filter, sizeof(filter)) != 0)
309 return -EFAULT;
310 if (filter.nr_filters == 0 ||
311 filter.nr_filters > 16 ||
312 filter.__reserved != 0)
313 return -EINVAL;
314
315 tf = memdup_user(_filter->filters, filter.nr_filters * sizeof(*tf));
316 if (IS_ERR(tf))
317 return PTR_ERR(tf);
318
319 ret = -EINVAL;
320 for (i = 0; i < filter.nr_filters; i++) {
321 if ((tf[i].info_filter & ~tf[i].info_mask) ||
322 tf[i].info_mask & WATCH_INFO_LENGTH)
323 goto err_filter;
324 /* Ignore any unknown types */
325 if (tf[i].type >= sizeof(wfilter->type_filter) * 8)
326 continue;
327 nr_filter++;
328 }
329
330 /* Now we need to build the internal filter from only the relevant
331 * user-specified filters.
332 */
333 ret = -ENOMEM;
334 wfilter = kzalloc(struct_size(wfilter, filters, nr_filter), GFP_KERNEL);
335 if (!wfilter)
336 goto err_filter;
337 wfilter->nr_filters = nr_filter;
338
339 q = wfilter->filters;
340 for (i = 0; i < filter.nr_filters; i++) {
341 if (tf[i].type >= sizeof(wfilter->type_filter) * BITS_PER_LONG)
342 continue;
343
344 q->type = tf[i].type;
345 q->info_filter = tf[i].info_filter;
346 q->info_mask = tf[i].info_mask;
347 q->subtype_filter[0] = tf[i].subtype_filter[0];
348 __set_bit(q->type, wfilter->type_filter);
349 q++;
350 }
351
352 kfree(tf);
353set:
354 pipe_lock(pipe);
355 wfilter = rcu_replace_pointer(wqueue->filter, wfilter,
356 lockdep_is_held(&pipe->mutex));
357 pipe_unlock(pipe);
358 if (wfilter)
359 kfree_rcu(wfilter, rcu);
360 return 0;
361
362err_filter:
363 kfree(tf);
364 return ret;
365}
366
367static void __put_watch_queue(struct kref *kref)
368{
369 struct watch_queue *wqueue =
370 container_of(kref, struct watch_queue, usage);
371 struct watch_filter *wfilter;
372 int i;
373
374 for (i = 0; i < wqueue->nr_pages; i++)
375 __free_page(wqueue->notes[i]);
376
377 wfilter = rcu_access_pointer(wqueue->filter);
378 if (wfilter)
379 kfree_rcu(wfilter, rcu);
380 kfree_rcu(wqueue, rcu);
381}
382
383/**
384 * put_watch_queue - Dispose of a ref on a watchqueue.
385 * @wqueue: The watch queue to unref.
386 */
387void put_watch_queue(struct watch_queue *wqueue)
388{
389 kref_put(&wqueue->usage, __put_watch_queue);
390}
391EXPORT_SYMBOL(put_watch_queue);
392
393static void free_watch(struct rcu_head *rcu)
394{
395 struct watch *watch = container_of(rcu, struct watch, rcu);
396
397 put_watch_queue(rcu_access_pointer(watch->queue));
398 put_cred(watch->cred);
399}
400
401static void __put_watch(struct kref *kref)
402{
403 struct watch *watch = container_of(kref, struct watch, usage);
404
405 call_rcu(&watch->rcu, free_watch);
406}
407
408/*
409 * Discard a watch.
410 */
411static void put_watch(struct watch *watch)
412{
413 kref_put(&watch->usage, __put_watch);
414}
415
416/**
417 * init_watch_queue - Initialise a watch
418 * @watch: The watch to initialise.
419 * @wqueue: The queue to assign.
420 *
421 * Initialise a watch and set the watch queue.
422 */
423void init_watch(struct watch *watch, struct watch_queue *wqueue)
424{
425 kref_init(&watch->usage);
426 INIT_HLIST_NODE(&watch->list_node);
427 INIT_HLIST_NODE(&watch->queue_node);
428 rcu_assign_pointer(watch->queue, wqueue);
429}
430
431/**
432 * add_watch_to_object - Add a watch on an object to a watch list
433 * @watch: The watch to add
434 * @wlist: The watch list to add to
435 *
436 * @watch->queue must have been set to point to the queue to post notifications
437 * to and the watch list of the object to be watched. @watch->cred must also
438 * have been set to the appropriate credentials and a ref taken on them.
439 *
440 * The caller must pin the queue and the list both and must hold the list
441 * locked against racing watch additions/removals.
442 */
443int add_watch_to_object(struct watch *watch, struct watch_list *wlist)
444{
445 struct watch_queue *wqueue = rcu_access_pointer(watch->queue);
446 struct watch *w;
447
448 hlist_for_each_entry(w, &wlist->watchers, list_node) {
449 struct watch_queue *wq = rcu_access_pointer(w->queue);
450 if (wqueue == wq && watch->id == w->id)
451 return -EBUSY;
452 }
453
454 watch->cred = get_current_cred();
455 rcu_assign_pointer(watch->watch_list, wlist);
456
457 spin_lock_bh(&wqueue->lock);
458 kref_get(&wqueue->usage);
459 kref_get(&watch->usage);
460 hlist_add_head(&watch->queue_node, &wqueue->watches);
461 spin_unlock_bh(&wqueue->lock);
462
463 hlist_add_head(&watch->list_node, &wlist->watchers);
464 return 0;
465}
466EXPORT_SYMBOL(add_watch_to_object);
467
468/**
469 * remove_watch_from_object - Remove a watch or all watches from an object.
470 * @wlist: The watch list to remove from
471 * @wq: The watch queue of interest (ignored if @all is true)
472 * @id: The ID of the watch to remove (ignored if @all is true)
473 * @all: True to remove all objects
474 *
475 * Remove a specific watch or all watches from an object. A notification is
476 * sent to the watcher to tell them that this happened.
477 */
478int remove_watch_from_object(struct watch_list *wlist, struct watch_queue *wq,
479 u64 id, bool all)
480{
481 struct watch_notification_removal n;
482 struct watch_queue *wqueue;
483 struct watch *watch;
484 int ret = -EBADSLT;
485
486 rcu_read_lock();
487
488again:
489 spin_lock(&wlist->lock);
490 hlist_for_each_entry(watch, &wlist->watchers, list_node) {
491 if (all ||
492 (watch->id == id && rcu_access_pointer(watch->queue) == wq))
493 goto found;
494 }
495 spin_unlock(&wlist->lock);
496 goto out;
497
498found:
499 ret = 0;
500 hlist_del_init_rcu(&watch->list_node);
501 rcu_assign_pointer(watch->watch_list, NULL);
502 spin_unlock(&wlist->lock);
503
504 /* We now own the reference on watch that used to belong to wlist. */
505
506 n.watch.type = WATCH_TYPE_META;
507 n.watch.subtype = WATCH_META_REMOVAL_NOTIFICATION;
508 n.watch.info = watch->info_id | watch_sizeof(n.watch);
509 n.id = id;
510 if (id != 0)
511 n.watch.info = watch->info_id | watch_sizeof(n);
512
513 wqueue = rcu_dereference(watch->queue);
514
515 /* We don't need the watch list lock for the next bit as RCU is
516 * protecting *wqueue from deallocation.
517 */
518 if (wqueue) {
519 post_one_notification(wqueue, &n.watch);
520
521 spin_lock_bh(&wqueue->lock);
522
523 if (!hlist_unhashed(&watch->queue_node)) {
524 hlist_del_init_rcu(&watch->queue_node);
525 put_watch(watch);
526 }
527
528 spin_unlock_bh(&wqueue->lock);
529 }
530
531 if (wlist->release_watch) {
532 void (*release_watch)(struct watch *);
533
534 release_watch = wlist->release_watch;
535 rcu_read_unlock();
536 (*release_watch)(watch);
537 rcu_read_lock();
538 }
539 put_watch(watch);
540
541 if (all && !hlist_empty(&wlist->watchers))
542 goto again;
543out:
544 rcu_read_unlock();
545 return ret;
546}
547EXPORT_SYMBOL(remove_watch_from_object);
548
549/*
550 * Remove all the watches that are contributory to a queue. This has the
551 * potential to race with removal of the watches by the destruction of the
552 * objects being watched or with the distribution of notifications.
553 */
554void watch_queue_clear(struct watch_queue *wqueue)
555{
556 struct watch_list *wlist;
557 struct watch *watch;
558 bool release;
559
560 rcu_read_lock();
561 spin_lock_bh(&wqueue->lock);
562
563 /* Prevent new additions and prevent notifications from happening */
564 wqueue->defunct = true;
565
566 while (!hlist_empty(&wqueue->watches)) {
567 watch = hlist_entry(wqueue->watches.first, struct watch, queue_node);
568 hlist_del_init_rcu(&watch->queue_node);
569 /* We now own a ref on the watch. */
570 spin_unlock_bh(&wqueue->lock);
571
572 /* We can't do the next bit under the queue lock as we need to
573 * get the list lock - which would cause a deadlock if someone
574 * was removing from the opposite direction at the same time or
575 * posting a notification.
576 */
577 wlist = rcu_dereference(watch->watch_list);
578 if (wlist) {
579 void (*release_watch)(struct watch *);
580
581 spin_lock(&wlist->lock);
582
583 release = !hlist_unhashed(&watch->list_node);
584 if (release) {
585 hlist_del_init_rcu(&watch->list_node);
586 rcu_assign_pointer(watch->watch_list, NULL);
587
588 /* We now own a second ref on the watch. */
589 }
590
591 release_watch = wlist->release_watch;
592 spin_unlock(&wlist->lock);
593
594 if (release) {
595 if (release_watch) {
596 rcu_read_unlock();
597 /* This might need to call dput(), so
598 * we have to drop all the locks.
599 */
600 (*release_watch)(watch);
601 rcu_read_lock();
602 }
603 put_watch(watch);
604 }
605 }
606
607 put_watch(watch);
608 spin_lock_bh(&wqueue->lock);
609 }
610
611 spin_unlock_bh(&wqueue->lock);
612 rcu_read_unlock();
613}
614
615/**
616 * get_watch_queue - Get a watch queue from its file descriptor.
617 * @fd: The fd to query.
618 */
619struct watch_queue *get_watch_queue(int fd)
620{
621 struct pipe_inode_info *pipe;
622 struct watch_queue *wqueue = ERR_PTR(-EINVAL);
623 struct fd f;
624
625 f = fdget(fd);
626 if (f.file) {
627 pipe = get_pipe_info(f.file, false);
628 if (pipe && pipe->watch_queue) {
629 wqueue = pipe->watch_queue;
630 kref_get(&wqueue->usage);
631 }
632 fdput(f);
633 }
634
635 return wqueue;
636}
637EXPORT_SYMBOL(get_watch_queue);
638
639/*
640 * Initialise a watch queue
641 */
642int watch_queue_init(struct pipe_inode_info *pipe)
643{
644 struct watch_queue *wqueue;
645
646 wqueue = kzalloc(sizeof(*wqueue), GFP_KERNEL);
647 if (!wqueue)
648 return -ENOMEM;
649
650 wqueue->pipe = pipe;
651 kref_init(&wqueue->usage);
652 spin_lock_init(&wqueue->lock);
653 INIT_HLIST_HEAD(&wqueue->watches);
654
655 pipe->watch_queue = wqueue;
656 return 0;
657}