From patchwork Mon Mar 7 15:57:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12772061 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD389C433F5 for ; Mon, 7 Mar 2022 15:58:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243067AbiCGP7B (ORCPT ); Mon, 7 Mar 2022 10:59:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229934AbiCGP66 (ORCPT ); Mon, 7 Mar 2022 10:58:58 -0500 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F17E31504 for ; Mon, 7 Mar 2022 07:58:02 -0800 (PST) Received: by mail-wr1-x436.google.com with SMTP id n15so9921954wra.6 for ; Mon, 07 Mar 2022 07:58:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lzlFDcKeup3tPIXzWHHjCUBp6o8hEnxEJJeuyu6PzKo=; b=cP22eTz+mci0yvwhJcdEqNvsht8SgC+hkgSNAC5SmWA39xhM1Bei4S28UpsHR7vReK GUY+ricvH5mNRjNdlxDlGQdmowAOcJ13OBONDALWch0w3lLUl1I5QsSVAtggGC5HQMg+ l3smR2Zn3JHfViQNRJi1yn4VLnPKsRz3mEf8mPvvk4k5osigx+lON3OAI2zw45aFpkSg Ucs0XivvN3+H4pfHVYb0A9pT5KkHyOnPmp5B9HCGBDhCxHdVYEjxZxC8fbLj6GRrRR8g rJ1+dA9B2rfU64NMN8N1hUG5GJvTCQDxxa9H5zOSyFKMa4n6RXE7rdLrO89M0aOsvnUd 7Pog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lzlFDcKeup3tPIXzWHHjCUBp6o8hEnxEJJeuyu6PzKo=; b=GKwmUc+WqobpZC5dmTK/AP6zth6fFGS9C3pE8FqENYPjt/HnDFty/bH9MRzg80RKhe UrNCk5CACuxCX+6TD2lZuqiZLBzcKejaAkROOEVkGtnoLTxMcuwnhucAJQfKa3qCxlHZ YsRJJH63PLK9/RssdYjsShBW3bQw2be80DB5jWq+u01XhldY9a74++Cu/9EFGjdVpWwK HbgCczHOdyal/x5A24SIGFsHD/36ZcWITq5tJ60XNvdEP59DvkiFpVJnAnaSEcI48xoK alOUdD3B9s0D9JjaN3JqLh+qczvZVFZFwxpXT9EdGukHwnJebcrM2ufBhBq4zoKmKE/Q c3jw== X-Gm-Message-State: AOAM5333INrAIr33qYjJQdGwJk7SRaYZPtYyY4UuGpCiD/E9ezEnAdk0 uUC1eHODjzktLm6SQZMVsIA= X-Google-Smtp-Source: ABdhPJzoSLeTAVFAymiQXSEnz/Dl4a2ASIZcIXuRWiVBhfIuXN9A6/+GSiAAgAjZv/U2w1JuyOdSeQ== X-Received: by 2002:adf:d84c:0:b0:1f0:651d:c6da with SMTP id k12-20020adfd84c000000b001f0651dc6damr8728883wrl.665.1646668680785; Mon, 07 Mar 2022 07:58:00 -0800 (PST) Received: from localhost.localdomain ([77.137.71.203]) by smtp.gmail.com with ESMTPSA id g6-20020a5d5406000000b001f049726044sm11686591wrv.79.2022.03.07.07.57.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Mar 2022 07:58:00 -0800 (PST) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH 1/5] fsnotify: move inotify control flags to mark flags Date: Mon, 7 Mar 2022 17:57:37 +0200 Message-Id: <20220307155741.1352405-2-amir73il@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220307155741.1352405-1-amir73il@gmail.com> References: <20220307155741.1352405-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org The inotify control flags in the mark mask (e.g. FS_IN_ONE_SHOT) are not relevant to object interest mask, so move them to the mark flags. This frees up some bits in the object interest mask. Signed-off-by: Amir Goldstein --- fs/notify/fsnotify.c | 4 +-- fs/notify/inotify/inotify_fsnotify.c | 2 +- fs/notify/inotify/inotify_user.c | 40 +++++++++++++++++----------- include/linux/fsnotify_backend.h | 11 ++++---- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 494f653efbc6..f5d2547e2411 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -255,7 +255,7 @@ static int fsnotify_handle_inode_event(struct fsnotify_group *group, if (WARN_ON_ONCE(!inode && !dir)) return 0; - if ((inode_mark->mask & FS_EXCL_UNLINK) && + if ((inode_mark->flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK) && path && d_unlinked(path->dentry)) return 0; @@ -583,7 +583,7 @@ static __init int fsnotify_init(void) { int ret; - BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25); + BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 23); ret = init_srcu_struct(&fsnotify_mark_srcu); if (ret) diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c index d92d7b0adc9a..49cfe2ae6d23 100644 --- a/fs/notify/inotify/inotify_fsnotify.c +++ b/fs/notify/inotify/inotify_fsnotify.c @@ -122,7 +122,7 @@ int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, u32 mask, fsnotify_destroy_event(group, fsn_event); } - if (inode_mark->mask & IN_ONESHOT) + if (inode_mark->flags & FSNOTIFY_MARK_FLAG_IN_ONESHOT) fsnotify_destroy_mark(inode_mark, group); return 0; diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c index 54583f62dc44..324c49d26b53 100644 --- a/fs/notify/inotify/inotify_user.c +++ b/fs/notify/inotify/inotify_user.c @@ -110,11 +110,26 @@ static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg) mask |= FS_EVENT_ON_CHILD; /* mask off the flags used to open the fd */ - mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK)); + mask |= (arg & IN_ALL_EVENTS); return mask; } +#define INOTIFY_MARK_FLAGS \ + (FSNOTIFY_MARK_FLAG_EXCL_UNLINK | FSNOTIFY_MARK_FLAG_IN_ONESHOT) + +static inline unsigned int inotify_arg_to_flags(u32 arg) +{ + unsigned int flags = 0; + + if (arg & IN_EXCL_UNLINK) + flags |= FSNOTIFY_MARK_FLAG_EXCL_UNLINK; + if (arg & IN_ONESHOT) + flags |= FSNOTIFY_MARK_FLAG_IN_ONESHOT; + + return flags; +} + static inline u32 inotify_mask_to_arg(__u32 mask) { return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED | @@ -526,13 +541,10 @@ static int inotify_update_existing_watch(struct fsnotify_group *group, struct fsnotify_mark *fsn_mark; struct inotify_inode_mark *i_mark; __u32 old_mask, new_mask; - __u32 mask; - int add = (arg & IN_MASK_ADD); + int replace = !(arg & IN_MASK_ADD); int create = (arg & IN_MASK_CREATE); int ret; - mask = inotify_arg_to_mask(inode, arg); - fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group); if (!fsn_mark) return -ENOENT; @@ -545,10 +557,12 @@ static int inotify_update_existing_watch(struct fsnotify_group *group, spin_lock(&fsn_mark->lock); old_mask = fsn_mark->mask; - if (add) - fsn_mark->mask |= mask; - else - fsn_mark->mask = mask; + if (replace) { + fsn_mark->mask = 0; + fsn_mark->flags &= ~INOTIFY_MARK_FLAGS; + } + fsn_mark->mask |= inotify_arg_to_mask(inode, arg); + fsn_mark->flags |= inotify_arg_to_flags(arg); new_mask = fsn_mark->mask; spin_unlock(&fsn_mark->lock); @@ -579,19 +593,17 @@ static int inotify_new_watch(struct fsnotify_group *group, u32 arg) { struct inotify_inode_mark *tmp_i_mark; - __u32 mask; int ret; struct idr *idr = &group->inotify_data.idr; spinlock_t *idr_lock = &group->inotify_data.idr_lock; - mask = inotify_arg_to_mask(inode, arg); - tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL); if (unlikely(!tmp_i_mark)) return -ENOMEM; fsnotify_init_mark(&tmp_i_mark->fsn_mark, group); - tmp_i_mark->fsn_mark.mask = mask; + tmp_i_mark->fsn_mark.mask = inotify_arg_to_mask(inode, arg); + tmp_i_mark->fsn_mark.flags = inotify_arg_to_flags(arg); tmp_i_mark->wd = -1; ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark); @@ -845,9 +857,7 @@ static int __init inotify_user_setup(void) BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT); BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW); BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED); - BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK); BUILD_BUG_ON(IN_ISDIR != FS_ISDIR); - BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT); BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22); diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 0805b74cae44..0bd63608e935 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -55,7 +55,6 @@ #define FS_ACCESS_PERM 0x00020000 /* access event in a permissions hook */ #define FS_OPEN_EXEC_PERM 0x00040000 /* open/exec event in a permission hook */ -#define FS_EXCL_UNLINK 0x04000000 /* do not send events if object is unlinked */ /* * Set on inode mark that cares about things that happen to its children. * Always set for dnotify and inotify. @@ -66,7 +65,6 @@ #define FS_RENAME 0x10000000 /* File was renamed */ #define FS_DN_MULTISHOT 0x20000000 /* dnotify multishot */ #define FS_ISDIR 0x40000000 /* event occurred against dir */ -#define FS_IN_ONESHOT 0x80000000 /* only send event once */ #define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO) @@ -106,8 +104,7 @@ FS_ERROR) /* Extra flags that may be reported with event or control handling of events */ -#define ALL_FSNOTIFY_FLAGS (FS_EXCL_UNLINK | FS_ISDIR | FS_IN_ONESHOT | \ - FS_DN_MULTISHOT | FS_EVENT_ON_CHILD) +#define ALL_FSNOTIFY_FLAGS (FS_ISDIR | FS_EVENT_ON_CHILD | FS_DN_MULTISHOT) #define ALL_FSNOTIFY_BITS (ALL_FSNOTIFY_EVENTS | ALL_FSNOTIFY_FLAGS) @@ -473,9 +470,13 @@ struct fsnotify_mark { struct fsnotify_mark_connector *connector; /* Events types to ignore [mark->lock, group->mark_mutex] */ __u32 ignored_mask; -#define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x01 + /* Internal fsnotify flags */ #define FSNOTIFY_MARK_FLAG_ALIVE 0x02 #define FSNOTIFY_MARK_FLAG_ATTACHED 0x04 + /* Backend controlled flags */ +#define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x10 +#define FSNOTIFY_MARK_FLAG_EXCL_UNLINK 0x20 +#define FSNOTIFY_MARK_FLAG_IN_ONESHOT 0x40 unsigned int flags; /* flags [mark->lock] */ }; From patchwork Mon Mar 7 15:57:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12772063 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 808A7C4332F for ; Mon, 7 Mar 2022 15:58:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243948AbiCGP7D (ORCPT ); Mon, 7 Mar 2022 10:59:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236128AbiCGP67 (ORCPT ); Mon, 7 Mar 2022 10:58:59 -0500 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 854F93137D for ; Mon, 7 Mar 2022 07:58:03 -0800 (PST) Received: by mail-wm1-x332.google.com with SMTP id r65so9482952wma.2 for ; Mon, 07 Mar 2022 07:58:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CjXRp1bdXZQL0cITkY0adRmoRgN6m5tRA5jHYrqnWvw=; b=cTpFz0OJmsR0jJIAYqPxk0aPmjBDgVkFjdvYJY5/1rZgvJC/MQt7K86isw8O8M2OSz pVYfAiS1EmwZZ+hoplFRfjxcRQQh/mDgpd9SH84Aip/dzXgOjj5dosSmLYh7SudYFnhr ni6okfwHdEHeXDMhFquAR/YWM6YDgrz03j1RmRrGzzm6YO09ANns2XwZCxMs6bfmTYYn ha23oU3f1/9jo//zYkZhZj3j+JZikb6ehY1ZRDKVMiR+1DBH8OAqDHbDh0p2iewoDjoM xm0r+Er0fx8a6FxTTHNX3ElZ8UG4m0kv5XL/GK5xekZrQIKzWl+UAhVgFsx26V26IDhq gG/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CjXRp1bdXZQL0cITkY0adRmoRgN6m5tRA5jHYrqnWvw=; b=Z2pkrkY4ZZZFFKo4leCqDKsm3Ov/SCxPiI+Qf0Xh2BcPob0A7GLuwvX668tPxFhIC0 55RxqzHPzQFTSNItu/t5Dw050JYX8DJWoaLQxblNOdSrVhNN9+yJFBnNN80GF3psQplK Aa87YkfhGKwHfouN2pXRV3LXwi+VgIth/yuE4OhTzR8fxc4GTYHuZ6RpCmXLeQVlTaIZ PgMAytS3GQRGKsopricsYZz/mih3LM74Q15tqwGmAlOFmrkqR5tM0TygsYeJ6KYoj7ZQ +QFSXNM3NaAxaNVFj6ZwqdKMrqp6RqQjXG85gPNU/KgkcMKA9TcvQ5lGOnJsTyN13OGc SHsg== X-Gm-Message-State: AOAM532yUJxB5P+0O4m8qHzAO2yNFyfuTNlF1IyIHtx7jtAUhzwPyTis oRE8srgJwcUTC+y+h/sHHjY= X-Google-Smtp-Source: ABdhPJy2Wyzi4G/URB5OKS0TD7RYdhx6yxQUXyE4n5Gx0khSleRgcDnEAqOEOhnyX8LLd9V3OQ4zNw== X-Received: by 2002:a1c:ed0e:0:b0:380:fa8c:da99 with SMTP id l14-20020a1ced0e000000b00380fa8cda99mr9658023wmh.135.1646668682051; Mon, 07 Mar 2022 07:58:02 -0800 (PST) Received: from localhost.localdomain ([77.137.71.203]) by smtp.gmail.com with ESMTPSA id g6-20020a5d5406000000b001f049726044sm11686591wrv.79.2022.03.07.07.58.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Mar 2022 07:58:01 -0800 (PST) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH 2/5] fsnotify: pass flags argument to fsnotify_add_mark() Date: Mon, 7 Mar 2022 17:57:38 +0200 Message-Id: <20220307155741.1352405-3-amir73il@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220307155741.1352405-1-amir73il@gmail.com> References: <20220307155741.1352405-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Instead of passing the allow_dups argument to fsnotify_add_mark() as a 'boolean' int, pass a generic flags argument and define the flag FSNOTIFY_ADD_MARK_ALLOW_DUPS to express the old allow_dups meaning. We are going to use the flags argument to pass more options to to fsnotify_add_mark(). Signed-off-by: Amir Goldstein --- fs/notify/mark.c | 13 ++++++------- include/linux/fsnotify_backend.h | 18 +++++++++--------- kernel/audit_fsnotify.c | 3 ++- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/fs/notify/mark.c b/fs/notify/mark.c index 4853184f7dde..190df435919f 100644 --- a/fs/notify/mark.c +++ b/fs/notify/mark.c @@ -574,7 +574,7 @@ static struct fsnotify_mark_connector *fsnotify_grab_connector( static int fsnotify_add_mark_list(struct fsnotify_mark *mark, fsnotify_connp_t *connp, unsigned int obj_type, - int allow_dups, __kernel_fsid_t *fsid) + int flags, __kernel_fsid_t *fsid) { struct fsnotify_mark *lmark, *last = NULL; struct fsnotify_mark_connector *conn; @@ -633,7 +633,7 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark, if ((lmark->group == mark->group) && (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) && - !allow_dups) { + !(flags & FSNOTIFY_ADD_MARK_ALLOW_DUPS)) { err = -EEXIST; goto out_err; } @@ -668,7 +668,7 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark, */ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, fsnotify_connp_t *connp, unsigned int obj_type, - int allow_dups, __kernel_fsid_t *fsid) + int flags, __kernel_fsid_t *fsid) { struct fsnotify_group *group = mark->group; int ret = 0; @@ -688,7 +688,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, fsnotify_get_mark(mark); /* for g_list */ spin_unlock(&mark->lock); - ret = fsnotify_add_mark_list(mark, connp, obj_type, allow_dups, fsid); + ret = fsnotify_add_mark_list(mark, connp, obj_type, flags, fsid); if (ret) goto err; @@ -708,14 +708,13 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, } int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp, - unsigned int obj_type, int allow_dups, - __kernel_fsid_t *fsid) + unsigned int obj_type, int flags, __kernel_fsid_t *fsid) { int ret; struct fsnotify_group *group = mark->group; mutex_lock(&group->mark_mutex); - ret = fsnotify_add_mark_locked(mark, connp, obj_type, allow_dups, fsid); + ret = fsnotify_add_mark_locked(mark, connp, obj_type, flags, fsid); mutex_unlock(&group->mark_mutex); return ret; } diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 0bd63608e935..0b548518b166 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -633,30 +633,30 @@ extern struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp, /* Get cached fsid of filesystem containing object */ extern int fsnotify_get_conn_fsid(const struct fsnotify_mark_connector *conn, __kernel_fsid_t *fsid); + /* attach the mark to the object */ +#define FSNOTIFY_ADD_MARK_ALLOW_DUPS 0x1 + extern int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp, unsigned int obj_type, - int allow_dups, __kernel_fsid_t *fsid); + int flags, __kernel_fsid_t *fsid); extern int fsnotify_add_mark_locked(struct fsnotify_mark *mark, fsnotify_connp_t *connp, - unsigned int obj_type, int allow_dups, + unsigned int obj_type, int flags, __kernel_fsid_t *fsid); /* attach the mark to the inode */ static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark, - struct inode *inode, - int allow_dups) + struct inode *inode, int flags) { return fsnotify_add_mark(mark, &inode->i_fsnotify_marks, - FSNOTIFY_OBJ_TYPE_INODE, allow_dups, NULL); + FSNOTIFY_OBJ_TYPE_INODE, flags, NULL); } static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark, - struct inode *inode, - int allow_dups) + struct inode *inode, int flags) { return fsnotify_add_mark_locked(mark, &inode->i_fsnotify_marks, - FSNOTIFY_OBJ_TYPE_INODE, allow_dups, - NULL); + FSNOTIFY_OBJ_TYPE_INODE, flags, NULL); } /* given a group and a mark, flag mark to be freed when all references are dropped */ diff --git a/kernel/audit_fsnotify.c b/kernel/audit_fsnotify.c index 02348b48447c..82233f651c62 100644 --- a/kernel/audit_fsnotify.c +++ b/kernel/audit_fsnotify.c @@ -100,7 +100,8 @@ struct audit_fsnotify_mark *audit_alloc_mark(struct audit_krule *krule, char *pa audit_update_mark(audit_mark, dentry->d_inode); audit_mark->rule = krule; - ret = fsnotify_add_inode_mark(&audit_mark->mark, inode, true); + ret = fsnotify_add_inode_mark(&audit_mark->mark, inode, + FSNOTIFY_ADD_MARK_ALLOW_DUPS); if (ret < 0) { fsnotify_put_mark(&audit_mark->mark); audit_mark = ERR_PTR(ret); From patchwork Mon Mar 7 15:57:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12772064 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4276C433F5 for ; Mon, 7 Mar 2022 15:58:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243967AbiCGP7E (ORCPT ); Mon, 7 Mar 2022 10:59:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241613AbiCGP7A (ORCPT ); Mon, 7 Mar 2022 10:59:00 -0500 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6B6631521 for ; Mon, 7 Mar 2022 07:58:04 -0800 (PST) Received: by mail-wr1-x42a.google.com with SMTP id u1so24012779wrg.11 for ; Mon, 07 Mar 2022 07:58:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FhxT+I9yip7vypG+Wqj2xpjVbvXzHRodQBlpPGS8dwo=; b=NFEYHwgxfRmCaO9ZtBOiTkSwfY+qk3hOUH3ZJXKFDNob5Fl0DBdlfFmncC/Kgh31wp FdXrGS7NE2go0yrSGqxtAq+u4Ukks4lkuCVjQK9xytXMYYYHpB2LnDka3qIOoy8YJrCC 5qFnrIK8HEFJIQm+l3pePEl7IqzaeLRQM26f1PE/rYlRu2/XAkf9r8YxuLZ7sU983Vef SqSzZvQD5ZZueTHM9T6ZI6Un1KDonw/r9TceHPJzD35UUBK224Uktfi4JxbIpnK6kZSG WabdGLbPGmfXmMGUpHS+R/alWcSaGn0G4x845TEiA6dY0kiK9usTe6JYzkc/R9fIO5v7 b1Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FhxT+I9yip7vypG+Wqj2xpjVbvXzHRodQBlpPGS8dwo=; b=Qc4vi0sAuiQxrrugHIGrzTqs+moo7lVm+FiOFOlG57prR90zkTYCZCWybp20Xd7PA8 6c9KPqdYMD7ZT52iau/+a/CzTqYLWsmJJVp6R/fji3ynjerB2Q74cIrR70y2bIr69Ko2 Zh0DnUnPSeCZVkSvc5Rf83KPAw+jQo2Q9HWNdsctZS6sJCr/NOkRvZ1XY/GLyabxfvO4 gfxNT9h94UVnIm2YSZGI7aEVS7wNMcQyTMoDDeuHq7CfCfVlkoRHPEUed677+QgdM/R9 zFiecP5oRPtojU/5nqxZmyuAhkWIcONf26EO4nqivlCzBqdwZy7tsacmG7PvpNA8TTB9 8diw== X-Gm-Message-State: AOAM531FOv3FcC378GabFtDk/O2fouuEBHtCsiDFi3lTsRQ45EIqLxTz Dkr1PTM7ccYn7EImdedRSKc= X-Google-Smtp-Source: ABdhPJzTqYGwQvN1gxbj35CLT/A2jfShvlFTgrqJsKwTK5FHd4xhI08cgp0/KrA7F4uhm7pGdxjk+Q== X-Received: by 2002:adf:a4d2:0:b0:1f0:326a:27f3 with SMTP id h18-20020adfa4d2000000b001f0326a27f3mr8995388wrb.382.1646668683222; Mon, 07 Mar 2022 07:58:03 -0800 (PST) Received: from localhost.localdomain ([77.137.71.203]) by smtp.gmail.com with ESMTPSA id g6-20020a5d5406000000b001f049726044sm11686591wrv.79.2022.03.07.07.58.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Mar 2022 07:58:02 -0800 (PST) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH 3/5] fsnotify: allow adding an inode mark without pinning inode Date: Mon, 7 Mar 2022 17:57:39 +0200 Message-Id: <20220307155741.1352405-4-amir73il@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220307155741.1352405-1-amir73il@gmail.com> References: <20220307155741.1352405-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org fsnotify_add_mark() and variants implicitly take a reference on inode when attaching a mark to an inode. Make that behavior opt-out with the flag FSNOTIFY_ADD_MARK_NO_IREF. Instead of taking the inode reference when attaching connector to inode and dropping the inode reference when detaching connector from inode, take the inode reference on attach of the first mark that wants to hold an inode reference and drop the inode reference on detach of the last mark that wants to hold an inode reference. This leaves the choice to the backend whether or not to pin the inode when adding an inode mark. This is intended to be used when adding a mark with ignored mask that is used for optimization in cases where group can afford getting unneeded events and reinstate the mark with ignored mask when inode is accessed again after being evicted. Signed-off-by: Amir Goldstein --- fs/notify/mark.c | 70 ++++++++++++++++++++++++++++---- include/linux/fsnotify_backend.h | 3 ++ 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/fs/notify/mark.c b/fs/notify/mark.c index 190df435919f..f71b6814bfa7 100644 --- a/fs/notify/mark.c +++ b/fs/notify/mark.c @@ -213,6 +213,17 @@ static void *fsnotify_detach_connector_from_object( if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) { inode = fsnotify_conn_inode(conn); inode->i_fsnotify_mask = 0; + + pr_debug("%s: inode=%p iref=%u sb_connectors=%lu icount=%u\n", + __func__, inode, atomic_read(&conn->proxy_iref), + atomic_long_read(&inode->i_sb->s_fsnotify_connectors), + atomic_read(&inode->i_count)); + + /* Unpin inode when detaching from connector */ + if (atomic_read(&conn->proxy_iref)) + atomic_set(&conn->proxy_iref, 0); + else + inode = NULL; } else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) { fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0; } else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) { @@ -240,12 +251,43 @@ static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark) /* Drop object reference originally held by a connector */ static void fsnotify_drop_object(unsigned int type, void *objp) { + struct inode *inode = objp; + if (!objp) return; /* Currently only inode references are passed to be dropped */ if (WARN_ON_ONCE(type != FSNOTIFY_OBJ_TYPE_INODE)) return; - fsnotify_put_inode_ref(objp); + + pr_debug("%s: inode=%p sb_connectors=%lu, icount=%u\n", __func__, + inode, atomic_long_read(&inode->i_sb->s_fsnotify_connectors), + atomic_read(&inode->i_count)); + + fsnotify_put_inode_ref(inode); +} + +/* Drop the proxy refcount on inode maintainted by connector */ +static struct inode *fsnotify_drop_iref(struct fsnotify_mark_connector *conn, + unsigned int *type) +{ + struct inode *inode = fsnotify_conn_inode(conn); + + if (WARN_ON_ONCE(!inode || conn->type != FSNOTIFY_OBJ_TYPE_INODE)) + return NULL; + + pr_debug("%s: inode=%p iref=%u sb_connectors=%lu icount=%u\n", + __func__, inode, atomic_read(&conn->proxy_iref), + atomic_long_read(&inode->i_sb->s_fsnotify_connectors), + atomic_read(&inode->i_count)); + + if (WARN_ON_ONCE(!atomic_read(&conn->proxy_iref)) || + !atomic_dec_and_test(&conn->proxy_iref)) + return NULL; + + fsnotify_put_inode_ref(inode); + *type = FSNOTIFY_OBJ_TYPE_INODE; + + return inode; } void fsnotify_put_mark(struct fsnotify_mark *mark) @@ -275,6 +317,9 @@ void fsnotify_put_mark(struct fsnotify_mark *mark) free_conn = true; } else { __fsnotify_recalc_mask(conn); + /* Unpin inode on last mark that wants inode refcount held */ + if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IREF) + objp = fsnotify_drop_iref(conn, &type); } WRITE_ONCE(mark->connector, NULL); spin_unlock(&conn->lock); @@ -499,7 +544,6 @@ static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp, unsigned int obj_type, __kernel_fsid_t *fsid) { - struct inode *inode = NULL; struct fsnotify_mark_connector *conn; conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL); @@ -507,6 +551,7 @@ static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp, return -ENOMEM; spin_lock_init(&conn->lock); INIT_HLIST_HEAD(&conn->list); + atomic_set(&conn->proxy_iref, 0); conn->type = obj_type; conn->obj = connp; /* Cache fsid of filesystem containing the object */ @@ -517,10 +562,6 @@ static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp, conn->fsid.val[0] = conn->fsid.val[1] = 0; conn->flags = 0; } - if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) { - inode = fsnotify_conn_inode(conn); - fsnotify_get_inode_ref(inode); - } fsnotify_get_sb_connectors(conn); /* @@ -529,8 +570,6 @@ static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp, */ if (cmpxchg(connp, NULL, conn)) { /* Someone else created list structure for us */ - if (inode) - fsnotify_put_inode_ref(inode); fsnotify_put_sb_connectors(conn); kmem_cache_free(fsnotify_mark_connector_cachep, conn); } @@ -649,6 +688,21 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark, /* mark should be the last entry. last is the current last entry */ hlist_add_behind_rcu(&mark->obj_list, &last->obj_list); added: + /* Pin inode on first mark that wants inode refcount held */ + if (conn->type == FSNOTIFY_OBJ_TYPE_INODE && + !(flags & FSNOTIFY_ADD_MARK_NO_IREF)) { + struct inode *inode = fsnotify_conn_inode(conn); + + mark->flags |= FSNOTIFY_MARK_FLAG_HAS_IREF; + if (atomic_inc_return(&conn->proxy_iref) == 1) + fsnotify_get_inode_ref(inode); + + pr_debug("%s: inode=%p iref=%u sb_connectors=%lu icount=%u\n", + __func__, inode, atomic_read(&conn->proxy_iref), + atomic_long_read(&inode->i_sb->s_fsnotify_connectors), + atomic_read(&inode->i_count)); + } + /* * Since connector is attached to object using cmpxchg() we are * guaranteed that connector initialization is fully visible by anyone diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 0b548518b166..de718864c5f5 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -425,6 +425,7 @@ struct fsnotify_mark_connector { unsigned short type; /* Type of object [lock] */ #define FSNOTIFY_CONN_FLAG_HAS_FSID 0x01 unsigned short flags; /* flags [lock] */ + atomic_t proxy_iref; /* marks that want inode reference held */ __kernel_fsid_t fsid; /* fsid of filesystem containing object */ union { /* Object pointer [lock] */ @@ -471,6 +472,7 @@ struct fsnotify_mark { /* Events types to ignore [mark->lock, group->mark_mutex] */ __u32 ignored_mask; /* Internal fsnotify flags */ +#define FSNOTIFY_MARK_FLAG_HAS_IREF 0x01 #define FSNOTIFY_MARK_FLAG_ALIVE 0x02 #define FSNOTIFY_MARK_FLAG_ATTACHED 0x04 /* Backend controlled flags */ @@ -636,6 +638,7 @@ extern int fsnotify_get_conn_fsid(const struct fsnotify_mark_connector *conn, /* attach the mark to the object */ #define FSNOTIFY_ADD_MARK_ALLOW_DUPS 0x1 +#define FSNOTIFY_ADD_MARK_NO_IREF 0x2 extern int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp, unsigned int obj_type, From patchwork Mon Mar 7 15:57:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12772062 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA6F6C433EF for ; Mon, 7 Mar 2022 15:58:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243901AbiCGP7C (ORCPT ); Mon, 7 Mar 2022 10:59:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237151AbiCGP7A (ORCPT ); Mon, 7 Mar 2022 10:59:00 -0500 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2C4731523 for ; Mon, 7 Mar 2022 07:58:05 -0800 (PST) Received: by mail-wm1-x336.google.com with SMTP id l10so5618671wmb.0 for ; Mon, 07 Mar 2022 07:58:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GAmgRFeCNQIyxVKMrZdhVGWCuIC+16jUJaEcAePpDzg=; b=VYUdGOlaujQujuOJ0nh4bdW0/3aDsi+dw/hSfOWplC2rj1L/Xtu19ytJt1Pmu41FJQ k/nJ7K2gvm4NZvjX8UNYcDbFfDjC+cSEpnWCz0Ng1PLEh1qhHHTj+aR9m5BcsJwfb0/A AeHbKDDnOch/zW7moj01Q7Z0pWxuELtqqEMZ7xyty7r3yYAh2NGEj58bdW3m8szSUYnA GdC0vwYrB1+7fsFAU+K6dgGbzeAMvM9WQoAUe928U3x/v8ilg8DuYmnovC9yX8mfjWot vw7DCYvwi2vcmmm+1IOBjCqsJnSpOJB1qWVEjTnYrcsHet6sDXbCk0fz9Ne2PhmAyGTe DW+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GAmgRFeCNQIyxVKMrZdhVGWCuIC+16jUJaEcAePpDzg=; b=N+hlZQC6+k5GdW3utbjRL7Qdz8KPUa8XFkqLfY93/fZXptCUylV+QVp4qC+tnbQJI9 n6Ybmv33N3gsfWJkSUQ89QuNrAk04pKx+Yy31RhfwrERaS0lV07FO9axYwF26bYMY/2F eOAXYYaa6zyA//pYbzpix5C1JMgc0IVaiLfx4s5g5FTR5/fAGf/zAfjmLmUlRtKKGdWl kH2e4Vyo3BNvIpzexpT8Xrkbf0dhHLd0JITPy/Rd1DcXpcDn4lI3mYqsGsrxvy9yVGgu OV+EZXdRrfiq6Pa9VPKoTpuGJrnjnIbjT9Sh1zY65njADYaA2hY9C7Kex0gNQm40xTbD 7ykw== X-Gm-Message-State: AOAM531pqiBr0glhvQVW/ftG9+lk3pFo8OLtDAbqY1IT9TkXoR4ZQ1a1 HUILIxqIXF3hBr7YUDQgmy8b0QZvtyQ= X-Google-Smtp-Source: ABdhPJx7GYUEnWLZtQ3xugcvMBOyyOQQswtYBFrEfoX52TBaHYo7AEc0JZFy925A2tS3+gH4ugrXjA== X-Received: by 2002:a05:600c:4611:b0:389:9f47:30cd with SMTP id m17-20020a05600c461100b003899f4730cdmr6709394wmo.185.1646668684406; Mon, 07 Mar 2022 07:58:04 -0800 (PST) Received: from localhost.localdomain ([77.137.71.203]) by smtp.gmail.com with ESMTPSA id g6-20020a5d5406000000b001f049726044sm11686591wrv.79.2022.03.07.07.58.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Mar 2022 07:58:04 -0800 (PST) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH 4/5] fanotify: add support for exclusive create of mark Date: Mon, 7 Mar 2022 17:57:40 +0200 Message-Id: <20220307155741.1352405-5-amir73il@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220307155741.1352405-1-amir73il@gmail.com> References: <20220307155741.1352405-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Similar to inotify's IN_MARK_CREATE, adding an fanotify mark with flag FAN_MARK_CREATE will fail with error EEXIST if an fanotify mark already exists on the object. Unlike inotify's IN_MARK_CREATE, FAN_MARK_CREATE has to supplied in combination with FAN_MARK_ADD (FAN_MARK_ADD is like inotify_add_watch() and the behavior of IN_MARK_ADD is the default for fanotify_mark()). Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 13 ++++++++++--- include/linux/fanotify.h | 8 +++++--- include/uapi/linux/fanotify.h | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 9b32b76a9c30..99c5ced6abd8 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1185,6 +1185,9 @@ static int fanotify_add_mark(struct fsnotify_group *group, mutex_unlock(&group->mark_mutex); return PTR_ERR(fsn_mark); } + } else if (flags & FAN_MARK_CREATE) { + ret = -EEXIST; + goto out; } /* @@ -1510,6 +1513,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, __kernel_fsid_t __fsid, *fsid = NULL; u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS; unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; + unsigned int mark_cmd = flags & FANOTIFY_MARK_CMD_BITS; bool ignored = flags & FAN_MARK_IGNORED_MASK; unsigned int obj_type, fid_mode; u32 umask = 0; @@ -1539,7 +1543,10 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, return -EINVAL; } - switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { + if (flags & FAN_MARK_CREATE && mark_cmd != FAN_MARK_ADD) + return -EINVAL; + + switch (mark_cmd) { case FAN_MARK_ADD: case FAN_MARK_REMOVE: if (!mask) @@ -1671,7 +1678,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, } /* create/update an inode mark */ - switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) { + switch (mark_cmd) { case FAN_MARK_ADD: if (mark_type == FAN_MARK_MOUNT) ret = fanotify_add_vfsmount_mark(group, mnt, mask, @@ -1749,7 +1756,7 @@ static int __init fanotify_user_setup(void) BUILD_BUG_ON(FANOTIFY_INIT_FLAGS & FANOTIFY_INTERNAL_GROUP_FLAGS); BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 12); - BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); + BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 10); fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC|SLAB_ACCOUNT); diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index 419cadcd7ff5..780f4b17d4c9 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -59,14 +59,16 @@ #define FANOTIFY_MARK_TYPE_BITS (FAN_MARK_INODE | FAN_MARK_MOUNT | \ FAN_MARK_FILESYSTEM) +#define FANOTIFY_MARK_CMD_BITS (FAN_MARK_ADD | FAN_MARK_REMOVE | \ + FAN_MARK_FLUSH) + #define FANOTIFY_MARK_FLAGS (FANOTIFY_MARK_TYPE_BITS | \ - FAN_MARK_ADD | \ - FAN_MARK_REMOVE | \ + FANOTIFY_MARK_CMD_BITS | \ FAN_MARK_DONT_FOLLOW | \ FAN_MARK_ONLYDIR | \ FAN_MARK_IGNORED_MASK | \ FAN_MARK_IGNORED_SURV_MODIFY | \ - FAN_MARK_FLUSH) + FAN_MARK_CREATE) /* * Events that can be reported with data type FSNOTIFY_EVENT_PATH. diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index e8ac38cc2fd6..c41feac21fe9 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -82,6 +82,7 @@ #define FAN_MARK_IGNORED_SURV_MODIFY 0x00000040 #define FAN_MARK_FLUSH 0x00000080 /* FAN_MARK_FILESYSTEM is 0x00000100 */ +#define FAN_MARK_CREATE 0x00000200 /* These are NOT bitwise flags. Both bits can be used togther. */ #define FAN_MARK_INODE 0x00000000 From patchwork Mon Mar 7 15:57:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12772065 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB683C433EF for ; Mon, 7 Mar 2022 15:58:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243980AbiCGP7F (ORCPT ); Mon, 7 Mar 2022 10:59:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243403AbiCGP7B (ORCPT ); Mon, 7 Mar 2022 10:59:01 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 039023153E for ; Mon, 7 Mar 2022 07:58:07 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id b5so24074619wrr.2 for ; Mon, 07 Mar 2022 07:58:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+o3fRMhYc5bUka8DDTFbEWlfpRNsPHxhCkbroUX0brI=; b=Iyb2EY7qYK2Sd/ELFKxNdY4ZvzmNcd7lIyNw62iMuu6WU/wBQfmLIlJjEFkgduNh81 KVybW44gRDho6QGTwrATkRUQf+bqApuJSfXMjl0tJtzZWIU9dh2B3gJRuTE5tNp/Ongk foC0D78MkL8GwngA75B3PjztcDG13GrXVuLwD04F6LEBWFEw3U87AAYO3KvyCFNyPydK uHm4mJjAHZQ9QyzO8zrLi55vlWO0iCckyG+mX8U5LT9C2t6/1dR1Z3TL2HIJPsdsS+sX 0RKo2xOYDPRy4Xm6T8Jup2xHEfErKaXVZA72LcuhseckOdMP4laIgfmPGNe3iNT9weVu yeag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+o3fRMhYc5bUka8DDTFbEWlfpRNsPHxhCkbroUX0brI=; b=3q9/vwO5YEMl3Tgi09hHUIwnMu9E5x+hCM0qxPGT2vO34i5D2d6r2ppvPSJO9nb8vO dQoMblNHkCdCnpXeRq89R9TTQfgdkW2a90f7GkxHFtr5zAN2KX+JP9O0lFWAOAQfasBK WKOIBgnsT5UPf0hx63Mz6UMV5CZTKDbKwRJHC2YRAWDgptFokWBD5XnorO6a+F2tSeC5 JqBFz/NAFY8NfZ6XpfpRRx7dfeQ6QcHJtAnDQg5hDKc6L9VfkcIsr/AcOnoYWoAZ7H61 VezGbva5hxgoS7nkpBjWIMsRjn0DmmkqZ1eQy/FMk9FeurISO9naqO3EWqlu/GLKIUPR lbMw== X-Gm-Message-State: AOAM533ImcdxM2gqbKKcxqfZs5zxBtV0jwYNSY7rgLpURDtonOUIgVXO 4c9Pp7euUIxqh48VaxHclDU= X-Google-Smtp-Source: ABdhPJybkKlg9IvDGHV5j2fh2N8bfHcgfSx4mhUQltqLxvTrLUejjxG5UXkRLDdHANHQ6iav/bsePg== X-Received: by 2002:adf:fd87:0:b0:1ed:e0c3:a2d4 with SMTP id d7-20020adffd87000000b001ede0c3a2d4mr8630629wrr.374.1646668685494; Mon, 07 Mar 2022 07:58:05 -0800 (PST) Received: from localhost.localdomain ([77.137.71.203]) by smtp.gmail.com with ESMTPSA id g6-20020a5d5406000000b001f049726044sm11686591wrv.79.2022.03.07.07.58.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Mar 2022 07:58:05 -0800 (PST) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH 5/5] fanotify: add support for "volatile" inode marks Date: Mon, 7 Mar 2022 17:57:41 +0200 Message-Id: <20220307155741.1352405-6-amir73il@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220307155741.1352405-1-amir73il@gmail.com> References: <20220307155741.1352405-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org When an inode mark is created with flag FAN_MARK_VOLATILE, it will not pin the marked inode to inode cache, so when inode is evicted from cache due to memory pressure, the mark will be lost. Volatile inode marks can be used to setup inode marks with ignored mask to suppress events from uninteresting files or directories in a lazy manner, upon receiving the first event, without having to iterate all the uninteresting files or directories before hand. The volatile inode mark feature allows performing this lazy marks setup without exhausting the system memory with pinned inodes. Link: https://lore.kernel.org/linux-fsdevel/CAOQ4uxiRDpuS=2uA6+ZUM7yG9vVU-u212tkunBmSnP_u=mkv=Q@mail.gmail.com/ Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 21 ++++++++++++++++++--- include/linux/fanotify.h | 1 + include/uapi/linux/fanotify.h | 1 + 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 99c5ced6abd8..6e9e4020ef40 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1122,11 +1122,14 @@ static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, fsnotify_connp_t *connp, unsigned int obj_type, + unsigned int fan_flags, __kernel_fsid_t *fsid) { struct ucounts *ucounts = group->fanotify_data.ucounts; struct fsnotify_mark *mark; int ret; + unsigned int fsn_flags = (fan_flags & FAN_MARK_VOLATILE) ? + FSNOTIFY_ADD_MARK_NO_IREF : 0; /* * Enforce per user marks limits per user in all containing user ns. @@ -1144,7 +1147,7 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, } fsnotify_init_mark(mark, group); - ret = fsnotify_add_mark_locked(mark, connp, obj_type, 0, fsid); + ret = fsnotify_add_mark_locked(mark, connp, obj_type, fsn_flags, fsid); if (ret) { fsnotify_put_mark(mark); goto out_dec_ucounts; @@ -1180,7 +1183,8 @@ static int fanotify_add_mark(struct fsnotify_group *group, mutex_lock(&group->mark_mutex); fsn_mark = fsnotify_find_mark(connp, group); if (!fsn_mark) { - fsn_mark = fanotify_add_new_mark(group, connp, obj_type, fsid); + fsn_mark = fanotify_add_new_mark(group, connp, obj_type, flags, + fsid); if (IS_ERR(fsn_mark)) { mutex_unlock(&group->mark_mutex); return PTR_ERR(fsn_mark); @@ -1604,6 +1608,17 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, mark_type != FAN_MARK_FILESYSTEM) goto fput_and_out; + /* + * Volatile is only relevant for inode, because only inode object can be + * evicted on memory pressure. Inode is pinned when attaching the mark + * to the inode, so require the FAN_MARK_CREATE flag to make sure that + * we are not updating an existing mark on a pinned inode. + */ + if (flags & FAN_MARK_VOLATILE && + (!(flags & FAN_MARK_CREATE) || + mark_type != FAN_MARK_INODE)) + goto fput_and_out; + /* * Events that do not carry enough information to report * event->fd require a group that supports reporting fid. Those @@ -1756,7 +1771,7 @@ static int __init fanotify_user_setup(void) BUILD_BUG_ON(FANOTIFY_INIT_FLAGS & FANOTIFY_INTERNAL_GROUP_FLAGS); BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 12); - BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 10); + BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 11); fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC|SLAB_ACCOUNT); diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index 780f4b17d4c9..bf88c547d93f 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -68,6 +68,7 @@ FAN_MARK_ONLYDIR | \ FAN_MARK_IGNORED_MASK | \ FAN_MARK_IGNORED_SURV_MODIFY | \ + FAN_MARK_VOLATILE | \ FAN_MARK_CREATE) /* diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index c41feac21fe9..1a67e6be994e 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -83,6 +83,7 @@ #define FAN_MARK_FLUSH 0x00000080 /* FAN_MARK_FILESYSTEM is 0x00000100 */ #define FAN_MARK_CREATE 0x00000200 +#define FAN_MARK_VOLATILE 0x00000400 /* These are NOT bitwise flags. Both bits can be used togther. */ #define FAN_MARK_INODE 0x00000000