1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> 4 */ 5 6 /* 7 * fsnotify inode mark locking/lifetime/and refcnting 8 * 9 * REFCNT: 10 * The group->recnt and mark->refcnt tell how many "things" in the kernel 11 * currently are referencing the objects. Both kind of objects typically will 12 * live inside the kernel with a refcnt of 2, one for its creation and one for 13 * the reference a group and a mark hold to each other. 14 * If you are holding the appropriate locks, you can take a reference and the 15 * object itself is guaranteed to survive until the reference is dropped. 16 * 17 * LOCKING: 18 * There are 3 locks involved with fsnotify inode marks and they MUST be taken 19 * in order as follows: 20 * 21 * group->mark_mutex 22 * mark->lock 23 * mark->connector->lock 24 * 25 * group->mark_mutex protects the marks_list anchored inside a given group and 26 * each mark is hooked via the g_list. It also protects the groups private 27 * data (i.e group limits). 28 29 * mark->lock protects the marks attributes like its masks and flags. 30 * Furthermore it protects the access to a reference of the group that the mark 31 * is assigned to as well as the access to a reference of the inode/vfsmount 32 * that is being watched by the mark. 33 * 34 * mark->connector->lock protects the list of marks anchored inside an 35 * inode / vfsmount and each mark is hooked via the i_list. 36 * 37 * A list of notification marks relating to inode / mnt is contained in 38 * fsnotify_mark_connector. That structure is alive as long as there are any 39 * marks in the list and is also protected by fsnotify_mark_srcu. A mark gets 40 * detached from fsnotify_mark_connector when last reference to the mark is 41 * dropped. Thus having mark reference is enough to protect mark->connector 42 * pointer and to make sure fsnotify_mark_connector cannot disappear. Also 43 * because we remove mark from g_list before dropping mark reference associated 44 * with that, any mark found through g_list is guaranteed to have 45 * mark->connector set until we drop group->mark_mutex. 46 * 47 * LIFETIME: 48 * Inode marks survive between when they are added to an inode and when their 49 * refcnt==0. Marks are also protected by fsnotify_mark_srcu. 50 * 51 * The inode mark can be cleared for a number of different reasons including: 52 * - The inode is unlinked for the last time. (fsnotify_inode_remove) 53 * - The inode is being evicted from cache. (fsnotify_inode_delete) 54 * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes) 55 * - Something explicitly requests that it be removed. (fsnotify_destroy_mark) 56 * - The fsnotify_group associated with the mark is going away and all such marks 57 * need to be cleaned up. (fsnotify_clear_marks_by_group) 58 * 59 * This has the very interesting property of being able to run concurrently with 60 * any (or all) other directions. 61 */ 62 63 #include <linux/fs.h> 64 #include <linux/init.h> 65 #include <linux/kernel.h> 66 #include <linux/kthread.h> 67 #include <linux/module.h> 68 #include <linux/mutex.h> 69 #include <linux/slab.h> 70 #include <linux/spinlock.h> 71 #include <linux/srcu.h> 72 #include <linux/ratelimit.h> 73 74 #include <linux/atomic.h> 75 76 #include <linux/fsnotify_backend.h> 77 #include "fsnotify.h" 78 79 #define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */ 80 81 struct srcu_struct fsnotify_mark_srcu; 82 static struct kmem_cache *fsnotify_mark_connector_cachep; 83 static struct kmem_cache *fsnotify_inode_mark_connector_cachep; 84 85 static DEFINE_SPINLOCK(destroy_lock); 86 static LIST_HEAD(destroy_list); 87 static struct fsnotify_mark_connector *connector_destroy_list; 88 89 static void fsnotify_mark_destroy_workfn(struct work_struct *work); 90 static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn); 91 92 static void fsnotify_connector_destroy_workfn(struct work_struct *work); 93 static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn); 94 95 void fsnotify_get_mark(struct fsnotify_mark *mark) 96 { 97 WARN_ON_ONCE(!refcount_read(&mark->refcnt)); 98 refcount_inc(&mark->refcnt); 99 } 100 101 static fsnotify_connp_t *fsnotify_object_connp(void *obj, 102 enum fsnotify_obj_type obj_type) 103 { 104 switch (obj_type) { 105 case FSNOTIFY_OBJ_TYPE_INODE: 106 return &((struct inode *)obj)->i_fsnotify_marks; 107 case FSNOTIFY_OBJ_TYPE_VFSMOUNT: 108 return &real_mount(obj)->mnt_fsnotify_marks; 109 case FSNOTIFY_OBJ_TYPE_SB: 110 return fsnotify_sb_marks(obj); 111 case FSNOTIFY_OBJ_TYPE_MNTNS: 112 return &((struct mnt_namespace *)obj)->n_fsnotify_marks; 113 default: 114 return NULL; 115 } 116 } 117 118 static __u32 *fsnotify_conn_mask_p(struct fsnotify_mark_connector *conn) 119 { 120 if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) 121 return &fsnotify_conn_inode(conn)->i_fsnotify_mask; 122 else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) 123 return &fsnotify_conn_mount(conn)->mnt_fsnotify_mask; 124 else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) 125 return &fsnotify_conn_sb(conn)->s_fsnotify_mask; 126 else if (conn->type == FSNOTIFY_OBJ_TYPE_MNTNS) 127 return &fsnotify_conn_mntns(conn)->n_fsnotify_mask; 128 return NULL; 129 } 130 131 __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn) 132 { 133 if (WARN_ON(!fsnotify_valid_obj_type(conn->type))) 134 return 0; 135 136 return READ_ONCE(*fsnotify_conn_mask_p(conn)); 137 } 138 139 static void fsnotify_get_sb_watched_objects(struct super_block *sb) 140 { 141 atomic_long_inc(fsnotify_sb_watched_objects(sb)); 142 } 143 144 static void fsnotify_put_sb_watched_objects(struct super_block *sb) 145 { 146 atomic_long_t *watched_objects = fsnotify_sb_watched_objects(sb); 147 148 /* the superblock can go away after this decrement */ 149 if (atomic_long_dec_and_test(watched_objects)) 150 wake_up_var(watched_objects); 151 } 152 153 static void fsnotify_get_inode_ref(struct inode *inode) 154 { 155 ihold(inode); 156 fsnotify_get_sb_watched_objects(inode->i_sb); 157 } 158 159 static void fsnotify_put_inode_ref(struct inode *inode) 160 { 161 /* read ->i_sb before the inode can go away */ 162 struct super_block *sb = inode->i_sb; 163 164 iput(inode); 165 fsnotify_put_sb_watched_objects(sb); 166 } 167 168 /* 169 * Grab or drop watched objects reference depending on whether the connector 170 * is attached and has any marks attached. 171 */ 172 static void fsnotify_update_sb_watchers(struct super_block *sb, 173 struct fsnotify_mark_connector *conn) 174 { 175 struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb); 176 bool is_watched = conn->flags & FSNOTIFY_CONN_FLAG_IS_WATCHED; 177 struct fsnotify_mark *first_mark = NULL; 178 unsigned int highest_prio = 0; 179 180 if (conn->obj) 181 first_mark = hlist_entry_safe(conn->list.first, 182 struct fsnotify_mark, obj_list); 183 if (first_mark) 184 highest_prio = first_mark->group->priority; 185 if (WARN_ON(highest_prio >= __FSNOTIFY_PRIO_NUM)) 186 highest_prio = 0; 187 188 /* 189 * If the highest priority of group watching this object is prio, 190 * then watched object has a reference on counters [0..prio]. 191 * Update priority >= 1 watched objects counters. 192 */ 193 for (unsigned int p = conn->prio + 1; p <= highest_prio; p++) 194 atomic_long_inc(&sbinfo->watched_objects[p]); 195 for (unsigned int p = conn->prio; p > highest_prio; p--) 196 atomic_long_dec(&sbinfo->watched_objects[p]); 197 conn->prio = highest_prio; 198 199 /* Update priority >= 0 (a.k.a total) watched objects counter */ 200 BUILD_BUG_ON(FSNOTIFY_PRIO_NORMAL != 0); 201 if (first_mark && !is_watched) { 202 conn->flags |= FSNOTIFY_CONN_FLAG_IS_WATCHED; 203 fsnotify_get_sb_watched_objects(sb); 204 } else if (!first_mark && is_watched) { 205 conn->flags &= ~FSNOTIFY_CONN_FLAG_IS_WATCHED; 206 fsnotify_put_sb_watched_objects(sb); 207 } 208 } 209 210 /* 211 * Grab or drop inode reference for the connector if needed. 212 * 213 * When it's time to drop the reference, we only clear the HAS_IREF flag and 214 * return the inode object. fsnotify_drop_object() will be resonsible for doing 215 * iput() outside of spinlocks. This happens when last mark that wanted iref is 216 * detached. 217 */ 218 static struct inode *fsnotify_update_iref(struct fsnotify_mark_connector *conn, 219 bool want_iref) 220 { 221 bool has_iref = conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF; 222 struct inode *inode = NULL; 223 224 if (conn->type != FSNOTIFY_OBJ_TYPE_INODE || 225 want_iref == has_iref) 226 return NULL; 227 228 if (want_iref) { 229 /* Pin inode if any mark wants inode refcount held */ 230 fsnotify_get_inode_ref(fsnotify_conn_inode(conn)); 231 conn->flags |= FSNOTIFY_CONN_FLAG_HAS_IREF; 232 } else { 233 /* Unpin inode after detach of last mark that wanted iref */ 234 inode = fsnotify_conn_inode(conn); 235 conn->flags &= ~FSNOTIFY_CONN_FLAG_HAS_IREF; 236 } 237 238 return inode; 239 } 240 241 /* 242 * Calculate mask of events for a list of marks. 243 * 244 * Return true if any of the attached marks want to hold an inode reference. 245 */ 246 static bool __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) 247 { 248 u32 new_mask = 0; 249 bool want_iref = false; 250 struct fsnotify_mark *mark; 251 252 assert_spin_locked(&conn->lock); 253 /* We can get detached connector here when inode is getting unlinked. */ 254 if (!fsnotify_valid_obj_type(conn->type)) 255 return NULL; 256 hlist_for_each_entry(mark, &conn->list, obj_list) { 257 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) 258 continue; 259 new_mask |= fsnotify_calc_mask(mark); 260 if (conn->type == FSNOTIFY_OBJ_TYPE_INODE && 261 !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)) 262 want_iref = true; 263 } 264 /* 265 * We use WRITE_ONCE() to prevent silly compiler optimizations from 266 * confusing readers not holding conn->lock with partial updates. 267 */ 268 WRITE_ONCE(*fsnotify_conn_mask_p(conn), new_mask); 269 270 return want_iref; 271 } 272 273 /* 274 * Calculate mask of events for a list of marks after attach/modify mark 275 * and get an inode reference for the connector if needed. 276 * 277 * A concurrent add of evictable mark and detach of non-evictable mark can 278 * lead to __fsnotify_recalc_mask() returning false want_iref, but in this 279 * case we defer clearing iref to fsnotify_recalc_mask_clear_iref() called 280 * from fsnotify_put_mark(). 281 */ 282 static void fsnotify_recalc_mask_set_iref(struct fsnotify_mark_connector *conn) 283 { 284 bool has_iref = conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF; 285 bool want_iref = __fsnotify_recalc_mask(conn) || has_iref; 286 287 (void) fsnotify_update_iref(conn, want_iref); 288 } 289 290 /* 291 * Calculate mask of events for a list of marks after detach mark 292 * and return the inode object if its reference is no longer needed. 293 */ 294 static void *fsnotify_recalc_mask_clear_iref(struct fsnotify_mark_connector *conn) 295 { 296 bool want_iref = __fsnotify_recalc_mask(conn); 297 298 return fsnotify_update_iref(conn, want_iref); 299 } 300 301 static bool fsnotify_conn_watches_children( 302 struct fsnotify_mark_connector *conn) 303 { 304 if (conn->type != FSNOTIFY_OBJ_TYPE_INODE) 305 return false; 306 307 return fsnotify_inode_watches_children(fsnotify_conn_inode(conn)); 308 } 309 310 static void fsnotify_conn_set_children_dentry_flags( 311 struct fsnotify_mark_connector *conn) 312 { 313 if (conn->type != FSNOTIFY_OBJ_TYPE_INODE) 314 return; 315 316 fsnotify_set_children_dentry_flags(fsnotify_conn_inode(conn)); 317 } 318 319 /* 320 * Calculate mask of events for a list of marks. The caller must make sure 321 * connector and connector->obj cannot disappear under us. Callers achieve 322 * this by holding a mark->lock or mark->group->mark_mutex for a mark on this 323 * list. 324 */ 325 void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) 326 { 327 bool update_children; 328 329 if (!conn) 330 return; 331 332 spin_lock(&conn->lock); 333 update_children = !fsnotify_conn_watches_children(conn); 334 fsnotify_recalc_mask_set_iref(conn); 335 update_children &= fsnotify_conn_watches_children(conn); 336 spin_unlock(&conn->lock); 337 /* 338 * Set children's PARENT_WATCHED flags only if parent started watching. 339 * When parent stops watching, we clear false positive PARENT_WATCHED 340 * flags lazily in __fsnotify_parent(). 341 */ 342 if (update_children) 343 fsnotify_conn_set_children_dentry_flags(conn); 344 } 345 346 /* Free all connectors queued for freeing once SRCU period ends */ 347 static void fsnotify_connector_destroy_workfn(struct work_struct *work) 348 { 349 struct fsnotify_mark_connector *conn, *free; 350 351 spin_lock(&destroy_lock); 352 conn = connector_destroy_list; 353 connector_destroy_list = NULL; 354 spin_unlock(&destroy_lock); 355 356 synchronize_srcu(&fsnotify_mark_srcu); 357 while (conn) { 358 free = conn; 359 conn = conn->destroy_next; 360 kfree(free); 361 } 362 } 363 364 static void fsnotify_untrack_connector(struct fsnotify_mark_connector *conn); 365 366 static void *fsnotify_detach_connector_from_object( 367 struct fsnotify_mark_connector *conn, 368 unsigned int *type) 369 { 370 fsnotify_connp_t *connp = fsnotify_object_connp(conn->obj, conn->type); 371 struct super_block *sb = fsnotify_connector_sb(conn); 372 struct inode *inode = NULL; 373 374 *type = conn->type; 375 if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) 376 return NULL; 377 378 if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) { 379 inode = fsnotify_conn_inode(conn); 380 inode->i_fsnotify_mask = 0; 381 fsnotify_untrack_connector(conn); 382 383 /* Unpin inode when detaching from connector */ 384 if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF)) 385 inode = NULL; 386 } else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) { 387 fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0; 388 } else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) { 389 fsnotify_conn_sb(conn)->s_fsnotify_mask = 0; 390 } else if (conn->type == FSNOTIFY_OBJ_TYPE_MNTNS) { 391 fsnotify_conn_mntns(conn)->n_fsnotify_mask = 0; 392 } 393 394 rcu_assign_pointer(*connp, NULL); 395 conn->obj = NULL; 396 conn->type = FSNOTIFY_OBJ_TYPE_DETACHED; 397 if (sb) 398 fsnotify_update_sb_watchers(sb, conn); 399 400 return inode; 401 } 402 403 static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark) 404 { 405 struct fsnotify_group *group = mark->group; 406 407 if (WARN_ON_ONCE(!group)) 408 return; 409 group->ops->free_mark(mark); 410 fsnotify_put_group(group); 411 } 412 413 /* Drop object reference originally held by a connector */ 414 static void fsnotify_drop_object(unsigned int type, void *objp) 415 { 416 if (!objp) 417 return; 418 /* Currently only inode references are passed to be dropped */ 419 if (WARN_ON_ONCE(type != FSNOTIFY_OBJ_TYPE_INODE)) 420 return; 421 fsnotify_put_inode_ref(objp); 422 } 423 424 void fsnotify_put_mark(struct fsnotify_mark *mark) 425 { 426 struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector); 427 void *objp = NULL; 428 unsigned int type = FSNOTIFY_OBJ_TYPE_DETACHED; 429 bool free_conn = false; 430 431 /* Catch marks that were actually never attached to object */ 432 if (!conn) { 433 if (refcount_dec_and_test(&mark->refcnt)) 434 fsnotify_final_mark_destroy(mark); 435 return; 436 } 437 438 /* 439 * We have to be careful so that traversals of obj_list under lock can 440 * safely grab mark reference. 441 */ 442 if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock)) 443 return; 444 445 hlist_del_init_rcu(&mark->obj_list); 446 if (hlist_empty(&conn->list)) { 447 objp = fsnotify_detach_connector_from_object(conn, &type); 448 free_conn = true; 449 } else { 450 struct super_block *sb = fsnotify_connector_sb(conn); 451 452 /* Update watched objects after detaching mark */ 453 if (sb) 454 fsnotify_update_sb_watchers(sb, conn); 455 objp = fsnotify_recalc_mask_clear_iref(conn); 456 type = conn->type; 457 } 458 WRITE_ONCE(mark->connector, NULL); 459 spin_unlock(&conn->lock); 460 461 fsnotify_drop_object(type, objp); 462 463 if (free_conn) { 464 spin_lock(&destroy_lock); 465 conn->destroy_next = connector_destroy_list; 466 connector_destroy_list = conn; 467 spin_unlock(&destroy_lock); 468 queue_work(system_dfl_wq, &connector_reaper_work); 469 } 470 /* 471 * Note that we didn't update flags telling whether inode cares about 472 * what's happening with children. We update these flags from 473 * __fsnotify_parent() lazily when next event happens on one of our 474 * children. 475 */ 476 spin_lock(&destroy_lock); 477 list_add(&mark->g_list, &destroy_list); 478 spin_unlock(&destroy_lock); 479 queue_delayed_work(system_dfl_wq, &reaper_work, 480 FSNOTIFY_REAPER_DELAY); 481 } 482 EXPORT_SYMBOL_GPL(fsnotify_put_mark); 483 484 /* 485 * Get mark reference when we found the mark via lockless traversal of object 486 * list. Mark can be already removed from the list by now and on its way to be 487 * destroyed once SRCU period ends. 488 * 489 * Also pin the group so it doesn't disappear under us. 490 */ 491 static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark) 492 { 493 if (refcount_inc_not_zero(&mark->refcnt)) { 494 spin_lock(&mark->lock); 495 if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) { 496 /* mark is attached, group is still alive then */ 497 atomic_inc(&mark->group->user_waits); 498 spin_unlock(&mark->lock); 499 return true; 500 } 501 spin_unlock(&mark->lock); 502 fsnotify_put_mark(mark); 503 } 504 return false; 505 } 506 507 /* 508 * Puts marks and wakes up group destruction if necessary. 509 * 510 * Pairs with fsnotify_get_mark_safe() 511 */ 512 static void fsnotify_put_mark_wake(struct fsnotify_mark *mark) 513 { 514 if (mark) { 515 struct fsnotify_group *group = mark->group; 516 517 fsnotify_put_mark(mark); 518 /* 519 * We abuse notification_waitq on group shutdown for waiting for 520 * all marks pinned when waiting for userspace. 521 */ 522 if (atomic_dec_and_test(&group->user_waits) && group->shutdown) 523 wake_up(&group->notification_waitq); 524 } 525 } 526 527 bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info) 528 __releases(&fsnotify_mark_srcu) 529 { 530 int type; 531 532 fsnotify_foreach_iter_type(type) { 533 struct fsnotify_mark *mark = iter_info->marks[type]; 534 535 /* This can fail if mark is being removed */ 536 while (mark && !fsnotify_get_mark_safe(mark)) { 537 if (mark->group == iter_info->current_group) { 538 __release(&fsnotify_mark_srcu); 539 goto fail; 540 } 541 /* This is a mark in an unrelated group, skip */ 542 mark = fsnotify_next_mark(mark); 543 iter_info->marks[type] = mark; 544 } 545 } 546 547 /* 548 * Now that all marks are pinned by refcount in the inode / vfsmount / etc 549 * lists, we can drop SRCU lock, and safely resume the list iteration 550 * once userspace returns. 551 */ 552 srcu_read_unlock(&fsnotify_mark_srcu, iter_info->srcu_idx); 553 554 return true; 555 556 fail: 557 for (type--; type >= 0; type--) 558 fsnotify_put_mark_wake(iter_info->marks[type]); 559 return false; 560 } 561 562 void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info) 563 __acquires(&fsnotify_mark_srcu) 564 { 565 int type; 566 567 iter_info->srcu_idx = srcu_read_lock(&fsnotify_mark_srcu); 568 fsnotify_foreach_iter_type(type) 569 fsnotify_put_mark_wake(iter_info->marks[type]); 570 } 571 572 /* 573 * Mark mark as detached, remove it from group list. Mark still stays in object 574 * list until its last reference is dropped. Note that we rely on mark being 575 * removed from group list before corresponding reference to it is dropped. In 576 * particular we rely on mark->connector being valid while we hold 577 * group->mark_mutex if we found the mark through g_list. 578 * 579 * Must be called with group->mark_mutex held. The caller must either hold 580 * reference to the mark or be protected by fsnotify_mark_srcu. 581 */ 582 void fsnotify_detach_mark(struct fsnotify_mark *mark) 583 { 584 fsnotify_group_assert_locked(mark->group); 585 WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) && 586 refcount_read(&mark->refcnt) < 1 + 587 !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)); 588 589 spin_lock(&mark->lock); 590 /* something else already called this function on this mark */ 591 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { 592 spin_unlock(&mark->lock); 593 return; 594 } 595 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED; 596 list_del_init(&mark->g_list); 597 spin_unlock(&mark->lock); 598 599 /* Drop mark reference acquired in fsnotify_add_mark_locked() */ 600 fsnotify_put_mark(mark); 601 } 602 603 /* 604 * Free fsnotify mark. The mark is actually only marked as being freed. The 605 * freeing is actually happening only once last reference to the mark is 606 * dropped from a workqueue which first waits for srcu period end. 607 * 608 * Caller must have a reference to the mark or be protected by 609 * fsnotify_mark_srcu. 610 */ 611 void fsnotify_free_mark(struct fsnotify_mark *mark) 612 { 613 struct fsnotify_group *group = mark->group; 614 615 spin_lock(&mark->lock); 616 /* something else already called this function on this mark */ 617 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { 618 spin_unlock(&mark->lock); 619 return; 620 } 621 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 622 spin_unlock(&mark->lock); 623 624 /* 625 * Some groups like to know that marks are being freed. This is a 626 * callback to the group function to let it know that this mark 627 * is being freed. 628 */ 629 if (group->ops->freeing_mark) 630 group->ops->freeing_mark(mark, group); 631 } 632 633 void fsnotify_destroy_mark(struct fsnotify_mark *mark, 634 struct fsnotify_group *group) 635 { 636 fsnotify_group_lock(group); 637 fsnotify_detach_mark(mark); 638 fsnotify_group_unlock(group); 639 fsnotify_free_mark(mark); 640 } 641 EXPORT_SYMBOL_GPL(fsnotify_destroy_mark); 642 643 /* 644 * Sorting function for lists of fsnotify marks. 645 * 646 * Fanotify supports different notification classes (reflected as priority of 647 * notification group). Events shall be passed to notification groups in 648 * decreasing priority order. To achieve this marks in notification lists for 649 * inodes and vfsmounts are sorted so that priorities of corresponding groups 650 * are descending. 651 * 652 * Furthermore correct handling of the ignore mask requires processing inode 653 * and vfsmount marks of each group together. Using the group address as 654 * further sort criterion provides a unique sorting order and thus we can 655 * merge inode and vfsmount lists of marks in linear time and find groups 656 * present in both lists. 657 * 658 * A return value of 1 signifies that b has priority over a. 659 * A return value of 0 signifies that the two marks have to be handled together. 660 * A return value of -1 signifies that a has priority over b. 661 */ 662 int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b) 663 { 664 if (a == b) 665 return 0; 666 if (!a) 667 return 1; 668 if (!b) 669 return -1; 670 if (a->priority < b->priority) 671 return 1; 672 if (a->priority > b->priority) 673 return -1; 674 if (a < b) 675 return 1; 676 return -1; 677 } 678 679 static int fsnotify_attach_info_to_sb(struct super_block *sb) 680 { 681 struct fsnotify_sb_info *sbinfo; 682 683 /* sb info is freed on fsnotify_sb_delete() */ 684 sbinfo = kzalloc_obj(*sbinfo); 685 if (!sbinfo) 686 return -ENOMEM; 687 688 INIT_LIST_HEAD(&sbinfo->inode_conn_list); 689 spin_lock_init(&sbinfo->list_lock); 690 /* 691 * cmpxchg() provides the barrier so that callers of fsnotify_sb_info() 692 * will observe an initialized structure 693 */ 694 if (cmpxchg(&sb->s_fsnotify_info, NULL, sbinfo)) { 695 /* Someone else created sbinfo for us */ 696 kfree(sbinfo); 697 } 698 return 0; 699 } 700 701 struct fsnotify_inode_mark_connector { 702 struct fsnotify_mark_connector common; 703 struct list_head conns_list; 704 }; 705 706 static struct inode *fsnotify_get_living_inode(struct fsnotify_sb_info *sbinfo) 707 { 708 struct fsnotify_inode_mark_connector *iconn; 709 struct inode *inode; 710 711 spin_lock(&sbinfo->list_lock); 712 /* Find the first non-evicting inode */ 713 list_for_each_entry(iconn, &sbinfo->inode_conn_list, conns_list) { 714 /* All connectors on the list are still attached to an inode */ 715 inode = iconn->common.obj; 716 /* 717 * For connectors without FSNOTIFY_CONN_FLAG_HAS_IREF 718 * (evictable marks) corresponding inode may well have 0 719 * refcount and can be undergoing eviction. OTOH list_lock 720 * protects us from the connector getting detached and inode 721 * freed. So we can poke around the inode safely. 722 */ 723 spin_lock(&inode->i_lock); 724 if (likely( 725 !(inode_state_read(inode) & (I_FREEING | I_WILL_FREE)))) { 726 __iget(inode); 727 spin_unlock(&inode->i_lock); 728 spin_unlock(&sbinfo->list_lock); 729 return inode; 730 } 731 spin_unlock(&inode->i_lock); 732 } 733 spin_unlock(&sbinfo->list_lock); 734 735 return NULL; 736 } 737 738 /** 739 * fsnotify_unmount_inodes - an sb is unmounting. Handle any watched inodes. 740 * @sbinfo: fsnotify info for superblock being unmounted. 741 * 742 * Walk all inode connectors for the superblock and free all associated marks. 743 */ 744 void fsnotify_unmount_inodes(struct fsnotify_sb_info *sbinfo) 745 { 746 struct inode *inode; 747 748 while ((inode = fsnotify_get_living_inode(sbinfo))) { 749 fsnotify_inode(inode, FS_UNMOUNT); 750 fsnotify_clear_marks_by_inode(inode); 751 iput(inode); 752 cond_resched(); 753 } 754 } 755 756 static void fsnotify_init_connector(struct fsnotify_mark_connector *conn, 757 void *obj, unsigned int obj_type) 758 { 759 spin_lock_init(&conn->lock); 760 INIT_HLIST_HEAD(&conn->list); 761 conn->flags = 0; 762 conn->prio = 0; 763 conn->type = obj_type; 764 conn->obj = obj; 765 } 766 767 static struct fsnotify_mark_connector * 768 fsnotify_alloc_inode_connector(struct inode *inode) 769 { 770 struct fsnotify_inode_mark_connector *iconn; 771 struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(inode->i_sb); 772 773 iconn = kmem_cache_alloc(fsnotify_inode_mark_connector_cachep, 774 GFP_KERNEL); 775 if (!iconn) 776 return NULL; 777 778 fsnotify_init_connector(&iconn->common, inode, FSNOTIFY_OBJ_TYPE_INODE); 779 spin_lock(&sbinfo->list_lock); 780 list_add(&iconn->conns_list, &sbinfo->inode_conn_list); 781 spin_unlock(&sbinfo->list_lock); 782 783 return &iconn->common; 784 } 785 786 static void fsnotify_untrack_connector(struct fsnotify_mark_connector *conn) 787 { 788 struct fsnotify_inode_mark_connector *iconn; 789 struct fsnotify_sb_info *sbinfo; 790 791 if (conn->type != FSNOTIFY_OBJ_TYPE_INODE) 792 return; 793 794 iconn = container_of(conn, struct fsnotify_inode_mark_connector, common); 795 sbinfo = fsnotify_sb_info(fsnotify_conn_inode(conn)->i_sb); 796 spin_lock(&sbinfo->list_lock); 797 list_del(&iconn->conns_list); 798 spin_unlock(&sbinfo->list_lock); 799 } 800 801 static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp, 802 void *obj, unsigned int obj_type) 803 { 804 struct fsnotify_mark_connector *conn; 805 806 if (obj_type == FSNOTIFY_OBJ_TYPE_INODE) { 807 struct inode *inode = obj; 808 809 conn = fsnotify_alloc_inode_connector(inode); 810 } else { 811 conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, 812 GFP_KERNEL); 813 if (conn) 814 fsnotify_init_connector(conn, obj, obj_type); 815 } 816 if (!conn) 817 return -ENOMEM; 818 819 /* 820 * cmpxchg() provides the barrier so that readers of *connp can see 821 * only initialized structure 822 */ 823 if (cmpxchg(connp, NULL, conn)) { 824 /* Someone else created list structure for us */ 825 fsnotify_untrack_connector(conn); 826 kfree(conn); 827 } 828 return 0; 829 } 830 831 /* 832 * Get mark connector, make sure it is alive and return with its lock held. 833 * This is for users that get connector pointer from inode or mount. Users that 834 * hold reference to a mark on the list may directly lock connector->lock as 835 * they are sure list cannot go away under them. 836 */ 837 static struct fsnotify_mark_connector *fsnotify_grab_connector( 838 fsnotify_connp_t *connp) 839 { 840 struct fsnotify_mark_connector *conn; 841 int idx; 842 843 idx = srcu_read_lock(&fsnotify_mark_srcu); 844 conn = srcu_dereference(*connp, &fsnotify_mark_srcu); 845 if (!conn) 846 goto out; 847 spin_lock(&conn->lock); 848 if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) { 849 spin_unlock(&conn->lock); 850 srcu_read_unlock(&fsnotify_mark_srcu, idx); 851 return NULL; 852 } 853 out: 854 srcu_read_unlock(&fsnotify_mark_srcu, idx); 855 return conn; 856 } 857 858 /* 859 * Add mark into proper place in given list of marks. These marks may be used 860 * for the fsnotify backend to determine which event types should be delivered 861 * to which group and for which inodes. These marks are ordered according to 862 * priority, highest number first, and then by the group's location in memory. 863 */ 864 static int fsnotify_add_mark_list(struct fsnotify_mark *mark, void *obj, 865 unsigned int obj_type, int add_flags) 866 { 867 struct super_block *sb = fsnotify_object_sb(obj, obj_type); 868 struct fsnotify_mark *lmark, *last = NULL; 869 struct fsnotify_mark_connector *conn; 870 fsnotify_connp_t *connp; 871 int cmp; 872 int err = 0; 873 874 if (WARN_ON(!fsnotify_valid_obj_type(obj_type))) 875 return -EINVAL; 876 877 /* 878 * Attach the sb info before attaching a connector to any object on sb. 879 * The sb info will remain attached as long as sb lives. 880 */ 881 if (sb && !fsnotify_sb_info(sb)) { 882 err = fsnotify_attach_info_to_sb(sb); 883 if (err) 884 return err; 885 } 886 887 connp = fsnotify_object_connp(obj, obj_type); 888 restart: 889 spin_lock(&mark->lock); 890 conn = fsnotify_grab_connector(connp); 891 if (!conn) { 892 spin_unlock(&mark->lock); 893 err = fsnotify_attach_connector_to_object(connp, obj, obj_type); 894 if (err) 895 return err; 896 goto restart; 897 } 898 899 /* is mark the first mark? */ 900 if (hlist_empty(&conn->list)) { 901 hlist_add_head_rcu(&mark->obj_list, &conn->list); 902 goto added; 903 } 904 905 /* should mark be in the middle of the current list? */ 906 hlist_for_each_entry(lmark, &conn->list, obj_list) { 907 last = lmark; 908 909 if ((lmark->group == mark->group) && 910 (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) && 911 !(mark->group->flags & FSNOTIFY_GROUP_DUPS)) { 912 err = -EEXIST; 913 goto out_err; 914 } 915 916 cmp = fsnotify_compare_groups(lmark->group, mark->group); 917 if (cmp >= 0) { 918 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list); 919 goto added; 920 } 921 } 922 923 BUG_ON(last == NULL); 924 /* mark should be the last entry. last is the current last entry */ 925 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list); 926 added: 927 if (sb) 928 fsnotify_update_sb_watchers(sb, conn); 929 /* 930 * Since connector is attached to object using cmpxchg() we are 931 * guaranteed that connector initialization is fully visible by anyone 932 * seeing mark->connector set. 933 */ 934 WRITE_ONCE(mark->connector, conn); 935 out_err: 936 spin_unlock(&conn->lock); 937 spin_unlock(&mark->lock); 938 return err; 939 } 940 941 /* 942 * Attach an initialized mark to a given group and fs object. 943 * These marks may be used for the fsnotify backend to determine which 944 * event types should be delivered to which group. 945 */ 946 int fsnotify_add_mark_locked(struct fsnotify_mark *mark, 947 void *obj, unsigned int obj_type, 948 int add_flags) 949 { 950 struct fsnotify_group *group = mark->group; 951 int ret = 0; 952 953 fsnotify_group_assert_locked(group); 954 955 /* 956 * LOCKING ORDER!!!! 957 * group->mark_mutex 958 * mark->lock 959 * mark->connector->lock 960 */ 961 spin_lock(&mark->lock); 962 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED; 963 964 list_add(&mark->g_list, &group->marks_list); 965 fsnotify_get_mark(mark); /* for g_list */ 966 spin_unlock(&mark->lock); 967 968 ret = fsnotify_add_mark_list(mark, obj, obj_type, add_flags); 969 if (ret) 970 goto err; 971 972 fsnotify_recalc_mask(mark->connector); 973 974 return ret; 975 err: 976 spin_lock(&mark->lock); 977 mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE | 978 FSNOTIFY_MARK_FLAG_ATTACHED); 979 list_del_init(&mark->g_list); 980 spin_unlock(&mark->lock); 981 982 fsnotify_put_mark(mark); 983 return ret; 984 } 985 986 int fsnotify_add_mark(struct fsnotify_mark *mark, void *obj, 987 unsigned int obj_type, int add_flags) 988 { 989 int ret; 990 struct fsnotify_group *group = mark->group; 991 992 fsnotify_group_lock(group); 993 ret = fsnotify_add_mark_locked(mark, obj, obj_type, add_flags); 994 fsnotify_group_unlock(group); 995 return ret; 996 } 997 EXPORT_SYMBOL_GPL(fsnotify_add_mark); 998 999 /* 1000 * Given a list of marks, find the mark associated with given group. If found 1001 * take a reference to that mark and return it, else return NULL. 1002 */ 1003 struct fsnotify_mark *fsnotify_find_mark(void *obj, unsigned int obj_type, 1004 struct fsnotify_group *group) 1005 { 1006 fsnotify_connp_t *connp = fsnotify_object_connp(obj, obj_type); 1007 struct fsnotify_mark_connector *conn; 1008 struct fsnotify_mark *mark; 1009 1010 if (!connp) 1011 return NULL; 1012 1013 conn = fsnotify_grab_connector(connp); 1014 if (!conn) 1015 return NULL; 1016 1017 hlist_for_each_entry(mark, &conn->list, obj_list) { 1018 if (mark->group == group && 1019 (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { 1020 fsnotify_get_mark(mark); 1021 spin_unlock(&conn->lock); 1022 return mark; 1023 } 1024 } 1025 spin_unlock(&conn->lock); 1026 return NULL; 1027 } 1028 EXPORT_SYMBOL_GPL(fsnotify_find_mark); 1029 1030 /* Clear any marks in a group with given type mask */ 1031 void fsnotify_clear_marks_by_group(struct fsnotify_group *group, 1032 unsigned int obj_type) 1033 { 1034 struct fsnotify_mark *lmark, *mark; 1035 LIST_HEAD(to_free); 1036 struct list_head *head = &to_free; 1037 1038 /* Skip selection step if we want to clear all marks. */ 1039 if (obj_type == FSNOTIFY_OBJ_TYPE_ANY) { 1040 head = &group->marks_list; 1041 goto clear; 1042 } 1043 /* 1044 * We have to be really careful here. Anytime we drop mark_mutex, e.g. 1045 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our 1046 * to_free list so we have to use mark_mutex even when accessing that 1047 * list. And freeing mark requires us to drop mark_mutex. So we can 1048 * reliably free only the first mark in the list. That's why we first 1049 * move marks to free to to_free list in one go and then free marks in 1050 * to_free list one by one. 1051 */ 1052 fsnotify_group_lock(group); 1053 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { 1054 if (mark->connector->type == obj_type) 1055 list_move(&mark->g_list, &to_free); 1056 } 1057 fsnotify_group_unlock(group); 1058 1059 clear: 1060 while (1) { 1061 fsnotify_group_lock(group); 1062 if (list_empty(head)) { 1063 fsnotify_group_unlock(group); 1064 break; 1065 } 1066 mark = list_first_entry(head, struct fsnotify_mark, g_list); 1067 fsnotify_get_mark(mark); 1068 fsnotify_detach_mark(mark); 1069 fsnotify_group_unlock(group); 1070 fsnotify_free_mark(mark); 1071 fsnotify_put_mark(mark); 1072 } 1073 } 1074 1075 /* Destroy all marks attached to an object via connector */ 1076 void fsnotify_destroy_marks(fsnotify_connp_t *connp) 1077 { 1078 struct fsnotify_mark_connector *conn; 1079 struct fsnotify_mark *mark, *old_mark = NULL; 1080 void *objp; 1081 unsigned int type; 1082 1083 conn = fsnotify_grab_connector(connp); 1084 if (!conn) 1085 return; 1086 /* 1087 * We have to be careful since we can race with e.g. 1088 * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the 1089 * list can get modified. However we are holding mark reference and 1090 * thus our mark cannot be removed from obj_list so we can continue 1091 * iteration after regaining conn->lock. 1092 */ 1093 hlist_for_each_entry(mark, &conn->list, obj_list) { 1094 fsnotify_get_mark(mark); 1095 spin_unlock(&conn->lock); 1096 if (old_mark) 1097 fsnotify_put_mark(old_mark); 1098 old_mark = mark; 1099 fsnotify_destroy_mark(mark, mark->group); 1100 spin_lock(&conn->lock); 1101 } 1102 /* 1103 * Detach list from object now so that we don't pin inode until all 1104 * mark references get dropped. It would lead to strange results such 1105 * as delaying inode deletion or blocking unmount. 1106 */ 1107 objp = fsnotify_detach_connector_from_object(conn, &type); 1108 spin_unlock(&conn->lock); 1109 if (old_mark) 1110 fsnotify_put_mark(old_mark); 1111 fsnotify_drop_object(type, objp); 1112 } 1113 1114 /* 1115 * Nothing fancy, just initialize lists and locks and counters. 1116 */ 1117 void fsnotify_init_mark(struct fsnotify_mark *mark, 1118 struct fsnotify_group *group) 1119 { 1120 memset(mark, 0, sizeof(*mark)); 1121 spin_lock_init(&mark->lock); 1122 refcount_set(&mark->refcnt, 1); 1123 fsnotify_get_group(group); 1124 mark->group = group; 1125 WRITE_ONCE(mark->connector, NULL); 1126 } 1127 EXPORT_SYMBOL_GPL(fsnotify_init_mark); 1128 1129 /* 1130 * Destroy all marks in destroy_list, waits for SRCU period to finish before 1131 * actually freeing marks. 1132 */ 1133 static void fsnotify_mark_destroy_workfn(struct work_struct *work) 1134 { 1135 struct fsnotify_mark *mark, *next; 1136 struct list_head private_destroy_list; 1137 1138 spin_lock(&destroy_lock); 1139 /* exchange the list head */ 1140 list_replace_init(&destroy_list, &private_destroy_list); 1141 spin_unlock(&destroy_lock); 1142 1143 synchronize_srcu(&fsnotify_mark_srcu); 1144 1145 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) { 1146 list_del_init(&mark->g_list); 1147 fsnotify_final_mark_destroy(mark); 1148 } 1149 } 1150 1151 /* Wait for all marks queued for destruction to be actually destroyed */ 1152 void fsnotify_wait_marks_destroyed(void) 1153 { 1154 flush_delayed_work(&reaper_work); 1155 } 1156 EXPORT_SYMBOL_GPL(fsnotify_wait_marks_destroyed); 1157 1158 __init void fsnotify_init_connector_caches(void) 1159 { 1160 fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector, 1161 SLAB_PANIC); 1162 fsnotify_inode_mark_connector_cachep = KMEM_CACHE( 1163 fsnotify_inode_mark_connector, 1164 SLAB_PANIC); 1165 } 1166