fsnotify: allow fsnotify_{peek,remove}_first_event with empty queue
authorAmir Goldstein <amir73il@gmail.com>
Thu, 4 Mar 2021 10:48:22 +0000 (12:48 +0200)
committerJan Kara <jack@suse.cz>
Tue, 16 Mar 2021 15:14:23 +0000 (16:14 +0100)
Current code has an assumtion that fsnotify_notify_queue_is_empty() is
called to verify that queue is not empty before trying to peek or remove
an event from queue.

Remove this assumption by moving the fsnotify_notify_queue_is_empty()
into the functions, allow them to return NULL value and check return
value by all callers.

This is a prep patch for multi event queues.

Link: https://lore.kernel.org/r/20210304104826.3993892-2-amir73il@gmail.com
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
fs/notify/fanotify/fanotify_user.c
fs/notify/inotify/inotify_user.c
fs/notify/notification.c
include/linux/fsnotify_backend.h

index 9e0c1af..1616220 100644 (file)
@@ -100,24 +100,30 @@ static struct fanotify_event *get_one_event(struct fsnotify_group *group,
 {
        size_t event_size = FAN_EVENT_METADATA_LEN;
        struct fanotify_event *event = NULL;
+       struct fsnotify_event *fsn_event;
        unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
 
        pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
 
        spin_lock(&group->notification_lock);
-       if (fsnotify_notify_queue_is_empty(group))
+       fsn_event = fsnotify_peek_first_event(group);
+       if (!fsn_event)
                goto out;
 
-       if (fid_mode) {
-               event_size += fanotify_event_info_len(fid_mode,
-                       FANOTIFY_E(fsnotify_peek_first_event(group)));
-       }
+       event = FANOTIFY_E(fsn_event);
+       if (fid_mode)
+               event_size += fanotify_event_info_len(fid_mode, event);
 
        if (event_size > count) {
                event = ERR_PTR(-EINVAL);
                goto out;
        }
-       event = FANOTIFY_E(fsnotify_remove_first_event(group));
+
+       /*
+        * Held the notification_lock the whole time, so this is the
+        * same event we peeked above.
+        */
+       fsnotify_remove_first_event(group);
        if (fanotify_is_perm_event(event->mask))
                FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
 out:
@@ -573,6 +579,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 fsnotify_event *fsn_event;
 
        /*
         * Stop new events from arriving in the notification queue. since
@@ -601,13 +608,12 @@ static int fanotify_release(struct inode *ignored, struct file *file)
         * dequeue them and set the response. They will be freed once the
         * response is consumed and fanotify_get_response() returns.
         */
-       while (!fsnotify_notify_queue_is_empty(group)) {
-               struct fanotify_event *event;
+       while ((fsn_event = fsnotify_remove_first_event(group))) {
+               struct fanotify_event *event = FANOTIFY_E(fsn_event);
 
-               event = FANOTIFY_E(fsnotify_remove_first_event(group));
                if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
                        spin_unlock(&group->notification_lock);
-                       fsnotify_destroy_event(group, &event->fse);
+                       fsnotify_destroy_event(group, fsn_event);
                } else {
                        finish_permission_event(group, FANOTIFY_PERM(event),
                                                FAN_ALLOW);
index c71be4f..a6c95bd 100644 (file)
@@ -146,10 +146,9 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
        size_t event_size = sizeof(struct inotify_event);
        struct fsnotify_event *event;
 
-       if (fsnotify_notify_queue_is_empty(group))
-               return NULL;
-
        event = fsnotify_peek_first_event(group);
+       if (!event)
+               return NULL;
 
        pr_debug("%s: group=%p event=%p\n", __func__, group, event);
 
index 75d79d6..001cfe7 100644 (file)
@@ -47,13 +47,6 @@ u32 fsnotify_get_cookie(void)
 }
 EXPORT_SYMBOL_GPL(fsnotify_get_cookie);
 
-/* return true if the notify queue is empty, false otherwise */
-bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
-{
-       assert_spin_locked(&group->notification_lock);
-       return list_empty(&group->notification_list) ? true : false;
-}
-
 void fsnotify_destroy_event(struct fsnotify_group *group,
                            struct fsnotify_event *event)
 {
@@ -141,33 +134,36 @@ void fsnotify_remove_queued_event(struct fsnotify_group *group,
 }
 
 /*
- * Remove and return the first event from the notification list.  It is the
- * responsibility of the caller to destroy the obtained event
+ * Return the first event on the notification list without removing it.
+ * Returns NULL if the list is empty.
  */
-struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
+struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group)
 {
-       struct fsnotify_event *event;
-
        assert_spin_locked(&group->notification_lock);
 
-       pr_debug("%s: group=%p\n", __func__, group);
+       if (fsnotify_notify_queue_is_empty(group))
+               return NULL;
 
-       event = list_first_entry(&group->notification_list,
-                                struct fsnotify_event, list);
-       fsnotify_remove_queued_event(group, event);
-       return event;
+       return list_first_entry(&group->notification_list,
+                               struct fsnotify_event, list);
 }
 
 /*
- * This will not remove the event, that must be done with
- * fsnotify_remove_first_event()
+ * Remove and return the first event from the notification list.  It is the
+ * responsibility of the caller to destroy the obtained event
  */
-struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group)
+struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
 {
-       assert_spin_locked(&group->notification_lock);
+       struct fsnotify_event *event = fsnotify_peek_first_event(group);
 
-       return list_first_entry(&group->notification_list,
-                               struct fsnotify_event, list);
+       if (!event)
+               return NULL;
+
+       pr_debug("%s: group=%p event=%p\n", __func__, group, event);
+
+       fsnotify_remove_queued_event(group, event);
+
+       return event;
 }
 
 /*
index e5409b8..7eb979b 100644 (file)
@@ -495,7 +495,13 @@ static inline void fsnotify_queue_overflow(struct fsnotify_group *group)
        fsnotify_add_event(group, group->overflow_event, NULL);
 }
 
-/* true if the group notification queue is empty */
+static inline bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
+{
+       assert_spin_locked(&group->notification_lock);
+
+       return list_empty(&group->notification_list);
+}
+
 extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);
 /* return, but do not dequeue the first event on the notification queue */
 extern struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group);