mark.c 29 KB

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