watch_queue.c 17 KB

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