4949 *
5050 * A list of notification marks relating to inode / mnt is contained in
5151 * fsnotify_mark_connector. That structure is alive as long as there are any
52- * marks in the list and is also protected by fsnotify_mark_srcu.
52+ * marks in the list and is also protected by fsnotify_mark_srcu. A mark gets
53+ * detached from fsnotify_mark_connector when last reference to the mark is
54+ * dropped. Thus having mark reference is enough to protect mark->connector
55+ * pointer and to make sure fsnotify_mark_connector cannot disappear. Also
56+ * because we remove mark from g_list before dropping mark reference associated
57+ * with that, any mark found through g_list is guaranteed to have
58+ * mark->connector set until we drop group->mark_mutex.
5359 *
5460 * LIFETIME:
5561 * Inode marks survive between when they are added to an inode and when their
@@ -103,26 +109,16 @@ void fsnotify_get_mark(struct fsnotify_mark *mark)
103109 atomic_inc (& mark -> refcnt );
104110}
105111
106- void fsnotify_put_mark (struct fsnotify_mark * mark )
107- {
108- if (atomic_dec_and_test (& mark -> refcnt )) {
109- spin_lock (& destroy_lock );
110- list_add (& mark -> g_list , & destroy_list );
111- spin_unlock (& destroy_lock );
112- queue_delayed_work (system_unbound_wq , & reaper_work ,
113- FSNOTIFY_REAPER_DELAY );
114- }
115- }
116-
117112static void __fsnotify_recalc_mask (struct fsnotify_mark_connector * conn )
118113{
119114 u32 new_mask = 0 ;
120115 struct fsnotify_mark * mark ;
121116
122117 assert_spin_locked (& conn -> lock );
123- hlist_for_each_entry (mark , & conn -> list , obj_list )
124- new_mask |= mark -> mask ;
125-
118+ hlist_for_each_entry (mark , & conn -> list , obj_list ) {
119+ if (mark -> flags & FSNOTIFY_MARK_FLAG_ATTACHED )
120+ new_mask |= mark -> mask ;
121+ }
126122 if (conn -> flags & FSNOTIFY_OBJ_TYPE_INODE )
127123 conn -> inode -> i_fsnotify_mask = new_mask ;
128124 else if (conn -> flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT )
@@ -131,8 +127,9 @@ static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
131127
132128/*
133129 * Calculate mask of events for a list of marks. The caller must make sure
134- * connector cannot disappear under us (usually by holding a mark->lock or
135- * mark->group->mark_mutex for a mark on this list).
130+ * connector and connector->inode cannot disappear under us. Callers achieve
131+ * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
132+ * list.
136133 */
137134void fsnotify_recalc_mask (struct fsnotify_mark_connector * conn )
138135{
@@ -164,7 +161,6 @@ static void fsnotify_connector_destroy_workfn(struct work_struct *work)
164161 }
165162}
166163
167-
168164static struct inode * fsnotify_detach_connector_from_object (
169165 struct fsnotify_mark_connector * conn )
170166{
@@ -187,14 +183,34 @@ static struct inode *fsnotify_detach_connector_from_object(
187183 return inode ;
188184}
189185
190- static struct inode * fsnotify_detach_from_object (struct fsnotify_mark * mark )
186+ static void fsnotify_final_mark_destroy (struct fsnotify_mark * mark )
187+ {
188+ if (mark -> group )
189+ fsnotify_put_group (mark -> group );
190+ mark -> free_mark (mark );
191+ }
192+
193+ void fsnotify_put_mark (struct fsnotify_mark * mark )
191194{
192195 struct fsnotify_mark_connector * conn ;
193196 struct inode * inode = NULL ;
194197 bool free_conn = false;
195198
199+ /* Catch marks that were actually never attached to object */
200+ if (!mark -> connector ) {
201+ if (atomic_dec_and_test (& mark -> refcnt ))
202+ fsnotify_final_mark_destroy (mark );
203+ return ;
204+ }
205+
206+ /*
207+ * We have to be careful so that traversals of obj_list under lock can
208+ * safely grab mark reference.
209+ */
210+ if (!atomic_dec_and_lock (& mark -> refcnt , & mark -> connector -> lock ))
211+ return ;
212+
196213 conn = mark -> connector ;
197- spin_lock (& conn -> lock );
198214 hlist_del_init_rcu (& mark -> obj_list );
199215 if (hlist_empty (& conn -> list )) {
200216 inode = fsnotify_detach_connector_from_object (conn );
@@ -205,27 +221,40 @@ static struct inode *fsnotify_detach_from_object(struct fsnotify_mark *mark)
205221 mark -> connector = NULL ;
206222 spin_unlock (& conn -> lock );
207223
224+ iput (inode );
225+
208226 if (free_conn ) {
209227 spin_lock (& destroy_lock );
210228 conn -> destroy_next = connector_destroy_list ;
211229 connector_destroy_list = conn ;
212230 spin_unlock (& destroy_lock );
213231 queue_work (system_unbound_wq , & connector_reaper_work );
214232 }
215-
216- return inode ;
233+ /*
234+ * Note that we didn't update flags telling whether inode cares about
235+ * what's happening with children. We update these flags from
236+ * __fsnotify_parent() lazily when next event happens on one of our
237+ * children.
238+ */
239+ spin_lock (& destroy_lock );
240+ list_add (& mark -> g_list , & destroy_list );
241+ spin_unlock (& destroy_lock );
242+ queue_delayed_work (system_unbound_wq , & reaper_work ,
243+ FSNOTIFY_REAPER_DELAY );
217244}
218245
219246/*
220- * Remove mark from inode / vfsmount list, group list, drop inode reference
221- * if we got one.
247+ * Mark mark as detached, remove it from group list. Mark still stays in object
248+ * list until its last reference is dropped. Note that we rely on mark being
249+ * removed from group list before corresponding reference to it is dropped. In
250+ * particular we rely on mark->connector being valid while we hold
251+ * group->mark_mutex if we found the mark through g_list.
222252 *
223253 * Must be called with group->mark_mutex held. The caller must either hold
224254 * reference to the mark or be protected by fsnotify_mark_srcu.
225255 */
226256void fsnotify_detach_mark (struct fsnotify_mark * mark )
227257{
228- struct inode * inode = NULL ;
229258 struct fsnotify_group * group = mark -> group ;
230259
231260 WARN_ON_ONCE (!mutex_is_locked (& group -> mark_mutex ));
@@ -234,31 +263,15 @@ void fsnotify_detach_mark(struct fsnotify_mark *mark)
234263 !!(mark -> flags & FSNOTIFY_MARK_FLAG_ATTACHED ));
235264
236265 spin_lock (& mark -> lock );
237-
238266 /* something else already called this function on this mark */
239267 if (!(mark -> flags & FSNOTIFY_MARK_FLAG_ATTACHED )) {
240268 spin_unlock (& mark -> lock );
241269 return ;
242270 }
243-
244271 mark -> flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED ;
245-
246- inode = fsnotify_detach_from_object (mark );
247-
248- /*
249- * Note that we didn't update flags telling whether inode cares about
250- * what's happening with children. We update these flags from
251- * __fsnotify_parent() lazily when next event happens on one of our
252- * children.
253- */
254-
255272 list_del_init (& mark -> g_list );
256-
257273 spin_unlock (& mark -> lock );
258274
259- if (inode )
260- iput (inode );
261-
262275 atomic_dec (& group -> num_marks );
263276
264277 /* Drop mark reference acquired in fsnotify_add_mark_locked() */
@@ -458,7 +471,9 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
458471 hlist_for_each_entry (lmark , & conn -> list , obj_list ) {
459472 last = lmark ;
460473
461- if ((lmark -> group == mark -> group ) && !allow_dups ) {
474+ if ((lmark -> group == mark -> group ) &&
475+ (lmark -> flags & FSNOTIFY_MARK_FLAG_ATTACHED ) &&
476+ !allow_dups ) {
462477 err = - EEXIST ;
463478 goto out_err ;
464479 }
@@ -509,7 +524,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
509524 mark -> group = group ;
510525 list_add (& mark -> g_list , & group -> marks_list );
511526 atomic_inc (& group -> num_marks );
512- fsnotify_get_mark (mark ); /* for i_list and g_list */
527+ fsnotify_get_mark (mark ); /* for g_list */
513528 spin_unlock (& mark -> lock );
514529
515530 ret = fsnotify_add_mark_list (mark , inode , mnt , allow_dups );
@@ -557,7 +572,8 @@ struct fsnotify_mark *fsnotify_find_mark(
557572 return NULL ;
558573
559574 hlist_for_each_entry (mark , & conn -> list , obj_list ) {
560- if (mark -> group == group ) {
575+ if (mark -> group == group &&
576+ (mark -> flags & FSNOTIFY_MARK_FLAG_ATTACHED )) {
561577 fsnotify_get_mark (mark );
562578 spin_unlock (& conn -> lock );
563579 return mark ;
@@ -637,23 +653,38 @@ void fsnotify_detach_group_marks(struct fsnotify_group *group)
637653void fsnotify_destroy_marks (struct fsnotify_mark_connector __rcu * * connp )
638654{
639655 struct fsnotify_mark_connector * conn ;
640- struct fsnotify_mark * mark ;
656+ struct fsnotify_mark * mark , * old_mark = NULL ;
657+ struct inode * inode ;
641658
642- while ((conn = fsnotify_grab_connector (connp ))) {
643- /*
644- * We have to be careful since we can race with e.g.
645- * fsnotify_clear_marks_by_group() and once we drop the list
646- * lock, mark can get removed from the obj_list and destroyed.
647- * But we are holding mark reference so mark cannot be freed
648- * and calling fsnotify_destroy_mark() more than once is fine.
649- */
650- mark = hlist_entry (conn -> list .first , struct fsnotify_mark ,
651- obj_list );
659+ conn = fsnotify_grab_connector (connp );
660+ if (!conn )
661+ return ;
662+ /*
663+ * We have to be careful since we can race with e.g.
664+ * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the
665+ * list can get modified. However we are holding mark reference and
666+ * thus our mark cannot be removed from obj_list so we can continue
667+ * iteration after regaining conn->lock.
668+ */
669+ hlist_for_each_entry (mark , & conn -> list , obj_list ) {
652670 fsnotify_get_mark (mark );
653671 spin_unlock (& conn -> lock );
672+ if (old_mark )
673+ fsnotify_put_mark (old_mark );
674+ old_mark = mark ;
654675 fsnotify_destroy_mark (mark , mark -> group );
655- fsnotify_put_mark ( mark );
676+ spin_lock ( & conn -> lock );
656677 }
678+ /*
679+ * Detach list from object now so that we don't pin inode until all
680+ * mark references get dropped. It would lead to strange results such
681+ * as delaying inode deletion or blocking unmount.
682+ */
683+ inode = fsnotify_detach_connector_from_object (conn );
684+ spin_unlock (& conn -> lock );
685+ if (old_mark )
686+ fsnotify_put_mark (old_mark );
687+ iput (inode );
657688}
658689
659690/*
@@ -686,9 +717,7 @@ void fsnotify_mark_destroy_list(void)
686717
687718 list_for_each_entry_safe (mark , next , & private_destroy_list , g_list ) {
688719 list_del_init (& mark -> g_list );
689- if (mark -> group )
690- fsnotify_put_group (mark -> group );
691- mark -> free_mark (mark );
720+ fsnotify_final_mark_destroy (mark );
692721 }
693722}
694723
0 commit comments