Skip to content

fsnotify: Avoid SRCU stalls with fanotify permission events

Jan Kara linux-fsdevel 7 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v7 Latest35 patches 7 repliesIn review
  2. v633 patches 9 repliesSuperseded
  3. v533 patches 24 repliesSuperseded
Earlier versions · 4
VersionPostedPatchesRepliesStatus
v425 patches12 repliesSuperseded
v322 patches6 repliesSuperseded
v222 patches13 repliesSuperseded
v122 patches56 repliesSuperseded

Changes in v7 from the cover letter

* Added Reviewed-by tags from Miklos
* Improved couple of comments suggested by Miklos
* Fixed possible NULL pointer dereference in audit_tree
* Cleaned up some patches based on Miklos' feedback

Patches in v7 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/35fsnotify: Remove unnecessary tests when showing fdinfofs/notify/fdinfo.c+1 -50 replies
2/35inotify: Remove inode pointers from debug messagesfs/notify/inotify/inotify_fsnotify.c fs/notify/inotify/inotify_user.c+12 -170 replies
3/35fanotify: Move recalculation of inode / vfsmount mask under mark_mutexfs/notify/fanotify/fanotify_user.c+6 -90 replies
4/35audit: Abstract hash key handlingkernel/audit_tree.c+28 -111 reply
5/35audit_tree: Use mark flags to check whether mark is alivekernel/audit_tree.c+2 -26 replies
6/35fsnotify: Update commentsfs/notify/mark.c+1 -120 replies
7/35fsnotify: Move mark list head from object into dedicated structurefs/inode.c fs/mount.h fs/namespace.c +8 more+132 -470 replies
8/35fsnotify: Move object pointer to fsnotify_mark_connectorfs/notify/dnotify/dnotify.c fs/notify/fdinfo.c fs/notify/fsnotify.h +5 more+74 -490 replies
9/35fsnotify: Make fsnotify_mark_connector hold inode referencefs/notify/fsnotify.h fs/notify/inode_mark.c fs/notify/mark.c +1 more+18 -450 replies
10/35fsnotify: Remove indirection from mark list additionfs/notify/fsnotify.h fs/notify/inode_mark.c fs/notify/mark.c +1 more+43 -860 replies
11/35fsnotify: Move fsnotify_destroy_marks()fs/notify/mark.c+36 -360 replies
12/35fsnotify: Move locking into fsnotify_recalc_mask()fs/notify/dnotify/dnotify.c fs/notify/fsnotify.h fs/notify/inode_mark.c +3 more+39 -400 replies
13/35fsnotify: Move locking into fsnotify_find_mark()fs/notify/inode_mark.c fs/notify/mark.c fs/notify/vfsmount_mark.c+10 -130 replies
14/35fsnotify: Determine lock in fsnotify_destroy_marks()fs/notify/fsnotify.h fs/notify/mark.c+11 -80 replies
15/35fsnotify: Remove indirection from fsnotify_detach_mark()fs/notify/fsnotify.h fs/notify/inode_mark.c fs/notify/mark.c +1 more+26 -490 replies
16/35fsnotify: Avoid double locking in fsnotify_detach_from_object()fs/notify/mark.c+1 -10 replies
17/35fsnotify: Remove useless list deletion and commentfs/notify/mark.c+0 -60 replies
18/35fsnotify: Lock object list with connector lockfs/notify/mark.c include/linux/fsnotify_backend.h+34 -590 replies
19/35fsnotify: Free fsnotify_mark_connector when there is no mark attachedfs/inode.c fs/mount.h fs/namespace.c +8 more+136 -660 replies
20/35inotify: Do not drop mark reference under idr_lockfs/notify/inotify/inotify_user.c+6 -180 replies
21/35fsnotify: Move queueing of mark for destruction into fsnotify_put_mark()fs/notify/inotify/inotify_user.c fs/notify/mark.c+30 -460 replies
22/35fsnotify: Detach mark from object list when last reference is droppedfs/notify/mark.c include/linux/fsnotify_backend.h kernel/audit_tree.c+105 -770 replies
23/35fsnotify: Remove special handling of mark destruction on group shutdownfs/notify/fsnotify.h fs/notify/group.c fs/notify/mark.c+12 -110 replies
24/35fsnotify: Provide framework for dropping SRCU lock in ->handle_eventfs/notify/fsnotify.h fs/notify/group.c fs/notify/mark.c +1 more+94 -00 replies
25/35fsnotify: Pass fsnotify_iter_info into handle_event handlerfs/notify/dnotify/dnotify.c fs/notify/fanotify/fanotify.c fs/notify/fsnotify.c +7 more+30 -150 replies
26/35fanotify: Release SRCU lock when waiting for userspace responsefs/notify/fanotify/fanotify.c+15 -20 replies
27/35fsnotify: Remove fsnotify_set_mark_{,ignored_}mask_locked()fs/notify/dnotify/dnotify.c fs/notify/fanotify/fanotify_user.c fs/notify/inotify/inotify_user.c +2 more+9 -330 replies
28/35fsnotify: Remove fsnotify_recalc_{inode|vfsmount}_mask()fs/notify/fanotify/fanotify_user.c fs/notify/inode_mark.c fs/notify/inotify/inotify_user.c +2 more+5 -190 replies
29/35fsnotify: Inline fsnotify_clear_{inode|vfsmount}_mark_group()fs/notify/inode_mark.c fs/notify/vfsmount_mark.c include/linux/fsnotify_backend.h+10 -170 replies
30/35fsnotify: Rename fsnotify_clear_marks_by_group_flags()fs/notify/mark.c include/linux/fsnotify_backend.h+8 -100 replies
31/35fsnotify: Remove fsnotify_detach_group_marks()fs/notify/fsnotify.h fs/notify/group.c fs/notify/mark.c +1 more+20 -380 replies
32/35fsnotify: Remove fsnotify_find_{inode|vfsmount}_mark()fs/notify/Makefile fs/notify/dnotify/dnotify.c fs/notify/fanotify/fanotify_user.c +7 more+18 -710 replies
33/35fsnotify: Drop inode_mark.cfs/notify/Makefile fs/notify/fsnotify.c fs/notify/inode_mark.c+59 -900 replies
34/35fsnotify: Add group pointer in fsnotify_init_mark()fs/notify/dnotify/dnotify.c fs/notify/fanotify/fanotify_user.c fs/notify/inotify/inotify_user.c +5 more+38 -320 replies
35/35fsnotify: Move ->free_mark callback to fsnotify_opsfs/notify/dnotify/dnotify.c fs/notify/fanotify/fanotify.c fs/notify/fanotify/fanotify.h +9 more+41 -350 replies

