fanotify: Make wait for permission event response interruptible
Subsystems (3)
Jan Kara
maintainerv2 cover fanotify: Make wait for permission event response interruptible
Hello,
When waiting for response to fanotify permission events, we currently use uninterruptible waits. That makes code simple however it can cause lots of processes to end up in uninterruptible sleep with hard reboot being the only alternative in case fanotify listener process stops responding (e.g. due to a bug in its implementation) - reported e.g. in [1]. Uninterruptible sleep also makes system hibernation fail if the listener gets frozen before the process generating fanotify permission event (as reported e.g. here [2]).
This patch set modifies fanotify so that it will use interruptible wait when waiting for fanotify permission event response. Patches are based on current Linus' tree for the ease of testing (I plan to rebase them on top of Amir's pending changes later). I have also create LTP test which stresses handling of permission events while sending processes signals to test the new code [3] Review, comments, and testing are welcome.
[1] https://lore.kernel.org/lkml/153474898224.6806.12518115530793064797.stgit@buzz/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lwn.net/ml/linux-fsdevel/[email protected]/
Changes since v1:
* leave pr_debug() calls alone (Amir)
* simplify permission event state tracking (Amir)
* split some changes into separate patches (Amir)
Honza
Jan Kara
maintainerPATCH v2 1/6 fanotify: Fold dequeue_event() into process_access_response()
Fold dequeue_event() into process_access_response(). This will make changes to use of ->response field easier.
--- fs/notify/fanotify/fanotify_user.c | 41 ++++++++++++-------------------------- 1 file changed, 13 insertions(+), 28 deletions(-)
fs/notify/fanotify/fanotify_user.c +13 -28
@@ -144,28 +144,6 @@ static int fill_event_metadata(struct fsnotify_group *group,return ret; } -static struct fanotify_perm_event_info *dequeue_event ( -struct fsnotify_group *group,int fd) -{ -struct fanotify_perm_event_info *event, *return_e =NULL ; - -spin_lock (&group->notification_lock); -list_for_each_entry (event, &group->fanotify_data.access_list, - fae.fse.list) { -if (event->fd != fd) -continue ; - -list_del_init (&event->fae.fse.list); - return_e = event; -break ; - } -spin_unlock (&group->notification_lock); - -pr_debug ("%s: found return_re=%p\n" , __func__, return_e); - -return return_e; -} -static int process_access_response (struct fsnotify_group *group,struct fanotify_response *response_struct) {
@@ -194,14 +172,21 @@ static int process_access_response(struct fsnotify_group *group,if ((response &FAN_AUDIT ) && !FAN_GROUP_FLAG (group,FAN_ENABLE_AUDIT ))return -EINVAL ; - event =dequeue_event (group, fd); -if (!event) -return -ENOENT ; +spin_lock (&group->notification_lock); +list_for_each_entry (event, &group->fanotify_data.access_list, + fae.fse.list) { +if (event->fd != fd) +continue ; - event->response = response; -wake_up (&group->fanotify_data.access_waitq); +list_del_init (&event->fae.fse.list); + event->response = response; +spin_unlock (&group->notification_lock); +wake_up (&group->fanotify_data.access_waitq); +return 0 ; + } +spin_unlock (&group->notification_lock); -return 0 ; +return -ENOENT ; } static ssize_t copy_event_to_user(struct fsnotify_group *group,
Signature · 1 line
2.16.4
Re: [PATCH 1/6] fanotify: Fold dequeue_event() into process_access_response()
Quoted 5 lines · Jan Kara
Fold
dequeue_event()intoprocess_access_response(). This will make changes to use of ->response field easier.Signed-off-by Jan Kara <[email protected]>
Quoted 68 lines
--- fs/notify/fanotify/fanotify_user.c | 41 ++++++++++++-------------------------- 1 file changed, 13 insertions(+), 28 deletions(-)fs/notify/fanotify/fanotify_user.c +13
-29@@ -144,28 +144,6 @@ static int fill_event_metadata(struct fsnotify_group *group,return ret; } -static struct fanotify_perm_event_info *dequeue_event ( -struct fsnotify_group *group,int fd) -{ -struct fanotify_perm_event_info *event, *return_e =NULL ; - -spin_lock (&group->notification_lock); -list_for_each_entry (event, &group->fanotify_data.access_list, - fae.fse.list) { -if (event->fd != fd) -continue ; - -list_del_init (&event->fae.fse.list); - return_e = event; -break ; - } -spin_unlock (&group->notification_lock); - -pr_debug ("%s: found return_re=%p\n" , __func__, return_e); - -return return_e; -} -static int process_access_response (struct fsnotify_group *group,struct fanotify_response *response_struct) {@@ -194,14 +172,21 @@ static int process_access_response(struct fsnotify_group *group,if ((response &FAN_AUDIT ) && !FAN_GROUP_FLAG (group,FAN_ENABLE_AUDIT ))return -EINVAL ; - event =dequeue_event (group, fd); -if (!event) -return -ENOENT ; +spin_lock (&group->notification_lock); +list_for_each_entry (event, &group->fanotify_data.access_list, + fae.fse.list) { +if (event->fd != fd) +continue ; - event->response = response; -wake_up (&group->fanotify_data.access_waitq); +list_del_init (&event->fae.fse.list); + event->response = response; +spin_unlock (&group->notification_lock); +wake_up (&group->fanotify_data.access_waitq); +return 0 ; + } +spin_unlock (&group->notification_lock); -return 0 ; +return -ENOENT ; } static ssize_t copy_event_to_user(struct fsnotify_group *group, --2.16.4
Jan Kara
maintainerPATCH v2 2/6 fanotify: Move locking inside get_one_event()
In reply to Jan Karaget_one_event() has a single caller and that just locks notification_lock around the call. Move locking inside get_one_event() as that will make using ->response field for permission event state easier.
--- fs/notify/fanotify/fanotify_user.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
fs/notify/fanotify/fanotify_user.c +11 -14
@@ -51,25 +51,25 @@ struct kmem_cache *fanotify_perm_event_cachep __read_mostly;* Get an fsnotify notification event if one exists and is small * enough to fit in "count". Return an error pointer if the count * is not large enough. -* -* Called with the group->notification_lock held. */ static struct fsnotify_event *get_one_event (struct fsnotify_group *group,size_t count) { -assert_spin_locked (&group->notification_lock); +struct fsnotify_event *event =NULL ;pr_debug ("%s: group=%p count=%zd\n" , __func__, group, count); - +spin_lock (&group->notification_lock);if (fsnotify_notify_queue_is_empty (group)) -return NULL ; + goto out; -if (FAN_EVENT_METADATA_LEN > count) -return ERR_PTR (-EINVAL ); - -/* held the notification_lock the whole time, so this is the -* same event we peeked above */ -return fsnotify_remove_first_event (group); +if (FAN_EVENT_METADATA_LEN > count) { + event =ERR_PTR (-EINVAL ); + goto out; + } + event =fsnotify_remove_first_event (group); +out: +spin_unlock (&group->notification_lock); +return event; } static int create_fd(struct fsnotify_group *group,
@@ -261,10 +261,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,add_wait_queue (&group->notification_waitq, &wait);while (1 ) { -spin_lock (&group->notification_lock); kevent =get_one_event (group, count); -spin_unlock (&group->notification_lock); -if (IS_ERR (kevent)) { ret =PTR_ERR (kevent);break ;
Signature · 1 line
2.16.4
Re: [PATCH 2/6] fanotify: Move locking inside get_one_event()
Quoted 7 lines · Jan Kara
get_one_event()has a single caller and that just locksnotification_lockaround the call. Move locking insideget_one_event()as that will make using ->response field for permission event state easier.Signed-off-by Jan Kara <[email protected]>
Quoted 59 lines
--- fs/notify/fanotify/fanotify_user.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)fs/notify/fanotify/fanotify_user.c +11
-15@@ -51,25 +51,25 @@ struct kmem_cache *fanotify_perm_event_cachep __read_mostly;* Get an fsnotify notification event if one exists and is small * enough to fit in "count". Return an error pointer if the count * is not large enough. -* -* Called with the group->notification_lock held. */ static struct fsnotify_event *get_one_event (struct fsnotify_group *group,size_t count) { -assert_spin_locked (&group->notification_lock); +struct fsnotify_event *event =NULL ;pr_debug ("%s: group=%p count=%zd\n" , __func__, group, count); - +spin_lock (&group->notification_lock);if (fsnotify_notify_queue_is_empty (group)) -return NULL ; + goto out; -if (FAN_EVENT_METADATA_LEN > count) -return ERR_PTR (-EINVAL ); - -/* held the notification_lock the whole time, so this is the -* same event we peeked above */ -return fsnotify_remove_first_event (group); +if (FAN_EVENT_METADATA_LEN > count) { + event =ERR_PTR (-EINVAL ); + goto out; + } + event =fsnotify_remove_first_event (group); +out: +spin_unlock (&group->notification_lock); +return event; } static int create_fd(struct fsnotify_group *group,@@ -261,10 +261,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,add_wait_queue (&group->notification_waitq, &wait);while (1 ) { -spin_lock (&group->notification_lock); kevent =get_one_event (group, count); -spin_unlock (&group->notification_lock); -if (IS_ERR (kevent)) { ret =PTR_ERR (kevent);break ; --2.16.4
Jan Kara
maintainerPATCH v2 3/6 fsnotify: Create function to remove event from notification list
In reply to Jan KaraCreate function to remove event from the notification list. Later it will be used from more places.
--- fs/notify/notification.c | 20 +++++++++++++------- include/linux/fsnotify_backend.h | 3 +++ 2 files changed, 16 insertions(+), 7 deletions(-)
fs/notify/notification.c +13 -7
@@ -141,6 +141,18 @@ int fsnotify_add_event(struct fsnotify_group *group,return ret; } +void fsnotify_remove_queued_event (struct fsnotify_group *group, +struct fsnotify_event *event) +{ +assert_spin_locked (&group->notification_lock); +/* +* We need to init list head for the case of overflow event so that +* check in fsnotify_add_event() works +*/ +list_del_init (&event->list); + group->q_len--; +} +/* * Remove and return the first event from the notification list. It is the * responsibility of the caller to destroy the obtained event
@@ -155,13 +167,7 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group) event =list_first_entry (&group->notification_list, struct fsnotify_event, list); -/* -* We need to init list head for the case of overflow event so that -* check in fsnotify_add_event() works -*/ -list_del_init (&event->list); - group->q_len--; - +fsnotify_remove_queued_event (group, event);return event; }
include/linux/fsnotify_backend.h +3 -0
@@ -416,6 +416,9 @@ extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);extern struct fsnotify_event *fsnotify_peek_first_event (struct fsnotify_group *group);/* return AND dequeue the first event on the notification queue */ extern struct fsnotify_event *fsnotify_remove_first_event (struct fsnotify_group *group); +/* Remove event queued in the notification list */ +extern void fsnotify_remove_queued_event (struct fsnotify_group *group, +struct fsnotify_event *event);/* functions used to manipulate the marks attached to inodes */
Signature · 1 line
2.16.4
Re: [PATCH 3/6] fsnotify: Create function to remove event from notification list
Quoted 5 lines · Jan Kara
Create function to remove event from the notification list. Later it will be used from more places.
Signed-off-by Jan Kara <[email protected]>
Quoted 60 lines
--- fs/notify/notification.c | 20 +++++++++++++------- include/linux/fsnotify_backend.h | 3 +++ 2 files changed, 16 insertions(+), 7 deletions(-)fs/notify/notification.c +13
-7@@ -141,6 +141,18 @@ int fsnotify_add_event(struct fsnotify_group *group,return ret; } +void fsnotify_remove_queued_event (struct fsnotify_group *group, +struct fsnotify_event *event) +{ +assert_spin_locked (&group->notification_lock); +/* +* We need to init list head for the case of overflow event so that +* check in fsnotify_add_event() works +*/ +list_del_init (&event->list); + group->q_len--; +} +/* * Remove and return the first event from the notification list. It is the * responsibility of the caller to destroy the obtained event @@ -155,13 +167,7 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group) event =list_first_entry (&group->notification_list, struct fsnotify_event, list); -/* -* We need to init list head for the case of overflow event so that -* check in fsnotify_add_event() works -*/ -list_del_init (&event->list); - group->q_len--; - +fsnotify_remove_queued_event (group, event);return event; }include/linux/fsnotify_backend.h +3
-1@@ -416,6 +416,9 @@ extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);extern struct fsnotify_event *fsnotify_peek_first_event (struct fsnotify_group *group);/* return AND dequeue the first event on the notification queue */ extern struct fsnotify_event *fsnotify_remove_first_event (struct fsnotify_group *group); +/* Remove event queued in the notification list */ +extern void fsnotify_remove_queued_event (struct fsnotify_group *group, +struct fsnotify_event *event);/* functions used to manipulate the marks attached to inodes */ --2.16.4
Jan Kara
maintainerPATCH v2 4/6 fanotify: Simplify cleaning of access_list
In reply to Jan KaraSimplify iteration cleaning access_list in fanotify_release(). That will make following changes more obvious.
--- fs/notify/fanotify/fanotify_user.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
fs/notify/fanotify/fanotify_user.c +4 -6
@@ -352,7 +352,7 @@ static ssize_t fanotify_write(struct file *file, const char __user *buf, size_tstatic int fanotify_release (struct inode *ignored,struct file *file) {struct fsnotify_group *group = file->private_data; -struct fanotify_perm_event_info *event, *next; +struct fanotify_perm_event_info *event;struct fsnotify_event *fsn_event;/*
@@ -367,11 +367,9 @@ static int fanotify_release(struct inode *ignored, struct file *file)* and simulate reply from userspace. */ spin_lock (&group->notification_lock); -list_for_each_entry_safe (event, next, &group->fanotify_data.access_list, - fae.fse.list) { -pr_debug ("%s: found group=%p event=%p\n" , __func__, group, - event); - +while (!list_empty (&group->fanotify_data.access_list)) { + event =list_first_entry (&group->fanotify_data.access_list, + struct fanotify_perm_event_info, fae.fse.list);list_del_init (&event->fae.fse.list); event->response =FAN_ALLOW ; }
Signature · 1 line
2.16.4
Re: [PATCH 4/6] fanotify: Simplify cleaning of access_list
Quoted 5 lines · Jan Kara
Simplify iteration cleaning
access_listinfanotify_release(). That will make following changes more obvious.Signed-off-by Jan Kara <[email protected]>
Quoted 35 lines
--- fs/notify/fanotify/fanotify_user.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)fs/notify/fanotify/fanotify_user.c +4
-7@@ -352,7 +352,7 @@ static ssize_t fanotify_write(struct file *file, const char __user *buf, size_tstatic int fanotify_release (struct inode *ignored,struct file *file) {struct fsnotify_group *group = file->private_data; -struct fanotify_perm_event_info *event, *next; +struct fanotify_perm_event_info *event;struct fsnotify_event *fsn_event;/* @@ -367,11 +367,9 @@ static int fanotify_release(struct inode *ignored, struct file *file)* and simulate reply from userspace. */ spin_lock (&group->notification_lock); -list_for_each_entry_safe (event, next, &group->fanotify_data.access_list, - fae.fse.list) { -pr_debug ("%s: found group=%p event=%p\n" , __func__, group, - event); - +while (!list_empty (&group->fanotify_data.access_list)) { + event =list_first_entry (&group->fanotify_data.access_list, + struct fanotify_perm_event_info, fae.fse.list);list_del_init (&event->fae.fse.list); event->response =FAN_ALLOW ; } --2.16.4
Jan Kara
maintainerPATCH v2 5/6 fanotify: Track permission event state
In reply to Jan KaraTrack whether permission event got already reported to userspace and whether userspace already answered to the permission event. Protect stores to this field together with updates to ->response field by group->notification_lock. This will allow aborting wait for reply to permission event from userspace.
--- fs/notify/fanotify/fanotify.c | 6 +++--- fs/notify/fanotify/fanotify.h | 10 +++++++++- fs/notify/fanotify/fanotify_user.c | 35 ++++++++++++++++++++++++++++------- 3 files changed, 40 insertions(+), 11 deletions(-)
fs/notify/fanotify/fanotify.c +3 -3
@@ -65,7 +65,8 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p\n" , __func__, group, event); -wait_event (group->fanotify_data.access_waitq, event->response); +wait_event (group->fanotify_data.access_waitq, + event->state ==FAN_EVENT_ANSWERED );/* userspace responded, convert to something usable */ switch (event->response & ~FAN_AUDIT ) {
@@ -81,8 +82,6 @@ static int fanotify_get_response(struct fsnotify_group *group,if (event->response &FAN_AUDIT )audit_fanotify (event->response & ~FAN_AUDIT ); - event->response =0 ; -pr_debug ("%s: group=%p event=%p about to return ret=%d\n" , __func__, group, event, ret);
@@ -167,6 +166,7 @@ struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group, goto out; event = &pevent->fae; pevent->response =0 ; + pevent->state =FAN_EVENT_INIT ; goto init; } event =kmem_cache_alloc (fanotify_event_cachep, gfp);
fs/notify/fanotify/fanotify.h +9 -1
@@ -22,6 +22,13 @@ struct fanotify_event_info {struct pid *pid; }; +/* Possible states of the permission event */ +enum { +FAN_EVENT_INIT , +FAN_EVENT_REPORTED , +FAN_EVENT_ANSWERED +}; +/* * Structure for permission fanotify events. It gets allocated and freed in * fanotify_handle_event() since we wait there for user response. When the
@@ -31,7 +38,8 @@ struct fanotify_event_info {*/ struct fanotify_perm_event_info {struct fanotify_event_info fae; -int response;/* userspace answer to question */ +unsigned short response;/* userspace answer to the event */ +unsigned short state;/* state of the event */ int fd;/* fd we passed to userspace for this event */ };
fs/notify/fanotify/fanotify_user.c +28 -7
@@ -50,7 +50,8 @@ struct kmem_cache *fanotify_perm_event_cachep __read_mostly;/* * Get an fsnotify notification event if one exists and is small * enough to fit in "count". Return an error pointer if the count -* is not large enough. +* is not large enough. When permission event is dequeued, its state is +* updated accordingly. */ static struct fsnotify_event *get_one_event (struct fsnotify_group *group,size_t count)
@@ -67,6 +68,8 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group, goto out; } event =fsnotify_remove_first_event (group); +if (fanotify_is_perm_event (event->mask)) +FANOTIFY_PE (event)->state =FAN_EVENT_REPORTED ; out:spin_unlock (&group->notification_lock);return event;
@@ -144,6 +147,21 @@ static int fill_event_metadata(struct fsnotify_group *group,return ret; } +/* +* Finish processing of permission event by setting it to ANSWERED state and +* drop group->notification_lock. +*/ +static void finish_permission_event (struct fsnotify_group *group, +struct fanotify_perm_event_info *event, +unsigned int response) + __releases(&group->notification_lock) +{ +assert_spin_locked (&group->notification_lock); + event->response = response; + event->state =FAN_EVENT_ANSWERED ; +spin_unlock (&group->notification_lock); +} +static int process_access_response (struct fsnotify_group *group,struct fanotify_response *response_struct) {
@@ -179,8 +197,7 @@ static int process_access_response(struct fsnotify_group *group,continue ;list_del_init (&event->fae.fse.list); - event->response = response; -spin_unlock (&group->notification_lock); +finish_permission_event (group, event, response);wake_up (&group->fanotify_data.access_waitq);return 0 ; }
@@ -302,7 +319,9 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,fsnotify_destroy_event (group, kevent); }else {if (ret <=0 ) { -FANOTIFY_PE (kevent)->response =FAN_DENY ; +spin_lock (&group->notification_lock); +finish_permission_event (group, +FANOTIFY_PE (kevent),FAN_DENY );wake_up (&group->fanotify_data.access_waitq); }else {spin_lock (&group->notification_lock);
@@ -371,7 +390,8 @@ static int fanotify_release(struct inode *ignored, struct file *file) event =list_first_entry (&group->fanotify_data.access_list, struct fanotify_perm_event_info, fae.fse.list);list_del_init (&event->fae.fse.list); - event->response =FAN_ALLOW ; +finish_permission_event (group, event,FAN_ALLOW ); +spin_lock (&group->notification_lock); }/*
@@ -384,10 +404,11 @@ static int fanotify_release(struct inode *ignored, struct file *file)if (!(fsn_event->mask &FANOTIFY_PERM_EVENTS )) {spin_unlock (&group->notification_lock);fsnotify_destroy_event (group, fsn_event); -spin_lock (&group->notification_lock); }else { -FANOTIFY_PE (fsn_event)->response =FAN_ALLOW ; +finish_permission_event (group,FANOTIFY_PE (fsn_event), +FAN_ALLOW ); } +spin_lock (&group->notification_lock); }spin_unlock (&group->notification_lock);
Signature · 1 line
2.16.4
Re: [PATCH 5/6] fanotify: Track permission event state
Quoted 8 lines · Jan Kara
Track whether permission event got already reported to userspace and whether userspace already answered to the permission event. Protect stores to this field together with updates to ->response field by
group->notification_lock. This will allow aborting wait for reply to permission event from userspace.Signed-off-by Jan Kara <[email protected]>
Quoted 158 lines
--- fs/notify/fanotify/fanotify.c | 6 +++--- fs/notify/fanotify/fanotify.h | 10 +++++++++- fs/notify/fanotify/fanotify_user.c | 35 ++++++++++++++++++++++++++++------- 3 files changed, 40 insertions(+), 11 deletions(-)fs/notify/fanotify/fanotify.c +3
-3@@ -65,7 +65,8 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p\n" , __func__, group, event); -wait_event (group->fanotify_data.access_waitq, event->response); +wait_event (group->fanotify_data.access_waitq, + event->state ==FAN_EVENT_ANSWERED );/* userspace responded, convert to something usable */ switch (event->response & ~FAN_AUDIT ) {@@ -81,8 +82,6 @@ static int fanotify_get_response(struct fsnotify_group *group,if (event->response &FAN_AUDIT )audit_fanotify (event->response & ~FAN_AUDIT ); - event->response =0 ; -pr_debug ("%s: group=%p event=%p about to return ret=%d\n" , __func__, group, event, ret);@@ -167,6 +166,7 @@ struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group, goto out; event = &pevent->fae; pevent->response =0 ; + pevent->state =FAN_EVENT_INIT ; goto init; } event =kmem_cache_alloc (fanotify_event_cachep, gfp);fs/notify/fanotify/fanotify.h +9
-1@@ -22,6 +22,13 @@ struct fanotify_event_info {struct pid *pid; }; +/* Possible states of the permission event */ +enum { +FAN_EVENT_INIT , +FAN_EVENT_REPORTED , +FAN_EVENT_ANSWERED +}; +/* * Structure for permission fanotify events. It gets allocated and freed in * fanotify_handle_event() since we wait there for user response. When the @@ -31,7 +38,8 @@ struct fanotify_event_info {*/ struct fanotify_perm_event_info {struct fanotify_event_info fae; -int response;/* userspace answer to question */ +unsigned short response;/* userspace answer to the event */ +unsigned short state;/* state of the event */ int fd;/* fd we passed to userspace for this event */ };fs/notify/fanotify/fanotify_user.c +28
-8@@ -50,7 +50,8 @@ struct kmem_cache *fanotify_perm_event_cachep __read_mostly;/* * Get an fsnotify notification event if one exists and is small * enough to fit in "count". Return an error pointer if the count -* is not large enough. +* is not large enough. When permission event is dequeued, its state is +* updated accordingly. */ static struct fsnotify_event *get_one_event (struct fsnotify_group *group,size_t count)@@ -67,6 +68,8 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group, goto out; } event =fsnotify_remove_first_event (group); +if (fanotify_is_perm_event (event->mask)) +FANOTIFY_PE (event)->state =FAN_EVENT_REPORTED ; out:spin_unlock (&group->notification_lock);return event;@@ -144,6 +147,21 @@ static int fill_event_metadata(struct fsnotify_group *group,return ret; } +/* +* Finish processing of permission event by setting it to ANSWERED state and +* drop group->notification_lock. +*/ +static void finish_permission_event (struct fsnotify_group *group, +struct fanotify_perm_event_info *event, +unsigned int response) + __releases(&group->notification_lock) +{ +assert_spin_locked (&group->notification_lock); + event->response = response; + event->state =FAN_EVENT_ANSWERED ; +spin_unlock (&group->notification_lock); +} +static int process_access_response (struct fsnotify_group *group,struct fanotify_response *response_struct) {@@ -179,8 +197,7 @@ static int process_access_response(struct fsnotify_group *group,continue ;list_del_init (&event->fae.fse.list); - event->response = response; -spin_unlock (&group->notification_lock); +finish_permission_event (group, event, response);wake_up (&group->fanotify_data.access_waitq);return 0 ; }@@ -302,7 +319,9 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,fsnotify_destroy_event (group, kevent); }else {if (ret <=0 ) { -FANOTIFY_PE (kevent)->response =FAN_DENY ; +spin_lock (&group->notification_lock); +finish_permission_event (group, +FANOTIFY_PE (kevent),FAN_DENY );wake_up (&group->fanotify_data.access_waitq); }else {spin_lock (&group->notification_lock);@@ -371,7 +390,8 @@ static int fanotify_release(struct inode *ignored, struct file *file) event =list_first_entry (&group->fanotify_data.access_list, struct fanotify_perm_event_info, fae.fse.list);list_del_init (&event->fae.fse.list); - event->response =FAN_ALLOW ; +finish_permission_event (group, event,FAN_ALLOW ); +spin_lock (&group->notification_lock); }/* @@ -384,10 +404,11 @@ static int fanotify_release(struct inode *ignored, struct file *file)if (!(fsn_event->mask &FANOTIFY_PERM_EVENTS )) {spin_unlock (&group->notification_lock);fsnotify_destroy_event (group, fsn_event); -spin_lock (&group->notification_lock); }else { -FANOTIFY_PE (fsn_event)->response =FAN_ALLOW ; +finish_permission_event (group,FANOTIFY_PE (fsn_event), +FAN_ALLOW ); } +spin_lock (&group->notification_lock); }spin_unlock (&group->notification_lock); --2.16.4
Jan Kara
maintainerPATCH v2 6/6 fanotify: Use interruptible wait when waiting for permission events
In reply to Jan KaraWhen waiting for response to fanotify permission events, we currently use uninterruptible waits. That makes code simple however it can cause lots of processes to end up in uninterruptible sleep with hard reboot being the only alternative in case fanotify listener process stops responding (e.g. due to a bug in its implementation). Uninterruptible sleep also makes system hibernation fail if the listener gets frozen before the process generating fanotify permission event.
Fix these problems by using interruptible sleep for waiting for response to fanotify event. This is slightly tricky though - we have to detect when the event got already reported to userspace as in that case we must not free the event. Instead we push the responsibility for freeing the event to the process that will write response to the event.
--- fs/notify/fanotify/fanotify.c | 35 ++++++++++++++++++++++++++++++++--- fs/notify/fanotify/fanotify.h | 3 ++- 2 files changed, 34 insertions(+), 4 deletions(-)
fs/notify/fanotify/fanotify.c +32 -3
@@ -57,6 +57,13 @@ static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)return 0 ; } +/* +* Wait for response to permission event. The function also takes care of +* freeing the permission event (or offloads that in case the wait is canceled +* by a signal). The function returns 0 in case access got allowed by userspace, +* -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case +* the wait got interrupted by a signal. +*/ static int fanotify_get_response (struct fsnotify_group *group,struct fanotify_perm_event_info *event,struct fsnotify_iter_info *iter_info)
@@ -65,8 +72,29 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p\n" , __func__, group, event); -wait_event (group->fanotify_data.access_waitq, - event->state ==FAN_EVENT_ANSWERED ); + ret =wait_event_interruptible (group->fanotify_data.access_waitq, + event->state ==FAN_EVENT_ANSWERED ); +/* Signal pending? */ +if (ret <0 ) { +spin_lock (&group->notification_lock); +/* Event reported to userspace and no answer yet? */ +if (event->state ==FAN_EVENT_REPORTED ) { +/* Event will get freed once userspace answers to it */ + event->state =FAN_EVENT_CANCELED ; +spin_unlock (&group->notification_lock); +return ret; + } +/* Event not yet reported? Just remove it. */ +if (event->state ==FAN_EVENT_INIT ) +fsnotify_remove_queued_event (group, &event->fae.fse); +/* +* Event may be also answered in case signal delivery raced +* with wakeup. In that case we have nothing to do besides +* freeing the event and reporting error. +*/ +spin_unlock (&group->notification_lock); + goto out; + }/* userspace responded, convert to something usable */ switch (event->response & ~FAN_AUDIT ) {
@@ -84,6 +112,8 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p about to return ret=%d\n" , __func__, group, event, ret); +out: +fsnotify_destroy_event (group, &event->fae.fse);return ret; }
@@ -255,7 +285,6 @@ static int fanotify_handle_event(struct fsnotify_group *group, }else if (fanotify_is_perm_event (mask)) { ret =fanotify_get_response (group,FANOTIFY_PE (fsn_event), iter_info); -fsnotify_destroy_event (group, fsn_event); } finish:if (fanotify_is_perm_event (mask))
fs/notify/fanotify/fanotify.h +2 -1
@@ -26,7 +26,8 @@ struct fanotify_event_info {enum {FAN_EVENT_INIT ,FAN_EVENT_REPORTED , -FAN_EVENT_ANSWERED +FAN_EVENT_ANSWERED , +FAN_EVENT_CANCELED , };/*
Signature · 1 line
2.16.4
Amir Goldstein
reviewerRe: [PATCH 6/6] fanotify: Use interruptible wait when waiting for permission events
Quoted 46 lines · Jan Kara
When waiting for response to fanotify permission events, we currently use uninterruptible waits. That makes code simple however it can cause lots of processes to end up in uninterruptible sleep with hard reboot being the only alternative in case fanotify listener process stops responding (e.g. due to a bug in its implementation). Uninterruptible sleep also makes system hibernation fail if the listener gets frozen before the process generating fanotify permission event.
Fix these problems by using interruptible sleep for waiting for response to fanotify event. This is slightly tricky though - we have to detect when the event got already reported to userspace as in that case we must not free the event. Instead we push the responsibility for freeing the event to the process that will write response to the event.
Reported-by Orion Poplawski <[email protected]>Reported-by Konstantin Khlebnikov <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/notify/fanotify/fanotify.c | 35 ++++++++++++++++++++++++++++++++--- fs/notify/fanotify/fanotify.h | 3 ++- 2 files changed, 34 insertions(+), 4 deletions(-)fs/notify/fanotify/fanotify.c +7
-0@@ -57,6 +57,13 @@ static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)return 0 ; } +/* +* Wait for response to permission event. The function also takes care of +* freeing the permission event (or offloads that in case the wait is canceled +* by a signal). The function returns 0 in case access got allowed by userspace, +* -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case +* the wait got interrupted by a signal. +*/ static int fanotify_get_response (struct fsnotify_group *group,struct fanotify_perm_event_info *event,struct fsnotify_iter_info *iter_info)@@ -65,8 +72,29 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p\n" , __func__, group, event);
fs/notify/fanotify/fanotify.c static int fanotify_get_response(struct fsnotify_group *group, · line 82
Quoted 10 lines
fs/notify/fanotify/fanotify.c +8
-2-wait_event (group->fanotify_data.access_waitq, - event->state ==FAN_EVENT_ANSWERED ); + ret =wait_event_interruptible (group->fanotify_data.access_waitq, + event->state ==FAN_EVENT_ANSWERED ); +/* Signal pending? */ +if (ret <0 ) { +spin_lock (&group->notification_lock); +/* Event reported to userspace and no answer yet? */ +if (event->state ==FAN_EVENT_REPORTED ) { +/* Event will get freed once userspace answers to it */
Did you forget to commit the
if (event->state == FAN_EVENT_CANCELED)
code?
Quoted 52 lines
fs/notify/fanotify/fanotify.c +17
-1+ event->state =FAN_EVENT_CANCELED ; +spin_unlock (&group->notification_lock); +return ret; + } +/* Event not yet reported? Just remove it. */ +if (event->state ==FAN_EVENT_INIT ) +fsnotify_remove_queued_event (group, &event->fae.fse); +/* +* Event may be also answered in case signal delivery raced +* with wakeup. In that case we have nothing to do besides +* freeing the event and reporting error. +*/ +spin_unlock (&group->notification_lock); + goto out; + }/* userspace responded, convert to something usable */ switch (event->response & ~FAN_AUDIT ) {@@ -84,6 +112,8 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p about to return ret=%d\n" , __func__, group, event, ret); +out: +fsnotify_destroy_event (group, &event->fae.fse);return ret; }@@ -255,7 +285,6 @@ static int fanotify_handle_event(struct fsnotify_group *group, }else if (fanotify_is_perm_event (mask)) { ret =fanotify_get_response (group,FANOTIFY_PE (fsn_event), iter_info); -fsnotify_destroy_event (group, fsn_event); } finish:if (fanotify_is_perm_event (mask))fs/notify/fanotify/fanotify.h +2
-2@@ -26,7 +26,8 @@ struct fanotify_event_info {enum {FAN_EVENT_INIT ,FAN_EVENT_REPORTED , -FAN_EVENT_ANSWERED +FAN_EVENT_ANSWERED , +FAN_EVENT_CANCELED , };/* -- 2.16.4
Jan Kara
maintainerRe: [PATCH 6/6] fanotify: Use interruptible wait when waiting for permission events
Quoted 47 lines · Amir Goldstein
Quoted 46 lines · Jan Kara
When waiting for response to fanotify permission events, we currently use uninterruptible waits. That makes code simple however it can cause lots of processes to end up in uninterruptible sleep with hard reboot being the only alternative in case fanotify listener process stops responding (e.g. due to a bug in its implementation). Uninterruptible sleep also makes system hibernation fail if the listener gets frozen before the process generating fanotify permission event.
Fix these problems by using interruptible sleep for waiting for response to fanotify event. This is slightly tricky though - we have to detect when the event got already reported to userspace as in that case we must not free the event. Instead we push the responsibility for freeing the event to the process that will write response to the event.
Reported-by Orion Poplawski <[email protected]>Reported-by Konstantin Khlebnikov <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/notify/fanotify/fanotify.c | 35 ++++++++++++++++++++++++++++++++--- fs/notify/fanotify/fanotify.h | 3 ++- 2 files changed, 34 insertions(+), 4 deletions(-)fs/notify/fanotify/fanotify.c +7
-0@@ -57,6 +57,13 @@ static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)return 0 ; } +/* +* Wait for response to permission event. The function also takes care of +* freeing the permission event (or offloads that in case the wait is canceled +* by a signal). The function returns 0 in case access got allowed by userspace, +* -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case +* the wait got interrupted by a signal. +*/ static int fanotify_get_response (struct fsnotify_group *group,struct fanotify_perm_event_info *event,struct fsnotify_iter_info *iter_info)@@ -65,8 +72,29 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p\n" , __func__, group, event);
Quoted 14 lines
fs/notify/fanotify/fanotify.c static int fanotify_get_response(struct fsnotify_group *group, · line 82
Quoted 10 lines
fs/notify/fanotify/fanotify.c +8
-2-wait_event (group->fanotify_data.access_waitq, - event->state ==FAN_EVENT_ANSWERED ); + ret =wait_event_interruptible (group->fanotify_data.access_waitq, + event->state ==FAN_EVENT_ANSWERED ); +/* Signal pending? */ +if (ret <0 ) { +spin_lock (&group->notification_lock); +/* Event reported to userspace and no answer yet? */ +if (event->state ==FAN_EVENT_REPORTED ) { +/* Event will get freed once userspace answers to it */ Did you forget to commit the
if (event->state==FAN_EVENT_CANCELED)
code?
Yeah, somehow I forgot that bit. Thanks for noticing! Attached is a fixed version of the patch.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
From 071ab4839c021dd759056a6728d9cd47bd189419 Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Tue, 8 Jan 2019 15:18:02 +0100
Subject: [PATCH] fanotify: Use interruptible wait when waiting for permission
events
When waiting for response to fanotify permission events, we currently use uninterruptible waits. That makes code simple however it can cause lots of processes to end up in uninterruptible sleep with hard reboot being the only alternative in case fanotify listener process stops responding (e.g. due to a bug in its implementation). Uninterruptible sleep also makes system hibernation fail if the listener gets frozen before the process generating fanotify permission event.
Fix these problems by using interruptible sleep for waiting for response to fanotify event. This is slightly tricky though - we have to detect when the event got already reported to userspace as in that case we must not free the event. Instead we push the responsibility for freeing the event to the process that will write response to the event.
--- fs/notify/fanotify/fanotify.c | 35 ++++++++++++++++++++++++++++++++--- fs/notify/fanotify/fanotify.h | 3 ++- fs/notify/fanotify/fanotify_user.c | 9 ++++++++- 3 files changed, 42 insertions(+), 5 deletions(-)
fs/notify/fanotify/fanotify.c +32 -3
@@ -57,6 +57,13 @@ static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)return 0 ; } +/* +* Wait for response to permission event. The function also takes care of +* freeing the permission event (or offloads that in case the wait is canceled +* by a signal). The function returns 0 in case access got allowed by userspace, +* -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case +* the wait got interrupted by a signal. +*/ static int fanotify_get_response (struct fsnotify_group *group,struct fanotify_perm_event_info *event,struct fsnotify_iter_info *iter_info)
@@ -65,8 +72,29 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p\n" , __func__, group, event); -wait_event (group->fanotify_data.access_waitq, - event->state ==FAN_EVENT_ANSWERED ); + ret =wait_event_interruptible (group->fanotify_data.access_waitq, + event->state ==FAN_EVENT_ANSWERED ); +/* Signal pending? */ +if (ret <0 ) { +spin_lock (&group->notification_lock); +/* Event reported to userspace and no answer yet? */ +if (event->state ==FAN_EVENT_REPORTED ) { +/* Event will get freed once userspace answers to it */ + event->state =FAN_EVENT_CANCELED ; +spin_unlock (&group->notification_lock); +return ret; + } +/* Event not yet reported? Just remove it. */ +if (event->state ==FAN_EVENT_INIT ) +fsnotify_remove_queued_event (group, &event->fae.fse); +/* +* Event may be also answered in case signal delivery raced +* with wakeup. In that case we have nothing to do besides +* freeing the event and reporting error. +*/ +spin_unlock (&group->notification_lock); + goto out; + }/* userspace responded, convert to something usable */ switch (event->response & ~FAN_AUDIT ) {
@@ -84,6 +112,8 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p about to return ret=%d\n" , __func__, group, event, ret); +out: +fsnotify_destroy_event (group, &event->fae.fse);return ret; }
@@ -255,7 +285,6 @@ static int fanotify_handle_event(struct fsnotify_group *group, }else if (fanotify_is_perm_event (mask)) { ret =fanotify_get_response (group,FANOTIFY_PE (fsn_event), iter_info); -fsnotify_destroy_event (group, fsn_event); } finish:if (fanotify_is_perm_event (mask))
fs/notify/fanotify/fanotify.h +2 -1
@@ -26,7 +26,8 @@ struct fanotify_event_info {enum {FAN_EVENT_INIT ,FAN_EVENT_REPORTED , -FAN_EVENT_ANSWERED +FAN_EVENT_ANSWERED , +FAN_EVENT_CANCELED , };/*
fs/notify/fanotify/fanotify_user.c +8 -1
@@ -156,10 +156,17 @@ static void finish_permission_event(struct fsnotify_group *group, unsigned int response) __releases(&group->notification_lock) { +bool destroy = false; +assert_spin_locked (&group->notification_lock); event->response = response; - event->state =FAN_EVENT_ANSWERED ; +if (event->state ==FAN_EVENT_CANCELED ) + destroy = true; +else + event->state =FAN_EVENT_ANSWERED ;spin_unlock (&group->notification_lock); +if (destroy) +fsnotify_destroy_event (group, &event->fae.fse); } static int process_access_response(struct fsnotify_group *group,
Signature · 1 line
2.16.4
Re: [PATCH 6/6] fanotify: Use interruptible wait when waiting for permission events
Quoted 64 lines · Jan Kara
Quoted 47 lines · Amir Goldstein
Quoted 46 lines · Jan Kara
When waiting for response to fanotify permission events, we currently use uninterruptible waits. That makes code simple however it can cause lots of processes to end up in uninterruptible sleep with hard reboot being the only alternative in case fanotify listener process stops responding (e.g. due to a bug in its implementation). Uninterruptible sleep also makes system hibernation fail if the listener gets frozen before the process generating fanotify permission event.
Fix these problems by using interruptible sleep for waiting for response to fanotify event. This is slightly tricky though - we have to detect when the event got already reported to userspace as in that case we must not free the event. Instead we push the responsibility for freeing the event to the process that will write response to the event.
Reported-by Orion Poplawski <[email protected]>Reported-by Konstantin Khlebnikov <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/notify/fanotify/fanotify.c | 35 ++++++++++++++++++++++++++++++++--- fs/notify/fanotify/fanotify.h | 3 ++- 2 files changed, 34 insertions(+), 4 deletions(-)fs/notify/fanotify/fanotify.c +7
-0@@ -57,6 +57,13 @@ static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)return 0 ; } +/* +* Wait for response to permission event. The function also takes care of +* freeing the permission event (or offloads that in case the wait is canceled +* by a signal). The function returns 0 in case access got allowed by userspace, +* -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case +* the wait got interrupted by a signal. +*/ static int fanotify_get_response (struct fsnotify_group *group,struct fanotify_perm_event_info *event,struct fsnotify_iter_info *iter_info)@@ -65,8 +72,29 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p\n" , __func__, group, event);Quoted 14 lines
fs/notify/fanotify/fanotify.c static int fanotify_get_response(struct fsnotify_group *group, · line 82
Quoted 10 lines
fs/notify/fanotify/fanotify.c +8
-2-wait_event (group->fanotify_data.access_waitq, - event->state ==FAN_EVENT_ANSWERED ); + ret =wait_event_interruptible (group->fanotify_data.access_waitq, + event->state ==FAN_EVENT_ANSWERED ); +/* Signal pending? */ +if (ret <0 ) { +spin_lock (&group->notification_lock); +/* Event reported to userspace and no answer yet? */ +if (event->state ==FAN_EVENT_REPORTED ) { +/* Event will get freed once userspace answers to it */ Did you forget to commit the
if (event->state==FAN_EVENT_CANCELED)
code?
Quoted 3 lines
Yeah, somehow I forgot that bit. Thanks for noticing! Attached is a fixed version of the patch.
Looks ok.
Feel free to add:
Thanks,
Amir.
Jan Kara
maintainerRe: [PATCH 6/6] fanotify: Use interruptible wait when waiting for permission events
Quoted 65 lines · Amir Goldstein
Quoted 64 lines · Jan Kara
Quoted 61 lines · Amir Goldstein
Quoted 46 lines · Jan Kara
When waiting for response to fanotify permission events, we currently use uninterruptible waits. That makes code simple however it can cause lots of processes to end up in uninterruptible sleep with hard reboot being the only alternative in case fanotify listener process stops responding (e.g. due to a bug in its implementation). Uninterruptible sleep also makes system hibernation fail if the listener gets frozen before the process generating fanotify permission event.
Fix these problems by using interruptible sleep for waiting for response to fanotify event. This is slightly tricky though - we have to detect when the event got already reported to userspace as in that case we must not free the event. Instead we push the responsibility for freeing the event to the process that will write response to the event.
Reported-by Orion Poplawski <[email protected]>Reported-by Konstantin Khlebnikov <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/notify/fanotify/fanotify.c | 35 ++++++++++++++++++++++++++++++++--- fs/notify/fanotify/fanotify.h | 3 ++- 2 files changed, 34 insertions(+), 4 deletions(-)fs/notify/fanotify/fanotify.c +7
-0@@ -57,6 +57,13 @@ static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)return 0 ; } +/* +* Wait for response to permission event. The function also takes care of +* freeing the permission event (or offloads that in case the wait is canceled +* by a signal). The function returns 0 in case access got allowed by userspace, +* -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case +* the wait got interrupted by a signal. +*/ static int fanotify_get_response (struct fsnotify_group *group,struct fanotify_perm_event_info *event,struct fsnotify_iter_info *iter_info)@@ -65,8 +72,29 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p\n" , __func__, group, event);fs/notify/fanotify/fanotify.c static int fanotify_get_response(struct fsnotify_group *group, · line 82
Quoted 10 lines
fs/notify/fanotify/fanotify.c +8
-2-wait_event (group->fanotify_data.access_waitq, - event->state ==FAN_EVENT_ANSWERED ); + ret =wait_event_interruptible (group->fanotify_data.access_waitq, + event->state ==FAN_EVENT_ANSWERED ); +/* Signal pending? */ +if (ret <0 ) { +spin_lock (&group->notification_lock); +/* Event reported to userspace and no answer yet? */ +if (event->state ==FAN_EVENT_REPORTED ) { +/* Event will get freed once userspace answers to it */ Did you forget to commit the
if (event->state==FAN_EVENT_CANCELED)
code?
Quoted 8 lines
Quoted 3 lines
Yeah, somehow I forgot that bit. Thanks for noticing! Attached is a fixed version of the patch.
Looks ok.
Feel free to add:
Reviewed-by Amir Goldstein <[email protected]>
Thanks for review Amir! I'll queue patches to my tree then.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 21 lines · Jan Kara
Hello,
When waiting for response to fanotify permission events, we currently use uninterruptible waits. That makes code simple however it can cause lots of processes to end up in uninterruptible sleep with hard reboot being the only alternative in case fanotify listener process stops responding (e.g. due to a bug in its implementation) - reported e.g. in [1]. Uninterruptible sleep also makes system hibernation fail if the listener gets frozen before the process generating fanotify permission event (as reported e.g. here [2]).
This patch set modifies fanotify so that it will use interruptible wait when waiting for fanotify permission event response. Patches are based on current Linus' tree for the ease of testing (I plan to rebase them on top of Amir's pending changes later). I have also create LTP test which stresses handling of permission events while sending processes signals to test the new code [3] Review, comments, and testing are welcome.
[1] https://lore.kernel.org/lkml/153474898224.6806.12518115530793064797.stgit@buzz/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lwn.net/ml/linux-fsdevel/[email protected]/
Quoted 7 lines
Changes since v1:
* leavepr_debug()calls alone (Amir)
* simplify permission event state tracking (Amir)
* split some changes into separate patches (Amir)Honza
I backported these patches to the RHEL7 kernel and have started running that. One thing I've noticed are messages like the following at login time:
bash: /etc/bash_completion.d/itweb-settings.bash: Interrupted system call
I've commented on a bash bug report here https://savannah.gnu.org/support/?109159
But I'm wondering if these changes are leading to more EINTR returns from open() than expected. Of if this is the new "normal", or if this points to bugs in the antivirus software holding the fanotify callbacks.
Thoughts?
Thanks again.
Signature · 5 lines
Orion Poplawski Manager of NWRA Technical Systems 720-772-5637 NWRA, Boulder/CoRA Office FAX: 303-415-9702 3380 Mitchell Lane [email protected] Boulder, CO 80301 https://www.nwra.com/
Orion Poplawski <[email protected]>:
Quoted 6 lines
I backported these patches to the RHEL7 kernel and have started running that. One thing I've noticed are messages like the following at login time:
bash:
/etc/bash_completion.d/itweb-settings.bash: Interrupted system call
Quoted 8 lines
[...]
But I'm wondering if these changes are leading to more EINTR returns from
open()than expected. Of if this is the new "normal", or if this points to bugs in the antivirus software holding the fanotify callbacks.Thoughts?
A great observation!
I believe 99.9% of Linux software is unprepared for an EINTR from regular file I/O. That might even be considered a violation of the API.
(You problem report is reminiscent of what I'm experiencing with emacs' "M-x compile" command. The compilation fails if I should change the window geometry simultaneously because a SIGWINCH hits the compilation job. Nothing to do with fanotify but somewhat analogous.)
Marko
Jan Kara
maintainerQuoted 30 lines · Orion Poplawski
Quoted 21 lines · Jan Kara
Hello,
When waiting for response to fanotify permission events, we currently use uninterruptible waits. That makes code simple however it can cause lots of processes to end up in uninterruptible sleep with hard reboot being the only alternative in case fanotify listener process stops responding (e.g. due to a bug in its implementation) - reported e.g. in [1]. Uninterruptible sleep also makes system hibernation fail if the listener gets frozen before the process generating fanotify permission event (as reported e.g. here [2]).
This patch set modifies fanotify so that it will use interruptible wait when waiting for fanotify permission event response. Patches are based on current Linus' tree for the ease of testing (I plan to rebase them on top of Amir's pending changes later). I have also create LTP test which stresses handling of permission events while sending processes signals to test the new code [3] Review, comments, and testing are welcome.
[1] https://lore.kernel.org/lkml/153474898224.6806.12518115530793064797.stgit@buzz/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lwn.net/ml/linux-fsdevel/[email protected]/Quoted 7 lines
Changes since v1:
* leavepr_debug()calls alone (Amir)
* simplify permission event state tracking (Amir)
* split some changes into separate patches (Amir)Honza
Quoted 7 lines
I backported these patches to the RHEL7 kernel and have started running that. One thing I've noticed are messages like the following at login time:
bash:
/etc/bash_completion.d/itweb-settings.bash: Interrupted system callI've commented on a bash bug report here
https://savannah.gnu.org/support/?109159
Thanks for trying these out! Yes, so the patches can definitely lead to EINTR returns from open(2) if there's fanotify permission event generated by it and the opening process has a signal pending. Now EINTR is documented as a possible return from open(2) but Marko is right that in practice open(2) on local filesystem never returns EINTR so programs just don't bother handling it. Since breaking userspace is no-go, we probably cannot apply the change as is.
What we can do easily is to change the wait_event_interruptible() to wait_event_killable(). This is what we commonly do when we want to allow administrator to interrupt a syscall but userspace is not prepared for EINTR. That will at least allow processes that are waiting for fanotify response to be killed. So I'll do this for the coming merge window (attached patch). However this will not solve your problems with hibernation as TASK_KILLABLE tasks cannot be hibernated AFAICS. I will have to talk with people more knowledgeable about hibernation if there's a solution to this.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
maintainerSorry, forgot to attach the patch...
Honza
Quoted 61 lines · Jan Kara
Quoted 30 lines · Orion Poplawski
Quoted 21 lines · Jan Kara
Hello,
When waiting for response to fanotify permission events, we currently use uninterruptible waits. That makes code simple however it can cause lots of processes to end up in uninterruptible sleep with hard reboot being the only alternative in case fanotify listener process stops responding (e.g. due to a bug in its implementation) - reported e.g. in [1]. Uninterruptible sleep also makes system hibernation fail if the listener gets frozen before the process generating fanotify permission event (as reported e.g. here [2]).
This patch set modifies fanotify so that it will use interruptible wait when waiting for fanotify permission event response. Patches are based on current Linus' tree for the ease of testing (I plan to rebase them on top of Amir's pending changes later). I have also create LTP test which stresses handling of permission events while sending processes signals to test the new code [3] Review, comments, and testing are welcome.
[1] https://lore.kernel.org/lkml/153474898224.6806.12518115530793064797.stgit@buzz/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lwn.net/ml/linux-fsdevel/[email protected]/Quoted 7 lines
Changes since v1:
* leavepr_debug()calls alone (Amir)
* simplify permission event state tracking (Amir)
* split some changes into separate patches (Amir)Honza
Quoted 7 lines
I backported these patches to the RHEL7 kernel and have started running that. One thing I've noticed are messages like the following at login time:
bash:
/etc/bash_completion.d/itweb-settings.bash: Interrupted system callI've commented on a bash bug report here
https://savannah.gnu.org/support/?109159Thanks for trying these out! Yes, so the patches can definitely lead to EINTR returns from open(2) if there's fanotify permission event generated by it and the opening process has a signal pending. Now EINTR is documented as a possible return from open(2) but Marko is right that in practice open(2) on local filesystem never returns EINTR so programs just don't bother handling it. Since breaking userspace is no-go, we probably cannot apply the change as is.
What we can do easily is to change the
wait_event_interruptible()towait_event_killable(). This is what we commonly do when we want to allow administrator to interrupt a syscall but userspace is not prepared for EINTR. That will at least allow processes that are waiting for fanotify response to be killed. So I'll do this for the coming merge window (attached patch). However this will not solve your problems with hibernation asTASK_KILLABLEtasks cannot be hibernated AFAICS. I will have to talk with people more knowledgeable about hibernation if there's a solution to this.Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
From b51905798195eeb427c873643b3ada0d7bd991a7 Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Thu, 21 Feb 2019 11:47:23 +0100
Subject: [PATCH] fanotify: Make waits for fanotify events only killable
Making waits for response to fanotify permission events interruptible can result in EINTR returns from open(2) or other syscalls when there's e.g. AV software that's monitoring the file. Orion reports that e.g. bash is complaining like:
bash: /etc/bash_completion.d/itweb-settings.bash: Interrupted system call
So for now convert the wait from interruptible to only killable one. That is mostly invisible to userspace. Sadly this breaks hibernation with fanotify permission events pending again but we have to put more thought into how to fix this without regressing userspace visible behavior.
--- fs/notify/fanotify/fanotify.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/notify/fanotify/fanotify.c +2 -2
@@ -92,8 +92,8 @@ static int fanotify_get_response(struct fsnotify_group *group,pr_debug ("%s: group=%p event=%p\n" , __func__, group, event); - ret =wait_event_interruptible (group->fanotify_data.access_waitq, - event->state ==FAN_EVENT_ANSWERED ); + ret =wait_event_killable (group->fanotify_data.access_waitq, + event->state ==FAN_EVENT_ANSWERED );/* Signal pending? */ if (ret <0 ) {spin_lock (&group->notification_lock);
Signature · 1 line
2.16.4