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