From patchwork Tue Oct 18 04:12:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Brennan X-Patchwork-Id: 3933 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1772889wrs; Mon, 17 Oct 2022 21:37:06 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4q7B+qMtklBDPJvI6M9sYJF8xikzsXMqdDAkyVYM3kn5cMD1cI+9/3/Hg9beXzRiXI+2LS X-Received: by 2002:a17:907:7606:b0:78e:61d:757e with SMTP id jx6-20020a170907760600b0078e061d757emr789362ejc.690.1666067826725; Mon, 17 Oct 2022 21:37:06 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1666067826; cv=pass; d=google.com; s=arc-20160816; b=hyQ1AafUpmximiUfnbbP8HPcbJQJMtBA4Mt/7xnqwYsZTDqwTfCzZ2dAYgfgp3Jk9c h3N/+tcAfews361AHolm3exxfO4/NnZB+pxEHo/gkK6csH48PMCPLeD1MODk8mIw20yC 2emhB6sE1S/DLsJlLdB5ulN1/xzQskPUsl+YWJJ3ee3zss942OQ0cSeVmA43IPQMtl1o zicc5mvGjYocPpb+FCbi1y5c0n90vZgcQC/vGVmeWGV1aEng2dEAVrWiHxIbTIIgSWFu BME0G5FeOVDAQFDB1+j/pLVo4zG3XRf01W0nd1WwU/mKu9DHFQvEcORcpfbCbgeJWsnB IdEA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=iCvkMCU/jylQ+ALpEDdsaggsn4HPn7lmO+cIKQC8Oss=; b=ijsoDwZkCKZ3HKXAnOiHJpWYa76tQMptzcWdqD2AWH6B248hG3qngROTEGW+H4nH0I wI+8HW5Cozzg6bgToK4C87lp883o8cxHRiK8N1gy21tWUTugBpvT8Y+RnrXJt+QUBpfl 513dvFsTILiBNoBVYn4mkmd9ho2p54dkkveEDI70YMplEtLFSODUtetKhgkuqY7UO23/ oIS467X6+z5lJ6CvssZ10khR67ReNjqDetqdibBHy4ukZPY4Gq9qY39PUmW8+ar5rLj6 EBWpU2khopctiLylG3Y7T7eFxnAWv5MkmPrad4iHZfgNxGzp5lDzRcW4RawVMYJ0P6kO cn1g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2022-7-12 header.b=Sjwz+r8H; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=GUrdzuos; arc=pass (i=1 spf=pass spfdomain=oracle.com dkim=pass dkdomain=oracle.com dmarc=pass fromdomain=oracle.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id go11-20020a1709070d8b00b0078ac0d5d03esi7735294ejc.562.2022.10.17.21.36.41; Mon, 17 Oct 2022 21:37:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2022-7-12 header.b=Sjwz+r8H; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=GUrdzuos; arc=pass (i=1 spf=pass spfdomain=oracle.com dkim=pass dkdomain=oracle.com dmarc=pass fromdomain=oracle.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230283AbiJREMw (ORCPT + 99 others); Tue, 18 Oct 2022 00:12:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229993AbiJREMp (ORCPT ); Tue, 18 Oct 2022 00:12:45 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC71B8A7C3; Mon, 17 Oct 2022 21:12:41 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29HNYIC3029594; Tue, 18 Oct 2022 04:12:39 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=iCvkMCU/jylQ+ALpEDdsaggsn4HPn7lmO+cIKQC8Oss=; b=Sjwz+r8H5agAEpW6OPNjfaeWbtj7NJV5IfnVAvfmtXLIfScIWQJsOdGSM2I1WHqybndp jnnbWZ+KvHwPTkP3UoZC46gFRcNjr28r3d5zSBXRINiwXJwmsmvFDwYH+nDCJ2Q6K/DO ZyPgrkq5wNMO5WxXGwBDY9VFsO1VAIXEToCFmExhO9h5foqiuGiguuBoPqM3jyCTTaoS UfBC7DkGEe2x4ZzJRDRtSbmIq6lExZxXaQDwTUcisMLsf+I0TuWkNi8B41JNuJIa4WBB Gn97pBUoXSwFE5ZAaK/nh/82pZoLVIasAV6nNPF1cE5fWJbV32fszWqx0FYO4bs1CSVW 3Q== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3k7mtywj1t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Oct 2022 04:12:38 +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 29I3puF7015903; Tue, 18 Oct 2022 04:12:37 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2174.outbound.protection.outlook.com [104.47.57.174]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3k8hqy6fdk-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Oct 2022 04:12:37 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KWAnh3k2tTsTyED+XSUbcxa0wbbZ2wKSipCP/GqubRWnGMXiTqLQlCRiydrNfqwhlALCIWNuTnQ4sTRyqOMAbOzdThSCINh0yPqJ6wkl7n7t9hidSNYPo+/yN6P4ofXBxPTW7A0eJTnvW9W73Vdgm9irzGTWfwjU9u1t1BzvjXMeNA/wJFglQkZ9hvClyHmbjfeoPc/3c/We4BvvD9cDHZ6NYvt47yuvLb80PyhC4JYA8/20kSo9LNidpmimsRHszea+SjzEAwzabxGULs7YQLgb2HCSa3CwsG/KSm8J+JIT6Yj8udhivqcZTKcwereE0VqbPCKSex6ZgXKIEWV3cg== 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=iCvkMCU/jylQ+ALpEDdsaggsn4HPn7lmO+cIKQC8Oss=; b=U+7trZt3eeSV0xBLV9+j3FWNK77B4MvY7pBIAyDRMjRlXOWC5HYb6aUplDwgJIjD818khvcZszUHrEu922ObxAc9qYNnd4/eHTKhP5K3NEg9SevJAVQO8qbFcbnRf5THa1TjbxyCq9zxMj+dMhbPBd0svq02TsL6QOuunxIofMqRHVd8hXIQ4RUuHoSYMnw2d/hnCUSx8MpaRRFB/ndHSBwQEtCcCswo9bTvGycH/Q8Q0M/CP0fmIYo73sIRglqsYIcPf7rqsp3n52MiL2wDt/kEidwa+Z2lKa7oSsVR1/MmSC29co0lNc1w57fx6EHEFcK+aR324MoS97t33ISA6g== 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=iCvkMCU/jylQ+ALpEDdsaggsn4HPn7lmO+cIKQC8Oss=; b=GUrdzuosKh5ka7ESplM0yp4Nsih7tWYTEQ6byltvtehk86o8FR8emd5SCgoShekDFNfjMWHOCvpEVDgd411XhhOXG/UABfMoU6Hy6Iwi97uMY2P4Y0kxr0QrtophnwZRc3/3XChnzCUru6hRYdYdzShrvuxJ28RhDNkrIs3eeSc= Received: from CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) by PH0PR10MB4567.namprd10.prod.outlook.com (2603:10b6:510:33::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5723.30; Tue, 18 Oct 2022 04:12:36 +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; Tue, 18 Oct 2022 04:12:36 +0000 From: Stephen Brennan To: Jan Kara , Alexander Viro Cc: Amir Goldstein , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Stephen Brennan Subject: [PATCH 1/2] fsnotify: Protect i_fsnotify_mask and child flags with inode rwsem Date: Mon, 17 Oct 2022 21:12:32 -0700 Message-Id: <20221018041233.376977-2-stephen.s.brennan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221018041233.376977-1-stephen.s.brennan@oracle.com> References: <20221013222719.277923-1-stephen.s.brennan@oracle.com> <20221018041233.376977-1-stephen.s.brennan@oracle.com> X-ClientProxiedBy: BY5PR03CA0007.namprd03.prod.outlook.com (2603:10b6:a03:1e0::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_|PH0PR10MB4567:EE_ X-MS-Office365-Filtering-Correlation-Id: 606e2529-1e30-42bf-ce09-08dab0befd1e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: as5Lg12inIXFgNzBiTfk5NPbs4CpnXG7TkDNQOQNpSLoxtCz7KiDbgdKld/nxCnx3jrP2WITPbXRqs8w8CNYfDKpBRVZkKgXxg6hCcoGMUWN5Io37Wyho4pmqhOAYPbfjz535s0jCXr2dnFAvSBZ+8xjeGMMbGOaKJc0rgo0zboEw+YCLprZlin6Q0ZyS4Uuyk4JnQj3kIK4vgZFyIfzj2KO/4WibN8SlXFhf4u30zXKwyFefjcvUQyAPu5cHW8TX9yfP8d1PFK7LjYadldGzUQfO5qtljGD6YeC5GRGErMRgUlEC/RqRwsGhZBMs5bm5+lAyLdQ0DVTYn6fI03TCwGhrYlq0/S+hYvwORo9MPBqLNGXDoOKM5h9LZtW4gaZWbVE5pry7yjOHyKuYJyYnfM1XRW5k6A5MojKiLVza/WnjClMHgeU2lWPwIHuO3UyqDXNkyudAqwYIphIRlwaRj+z4Y3ycnSXSVXUly5ZZmAyBgV0MzQFxiguLY/rH80mrFoVIt2Vo1Eocv18Y/WANMnUrEv89nwBQJqjmoPy0NQlZh/q1wE8qh18UlSiv3JruxcagBnJL4M3Mq9xNHOZyFuzGsHKfsSzCv99Eb74da1WNoRAR4bWV8uQEHBIZ9TzKY78gqEyAcxGt/63dRmqiNC/BgJ9Jb67+S60lokedgbKrteRK/sVe7EarJ+cmNpVqhmoxEy0u59hfCkaItXSrw== 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)(396003)(346002)(376002)(366004)(39860400002)(451199015)(54906003)(110136005)(66476007)(66556008)(4326008)(66946007)(8676002)(107886003)(6666004)(2616005)(316002)(1076003)(186003)(2906002)(103116003)(86362001)(83380400001)(41300700001)(5660300002)(6506007)(36756003)(26005)(8936002)(6486002)(478600001)(6512007)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ctcxDY6qKAkd4XYhUo2FYRHdZK5EbMtzaAUpVA5QNks6F8cUPp+jdA/sELMGEpCLUoVHWLDtdmvkE+1iX6jCfzM3siLVZ9c+vvZyXxoNkcroVDyFZ1RDsHBcKNX1JlRiP+Tojwep+7cW3pEaT7PyEti6xmCgvpTt3Z+6fScWpWbJy5p7DMdD1dWxiXt9UNz/wLKIfxTS4glNB+NqwkWWSsuWMuZ1c8bReRpTRqIyB96ZzPWKEfDXyAYLKFLCL0LlbZiCtcEhOUBuiYkQGbfwLDz0p2bfp5DCcNzbaUVt64nJIV9MMucRZbBQFkAdwskY/SvKwvKmrhululo2TsFK29ce11EuphJr1sVQCWjDnHE57L8CyJztAdcLqaPTX2jGIlpnkvzZBJjvHL7pLEbFuLcEKOEJRQv2gaECyYd/Im6+GoHwM5LCEdk5OqzteiKD1FXrJsDWcBmv8pAJaGeVRU3SQTlGT5Q/5DMSxMW5ePjwt+GfhZgTEJVbd0kEe0r6bKfzmZ6NaoKy7oMJ5RPYTIsep9rKIvfTovPOJWnmVfKGWtvFWfgAVpLBsfIv2tQB6kFqSD2aEEJ/A0C67+JF1VowDVUBFIhJbwSsEF5ixNQjC7nlKGDw3KKv87Qk1xcTDaI0F1Di9pvi59b1omlJFJuLEQB//uSQ86O1voQiDHfflGmcJVIWtkcJJgE0xC7AEJ96QzRth2PRPiDO2d5l0Z8n4qBlMTiWKHQKhaqQtd+QCpWwjPf45auOkpLqULONZ1hojbaRuFVOPNQQnlfFgPBtzxqgaf8A/DfcC7YjSycbGz2UGyFl4ifV2keHf2Kv7uPA7NFqnxyHDbmwvhgFIdDc60LMkxq8X1lyfA253OP+4xMPoaZ83qudu4B5JGLFKGx++oDKC9PHaXZJcX1+IbFbJSBGTv+eOw6VtKFGqeZK1dFFL7YvcWdP7ZC7T9zECXtYZq9/lCRH9hapkPAXAQEyap1w04s+hxYiNWZjdbaTb3pVzuPLAnws2UgvjOOZZVhbpFrHrSGPV2SSXdEwLQWQekUOB60w3wszEGIl9zu2H+tuLyzLcSO8UJkN9I1WX/gN8+ei/iD9DE4LLQ5Tq86JWXNltnW+5R1DknM92Dl3/4t2kWKzIDq3yCkEV9KnIZEGq4kTEJNyruUWpeHDILVnWUyNq01TjhYidmGashbvHNYpMZx36FD4J9meFbBye9Alv9TH6LBJZX5LWfdeiT5b5b6me1nfU2Xm3P1CogJLDPQc7Zkwe6zU+coennt4JBd1gwcam0XPLk2fGoruEI5J6vWL0+3CDp3BgMm/zoWKaiBHpk+tpxDiQcZjykR8DduCBn2gb8dSFpYb+x6Fmy3M7qjpxK/qlIVw7uDRzikooGlWW77h4G3Z2zdeBr73TIsjq2ph74PLdB9Egdsbuu694+uFpEr+wMagFlg6Idqtg4Up93BKWXoM03BpvPqtl/TXUBkPs0j6hDsw2+XZ08UKYBZybEh9N57Sf67qYj7CE654bHrCYIyW1YI/WD6zxjE4t3vfJ278KVMexM1zuo5g9ZzORmjIMP6aEHfFkFM2sWQmRBDK2iLETZdOSZFt13HDd/msX5yR7usOxdM0lg== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 606e2529-1e30-42bf-ce09-08dab0befd1e X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4166.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Oct 2022 04:12:36.4525 (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: l9oUkbtSLu/mnZpqiMoO5mNZhPO4hTN1SytDJFSU7+HM7x3xF+I8iA26jUinBQYpj4+FXrdmPIrrHFplw7UBgz30yphH1qLR4BIRaQ9slZA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB4567 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-17_13,2022-10-17_02,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 adultscore=0 bulkscore=0 malwarescore=0 phishscore=0 mlxlogscore=999 spamscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210180022 X-Proofpoint-ORIG-GUID: Wej28yRvfJB63sqGzOKOsKimxwAjcCi- X-Proofpoint-GUID: Wej28yRvfJB63sqGzOKOsKimxwAjcCi- X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746998737531725268?= X-GMAIL-MSGID: =?utf-8?q?1746998737531725268?= 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(). Protect the child flag update with i_rwsem held exclusive, to ensure that we only iterate over the child list when it's absolutely necessary, and even then, only once. Signed-off-by: Stephen Brennan --- Notes: It seems that there are two implementation options for this, regarding what i_rwsem protects: 1. Both updates to i_fsnotify_mask, and the child dentry flags, or 2. Only updates to the child dentry flags I wanted to do #1, but it got really tricky with fsnotify_put_mark(). We don't want to hold the inode lock whenever we decrement the refcount, but if we don't, then we're stuck holding a spinlock when the refcount goes to zero, and we need to grab the inode rwsem to synchronize the update to the child flags. I'm sure there's a way around this, but I didn't keep going with it. With #1, as currently implemented, we have the unfortunate effect of that a mark can be added, can see that no update is required, and return, despite the fact that the flag update is still in progress on a different CPU/thread. From our discussion, that seems to be the current status quo, but I wanted to explicitly point that out. If we want to move to #1, it should be possible with some work. fs/notify/fsnotify.c | 12 ++++++++-- fs/notify/mark.c | 55 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 7974e91ffe13..e887a195983b 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -207,8 +207,16 @@ int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data, parent = dget_parent(dentry); 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); + if (unlikely(parent_watched && !p_mask)) { + /* + * Flag would be cleared soon by + * __fsnotify_update_child_dentry_flags(), but as an + * optimization, clear it now. + */ + spin_lock(&dentry->d_lock); + dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED; + spin_unlock(&dentry->d_lock); + } /* * Include parent/name in notification either if some notification diff --git a/fs/notify/mark.c b/fs/notify/mark.c index c74ef947447d..da9f944fcbbb 100644 --- a/fs/notify/mark.c +++ b/fs/notify/mark.c @@ -184,15 +184,36 @@ static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) */ void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) { + struct inode *inode = NULL; + int watched_before, watched_after; + if (!conn) return; - spin_lock(&conn->lock); - __fsnotify_recalc_mask(conn); - spin_unlock(&conn->lock); - if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) - __fsnotify_update_child_dentry_flags( - fsnotify_conn_inode(conn)); + if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) { + /* + * For inodes, we may need to update flags on the child + * dentries. To ensure these updates occur exactly once, + * synchronize the recalculation with the inode mutex. + */ + inode = fsnotify_conn_inode(conn); + spin_lock(&conn->lock); + watched_before = fsnotify_inode_watches_children(inode); + __fsnotify_recalc_mask(conn); + watched_after = fsnotify_inode_watches_children(inode); + spin_unlock(&conn->lock); + + inode_lock(inode); + if ((watched_before && !watched_after) || + (!watched_before && watched_after)) { + __fsnotify_update_child_dentry_flags(inode); + } + inode_unlock(inode); + } else { + spin_lock(&conn->lock); + __fsnotify_recalc_mask(conn); + spin_unlock(&conn->lock); + } } /* Free all connectors queued for freeing once SRCU period ends */ @@ -295,6 +316,8 @@ void fsnotify_put_mark(struct fsnotify_mark *mark) struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector); void *objp = NULL; unsigned int type = FSNOTIFY_OBJ_TYPE_DETACHED; + struct inode *inode = NULL; + int watched_before, watched_after; bool free_conn = false; /* Catch marks that were actually never attached to object */ @@ -311,17 +334,31 @@ void fsnotify_put_mark(struct fsnotify_mark *mark) if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock)) return; + if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) { + inode = fsnotify_conn_inode(conn); + watched_before = fsnotify_inode_watches_children(inode); + } + hlist_del_init_rcu(&mark->obj_list); if (hlist_empty(&conn->list)) { objp = fsnotify_detach_connector_from_object(conn, &type); free_conn = true; + watched_after = 0; } else { objp = __fsnotify_recalc_mask(conn); type = conn->type; + watched_after = fsnotify_inode_watches_children(inode); } WRITE_ONCE(mark->connector, NULL); spin_unlock(&conn->lock); + if (inode) { + inode_lock(inode); + if (watched_before && !watched_after) + __fsnotify_update_child_dentry_flags(inode); + inode_unlock(inode); + } + fsnotify_drop_object(type, objp); if (free_conn) { @@ -331,12 +368,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); From patchwork Tue Oct 18 04:12:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Brennan X-Patchwork-Id: 3932 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1772801wrs; Mon, 17 Oct 2022 21:36:46 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5zS1YVZfcXMUGhcQF/23OxhaBiZhafd3aUKPfnJUfBYm8Z0cbI//5RaVU7/Z8If/9e910R X-Received: by 2002:a05:6402:440c:b0:43a:1124:e56a with SMTP id y12-20020a056402440c00b0043a1124e56amr924334eda.134.1666067805930; Mon, 17 Oct 2022 21:36:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1666067805; cv=pass; d=google.com; s=arc-20160816; b=PwPJYnRPDpWxNPwLcm7JGF7Xvq2gXyC/Ntw7R8PcOePudItk+g/FnjjB+weozvdtF/ rGH9inf+ug/x8pt/urgZLc0aI232IiVNemq+43G6OwochvdIsuk0oDqvyMdOLtWuHgUc 0Mjh2s36UUfG+LA9pFk8Q1qtZPzIBMulrXoRv9rUkPx4YccztevtkKMdfWBZzpH07eAp SAxpguh3zoRAFQL5Fo1KpjWcM771qimABOuSD+e7d//zF5X2DJRTZdEDnSZayxVXKshC LXcsMQ87oLt0c5hCwLc/ZhSxVZqzaPf1Ibu7OUgY81ocXPyUrftyugd5I0IzrvmYE3rn drmA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=EoPDz7SHNnc8/VHYvCgMBz6YOkq1tx8CVnYdSlXCcqU=; b=OKYbi4vjQbO7URxuL4JQXJ29huAhGeYp8rTo+mneOjCVUvalQkdkKIgtTbGZspB7t5 3fdhK/3rtHJYUhjnEOX1jDL19ekgK4YIxBoKYS45A23Mp0dKNC7lcVssLrL7srqH/nSO 7en9RPvkaoiNY/yjTqnBZIr7E76oZSZAOaJbX9oFUsojMC6Ay8F2OaC2vy2Zpa0AAX1C 9XuI6U/6gWzBZOAon1+UTuP/GHiS8thGJIEbO0vS7Vq2BhZxbKKIn893Bqm5O2BYXehY 58OtBUxA7gew2efCuj8rWJsJfmcfmpReYMXczABgRlGP9tQFYi2amsR1dTY8h4HCT/bo VB6A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2022-7-12 header.b="va/dHIGx"; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b="C5dXo/9X"; arc=pass (i=1 spf=pass spfdomain=oracle.com dkim=pass dkdomain=oracle.com dmarc=pass fromdomain=oracle.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n11-20020a170906118b00b00782650a2738si8243272eja.24.2022.10.17.21.36.21; Mon, 17 Oct 2022 21:36:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2022-7-12 header.b="va/dHIGx"; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b="C5dXo/9X"; arc=pass (i=1 spf=pass spfdomain=oracle.com dkim=pass dkdomain=oracle.com dmarc=pass fromdomain=oracle.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229964AbiJREM5 (ORCPT + 99 others); Tue, 18 Oct 2022 00:12:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230148AbiJREMq (ORCPT ); Tue, 18 Oct 2022 00:12:46 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38CF189CC0; Mon, 17 Oct 2022 21:12:46 -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 29HNYRBV028823; Tue, 18 Oct 2022 04:12:42 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=EoPDz7SHNnc8/VHYvCgMBz6YOkq1tx8CVnYdSlXCcqU=; b=va/dHIGxefvu0E7a9vR65tCLO3bdXzYMgLtrGaACmjvauDtPwSRk+e8EjXjSKnI5g9QX xbOtwkjQVRQ8jvZTl++uOU7vO3aNx5k0Is2BwS2pT2XRPTLAQbmUtEbA8hFvEWD+jwKR DPsMiqdasoTlA3kuLY9c5WzK0mAv145VKgUo76K/ZQgmRp1b6tCbAOAhu44+4+TzY82L OB3K1OKMtZUnA3DTVk6IgGD/s2+8geu2VxiPkNOX2WBlqIq3KU2xZXpZoA7H++zETBnp gp97Yg/LsVABN3/1/1LaYsPgtERSQvv+V65I0eZbvbgcCOCUPG52Npwf5Dpmsuxxrq+b AQ== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3k99nt9m15-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Oct 2022 04:12:42 +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 29I0f8Zk017332; Tue, 18 Oct 2022 04:12:40 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2175.outbound.protection.outlook.com [104.47.57.175]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3k8hu6qbqn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Oct 2022 04:12:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hK+i0ZnsRFbSJc8P4toSThNJ5v2clJtQIBD23U4baQ/sZ2/DccnnDzoOt4DyhZTTP50HtTlS5gMieTaquKqn6DH7sZOJ9ysuDeYXBXKwpZPL1pk6kFD+kY+cGAo5ov0J91igO6V3Z31BG4siqu9VQR1Flhmybdp4BezmpSOqrcLWFR+csjK1Pm4sK2wAloJ2mWL6/CeW9B2RBQmTXE1rv1WDr4H01rOk5503RQyowEUsHr0N9hm/jhtVmBexR91wGaIoFR1HoASTPvOQeMu8cKgYFCN34yjk0be4dgXQga70eHi2YobJRCCYW21CJYRtcPpBXoh6zIT8PvWifMEMVw== 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=EoPDz7SHNnc8/VHYvCgMBz6YOkq1tx8CVnYdSlXCcqU=; b=OGYp+7zhKgG+vLVbT9KdhebMNmGvGE41XVBperGSVBLK6Pg8HLTQdZ0iI+nSmRzFBEZHv9TFTecryN15e1VzLzV7GHuUMdMYDi6DTyl250On7pzd8aL3dZtUvROh8UP5yo8jXXUeHxBPGyF59Lt9SUgnrS5Iu52TUefz795I1uw9plhg0zW8gdZDPs1P3kY9TaH//erTPlPloitsVUnmov3ITtImQHXuYussUXS/aE89vpYoyGp1+Jr1qx0MfpjOKcswDbfDG1j8eKW1Ea7zCgoH4lzJgxBFESbbBDR7POYXWAEYtnH130RF5mOHtF0Yy49MbAz6JMhaCQyPoLCbhw== 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=EoPDz7SHNnc8/VHYvCgMBz6YOkq1tx8CVnYdSlXCcqU=; b=C5dXo/9XI13RmzMj9jPy0Vtjehopd3kMjuMLMHNtboJLvuUIyNtG5mglIFTb3xDWD8LzpyjG9yP3kDowup3+K43AkFZMcRn+tGU0k8RIU1RqnivJWEdDJyxjS8aqqmqqH3P8cOwhX/+J7pyWAyREtl25e67bFTPDlDKKWlFubDE= Received: from CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) by PH0PR10MB4567.namprd10.prod.outlook.com (2603:10b6:510:33::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5723.30; Tue, 18 Oct 2022 04:12:38 +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; Tue, 18 Oct 2022 04:12:38 +0000 From: Stephen Brennan To: Jan Kara , Alexander Viro Cc: Amir Goldstein , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Stephen Brennan Subject: [PATCH 2/2] fsnotify: allow sleepable child flag update Date: Mon, 17 Oct 2022 21:12:33 -0700 Message-Id: <20221018041233.376977-3-stephen.s.brennan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221018041233.376977-1-stephen.s.brennan@oracle.com> References: <20221013222719.277923-1-stephen.s.brennan@oracle.com> <20221018041233.376977-1-stephen.s.brennan@oracle.com> X-ClientProxiedBy: BYAPR21CA0014.namprd21.prod.outlook.com (2603:10b6:a03:114::24) To CH2PR10MB4166.namprd10.prod.outlook.com (2603:10b6:610:78::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4166:EE_|PH0PR10MB4567:EE_ X-MS-Office365-Filtering-Correlation-Id: fd3df2a1-5cbc-46e3-707e-08dab0befe65 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: O1+BqB8oC31fmNidx5OzgHLO99RebofHeJq6zpAUb5WR/Kl/yRQMbbuImkJo31Q4PfPlJc7HuAGiQEWD1EHJGHVElzA2jHS5QEgX3v2eZBcxGZOrjVQncd506ahaww02Yb4onxMpyFKim/NyNqmr9/j7zLaGAvwWUSKbSL0H1sOUZJCkdE/5Nbh5N4SJlpIYrkZUNCY9wNTijWyqoTXfxphCGqUSWwoZVhWfebFXe7EVdAQj6gIVkQe8Dhij99aVzhAmeF/5C5STynGiRjgWTovDSbmdppXaYtUQwCZaXzAbm8Ec0PpwOFkx/uuYjXLapWU8ewORV3Ga/A+T7qgAIlINJ/KjCOlMk1LN6EBQ5umjWSsKwrpnBIZeuKTdjR5qETjSAf2CcsSIBytr+1jZN7OKU7E8B6UtpVcQTZaPGIjz38+UczCRaMNG6qH6cL81iIWAD/Xo+E1sWpA9Seomb/pBErOcFUz89yoKQEiEF9KxFG1x7kYNXbQouFkii2ZvlYLI0LXiMR1W0Rz3qpz2DhzRv/UB0rbYGubsduFu7UqRgt1bb0ob14cQg/+eNwhubm9cXx1cnPZCMdokAgURTWqXZccYgRnsOejQP61SYGI6V4ulGU9xDTwsOJxNdhI6FJNQjLrhgqdS0ZY4w2wDngsdafrrYlZBJWEYlu3eI+I17HKe56I3yYIFzZyhLSRXIkALN12m9BXqhX8u4QJtIA== 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)(396003)(346002)(376002)(366004)(39860400002)(451199015)(54906003)(110136005)(66476007)(66556008)(4326008)(66946007)(8676002)(107886003)(6666004)(2616005)(316002)(1076003)(186003)(2906002)(103116003)(86362001)(83380400001)(41300700001)(5660300002)(6506007)(36756003)(26005)(8936002)(6486002)(478600001)(6512007)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 4q7AH11isz7V+EpsANO9k2KKIcjE3+hIgXl0z3XlMySTDdqIWEFke9l9bvifROpYWxZgvia0kF3tWXofc0/A66FzxneDzeLiXmXnVXi+16KH7EVNXmBSeh+4DnCkcbVd4MMlL64EWf5JOBceyQkGVKFo6ol1lZ9XDytol8BnB1euKzFjN2VfE9crGJyEOkkzBkSZ0y2pyhDoKV2J4bYk72lNdbJoRMBKOmic4FSUc7du8zsg3yWEf2ipHUiMTDYmwjA1l0Danuq1yzRYNauBSs6b1HBcCgHU4bfFFk2LANp66Z6SlXEyWcUYP7aYP57tBCCwF6TcK/aaubkvFnIaZIPcm8WLWphAXQgxVqzuVwO3Cr6r8kMRkp1VrySPv/bXo6Fx0RCns16ljkFgg8Mlvm3S8FoaxbCfSQg//5EieFKUWKJ9R7AXD9hTkx6ZXFuM7zovG3qamdjeHsr2Ig4TPyZEItACrqKC3DpXjT+JGTY+bjTGQZiTNi1qm/7S+jEH0Uu6fH3pf2GKrlTQ7xJP6GSnopbWbq98izBjBkzktfjI7hivJsWPaBrScsyJ82l2u1RfBTs8k2loIHDfbGpNpi1mXGGR9nb5DCIGARaEdJed0LYi9tt5B7La603ohnFGE/bh/unITRy3LtDIaaXhAdJEPtuR7H8u/ElxsmUy5ThHoJq6c4/XuPj/LzYV6A2NlVW10ZDWJBMhbgoOzbNx74sDjS4hVD7Ve4rUpL7vD41kzQXobAK135THkXRc7ynRPEpvcWZ2E99ZedQzZX9x+7NEqqUs9+rxBt0R+LRur/czMBRXu0gagWSbISqvI/nXnDwMVGHzGr8R1xhRhoSc7ywD0z00ZqzI0d4fvF4FUr0WpCWAU9GA5NpmtPPviFIkWbHu/94p/MQTW4ICfS19CxIK8rqH/S/lVG3mSoNrr/GEVToAM8swVTDf6exF1oi085ao84Nl5bZ82Ef0+IYM1x/0P5s63xZwd8CCSrYPgISBpQGIG+uojjSroDHTDT9f3GmVYEYnEpsorzrf1M2HJewBErUEKEHOUnr/AYiGrErSXREHYulr01LBRttcd0J/MNJiLMezkdlqKzIrxe88/S9h6HRsYkJ4/2ZGlk+nC/WNvScWxxuvQvdOinfYBvZGsWVdGJ4u0FnKqiQFmzVXI503vGf3aKlfnBrHyn4uaWpV+OOm7kS/9e4lKTHsM9VSUy0CVvTJq4E9tKpAROiArKnyhukd79i5mC49lC1gJri9igv6X9/oT+2w9m1BLwF4l3wipwxeg5xhzUlpdb3CSIOFH5hijbZTmwgVcJsKYh70MntPKS39UkqIACckV+zZ1v+bXjdmj+UyMxQ2130Yxk7oL2thbJWQUwtMb0NGxrKh7PT/vRie5qIv1eT+64GPAbdmlb9s9uaqJoW8ym9qzErcATneiuRUdVTDWELoh0NfayCSSsBHnxT5MgjR9rte+Q0bkk1B1x2HrmdsKqedcgyVb/8wZqJ+n+sYJcGKk2OTKAXy2DHRttw6R4gopeQEcajVRN501sdkdrXrYzzTs3AmuvfX16IgKVVhGKkI3+/JgOrwArWc5oW7ZVnyEdtwRs9dC9nWQ3poE1+Jc24+UA== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: fd3df2a1-5cbc-46e3-707e-08dab0befe65 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4166.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Oct 2022 04:12:38.5943 (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: F49K0fWqHl24ld/fdh0dU6qqjrjQITogcytWSjwwsrDg+3VxgLy8JR/m6zVx2qFzzH9c2xDrPrZ3a34OgS7dLyQ/wTw+1rcthqmVDKqQ+7g= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB4567 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-17_13,2022-10-17_02,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-2210180022 X-Proofpoint-ORIG-GUID: uHVjTIAz4b_Cv31BNQlUmH9Eq0K5Ig-N X-Proofpoint-GUID: uHVjTIAz4b_Cv31BNQlUmH9Eq0K5Ig-N X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746998715860817796?= X-GMAIL-MSGID: =?utf-8?q?1746998715860817796?= 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 | 72 ++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index e887a195983b..499b19272b32 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -102,10 +102,13 @@ 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_child_dentry_flags(struct inode *inode) { - struct dentry *alias; + struct dentry *child, *alias, *last_ref = NULL; + struct list_head *p; int watched; if (!S_ISDIR(inode->i_mode)) @@ -114,30 +117,55 @@ 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; + alias = d_find_any_alias(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); + p = alias->d_subdirs.next; - 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); + while (p != &alias->d_subdirs) { + child = list_entry(p, struct dentry, 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); + if (last_ref) + dput(last_ref); + last_ref = child; + cond_resched(); + spin_lock(&alias->d_lock); } - spin_unlock(&alias->d_lock); + p = p->next; + + 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); + if (last_ref) + dput(last_ref); } /* Are inode/sb/mount interested in parent and name info with this event? */