From patchwork Fri Oct 21 01:03:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Brennan X-Patchwork-Id: 13014173 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 028D5C433FE for ; Fri, 21 Oct 2022 01:03:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229932AbiJUBDZ (ORCPT ); Thu, 20 Oct 2022 21:03:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229454AbiJUBDX (ORCPT ); Thu, 20 Oct 2022 21:03:23 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78487229E44; Thu, 20 Oct 2022 18:03:22 -0700 (PDT) Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29L0EBqI012288; Fri, 21 Oct 2022 01:03:18 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=tu7DynKVPxM0ueG7tyGzCY+i0KOKgPMaDN3fSXT56HQ=; b=HN65lLupuMRRcHz7l9wxBQMysVrE8bdgM0TylKtyVck+/XU1nn0uLD84TzzaIPoKrfpH fvapwLbpgJ4s8oKJtYWx1+F6ASH02xwQDkfOQZ4mByVsbt0ZJhXPbZyUcBOgWC9S6wAw Jyyc+oblhbvGQzyKlQe8F8gSTkfL2Mjt7nGGGAk9AfYOqNNTvAP31jn9QKICLQU9q4mU ah5MqCTWKDVXbdApbXN8ZQaz0jIOwDCiwjrYIX7Kv/ci4XUu+guikHVKtE+KX0z6gXVz nWzPNEJWfprvJagnHlh0aWHNDqm8SXsiWr1wqSywTPMrc+aBCo0t0/Nfe5vRmjUBizKn 6A== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3k9awwaaxf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 21 Oct 2022 01:03:18 +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 29L0CrJi014702; Fri, 21 Oct 2022 01:03:17 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam04lp2042.outbound.protection.outlook.com [104.47.74.42]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3k8hu99feg-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 21 Oct 2022 01:03:17 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C5k8zWEh4H2TTb4laN5yF8dgCRPSTqHRRWpu4lr6my39W3bL0OgPz3yzHyuyBmirj3FlnajAYx0lTDyPzxWzmKTDT6QF269DiA+NoLVbc2Q5/cLawDi60bEouM9AlZTW1IglSS0Qn5Et92xSq3L7cbEouoG6wcljD9C4Lca5exPTqhixlzDSxs7aVmmljcfhOtTabxrP9nLle54pPl3ou7jWdkCklh+lYac83QnF4llxoDD4LyTA/kZlPC3e0/r1VOX2UaQoaoCPzKH9ME1Zj3NYWHjzRG/QUTROQQCG3jU2H+INRwLBRrpCGBZIbSHtDut5V4Fw6kyqIBENQ2Ip7Q== 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=tu7DynKVPxM0ueG7tyGzCY+i0KOKgPMaDN3fSXT56HQ=; b=Haz196K2wjJgOyQfgIj2KfzX6DI7++VynJ5pknDec5upuY4YZYb5tkCno9pfQShoB9yTifFpmXsNfKA06/j8tIEx91GhV6Ugpae2ObqGmlbcyJDOaktaeoYrAu41pWS/9kwxNeibqJuMc8+KPeQtmVfMxJm3tymLT6f7obYW4Eub14boMijf1Zg0WTaGnW7sXRItOLvl5zML3PEygQBsHX6SjL20e5NnkfuBFbL6DIotmI/1q8+l7oyO8aT16r6niYYKnL0eRnxr10jHzOxJd5a+EZ3wuE7hN5Nu+zcy4+uzxXHDjx5zxhR9K1oVDhkWH5Qg5tXe/hIjy6ZIgPXNIA== 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=tu7DynKVPxM0ueG7tyGzCY+i0KOKgPMaDN3fSXT56HQ=; b=dpgIjqFzlQFjnPJFssV4SM1hYavkxGE03/xj/x0O1i+4aMTnsYakt8aNtSuwwPE/5JwZp0aqh+LqXl5dmXDmle+lH8IU4ZeptN0Ku4BsOkilO1SmD9CJ8hDWnHFW53nf83+kwWu6FiYmlghVIUHUGcL2lT34FXPKOApsXPbNMa8= Received: from CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) by SJ0PR10MB4446.namprd10.prod.outlook.com (2603:10b6:a03:2d9::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5723.35; Fri, 21 Oct 2022 01:03:15 +0000 Received: from CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::5374:25b4:8dea:31dc]) by CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::5374:25b4:8dea:31dc%7]) with mapi id 15.20.5723.033; Fri, 21 Oct 2022 01:03:15 +0000 From: Stephen Brennan To: Jan Kara , Alexander Viro Cc: linux-fsdevel@vger.kernel.org, Amir Goldstein , linux-kernel@vger.kernel.org, Stephen Brennan Subject: [PATCH v2 1/3] fsnotify: Use d_find_any_alias to get dentry associated with inode Date: Thu, 20 Oct 2022 18:03:08 -0700 Message-Id: <20221021010310.29521-2-stephen.s.brennan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221021010310.29521-1-stephen.s.brennan@oracle.com> References: <20221018041233.376977-1-stephen.s.brennan@oracle.com> <20221021010310.29521-1-stephen.s.brennan@oracle.com> X-ClientProxiedBy: BYAPR05CA0034.namprd05.prod.outlook.com (2603:10b6:a03:c0::47) To CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4166:EE_|SJ0PR10MB4446:EE_ X-MS-Office365-Filtering-Correlation-Id: 8c4a2573-6465-4d27-f209-08dab30008ad X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mWmK0DAkLaJ3Rb+cX6+WCHc0CQHM6u5eYhojKk1JHExrk9mYDqf4J4/A70lZBHcmkKzp0YxZtxb/ANNEMDtyHOENUrCayORQvMYZfll2yZcC2pPatCKf0n2/QFXnWsa8vhlNphQw/qxS6k9RiMs5pOqA2tLfqrgl0X1AkNEijO3xIhIymEHCXKgJISFQigr9FWtGNUBDFWuhA/KYLTa5CiUvq7CaLOIYgXKzLfzbasaNQ4mwVST49eNBJwOnFSLNJRtkn0mmfHNOzI6G5Su8BuiLwdi3xKzgPx7JcYUN9ZF88xphl/mD7QupaKbjobC5/qaei5JwQpZPsDtbm2tkEYyOB/KtQnDRNc0Mr24jqE+ZvUDBwRBNcInTHemNfUikJphXFyLn5lDbR1DF6vRI2pLwxCQ00AjxFHkWDI+kiI3/Bl0X1v6vBczQ+3+eNZ2MdoYkI4gyCW6r40Onl9XGDwIW7d39wpVu0MWvtVbr8VDOAtZcHgxONz5IBTGUmxeIKdOgnhDDLSNLKjUG3VsEsHOsujA95n5FMGRthqu+q1fO1IbQc2pvR8oOWzaaPDTbfP6OuhrHpDGPcfUUbR8PVA842vJqnp2OmUZVKNyJiKl/q6B7TEfkpqf8kGkn4NxLk1RABN1Y/HR7iLnFROqq2WiuAQ5Es/lpCpKJWE+f7xdQQOGPIEZdVY7XiIJ6p/Cr2jNOoUzNx7c3Hhmq25Rudg== 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)(39860400002)(136003)(376002)(346002)(396003)(366004)(451199015)(6506007)(36756003)(86362001)(38100700002)(316002)(2906002)(83380400001)(103116003)(1076003)(110136005)(478600001)(107886003)(6666004)(26005)(186003)(6512007)(8936002)(2616005)(66556008)(4326008)(66476007)(66946007)(5660300002)(8676002)(41300700001)(54906003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kjw9E91OUgpn1LgT4+nSrZZ+OfizDzSi+Yb8s+h9FLzUIGzIibP3im78S4Bt+iatJGP1uBPjYhKHCYnZigPyovyIQE0HMZYbsR6BklF9nhCgkikRDCjKzzyEmGr+6y9S3dCVydSv+GS66ONFlmrN18tIvQCQ1Ui+VdATQj4MFEi+qcbW+dHUerk0JuReJCmVyNckKREusHMGcrowVcCrNlwM/Dhway4zx7b+cNAeHG9Ncjs+u5kXrtiN6248tNApjh+weacWcG1miV3XKfaQUpoI0S6pSYpz4koZePkwpfopgX1kXFXBLFuDi3mQdeYLxeM4TsA0MkSmQxXXKMD2+iEVmwC8Lp6w5ruQ8zBiVyssGt97wjRCOA/GdoHgoleyg4c0lQr39Fwg3wThJgOn/H1FO3dQ0ZIUGYHrTOkq1pWejv8Rux28FeaF09e2UpWgXANNvP4FwV4EAhMswtQm9j9xM6c3B+MSmZo31BcJwoEBZ5XaMCESWqByjsI/4AdWVAYm/EADgAKEyILiLBNDlrcGCPuOOhc8ZoIyEP7DSMIUfapLFvAca48IkmZimtLhOo+G+ydXdwFYxo4L1mDEeac3OmwwzEdDyKak2GyvaZIgK6DQsTzkAx4pjnRW1kVagvUUWh6ap8Z2JuLuDyA7ywnZNwPFIDHmuVyx6K8WLAbzCifP9Gi4T+LlhRxfI4AKJodezUpQd9evvAcavojexWroc4ixHXKXmfGpdgt2SkeZwG9M9kY/e99dcniWeP9MUcDWdETxyVaQ6UHgI9GlLirMy8uUyYCgjYOW1jrRWSZtKppDfs+MG+D7CsAB2F1oTgUKvdN/tpx3IT79htxeS0KD94/fGmdwSoDgtaDtj2tvxAfNvGSwrBq6LIEw8gSMVLEUl3LljYZmvwnOdoAoh9zvG+lDtMx21c21Hz5w9h4y0TEbDXWRvkhLS3dUfAuPzPmcIUPbuh4VM2aruk4dH61om5YjFNQSvFm+hQjOWWGUkqnnh8pfwUWy3yJ/EGH81P/l3JcP+tsXFBqqyQxbUTvpzMCZusrBGXw/8AAU+7p6mtt96SC2HiUt2x1zKrj4MxjiSBhk9EiqCvP5keglfYiF+LaKVCJzozRkXBJ0qvreEfTeTNlfdaeswZmhv91vJ23S/9rXzXRwVj3BEDkoGSDXehzocNfreMsHls68dlzQviz+0ZygW0ylPEKbGJ3IWBt+LVCmrKZvBhlIbGlo/S6aS5Qpc1ZFiAgsF/oy3FmwvoOYjQC54QThGlwA26EnrzWsAF1QNKYWQEC2KzzZLiD5JOgMd7lIjnh0rqraP8xOapkeOvMdYfb927iG97nxSC9bHlC+BWOB+dkR3EuG6Z33k4m0iY+g0rS+Ly4kv4qGP2hQEuw0lZY2/CowYhbS3TKHBapHFTEaGK2/v9xWIkEr+v3R+X8Q99vNkT2bNT4bPtxi2bi0KjgCBMijQTIowyZaACnzzw0RD2V5doG+FE1kAkB3xQbZQMOck8Rhbj/v1CRfUaqsf2hH+2PWiBKjxtCHjlcTn5G7ZrHepEu28HK61HFYqjV63kUh5BepBF+JTiRZ0/0VQ9U62NjEVss1NFmLPeH6sLhQiBTdtPTUDA== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8c4a2573-6465-4d27-f209-08dab30008ad X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4166.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Oct 2022 01:03:15.5054 (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: aY9T35If4tut+pIrY2DFxTpejahUirE0hPHUBIIOq3smAnuDgLBg4ztCSA2LfzwV65tH5KVtKhIMTApuu5yE7sd3pkjtL431XSRfuzypkoE= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4446 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-20_13,2022-10-20_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxlogscore=999 malwarescore=0 spamscore=0 suspectscore=0 phishscore=0 adultscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210210004 X-Proofpoint-GUID: He38wPzaC4Gr-ks9JgJV1p0TrYcT7fka X-Proofpoint-ORIG-GUID: He38wPzaC4Gr-ks9JgJV1p0TrYcT7fka 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 --- fs/notify/fsnotify.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 7974e91ffe13..6c338322f0c3 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_child_dentry_flags(struct inode *inode) { - struct dentry *alias; + struct dentry *alias, *child; int watched; if (!S_ISDIR(inode->i_mode)) @@ -114,30 +114,25 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode) /* 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 */ - 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); - 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); } /* Are inode/sb/mount interested in parent and name info with this event? */ From patchwork Fri Oct 21 01:03:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Brennan X-Patchwork-Id: 13014175 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 EAFBEC4332F for ; Fri, 21 Oct 2022 01:03:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229452AbiJUBD2 (ORCPT ); Thu, 20 Oct 2022 21:03:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229975AbiJUBD0 (ORCPT ); Thu, 20 Oct 2022 21:03:26 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0526623081F; Thu, 20 Oct 2022 18:03:25 -0700 (PDT) Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29L0FYRC006828; Fri, 21 Oct 2022 01:03:21 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=0WOghY6ZIKv7cRpCv/K161JkS/g/KW77oLuKKhjFVco=; b=kCj17FdMIR5X9rviZ17uymsC5B2TaDdXEZEDovFlEth8MXe1jhjv8hcWZ8QPRbYJC2cz 672t2YqNQ/qHBamoj9BglMj7V09bZCkzXUrKy6PNK2WUrTMVc0HWw1YRV7nf26Ye8Nob KnfF7XlL/+pphsR8GEHE/Oes5xmkeGDlp13r5j8cUuLgSyp1EW8M4C6Pn5nLoMBeGAfW Jn/HwqnuHIICBXfFt8atWGFvhvDKMfxpV8T9gVw/voBQ+JPRMMROa2awM3WUGZeTdMiX vbmU3aVqEwmoZgpFPcOdh92E/s829g5KLikbYIBX7ZIwVUymR8D5FArnwoX84SdCU233 Mw== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3k99ntk4n4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 21 Oct 2022 01:03:21 +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 29KKxtHV017110; Fri, 21 Oct 2022 01:03:19 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam04lp2048.outbound.protection.outlook.com [104.47.74.48]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3k8huacft0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 21 Oct 2022 01:03:19 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AsZTZHCP0xrNZvXeHwKOJZyI5GrghwUyjhHtmI0HFBSazY2sgm9JNcWsypauhBb/9I0vlCXWprUxR3KB6LZJJSH02bvsMYtoIQVNmEHdR8ERAdvaIRKwqg4d5G6eyfihnb5+DkHIw/FGkLXMTHziCm7bazo7/f6DOrnOn7McpuwC8hb+7luu/Vb9lAswgjnf2m6yDftaR+f9K1nbOI+nB55eEx7dKyrDWdWBJAzKBpTf2tS7sJBsZbouT5QhVjZx77OfFVHt3Ss2tG+7vSU1XAV21aOF5/WpRzPXIigiY4jU5wdLR+TxXUVzqQqRpuc77E1t5vQWQvR6cGLj53b4yw== 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=0WOghY6ZIKv7cRpCv/K161JkS/g/KW77oLuKKhjFVco=; b=MGaP/0IoCu27uNvvyk8athyr70/cN4UbPLtx5gnchFWNpFYshryApUi552dxdcchk2AtNfumBGcruRRaBtnb3xkw2Bq+gM8HKa0uYHN5kiWSXsQPJhS6KGpPRiHF7r/WE+RCFDUDizInRBdAHVN7QywvC03k3H36UM+wtNMyIWidAxEU4Grfesxpqb79QGI35Aksr9ggU1JMMIVD6fe0YYikQXF4ZGygIfbZxpCIhp6hEh9N7yl/sCHVHvKUNorXqQZMBs2e/cNYYQv/rWjajkXvbH+x2M6OXekVrWiWWwO+nSOJU5WnAYLcp0KDUsJSm1YiLv06mH5N6yomyTpiQQ== 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=0WOghY6ZIKv7cRpCv/K161JkS/g/KW77oLuKKhjFVco=; b=siBR6jTbWtyI4go+4fyUo16hlcj+17ZT2Otlg3PJ23Iz7byMl7Soiz8VZU+BR5RlJEKAt3+3x5TCSvKocp0WlrQB9F/OIGlvULT9hBLzyP5cDYbaqx/M1/QEhc04hMfLdVif2YTPACrcoN1wEUKWui0nDnVUjzKYCnTiBzyXuxU= Received: from CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) by SJ0PR10MB4446.namprd10.prod.outlook.com (2603:10b6:a03:2d9::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5723.35; Fri, 21 Oct 2022 01:03:17 +0000 Received: from CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::5374:25b4:8dea:31dc]) by CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::5374:25b4:8dea:31dc%7]) with mapi id 15.20.5723.033; Fri, 21 Oct 2022 01:03:17 +0000 From: Stephen Brennan To: Jan Kara , Alexander Viro Cc: linux-fsdevel@vger.kernel.org, Amir Goldstein , linux-kernel@vger.kernel.org, Stephen Brennan Subject: [PATCH v2 2/3] fsnotify: Protect i_fsnotify_mask and child flags with inode rwsem Date: Thu, 20 Oct 2022 18:03:09 -0700 Message-Id: <20221021010310.29521-3-stephen.s.brennan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221021010310.29521-1-stephen.s.brennan@oracle.com> References: <20221018041233.376977-1-stephen.s.brennan@oracle.com> <20221021010310.29521-1-stephen.s.brennan@oracle.com> X-ClientProxiedBy: BYAPR05CA0024.namprd05.prod.outlook.com (2603:10b6:a03:c0::37) To CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4166:EE_|SJ0PR10MB4446:EE_ X-MS-Office365-Filtering-Correlation-Id: 6d0501ac-5b35-4aa3-e435-08dab30009dc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fmX0wShInbZFFaGo5vrWpNpuBPrhGX0A7ptPwlPYCqRn80AA4IvO5YlzAKeTMbbcT0agquW4D30IHwjYU8Gg7+PU33QXpyp6z5Z/KcZeJqxAdj9Q1N+0FpFcTsDw37CntXuF8W0TAw7PeBE9AZmfEqhaFJ2eV1P7pUi8PRpXHmLszgQzvJ+e6mvcTX+DBO5c91zXSpReoU8UF7jxmubN84OzXFb7NNNoUgSuG/zsUxCsZ6Ib0UfdyhmtcrwTyAJngw/6OJz/ytDo5bhoRbibHAzz8x0jET7YLNEiLAiAtqiRA5NYDHkQzbyaKRg4uB/QQtfiJko3+ScLOPbxO+IOnWQPCJzYhhbIl2t+smkXtXrySHWrGjxbn2Rsq6FlQbsviAYz/VdRO6sUJ7KgfZo9aw5fC3aEgGNCCI65G8C1bMXEQIugTeX1Pgs2M6PIsMclQ5dWk1n0JK5sHlkWbN32BQjFP/QQuH4EMDAPJfUAm7dOBTQA+XqEqOJ70wJDZotpx8d5FfHLMGMjMPEYpZ6oXcWcQlXFUF6uT5DXUadg+CDDqpFO8PAbtz4oRq9jufnKaqdK/pyMOdGLUWetgUV9iXwOnbC/1PkoUOJ513jrD3E8OxIUpEzb3Jupe7Jo93U4Bc5fM7U7ff9GqdFVUT9aM0lwfvlkLynZNxA/FkCQSwHhUealJottsZZDvYWOT3bkDVlsJZoEHPoKY2Qa4h1SvQ== 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)(39860400002)(136003)(376002)(346002)(396003)(366004)(451199015)(6506007)(36756003)(86362001)(38100700002)(316002)(2906002)(83380400001)(103116003)(1076003)(110136005)(478600001)(107886003)(6666004)(26005)(186003)(6512007)(30864003)(8936002)(2616005)(66556008)(4326008)(66476007)(66946007)(5660300002)(8676002)(41300700001)(54906003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: YWULr8ULvyPQYvVxYl1laLmMLqP5G7viEPMizsSSHW048PEJh7OlKbjQ0bGNo98QDNw5Q5f64k5AUN0P9Vpl5x2F7UNxAs76yLleVFJfxdjRGmOAwjjDxXKF9muq8bu9w8tU8KxOv+oMBO+UtIMq9IkV15NGKrXGkHODkqq/KAH00BBmJOLVX6kQlxB11IsjEQmZ5bc9qY5AF/bdhlBGtiQWPPJs8h2R/wsR8V/H3rh2m3WdcUdX18GiUBmOl6q69T+srcUCfyDtcrg9mAysCmu+dfs78B7j+lOz3VW9Cxtt4+K/gf8Hx2NkCK9Q9ad/V5ekYwjzJuY6QWIH2BLzljVyn1wzElvDdDzPL8cqUzpgJq4D5WHVnynSMYm8n1Lk4vFIDp7aHGx1OvSoMumvgwGcXKncmNPLPOs5Fa4LYxJ4GHR1eL3W7ob1jKdf0aZPhB6vFcLruq9nEnaPHnKhzGXf65IqzwYgmbSgr8L7wQCvdG1yUnsh2PYAYs1xgzW673fCssMC8Xf/hnhD7sTRK/c7o2Wnu91BV/hMVfp1MeffDgagR1MPhkG1iSI2Lu7IKen1oK7YoStpYbcnJs/41kFsjoJUNr4Ajp3JILkFByDc+A3dlyUL1LbXFSe9ip4S5B277HgtFTHR4U5IYKSpPZZ5gyKzzAFbPrWvHJhtkYVXkFYt/lwCb6PMb8i8Tbln5Xifz1M15QYcUT0ttBLoLlTxt+JEuEN2dmQwrMvbAvjCrcDrITI5q/SW8Oy+LzFJOMqGQZ4xhst736nAEbjPrIHCYtfYz7WKNpYDRg+CSQiQvywWQuiNaDzgAD1NY39c3lM43WbPiivl34v1cSNHf11dyEa0DwCpXejbPX5OzeCD+54gG5Wt48+MdpNiA12rhezV5j3lkWNDGtVMs/CpeB7nzIbtD21YExT8+uGBLyecGeL8rLd0tewF1BZ8TEkrB5QMmjSxfdxd/zzML0zCd2SaRaBrttJpX1FrFiuv9M765+rHhuGlKLTC48ZM56s+r7dbq0VIn0MNqptokLwHpwi8HhbNImhAAPXyS3U6CX6Ad4alH0Va+0tcEYMisMPMgadUJM5Mri+HMiw5Dxw96ZXOWiHYeE4DrcoUd1Qi86WecRS8VUJCfZ4Af4slNXT3gdnCo791GypQuBwy8Pu+EcLeMq43mSahxccvqNniye3zGUIbOQngwaKjWD8OYQb8vcA2YgzvriW1wfprvjwqK8WpeNSBJF1VaDJXQ5k7Sbhq0Rxs1grdR2h1AzTYK88EJqXtbTA8xzyVoH4POw4WpDlQ7hV13d+gWHm6SNdTWXQygVhjP/Y4fwjKzvR23XBp+wNnbrzHhkjnth2xuZV0tL0ESe0aGjkk+i1GBAd3lieTsU7M0YRV5vZmeNr+CxSnKK2ialNojJMcSYStULb/67IdspegJIC7AdvBpV/Qr96btpk3mH7/ECD4cSaIb2/FRK/6se1DGutpEANxDHNQ7YOm3mBdEXzpMJcbKjHj9pwx7pL2+sxqaUiFnkYAaLksl2jpNeNvPtftUoMEykq3+DYAJiPAXqTJunlzxsoyqZf6oyfZWYJoLHylXcrdX03glog94BJx0h1hMyqfRSkmpQ== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6d0501ac-5b35-4aa3-e435-08dab30009dc X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4166.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Oct 2022 01:03:17.5064 (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: KhnZeaAPuz3z+tAugMulBRKyvmTkrUxXLp3vvZ9gXMb/6obv7TMwYIUfVhXVjnUeuFImKHdlICPI59CgQYy41Yu1NR9jaWjF8o1UdNyI7I0= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4446 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-20_13,2022-10-20_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 adultscore=0 mlxscore=0 spamscore=0 bulkscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210210004 X-Proofpoint-ORIG-GUID: Soumt0Sm6JGvDplm3v264gPdykMAnNyu X-Proofpoint-GUID: Soumt0Sm6JGvDplm3v264gPdykMAnNyu Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org When an inode is interested in events on its children, it must set DCACHE_FSNOTIFY_PARENT_WATCHED flag on all its children. Currently, when the fsnotify connector is removed and i_fsnotify_mask becomes zero, we lazily allow __fsnotify_parent() to do this the next time we see an event on a child. However, if the list of children is very long (e.g., in the millions), and lots of activity is occurring on the directory, then it's possible for many CPUs to end up blocked on the inode spinlock in __fsnotify_update_child_flags(). Each CPU will then redundantly iterate over the very long list of children. This situation can cause soft lockups. To avoid this, stop lazily updating child flags in __fsnotify_parent(). Instead, update flags when we disconnect a mark connector. Remember the state of the children flags in the fsnotify_mark_connector flags. Provide mutual exclusion by holding i_rwsem exclusive while we update children, and use the cached state to avoid updating flags unnecessarily. Signed-off-by: Stephen Brennan --- fs/notify/fsnotify.c | 22 ++++++- fs/notify/fsnotify.h | 31 ++++++++- fs/notify/mark.c | 106 ++++++++++++++++++++----------- include/linux/fsnotify_backend.h | 8 +++ 4 files changed, 127 insertions(+), 40 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 6c338322f0c3..f83eca4fb841 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -103,13 +103,15 @@ 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) +bool __fsnotify_update_children_dentry_flags(struct inode *inode) { struct dentry *alias, *child; int watched; if (!S_ISDIR(inode->i_mode)) - return; + return false; + + lockdep_assert_held_write(&inode->i_rwsem); /* determine if the children should tell inode about their events */ watched = fsnotify_inode_watches_children(inode); @@ -133,6 +135,20 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode) spin_unlock(&child->d_lock); } spin_unlock(&alias->d_lock); + return watched; +} + +void __fsnotify_update_child_dentry_flags(struct inode *inode, struct dentry *dentry) +{ + /* + * Flag would be cleared soon by + * __fsnotify_update_child_dentry_flags(), but as an + * optimization, clear it now. + */ + spin_lock(&dentry->d_lock); + 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? */ @@ -203,7 +219,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..182d93014c6b 100644 --- a/fs/notify/fsnotify.h +++ b/fs/notify/fsnotify.h @@ -70,11 +70,40 @@ static inline void fsnotify_clear_marks_by_sb(struct super_block *sb) fsnotify_destroy_marks(&sb->s_fsnotify_marks); } +static inline bool fsnotify_children_need_update(struct fsnotify_mark_connector *conn, + struct inode *inode) +{ + bool watched, flags_set; + watched = fsnotify_inode_watches_children(inode); + flags_set = conn->flags & FSNOTIFY_CONN_FLAG_WATCHES_CHILDREN; + return (watched && !flags_set) || (!watched && flags_set); +} + /* * 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 bool __fsnotify_update_children_dentry_flags(struct inode *inode); + +static inline void fsnotify_update_children_dentry_flags(struct fsnotify_mark_connector *conn, + struct inode *inode) +{ + bool need_update; + inode_lock(inode); + spin_lock(&conn->lock); + need_update = fsnotify_children_need_update(conn, inode); + spin_unlock(&conn->lock); + if (need_update) { + bool watched = __fsnotify_update_children_dentry_flags(inode); + spin_lock(&conn->lock); + if (watched) + conn->flags |= FSNOTIFY_CONN_FLAG_WATCHES_CHILDREN; + else + conn->flags &= ~FSNOTIFY_CONN_FLAG_WATCHES_CHILDREN; + spin_unlock(&conn->lock); + } + inode_unlock(inode); +} extern struct kmem_cache *fsnotify_mark_connector_cachep; diff --git a/fs/notify/mark.c b/fs/notify/mark.c index c74ef947447d..ecfd355a93f2 100644 --- a/fs/notify/mark.c +++ b/fs/notify/mark.c @@ -130,30 +130,39 @@ static void fsnotify_get_inode_ref(struct inode *inode) * iput() outside of spinlocks. This happens when last mark that wanted iref is * detached. */ -static struct inode *fsnotify_update_iref(struct fsnotify_mark_connector *conn, - bool want_iref) +static struct inode *fsnotify_update_inode_conn_flags(struct fsnotify_mark_connector *conn, + bool want_iref, int *flags) { bool has_iref = conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF; - struct inode *inode = NULL; + struct inode *inode = NULL, *ret = NULL; - if (conn->type != FSNOTIFY_OBJ_TYPE_INODE || - want_iref == has_iref) + if (conn->type != FSNOTIFY_OBJ_TYPE_INODE) 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; + inode = fsnotify_conn_inode(conn); + + if (want_iref != has_iref) { + if (want_iref) { + /* Pin inode if any mark wants inode refcount held */ + fsnotify_get_inode_ref(inode); + conn->flags |= FSNOTIFY_CONN_FLAG_HAS_IREF; + } else { + /* Unpin inode after detach of last mark that wanted iref */ + conn->flags &= ~FSNOTIFY_CONN_FLAG_HAS_IREF; + ret = inode; + *flags |= FSNOTIFY_OBJ_FLAG_NEED_IPUT; + } + } + if (fsnotify_children_need_update(conn, inode)) { + ret = inode; + *flags |= FSNOTIFY_OBJ_FLAG_UPDATE_CHILDREN; } - return inode; + return ret; } -static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) +static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn, + int *flags) { u32 new_mask = 0; bool want_iref = false; @@ -173,7 +182,7 @@ static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) } *fsnotify_conn_mask_p(conn) = new_mask; - return fsnotify_update_iref(conn, want_iref); + return fsnotify_update_inode_conn_flags(conn, want_iref, flags); } /* @@ -184,15 +193,19 @@ static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) */ void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) { + struct inode *inode = NULL; + int flags = 0; + if (!conn) return; spin_lock(&conn->lock); - __fsnotify_recalc_mask(conn); + inode = __fsnotify_recalc_mask(conn, &flags); spin_unlock(&conn->lock); - if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) - __fsnotify_update_child_dentry_flags( - fsnotify_conn_inode(conn)); + + if (flags & FSNOTIFY_OBJ_FLAG_UPDATE_CHILDREN) + fsnotify_update_children_dentry_flags(conn, inode); + WARN_ON_ONCE(flags & FSNOTIFY_OBJ_FLAG_NEED_IPUT); } /* Free all connectors queued for freeing once SRCU period ends */ @@ -240,7 +253,8 @@ static void fsnotify_put_sb_connectors(struct fsnotify_mark_connector *conn) static void *fsnotify_detach_connector_from_object( struct fsnotify_mark_connector *conn, - unsigned int *type) + unsigned int *type, + unsigned int *flags) { struct inode *inode = NULL; @@ -252,8 +266,11 @@ static void *fsnotify_detach_connector_from_object( inode = fsnotify_conn_inode(conn); inode->i_fsnotify_mask = 0; - /* Unpin inode when detaching from connector */ - if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF)) + if (conn->flags & FSNOTIFY_CONN_FLAG_WATCHES_CHILDREN) + *flags |= FSNOTIFY_OBJ_FLAG_UPDATE_CHILDREN; + if (conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF) + *flags |= ~FSNOTIFY_OBJ_FLAG_NEED_IPUT; + if (!*flags) inode = NULL; } else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) { fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0; @@ -280,14 +297,35 @@ static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark) } /* Drop object reference originally held by a connector */ -static void fsnotify_drop_object(unsigned int type, void *objp) +static void fsnotify_drop_object(struct fsnotify_mark_connector *conn, + unsigned int type, void *objp, int flags) { if (!objp) return; /* Currently only inode references are passed to be dropped */ if (WARN_ON_ONCE(type != FSNOTIFY_OBJ_TYPE_INODE)) return; - fsnotify_put_inode_ref(objp); + + if (flags & FSNOTIFY_OBJ_FLAG_UPDATE_CHILDREN) + /* + * At this point, we've already detached the connector from the + * inode. It's entirely possible that another connector has been + * attached, and that connector would assume that the children's + * flags are all clear. There are two possibilities: + * (a) The connector has not yet attached a mark that watches its + * children. In this case, we will properly clear out the flags, + * and the connector's flags will be consistent with the + * children. + * (b) The connector attaches a mark that watches its children. + * It may have even already altered i_fsnotify_mask and/or + * altered the child dentry flags. In this case, our call here + * will read the correct value of i_fsnotify_mask and apply it + * to the children, which duplicates some work, but isn't + * harmful. + */ + fsnotify_update_children_dentry_flags(conn, objp); + if (flags & FSNOTIFY_OBJ_FLAG_NEED_IPUT) + fsnotify_put_inode_ref(objp); } void fsnotify_put_mark(struct fsnotify_mark *mark) @@ -296,6 +334,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark) void *objp = NULL; unsigned int type = FSNOTIFY_OBJ_TYPE_DETACHED; bool free_conn = false; + int flags = 0; /* Catch marks that were actually never attached to object */ if (!conn) { @@ -313,16 +352,16 @@ void fsnotify_put_mark(struct fsnotify_mark *mark) hlist_del_init_rcu(&mark->obj_list); if (hlist_empty(&conn->list)) { - objp = fsnotify_detach_connector_from_object(conn, &type); + objp = fsnotify_detach_connector_from_object(conn, &type, &flags); free_conn = true; } else { - objp = __fsnotify_recalc_mask(conn); + objp = __fsnotify_recalc_mask(conn, &flags); type = conn->type; } WRITE_ONCE(mark->connector, NULL); spin_unlock(&conn->lock); - fsnotify_drop_object(type, objp); + fsnotify_drop_object(conn, type, objp, flags); if (free_conn) { spin_lock(&destroy_lock); @@ -331,12 +370,6 @@ void fsnotify_put_mark(struct fsnotify_mark *mark) spin_unlock(&destroy_lock); queue_work(system_unbound_wq, &connector_reaper_work); } - /* - * Note that we didn't update flags telling whether inode cares about - * what's happening with children. We update these flags from - * __fsnotify_parent() lazily when next event happens on one of our - * children. - */ spin_lock(&destroy_lock); list_add(&mark->g_list, &destroy_list); spin_unlock(&destroy_lock); @@ -834,6 +867,7 @@ void fsnotify_destroy_marks(fsnotify_connp_t *connp) struct fsnotify_mark *mark, *old_mark = NULL; void *objp; unsigned int type; + int flags = 0; conn = fsnotify_grab_connector(connp); if (!conn) @@ -859,11 +893,11 @@ void fsnotify_destroy_marks(fsnotify_connp_t *connp) * mark references get dropped. It would lead to strange results such * as delaying inode deletion or blocking unmount. */ - objp = fsnotify_detach_connector_from_object(conn, &type); + objp = fsnotify_detach_connector_from_object(conn, &type, &flags); spin_unlock(&conn->lock); if (old_mark) fsnotify_put_mark(old_mark); - fsnotify_drop_object(type, objp); + fsnotify_drop_object(conn, type, objp, flags); } /* diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index d7d96c806bff..942fbcc34286 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -474,6 +474,7 @@ struct fsnotify_mark_connector { unsigned short type; /* Type of object [lock] */ #define FSNOTIFY_CONN_FLAG_HAS_FSID 0x01 #define FSNOTIFY_CONN_FLAG_HAS_IREF 0x02 +#define FSNOTIFY_CONN_FLAG_WATCHES_CHILDREN 0x04 unsigned short flags; /* flags [lock] */ __kernel_fsid_t fsid; /* fsid of filesystem containing object */ union { @@ -485,6 +486,13 @@ struct fsnotify_mark_connector { struct hlist_head list; }; +/* + * Objects may need some additional actions to be taken when the last reference + * is dropped. Define flags to indicate which actions are necessary. + */ +#define FSNOTIFY_OBJ_FLAG_NEED_IPUT 0x01 +#define FSNOTIFY_OBJ_FLAG_UPDATE_CHILDREN 0x02 + /* * A mark is simply an object attached to an in core inode which allows an * fsnotify listener to indicate they are either no longer interested in events From patchwork Fri Oct 21 01:03: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: 13014176 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 1DA26C43217 for ; Fri, 21 Oct 2022 01:03:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229981AbiJUBDa (ORCPT ); Thu, 20 Oct 2022 21:03:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229983AbiJUBD0 (ORCPT ); Thu, 20 Oct 2022 21:03:26 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB73023082A; Thu, 20 Oct 2022 18:03:25 -0700 (PDT) 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 29L0EDxM016669; Fri, 21 Oct 2022 01:03:22 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=U4SsRmB3G9PJJFDy2/xoex3JNU0etDYF8lljuvSJL4A=; b=fo8ctwo1GMJEkGKlJ8czaX1kZ1KpeToJM9ewvgdhT47qSQhElgixhSDrBiz7c6i4myDk 93aqkGOLK0Yr3Z1xpw/kI2n50/6zSJ1WYemOMKFnoT+GFMwwwibCAXThLEX905Ieducj 8A1GiI6hAyeV+6nflvMxCWPcTR63kZsMz4Px59Ise/Ot1p5JqBIOL+u6+m8bnUSWhkx4 W5enJeC1xpm0lLaVKuCcK5kxfrApA6dXCIvK4gYLSi3H3dV8WogGtcOHNOkvxciB4iUx +AQl1xSNf5ppQnkTCPoQ8/Gy1hTwxihwMJa9YxECOayaqLn8rV90nM+8D1SDaiJM3lco Tg== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3k7ndtqp4q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 21 Oct 2022 01:03:22 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 29KL9HmT018261; Fri, 21 Oct 2022 01:03:21 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam04lp2048.outbound.protection.outlook.com [104.47.74.48]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3k8j0tge9k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 21 Oct 2022 01:03:21 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oasWSnRyud56tV2DbBDE9HKTEyh02AK0qgPa5QZdDwQFd57Xr+I3R1UwUHgrtnJ1hNuel7qBJxQdurYjMBEF/ESXPVSJ/VHgKdPfQRSGkBEsOJs2WLFhnniD5OcdyIbxymyTjoerv91o/PgD55sN8/EZPNQCUO/ZchxhvsuhYvXvnbN316c8jmPjqE3iPuiZye5Ei/sE2bstsJdq8kOkMGNVugvnjghjtw8jb30W/G5aHp4kirkacHzWAY07WG8zaDj50qmp64rX8gQEpvglvKJcicZn5TdIVj05DUPpTUiEb9z8XNpw4N8udK0ndU5pkQGVYhTKdQFBBrbXY6em6Q== 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=U4SsRmB3G9PJJFDy2/xoex3JNU0etDYF8lljuvSJL4A=; b=f8CaPmkRQbi7GspwwBxHXBjOKKdTXRP+P2FLEROWXesJnfTxQHYGbX7RzO3yCrJChzJS6srLUt1q8q+iIGPHLtq7n5/JDiU2heRQHYVnbtkD//nJJ3Tml32L6pdVIovELBveCaKAeRhNtLYdbaws1jOikpm8pSBWRtNUywbWumgmtoIyqJ+8udaRtqlFxnBLNEq4dZYvPyI26d68JIrVH3yNfPevDGAjeYyEU35KFd5//vCgQrDBASDMDCYFml1Z7qPuvCDjv0rfzoUhCpCcvkETTjwL182Pg8g91jUngC6wAqQ7iPWMBTc7FBGL0sCVL4hRQ6swIdwrURJD6gMidQ== 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=U4SsRmB3G9PJJFDy2/xoex3JNU0etDYF8lljuvSJL4A=; b=hDEjfLMobA7zl5JaVpDRcC7sgTnjfvzv01UK0K4M6SjspHq2mwH68SVSa2vNqCSMWdk7QkXLykNXOxHv1g2NkaGjM45odDV7EzTXqmQrwg/L8rWkzzQHYDLDOQ7K1K1HZXljYZMpQ2A6hRtsvgSJWRKzbN6z/8sf4TnDR27RIrw= Received: from CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) by SJ0PR10MB4446.namprd10.prod.outlook.com (2603:10b6:a03:2d9::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5723.35; Fri, 21 Oct 2022 01:03:19 +0000 Received: from CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::5374:25b4:8dea:31dc]) by CH2PR10MB4166.namprd10.prod.outlook.com ([fe80::5374:25b4:8dea:31dc%7]) with mapi id 15.20.5723.033; Fri, 21 Oct 2022 01:03:19 +0000 From: Stephen Brennan To: Jan Kara , Alexander Viro Cc: linux-fsdevel@vger.kernel.org, Amir Goldstein , linux-kernel@vger.kernel.org, Stephen Brennan Subject: [PATCH v2 3/3] fsnotify: allow sleepable child flag update Date: Thu, 20 Oct 2022 18:03:10 -0700 Message-Id: <20221021010310.29521-4-stephen.s.brennan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221021010310.29521-1-stephen.s.brennan@oracle.com> References: <20221018041233.376977-1-stephen.s.brennan@oracle.com> <20221021010310.29521-1-stephen.s.brennan@oracle.com> X-ClientProxiedBy: BYAPR11CA0103.namprd11.prod.outlook.com (2603:10b6:a03:f4::44) To CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4166:EE_|SJ0PR10MB4446:EE_ X-MS-Office365-Filtering-Correlation-Id: cc24e55a-ed3e-448d-6d0f-08dab3000b28 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AXZfaspf+88LZXLT8EZn2DCncVaLTCsOyDFqUu0aZbKgfBqPWNDCiGAJZsPUgBg56fzyR6Y1Jj5y7nJBwVtcSNgoYgZ5ynoUuAM/HbzdqB4iHnqblng6hAaItYDxx55budj0vE/+kAMKL7g95fv6qpTTfbynfnqy6eW58zWw7vh0pGf67XFpKVDMeSJi7bUUPCGUt1zwJ+qIbqOMpre3nWJ1gOnMCQibtqWmjPstSWgc25lBdbMLp6+4awiXMySd3cbg26+LXAjMMA6IlmSUZIQXauUND5pGvEppFkpwbmrvPjUYRmITWjQdzrzlH9TWcpMbI5tG885KLaafLEtUBL24py53/Rr3Obh7VrPGO+Thp93uE7rxjkkozHfI3hwRDmJx3RKRJpjN498ofT4v7YFbJoM5Z8ugcALBu2KAcgejvTndsVaG0IeZyudTQeEzJ1VCMASlRMaXqaqNb8ZIUdepUSlx9E2mO8d/myOgnMa2o6Fvq0C9GjP/WKu/j2h/mbOwpppYprsx/WQ+np+r2rPla/Dv2jlGFXQw4x1HVYsatHB+buI/zuxw52ev303bJVutHZ0SvlxQbwWlIOTxZhtRiDn9UmTzuyTdViA58IAg871IsdsQV9PGJtFljLw/sNUW8n7BNlBK9r2ckYuQUoX1VCw4oZitb11LKeZORGBrsIjjJZc44FZzJL0dxvp/+7f/LKPiaV/FZNcjoNdBAg== 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)(39860400002)(136003)(376002)(346002)(396003)(366004)(451199015)(6506007)(36756003)(86362001)(38100700002)(316002)(2906002)(83380400001)(103116003)(1076003)(110136005)(478600001)(107886003)(6666004)(26005)(186003)(6512007)(8936002)(2616005)(66556008)(4326008)(66476007)(66946007)(5660300002)(8676002)(41300700001)(54906003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LlsO9tCZnn1kpKorq8Ng+F7ojN6zKiaM2zQ6BXPzMTYJPA042CrivsJZlQrD+XNVsJqXvscsfURIVkHuRvdZDlPldVwq3t4kJRykKMDUXVVXc+XBdlv/yvRF+X8UwEfnEGAlkO+XMocraflLhBsxQtSSkXqSC8weES9Wzzhi0KFglk02v9VwketfSZoEF5MSLSXyasiRatRsjDmybHSVe5nDDEUOjNKNbEF/F6nDYBcsFiMQKZt71m8P1QuQn8rdBAKG7z4W2ZCKNCp0lTQJU1C+gvUO5SE1AUY9hnr5zFpqfPh5LB6lsLOC0m1VyEbLO0YgX/0Jvnzv3wGN2nQ3aQeWvCQKKt9BqYB0z/8nL5eFAHrtdQ3WGbE7nPvhrHpTwY4U0EAfUX/6gpOOSqFL+ayX5/eoljDpLFhJvhPb+WD3IyK3O1lVutK0FES80h6PEAj3FRrr8kvTr09CmD7L2pve68t6URBP85ZZASPkRl8rQtbdX0XqNKFgEkCTosED5aR0SYQXA+QC/bCK0wCYNoHCrfZDuUw0VIxZiS+2L5QzYI5qY8CH+LLYJp2B1oFnwlduoBfT4CjrAPkqbCV/iPQJeVOAuZvbMxgaXaECLcWHgVg5GkG0jKHuHrVhRthZjg62V7KlTXnUFG9uwWfMguS1zxNd8H0IMw9D91G1gCl/6Gpir9FYHgtywqVmM0gDubRVj0Vulf8diQW3XS/GOlXWAe6ceoX/oORYBpUlWHi8Im1YvuzFx+ZCNFQHEoEQmdbx3Qr6vR23AYPS+pehCCcOtYLI6YgAko+cUO94A5xxCXdB155AxMHsiNt2jhDaNQSEbwV9I6+jGfm3VLri0jJ/PK50Y10J3x+jxgCd+5Od3W5fHyGizLlFPBDlKLLwQGLoBsvTQvafAnTgYTYoRIQ/5eqI3/nO4qytxERjUKMEM3mzDM3S2ANN+eb9OGvHElOrnnjyL5hd/hg+Uo2WJIc3UnjbxXQZWD18EukPunZAQwhSl5U58FBRppFyvY7YXC0LG1vkKgyw0T0MndXACiWw5TnF+XlG9NpimRPVru5cbAKGXSKpk2ssOzcBI2+QZXsCJOB3Yyh5G4aBvcsgalWV/82lEAAlA1x+lxbRA8c6BwANkss1cu4/hBd48IZfZktcKOGc19CNpd1+aT0Cbl4XqG4p3cQZIVmRx29J60YqNQUqIJ1S33cCDkxUyg9sK3fbFiQHnIofjvnoI72mKNcYWUWal5pFDfWsXA3cBPKgHTIU8OoOD5I0FXNgD0iuPKwq3K9IC8vTVZhVAm1M0u6D8xdpLGGNnddFzw78W2OXEGYUm6jMemNkZycUUhxh0qaS2ELO67wvZrlYiR05/lhuuqM00BOFlIZfOr8KWnYuoLBHU6X9SXDvrCDnmKsFTPeyVaNKAN8gp75uB9cjRMUROCJlSJymlAGM2jYP41b3gfEpgLRGx2lABNgWHSmVbYiiJFtkgFuH1QDItcMkkUvq/WQGC2NRnLaYnDLh1/iU/b/0ywgqt48eIAnqG268VsYr3Bi1/v9VaIeSRaLWvb3IDB7yPIpzoUf+fbWRvdoXgHw7X/qoqEpnUehTTFct/jSx8ujOtq2+WaA9mlEAmg== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: cc24e55a-ed3e-448d-6d0f-08dab3000b28 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4166.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Oct 2022 01:03:19.6478 (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: FZdg5d5ARpWwymK2UIjXov7Cr4XKnIP8hvIJPZhEZx+aFoNUU+UJ5dcIaFw4meHVjC62HBgHYDpeawE4XHv/SsH/A7Z86NrHYSTS7qDGo7I= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4446 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-20_13,2022-10-20_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 bulkscore=0 mlxscore=0 suspectscore=0 mlxlogscore=999 phishscore=0 malwarescore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210210004 X-Proofpoint-ORIG-GUID: S5OU2UjuDetV2H9CkBqqHX3e5PMccWOM X-Proofpoint-GUID: S5OU2UjuDetV2H9CkBqqHX3e5PMccWOM 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 parent->d_lock, this can trigger soft lockups. It would be best to make this iteration sleepable. Since we have the inode locked exclusive, we can drop the parent->d_lock and sleep, holding a reference to a child dentry, and continue iteration once we wake. Signed-off-by: Stephen Brennan --- fs/notify/fsnotify.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index f83eca4fb841..061e626127ca 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -102,10 +102,12 @@ 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 */ bool __fsnotify_update_children_dentry_flags(struct inode *inode) { - struct dentry *alias, *child; + struct dentry *child, *alias, *last_ref = NULL; int watched; if (!S_ISDIR(inode->i_mode)) @@ -119,11 +121,38 @@ bool __fsnotify_update_children_dentry_flags(struct inode *inode) /* Since this is a directory, there damn well better only be one child */ alias = d_find_any_alias(inode); - /* 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) { + if (need_resched()) { + /* + * We need to hold a reference while we sleep. But 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. + */ + dget(child); + spin_unlock(&alias->d_lock); + dput(last_ref); + last_ref = child; + cond_resched(); + spin_lock(&alias->d_lock); + if (child->d_parent != alias) + goto retry; + } + if (!child->d_inode) continue; @@ -135,6 +164,8 @@ bool __fsnotify_update_children_dentry_flags(struct inode *inode) spin_unlock(&child->d_lock); } spin_unlock(&alias->d_lock); + if (last_ref) + dput(last_ref); return watched; }