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