From patchwork Tue Jan 8 16:46:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 10752461 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 05261746 for ; Tue, 8 Jan 2019 16:46:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E8B95237F1 for ; Tue, 8 Jan 2019 16:46:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DC57228464; Tue, 8 Jan 2019 16:46:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6AC62237F1 for ; Tue, 8 Jan 2019 16:46:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728695AbfAHQqR (ORCPT ); Tue, 8 Jan 2019 11:46:17 -0500 Received: from mx2.suse.de ([195.135.220.15]:49384 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727840AbfAHQqR (ORCPT ); Tue, 8 Jan 2019 11:46:17 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 04547B134; Tue, 8 Jan 2019 16:46:16 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id 8890A1E07FF; Tue, 8 Jan 2019 17:46:14 +0100 (CET) From: Jan Kara To: Cc: Amir Goldstein , Vivek Trivedi , Orion Poplawski , Konstantin Khlebnikov , Jan Kara Subject: [PATCH 1/4] fanotify: Fold dequeue_event() into process_access_response() Date: Tue, 8 Jan 2019 17:46:08 +0100 Message-Id: <20190108164611.11440-2-jack@suse.cz> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20190108164611.11440-1-jack@suse.cz> References: <20190108164611.11440-1-jack@suse.cz> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Fold dequeue_event() into process_access_response(). This will make changes to use of ->response field easier. Signed-off-by: Jan Kara --- fs/notify/fanotify/fanotify_user.c | 41 ++++++++++++-------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 9c870b0d2b56..908ebc421d15 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -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, From patchwork Tue Jan 8 16:46:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 10752463 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4E6D41515 for ; Tue, 8 Jan 2019 16:46:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3E53127968 for ; Tue, 8 Jan 2019 16:46:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3232528464; Tue, 8 Jan 2019 16:46:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CAEB127968 for ; Tue, 8 Jan 2019 16:46:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728763AbfAHQqR (ORCPT ); Tue, 8 Jan 2019 11:46:17 -0500 Received: from mx2.suse.de ([195.135.220.15]:49382 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728228AbfAHQqR (ORCPT ); Tue, 8 Jan 2019 11:46:17 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id E9009B12D; Tue, 8 Jan 2019 16:46:15 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id 89A291E157B; Tue, 8 Jan 2019 17:46:14 +0100 (CET) From: Jan Kara To: Cc: Amir Goldstein , Vivek Trivedi , Orion Poplawski , Konstantin Khlebnikov , Jan Kara Subject: [PATCH 2/4] fanotify: Move locking inside get_one_event() Date: Tue, 8 Jan 2019 17:46:09 +0100 Message-Id: <20190108164611.11440-3-jack@suse.cz> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20190108164611.11440-1-jack@suse.cz> References: <20190108164611.11440-1-jack@suse.cz> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP get_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. Signed-off-by: Jan Kara --- fs/notify/fanotify/fanotify_user.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 908ebc421d15..2b2c8b8a17bd 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -51,25 +51,24 @@ 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); - - pr_debug("%s: group=%p count=%zd\n", __func__, group, count); + struct fsnotify_event *event = NULL; + 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 +260,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; From patchwork Tue Jan 8 16:46:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 10752467 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9542991E for ; Tue, 8 Jan 2019 16:46:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8410B27968 for ; Tue, 8 Jan 2019 16:46:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 763E028464; Tue, 8 Jan 2019 16:46:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E39CF27968 for ; Tue, 8 Jan 2019 16:46:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728826AbfAHQqT (ORCPT ); Tue, 8 Jan 2019 11:46:19 -0500 Received: from mx2.suse.de ([195.135.220.15]:49388 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728041AbfAHQqS (ORCPT ); Tue, 8 Jan 2019 11:46:18 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id F3B73B130; Tue, 8 Jan 2019 16:46:15 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id 8FF071E157D; Tue, 8 Jan 2019 17:46:14 +0100 (CET) From: Jan Kara To: Cc: Amir Goldstein , Vivek Trivedi , Orion Poplawski , Konstantin Khlebnikov , Jan Kara Subject: [PATCH 3/4] fanotify: Track permission event state Date: Tue, 8 Jan 2019 17:46:10 +0100 Message-Id: <20190108164611.11440-4-jack@suse.cz> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20190108164611.11440-1-jack@suse.cz> References: <20190108164611.11440-1-jack@suse.cz> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Track whether permission event got already reported to userspace and whether userspace already answered to the permission event in high bits of its ->response field. Also protect stores to ->response field by group->notification_lock. This will allow aborting wait for reply to permission event from userspace. Signed-off-by: Jan Kara --- fs/notify/fanotify/fanotify.c | 16 ++++++++++------ fs/notify/fanotify/fanotify.h | 10 +++++++++- fs/notify/fanotify/fanotify_user.c | 17 ++++++++++++----- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 3723f3d18d20..cca13adc3a4c 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -62,13 +62,19 @@ static int fanotify_get_response(struct fsnotify_group *group, struct fsnotify_iter_info *iter_info) { int ret; + unsigned int response; + BUILD_BUG_ON(FAN_EVENT_STATE_MASK & (FAN_AUDIT | FAN_ALLOW | FAN_DENY)); 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->response & FAN_EVENT_STATE_MASK) == + FAN_EVENT_ANSWERED); + + response = event->response & ~FAN_EVENT_STATE_MASK; /* userspace responded, convert to something usable */ - switch (event->response & ~FAN_AUDIT) { + switch (response & ~FAN_AUDIT) { case FAN_ALLOW: ret = 0; break; @@ -78,10 +84,8 @@ static int fanotify_get_response(struct fsnotify_group *group, } /* Check if the response should be audited */ - if (event->response & FAN_AUDIT) - audit_fanotify(event->response & ~FAN_AUDIT); - - event->response = 0; + if (response & FAN_AUDIT) + audit_fanotify(response & ~FAN_AUDIT); pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, group, event, ret); diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index ea05b8a401e7..954d997745c3 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -22,6 +22,12 @@ struct fanotify_event_info { struct pid *pid; }; +/* State of permission event we store inside response field */ +#define FAN_EVENT_STATE_MASK 0xc0000000 + +#define FAN_EVENT_REPORTED 0x40000000 /* Event reported to userspace */ +#define FAN_EVENT_ANSWERED 0x80000000 /* Event answered by userspace */ + /* * 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 +37,9 @@ struct fanotify_event_info { */ struct fanotify_perm_event_info { struct fanotify_event_info fae; - int response; /* userspace answer to question */ + unsigned int response; /* userspace answer to the event. We also use + * high bits of this field for recording state + * of the event. */ int fd; /* fd we passed to userspace for this event */ }; diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 2b2c8b8a17bd..611c2ff50d64 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -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) @@ -66,6 +67,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)->response = FAN_EVENT_REPORTED; out: spin_unlock(&group->notification_lock); return event; @@ -178,7 +181,7 @@ static int process_access_response(struct fsnotify_group *group, continue; list_del_init(&event->fae.fse.list); - event->response = response; + event->response = response | FAN_EVENT_ANSWERED; spin_unlock(&group->notification_lock); wake_up(&group->fanotify_data.access_waitq); return 0; @@ -301,7 +304,10 @@ 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); + FANOTIFY_PE(kevent)->response = + FAN_DENY | FAN_EVENT_ANSWERED; + spin_unlock(&group->notification_lock); wake_up(&group->fanotify_data.access_waitq); } else { spin_lock(&group->notification_lock); @@ -372,7 +378,7 @@ static int fanotify_release(struct inode *ignored, struct file *file) event); list_del_init(&event->fae.fse.list); - event->response = FAN_ALLOW; + event->response = FAN_ALLOW | FAN_EVENT_ANSWERED; } /* @@ -387,7 +393,8 @@ static int fanotify_release(struct inode *ignored, struct file *file) fsnotify_destroy_event(group, fsn_event); spin_lock(&group->notification_lock); } else { - FANOTIFY_PE(fsn_event)->response = FAN_ALLOW; + FANOTIFY_PE(fsn_event)->response = + FAN_ALLOW | FAN_EVENT_ANSWERED; } } spin_unlock(&group->notification_lock); From patchwork Tue Jan 8 16:46:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 10752465 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 208DA746 for ; Tue, 8 Jan 2019 16:46:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0EFCB237F1 for ; Tue, 8 Jan 2019 16:46:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0306C27FA3; Tue, 8 Jan 2019 16:46:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4330A27968 for ; Tue, 8 Jan 2019 16:46:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728800AbfAHQqT (ORCPT ); Tue, 8 Jan 2019 11:46:19 -0500 Received: from mx2.suse.de ([195.135.220.15]:49380 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728212AbfAHQqS (ORCPT ); Tue, 8 Jan 2019 11:46:18 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 09D86B12B; Tue, 8 Jan 2019 16:46:16 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id 951F41E1580; Tue, 8 Jan 2019 17:46:14 +0100 (CET) From: Jan Kara To: Cc: Amir Goldstein , Vivek Trivedi , Orion Poplawski , Konstantin Khlebnikov , Jan Kara Subject: [PATCH 4/4] fanotify: Use interruptible wait when waiting for permission events Date: Tue, 8 Jan 2019 17:46:11 +0100 Message-Id: <20190108164611.11440-5-jack@suse.cz> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20190108164611.11440-1-jack@suse.cz> References: <20190108164611.11440-1-jack@suse.cz> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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. Signed-off-by: Jan Kara --- fs/notify/fanotify/fanotify.c | 36 ++++++++++++++++++++++++++---- fs/notify/fanotify/fanotify.h | 1 + fs/notify/fanotify/fanotify_user.c | 45 ++++++++++++++++++++++++-------------- fs/notify/notification.c | 20 +++++++++++------ include/linux/fsnotify_backend.h | 3 +++ 5 files changed, 78 insertions(+), 27 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index cca13adc3a4c..cb7f7c5484fc 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -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) @@ -67,9 +74,31 @@ static int fanotify_get_response(struct fsnotify_group *group, BUILD_BUG_ON(FAN_EVENT_STATE_MASK & (FAN_AUDIT | FAN_ALLOW | FAN_DENY)); pr_debug("%s: group=%p event=%p\n", __func__, group, event); - wait_event(group->fanotify_data.access_waitq, + ret = wait_event_interruptible(group->fanotify_data.access_waitq, (event->response & FAN_EVENT_STATE_MASK) == FAN_EVENT_ANSWERED); + /* Signal pending? */ + if (ret < 0) { + spin_lock(&group->notification_lock); + /* Event reported to userspace and no answer yet? */ + if ((event->response & FAN_EVENT_STATE_MASK) == + FAN_EVENT_REPORTED) { + /* Event will get freed once userspace answers to it */ + event->response = FAN_EVENT_CANCELED; + spin_unlock(&group->notification_lock); + return ret; + } + /* Event not yet reported? Just remove it. */ + if ((event->response & FAN_EVENT_STATE_MASK) == 0) + 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; + } response = event->response & ~FAN_EVENT_STATE_MASK; @@ -87,8 +116,8 @@ static int fanotify_get_response(struct fsnotify_group *group, if (response & FAN_AUDIT) audit_fanotify(response & ~FAN_AUDIT); - 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; } @@ -259,7 +288,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)) diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index 954d997745c3..57286518f420 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -27,6 +27,7 @@ struct fanotify_event_info { #define FAN_EVENT_REPORTED 0x40000000 /* Event reported to userspace */ #define FAN_EVENT_ANSWERED 0x80000000 /* Event answered by userspace */ +#define FAN_EVENT_CANCELED 0xc0000000 /* Event got canceled by a signal */ /* * Structure for permission fanotify events. It gets allocated and freed in diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 611c2ff50d64..d6ae9e9338e6 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -146,6 +146,21 @@ static int fill_event_metadata(struct fsnotify_group *group, return ret; } +static void set_event_response(struct fsnotify_group *group, + struct fanotify_perm_event_info *event, + unsigned int response) +{ + spin_lock(&group->notification_lock); + /* Waiter got aborted by a signal? Free the event. */ + if (unlikely(event->response == FAN_EVENT_CANCELED)) { + spin_unlock(&group->notification_lock); + fsnotify_destroy_event(group, &event->fae.fse); + return; + } + event->response = response | FAN_EVENT_ANSWERED; + spin_unlock(&group->notification_lock); +} + static int process_access_response(struct fsnotify_group *group, struct fanotify_response *response_struct) { @@ -181,8 +196,8 @@ static int process_access_response(struct fsnotify_group *group, continue; list_del_init(&event->fae.fse.list); - event->response = response | FAN_EVENT_ANSWERED; spin_unlock(&group->notification_lock); + set_event_response(group, event, response); wake_up(&group->fanotify_data.access_waitq); return 0; } @@ -304,10 +319,8 @@ static ssize_t fanotify_read(struct file *file, char __user *buf, fsnotify_destroy_event(group, kevent); } else { if (ret <= 0) { - spin_lock(&group->notification_lock); - FANOTIFY_PE(kevent)->response = - FAN_DENY | FAN_EVENT_ANSWERED; - spin_unlock(&group->notification_lock); + set_event_response(group, FANOTIFY_PE(kevent), + FAN_DENY); wake_up(&group->fanotify_data.access_waitq); } else { spin_lock(&group->notification_lock); @@ -357,7 +370,7 @@ static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t static 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; /* @@ -372,13 +385,13 @@ 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 | FAN_EVENT_ANSWERED; + spin_unlock(&group->notification_lock); + set_event_response(group, event, FAN_ALLOW); + spin_lock(&group->notification_lock); } /* @@ -388,14 +401,14 @@ static int fanotify_release(struct inode *ignored, struct file *file) */ while (!fsnotify_notify_queue_is_empty(group)) { fsn_event = fsnotify_remove_first_event(group); + spin_unlock(&group->notification_lock); 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 | FAN_EVENT_ANSWERED; + set_event_response(group, FANOTIFY_PE(fsn_event), + FAN_ALLOW); } + spin_lock(&group->notification_lock); } spin_unlock(&group->notification_lock); diff --git a/fs/notify/notification.c b/fs/notify/notification.c index 3c3e36745f59..2195a5cf745a 100644 --- a/fs/notify/notification.c +++ b/fs/notify/notification.c @@ -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; } diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 7639774e7475..cddf839bac96 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -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 */