1 // SPDX-License-Identifier: GPL-2.0
2 /* Watch queue and general notification mechanism, built on pipes
4 * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * See Documentation/core-api/watch_queue.rst
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>
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>
30 MODULE_DESCRIPTION("Watch queue");
31 MODULE_AUTHOR("Red Hat, Inc.");
32 MODULE_LICENSE("GPL");
34 #define WATCH_QUEUE_NOTE_SIZE 128
35 #define WATCH_QUEUE_NOTES_PER_PAGE (PAGE_SIZE / WATCH_QUEUE_NOTE_SIZE)
38 * This must be called under the RCU read-lock, which makes
39 * sure that the wqueue still exists. It can then take the lock,
40 * and check that the wqueue hasn't been destroyed, which in
41 * turn makes sure that the notification pipe still exists.
43 static inline bool lock_wqueue(struct watch_queue *wqueue)
45 spin_lock_bh(&wqueue->lock);
46 if (unlikely(wqueue->defunct)) {
47 spin_unlock_bh(&wqueue->lock);
53 static inline void unlock_wqueue(struct watch_queue *wqueue)
55 spin_unlock_bh(&wqueue->lock);
58 static void watch_queue_pipe_buf_release(struct pipe_inode_info *pipe,
59 struct pipe_buffer *buf)
61 struct watch_queue *wqueue = (struct watch_queue *)buf->private;
65 /* We need to work out which note within the page this refers to, but
66 * the note might have been maximum size, so merely ANDing the offset
67 * off doesn't work. OTOH, the note must've been more than zero size.
69 bit = buf->offset + buf->len;
70 if ((bit & (WATCH_QUEUE_NOTE_SIZE - 1)) == 0)
71 bit -= WATCH_QUEUE_NOTE_SIZE;
72 bit /= WATCH_QUEUE_NOTE_SIZE;
77 set_bit(bit, wqueue->notes_bitmap);
78 generic_pipe_buf_release(pipe, buf);
81 // No try_steal function => no stealing
82 #define watch_queue_pipe_buf_try_steal NULL
84 /* New data written to a pipe may be appended to a buffer with this type. */
85 static const struct pipe_buf_operations watch_queue_pipe_buf_ops = {
86 .release = watch_queue_pipe_buf_release,
87 .try_steal = watch_queue_pipe_buf_try_steal,
88 .get = generic_pipe_buf_get,
92 * Post a notification to a watch queue.
94 * Must be called with the RCU lock for reading, and the
95 * watch_queue lock held, which guarantees that the pipe
96 * hasn't been released.
98 static bool post_one_notification(struct watch_queue *wqueue,
99 struct watch_notification *n)
102 struct pipe_inode_info *pipe = wqueue->pipe;
103 struct pipe_buffer *buf;
105 unsigned int head, tail, mask, note, offset, len;
111 spin_lock_irq(&pipe->rd_wait.lock);
113 mask = pipe->ring_size - 1;
116 if (pipe_full(head, tail, pipe->ring_size))
119 note = find_first_bit(wqueue->notes_bitmap, wqueue->nr_notes);
120 if (note >= wqueue->nr_notes)
123 page = wqueue->notes[note / WATCH_QUEUE_NOTES_PER_PAGE];
124 offset = note % WATCH_QUEUE_NOTES_PER_PAGE * WATCH_QUEUE_NOTE_SIZE;
126 len = n->info & WATCH_INFO_LENGTH;
127 p = kmap_atomic(page);
128 memcpy(p + offset, n, len);
131 buf = &pipe->bufs[head & mask];
133 buf->private = (unsigned long)wqueue;
134 buf->ops = &watch_queue_pipe_buf_ops;
135 buf->offset = offset;
137 buf->flags = PIPE_BUF_FLAG_WHOLE;
138 smp_store_release(&pipe->head, head + 1); /* vs pipe_read() */
140 if (!test_and_clear_bit(note, wqueue->notes_bitmap)) {
141 spin_unlock_irq(&pipe->rd_wait.lock);
144 wake_up_interruptible_sync_poll_locked(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
148 spin_unlock_irq(&pipe->rd_wait.lock);
150 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
154 buf = &pipe->bufs[(head - 1) & mask];
155 buf->flags |= PIPE_BUF_FLAG_LOSS;
160 * Apply filter rules to a notification.
162 static bool filter_watch_notification(const struct watch_filter *wf,
163 const struct watch_notification *n)
165 const struct watch_type_filter *wt;
166 unsigned int st_bits = sizeof(wt->subtype_filter[0]) * 8;
167 unsigned int st_index = n->subtype / st_bits;
168 unsigned int st_bit = 1U << (n->subtype % st_bits);
171 if (!test_bit(n->type, wf->type_filter))
174 for (i = 0; i < wf->nr_filters; i++) {
175 wt = &wf->filters[i];
176 if (n->type == wt->type &&
177 (wt->subtype_filter[st_index] & st_bit) &&
178 (n->info & wt->info_mask) == wt->info_filter)
182 return false; /* If there is a filter, the default is to reject. */
186 * __post_watch_notification - Post an event notification
187 * @wlist: The watch list to post the event to.
188 * @n: The notification record to post.
189 * @cred: The creds of the process that triggered the notification.
190 * @id: The ID to match on the watch.
192 * Post a notification of an event into a set of watch queues and let the users
195 * The size of the notification should be set in n->info & WATCH_INFO_LENGTH and
196 * should be in units of sizeof(*n).
198 void __post_watch_notification(struct watch_list *wlist,
199 struct watch_notification *n,
200 const struct cred *cred,
203 const struct watch_filter *wf;
204 struct watch_queue *wqueue;
207 if (((n->info & WATCH_INFO_LENGTH) >> WATCH_INFO_LENGTH__SHIFT) == 0) {
214 hlist_for_each_entry_rcu(watch, &wlist->watchers, list_node) {
217 n->info &= ~WATCH_INFO_ID;
218 n->info |= watch->info_id;
220 wqueue = rcu_dereference(watch->queue);
221 wf = rcu_dereference(wqueue->filter);
222 if (wf && !filter_watch_notification(wf, n))
225 if (security_post_notification(watch->cred, cred, n) < 0)
228 if (lock_wqueue(wqueue)) {
229 post_one_notification(wqueue, n);
230 unlock_wqueue(wqueue);
236 EXPORT_SYMBOL(__post_watch_notification);
239 * Allocate sufficient pages to preallocation for the requested number of
242 long watch_queue_set_size(struct pipe_inode_info *pipe, unsigned int nr_notes)
244 struct watch_queue *wqueue = pipe->watch_queue;
246 unsigned long *bitmap;
247 unsigned long user_bufs;
248 int ret, i, nr_pages;
256 nr_notes > 512) /* TODO: choose a better hard limit */
259 nr_pages = (nr_notes + WATCH_QUEUE_NOTES_PER_PAGE - 1);
260 nr_pages /= WATCH_QUEUE_NOTES_PER_PAGE;
261 user_bufs = account_pipe_buffers(pipe->user, pipe->nr_accounted, nr_pages);
263 if (nr_pages > pipe->max_usage &&
264 (too_many_pipe_buffers_hard(user_bufs) ||
265 too_many_pipe_buffers_soft(user_bufs)) &&
266 pipe_is_unprivileged_user()) {
271 nr_notes = nr_pages * WATCH_QUEUE_NOTES_PER_PAGE;
272 ret = pipe_resize_ring(pipe, roundup_pow_of_two(nr_notes));
277 pages = kcalloc(sizeof(struct page *), nr_pages, GFP_KERNEL);
281 for (i = 0; i < nr_pages; i++) {
282 pages[i] = alloc_page(GFP_KERNEL);
285 pages[i]->index = i * WATCH_QUEUE_NOTES_PER_PAGE;
288 bitmap = bitmap_alloc(nr_notes, GFP_KERNEL);
292 bitmap_fill(bitmap, nr_notes);
293 wqueue->notes = pages;
294 wqueue->notes_bitmap = bitmap;
295 wqueue->nr_pages = nr_pages;
296 wqueue->nr_notes = nr_notes;
301 __free_page(pages[i]);
304 (void) account_pipe_buffers(pipe->user, nr_pages, pipe->nr_accounted);
309 * Set the filter on a watch queue.
311 long watch_queue_set_filter(struct pipe_inode_info *pipe,
312 struct watch_notification_filter __user *_filter)
314 struct watch_notification_type_filter *tf;
315 struct watch_notification_filter filter;
316 struct watch_type_filter *q;
317 struct watch_filter *wfilter;
318 struct watch_queue *wqueue = pipe->watch_queue;
319 int ret, nr_filter = 0, i;
325 /* Remove the old filter */
330 /* Grab the user's filter specification */
331 if (copy_from_user(&filter, _filter, sizeof(filter)) != 0)
333 if (filter.nr_filters == 0 ||
334 filter.nr_filters > 16 ||
335 filter.__reserved != 0)
338 tf = memdup_user(_filter->filters, filter.nr_filters * sizeof(*tf));
343 for (i = 0; i < filter.nr_filters; i++) {
344 if ((tf[i].info_filter & ~tf[i].info_mask) ||
345 tf[i].info_mask & WATCH_INFO_LENGTH)
347 /* Ignore any unknown types */
348 if (tf[i].type >= WATCH_TYPE__NR)
353 /* Now we need to build the internal filter from only the relevant
354 * user-specified filters.
357 wfilter = kzalloc(struct_size(wfilter, filters, nr_filter), GFP_KERNEL);
360 wfilter->nr_filters = nr_filter;
362 q = wfilter->filters;
363 for (i = 0; i < filter.nr_filters; i++) {
364 if (tf[i].type >= WATCH_TYPE__NR)
367 q->type = tf[i].type;
368 q->info_filter = tf[i].info_filter;
369 q->info_mask = tf[i].info_mask;
370 q->subtype_filter[0] = tf[i].subtype_filter[0];
371 __set_bit(q->type, wfilter->type_filter);
378 wfilter = rcu_replace_pointer(wqueue->filter, wfilter,
379 lockdep_is_held(&pipe->mutex));
382 kfree_rcu(wfilter, rcu);
390 static void __put_watch_queue(struct kref *kref)
392 struct watch_queue *wqueue =
393 container_of(kref, struct watch_queue, usage);
394 struct watch_filter *wfilter;
397 for (i = 0; i < wqueue->nr_pages; i++)
398 __free_page(wqueue->notes[i]);
399 kfree(wqueue->notes);
400 bitmap_free(wqueue->notes_bitmap);
402 wfilter = rcu_access_pointer(wqueue->filter);
404 kfree_rcu(wfilter, rcu);
405 kfree_rcu(wqueue, rcu);
409 * put_watch_queue - Dispose of a ref on a watchqueue.
410 * @wqueue: The watch queue to unref.
412 void put_watch_queue(struct watch_queue *wqueue)
414 kref_put(&wqueue->usage, __put_watch_queue);
416 EXPORT_SYMBOL(put_watch_queue);
418 static void free_watch(struct rcu_head *rcu)
420 struct watch *watch = container_of(rcu, struct watch, rcu);
422 put_watch_queue(rcu_access_pointer(watch->queue));
423 atomic_dec(&watch->cred->user->nr_watches);
424 put_cred(watch->cred);
428 static void __put_watch(struct kref *kref)
430 struct watch *watch = container_of(kref, struct watch, usage);
432 call_rcu(&watch->rcu, free_watch);
438 static void put_watch(struct watch *watch)
440 kref_put(&watch->usage, __put_watch);
444 * init_watch - Initialise a watch
445 * @watch: The watch to initialise.
446 * @wqueue: The queue to assign.
448 * Initialise a watch and set the watch queue.
450 void init_watch(struct watch *watch, struct watch_queue *wqueue)
452 kref_init(&watch->usage);
453 INIT_HLIST_NODE(&watch->list_node);
454 INIT_HLIST_NODE(&watch->queue_node);
455 rcu_assign_pointer(watch->queue, wqueue);
458 static int add_one_watch(struct watch *watch, struct watch_list *wlist, struct watch_queue *wqueue)
460 const struct cred *cred;
463 hlist_for_each_entry(w, &wlist->watchers, list_node) {
464 struct watch_queue *wq = rcu_access_pointer(w->queue);
465 if (wqueue == wq && watch->id == w->id)
469 cred = current_cred();
470 if (atomic_inc_return(&cred->user->nr_watches) > task_rlimit(current, RLIMIT_NOFILE)) {
471 atomic_dec(&cred->user->nr_watches);
475 watch->cred = get_cred(cred);
476 rcu_assign_pointer(watch->watch_list, wlist);
478 kref_get(&wqueue->usage);
479 kref_get(&watch->usage);
480 hlist_add_head(&watch->queue_node, &wqueue->watches);
481 hlist_add_head_rcu(&watch->list_node, &wlist->watchers);
486 * add_watch_to_object - Add a watch on an object to a watch list
487 * @watch: The watch to add
488 * @wlist: The watch list to add to
490 * @watch->queue must have been set to point to the queue to post notifications
491 * to and the watch list of the object to be watched. @watch->cred must also
492 * have been set to the appropriate credentials and a ref taken on them.
494 * The caller must pin the queue and the list both and must hold the list
495 * locked against racing watch additions/removals.
497 int add_watch_to_object(struct watch *watch, struct watch_list *wlist)
499 struct watch_queue *wqueue;
504 wqueue = rcu_access_pointer(watch->queue);
505 if (lock_wqueue(wqueue)) {
506 spin_lock(&wlist->lock);
507 ret = add_one_watch(watch, wlist, wqueue);
508 spin_unlock(&wlist->lock);
509 unlock_wqueue(wqueue);
515 EXPORT_SYMBOL(add_watch_to_object);
518 * remove_watch_from_object - Remove a watch or all watches from an object.
519 * @wlist: The watch list to remove from
520 * @wq: The watch queue of interest (ignored if @all is true)
521 * @id: The ID of the watch to remove (ignored if @all is true)
522 * @all: True to remove all objects
524 * Remove a specific watch or all watches from an object. A notification is
525 * sent to the watcher to tell them that this happened.
527 int remove_watch_from_object(struct watch_list *wlist, struct watch_queue *wq,
530 struct watch_notification_removal n;
531 struct watch_queue *wqueue;
538 spin_lock(&wlist->lock);
539 hlist_for_each_entry(watch, &wlist->watchers, list_node) {
541 (watch->id == id && rcu_access_pointer(watch->queue) == wq))
544 spin_unlock(&wlist->lock);
549 hlist_del_init_rcu(&watch->list_node);
550 rcu_assign_pointer(watch->watch_list, NULL);
551 spin_unlock(&wlist->lock);
553 /* We now own the reference on watch that used to belong to wlist. */
555 n.watch.type = WATCH_TYPE_META;
556 n.watch.subtype = WATCH_META_REMOVAL_NOTIFICATION;
557 n.watch.info = watch->info_id | watch_sizeof(n.watch);
560 n.watch.info = watch->info_id | watch_sizeof(n);
562 wqueue = rcu_dereference(watch->queue);
564 if (lock_wqueue(wqueue)) {
565 post_one_notification(wqueue, &n.watch);
567 if (!hlist_unhashed(&watch->queue_node)) {
568 hlist_del_init_rcu(&watch->queue_node);
572 unlock_wqueue(wqueue);
575 if (wlist->release_watch) {
576 void (*release_watch)(struct watch *);
578 release_watch = wlist->release_watch;
580 (*release_watch)(watch);
585 if (all && !hlist_empty(&wlist->watchers))
591 EXPORT_SYMBOL(remove_watch_from_object);
594 * Remove all the watches that are contributory to a queue. This has the
595 * potential to race with removal of the watches by the destruction of the
596 * objects being watched or with the distribution of notifications.
598 void watch_queue_clear(struct watch_queue *wqueue)
600 struct watch_list *wlist;
605 spin_lock_bh(&wqueue->lock);
607 /* Prevent new notifications from being stored. */
608 wqueue->defunct = true;
610 while (!hlist_empty(&wqueue->watches)) {
611 watch = hlist_entry(wqueue->watches.first, struct watch, queue_node);
612 hlist_del_init_rcu(&watch->queue_node);
613 /* We now own a ref on the watch. */
614 spin_unlock_bh(&wqueue->lock);
616 /* We can't do the next bit under the queue lock as we need to
617 * get the list lock - which would cause a deadlock if someone
618 * was removing from the opposite direction at the same time or
619 * posting a notification.
621 wlist = rcu_dereference(watch->watch_list);
623 void (*release_watch)(struct watch *);
625 spin_lock(&wlist->lock);
627 release = !hlist_unhashed(&watch->list_node);
629 hlist_del_init_rcu(&watch->list_node);
630 rcu_assign_pointer(watch->watch_list, NULL);
632 /* We now own a second ref on the watch. */
635 release_watch = wlist->release_watch;
636 spin_unlock(&wlist->lock);
641 /* This might need to call dput(), so
642 * we have to drop all the locks.
644 (*release_watch)(watch);
652 spin_lock_bh(&wqueue->lock);
655 spin_unlock_bh(&wqueue->lock);
660 * get_watch_queue - Get a watch queue from its file descriptor.
661 * @fd: The fd to query.
663 struct watch_queue *get_watch_queue(int fd)
665 struct pipe_inode_info *pipe;
666 struct watch_queue *wqueue = ERR_PTR(-EINVAL);
671 pipe = get_pipe_info(f.file, false);
672 if (pipe && pipe->watch_queue) {
673 wqueue = pipe->watch_queue;
674 kref_get(&wqueue->usage);
681 EXPORT_SYMBOL(get_watch_queue);
684 * Initialise a watch queue
686 int watch_queue_init(struct pipe_inode_info *pipe)
688 struct watch_queue *wqueue;
690 wqueue = kzalloc(sizeof(*wqueue), GFP_KERNEL);
695 kref_init(&wqueue->usage);
696 spin_lock_init(&wqueue->lock);
697 INIT_HLIST_HEAD(&wqueue->watches);
699 pipe->watch_queue = wqueue;