watch_queue.c 17 KB

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