From patchwork Thu Jul 14 00:22:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Imran Khan X-Patchwork-Id: 12917208 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 C896FCCA479 for ; Thu, 14 Jul 2022 00:22:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231214AbiGNAWr (ORCPT ); Wed, 13 Jul 2022 20:22:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229806AbiGNAWp (ORCPT ); Wed, 13 Jul 2022 20:22:45 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96B0D15FF8; Wed, 13 Jul 2022 17:22:44 -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 26DNQHpj006654; Thu, 14 Jul 2022 00:22:40 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-2021-07-09; bh=wtPv/41bUIsMzjDg6c9NQmzcUAxI1Qg9wBfkypVrCOY=; b=NphYbOYV//2yIWYNt3CdY7y1kYPyh2u21LkW1N/Etkqz8EA6IMa7cfXZvOi2tX3Qgqr0 JIxfei7xltu2f+z3BK7QLl/sqYgWUw2aDm7/hA3mODZYAU4D2S0d7KMNGSTqMtLEpkC3 ckYJFwzHEAb7Y50x3w4TY8AWdzh1C19jZzHVmuMFQc2yV4zkBvVnau36iEPzD8UeBuCf K0F3W11nuuqAlSgBPLT470xk0Ajkwmsc391tCL3OOA5lfTqEMS+be+zt/f/unFFkNcsM vw884vQ8annHDF7qFtp98xtmyfvoShQf+AWrObiFbNBOZFvM1BsBdTlq9UfwMan7ZcdD HQ== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h727sm35v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Jul 2022 00:22:40 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 26E0FcD7023320; Thu, 14 Jul 2022 00:22:39 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2174.outbound.protection.outlook.com [104.47.73.174]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3h7045pes3-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Jul 2022 00:22:39 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aPO7MR3YrhM9fxnFdJd7Ru6JP1nhCdFy7BqO4Hbz8J4E7rcUcqGJmbJwDY+nwb4EozJ4nLZ/dEcalf3XkWJteDhPsRXf/Dqpjy1KJuM0835OIj8DTLadcaiJwGgKPGfghw+sfGk70HF3lNzDxE2UYBgDvVcOA22zWyUbgaEaib9uSFEpkbVaDpTMsZAzwQGZDftikmeWBeWmlBBZ2+lMRSVOD3KJz135iCAngTKwdXZ2t4B8KzQiTXy2qVbU5IRFnRi5t8lBsOT4tiY6aNvMoivHVx+EIHubJ1mw5eTHCQfcPuKlSRKSVfM9M0wyRUTBtcakfnwCiXKjDAHLsvX+DA== 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=wtPv/41bUIsMzjDg6c9NQmzcUAxI1Qg9wBfkypVrCOY=; b=iSDSBp4FabAw4wC4p9W2+hAMtxbwRO40fqcUVj8K5HlYe2vM3LU5yJt+JFCMFUV4ieKLKD0jpZ09YltqVqErADzj+Cerrp9K3jGMwp174oIKWNDpVIp6KSMLM05V4mhWmoM1mNezWX8Ndns+opENE/sUnNo0basAhkbnYns0ZPg7u8vX+thIIYKm1GpY8n0DAu+/EiwYp+UjG84ut3HQC8YuKh2KFH9svPxKv2GQjYwwP4vJPlQAiCsb1uh+BbqjMihfQuM7FqJZD7QMQQNw49CvUoBVEd8q3TKAWCLbsMFRJ6ohaCum7PMcJrQ8KA+cWwjMQvdSP3HX6DI3gIqQ0Q== 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=wtPv/41bUIsMzjDg6c9NQmzcUAxI1Qg9wBfkypVrCOY=; b=bFqglzIfDBSPxpKjI3gmueW4EHCdlYhAJnak5TjeS/yrWA+/a2E51mDYxKupZMJW5olmuhGUDdh1t6k8Ch+wu2WzUn7uL37j3IPDQUnKY+2pSFmF1xuG90ow5J3fTGVZlumA/neHvu3khR5kK8QayfjHFKX9KsS95tVXo3k7puI= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by BY5PR10MB3986.namprd10.prod.outlook.com (2603:10b6:a03:1fb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.12; Thu, 14 Jul 2022 00:22:38 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::dd9d:e9dd:d864:524c]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::dd9d:e9dd:d864:524c%7]) with mapi id 15.20.5438.013; Thu, 14 Jul 2022 00:22:38 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 1/5] kernfs: Use a per-fs rwsem to protect per-fs list of kernfs_super_info. Date: Thu, 14 Jul 2022 10:22:20 +1000 Message-Id: <20220714002224.3641629-2-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220714002224.3641629-1-imran.f.khan@oracle.com> References: <20220714002224.3641629-1-imran.f.khan@oracle.com> X-ClientProxiedBy: SYCP282CA0011.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:80::23) To CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 488e2ba5-c316-4262-eef7-08da652ef4fe X-MS-TrafficTypeDiagnostic: BY5PR10MB3986:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XDOzEK7Vgewh+9L8N8HhZoKqG2yZoae7BEn0GEK+NToZE6/hriXgYITYoVW4WAlijXyG5AyVAU8t/YwbXybwuBXDzgIDbk66gFJ2L4p9J7dIE8CGqs9YgV/isC08pVoEqzq4QUi+QEo1c2fksAhI83cAymfLbWqKxJYydfIkxDvzioULYxgqqf1lmGn8LVhpjiHiCiIeKZbdl3/E5F4Pxsg+bHo/6Tl7WMTwcR5E4yeZmhD6n24+n7on0ojd5qa+/i13mnB9WLsL/EK2d5olED8o3K4TvTmobV445j4NlRtPHzzNxHoVhJz/OA4WF4qQwsQT70o7fhaViCgrIdwUJVJE8MkksutYnY2ZAYEbPVEiOK99x3nnyicSlaCGiIQ8DgQYhHQIol59VsGAYihQVE/U9xvdb1Xjbe7HRjktgq5j42lu53Z+1Hociuj8T3xkuNicqcYciBsW2EfMSbNyJrXJLOUdik3RlXdM4TfjwK+RTjmm08X3uCmkXNi2L6N2VgWM7ylLMvyVRywGllIqCB4kuENrL0y4CjQm1XFKwNazQfBaFFqnAcvrf6Y1NDzHXpfCHYGO1GFN/1O2HnfyKV3Jr0NVERk6ui6UPSxn6JoAv9YQvFurM9Hy2KvjLHfpeHodhjZlINTBGR6Z03Bc8MzGvIuySOnDOQh2O4vIXql213fJ5EgK/Di7MpzSTqC6X5WXiSe1C+xkPBsFqVj1llARtyvpBoU3b3blFJrckUZIbv8+qn6yEn79TWopRDfKypVwEFNJ3BJrS7Dc8VeCdMnpksGhrYUGjg7N8jZEyZ9fXZWQq1g1gFWwSjhzF/Wz X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4468.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(366004)(346002)(39860400002)(136003)(396003)(376002)(26005)(478600001)(6666004)(52116002)(6506007)(6486002)(6512007)(66946007)(41300700001)(66476007)(8676002)(66556008)(86362001)(316002)(4326008)(83380400001)(38350700002)(1076003)(38100700002)(2616005)(186003)(36756003)(8936002)(5660300002)(103116003)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: isN7T9dq8hJtdco9GktbeWTPJChnYRLxIyfQGung5uQT0Z2QpG8WCFikCtdgUhw6ovMv3ziL+DUHsxgdu68OyXwhICx5rNF51CR3eLAHtvX5bJt72IYfW9kpWEn83us3hF2rRQLXLquGtSF5U0kO27/gbF0sTUoauGxUWdymjrzg4+GFqFYGEx53nYa9h/B4GDD2lKpmDYk4CO9lwP9wU2RX2fVyj4qMk5PrMHRMsIoUAubIIYz9kQwbST2KHSivdnMN8Cwb2AWdiR2QKSj85pr5NYQkAy/FxPTqd4BFCelD4Vp68zMwDs+A8W/Wq/WrZ8q7snGotx8lf0BfeB4dmIHgjmNzhwNc3eYxVSLkC5Fpt/ALFAc+8oTGF7fMlCv0wiylcpG7SYjfEfXJUQqW3ltDu1UJA0owqZBiTu9iLpUpiFZWeaJtZiDq0HnfimaNeEGvJVwV171/qfDeFRpCJBTChmzzmF65jpSCu0shBZMgqtF2VKGQs6wk+HQijC+MSuzvAsCKKtzT+kdh2J1flzQLMZmkGoNeB/j3yNlZcntJslsdl9b9GVraD2KYYEFUJYfvY+bW/0JoDK6szGg08HcuQmJJQAgRql1VzWLFh2rxlRu7qrxp7BQzRjY2Yhzv01dUJluuYhfMT3WYHKCwsnY4SpzummtJNJwHyo8xgoRKR1uXBtqIRUhGi0EfKHeJA8hfCnRxSXfJjNEFWWF6VBfgEK+l7RCa7A5oRBwyuUAXjQ/MamVu+CwN7lwIrjuhPc1t+YlasKE/2/v7ODItOlPyNSqBdmfAGu2uRjRZlhntTDCa7AzVVy8txzOilUzGLxHvOzCP3pllla+W5kncXIuITrKXH2QoHP59FzwWz/8eEArlK+DuG9cfhPCPFFnA5M0PMxKNO0PvHiWQaVjuWhWC18MosHYWKyg7HOPC16oKtSfB4lRcGtLe3XfoHl6RgRwESSnga6GnKOB5ex/targKAS94cN9Fze2k5iaG/9DgKUzLET0pQ5I9wiPfuKGHqB7Q/H25ocsUWdnzZv5WkYL7RRpiIfuYXnml5yyFu5Z4vLTvxTqhQxW5082KNGCbajcHnBn42ZIVe+sLMZh1SbBEQCmBIe7YtzzJz3qlJuMvv4vx4P3ca7wYmnpvk15eshZX+dj8TrTw1gmRpFn4hArDnjfV55vbrLhxTpZ6d8Itd16ZFaBXyTB+Db3X0ybriR+gRuR3twxR0eNp+EeAWHG7z7rVhyCqg/lUFtAERNH/cgReYrn+r6rqVlgPnoU4RSLZzWL3DWcRkIC7lPIbaTGFc6KrUTlr8BHXi/OyUt2CIYYJ/Rr5Bl7HiztMHB5Lkq9w3nG1hs6tw4S2Ub1eXrOLpRyRJqOK00zgKX+MlbwG5S9qn4yVMgosok8uuHM6lUvRx+n9ZjsWDK9fIjcb0w4MR4wbyNtree+Ly4WLuZrkTyMYSgt4UKFbxUf/+sLD24pnbT78/g+AEvPRiQt9givP+VBaj3dn3a9T9hfVhjvdkaeU52CEXP/sApsshOyq+SB0XghTa41U4WPC1ROwzpk8S50SYjwpffaOOHqHLkF/GnK0txQO62XbefX3Av+v6unc1AflaPJ/3dxIcWVXwA== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 488e2ba5-c316-4262-eef7-08da652ef4fe X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jul 2022 00:22:38.1145 (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: QLxelA0cFN4u0kcLv0wY8ivxKaPa86lIVQ0a/5z/YNe58tpLzNCfLynohplSar/2R3jVGb9KNkY0rkheN95pHQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR10MB3986 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-13_13:2022-07-13,2022-07-13 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 malwarescore=0 spamscore=0 adultscore=0 suspectscore=0 bulkscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2207140000 X-Proofpoint-ORIG-GUID: 9lZeNpbxf_VgxmlarJpbgdx32stvyYPx X-Proofpoint-GUID: 9lZeNpbxf_VgxmlarJpbgdx32stvyYPx Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Right now per-fs kernfs_rwsem protects list of kernfs_super_info instances for a kernfs_root. Since kernfs_rwsem is used to synchronize several other operations across kernfs and since most of these operations don't impact kernfs_super_info, we can use a separate per-fs rwsem to synchronize access to list of kernfs_super_info. This helps in reducing contention around kernfs_rwsem and also allows operations that change/access list of kernfs_super_info to proceed without contending for kernfs_rwsem. Signed-off-by: Imran Khan --- fs/kernfs/dir.c | 1 + fs/kernfs/file.c | 2 ++ fs/kernfs/kernfs-internal.h | 1 + fs/kernfs/mount.c | 8 ++++---- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 1cc88ba6de907..45e1882bd51fb 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -924,6 +924,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops, idr_init(&root->ino_idr); init_rwsem(&root->kernfs_rwsem); INIT_LIST_HEAD(&root->supers); + init_rwsem(&root->supers_rwsem); /* * On 64bit ino setups, id is ino. On 32bit, low 32bits are ino. diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index baff4b1d40c76..77c3e34a6c7da 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -927,6 +927,7 @@ static void kernfs_notify_workfn(struct work_struct *work) /* kick fsnotify */ down_write(&root->kernfs_rwsem); + down_write(&root->supers_rwsem); list_for_each_entry(info, &kernfs_root(kn)->supers, node) { struct kernfs_node *parent; struct inode *p_inode = NULL; @@ -962,6 +963,7 @@ static void kernfs_notify_workfn(struct work_struct *work) iput(inode); } + up_write(&root->supers_rwsem); up_write(&root->kernfs_rwsem); kernfs_put(kn); diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index 3ae214d02d441..3cd17c100d10e 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -47,6 +47,7 @@ struct kernfs_root { wait_queue_head_t deactivate_waitq; struct rw_semaphore kernfs_rwsem; + struct rw_semaphore supers_rwsem; }; /* +1 to avoid triggering overflow warning when negating it */ diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index d0859f72d2d64..d2be1c3047157 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -347,9 +347,9 @@ int kernfs_get_tree(struct fs_context *fc) } sb->s_flags |= SB_ACTIVE; - down_write(&root->kernfs_rwsem); + down_write(&root->supers_rwsem); list_add(&info->node, &info->root->supers); - up_write(&root->kernfs_rwsem); + up_write(&root->supers_rwsem); } fc->root = dget(sb->s_root); @@ -376,9 +376,9 @@ void kernfs_kill_sb(struct super_block *sb) struct kernfs_super_info *info = kernfs_info(sb); struct kernfs_root *root = info->root; - down_write(&root->kernfs_rwsem); + down_write(&root->supers_rwsem); list_del(&info->node); - up_write(&root->kernfs_rwsem); + up_write(&root->supers_rwsem); /* * Remove the superblock from fs_supers/s_instances From patchwork Thu Jul 14 00:22:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Imran Khan X-Patchwork-Id: 12917209 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 15FE7C433EF for ; Thu, 14 Jul 2022 00:22:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231621AbiGNAWx (ORCPT ); Wed, 13 Jul 2022 20:22:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231217AbiGNAWr (ORCPT ); Wed, 13 Jul 2022 20:22:47 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D92C115FC3; Wed, 13 Jul 2022 17:22: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 26DNwvvL021458; Thu, 14 Jul 2022 00:22:43 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-2021-07-09; bh=skvsG77Zdam6PW6q5LdzG8SO7g9xvzI4q6s3C0hlbLQ=; b=nkThVIw4SA6tyXjKuwiNWX46ncUReNCYeGnU1ijZnlk4LfSa4nhw706RQSz4StiUx+ZF jU58afiILEvSVRD3HGWovYKVHCq0yZ/KwNW7IM0ES0cs/0AdKuqsLex7NvGDeRZp4StZ NqlI3fnAGLbsboSNw78xoDciIQxiBotft33cHbDm59QRMq777KcdJJl8XcetYFtBGcTU gdSms6jU+VGVMngCggbRR5irEe48LXIRZUwXF7T74WkSzYjknMqfgj0PAqyEMEDmF3VE y+FtmoBQGsO4EFdsSnwXU9IpUtpBtj3aaK3+K/I6NbTDfMgP5iIHyq48+Goy7rp+GaMp DA== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h71rg3j6b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Jul 2022 00:22:43 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 26E0FPH7024772; Thu, 14 Jul 2022 00:22:42 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2045.outbound.protection.outlook.com [104.47.66.45]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3h7045nsut-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Jul 2022 00:22:42 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nNZ0SNb0z/L69ZdVevTLC/IUcmWyiz/XSBbB9brcnn05sN7qjnoeB9XjXzMVTPLc3S+x2QJCl0Tvr2fsHof0FyPXaTQ9k7XOjMb/Q/QwaG3ms28VdY7W9RkVU3iONVs1OmNWbo2aT9k5W6p/tMK9jcKIqjnZf5JM1nE5QduShypiaqD0pk2CxaO9xAT+86HmCAFFIODLmWeI9W6jq2y1leKemxPNs5PrQPCIEUBNJ5w+lCaOIoi7x9KDsLbP3NdmBzBEUqTsUmrbkiAiUfQ98z50FyvJF5pIBKOFP5heoOdPtYzsDiGevGA/2uOinXHWuIfYXE+uZH5tCGoT0cIaZg== 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=skvsG77Zdam6PW6q5LdzG8SO7g9xvzI4q6s3C0hlbLQ=; b=oGUgo4GnsGeNqwkHHFSw+SiPRr9/5bAiOZpGrMVVN1VEkTGop2viLJc0Cy6Bu1xWrh/rFRaI8Hi3+Pf0H0AX4XuNPBPJ3s8/IgFml3D9BnvqAli4vm6wsYzNht9vI+fGZo1HIN2u5WpV0weYkL9c4vgZliDenmfxuyZax0aykn3J0IDlZOeUXPm/sGld/vIYyFgzShYAnZynK7H3vvG2XkICxJs4/P0oQxKkzS7Z0NpZka3Oo5I6bMm6lmvuoYomTmkSvGm1BOLU0Yw0vMl2ixcXxTklrE8W5nZ54Ofyi6j8U9XhLJf7bCItR9WcfLNawjI+GSz+zT9OoN+nk5SqHw== 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=skvsG77Zdam6PW6q5LdzG8SO7g9xvzI4q6s3C0hlbLQ=; b=RI3amCZoS1d6jWPRc0MLGmFrhz1aY07menw7V9heufNSxEPgzqZ2RrOoL8e/BSxdMr7cqrShi8+/B0mCqBNwWAQZYEFLy5OATNxSOydQ7o3nYbQlKArKaCnwsSamsvsLPVJ9sbaHgBFpoAKL7uBCgPGpG6mpdGlit3ssznCdtfI= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by BY5PR10MB3986.namprd10.prod.outlook.com (2603:10b6:a03:1fb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.12; Thu, 14 Jul 2022 00:22:40 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::dd9d:e9dd:d864:524c]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::dd9d:e9dd:d864:524c%7]) with mapi id 15.20.5438.013; Thu, 14 Jul 2022 00:22:39 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 2/5] kernfs: Change kernfs_rename_lock into a read-write lock. Date: Thu, 14 Jul 2022 10:22:21 +1000 Message-Id: <20220714002224.3641629-3-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220714002224.3641629-1-imran.f.khan@oracle.com> References: <20220714002224.3641629-1-imran.f.khan@oracle.com> X-ClientProxiedBy: SYCP282CA0011.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:80::23) To CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 45d57b91-0e43-4a4c-38df-08da652ef618 X-MS-TrafficTypeDiagnostic: BY5PR10MB3986:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZEaBzf8LJUC6zD4VOMFYqjoTi7L7o7SIZV2dZF+D8elwdQuc853ptQVYpJ7GF4PZPQdd2+IX+D+XWUTERYIOTdIEj7sq/Vi+4aBjGM3LIz+gK67CwZnMZ4nzQs39TzoPVIPBUoqBFaCKIYAdxzOFzuM6FJoNntxz9gOlV6k8VnLoWW3jejTk50jvsWljqNK49B7lHmftr9mqtiIFiaRog4xUDiD8hIYnDuwXn8FGP4t+TZjo0up7JCKUjBLqCU1D9yuZbVMyfigN5p7KBXhsDgBaEwshlOLx+SRT8hZgL6t9A5i8gbPZK/ZNCHpqe8AtnHdsubPH4XGoEDpEslUQiB7C1fGIGCVdVV5zrFIbxcjQUTJi6tItKUOcbmcKrteQ6TymL3JijLO3/Y3Yf42JPkJlF/H8bb0Znc1wbAqYCi0aUgoSX2sDi0IfDa4y9Bn/fFtWSOxANinEOnicOxPaS5upluC8jTpsD9AHaZFMcIaRJPVgeoE6d8bhd4U4nr4XXEQFKYnogB/4Sp5prJNyhPsLKQIpFOVSoihR7wTP17D9On+iKyUPklA+9Q1FJI0gc5CK3pYYICpF06NBq8/0JCTynQ1sQ7wzwko0YyVrGJATqpiZ5vVn5AGlz+LIE87aCW+T6B1OlQaqxi/5AktGHBSCl1tRhqo4q9i07qLNOag8r7GLoEuM91UNSDH+x4XaBU+bSl2m119RHh6UAJEaQstoLzz4q+Lo4obNrHnZ9Dvb+xI8nmDREvTkICC6BnX2aj2WNbW3SIDB+KcB4/p7IpSUCCQntDG2FP3ztyxg4l287TQtavF7vjZDZuEb2kdY X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4468.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(366004)(346002)(39860400002)(136003)(396003)(376002)(26005)(478600001)(6666004)(52116002)(6506007)(6486002)(6512007)(66946007)(41300700001)(66476007)(8676002)(66556008)(86362001)(316002)(4326008)(83380400001)(38350700002)(1076003)(38100700002)(2616005)(186003)(36756003)(8936002)(5660300002)(103116003)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: uxMVFj72Gyl43NB5vf3rT4E/hb9wEHvHFWyWP1Qb076yUVS3y6C7h45NCY+eQpLtUGy9KNVtFusrsP3bYrM82ogZsishUZv+QWRn3MOr5opaUQe/kicmC5Tf2xpTiBRWFWnBgyU3Z+gVIaxY/dq8KQqsFHzDWDeXT4DdlNPV79G1i1qYBwGF0P0rLX/Xbxfwl+zTx61QqAHf1pJhLH1kyIm6l0rikU3TvogU982kqlX1ueTGxEDso9WGjsCEgGS+8mvCKj5XcPCM2Ji5iVB08T/FAKnWVB9qPSB7VP0mbnoAiOPMu+aQjyFUc305DfU1LiO1P+cyx8os+hTpKF+BCAU+5vkGfEV47rH4qc5ITwsPinaJX+d84kTF7Z3bD4rTovbvGYz7kSjyaFoVo8UBgkx6nU5+5Hh3anO6e6N1T+VjnItBPEFv3CuOefYHdizwznexQPShnjTFvLuHtrjHyL7JD4MNxje8V56BaDe5fNC77epBD5VY0XLLUuUiW4MOn3CvPcBXmYsIuLh3Fhm52aLRxqCGlrA97VjpLpyBmD9QImLJ8sFdAEU4ZREk1Ppq+4G+RxD3rF21LEaye5mSoead6iw5DiokCLsX7AXCiKvW9LScpAdCYgUeLgBxOS0EAd0LC9wTiittLPoV59JW9CQx7k7IfKJL49imeKe05Sm9DSA4PMm8mPKbtUcSKDUrcxAi6e8y56j3Oi5Sd0j//jIvWJL0MlWxISBPpVQZZKfb0RaYptlBQXD4LnUOKuo4yWHcLg0yRu86sooDrKpRtpaAXAOVGRacwRob6tjvOCQNGO4Om57xyRlSv7LlIt3yALPn6em59bm+YIP1lyYj49Inq05CucfUtxu6g8VqhndhMAzYBS8uuZ/WDdCiVsOf/Itzb4qnehSH8JwZ4ppUqfBEGe/5O7EaoeMFiC4pSudh+2ERTMVxz6BbmgihWmIbyTHCa/D6sTA6INgQHriHKtddAGc4kID1WPgBZJMqKIDQSH32bHjptNXOGh+b2Iyrr518PTSxNJ7j2X+KhPTFYrOrtBa6FvA+6k1hgASuV8rXtHQRjBuHWTui8q6/Z8ITkegUcsvvnKJwK0SC9HLD3YoYm0oPLPL3xGBxKgYnc7fZsmfdgb7Bu7IYyBdjd+nMIBm1kULvad2gWjw14gf3iKvG3Hrah4UF4En4AYNW/eDBQspYdAUgMpuGn/Hb0/r7ylYEyKsEUmER9XolG4pxi9WfrDmyAJy6ylMo/jQBf00/QxvmbH3E8MOwKfIp6W/ovq6me0I6KDc7piiUdJimRLWq7cbXJa85X20dSazk9uNKjEah5upMCfks/lamDd+jbWD1jeKNcJ3kW2Owx9Xg/lmUf/2dCwy9UqgI3X7fU4F2TMvO7gEfgZ8d4av++x+U8yIa3nPmKIQqnsUbnaIN1wSMhV4PfUoJzD1iFZ/2FkE8t1WYn0QwevmEiIC+KyCq36iMLw7j+RZAU4G7D7Uu0AN/4SyR1lMmZyDh5S+KbUmsRWKx6iNUh8ihrSUgx5W+EU746dc2Y8IJrXNzqFcXmE71xYmyYgh8ip3pChy1TGIm+/OoBV+pwTKgWungu0fcnaFQrqbVwrRji4GuUZlNjw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 45d57b91-0e43-4a4c-38df-08da652ef618 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jul 2022 00:22:39.9123 (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: 6l3IC+0DLzLNKqCrXVVyPxSmA3z4nHnD31r4WJx1h+FBoAk1ryuaE0itbqqqJmGeZXYj4GjSoSjU7QvPwPOfmg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR10MB3986 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-13_13:2022-07-13,2022-07-13 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 phishscore=0 mlxscore=0 suspectscore=0 adultscore=0 spamscore=0 mlxlogscore=999 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2207140000 X-Proofpoint-GUID: Dcip7RxgpWO6njo_gpdYMKEHTCxIcNnq X-Proofpoint-ORIG-GUID: Dcip7RxgpWO6njo_gpdYMKEHTCxIcNnq Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org kernfs_rename_lock protects a node's ->parent and thus kernfs topology. Thus it can be used in cases that rely on a stable kernfs topology. Change it to a read-write lock for better scalability. Suggested by: Al Viro Signed-off-by: Imran Khan --- fs/kernfs/dir.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 45e1882bd51fb..d2a0b4acd0733 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -17,7 +17,7 @@ #include "kernfs-internal.h" -static DEFINE_SPINLOCK(kernfs_rename_lock); /* kn->parent and ->name */ +static DEFINE_RWLOCK(kernfs_rename_lock); /* kn->parent and ->name */ /* * Don't use rename_lock to piggy back on pr_cont_buf. We don't want to * call pr_cont() while holding rename_lock. Because sometimes pr_cont() @@ -192,9 +192,9 @@ int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen) unsigned long flags; int ret; - spin_lock_irqsave(&kernfs_rename_lock, flags); + read_lock_irqsave(&kernfs_rename_lock, flags); ret = kernfs_name_locked(kn, buf, buflen); - spin_unlock_irqrestore(&kernfs_rename_lock, flags); + read_unlock_irqrestore(&kernfs_rename_lock, flags); return ret; } @@ -220,9 +220,9 @@ int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from, unsigned long flags; int ret; - spin_lock_irqsave(&kernfs_rename_lock, flags); + read_lock_irqsave(&kernfs_rename_lock, flags); ret = kernfs_path_from_node_locked(to, from, buf, buflen); - spin_unlock_irqrestore(&kernfs_rename_lock, flags); + read_unlock_irqrestore(&kernfs_rename_lock, flags); return ret; } EXPORT_SYMBOL_GPL(kernfs_path_from_node); @@ -288,10 +288,10 @@ struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn) struct kernfs_node *parent; unsigned long flags; - spin_lock_irqsave(&kernfs_rename_lock, flags); + read_lock_irqsave(&kernfs_rename_lock, flags); parent = kn->parent; kernfs_get(parent); - spin_unlock_irqrestore(&kernfs_rename_lock, flags); + read_unlock_irqrestore(&kernfs_rename_lock, flags); return parent; } @@ -1650,7 +1650,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, kernfs_get(new_parent); /* rename_lock protects ->parent and ->name accessors */ - spin_lock_irq(&kernfs_rename_lock); + write_lock_irq(&kernfs_rename_lock); old_parent = kn->parent; kn->parent = new_parent; @@ -1661,7 +1661,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, kn->name = new_name; } - spin_unlock_irq(&kernfs_rename_lock); + write_unlock_irq(&kernfs_rename_lock); kn->hash = kernfs_name_hash(kn->name, kn->ns); kernfs_link_sibling(kn); From patchwork Thu Jul 14 00:22:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Imran Khan X-Patchwork-Id: 12917210 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 CDE1EC433EF for ; Thu, 14 Jul 2022 00:23:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231956AbiGNAXK (ORCPT ); Wed, 13 Jul 2022 20:23:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231342AbiGNAXH (ORCPT ); Wed, 13 Jul 2022 20:23:07 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8F1D17062; Wed, 13 Jul 2022 17:22:49 -0700 (PDT) Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 26DNfw2c023362; Thu, 14 Jul 2022 00:22: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-2021-07-09; bh=fA77Qcm8rRzG9dbv7FNVc6h8ApUU3OW+4w8rD4YBgIg=; b=qvIHurcB8LuYWiiotIut/PqT0+rQeUlwHNwQhHuEQPm8kqkC5RpRdTVmRERFSReqBbeU OH13kilwDp3g4laIYn4ksJYaXVO+77/IS23L10V61OvnYx/YZWlM16YwhxVO84YCC6Og +QtDOb/sWtwmD0RkGf5jIyyRnVH9pvJYwchpMvZ3rchbWm3b0YpwTXhtT1ofMjSCE1te wnOk/zYBCifNJ71XKMb3nHyR4P13EIuUpL/aSIJx5I6Jm8uXuIPmfzA7g+x0ON8eHgz9 OsRTQCGRZeYFmRoT4NQnuuurBgd0Q5ox4ITyt1lnDDSNbbn7j5JbbxmSYBIqF/m3oyqd Ew== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h71scbsnf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Jul 2022 00:22:45 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 26E0FO0f024677; Thu, 14 Jul 2022 00:22:44 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2177.outbound.protection.outlook.com [104.47.73.177]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3h7045nsv4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Jul 2022 00:22:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BUb4P58xceqLx5B9jOm8nnUdC8+P04EclQx08OEcDYFsXONOMVhrUPKhU87xMB+MF85/ooONASm4HmzX/wS6fQI7HwDIJ31ThKu+a2pyAfh/XQWDM5AaqompeR6dBdeR97lErlQHwpvnRoZMksocXAt0BMZgbQseKjU9+C+NrRTy0pvFEysOm4ODfIs29BLHykF2Cp10xBwQFFtNvii7Y7rkK2hB7gpXFkycMLxgFzULL0ga64dVeDj6V+3bbRow9M7OE8LgJW+yB7WKdzUGl/Lbgs3LQONAzjpSFfFb31giJHuVFqZxUDb29N/fVFqNcPtj/+KSMNOBnr0CmbQZmg== 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=fA77Qcm8rRzG9dbv7FNVc6h8ApUU3OW+4w8rD4YBgIg=; b=irZZLIvlWeyv1kSIPDaahNOV/kDCJXV2IZfmd+p5JxdqpguXgyJAYD6Xd+gmLgSbfsLrqcLues0ae87ZJcOi3859o2lxrHfVg2pz1ePmtp4u7EFYLkWwTZSPbaMynZznQZPuhixCa9hlMIQ7+MZmFrk6Wf4DMiWPWt4oZGrVdosAfp3A9OWuODpLwrhJ40FtgJzAZ0nbKFAzrr9O/oNHHejfmMJ/ya/7N3odciLvLIzscSBzcBejYDT1cLn6UbDrD6SpX9zTviAVzcoYf82YCzxGVSPiYIKAkGqni78KQi5u0M7HtxGUIA2aZrzjZwrez1gC2jdnya2pGDgLQ2kRHA== 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=fA77Qcm8rRzG9dbv7FNVc6h8ApUU3OW+4w8rD4YBgIg=; b=rkLZMfkXF9JieF/ipqPToZt1YZ9q5JhMtCeWiiasBjJI2/GxSV8JpuMpDgVHZ6JfLBcNAOmJXzsFR12jI0yzBDUnL4FYO5lrUw5EGNqBiNyBQMbgGUz56YOvED+Vs5rsRtNCPJeiyd9ANRdia8YGfE2uLg5409AMWftR441trnQ= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by BY5PR10MB3986.namprd10.prod.outlook.com (2603:10b6:a03:1fb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.12; Thu, 14 Jul 2022 00:22:41 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::dd9d:e9dd:d864:524c]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::dd9d:e9dd:d864:524c%7]) with mapi id 15.20.5438.013; Thu, 14 Jul 2022 00:22:41 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 3/5] kernfs: Introduce interface to access per-fs rwsem. Date: Thu, 14 Jul 2022 10:22:22 +1000 Message-Id: <20220714002224.3641629-4-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220714002224.3641629-1-imran.f.khan@oracle.com> References: <20220714002224.3641629-1-imran.f.khan@oracle.com> X-ClientProxiedBy: SYCP282CA0011.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:80::23) To CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d181f560-3852-4b6e-b470-08da652ef731 X-MS-TrafficTypeDiagnostic: BY5PR10MB3986:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: meHSPJsYyftWv0nLWdXj38xViaIg0idmQQmTfjfGcn1ZDAh9BAMMRhTEfNYbi+NJParFmouat9OkiFG7s3wMjw8onhXnt9h7MkOW4uTIlN2Z1gTVlQ3lsxQZURKsZnfxnVYQeLEmsuVQVzm7+okQZfXNhegBUgqhpBv3njfGJQ9b9MK9iAxpKjiiuWsNI+B2FeWHwuT5CTlkqKoJRW44kTmGC5CZB/sxrIbnLjJ/Zm736uILvk0t+3EDCUff0GXkVE/RwkWUz8DY9Eg6MroUNV5jTBzybUPpSCz/d/03my1Bd9fPE6O1ALCWtlKCYFih0SrbdIzA9TwQnDuGyd5YsnHH6pzMoIgtmuhMlQ/Sbd8FcLvJg3tbHyvJdlofGZHOzusm6Rjjdlx+AsABED3GIvEhFxEZLkKoBVrkdo7fgyTagjUOuv5X3375RNfeFFDhzYT8AwNuFq7Qc7x+InDvGs8UBK0Nj503kNgeR+rk9VdlwMdfQh+MqAclG8hWKegMnf3KlcTz5SXnmcBc5M8vvmD2L5TU4nJTP4b4pVVg3twmmgQA7lWYhk4GAoE/H/LCMUUgwyZmnA65B/Kg0jXfXsxoYjRMPxGb4Yrcj9DYhYEDF/9xIvs5Iau75MbQ3xtGtXzlTcR277e0EKZlUCFhoJ0dsgwgL68RATV6RUfsVVjHeYdec/1cKWZVhqZENMb9iIIsjHxoLXVkefOOvrRsajphoYRADpiSobglGRJ6QQOndE/76BOLBq+MzLW31bhTGVc5VE5dDEfVs31bUXiol3WDY9ehkShAgaB6x4BxuX98rGqOCGhlkFIs6SfrDrOt X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4468.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(366004)(346002)(39860400002)(136003)(396003)(376002)(26005)(478600001)(6666004)(52116002)(6506007)(6486002)(6512007)(66946007)(41300700001)(66476007)(8676002)(66556008)(86362001)(316002)(4326008)(83380400001)(38350700002)(1076003)(38100700002)(2616005)(30864003)(186003)(36756003)(8936002)(5660300002)(103116003)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 45KjFioyWwxJh59IyFFF1pVnR0nBvB/c5Q3IR3GQWqyC/FZ8YGZSKX1wP91H3nuij2xjTNjocxsXnVlLn6kMnmbdvImEbl256c80WcNu3i5YeEOG/DAPA+Bj/wyKXcFvrs49YeOVULhoQDm3eDY866J1n1fNhxDYEfsNYHbh6wLXunsSiDc0WoXtSxRnOf5diZ8usVh+FwvPZtudb2phCSE98KTRf3wzbrWalYhLqgNJai1/XxVyuQn/ET+rJMx1A+9LS/Pc+3dQbc7m+6UaoBjByWE6FxOZvzsUkQi4vv9sGQ/7KozFEdKZCfn6oktDZ+S6cd3Jfi350OEy074EtaQ8qqaz25ncxoY1ni6eoarM853iEH+F25/UXe5PcD0Q+I+l1cuwQzv+say5LS7xP4DLY0TRvRINfujlonhDvWPJNDdTwAKcKgnOsVoj98NcLY+KXDKSATp3vJTt0zToJgFNXB5ZRI7D6zb7hCp8tW3HSFIDWJyJoorJ5JBara+HKAd9xxhtkGYUXeLell52OASY4tIXkTIv8KAnPvDeVGD7Gc/oBrbl3qmsUH3nDHUyYAD2Dp9L8n8I8icJe27O4tfebr3BDBh1YzkyW1BWbWFdq3nYaF9Cj3g9fKpdIukmwnp69rRke/J0Q0xRdVgYBNHkv7hxF8ejQxz9dwtlbcfke+BXAH0ktik3OZSMlpWrWD4vydyYzD4wkTrdvPozt7lZGzNk5QkWvtjH7x3N/uw7kK3LayN4/nFhfB/IiZD8FFcHgulPKPbceRlLbOdjOxA/6BbWcN3gWP/BKHhTuAkIdZK7VEtYajSb8Ww2TY3/+YGszAdu6SZzB0FMPjQQgrkHHh9CK4NUNHvmtPv0/47jfFsDhIkSZ1nGb1O+ADsGNNcn6o3K8lM/At5dfTOkF1dSEiqloG1ykv4hdTBnCH+Hk87PlrZrv3za9S+7X8rQS2SvjsO9pN5sEI+F824E8/Nhwzvs1lOCzvSnTXpHZTxqXncV1tecntuHB015g1+kIJ816LxP3ZFfqkCup4va9V9riKnASX0rGcsmK18t4KL8CrfQOSmxqj3+RnbWkdBvuMP4djUL+ywr8jA60y8Tqz4p13EgdVMhIN/29OUEok0q4jfzYsi/nCi2l4nd5iCY7VXF2j/QqFYVK4mNZQd01aw0E5eyBjut4KaU2Zyd8WTDRT6hC3f8TANxntST3Dgk3HGeJd6r9UR410uAUoMTSQQwtt/mh3mk8AMqfYZabejFcYcu8lYAfFNGGlqQqvfzyYLs/ssktbWcwPp+0eBoMDKAsDLxSz/4ycZbpSIiWaDu6zYXXhoz6N7R+MCDkpqeY0tuhQxj8vKnrU0CACpVERNXATtzf8rpWtW8GNVDGjoMb8oxwyFCeKU7RWxR48b4wghSuTlL/zSuWfDDZRBBTG12Y6vXn8Jaxo+jk+YJ8kj8gHBea1RxMqd8cIuYRbVLrE+IOoMpecSpJeEoy+40PzOrNho4iE5QYRQqNrws6LrmpBpc3QKz8fJlu9Lkh2E5rCUJ8sKq0FuahGI6+aautforJ07c+LnXysqgATsFEiZeQyLSA//TggNP1TYAGFq4LduDZr4wP9lS9a9gpzo+VQ== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d181f560-3852-4b6e-b470-08da652ef731 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jul 2022 00:22:41.7727 (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: d+bSV84qsbsmux8bxwkwxIDRCy7rQP52njbcFSbu/mulZ2nyP+8HYAZrkA1gExuBINx3tcCoeLaYoY2GmKe5lQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR10MB3986 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-13_13:2022-07-13,2022-07-13 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 phishscore=0 mlxscore=0 suspectscore=0 adultscore=0 spamscore=0 mlxlogscore=999 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2207140000 X-Proofpoint-GUID: pDdsCtU8x4TMlW1FsNxnc7Avl5GJmBWv X-Proofpoint-ORIG-GUID: pDdsCtU8x4TMlW1FsNxnc7Avl5GJmBWv Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org per-fs rwsem is used across kernfs for synchronization purposes. Having an interface to access it not only avoids code duplication, it can also help in changing the underlying locking mechanism without needing to change the lock users. For example next patch modifies this interface to make use of hashed rwsems in place of per-fs rwsem. Signed-off-by: Imran Khan --- fs/kernfs/dir.c | 119 ++++++++++++++++++------------------ fs/kernfs/file.c | 5 +- fs/kernfs/inode.c | 26 ++++---- fs/kernfs/kernfs-internal.h | 78 +++++++++++++++++++++++ fs/kernfs/mount.c | 6 +- fs/kernfs/symlink.c | 6 +- 6 files changed, 159 insertions(+), 81 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index d2a0b4acd0733..73f4ebc1464e2 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -33,7 +33,7 @@ static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr */ static bool kernfs_active(struct kernfs_node *kn) { - lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem); + kernfs_rwsem_assert_held(kn); return atomic_read(&kn->active) >= 0; } @@ -467,12 +467,20 @@ static void kernfs_drain(struct kernfs_node *kn) __releases(&kernfs_root(kn)->kernfs_rwsem) __acquires(&kernfs_root(kn)->kernfs_rwsem) { + struct rw_semaphore *rwsem; struct kernfs_root *root = kernfs_root(kn); - lockdep_assert_held_write(&root->kernfs_rwsem); + /** + * kn has the same root as its ancestor, so it can be used to get + * per-fs rwsem. + */ + rwsem = kernfs_rwsem_ptr(kn); + + kernfs_rwsem_assert_held_write(kn); + WARN_ON_ONCE(kernfs_active(kn)); - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); if (kernfs_lockdep(kn)) { rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_); @@ -491,7 +499,7 @@ static void kernfs_drain(struct kernfs_node *kn) kernfs_drain_open_files(kn); - down_write(&root->kernfs_rwsem); + kernfs_down_write(kn); } /** @@ -726,12 +734,12 @@ struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root, int kernfs_add_one(struct kernfs_node *kn) { struct kernfs_node *parent = kn->parent; - struct kernfs_root *root = kernfs_root(parent); struct kernfs_iattrs *ps_iattr; + struct rw_semaphore *rwsem; bool has_ns; int ret; - down_write(&root->kernfs_rwsem); + rwsem = kernfs_down_write(parent); ret = -EINVAL; has_ns = kernfs_ns_enabled(parent); @@ -762,7 +770,7 @@ int kernfs_add_one(struct kernfs_node *kn) ps_iattr->ia_mtime = ps_iattr->ia_ctime; } - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); /* * Activate the new node unless CREATE_DEACTIVATED is requested. @@ -776,7 +784,7 @@ int kernfs_add_one(struct kernfs_node *kn) return 0; out_unlock: - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); return ret; } @@ -797,7 +805,7 @@ static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent, bool has_ns = kernfs_ns_enabled(parent); unsigned int hash; - lockdep_assert_held(&kernfs_root(parent)->kernfs_rwsem); + kernfs_rwsem_assert_held(parent); if (has_ns != (bool)ns) { WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", @@ -829,7 +837,7 @@ static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent, size_t len; char *p, *name; - lockdep_assert_held_read(&kernfs_root(parent)->kernfs_rwsem); + kernfs_rwsem_assert_held_read(parent); spin_lock_irq(&kernfs_pr_cont_lock); @@ -867,12 +875,12 @@ struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name, const void *ns) { struct kernfs_node *kn; - struct kernfs_root *root = kernfs_root(parent); + struct rw_semaphore *rwsem; - down_read(&root->kernfs_rwsem); + rwsem = kernfs_down_read(parent); kn = kernfs_find_ns(parent, name, ns); kernfs_get(kn); - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); return kn; } @@ -892,12 +900,12 @@ struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path, const void *ns) { struct kernfs_node *kn; - struct kernfs_root *root = kernfs_root(parent); + struct rw_semaphore *rwsem; - down_read(&root->kernfs_rwsem); + rwsem = kernfs_down_read(parent); kn = kernfs_walk_ns(parent, path, ns); kernfs_get(kn); - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); return kn; } @@ -1062,7 +1070,7 @@ struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent, static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) { struct kernfs_node *kn; - struct kernfs_root *root; + struct rw_semaphore *rwsem; if (flags & LOOKUP_RCU) return -ECHILD; @@ -1078,13 +1086,12 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) parent = kernfs_dentry_node(dentry->d_parent); if (parent) { spin_unlock(&dentry->d_lock); - root = kernfs_root(parent); - down_read(&root->kernfs_rwsem); + rwsem = kernfs_down_read(parent); if (kernfs_dir_changed(parent, dentry)) { - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); return 0; } - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); } else spin_unlock(&dentry->d_lock); @@ -1095,8 +1102,7 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) } kn = kernfs_dentry_node(dentry); - root = kernfs_root(kn); - down_read(&root->kernfs_rwsem); + rwsem = kernfs_down_read(kn); /* The kernfs node has been deactivated */ if (!kernfs_active(kn)) @@ -1115,10 +1121,10 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) kernfs_info(dentry->d_sb)->ns != kn->ns) goto out_bad; - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); return 1; out_bad: - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); return 0; } @@ -1132,12 +1138,11 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir, { struct kernfs_node *parent = dir->i_private; struct kernfs_node *kn; - struct kernfs_root *root; struct inode *inode = NULL; const void *ns = NULL; + struct rw_semaphore *rwsem; - root = kernfs_root(parent); - down_read(&root->kernfs_rwsem); + rwsem = kernfs_down_read(parent); if (kernfs_ns_enabled(parent)) ns = kernfs_info(dir->i_sb)->ns; @@ -1148,7 +1153,7 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir, * create a negative. */ if (!kernfs_active(kn)) { - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); return NULL; } inode = kernfs_get_inode(dir->i_sb, kn); @@ -1163,7 +1168,7 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir, */ if (!IS_ERR(inode)) kernfs_set_rev(parent, dentry); - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); /* instantiate and hash (possibly negative) dentry */ return d_splice_alias(inode, dentry); @@ -1286,7 +1291,7 @@ static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos, { struct rb_node *rbn; - lockdep_assert_held_write(&kernfs_root(root)->kernfs_rwsem); + kernfs_rwsem_assert_held_write(root); /* if first iteration, visit leftmost descendant which may be root */ if (!pos) @@ -1321,9 +1326,9 @@ static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos, void kernfs_activate(struct kernfs_node *kn) { struct kernfs_node *pos; - struct kernfs_root *root = kernfs_root(kn); + struct rw_semaphore *rwsem; - down_write(&root->kernfs_rwsem); + rwsem = kernfs_down_write(kn); pos = NULL; while ((pos = kernfs_next_descendant_post(pos, kn))) { @@ -1337,7 +1342,7 @@ void kernfs_activate(struct kernfs_node *kn) pos->flags |= KERNFS_ACTIVATED; } - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); } static void __kernfs_remove(struct kernfs_node *kn) @@ -1348,7 +1353,7 @@ static void __kernfs_remove(struct kernfs_node *kn) if (!kn) return; - lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem); + kernfs_rwsem_assert_held_write(kn); /* * This is for kernfs_remove_self() which plays with active ref @@ -1417,16 +1422,14 @@ static void __kernfs_remove(struct kernfs_node *kn) */ void kernfs_remove(struct kernfs_node *kn) { - struct kernfs_root *root; + struct rw_semaphore *rwsem; if (!kn) return; - root = kernfs_root(kn); - - down_write(&root->kernfs_rwsem); + rwsem = kernfs_down_write(kn); __kernfs_remove(kn); - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); } /** @@ -1512,9 +1515,9 @@ void kernfs_unbreak_active_protection(struct kernfs_node *kn) bool kernfs_remove_self(struct kernfs_node *kn) { bool ret; - struct kernfs_root *root = kernfs_root(kn); + struct rw_semaphore *rwsem; - down_write(&root->kernfs_rwsem); + rwsem = kernfs_down_write(kn); kernfs_break_active_protection(kn); /* @@ -1542,9 +1545,9 @@ bool kernfs_remove_self(struct kernfs_node *kn) atomic_read(&kn->active) == KN_DEACTIVATED_BIAS) break; - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); schedule(); - down_write(&root->kernfs_rwsem); + rwsem = kernfs_down_write(kn); } finish_wait(waitq, &wait); WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb)); @@ -1557,7 +1560,7 @@ bool kernfs_remove_self(struct kernfs_node *kn) */ kernfs_unbreak_active_protection(kn); - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); return ret; } @@ -1574,7 +1577,7 @@ int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, const void *ns) { struct kernfs_node *kn; - struct kernfs_root *root; + struct rw_semaphore *rwsem; if (!parent) { WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n", @@ -1582,14 +1585,14 @@ int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, return -ENOENT; } - root = kernfs_root(parent); - down_write(&root->kernfs_rwsem); + + rwsem = kernfs_down_write(parent); kn = kernfs_find_ns(parent, name, ns); if (kn) __kernfs_remove(kn); - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); if (kn) return 0; @@ -1608,16 +1611,15 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, const char *new_name, const void *new_ns) { struct kernfs_node *old_parent; - struct kernfs_root *root; const char *old_name = NULL; + struct rw_semaphore *rwsem; int error; /* can't move or rename root */ if (!kn->parent) return -EINVAL; - root = kernfs_root(kn); - down_write(&root->kernfs_rwsem); + rwsem = kernfs_down_write(kn); error = -ENOENT; if (!kernfs_active(kn) || !kernfs_active(new_parent) || @@ -1671,7 +1673,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, error = 0; out: - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); return error; } @@ -1742,14 +1744,13 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx) struct dentry *dentry = file->f_path.dentry; struct kernfs_node *parent = kernfs_dentry_node(dentry); struct kernfs_node *pos = file->private_data; - struct kernfs_root *root; const void *ns = NULL; + struct rw_semaphore *rwsem; if (!dir_emit_dots(file, ctx)) return 0; - root = kernfs_root(parent); - down_read(&root->kernfs_rwsem); + rwsem = kernfs_down_read(parent); if (kernfs_ns_enabled(parent)) ns = kernfs_info(dentry->d_sb)->ns; @@ -1766,12 +1767,12 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx) file->private_data = pos; kernfs_get(pos); - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); if (!dir_emit(ctx, name, len, ino, type)) return 0; - down_read(&root->kernfs_rwsem); + rwsem = kernfs_down_read(parent); } - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); file->private_data = NULL; ctx->pos = INT_MAX; return 0; diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 77c3e34a6c7da..7c8bdf4ce31f4 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -911,6 +911,7 @@ static void kernfs_notify_workfn(struct work_struct *work) struct kernfs_node *kn; struct kernfs_super_info *info; struct kernfs_root *root; + struct rw_semaphore *rwsem; repeat: /* pop one off the notify_list */ spin_lock_irq(&kernfs_notify_lock); @@ -925,7 +926,7 @@ static void kernfs_notify_workfn(struct work_struct *work) root = kernfs_root(kn); /* kick fsnotify */ - down_write(&root->kernfs_rwsem); + rwsem = kernfs_down_write(kn); down_write(&root->supers_rwsem); list_for_each_entry(info, &kernfs_root(kn)->supers, node) { @@ -965,7 +966,7 @@ static void kernfs_notify_workfn(struct work_struct *work) } up_write(&root->supers_rwsem); - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); kernfs_put(kn); goto repeat; } diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c index 3d783d80f5daa..efe5ae98abf46 100644 --- a/fs/kernfs/inode.c +++ b/fs/kernfs/inode.c @@ -99,11 +99,11 @@ int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) { int ret; - struct kernfs_root *root = kernfs_root(kn); + struct rw_semaphore *rwsem; - down_write(&root->kernfs_rwsem); + rwsem = kernfs_down_write(kn); ret = __kernfs_setattr(kn, iattr); - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); return ret; } @@ -112,14 +112,13 @@ int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, { struct inode *inode = d_inode(dentry); struct kernfs_node *kn = inode->i_private; - struct kernfs_root *root; + struct rw_semaphore *rwsem; int error; if (!kn) return -EINVAL; - root = kernfs_root(kn); - down_write(&root->kernfs_rwsem); + rwsem = kernfs_down_write(kn); error = setattr_prepare(&init_user_ns, dentry, iattr); if (error) goto out; @@ -132,7 +131,7 @@ int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, setattr_copy(&init_user_ns, inode, iattr); out: - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); return error; } @@ -187,14 +186,14 @@ int kernfs_iop_getattr(struct user_namespace *mnt_userns, { struct inode *inode = d_inode(path->dentry); struct kernfs_node *kn = inode->i_private; - struct kernfs_root *root = kernfs_root(kn); + struct rw_semaphore *rwsem; - down_read(&root->kernfs_rwsem); + rwsem = kernfs_down_read(kn); spin_lock(&inode->i_lock); kernfs_refresh_inode(kn, inode); generic_fillattr(&init_user_ns, inode, stat); spin_unlock(&inode->i_lock); - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); return 0; } @@ -277,22 +276,21 @@ void kernfs_evict_inode(struct inode *inode) int kernfs_iop_permission(struct user_namespace *mnt_userns, struct inode *inode, int mask) { + struct rw_semaphore *rwsem; struct kernfs_node *kn; - struct kernfs_root *root; int ret; if (mask & MAY_NOT_BLOCK) return -ECHILD; kn = inode->i_private; - root = kernfs_root(kn); - down_read(&root->kernfs_rwsem); + rwsem = kernfs_down_read(kn); spin_lock(&inode->i_lock); kernfs_refresh_inode(kn, inode); ret = generic_permission(&init_user_ns, inode, mask); spin_unlock(&inode->i_lock); - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); return ret; } diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index 3cd17c100d10e..0babc3dc4f4a3 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -169,4 +169,82 @@ extern const struct inode_operations kernfs_symlink_iops; * kernfs locks */ extern struct kernfs_global_locks *kernfs_locks; + +static inline struct rw_semaphore *kernfs_rwsem_ptr(struct kernfs_node *kn) +{ + struct kernfs_root *root = kernfs_root(kn); + + return &root->kernfs_rwsem; +} + +static inline void kernfs_rwsem_assert_held(struct kernfs_node *kn) +{ + lockdep_assert_held(kernfs_rwsem_ptr(kn)); +} + +static inline void kernfs_rwsem_assert_held_write(struct kernfs_node *kn) +{ + lockdep_assert_held_write(kernfs_rwsem_ptr(kn)); +} + +static inline void kernfs_rwsem_assert_held_read(struct kernfs_node *kn) +{ + lockdep_assert_held_read(kernfs_rwsem_ptr(kn)); +} + +/** + * kernfs_down_write() - Acquire kernfs rwsem + * + * @kn: kernfs_node for which rwsem needs to be taken + * + * Return: pointer to acquired rwsem + */ +static inline struct rw_semaphore *kernfs_down_write(struct kernfs_node *kn) +{ + struct rw_semaphore *rwsem = kernfs_rwsem_ptr(kn); + + down_write(rwsem); + + return rwsem; +} + +/** + * kernfs_up_write - Release kernfs rwsem + * + * @rwsem: address of rwsem to release + * + * Return: void + */ +static inline void kernfs_up_write(struct rw_semaphore *rwsem) +{ + up_write(rwsem); +} + +/** + * kernfs_down_read() - Acquire kernfs rwsem + * + * @kn: kernfs_node for which rwsem needs to be taken + * + * Return: pointer to acquired rwsem + */ +static inline struct rw_semaphore *kernfs_down_read(struct kernfs_node *kn) +{ + struct rw_semaphore *rwsem = kernfs_rwsem_ptr(kn); + + down_read(rwsem); + + return rwsem; +} + +/** + * kernfs_up_read - Release kernfs rwsem + * + * @rwsem: address of rwsem to release + * + * Return: void + */ +static inline void kernfs_up_read(struct rw_semaphore *rwsem) +{ + up_read(rwsem); +} #endif /* __KERNFS_INTERNAL_H */ diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index d2be1c3047157..3c5334b74f369 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -237,9 +237,9 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn, static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *kfc) { struct kernfs_super_info *info = kernfs_info(sb); - struct kernfs_root *kf_root = kfc->root; struct inode *inode; struct dentry *root; + struct rw_semaphore *rwsem; info->sb = sb; /* Userspace would break if executables or devices appear on sysfs */ @@ -257,9 +257,9 @@ static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *k sb->s_shrink.seeks = 0; /* get root inode, initialize and unlock it */ - down_read(&kf_root->kernfs_rwsem); + rwsem = kernfs_down_read(info->root->kn); inode = kernfs_get_inode(sb, info->root->kn); - up_read(&kf_root->kernfs_rwsem); + kernfs_up_read(rwsem); if (!inode) { pr_debug("kernfs: could not get root inode\n"); return -ENOMEM; diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c index 0ab13824822f7..9d41036025547 100644 --- a/fs/kernfs/symlink.c +++ b/fs/kernfs/symlink.c @@ -113,12 +113,12 @@ static int kernfs_getlink(struct inode *inode, char *path) struct kernfs_node *kn = inode->i_private; struct kernfs_node *parent = kn->parent; struct kernfs_node *target = kn->symlink.target_kn; - struct kernfs_root *root = kernfs_root(parent); + struct rw_semaphore *rwsem; int error; - down_read(&root->kernfs_rwsem); + rwsem = kernfs_down_read(parent); error = kernfs_get_target_path(parent, target, path); - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); return error; } From patchwork Thu Jul 14 00:22:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Imran Khan X-Patchwork-Id: 12917215 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 14E67C433EF for ; Thu, 14 Jul 2022 00:23:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232274AbiGNAXb (ORCPT ); Wed, 13 Jul 2022 20:23:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231934AbiGNAXK (ORCPT ); Wed, 13 Jul 2022 20:23:10 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A2D317E36; Wed, 13 Jul 2022 17:22:53 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 26DNvOO9009847; Thu, 14 Jul 2022 00:22:48 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-2021-07-09; bh=YKDPmgTvRsBHJ/cAuZsS3uWVWhJr1o9jBza9dEy+EyA=; b=kmIEPLW8Lq+9Lp7moikEvt2uUo6c8TxzGHSXtGbDDPn5ck8TTzKKVvQ5eepHgPPN8Buj guwQi8fVtctUY2lTmCo2HAUF/HUiVkHA9fDrnB/GoDrHznOGMNdR+ZP+KXhLIu8MvE9S tpyENT3sxXGPPAyfwCgrtYUrr7A/HPR34h4o2gFZlHWGd2SE0c6UWSaER/v6PhIW7y9R 9odQ0cFHZmADBThRl6BY/1zdHmT382qm9pBza/jeaNrZjATQIkLyk+neUrGDzaRCEuyE QEhibSbQK1sF2miKgtZunfWy7PgEqwPDMJMo/PH7zX+DgVBSz3n/W+JQ1vlW63E15qeG ag== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h71r1b9x7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Jul 2022 00:22:47 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 26E0GteK031842; Thu, 14 Jul 2022 00:22:47 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2040.outbound.protection.outlook.com [104.47.66.40]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3h70457bqh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Jul 2022 00:22:46 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Lf+qH/dZ1JW7ejs9ixoGlxcMyA9NamkCxIeca2DDrntdMXOorAISzrd8Ivr6Ll5GdzmwftaGNuUFBcGovRCYBXHefyGNK6mRbnjYh3jPv1ZfUnvfXRs3lbDfkWmmhYimlKR0A/fHm2A3QEKT6GpfxeCl2WonKm2VROCDbDpaHCU8HflvFEajia6+hrNifM9rnSIsBN2Hq/B//w8ALk9Z4pWxHb0IgJsgv/Uvr0auDXrN6uKQr+PVXoqZ0TESHmjdRy0LLkrP7h2PGiRfvKINmJfYSXBZookFMLkiQCqjCrquz3326bzQNy7mHg2f24HBvIM5YlGFWyotVnRXo9Y4tw== 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=YKDPmgTvRsBHJ/cAuZsS3uWVWhJr1o9jBza9dEy+EyA=; b=kPZ0ZDN2wMZSnwW6amzBCCn0UwH3KasO2w18NtqW9VGQFU9pQpUPJPSePLNwYRhRUBOxYEqocVBxNNh70HU333NPGj/RtgKD0xR4U0P541Up9qNjZAz3QdyKl3Dtn3xMkawyW2GAaoQPPrFLSiGmTcHnKhmq15Ej4mNQ4RBDq5sv5Jlez/MIJCfaO7Kv1fXL+3PNRNekFHD8oun67uHLcU/Oy1TPBFbyeMlbWKaZQYURzaE2mDOOAFtmUw6M0iW6YFk7PQCfF52AGzZvOlD7kUFCTQWtjUrpeAXxqTMfClzAlJUh7Z0kVzPFMy2VbBCmfrbWDLbAG+xFe6FllJNMbw== 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=YKDPmgTvRsBHJ/cAuZsS3uWVWhJr1o9jBza9dEy+EyA=; b=LqCa+nT9dp+2xF9PbFPFlzUjThIdwFodcdIVyumGFOGV1kAFiKZ5TbeN430tM3wXJp37lGOWhU4TpnuG9HvsjP9Lp+DI2sPyO6NogAhiuuKmYkCW6bIkwR9ubv17vULK7azVkcVSXJ5sOyI37Y5xFIbAAHbRaCDOOjmHNcSJAek= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by BY5PR10MB3986.namprd10.prod.outlook.com (2603:10b6:a03:1fb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.12; Thu, 14 Jul 2022 00:22:44 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::dd9d:e9dd:d864:524c]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::dd9d:e9dd:d864:524c%7]) with mapi id 15.20.5438.013; Thu, 14 Jul 2022 00:22:43 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 4/5] kernfs: Replace per-fs rwsem with hashed rwsems. Date: Thu, 14 Jul 2022 10:22:23 +1000 Message-Id: <20220714002224.3641629-5-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220714002224.3641629-1-imran.f.khan@oracle.com> References: <20220714002224.3641629-1-imran.f.khan@oracle.com> X-ClientProxiedBy: SYCP282CA0011.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:80::23) To CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b1b74f4e-1ccc-47c6-e328-08da652ef85b X-MS-TrafficTypeDiagnostic: BY5PR10MB3986:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PdJaYDoOkdrQeCEWtPDjD6zzs3XIS6blO5ZpgC7W2LhJ7yc7mFCz/rkPzXYPyFzOjWhUkdgikaTQTpbFHK4IBnVgeXEHPeffynkuESg2OBt8cr/5gw4B7e1uNRcTwUeduW8BdQj0HuH7T9r4RbvzpcySTWAbx+InkjRNB8QU7KDBwLoA4ZpluG8sTfbop716Br5/0AEJPDNZ+K6L8BG3qtrW9oduvLr54HBbbWHlHydZpIFySHSfcUHPWU7rS1QYcPNqX9IXWelu1cw3YKKZeNUiRmLaMHSUDSG0MWlf/Z4TJVFKUt1pvRCen9HEFGhOgE987uxV6Ppqn1x9qViW8G2fNbjrVnOM2ACILz07AcyE0yFiYfb/UaDL9QhzBKhAw4UwCrf8LnYVMv52iVYRI3Pk5wSULU85ObhNVdDVPrFOsQHg1YrkshoWO7uVjb17a2XGgg/kJYGtrtuuPs/18HrCy1AQVcERe8QD2/6h4z6l2Kh/46vvufdGRKAqZl7/wyQmkuldsG3rKS+xy9wrsJr1dGdy2Nz1ld/nTDAKMNVi2HZqRdrrXTq9QzP/FT1BvmxWsm2SmwWrFDtq2bYLzaivBZ+h4RgRsSlaHaSyqrnCY0pI2+QMVkZWdhoNu7zesJrksHwW7pkChrusF3MV/IaVfZQ2lSU/CC+WJiMFO9lRq6Lonu+XHhcz40uNS8Vuq+3wFn15myIDoGlhFKNJvsZDLp4C+7iGrJ2pC0XYa2iK+i0CYBJu4ARoXzCa8Ta73ezO3sT4txa3apTqqJ3CJHZzM9YW9/rsB9ntQxNZ3c5qhr9aKtpuRTSHieU+WUlW X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4468.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(366004)(346002)(39860400002)(136003)(396003)(376002)(26005)(478600001)(6666004)(52116002)(6506007)(6486002)(6512007)(66946007)(41300700001)(66476007)(8676002)(66556008)(86362001)(316002)(4326008)(83380400001)(38350700002)(1076003)(38100700002)(2616005)(30864003)(186003)(36756003)(8936002)(5660300002)(103116003)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qBDi7jQ2tIk7qFl8y+fh/IzXttJDMx8hEx5APcOdZcidpDLyXl9WoPGs89+oYEfB1yiIVhUKAwqrnKVMzQgVeZnNd6MFXiJnJv/ye+zvSHYr97dfriDzHNOo4zh3kaj2hgXY1dGnelStCoyG8R6Z8Ol8/njUs3EGKHPG95rajzqvHub//7jj5qqvSaBmMv/dm6dLhSE1VXGjPN5JVc4E0QQ7Dv4MhLWj5Qeb16kFMGAvjve4DDVKD1mt9JcH2mhVg6MPd2VbiVuT9NjRm/rPDqLH7SblND4vBLQN4Pb3RKREXFY7cMLQRhrpQ9/+Gl9n71yWBvemlSRJPKAR4IMTsKLycZfL+lbw+T1QiC4KDKFtjkQtPMPvgAXbv5dZRpD9HH5jEzX1ILkObt3dEFrihyTOceoZ6I3+b/czUN/MT++8inkAbrbxrddlsTvTUIBEKdba0V3OGqVZCVVuxLdKES+iAGJvZO8aINruK/w7w0VIzLAhVVKYoBTpMdzj+bvTk7jyWq664AUkw0a+pQfZRAsB2dilLsPFOd/cWArdXSmXNKvIjFJO/0NBgfTtXBzCVRrojVNaYTSScLDPUDsf2db7/Ow0RkC/ppY98js4z5WDHugFxA0ExhvLjxaom1ZU8K7C44Iw9GE8xpUMj7VSFACv98EoOINpyQetGLfSohrqgIRo31f8D9cxVTBpgYFrNUIgkVmx4fQHdsrhN/DBcEyIw5IlTIF2KSBD+GxRT2/xhCyUhZ31Ms6ViVqsbcV7xSw6noJTj5wS78rQAOFzKyUD4ERQdcMMvV32/bhszMlYHcbPHV6Apb6XezVXCFaxM8cZ+1hDs2JcgYFJDDOu72iDn26msw4yvupAU2oLVgJo8Jh7iTzmO3OzJYqohF2rlH2dY0xm9LfhqJDfa8SFPYeJ/0GzZngkUXr/c4S+W8kuHQZrnty5stWh5uIyjPhmZ5g+DO5nkRYAU6t8Krc8bEqU9zyU6VrxFive3bbc/FVoE7sIQ8HItMJ//rcmuHSyy6+ESbVhJj0UI0jGszOwQvXP1BN+F192zeMXk/RAuLhORMiWP8yn9fJnBHpVrCGHLErLmhw8FCbnIHEn0RqEH4GRUFm/N3I+LsEgXxMvaQDXDSQgg8yEcC4I3rneB7bTs/yYTlELIvzQKnXG8vvGNo3NZGwu8j1OzpaCjEiT4lauVtqtGnKAa79Jzoy9/9lniA4vxVh53ddD4OYoSl9A6tUW1qMHoEAClrojJD/pZysjBzCHIhv0m2dm2tGN/dBhLjoPmBZztgpwSfqr56W5vzR9GDH+2Csue0sk2ZNZc022YhLNTfol08PrlZTG3nZUAbbXvDuWuSWNI9t27dXaURry6wgPOfunshzEFWaEB55Ja3VZ0cjK/hhonBUPsQD1we2xfRdOzI3jFQJUyWaRB4LH+VXewA/ySatGI/oyGv0ZIItlYc8LQ8m7h2f6N3GH69i0ZgPibYOuFUyXbzWxhxtDdwafXldRyitBA3iP4PHDfmGuj0fpiPLZpdi/W1ICX6oSGFt1lNaonMZQ5ckDS+BNrZQ0pD+rmsy20iIZ+rn8B1EcdAUQJG7BzdxxAgsyDgqxIhi3YYiYIeYqGL8Vzw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b1b74f4e-1ccc-47c6-e328-08da652ef85b X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jul 2022 00:22:43.9143 (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: hZ6mYgwjv7gwZMu6W4yX2dkKk7AycGHBVdN6U8Nbo8uyfaT5AXWCBA/djcPjQyNluvxg1g8+UvmlrSR2PqnTmw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR10MB3986 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-13_13:2022-07-13,2022-07-13 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxlogscore=999 mlxscore=0 suspectscore=0 phishscore=0 spamscore=0 malwarescore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2207140000 X-Proofpoint-ORIG-GUID: EiQ-pNSNHK2a3LK3NuCOq66QGEUDZxJe X-Proofpoint-GUID: EiQ-pNSNHK2a3LK3NuCOq66QGEUDZxJe Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Having a single rwsem to synchronize all operations across a kernfs based file system (cgroup, sysfs etc.) does not scale well. The contention around this single rwsem becomes more apparent in large scale systems with few hundred CPUs where most of the CPUs have running tasks that are opening, accessing or closing sysfs files at any point of time. Using hashed rwsems in place of a per-fs rwsem, can significantly reduce contention around per-fs rwsem and hence provide better scalability. Moreover as these hashed rwsems are not part of kernfs_node objects we will not see any singnificant change in memory utilization of kernfs based file systems like sysfs, cgroupfs etc. Modify interface introduced in previous patch to make use of hashed rwsems. Just like earlier change use kernfs_node address as hashing key. Since we are getting rid of per-fs lock, in certain cases we may need to acquire locks corresponding to multiple nodes and in such cases of nested locking, locks are taken in order of their addresses. Introduce helpers to acquire rwsems corresponding to multiple nodes for such cases. For operations that involve finding the node first and then operating on it (for example operations involving find_and_get_ns), acquiring rwsem for the node being searched is not possible. Such operations need to make sure that a concurrent remove does not remove the found node. Introduce a per-fs mutex (kernfs_topo_mutex) that can be used to synchronize these operations against parallel removal/renaming of involved node. Also replace usage of kernfs_pr_cont_buf with another global buffer in kernfs_walk_ns. This is because kernfs_pr_cont_buf is protected by a spinlock but now kernfs_walk_ns needs to acquire hashed rwsem corresponding to nodes further down in the path and this can't be done under spinlock. Having another buffer to piggyback the path in kernfs_walk_ns and protecting this with kernfs_topo_mutex (mentioned earlier) would avoid need of spinlock and also ensure that there is no topology change. Replacing global mutex and spinlocks with hashed ones (as mentioned in previous changes) and global rwsem with hashed rwsem (as done in this change) reduces contention around kernfs and results in better performance numbers. For example on a system with 384 cores, if I run 200 instances of an application which is mostly executing the following loop: for (int loop = 0; loop <100 ; loop++) { for (int port_num = 1; port_num < 2; port_num++) { for (int gid_index = 0; gid_index < 254; gid_index++ ) { char ret_buf[64], ret_buf_lo[64]; char gid_file_path[1024]; int ret_len; int ret_fd; ssize_t ret_rd; ub4 i, saved_errno; memset(ret_buf, 0, sizeof(ret_buf)); memset(gid_file_path, 0, sizeof(gid_file_path)); ret_len = snprintf(gid_file_path, sizeof(gid_file_path), "/sys/class/infiniband/%s/ports/%d/gids/%d", dev_name, port_num, gid_index); ret_fd = open(gid_file_path, O_RDONLY | O_CLOEXEC); if (ret_fd < 0) { printf("Failed to open %s\n", gid_file_path); continue; } /* Read the GID */ ret_rd = read(ret_fd, ret_buf, 40); if (ret_rd == -1) { printf("Failed to read from file %s, errno: %u\n", gid_file_path, saved_errno); continue; } close(ret_fd); } } I can see contention around above mentioned locks as follows: - 54.07% 53.60% showgids [kernel.kallsyms] [k] osq_lock - 53.60% __libc_start_main - 32.29% __GI___libc_open entry_SYSCALL_64_after_hwframe do_syscall_64 sys_open do_sys_open do_filp_open path_openat vfs_open do_dentry_open kernfs_fop_open mutex_lock - __mutex_lock_slowpath - 32.23% __mutex_lock.isra.5 osq_lock - 21.31% __GI___libc_close entry_SYSCALL_64_after_hwframe do_syscall_64 exit_to_usermode_loop task_work_run ____fput __fput kernfs_fop_release kernfs_put_open_node.isra.8 mutex_lock - __mutex_lock_slowpath - 21.28% __mutex_lock.isra.5 osq_lock - 10.49% 10.39% showgids [kernel.kallsyms] [k] down_read 10.39% __libc_start_main __GI___libc_open entry_SYSCALL_64_after_hwframe do_syscall_64 sys_open do_sys_open do_filp_open - path_openat - 9.72% link_path_walk - 5.21% inode_permission - __inode_permission - 5.21% kernfs_iop_permission down_read - 4.08% walk_component lookup_fast - d_revalidate.part.24 - 4.08% kernfs_dop_revalidate - 7.48% 7.41% showgids [kernel.kallsyms] [k] up_read 7.41% __libc_start_main __GI___libc_open entry_SYSCALL_64_after_hwframe do_syscall_64 sys_open do_sys_open do_filp_open - path_openat - 7.01% link_path_walk - 4.12% inode_permission - __inode_permission - 4.12% kernfs_iop_permission up_read - 2.61% walk_component lookup_fast - d_revalidate.part.24 - 2.61% kernfs_dop_revalidate Moreover this run of 200 application isntances takes 32-34 secs. to complete. With the patched kernel and on the same test setup, we no longer see contention around osq_lock (i.e kernfs_open_file_mutex) and also contention around per-fs kernfs_rwsem has reduced significantly as well. This can be seen in the following perf snippet: - 1.66% 1.65% showgids [kernel.kallsyms] [k] down_read 1.65% __libc_start_main __GI___libc_open entry_SYSCALL_64_after_hwframe do_syscall_64 sys_open do_sys_open do_filp_open - path_openat - 1.62% link_path_walk - 0.98% inode_permission - __inode_permission + 0.98% kernfs_iop_permission - 0.52% walk_component lookup_fast - d_revalidate.part.24 - 0.52% kernfs_dop_revalidate - 1.12% 1.11% showgids [kernel.kallsyms] [k] up_read 1.11% __libc_start_main __GI___libc_open entry_SYSCALL_64_after_hwframe do_syscall_64 sys_open do_sys_open do_filp_open - path_openat - 1.11% link_path_walk - 0.69% inode_permission - __inode_permission - 0.69% kernfs_iop_permission up_read Moreover the test execution time has reduced from 32-34 secs to 18-19 secs. Signed-off-by: Imran Khan --- fs/kernfs/Makefile | 2 +- fs/kernfs/dir.c | 161 +++++++++++++++++----- fs/kernfs/inode.c | 20 +++ fs/kernfs/kernfs-internal.c | 259 ++++++++++++++++++++++++++++++++++++ fs/kernfs/kernfs-internal.h | 47 ++++++- fs/kernfs/mount.c | 9 ++ fs/kernfs/symlink.c | 13 +- include/linux/kernfs.h | 1 + 8 files changed, 474 insertions(+), 38 deletions(-) create mode 100644 fs/kernfs/kernfs-internal.c diff --git a/fs/kernfs/Makefile b/fs/kernfs/Makefile index 4ca54ff54c986..778da6b118e9b 100644 --- a/fs/kernfs/Makefile +++ b/fs/kernfs/Makefile @@ -3,4 +3,4 @@ # Makefile for the kernfs pseudo filesystem # -obj-y := mount.o inode.o dir.o file.o symlink.o +obj-y := mount.o inode.o dir.o file.o symlink.o kernfs-internal.o diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 73f4ebc1464e2..7d02c3dd2c207 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -17,7 +17,7 @@ #include "kernfs-internal.h" -static DEFINE_RWLOCK(kernfs_rename_lock); /* kn->parent and ->name */ +DEFINE_RWLOCK(kernfs_rename_lock); /* kn->parent and ->name */ /* * Don't use rename_lock to piggy back on pr_cont_buf. We don't want to * call pr_cont() while holding rename_lock. Because sometimes pr_cont() @@ -27,13 +27,13 @@ static DEFINE_RWLOCK(kernfs_rename_lock); /* kn->parent and ->name */ */ static DEFINE_SPINLOCK(kernfs_pr_cont_lock); static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by pr_cont_lock */ +static char kernfs_path_buf[PATH_MAX]; /* protected by kernfs_topo_mutex */ static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr */ #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb) static bool kernfs_active(struct kernfs_node *kn) { - kernfs_rwsem_assert_held(kn); return atomic_read(&kn->active) >= 0; } @@ -458,14 +458,15 @@ void kernfs_put_active(struct kernfs_node *kn) /** * kernfs_drain - drain kernfs_node * @kn: kernfs_node to drain + * @anc: ancestor of kernfs_node to drain * * Drain existing usages and nuke all existing mmaps of @kn. Mutiple * removers may invoke this function concurrently on @kn and all will * return after draining is complete. */ -static void kernfs_drain(struct kernfs_node *kn) - __releases(&kernfs_root(kn)->kernfs_rwsem) - __acquires(&kernfs_root(kn)->kernfs_rwsem) +static void kernfs_drain(struct kernfs_node *kn, struct kernfs_node *anc) + __releases(kernfs_rwsem_ptr(anc)) + __acquires(kernfs_rwsem_ptr(anc)) { struct rw_semaphore *rwsem; struct kernfs_root *root = kernfs_root(kn); @@ -476,10 +477,11 @@ static void kernfs_drain(struct kernfs_node *kn) */ rwsem = kernfs_rwsem_ptr(kn); - kernfs_rwsem_assert_held_write(kn); + kernfs_rwsem_assert_held_write(anc); WARN_ON_ONCE(kernfs_active(kn)); + rwsem = kernfs_rwsem_ptr(anc); kernfs_up_write(rwsem); if (kernfs_lockdep(kn)) { @@ -499,7 +501,7 @@ static void kernfs_drain(struct kernfs_node *kn) kernfs_drain_open_files(kn); - kernfs_down_write(kn); + kernfs_down_write(anc); } /** @@ -739,6 +741,11 @@ int kernfs_add_one(struct kernfs_node *kn) bool has_ns; int ret; + /** + * The node being added is not active at this point of time and may + * be activated later depending on CREATE_DEACTIVATED flag. So at + * this point of time just locking the parent is enough. + */ rwsem = kernfs_down_write(parent); ret = -EINVAL; @@ -836,28 +843,35 @@ static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent, { size_t len; char *p, *name; + struct rw_semaphore *rwsem; kernfs_rwsem_assert_held_read(parent); - spin_lock_irq(&kernfs_pr_cont_lock); + lockdep_assert_held(&kernfs_root(parent)->kernfs_topo_mutex); - len = strlcpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf)); + /* Caller has kernfs_topo_mutex so topology will not change */ + p = kernfs_path_buf; + len = strlcpy(p, path, PATH_MAX); - if (len >= sizeof(kernfs_pr_cont_buf)) { - spin_unlock_irq(&kernfs_pr_cont_lock); + if (len >= PATH_MAX) { + kfree(p); return NULL; } - p = kernfs_pr_cont_buf; - + rwsem = kernfs_rwsem_ptr(parent); while ((name = strsep(&p, "/")) && parent) { if (*name == '\0') continue; parent = kernfs_find_ns(parent, name, ns); + /* + * Release rwsem for node whose child RB tree has been + * traversed. + */ + kernfs_up_read(rwsem); + if (parent) /* Acquire rwsem before traversing child RB tree */ + rwsem = kernfs_down_read(parent); } - spin_unlock_irq(&kernfs_pr_cont_lock); - return parent; } @@ -876,11 +890,20 @@ struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent, { struct kernfs_node *kn; struct rw_semaphore *rwsem; + struct kernfs_root *root = kernfs_root(parent); + /** + * We don't have address of kernfs_node (that is being searched) + * yet. Acquiring root->kernfs_topo_mutex and releasing it after + * pinning the found kernfs_node, ensures that found kernfs_node + * will not disappear due to a parallel remove operation. + */ + mutex_lock(&root->kernfs_topo_mutex); rwsem = kernfs_down_read(parent); kn = kernfs_find_ns(parent, name, ns); kernfs_get(kn); kernfs_up_read(rwsem); + mutex_unlock(&root->kernfs_topo_mutex); return kn; } @@ -901,11 +924,26 @@ struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent, { struct kernfs_node *kn; struct rw_semaphore *rwsem; + struct kernfs_root *root = kernfs_root(parent); + /** + * We don't have address of kernfs_node (that is being searched) + * yet. Acquiring root->kernfs_topo_mutex and releasing it after + * pinning the found kernfs_node, ensures that found kernfs_node + * will not disappear due to a parallel remove operation. + */ + mutex_lock(&root->kernfs_topo_mutex); rwsem = kernfs_down_read(parent); kn = kernfs_walk_ns(parent, path, ns); kernfs_get(kn); - kernfs_up_read(rwsem); + if (kn) + /* Release lock taken under kernfs_walk_ns */ + kernfs_up_read(kernfs_rwsem_ptr(kn)); + else + /* Release parent lock because walk_ns bailed out early */ + kernfs_up_read(rwsem); + + mutex_unlock(&root->kernfs_topo_mutex); return kn; } @@ -930,9 +968,9 @@ struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops, return ERR_PTR(-ENOMEM); idr_init(&root->ino_idr); - init_rwsem(&root->kernfs_rwsem); INIT_LIST_HEAD(&root->supers); init_rwsem(&root->supers_rwsem); + mutex_init(&root->kernfs_topo_mutex); /* * On 64bit ino setups, id is ino. On 32bit, low 32bits are ino. @@ -1102,6 +1140,11 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) } kn = kernfs_dentry_node(dentry); + /** + * For dentry revalidation just acquiring kernfs_node's rwsem for + * reading should be enough. If a competing rename or remove wins + * one of the checks below will fail. + */ rwsem = kernfs_down_read(kn); /* The kernfs node has been deactivated */ @@ -1141,24 +1184,35 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir, struct inode *inode = NULL; const void *ns = NULL; struct rw_semaphore *rwsem; + struct kernfs_root *root = kernfs_root(parent); + /** + * We don't have address of kernfs_node (that is being searched) + * yet. So take root->kernfs_topo_mutex to avoid parallel removal of + * found kernfs_node. + */ + mutex_lock(&root->kernfs_topo_mutex); rwsem = kernfs_down_read(parent); if (kernfs_ns_enabled(parent)) ns = kernfs_info(dir->i_sb)->ns; kn = kernfs_find_ns(parent, dentry->d_name.name, ns); + kernfs_up_read(rwsem); /* attach dentry and inode */ if (kn) { /* Inactive nodes are invisible to the VFS so don't * create a negative. */ + rwsem = kernfs_down_read(kn); if (!kernfs_active(kn)) { kernfs_up_read(rwsem); + mutex_unlock(&root->kernfs_topo_mutex); return NULL; } inode = kernfs_get_inode(dir->i_sb, kn); if (!inode) inode = ERR_PTR(-ENOMEM); + kernfs_up_read(rwsem); } /* * Needed for negative dentry validation. @@ -1166,9 +1220,11 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir, * or transforms from positive dentry in dentry_unlink_inode() * called from vfs_rmdir(). */ + rwsem = kernfs_down_read(parent); if (!IS_ERR(inode)) kernfs_set_rev(parent, dentry); kernfs_up_read(rwsem); + mutex_unlock(&root->kernfs_topo_mutex); /* instantiate and hash (possibly negative) dentry */ return d_splice_alias(inode, dentry); @@ -1348,19 +1404,26 @@ void kernfs_activate(struct kernfs_node *kn) static void __kernfs_remove(struct kernfs_node *kn) { struct kernfs_node *pos; + struct rw_semaphore *rwsem; + struct kernfs_root *root; /* Short-circuit if non-root @kn has already finished removal. */ if (!kn) return; - kernfs_rwsem_assert_held_write(kn); + root = kernfs_root(kn); /* * This is for kernfs_remove_self() which plays with active ref * after removal. */ - if (kn->parent && RB_EMPTY_NODE(&kn->rb)) + mutex_lock(&root->kernfs_topo_mutex); + rwsem = kernfs_down_write(kn); + if (kn->parent && RB_EMPTY_NODE(&kn->rb)) { + kernfs_up_write(rwsem); + mutex_unlock(&root->kernfs_topo_mutex); return; + } pr_debug("kernfs %s: removing\n", kn->name); @@ -1370,8 +1433,11 @@ static void __kernfs_remove(struct kernfs_node *kn) if (kernfs_active(pos)) atomic_add(KN_DEACTIVATED_BIAS, &pos->active); + kernfs_up_write(rwsem); + /* deactivate and unlink the subtree node-by-node */ do { + rwsem = kernfs_down_write(kn); pos = kernfs_leftmost_descendant(kn); /* @@ -1389,10 +1455,25 @@ static void __kernfs_remove(struct kernfs_node *kn) * error paths without worrying about draining. */ if (kn->flags & KERNFS_ACTIVATED) - kernfs_drain(pos); + kernfs_drain(pos, kn); else WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS); + kernfs_up_write(rwsem); + + /** + * By now node and all of its descendants have been deactivated + * Once a descendant has been drained, acquire its parent's lock + * and unlink it from parent's children rb tree. + * We drop kn's lock before acquiring pos->parent's lock to avoid + * deadlock that will happen if pos->parent and kn hash to same lock. + * Dropping kn's lock should be safe because it is in deactived state. + * Further root->kernfs_topo_mutex ensures that we will not have + * concurrent instances of __kernfs_remove + */ + if (pos->parent) + rwsem = kernfs_down_write(pos->parent); + /* * kernfs_unlink_sibling() succeeds once per node. Use it * to decide who's responsible for cleanups. @@ -1410,8 +1491,12 @@ static void __kernfs_remove(struct kernfs_node *kn) kernfs_put(pos); } + if (pos->parent) + kernfs_up_write(rwsem); kernfs_put(pos); } while (pos != kn); + + mutex_unlock(&root->kernfs_topo_mutex); } /** @@ -1422,14 +1507,10 @@ static void __kernfs_remove(struct kernfs_node *kn) */ void kernfs_remove(struct kernfs_node *kn) { - struct rw_semaphore *rwsem; - if (!kn) return; - rwsem = kernfs_down_write(kn); __kernfs_remove(kn); - kernfs_up_write(rwsem); } /** @@ -1531,9 +1612,11 @@ bool kernfs_remove_self(struct kernfs_node *kn) */ if (!(kn->flags & KERNFS_SUICIDAL)) { kn->flags |= KERNFS_SUICIDAL; + kernfs_up_write(rwsem); __kernfs_remove(kn); kn->flags |= KERNFS_SUICIDED; ret = true; + rwsem = kernfs_down_write(kn); } else { wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq; DEFINE_WAIT(wait); @@ -1588,11 +1671,17 @@ int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, rwsem = kernfs_down_write(parent); + /** + * Since the node being searched will be removed eventually, + * we don't need to take root->kernfs_topo_mutex. + * Even if a parallel remove succeeds, the subsequent __kernfs_remove + * will detect it and bail-out early. + */ kn = kernfs_find_ns(parent, name, ns); - if (kn) - __kernfs_remove(kn); kernfs_up_write(rwsem); + if (kn) + __kernfs_remove(kn); if (kn) return 0; @@ -1612,14 +1701,26 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, { struct kernfs_node *old_parent; const char *old_name = NULL; - struct rw_semaphore *rwsem; + struct kernfs_rwsem_token token; int error; + struct kernfs_root *root = kernfs_root(kn); /* can't move or rename root */ if (!kn->parent) return -EINVAL; - rwsem = kernfs_down_write(kn); + mutex_lock(&root->kernfs_topo_mutex); + old_parent = kn->parent; + kernfs_get(old_parent); + kernfs_down_write_triple_nodes(kn, old_parent, new_parent, &token); + while (old_parent != kn->parent) { + kernfs_put(old_parent); + kernfs_up_write_triple_nodes(kn, old_parent, new_parent, &token); + old_parent = kn->parent; + kernfs_get(old_parent); + kernfs_down_write_triple_nodes(kn, old_parent, new_parent, &token); + } + kernfs_put(old_parent); error = -ENOENT; if (!kernfs_active(kn) || !kernfs_active(new_parent) || @@ -1654,7 +1755,6 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, /* rename_lock protects ->parent and ->name accessors */ write_lock_irq(&kernfs_rename_lock); - old_parent = kn->parent; kn->parent = new_parent; kn->ns = new_ns; @@ -1673,7 +1773,8 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, error = 0; out: - kernfs_up_write(rwsem); + mutex_unlock(&root->kernfs_topo_mutex); + kernfs_up_write_triple_nodes(kn, new_parent, old_parent, &token); return error; } diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c index efe5ae98abf46..36a40b08b97fa 100644 --- a/fs/kernfs/inode.c +++ b/fs/kernfs/inode.c @@ -101,6 +101,12 @@ int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) int ret; struct rw_semaphore *rwsem; + /** + * Since we are only modifying the inode attribute, we just need + * to lock involved node. Operations that add or remove a node + * acquire parent's lock before changing the inode attributes, so + * such operations are also in sync with this interface. + */ rwsem = kernfs_down_write(kn); ret = __kernfs_setattr(kn, iattr); kernfs_up_write(rwsem); @@ -118,6 +124,12 @@ int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, if (!kn) return -EINVAL; + /** + * Since we are only modifying the inode attribute, we just need + * to lock involved node. Operations that add or remove a node + * acquire parent's lock before changing the inode attributes, so + * such operations are also in sync with .setattr backend. + */ rwsem = kernfs_down_write(kn); error = setattr_prepare(&init_user_ns, dentry, iattr); if (error) @@ -188,6 +200,10 @@ int kernfs_iop_getattr(struct user_namespace *mnt_userns, struct kernfs_node *kn = inode->i_private; struct rw_semaphore *rwsem; + /** + * As we are only reading ->iattr, acquiring kn's rwsem for + * reading is enough. + */ rwsem = kernfs_down_read(kn); spin_lock(&inode->i_lock); kernfs_refresh_inode(kn, inode); @@ -285,6 +301,10 @@ int kernfs_iop_permission(struct user_namespace *mnt_userns, kn = inode->i_private; + /** + * As we are only reading ->iattr, acquiring kn's rwsem for + * reading is enough. + */ rwsem = kernfs_down_read(kn); spin_lock(&inode->i_lock); kernfs_refresh_inode(kn, inode); diff --git a/fs/kernfs/kernfs-internal.c b/fs/kernfs/kernfs-internal.c new file mode 100644 index 0000000000000..80d7d64532fe3 --- /dev/null +++ b/fs/kernfs/kernfs-internal.c @@ -0,0 +1,259 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * This file provides inernal helpers for kernfs. + */ + +#include "kernfs-internal.h" + +static void kernfs_swap_rwsems(struct rw_semaphore **array, int i, int j) +{ + struct rw_semaphore *tmp; + + tmp = array[i]; + array[i] = array[j]; + array[j] = tmp; +} + +static void kernfs_sort_rwsems(struct kernfs_rwsem_token *token) +{ + struct rw_semaphore **array = &token->rwsems[0]; + + if (token->count == 2) { + if (array[0] == array[1]) + token->count = 1; + else if (array[0] > array[1]) + kernfs_swap_rwsems(array, 0, 1); + } else { + if (array[0] == array[1] && array[0] == array[2]) + token->count = 1; + else { + if (array[0] > array[1]) + kernfs_swap_rwsems(array, 0, 1); + + if (array[0] > array[2]) + kernfs_swap_rwsems(array, 0, 2); + + if (array[1] > array[2]) + kernfs_swap_rwsems(array, 1, 2); + + if (array[0] == array[1] || array[1] == array[2]) + token->count = 2; + } + } +} + +/** + * kernfs_down_write_double_nodes() - take hashed rwsem for 2 nodes + * + * @kn1: first kernfs_node of interest + * @kn2: second kernfs_node of interest + * @token: token to pass unlocking information to caller + * + * Acquire hashed rwsem for 2 nodes. Some operation may need to acquire + * hashed rwsems for 2 nodes (for example for a node and its parent). + * This function can be used in such cases. + * + * Return: void + */ +void kernfs_down_write_double_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_rwsem_token *token) +{ + struct rw_semaphore **array = &token->rwsems[0]; + + array[0] = kernfs_rwsem_ptr(kn1); + array[1] = kernfs_rwsem_ptr(kn2); + token->count = 2; + + kernfs_sort_rwsems(token); + + if (token->count == 1) { + /* Both nodes hash to same rwsem */ + down_write_nested(array[0], 0); + } else { + /* Both nodes hash to different rwsems */ + down_write_nested(array[0], 0); + down_write_nested(array[1], 1); + } +} + +/** + * kernfs_up_write_double_nodes - release hashed rwsem for 2 nodes + * + * @kn1: first kernfs_node of interest + * @kn2: second kernfs_node of interest + * @token: token to indicate unlocking information + * ->rwsems is a sorted list of rwsem addresses + * ->count contains number of unique locks + * + * Release hashed rwsems for 2 nodes + * + * Return: void + */ +void kernfs_up_write_double_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_rwsem_token *token) +{ + struct rw_semaphore **array = &token->rwsems[0]; + + if (token->count == 1) { + /* Both nodes hash to same rwsem */ + up_write(array[0]); + } else { + /* Both nodes hashe to different rwsems */ + up_write(array[0]); + up_write(array[1]); + } +} + +/** + * kernfs_down_read_double_nodes() - take hashed rwsem for 2 nodes + * + * @kn1: first kernfs_node of interest + * @kn2: second kernfs_node of interest + * @token: token to pass unlocking information to caller + * + * Acquire hashed rwsem for 2 nodes. Some operation may need to acquire + * hashed rwsems for 2 nodes (for example for a node and its parent). + * This function can be used in such cases. + * + * Return: void + */ +void kernfs_down_read_double_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_rwsem_token *token) +{ + struct rw_semaphore **array = &token->rwsems[0]; + + array[0] = kernfs_rwsem_ptr(kn1); + array[1] = kernfs_rwsem_ptr(kn2); + token->count = 2; + + kernfs_sort_rwsems(token); + + if (token->count == 1) { + /* Both nodes hash to same rwsem */ + down_read_nested(array[0], 0); + } else { + /* Both nodes hash to different rwsems */ + down_read_nested(array[0], 0); + down_read_nested(array[1], 1); + } +} + +/** + * kernfs_up_read_double_nodes - release hashed rwsem for 2 nodes + * + * @kn1: first kernfs_node of interest + * @kn2: second kernfs_node of interest + * @token: token to indicate unlocking information + * ->rwsems is a sorted list of rwsem addresses + * ->count contains number of unique locks + * + * Release hashed rwsems for 2 nodes + * + * Return: void + */ +void kernfs_up_read_double_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_rwsem_token *token) +{ + struct rw_semaphore **array = &token->rwsems[0]; + + if (token->count == 1) { + /* Both nodes hash to same rwsem */ + up_read(array[0]); + } else { + /* Both nodes hashe to different rwsems */ + up_read(array[0]); + up_read(array[1]); + } +} + +/** + * kernfs_down_write_triple_nodes() - take hashed rwsem for 3 nodes + * + * @kn1: first kernfs_node of interest + * @kn2: second kernfs_node of interest + * @kn3: third kernfs_node of interest + * @token: token to pass unlocking information to caller + * + * Acquire hashed rwsem for 3 nodes. Some operation may need to acquire + * hashed rwsems for 3 nodes (for example rename operation needs to + * acquire rwsem corresponding to node, its current parent and its future + * parent). This function can be used in such cases. + * + * Return: void + */ +void kernfs_down_write_triple_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_node *kn3, + struct kernfs_rwsem_token *token) +{ + struct rw_semaphore **array = &token->rwsems[0]; + + array[0] = kernfs_rwsem_ptr(kn1); + array[1] = kernfs_rwsem_ptr(kn2); + array[2] = kernfs_rwsem_ptr(kn3); + token->count = 3; + + kernfs_sort_rwsems(token); + + if (token->count == 1) { + /* All 3 nodes hash to same rwsem */ + down_write_nested(array[0], 0); + } else if (token->count == 2) { + /** + * Two nodes hash to same rwsem, and these + * will occupy consecutive places in array after + * sorting. + */ + down_write_nested(array[0], 0); + down_write_nested(array[2], 1); + } else { + /* All 3 nodes hashe to different rwsems */ + down_write_nested(array[0], 0); + down_write_nested(array[1], 1); + down_write_nested(array[2], 2); + } +} + +/** + * kernfs_up_write_triple_nodes - release hashed rwsem for 3 nodes + * + * @kn1: first kernfs_node of interest + * @kn2: second kernfs_node of interest + * @kn3: third kernfs_node of interest + * @token: token to indicate unlocking information + * ->rwsems is a sorted list of rwsem addresses + * ->count contains number of unique locks + * + * Release hashed rwsems for 3 nodes + * + * Return: void + */ +void kernfs_up_write_triple_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_node *kn3, + struct kernfs_rwsem_token *token) +{ + struct rw_semaphore **array = &token->rwsems[0]; + + if (token->count == 1) { + /* All 3 nodes hash to same rwsem */ + up_write(array[0]); + } else if (token->count == 2) { + /** + * Two nodes hash to same rwsem, and these + * will occupy consecutive places in array after + * sorting. + */ + up_write(array[0]); + up_write(array[2]); + } else { + /* All 3 nodes hashe to different rwsems */ + up_write(array[0]); + up_write(array[1]); + up_write(array[2]); + } +} diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index 0babc3dc4f4a3..8dc99875da32d 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -19,6 +19,20 @@ #include #include +/** + * Token for nested locking interfaces. + * + * rwsems: array of rwsems to acquire + * count: has 2 uses + * As input argument it specifies size of ->rwsems array + * As return argument it specifies number of unique rwsems + * present in ->rwsems array + */ +struct kernfs_rwsem_token { + struct rw_semaphore *rwsems[3]; + int count; +}; + struct kernfs_iattrs { kuid_t ia_uid; kgid_t ia_gid; @@ -46,8 +60,8 @@ struct kernfs_root { struct list_head supers; wait_queue_head_t deactivate_waitq; - struct rw_semaphore kernfs_rwsem; struct rw_semaphore supers_rwsem; + struct mutex kernfs_topo_mutex; }; /* +1 to avoid triggering overflow warning when negating it */ @@ -169,12 +183,13 @@ extern const struct inode_operations kernfs_symlink_iops; * kernfs locks */ extern struct kernfs_global_locks *kernfs_locks; +extern rwlock_t kernfs_rename_lock; static inline struct rw_semaphore *kernfs_rwsem_ptr(struct kernfs_node *kn) { - struct kernfs_root *root = kernfs_root(kn); + int idx = hash_ptr(kn, NR_KERNFS_LOCK_BITS); - return &root->kernfs_rwsem; + return &kernfs_locks->kernfs_rwsem[idx]; } static inline void kernfs_rwsem_assert_held(struct kernfs_node *kn) @@ -247,4 +262,30 @@ static inline void kernfs_up_read(struct rw_semaphore *rwsem) { up_read(rwsem); } + +void kernfs_down_write_double_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_rwsem_token *token); + +void kernfs_up_write_double_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_rwsem_token *token); + +void kernfs_down_read_double_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_rwsem_token *token); + +void kernfs_up_read_double_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_rwsem_token *token); + +void kernfs_down_write_triple_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_node *kn3, + struct kernfs_rwsem_token *token); + +void kernfs_up_write_triple_nodes(struct kernfs_node *kn1, + struct kernfs_node *kn2, + struct kernfs_node *kn3, + struct kernfs_rwsem_token *token); #endif /* __KERNFS_INTERNAL_H */ diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index 3c5334b74f369..b9b8cc2c16fd9 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -388,6 +388,14 @@ void kernfs_kill_sb(struct super_block *sb) kfree(info); } +static void __init kernfs_rwsem_init(void) +{ + int count; + + for (count = 0; count < NR_KERNFS_LOCKS; count++) + init_rwsem(&kernfs_locks->kernfs_rwsem[count]); +} + static void __init kernfs_mutex_init(void) { int count; @@ -402,6 +410,7 @@ static void __init kernfs_lock_init(void) WARN_ON(!kernfs_locks); kernfs_mutex_init(); + kernfs_rwsem_init(); } void __init kernfs_init(void) diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c index 9d41036025547..d71aa73acec87 100644 --- a/fs/kernfs/symlink.c +++ b/fs/kernfs/symlink.c @@ -113,12 +113,17 @@ static int kernfs_getlink(struct inode *inode, char *path) struct kernfs_node *kn = inode->i_private; struct kernfs_node *parent = kn->parent; struct kernfs_node *target = kn->symlink.target_kn; - struct rw_semaphore *rwsem; int error; - - rwsem = kernfs_down_read(parent); + unsigned long flags; + + /** + * kernfs_get_target_path needs that all nodes in the path don't + * undergo a parent change in the middle of it. Since ->parent + * change happens under kernfs_rename_lock, acquire the same. + */ + read_lock_irqsave(&kernfs_rename_lock, flags); error = kernfs_get_target_path(parent, target, path); - kernfs_up_read(rwsem); + read_unlock_irqrestore(&kernfs_rename_lock, flags); return error; } diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 367044d7708c6..7d9de9aee102a 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -89,6 +89,7 @@ struct kernfs_iattrs; */ struct kernfs_global_locks { struct mutex open_file_mutex[NR_KERNFS_LOCKS]; + struct rw_semaphore kernfs_rwsem[NR_KERNFS_LOCKS]; }; enum kernfs_node_type { From patchwork Thu Jul 14 00:22:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Imran Khan X-Patchwork-Id: 12917211 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 25349C43334 for ; Thu, 14 Jul 2022 00:23:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231342AbiGNAXP (ORCPT ); Wed, 13 Jul 2022 20:23:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231476AbiGNAXI (ORCPT ); Wed, 13 Jul 2022 20:23:08 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CA3C17AB5; Wed, 13 Jul 2022 17:22:52 -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 26E09RZg021473; Thu, 14 Jul 2022 00:22:49 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-2021-07-09; bh=TQcsHYOiQzrFR86kwcDg5R/L0gioZBwzsHJI+4HDMvA=; b=aFbysCsIYDLkuYE2RqDr921h0RA9gg01OHi90HhtHRBItrtsuMFAQeNHQnx6IHe3cB8M 2spoKXuFdWevLywbnCRqlFwh5RnsDCc+h+iEujs8AtqKg/7OTMTaeCl/1BKmwUUyoH/L 193E2sydJN0DCMVFy+VxG2qHppz54e07tjjMRWMBUiVmnjNR+31Z807aZIcFYtD0WkVm hxM4NFCxUXBEPQvMXfVliv8cSYj/TfUiJ42ZTYrQc+B0RTGWC6XzXZw2lGAvcbvzdn1i yWTppV1GiubpDP8Y8kh/db/jq3XEYu4QhmmY/oKOFSOkKwWmvst/dVdstXD0CfFqwRXG 6A== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h71rg3j6c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Jul 2022 00:22:48 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 26E0GteL031842; Thu, 14 Jul 2022 00:22:47 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2040.outbound.protection.outlook.com [104.47.66.40]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3h70457bqh-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Jul 2022 00:22:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OmLKOHsYSDB+lWX43GrtfKa+KuqwdrwAFicEnNmR7vxefH5+uRvGvsCk0MYF+vRivu0fD77OFqJDuH+pkVbr0sUN5TKwYzoRINVP5vvoE6QVBXJmFBgj3PRt7R9kzzbxoTGQsFoeYKJBscerXZupHRmzq1vqi9Sf+lCkVxwnl8tWqkfyv1SM9Atn62iNrObtwyLZ4OMawMIC4G3BipLoohXvnaYDkidaY2QrdetAxxSkKO51FScABbcX5rpnRQKVYbntT0AwSVEAUOTNt/6VQjukKmDxPs1Vimrjm/0JnilH3J5LkbEnBeWZPKYOjSQnlNc3gu0RfBApItec4f8Xhw== 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=TQcsHYOiQzrFR86kwcDg5R/L0gioZBwzsHJI+4HDMvA=; b=U/Zw2z9M2kWjrAo/iidxvrPhINLOsi9l2qnnIChp5tINN7p4pMMFyc5Wiqf5Iu7Pw3/wgo7U5si74OqlWUuh6A6FWIe30BOvR48iQX/vdqJvyB/fliq4Lu4BUYaID7y3AA2s5yKnHKJxcZUm06cUyKHImV/xy+/Iuf9cdF6n/B2mEynYlgiVOoE8/0Q73BoSMcXKIKqbNIza0ADI7mu00WXiiPGiFkyJtfOH43q7N9wbPRDlRUTj0TZin7OzxuSJo5OJXxhiALxyUExg9a1n0wGaGxNBjJFAw4sXU8WzmyXaYsjudTsKh2hTO+iQSj60p8yjpYel5HfCp1e9wPT4hw== 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=TQcsHYOiQzrFR86kwcDg5R/L0gioZBwzsHJI+4HDMvA=; b=U2DkH7L+k9pJT6ogYrJM/BStBNYHRRX60dHxZOGv8nqiDFJFH0RwA5W61tqsi1aUEgNyCzZS+g9uaRhXmh4scDghjpolyekrVyvzvlcyGG9lA+c3zxUJIb0gL+Wyn15qKifSLE2q0w58S0ujVlv7UnDs8gQRXbVZqdfI237wnmg= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by BY5PR10MB3986.namprd10.prod.outlook.com (2603:10b6:a03:1fb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.12; Thu, 14 Jul 2022 00:22:45 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::dd9d:e9dd:d864:524c]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::dd9d:e9dd:d864:524c%7]) with mapi id 15.20.5438.013; Thu, 14 Jul 2022 00:22:45 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 5/5] kernfs: Add a document to describe hashed locks used in kernfs. Date: Thu, 14 Jul 2022 10:22:24 +1000 Message-Id: <20220714002224.3641629-6-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220714002224.3641629-1-imran.f.khan@oracle.com> References: <20220714002224.3641629-1-imran.f.khan@oracle.com> X-ClientProxiedBy: SYCP282CA0011.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:80::23) To CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 51970c65-7765-43fb-5775-08da652ef994 X-MS-TrafficTypeDiagnostic: BY5PR10MB3986:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8tQrEtlEV1I/ivvsQEg0hMvtycPmzUqwrpeoH8FyEVSgat4GmIRYqOlhdSpt+l5rUlfKMUeJI3coTmKPVNyUrCD/7K2+v40KntjD94mn8HvncQ8INkg+M80vsNHFnQp1yxkwCtIG+yFx4oDkg2SsiDIdfYLyM3jMw8YGZuZ7MqWpwvB97ITY97X19ujHHc0eoaaW6cz99S1BtbQ7IYwo2817KYlNTHbLeDEe30K/V2Y7VwQ4fH5og2xmCi8n2Hrp8uXvJOKBfANC7s3VxEtskwNhgyBOTvCe3ob1iZlzmXUi67dnUOH8nxa1SNj+U/70LM2BuKgsmhOzde0Doz6QNo867TLWoH3SSfQCdVaPQm9YxWW8A7tAYwBtxO41ISrZZmROGDerRLbpQXHOZcP0ZyYdDUtP4BHBNDPc5ADrX2loIoGUpAnBofpDK1rgdDKKsYgMly2TWEYC9oPHCXsW31/wuPOIUt1qZk9b2ezXcTMMLf19EUmrhPyigXCGg8DbhUd3vmxqmfwV7hmzor9JWuHNRRiogEwVNskf7VswlzQuWb5GkcBe7WnTWD1qrOocnya8erjNfIDy7i9gm/fv5KmwLKsLxRtAlRo6UlZqu1LP6HFlS9O3W/9IMK1jYkHNVQxry8s3AQPm2i4euhxUR4gMLL8ddPcywNbZGJOxDrXiE3khH29vpMsoeOkjIqp8xxnd5tdFSTtnFqkv08hY8zmvP+yx1PsS0aJqkLKkbGD0YvDlnWnh6Y17gZPvwR8GMmtsAjpU4qeUtDYT45BU+i/Ye4H3B0SEFWviTR+Z8Rp9RjdxVDb0Z4pyG4nQf4Lf X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4468.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(366004)(346002)(39860400002)(136003)(396003)(376002)(26005)(478600001)(6666004)(52116002)(6506007)(6486002)(6512007)(66946007)(41300700001)(66476007)(8676002)(66556008)(86362001)(316002)(4326008)(83380400001)(38350700002)(1076003)(38100700002)(2616005)(186003)(36756003)(8936002)(5660300002)(103116003)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AhsNMf9Vbsc32H//rHFQf/XKbyiLk3gjY24Xo/+yVvLhQrczD8P2SF3B1AW5G51OkNjyUh1FgpcU9ZsSViV57oYCZR8IgqsSSGz8IKGWq5fVhvf/OArWOTXDm7pcWKfDRKxmEL5nb9Yj2Gn3x/7a/z0lt2qfN4KyteqQ7r09d9rCdsjrQbIkub5BZ8ZGmfCVEvwCFAwNOf0eJ0Bodk4XJGaPjbLtxB61tAxlfpiNgVPhyGB6F+9BH/PO0nfuLG43svH1z9fm57ijaF+Azn2zIwtvvwEvet5HlNzrBPF3zC0maddzRUg/bC3whxCL7MvGDREXtHqApv6HGzL04JvLSmply9x0rLB2W2CrMITLwxk0z7R2RsFQdBJJGOrYrnoLJPJVgiNmzqyj0qa5C34j9TrPxIMQNZ6bdNBuLb1PmzJ4Zx8K7E25jjuPQ7AWkDjk9Mhmv9TbygwflMrh5nbYOlEXZJtOhnDOxjmixcNjRP7oWIOh5TDiluSrVtFVECsgiNGhL4tFAHpOAX85tXPSYQyt8WrbM8JABrlP2AIz02CNWIRca5O9Em1A+dfK8zrpz08dYRkaA6Ft2dA3sEGx8Ml09s5By3T688ZMl5qO6TZs2ENU+sWyrAcBoKstzJ7WuYBiBzXzO5wmwwckpw/LvEygcEdM0FGzlna5unmhkPqAp9nzf9e0M0cx4fBY/B8MdTKRCU3Sl+4h+vtqBzc8zmr+xBX+xUsXw3KXvsmZK/63dkEhVG3pQrlBW4Qe9CvWjafPWD8nMPi3Ivnh69NII/Up1S7Sa20bl35A/JbZW2pRndYC/BxuY7AjhohE/2Yanpt7r0ED96r8NTs45dOmn3IyJGvhovR0b+3YR13co+btgX7p3ByzXdR+D9J2CEKZo6wbh5t/8avON/fdnMOoqDI68NE/Wkm/ZSmywzCVW5F4CByZfJaS8qXmlrJcSry6rk3SBOd8HsbxYMXE0RtLYtDE99ENcw48QPUc5mC/yygTBzChWU872+2vJMINvIglt/4hVMKPs68X2ClnVmHC6KMxpe+teS9dDzHgARc0GJWYKcB+zQ1NVDq5IIUxzdWVVQKppiY5UHcNHH31snlz402Kl/8D1cAq7QUz75Nff4LJi7uXzl4fLX3E6U9kLUsqYsG1r7JfOhuOSzT5FD0ba0ZPEbE1E62n7JUn+IWNWS/+bfa0SYWxaP4hfhG7mJQ72YkYI8C7uX7j7XrwNGpNclaA72AwtOJtI/FI0N7RYvw7+cA0hDTLgasymLImZp/ms7NxoAatDzWPI7AbBhXqrbBp1VHNxAc4tOWpCWDQ+PCwc1tIiOlC+laV7dol0C9hY5En8UhzfMMWLJ9Wdlx55VKstfs4OMwlYOodmii1T98ySOj4TjPKyr6dQmwtGgSn0xmRoFfKJhDmrjiCbMmMctc+VBLUImjI8dbcaUWr1d92zmmRZyWK2+QuE3lCdNz+8RSmEghLdgRjptnSRR+Jf31j6RqtFeal/pCVp+qor0dJgxrrUHNTf8KmlkQi4IUxoNHKqtLPKBGmLHsI8nAI5jdDHlexYafY2/g4mWGcZg7ILW1jydnogPUbvlVJDL/5QkLUp2uAv2omKyP2e4HHpg== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 51970c65-7765-43fb-5775-08da652ef994 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jul 2022 00:22:45.7277 (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: BgedZBM7oquJnLwHT0UV/4kBEGTOAJ5KmckLJWCb6FVcVd7El5lEXhAJielCQtYcU94EtIu1tUhXnONP0VMNHQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR10MB3986 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-13_13:2022-07-13,2022-07-13 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxlogscore=999 mlxscore=0 suspectscore=0 phishscore=0 spamscore=0 malwarescore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2207140000 X-Proofpoint-GUID: 8vOw77miQM3a3dKQhrL77nV1UtK5QGTg X-Proofpoint-ORIG-GUID: 8vOw77miQM3a3dKQhrL77nV1UtK5QGTg Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org This document describes usage and proof of various hashed locks introduced in this patch set. Signed-off-by: Imran Khan --- .../filesystems/kernfs-hashed-locks.rst | 214 ++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 Documentation/filesystems/kernfs-hashed-locks.rst diff --git a/Documentation/filesystems/kernfs-hashed-locks.rst b/Documentation/filesystems/kernfs-hashed-locks.rst new file mode 100644 index 0000000000000..a000e6fcf78ca --- /dev/null +++ b/Documentation/filesystems/kernfs-hashed-locks.rst @@ -0,0 +1,214 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +=================== +kernfs hashed locks +=================== + +kernfs uses following hashed locks + +1. Hashed mutexes +2. Hashed rwsem + +In certain cases hashed rwsem needs to work in conjunction with a per-fs mutex +(Described further below).So this document describes this mutex as well. + +A kernfs_global_locks object (defined below) provides hashed mutexes, +hashed spinlocks and hashed rwsems. + + struct kernfs_global_locks { + struct mutex open_file_mutex[NR_KERNFS_LOCKS]; + struct rw_semaphore kernfs_rwsem[NR_KERNFS_LOCKS]; + }; + +For all hashed locks address of a kernfs_node object acts as hashing key. + +For the remainder of this document a node means a kernfs_node object. The +node can refer to a file, directory or symlink of a kernfs based file system. +Also a node's mutex or rwsem refers to hashed mutex, or hashed rwsem +corresponding to the node. It does not mean any locking construct embedded in +the kernfs_node itself. + +What is protected by hashed locks +================================= + +(1) There's one kernfs_open_file for each open file and all kernfs_open_file + instances corresponding to a kernfs_node are maintained in a list. + hashed mutexes or kernfs_global_locks.open_file_mutex[index] protects + this list. + +(2) Hashed rwsems or kernfs_global_locks.kernfs_rwsem[index] protects node's + state and synchronizes operations that change state of a node or depend on + the state of a node. + +(3) per-fs mutex (mentioned earlier) provides synchronization between lookup + and remove operations. It also protects against topology change. + While looking for a node we will not have address of corresponding node + so we can't acquire node's rwsem right from the beginning. + On the other hand a parallel remove operation for the same node can acquire + corresponding rwsem and go ahead with node removal. So it may happen that + search operation for the node finds and returns it but before it can be + pinned or used, the remove operation, that was going on in parallel, removes + the node and hence makes its any future use wrong. + per-fs mutex ensures that for competing search and remove operations only + one proceeds at a time and since object returned by search is pinned before + releasing the per-fs mutex, it will be available for subsequent usage. + + This per-fs mutex also protects against topology change during path walks. + During path walks we need to acquire and release rwsems corresponding to + directories so that these directories don't move and their children RB tree + does not change. Since these rwsems can't be taken under a spinlock, + kernfs_rename_lock can't be used and needed protection against topology + change is provided by per-fs mutex. + +Lock usage and proof +======================= + +(1) Hashed mutexes + + Since hashed mutexes protect the list of kernfs_open_file instances + corresponding to a kernfs_node, ->open and ->release backends of + file_operations need to acquire hashed mutex corresponding to kernfs_node. + Also when a kernfs_node is removed, all of its kernfs_open_file instances + are drained after deactivating the node. This drain operation acquires + hashed mutex to traverse list of kernfs_open_file instances. + So addition (via ->open), deletion (via ->release) and traversal + (during kernfs_drain) of kernfs_open_file list occurs in a synchronous + manner. + +(2) Hashed rwsems + + 3.1. A node's rwsem protects its state and needs to be acquired to: + 3.1.a. Remove the node + 3.1.b. Move the node + 3.1.c. Travers or modify a node's children RB tree (for + directories), i.e to add/remove files/subdirectories + within/from a directory. + 3.1.d. Modify or access node's inode attributes + + 3.2. Hashed rwsems are used in following operations: + + 3.2.a. Addition of a new node + + While adding a new kernfs_node under a kernfs directory + kernfs_add_one acquires directory node's rwsem for + writing. Clause 3.1.a ensures that directory exists + throughout the operation. Clause 3.1.c ensures proper + updation of children rb tree (i.e ->dir.children). + Clause 3.1.d ensures correct modification of inode + attribute to reflect timestamp of this operation. + If the directory gets removed while waiting for semaphore, + the subsequent checks in kernfs_add_one will fail resulting + in early bail out from kernfs_add_one. + + 3.2.b. Removal of a node + + Removal of a node involves recursive removal of all of its + descendants as well. per-fs mutex (i.e kernfs_rm_mutex) avoids + concurrent node removals even if the nodes are different. + + At first node's rwsem is acquired. Clause 3.1.c avoids parallel + modification of descendant tree and while holding this rwsem + each of the descendants are deactivated. + + Once a descendant has been deactivated and drained, its parent's + rwsem is taken. Clause 3.1.c ensures proper unlinking of this + descendant from its siblings. Clause 3.1.d ensures that parent's + inode attributes are correctly updated to record time stamp of + removal. + + 3.2.c. Movement of a node + + Moving or renaming a node (kernfs_rename_ns) acquires rwsem for + node and its old and new parents. Clauses 3.1.b and 3.1.c avoid + concurrent move operations for the same node. + Also if old parent of a node changes while waiting for rwsem, + the acquisition of rwsem for 3 involved nodes is attempted + again. It is always ensured that as far as old parent is + concerned, rwsem corresponding to current parent is acquired. + + 3.2.d. Reading a directory + + For diectory reading kernfs_fop_readdir acquires directory + node's rwsem for reading. Clause 3.1.c ensures a consistent view + of children RB tree. + As far as directroy being read is concerned, if it gets removed + while waiting for semaphore, the for loop that iterates through + children will be ineffective. So for this operation acquiring + directory node's rwsem for reading is enough. + + 3.2.e. Dentry revalidation + + A dentry revalidation (kernfs_dop_revalidate) can happen for a + negative or for a normal dentry. + For negative dentries we just need to check parent change, so in + this case acquiring parent kernfs_node's rwsem for reading is + enough. + For a normal dentry acquiring node's rwsem for reading is enough + (Clause 3.1.a and 3.1.b). + If node gets removed while waiting for the lock subsequent checks + in kernfs_dop_revalidate will fail and kernfs_dop_revalidate will + exit early. + + 3.2.f. kernfs_node lookup + + While searching for a node under a given parent + (kernfs_find_and_get_ns, kernfs_walk_and_get_ns) rwsem of parent + node is acquired for reading. Clause 3.1.c ensures a consistent + view of parent's children RB tree. To avoid parallel removal of + found node before it gets pinned, these operation make use of + per-fs mutex (kernfs_rm_mutex) as explained earlier. + This per-fs mutex is also taken during kernfs_node removal + (__kernfs_remove). + + If the node being searched gets removed while waiting for the + mutex or rwsem, the subsequent kernfs_find_ns or kernfs_walk_ns + will fail. + + 3.2.g. kenfs_node's inode lookup + + Looking up for inode instances via kernfs_iop_lookup involves + node lookup. So locks acquired are same as ones required in 3.2.f. + Also once node lookup is complete parent's rwsem is released and + rwsem of found node is acquired to get corresponding inode. + Since we are operating under per-fs kernfs_rm_mutex the found node + will not disappear in the middle. + + 3.2.h. Updating or reading inode attribute + + Interfaces that change inode attributes(i.e kernfs_setattr and + kernfs_iop_setattr) acquire node's rwsem for writing. + If the kernfs_node gets removed while waiting for the semaphore + the subsequent __kernfs_setattr will fail. + From 3.2.a and 3.2.b we know that updates due to addition or + removal of nodes will not happen in parallel. + So just locking the kernfs_node in these cases is enough to + guarantee correct modification of inode attributes. + Similarly the interfaces that read inode attributes + (i.e kernfs_iop_getattr, kernfs_iop_permission) just need to + acquire involved node's rwsem for reading. + + 3.2.i. kernfs file event generation + + kernfs_notify pins involved node before scheduling + kernfs_notify_work and kernfs_notify_workfn acquires node's + rwsem. Clauses in 3.1 ensure a consistent view of node state + throughout execution of work handler. + + 3.2.j. mount + + kernfs_fill_super, invoked during mount operation, acquires root + node's rwsem. During mount process there can't be other execution + contexts trying to move or delete the node so just locking the + involved node(i.e the root node) is enough. + + 3.2.k. while activating a node + + For a node that started as deactivated, kernfs_activate + activates the node. In this case acquiring node's rwsem is + enough. Since the node is not active yet any parallel removal + that wins the race for rwsem will skip this node and its + descendents. Also user space can't see a deactivated node so we + don't have any parallel access emanating from their as well. + + 3.3 For operations that involve locking multiple nodes at the same time + locks are acquired in order of their addresses.