Reviews trailers per patch

All 35 patches have a Reviewed-by or Acked-by.

PatchMiklos SzerediAmir GoldsteinPaul Moore
1/35R-b R-b
2/35R-b R-b
3/35R-b R-b
4/35R-b A-b
5/35R-b R-b A-b
6/35R-b R-b
7/35R-b R-b
8/35R-b R-b
9/35R-b R-b
10/35R-b R-b
11/35R-b R-b
12/35R-b R-b
13/35R-b R-b
14/35R-b R-b
15/35R-b R-b
16/35R-b
17/35R-b R-b
18/35R-b R-b
19/35R-b R-b
20/35R-b R-b
21/35R-b R-b
22/35R-b R-b
23/35R-b R-b
24/35R-b R-b
25/35R-b R-b
26/35R-b R-b
27/35R-b R-b
28/35R-b R-b
29/35R-b R-b
30/35R-b R-b
31/35R-b R-b
32/35R-b R-b
33/35R-b R-b
34/35R-b R-b
35/35R-b R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 3 reviewers.

Read the v7 thread

Cover letter [PATCH 0/35 v7] fsnotify: Avoid SRCU stalls with fanotify permission events

Hello,

This is the seventh revision of my patches to avoid SRCU stalls when fanotify waits for response to permission events from userspace processes. Thanks to Amir, Paul, and Miklos for review! It also passes a new LTP test that tries to provoke hangs in fanotify subsystem when there are unanswered fanotify permission events. If nobody has more objections, I'll push the changes to my tree to queue them for the next merge window.

Changes since v6:
* Added Reviewed-by tags from Miklos
* Improved couple of comments suggested by Miklos
* Fixed possible NULL pointer dereference in audit_tree
* Cleaned up some patches based on Miklos' feedback

Changes since v5:
* Added Reviewed-by tags from Amir
* Fixed up __rcu annotation
* Fixed minor issues spotted by 0-day in the middle of the series
* Added fsnotify_attach_connector_to_object()
* Removed igrab()/iput() from fsnotify_recalc_mask()

Changes since v4:
* Further split up of patches as requested by Miklos
* Moved some hunks between patches to make things more logical
* Couple of smaller improvements suggested by Miklos
* Rebased on top of 4.11-rc2

Changes since v3:
* added Reviewed-by tags
* split adding of fsnotify_mark_connector into 4 smaller parts as Miklos asked
* simplified API of fsnotify_prepare/finish_user_wait()

Changes since v2:
* added Reviewed-by tags
* dropped fsnotify_put_list() abstraction
* use rcu_assign_pointer() where appropriate

Changes since v1:
* renamed fsnotify_mark_list to fsnotify_mark_connector and couple other things
* updated some comments and changelogs to better explain what is going on
* made audit use inode pointer as a key again
* added Reviewed-by tags
* dropped two audit fixes that got already merged
* added cleanup of mark destruction functions

Patch set overview
------------------

Currently, fanotify waits for response to a permission even from userspace process while holding fsnotify_mark_srcu lock. That has a consequence that when userspace process takes long to respond or does not respond at all, fsnotify_mark_srcu period cannot ever complete blocking reclaim of any notification marks and also blocking any process that did synchronize_srcu() on fsnotify_mark_srcu. Effectively, this eventually blocks anybody interacting with the notification subsystem. Miklos has some real world reports of this happening. Although this in principle a problem of broken userspace application (which futhermore has to have CAP_SYS_ADMIN in init_user_ns, so it is not a security problem), it is still nasty that a simple error can block the kernel like this.

This patch set solves this problem. The basic idea of the solution is that when fanotify needs to wait for response from userspace process, it grabs reference to the mark which generated the event and drops fsnotify_mark_srcu lock. When userspace responds, we grab fsnotify_mark_srcu again, drop the mark reference, and continue iterating the list of marks attached to the inode / vfsmount delivering the event to other notification groups. What complicates this simple approach is that the mark for which we wait for response has to stay pinned in the list of marks attached to the inode / vfsmount so that we can resume iteration of the list when userspace responds but on the other hand when the inode gets unlinked while we wait for userspace reponse, we need to destroy the mark (or at least detach it from the inode).

The first 5 patches contain some initial fixes and cleanups. Patches 6-17 implement attaching of marks to inode / vfsmount via a dedicated structure which allows us to detach list of marks from the object without having to destroy the list itself. Patches 18-20 implement removal of mark from the list of marks attached to an object when last mark reference is dropped. Patches 21-24 then implement dropping of SRCU lock when waiting on response from userspace. Patches 25-33 are mostly trivial cleanups that get rid of trivial wrappers and one pointer in the mark structure.

Patches have survived testing with inotify/fanotify tests in LTP.

Finally, to ease experimenting with the patches I've pushed them out to
git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs.git fsnotify

Honza