From patchwork Fri Nov 11 22:06:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Brennan X-Patchwork-Id: 13040870 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 6422DC433FE for ; Fri, 11 Nov 2022 22:06:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234312AbiKKWGk (ORCPT ); Fri, 11 Nov 2022 17:06:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234299AbiKKWGf (ORCPT ); Fri, 11 Nov 2022 17:06:35 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0292831214; Fri, 11 Nov 2022 14:06:34 -0800 (PST) Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2ABLomD8025514; Fri, 11 Nov 2022 22:06:30 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=3yrv6I6Gy849InlmcC+R+QpZCaxpqBZ/IFHX6Kqy7z4=; b=V0ep9cOFNP2GkNY5p/BOnc/mV2SqfDMHCmaIIxFOlgrSEUbjtOOo8P125Q5AjyQRYTh6 BM1iP1Cd84jnln3qHlk2or8o+24eXKKVHk375lpuqjQzFcNGUkNHLNHWANEuHDQwOmvR DVhVpTl970Yu6UGZdq1mTsVXMmT6fCBDDUKW9Sj0SojLwY2mbUJxMvaA+8AGKCidQ/8v CAEyVEikrqM2ZB+9koaIXx4r09gJ2bGlaybhU+b0vT1YQO68Pm7jW4IX0lDIQ/Ps1qr3 5QiQ8ah8soytiqlZ5nYkat9y0AP5/ior5zvl0maBp4jRbU1LJnm5AxjzT6JUVkS+YUUh WQ== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ksxnrr133-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Nov 2022 22:06:28 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 2ABLQB9a021585; Fri, 11 Nov 2022 22:06:21 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2173.outbound.protection.outlook.com [104.47.73.173]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3kpcq6v66y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Nov 2022 22:06:21 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=m08OGeZeIxEniCWgdOELjB2ALWinXFiQE8B0u/kQd2mK49RwVzTqECt7VU3iHPiiJoj6kgP8+EIygPHAPc+BMo/JIfX3ucZrfYOzZMytFryK7hGrdvOBQFiRb8kbq7IV7nKBTWlX7oxMuwOPbGAy01Sma//fI1CI6DeAiG8v0sVKr8mFTe2PCq3ZgnSsdQ9H+NKec2RA38MqC078yt4Bd2U+IsEmTt5z1zQbHX6VrSZlB07c20e4/CG7bokEmJMuEOoN6AzBB/0JlkDsVGLc4bBEGsFCVkCLj+Y1/+VEtUTaMI1h/K6SrHFwAysQRq/wRdk1eC42z3tWkVCg0JjJvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3yrv6I6Gy849InlmcC+R+QpZCaxpqBZ/IFHX6Kqy7z4=; b=iFga3xWIVT9GLHO2J5o6kkgiIP1nTXPHtOdgPHzx0BPiD6Y5wW0czOf/5+XgmC97MtKiXfIISOFxopORv9BFNm1DsJtL9C3ogi0oXoDOkb0ekSYF5Zg63TgJjlitGtmCU5ekEYxX2mNS9pMq23IsFQy9Hc/qHNy81vwbTRh1guymMOqer52fJ0a+RTEilToGlCl6PSlUyqCYTgqP8/X4SS73GRsNaaDfzbCxAnpF2y9S0zvmGQETXo0Er7x9QlkLJjW+hPcYS2d2tZjd9BpjGG1rt8soeuCBW51OAYbvN3uQF/Rc/a6wRFjxBOhh49CUlbJig2/iDVhkxVm4PJrQQg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3yrv6I6Gy849InlmcC+R+QpZCaxpqBZ/IFHX6Kqy7z4=; b=fgPMZiK0CaZjZ9gg4Dpn8PmD1Gtv3X+R/bLvlYYXnhuPilzog76HISmYrhLNCSFLqtKHjaIp9Fl0IkIb6M3oE1k+ybX9Wi0a811+kqZh2iiqIZy+NX8epj4tn7X6GcLlAZcMoFFfBm2x9Kez2yydVn2uhhvDBM8WN6dKUyqf/Ek= Received: from CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) by PH0PR10MB4535.namprd10.prod.outlook.com (2603:10b6:510:31::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.14; Fri, 11 Nov 2022 22:06:19 +0000 Received: from CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::c544:e51b:19a0:35b2]) by CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::c544:e51b:19a0:35b2%7]) with mapi id 15.20.5813.013; Fri, 11 Nov 2022 22:06:19 +0000 From: Stephen Brennan To: Jan Kara Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Al Viro , Stephen Brennan , Amir Goldstein Subject: [PATCH v4 1/5] fsnotify: clear PARENT_WATCHED flags lazily Date: Fri, 11 Nov 2022 14:06:10 -0800 Message-Id: <20221111220614.991928-2-stephen.s.brennan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221111220614.991928-1-stephen.s.brennan@oracle.com> References: <20221028001016.332663-1-stephen.s.brennan@oracle.com> <20221111220614.991928-1-stephen.s.brennan@oracle.com> X-ClientProxiedBy: SA1PR05CA0004.namprd05.prod.outlook.com (2603:10b6:806:2d2::29) To CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4166:EE_|PH0PR10MB4535:EE_ X-MS-Office365-Filtering-Correlation-Id: 35d3b148-cf48-4694-f958-08dac430f5e9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZMXJb07lkUQZNxvXKtX5BYYz7dxzUlmoQMSCwoqhF/kMG6+xCyV05NLeat/ulnq/w50A0LgaAZZqp6+FOYxGmIQh4Y0IQyWm+XPPeO/lTg+tHwviOTGH0F3qhQNrR/PqPkxwS5lDUqxoWnnFvK4R+0NQPhLkENz5HBAZt4ypmbtZ+sV7AghjzkSvxWRcYAEGlnQWRyK11a/Nt6cg7jI4DtxxWy5Z6snHdaoEGA77hfAT1etq3FI7HQSr9To22zFGTWP/zpn8JZduVhBLzkXHTqHb4sLE+6BXnrAzCPZoTp3rNcD0xXCB8Si7dAn9J3kXvQoDb93Yks3aMnkeWq1g+24e0hRjFxv/+Lc0jzhTXXCjFer7yqz3Cp78QQTcwGHUVq7JOSo+h3//QqkcyxEZcT8JsMV/2UPHisegjKH7znp8KjymdstsTsj7V/6w2aH+elRk9P/uQh5/+4yiiMFkAzGZIaay5n/E0J6HwVWH8kb6maEvL9QwYekcLm3CLAA8sW0WYr51cWaQRNOQypLe/OQP9mBJM31I2iqRJ1cO0CGfv5InaU0khguMVwA2f1GvpS8y8umOsMy3spidkmqQQzmsmZnYkuGDlEFAMkCeMJ5vFku1q/dXZwTWB3lEf+Zn8VVVebaFyhu6We30QxMHOhYkcSSztDBck00+3DCiU+TdOUBK+P1SkNFRGSV3BsB9W9kuBruxv9LR5jHGQhumdQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR10MB4166.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(136003)(39860400002)(346002)(396003)(376002)(366004)(451199015)(86362001)(83380400001)(103116003)(38100700002)(66946007)(2906002)(8936002)(5660300002)(4326008)(8676002)(66476007)(41300700001)(186003)(6506007)(26005)(1076003)(66556008)(6512007)(6666004)(54906003)(2616005)(6916009)(316002)(6486002)(478600001)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: q520Cc6u03dgODZf5qTDATRktLdEs4bTgiK1VtxWM9pYlPKs8QmFnpnT1kuHJJYNxejyzs/RkfOZhBvxrIAQRapflwqdYdg1LKe5cmto1PzjG0eYwbNWwTSkLqk//kSJRu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/nRE3xqNVsvVPpr4haA5Ni3lZpX5JWLisonwDzLbvH+UuK05YdiI9D2T2F3KnVNcKlSJpSAV0PVAG/dIgAuiTNfJABX/0fokZYaRqJdRX0/fKNcAYU+JgUiCS+zbwEWm+eKRT5k9b7Mszhn9Gius0Ts6w64RgoaB/rQdPiaZtx2+7Nn8JSSmxZDf9QitlFFHvm8sfyrl7nMEA63QYeD1Y5mO2TBHZYj3MPgnUMjyw31fWBwA+jmbo6BY1ewf5aObsf0jFZ0jCX/f0rog38M/GHv8XbhW5Xhmszsey3rmsJFHtms4MbcXIUxN4DXYh/NjGJSw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 35d3b148-cf48-4694-f958-08dac430f5e9 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4166.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Nov 2022 22:06:19.1134 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: r3fPDBoGziogybevJFVA+FAOITFP0SHFtG0XyNbTjkivgNYyBr0BweXyZmTU0i4QkzUuDUdQo+He86c97btXSo/PopiKXHthuXsIuk8gfDw= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB4535 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-11_11,2022-11-11_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 spamscore=0 adultscore=0 malwarescore=0 mlxlogscore=999 mlxscore=0 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2211110149 X-Proofpoint-GUID: hfbPWyGWbEGvUlwFYDFWtXmPFsHkiexv X-Proofpoint-ORIG-GUID: hfbPWyGWbEGvUlwFYDFWtXmPFsHkiexv Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Amir Goldstein Call fsnotify_update_children_dentry_flags() to set PARENT_WATCHED flags only when parent starts watching children. When parent stops watching children, clear false positive PARENT_WATCHED flags lazily in __fsnotify_parent() for each accessed child. Suggested-by: Jan Kara Signed-off-by: Amir Goldstein Signed-off-by: Stephen Brennan --- Notes: This is Amir's patch based on Jan's idea, with no changes or alterations from my side. fs/notify/fsnotify.c | 26 ++++++++++++++++++++------ fs/notify/fsnotify.h | 3 ++- fs/notify/mark.c | 32 +++++++++++++++++++++++++++++--- include/linux/fsnotify_backend.h | 8 +++++--- 4 files changed, 56 insertions(+), 13 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 7974e91ffe13..2c50e9e50d35 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -103,17 +103,13 @@ void fsnotify_sb_delete(struct super_block *sb) * parent cares. Thus when an event happens on a child it can quickly tell * if there is a need to find a parent and send the event to the parent. */ -void __fsnotify_update_child_dentry_flags(struct inode *inode) +void fsnotify_update_children_dentry_flags(struct inode *inode, bool watched) { struct dentry *alias; - int watched; if (!S_ISDIR(inode->i_mode)) return; - /* determine if the children should tell inode about their events */ - watched = fsnotify_inode_watches_children(inode); - spin_lock(&inode->i_lock); /* run all of the dentries associated with this inode. Since this is a * directory, there damn well better only be one item on this list */ @@ -140,6 +136,24 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode) spin_unlock(&inode->i_lock); } +/* + * Lazily clear false positive PARENT_WATCHED flag for child whose parent had + * stopped wacthing children. + */ +static void fsnotify_update_child_dentry_flags(struct inode *inode, + struct dentry *dentry) +{ + spin_lock(&dentry->d_lock); + /* + * d_lock is a sufficient barrier to prevent observing a non-watched + * parent state from before the fsnotify_update_children_dentry_flags() + * or fsnotify_update_flags() call that had set PARENT_WATCHED. + */ + if (!fsnotify_inode_watches_children(inode)) + dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED; + spin_unlock(&dentry->d_lock); +} + /* Are inode/sb/mount interested in parent and name info with this event? */ static bool fsnotify_event_needs_parent(struct inode *inode, struct mount *mnt, __u32 mask) @@ -208,7 +222,7 @@ int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data, p_inode = parent->d_inode; p_mask = fsnotify_inode_watches_children(p_inode); if (unlikely(parent_watched && !p_mask)) - __fsnotify_update_child_dentry_flags(p_inode); + fsnotify_update_child_dentry_flags(p_inode, dentry); /* * Include parent/name in notification either if some notification diff --git a/fs/notify/fsnotify.h b/fs/notify/fsnotify.h index fde74eb333cc..bce9be36d06b 100644 --- a/fs/notify/fsnotify.h +++ b/fs/notify/fsnotify.h @@ -74,7 +74,8 @@ static inline void fsnotify_clear_marks_by_sb(struct super_block *sb) * update the dentry->d_flags of all of inode's children to indicate if inode cares * about events that happen to its children. */ -extern void __fsnotify_update_child_dentry_flags(struct inode *inode); +extern void fsnotify_update_children_dentry_flags(struct inode *inode, + bool watched); extern struct kmem_cache *fsnotify_mark_connector_cachep; diff --git a/fs/notify/mark.c b/fs/notify/mark.c index c74ef947447d..6797a2952f87 100644 --- a/fs/notify/mark.c +++ b/fs/notify/mark.c @@ -176,6 +176,24 @@ static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) return fsnotify_update_iref(conn, want_iref); } +static bool fsnotify_conn_watches_children( + struct fsnotify_mark_connector *conn) +{ + if (conn->type != FSNOTIFY_OBJ_TYPE_INODE) + return false; + + return fsnotify_inode_watches_children(fsnotify_conn_inode(conn)); +} + +static void fsnotify_conn_set_children_dentry_flags( + struct fsnotify_mark_connector *conn) +{ + if (conn->type != FSNOTIFY_OBJ_TYPE_INODE) + return; + + fsnotify_update_children_dentry_flags(fsnotify_conn_inode(conn), true); +} + /* * Calculate mask of events for a list of marks. The caller must make sure * connector and connector->obj cannot disappear under us. Callers achieve @@ -184,15 +202,23 @@ static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) */ void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) { + bool update_children; + if (!conn) return; spin_lock(&conn->lock); + update_children = !fsnotify_conn_watches_children(conn); __fsnotify_recalc_mask(conn); + update_children &= fsnotify_conn_watches_children(conn); spin_unlock(&conn->lock); - if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) - __fsnotify_update_child_dentry_flags( - fsnotify_conn_inode(conn)); + /* + * Set children's PARENT_WATCHED flags only if parent started watching. + * When parent stops watching, we clear false positive PARENT_WATCHED + * flags lazily in __fsnotify_parent(). + */ + if (update_children) + fsnotify_conn_set_children_dentry_flags(conn); } /* Free all connectors queued for freeing once SRCU period ends */ diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index d7d96c806bff..1276de409724 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -563,12 +563,14 @@ static inline __u32 fsnotify_parent_needed_mask(__u32 mask) static inline int fsnotify_inode_watches_children(struct inode *inode) { + __u32 parent_mask = READ_ONCE(inode->i_fsnotify_mask); + /* FS_EVENT_ON_CHILD is set if the inode may care */ - if (!(inode->i_fsnotify_mask & FS_EVENT_ON_CHILD)) + if (!(parent_mask & FS_EVENT_ON_CHILD)) return 0; /* this inode might care about child events, does it care about the * specific set of events that can happen on a child? */ - return inode->i_fsnotify_mask & FS_EVENTS_POSS_ON_CHILD; + return parent_mask & FS_EVENTS_POSS_ON_CHILD; } /* @@ -582,7 +584,7 @@ static inline void fsnotify_update_flags(struct dentry *dentry) /* * Serialisation of setting PARENT_WATCHED on the dentries is provided * by d_lock. If inotify_inode_watched changes after we have taken - * d_lock, the following __fsnotify_update_child_dentry_flags call will + * d_lock, the following fsnotify_update_children_dentry_flags call will * find our entry, so it will spin until we complete here, and update * us with the new state. */ From patchwork Fri Nov 11 22:06:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Brennan X-Patchwork-Id: 13040872 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 EA270C433FE for ; Fri, 11 Nov 2022 22:06:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234410AbiKKWGn (ORCPT ); Fri, 11 Nov 2022 17:06:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234318AbiKKWGh (ORCPT ); Fri, 11 Nov 2022 17:06:37 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC39B2FC35; Fri, 11 Nov 2022 14:06:36 -0800 (PST) Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2ABLomDE025514; Fri, 11 Nov 2022 22:06:33 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=FtqVkv0iOH1SN8NSSVD9V+dDgHKSvOJG9Pe24Pax1O4=; b=qu5KhkgyAmxUUl6q+T5YyodabUvP1D/i/r+UabMI+9WMKptFVzSGLHXX/I3xovMMgI5B 9LI0ViQU4fk6UOin+TgNTuThRC1WaqZh2akTH+tIiPaPebVoPNmcQRC2bcsdB3Uey1bs QqyMoD598EqMdIHYc6hC1rp+okWUJhQg/FEOtIYILnHI9c8EbXbfhOv0GaIDjLdBxmxw 0XLe60RttOyI/ze4VVWNZWKXc7eUWF//iovSmgQltrxAefyp5aFIpIZ6PQYoI4ATVfNf qMFGrS+Tv+6Aony8/W4+IzHotQo3AsjhIDqXyDdeHmk/eMFZUNNNIZfUfBULRIFw1rEk /w== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ksxnrr137-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Nov 2022 22:06:29 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 2ABKXRTm011811; Fri, 11 Nov 2022 22:06:23 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2173.outbound.protection.outlook.com [104.47.73.173]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3kpcth2hng-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Nov 2022 22:06:23 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DzuFNpylx3AKnfDCAWmpdpQXxiTa7rYZAEDrJ3n7hj7ZFiq+Kaz5q76/HG8NbxrgbuK7xvskVPT2VdxrimNPYtXQwMriD7E5NLF1+F25jKxsvJF8HexqBDgXe4HJibD/KkXj+59SvSNLc+jms+CWLP1sGrHz78XGWkkLdUPvpVLHx6zxlnvsecl2CwssAN2f0wDhuVklCY+b+eLuVi0URc8M/5MkLKySziN8/nkJc42JymjJ4KVj3nUlxWvo/UA4UWF1apTJUp/EVs9FDNg/FlIGBqzBmb41ReFnBcoxS3o8/w09W2G6HXXOMLwTyXuzqPx5QUfwwGBYFS+IeLT6uQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=FtqVkv0iOH1SN8NSSVD9V+dDgHKSvOJG9Pe24Pax1O4=; b=gxP/F5m2tpRGW94JDSy9wClbSL74kdpgE3J/K3ECPTAD3JnVqXQXbWqKVVL6yhG/myVl04+POFYrE0XUXNMT2H4zyNQyxegYn8UIPhyPYraj0DowO9QcKV/Zn1MtoBVx+yDvdo6zK/OqtpqiNrpRJ+4DHYlmHoZo/QZsd8OAl850c0ItjE3s6Ip0f1llucvQ7YdnTOJRZBX0lUa5rRqbC3s/6I/K/Tt9n0Ops7x6lmbF5Pf5Ll7mRunOHYfye3RIDA+DrIQPAbnZ3SyurfTFJBhMO0VivcGQER04N5hFf8elwYqKTrl3csecbmIgzMPht4yHtMCS5TxArltIM6AfFA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=FtqVkv0iOH1SN8NSSVD9V+dDgHKSvOJG9Pe24Pax1O4=; b=Btm6Rm7kDP00UZget7vJVPqnqVwTnvpHs1VKlTqCeBqSFaDV/FXrAZ/BtWT30iH11DW34HC3x+u8NRraT2h9VWzGDeBXI6jAW5JN1TZJsEhoS0B87jqTA7VY+HgZzYowPTFqyTny1xpess0LlrH6FUnPhVjuqkYNgBeNwhmILsg= Received: from CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) by PH0PR10MB4535.namprd10.prod.outlook.com (2603:10b6:510:31::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.14; Fri, 11 Nov 2022 22:06:21 +0000 Received: from CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::c544:e51b:19a0:35b2]) by CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::c544:e51b:19a0:35b2%7]) with mapi id 15.20.5813.013; Fri, 11 Nov 2022 22:06:21 +0000 From: Stephen Brennan To: Jan Kara Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Al Viro , Stephen Brennan , Amir Goldstein Subject: [PATCH v4 2/5] fsnotify: Use d_find_any_alias to get dentry associated with inode Date: Fri, 11 Nov 2022 14:06:11 -0800 Message-Id: <20221111220614.991928-3-stephen.s.brennan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221111220614.991928-1-stephen.s.brennan@oracle.com> References: <20221028001016.332663-1-stephen.s.brennan@oracle.com> <20221111220614.991928-1-stephen.s.brennan@oracle.com> X-ClientProxiedBy: SA9PR13CA0132.namprd13.prod.outlook.com (2603:10b6:806:27::17) To CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4166:EE_|PH0PR10MB4535:EE_ X-MS-Office365-Filtering-Correlation-Id: 5036e0a7-4f56-45fe-6efd-08dac430f729 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jiW+7QnRflwaeEoKoAXq+gGdhqm6i0ThF8sxlaH5Ikmqa2bzMFgQUglV3/TXIZ4Flet+AehxM3CZqvEtNVJbTUkcbrNLjY4LOOl9ak06CnYm/280Z1nhVIUmHFQ96v8h84flux1tebq+v7T3ncAQ46JYMILx64AmTErH2/bITaC5P7zy+po/Sgru0BAt/q8Bh6qohGq0QqSQPcquoSZO0J2hjQnL5Jlv4Cw46rmpV5jyiZv3voGy/s1oqBTi+XtCV4lego6IblnUEwV7tNPzNDEtGzCGouJNYOAupF4fw9mOE/lVbKVizs2nQZn42HhYyYthqeIZIE2N0JMQs7zQBSQj3oTnD46nT3npPfyX/xAWiHARG1a4VrXya107z9iqLgC0Y+Pz7/iRkkEqngW3rvPyN24GZb9XmwsFc+sA2FkHPRl/EUXjqUDWPC7QzbOiJWCWYg14TmPg0SRmFv073rv9jY193Z+MSPZ8lA8JKXS85M1HzwvfRhDxnLS/5iCRJZlyfWTZ76kPz67cvWLQL/OEsQ17demusFj69bhpoyFUQffTWN+/DT2goMIuPWaJfseYBCzzK7aDWuePLy0Yd51dJ5pD0vVNd55Six/NQk5OcyoTh8SqetwWNpTv0SxI9cCdZcgnvka9Ideb3sBaczwawmTdHUyglpN3ORUVWtG9sgasMFDOeKjenPlJdZrSlyIbAdy7mNrw1i7gkUyjtg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR10MB4166.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(136003)(39860400002)(346002)(396003)(376002)(366004)(451199015)(86362001)(83380400001)(103116003)(38100700002)(66946007)(2906002)(8936002)(5660300002)(4326008)(8676002)(66476007)(41300700001)(186003)(6506007)(26005)(1076003)(66556008)(6512007)(6666004)(54906003)(2616005)(6916009)(316002)(6486002)(478600001)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LmipkT2K9W/wK6Q93AciZXsOHjEfZET4son8AuFN4fZNEus6nKPig3Hm87hzjYrmsMWM8Kso9rk+4Z5WfHzaQM+bpAC4a6p7M7wiv6gkzjCbpwutQNc6n6f4LvXMi60yahNAdMeZGnRfVpeJhQU6NaETsO+kyEJIomDn+8wT5trX2EYL5vqlsekCi4TdnCFQs0ec/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ioxS31xQs0it9V9Z1EQjWbgaMl1/e0hQhm3ermhziWCldS+lPPFgSaXMrcxOxrMaNyfv2R8mN7vvVyq2HeQmBxCcvZn84sLoS21XEMVHs4weUcMHkpWzaBoc/0HaVA60I5B9GyrueLYWP63AJZ3J5+wqXXv5orB9k9JhuVu7sAqJqY7owBEQT95sZIP/62TWfcJCQ7IKx+vfuQSm99vLbp6vPMjPsKfy1hCcPaWXxKa5WRWxb+I0vcgz/w== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5036e0a7-4f56-45fe-6efd-08dac430f729 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4166.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Nov 2022 22:06:21.1758 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: CbDyTeGGLxRsO6BGI6Four0J2yrlWvYpT+aZGePRzj6QkPSvFCAj9E1dM2bFYURAWrRK/9eKBxihUMpeowfIZrI6OsCkWZq+U50xi2HvuCc= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB4535 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-11_11,2022-11-11_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 mlxscore=0 spamscore=0 bulkscore=0 suspectscore=0 adultscore=0 malwarescore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2211110149 X-Proofpoint-GUID: CVkloTK151cW8yO6y7JXXcHrtvwDu6A9 X-Proofpoint-ORIG-GUID: CVkloTK151cW8yO6y7JXXcHrtvwDu6A9 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Rather than iterating over the inode's i_dentry (requiring holding the i_lock for the entire duration of the function), we know that there should be only one item in the list. Use d_find_any_alias() and no longer hold i_lock. Signed-off-by: Stephen Brennan Reviewed-by: Amir Goldstein --- Notes: Changes in v4: - Bail out if d_find_any_alias() returns NULL - Rebase on Amir's patch Changes in v3: - Add newlines in block comment - d_find_any_alias() returns a reference, which I was leaking. Add a dput(alias) at the end. - Add Amir's R-b fs/notify/fsnotify.c | 46 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 2c50e9e50d35..409d479cbbc6 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -105,35 +105,35 @@ void fsnotify_sb_delete(struct super_block *sb) */ void fsnotify_update_children_dentry_flags(struct inode *inode, bool watched) { - struct dentry *alias; + struct dentry *alias, *child; if (!S_ISDIR(inode->i_mode)) return; - spin_lock(&inode->i_lock); - /* run all of the dentries associated with this inode. Since this is a - * directory, there damn well better only be one item on this list */ - hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { - struct dentry *child; - - /* run all of the children of the original inode and fix their - * d_flags to indicate parental interest (their parent is the - * original inode) */ - spin_lock(&alias->d_lock); - list_for_each_entry(child, &alias->d_subdirs, d_child) { - if (!child->d_inode) - continue; + /* Since this is a directory, there damn well better only be one child */ + alias = d_find_any_alias(inode); + if (!alias) + return; - spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); - if (watched) - child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED; - else - child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED; - spin_unlock(&child->d_lock); - } - spin_unlock(&alias->d_lock); + /* + * run all of the children of the original inode and fix their + * d_flags to indicate parental interest (their parent is the + * original inode) + */ + spin_lock(&alias->d_lock); + list_for_each_entry(child, &alias->d_subdirs, d_child) { + if (!child->d_inode) + continue; + + spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); + if (watched) + child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED; + else + child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED; + spin_unlock(&child->d_lock); } - spin_unlock(&inode->i_lock); + spin_unlock(&alias->d_lock); + dput(alias); } /* From patchwork Fri Nov 11 22:06:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Brennan X-Patchwork-Id: 13040871 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 0CE8EC43219 for ; Fri, 11 Nov 2022 22:06:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234272AbiKKWGl (ORCPT ); Fri, 11 Nov 2022 17:06:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234255AbiKKWGg (ORCPT ); Fri, 11 Nov 2022 17:06:36 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B6982DAB0; Fri, 11 Nov 2022 14:06:35 -0800 (PST) Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2ABLomDB025514; Fri, 11 Nov 2022 22:06:32 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=r+JK2xhlQ14b6zZzmCECL5gWzBVH/1UEmZ33S4f1XoI=; b=tclGVTzxO0Yf+DxYHa6xvl/A+73Ce+8JoBN1LMYtFxDRWuXG7zRmYUq5qc++Fdxrt02A veJlr8LPE3+W8J3SwDyQMAYktCWSi/31HDDmRrlCZJL7QjV0BeUJTZdfV4qN/J7LWk2A wPJsdwdKy7ha/yUAcC2nvus5hrwavzgkQtN1cCBlUk2cz06gUyZA1U6DRXyKSs8X/6rr bQM1ZPR/wAEH+a/tjw2hncP00lZyTvEZF0bhhec4JB/LQZocQuafT29Y1EawvBpsDai3 9gkc9e3QDzE9/mFO2+/KrW4aHM1MgpM3gea8Arttdulb9TyU4o6DysMM2k1RoMVuzZCs aA== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ksxnrr13d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Nov 2022 22:06:30 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 2ABLV4h1009032; Fri, 11 Nov 2022 22:06:25 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2175.outbound.protection.outlook.com [104.47.73.175]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3kpcqmyka4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Nov 2022 22:06:25 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gC+88rtip8Jd2v9ropx1S8A0E5/XMXVolcAwXVjlqQgxYvJqAm53fsneNKC79OPWC3YofWAXdNAmEg/6Ykc+zSCUZhNhkQvUX/4LKsB2HWs1gp4le/6BeKJ3YcG9JGcA642ghRahxNX1H6Wtwsc+cSq4cIVsPRUfsH6p/zlGzjEnhvMHdRNliWacr4BIC5+51xj9TjYUoLw8V0BX5kDqEReAPPznGRGpzH6ma/2Uo4wBlzHjnH+Pho2bjdp79NCmORa3sF0CJDFPysHTnnLqfwkIwm0nY4b3vUb7EmVUTfABooZm2pUamNq1dQyxY3RnHVr/OMZQ9OodRobEKTzsfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=r+JK2xhlQ14b6zZzmCECL5gWzBVH/1UEmZ33S4f1XoI=; b=dajKbW+4NKeUxYJKYplFNCWgT4i/F5dtPQisuMFqRX5PMZ5dPIGrlxvQHRksgaSF6D0uhe8xWoSAMnDkBWrf1O5jxUrk6MGaldN8ht7NCsC4q9+BVujbpf478ArXonXiGXdJv0SVYhHtLpJ3fSt4v23DqfVuPKiD9tIvtlAD6MjHK9/xJM/t7KVOuwO2xgb7WZsCgkYIC+DxAdXoSPdAYICG4h+jcVDiQa22/urnbkvIxRjzJWGYyHCuMmwn6dgRM05P2E1kOGbmT6u7vCIJsBFHBmYF6vctxw6OOBPT3pEGHyopLxZYs+5Q9Wklg8X482CpFy1Z0bgNFZ+7BDM6pg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=r+JK2xhlQ14b6zZzmCECL5gWzBVH/1UEmZ33S4f1XoI=; b=CeFVh9we39c5aCNwS1Xv19L1RIsDI9eqKYOkfV7XEy894YP2VXh/KYOvjDWWUCoZ34dBPpxCnd0C4IMyXfgCDX8YVM2Sf0iTpjoLEqea6goojd/Hvq1wnJRmjm3pTeRtGENIC9yLW2RjyM+GRX1FhFdok/y2j3UGHDDBP95kEyQ= Received: from CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) by PH0PR10MB4535.namprd10.prod.outlook.com (2603:10b6:510:31::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.14; Fri, 11 Nov 2022 22:06:23 +0000 Received: from CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::c544:e51b:19a0:35b2]) by CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::c544:e51b:19a0:35b2%7]) with mapi id 15.20.5813.013; Fri, 11 Nov 2022 22:06:23 +0000 From: Stephen Brennan To: Jan Kara Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Al Viro , Stephen Brennan , Amir Goldstein Subject: [PATCH v4 3/5] dnotify: move fsnotify_recalc_mask() outside spinlock Date: Fri, 11 Nov 2022 14:06:12 -0800 Message-Id: <20221111220614.991928-4-stephen.s.brennan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221111220614.991928-1-stephen.s.brennan@oracle.com> References: <20221028001016.332663-1-stephen.s.brennan@oracle.com> <20221111220614.991928-1-stephen.s.brennan@oracle.com> X-ClientProxiedBy: SA9P223CA0026.NAMP223.PROD.OUTLOOK.COM (2603:10b6:806:26::31) To CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4166:EE_|PH0PR10MB4535:EE_ X-MS-Office365-Filtering-Correlation-Id: ca28bbd8-9ed6-4460-e764-08dac430f861 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xKSfbNqE8eWBNoX3qvAaEphp82O/T+EZXkFaa/C4ciCocyJHEROKhvRuKocLDuPu7YiGRAS9Adx8IWJ36FS52dAV2L7CvjgpkghjPSGPzvGPu7niNHpm0TVG8INiH7cosleQl6CXXFep7bwE8FEOUnLy7kIanyhsdKzDzBb5kbMSbCmTXlY7c1NgsmEnOEayK14781Y1eQPPoz7beFkgJv/7PdD1Ujkh9mN/UPZ6+bvolh98ItzwqB3+AF4DAoF3i5tUXy42ORLE6i8igibP8NoOyOcn8CHEZ2dvygtWeqezwIUOUt+7oLxgL5JrpJg8TYnmCqaPsyMOt5BRtnd4zS0aRIPouEFiYwCMGkq4SWY3jPZam20268aY9mURXid5gn4Kf5tbYq/CrV0Om3qMf+VbkOYk+47w5tPRiUvbd71GR2Ad1jON9+DQqXDg4szUi0h4fTYaVy1hzsDvcWtUeTNLIwLG1f1PnXv6Aotyc29IbcmYi/kANLIvE81j4zFFmEZi4M4rkQI09fyB5vW2C5CJCgyKQfPMH3otO9y00ZCscHrrT8NY1W4M5lSKjvOri8dZgyk1ih+41d1gSUdGnXWWHei0X9JgtwiVvs5biUxjlfZ0O+puHSl4csp+wXmPY4waNsSfgDKdbU0xu1BIrJVKU4QY61PvkhBd0U2WT4lGQcMiXAlg5TkA92A+0TcR4PeKey8IkI1UcAkjQvXfRQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR10MB4166.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(136003)(39860400002)(346002)(396003)(376002)(366004)(451199015)(86362001)(83380400001)(103116003)(38100700002)(66946007)(2906002)(8936002)(5660300002)(4326008)(8676002)(66476007)(41300700001)(186003)(6506007)(26005)(1076003)(66556008)(6512007)(6666004)(54906003)(2616005)(6916009)(316002)(6486002)(478600001)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: CeSS0YJzUFlLH/1X6uxuCu2Xz6h+0XHIzQ2RCP5I2pMa2d0dy0RVIlwHTXAI8oMuxbyRsv2au0rpySyWKDATERTg1p0p287dEQ/iS8kygbd3CuXaZafCzE2yBZMfE5Ej/XdAfDXfA7/7dpv6csLoYpKHYlDuIFWHVi2jhaxsfIFLQcMW9pFFkctktePrfr2L15NZuAWqoxSVLep/xsIh1svoS9ZMVyqtU4xWjggeAbb3cpSGI+Pp/0SI4nJf+gwAfgoJtqdihejVeGQrOAevEBWHryRbY1BQSQIEnVqJGTXOACL5xZsLj61KJLvrlCdkGk4LU+HM7MoOehifQOwrCRKTe2ePgjpqg7tDfjXi5IfMocso2RoPmXeDNoWHPOoft6k96g226aRaWarQQsMIcf/J0zxvbYVnlDVWw2Clbr60YikP9qTmJj7fkLSHAfjurgxLheYi5V0bckgdVbcbpopKA5yyPfg5f/iaJL1O6kyv255hpdYsUc2/fsB3b1zlK0rkrUOEV/EemTrXKtZ6EspLIFEjtW3kBOSVOa3HdcIs6YMtTS2M63qR5kII8BLRjlfwte9AUqkUFpVtgmVU097h+SjzCwyDcuUZV6M8mMk/de+AwOG7sP5g6hmActXmrTdswNyE9y06Z5jvgr3jGcN9eALaKXWCKAO0FOX6JHfFOe1xhvw1z1FZTYHhHl0ic8kBnkcLkRBOmu5sO9RbgbDSd0E1WyxKbCpq5xPgvNtXZmNIyqfeKtdEbzf2SMIKXYKNKdn//cGHnZRre3l41vTiSYMmz416aa0cTopxZ+HMmh3gdPJvwwAajsXlSvYv41Xz88HneBy/IEp5QUWbfymULKICo5p61k4H16+pBDSyp58kXkfpYFW8Hlcjam+NzwLyL70ncS7ImBM8RgZWW8JCX+p6gTRXkeg8F/ttQaSJnv2I2GG2TYImgHnFvcc58O1+rvIsLwa1fr9uvoe+D4g+wXX12vxCOx7StXjQDkupLP/xjm21aSS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XAvyRn9dYalKt2yZKVbXk6GHNJBGnybsI5uXRbZw+m7LRmBQfDVD4eIcaF1F/Z9Mo7DwE5DQBt2BGMRLtvE7HU77S3E4LL64zo43KawQgOAgUzLySN+6gMXAMmPR314DYEcVRcc6Ml2JAdR5lLa5jQnU0pftoALQKdR1mPQk3hVUycRHCHNjWQjQjvO3RvheqIs3MC40XtSHgUa6O3Am8kLSU8bZiiGmFRyYj78ai7wJuqQW4btLMWovY/kBwUoPL/NQJ1nR8hLtIKDtddTQAOr1OlXcHlSw8DbDPE/4Xj0zhti3fNh6bsCGFcvein+zASW0kHKKxC2IHKuuNBRvn05ZaQm2KdmBLNLLWpUHtG76GwkboFdxYKFtFu2j5iPMeSqFhJ043o135m6Wzu1eanxkAOyqGG+Kb/REVENbvyMZh4uDQRRbHIPu/Sgh+SVelrnfzQYkHNaepEiOkVOYsMbn3XFTqbq5VLpMlPbZPk1/dT9QJqJsvPVzHXWZCTMI300+TxcpDzQg/i2g== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: ca28bbd8-9ed6-4460-e764-08dac430f861 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4166.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Nov 2022 22:06:23.2080 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: LYt2vABgmycrhUp87PpWREASBFCAerA+Uj7vdUzG2goEVQwEhWhmeNUfgpl02LuWx/LaBS9FzShw5TCg2WITdnyG03Oh1TzVCMTZ0UB381w= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB4535 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-11_11,2022-11-11_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 adultscore=0 mlxscore=0 suspectscore=0 spamscore=0 malwarescore=0 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2211110149 X-Proofpoint-GUID: f5ZYdeS7SW-nIxuPzd6BJ-bH_RQofcUv X-Proofpoint-ORIG-GUID: f5ZYdeS7SW-nIxuPzd6BJ-bH_RQofcUv Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org In order to allow sleeping during fsnotify_recalc_mask(), we need to ensure no callers are holding a spinlock. Signed-off-by: Stephen Brennan Reviewed-by: Amir Goldstein --- fs/notify/dnotify/dnotify.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/fs/notify/dnotify/dnotify.c b/fs/notify/dnotify/dnotify.c index 190aa717fa32..a9f05b3cf5ea 100644 --- a/fs/notify/dnotify/dnotify.c +++ b/fs/notify/dnotify/dnotify.c @@ -58,10 +58,10 @@ struct dnotify_mark { * dnotify cares about for that inode may change. This function runs the * list of everything receiving dnotify events about this directory and calculates * the set of all those events. After it updates what dnotify is interested in - * it calls the fsnotify function so it can update the set of all events relevant - * to this inode. + * it returns true if fsnotify_recalc_mask() should be called to update the set + * of all events related to this inode. */ -static void dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark) +static bool dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark) { __u32 new_mask = 0; struct dnotify_struct *dn; @@ -74,10 +74,9 @@ static void dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark) for (dn = dn_mark->dn; dn != NULL; dn = dn->dn_next) new_mask |= (dn->dn_mask & ~FS_DN_MULTISHOT); if (fsn_mark->mask == new_mask) - return; + return false; fsn_mark->mask = new_mask; - - fsnotify_recalc_mask(fsn_mark->connector); + return true; } /* @@ -97,6 +96,7 @@ static int dnotify_handle_event(struct fsnotify_mark *inode_mark, u32 mask, struct dnotify_struct **prev; struct fown_struct *fown; __u32 test_mask = mask & ~FS_EVENT_ON_CHILD; + bool recalc = false; /* not a dir, dnotify doesn't care */ if (!dir && !(mask & FS_ISDIR)) @@ -118,12 +118,15 @@ static int dnotify_handle_event(struct fsnotify_mark *inode_mark, u32 mask, else { *prev = dn->dn_next; kmem_cache_free(dnotify_struct_cache, dn); - dnotify_recalc_inode_mask(inode_mark); + recalc = dnotify_recalc_inode_mask(inode_mark); } } spin_unlock(&inode_mark->lock); + if (recalc) + fsnotify_recalc_mask(inode_mark->connector); + return 0; } @@ -158,6 +161,7 @@ void dnotify_flush(struct file *filp, fl_owner_t id) struct dnotify_struct **prev; struct inode *inode; bool free = false; + bool recalc = false; inode = file_inode(filp); if (!S_ISDIR(inode->i_mode)) @@ -176,7 +180,7 @@ void dnotify_flush(struct file *filp, fl_owner_t id) if ((dn->dn_owner == id) && (dn->dn_filp == filp)) { *prev = dn->dn_next; kmem_cache_free(dnotify_struct_cache, dn); - dnotify_recalc_inode_mask(fsn_mark); + recalc = dnotify_recalc_inode_mask(fsn_mark); break; } prev = &dn->dn_next; @@ -184,6 +188,9 @@ void dnotify_flush(struct file *filp, fl_owner_t id) spin_unlock(&fsn_mark->lock); + if (recalc) + fsnotify_recalc_mask(fsn_mark->connector); + /* nothing else could have found us thanks to the dnotify_groups mark_mutex */ if (dn_mark->dn == NULL) { @@ -268,6 +275,7 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) struct file *f; int destroy = 0, error = 0; __u32 mask; + bool recalc = false; /* we use these to tell if we need to kfree */ new_fsn_mark = NULL; @@ -377,9 +385,11 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) else if (error == -EEXIST) error = 0; - dnotify_recalc_inode_mask(fsn_mark); + recalc = dnotify_recalc_inode_mask(fsn_mark); out: spin_unlock(&fsn_mark->lock); + if (recalc) + fsnotify_recalc_mask(fsn_mark->connector); if (destroy) fsnotify_detach_mark(fsn_mark); From patchwork Fri Nov 11 22:06:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Brennan X-Patchwork-Id: 13040874 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 94BA6C433FE for ; Fri, 11 Nov 2022 22:07:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234486AbiKKWHj (ORCPT ); Fri, 11 Nov 2022 17:07:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234569AbiKKWHO (ORCPT ); Fri, 11 Nov 2022 17:07:14 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 102F143AE7; Fri, 11 Nov 2022 14:06:49 -0800 (PST) Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2ABLomDe025514; Fri, 11 Nov 2022 22:06:46 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=wFpbesVg/nz+VUPlEsYVZunVsPHTHm38szTsgXmNXsM=; b=uI6iNXcDWkuLhWogJxLHE1C00OPbOEcLun9h8zI+fB2i13GcLTnIGN7JtckcyF9fv6kf U+hAcM60wFaIbq9nrXGlk/JtPZgAmbXrg8Ky0Ttj6HhUVh920sfwbaH/O4tsxA1n/HOi GfPxREClNjQpdurOlY+ZZXC5oPvFwQXsmVI1CYi0HT8NqudUqL8FGQwXgP0e4rTh5euQ unKG6Yn/TwOSgesdhyrmr2en9hJSza8DARMxkw2KvRa9/CERbkF/A+O5g4BPlx+lQUKJ cWeCMDX9c6cVMe7BybYzuiB0xdvJerrDX3Kdk5D96Cg8jCE4NDHG6tZsd4em+7azjeB4 4w== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ksxnrr13k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Nov 2022 22:06:44 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 2ABLUOuw022311; Fri, 11 Nov 2022 22:06:27 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2172.outbound.protection.outlook.com [104.47.73.172]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3kpcytsv08-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Nov 2022 22:06:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XedXquWRNvlCDD3RbEvWfsRLtKWe0KNkc0Rr1Ml7msi0V2K1q7PI4pdlAkU2JTBM/Lrjo6u0zoRThHwMYJeT7SADwxwtKElgdo7CgDj2tO/B9pCBuhC6gXRaw9EoXLUOUrgn7Ks91MgHyE0ul2bS1uyH1wtPmExLjoDyKDQ3PLpY8KbzZ/VADzyAYlXppyY447+5IsdklujEOe2b34R6Dk2DIris0LXCkvHN4JBqLHGH88BEakr+nps+WSNMw3eUEthfv2kzicmxvQVegA6ev08hXjuAoD56Bym4ar5/T9e7QG+PNELIaXmaKi1Pg9mlAPnjqLlBbN6BLE9LDSv5AA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wFpbesVg/nz+VUPlEsYVZunVsPHTHm38szTsgXmNXsM=; b=HjKV+eRQdteUnBBSJBAq66EGI+v+7bWSMfpRCd1Ma4OMYImZKyRy513y3Ct28jUOsEf2b1uJmJ4v11P75gc+1C4rzyBXPdrb3+Lg7OkbisaxMSEE7UWXK7h511TPDBtJ4GejbPoAJAbzOIhMy4vcF8h/MlMq8sB7pA80KTpFzpYbeFdPV+tsSlO+yUH3Brq1vto8JaLuAP+WJSyUdWDaXQH3TL5RwcVXF4n8WTSNsIFvjBma2eJtLSyemYZsqPIhCMoyvjheK1BSjAfzDVliuD7PuF8/cphCFG8IvwsXQ2DGf5Yh383E4NoR7W5z0T+s9yZ0eO6o5Xt4a7FM3/53xw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wFpbesVg/nz+VUPlEsYVZunVsPHTHm38szTsgXmNXsM=; b=Y6IhWhV4vbAkwE0Rt6X3WF79fyRbDy4IlSGQT4huzx/SK1OHDFptI5YuQKy6kyCjxYdDq5kDhi0QWQh7KfbzpdGvLm8ddOLWX9zFCOdw6PzRcr2DG7PoPd43NwMhZvqnw4cnqpZAfEwb7kbSM+4xLEwUu8dDMnGRRUYkhk6rouI= Received: from CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) by PH0PR10MB4535.namprd10.prod.outlook.com (2603:10b6:510:31::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.14; Fri, 11 Nov 2022 22:06:25 +0000 Received: from CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::c544:e51b:19a0:35b2]) by CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::c544:e51b:19a0:35b2%7]) with mapi id 15.20.5813.013; Fri, 11 Nov 2022 22:06:25 +0000 From: Stephen Brennan To: Jan Kara Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Al Viro , Stephen Brennan , Amir Goldstein Subject: [PATCH v4 4/5] fsnotify: allow sleepable child flag update Date: Fri, 11 Nov 2022 14:06:13 -0800 Message-Id: <20221111220614.991928-5-stephen.s.brennan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221111220614.991928-1-stephen.s.brennan@oracle.com> References: <20221028001016.332663-1-stephen.s.brennan@oracle.com> <20221111220614.991928-1-stephen.s.brennan@oracle.com> X-ClientProxiedBy: SN7PR04CA0112.namprd04.prod.outlook.com (2603:10b6:806:122::27) To CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4166:EE_|PH0PR10MB4535:EE_ X-MS-Office365-Filtering-Correlation-Id: 3825e25f-5399-4873-5e03-08dac430f989 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1TMMcGcBWb/3ISOWaUqoHEjv4qQ4WhU9iIzyM+VJc84dDnkIbNXh+4tE5I2jwOhJ2OiAmFbTiOyn0vtqurGeNWhTgkYwEPNry0t24zDUzT3muwJv5p53xx3RE0SVYvvdp5QXisUaSkPghkCjxsvQn+sCYur4f13GFdk6Gil48lFo28i+XCi/dv+h/8dMMrsKt5rVmCHpXYAmdMK/dJ0lhmxziKCCtNr5h+9OG6Mb/4fYDnbwicgGBSozmW1gy49cKl/71/4oayhyA4bqkDvBhXk6DduicRKbPqB5GrTAYVqopgCvXv6lYddLjzfLaXXULoxenmHThUaSwgCCEfGRhIH+2Chyq3xTM8LRZ0xKf+3+XE21KGNdauvDzeVchikZtCTFWMFo0CRsfSILNrToUhn8rpLLXPEUIlqL2u/+8z/8H3ieQDTU1zBN3C7XuP92juGZTGlsKmjU2Xhwy144zsM2Qh4krATmWhvyj7tbiRdziPe8qFZAFPM7fI3FgO0poDmPjg4XpRo116irww/OORDA8T5UmqtUe0u5PA74bwwvWPv8qqu4bWY63aU1P1/EbVDJat50ezz18VnxuEZ+hg5uPVGFpgBqTQArbTyaQNQWJaSCXePIBinIYCufNUkwIH0vwm8Cbp8lKE+mBW+7GGTz/WRPflgVvVN0TFpVRpkZzw8nEY/xzTv5X/xWTicZx+0foLZ1JTDYMVG642GiJA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR10MB4166.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(136003)(39860400002)(346002)(396003)(376002)(366004)(451199015)(86362001)(83380400001)(103116003)(38100700002)(66946007)(2906002)(15650500001)(8936002)(5660300002)(4326008)(8676002)(66476007)(41300700001)(186003)(6506007)(26005)(1076003)(66556008)(6512007)(6666004)(54906003)(2616005)(6916009)(316002)(6486002)(478600001)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kWLlrnwiUJLxpQDIrVy3XIAn+/7A6Iax/+lCF17mfd+p9pJI96eYyme3NChba9Wed/fyF0sylJHQDPxbHPy2CpA5fPm4Tm8eHad1ZCuB9S838x8y07pbcU42PRxjzl/P4lNWl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+M3xGD73miay++TyOLk1UEpjJL3YN08u3ks20PXYq1Hk7rOBy1fO6MigiQtojaRup0ciQ055JTplzXZ2XTczaKsHCQ/fH5JzUFgk7/UuSWQg1Kjdz+T4AGuonzClX+vIkfzofyue4GrhtyDjngylkD/z9UGJr4UibQTnMeqmxLgGmlSXDrt5J/VMdbcTYf9P72P2rnPFyKxyp1jC9sFJzS5/mMaXoulhV7x3bFoE3QXBVIiv6uHy0OzBbG9vvjfYgAdWBUHXc1DYaI3/ZNv0ZUOKFFISEUReOdIAWiYsEofIU8HmX1WEt2wQ7bmrZJomNGnR/iFh9FSMVy5LCoxFw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3825e25f-5399-4873-5e03-08dac430f989 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4166.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Nov 2022 22:06:25.1912 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: qVJXA8GYCDGzp61qLhOXARA8bg23L3qTtnMlEXIqE0zrcy9xkKpe5aXKUl0xBxHRYWDY7mJt1UoU3R4Vl5m6/NYurnFPci8lQe4/dNo5sOk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB4535 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-11_11,2022-11-11_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 spamscore=0 malwarescore=0 adultscore=0 mlxscore=0 mlxlogscore=999 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2211110149 X-Proofpoint-GUID: F6UNK_18169HMcm9l9SOvn3s6v2wHvvi X-Proofpoint-ORIG-GUID: F6UNK_18169HMcm9l9SOvn3s6v2wHvvi Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org With very large d_subdirs lists, iteration can take a long time. Since iteration needs to hold alias->d_lock, this can trigger soft lockups. It would be best to make this iteration sleepable. We can drop alias->d_lock and sleep, by taking a reference to the current child. However, we need to be careful, since it's possible for the child's list pointers to be modified once we drop alias->d_lock. The following are the only cases where the list pointers are modified: 1. dentry_unlist() in fs/dcache.c This is a helper of dentry_kill(). This function is quite careful to check the reference count of the dentry once it has taken the requisite locks, and bail out if a new reference was taken. So we can be safe that, assuming we found the dentry and took a reference before dropping alias->d_lock, any concurrent dentry_kill() should bail out and leave our list pointers untouched. 2. __d_move() in fs/dcache.c If the child was moved to a new parent, then we can detect this by testing d_parent and retrying the iteration. 3. Initialization code in d_alloc() family We are safe from this code, since we cannot encounter a dentry until it has been initialized. 4. Cursor code in fs/libfs.c for dcache_readdir() Dentries with DCACHE_DENTRY_CURSOR should be skipped before sleeping, since we could awaken to find they have skipped over a portion of the child list. Given these considerations, we can carefully write a loop that iterates over d_subdirs and is capable of going to sleep periodically. Signed-off-by: Stephen Brennan --- Notes: v4: - I've updated this patch so it should be safe even without the inode locked, by handling cursors and d_move() races. - Moved comments to lower indentation - I didn't keep Amir's R-b since this was a fair bit of change. v3: - removed if statements around dput() v2: - added a check for child->d_parent != alias and retry logic fs/notify/fsnotify.c | 46 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 409d479cbbc6..0ba61211456c 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -105,7 +105,7 @@ void fsnotify_sb_delete(struct super_block *sb) */ void fsnotify_update_children_dentry_flags(struct inode *inode, bool watched) { - struct dentry *alias, *child; + struct dentry *alias, *child, *last_ref = NULL; if (!S_ISDIR(inode->i_mode)) return; @@ -116,12 +116,49 @@ void fsnotify_update_children_dentry_flags(struct inode *inode, bool watched) return; /* - * run all of the children of the original inode and fix their - * d_flags to indicate parental interest (their parent is the - * original inode) + * These lists can get very long, so we may need to sleep during + * iteration. Normally this would be impossible without a cursor, + * but since we have the inode locked exclusive, we're guaranteed + * that the directory won't be modified, so whichever dentry we + * pick to sleep on won't get moved. So, start a manual iteration + * over d_subdirs which will allow us to sleep. */ spin_lock(&alias->d_lock); +retry: list_for_each_entry(child, &alias->d_subdirs, d_child) { + /* + * We need to hold a reference while we sleep. But we cannot + * sleep holding a reference to a cursor, or we risk skipping + * through the list. + * + * When we wake, dput() could free the dentry, invalidating the + * list pointers. We can't look at the list pointers until we + * re-lock the parent, and we can't dput() once we have the + * parent locked. So the solution is to hold onto our reference + * and free it the *next* time we drop alias->d_lock: either at + * the end of the function, or at the time of the next sleep. + */ + if (child->d_flags & DCACHE_DENTRY_CURSOR) + continue; + if (need_resched()) { + dget(child); + spin_unlock(&alias->d_lock); + dput(last_ref); + last_ref = child; + cond_resched(); + spin_lock(&alias->d_lock); + /* Check for races with __d_move() */ + if (child->d_parent != alias) + goto retry; + } + + /* + * This check is after the cond_resched() for a reason: it is + * safe to sleep holding a negative dentry reference. If the + * directory contained many negative dentries, and we skipped + * them checking need_resched(), we may never end up sleeping + * where necessary, and could trigger a soft lockup. + */ if (!child->d_inode) continue; @@ -133,6 +170,7 @@ void fsnotify_update_children_dentry_flags(struct inode *inode, bool watched) spin_unlock(&child->d_lock); } spin_unlock(&alias->d_lock); + dput(last_ref); dput(alias); } From patchwork Fri Nov 11 22:06:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Brennan X-Patchwork-Id: 13040873 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 8BABAC4332F for ; Fri, 11 Nov 2022 22:07:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234419AbiKKWHU (ORCPT ); Fri, 11 Nov 2022 17:07:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234555AbiKKWHO (ORCPT ); Fri, 11 Nov 2022 17:07:14 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12A4C50F34; Fri, 11 Nov 2022 14:06:48 -0800 (PST) Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2ABLomDa025514; Fri, 11 Nov 2022 22:06:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=grHxMB6fo3nJ+yilwLG4loljxrOwvfJeEPETyErjoHM=; b=TFOMD5BReLxYGHpHjipPfz7b1nQgGHwtX5VFkzrBTeQm590dygXkNFQvvVcTCgYemCtC IFCRGVfRto0OeigDChDky7X6tYvOeJuLOX/33FNWqvh0K73tHY8fKt+ttRIvUwe08Ae9 xCc/hsbtnIS1BxLJgwGJUInDxUR+2H2dMxS0pVq72AAYfbAW1262Vh8kGdUCoHUfxgi4 ylDuN0Fy8jZ5Jrd9qSkHCFY8m/8EXkZqS+T3IjUu1ata8pxOfHmdO3G8Wc2BG95xJUWt klwr24Wg2z4pJex2eIVHazYSuGbF+aOJg3IjbVOOqPXlAieVt2mGIboxgNopBewaWX2v Ww== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ksxnrr13q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Nov 2022 22:06:40 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 2ABLGI9g021613; Fri, 11 Nov 2022 22:06:29 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2171.outbound.protection.outlook.com [104.47.73.171]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3kpcq6v6ac-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Nov 2022 22:06:29 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UJFz+/Ex4x97cD5mB/iQlP3v+lvpGmvtoSsoVGWm0uq+2dnVj6qG6SBfKatDcfCbaPhVaP3c6l6Z/oqZ2YScApk+NZAvVFxRTKeIZ94efMJK7USHPRuRuHvT0RJio9clmK5m1aYBMZXH386fds1tFLNrtKMrNmJ/hKdlWCJSIM15cEVjO/bsvqpriIEX7Yjdn6zLNWd1x5XibixlnGwSBZ9bLVTjlQcSKqe2GhAM1j1L2wa3v5H8pzWM3ABTGGP8mQ07Z5hmeeVGmDuAblm/tRda0g6B9BdmAOT08LxU3aheXukv8AxR3xebw68N0qBoUNQA2dGjLDolmFe12o02pA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=grHxMB6fo3nJ+yilwLG4loljxrOwvfJeEPETyErjoHM=; b=ZUhBBq7UbJQs727/Vo0UFU5+/tHPVH7pheKInyPYjd0aEKAhm1CNS37yTDxk0gNljJEVVTjobjrpMCkYHgV5U7UKkUjDFydvtHcdCPZOo2fyeSJLEYd78lRKg4BMzG8LB+3y/hQVE07K5dpJm7p1a3KVVm0YxlQVx8J0s58ZPkRc2YN/ckGkIh7yCWjrLppEUUbTL71R6r0eDjV4iyNe0dikUlPdmCwAk06vuCya9adiRH0PUWOS7iPfz+yE0n0GHoBsZR5nnY0vVKwZS7oRoZJyzgZpCpVgrYuLwrwhCY6t2+PFaW8na1sZ+Yva1dnOrLG3TmPiThXJc98VZRhVuQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=grHxMB6fo3nJ+yilwLG4loljxrOwvfJeEPETyErjoHM=; b=KwofHK8KcWvcTCjHi8mgfpTbOOaI7vkGJIO5KkRTw2yYUFIxS3dBuFv0Futaq4jxykXQt6k/jrhoK89lGH42Gffef5E5mB4+I8EF1jl/+AwnOS7A9Rs+FyumJ+NiuDXU1fruho3UCvIPqWbHBM8FCgy40q0ipecfF1RGwjRCgDE= Received: from CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) by PH0PR10MB4535.namprd10.prod.outlook.com (2603:10b6:510:31::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.14; Fri, 11 Nov 2022 22:06:27 +0000 Received: from CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::c544:e51b:19a0:35b2]) by CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::c544:e51b:19a0:35b2%7]) with mapi id 15.20.5813.013; Fri, 11 Nov 2022 22:06:27 +0000 From: Stephen Brennan To: Jan Kara Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Al Viro , Stephen Brennan , Amir Goldstein Subject: [PATCH v4 5/5] fsnotify: require inode lock held during child flag update Date: Fri, 11 Nov 2022 14:06:14 -0800 Message-Id: <20221111220614.991928-6-stephen.s.brennan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221111220614.991928-1-stephen.s.brennan@oracle.com> References: <20221028001016.332663-1-stephen.s.brennan@oracle.com> <20221111220614.991928-1-stephen.s.brennan@oracle.com> X-ClientProxiedBy: SA9PR13CA0136.namprd13.prod.outlook.com (2603:10b6:806:27::21) To CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4166:EE_|PH0PR10MB4535:EE_ X-MS-Office365-Filtering-Correlation-Id: 544f64f8-738e-41b5-e518-08dac430fad4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nG2/8HQTsvcJMIw/trqpW3i7AddqYrSvCiWPBJSvy7ZSK2KKutd64fzO0g92GhMjJQd0Q0Pfxu8Xjt6vpvGL7Iog8gv0dEMiKuKuEIESn5v+oMCUFKtmhiCNEIrU3dkTGVhAESw82Yl9ztBzny1lOuibkpnJmLgdDaRixRfLe5/iG5fAh7H1oqR7ogsXxhzNOQxb+4NawMB5AU1vioueAweaqA9H9Ge+nluSTZLbE0/tS3GJfUixH9l5VS8YwzngNChaRW2Q9onYzNnfWaq/tuLJEep4IGnHmFuGY32DLHVqg/7SHtnbz+k22bP3tO/sKbiyGDSWTt9dSUsUQVzfrT1Ds0fHRn5s5yhblV/+oW7fqCyKq+ajzpBHaMMDosFoNiBdPTaG/JCruAf4LsvjIdeQYfnShu2bhO8QoRo2zkVP/4O0aZ5BIavlrb47S2j0s41Bj7MsCQA4aGaeqYU3En1IMhw1ObDKFq2Ng2a+RfjfaM2cDTZKXuAskLrhD4dYj2rTge3oQKHf9LQqICgAzt5Rd0NTJ0z6P+YCdg1yhsiwQf+ZYRnIj7RQL8wHLXO2RR+3BJPeMqgWURPqmsZ+GiXd8fnLyRDFr92Td/wZd7yZsMMNYWdBESW4XQP6iR13zUYd/mSDuSlUofPj0qnWJdBAhZvXrjK1nvgKcjOv7gvF3heimVfoqW/x++tXFu+sb7cVLEIKoc7Vend9HZFeAw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR10MB4166.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(136003)(39860400002)(346002)(396003)(376002)(366004)(451199015)(86362001)(83380400001)(103116003)(38100700002)(66946007)(2906002)(15650500001)(8936002)(5660300002)(4326008)(8676002)(66476007)(41300700001)(186003)(6506007)(26005)(1076003)(66556008)(6512007)(6666004)(54906003)(2616005)(6916009)(316002)(6486002)(478600001)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: oIW4ZQQEQSPatLs+VOyNFcZ7q34Ozc7tqOu9ZmRlyYe//OKlNS4F4goUc6k4ERoqrZNkimPnb01ICpzwdVMxKBRJbMH5G1we9qyugJtNuyq8vFI6Uyv9zJelvnvSCDBXjr1gVBAaETgmDlpZDVsEtaGzapAd19PmlMQ8XNpumrZZQVS3giMxzIq8JwiMdSYS84xvWgkCpfo86/zUZxf9d87vUgR+yjkf42KDDNe+a9Ba9vCZbW44FchG8omei0Fm9uCiMzh1MYkuJOvHMCKZbYuazssw/8zd/NNaTkicfgMwEWSq54B4c6lGMNNaznxCw4bo/xagQ5JDiOh5Rggjr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GRutv4EpPLrtk+Rs27yyORqLIMIrUsc+lXQzlTCwpu7GP6LIEulaHezucjYD568ASVaPRSP4kYLltWIqk2BFzo/1sk8mjLw+AHed2zNcB5XYSJFMh7o4bIYNCXsNGkHJ/f82vA8EqnBz0XlbN8zR7O8i4VfWAZ/0ZZSHuu18njV86PLYnnpFHSZsk+cw8OAm2DgQ== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: ERR566wigci8SddhBxDTim2rClU2xoyOMjdAVE3Ap4iBYsax0qNbKKLaZ+KKoFwGCtJDDZoXXOMt97ol4atXxESdVdGyTs+QonZ9lnC5N74ZnFTWh3XdBh6yObN6TCCI5X7U/Y3+34hbAsR+aC2BxeMywZzNHUVYmm0MAJLxDEdMQa/7mh754AocSaLLeSbg9dXElVAyAQ3ptrKlz6kGKb4j8l+P9uyvYvuuDGWHG1UC4S6lq1O0P2qV98MqB0OTOYOV7IWik4RoonW1YAQgmc3W3AC2Q/UrH24604OSJNtjSIlH3U1q2hmb36gZgDYKRcI6OKyacL7pi0O89+Y5u1yezlG5Hks5PG2AwbazBBLt9HYqHnRurscYvOS37bD7rfUFm2za4xjsr6L0fswB7oJKkjk5K8jFTIjzapUfrG7logBFHxu45DykQGGmBIdmxDJuBFA15ELXgrnLfQ8xbD3V4i+VQz4VP2jJNiQcVURpkqQD7cIfNDe9qd3suNt/h+b9/AFzACO16mZdF7s898c4Nk+9HDXe+87DVltqtZulxqsiDsyEuemP+/DvqRPA0QEYt/SFEhEJqEdMIYGSIk2H8hzFYg6SyGLbs++EQ2jv85uVfupnwKwO8MzrzuhkeNgWKghPHnIxBYaLdf8Jpa1FmXfzZc7h9W/2t2qNI9xWhxSSGdxSyaK6ufYFgq50847Jjgzum3tTxAqttvHhpDphXenGfHwPo3d/A5HhLMmr56Nkdfy6xvoMAKRm96EJlKiPhSdhu7q7jbtA/HHRuNUNLrHKyUAs5+LKKjiF8/I0P8eXhmDwZaNqzz2yZ9TksOnoPDlw5Ss2cnIyMPBxtfnDXjhN+hRwFBlf/IMrDV6R6tDEECFpRS4+xss7dtD66ogvq0o1MHfL/kXiSvNNbQ== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 544f64f8-738e-41b5-e518-08dac430fad4 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4166.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Nov 2022 22:06:27.5215 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: /gaz0czNXxvtwQfP58V1BKZ3Syo5lNGrGBNSr9xo/TEDYf8lAZ3wb12bsA07HGYwFWY5galK9zC4r2DKnNoNgd589FPR6sqimncAUmqfn4U= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB4535 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-11_11,2022-11-11_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 spamscore=0 adultscore=0 malwarescore=0 mlxlogscore=999 mlxscore=0 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2211110149 X-Proofpoint-GUID: iqMfUkqRJA6nKGlwYvPQ58NwXUvJFJnh X-Proofpoint-ORIG-GUID: iqMfUkqRJA6nKGlwYvPQ58NwXUvJFJnh Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org With the prior changes to fsnotify, it is now possible for fsnotify_recalc_mask() to return before all children flags have been set. Imagine that two CPUs attempt to add a mark to an inode which would require watching the children of that directory: CPU 1: CPU 2: fsnotify_recalc_mask() { spin_lock(); update_children = ... __fsnotify_recalc_mask(); update_children = ... spin_unlock(); // update_children is true! fsnotify_conn_set_children_dentry_flags() { // updating flags ... cond_resched(); fsnotify_recalc_mask() { spin_lock(); update_children = ... __fsnotify_recalc_mask(); update_children = ... spin_unlock(); // update_children is false } // returns to userspace, but // not all children are marked // continue updating flags } } To prevent this situation, hold the directory inode lock. This ensures that any concurrent update to the mask will block until the update is complete, so that we can guarantee that child flags are set prior to returning. Since the directory inode lock is now held during iteration over d_subdirs, we are guaranteed that __d_move() cannot remove the dentry we hold, so we no longer need check whether we should retry iteration. We also are guaranteed that no cursors are moving through the list, since simple_readdir() holds the inode read lock. Simplify the iteration by removing this logic. Signed-off-by: Stephen Brennan --- fs/notify/fsnotify.c | 25 +++++++++---------------- fs/notify/mark.c | 8 ++++++++ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 0ba61211456c..b5778775b88d 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -102,6 +102,8 @@ void fsnotify_sb_delete(struct super_block *sb) * on a child we run all of our children and set a dentry flag saying that the * parent cares. Thus when an event happens on a child it can quickly tell * if there is a need to find a parent and send the event to the parent. + * + * Context: inode locked exclusive */ void fsnotify_update_children_dentry_flags(struct inode *inode, bool watched) { @@ -124,22 +126,16 @@ void fsnotify_update_children_dentry_flags(struct inode *inode, bool watched) * over d_subdirs which will allow us to sleep. */ spin_lock(&alias->d_lock); -retry: list_for_each_entry(child, &alias->d_subdirs, d_child) { /* - * We need to hold a reference while we sleep. But we cannot - * sleep holding a reference to a cursor, or we risk skipping - * through the list. - * - * When we wake, dput() could free the dentry, invalidating the - * list pointers. We can't look at the list pointers until we - * re-lock the parent, and we can't dput() once we have the - * parent locked. So the solution is to hold onto our reference - * and free it the *next* time we drop alias->d_lock: either at - * the end of the function, or at the time of the next sleep. + * We need to hold a reference while we sleep. When we wake, + * dput() could free the dentry, invalidating the list pointers. + * We can't look at the list pointers until we re-lock the + * parent, and we can't dput() once we have the parent locked. + * So the solution is to hold onto our reference and free it the + * *next* time we drop alias->d_lock: either at the end of the + * function, or at the time of the next sleep. */ - if (child->d_flags & DCACHE_DENTRY_CURSOR) - continue; if (need_resched()) { dget(child); spin_unlock(&alias->d_lock); @@ -147,9 +143,6 @@ void fsnotify_update_children_dentry_flags(struct inode *inode, bool watched) last_ref = child; cond_resched(); spin_lock(&alias->d_lock); - /* Check for races with __d_move() */ - if (child->d_parent != alias) - goto retry; } /* diff --git a/fs/notify/mark.c b/fs/notify/mark.c index 6797a2952f87..f39cd88ad778 100644 --- a/fs/notify/mark.c +++ b/fs/notify/mark.c @@ -203,10 +203,15 @@ static void fsnotify_conn_set_children_dentry_flags( void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) { bool update_children; + struct inode *inode = NULL; if (!conn) return; + if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) { + inode = fsnotify_conn_inode(conn); + inode_lock(inode); + } spin_lock(&conn->lock); update_children = !fsnotify_conn_watches_children(conn); __fsnotify_recalc_mask(conn); @@ -219,6 +224,9 @@ void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) */ if (update_children) fsnotify_conn_set_children_dentry_flags(conn); + + if (inode) + inode_unlock(inode); } /* Free all connectors queued for freeing once SRCU period ends */