From patchwork Wed Apr 13 09:09:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811760 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 87F71C433F5 for ; Wed, 13 Apr 2022 09:09:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234272AbiDMJMG (ORCPT ); Wed, 13 Apr 2022 05:12:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234275AbiDMJMD (ORCPT ); Wed, 13 Apr 2022 05:12:03 -0400 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 DAAD21CFE2 for ; Wed, 13 Apr 2022 02:09:40 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id q3so1010090wrj.7 for ; Wed, 13 Apr 2022 02:09:40 -0700 (PDT) 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=UXs35vihM88XJ2kkE3QaoH5UldyApMgkiHQew6I8P28=; b=mDn1Nl4Fp9bEq5ORyeDomf51yk7opH7Jq7v+Odj7mhZ3QbdtSwJP9L+M1VMWk4Pzs/ K21QDeellslBgquSW8nxeRefOMCwKfu5mxUKVVwLnZtr3FaQse1rm5yA9O4A8ZW1gfYd 53B6qRrrDJU0j+1gWMVsrMQj2oBNPXitqa4VcuMyLnNcg/KsE+IN0549K7SgF/S9nVKy RaneE5ZMuEzMBZw7taBgsHR4oy0iNpzFHUFxeUbKj98mZtn2ES3xjINegh16Q4NLZZUk Wy/8/j8dHQ+uMPBKsylKPydXWuSpK8F3Yd0zWfpLoa/4QQGjTvzpCSxKbHSH0Bns40i9 kp0w== 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=UXs35vihM88XJ2kkE3QaoH5UldyApMgkiHQew6I8P28=; b=febM8eNx4i8jMUX2wPwwPqeUE3mSy+RAcBKeojgYzsLn/GmNjAfyuUBnWIffAzBHWr jPg1/MFB8921iXun70672/sZV7kSNW3KmbzkD35c+4KCrFVzEVxzpEKhxP7Ngyn3wnGN n9mE0N5Y+xm/y3oHTp0T3eR5Kd/sEBd+w1k6vQEJms/1D65wm3BH7EzTDhry/L7GEkd8 nuPI/kasxlAHDcezMntR4BhQkf671gPsBSy+ylSGA4QsEL7WIJ9LXkcMEDIeHcRpyuEg nAs2yYfh5qrnfsjv9eeWzcJarMM7aEBLcdw1T7AGNDGHzR2+WnxiP3cedi9g5Xe2sEz8 8Yqw== X-Gm-Message-State: AOAM53117RRhPsV9j0edi74e8WXaw5Em/UYw1sRbSE9nsPXZzjZ2cBUb f0ho4AWNFfVxiOjvGGcqq7s= X-Google-Smtp-Source: ABdhPJy+6/a8/RpOxJRC4tc+N5Xl7SZmIRFSrz8bU2sz1vtSQ2x1LpCg+keFefuB8W1ZQJBqhSB5Sg== X-Received: by 2002:adf:f44d:0:b0:207:b33f:ea7b with SMTP id f13-20020adff44d000000b00207b33fea7bmr3422904wrp.628.1649840979509; Wed, 13 Apr 2022 02:09:39 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:39 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 01/16] inotify: show inotify mask flags in proc fdinfo Date: Wed, 13 Apr 2022 12:09:20 +0300 Message-Id: <20220413090935.3127107-2-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org The inotify mask flags IN_ONESHOT and IN_EXCL_UNLINK are not "internal to kernel" and should be exposed in procfs fdinfo so CRIU can restore them. Fixes: 6933599697c9 ("inotify: hide internal kernel bits from fdinfo") Signed-off-by: Amir Goldstein --- fs/notify/fdinfo.c | 11 ++--------- fs/notify/inotify/inotify.h | 12 ++++++++++++ fs/notify/inotify/inotify_user.c | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/fs/notify/fdinfo.c b/fs/notify/fdinfo.c index 57f0d5d9f934..3451708fd035 100644 --- a/fs/notify/fdinfo.c +++ b/fs/notify/fdinfo.c @@ -83,16 +83,9 @@ static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark) inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark); inode = igrab(fsnotify_conn_inode(mark->connector)); if (inode) { - /* - * IN_ALL_EVENTS represents all of the mask bits - * that we expose to userspace. There is at - * least one bit (FS_EVENT_ON_CHILD) which is - * used only internally to the kernel. - */ - u32 mask = mark->mask & IN_ALL_EVENTS; - seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:%x ", + seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:0 ", inode_mark->wd, inode->i_ino, inode->i_sb->s_dev, - mask, mark->ignored_mask); + inotify_mark_user_mask(mark)); show_mark_fhandle(m, inode); seq_putc(m, '\n'); iput(inode); diff --git a/fs/notify/inotify/inotify.h b/fs/notify/inotify/inotify.h index 2007e3711916..8f00151eb731 100644 --- a/fs/notify/inotify/inotify.h +++ b/fs/notify/inotify/inotify.h @@ -22,6 +22,18 @@ static inline struct inotify_event_info *INOTIFY_E(struct fsnotify_event *fse) return container_of(fse, struct inotify_event_info, fse); } +/* + * INOTIFY_USER_FLAGS represents all of the mask bits that we expose to + * userspace. There is at least one bit (FS_EVENT_ON_CHILD) which is + * used only internally to the kernel. + */ +#define INOTIFY_USER_MASK (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK) + +static inline __u32 inotify_mark_user_mask(struct fsnotify_mark *fsn_mark) +{ + return fsn_mark->mask & INOTIFY_USER_MASK; +} + extern void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark, struct fsnotify_group *group); extern int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c index 54583f62dc44..3ef57db0ec9d 100644 --- a/fs/notify/inotify/inotify_user.c +++ b/fs/notify/inotify/inotify_user.c @@ -110,7 +110,7 @@ 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 & INOTIFY_USER_MASK); return mask; } From patchwork Wed Apr 13 09:09:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811761 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 2963AC433EF for ; Wed, 13 Apr 2022 09:09:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234283AbiDMJMK (ORCPT ); Wed, 13 Apr 2022 05:12:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234286AbiDMJMD (ORCPT ); Wed, 13 Apr 2022 05:12:03 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AD571CFEE for ; Wed, 13 Apr 2022 02:09:42 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id m14so1647321wrb.6 for ; Wed, 13 Apr 2022 02:09:42 -0700 (PDT) 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=MPpwsLuqEQgOfuuaDtCHGwQaMztJ8PuWPMUjX+42Cm8=; b=eXSBXyAhRlEIwuYV4olWkLA26E7dQEQhuJDjtmbznunAOemkFHkU19mvPI6Qyz5qUz wt0bWcU7NV70bhVlsVQUtfL47VfY+a8DK0zV4IkPikSqxN55JlrwXj8DU9LgcVKESmjc JQzEYkwUWo/j9z7hI3ZuhIni1oATIJd9Jc2xhjfbg5nxpIJUG7J55i2LaKHC+IQS7kin 8dSeeC38yKO8dbuF7xgnn2uUNv8uOc2RcyU6edrzMRJxoExl7GaZ9GaPjIadqDA8QqoO aeynrgy1KwpcFy7eiQTatLaYi8ikP403dQzPhOi/VzBKYMQIIYRn3CflGZp9lNGtGICz gc9Q== 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=MPpwsLuqEQgOfuuaDtCHGwQaMztJ8PuWPMUjX+42Cm8=; b=FnM/AYLwRzn37EfJs9N1ITFZ/zyOXDq2gWXx+TTIP4WsvXT6GFufpwCDQKgNrFFfe/ dlG73Al6N+UkD8rZRsfKKpnXQFBhq+WPN7zp0jaFikbPGo2JtYEyP8SvNhS1y4K0rZq9 xOIRQTIyOv9soR245adYp67Bn+kH2GESO5Vcs4Cj8WIblcBWB75RFgLCz5OGyVfIn/wZ cbxZQY9TLGxOf0k6p0F8lCUQxeYvtYZcY42KwcbXeV0R0Wj1C2llcp4Z0HmhRymorrXI xR/hj8lvqeI/IzrA+7wJ3O/k5FQkrEF8l2zF4x5afNG1rDKnkuQ4jsSpFQGmc5ZQN6iE rG7Q== X-Gm-Message-State: AOAM531+aY7EPfc45t/vd7OD8wKF+vEKtUO/VN5jsyqTHV/zTJlShvcE wQLHhdi6Br9fOLJI14s1wCpQaY45sZU= X-Google-Smtp-Source: ABdhPJwmqzBMkwKdm9nnVewgp1364DRdW9oODjZuuOlTrDWzhkkglJ8CUO3g6aUTMGtDQ6wDbdE5Yg== X-Received: by 2002:a5d:6788:0:b0:207:ad57:cc71 with SMTP id v8-20020a5d6788000000b00207ad57cc71mr6053349wru.180.1649840980779; Wed, 13 Apr 2022 02:09:40 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:40 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 02/16] inotify: move control flags from mask to mark flags Date: Wed, 13 Apr 2022 12:09:21 +0300 Message-Id: <20220413090935.3127107-3-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-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.h | 11 ++++++-- fs/notify/inotify/inotify_fsnotify.c | 2 +- fs/notify/inotify/inotify_user.c | 38 ++++++++++++++++++---------- include/linux/fsnotify_backend.h | 16 +++++++----- 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 70a8516b78bc..6eee19d15e8c 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -253,7 +253,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; @@ -581,7 +581,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.h b/fs/notify/inotify/inotify.h index 8f00151eb731..7d5df7a21539 100644 --- a/fs/notify/inotify/inotify.h +++ b/fs/notify/inotify/inotify.h @@ -27,11 +27,18 @@ static inline struct inotify_event_info *INOTIFY_E(struct fsnotify_event *fse) * userspace. There is at least one bit (FS_EVENT_ON_CHILD) which is * used only internally to the kernel. */ -#define INOTIFY_USER_MASK (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK) +#define INOTIFY_USER_MASK (IN_ALL_EVENTS) static inline __u32 inotify_mark_user_mask(struct fsnotify_mark *fsn_mark) { - return fsn_mark->mask & INOTIFY_USER_MASK; + __u32 mask = fsn_mark->mask & INOTIFY_USER_MASK; + + if (fsn_mark->flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK) + mask |= IN_EXCL_UNLINK; + if (fsn_mark->flags & FSNOTIFY_MARK_FLAG_IN_ONESHOT) + mask |= IN_ONESHOT; + + return mask; } extern void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark, 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 3ef57db0ec9d..d8907d32a05b 100644 --- a/fs/notify/inotify/inotify_user.c +++ b/fs/notify/inotify/inotify_user.c @@ -115,6 +115,21 @@ static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg) 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..b1c72edd9784 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,14 @@ 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 -#define FSNOTIFY_MARK_FLAG_ALIVE 0x02 -#define FSNOTIFY_MARK_FLAG_ATTACHED 0x04 + /* General fsnotify mark flags */ +#define FSNOTIFY_MARK_FLAG_ALIVE 0x0001 +#define FSNOTIFY_MARK_FLAG_ATTACHED 0x0002 + /* inotify mark flags */ +#define FSNOTIFY_MARK_FLAG_EXCL_UNLINK 0x0010 +#define FSNOTIFY_MARK_FLAG_IN_ONESHOT 0x0020 + /* fanotify mark flags */ +#define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x0100 unsigned int flags; /* flags [mark->lock] */ }; From patchwork Wed Apr 13 09:09:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811762 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 51859C433EF for ; Wed, 13 Apr 2022 09:09:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234289AbiDMJMN (ORCPT ); Wed, 13 Apr 2022 05:12:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230111AbiDMJMF (ORCPT ); Wed, 13 Apr 2022 05:12:05 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B58B91DA49 for ; Wed, 13 Apr 2022 02:09:43 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id p18so826415wru.5 for ; Wed, 13 Apr 2022 02:09:43 -0700 (PDT) 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=OAEdHhjLwuYPCqjVFR53AS/kwFMiTKuiUBzvVoBwHmE=; b=X+cSrp/Uuv8T7CBKlQ3ugWF/xLISJ2pVoY2itUr8+MGhZaDlFx1Bkf2/Beim5x0mHp DMVGmyF7IgjDJhSak1OTy5zwV79QZGcZverFe5ESTPa4RiMQ5bpn1NNIqpUUGkVIA0QT /cZelOy5843Z7SFn0jvBEoBSz9xL/qhHoyqswbBv+3NLGIRDSgIn3GwiGnYW7k2yZ0GR EGyxRwDst4CghvMdLiWhl4QSqEAK8Nhg+A99cODQ1R2d6m6Y4enPZZb6Bw+/2Tk5NsFH /mccUTD0qTc6tc7pxjkJhqHGTqaUGVWRvwulaAbXw7WPvcH70UysOzP3IG1ofx7Ui0yF c+IA== 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=OAEdHhjLwuYPCqjVFR53AS/kwFMiTKuiUBzvVoBwHmE=; b=6xeQCWuFxwsUK1OsslCAOFdr71PR+laSnitwrSUluV5MjVM49Oiobkblr7kXc+15LK BPDzy6d4OleNTjH11/ShLttUGMilMqtkdo31wcB77IjgRhFSiN2P9mGqGe7G2ww5xpJx FzspF38Wfo35aBM3VPRZ1hbA/Y1XsVZqAs74uUv4oXov7FJ8aLCGjms485kS6lS77GK4 IUCdPjV+k2yYIZSnMc4jkTiSKVCoPc1UooAlyiKo4WnLn/3jPQXTe+b3aqgf1mV0wcV2 CBnndBS9gm7QQ1RhZhuxOvQ3wPsI/BCcfULGqeQSa82cDgPt+txVrOySZZiRmW6Qd5+8 turg== X-Gm-Message-State: AOAM5337lWYMKmjMwP8EMPN+gO+KNEar6pClQQWJPnq+dQRwBu2UDk4S B6cH0o/YMSB/CboctwoRKsw= X-Google-Smtp-Source: ABdhPJxlY/B6E4d3kZUO4hPub9C/MZrIkrgq59bUqMvo3K9M+45kuoxxJ9EcKvC9TZBzLnUjwAZPDg== X-Received: by 2002:adf:f3cc:0:b0:207:ad57:cc53 with SMTP id g12-20020adff3cc000000b00207ad57cc53mr6264776wrp.58.1649840982268; Wed, 13 Apr 2022 02:09:42 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:41 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 03/16] fsnotify: fix wrong lockdep annotations Date: Wed, 13 Apr 2022 12:09:22 +0300 Message-Id: <20220413090935.3127107-4-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Commit 6960b0d909cd ("fsnotify: change locking order") changed some of the mark_mutex locks in direct reclaim path to use: mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); This change is explained: "...It uses nested locking to avoid deadlock in case we do the final iput() on an inode which still holds marks and thus would take the mutex again when calling fsnotify_inode_delete() in destroy_inode()." The problem is that the mutex_lock_nested() is not a nested lock at all. In fact, it has the opposite effect of preventing lockdep from warning about a very possible deadlock. Due to these wrong annotations, a deadlock that was introduced with nfsd filecache in kernel v5.4 went unnoticed in v5.4.y for over two years until it was reported recently by Khazhismel Kumykov, only to find out that the deadlock was already fixed in kernel v5.5. Fix the wrong lockdep annotations. Cc: Khazhismel Kumykov Fixes: 6960b0d909cd ("fsnotify: change locking order") Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/ Signed-off-by: Amir Goldstein --- fs/notify/mark.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fs/notify/mark.c b/fs/notify/mark.c index 4853184f7dde..c86982be2d50 100644 --- a/fs/notify/mark.c +++ b/fs/notify/mark.c @@ -452,7 +452,7 @@ void fsnotify_free_mark(struct fsnotify_mark *mark) void fsnotify_destroy_mark(struct fsnotify_mark *mark, struct fsnotify_group *group) { - mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); + mutex_lock(&group->mark_mutex); fsnotify_detach_mark(mark); mutex_unlock(&group->mark_mutex); fsnotify_free_mark(mark); @@ -770,7 +770,7 @@ void fsnotify_clear_marks_by_group(struct fsnotify_group *group, * move marks to free to to_free list in one go and then free marks in * to_free list one by one. */ - mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); + mutex_lock(&group->mark_mutex); list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { if (mark->connector->type == obj_type) list_move(&mark->g_list, &to_free); @@ -779,7 +779,7 @@ void fsnotify_clear_marks_by_group(struct fsnotify_group *group, clear: while (1) { - mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); + mutex_lock(&group->mark_mutex); if (list_empty(head)) { mutex_unlock(&group->mark_mutex); break; From patchwork Wed Apr 13 09:09:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811763 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 88AFEC433F5 for ; Wed, 13 Apr 2022 09:09:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234296AbiDMJMP (ORCPT ); Wed, 13 Apr 2022 05:12:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234276AbiDMJMH (ORCPT ); Wed, 13 Apr 2022 05:12:07 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25EA61EC63 for ; Wed, 13 Apr 2022 02:09:45 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id t1so1654504wra.4 for ; Wed, 13 Apr 2022 02:09:45 -0700 (PDT) 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=eps8yFsEpxS8bu8r3iUtycLYs0pOhOSbpDv2fHjkFlI=; b=LdffZ9sX73iEbZcStazRQnNVmkfje0HqnBOsCf9IkHC1mfyDlEXqWVl0OBt3+1bHWZ D+1mQ5ZrOg34D/4lbrkCo3QCQis9VC8cNhPUHtpqmEFgWABYjYl265bkZf8a3/7yzt35 6BUmHklBKr56bQwhUAPvXGyYsr2GI1Z1wH0Zn0kjOCJKuwPuemX6qvDK9JZrhu6/lTPL l/4neAlEZ/cJL3vTwqeru4cfYLY81e/AuaM7moWL682ypdreayu9jLgt05AY+06AqV5n 6nntNRB0OICLuy5wLi7lXHAhg+aaYeBnpt5/nRnTu2nGDNHTg2uMx+X9UYWGtF0qozyC ELWA== 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=eps8yFsEpxS8bu8r3iUtycLYs0pOhOSbpDv2fHjkFlI=; b=mFx9qJevuAQtlfvV5Eeg471Rhq8tbuISh75kps0Kz5h+Q5DxiWQVy48HHSfoNhML2k clmbwlizum2bRVLses9B8EQRNfBzZGQyqwn1J0aTioIHybmuYbxZVitGyI1KYamhqPLw Un64FrU4ZYgXZmtgiIgWkxxM1IiBalr4jAg635WdZrzgtwZnnIzRFrIDzAIB8fnP7TFD 4DY2SmCGfUyPry+hzEKbSXzb1opf48wZ77uSUZj3Zlw30hL1auVblxb9JO+sJcCzctV6 4tLI1pTWNW5Nm9v0mtwmjEvJcV1LNau1vHHbU5cOw0K3iZdzSkOnlwybkVZDHySefOIq IjdA== X-Gm-Message-State: AOAM531W69bWV/x3UL9hOcpqkg0TxMX4Rp2BpS0DXI5HkWTkwO8aGOoi O+5xHcrh23EdyQd1MqFZG8Y= X-Google-Smtp-Source: ABdhPJyQdf/fvYgjkuq6d67c/TOCmo5xVSBjDhMSF1IKWj7FC2hNH2xRFfs0dMb6uZR9gOYxDZ0Tbg== X-Received: by 2002:a05:6000:1842:b0:207:9b57:6bbf with SMTP id c2-20020a056000184200b002079b576bbfmr17130352wri.336.1649840983554; Wed, 13 Apr 2022 02:09:43 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:43 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 04/16] fsnotify: pass flags argument to fsnotify_add_mark() via mark Date: Wed, 13 Apr 2022 12:09:23 +0300 Message-Id: <20220413090935.3127107-5-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-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 an argument, define the mark flag FSNOTIFY_MARK_FLAG_ALLOW_DUPS to express the old allow_dups meaning and pass this information on the mark itself. We use mark->flags to pass inotify control flags and will pass more control flags in the future so let's make this the standard. Although the FSNOTIFY_MARK_FLAG_ALLOW_DUPS flag is not used after the call to fsnotify_add_mark(), it does not hurt to leave this information on the mark for future use. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 2 +- fs/notify/inotify/inotify_user.c | 4 ++-- fs/notify/mark.c | 13 ++++++------- include/linux/fsnotify_backend.h | 19 ++++++++++--------- kernel/audit_fsnotify.c | 3 ++- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 9b32b76a9c30..0f0db1efa379 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1144,7 +1144,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, fsid); if (ret) { fsnotify_put_mark(mark); goto out_dec_ucounts; diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c index d8907d32a05b..6fc0f598a7aa 100644 --- a/fs/notify/inotify/inotify_user.c +++ b/fs/notify/inotify/inotify_user.c @@ -603,7 +603,6 @@ static int inotify_new_watch(struct fsnotify_group *group, fsnotify_init_mark(&tmp_i_mark->fsn_mark, group); 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); @@ -618,7 +617,8 @@ static int inotify_new_watch(struct fsnotify_group *group, } /* we are on the idr, now get on the inode */ - ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0); + ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, + inotify_arg_to_flags(arg)); if (ret) { /* we failed to get on the inode, get off the idr */ inotify_remove_from_idr(group, tmp_i_mark); diff --git a/fs/notify/mark.c b/fs/notify/mark.c index c86982be2d50..ea8f557881b1 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) + __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) { + !(mark->flags & FSNOTIFY_MARK_FLAG_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) + __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, 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, __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, fsid); mutex_unlock(&group->mark_mutex); return ret; } diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index b1c72edd9784..2ff686882303 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -473,6 +473,7 @@ struct fsnotify_mark { /* General fsnotify mark flags */ #define FSNOTIFY_MARK_FLAG_ALIVE 0x0001 #define FSNOTIFY_MARK_FLAG_ATTACHED 0x0002 +#define FSNOTIFY_MARK_FLAG_ALLOW_DUPS 0x0004 /* inotify mark flags */ #define FSNOTIFY_MARK_FLAG_EXCL_UNLINK 0x0010 #define FSNOTIFY_MARK_FLAG_IN_ONESHOT 0x0020 @@ -634,30 +635,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 */ extern int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp, unsigned int obj_type, - int allow_dups, __kernel_fsid_t *fsid); + __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, __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) { + mark->flags = flags; return fsnotify_add_mark(mark, &inode->i_fsnotify_marks, - FSNOTIFY_OBJ_TYPE_INODE, allow_dups, NULL); + FSNOTIFY_OBJ_TYPE_INODE, NULL); } static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark, - struct inode *inode, - int allow_dups) + struct inode *inode, int flags) { + mark->flags = flags; return fsnotify_add_mark_locked(mark, &inode->i_fsnotify_marks, - FSNOTIFY_OBJ_TYPE_INODE, allow_dups, - NULL); + FSNOTIFY_OBJ_TYPE_INODE, 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..3c35649bc7f5 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_MARK_FLAG_ALLOW_DUPS); if (ret < 0) { fsnotify_put_mark(&audit_mark->mark); audit_mark = ERR_PTR(ret); From patchwork Wed Apr 13 09:09:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811764 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 BCFF7C433EF for ; Wed, 13 Apr 2022 09:10:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234307AbiDMJMS (ORCPT ); Wed, 13 Apr 2022 05:12:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233053AbiDMJMI (ORCPT ); Wed, 13 Apr 2022 05:12:08 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FE8D19281 for ; Wed, 13 Apr 2022 02:09:46 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 123-20020a1c1981000000b0038b3616a71aso738293wmz.4 for ; Wed, 13 Apr 2022 02:09:46 -0700 (PDT) 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=vnnFavNEYKMelMf26drUVQFBja+hKNlJs4XMRp/MaJQ=; b=S3N7KR9lBdVsxIbMvq1ys48Efe+Az+fSKJL7UT62Bhndgcx4UhqJX7nfBnhwLKuaXb 6wjL12V4+PVVlilevYFbUZNQev8r9IjOsWYgX7J9F8oHT7KhVr40H4tvLV5HcbR+05w9 QfU8GYrBx52xJhwYv6LyyJetGFmbaMetND0E8oy/2aiBUyq18+SPJ5quHgbdySYCY/9E 3aiQzPJ/trnwrMQ662vAuSa7HF6mBblM5dAOaCQSxJnNYP/KHcu2MbNPArd5ehkvgudK WoD8DEuvAAASQzXWMq4XFtZdoYSs6adg4lvBdj3HHAtsNZ+U8NjZa2Dp84m+mnRvjyeo Aq4w== 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=vnnFavNEYKMelMf26drUVQFBja+hKNlJs4XMRp/MaJQ=; b=B9e8gAwq6LZLpY2kORrbhZTAmUonpa5TlQfOc/AL7o2KCJ4/IoUXbsnjdyh+GpDX5/ aXz5Z0qUTPMcb1ix7LQus0z7vf+onMgAUeFvy/G/oTyL1f+ja0CRG2i6FdMCKYuVadBY lgDq8JoWfAqW7qxAVa5L1Mow3uuFI49tjT7TZhRxkR5wtHnxLfAcb4/0YuTB/Oeznpej ks//DQmPZRR6xmYedRewgQZt0knsIGn1X22lmUo/WAxCFy2FxNWT/BZGHlUgl/McDYEg +qu/2PxSw7GJIWKfxCK0OfdNUto1db4DzhRJzm2tOkoB1yJnE/5hslxiXXDFPvdi4F5v 54wg== X-Gm-Message-State: AOAM531J5XrWoC1pER9V4W996O6mNiEtpOH+KM/IWsfRROqNOkQ05n/S AsvIj1cZb6aOr1eMp6CAJ1bCFyqPQMA= X-Google-Smtp-Source: ABdhPJx4jZ2Dxb4ChtKDKfBjrUaa+Sjvg82Ez4UoviyWp/iFKYAMdx3rwnb4Hg5H4FKXyBbs8+RgOA== X-Received: by 2002:a7b:c057:0:b0:37b:ebad:c9c8 with SMTP id u23-20020a7bc057000000b0037bebadc9c8mr7560486wmc.61.1649840985059; Wed, 13 Apr 2022 02:09:45 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:44 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 05/16] fsnotify: pass flags argument to fsnotify_alloc_group() Date: Wed, 13 Apr 2022 12:09:24 +0300 Message-Id: <20220413090935.3127107-6-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Add flags argument to fsnotify_alloc_group(), define and use the flag FSNOTIFY_GROUP_USER in inotify and fanotify instead of the helper fsnotify_alloc_user_group() to indicate user allocation. Although the flag FSNOTIFY_GROUP_USER is currently not used after group allocation, we store the flags argument in the group struct for future use of other group flags. Signed-off-by: Amir Goldstein --- fs/nfsd/filecache.c | 3 ++- fs/notify/dnotify/dnotify.c | 2 +- fs/notify/fanotify/fanotify_user.c | 3 ++- fs/notify/group.c | 21 +++++++++------------ fs/notify/inotify/inotify_user.c | 3 ++- include/linux/fsnotify_backend.h | 10 ++++++++-- kernel/audit_fsnotify.c | 3 ++- kernel/audit_tree.c | 2 +- kernel/audit_watch.c | 2 +- 9 files changed, 28 insertions(+), 21 deletions(-) diff --git a/fs/nfsd/filecache.c b/fs/nfsd/filecache.c index c08882f5867b..79a5b052fcdf 100644 --- a/fs/nfsd/filecache.c +++ b/fs/nfsd/filecache.c @@ -673,7 +673,8 @@ nfsd_file_cache_init(void) goto out_shrinker; } - nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops); + nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops, + 0); if (IS_ERR(nfsd_file_fsnotify_group)) { pr_err("nfsd: unable to create fsnotify group: %ld\n", PTR_ERR(nfsd_file_fsnotify_group)); diff --git a/fs/notify/dnotify/dnotify.c b/fs/notify/dnotify/dnotify.c index 829dd4a61b66..e4779926edf4 100644 --- a/fs/notify/dnotify/dnotify.c +++ b/fs/notify/dnotify/dnotify.c @@ -401,7 +401,7 @@ static int __init dnotify_init(void) SLAB_PANIC|SLAB_ACCOUNT); dnotify_mark_cache = KMEM_CACHE(dnotify_mark, SLAB_PANIC|SLAB_ACCOUNT); - dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops); + dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops, 0); if (IS_ERR(dnotify_group)) panic("unable to allocate fsnotify group for dnotify\n"); dnotify_sysctl_init(); diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 0f0db1efa379..bf72856da42e 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1355,7 +1355,8 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) f_flags |= O_NONBLOCK; /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ - group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops); + group = fsnotify_alloc_group(&fanotify_fsnotify_ops, + FSNOTIFY_GROUP_USER); if (IS_ERR(group)) { return PTR_ERR(group); } diff --git a/fs/notify/group.c b/fs/notify/group.c index b7d4d64f87c2..18446b7b0d49 100644 --- a/fs/notify/group.c +++ b/fs/notify/group.c @@ -112,7 +112,8 @@ void fsnotify_put_group(struct fsnotify_group *group) EXPORT_SYMBOL_GPL(fsnotify_put_group); static struct fsnotify_group *__fsnotify_alloc_group( - const struct fsnotify_ops *ops, gfp_t gfp) + const struct fsnotify_ops *ops, + int flags, gfp_t gfp) { struct fsnotify_group *group; @@ -133,6 +134,7 @@ static struct fsnotify_group *__fsnotify_alloc_group( INIT_LIST_HEAD(&group->marks_list); group->ops = ops; + group->flags = flags; return group; } @@ -140,20 +142,15 @@ static struct fsnotify_group *__fsnotify_alloc_group( /* * Create a new fsnotify_group and hold a reference for the group returned. */ -struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops) +struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops, + int flags) { - return __fsnotify_alloc_group(ops, GFP_KERNEL); -} -EXPORT_SYMBOL_GPL(fsnotify_alloc_group); + gfp_t gfp = (flags & FSNOTIFY_GROUP_USER) ? GFP_KERNEL_ACCOUNT : + GFP_KERNEL; -/* - * Create a new fsnotify_group and hold a reference for the group returned. - */ -struct fsnotify_group *fsnotify_alloc_user_group(const struct fsnotify_ops *ops) -{ - return __fsnotify_alloc_group(ops, GFP_KERNEL_ACCOUNT); + return __fsnotify_alloc_group(ops, flags, gfp); } -EXPORT_SYMBOL_GPL(fsnotify_alloc_user_group); +EXPORT_SYMBOL_GPL(fsnotify_alloc_group); int fsnotify_fasync(int fd, struct file *file, int on) { diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c index 6fc0f598a7aa..65ff637cb4a3 100644 --- a/fs/notify/inotify/inotify_user.c +++ b/fs/notify/inotify/inotify_user.c @@ -656,7 +656,8 @@ static struct fsnotify_group *inotify_new_group(unsigned int max_events) struct fsnotify_group *group; struct inotify_event_info *oevent; - group = fsnotify_alloc_user_group(&inotify_fsnotify_ops); + group = fsnotify_alloc_group(&inotify_fsnotify_ops, + FSNOTIFY_GROUP_USER); if (IS_ERR(group)) return group; diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 2ff686882303..2057ae4bf8e9 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -210,6 +210,11 @@ struct fsnotify_group { unsigned int priority; bool shutdown; /* group is being shut down, don't queue more events */ +#define FSNOTIFY_GROUP_USER 0x01 /* user allocated group */ +#define FSNOTIFY_GROUP_FLAG(group, flag) \ + ((group)->flags & FSNOTIFY_GROUP_ ## flag) + int flags; + /* stores all fastpath marks assoc with this group so they can be cleaned on unregister */ struct mutex mark_mutex; /* protect marks_list */ atomic_t user_waits; /* Number of tasks waiting for user @@ -544,8 +549,9 @@ static inline void fsnotify_update_flags(struct dentry *dentry) /* called from fsnotify listeners, such as fanotify or dnotify */ /* create a new group */ -extern struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops); -extern struct fsnotify_group *fsnotify_alloc_user_group(const struct fsnotify_ops *ops); +extern struct fsnotify_group *fsnotify_alloc_group( + const struct fsnotify_ops *ops, + int flags); /* get reference to a group */ extern void fsnotify_get_group(struct fsnotify_group *group); /* drop reference on a group from fsnotify_alloc_group */ diff --git a/kernel/audit_fsnotify.c b/kernel/audit_fsnotify.c index 3c35649bc7f5..95e8b75e7634 100644 --- a/kernel/audit_fsnotify.c +++ b/kernel/audit_fsnotify.c @@ -182,7 +182,8 @@ static const struct fsnotify_ops audit_mark_fsnotify_ops = { static int __init audit_fsnotify_init(void) { - audit_fsnotify_group = fsnotify_alloc_group(&audit_mark_fsnotify_ops); + audit_fsnotify_group = fsnotify_alloc_group(&audit_mark_fsnotify_ops, + 0); if (IS_ERR(audit_fsnotify_group)) { audit_fsnotify_group = NULL; audit_panic("cannot create audit fsnotify group"); diff --git a/kernel/audit_tree.c b/kernel/audit_tree.c index e7315d487163..b5c02f8573fe 100644 --- a/kernel/audit_tree.c +++ b/kernel/audit_tree.c @@ -1074,7 +1074,7 @@ static int __init audit_tree_init(void) audit_tree_mark_cachep = KMEM_CACHE(audit_tree_mark, SLAB_PANIC); - audit_tree_group = fsnotify_alloc_group(&audit_tree_ops); + audit_tree_group = fsnotify_alloc_group(&audit_tree_ops, 0); if (IS_ERR(audit_tree_group)) audit_panic("cannot initialize fsnotify group for rectree watches"); diff --git a/kernel/audit_watch.c b/kernel/audit_watch.c index 713b256be944..4b0957aa2cd4 100644 --- a/kernel/audit_watch.c +++ b/kernel/audit_watch.c @@ -493,7 +493,7 @@ static const struct fsnotify_ops audit_watch_fsnotify_ops = { static int __init audit_watch_init(void) { - audit_watch_group = fsnotify_alloc_group(&audit_watch_fsnotify_ops); + audit_watch_group = fsnotify_alloc_group(&audit_watch_fsnotify_ops, 0); if (IS_ERR(audit_watch_group)) { audit_watch_group = NULL; audit_panic("cannot create audit fsnotify group"); From patchwork Wed Apr 13 09:09:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811766 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 82B50C433EF for ; Wed, 13 Apr 2022 09:10:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234315AbiDMJMX (ORCPT ); Wed, 13 Apr 2022 05:12:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234280AbiDMJMI (ORCPT ); Wed, 13 Apr 2022 05:12:08 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E27551CFEE for ; Wed, 13 Apr 2022 02:09:47 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id e21so1639529wrc.8 for ; Wed, 13 Apr 2022 02:09:47 -0700 (PDT) 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=I1whyWEIXH2ivb44csyQcGxAj08ELh3kxzmcthXa6UQ=; b=BJPkIEzH081jNyjdpNxRltIBRVGDULvBD4baXDCVH6fNG6wd9Yo+a0HO231otplyfR qRH9ND5d871fOOH5DSRnZtLHRTEiK5svUttdCOaGnMnkK4TVf/e0h+jhnO2Ap7hIh4kE 2JKk6pk+dSCNygYA6O8S9xwHVDrjdrcJ52GvB7R3OB3OOJZKeQ1rKcM3KRk8Y9MbLldG AQ9B6j/wzDlfJlJhV8NuVv4Y6bHlaPffogr/m9GBSeQaoOF2D9eOKPuofbYxK/Y77EgY qwaUCUhYOM/Rt9E/hST0LJcaPVyNopAp7vykyJLxLBoHG5Md5QWCkRSCovqYdwWDNgQL vvqw== 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=I1whyWEIXH2ivb44csyQcGxAj08ELh3kxzmcthXa6UQ=; b=6M4hKhxBQQ9wMQ6BpRPM/JR3gQ/38hw7KdzvjNrsbbsSItBe7LuqXAoHRppGPKyZqc bcZqMyMWOn9pPYC8xViFC0Z++cKEz1cx5fm39+dlG8PGDc/D8LmEJJQ7IJWUeWVx1acE HrFm+vytevsf5ou3W9YtG6kbTcTN78Zi6Gq1GBq+B1/b3+vUFzEozMi5eWayV9krkbMS CwcN4yXT0iKQsKxP9QdBTc9CoSEZS+vqwwsnpApHL+hOeE1/GxOfTvWKZSsqiDHlIRRq LLd3r9uD+6/++gCRuwCd2TKubf7h5cGmRXrwe4VCaOyGBzA6+Ye9YV6SMlvinDnLjdOL W8Xw== X-Gm-Message-State: AOAM533t7La4c5wQMGFw5MwzVj/mPJy3DVBAACf9Pmw7ZwOKW8hvlZI2 Me8ul1Cax+SUCuBRGHgDFCSHVN6W0vM= X-Google-Smtp-Source: ABdhPJxFXPfKfr0f7/HhpWBwBRxT6SCVqUGZKoCaW8EHwt0QtCL3hogcngXS6fPdjVbUvTRNef7+RQ== X-Received: by 2002:adf:eb89:0:b0:1e4:b8f4:da74 with SMTP id t9-20020adfeb89000000b001e4b8f4da74mr32694744wrn.408.1649840986471; Wed, 13 Apr 2022 02:09:46 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:46 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 06/16] fsnotify: create helpers for group mark_mutex lock Date: Wed, 13 Apr 2022 12:09:25 +0300 Message-Id: <20220413090935.3127107-7-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Create helpers to take and release the group mark_mutex lock. Define a flag FSNOTIFY_GROUP_NOFS in fsnotify_group that determines if the mark_mutex lock is fs reclaim safe or not. If not safe, the lock helpers take the lock and disable direct fs reclaim. In that case we annotate the mutex with a different lockdep class to express to lockdep that an allocation of mark of an fs reclaim safe group may take the group lock of another "NOFS" group to evict inodes. For now, converted only the callers in common code and no backend defines the NOFS flag. It is intended to be set by fanotify for evictable marks support. Suggested-by: Jan Kara Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/ Signed-off-by: Amir Goldstein --- fs/notify/fdinfo.c | 4 ++-- fs/notify/group.c | 11 +++++++++++ fs/notify/mark.c | 24 +++++++++++------------- include/linux/fsnotify_backend.h | 28 ++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/fs/notify/fdinfo.c b/fs/notify/fdinfo.c index 3451708fd035..1f34c5c29fdb 100644 --- a/fs/notify/fdinfo.c +++ b/fs/notify/fdinfo.c @@ -28,13 +28,13 @@ static void show_fdinfo(struct seq_file *m, struct file *f, struct fsnotify_group *group = f->private_data; struct fsnotify_mark *mark; - mutex_lock(&group->mark_mutex); + fsnotify_group_lock(group); list_for_each_entry(mark, &group->marks_list, g_list) { show(m, mark); if (seq_has_overflowed(m)) break; } - mutex_unlock(&group->mark_mutex); + fsnotify_group_unlock(group); } #if defined(CONFIG_EXPORTFS) diff --git a/fs/notify/group.c b/fs/notify/group.c index 18446b7b0d49..3664029af5ae 100644 --- a/fs/notify/group.c +++ b/fs/notify/group.c @@ -115,6 +115,7 @@ static struct fsnotify_group *__fsnotify_alloc_group( const struct fsnotify_ops *ops, int flags, gfp_t gfp) { + static struct lock_class_key nofs_marks_lock; struct fsnotify_group *group; group = kzalloc(sizeof(struct fsnotify_group), gfp); @@ -135,6 +136,16 @@ static struct fsnotify_group *__fsnotify_alloc_group( group->ops = ops; group->flags = flags; + /* + * For most backends, eviction of inode with a mark is not expected, + * because marks hold a refcount on the inode against eviction. + * + * Use a different lockdep class for groups that support evictable + * inode marks, because with evictable marks, mark_mutex is NOT + * fs-reclaim safe - the mutex is taken when evicting inodes. + */ + if (FSNOTIFY_GROUP_FLAG(group, NOFS)) + lockdep_set_class(&group->mark_mutex, &nofs_marks_lock); return group; } diff --git a/fs/notify/mark.c b/fs/notify/mark.c index ea8f557881b1..7120918d8251 100644 --- a/fs/notify/mark.c +++ b/fs/notify/mark.c @@ -398,9 +398,7 @@ void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info) */ void fsnotify_detach_mark(struct fsnotify_mark *mark) { - struct fsnotify_group *group = mark->group; - - WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex)); + fsnotify_group_assert_locked(mark->group); WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) && refcount_read(&mark->refcnt) < 1 + !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)); @@ -452,9 +450,9 @@ void fsnotify_free_mark(struct fsnotify_mark *mark) void fsnotify_destroy_mark(struct fsnotify_mark *mark, struct fsnotify_group *group) { - mutex_lock(&group->mark_mutex); + fsnotify_group_lock(group); fsnotify_detach_mark(mark); - mutex_unlock(&group->mark_mutex); + fsnotify_group_unlock(group); fsnotify_free_mark(mark); } EXPORT_SYMBOL_GPL(fsnotify_destroy_mark); @@ -673,7 +671,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct fsnotify_group *group = mark->group; int ret = 0; - BUG_ON(!mutex_is_locked(&group->mark_mutex)); + fsnotify_group_assert_locked(group); /* * LOCKING ORDER!!!! @@ -713,9 +711,9 @@ int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp, int ret; struct fsnotify_group *group = mark->group; - mutex_lock(&group->mark_mutex); + fsnotify_group_lock(group); ret = fsnotify_add_mark_locked(mark, connp, obj_type, fsid); - mutex_unlock(&group->mark_mutex); + fsnotify_group_unlock(group); return ret; } EXPORT_SYMBOL_GPL(fsnotify_add_mark); @@ -769,24 +767,24 @@ void fsnotify_clear_marks_by_group(struct fsnotify_group *group, * move marks to free to to_free list in one go and then free marks in * to_free list one by one. */ - mutex_lock(&group->mark_mutex); + fsnotify_group_lock(group); list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { if (mark->connector->type == obj_type) list_move(&mark->g_list, &to_free); } - mutex_unlock(&group->mark_mutex); + fsnotify_group_unlock(group); clear: while (1) { - mutex_lock(&group->mark_mutex); + fsnotify_group_lock(group); if (list_empty(head)) { - mutex_unlock(&group->mark_mutex); + fsnotify_group_unlock(group); break; } mark = list_first_entry(head, struct fsnotify_mark, g_list); fsnotify_get_mark(mark); fsnotify_detach_mark(mark); - mutex_unlock(&group->mark_mutex); + fsnotify_group_unlock(group); fsnotify_free_mark(mark); fsnotify_put_mark(mark); } diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 2057ae4bf8e9..fc6fdbaac797 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -20,6 +20,7 @@ #include #include #include +#include /* * IN_* from inotfy.h lines up EXACTLY with FS_*, this is so we can easily @@ -211,9 +212,11 @@ struct fsnotify_group { bool shutdown; /* group is being shut down, don't queue more events */ #define FSNOTIFY_GROUP_USER 0x01 /* user allocated group */ +#define FSNOTIFY_GROUP_NOFS 0x02 /* group lock is not direct reclaim safe */ #define FSNOTIFY_GROUP_FLAG(group, flag) \ ((group)->flags & FSNOTIFY_GROUP_ ## flag) int flags; + unsigned int owner_flags; /* stored flags of mark_mutex owner */ /* stores all fastpath marks assoc with this group so they can be cleaned on unregister */ struct mutex mark_mutex; /* protect marks_list */ @@ -255,6 +258,31 @@ struct fsnotify_group { }; }; +/* + * These helpers are used to prevent deadlock when reclaiming inodes with + * evictable marks of the same group that is allocating a new mark. + */ +static inline void fsnotify_group_lock(struct fsnotify_group *group) +{ + mutex_lock(&group->mark_mutex); + if (FSNOTIFY_GROUP_FLAG(group, NOFS)) + group->owner_flags = memalloc_nofs_save(); +} + +static inline void fsnotify_group_unlock(struct fsnotify_group *group) +{ + if (FSNOTIFY_GROUP_FLAG(group, NOFS)) + memalloc_nofs_restore(group->owner_flags); + mutex_unlock(&group->mark_mutex); +} + +static inline void fsnotify_group_assert_locked(struct fsnotify_group *group) +{ + WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex)); + if (FSNOTIFY_GROUP_FLAG(group, NOFS)) + WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS)); +} + /* When calling fsnotify tell it if the data is a path or inode */ enum fsnotify_data_type { FSNOTIFY_EVENT_NONE, From patchwork Wed Apr 13 09:09:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811767 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 988AFC4332F for ; Wed, 13 Apr 2022 09:10:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234320AbiDMJMY (ORCPT ); Wed, 13 Apr 2022 05:12:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234281AbiDMJMJ (ORCPT ); Wed, 13 Apr 2022 05:12:09 -0400 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 2CDBA1D0E1 for ; Wed, 13 Apr 2022 02:09:49 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id q8so421355wmc.0 for ; Wed, 13 Apr 2022 02:09:49 -0700 (PDT) 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=3MySchIr/iy1RGi+DahX0CMIibknFFN8LqIdUOKRVN4=; b=B0ksQiyq6SuxfMHz2oAXPo24IoCbEBOVB18PwFxD8ImgJlr9LtYrhXaHjK0NI5EbD4 O+2P3az++k1555cZi1PTjV/CsVrO4rh29iHvbfBpp02lBOOMZfSISCaAP2resy6dRkNi B776vP0TbqBks0koqX7hfgoRnq6JmIIrS8h4P3u79gaBASbYgg9/WO5qZoecYu9xHd14 JfJAP/+6aLKFgihyJN7kuKMzRdGhtXWp82vJKgyFmcrQsip6KqYD+yQ0CWiykqC0IYIN wV90Mvj/t5Da2Hvd9U6bZ9d6kxfNA6UCap1VuBs4iyq5i3fLFMLRhxs5zrzNfl/oJyP/ rEQQ== 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=3MySchIr/iy1RGi+DahX0CMIibknFFN8LqIdUOKRVN4=; b=1KTjgvXz7O8UGJjb7wyaKhM8XLZY0dNQBG9TTLSGuIPz5vZrXBgJ0qWC+9W4zlWS/B kpK4MmD22FplCdBv1Ie0RUwnuxJHHahwlPV8CPLyl7GOClDTaBJZ12kJnMSuB0kX2eTi YJQRjwoVJ3ZD2DP0caQR4ybQjmEXVjqnGAZ7KXl123LE3JmzrbibAPRWDD2GtIFxgTBU AWSuBdmR3i1SBQztGAeACo+AePwrlcqPyZYU1JNBw6Ep+TfPprqud2E4pAyxxR+dy92q m4OVSOJUO8Nl+8dSEWaXIz+T4Ld/pDJF5FQSu3PulRnuPKSSIvY0TPQjVyqhVnwJmYYL 291Q== X-Gm-Message-State: AOAM532bW1vCl9QVDmkOuY49haBOwgkEWtlIvLTdhYC+oL3ki3v9Ndav BfU4ST/oGVcg+L5/qOcB7CVVMVQBdvI= X-Google-Smtp-Source: ABdhPJxUUMASvTUWq1oDDqh9lJwUkSPGp8P9+NxwLCP+AUNXc+wglKBntdTGHebm7yQigHX5+zE0gw== X-Received: by 2002:a05:600c:1509:b0:38e:d707:90cb with SMTP id b9-20020a05600c150900b0038ed70790cbmr1611618wmg.120.1649840987744; Wed, 13 Apr 2022 02:09:47 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:47 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 07/16] inotify: use fsnotify group lock helpers Date: Wed, 13 Apr 2022 12:09:26 +0300 Message-Id: <20220413090935.3127107-8-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org inotify inode marks pin the inode so there is no need to set the FSNOTIFY_GROUP_NOFS flag. Suggested-by: Jan Kara Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/ Signed-off-by: Amir Goldstein --- fs/notify/inotify/inotify_user.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c index 65ff637cb4a3..5cdb2f74bca6 100644 --- a/fs/notify/inotify/inotify_user.c +++ b/fs/notify/inotify/inotify_user.c @@ -640,13 +640,13 @@ static int inotify_update_watch(struct fsnotify_group *group, struct inode *inod { int ret = 0; - mutex_lock(&group->mark_mutex); + fsnotify_group_lock(group); /* try to update and existing watch with the new arg */ ret = inotify_update_existing_watch(group, inode, arg); /* no mark present, try to add a new one */ if (ret == -ENOENT) ret = inotify_new_watch(group, inode, arg); - mutex_unlock(&group->mark_mutex); + fsnotify_group_unlock(group); return ret; } From patchwork Wed Apr 13 09:09:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811765 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 5C260C433F5 for ; Wed, 13 Apr 2022 09:10:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234279AbiDMJMV (ORCPT ); Wed, 13 Apr 2022 05:12:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234285AbiDMJML (ORCPT ); Wed, 13 Apr 2022 05:12:11 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89549205E2 for ; Wed, 13 Apr 2022 02:09:50 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id m14so1647807wrb.6 for ; Wed, 13 Apr 2022 02:09:50 -0700 (PDT) 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=TbF/maklFafxqoydmIIFrPLNRnOTGTA99hRz5s5qH48=; b=XER5PI0Ozt7s55M9F9+E299ErMUHdxKVbdhMeTSoQSdFswCOvSgvVkbHulm9KhCAUX OdGOPj5tHMit4vnXOPzF815KW5m4JOaWmbUkH18Q5uK9WfbKr9tfwv70Q6XqfMbZqgP6 HKWbL8SdZOf+L+y1BNl2dc2qDcxhp2BuiQ3Lb7V0PbAlPVkKqJsekVPlgGVlNj/3WngR W2aKzY00A4iANce7J1FLJpI0KzI6ZnYyiMyUadH7b2qwKby3v9ojFM4HDtBD3FC9j+W+ OlXkvhtOtSQHNSwnMlfTDihUwBxG+KHZikS6v4flQjpvT6hrFlrQ7vTF/ZaYt+bcItGb NuNw== 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=TbF/maklFafxqoydmIIFrPLNRnOTGTA99hRz5s5qH48=; b=oAznzLbF4F1TZPSwt4PFWjP7qb7FMUqDRr2lQuYxx7INb4j559wfMQCfWmEQNBWkW9 0wZVgUM2HYszxSL6+sl+pf03RoCg2jy2AQCvYdHDsS00KBZmzdc9WalrEDjyx9SWAafk bZ4RcoQo5nYyt46+paW7Mj57IqrIKdrVf/8x0xBq9n1VG74GL8yHQ/ygna1qvm4fyIE8 bWn1j8on9IrYynG+5Pxh8olVqHLif1ndtO5DegKzSUQadTJKIcV7OTZSnn8nHRPlhP+c jJl0RSo0dJPulvIsNiMtH0Bb0kxdZ3JN5CCuQWYkgGg0undEZ8oK3HDq0wjCcTmxdtPr AP4A== X-Gm-Message-State: AOAM532XvYvDguA6uEootv6rlvxYFV1kPw0pe0sor1TAtedOC7PJnC19 7eS+2DtWay7ItyLStBwOLXs= X-Google-Smtp-Source: ABdhPJxVVjJcMwBfsV07wbFtS/alZdnoeAKbZQVvyREd9adzsspDAUeEqNGCmRJqj0cLInAWoGgX5g== X-Received: by 2002:a5d:46cc:0:b0:207:ad3b:bf2 with SMTP id g12-20020a5d46cc000000b00207ad3b0bf2mr6166696wrs.547.1649840989063; Wed, 13 Apr 2022 02:09:49 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:48 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 08/16] audit: use fsnotify group lock helpers Date: Wed, 13 Apr 2022 12:09:27 +0300 Message-Id: <20220413090935.3127107-9-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org audit inode marks pin the inode so there is no need to set the FSNOTIFY_GROUP_NOFS flag. Suggested-by: Jan Kara Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/ Signed-off-by: Amir Goldstein --- kernel/audit_tree.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/kernel/audit_tree.c b/kernel/audit_tree.c index b5c02f8573fe..e867c17d3f84 100644 --- a/kernel/audit_tree.c +++ b/kernel/audit_tree.c @@ -351,7 +351,7 @@ static void untag_chunk(struct audit_chunk *chunk, struct fsnotify_mark *mark) struct audit_chunk *new; int size; - mutex_lock(&audit_tree_group->mark_mutex); + fsnotify_group_lock(audit_tree_group); /* * mark_mutex stabilizes chunk attached to the mark so we can check * whether it didn't change while we've dropped hash_lock. @@ -368,7 +368,7 @@ static void untag_chunk(struct audit_chunk *chunk, struct fsnotify_mark *mark) replace_mark_chunk(mark, NULL); spin_unlock(&hash_lock); fsnotify_detach_mark(mark); - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); audit_mark_put_chunk(chunk); fsnotify_free_mark(mark); return; @@ -385,12 +385,12 @@ static void untag_chunk(struct audit_chunk *chunk, struct fsnotify_mark *mark) */ replace_chunk(new, chunk); spin_unlock(&hash_lock); - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); audit_mark_put_chunk(chunk); return; out_mutex: - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); } /* Call with group->mark_mutex held, releases it */ @@ -400,19 +400,19 @@ static int create_chunk(struct inode *inode, struct audit_tree *tree) struct audit_chunk *chunk = alloc_chunk(1); if (!chunk) { - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); return -ENOMEM; } mark = alloc_mark(); if (!mark) { - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); kfree(chunk); return -ENOMEM; } if (fsnotify_add_inode_mark_locked(mark, inode, 0)) { - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); fsnotify_put_mark(mark); kfree(chunk); return -ENOSPC; @@ -422,7 +422,7 @@ static int create_chunk(struct inode *inode, struct audit_tree *tree) if (tree->goner) { spin_unlock(&hash_lock); fsnotify_detach_mark(mark); - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); fsnotify_free_mark(mark); fsnotify_put_mark(mark); kfree(chunk); @@ -444,7 +444,7 @@ static int create_chunk(struct inode *inode, struct audit_tree *tree) */ insert_hash(chunk); spin_unlock(&hash_lock); - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); /* * Drop our initial reference. When mark we point to is getting freed, * we get notification through ->freeing_mark callback and cleanup @@ -462,7 +462,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree) struct audit_node *p; int n; - mutex_lock(&audit_tree_group->mark_mutex); + fsnotify_group_lock(audit_tree_group); mark = fsnotify_find_mark(&inode->i_fsnotify_marks, audit_tree_group); if (!mark) return create_chunk(inode, tree); @@ -478,7 +478,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree) for (n = 0; n < old->count; n++) { if (old->owners[n].owner == tree) { spin_unlock(&hash_lock); - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); fsnotify_put_mark(mark); return 0; } @@ -487,7 +487,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree) chunk = alloc_chunk(old->count + 1); if (!chunk) { - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); fsnotify_put_mark(mark); return -ENOMEM; } @@ -495,7 +495,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree) spin_lock(&hash_lock); if (tree->goner) { spin_unlock(&hash_lock); - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); fsnotify_put_mark(mark); kfree(chunk); return 0; @@ -515,7 +515,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree) */ replace_chunk(chunk, old); spin_unlock(&hash_lock); - mutex_unlock(&audit_tree_group->mark_mutex); + fsnotify_group_unlock(audit_tree_group); fsnotify_put_mark(mark); /* pair to fsnotify_find_mark */ audit_mark_put_chunk(old); @@ -1044,12 +1044,12 @@ static void audit_tree_freeing_mark(struct fsnotify_mark *mark, { struct audit_chunk *chunk; - mutex_lock(&mark->group->mark_mutex); + fsnotify_group_lock(mark->group); spin_lock(&hash_lock); chunk = mark_chunk(mark); replace_mark_chunk(mark, NULL); spin_unlock(&hash_lock); - mutex_unlock(&mark->group->mark_mutex); + fsnotify_group_unlock(mark->group); if (chunk) { evict_chunk(chunk); audit_mark_put_chunk(chunk); From patchwork Wed Apr 13 09:09:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811769 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 1ED89C433EF for ; Wed, 13 Apr 2022 09:10:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233892AbiDMJNH (ORCPT ); Wed, 13 Apr 2022 05:13:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234286AbiDMJMM (ORCPT ); Wed, 13 Apr 2022 05:12:12 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFF7B186C2 for ; Wed, 13 Apr 2022 02:09:51 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id r133-20020a1c448b000000b0038ccb70e239so3495518wma.3 for ; Wed, 13 Apr 2022 02:09:51 -0700 (PDT) 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=TU96ZlVLsOoM0i+tAbwz9u493gtmbJdidxT26D+Nm7U=; b=m6HYopknGesTWFQjceC7TRlI5Oj6n9F95MqHUwy13IxtdXbup2Rw7KA/Rm85vTOSZs PFcwPf9upYShR3O8YjwNsKNM7Q8AnKfcmheiICM3nY5I/wohgxVYB4MVnH/Lg9UzKi3z zOQhwlLMBZTM5gwYGxj4hW0jd4AawUXwI4M9lhUtwQyOXhO2+TUIddXzS7Uhn6J8EK1k depcK34M744TKcSLJwiNz4I4Iy6KAHKwlestzt8oUylhgdoguTAVzZuvAcQ2Ux6c22Pv pGUvRnzIW2JahoiHgQNC+IUjB4tf8DPNuaCVRND7gMYsz9xIVkZ7BAIVb84qaC0aP478 k+xQ== 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=TU96ZlVLsOoM0i+tAbwz9u493gtmbJdidxT26D+Nm7U=; b=tqoz11b/2mSYtvrvMENePxDvNSK8i2hxUWdZsGjjyj7uG9+RUGmj1WXuujzi0Nbwyr 5wrLHV3ICShWITVwuF/dsVqKhBEkUvsqUkGsSFONSfuVWsArkdk56FDZAOmu5HGUyRnf 0EGtwUR7MZqKAhjo/kYjWGTkW/HxM83Q91u7HuXu2EabL0hd3ISKBJvflOdl8CxUvRyu /LpHmywYlJSMNfdYDtHtidKC3Pj2TvNhb2T7dXq71al28y+qh98JMNdzp/fReQcVpj/s oeLnOYnopqVjBODPJsagAyxEOCLfe6Ha8seQSVuKeOsGdIgkXLAeiGlSJJBPybBEcqvW rDZQ== X-Gm-Message-State: AOAM532FH+cz7RQcMgfnpURRpHWgXEQDrOWVv/v2Z+x2sefi4lH99VFD J0tmeoNmttnI+V5nyQgsBR0= X-Google-Smtp-Source: ABdhPJy/dlY17hCUTmsW39RRjUmjKZMhQXaqTmYlI7mqh1/5Au+2sUjkPK3V/+kHoBJjNepF8geleQ== X-Received: by 2002:a1c:acc6:0:b0:38e:b184:7721 with SMTP id v189-20020a1cacc6000000b0038eb1847721mr7674757wme.94.1649840990434; Wed, 13 Apr 2022 02:09:50 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:49 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 09/16] nfsd: use fsnotify group lock helpers Date: Wed, 13 Apr 2022 12:09:28 +0300 Message-Id: <20220413090935.3127107-10-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Before commit 9542e6a643fc6 ("nfsd: Containerise filecache laundrette") nfsd would close open files in direct reclaim context and that could cause a deadlock when fsnotify mark allocation went into direct reclaim and nfsd shrinker tried to free existing fsnotify marks. To avoid issues like this in future code, set the FSNOTIFY_GROUP_NOFS flag on nfsd fsnotify group to prevent going into direct reclaim from fsnotify_add_inode_mark(). Suggested-by: Jan Kara Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/ Signed-off-by: Amir Goldstein --- fs/nfsd/filecache.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fs/nfsd/filecache.c b/fs/nfsd/filecache.c index 79a5b052fcdf..258599db119f 100644 --- a/fs/nfsd/filecache.c +++ b/fs/nfsd/filecache.c @@ -119,14 +119,14 @@ nfsd_file_mark_find_or_create(struct nfsd_file *nf) struct inode *inode = nf->nf_inode; do { - mutex_lock(&nfsd_file_fsnotify_group->mark_mutex); + fsnotify_group_lock(nfsd_file_fsnotify_group); mark = fsnotify_find_mark(&inode->i_fsnotify_marks, - nfsd_file_fsnotify_group); + nfsd_file_fsnotify_group); if (mark) { nfm = nfsd_file_mark_get(container_of(mark, struct nfsd_file_mark, nfm_mark)); - mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex); + fsnotify_group_unlock(nfsd_file_fsnotify_group); if (nfm) { fsnotify_put_mark(mark); break; @@ -134,8 +134,9 @@ nfsd_file_mark_find_or_create(struct nfsd_file *nf) /* Avoid soft lockup race with nfsd_file_mark_put() */ fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group); fsnotify_put_mark(mark); - } else - mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex); + } else { + fsnotify_group_unlock(nfsd_file_fsnotify_group); + } /* allocate a new nfm */ new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL); @@ -674,7 +675,7 @@ nfsd_file_cache_init(void) } nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops, - 0); + FSNOTIFY_GROUP_NOFS); if (IS_ERR(nfsd_file_fsnotify_group)) { pr_err("nfsd: unable to create fsnotify group: %ld\n", PTR_ERR(nfsd_file_fsnotify_group)); From patchwork Wed Apr 13 09:09:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811768 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 64FBEC433EF for ; Wed, 13 Apr 2022 09:10:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230141AbiDMJNE (ORCPT ); Wed, 13 Apr 2022 05:13:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229864AbiDMJMP (ORCPT ); Wed, 13 Apr 2022 05:12:15 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CAB219281 for ; Wed, 13 Apr 2022 02:09:53 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id g18so1626616wrb.10 for ; Wed, 13 Apr 2022 02:09:53 -0700 (PDT) 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=Vtr5pyBasxSToiXWwCcaDeyWbLzuF18LebaN1QarL1U=; b=Vi266M5Sy4e/XP45T8P7eW0N2/NgVjiTnWl9BCM+ZClmZsZstFlLe8Zqad32g5lLzL 7miNpu3/X0ttiyx9rQhFD8BIiWYKzS7HKsAWXpiOPQNzxTX1yzk2E/2giSRZt+zVfp/l z8l/Ei0HU9szIdu54FUdZ6h4DNa+p2Q/5J2h2bKa68+VBr5441xkBpzIEWl+hVjJe3ru ZevM0KBzdEquBgiwAUjpSTcPCJyPb90V3mHpiIDEylzNsxQFzSCzPbIIFa0yNHUJZTZX uijj7DQOmjd8sRViF9JfgIs27JsCPPlt6Bh6Nc5C95E9j1T1an4P60ZBxq2+ik+HMQDP Ij9w== 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=Vtr5pyBasxSToiXWwCcaDeyWbLzuF18LebaN1QarL1U=; b=kEJJzWlSoaYTY3ZabjSpRmGNT457o9kHgvoPTnBzuQ1JOu2/gwqDtCp8n1/9LWDfrl 09BCLiRDpqXKnHlycknfFXNdrlEbjqkpKRn6SOqeSQPr++GKf34fnfa5166gGxAnz2Bp BPUAOwDi+ABM5qpWnw0wc6tcFaonyJobSdeUaVmF+tz2cbycr+vTREyazudSObwcW0YK HZhkwRlTgmhG43c7AHkbOswNvGpuHX6aU+phnodDlWQI4a80ZpqHu2i9PRKC59+x1Rth ZcBcVlNQ0vWddrXY6SoUpuK9rRomVkgcTTQEfV+ADZGFXcb7pyqKxGS7DOok6+YEA/TX NX/Q== X-Gm-Message-State: AOAM530hhZ5tcbVaqD7PmYowFfbNsAKtdI9tstxpIlzSwhJC+hD57fmi OuQHv6TQch03zUiHxNgeRng= X-Google-Smtp-Source: ABdhPJwnSUrjrSAuV1FSir2prfMlt0m/SpmZiqFUP/+DT5xgvRubXisbIVrtV80/guFnaRNlampNzw== X-Received: by 2002:a05:6000:703:b0:207:a34c:e07 with SMTP id bs3-20020a056000070300b00207a34c0e07mr13160526wrb.540.1649840991748; Wed, 13 Apr 2022 02:09:51 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:51 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 10/16] dnotify: use fsnotify group lock helpers Date: Wed, 13 Apr 2022 12:09:29 +0300 Message-Id: <20220413090935.3127107-11-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Before commit 9542e6a643fc6 ("nfsd: Containerise filecache laundrette") nfsd would close open files in direct reclaim context. There is no guarantee that others memory shrinkers don't do the same and no guarantee that future shrinkers won't do that. For example, if overlayfs implements inode cache of fscache would keep open files to cached objects, inode shrinkers could end up closing open files to underlying fs. Direct reclaim from dnotify mark allocation context may try to close open files that have dnotify marks of the same group and hit a deadlock on mark_mutex. Set the FSNOTIFY_GROUP_NOFS flag to prevent going into direct reclaim from allocations under dnotify group lock and use the safe group lock helpers. Suggested-by: Jan Kara Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/ Signed-off-by: Amir Goldstein --- fs/notify/dnotify/dnotify.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fs/notify/dnotify/dnotify.c b/fs/notify/dnotify/dnotify.c index e4779926edf4..190aa717fa32 100644 --- a/fs/notify/dnotify/dnotify.c +++ b/fs/notify/dnotify/dnotify.c @@ -168,7 +168,7 @@ void dnotify_flush(struct file *filp, fl_owner_t id) return; dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark); - mutex_lock(&dnotify_group->mark_mutex); + fsnotify_group_lock(dnotify_group); spin_lock(&fsn_mark->lock); prev = &dn_mark->dn; @@ -191,7 +191,7 @@ void dnotify_flush(struct file *filp, fl_owner_t id) free = true; } - mutex_unlock(&dnotify_group->mark_mutex); + fsnotify_group_unlock(dnotify_group); if (free) fsnotify_free_mark(fsn_mark); @@ -324,7 +324,7 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) new_dn_mark->dn = NULL; /* this is needed to prevent the fcntl/close race described below */ - mutex_lock(&dnotify_group->mark_mutex); + fsnotify_group_lock(dnotify_group); /* add the new_fsn_mark or find an old one. */ fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, dnotify_group); @@ -334,7 +334,7 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) } else { error = fsnotify_add_inode_mark_locked(new_fsn_mark, inode, 0); if (error) { - mutex_unlock(&dnotify_group->mark_mutex); + fsnotify_group_unlock(dnotify_group); goto out_err; } spin_lock(&new_fsn_mark->lock); @@ -383,7 +383,7 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) if (destroy) fsnotify_detach_mark(fsn_mark); - mutex_unlock(&dnotify_group->mark_mutex); + fsnotify_group_unlock(dnotify_group); if (destroy) fsnotify_free_mark(fsn_mark); fsnotify_put_mark(fsn_mark); @@ -401,7 +401,8 @@ static int __init dnotify_init(void) SLAB_PANIC|SLAB_ACCOUNT); dnotify_mark_cache = KMEM_CACHE(dnotify_mark, SLAB_PANIC|SLAB_ACCOUNT); - dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops, 0); + dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops, + FSNOTIFY_GROUP_NOFS); if (IS_ERR(dnotify_group)) panic("unable to allocate fsnotify group for dnotify\n"); dnotify_sysctl_init(); From patchwork Wed Apr 13 09:09:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811771 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 74D91C433F5 for ; Wed, 13 Apr 2022 09:10:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234288AbiDMJNL (ORCPT ); Wed, 13 Apr 2022 05:13:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234278AbiDMJMP (ORCPT ); Wed, 13 Apr 2022 05:12:15 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F518205E2 for ; Wed, 13 Apr 2022 02:09:54 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id m33-20020a05600c3b2100b0038ec0218103so741469wms.3 for ; Wed, 13 Apr 2022 02:09:54 -0700 (PDT) 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=vjMJnOQ2xiLWlxLaltEAkegpH7Np+Ce8U+zcpqIVObE=; b=gwKS/LVuofS8tMZnsH60vY6DbrP2TrAFJhn3Mt19OtRdqPNNXQo/yljg6xWtk2D8I6 Uo/VoW4RynfxOsF/gbZmBSZ/rw2UkteB6/CiPXtwsvmDBvhiSSsi+17cO7xgYL32omo9 VwMv7xhkcM+h9oaC+QHA3ellArWHdp2DPPzHVaLmXKUbA11hrCATBzuWqxr7EpCiBcDQ pIvcMnWEHTvQR+ZIX0odwAbsnDxyEEDT6nQcGiOFb+yF9w3P+fjm33yxzitaCeJmg0yR w3BLUAoKtnOfQmaH1WJCRfNTBB0RdJo3CKtwLYs49P0nzJaNlgRwmM1bOcbCcx7YvuH+ JIwg== 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=vjMJnOQ2xiLWlxLaltEAkegpH7Np+Ce8U+zcpqIVObE=; b=mOSq7wG8yQvMfEgsUB0T+JADhACWj5UCI5j9ljSjJKE4K3V+llb7uRSPgMVlm3pPj1 EMH+DM6GD7NUD57nQ70TPbwJwVvGdoC4KbYgBkvzZI5kmoo8a7HF72LSfvS4aPwLMjg8 ZVzgThg2THEvbck7sfO7oUBk1RkY82FpFkD+ohrL01xkJvsUCN25mR+IGDzDMlr9vWxx 3SvVsK2vYvKe0Ely+ex48fA6R7O/woGXxG/lXJ9r85XDm6NPLotnfgNsj2C+fKVsq52m Di25dpUNuZMK77ESnrt5fW12leCND5wX0yyTi1DN1Djg40mQyasmhWuQqvX0yQd15dr/ QNtw== X-Gm-Message-State: AOAM531KwfQ+GMnZecgl/IN6EStnqdjXvTaAfAGoN2uJY6o5NOwcLCXf Ghng5Y3wSpOaCiTxeA9iRqUgwYhlXNc= X-Google-Smtp-Source: ABdhPJxEkUgmN3QPb2apEIBkyU2stKugie/10vY23Wh8IwKbCmw+waggvhOhmKevufx1gEqf80tucg== X-Received: by 2002:a05:600c:54e:b0:38e:bbef:9115 with SMTP id k14-20020a05600c054e00b0038ebbef9115mr7628224wmc.60.1649840992906; Wed, 13 Apr 2022 02:09:52 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:52 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 11/16] fsnotify: allow adding an inode mark without pinning inode Date: Wed, 13 Apr 2022 12:09:30 +0300 Message-Id: <20220413090935.3127107-12-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-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 mark flag FSNOTIFY_MARK_FLAG_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. Backends can "upgrade" an existing mark to take an inode reference, but cannot "downgrade" a mark with inode reference to release the refernce. 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 | 80 ++++++++++++++++++++++++-------- include/linux/fsnotify_backend.h | 2 + 2 files changed, 62 insertions(+), 20 deletions(-) diff --git a/fs/notify/mark.c b/fs/notify/mark.c index 7120918d8251..e38cb241536f 100644 --- a/fs/notify/mark.c +++ b/fs/notify/mark.c @@ -116,20 +116,67 @@ __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn) return *fsnotify_conn_mask_p(conn); } -static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) +/* + * Update the proxy refcount on inode maintainted by connector. + * + * When it's time to drop the proxy refcount, clear the HAS_IREF flag + * and return the inode object. fsnotify_drop_object() will be resonsible + * for doing iput() outside of spinlocks when last mark that wanted iref + * is detached. + * + * Note that the proxy refcount is NOT dropped if backend only sets the + * NO_IREF mark flag and does detach the mark! + */ +static void fsnotify_get_inode_ref(struct inode *inode) +{ + ihold(inode); + atomic_long_inc(&inode->i_sb->s_fsnotify_connectors); +} + +static struct inode *fsnotify_update_iref(struct fsnotify_mark_connector *conn, + bool want_iref) +{ + bool has_iref = conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF; + struct inode *inode = NULL; + + if (conn->type != FSNOTIFY_OBJ_TYPE_INODE || + want_iref == has_iref) + return NULL; + + if (want_iref) { + /* Pin inode if any mark wants inode refcount held */ + fsnotify_get_inode_ref(fsnotify_conn_inode(conn)); + conn->flags |= FSNOTIFY_CONN_FLAG_HAS_IREF; + } else { + /* Unpin inode after detach of last mark that wanted iref */ + inode = fsnotify_conn_inode(conn); + conn->flags &= ~FSNOTIFY_CONN_FLAG_HAS_IREF; + } + + return inode; +} + +static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) { u32 new_mask = 0; + bool want_iref = false; struct fsnotify_mark *mark; assert_spin_locked(&conn->lock); /* We can get detached connector here when inode is getting unlinked. */ if (!fsnotify_valid_obj_type(conn->type)) - return; + return NULL; hlist_for_each_entry(mark, &conn->list, obj_list) { - if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) - new_mask |= fsnotify_calc_mask(mark); + if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) + continue; + new_mask |= fsnotify_calc_mask(mark); + if (conn->type == FSNOTIFY_OBJ_TYPE_INODE && + !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)) + want_iref = true; } *fsnotify_conn_mask_p(conn) = new_mask; + + return fsnotify_update_iref(conn, want_iref); } /* @@ -169,12 +216,6 @@ static void fsnotify_connector_destroy_workfn(struct work_struct *work) } } -static void fsnotify_get_inode_ref(struct inode *inode) -{ - ihold(inode); - atomic_long_inc(&inode->i_sb->s_fsnotify_connectors); -} - static void fsnotify_put_inode_ref(struct inode *inode) { struct super_block *sb = inode->i_sb; @@ -213,6 +254,10 @@ static void *fsnotify_detach_connector_from_object( if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) { inode = fsnotify_conn_inode(conn); inode->i_fsnotify_mask = 0; + + /* Unpin inode when detaching from connector */ + if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF)) + 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) { @@ -223,6 +268,7 @@ static void *fsnotify_detach_connector_from_object( rcu_assign_pointer(*(conn->obj), NULL); conn->obj = NULL; conn->type = FSNOTIFY_OBJ_TYPE_DETACHED; + conn->flags = 0; return inode; } @@ -274,7 +320,8 @@ void fsnotify_put_mark(struct fsnotify_mark *mark) objp = fsnotify_detach_connector_from_object(conn, &type); free_conn = true; } else { - __fsnotify_recalc_mask(conn); + objp = __fsnotify_recalc_mask(conn); + type = conn->type; } WRITE_ONCE(mark->connector, NULL); spin_unlock(&conn->lock); @@ -497,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); @@ -505,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); + conn->flags = 0; conn->type = obj_type; conn->obj = connp; /* Cache fsid of filesystem containing the object */ @@ -515,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); /* @@ -527,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); } @@ -690,8 +731,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, if (ret) goto err; - if (mark->mask || mark->ignored_mask) - fsnotify_recalc_mask(mark->connector); + fsnotify_recalc_mask(mark->connector); return ret; err: diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index fc6fdbaac797..478a985d13c2 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -457,6 +457,7 @@ struct fsnotify_mark_connector { spinlock_t lock; unsigned short type; /* Type of object [lock] */ #define FSNOTIFY_CONN_FLAG_HAS_FSID 0x01 +#define FSNOTIFY_CONN_FLAG_HAS_IREF 0x02 unsigned short flags; /* flags [lock] */ __kernel_fsid_t fsid; /* fsid of filesystem containing object */ union { @@ -512,6 +513,7 @@ struct fsnotify_mark { #define FSNOTIFY_MARK_FLAG_IN_ONESHOT 0x0020 /* fanotify mark flags */ #define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x0100 +#define FSNOTIFY_MARK_FLAG_NO_IREF 0x0200 unsigned int flags; /* flags [mark->lock] */ }; From patchwork Wed Apr 13 09:09:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811770 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 F3870C433FE for ; Wed, 13 Apr 2022 09:10:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232123AbiDMJNK (ORCPT ); Wed, 13 Apr 2022 05:13:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234298AbiDMJMP (ORCPT ); Wed, 13 Apr 2022 05:12:15 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81ADB205EF for ; Wed, 13 Apr 2022 02:09:55 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id r7so654058wmq.2 for ; Wed, 13 Apr 2022 02:09:55 -0700 (PDT) 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=IZfDOJDN+f6HlsMcD0ehVtQsF1UIr7uk2DkRuU+2Oys=; b=oAymXG22XnMlEQtmUOKZrg6+rPND9YkUF/KliX5DA+O8XsoeeLI0rXgPt7twN7A9RR eGDvEwpji2nnBUlBwpqBvObPuPrMJY+3pVNWo87ECK4aPzfhwJtfjvrT5eq2oSzljsls 0vKcpthR/onNQEGY2R+KNHY7qk8a0LQGNfqoqtIvlwsYIUrA8UMjo/qJLwKlCA7VQEFq FC4GeYbjiazct2sKHukmHL3v6dO9YCaRDGv8raRVBPnP6CtWsmdbU8qUb0o1Oaec5/fO vx3fdbJY0szCTibRcUOSOun/RGaNnQd7nZPygp504i6kWwYNvbRE8xCtLmb0s25rlMHx hPSA== 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=IZfDOJDN+f6HlsMcD0ehVtQsF1UIr7uk2DkRuU+2Oys=; b=TBrJV9qnLWe6yhqymBcI2NiozLGyErk4K4IRq3H2dZdIm6y2Ib8OeVZqHfA/8WUftX ut+EsUeym1w8u8vrWkV3LTx21QO3paiW+PPxeUaLYGflOpzx3DH4Zwd7qb9kI1pX7Ig3 IlVN5FmyTqcW4zwmhph+5zq5VfOte9VGKpHd2PWefWJWyCTXW141hJg771zijCbfBLwA qQrEeRZ4At+o5KKe+LZAI34MMEwJ7pJYLyk3isf3BYam5JPJbLwwaSf+ilWGDc1bVhed 1MAtBhkKdS/ozSzroBMWn4mALghZhgf9Hd4/Po/WO5avN2RJdYbOEjPFVLgAXY/fgeKr iiow== X-Gm-Message-State: AOAM530sJw5H1clB3NtF31XVLhOWOtKu/bPwSAP1NlKYFIQTexfxhbGN /MBv4SSUP/PZ7yVp09OTiFw= X-Google-Smtp-Source: ABdhPJysvdoiWyRERFbG9dmJfRGwWwYo8ZHJ+Lu6VZ7X7cC2XAfLpZ9jSzuwEb3JLksvijLUdzrH5Q== X-Received: by 2002:a05:600c:2113:b0:38e:bc71:2b0 with SMTP id u19-20020a05600c211300b0038ebc7102b0mr7539880wml.153.1649840994059; Wed, 13 Apr 2022 02:09:54 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:53 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 12/16] fanotify: create helper fanotify_mark_user_flags() Date: Wed, 13 Apr 2022 12:09:31 +0300 Message-Id: <20220413090935.3127107-13-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org To translate from fsnotify mark flags to user visible flags. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.h | 10 ++++++++++ fs/notify/fdinfo.c | 6 ++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index a3d5b751cac5..87142bc0131a 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -490,3 +490,13 @@ static inline unsigned int fanotify_event_hash_bucket( { return event->hash & FANOTIFY_HTABLE_MASK; } + +static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark) +{ + unsigned int mflags = 0; + + if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY) + mflags |= FAN_MARK_IGNORED_SURV_MODIFY; + + return mflags; +} diff --git a/fs/notify/fdinfo.c b/fs/notify/fdinfo.c index 1f34c5c29fdb..59fb40abe33d 100644 --- a/fs/notify/fdinfo.c +++ b/fs/notify/fdinfo.c @@ -14,6 +14,7 @@ #include #include "inotify/inotify.h" +#include "fanotify/fanotify.h" #include "fdinfo.h" #include "fsnotify.h" @@ -103,12 +104,9 @@ void inotify_show_fdinfo(struct seq_file *m, struct file *f) static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark) { - unsigned int mflags = 0; + unsigned int mflags = fanotify_mark_user_flags(mark); struct inode *inode; - if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY) - mflags |= FAN_MARK_IGNORED_SURV_MODIFY; - if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) { inode = igrab(fsnotify_conn_inode(mark->connector)); if (!inode) From patchwork Wed Apr 13 09:09:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811774 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 5E2EEC433EF for ; Wed, 13 Apr 2022 09:11:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234295AbiDMJNR (ORCPT ); Wed, 13 Apr 2022 05:13:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234305AbiDMJMR (ORCPT ); Wed, 13 Apr 2022 05:12:17 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B0AD27FC3 for ; Wed, 13 Apr 2022 02:09:56 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id ay11-20020a05600c1e0b00b0038eb92fa965so3495104wmb.4 for ; Wed, 13 Apr 2022 02:09:56 -0700 (PDT) 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=+uA5AZngPQPmtnBFYJMCWANv4BTmdB0JIX9A66fXeu0=; b=GDj+Bf1JsyHw41RiOfHFyzOPLRqshmkSu2S8QA/QQWhzLUfgziyCZUrW3Qfi+KF7Hj OuyrYE0NAVeCznK/DRZklEAqK4vlP2Q86k+8pr5g7EgCQ3hywMSGBykBfj5xD0xpRO6e QzyHyPxDat8wOjg+74tD+xrci1E9EniqxU0wAuN8KuCmFE5n3h+RaEbyKgpu6GF6HrYU vChgtj/nKlWi2zGq2I20gHw2SqSrLisXpjBE/NY3FsKFIMGa1iL/uYZtO1P7o40AoJ5f q3noTIVxDmnkyeFvnJLvI2JJwC13bkfWNhROhigSfTvV6bV9NDmCGMmMlKLsWGTbX7BQ jCIA== 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=+uA5AZngPQPmtnBFYJMCWANv4BTmdB0JIX9A66fXeu0=; b=IYc6XjAn+GVlTFuNI54QLFRFzB3JBXniRZH6UoZsI4IRW3u2sHopoScea/xouS0qXP KW79GekX0Plq++fNyQsy7pxV8p7zW4AjZIPjlNs4qFMhC8liDVf43TKYBf7v5gwoyjIT A1n0fvoweUc8YrLQTwDrv5KJ3g0jhN7H87o0QsYpJPGpDtC2u5I/ciSyI+NXWBnRsKg+ xJEizlObW9C3VnV2ddfbIWCSUYCAIDq6fCuzN5N3sYlHYylmRkzRZ7IXWUYBiGvqNr8b xY2n3WYCafP4LAfo8VQnvbbXh/xHdX0VYP5Q9Tr9jvzEUhO0al1eP2NTPyw3AhzeQYPI ei8g== X-Gm-Message-State: AOAM5305JTF6B3t0vgHApBBLL+W/K12n7GRPb714/HZGvKkxmHXHfDPJ a2N9QLfMuePa7QVfCfBj+FOe6ZTFlDM= X-Google-Smtp-Source: ABdhPJy7W0/86CsrHttU8XPHzC82AJVaPW2oxVD7TW3329XDSifuv3w2kfnc//ww8pwhAuwr8xJ7bg== X-Received: by 2002:a7b:c5cd:0:b0:38c:8b1b:d220 with SMTP id n13-20020a7bc5cd000000b0038c8b1bd220mr7577973wmk.118.1649840995400; Wed, 13 Apr 2022 02:09:55 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:54 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 13/16] fanotify: factor out helper fanotify_mark_update_flags() Date: Wed, 13 Apr 2022 12:09:32 +0300 Message-Id: <20220413090935.3127107-14-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Handle FAN_MARK_IGNORED_SURV_MODIFY flag change in a helper that is called after updating the mark mask. Move recalc of object mask inside fanotify_mark_add_to_mask() which makes the code a bit simpler to follow. Add also helper to translate fsnotify mark flags to user visible fanotify mark flags. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 41 ++++++++++++++++-------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index bf72856da42e..d8d44a5b37e3 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1081,42 +1081,48 @@ static int fanotify_remove_inode_mark(struct fsnotify_group *group, flags, umask); } -static void fanotify_mark_add_ignored_mask(struct fsnotify_mark *fsn_mark, - __u32 mask, unsigned int flags, - __u32 *removed) +static int fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark, + unsigned int flags, bool *recalc) { - fsn_mark->ignored_mask |= mask; - /* * Setting FAN_MARK_IGNORED_SURV_MODIFY for the first time may lead to * the removal of the FS_MODIFY bit in calculated mask if it was set * because of an ignored mask that is now going to survive FS_MODIFY. */ if ((flags & FAN_MARK_IGNORED_SURV_MODIFY) && + (flags & FAN_MARK_IGNORED_MASK) && !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) { fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; if (!(fsn_mark->mask & FS_MODIFY)) - *removed = FS_MODIFY; + *recalc = true; } + + return 0; } -static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, - __u32 mask, unsigned int flags, - __u32 *removed) +static int fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, + __u32 mask, unsigned int flags) { - __u32 oldmask, newmask; + bool recalc = false; + int ret; spin_lock(&fsn_mark->lock); - oldmask = fsnotify_calc_mask(fsn_mark); if (!(flags & FAN_MARK_IGNORED_MASK)) { fsn_mark->mask |= mask; } else { - fanotify_mark_add_ignored_mask(fsn_mark, mask, flags, removed); + fsn_mark->ignored_mask |= mask; } - newmask = fsnotify_calc_mask(fsn_mark); + + recalc = fsnotify_calc_mask(fsn_mark) & + ~fsnotify_conn_mask(fsn_mark->connector); + + ret = fanotify_mark_update_flags(fsn_mark, flags, &recalc); spin_unlock(&fsn_mark->lock); - return newmask & ~oldmask; + if (recalc) + fsnotify_recalc_mask(fsn_mark->connector); + + return ret; } static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, @@ -1174,8 +1180,7 @@ static int fanotify_add_mark(struct fsnotify_group *group, __kernel_fsid_t *fsid) { struct fsnotify_mark *fsn_mark; - __u32 added, removed = 0; - int ret = 0; + int ret; mutex_lock(&group->mark_mutex); fsn_mark = fsnotify_find_mark(connp, group); @@ -1197,9 +1202,7 @@ static int fanotify_add_mark(struct fsnotify_group *group, goto out; } - added = fanotify_mark_add_to_mask(fsn_mark, mask, flags, &removed); - if (removed || (added & ~fsnotify_conn_mask(fsn_mark->connector))) - fsnotify_recalc_mask(fsn_mark->connector); + ret = fanotify_mark_add_to_mask(fsn_mark, mask, flags); out: mutex_unlock(&group->mark_mutex); From patchwork Wed Apr 13 09:09:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811775 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 E7ED6C433FE for ; Wed, 13 Apr 2022 09:11:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231165AbiDMJNV (ORCPT ); Wed, 13 Apr 2022 05:13:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234308AbiDMJMT (ORCPT ); Wed, 13 Apr 2022 05:12:19 -0400 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 0C536286D7 for ; Wed, 13 Apr 2022 02:09:58 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id g18so1626884wrb.10 for ; Wed, 13 Apr 2022 02:09:57 -0700 (PDT) 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=kQP4t31IBbyZUrqerF0qyM4AohJqwX6xLtWXo7ap3iI=; b=IHGvyNqfqGFDmeZ5c0QPZwvQdqjby+omiTMP4e66jIEThFgXPyXO1wc0dSd+EUEHpB lForOGPv9E4Bg13cUGe7qw6r6kPIN+EisT91ZsFJmqxU2Fyu+NxRWITBB2LKh0FHjfbG uUwO7Y8q49pZnpAPakfgaBrohRdWTl6qPl3la3xKk9yxv2tpdAFRPCq51vWjyD2V4cqb OIU1N8irzWZq15yhpeDO2I0UsDBke9aEQ3/tXpzZacc497zRLrybzU+Gz5clEsW3sJ/T m6UEPpW2Wbl87VapgI4VyhM4IArnLCmzOgRRgz4Qfpe1cQ7cz1LOytgWowPFQZ5zv82P 0STA== 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=kQP4t31IBbyZUrqerF0qyM4AohJqwX6xLtWXo7ap3iI=; b=xh8b6V4ZwND6Jq+hFry4/lOcSEaAmgnbXrqRFE4YLh06tDUU+RE9HHuW/ZFXoILqwg GLz8PAlwKQ0Ba5xhPyUkj5DP6rLet1fEhTZsdYfNcyknkSU5TFSJNmRsFlviGohGi1Yf i+x88Nw07EeffwvZn9uUl4tXImbgX/KRFfw8PAuVBuAL9udMCKH546zmg+OvDE4bp6CM kid4TmUFJRMnOriTz9Ebd9nWCKA5z3k3dAnL9OXV0zG96WRZ1r2088yOb1cyJZeFy1rx J8qSJzo8nnpu/rkEfTcmxMVhkPiHfHNhsFeWSI4ru4yxrmeV8Yq63Wc8guvlYRb1btrM 9gNw== X-Gm-Message-State: AOAM531lh7by8a1Sz4Y9/47SW7h+eVrSTXwflD/M7LGfaejUdugJ5SPB w9xh1WBuSudZ5gtFguRIQPPT+Wfm/Gk= X-Google-Smtp-Source: ABdhPJy0se1JFu7KSwzKCW6O0bALuXdqF/42qn02hUHy6mQVUykTxwzWst/jpFkrs4thLtNhCodX5A== X-Received: by 2002:adf:9e49:0:b0:205:b77d:adf8 with SMTP id v9-20020adf9e49000000b00205b77dadf8mr31631397wre.85.1649840996522; Wed, 13 Apr 2022 02:09:56 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:56 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 14/16] fanotify: implement "evictable" inode marks Date: Wed, 13 Apr 2022 12:09:33 +0300 Message-Id: <20220413090935.3127107-15-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-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_EVICTABLE, 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. When an inode mark with flag FAN_MARK_EVICATBLE is updated without using this flag, the marked inode is pinned to inode cache. When an inode mark is updated with flag FAN_MARK_EVICTABLE but an existing mark already has the inode pinned, the mark update fails with error EEXIST. Evictable 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 evictbale inode mark feature allows performing this lazy marks setup without exhausting the system memory with pinned inodes. This change does not enable the feature yet. 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.h | 2 ++ fs/notify/fanotify/fanotify_user.c | 37 +++++++++++++++++++++++++++++- include/uapi/linux/fanotify.h | 1 + 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index 87142bc0131a..80e0ec95b113 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -497,6 +497,8 @@ static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark) if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY) mflags |= FAN_MARK_IGNORED_SURV_MODIFY; + if (mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF) + mflags |= FAN_MARK_EVICTABLE; return mflags; } diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index d8d44a5b37e3..0b4beba95fa8 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1084,6 +1084,8 @@ static int fanotify_remove_inode_mark(struct fsnotify_group *group, static int fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark, unsigned int flags, bool *recalc) { + bool want_iref = !(flags & FAN_MARK_EVICTABLE); + /* * Setting FAN_MARK_IGNORED_SURV_MODIFY for the first time may lead to * the removal of the FS_MODIFY bit in calculated mask if it was set @@ -1097,6 +1099,18 @@ static int fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark, *recalc = true; } + if (fsn_mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE || + want_iref == !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)) + return 0; + + /* + * NO_IREF may be removed from a mark, but not added. + * When removed, fsnotify_recalc_mask() will take the inode ref. + */ + WARN_ON_ONCE(!want_iref); + fsn_mark->flags &= ~FSNOTIFY_MARK_FLAG_NO_IREF; + *recalc = true; + return 0; } @@ -1128,6 +1142,7 @@ static int 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; @@ -1150,6 +1165,9 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, } fsnotify_init_mark(mark, group); + if (fan_flags & FAN_MARK_EVICTABLE) + mark->flags |= FSNOTIFY_MARK_FLAG_NO_IREF; + ret = fsnotify_add_mark_locked(mark, connp, obj_type, fsid); if (ret) { fsnotify_put_mark(mark); @@ -1185,13 +1203,22 @@ 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); } } + /* + * Non evictable mark cannot be downgraded to evictable mark. + */ + ret = -EEXIST; + if (flags & FAN_MARK_EVICTABLE && + !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)) + goto out; + /* * Error events are pre-allocated per group, only if strictly * needed (i.e. FAN_FS_ERROR was requested). @@ -1601,6 +1628,14 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, mark_type != FAN_MARK_FILESYSTEM) goto fput_and_out; + /* + * Evictable is only relevant for inode marks, because only inode object + * can be evicted on memory pressure. + */ + if (flags & FAN_MARK_EVICTABLE && + 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 diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index e8ac38cc2fd6..f1f89132d60e 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_EVICTABLE 0x00000200 /* These are NOT bitwise flags. Both bits can be used togther. */ #define FAN_MARK_INODE 0x00000000 From patchwork Wed Apr 13 09:09:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811773 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 AC876C433F5 for ; Wed, 13 Apr 2022 09:10:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234291AbiDMJNP (ORCPT ); Wed, 13 Apr 2022 05:13:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231165AbiDMJMU (ORCPT ); Wed, 13 Apr 2022 05:12:20 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9526A2ED6B for ; Wed, 13 Apr 2022 02:09:59 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id q3so1011281wrj.7 for ; Wed, 13 Apr 2022 02:09:59 -0700 (PDT) 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=qSJ0rqC4gIfg/vNqPrVZaeJuRUg8aA2BJvhi9wYEDeQ=; b=K9FzpRFLC3iF4sLwyAebNr1wbK7EmRWPzMcyQgvjpb1ckuZRUkRm15wb1xc2Uk86v/ d63vrlUlglK8EntW652mgL+E7LbaDqiSa+48tyzZt28j+kp0aTXOV6eCqSm83SJBYqu4 2Glm8LcRgQnzEu0RDhB3DaC+d7WDSrCgH8aealuy8tx3bp7tkzt4JFrDeEK3PwzdDNLP qOlYl33+3uohobLDIN4xXPQd65FGIrCsN4oNJra+yQR92qjDEY/G8xSrTbxqx/W0ObEy 6U77MRd9htvrdEoQ2VdONt0YiRAibQX27Plq6yQRNcJnUuq+Wf/9rF+A3jLRHy0/u1je GfcQ== 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=qSJ0rqC4gIfg/vNqPrVZaeJuRUg8aA2BJvhi9wYEDeQ=; b=H1H4m2Vxr8NC4I6ERUbgkRCRWiUV99wGbsE48zjlVAriP3CrE/r5WL7SkMVN0JbTkK BOaUIWVwiPIB1DJrQAKmzlTWJoVaDnHnL5LyJ1xlFsjQuea+cArOZW1YW+TLyudsE/p1 AXOHbxhFidk9NC208MdWFJTubM/ggKkpjfkQ/CZUmoSx8RywqKt8a46Wf0uIucxti3z/ 3/wS66ygDXgNxibdZ98YTabXXacHtZZ/Y6cdXIQzO+HlizjPQQj6xdehBTNRbgUmxG6l Rd0V5HfNleg/h64ghCHb48fdQH/dpuXbo4fa6XNlRVlBXxyDP200x4lXp9Kh/O7MfTzD lVNA== X-Gm-Message-State: AOAM532vwTeQarry6hsOUJcNNtfp3PW72o7jvHA0WsF+TQUyS653kOxc hQkqfhozdNoq389d8loEbiw= X-Google-Smtp-Source: ABdhPJxUXHaEdWjrNTyUzAl7+0FChgk9iohtl+sgnittC9SL5S8NVIpFZqnj8kZLm8WLq5Q7spZivA== X-Received: by 2002:a05:6000:1842:b0:207:9b57:6bbf with SMTP id c2-20020a056000184200b002079b576bbfmr17131063wri.336.1649840998059; Wed, 13 Apr 2022 02:09:58 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:57 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 15/16] fanotify: use fsnotify group lock helpers Date: Wed, 13 Apr 2022 12:09:34 +0300 Message-Id: <20220413090935.3127107-16-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Direct reclaim from fanotify mark allocation context may try to evict inodes with evictable marks of the same group and hit this deadlock: [<0>] fsnotify_destroy_mark+0x1f/0x3a [<0>] fsnotify_destroy_marks+0x71/0xd9 [<0>] __destroy_inode+0x24/0x7e [<0>] destroy_inode+0x2c/0x67 [<0>] dispose_list+0x49/0x68 [<0>] prune_icache_sb+0x5b/0x79 [<0>] super_cache_scan+0x11c/0x16f [<0>] shrink_slab.constprop.0+0x23e/0x40f [<0>] shrink_node+0x218/0x3e7 [<0>] do_try_to_free_pages+0x12a/0x2d2 [<0>] try_to_free_pages+0x166/0x242 [<0>] __alloc_pages_slowpath.constprop.0+0x30c/0x903 [<0>] __alloc_pages+0xeb/0x1c7 [<0>] cache_grow_begin+0x6f/0x31e [<0>] fallback_alloc+0xe0/0x12d [<0>] ____cache_alloc_node+0x15a/0x17e [<0>] kmem_cache_alloc_trace+0xa1/0x143 [<0>] fanotify_add_mark+0xd5/0x2b2 [<0>] do_fanotify_mark+0x566/0x5eb [<0>] __x64_sys_fanotify_mark+0x21/0x24 [<0>] do_syscall_64+0x6d/0x80 [<0>] entry_SYSCALL_64_after_hwframe+0x44/0xae Set the FSNOTIFY_GROUP_NOFS flag to prevent going into direct reclaim from allocations under fanotify group lock and use the safe group lock helpers. Suggested-by: Jan Kara Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/ Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 0b4beba95fa8..fe2534043c17 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1035,10 +1035,10 @@ static int fanotify_remove_mark(struct fsnotify_group *group, __u32 removed; int destroy_mark; - mutex_lock(&group->mark_mutex); + fsnotify_group_lock(group); fsn_mark = fsnotify_find_mark(connp, group); if (!fsn_mark) { - mutex_unlock(&group->mark_mutex); + fsnotify_group_unlock(group); return -ENOENT; } @@ -1048,7 +1048,7 @@ static int fanotify_remove_mark(struct fsnotify_group *group, fsnotify_recalc_mask(fsn_mark->connector); if (destroy_mark) fsnotify_detach_mark(fsn_mark); - mutex_unlock(&group->mark_mutex); + fsnotify_group_unlock(group); if (destroy_mark) fsnotify_free_mark(fsn_mark); @@ -1200,13 +1200,13 @@ static int fanotify_add_mark(struct fsnotify_group *group, struct fsnotify_mark *fsn_mark; int ret; - mutex_lock(&group->mark_mutex); + fsnotify_group_lock(group); fsn_mark = fsnotify_find_mark(connp, group); if (!fsn_mark) { fsn_mark = fanotify_add_new_mark(group, connp, obj_type, flags, fsid); if (IS_ERR(fsn_mark)) { - mutex_unlock(&group->mark_mutex); + fsnotify_group_unlock(group); return PTR_ERR(fsn_mark); } } @@ -1232,7 +1232,7 @@ static int fanotify_add_mark(struct fsnotify_group *group, ret = fanotify_mark_add_to_mask(fsn_mark, mask, flags); out: - mutex_unlock(&group->mark_mutex); + fsnotify_group_unlock(group); fsnotify_put_mark(fsn_mark); return ret; @@ -1386,7 +1386,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ group = fsnotify_alloc_group(&fanotify_fsnotify_ops, - FSNOTIFY_GROUP_USER); + FSNOTIFY_GROUP_USER | FSNOTIFY_GROUP_NOFS); if (IS_ERR(group)) { return PTR_ERR(group); } From patchwork Wed Apr 13 09:09:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 12811772 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 A0733C433EF for ; Wed, 13 Apr 2022 09:10:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234278AbiDMJNO (ORCPT ); Wed, 13 Apr 2022 05:13:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234339AbiDMJM2 (ORCPT ); Wed, 13 Apr 2022 05:12:28 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF0703DDD0 for ; Wed, 13 Apr 2022 02:10:00 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id m14so1648350wrb.6 for ; Wed, 13 Apr 2022 02:10:00 -0700 (PDT) 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=MXNZwzrbtI4Eu4i/LOeqiME138fc1RP1+sEhKATJ+qs=; b=QwIS+Wlbhs++k9RgPI8+NvYdNn3ycIgGewaNLpgvCHQJ2LnEkEtZZ6G31Nd74lSC0g ae8Odru0e/OoT82iFJAe4rdkZm5KLV5CB6KxNNNPZdhjwRfsQ2/H8MoNtB8r4nfWXdLg zZRqKxpjWM69gjCsPiS35JGL16ob3MjCaEkrjq09UFsdf4IJ9h4hX8DpEAWRP6uM+45a uunG0PYsSoPE3GDc2yZLEMml4BXCrTYFBaTDjFJ9qeVZCkfEHuADmXOSSd7gXqCkqxmd ZCiqasgpSUivTsKLsHzBDwHp5+gt+4wFmkUnnlBFfV0dK5mnWOOuRD+VH387udQP3rJU F3SQ== 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=MXNZwzrbtI4Eu4i/LOeqiME138fc1RP1+sEhKATJ+qs=; b=wabklOFOc/budT6StE+c++vV7uFA50O3wKidtU35hqDqCSrFx75gzX4rnNOUM2OuHj r0IbVi9BfxuxJxQozT9RYtwKPWPyenOFBY2sp0JBjNGKI4F4vRIr2tvSiFWvhjF9nBmT 1ZaVl1lMknCgyQW2XVCmbyBFqr9UNfN2TK2VOmCsawKJt2KPv5FTw18TJW2aDktTul1b ZQ63M/Hdi3EK/KTHG3vvHNWYYOSIZbGoB1D5SBB+hiGvH/LilG95GVERIu5C52mup2AR 7M8aON1kruQsdIygSBp0m+h6snGDKJgABichFj7oOlRHRIDBowrZ1AN8F9WI5X0lCZLC J+tA== X-Gm-Message-State: AOAM5335LeuwpwPYaepLe2bFQEa2rNhy2nxuOxu2fuVhRyLV2f+KzdXu XKpK4zZHUfQ50L0v2x+i1eEwi3Dezkc= X-Google-Smtp-Source: ABdhPJwtBC0nW0xb0bptjvivdsjxzVhmbvuTrektOtNSBuN6ta1sv/DFvHvScG5Mk0wfvU/9ffbB5g== X-Received: by 2002:a05:6000:1a85:b0:205:a234:d0a5 with SMTP id f5-20020a0560001a8500b00205a234d0a5mr32554598wry.126.1649840999417; Wed, 13 Apr 2022 02:09:59 -0700 (PDT) Received: from localhost.localdomain ([5.29.13.154]) by smtp.gmail.com with ESMTPSA id bk1-20020a0560001d8100b002061d6bdfd0sm24050518wrb.63.2022.04.13.02.09.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 02:09:58 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Matthew Bobrowski , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 16/16] fanotify: enable "evictable" inode marks Date: Wed, 13 Apr 2022 12:09:35 +0300 Message-Id: <20220413090935.3127107-17-amir73il@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220413090935.3127107-1-amir73il@gmail.com> References: <20220413090935.3127107-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Now that the direct reclaim path is handled we can enable evictable inode marks. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 2 +- include/linux/fanotify.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index fe2534043c17..87756a015be9 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1788,7 +1788,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..edc28555814c 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -66,6 +66,7 @@ FAN_MARK_ONLYDIR | \ FAN_MARK_IGNORED_MASK | \ FAN_MARK_IGNORED_SURV_MODIFY | \ + FAN_MARK_EVICTABLE | \ FAN_MARK_FLUSH) /*