From nobody Sun May 10 17:53:08 2026 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 2E00BC433EF for ; Sun, 10 Apr 2022 02:37:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243385AbiDJCj6 (ORCPT ); Sat, 9 Apr 2022 22:39:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243374AbiDJCjx (ORCPT ); Sat, 9 Apr 2022 22:39:53 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FEEEC59 for ; Sat, 9 Apr 2022 19:37:43 -0700 (PDT) Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 239F0Mgf008887; Sun, 10 Apr 2022 02:37:38 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=iI/GmeI6Eq19dSmju4TNY88oGpTTMjcASSUsBMQd4dI=; b=KiXptsX+RHTKfW+8n5zoQKfuxotaFhzA9qhYojfTzlNQQswSPBPLYa3s5dtXhh/93epN 9uR9iHWOB8ITJ9r4bojvYsVXqrUABpQbIp5qNpu/SKpM/R5FKTn6L7ZoAH8yaqBhmiPe KJx6vLfogMW2/ME52pyvBVIbv20NXeoxgABlBLC1DaPZZ9JtXt6gGkee8XMLqRBybVCZ WRkU8lyFlyqlvKbiSMu/J8X6BMyPwrfXo+8UTI0vXAbNZXgALABz9nQjbkb52XCqL6mF JGMLlBDQ4ZnZcBMfgy9V0QLNujCgIyFNkgMBDX2wjrSkM6F2cV9vwi2IsLVkms3m4ln/ PQ== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com with ESMTP id 3fb0x290pw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:37 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 23A2VFxF034320; Sun, 10 Apr 2022 02:37:37 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2101.outbound.protection.outlook.com [104.47.58.101]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fb0k0gmt5-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:37 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=afLNwrWnWvuqTxPDYmT1wzo3DMXIENv08IWgBnHTehLHi/ClhO3M85lBb4LdEppvXLxKDdosIASyEaiTGxjfU3nT5AxxXnixk52oJ+u8O1ulhG1d/rRacIgYWwcD8A2TmK+Wkt/VwBv+s76ly1wIUybt9162sVR+Nkxvz91zYN3bX57zQFTjOohbUXIqN1f7AuO3Z+Pe3s6eL069YxieUPpEHYvtU0+cAI1rGaKtvP/LzAjeRUs+4xX7IqrJGcfdX2EECaDaijT6fumtXEG6sGlg4r6wJN32Keht9gBxs7awZ/5kzAyLWPPErfFzplu8H/S+2mUfu7vMBC+q6goBzQ== 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=iI/GmeI6Eq19dSmju4TNY88oGpTTMjcASSUsBMQd4dI=; b=RDmRpCbvOzabTXSeNrUotxz9OCdy8biFvRss94Odu74NXlntPRyulyc6pr/UsyNgNuL6kMjhtje9smLVWYPknZDsJcd1L8Xe6QMwtLuVLatk+ajo53lls7kvevWQsZ0F+IoIG8CkA3d5oveilMuIhK0uxVLsnr2FzVrd05uGGFpSm/+JbkxFCQRH74CnVRPALGHlT+zQXCAJZl1eeLy57RzS8xZKB4vyZ6aOSz+jYaLsR2R7o3IoUwlAzh3QqeB2HnkQfkeLWiXkKIyybgTzt+f7THI5m/rSpV7B5QKWPpXw7LL5CQUz/b/Bzi4b8NZs9C+lLlvf0Yu5qXxZ/QDNeQ== 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=iI/GmeI6Eq19dSmju4TNY88oGpTTMjcASSUsBMQd4dI=; b=D/3E3G5wIwT+m+DxWRbM7lInJN1Jtg1nC4NFxzVB8yR6n262C9zGF1w201fzDT6zBK0Lslye988zRUzBpTGyNLtlQOBYembVEWiCsD9JPCdyKJ9XQIUiiJEs1j4pAUPbDgBnsd3GG6jnPv/h3TOD3zwilYPfJuxOQ7bMw0sw1hU= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by CY4PR10MB1238.namprd10.prod.outlook.com (2603:10b6:910:7::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5144.28; Sun, 10 Apr 2022 02:37:35 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa%9]) with mapi id 15.20.5144.028; Sun, 10 Apr 2022 02:37:35 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org, ebiederm@xmission.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH v8 01/10] kernfs: Remove reference counting for kernfs_open_node. Date: Sun, 10 Apr 2022 12:37:10 +1000 Message-Id: <20220410023719.1752460-2-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220410023719.1752460-1-imran.f.khan@oracle.com> References: <20220410023719.1752460-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0054.ausprd01.prod.outlook.com (2603:10c6:10:2::18) 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: 3134378d-ece9-4381-ae61-08da1a9b120c X-MS-TrafficTypeDiagnostic: CY4PR10MB1238:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gxytJg89z68BIjCJuBzZy37aLO+PJdsqLUdWa9fIyqX7aF21oya5wA4OXPou6g8xK1hNumzNXCXcuKTOGQfef2LAgmc7Uil32gcEAZ27zGvbgTbzm0ob/d8v/0wp5gwOBdgl7Eo9FpbTFfo6/vAfPrFHb/wBhVYxjXIcvZR6egwSVhuxbcfH+1cyBEROat3vXzpwsBqwxFAvIH/t+wmHXzJEAd9SOZYRGIQlnhsA+wKZYf8OyqZ2qFmqW1n4WfXwSrJPA482kI5+ZkKt9CvjZwu+YzhzZOpXOGKBSlGRLBAOJ/UEGzWeqR6LKpctWZCwsgdY1wE4Pz2k40RxMgXSe6oJiTkPdRPEOIskcGThvheHaDgL5MqrIWHx1IV3rF9/xG93dSt/PpTVkf+sEsGAbc5RrbG770j3NtZdqaMKu4wfrYQagYC1JgJDfA0lpK+PJ74pCCuznPGIXR/YKk+ZDp+M8SOmhYXAYLWoh6ogJXlfOv52pvad4ILEFqUNNt9XrfEwI/8Z30v+FJr81g7dDjmEBj0/6cwRWmA71K//U9Mg4Qg9ncEz7Ai1TnqhbwiYFiQTx6h9R9QGtLJRXb9XJHyqNZZNnmgOQTCWhBZ94Yw47n5MRNGgOxK8iRrARPDqmWDzK2NgMJa4ZkWg46wZ4OQ0F0rcO/4hXGcL8IghAbhmRCAJQZeOQdJzcCY/GRAxVoJdYcPG1s6qh4D9r+gKlA== 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:(13230001)(366004)(1076003)(26005)(186003)(66946007)(66556008)(66476007)(8676002)(4326008)(83380400001)(36756003)(316002)(6486002)(103116003)(508600001)(8936002)(2616005)(6666004)(5660300002)(38100700002)(38350700002)(86362001)(2906002)(6512007)(6506007)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?6p+ma4joGRKpPblHEUFIv2rOwSv6z4jykr6DXeKb8ll+phsPVDHI/ku3mWfw?= =?us-ascii?Q?5GBgtmpER6f6MpPBLOAHCptgZGJM28EnuIB7vkTE4rXVxBhrU8uYcZea6ehz?= =?us-ascii?Q?uj5TChlO8tgWCjoUgZgYWC53gB0rJ9SUIXKa6zPCq0nEF6meju6lZ/GtQJ1f?= =?us-ascii?Q?kySwtC6VLgxPOhiWICospI3K5iRsLmzhWPD10PCRITkf909lNAM0iF5vDNmR?= =?us-ascii?Q?gpmZGpS5D198YBga55G+1558zW5H5nJPrwUSqVGPh5sPsIGp0RJA9jkrRGs5?= =?us-ascii?Q?BbbFc4614v/7wDBPtDOe15b2q5Y8JFdckVFiWt8ABknEqKxCvv7I9cod7SzR?= =?us-ascii?Q?953+6SHJkyFgj59GWsF6khBUEsV1gD3CAUZpyyCuB+t9+P/7+Xh0anVwOgRh?= =?us-ascii?Q?r7pGeeAPnc3xn2gpT+f2P3IxkGEkJTMfcU0POOr7Yo/63/Tylu3GjjJ/1NE1?= =?us-ascii?Q?4tJIaCf3QEjeMHdPJhOT4QAjaoYKcdCBOybA55q3n8YYONUDoE4kBy0En+Qn?= =?us-ascii?Q?38IwBzTvcdP3ozeGlK304/4Y5D6D/yJh1dEs+GALA8QC+K1eeLJPTFiawDSC?= =?us-ascii?Q?mFD2KknF5jv9VOw0CGQ65HKKi4IyxbDu+rVKJESPG++rm2Gk3cMUHAJOndco?= =?us-ascii?Q?nTRjs564cys4EasCewe8tF7P4Rt5kngWgXaje8RJm76aOPYtssuvNKv17Cpx?= =?us-ascii?Q?hP6Ju7ATyDJQzIQYDfBCkVX/SUAuDWrVnKxdgaEhr8y2tdsCdEcPD74yCAug?= =?us-ascii?Q?olDOSCekdZNfk4kT6Mpo2HMkXwEGjUhl/V4EkMxgrJpQWofkgtPCxzPVW+PJ?= =?us-ascii?Q?3vUsTMTi0QNB3hCzYNzzsvjdajOThsKJhtTySZ7CTM5JECx5nxjdioh8/w8J?= =?us-ascii?Q?eDLvR8kKrqcpoxNs27+rLJmf/ZwfsNT1qU07xkV3S9bHJX763Nu9LCpUKRom?= =?us-ascii?Q?G6M9xr1pB78cizwUWrLqL0vb45SMS4wEip4SOiBicj2AkcInK3AAvd/hzhU9?= =?us-ascii?Q?zswkk/EySfvNO/nvi8CvqUVoZXx2xnIj1x/wgz8MERLnqa1vFfqCAfyEoSM8?= =?us-ascii?Q?0looanmfyYlUxE8TuY2dxhUkxZd/LBk4sS7hSNKA37oCpNn6iY6s2q5SDPi7?= =?us-ascii?Q?fPRsYSHZhmG51xK1AwqjEkBI4pqP1JPBgnQKOTCIENepM6A478jU8w7hLfEM?= =?us-ascii?Q?87YEMfC1+pLfEP1lu/Ngsf0zsFO0VhCjpeYtahwERdm/HRBxyfdR+iZjVJBg?= =?us-ascii?Q?hZg6hxl6FfPFhux+94+IlV0n193CyZ1d7Xa5uJK0GIHbyuukPGnFwF2PxpYA?= =?us-ascii?Q?0WE4s96RSisR7fA4qpTArj0jJd2SgndteHSKsKdiX4vCw/Uoll1YDE9HngCp?= =?us-ascii?Q?RXBzXRPU3GpOSv5a/ED3O3++orOKYhbMHS5+Kdb/mQnpuXsTK7ojr6PQQO0x?= =?us-ascii?Q?oboBjfz8jf4xE+65Omypx+0NDcnZVzF4ZQ9a7fgEOuk6WZa14DBB8YwufJGU?= =?us-ascii?Q?+wJ1J1sZr3J6gVV55VC5EZY+iHLqCgNL69T0OaS8HTog/kP7eCgwdlPpNYp+?= =?us-ascii?Q?LK2o2NEYMEku7yvurCsOrBuE2vFBNizyV2lLclZKvauYH8lTYIoHYl6/bLpX?= =?us-ascii?Q?h67uN2+++isWr25WF8Lx5Uz3LqtV+Cyw2xbNO2a3UiFr0pcDYy5qWHrRVGhy?= =?us-ascii?Q?v3UFz8Ps+QmpuL7GpfwVCot+JgLQ6KtOc8uH5IgQRFxbtLBkBHyg6s1fdMpp?= =?us-ascii?Q?A/wWkBQ/SAcikybGAXI4Gu1BHwCrvcA=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3134378d-ece9-4381-ae61-08da1a9b120c X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Apr 2022 02:37:35.2670 (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: HQLyBmoQC6D1TuRf9wmx9sKy6XHyVIKoCMii17uHKEh0Kd10n7rXd8gn8kG9smvUScdrZzrTDdtuDeY7jSx05g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR10MB1238 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.858 definitions=2022-04-09_25:2022-04-08,2022-04-09 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxlogscore=999 mlxscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204100014 X-Proofpoint-ORIG-GUID: 0ifhU8ik35TUft6en-SInwe0B3AFYT6_ X-Proofpoint-GUID: 0ifhU8ik35TUft6en-SInwe0B3AFYT6_ Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The decision to free kernfs_open_node object in kernfs_put_open_node can be taken based on whether kernfs_open_node->files list is empty or not. As far as kernfs_drain_open_files is concerned it can't overlap with kernfs_fops_open and hence can check for ->attr.open optimistically (if ->attr.open is NULL) or under kernfs_open_file_mutex (if it needs to traverse the ->files list.) Thus kernfs_drain_open_files can work w/o ref counting involved kernfs_open_node as well. So remove ->refcnt and modify the above mentioned users accordingly. Suggested by: Al Viro Signed-off-by: Imran Khan --- fs/kernfs/file.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 88423069407c..aea6968c979e 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -33,7 +33,6 @@ static DEFINE_SPINLOCK(kernfs_open_node_lock); static DEFINE_MUTEX(kernfs_open_file_mutex); =20 struct kernfs_open_node { - atomic_t refcnt; atomic_t event; wait_queue_head_t poll; struct list_head files; /* goes through kernfs_open_file.list */ @@ -530,10 +529,8 @@ static int kernfs_get_open_node(struct kernfs_node *kn, } =20 on =3D kn->attr.open; - if (on) { - atomic_inc(&on->refcnt); + if (on) list_add_tail(&of->list, &on->files); - } =20 spin_unlock_irq(&kernfs_open_node_lock); mutex_unlock(&kernfs_open_file_mutex); @@ -548,7 +545,6 @@ static int kernfs_get_open_node(struct kernfs_node *kn, if (!new_on) return -ENOMEM; =20 - atomic_set(&new_on->refcnt, 0); atomic_set(&new_on->event, 1); init_waitqueue_head(&new_on->poll); INIT_LIST_HEAD(&new_on->files); @@ -557,11 +553,12 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, =20 /** * kernfs_put_open_node - put kernfs_open_node - * @kn: target kernfs_nodet + * @kn: target kernfs_node * @of: associated kernfs_open_file * * Put @kn->attr.open and unlink @of from the files list. If - * reference count reaches zero, disassociate and free it. + * list of associated open files becomes empty, disassociate and + * free kernfs_open_node. * * LOCKING: * None. @@ -578,7 +575,7 @@ static void kernfs_put_open_node(struct kernfs_node *kn, if (of) list_del(&of->list); =20 - if (atomic_dec_and_test(&on->refcnt)) + if (list_empty(&on->files)) kn->attr.open =3D NULL; else on =3D NULL; @@ -768,15 +765,15 @@ void kernfs_drain_open_files(struct kernfs_node *kn) if (!(kn->flags & (KERNFS_HAS_MMAP | KERNFS_HAS_RELEASE))) return; =20 - spin_lock_irq(&kernfs_open_node_lock); on =3D kn->attr.open; - if (on) - atomic_inc(&on->refcnt); - spin_unlock_irq(&kernfs_open_node_lock); if (!on) return; =20 mutex_lock(&kernfs_open_file_mutex); + if (!kn->attr.open) { + mutex_unlock(&kernfs_open_file_mutex); + return; + } =20 list_for_each_entry(of, &on->files, list) { struct inode *inode =3D file_inode(of->file); @@ -789,8 +786,6 @@ void kernfs_drain_open_files(struct kernfs_node *kn) } =20 mutex_unlock(&kernfs_open_file_mutex); - - kernfs_put_open_node(kn, NULL); } =20 /* --=20 2.30.2 From nobody Sun May 10 17:53:08 2026 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 B48E5C433EF for ; Sun, 10 Apr 2022 02:37:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243410AbiDJCkF (ORCPT ); Sat, 9 Apr 2022 22:40:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243402AbiDJCjz (ORCPT ); Sat, 9 Apr 2022 22:39:55 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CADBA43489 for ; Sat, 9 Apr 2022 19:37:45 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 239FZSw1029058; Sun, 10 Apr 2022 02:37: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=AvWvzVdfHaC18TpjvuIW94HOcyxivUQkHg1n/ZppXtY=; b=vqcRAFl5B7UsFzaeouSPqKO9Y7hyRlcOACUgZmrLiKsw+TQREdOxM/h5elhWaFN98Q4W oXXM6YtbS4Yj4HhjfYrrKBtOe42l4AeIekfqgjiBg/LHnJD4wMpt5JmiFH7aySWDEFVu l1MYtlBr3tD9GLsusvc9xXpxq0sWGvRWAdnJPuOTr6hsHsYGS4ZgC7zwyBP7624lW30x FJoGArkqO+VEUK5mYtd0nOPX+7woqtYu1NiHroUmxY4SSBvYepC1GZL6f1z/549tThNF AYGnPIvoW42ejjQr4DQTZPjn0NFtnis+EmmXT/JqfXYPdwGjbO4jahKK+fdNW4+9mzVK kQ== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com with ESMTP id 3fb1rs0yqx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:40 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 23A2V1Pj000864; Sun, 10 Apr 2022 02:37:39 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2104.outbound.protection.outlook.com [104.47.58.104]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3fb0k0tn7q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:39 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=d6AbXw75qcKE3va9QBR6X2FdnAhDRRFG67b4ZtqyIF9kgn36zEne3/LG/37Oc2wOXLtkUgAzbvg69YeOjww7p/c3+Ftx19oUV5GGaKBYPYMEiDCFLG3+kFbLZ7QBfbpc3F/gnWPxnU1N2lZaIbBNnRfL6twY1+HY3w9gKAyP7uNArvseE4CDrJrzrJaSnBQ8fvlIoF1VAU3a0FMWI8aZCVygR9Ok7VS9B7AZuvyMJQM/DNqqTCZSV1B1Bd5Knyt4oPeEyvZD7a/z/KuxiXWCpRizTVYaYlzZrtMdmD23PjfqlJ5j+Mbl4ExUOO4lageTAlShywAtzsaTJZKNsVke+g== 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=AvWvzVdfHaC18TpjvuIW94HOcyxivUQkHg1n/ZppXtY=; b=RBCMSNRDV6MGSFINkYH/ZQn8LDpdDTb9pFe5e2kbGJhrLah+ALs4yaOX1fKrLVEC4zjzzn3dfkrolPDLTdtt5efVwkYMqFWS9J5QYRpsyrskOeiVOsZ9nkoeJ1O89oLBiJXTjvTawok6Vs4NlLGh87NJ58OUlzW59Pa3zROLnW6+mZbxRfntXOEtGCcZ4UjUr5pcOuabARqPFn0aVEppfSZO5dpisB7+qfcDq6iAxsTRc/WfJf/mMMeexMBV2UxjFrSn7YDQn4Uca53UAFQX5gqTdPvrd5/xsVbUwuplBVq15RFbZR3J60VoZuDAQeyrW5XoO/BCefyoVYUhvXLP8A== 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=AvWvzVdfHaC18TpjvuIW94HOcyxivUQkHg1n/ZppXtY=; b=PvHvvV6VB39XaQfCwNl0Ogb0xGE3bFzrdYaWSik9NWNhEqzK3LKRsVahGYLx6njCCQ4McMKA7TLytzZ1ZHikfmhnW5kXuAJjTUmFIsWsWnQiIln+XbvTlIEo/rdUxQMFSAVZTMhIGPjJ/KieVvVuScywP8tb/RdV9wiJMuYbIDc= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by CY4PR10MB1238.namprd10.prod.outlook.com (2603:10b6:910:7::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5144.28; Sun, 10 Apr 2022 02:37:37 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa%9]) with mapi id 15.20.5144.028; Sun, 10 Apr 2022 02:37:37 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org, ebiederm@xmission.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH v8 02/10] kernfs: make ->attr.open RCU protected. Date: Sun, 10 Apr 2022 12:37:11 +1000 Message-Id: <20220410023719.1752460-3-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220410023719.1752460-1-imran.f.khan@oracle.com> References: <20220410023719.1752460-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0054.ausprd01.prod.outlook.com (2603:10c6:10:2::18) 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: eb3e2586-844f-4acc-ab70-08da1a9b1317 X-MS-TrafficTypeDiagnostic: CY4PR10MB1238:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bN12NO+mWytT9McQulJ7pAdM+Koj7TQwW4vdGG35caMrFr41xnkcJXmNze2m1fazPhb6Xj/DacKnICKYoEF8CoxJCREL62kho53IXl7GVlVDsZ/Pfm3egGO+Lh4dHbMdtKgKu4RkwWbr/kVvgo7JVuTa5diSVwervGCb9IkNK0ifjNeAvHTDZvrJSyZZiLMDVdGDW4uGi233ydbVS3pQhhpDlEALrQhnLtSZDtyx+11oxrmaN6hPDJ2+y5KPpFa5tLGgI4MFZ5KkHRLtzOMA1uJ0w11KxhXz2e7WR/D4uOV8D5CdSaAWEUaBgvFKPVywyk3oUJgM1vimcQDaZko6YPff++d7TcmKoCKWd5PNAO/qTHRItwJFhXHWjw+uDI2bhXp+zVzyy5FOXmfsSzVn/tMOJtJ4Rm5nSYM5wUVW44dAJ8mKGfKOjBbBS2bAFmCPvKprooOHlAVRB0/wz2xX63HnM1B6oZt6HfCOhz5tl82oNEW1VENqaKwUj0TUQOPcgV5ry/3OduGPEcOswRr01yK0/BvUTlCb+LlEsxz2dV8cJMoDphv98qBsDYedStwG8HEeisjWOJZPjbmIEQGZtWO6KWZjT+azyeR8GRgMVQ4mZ62247MyMMyxpZc9JNeKfhhcew0RQElvqhjRTY/YXP94NSs0btT3H415Y6/lI57/5ZFxZXLe5EgG+ody9MDQsmvcKkNTgtKiRcQj9Mr6PQ== 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:(13230001)(366004)(1076003)(26005)(186003)(66946007)(66556008)(66476007)(8676002)(4326008)(83380400001)(36756003)(316002)(6486002)(103116003)(508600001)(8936002)(2616005)(6666004)(5660300002)(38100700002)(38350700002)(86362001)(2906002)(6512007)(6506007)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?az/j3Nn5Zo2qB+JxIsxQoOk/Ap7ws2Kh3LXZUzj+RLhzdUMXg/sNG4/aW41C?= =?us-ascii?Q?umR9h3ESjho3xvWgb0T9JA08X0cyTfv8q8GLe5vUYBI3rjQa78uhAfI79kU6?= =?us-ascii?Q?QNcpZa+MWZ1zZLPWZqxPzelPEC0NJ3nSrjNDI/kAI/+KJ+KiX7Hx7sD6R2Mu?= =?us-ascii?Q?Pl0eb9UgZVmGL8UKXopdAv/iXpw/dz63DMg3tLU4fHjGXOETrp4TpbtR+SEL?= =?us-ascii?Q?asieut4e/vYu8zA/OlLXPd8Ins3K1GRrjURUCaDx8M17zpjtyLbQRrRIITmL?= =?us-ascii?Q?9OOU8LLDLuieywzUiafBsW3ImAByeofBKdO97km6zC9TCsfcuzDDnFG2cDQa?= =?us-ascii?Q?akvmtUdMx753NEg0s1CbpoCMIcaucA/62twMzE/mAGS55gnE//iPK0ZheXYa?= =?us-ascii?Q?etuWfLgv9yAOg25cHoWN+Hy8b4HzCVA8U4pcSeHH9KNkWPlPfUeDvGbyDutv?= =?us-ascii?Q?t6AQZoMYRKK87GaZt1twVqPQ4+PIMYEz4+ugsYCNANRcGHPRL66eyCWWQCI/?= =?us-ascii?Q?+ig0L4EviRnFeZmPeoaaflppLpt6LSWpbrkX/7Ai5DkmGkQw3/B0L9F7iZ2E?= =?us-ascii?Q?KDmwU1xQJsdXnL2OVhOC9wydG7e69QPMZBo4meHRTQIUYzpFnKb7JSgaWxPT?= =?us-ascii?Q?7sALWcuFesQtT7iWtJ2w1qc17rMbKBpZQtzPqUpUQYS3Y379m/C+/zEgKMaL?= =?us-ascii?Q?/EhRi12wJMbQJlZCHq2Ul9mOHqVZaa2ULprukrYyl3j5zJ/v2GWrgN7Bu24w?= =?us-ascii?Q?UXsE0kErC5CNqWn0jBcvxS7z4sZfngFA6e9U3IQxgqt9Pxq+Eg5Ps4218/Kv?= =?us-ascii?Q?Y2Sf390IeNhPaOO9fKItslaGEEsv6I8VhLuM388ifNWagaNNkLu80PDk7gj9?= =?us-ascii?Q?Q5g3RRXmLhy33++dw9r9mkzK+vo8kl5jTeKGjPyJgXIsdGj4a+P9DWdpPv9h?= =?us-ascii?Q?nxiuUwBlXIz22BAis6cQjqJckKtd3clJsiiE4y86Ye0psrEQciXnghuz7jsk?= =?us-ascii?Q?34wtd0Bu/25tUPEUSuieV1o5cQKYfHGC/qMrpKxc2iLCfMGaRb5kFLgtWFrN?= =?us-ascii?Q?Dx0eVMJKMlpFvkcJNEa9T0PtdGjBAWWsO114ELa3viKMcNXfZh0WXbQisS/i?= =?us-ascii?Q?02tSKCRVGZIe6qNDAnAM/gg3SJFD9eBycOKIZwNhFrlYN012auyELW1dohs5?= =?us-ascii?Q?CffMk5jAaIhL5eHitG9/jUxEkHO0dKuDiF2cyFydZdkUCWVxCykwkwQby5LD?= =?us-ascii?Q?skN/f9s5vRq2JAsaqjzSSKe+VyCpLNeVfmTwFeIF6GgXsYKsLONtqBJSiwKk?= =?us-ascii?Q?4wowF7/KCtg4hyEPCdxtsunJ7tb2uJtcH0CCWyiV06Gj5TWSXHQDYLmU9hF9?= =?us-ascii?Q?kiFWtz9N7NMUlySWmiOY6fuN2iuw3ryZhmzr8djc6SXV/3OvZh3ivcMfhfHc?= =?us-ascii?Q?4b6+w6QQoYn6xbp/tqPiL2sIeyF2REfxphl5OXzZO+byRIufUaY2E0ba5nHB?= =?us-ascii?Q?nglHNyP60Pphb59SFOlNVBJFVep08wsiqhKSATq0flUKa+8vkgn8HzgHMW5l?= =?us-ascii?Q?NJemF92ZTv5U091auvT3Ly6mPvLaBrpIyRyrY7wWc4DVlrlB099T3S6WFGYk?= =?us-ascii?Q?szfsLHOSCQrRBwg9gE7v1IyVhgM9U0UUBBoppd/0sY6H3oc8hh3wJw5dD2uO?= =?us-ascii?Q?080MDMrwqVk/+Lda68x9ELZR3a13/uO3iNWWnW/Gx9ZlZsN3/rLj1Q6sZ9g2?= =?us-ascii?Q?YVl8LJiubnXIadzndqjKh2sFwUWgSis=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: eb3e2586-844f-4acc-ab70-08da1a9b1317 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Apr 2022 02:37:37.2041 (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: siZ20musZWQx3aOfurZ9wtSuxnQ/XnaJgm3cdSwrdiatdCASh+i5oVZKzRhRZMxaoiWT3ygRbzVhhri/oBRrKw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR10MB1238 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.858 definitions=2022-04-09_25:2022-04-08,2022-04-09 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 adultscore=0 phishscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204100014 X-Proofpoint-ORIG-GUID: KYVYt-KbzWIwBA59itLAo8qENGE5RelR X-Proofpoint-GUID: KYVYt-KbzWIwBA59itLAo8qENGE5RelR Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" After removal of kernfs_open_node->refcnt in the previous patch, kernfs_open_node_lock can be removed as well by making ->attr.open RCU protected. kernfs_put_open_node can delegate freeing to ->attr.open to RCU and other readers of ->attr.open can do so under rcu_read_(un)lock. So make ->attr.open RCU protected and remove global kernfs_open_node_lock. Suggested by: Al Viro Signed-off-by: Imran Khan --- fs/kernfs/file.c | 101 ++++++++++++++++++++++------------------- include/linux/kernfs.h | 2 +- 2 files changed, 55 insertions(+), 48 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index aea6968c979e..bc393dcf4efa 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -23,16 +23,16 @@ * for each kernfs_node with one or more open files. * * kernfs_node->attr.open points to kernfs_open_node. attr.open is - * protected by kernfs_open_node_lock. + * RCU protected. * * filp->private_data points to seq_file whose ->private points to * kernfs_open_file. kernfs_open_files are chained at * kernfs_open_node->files, which is protected by kernfs_open_file_mutex. */ -static DEFINE_SPINLOCK(kernfs_open_node_lock); static DEFINE_MUTEX(kernfs_open_file_mutex); =20 struct kernfs_open_node { + struct rcu_head rcu_head; atomic_t event; wait_queue_head_t poll; struct list_head files; /* goes through kernfs_open_file.list */ @@ -156,8 +156,9 @@ static void kernfs_seq_stop(struct seq_file *sf, void *= v) static int kernfs_seq_show(struct seq_file *sf, void *v) { struct kernfs_open_file *of =3D sf->private; + struct kernfs_open_node *on =3D rcu_dereference_raw(of->kn->attr.open); =20 - of->event =3D atomic_read(&of->kn->attr.open->event); + of->event =3D atomic_read(&on->event); =20 return of->kn->attr.ops->seq_show(sf, v); } @@ -180,6 +181,7 @@ static ssize_t kernfs_file_read_iter(struct kiocb *iocb= , struct iov_iter *iter) struct kernfs_open_file *of =3D kernfs_of(iocb->ki_filp); ssize_t len =3D min_t(size_t, iov_iter_count(iter), PAGE_SIZE); const struct kernfs_ops *ops; + struct kernfs_open_node *on; char *buf; =20 buf =3D of->prealloc_buf; @@ -201,7 +203,8 @@ static ssize_t kernfs_file_read_iter(struct kiocb *iocb= , struct iov_iter *iter) goto out_free; } =20 - of->event =3D atomic_read(&of->kn->attr.open->event); + on =3D rcu_dereference_raw(of->kn->attr.open); + of->event =3D atomic_read(&on->event); ops =3D kernfs_ops(of->kn); if (ops->read) len =3D ops->read(of, buf, len, iocb->ki_pos); @@ -519,36 +522,34 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, { struct kernfs_open_node *on, *new_on =3D NULL; =20 - retry: mutex_lock(&kernfs_open_file_mutex); - spin_lock_irq(&kernfs_open_node_lock); - - if (!kn->attr.open && new_on) { - kn->attr.open =3D new_on; - new_on =3D NULL; - } - - on =3D kn->attr.open; - if (on) - list_add_tail(&of->list, &on->files); - - spin_unlock_irq(&kernfs_open_node_lock); - mutex_unlock(&kernfs_open_file_mutex); =20 + /** + * ->attr.open changes under kernfs_open_file_mutex so we don't + * need rcu_read_lock to ensure its existence. + */ + on =3D rcu_dereference_protected(kn->attr.open, + lockdep_is_held(&kernfs_open_file_mutex)); if (on) { - kfree(new_on); + list_add_tail(&of->list, &on->files); + mutex_unlock(&kernfs_open_file_mutex); return 0; + } else { + /* not there, initialize a new one and retry */ + new_on =3D kmalloc(sizeof(*new_on), GFP_KERNEL); + if (!new_on) { + mutex_unlock(&kernfs_open_file_mutex); + return -ENOMEM; + } + atomic_set(&new_on->event, 1); + init_waitqueue_head(&new_on->poll); + INIT_LIST_HEAD(&new_on->files); + list_add_tail(&of->list, &new_on->files); + rcu_assign_pointer(kn->attr.open, new_on); } + mutex_unlock(&kernfs_open_file_mutex); =20 - /* not there, initialize a new one and retry */ - new_on =3D kmalloc(sizeof(*new_on), GFP_KERNEL); - if (!new_on) - return -ENOMEM; - - atomic_set(&new_on->event, 1); - init_waitqueue_head(&new_on->poll); - INIT_LIST_HEAD(&new_on->files); - goto retry; + return 0; } =20 /** @@ -566,24 +567,30 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, static void kernfs_put_open_node(struct kernfs_node *kn, struct kernfs_open_file *of) { - struct kernfs_open_node *on =3D kn->attr.open; - unsigned long flags; + struct kernfs_open_node *on; + + /* ->attr.open NULL means there are no more open files */ + if (rcu_dereference_raw(kn->attr.open) =3D=3D NULL) + return; =20 mutex_lock(&kernfs_open_file_mutex); - spin_lock_irqsave(&kernfs_open_node_lock, flags); + + on =3D rcu_dereference_protected(kn->attr.open, + lockdep_is_held(&kernfs_open_file_mutex)); + + if (!on) { + mutex_unlock(&kernfs_open_file_mutex); + return; + } =20 if (of) list_del(&of->list); =20 - if (list_empty(&on->files)) - kn->attr.open =3D NULL; - else - on =3D NULL; - - spin_unlock_irqrestore(&kernfs_open_node_lock, flags); + if (list_empty(&on->files)) { + rcu_assign_pointer(kn->attr.open, NULL); + kfree_rcu(on, rcu_head); + } mutex_unlock(&kernfs_open_file_mutex); - - kfree(on); } =20 static int kernfs_fop_open(struct inode *inode, struct file *file) @@ -765,12 +772,13 @@ void kernfs_drain_open_files(struct kernfs_node *kn) if (!(kn->flags & (KERNFS_HAS_MMAP | KERNFS_HAS_RELEASE))) return; =20 - on =3D kn->attr.open; - if (!on) + if (rcu_dereference_raw(kn->attr.open) =3D=3D NULL) return; =20 mutex_lock(&kernfs_open_file_mutex); - if (!kn->attr.open) { + on =3D rcu_dereference_check(kn->attr.open, + lockdep_is_held(&kernfs_open_file_mutex)); + if (!on) { mutex_unlock(&kernfs_open_file_mutex); return; } @@ -805,7 +813,7 @@ void kernfs_drain_open_files(struct kernfs_node *kn) __poll_t kernfs_generic_poll(struct kernfs_open_file *of, poll_table *wait) { struct kernfs_node *kn =3D kernfs_dentry_node(of->file->f_path.dentry); - struct kernfs_open_node *on =3D kn->attr.open; + struct kernfs_open_node *on =3D rcu_dereference_raw(kn->attr.open); =20 poll_wait(of->file, &on->poll, wait); =20 @@ -912,14 +920,13 @@ void kernfs_notify(struct kernfs_node *kn) return; =20 /* kick poll immediately */ - spin_lock_irqsave(&kernfs_open_node_lock, flags); - on =3D kn->attr.open; + rcu_read_lock(); + on =3D rcu_dereference(kn->attr.open); if (on) { atomic_inc(&on->event); wake_up_interruptible(&on->poll); } - spin_unlock_irqrestore(&kernfs_open_node_lock, flags); - + rcu_read_unlock(); /* schedule work to kick fsnotify */ spin_lock_irqsave(&kernfs_notify_lock, flags); if (!kn->attr.notify_next) { diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index e2ae15a6225e..13f54f078a52 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -114,7 +114,7 @@ struct kernfs_elem_symlink { =20 struct kernfs_elem_attr { const struct kernfs_ops *ops; - struct kernfs_open_node *open; + struct kernfs_open_node __rcu *open; loff_t size; struct kernfs_node *notify_next; /* for kernfs_notify() */ }; --=20 2.30.2 From nobody Sun May 10 17:53:08 2026 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 1DED5C433F5 for ; Sun, 10 Apr 2022 02:38:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243446AbiDJCkP (ORCPT ); Sat, 9 Apr 2022 22:40:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243403AbiDJCjz (ORCPT ); Sat, 9 Apr 2022 22:39:55 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D13D24551D for ; Sat, 9 Apr 2022 19:37:45 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 239FdvW0031973; Sun, 10 Apr 2022 02:37:41 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=FSkZ381vqEqWfmePG2cYHBbVixRa9cGXw5IfNENUE1s=; b=KZnUYxgGcrJ+3cCP1ZheRHXLtQV7oGzmPSU+9qHJce6LvieNbJIrAjeqAKewPZNsgm2M Zl/caZYCR0E8cBXD2sdDtZ+sXrtG15Uz3NNEBUQzajN7W4mSJtiOTB+J5Bw9E2+D80Qm ZD9IEb5xa+9uwAM296IoMy0G1yRUoHbB7AKybpnimG1O5cGcO2G1kw1mcr1VocVej0L+ RJePZ75LpTL+XHBqgVGkaka1WeEXT6ZW7NU+MjAOoJJmvVHJ5zTseKNVvM2lvbLnDyaI Lgn++mcCdwNQgrCbAGstRZrKM2bXbS90qH6YwYq/9/KH7YrOsTMqObGtZCgsLHDR2CT4 Fw== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com with ESMTP id 3fb0jd110w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:41 +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 23A2UhNM024205; Sun, 10 Apr 2022 02:37:41 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2104.outbound.protection.outlook.com [104.47.58.104]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fb0k0h5gb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YwXVhiEO87iv0jr2iCeZg9W1mhxfdanAKn4/d/Dm5kLJiJ7K6rcnNgTZAYGYR4uNv0yYg5PgflgzhtyNmx+ZmwuILXaaiicCcZicZJ/X9FLZPfP1VD3gRDBABhsCJjN+DJK2kVRvBfTDPBaaWCZl3cdDk2sla+EZOcTkRy40CDsWyxrhSVpPutAkK0Ui17KWZKATEgngn5cdBfTVQ9hRaoV3XPBAu1rOAGxO0ZzBZ0WhuQtpWZzizKHcVCL8/IkvX/cs6ouJYMvaz/JBWDre1uUcncjuMIb73qrnPV9sOTNFWJ1GliB2+EbySd5kKGkMi4NVJAbFNpfUO+RIqoHVdQ== 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=FSkZ381vqEqWfmePG2cYHBbVixRa9cGXw5IfNENUE1s=; b=A4u3c9fcK8wkr+lHiDcxAqtBcOwH+JJG1HCCDN3sHGYI+xT2IChDwp3O8HqzLveebNpa0gEyCumMXU9gafjg8WaMun0zbbyHlaxKia8XuMzDzbWz0z0QTIzxFyay/KWhkgfZZ8h+c4/SFY7zXlY13yBuKSBL2YKNNNiFiBKVgzSkpQTcIWUmFRjtSn0ZGQQ2z3kwdRhvOj+7Ft9XjXR8K8M6a17SdsSnJmWs4fkunyb9HcME7Wc/23UoYcyOB+QavqlbEmCpfg1L6s7w9xoGntd/yx9EvYRiN+0v/jCGso73InGzDnnmT7DfYBFH+f6SsY7PZfpxnmmgy4Df+htPFQ== 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=FSkZ381vqEqWfmePG2cYHBbVixRa9cGXw5IfNENUE1s=; b=p/rIW6euWZtmaWw3rxGYB7H0WIcOlfSM/yGQaAeD0TvhlEdAN6RHK9SkbKm//fY0u4KPnZ8wyJkz9CcSOQJugDFCvdYnE2NmUBswjdTK1o4IHvNGWord7Nu78HP0w9w3ZrWy5tWnobDzyxxEBNyKh6K6+9Mpshfnd98NGetdWwc= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by CY4PR10MB1238.namprd10.prod.outlook.com (2603:10b6:910:7::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5144.28; Sun, 10 Apr 2022 02:37:39 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa%9]) with mapi id 15.20.5144.028; Sun, 10 Apr 2022 02:37:39 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org, ebiederm@xmission.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH v8 03/10] kernfs: Change kernfs_notify_list to llist. Date: Sun, 10 Apr 2022 12:37:12 +1000 Message-Id: <20220410023719.1752460-4-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220410023719.1752460-1-imran.f.khan@oracle.com> References: <20220410023719.1752460-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0054.ausprd01.prod.outlook.com (2603:10c6:10:2::18) 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: 67c7c505-a70d-495d-c02c-08da1a9b144d X-MS-TrafficTypeDiagnostic: CY4PR10MB1238:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pksK8kIA0yrY4QJ/3RjtUTBHFpUOtpuwtJSZ3xMNrgxQfGwufnne001lj0XH7LR2lWs9sQnt/JEkl7pM0IkrRMHZCXpMZHd0yZEzB4MNY2/OUsk4rh4cp1to5BRYTzYoordTFt310VsJ2VNCTIA16ktr3qkAcuu6YcFjU1FNEyK2RgwbgZjSGkbZiSvQ1xXe0lrIICd6H24Ex40H8RRq7kn9rfrL4H7L64m3AtxNHTdB8/9wDXmeOWUA223Z1L/C4888mnZLFKHMgNCegiqI62DKnG/kZnzGeaE2asg/FdVX+12Jxe7Tl6eXl8FkeK8qB55F7Qh/8mxL0ZVds4a9h6iJdBY6vL9o9AyjZXSYnAE3QwsKGGZhZAH+vN4lE3rnKI9pZohtdBMpqr81KdJvm9SNvnOsjIuWZWitJ/Z+fa4zmw4bpSa7VgGpdkjndAlo57qeTE5j4bJ3MTZ2Oavq3x13AXOX+r92vyAIwHIEpcEhXKQ4l7C9hOwfi8ftlEarvr9VNNzWcEiIrD3EsIH+OptVhMBD76xGZmLMNybP5V3e4cv9pXeKgdj0zdUJwVRktyrbX0hCIZn8U+qNb4+p43QEgRlw8nRMDe8cyK3ifBogEKpv+hwY0M7OmITA5E7TalLY5U3J967/2je5BiJsDqwhFQTDq6gll+Go8yyCliFzF5oP2Al9SmdSAcb0etebPRcTwuP6ZQD4rUr+0SmEFQ== 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:(13230001)(366004)(1076003)(26005)(186003)(66946007)(66556008)(66476007)(8676002)(4326008)(83380400001)(36756003)(316002)(6486002)(103116003)(508600001)(8936002)(2616005)(6666004)(5660300002)(38100700002)(38350700002)(86362001)(2906002)(6512007)(6506007)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?KoDibE7sPWajhVfJ8rKXot3IqaxuajRwtC17dDr8LP9pXQRD3Mj4HvU+5m0S?= =?us-ascii?Q?6qZYCiy5speN/Y6j8gv78z35+dpIJm0MaIvUugv8LsjHW+4xOum7i2h0SK16?= =?us-ascii?Q?bgboWwKQkSkPpMPSTW5UADpbnAp7ONQbExomPmLAcT+Pab1emqQXhXsOb7V5?= =?us-ascii?Q?mnI288wSoVIcn1xyglu0jU9GpUeYNgD0q03Ito34gnnkLkkl2b2mmsdojVdj?= =?us-ascii?Q?MA6XE1BEqveZMeeQcGyZczBPAant9huWhskoXJ59DWH6YTdNCbXuLCCzfq2m?= =?us-ascii?Q?Aiihz6gpIbjVOjPSh+hYE6a/GmigoYqYq7P4mB1x59vatqyiMnaczWkBydcx?= =?us-ascii?Q?5hOevqiUl/ITNX5NGXcsqICQYndbAhffTozgIouPliC+liJroW+WPPFB+k0O?= =?us-ascii?Q?pL82LBIKd3E4ZgBc65bD4uPZTIq342WZhYM7Zy+sPpiRRimq6z267rfAOTBf?= =?us-ascii?Q?IbQo/Sa5kjoJ9xlC+AdpkxmdSycFNXSoPrUz55K+/GZ+KIx5Ycqws3ExZErT?= =?us-ascii?Q?lF45ePnJfrWnRTKyqR+SB/EzrePI62gLsx9H10Qlgv2vlMoQTwz3Og4onml3?= =?us-ascii?Q?OgvAn1/NkLIPTI1+OegCuR4kDPDZBH5TFFVgIqZ8MTOYD9zIK/Rps7/FcKUd?= =?us-ascii?Q?Xpxd/yhlkPqiUZrxdDzd0pac1JWpmMUL9Kuw9xI3hWi7n+j7Z/a5T+Z930az?= =?us-ascii?Q?FS3tGz6jN/LC6xPNn2l9BcU9EVHNE4AxukSW23LyhQEOJZuUM1MsH4QzCSbD?= =?us-ascii?Q?NvEjBYCiunOmxSGjfYvJwL9B1B89lKB4s95MCBxVtwG7CGly9/+m8wnmj3B4?= =?us-ascii?Q?X64fBeE5Kb1JAqCM9PzFa3rXlkLkL2B3yvzZ6NCGgpA+v4OTSWqJXchp+qys?= =?us-ascii?Q?ouWhjPJFWp+bR7M0Lr1BvZcR6ObF3qvUkRXGmynVLfltpzoh01cNKGJQACVP?= =?us-ascii?Q?Zb0IWC7fjAWLGf4hDGBACu4DxJsmlPLKqEjpL4ZSyHFaYtKJwRrNIHdtlOLR?= =?us-ascii?Q?Gk0H/zC7c3f0nLDXpq+eOu7i+LVrRkD+HaHfHlU0n+seiG+Q0Nf3iU1a7I4e?= =?us-ascii?Q?N/+ALsJoYN8XujGcosyCIaLXe5w3qkog1ge4sSQhV+Ae+00J8Y3MdOtJSHeT?= =?us-ascii?Q?2I01okOT52DGruacbpLnpkDGNsqo5CpCCcxk4wSpWrWKmiLlwx+VLoU1JsHr?= =?us-ascii?Q?S5bpgiZy0bDJTdT0neSfj6liyM40m0ANeIXHNeB6gC9PEkhICGlN6OwQ4hdC?= =?us-ascii?Q?1wmqZscFIihx5uEgHtm/LjqrDsoKYWg+D1YSycZeZMH7aGoShM2jd34l5atI?= =?us-ascii?Q?Hr/f5tlaGqOzR6yWbi2U5RhvoaWmcly1aMAlWSm+bwbkraUT/BNWj6Y0IZ95?= =?us-ascii?Q?bSWGJiAbUKLgpO9J/ES/1nOKIn1yHAXgGsQc/X4u4K1tVZe5ghxx7kmWQb6w?= =?us-ascii?Q?dH+xogZ1IrhN24NAOWvLuWE8oV0q0KsRJh6QRSogTxok+hUwAt+OSV7dqzqW?= =?us-ascii?Q?/aeuni8SQgJkH1A6har8RZxfAXIS8nhgOwjCn2+KKIIje+UJl9hIV4DcUrBe?= =?us-ascii?Q?pdxclj3F42vVWr2Q7IKh5eeOmSX1oOhuAw3aoI8QpdB2lWK5kZarZkDX5spJ?= =?us-ascii?Q?mpvTpgrT0t8HMtSKVZicuMCXeS/JHtN9M548kXcmMYY0TRHr3RX7GnbtTWDs?= =?us-ascii?Q?SGo5zzDkuPObIDJhfEG3xTP3aF+GDl/m6+cpcU5iKQRMfcHxX+CzvHbPo7CE?= =?us-ascii?Q?JyAq6h3iH/hCYYFHtTPTdD3wrv6zGCw=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 67c7c505-a70d-495d-c02c-08da1a9b144d X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Apr 2022 02:37:39.1113 (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: tw6jpbu642cG4q6JnycEdf7z1XaHJe/RsgKmF+vbZ9CiSl+eGjMX9sUMa+tqJGP2oh+BNp604T2v/ryUC2HQ2Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR10MB1238 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.858 definitions=2022-04-09_25:2022-04-08,2022-04-09 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 malwarescore=0 adultscore=0 phishscore=0 suspectscore=0 mlxlogscore=999 bulkscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204100014 X-Proofpoint-ORIG-GUID: PwvC6tK5ZNgzD2VYDlhUHqpq2vYKAODq X-Proofpoint-GUID: PwvC6tK5ZNgzD2VYDlhUHqpq2vYKAODq Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" At present kernfs_notify_list is implemented as a singly linked list of kernfs_node(s), where last element points to itself and value of ->attr.next tells if node is present on the list or not. Both addition and deletion to list happen under kernfs_notify_lock. Change kernfs_notify_list to llist so that addition to list can heppen locklessly. We still need kernfs_notify_lock for consumers (kernfs_notify\ _workfn) because there can be multiple concurrent work items. Also with this approach we don't check if a kernfs_node is already present in the kernfs_notify_list i.e whether ->attr.next =3D=3D NULL but that shou= ld be fine as it will allow serial processing of all events submitted for the same node while the node was still in event notification list. With earlier approach as long as a node was in the event notification list i.e ->attr.ne= xt !=3D NULL the subsequent events were ignored. Suggested by: Al Viro Signed-off-by: Imran Khan --- fs/kernfs/file.c | 49 ++++++++++++++++++++++-------------------- include/linux/kernfs.h | 2 +- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index bc393dcf4efa..c89220dcfdc1 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -38,18 +38,17 @@ struct kernfs_open_node { struct list_head files; /* goes through kernfs_open_file.list */ }; =20 -/* - * kernfs_notify() may be called from any context and bounces notifications - * through a work item. To minimize space overhead in kernfs_node, the - * pending queue is implemented as a singly linked list of kernfs_nodes. - * The list is terminated with the self pointer so that whether a - * kernfs_node is on the list or not can be determined by testing the next - * pointer for NULL. +/** + * attribute_to_node - get kernfs_node object corresponding to a kernfs at= tribute + * @ptr: &struct kernfs_elem_attr + * @type: struct kernfs_node + * @member: name of member (i.e attr) */ -#define KERNFS_NOTIFY_EOL ((void *)&kernfs_notify_list) +#define attribute_to_node(ptr, type, member) \ + container_of(ptr, type, member) =20 static DEFINE_SPINLOCK(kernfs_notify_lock); -static struct kernfs_node *kernfs_notify_list =3D KERNFS_NOTIFY_EOL; +static LLIST_HEAD(kernfs_notify_list); =20 static struct kernfs_open_file *kernfs_of(struct file *file) { @@ -846,18 +845,25 @@ static void kernfs_notify_workfn(struct work_struct *= work) struct kernfs_node *kn; struct kernfs_super_info *info; struct kernfs_root *root; + struct llist_node *free; + struct kernfs_elem_attr *attr; repeat: - /* pop one off the notify_list */ + /** + * pop one off the notify_list. + * There can be multiple concurrent work items. + * Use kernfs_notify_lock to synchronize between multipl consumers. + */ spin_lock_irq(&kernfs_notify_lock); - kn =3D kernfs_notify_list; - if (kn =3D=3D KERNFS_NOTIFY_EOL) { + if (llist_empty(&kernfs_notify_list)) { spin_unlock_irq(&kernfs_notify_lock); return; } - kernfs_notify_list =3D kn->attr.notify_next; - kn->attr.notify_next =3D NULL; + + free =3D llist_del_first(&kernfs_notify_list); spin_unlock_irq(&kernfs_notify_lock); =20 + attr =3D llist_entry(free, struct kernfs_elem_attr, notify_next); + kn =3D attribute_to_node(attr, struct kernfs_node, attr); root =3D kernfs_root(kn); /* kick fsnotify */ down_write(&root->kernfs_rwsem); @@ -913,12 +919,14 @@ static void kernfs_notify_workfn(struct work_struct *= work) void kernfs_notify(struct kernfs_node *kn) { static DECLARE_WORK(kernfs_notify_work, kernfs_notify_workfn); - unsigned long flags; struct kernfs_open_node *on; =20 if (WARN_ON(kernfs_type(kn) !=3D KERNFS_FILE)) return; =20 + /* Because we are using llist for kernfs_notify_list */ + WARN_ON_ONCE(in_nmi()); + /* kick poll immediately */ rcu_read_lock(); on =3D rcu_dereference(kn->attr.open); @@ -928,14 +936,9 @@ void kernfs_notify(struct kernfs_node *kn) } rcu_read_unlock(); /* schedule work to kick fsnotify */ - spin_lock_irqsave(&kernfs_notify_lock, flags); - if (!kn->attr.notify_next) { - kernfs_get(kn); - kn->attr.notify_next =3D kernfs_notify_list; - kernfs_notify_list =3D kn; - schedule_work(&kernfs_notify_work); - } - spin_unlock_irqrestore(&kernfs_notify_lock, flags); + kernfs_get(kn); + llist_add(&kn->attr.notify_next, &kernfs_notify_list); + schedule_work(&kernfs_notify_work); } EXPORT_SYMBOL_GPL(kernfs_notify); =20 diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 13f54f078a52..2dd9c8df0f4f 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -116,7 +116,7 @@ struct kernfs_elem_attr { const struct kernfs_ops *ops; struct kernfs_open_node __rcu *open; loff_t size; - struct kernfs_node *notify_next; /* for kernfs_notify() */ + struct llist_node notify_next; /* for kernfs_notify() */ }; =20 /* --=20 2.30.2 From nobody Sun May 10 17:53:08 2026 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 BC4F1C433F5 for ; Sun, 10 Apr 2022 02:38:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243411AbiDJCkL (ORCPT ); Sat, 9 Apr 2022 22:40:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243392AbiDJCj4 (ORCPT ); Sat, 9 Apr 2022 22:39:56 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B31218361 for ; Sat, 9 Apr 2022 19:37:47 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 239F70Ls028689; Sun, 10 Apr 2022 02:37: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=qD4t3w6alfJ01hnRfgjEsyYeuoRlfKzRDivwA7WzonQ=; b=jO5O7FhH390flX2IakQT06YDmb/trzp6rbmEA65STFy4405XXFllp/LfkWKMgjhZ731x w2p3iiDbJ+PHV20OdHTflwpBPEf3lKyFpOvodvAZlu3RF9dQnkBjIt41p81afGE54AR3 mbSKReRhKqZhPlXh3rc6dU1Gh6iuG6bgJmbJjYE6r8r29czBILAQAWpbyBJm21C/vJza oYq7+jNZ5EovmCnedpwuOUzsMQEA7oMifDfK1e/6fyQZXwdUTRVPFC3KqVldFUgfSYNO Gnmw5a7+wyyRBFO99vgnKr9m++dtdwAPbDT0ZfDBbQmYdAtHvhxEO7euQW7AGY8QQcA8 cQ== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com with ESMTP id 3fb1rs0yqy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:43 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 23A2VFA6034326; Sun, 10 Apr 2022 02:37:42 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2101.outbound.protection.outlook.com [104.47.58.101]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fb0k0gmts-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:42 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QLOIS14vkyR8TwD9HqP/+v7dlqM4tRLf+WPUhVhUqvqKqotUB7KEPrffwBZZKUE0buDuVZ1Itz9jxt2lJp98ZTdi07McqjsLIZkjraIVaEVfPFSiNVlSUbpZp8OMz2Mz4tzDljKut87jkPurcitTpbc7wyn1oSniZQGPZp3Iy5FfREoFi02pjXiS3AJTPpjbFgmkn56s9oivWS2ufmt32694nloU2gteIfHJgUiHooQQlk8laU88gW2ERWjWL9xC43biV8m9FNXy2/5fzdC+2eWpwtsiJ6oOvWQlRcYCleFo40u8jEWNxgBjrA7QXOHJjvrJogtixAlESHGkzdxAtQ== 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=qD4t3w6alfJ01hnRfgjEsyYeuoRlfKzRDivwA7WzonQ=; b=fqXGoJBI0HK3Vjm0aYY4Py9sjysn/VlnlmyN20ofR5Hh3+1nrKtyWvZylAtaJh8BglAuvmZukqO2gbwJxb4AGwjYIuQisiuQ4VNJ5bWfBW7ZLzFc5yXEHTCNrmLTE8shcwt7C7gdgUZh4NVIDE8VR4KoVKciibZ6W/ahMub83bV+xhidqzryz6u3iGNUG7lTF7wYERTDWG5scqUuB+qbR6trw5WJrJGhvzaCtB1QjA7ez4MRixntqA2QZw2XudqchAPe8nK0a1yzwbtDCB4RC6e/O78MfKhjrQe2wz2zwOKr984X/h2aSmaXFL1+CxC2tXnSFVZmnxCuC7eNFQvWMw== 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=qD4t3w6alfJ01hnRfgjEsyYeuoRlfKzRDivwA7WzonQ=; b=DAqRJtlIKA62aHa+eYWtD7en5caDxzrpapQAqhdx2s0HXecis9eENLBp2zcxQiypkgHj0tGioZhj/92jQbxfYd9ql8Rgj4G6ZCLe9Wv9VVKcSoRAYjotQE9mfYApqxjjJahxlN78MUOeE6QNzgiwv3dIi4K/Yb5rKpQcSe+GCyI= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by CY4PR10MB1238.namprd10.prod.outlook.com (2603:10b6:910:7::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5144.28; Sun, 10 Apr 2022 02:37:41 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa%9]) with mapi id 15.20.5144.028; Sun, 10 Apr 2022 02:37:41 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org, ebiederm@xmission.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH v8 04/10] kernfs: Introduce interface to access global kernfs_open_file_mutex. Date: Sun, 10 Apr 2022 12:37:13 +1000 Message-Id: <20220410023719.1752460-5-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220410023719.1752460-1-imran.f.khan@oracle.com> References: <20220410023719.1752460-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0054.ausprd01.prod.outlook.com (2603:10c6:10:2::18) 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: f60a2418-57ee-4fc4-e983-08da1a9b1577 X-MS-TrafficTypeDiagnostic: CY4PR10MB1238:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /rm3KNwJwgZ0uT4LSHrui8BAaqoOLrYPJmiPNdlY0d9ccP8hEkPx/2uhMeSo9/8ykHUFVg1ctXOV4P3s2/IheJncoxmmXeksHeGDWjhl6TmorS/IU9gX3zwVfyZERaeRfkA877ARLxmYC/TJedXLF610+ydwJeOGdHetxfV/efvVvFtBVoA7IIAFzRcZuitQl/3josRUeJKCWEx66ZV06QvnTBsH5AlfAt8iEgRWDqk529q2ufJuX3hFNRE4VZTkoaqkz3iHedB7SZOBOYhUSuumMRJQi4sVOBopD/FFHgrV6HkNSkEGXAsINWJi7LAzLU8xcHEPv4Hp7H6K2iBlEtwiIMXKLeXFxdiSmNwCMkdQJmqBCWoJVtOpMXlcvs1WOdoLKALgn+6/daupSXa4/FAjA6xLJ70l5UHa/AuEJ2RlsrQSjZO878KWwSNK2o15LhmWaFxGdhsHoh5GhKswcYjXpD9SHmiuZk5uE4chJ9yfbbDDi/6WyZS0yH+u3KC9M6X7KbD28nNzskJJZg0SQ3ZjwqoX0q/Z1n/Md5+FgtEPEB1pRP0FuxBjOuojiYz+A24n97oG3CemySanxecvKvGSg3W5K1MTpRLoMx+W6sc3zMCe3olQyQiFecj79PAvMUIeIhRi3CL9rHVW9kc+Qdxdex4ylZMUEDVek2nrx6BLiOW2RmTVEOP0Cgo3s2JDejbr2YdERC3QFIlZ3WJXJQ== 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:(13230001)(366004)(1076003)(26005)(186003)(66946007)(66556008)(66476007)(8676002)(4326008)(83380400001)(36756003)(316002)(6486002)(103116003)(508600001)(8936002)(2616005)(6666004)(5660300002)(38100700002)(38350700002)(86362001)(2906002)(6512007)(6506007)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?kAz6FeEJ3A2knnd0PPAerO58ubHx9yo7Q/K4FNWPJWQhddmRpZLUVN2nBVwp?= =?us-ascii?Q?2g+PTbNMTGJYfFsjCOJeSN8qhK37GGXCNmY6MoiXPFf7sTiePaOZffBf+8ti?= =?us-ascii?Q?GnZLcm9tAjqYYuOLYNGGxidCl/ZLX12khkroPWVKEC47qzqvcnCJxQboo0lk?= =?us-ascii?Q?FPew8w1KaaAtgFxIiRA4Ucjw0VtVRrgadn6jyDDVzm5J6v5M/CWS4RbNolVz?= =?us-ascii?Q?GPhOlg4r9LSSOXa0AoE7jjLLOGdwgf2f0huHZ+vC0+XxVVnx1d8dpAYDfsC6?= =?us-ascii?Q?Vp3By5jEoa1fbWs9bx2wkE6o8BXVZEo6kEWyljUmewo1C3QswokcYzcApm3Y?= =?us-ascii?Q?RiLusE9luHsQYskNka1scIWStrUddKowdcPH0EMzDJXdbX8UbMdfhH3IG/7o?= =?us-ascii?Q?8SBxRhqJMvqEaHLv0FOMLAkqXAPQ8oeujiZAE7DEPD2QTHIKb1lFJQhVbS+0?= =?us-ascii?Q?65dTfBIvGKB7KUFfS91wE23sGJSYp2u2GmdVrPgppD6qe0KBccl+rrPXniXp?= =?us-ascii?Q?7eJiGRRpF0qWEvUBN/x2/XtfXp3TowKliiRpF87MdaY044a2Cj8d1hbfuhFM?= =?us-ascii?Q?H+KiqCUpnfQ8P8Pre7ujsFZFBx6biFwW2SrbNH3GBn20HQ5vgZlFmEgr52jK?= =?us-ascii?Q?E8An8Mjx23Q+vG+lPiMokoKE5wbasINgOgyW2T9vPeFzgrjEMjf/XEFR3XEA?= =?us-ascii?Q?poyouRQwUudE2fh5WEI9cx8s2g1A2Wto+Y4ALZQBUcu5ij2LlZO7G61e3tJo?= =?us-ascii?Q?5dIXyVshbtT8A5s3G0UeyOt1yLuJfTFOUS+AleIPpY+urU891JCddBKHu5E3?= =?us-ascii?Q?6+fFQHKgMnlrLwCeTjZ7j2eo9TtvpPQ5ml/6CobRIk+cp3u50ktwPbRMsxPw?= =?us-ascii?Q?llHcsK1VSxhR7Vlrzg8Yyv3vmSSz+XSL/hTHmO48JXH/zu0g/f/qN8GW28v7?= =?us-ascii?Q?ZgM0XZ8l7foLqkqaz4rUjJrJ31K0t/ProCz+ukIxYcRtmqdDVXvt+b+NfiSw?= =?us-ascii?Q?Ev2zMe54XhOEC0s1//5TMn7NX1IVI5/urNzTVpqMKyv3PLJkyytYFBEZ6VbL?= =?us-ascii?Q?/XWQ7LoR0mYq+KauwR6zxRk8NDGZnVsg9ZeGPwa8UCwa/Frv8M2kOavMnyjd?= =?us-ascii?Q?x6+lYvSyUv32fwFxCoxnrhZfbKvsSA0lgUqanu4Il+uIakyMFWKcofhWKSlK?= =?us-ascii?Q?bWMwa2QXolrjSG+M4oWxJfH1XNty0UVaPR3w2GJdTuURXLcxkkdEM0tGHdro?= =?us-ascii?Q?KAYdjOvRhY0oguj6AC6lnpZXuB4VYEVXwyuTccPSQsw70c5UvT/16RVuu/2t?= =?us-ascii?Q?/sKgm/jDefwmHJB2DM5KB9tClyloVStYYlsaQB/duTOrUgZGOWwlccV8xeDC?= =?us-ascii?Q?qM8PR//1ou298taHYXklZGtKrvkFXMWRaXndRiqClXMEpt9G+mMbSQt2iTHQ?= =?us-ascii?Q?mwaXJiB7yfIeF1mfUdMdtc1Jameq+ksvRDTMqVB4DdjN9C+TN5x8VTaLCyeU?= =?us-ascii?Q?H4JrsbeUGwVq92BgtC7TL0My7MdzktyBVEuOZt23NJN7fP5xa1XdVRIqNwyg?= =?us-ascii?Q?5eGD6gBJn3GXuV9fnLDQelRN5rkozeNU9s7hyTpHe9mHvrfKCRr4m3PzEbcY?= =?us-ascii?Q?U+a61cJyWr9ZOaA09YZF6hdOaizbwlFsUru7LxO/9StcsMz29Mm3kYrWpWH8?= =?us-ascii?Q?HNwqotHKgbZw3zxEyUTWEThmtWNwcgKCiUb0RjyQVMgSqB/vEQGoOi479oNY?= =?us-ascii?Q?XwJHBNImpQyLY7/mEkB2dsmwjHKWJw0=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f60a2418-57ee-4fc4-e983-08da1a9b1577 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Apr 2022 02:37:41.0185 (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: j17eGAn7PNOHmgu2N5aD5AtPa+Gmc0KrMhOtFAJ1FCB6fPV3PsrDJZ2cN9IlIH88yjMkCaDQZsFT+DXM8FV+tw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR10MB1238 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.858 definitions=2022-04-09_25:2022-04-08,2022-04-09 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxlogscore=999 mlxscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204100014 X-Proofpoint-ORIG-GUID: hOogNOBmlsjW35n5_E6HH5I2Ski0F-8t X-Proofpoint-GUID: hOogNOBmlsjW35n5_E6HH5I2Ski0F-8t Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This allows to change underlying mutex locking, without needing to change the users of the lock. For example next patch modifies this interface to use hashed mutexes in place of a single global kernfs_open_file_mutex. Signed-off-by: Imran Khan --- fs/kernfs/file.c | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index c89220dcfdc1..214b48d59148 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -47,6 +47,22 @@ struct kernfs_open_node { #define attribute_to_node(ptr, type, member) \ container_of(ptr, type, member) =20 +static inline struct mutex *kernfs_open_file_mutex_ptr(struct kernfs_node = *kn) +{ + return &kernfs_open_file_mutex; +} + +static inline struct mutex *kernfs_open_file_mutex_lock(struct kernfs_node= *kn) +{ + struct mutex *lock; + + lock =3D kernfs_open_file_mutex_ptr(kn); + + mutex_lock(lock); + + return lock; +} + static DEFINE_SPINLOCK(kernfs_notify_lock); static LLIST_HEAD(kernfs_notify_list); =20 @@ -520,8 +536,9 @@ static int kernfs_get_open_node(struct kernfs_node *kn, struct kernfs_open_file *of) { struct kernfs_open_node *on, *new_on =3D NULL; + struct mutex *mutex =3D NULL; =20 - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); =20 /** * ->attr.open changes under kernfs_open_file_mutex so we don't @@ -531,13 +548,13 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, lockdep_is_held(&kernfs_open_file_mutex)); if (on) { list_add_tail(&of->list, &on->files); - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return 0; } else { /* not there, initialize a new one and retry */ new_on =3D kmalloc(sizeof(*new_on), GFP_KERNEL); if (!new_on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return -ENOMEM; } atomic_set(&new_on->event, 1); @@ -546,7 +563,7 @@ static int kernfs_get_open_node(struct kernfs_node *kn, list_add_tail(&of->list, &new_on->files); rcu_assign_pointer(kn->attr.open, new_on); } - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); =20 return 0; } @@ -567,12 +584,13 @@ static void kernfs_put_open_node(struct kernfs_node *= kn, struct kernfs_open_file *of) { struct kernfs_open_node *on; + struct mutex *mutex =3D NULL; =20 /* ->attr.open NULL means there are no more open files */ if (rcu_dereference_raw(kn->attr.open) =3D=3D NULL) return; =20 - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); =20 on =3D rcu_dereference_protected(kn->attr.open, lockdep_is_held(&kernfs_open_file_mutex)); @@ -589,7 +607,7 @@ static void kernfs_put_open_node(struct kernfs_node *kn, rcu_assign_pointer(kn->attr.open, NULL); kfree_rcu(on, rcu_head); } - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); } =20 static int kernfs_fop_open(struct inode *inode, struct file *file) @@ -731,7 +749,7 @@ static void kernfs_release_file(struct kernfs_node *kn, * here because drain path may be called from places which can * cause circular dependency. */ - lockdep_assert_held(&kernfs_open_file_mutex); + lockdep_assert_held(kernfs_open_file_mutex_ptr(kn)); =20 if (!of->released) { /* @@ -748,11 +766,12 @@ static int kernfs_fop_release(struct inode *inode, st= ruct file *filp) { struct kernfs_node *kn =3D inode->i_private; struct kernfs_open_file *of =3D kernfs_of(filp); + struct mutex *mutex =3D NULL; =20 if (kn->flags & KERNFS_HAS_RELEASE) { - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); kernfs_release_file(kn, of); - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); } =20 kernfs_put_open_node(kn, of); @@ -767,6 +786,7 @@ void kernfs_drain_open_files(struct kernfs_node *kn) { struct kernfs_open_node *on; struct kernfs_open_file *of; + struct mutex *mutex =3D NULL; =20 if (!(kn->flags & (KERNFS_HAS_MMAP | KERNFS_HAS_RELEASE))) return; @@ -774,7 +794,7 @@ void kernfs_drain_open_files(struct kernfs_node *kn) if (rcu_dereference_raw(kn->attr.open) =3D=3D NULL) return; =20 - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); on =3D rcu_dereference_check(kn->attr.open, lockdep_is_held(&kernfs_open_file_mutex)); if (!on) { @@ -792,7 +812,7 @@ void kernfs_drain_open_files(struct kernfs_node *kn) kernfs_release_file(kn, of); } =20 - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); } =20 /* --=20 2.30.2 From nobody Sun May 10 17:53:08 2026 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 DA8E3C433EF for ; Sun, 10 Apr 2022 02:38:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235511AbiDJCkU (ORCPT ); Sat, 9 Apr 2022 22:40:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243394AbiDJCj7 (ORCPT ); Sat, 9 Apr 2022 22:39:59 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97C4415833 for ; Sat, 9 Apr 2022 19:37:49 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 239FZSw2029058; Sun, 10 Apr 2022 02:37: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=32Ru7ruiQ+QqISbRWomga8Ujk0sTnxMOyWp7P6ooY/c=; b=gnpbXPORdTSJui2xYdRVG5jMh3TXyfxqgWjJRQpclhfP/DgRKhqgLRp8aC2HzbstVsEw jZT2iNcNM6+o03jYhVtT3/INJPJomVgM2BfVtbTB3IIrHBhJMyA+ZYsVnWufO+MQ3HKG intyr23aAJmxPMX8XPqrFKvpV4330r5lCVu1lo39dH/0t6nlJZm+WqO0q/k8MYX85mNm MtCEbPOtoTZN53XQVn/9VQ3jum6Z9jN5wNIv6JbdB8TDy67a9u+X6TG8hmGufCmGj6vl cHbUnzLAkFfEHdtXmMpuhx0C9SWrepE2yWist5YvcmXcwB59m62WBaP1rRh7UE+9AgAn WA== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com with ESMTP id 3fb1rs0yr0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:45 +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 23A2Udhf024144; Sun, 10 Apr 2022 02:37:44 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2102.outbound.protection.outlook.com [104.47.58.102]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fb0k0h5gn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H2KPr2/U3ezKYdVuHwZRd00XCZTkNZTlY1Y0/9SfBjt1NXjDe81z/AFbfVeANIGgSO3GO+i2siamoMBYpW0Qp+E5dJ9x29b4y0zxuyks4Coz1Nd2QVUAO7UlqTvPpon7WaNgkf52FuIs9SuOM2t5O2t7Zrd7/GDRu5kukZyBM6+DawBWdBUvhmezaQIp9y7TlcBKbhZQ8HrDZ07N9JOREpo2tSLbRiHT5Vbv6D7Oe3w+PLn4NDlo4NbnO4eVLltL2/QUYTHY/VIpQOCbXZBVwEHB2PXWmrgxVttunAaUbXnMRk4vzD5NFDUw0/fpwtffwHB9NV/mmwZAa3B6LYke2Q== 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=32Ru7ruiQ+QqISbRWomga8Ujk0sTnxMOyWp7P6ooY/c=; b=mafO/o+E9kgs4uW0ACjTvr3Q6GzPfaqyTD+amtNi40dvpaPZUMJLXHtNgMo4jnc0GAGTA1pzqpCCJ79PLPYEGbRyPeBX46k5OQQyJtybklhHq0kjyZpCIzzSYLoC+gYdjc0beGIqr30IVKETfvUn1cYLLKSiKaddO27+QaQafOXe6O1YDpvjULBcHRLPxH8lH6SeL95COyZTk0PRtzUAhJHtiOyt422BO3XAQw8S7e1nU68lm9v80cRt3dJACGsEXRdHm5e3ohAZwOmf2IHw2Kaf3qWzHH6gbBdt6nMBI+7oxZ+pAjiJO6GVsa3aDUYyvvhKv1Ogt16wp+OQWb4DRA== 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=32Ru7ruiQ+QqISbRWomga8Ujk0sTnxMOyWp7P6ooY/c=; b=hP3JkCILkoi1PQU/35z6IkuHDtZmN5RChXa/202CIsd7OhMZxkSAGzuZ3aqCETzVQwGdfydMnJviFB1sIN63y/0/JDW+mglNcQCLvWgtKxwDfbU/c50IW1G983FPlh2mY7Utm49nf2U95pDKmPTWL7wosuiW8ambDwIuOU8kSWo= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by CY4PR10MB1238.namprd10.prod.outlook.com (2603:10b6:910:7::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5144.28; Sun, 10 Apr 2022 02:37:42 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa%9]) with mapi id 15.20.5144.028; Sun, 10 Apr 2022 02:37:42 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org, ebiederm@xmission.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH v8 05/10] kernfs: Replace global kernfs_open_file_mutex with hashed mutexes. Date: Sun, 10 Apr 2022 12:37:14 +1000 Message-Id: <20220410023719.1752460-6-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220410023719.1752460-1-imran.f.khan@oracle.com> References: <20220410023719.1752460-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0054.ausprd01.prod.outlook.com (2603:10c6:10:2::18) 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: 590656ec-06aa-460e-337c-08da1a9b1691 X-MS-TrafficTypeDiagnostic: CY4PR10MB1238:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eSlp57PNYacubQCwMWuN1EhSwvHoTkrdjHIVDkyH7znkC5MixbSivD23XHvM7egCCphlmFZDtkgGWhxVLHMze5N2X/YGlMy9OLeIK/JPQz6+rzsCpNbN8iYfpqq567HtiOZKUw1JAio2jrJ4lVBjQWRM4RRbjTaw6ehDMGlGim+Iih1zFG7CfXLE5d0sZpeLoFhGhF/DNuhRCcdqZy1VKhr7rdFHmjKtmpALjEMriK4XrltqcXxhkloI+6Uq/n7BrSyuaB04LMNpyIUcrbOBn00BoopD+jCLOZg5ZsPZE/i149ZDQg8KDDA29FWCwefil+2jSX1sSPnIN+M+QErTaueKNMUH62Xott9gLdjxDY++j/7ouRSGQXIC4CdRVm/dKKlqYC7b4SEI31mYqHL+CIz2sQXcDptOAMUWyotuWxOjQE1Of+B4MBB0PTtl+TaXokuapvfxFV6cfueskPC9OgtNaiekM3sZwoCXC6LSWsiuN1GNCkXBccy2AK2vLDtC6zDUnWng1Ri76M6cAaGdfKiwHa+smgWDpHoQJ2kQPE1KOr0CqHOpHsIHpkg9tNubDQSeWgX5w6EG23ZRMnlgZDURI7zCmwv5VQRtmdUYUZ2PSjWN26B2Ip8l5w/rOxo9wOiGt1j2VRtINo9V31r7TXrV/+U9UN1HDMf+iAYz6ElvfuT34lWAPQ0OMqHLMQNNJuqblw5V+VPM2r0IBPLQQA== 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:(13230001)(366004)(1076003)(26005)(186003)(66946007)(66556008)(66476007)(8676002)(4326008)(83380400001)(36756003)(316002)(6486002)(103116003)(508600001)(8936002)(2616005)(6666004)(5660300002)(38100700002)(38350700002)(86362001)(2906002)(6512007)(6506007)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?7tQccRH/uC6g0Pl7yaN/4dRPb1wIkpz/fOdVdZ3ubZqdbBECgwLY01dekJfE?= =?us-ascii?Q?pAotsBggmF3pzeVeso8BxdMkmEAD12kGKFxLbDSA5OLRENLzqwk2ZOL3bqGn?= =?us-ascii?Q?Pl8eXWij7YRRZeSCXayuKIIsqwrzJZMwwxTeBPNtyuEOPHASkS5jcp+Jt+2h?= =?us-ascii?Q?ZlVXNAX7Raj40GjAvANg8mswmlw/mo5tfdBOYmKG2O9nHlK09jxiaDZE0pRK?= =?us-ascii?Q?XdBpWHe8JU3f1/sSEaXLmNGRWwQP4hJCFm2jrBVEWNvryWAPFXPfvSPXScYk?= =?us-ascii?Q?RkSJ+ScvCAWknqu4PhKmYunkUvldht96AtKmTS99An3gOpX0WW7p/a2Z3brt?= =?us-ascii?Q?wkkr+fVX3br9BcwK0qacEgCsJ3+upepCq8gZyN3n8IDvRbEx7/rSJSyRQ/vI?= =?us-ascii?Q?3YO34bXj1BRKlFB7iuZkDEsrEOphDfH3CK+yFjqXZUpRktLJDNO5WwrGt2T7?= =?us-ascii?Q?jHJEZfxdnzgxe51SKTRrITodAojsBKpJ/F/my0DLd1Dfqj3hOFuZu+f6CkvY?= =?us-ascii?Q?LHlX8rcmtgW5MvRdXA0tSKaRg+lf4dTUPbivF7WXJnpNlBZxfB2KNEvQiYlW?= =?us-ascii?Q?iylpZ5LuPZaElD9jp4DRx5HZCP7UrINcnh0WryVaieExZPcA8M2ueKHJw75n?= =?us-ascii?Q?DyGmRU4ZgQPL+N3oCLixHFauGsPAabPnxQMhEt2465HuE9lnqIEK4HGg56sy?= =?us-ascii?Q?+LRAhs4fG5AlY9GOjxDODR9emGceGAclxrn4n/ixRrgQ/+UJsu677EPbyd2Q?= =?us-ascii?Q?MoJJU14+qKsFRM51nQE4QC5qrqacOYCPnYcTGhsgNOA1gGWCBM3jwhTJF7ez?= =?us-ascii?Q?XtH3R9yBOxYfpR3cqoIHC87jRGka8YPsebhLX86CLC9eCFfDKGXLJcEfU/p0?= =?us-ascii?Q?0c7ro8wxkyWg04EM0rNrEp/UME7uhQ28Gj5VVhf8X1VYcMVVJtSQm+qGRku9?= =?us-ascii?Q?JF4toqQNlyMRYmULji7S3B3fhhs6DB3+ge0TrPb1I49yTEurR8Ls9OEG0naW?= =?us-ascii?Q?25uSApFcutW6m75PJ3gEeJSxD0dV56FIvtPMJTW4m7rFmEA/fMREvL9lonL3?= =?us-ascii?Q?zd1exc1OFihpawBD0jnKSFho2bzMdhrFv5huRq1wpOH/fCcJ/uj+XA2fBnyi?= =?us-ascii?Q?fpZhA3ULzXB6waVVRvPgX9AQQ4MX0vj+sVs0dECxInKsSAcyZgHzXUFyISqH?= =?us-ascii?Q?wDBoAxZQ6vSKH5zbQAzpzi49pABVO8oUIcSFO6VeoSbm83bNQK5iqdfx2z5P?= =?us-ascii?Q?yK37FHGmqEzcasL7KwYQJHugEEc51QmyvlEcwf57OlAim7FaB2LftCXUZZct?= =?us-ascii?Q?CX+c2d2Wxi4CRPwuzYPadQ//CO/pbNhhXki93pe73Vq0kZ27faJoQK9DA2n1?= =?us-ascii?Q?qOA6ECfG936QQwirMluknxOxI9QP3/1fFHAZpNTNK1RkbbAElGsRurhqF6Xw?= =?us-ascii?Q?AgPq12Pzk0jbJJWlSLwh+8DH3UsjhvsVaPikd2pF9nzB4biFaDohusB3Si2Q?= =?us-ascii?Q?LNhcgnfQCCHOoXVyK5nvXNenWi57cSjwbVGtuhgopxUEFxRSwiGJfpc0IssH?= =?us-ascii?Q?y4DiaiYg3BU8iv0tLhtL3wMjtaVypUJQFAxplonc8avWZNuu2MYUjeUyfrCR?= =?us-ascii?Q?ATL54Gv0Wcm0tRwE/e/8RdIEAybb3fU8Y+QLz3U+5xSq/Gtd0L0PSs0cBS+w?= =?us-ascii?Q?qO1QAzyZpJtbLd/h/E91LLkH9msMJBl8R6Bh7iNeCcRAWz9JxSy3a7g1R4oN?= =?us-ascii?Q?DEACqnOmT22Z5HcH1po5fckiFnpBRV4=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 590656ec-06aa-460e-337c-08da1a9b1691 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Apr 2022 02:37:42.8463 (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: qFZo32xvsLaALya2p/pbwK0Cwt1EG3Bi62AxfjVzPpaDzrW5h2XHSzoKj+dX0EXqZEx96g1pq5rHDxQA82yNYA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR10MB1238 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.858 definitions=2022-04-09_25:2022-04-08,2022-04-09 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 malwarescore=0 adultscore=0 phishscore=0 suspectscore=0 mlxlogscore=999 bulkscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204100014 X-Proofpoint-ORIG-GUID: ZpCKjBjQSEjA90lMYjM6zKrk1C-dpxFD X-Proofpoint-GUID: ZpCKjBjQSEjA90lMYjM6zKrk1C-dpxFD Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In current kernfs design a single mutex, kernfs_open_file_mutex, protects the list of kernfs_open_file instances corresponding to a sysfs attribute. So even if different tasks are opening or closing different sysfs files they can contend on osq_lock of this mutex. The contention is 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 mutexes in place of a single global mutex, can significantly reduce contention around global mutex and hence can provide better scalability. Moreover as these hashed mutexes 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 mutexes. Use kernfs_node address as hashing key. Signed-off-by: Imran Khan --- fs/kernfs/file.c | 32 +++++++++++++----------- fs/kernfs/mount.c | 14 +++++++++++ include/linux/kernfs.h | 57 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 15 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 214b48d59148..0946ab341ce4 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -19,18 +19,14 @@ #include "kernfs-internal.h" =20 /* - * There's one kernfs_open_file for each open file and one kernfs_open_node - * for each kernfs_node with one or more open files. - * + * kernfs locks + */ +extern struct kernfs_global_locks *kernfs_locks; + +/* * kernfs_node->attr.open points to kernfs_open_node. attr.open is * RCU protected. - * - * filp->private_data points to seq_file whose ->private points to - * kernfs_open_file. kernfs_open_files are chained at - * kernfs_open_node->files, which is protected by kernfs_open_file_mutex. */ -static DEFINE_MUTEX(kernfs_open_file_mutex); - struct kernfs_open_node { struct rcu_head rcu_head; atomic_t event; @@ -49,7 +45,9 @@ struct kernfs_open_node { =20 static inline struct mutex *kernfs_open_file_mutex_ptr(struct kernfs_node = *kn) { - return &kernfs_open_file_mutex; + int idx =3D hash_ptr(kn, NR_KERNFS_LOCK_BITS); + + return &kernfs_locks->open_file_mutex[idx].lock; } =20 static inline struct mutex *kernfs_open_file_mutex_lock(struct kernfs_node= *kn) @@ -545,7 +543,7 @@ static int kernfs_get_open_node(struct kernfs_node *kn, * need rcu_read_lock to ensure its existence. */ on =3D rcu_dereference_protected(kn->attr.open, - lockdep_is_held(&kernfs_open_file_mutex)); + lockdep_is_held(mutex)); if (on) { list_add_tail(&of->list, &on->files); mutex_unlock(mutex); @@ -593,10 +591,10 @@ static void kernfs_put_open_node(struct kernfs_node *= kn, mutex =3D kernfs_open_file_mutex_lock(kn); =20 on =3D rcu_dereference_protected(kn->attr.open, - lockdep_is_held(&kernfs_open_file_mutex)); + lockdep_is_held(mutex)); =20 if (!on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return; } =20 @@ -769,6 +767,10 @@ static int kernfs_fop_release(struct inode *inode, str= uct file *filp) struct mutex *mutex =3D NULL; =20 if (kn->flags & KERNFS_HAS_RELEASE) { + /** + * Callers of kernfs_fop_release, don't need global + * exclusion so using hashed mutex here is safe. + */ mutex =3D kernfs_open_file_mutex_lock(kn); kernfs_release_file(kn, of); mutex_unlock(mutex); @@ -796,9 +798,9 @@ void kernfs_drain_open_files(struct kernfs_node *kn) =20 mutex =3D kernfs_open_file_mutex_lock(kn); on =3D rcu_dereference_check(kn->attr.open, - lockdep_is_held(&kernfs_open_file_mutex)); + lockdep_is_held(mutex)); if (!on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return; } =20 diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index cfa79715fc1a..a64a04efc9be 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -20,6 +20,7 @@ #include "kernfs-internal.h" =20 struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache; +struct kernfs_global_locks *kernfs_locks; =20 static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *den= try) { @@ -387,6 +388,17 @@ void kernfs_kill_sb(struct super_block *sb) kfree(info); } =20 +void __init kernfs_lock_init(void) +{ + int count; + + kernfs_locks =3D kmalloc(sizeof(struct kernfs_global_locks), GFP_KERNEL); + WARN_ON(!kernfs_locks); + + for (count =3D 0; count < NR_KERNFS_LOCKS; count++) + mutex_init(&kernfs_locks->open_file_mutex[count].lock); +} + void __init kernfs_init(void) { kernfs_node_cache =3D kmem_cache_create("kernfs_node_cache", @@ -397,4 +409,6 @@ void __init kernfs_init(void) kernfs_iattrs_cache =3D kmem_cache_create("kernfs_iattrs_cache", sizeof(struct kernfs_iattrs), 0, SLAB_PANIC, NULL); + + kernfs_lock_init(); } diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 2dd9c8df0f4f..cc514bda0ae7 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -18,6 +18,7 @@ #include #include #include +#include =20 struct file; struct dentry; @@ -34,6 +35,60 @@ struct kernfs_fs_context; struct kernfs_open_node; struct kernfs_iattrs; =20 +/* + * NR_KERNFS_LOCK_BITS determines size (NR_KERNFS_LOCKS) of hash + * table of locks. + * Having a small hash table would impact scalability, since + * more and more kernfs_node objects will end up using same lock + * and having a very large hash table would waste memory. + * + * At the moment size of hash table of locks is being set based on + * the number of CPUs as follows: + * + * NR_CPU NR_KERNFS_LOCK_BITS NR_KERNFS_LOCKS + * 1 1 2 + * 2-3 2 4 + * 4-7 4 16 + * 8-15 6 64 + * 16-31 8 256 + * 32 and more 10 1024 + * + * The above relation between NR_CPU and number of locks is based + * on some internal experimentation which involved booting qemu + * with different values of smp, performing some sysfs operations + * on all CPUs and observing how increase in number of locks impacts + * completion time of these sysfs operations on each CPU. + */ +#ifdef CONFIG_SMP +#define NR_KERNFS_LOCK_BITS (2 * (ilog2(NR_CPUS < 32 ? NR_CPUS : 32))) +#else +#define NR_KERNFS_LOCK_BITS 1 +#endif + +#define NR_KERNFS_LOCKS (1 << NR_KERNFS_LOCK_BITS) + +/* + * There's one kernfs_open_file for each open file and one kernfs_open_node + * for each kernfs_node with one or more open files. + * + * filp->private_data points to seq_file whose ->private points to + * kernfs_open_file. + * kernfs_open_files are chained at kernfs_open_node->files, which is + * protected by kernfs_open_file_mutex.lock. + */ +struct kernfs_open_file_mutex { + struct mutex lock; +} ____cacheline_aligned_in_smp; + +/* + * To reduce possible contention in sysfs access, arising due to single + * locks, use an array of locks and use kernfs_node object address as + * hash keys to get the index of these locks. + */ +struct kernfs_global_locks { + struct kernfs_open_file_mutex open_file_mutex[NR_KERNFS_LOCKS]; +}; + enum kernfs_node_type { KERNFS_DIR =3D 0x0001, KERNFS_FILE =3D 0x0002, @@ -397,6 +452,8 @@ void kernfs_kill_sb(struct super_block *sb); =20 void kernfs_init(void); =20 +void kernfs_lock_init(void); + struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *roo= t, u64 id); #else /* CONFIG_KERNFS */ --=20 2.30.2 From nobody Sun May 10 17:53:08 2026 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 9FCBEC433F5 for ; Sun, 10 Apr 2022 02:38:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239027AbiDJCkd (ORCPT ); Sat, 9 Apr 2022 22:40:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243390AbiDJCkB (ORCPT ); Sat, 9 Apr 2022 22:40:01 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08F6418361 for ; Sat, 9 Apr 2022 19:37:52 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 239F70Lt028689; Sun, 10 Apr 2022 02:37: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=NE865zc2G8x8jy0Jq2ZC3sS4abXHbV/sCkLxsasbllc=; b=SnDHv2yltkTfSPPUqy54Ly3gfrWKU9KDmKgS7sn+wxH4t30Rndk62Q+Iq4jCW1ZO1+Ko PnjLpJokl033S3jr6RywkVSbR9jHi2hkeo/+UIm1+pXqClBz3lfFYbC3brwN1t7aFtCQ k5n+Ulh0fpmPMh6QlenTshncfunICIkeNdXVEGjyzwt/g/HpgNTkdvnwxY4Wfjh/AIsK DKFxJf7D1MFT3RhMn+Qqr5AJnZSGmBxOyM80QVKcUfm6PLw00H/iMN5j9C8T3MIoy5hm W7rR160t8xQs4/0yiE1Wny3l1dkoxiL3qppJ0Sgyx7UZtYYGlQbnoIChMEhR7wjOq07G IA== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com with ESMTP id 3fb1rs0yr1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:47 +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 23A2Uw16023458; Sun, 10 Apr 2022 02:37:47 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2102.outbound.protection.outlook.com [104.47.58.102]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fb0k092gq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:46 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DmOdRyaCBHN3GyLyXXFBqFHcn5q2H/SlPs/5jSk3rxzVanZ25tE8PPUpqwKS1+9/nGaceNjfaEu7kHwqVwOVy1tVFaROlYpcT7sZq50bAAbiooZL2OsPurvD0XkDakyQ0FfQvqyQr5AAZDItLr/c368O7GD6lH6IEKYhdtANC78OooGNd4nNvgnFoyzeXKb5dvYWL97J3BY956KgoJJOb0xBiMn4V6o8sq53FRqybb1YNebVOKycn72oLcJ/4K6Kp/M0ehfIVCleMZ7zq81Rrc8vEuvONyKQC6T6JJXxYmgvjPlMj/eGIeMbuKLapu0TJveBEFfGu+xs/xOOB6s2/g== 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=NE865zc2G8x8jy0Jq2ZC3sS4abXHbV/sCkLxsasbllc=; b=B8ziJUnxuKIpRlFb1UXpG/qAzlVx0toSCvEyjxV7y9k/kgt8WXsR4ABHWGF4hqF/ZQoq7v09aL+8jhlcRAO7+FtCoetYhaWG64JPvb3Hog2tqhVrPQXoglFq3O/uTA0IMbNb0nvBhqrxID3GnzWShuuonWMitieZuD2nkY5q0YrqU0r2RovBMPWvBwWEh3e5m7atuoTb7Y+qR2kJzB2BsPyuaZLYaa4YR+2wsBLVjMIT2b4vr2y/8Ueq8b3+5xpZBKpYikoj5ZP2oR9HkyCWD+zul4YHmyCDKV47kEDGNOs6Xu1qPGcOrV7m1vUpQ5nSD+GMKDhEGuyzaBmQwRUyLw== 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=NE865zc2G8x8jy0Jq2ZC3sS4abXHbV/sCkLxsasbllc=; b=Sgd85Vkc+VoTNGPbSm5PZASTPD4eaMx5SFlcXjPK2ejI8ZBKp35XtOTtDm9yKYcsp1+l2ubjTGfojraukTembvhaMKL/DHMW5fIv4186fjgZaLRF10l1xkXjG8IDyd06pAzGFhxLwxI7WT7EKtZdLaFGBcpXMUJngt05Un4Ite4= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by CY4PR10MB1238.namprd10.prod.outlook.com (2603:10b6:910:7::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5144.28; Sun, 10 Apr 2022 02:37:44 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa%9]) with mapi id 15.20.5144.028; Sun, 10 Apr 2022 02:37:44 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org, ebiederm@xmission.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH v8 06/10] kernfs: Use a per-fs rwsem to protect per-fs list of kernfs_super_info. Date: Sun, 10 Apr 2022 12:37:15 +1000 Message-Id: <20220410023719.1752460-7-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220410023719.1752460-1-imran.f.khan@oracle.com> References: <20220410023719.1752460-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0054.ausprd01.prod.outlook.com (2603:10c6:10:2::18) 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: bb08536c-20d7-48fa-faf4-08da1a9b17b8 X-MS-TrafficTypeDiagnostic: CY4PR10MB1238:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: w+dS+SSQ9vRYmjsDMjRBSdgohPEp2CEgtf2KTKPnlSyu/Jg4nA0JfMsvBtC/6coDowFSwKN7hAZsXgIUNHGLOzvH/65H6iLVpsdoaDeKNKhPlKCgMRdm9jlavlsBDBCewNHvcT2dx0VUvgrDUTmelBBz43n2viPdb0KHKv7MQRr4Ub2WIV5doQ/q1sj330HJME3cms9OiO3guJcSgCxq3x5mHL8KMDuZ07nlVuVhkBhjw0gyOVbzKOGtd5NQrLRJFU6Xz3JNiHK7fkKRPVPXDRoQXJw2Nlqo8xSc3niCFjaH9AWo1ZQs4cNKJBUqISvdHJnJ/ZdKFEUu4JGmT/7fOnWuOuPYmSNEYZYrXIDZBTdy44Pv4XtNSgHOwl67spGi5loHcWydkLDtdSl7/m0xGVVgq3pknNaqH9IqOhVq7LYx6L9Qr8TfqVlAYffrlS2mEYvvphpXSeQkaNChj7AoQ7yNhdtzD7P+zAN9FyQI7O4Lq7PdHiPGZF7ckDofduTqEsqpKpAecykCiElrTfKQSYul1TmK+4DZcpITuuvX7g2jbhy/iI3lUUHZDawI0rV6z1zhBo6rHuylojwiSB56Ho6N4uSc1y2IBM+QyUMZDrQmJj8xs/GWYaOSzPNnJWCwG7PDpiyCiIVqllHlDpdFFacSw37P+N13ee3sctNMIhdXPjJjrALAaHZrKAqMbIGSHwNGNrBa88Tx65fdonPyZA== 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:(13230001)(366004)(1076003)(26005)(186003)(66946007)(66556008)(66476007)(8676002)(4326008)(83380400001)(36756003)(316002)(6486002)(103116003)(508600001)(8936002)(2616005)(6666004)(5660300002)(38100700002)(38350700002)(86362001)(2906002)(6512007)(6506007)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?W8IZqy40+OQSheB9CfZtqt5UpyCRvviqecURbZKp0si21zKeRvDvbfhVYUHO?= =?us-ascii?Q?cIihD6qx6/uEGr32vgutswKdbjIp06L6JxrEvI8xrZJuiyecwCuKx5J7kVYF?= =?us-ascii?Q?JOXry5bO4UxY/8QrqDpU3zoaf6MjNwM3rfkxIFNI6V1oju83NCAspCOii6H4?= =?us-ascii?Q?uS9QBWxsYkc5yufsy1jfQckMLsSez0bl0jJYLhRVUbilyr41J0Q7g1KKl21A?= =?us-ascii?Q?HHE1Fo1NmcgjlnWNX0/7cvVADcKHgJQWwmJcZV8OXBH5TlRTUhKY8Yl1d4uA?= =?us-ascii?Q?A4i5n27ME+MrO3Ji/MaVr/OoRlLkQfL3tLcYI//+eBB/zcxblkwUwgJaDNK6?= =?us-ascii?Q?LgXLy9UcliZ3WvD+wAhgx8j+VjzlRYGgKdvppsvHk1pHgqwHcENLpzZk3E+4?= =?us-ascii?Q?wB1TunEI0Qln2x3KPtmRlIRMxF3uBXUO7H0mRquM81oTtdo2XZ0PHROOFYvw?= =?us-ascii?Q?uecxzno7s1bTxH3/gIg+htqp6KUUX3WsS9o+nEqERcEzfMl6W5JMeKJiCS/F?= =?us-ascii?Q?yam6niiGEeZcpvg9EBbG56qr8hkU9dkTaAzqMi8UVs5B0FJrkfvkKZuegdFm?= =?us-ascii?Q?bkjNxkAn3WpUxbcFq7/hu6r6lmuiU3UbZzL4tuougb68KVFavcASXt+Ew/vg?= =?us-ascii?Q?Uc0UtalF8G81diByTT3UI1uPS/GCdSpg7lB7Z2i4vtPo7MKtsQFmAvDbeA+t?= =?us-ascii?Q?JP+2yyGkAh+oHsAK6mYwDcPJVkXxpE2QmSot4+ydSsJjGmP65lc1Y5j/JnAd?= =?us-ascii?Q?mXjjmvgHqhbYJB77JBFsqxMsO3XhMU7lhyurFNUCiOyK3nDJM09Hi/6HOTGI?= =?us-ascii?Q?nWn7dlYJOMjQ74IMvxiFPTdfLbwbsZ9Hni+t+WO5Q72DWX2Y+ZZsBFBNIK++?= =?us-ascii?Q?N8MpBI9JfzhHyeW1tprXxYFF1alfuHIEnmg5tXSIIKsqGFrBXxHu2Z+6++kR?= =?us-ascii?Q?U6Ew8mzGfkMXFnZxh3vgiiEvDeUSsDnOQB+nL99wrZo9v6onT8vpY0OCxMFw?= =?us-ascii?Q?9wRnAESg3Gtu3Y1RMc6Uf7FR88eNR9wSJaf6y/Mwy6vHLORHlRQ3UTQzuajI?= =?us-ascii?Q?6TksMmWUL7+kSYeTcA27fjKnDDzbkI5xEoalX+bhE+JxzqOHKVr1/JWAnNHA?= =?us-ascii?Q?ravaicFs59K/yZb2lBsravKBpe/PfCBJ93KNbc1stb1rKP2QqQEG6oLNDHCW?= =?us-ascii?Q?HrQ3ptKcX6+J7w9Sd1D+DqD3hEiz0IFZcD5R7/vvr1EfWoWI00rCqOir9dub?= =?us-ascii?Q?FFBs3eS+op89ZXfmZFJ5gKXhqGjHc8fE4oOwG2mFkXLyapqd2DytDACuPNTz?= =?us-ascii?Q?NyUX4M2CaoTgRePo1zJvK1RRz02uU7krZ/p/0xRE4JgjY+XDS9BmBpSw95G+?= =?us-ascii?Q?Api11Xov16BvBxLaAJ/Yl4FLHqp/bYG6HPoIMeCCgPslddHpQyJ72BcvZV+a?= =?us-ascii?Q?UGqyETUohiDWIKvSJmTuV6BWKh763cR404gaftt2O2WZawoxWYHwt14MTZTI?= =?us-ascii?Q?7+r4CemjyugD+3Ud+ZP5+7gTOMelgSVc7US29j5bpH0rP5kRF3I8sxy/klf6?= =?us-ascii?Q?0xJev3UKR6vdZslzRF4i0yrIH4mdAHp1m6huw7IT8weBASCCxrjIr/HKqLSH?= =?us-ascii?Q?aJdOXmvSS36eCJ3tJYjHvczN3tu62Auy2/LNjCYEmPM6kMmenWYnxw8PILUH?= =?us-ascii?Q?kDfE3s5VPt374tw5MBLMp8zYEzHtG7ZbrCh+fytUw7L4WN2e/zbBTdbgWNC/?= =?us-ascii?Q?GXYjWs5q5Y/X6Zc+pUi1+jQMuNglJzU=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: bb08536c-20d7-48fa-faf4-08da1a9b17b8 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Apr 2022 02:37:44.8016 (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: oZ4winJfthGmoY/4fBxVFj40V3/8Fc049mCWWTN2a4gOJ6V473jJ3w//6e/O8QpWT54NeuZbGqGEW375f/CaaA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR10MB1238 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.858 definitions=2022-04-09_25:2022-04-08,2022-04-09 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 adultscore=0 mlxscore=0 bulkscore=0 phishscore=0 spamscore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204100014 X-Proofpoint-ORIG-GUID: yD9k-EHD2wxibmQdxwcLkxTTeYSUtLqx X-Proofpoint-GUID: yD9k-EHD2wxibmQdxwcLkxTTeYSUtLqx Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 61a8edc4ba8b..17b438498c0b 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -917,6 +917,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_sy= scall_ops *scops, idr_init(&root->ino_idr); init_rwsem(&root->kernfs_rwsem); INIT_LIST_HEAD(&root->supers); + init_rwsem(&root->supers_rwsem); =20 /* * 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 0946ab341ce4..0bffe5d0f510 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -890,6 +890,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) /* kick fsnotify */ down_write(&root->kernfs_rwsem); =20 + down_write(&root->supers_rwsem); list_for_each_entry(info, &kernfs_root(kn)->supers, node) { struct kernfs_node *parent; struct inode *p_inode =3D NULL; @@ -925,6 +926,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) =20 iput(inode); } + up_write(&root->supers_rwsem); =20 up_write(&root->kernfs_rwsem); kernfs_put(kn); diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index eeaa779b929c..82c6b16645bc 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -47,6 +47,7 @@ struct kernfs_root { =20 wait_queue_head_t deactivate_waitq; struct rw_semaphore kernfs_rwsem; + struct rw_semaphore supers_rwsem; }; =20 /* +1 to avoid triggering overflow warning when negating it */ diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index a64a04efc9be..1ac36b2a89ab 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 |=3D SB_ACTIVE; =20 - 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); } =20 fc->root =3D dget(sb->s_root); @@ -376,9 +376,9 @@ void kernfs_kill_sb(struct super_block *sb) struct kernfs_super_info *info =3D kernfs_info(sb); struct kernfs_root *root =3D info->root; =20 - down_write(&root->kernfs_rwsem); + down_write(&root->supers_rwsem); list_del(&info->node); - up_write(&root->kernfs_rwsem); + up_write(&root->supers_rwsem); =20 /* * Remove the superblock from fs_supers/s_instances --=20 2.30.2 From nobody Sun May 10 17:53:08 2026 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 D0C95C433EF for ; Sun, 10 Apr 2022 02:38:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243454AbiDJCk1 (ORCPT ); Sat, 9 Apr 2022 22:40:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243406AbiDJCkE (ORCPT ); Sat, 9 Apr 2022 22:40:04 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 692E120BFA for ; Sat, 9 Apr 2022 19:37:54 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 239F4v1R029741; Sun, 10 Apr 2022 02:37: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=CY/y9L7LrcOra1Drfru3ZKfpj+S97x8rwFLWbZSFtzs=; b=rjPlEELNYP9/m3iFJ0kCTEfrNiykuNLP8op9exZQ2+s7nrQDuC0w/+bkr3oX4KHqv86o JgKEG/TiQrMENq98Ha+LMCRx/GQ8lVCFIOn+fq6xngDsybOUNxr60etVuu6QX3spXzZP jgbLY4EOnMNg+4AOf2tgGlGk9gp4LblanJ/23VCJg9hkq8kGw0VHR2KlIRuxOEXD5Pkl IKlS1nKbqkieTL0cvyvcQOZwb+O8LTBZtB9rEY7D8vfg+Qzu/AU/zZ9iROWGk25VgOYw IqOLdsawfu2FXu5/uVtaNJBzhdYmFA0ztj+sZsR5Gsdq5LC1nJyVUVws1DgQnNJZXZ3F pQ== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com with ESMTP id 3fb0jd1110-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:49 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 23A2VrdH034017; Sun, 10 Apr 2022 02:37:48 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2102.outbound.protection.outlook.com [104.47.58.102]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3fb0k13bft-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:48 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IPPRfrHVN4kpOOrTELYtkdJLfmFfaB9Tt1Fq34UG3dd2LzBOd0JgheI1hXbaZqtW57sEyEFzsH/ALiPZoboqspSizoO5RRyxIjZeqZG76HXhPXWrZBxhJWvlnfB4gYNvjiWbMlFGCXszDOyJMRrJdEb4H2vEHVUEdkw43a0q/fB5XRR4pyLUUvgn69C/Kz2b0HgTqNxotX9AfzTh3mOVZazFS0lk6ZEFVy+f14x14D8YbOM1WEdqwye3Q/buYKJSeFyYNfyMv04HktqYWbTeCGMHvNFQSRyTB5yJNQDmT5aN/maMnyTGqMdSryPIq3sc+CKNjOxEZRu2cbhycMYRCw== 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=CY/y9L7LrcOra1Drfru3ZKfpj+S97x8rwFLWbZSFtzs=; b=D5ZiA6jzGM0LK2kje8n+f3MVJ/aWRJkk2nJXBQkqAIkXsemJmRCn5TAP8NWbeQ5y8X7rZbyv7wqUv3RoeaCcsTXfAaxK56oUeiTFefjt8x6iGQ/mR1r/eaYEY01g1TbOFGc7BYwYLp1CNlHG2b/QVZ9kshTORkHbjTVOn6JJRbhL+wSB44te8Q7R+ac0hzpppgDn1ngvVJysfxZTozV+gZnN3oXXtOQNn1v5DWXub/u+bZ3+WVqsebZXZ6qSzQGotu0axU4eEFxY9it8PrvNALyacTimTW19WPmUG47v6UFEUw5ytyXNd8manssfQlicctj86Ljh/9694eeT3xuiyw== 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=CY/y9L7LrcOra1Drfru3ZKfpj+S97x8rwFLWbZSFtzs=; b=bj2eMX+p1/2xRsNBrrTyRLDAbW65Wh/4Pq0WouqFeMH+654X/qyVSRui2dRr8RM+nFGqnVJeX+h9Q8Ihx8UYdYCjAdi5ZGrB++eJaOYXVN7k4v/QItoioGlWa5FatSFqIalo4AE1dys7bDG979W+3Mxxu0cZYb3LAbIUoDpigKU= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by CY4PR10MB1238.namprd10.prod.outlook.com (2603:10b6:910:7::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5144.28; Sun, 10 Apr 2022 02:37:46 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa%9]) with mapi id 15.20.5144.028; Sun, 10 Apr 2022 02:37:46 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org, ebiederm@xmission.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH v8 07/10] kernfs: Change kernfs_rename_lock into a read-write lock. Date: Sun, 10 Apr 2022 12:37:16 +1000 Message-Id: <20220410023719.1752460-8-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220410023719.1752460-1-imran.f.khan@oracle.com> References: <20220410023719.1752460-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0054.ausprd01.prod.outlook.com (2603:10c6:10:2::18) 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: 008d125e-a24c-4cd3-3173-08da1a9b18bf X-MS-TrafficTypeDiagnostic: CY4PR10MB1238:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JntIhHrfMrYfQ0f+MNVeb17/LM7lW1mUi1t5FSsBepT/J8AeCCj2zpQkjr5iKUnuvpEXoD5crgQZYk+n8qE4qLxYzgRz0CBkO5VkNGmxLsUDwIAkYa8NzU1YQgqyDyr35mbFlbnPhTEowqGIBbUeksol8505ZpDgBlN9CVS5zs3Edf0iME1aQqFKXOPcCSvjuQg7CabKyF0sV4kI0/b7wh6CZ9IUU0hJZc5L+uZRnb3RLkimMVvVh0vU2wSfwOk2xEs0rKNvFIrZRzkovdRv+dodzDVnqAM6gmDDQ6HzlsKbyRxN7LzIw473+kBewwWyerzGTd3dSeGGUDhPx2ktVj1hRcT1hrF6GVvBtJtKk7763qNLx8WdGIM3NV5vtHB57OOt4N+DkP5d75mqsPM9WJQ/xeP7bN2GIjUKFxQvOwINembqgww9C/2T1vTqi1ZpHU8H/FL4PQwBPB/LYkk9msla8yfgd5XKHcraJGyWhFgKjtmZszDUIN8RrU0RPkVDWEOrraLbTAZBkVcf/6uQNmOalJgPUP9Hc7+hWeLroD2A2HWPbsK+8ljlzW2bHKkIdWGr721+etVqSX/EKDiw9Rulwa/p4kueoDmnvsIYU2pyF8DEMooUXXAUSQMRh6185cipj5z6Xj6iHx6/iJvRt2joWAiUIyUWOmLxkNttELG94fRRVKUuShpXZcb/dWif 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:(13230001)(366004)(1076003)(26005)(186003)(66946007)(66556008)(66476007)(8676002)(4326008)(83380400001)(36756003)(316002)(6486002)(103116003)(508600001)(8936002)(2616005)(6666004)(5660300002)(38100700002)(38350700002)(86362001)(2906002)(6512007)(6506007)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?sQ7Jl2wmVRUVKb2G/gx/449cGvqKNV/q7b5npICieQxhQGWzAxO7WBiPU9rB?= =?us-ascii?Q?NEfa0LMMxDosC42dRVBWj6kRkEhv8TG4ur+q4Y36oGEStG3mp8t2i9CXKW7g?= =?us-ascii?Q?iVfXzhP+Oh6qGp3UjTtrOlgM0tgXs3CSqrw69fdSD4y6Ms0T+4+aN668mFUQ?= =?us-ascii?Q?NqGNb6BQRA0n0Bs2RaabTSRVpCJ7isPAPPzfjlYa0HjE6lmmK5reyjhb++AI?= =?us-ascii?Q?lWobtrpIlpxCNjq7HoNjTviLDgYX2WSPns9FD1fl8vwsAFsGbRMfTe2WhFY7?= =?us-ascii?Q?R492qpkFCBmNbGoRu3WNyvya5eyz4qkDZL/j8VjazrNw8KTK6AaqZ5F3NdV0?= =?us-ascii?Q?CS8QCH/sx9TNz0fk5hdlSZeVWpu5+cuX8zdJswGwGKLucymImXrv2zxwptAC?= =?us-ascii?Q?v+vpTJqQ1jsxJtSMmfApfJe2rMn8I4mhOTw8Me9UXzIrWKo168Bl8yqUCzjL?= =?us-ascii?Q?TgXP2hPdwYBbVCxQF+pU+73uNVybwToxsbC8nGS07Ezv9y0L6kyYlz0mnDHw?= =?us-ascii?Q?aaVcQLHTmX+cikauas3ZyZ/DupCogvSeXaqWg3pv6BbPAduYvJ1siAVnYBen?= =?us-ascii?Q?6NeGlXL4ztuMx9T+bC5GvkimUAcDbbbXoqeXdTJ++0Z5CQ1Rm8PhOQcYx7zd?= =?us-ascii?Q?cuHk4qCX/dGcIG5TWQYZJAZput87GCPUve2K2plbHisT9pKKl4Di8bZgoJDH?= =?us-ascii?Q?pjIr6lFeIl9WzUk6yjTB3Zj7B3LGML/a0D12yiRcAJghnk6hivzIqLPwKALi?= =?us-ascii?Q?tRsuSabZA1b1N34tJryPRYrhgsAGb33IXD1seCr/Xdghdsc05X4vO9cAhfNo?= =?us-ascii?Q?dW5c6TUxHrbkNoYEjCs/289zhYEomkJCUEg6LkID/I1WrOeIX/oFKfvjwuXs?= =?us-ascii?Q?rhv1Zw62Zo2IQduvwti8DzikUDxC4UzZNI5eKSOfU8gx+nNN0pxz+jBNyZTg?= =?us-ascii?Q?mu8RCg43FwAhrvwQCavxAckXyjAk2GUSw4G2y/kpYDXoxdzqyqrS9ZAKUJ76?= =?us-ascii?Q?b+Ycw8u97TbN/qb4fBwSAT4npTo0G6jktWLLFCB+dpsqQk2VOAoqlcqAHr+Y?= =?us-ascii?Q?oeUU5eD1Sv1PiexmHL4P7pyPo0Gb+VZmlM4jNhW1VBXOJZ5vBxDMvs21xwFz?= =?us-ascii?Q?ux+hjl9q+8h3cxKqnxcp0Stf6s7AvJ1qcGeGI+1FpQg75/pq75QtQIz0w/Oz?= =?us-ascii?Q?1lzp6Kjxcpy/Dlkex6nBeoW+X2h3zd/BglEL4NifchDuEdGPFLKA1zpwjTC6?= =?us-ascii?Q?nQU+395kMqbjy4sbUOXtP5DrysRvYTAn1sAhbSvLl7IejMEyRlxR+Gkr5fv0?= =?us-ascii?Q?ACsZfcJYHilnMzFPoojy60QvOj24tJ7Ay150E7yR9mAXlLy/xlteOQkgBVcq?= =?us-ascii?Q?jT7w6yjLffxq9kyPwtZAeFxc4LbYNj3VvvrHSoF+HRS5S7V6yttwRR/W2N1d?= =?us-ascii?Q?RCEIU8SYw0nQtoTSpsfcpQJCfgQIrl9WcWkxyozlcG+CUyDgLzB/qvBRMcFf?= =?us-ascii?Q?i+QA7eaLYLxaAxudlFpFxSXOTe4ujARkvMhUJpzUtQdqYDLe826uNNTyfGDS?= =?us-ascii?Q?UmwPA6MGV048Zt1kqq0+TElHbhLdjgzY6+FZh5XECQygWTEe0gl6TObC3Hbn?= =?us-ascii?Q?tgO7uWiZT5h8Pe68txmrkQwfqJ8Sk/zZhBC0QZnnGfiKubTH0KpTT4Fq2zAJ?= =?us-ascii?Q?ZgdZ9lF3Cmj3ddTKCb4qQX994bUKf3AYkNbTGtFIWp1tDA42ckVYlCH8L2fK?= =?us-ascii?Q?09zNa2TZ+s0jP0hGKa93C1HYIju4d0s=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 008d125e-a24c-4cd3-3173-08da1a9b18bf X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Apr 2022 02:37:46.5033 (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: EAdMZKuwHV64e4pjwYNKB654e3rTbeHFNbjZxlhceufC3F6GWw6RezlbXMWgy09oI5wmdhDjcprTjaaH0Zaecw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR10MB1238 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.858 definitions=2022-04-09_25:2022-04-08,2022-04-09 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 bulkscore=0 adultscore=0 suspectscore=0 spamscore=0 mlxlogscore=999 phishscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204100014 X-Proofpoint-ORIG-GUID: bdOGdnNfZvs_eG_uuecm9uXmU3pkXxgo X-Proofpoint-GUID: bdOGdnNfZvs_eG_uuecm9uXmU3pkXxgo Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" kernfs_rename_lock protects a node's ->parent and ->name and its current us= ers can be easily divided into readers or writers of ->parent and ->name. kernfs_rename_lock also protects a static buffer (kernfs_pr_cont_buf) which is used to hold read attributes (name, path etc.) of a kernfs_node and we can maintain this functionality by accessing this buffer under write_lock. So change kernfs_rename_lock into a read-write lock for better scalability. Suggested by: Al Viro Signed-off-by: Imran Khan --- fs/kernfs/dir.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 17b438498c0b..8e8c8b2c350d 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -17,7 +17,7 @@ =20 #include "kernfs-internal.h" =20 -static DEFINE_SPINLOCK(kernfs_rename_lock); /* kn->parent and ->name */ +static DEFINE_RWLOCK(kernfs_rename_lock); /* kn->parent and ->name */ static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by rename_lock */ static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr */ =20 @@ -184,9 +184,9 @@ int kernfs_name(struct kernfs_node *kn, char *buf, size= _t buflen) unsigned long flags; int ret; =20 - spin_lock_irqsave(&kernfs_rename_lock, flags); + read_lock_irqsave(&kernfs_rename_lock, flags); ret =3D kernfs_name_locked(kn, buf, buflen); - spin_unlock_irqrestore(&kernfs_rename_lock, flags); + read_unlock_irqrestore(&kernfs_rename_lock, flags); return ret; } =20 @@ -212,9 +212,9 @@ int kernfs_path_from_node(struct kernfs_node *to, struc= t kernfs_node *from, unsigned long flags; int ret; =20 - spin_lock_irqsave(&kernfs_rename_lock, flags); + read_lock_irqsave(&kernfs_rename_lock, flags); ret =3D 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); @@ -229,12 +229,12 @@ void pr_cont_kernfs_name(struct kernfs_node *kn) { unsigned long flags; =20 - spin_lock_irqsave(&kernfs_rename_lock, flags); + write_lock_irqsave(&kernfs_rename_lock, flags); =20 kernfs_name_locked(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf)); pr_cont("%s", kernfs_pr_cont_buf); =20 - spin_unlock_irqrestore(&kernfs_rename_lock, flags); + write_unlock_irqrestore(&kernfs_rename_lock, flags); } =20 /** @@ -248,7 +248,7 @@ void pr_cont_kernfs_path(struct kernfs_node *kn) unsigned long flags; int sz; =20 - spin_lock_irqsave(&kernfs_rename_lock, flags); + write_lock_irqsave(&kernfs_rename_lock, flags); =20 sz =3D kernfs_path_from_node_locked(kn, NULL, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf)); @@ -265,7 +265,7 @@ void pr_cont_kernfs_path(struct kernfs_node *kn) pr_cont("%s", kernfs_pr_cont_buf); =20 out: - spin_unlock_irqrestore(&kernfs_rename_lock, flags); + write_unlock_irqrestore(&kernfs_rename_lock, flags); } =20 /** @@ -280,10 +280,10 @@ struct kernfs_node *kernfs_get_parent(struct kernfs_n= ode *kn) struct kernfs_node *parent; unsigned long flags; =20 - spin_lock_irqsave(&kernfs_rename_lock, flags); + read_lock_irqsave(&kernfs_rename_lock, flags); parent =3D kn->parent; kernfs_get(parent); - spin_unlock_irqrestore(&kernfs_rename_lock, flags); + read_unlock_irqrestore(&kernfs_rename_lock, flags); =20 return parent; } @@ -824,12 +824,12 @@ static struct kernfs_node *kernfs_walk_ns(struct kern= fs_node *parent, lockdep_assert_held_read(&kernfs_root(parent)->kernfs_rwsem); =20 /* grab kernfs_rename_lock to piggy back on kernfs_pr_cont_buf */ - spin_lock_irq(&kernfs_rename_lock); + write_lock_irq(&kernfs_rename_lock); =20 len =3D strlcpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf)); =20 if (len >=3D sizeof(kernfs_pr_cont_buf)) { - spin_unlock_irq(&kernfs_rename_lock); + write_unlock_irq(&kernfs_rename_lock); return NULL; } =20 @@ -841,7 +841,7 @@ static struct kernfs_node *kernfs_walk_ns(struct kernfs= _node *parent, parent =3D kernfs_find_ns(parent, name, ns); } =20 - spin_unlock_irq(&kernfs_rename_lock); + write_unlock_irq(&kernfs_rename_lock); =20 return parent; } @@ -1635,7 +1635,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct k= ernfs_node *new_parent, kernfs_get(new_parent); =20 /* rename_lock protects ->parent and ->name accessors */ - spin_lock_irq(&kernfs_rename_lock); + write_lock_irq(&kernfs_rename_lock); =20 old_parent =3D kn->parent; kn->parent =3D new_parent; @@ -1646,7 +1646,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct k= ernfs_node *new_parent, kn->name =3D new_name; } =20 - spin_unlock_irq(&kernfs_rename_lock); + write_unlock_irq(&kernfs_rename_lock); =20 kn->hash =3D kernfs_name_hash(kn->name, kn->ns); kernfs_link_sibling(kn); --=20 2.30.2 From nobody Sun May 10 17:53:08 2026 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 AB1AEC433EF for ; Sun, 10 Apr 2022 02:38:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243418AbiDJCkt (ORCPT ); Sat, 9 Apr 2022 22:40:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243425AbiDJCkF (ORCPT ); Sat, 9 Apr 2022 22:40:05 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC5384ECD5 for ; Sat, 9 Apr 2022 19:37:55 -0700 (PDT) Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 239MexTI001710; Sun, 10 Apr 2022 02:37:51 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=Z5Ol7w/EPzmU8l38c/lxm08Onuv1cO3WEBJ69b9xmnU=; b=horo5eKvBFc36oxCST5ewAG4Pd4ZbuShxGvUpqZvS1ISmTWB01LznmUVEU42NfqsWESG 9Eez4Tr6+UkuPCU2myjZ1OrTb9Y9+xktdB0SIfdSyBxI4Wk62llrLZ7ZbufTCdEE88Dj pIdmwMk7PyPIaouCa/wYN5JgMIvIsOy3Y23Zk9GTAjnblwFNqo0p//MaDrzZJGAqXVJ2 sEYh7Fn+A4QCNDS2yDhbeF1Y4/gC8mWKwAf6kDdM27HMZ7iT08qY2b2r7DP014CsDTJx YaSe6k5izsFQYaBuNrf/jgd3vJVr8wfiifG40JW+AG9ExivadKfSJPi9GRkZk0M+q84v Ww== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com with ESMTP id 3fb0x290q2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:51 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 23A2VFpV034352; Sun, 10 Apr 2022 02:37:50 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2101.outbound.protection.outlook.com [104.47.58.101]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fb0k0gmus-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:50 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XuGnzKq9eNuFYSIQaEUhRqmoyVWUqEl1bgld4r76lI9bUjqDXP+oPqWruSgmTA9QZJ6m6gvE+9ylSxF8+C+x/d8UnngD0CKIMyLXt6Ov+hkZRKKQ0CdQAKvTWHSF4djX2cc8NFOz+DL2hg3GJvQ1CeD8YQdlYxDtYLNGX53Oygez+pLLwbKOPQ0XKnHulRiiNlsxtWuBoB3LV0/B3pL3LKo3VSgRzrtnvYh4SrZJVdW3aSiWDJTJhRbRXaAWvSaOlaQYiWe5RFTIdjnwu93/NKMxeHeilEU5Wxr6nTvLVC1Iw2GDTq5jU2upF5lXtwTD6vJF0A2jbqLklA2S4bfxsg== 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=Z5Ol7w/EPzmU8l38c/lxm08Onuv1cO3WEBJ69b9xmnU=; b=mjhmPMBuZEuP2WNipNrDpmr4fRBV5zdd1st1tUCxcNu3YcE4In7uzX9/pgynqM3CMqLO6Nwyvj8e+n6SmrGLGzENPRJAzQ8k/YQ5Tg7s8UxKRtqtd7S9TPOToWnqBzv9NEqbluP+BvdigMABK+/yzHX//dUHnqSL6N08ybjVZWA+raCKSboS35YZvFBrUxLlahiXx94ir/7u9DtM53TuXWAvojVW7RYLh2FAaG6JeLzkCNCfIW93nzHHTOQGxkqcvWQrB9FUI+K1jXp/vsn/MotXB5F+otZdKC1kO4qEhwsODwHp/JC3vNI5FpUz0R8IHjIrMc0XjAOKPBDXHhuKBQ== 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=Z5Ol7w/EPzmU8l38c/lxm08Onuv1cO3WEBJ69b9xmnU=; b=Y9F+Ltj3vBxRfx4cMvbIp5+rShVY8QJ/fLFTDUKTucEzbUS5aGx+1YiqG5gwRJNT6gn4uGJXIZx5XIfLq2niF0rgAGr5zeuSiBTPZnZsyL/HdUjQOYqWswolYGMooZEkmnJ4POTJW/tsIDUbWwZbmwLRPFk0qE9Ly5DBHF3CpzM= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by CY4PR10MB1238.namprd10.prod.outlook.com (2603:10b6:910:7::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5144.28; Sun, 10 Apr 2022 02:37:48 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa%9]) with mapi id 15.20.5144.028; Sun, 10 Apr 2022 02:37:48 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org, ebiederm@xmission.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH v8 08/10] kernfs: Introduce interface to access per-fs rwsem. Date: Sun, 10 Apr 2022 12:37:17 +1000 Message-Id: <20220410023719.1752460-9-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220410023719.1752460-1-imran.f.khan@oracle.com> References: <20220410023719.1752460-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0054.ausprd01.prod.outlook.com (2603:10c6:10:2::18) 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: 5ac67661-c33e-42d6-fbf3-08da1a9b19e6 X-MS-TrafficTypeDiagnostic: CY4PR10MB1238:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GLkWdEhobpx5wxcJDDEVEM8goNgmzoV6I3QKDU9wKvQEf0y95daPTMMDgo0lo5BHgDfn0FS79sIy0CjEq/hG0VLtfkTYnuffIHqk1HnKYamfmhozxZHPpSyWkump2QHAU8sTum3xqa/daueweWvq+MEIOf/WtS3ieEIv56nIYwpSKcBNuzDAgxdtDqqMqeKAtqdhZHPtxNjw2pUSIS/3+cUWpcS9tRvs6qkiYN6DqPnwdewk83vpHW2K6pFsOME5lqe+YoC7rVd/YM9uIfxmDMuiXNjob8AronxmT1vLzTAIHzv1pTVQfFbuuLzBWdc9OKdbvHsJHBHvZWLR/UCYOp1E37/gMo7VtAuZw4N0rXPQbk76d/FiyPIRG1d+ah7/JLhijXGk0l59wy0s0HfmEnB7Pt3vkQDoaNvptCf4VT7o2cVX9/wRjm5YiBVZUjJjAHhNs9LZZQ9T9bA9Uj9bAWBSL5MAp4yeSGt9dw+V2tphiQ/qYMsiL5CBiJ3yga4ZQ8RA/JbyMPIWlkaYnQ3Jx65sVhPmqz1dgEjyemjHHbO59nhRXiwSbKuw82uMw08MFUkQ7MzCnkJCCk/439Xnd0p6VBky0IaeegU8txurh+rUQHTBAuwsjfZztM/eEaKt+qHcIWUSuEyO09dfwJoetm4JfOgAMYv7FlTKFuReo2OfFRUmiUE6Te0aEqHRVi3p6Xr9XVneZkSedfX0uQWOCg== 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:(13230001)(366004)(1076003)(26005)(186003)(66946007)(66556008)(66476007)(8676002)(4326008)(83380400001)(36756003)(316002)(6486002)(103116003)(508600001)(8936002)(2616005)(30864003)(6666004)(5660300002)(38100700002)(38350700002)(86362001)(2906002)(6512007)(6506007)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?84Y05QvgkH655cZjb/z0nzVLUY+HFiIYNlJUh0qfAzd5UaqWcX2uh8jgiPx2?= =?us-ascii?Q?9pWI/06g+5EJuiI3P3exkGT9+Lpw3D1dqSDljLd/M+luj9L6WR6Brr9gHSwD?= =?us-ascii?Q?QKIksTE7MVFojCNzCk4TGppANzlUn+BAMCHBPxtiyjVSZptA0pBgrNdWJMsw?= =?us-ascii?Q?KNjESMVDmSoVWyMTwAFuQYn3FJL+5RWWHD5/8qlRK+cedA1SsMdgTQzoTdEm?= =?us-ascii?Q?fJh7oWA9dfdq349amw13XO5/Qt0WU3LiQ0xFSwD9R108gLMb+1p8YTImjnOH?= =?us-ascii?Q?DkGuuwO5WBnozoV6ngvx/Dz5I38nTqSD5uhLRoWawdTpr4A4ccxFTMZRiBbX?= =?us-ascii?Q?tOfe17uOjvvmBxGqPF2xJksoEZboEJV8QtEpuvouTePI+WkjNKZYUQiXxRYN?= =?us-ascii?Q?yykpkXtKgaCS3bzfhaA3FHrsg8DlHpxhM94XHVosn2dZagxjWW6Bt/ub5mQV?= =?us-ascii?Q?mZc/2okeWEBOR/Iy9AqQPZFhpBc8FbvjWTzXsCJpOgQ/h8WCqbRBcMgyUVJx?= =?us-ascii?Q?7vrD78W9f4BnI6aaBoFhrDqXEAfdWITZEy1NycSo5X62pkv1EB9An6Imd50H?= =?us-ascii?Q?RVkncxl3Op71xv+iqlraz2cOWkxvoTgCGrS6+MtIY9eThzIqB0ycQXldMZbm?= =?us-ascii?Q?pg7QTBOklcpCEG28416pTlINsHu1WWM4Bo6E2lPY3Az78yvOtLxpnZfYP1X+?= =?us-ascii?Q?c8QPNKgRwp6IgXDe6I/uwNIj7s5Py1YedJvHu3FQjkQq3ctctna8dXO9D8jd?= =?us-ascii?Q?PzwTf45t87hfYNo9zJZEHwYXtS3EvDc4purYuXmJVbkNckc5YrVdQNI71muw?= =?us-ascii?Q?N+kDjAvN8UBOLgcIfij2tGOSOAmhySLhjTyvlJED5e9AsR+GOlZs3OmMD/Ab?= =?us-ascii?Q?SaxDvaFV85ArWjHBsErc/g/bc2HqlkRldd6IcihHeD9/9FJR0ArxenrpOCj0?= =?us-ascii?Q?PDHGPlsMqdofpm1jhFO8GUcOYn+fb8AxkrPVO4OJlN6eVI9JU3wJLDWvHJPo?= =?us-ascii?Q?fFZ0pNs3GnvHmk5qAT5x+EEOEX0xFbrCh5CvXAikh7DtOCvg+K0OIbpAFTZI?= =?us-ascii?Q?zEfC7AueWg0Ob9U2WRxg7pcHU0xg2676fll8HCRRVYUkln1hJXb8PmhqcB0h?= =?us-ascii?Q?E7QFm8A/52FQdvMiKkzwJWwopbSdG1W4v+/JN0UY0C/nxXx/Ym8ECk9YR3SY?= =?us-ascii?Q?de4WyblH1VGjx6EHiB8z9zN3q3qYtqrKRbEd1YrnSceZjZrT1u/tt+PRz+u6?= =?us-ascii?Q?zaO+aZZ0LLxbbqwBY624r1xyU0FzANrCRSUaWtt91VcDskjrrnTqqO8Z8mUU?= =?us-ascii?Q?gh/oLsHcvcVxEmDKoCrjg5DROwjbzPqz90XZg7t1KmmLkw0yYvlqgf6pVb14?= =?us-ascii?Q?3TomVB0iWdAElldqtK2CGQhJuHUM7QopNMJhdTEvyok2TNtpPTeMQ2052z2h?= =?us-ascii?Q?t3aPA75fzktMsZzgaD/3/yoLKpiH4dwfyLqLzyvBlMHDfTehl7JfiFco787c?= =?us-ascii?Q?Rt1Zy/Al6QQHPPv37Lfp4jyxhCQwuZjHC9lzpyuOuzMfOfMehq0+FNOcPS0E?= =?us-ascii?Q?g0DiZVNKjjjQlFJcjrUFKkNbn/45V+7MgHdwhoyGoNZ/5ob8dcgzWQ29V39u?= =?us-ascii?Q?5AL5br+lfF9kVamum3MKgsw+pgsF8EiI5SJBaIRgts1IfvYxEZWsDb2A1W1v?= =?us-ascii?Q?uD1GIdLRSdADfm9H7EztxA6+qjLkFlBtZxjaulZ7rbs+29VuegAAwTw3j57q?= =?us-ascii?Q?peHiMZ8VratirieId5Dgu2tigyEi2yA=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5ac67661-c33e-42d6-fbf3-08da1a9b19e6 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Apr 2022 02:37:48.5210 (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: C/0cijGhCjdzKi+TqOWiGdnIwceBfjP7rduHg0zglRYg/c5dPAIphtmZXvMYOU8zMPJETmhtkTd8goOPBPaESA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR10MB1238 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.858 definitions=2022-04-09_25:2022-04-08,2022-04-09 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxlogscore=999 mlxscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204100014 X-Proofpoint-ORIG-GUID: tfWoCSAB7jgH5JSUTCRFRVFGwiCALvlT X-Proofpoint-GUID: tfWoCSAB7jgH5JSUTCRFRVFGwiCALvlT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 | 114 ++++++++++++++++++------------------ 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, 156 insertions(+), 79 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 8e8c8b2c350d..f8520d842b39 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -25,7 +25,7 @@ static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr= */ =20 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) >=3D 0; } =20 @@ -461,10 +461,16 @@ static void kernfs_drain(struct kernfs_node *kn) { struct kernfs_root *root =3D kernfs_root(kn); =20 - 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. + */ + struct rw_semaphore *rwsem =3D kernfs_rwsem_ptr(kn); + + kernfs_rwsem_assert_held_write(kn); WARN_ON_ONCE(kernfs_active(kn)); =20 - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); =20 if (kernfs_lockdep(kn)) { rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_); @@ -483,7 +489,7 @@ static void kernfs_drain(struct kernfs_node *kn) =20 kernfs_drain_open_files(kn); =20 - down_write(&root->kernfs_rwsem); + kernfs_down_write(kn); } =20 /** @@ -718,12 +724,12 @@ struct kernfs_node *kernfs_find_and_get_node_by_id(st= ruct kernfs_root *root, int kernfs_add_one(struct kernfs_node *kn) { struct kernfs_node *parent =3D kn->parent; - struct kernfs_root *root =3D kernfs_root(parent); struct kernfs_iattrs *ps_iattr; + struct rw_semaphore *rwsem; bool has_ns; int ret; =20 - down_write(&root->kernfs_rwsem); + rwsem =3D kernfs_down_write(parent); =20 ret =3D -EINVAL; has_ns =3D kernfs_ns_enabled(parent); @@ -754,7 +760,7 @@ int kernfs_add_one(struct kernfs_node *kn) ps_iattr->ia_mtime =3D ps_iattr->ia_ctime; } =20 - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); =20 /* * Activate the new node unless CREATE_DEACTIVATED is requested. @@ -768,7 +774,7 @@ int kernfs_add_one(struct kernfs_node *kn) return 0; =20 out_unlock: - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); return ret; } =20 @@ -789,7 +795,7 @@ static struct kernfs_node *kernfs_find_ns(struct kernfs= _node *parent, bool has_ns =3D kernfs_ns_enabled(parent); unsigned int hash; =20 - lockdep_assert_held(&kernfs_root(parent)->kernfs_rwsem); + kernfs_rwsem_assert_held(parent); =20 if (has_ns !=3D (bool)ns) { WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", @@ -821,7 +827,7 @@ static struct kernfs_node *kernfs_walk_ns(struct kernfs= _node *parent, size_t len; char *p, *name; =20 - lockdep_assert_held_read(&kernfs_root(parent)->kernfs_rwsem); + kernfs_rwsem_assert_held_read(parent); =20 /* grab kernfs_rename_lock to piggy back on kernfs_pr_cont_buf */ write_lock_irq(&kernfs_rename_lock); @@ -860,12 +866,12 @@ struct kernfs_node *kernfs_find_and_get_ns(struct ker= nfs_node *parent, const char *name, const void *ns) { struct kernfs_node *kn; - struct kernfs_root *root =3D kernfs_root(parent); + struct rw_semaphore *rwsem; =20 - down_read(&root->kernfs_rwsem); + rwsem =3D kernfs_down_read(parent); kn =3D kernfs_find_ns(parent, name, ns); kernfs_get(kn); - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); =20 return kn; } @@ -885,12 +891,12 @@ struct kernfs_node *kernfs_walk_and_get_ns(struct ker= nfs_node *parent, const char *path, const void *ns) { struct kernfs_node *kn; - struct kernfs_root *root =3D kernfs_root(parent); + struct rw_semaphore *rwsem; =20 - down_read(&root->kernfs_rwsem); + rwsem =3D kernfs_down_read(parent); kn =3D kernfs_walk_ns(parent, path, ns); kernfs_get(kn); - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); =20 return kn; } @@ -1055,7 +1061,7 @@ struct kernfs_node *kernfs_create_empty_dir(struct ke= rnfs_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; =20 if (flags & LOOKUP_RCU) return -ECHILD; @@ -1071,13 +1077,12 @@ static int kernfs_dop_revalidate(struct dentry *den= try, unsigned int flags) parent =3D kernfs_dentry_node(dentry->d_parent); if (parent) { spin_unlock(&dentry->d_lock); - root =3D kernfs_root(parent); - down_read(&root->kernfs_rwsem); + rwsem =3D 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); =20 @@ -1088,8 +1093,7 @@ static int kernfs_dop_revalidate(struct dentry *dentr= y, unsigned int flags) } =20 kn =3D kernfs_dentry_node(dentry); - root =3D kernfs_root(kn); - down_read(&root->kernfs_rwsem); + rwsem =3D kernfs_down_read(kn); =20 /* The kernfs node has been deactivated */ if (!kernfs_active(kn)) @@ -1108,10 +1112,10 @@ static int kernfs_dop_revalidate(struct dentry *den= try, unsigned int flags) kernfs_info(dentry->d_sb)->ns !=3D kn->ns) goto out_bad; =20 - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); return 1; out_bad: - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); return 0; } =20 @@ -1125,12 +1129,11 @@ static struct dentry *kernfs_iop_lookup(struct inod= e *dir, { struct kernfs_node *parent =3D dir->i_private; struct kernfs_node *kn; - struct kernfs_root *root; struct inode *inode =3D NULL; const void *ns =3D NULL; + struct rw_semaphore *rwsem; =20 - root =3D kernfs_root(parent); - down_read(&root->kernfs_rwsem); + rwsem =3D kernfs_down_read(parent); if (kernfs_ns_enabled(parent)) ns =3D kernfs_info(dir->i_sb)->ns; =20 @@ -1141,7 +1144,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 =3D kernfs_get_inode(dir->i_sb, kn); @@ -1156,7 +1159,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); =20 /* instantiate and hash (possibly negative) dentry */ return d_splice_alias(inode, dentry); @@ -1279,7 +1282,7 @@ static struct kernfs_node *kernfs_next_descendant_pos= t(struct kernfs_node *pos, { struct rb_node *rbn; =20 - lockdep_assert_held_write(&kernfs_root(root)->kernfs_rwsem); + kernfs_rwsem_assert_held_write(root); =20 /* if first iteration, visit leftmost descendant which may be root */ if (!pos) @@ -1314,9 +1317,9 @@ static struct kernfs_node *kernfs_next_descendant_pos= t(struct kernfs_node *pos, void kernfs_activate(struct kernfs_node *kn) { struct kernfs_node *pos; - struct kernfs_root *root =3D kernfs_root(kn); + struct rw_semaphore *rwsem; =20 - down_write(&root->kernfs_rwsem); + rwsem =3D kernfs_down_write(kn); =20 pos =3D NULL; while ((pos =3D kernfs_next_descendant_post(pos, kn))) { @@ -1330,14 +1333,14 @@ void kernfs_activate(struct kernfs_node *kn) pos->flags |=3D KERNFS_ACTIVATED; } =20 - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); } =20 static void __kernfs_remove(struct kernfs_node *kn) { struct kernfs_node *pos; =20 - lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem); + kernfs_rwsem_assert_held_write(kn); =20 /* * Short-circuit if non-root @kn has already finished removal. @@ -1407,11 +1410,11 @@ static void __kernfs_remove(struct kernfs_node *kn) */ void kernfs_remove(struct kernfs_node *kn) { - struct kernfs_root *root =3D kernfs_root(kn); + struct rw_semaphore *rwsem; =20 - down_write(&root->kernfs_rwsem); + rwsem =3D kernfs_down_write(kn); __kernfs_remove(kn); - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); } =20 /** @@ -1497,9 +1500,9 @@ void kernfs_unbreak_active_protection(struct kernfs_n= ode *kn) bool kernfs_remove_self(struct kernfs_node *kn) { bool ret; - struct kernfs_root *root =3D kernfs_root(kn); + struct rw_semaphore *rwsem; =20 - down_write(&root->kernfs_rwsem); + rwsem =3D kernfs_down_write(kn); kernfs_break_active_protection(kn); =20 /* @@ -1527,9 +1530,9 @@ bool kernfs_remove_self(struct kernfs_node *kn) atomic_read(&kn->active) =3D=3D KN_DEACTIVATED_BIAS) break; =20 - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); schedule(); - down_write(&root->kernfs_rwsem); + rwsem =3D kernfs_down_write(kn); } finish_wait(waitq, &wait); WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb)); @@ -1542,7 +1545,7 @@ bool kernfs_remove_self(struct kernfs_node *kn) */ kernfs_unbreak_active_protection(kn); =20 - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); return ret; } =20 @@ -1559,7 +1562,7 @@ int kernfs_remove_by_name_ns(struct kernfs_node *pare= nt, const char *name, const void *ns) { struct kernfs_node *kn; - struct kernfs_root *root; + struct rw_semaphore *rwsem; =20 if (!parent) { WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n", @@ -1567,14 +1570,13 @@ int kernfs_remove_by_name_ns(struct kernfs_node *pa= rent, const char *name, return -ENOENT; } =20 - root =3D kernfs_root(parent); - down_write(&root->kernfs_rwsem); + rwsem =3D kernfs_down_write(parent); =20 kn =3D kernfs_find_ns(parent, name, ns); if (kn) __kernfs_remove(kn); =20 - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); =20 if (kn) return 0; @@ -1593,16 +1595,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 =3D NULL; + struct rw_semaphore *rwsem; int error; =20 /* can't move or rename root */ if (!kn->parent) return -EINVAL; =20 - root =3D kernfs_root(kn); - down_write(&root->kernfs_rwsem); + rwsem =3D kernfs_down_write(kn); =20 error =3D -ENOENT; if (!kernfs_active(kn) || !kernfs_active(new_parent) || @@ -1656,7 +1657,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct k= ernfs_node *new_parent, =20 error =3D 0; out: - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); return error; } =20 @@ -1727,14 +1728,13 @@ static int kernfs_fop_readdir(struct file *file, st= ruct dir_context *ctx) struct dentry *dentry =3D file->f_path.dentry; struct kernfs_node *parent =3D kernfs_dentry_node(dentry); struct kernfs_node *pos =3D file->private_data; - struct kernfs_root *root; const void *ns =3D NULL; + struct rw_semaphore *rwsem; =20 if (!dir_emit_dots(file, ctx)) return 0; =20 - root =3D kernfs_root(parent); - down_read(&root->kernfs_rwsem); + rwsem =3D kernfs_down_read(parent); =20 if (kernfs_ns_enabled(parent)) ns =3D kernfs_info(dentry->d_sb)->ns; @@ -1751,12 +1751,12 @@ static int kernfs_fop_readdir(struct file *file, st= ruct dir_context *ctx) file->private_data =3D pos; kernfs_get(pos); =20 - 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 =3D kernfs_down_read(parent); } - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); file->private_data =3D NULL; ctx->pos =3D INT_MAX; return 0; diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 0bffe5d0f510..03700388baa9 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -869,6 +869,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) struct kernfs_root *root; struct llist_node *free; struct kernfs_elem_attr *attr; + struct rw_semaphore *rwsem; repeat: /** * pop one off the notify_list. @@ -888,7 +889,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) kn =3D attribute_to_node(attr, struct kernfs_node, attr); root =3D kernfs_root(kn); /* kick fsnotify */ - down_write(&root->kernfs_rwsem); + rwsem =3D kernfs_down_write(kn); =20 down_write(&root->supers_rwsem); list_for_each_entry(info, &kernfs_root(kn)->supers, node) { @@ -928,7 +929,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) } up_write(&root->supers_rwsem); =20 - 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 3d783d80f5da..efe5ae98abf4 100644 --- a/fs/kernfs/inode.c +++ b/fs/kernfs/inode.c @@ -99,11 +99,11 @@ int __kernfs_setattr(struct kernfs_node *kn, const stru= ct iattr *iattr) int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) { int ret; - struct kernfs_root *root =3D kernfs_root(kn); + struct rw_semaphore *rwsem; =20 - down_write(&root->kernfs_rwsem); + rwsem =3D kernfs_down_write(kn); ret =3D __kernfs_setattr(kn, iattr); - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); return ret; } =20 @@ -112,14 +112,13 @@ int kernfs_iop_setattr(struct user_namespace *mnt_use= rns, struct dentry *dentry, { struct inode *inode =3D d_inode(dentry); struct kernfs_node *kn =3D inode->i_private; - struct kernfs_root *root; + struct rw_semaphore *rwsem; int error; =20 if (!kn) return -EINVAL; =20 - root =3D kernfs_root(kn); - down_write(&root->kernfs_rwsem); + rwsem =3D kernfs_down_write(kn); error =3D setattr_prepare(&init_user_ns, dentry, iattr); if (error) goto out; @@ -132,7 +131,7 @@ int kernfs_iop_setattr(struct user_namespace *mnt_usern= s, struct dentry *dentry, setattr_copy(&init_user_ns, inode, iattr); =20 out: - up_write(&root->kernfs_rwsem); + kernfs_up_write(rwsem); return error; } =20 @@ -187,14 +186,14 @@ int kernfs_iop_getattr(struct user_namespace *mnt_use= rns, { struct inode *inode =3D d_inode(path->dentry); struct kernfs_node *kn =3D inode->i_private; - struct kernfs_root *root =3D kernfs_root(kn); + struct rw_semaphore *rwsem; =20 - down_read(&root->kernfs_rwsem); + rwsem =3D 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); =20 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; =20 if (mask & MAY_NOT_BLOCK) return -ECHILD; =20 kn =3D inode->i_private; - root =3D kernfs_root(kn); =20 - down_read(&root->kernfs_rwsem); + rwsem =3D kernfs_down_read(kn); spin_lock(&inode->i_lock); kernfs_refresh_inode(kn, inode); ret =3D generic_permission(&init_user_ns, inode, mask); spin_unlock(&inode->i_lock); - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); =20 return ret; } diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index 82c6b16645bc..0c49cf57f80f 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -165,4 +165,82 @@ void kernfs_drain_open_files(struct kernfs_node *kn); */ extern const struct inode_operations kernfs_symlink_iops; =20 +static inline struct rw_semaphore *kernfs_rwsem_ptr(struct kernfs_node *kn) +{ + struct kernfs_root *root =3D 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 *k= n) +{ + struct rw_semaphore *rwsem =3D 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 =3D 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 1ac36b2a89ab..0e872824b7db 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -237,9 +237,9 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *k= n, static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_cont= ext *kfc) { struct kernfs_super_info *info =3D kernfs_info(sb); - struct kernfs_root *kf_root =3D kfc->root; struct inode *inode; struct dentry *root; + struct rw_semaphore *rwsem; =20 info->sb =3D sb; /* Userspace would break if executables or devices appear on sysfs */ @@ -257,9 +257,9 @@ static int kernfs_fill_super(struct super_block *sb, st= ruct kernfs_fs_context *k sb->s_shrink.seeks =3D 0; =20 /* get root inode, initialize and unlock it */ - down_read(&kf_root->kernfs_rwsem); + rwsem =3D kernfs_down_read(info->root->kn); inode =3D 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 0ab13824822f..9d4103602554 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 =3D inode->i_private; struct kernfs_node *parent =3D kn->parent; struct kernfs_node *target =3D kn->symlink.target_kn; - struct kernfs_root *root =3D kernfs_root(parent); + struct rw_semaphore *rwsem; int error; =20 - down_read(&root->kernfs_rwsem); + rwsem =3D kernfs_down_read(parent); error =3D kernfs_get_target_path(parent, target, path); - up_read(&root->kernfs_rwsem); + kernfs_up_read(rwsem); =20 return error; } --=20 2.30.2 From nobody Sun May 10 17:53:08 2026 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 A0FC4C433EF for ; Sun, 10 Apr 2022 02:38:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243486AbiDJCkl (ORCPT ); Sat, 9 Apr 2022 22:40:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243443AbiDJCkO (ORCPT ); Sat, 9 Apr 2022 22:40:14 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7E4643489 for ; Sat, 9 Apr 2022 19:37:58 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 23A0nRJY022836; Sun, 10 Apr 2022 02:37:53 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=G+zk6BTdPp3WcWupeg8y67GItBolaH7F3aaOuKbLi2U=; b=lPV0MasBJuxI6kSk8jZ+zCUgVl5bysqNEeLUS1t7FCnejSHLA3b6W9TuN4XkGru4L58/ d15ae7/vdM/3Nb4QKnoY9RiBOo4o1SsIt7lLlgXI7NDnTNBL+b/jySb026+dJo87dRiJ yqb+HRzK7f+FwwJa7aqGGBiNEqXDds4IUhrvAZhaRWzQHne/5gQUUeAW2rm1QDnDyT+k YkdbJOJ31PU3r75gylcX6u4ACAv+2+9uJwCi7SPJdfZ4CzCmrdxVx45eofjRUWX5Lsog CRrj6AAtLGk9wua46HDMU3xBvbATBtoYBkKfBlEGPnfIsuMjTbpKS5pjOtU+8D1Lcark OA== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com with ESMTP id 3fb0jd1111-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:53 +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 23A2UeME024150; Sun, 10 Apr 2022 02:37:53 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2104.outbound.protection.outlook.com [104.47.58.104]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fb0k0h5hs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:52 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EUcLCC7yzbt7pO8pgQc3xhC27PFiqgwMH7p29pCl3k3BSq3S4oOFCf4G64RMKVM9EjyDyxpldFE1136xDGc2turZZsPgcpub9oyFEwNjy0VuteWMeaaEglkCy9CUq8YLScX1Nlt2J1DLy5vISxbfnHPjidU//OWY9PWzFXadOxXNBwoQZa1fY6G/ayuBPo2dFSy0a66kp09YYCacD6CVtEOHNtqnkuPgrSLR9WUIOP/2QFk/6fxsMrhgR6UJEq4o7aoy0PGy6hkzDGQQem0G9Fv3KDH6EzKRftdH2p8VgO4BkSVZkpjdP/GgNiYS7ZC2WqD27e6j2MbG5CjaV6hMAg== 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=G+zk6BTdPp3WcWupeg8y67GItBolaH7F3aaOuKbLi2U=; b=cjpxkX509bI0xXphOL3EsuqrfsEq6N/6xWZCVkJkUHv8ZK3hQvWLsTJlm6NRDpEMbmeWQCQlCPyU/gvDMtWev91RV9HEspBlH/3geca4y1VPtZVdmo8O3gnEi/PoqT35BPx1aB011xlqUkyu8TDt7cf+w/6/MyVEYRgZwZjqnT6YlOK9EhtpX1oPYKUd/nNrQV/7A/9PzYSZ7xxOdaioI1JxaZKHUS79JFG62PYdHNxA/2b3A0qGHl/f3ZVEGyt3h8dsrBkYSe04IkSrZ+Ygw4/Wt2OScaW7qwwPe9K9i4NsQo17UrX6JIyRsMLWGJoeDYraR6LqH7MCe5t8+8Pfxw== 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=G+zk6BTdPp3WcWupeg8y67GItBolaH7F3aaOuKbLi2U=; b=jRMx4NQNDTvVf2nrNPILexhtMUj8pVYUAjSLlph1uVQVi/3MPfWyeNEGyALApaEEhheFzlL3gcPyBJQba8Gt7BFcVnXOVF//yC7cbp+SD4mxXAHlQSipPcFCnDnf7hs/FnVk2xIej2id214eVqV0cZgeXW3aRaGinArQiQSySEo= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by CY4PR10MB1238.namprd10.prod.outlook.com (2603:10b6:910:7::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5144.28; Sun, 10 Apr 2022 02:37:50 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa%9]) with mapi id 15.20.5144.028; Sun, 10 Apr 2022 02:37:50 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org, ebiederm@xmission.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH v8 09/10] kernfs: Replace per-fs rwsem with hashed rwsems. Date: Sun, 10 Apr 2022 12:37:18 +1000 Message-Id: <20220410023719.1752460-10-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220410023719.1752460-1-imran.f.khan@oracle.com> References: <20220410023719.1752460-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0054.ausprd01.prod.outlook.com (2603:10c6:10:2::18) 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: 2683126d-a28f-49f3-ffb5-08da1a9b1b09 X-MS-TrafficTypeDiagnostic: CY4PR10MB1238:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ojehbUo4/cpQAJy5VqNUV/j5F6EBVvNO+1ILgZLv5lhyUsmQ2kFH7I0UR1+eIWpYRB5YR4cetqlLs/ASKd6c5I0YdBPXNG88WSECcpYxOe7fTJGLydPZr8ijyX7jnY/yoMnwz1a2ubyqMC00qdhMwJ5lbteapgsGBeFRgssN/JLIoKScFgnmcpmSWgVBwazNFl/7ybMTmvwxCpyJcpMhqKTfj6ix8z/cdCKsACnKaBnxsp6Neo1oGKNC26OupJZS0qRls3bBqt6TEqOqKzkwqQs1+WeLdg5RCeZhvagfp2k7B/NzvgVRymOjG48h1nCYWkjzN3zWNQlHmX9nR5ABMopXzw0kesihbCkB075iLbL6BsiK1jUwHjEbjEIJl2A5lJi1jjncj1ynV+pvSnsfYYdn1/Bior/kNtiJ8KRC7KNdOJsZyns6SQIzU5FN0NXc6gJLperyzVjk0gYEXTXZZk1jWgPTTKlglW5dzU679q0W5mbNdrgUZhrSlvP84w+RNTbTjlNnYWGhrEpJrWPa9BRMxE9iFoAv9KI4P/DtXmzo7NTCk+Jgg+oTJkDklaca/1YUpw4rO7aiecjnNq38fPvoxQWCrVaoJBO48xNRqoLmDXRpbAXDhaB3JPIEKfrD1RVuOR3JF01MCdgbW3olb6rO7LiWOQnprXc5cqeh5u3PADDSOSEiNYLh2V3kxqS2Mbuoi1vO/61IdAF5JLvi2w== 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:(13230001)(366004)(1076003)(26005)(186003)(66946007)(66556008)(66476007)(8676002)(4326008)(83380400001)(36756003)(316002)(6486002)(103116003)(508600001)(8936002)(2616005)(30864003)(6666004)(5660300002)(38100700002)(38350700002)(86362001)(2906002)(6512007)(6506007)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?xUGNrMRg+cq6fAVy3zjm6zOg8oHj5y+XhQgwzMRFrDsvBa15NYO4O45rDhzw?= =?us-ascii?Q?49uauHbT7HcSw+6CXdQ0BfBYwvPCkgCdnGj0dt4rb4kx9t2cGFvfwlXBOFhu?= =?us-ascii?Q?Ip6JEQkTEeJX3cSFMhIcOOO9iNyXc6/p0pTT2x0P+bS7M4K9pTaBS/uE7IUK?= =?us-ascii?Q?cYGsK/hulB+VSNTMTPmN1ZwaVvxmBwTagOj1PQaXjBItZ4GLsz7VbuwHbc3Q?= =?us-ascii?Q?8kohYe71/m6tfVRTmOcGf0XX0IWjF/bkwVVOn0hBuFimpv3cL0pQhss7fRrb?= =?us-ascii?Q?Cz182BDBpWwSlrz37qsn3j9c7n9h057zRDmiYRz4+x5X44xNjEx28ZXeIYix?= =?us-ascii?Q?49QBAbLW6QbAoioU3HxK9VpQFurYxmr+p99GPX+ppaDfb9TGj3bC1iuWU363?= =?us-ascii?Q?X12KNzsBUlyTH4drz2YBlcC7Pju2XlX/J72xRuXAnHyGi4DJTnbHrCpPq+3g?= =?us-ascii?Q?LuR8AIA96baLatPAlY8pxxFAZOaTvSVwvyi0YNIs2HNlo1O3zXH/8bXJ1fSq?= =?us-ascii?Q?fHrtONSJBrl3O+r7GDgwp7Rozrvy89N/81nUNwAyEttoNxBSNptgLL1Q/Ssb?= =?us-ascii?Q?8K0g6LVOWfGWkDOcfwMSgorgzOn3GtrvIlbdT+jqYrGZAjq4+FIJ6Fi+A1I6?= =?us-ascii?Q?jeWld9PW5T9K5uGQryWAV1geozcgcFcuzkStcTreq7QfaL5iHMOWGalAYlm8?= =?us-ascii?Q?r3ftF7DZlFPUOm6ii1Bkf8OKD2X5q02enrVzjwCqe4HnNv9GmCgk1WGo1C3O?= =?us-ascii?Q?EQeOfSx10Tzv9VAGMRVaYOkL59Un5e0Cfe3YV86plro8ZrOjjojglNN2NoCx?= =?us-ascii?Q?rMjpLGnEkELzhEsFfWdwGyiaoJlhewb+86pKdQM7yb/2tyNb3pRosL/JVa8e?= =?us-ascii?Q?AP2av18v2oYfgfppsU/um4mhsbJZipKKNLlEjzne9HS3VxATkPtyWjaGQFTg?= =?us-ascii?Q?ijk24M4ozZy5ZGauQNwxGAeAMH0/1zSgDKoFquzKggL4VBvejmDYX64ehXTt?= =?us-ascii?Q?bP1wD8DVt8gGPzmFt8P8OQnB+TryystlZ+mHAVUAaraNZ7yCaaVIzTOBkwql?= =?us-ascii?Q?AP7tsDv3WO2A2SViOVFT/w9qynDnmc+jo3t0Hi2DFYTN7f8/L5fZSRC0KU+H?= =?us-ascii?Q?W8WD81Se10EPjgbxW1PPse+vg0dO3863eJcZo9t0KC/MAcR8GFqmJJJ0RbX8?= =?us-ascii?Q?G5MKk4ldzKAYEKQHV6ylZpiqXcwBwPl08P9SGuoHEvYjCHyOCdQm3QRVCMJr?= =?us-ascii?Q?X4QS+wyrymbW8EdpxWaJOQot2NIlXfSj65NSlkGVnkQfnWSE0x2qkF6aeTn4?= =?us-ascii?Q?OwOgQwmQ1OstSZ8UOwNEJHFDHmxGx/85cgc6GDMcpmJVs7dddMzSDGHarwmq?= =?us-ascii?Q?XsbB2MpS46w0bxmROjj3r5LZcPGySYVEtyWi1zN8ssjlLZdFYRY/FX0Nevpd?= =?us-ascii?Q?c7WzCzBNr4Lr01zRAnppkmjuzWyrk+sZCNHGyZgAaE+/3gv3Qb18JOFWyQ8o?= =?us-ascii?Q?in4SMW93WGLQeKwz+VxG7onmqk4An/NYtNZjGD1SCfcTt/EZdwPPUZjmGkBr?= =?us-ascii?Q?LjyVVtgXGdF/dwaeTV9G8gsc9w0VT+FOLeA533eOvxkYbGq9fakirw3Z2s53?= =?us-ascii?Q?7AXCAEMyQ2mi1itjoolNBe7hTjuFKTY2mqo5ioEgi6iPQbc4Wc9Ku4DaPjpv?= =?us-ascii?Q?Yh2NaWSHx8v+pKzOGHyyVokEtdGfUMoS3cy5pvxviF4tGi+wtpVjfS3ud/JH?= =?us-ascii?Q?DCNZ1DbR7DvosLO8VGZxJVsLQU+kWHQ=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2683126d-a28f-49f3-ffb5-08da1a9b1b09 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Apr 2022 02:37:50.4113 (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: eeCtssZct+jIwtPFFcEOgNmiJXETzQfVI2c4Ncrn1aetX7QJZei3u94y9gYCZ6JloHRtS7n+fFNdputzHC94nA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR10MB1238 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.858 definitions=2022-04-09_25:2022-04-08,2022-04-09 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 malwarescore=0 adultscore=0 phishscore=0 suspectscore=0 mlxlogscore=999 bulkscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204100014 X-Proofpoint-ORIG-GUID: 1lyq_sd3uaPfGKmsbRp4NPlq42AxOt8o X-Proofpoint-GUID: 1lyq_sd3uaPfGKmsbRp4NPlq42AxOt8o Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 that can be used to synchronize these operations against parallel removal of involved node. 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 =3D 0; loop <100 ; loop++) { for (int port_num =3D 1; port_num < 2; port_num++) { for (int gid_index =3D 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 =3D snprintf(gid_file_path, sizeof(gid_file_path), "/sys/class/infiniband/%s/ports/%d/gids/%d", dev_name, port_num, gid_index); ret_fd =3D 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 =3D read(ret_fd, ret_buf, 40); if (ret_rd =3D=3D -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 | 171 +++++++++++++++++++----- fs/kernfs/inode.c | 20 +++ fs/kernfs/kernfs-internal.c | 259 ++++++++++++++++++++++++++++++++++++ fs/kernfs/kernfs-internal.h | 52 +++++++- fs/kernfs/mount.c | 4 +- fs/kernfs/symlink.c | 11 +- include/linux/kernfs.h | 1 + 8 files changed, 478 insertions(+), 42 deletions(-) create mode 100644 fs/kernfs/kernfs-internal.c diff --git a/fs/kernfs/Makefile b/fs/kernfs/Makefile index 4ca54ff54c98..778da6b118e9 100644 --- a/fs/kernfs/Makefile +++ b/fs/kernfs/Makefile @@ -3,4 +3,4 @@ # Makefile for the kernfs pseudo filesystem # =20 -obj-y :=3D mount.o inode.o dir.o file.o symlink.o +obj-y :=3D 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 f8520d842b39..bdc355143735 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -17,7 +17,7 @@ =20 #include "kernfs-internal.h" =20 -static DEFINE_RWLOCK(kernfs_rename_lock); /* kn->parent and ->name */ +DEFINE_RWLOCK(kernfs_rename_lock); /* kn->parent and ->name */ static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by rename_lock */ static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr */ =20 @@ -25,7 +25,6 @@ static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr= */ =20 static bool kernfs_active(struct kernfs_node *kn) { - kernfs_rwsem_assert_held(kn); return atomic_read(&kn->active) >=3D 0; } =20 @@ -450,26 +449,24 @@ 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 kernfs_root *root =3D kernfs_root(kn); =20 - /** - * kn has the same root as its ancestor, so it can be used to get - * per-fs rwsem. - */ - struct rw_semaphore *rwsem =3D kernfs_rwsem_ptr(kn); + struct rw_semaphore *rwsem; =20 - kernfs_rwsem_assert_held_write(kn); + kernfs_rwsem_assert_held_write(anc); WARN_ON_ONCE(kernfs_active(kn)); =20 + rwsem =3D kernfs_rwsem_ptr(anc); kernfs_up_write(rwsem); =20 if (kernfs_lockdep(kn)) { @@ -489,7 +486,7 @@ static void kernfs_drain(struct kernfs_node *kn) =20 kernfs_drain_open_files(kn); =20 - kernfs_down_write(kn); + kernfs_down_write(anc); } =20 /** @@ -729,6 +726,11 @@ int kernfs_add_one(struct kernfs_node *kn) bool has_ns; int ret; =20 + /** + * 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 =3D kernfs_down_write(parent); =20 ret =3D -EINVAL; @@ -826,28 +828,39 @@ static struct kernfs_node *kernfs_walk_ns(struct kern= fs_node *parent, { size_t len; char *p, *name; + struct rw_semaphore *rwsem; =20 kernfs_rwsem_assert_held_read(parent); =20 - /* grab kernfs_rename_lock to piggy back on kernfs_pr_cont_buf */ - write_lock_irq(&kernfs_rename_lock); + p =3D kzalloc(PATH_MAX, GFP_KERNEL); + if (!p) + return NULL; =20 - len =3D strlcpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf)); + /* Caller has kernfs_rm_mutex so topology will not change */ + len =3D strlcpy(p, path, PATH_MAX); =20 - if (len >=3D sizeof(kernfs_pr_cont_buf)) { - write_unlock_irq(&kernfs_rename_lock); + if (len >=3D PATH_MAX) { + kfree(p); return NULL; } =20 - p =3D kernfs_pr_cont_buf; + rwsem =3D kernfs_rwsem_ptr(parent); =20 while ((name =3D strsep(&p, "/")) && parent) { if (*name =3D=3D '\0') continue; + parent =3D 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 =3D kernfs_down_read(parent); } =20 - write_unlock_irq(&kernfs_rename_lock); + kfree(p); =20 return parent; } @@ -867,11 +880,20 @@ struct kernfs_node *kernfs_find_and_get_ns(struct ker= nfs_node *parent, { struct kernfs_node *kn; struct rw_semaphore *rwsem; + struct kernfs_root *root =3D kernfs_root(parent); =20 + /** + * We don't have address of kernfs_node (that is being searched) + * yet. Acquiring root->kernfs_rm_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_rm_mutex); rwsem =3D kernfs_down_read(parent); kn =3D kernfs_find_ns(parent, name, ns); kernfs_get(kn); kernfs_up_read(rwsem); + mutex_unlock(&root->kernfs_rm_mutex); =20 return kn; } @@ -892,11 +914,26 @@ struct kernfs_node *kernfs_walk_and_get_ns(struct ker= nfs_node *parent, { struct kernfs_node *kn; struct rw_semaphore *rwsem; + struct kernfs_root *root =3D kernfs_root(parent); =20 + /** + * We don't have address of kernfs_node (that is being searched) + * yet. Acquiring root->kernfs_rm_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_rm_mutex); rwsem =3D kernfs_down_read(parent); kn =3D 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_rm_mutex); =20 return kn; } @@ -921,9 +958,9 @@ struct kernfs_root *kernfs_create_root(struct kernfs_sy= scall_ops *scops, return ERR_PTR(-ENOMEM); =20 idr_init(&root->ino_idr); - init_rwsem(&root->kernfs_rwsem); INIT_LIST_HEAD(&root->supers); init_rwsem(&root->supers_rwsem); + mutex_init(&root->kernfs_rm_mutex); =20 /* * On 64bit ino setups, id is ino. On 32bit, low 32bits are ino. @@ -1093,6 +1130,11 @@ static int kernfs_dop_revalidate(struct dentry *dent= ry, unsigned int flags) } =20 kn =3D 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 =3D kernfs_down_read(kn); =20 /* The kernfs node has been deactivated */ @@ -1132,24 +1174,35 @@ static struct dentry *kernfs_iop_lookup(struct inod= e *dir, struct inode *inode =3D NULL; const void *ns =3D NULL; struct rw_semaphore *rwsem; + struct kernfs_root *root =3D kernfs_root(parent); =20 + /** + * We don't have address of kernfs_node (that is being searched) + * yet. So take root->kernfs_rm_mutex to avoid parallel removal of + * found kernfs_node. + */ + mutex_lock(&root->kernfs_rm_mutex); rwsem =3D kernfs_down_read(parent); if (kernfs_ns_enabled(parent)) ns =3D kernfs_info(dir->i_sb)->ns; =20 kn =3D 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 =3D kernfs_down_read(kn); if (!kernfs_active(kn)) { kernfs_up_read(rwsem); + mutex_unlock(&root->kernfs_rm_mutex); return NULL; } inode =3D kernfs_get_inode(dir->i_sb, kn); if (!inode) inode =3D ERR_PTR(-ENOMEM); + kernfs_up_read(rwsem); } /* * Needed for negative dentry validation. @@ -1157,9 +1210,11 @@ static struct dentry *kernfs_iop_lookup(struct inode= *dir, * or transforms from positive dentry in dentry_unlink_inode() * called from vfs_rmdir(). */ + rwsem =3D kernfs_down_read(parent); if (!IS_ERR(inode)) kernfs_set_rev(parent, dentry); kernfs_up_read(rwsem); + mutex_unlock(&root->kernfs_rm_mutex); =20 /* instantiate and hash (possibly negative) dentry */ return d_splice_alias(inode, dentry); @@ -1339,27 +1394,40 @@ 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; + + if (!kn) + return; =20 - kernfs_rwsem_assert_held_write(kn); + root =3D kernfs_root(kn); =20 /* * Short-circuit if non-root @kn has already finished removal. * This is for kernfs_remove_self() which plays with active ref * after removal. */ - if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb))) + mutex_lock(&root->kernfs_rm_mutex); + rwsem =3D kernfs_down_write(kn); + if (kn->parent && RB_EMPTY_NODE(&kn->rb)) { + kernfs_up_write(rwsem); + mutex_unlock(&root->kernfs_rm_mutex); return; + } =20 pr_debug("kernfs %s: removing\n", kn->name); =20 /* prevent any new usage under @kn by deactivating all nodes */ pos =3D NULL; + while ((pos =3D kernfs_next_descendant_post(pos, kn))) if (kernfs_active(pos)) atomic_add(KN_DEACTIVATED_BIAS, &pos->active); + kernfs_up_write(rwsem); =20 /* deactivate and unlink the subtree node-by-node */ do { + rwsem =3D kernfs_down_write(kn); pos =3D kernfs_leftmost_descendant(kn); =20 /* @@ -1377,10 +1445,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) !=3D KN_DEACTIVATED_BIAS); =20 + 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_rm_mutex ensures that we will not have + * concurrent instances of __kernfs_remove + */ + if (pos->parent) + rwsem =3D kernfs_down_write(pos->parent); + /* * kernfs_unlink_sibling() succeeds once per node. Use it * to decide who's responsible for cleanups. @@ -1398,8 +1481,12 @@ static void __kernfs_remove(struct kernfs_node *kn) kernfs_put(pos); } =20 + if (pos->parent) + kernfs_up_write(rwsem); kernfs_put(pos); } while (pos !=3D kn); + + mutex_unlock(&root->kernfs_rm_mutex); } =20 /** @@ -1410,11 +1497,7 @@ static void __kernfs_remove(struct kernfs_node *kn) */ void kernfs_remove(struct kernfs_node *kn) { - struct rw_semaphore *rwsem; - - rwsem =3D kernfs_down_write(kn); __kernfs_remove(kn); - kernfs_up_write(rwsem); } =20 /** @@ -1516,9 +1599,11 @@ bool kernfs_remove_self(struct kernfs_node *kn) */ if (!(kn->flags & KERNFS_SUICIDAL)) { kn->flags |=3D KERNFS_SUICIDAL; + kernfs_up_write(rwsem); __kernfs_remove(kn); kn->flags |=3D KERNFS_SUICIDED; ret =3D true; + rwsem =3D kernfs_down_write(kn); } else { wait_queue_head_t *waitq =3D &kernfs_root(kn)->deactivate_waitq; DEFINE_WAIT(wait); @@ -1572,11 +1657,17 @@ int kernfs_remove_by_name_ns(struct kernfs_node *pa= rent, const char *name, =20 rwsem =3D kernfs_down_write(parent); =20 + /** + * Since the node being searched will be removed eventually, + * we don't need to take root->kernfs_rm_mutex. + * Even if a parallel remove succeeds, the subsequent __kernfs_remove + * will detect it and bail-out early. + */ kn =3D kernfs_find_ns(parent, name, ns); - if (kn) - __kernfs_remove(kn); =20 kernfs_up_write(rwsem); + if (kn) + __kernfs_remove(kn); =20 if (kn) return 0; @@ -1596,14 +1687,26 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct= kernfs_node *new_parent, { struct kernfs_node *old_parent; const char *old_name =3D NULL; - struct rw_semaphore *rwsem; + struct kernfs_rwsem_token token; int error; + struct kernfs_root *root =3D kernfs_root(kn); =20 /* can't move or rename root */ if (!kn->parent) return -EINVAL; =20 - rwsem =3D kernfs_down_write(kn); + mutex_lock(&root->kernfs_rm_mutex); + old_parent =3D kn->parent; + kernfs_get(old_parent); + kernfs_down_write_triple_nodes(kn, old_parent, new_parent, &token); + while (old_parent !=3D kn->parent) { + kernfs_put(old_parent); + kernfs_up_write_triple_nodes(kn, old_parent, new_parent, &token); + old_parent =3D kn->parent; + kernfs_get(old_parent); + kernfs_down_write_triple_nodes(kn, old_parent, new_parent, &token); + } + kernfs_put(old_parent); =20 error =3D -ENOENT; if (!kernfs_active(kn) || !kernfs_active(new_parent) || @@ -1638,7 +1741,6 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct k= ernfs_node *new_parent, /* rename_lock protects ->parent and ->name accessors */ write_lock_irq(&kernfs_rename_lock); =20 - old_parent =3D kn->parent; kn->parent =3D new_parent; =20 kn->ns =3D new_ns; @@ -1657,7 +1759,8 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct k= ernfs_node *new_parent, =20 error =3D 0; out: - kernfs_up_write(rwsem); + mutex_unlock(&root->kernfs_rm_mutex); + kernfs_up_write_triple_nodes(kn, new_parent, old_parent, &token); return error; } =20 diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c index efe5ae98abf4..36a40b08b97f 100644 --- a/fs/kernfs/inode.c +++ b/fs/kernfs/inode.c @@ -101,6 +101,12 @@ int kernfs_setattr(struct kernfs_node *kn, const struc= t iattr *iattr) int ret; struct rw_semaphore *rwsem; =20 + /** + * 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 =3D kernfs_down_write(kn); ret =3D __kernfs_setattr(kn, iattr); kernfs_up_write(rwsem); @@ -118,6 +124,12 @@ int kernfs_iop_setattr(struct user_namespace *mnt_user= ns, struct dentry *dentry, if (!kn) return -EINVAL; =20 + /** + * 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 =3D kernfs_down_write(kn); error =3D setattr_prepare(&init_user_ns, dentry, iattr); if (error) @@ -188,6 +200,10 @@ int kernfs_iop_getattr(struct user_namespace *mnt_user= ns, struct kernfs_node *kn =3D inode->i_private; struct rw_semaphore *rwsem; =20 + /** + * As we are only reading ->iattr, acquiring kn's rwsem for + * reading is enough. + */ rwsem =3D 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_u= serns, =20 kn =3D inode->i_private; =20 + /** + * As we are only reading ->iattr, acquiring kn's rwsem for + * reading is enough. + */ rwsem =3D 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 000000000000..80d7d64532fe --- /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 =3D array[i]; + array[i] =3D array[j]; + array[j] =3D tmp; +} + +static void kernfs_sort_rwsems(struct kernfs_rwsem_token *token) +{ + struct rw_semaphore **array =3D &token->rwsems[0]; + + if (token->count =3D=3D 2) { + if (array[0] =3D=3D array[1]) + token->count =3D 1; + else if (array[0] > array[1]) + kernfs_swap_rwsems(array, 0, 1); + } else { + if (array[0] =3D=3D array[1] && array[0] =3D=3D array[2]) + token->count =3D 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] =3D=3D array[1] || array[1] =3D=3D array[2]) + token->count =3D 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 =3D &token->rwsems[0]; + + array[0] =3D kernfs_rwsem_ptr(kn1); + array[1] =3D kernfs_rwsem_ptr(kn2); + token->count =3D 2; + + kernfs_sort_rwsems(token); + + if (token->count =3D=3D 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 =3D &token->rwsems[0]; + + if (token->count =3D=3D 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 =3D &token->rwsems[0]; + + array[0] =3D kernfs_rwsem_ptr(kn1); + array[1] =3D kernfs_rwsem_ptr(kn2); + token->count =3D 2; + + kernfs_sort_rwsems(token); + + if (token->count =3D=3D 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 =3D &token->rwsems[0]; + + if (token->count =3D=3D 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 =3D &token->rwsems[0]; + + array[0] =3D kernfs_rwsem_ptr(kn1); + array[1] =3D kernfs_rwsem_ptr(kn2); + array[2] =3D kernfs_rwsem_ptr(kn3); + token->count =3D 3; + + kernfs_sort_rwsems(token); + + if (token->count =3D=3D 1) { + /* All 3 nodes hash to same rwsem */ + down_write_nested(array[0], 0); + } else if (token->count =3D=3D 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 =3D &token->rwsems[0]; + + if (token->count =3D=3D 1) { + /* All 3 nodes hash to same rwsem */ + up_write(array[0]); + } else if (token->count =3D=3D 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 0c49cf57f80f..4e630abe4a18 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -19,6 +19,20 @@ #include #include =20 +/** + * 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; =20 wait_queue_head_t deactivate_waitq; - struct rw_semaphore kernfs_rwsem; struct rw_semaphore supers_rwsem; + struct mutex kernfs_rm_mutex; }; =20 /* +1 to avoid triggering overflow warning when negating it */ @@ -165,11 +179,17 @@ void kernfs_drain_open_files(struct kernfs_node *kn); */ extern const struct inode_operations kernfs_symlink_iops; =20 +/* + * 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 =3D kernfs_root(kn); + int idx =3D hash_ptr(kn, NR_KERNFS_LOCK_BITS); =20 - return &root->kernfs_rwsem; + return &kernfs_locks->kernfs_rwsem[idx]; } =20 static inline void kernfs_rwsem_assert_held(struct kernfs_node *kn) @@ -243,4 +263,30 @@ static inline void kernfs_up_read(struct rw_semaphore = *rwsem) up_read(rwsem); } =20 + +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 0e872824b7db..5efa0f4ca209 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -395,8 +395,10 @@ void __init kernfs_lock_init(void) kernfs_locks =3D kmalloc(sizeof(struct kernfs_global_locks), GFP_KERNEL); WARN_ON(!kernfs_locks); =20 - for (count =3D 0; count < NR_KERNFS_LOCKS; count++) + for (count =3D 0; count < NR_KERNFS_LOCKS; count++) { mutex_init(&kernfs_locks->open_file_mutex[count].lock); + init_rwsem(&kernfs_locks->kernfs_rwsem[count]); + } } =20 void __init kernfs_init(void) diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c index 9d4103602554..5e404ea455bd 100644 --- a/fs/kernfs/symlink.c +++ b/fs/kernfs/symlink.c @@ -110,15 +110,20 @@ static int kernfs_get_target_path(struct kernfs_node = *parent, =20 static int kernfs_getlink(struct inode *inode, char *path) { + unsigned long flags; struct kernfs_node *kn =3D inode->i_private; struct kernfs_node *parent =3D kn->parent; struct kernfs_node *target =3D kn->symlink.target_kn; - struct rw_semaphore *rwsem; int error; =20 - rwsem =3D kernfs_down_read(parent); + /** + * 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 =3D kernfs_get_target_path(parent, target, path); - kernfs_up_read(rwsem); + read_unlock_irqrestore(&kernfs_rename_lock, flags); =20 return error; } diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index cc514bda0ae7..19506bdb6d2b 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -87,6 +87,7 @@ struct kernfs_open_file_mutex { */ struct kernfs_global_locks { struct kernfs_open_file_mutex open_file_mutex[NR_KERNFS_LOCKS]; + struct rw_semaphore kernfs_rwsem[NR_KERNFS_LOCKS]; }; =20 enum kernfs_node_type { --=20 2.30.2 From nobody Sun May 10 17:53:08 2026 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 2080AC433F5 for ; Sun, 10 Apr 2022 02:38:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243403AbiDJCk4 (ORCPT ); Sat, 9 Apr 2022 22:40:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243419AbiDJCkR (ORCPT ); Sat, 9 Apr 2022 22:40:17 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D443673CA for ; Sat, 9 Apr 2022 19:38:05 -0700 (PDT) Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 239Dvx65028018; Sun, 10 Apr 2022 02:37:56 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=Gfxrg0N7X+2jUeRRxfKklMIvmi4/26qPXTKfyUbtT18=; b=bhBSy0dh5OuGyicEPTIHRxbRSRpQGGddwxL4TL2qylO8h1a4Ay1dHxJGxlgw3HuT72Nk 6IcrR0uBiraPCkYOc4hFvuPHJM6KQifJfa/NywUb+7QSpBC3P1WPvEQjeRtEsH5I6nS2 CExnIh7KCf70nmhoLnOkGZ3NZ3doe86J1F2kcDZVI6RkOJmWTbuh+pnxlSKFN1l4ddyr 6zL36qGXTgY7jCsEjtSmZQPUlUBveDQL2pdd3wFpOZNdx5gfIn62vvWFeDjOja1bcisA aK8GO9MxMDxe/JiJfuVCmCG12Ai5ETCLGvGsTQfYjlO7bpOcHgHoDzsUts4BMiCKK1ck OQ== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com with ESMTP id 3fb219rymu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:55 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 23A2Vt8r034106; Sun, 10 Apr 2022 02:37:54 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2108.outbound.protection.outlook.com [104.47.58.108]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3fb0k13bhw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Apr 2022 02:37:54 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ADs5QKVKqVshHBhigr/4H/Je1mkxDU73xnVVL0OpsKQPIFsASZZXaKHUvvwPs0f7+lDGUi3nQM9+aRvNk2myBmKF+FFLcJZOiZ+KqXovxzfcM9LFN90kPdxlSQh20GfKMEyK65pGLHg9AZtm1+9Q5Aiz63dh/DJrT0yYQ52TGYaMCbLwstjre3w7uvVujl0WayUvmq4RU4xLtan+LBfJ19etUVKSwDKpFH0nGqDtPNMVQZ1NbjWfjQspr9vLHUGE2APMfGg0rL3onCkvLHMgOh7zMOkxXhoPgaXyhqwZdUw5hhb+nm6xtqmiZDJYu9dBbp9hbmgDcz+L3I7x0PMkDQ== 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=Gfxrg0N7X+2jUeRRxfKklMIvmi4/26qPXTKfyUbtT18=; b=Oc6MzopVltkiZvI6r9Y//KUjFoKq6CSqFNjicjrglq6VuGz8oSQgNIJWjK9o2lZHDr4Bnwqp41CvQ8PhhxZnQdtInZ93bd3TF38M4wGid/tNtJIIKCx4wWQimmuNVQ+9r47N38B39yK6kTdOoYti2vBEoO0H8roOV2lY/K87yOiw4eaSrhmp0atxwUMFB/i0GMoHEe+ahF8YEKewpj6P6NEU+eo4VcCGIVgRmvoH27eGOttF2RAZhM40QkbZfbB3CobcmuyUcb8YAcj8qzcVsM8HeqgdOQfZ5OvDDP5kM6v/bWv1zT/Z4O2VyNZi4d7qB8Vs01n0c5uCK79qBewg4w== 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=Gfxrg0N7X+2jUeRRxfKklMIvmi4/26qPXTKfyUbtT18=; b=ejpP6C8hANNMtwk7SAJ7/qhbyfzkqXzIxqzIY1+O4AXo1QuQundwsYjB0ZE4wlR2t0LHeIot2F9fh1d+9z4ItNtSmUofwyXR3qLV1uerhW8mzYcE+zadmT/Bx4ozNMC6WjBg1gModG0jdKbVifz2b+LHv7MfEt0vKUF5OiixgOg= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by CY4PR10MB1238.namprd10.prod.outlook.com (2603:10b6:910:7::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5144.28; Sun, 10 Apr 2022 02:37:52 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::b5ab:1c3e:6540:d2fa%9]) with mapi id 15.20.5144.028; Sun, 10 Apr 2022 02:37:52 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org, ebiederm@xmission.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH v8 10/10] kernfs: Add a document to describe hashed locks used in kernfs. Date: Sun, 10 Apr 2022 12:37:19 +1000 Message-Id: <20220410023719.1752460-11-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220410023719.1752460-1-imran.f.khan@oracle.com> References: <20220410023719.1752460-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0054.ausprd01.prod.outlook.com (2603:10c6:10:2::18) 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: 6e7e2195-7fc4-4841-4432-08da1a9b1c42 X-MS-TrafficTypeDiagnostic: CY4PR10MB1238:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KCUSQjfjzBzaha12oQzt4M3uQL6uPOS/vpzTx1jz+RgK7XP2IdpqnaUPYPnABSq1RulnJfL6ATxXNnl7Q+fQRGHrHtpBqDsMk04vh3WCPupWHLrZs+Gcj8P0d7TsPfyaoFoTISdRD+sDRl9iPF8Ueij98vd8kkD38sPq7vVkBxi7AhOX0L6HZoDNyDpPpxtTlw2acGP1nr1UxzzXk02gSwXwq7IDzzwZ5/PSvTsojWGkxuZXUoQkVR8XRpeTw5fshXJTAnk7AmBHi7FBhrZyrArlb2RwMSMWA25sUYCO3615HZ4FjSf+pLvMuqk2znjB4Xjb+kCPjYQBoHi6k/W2j22lWvZwvtNCZCJpS5QAT0ubICJuED1IZc/HRKdGn/l39iWGd253SULEdhh9lGd8VzUU730rW2jTdCS6bG45bQnhVcXkc2FRYLNzWa4DM3oOPY8VTQ3S2e4j8dIJGwfmY5X5PHrMLg9pzzrfdc69Wm+xSn8HvUaGt+naX45trv62b5AK74zbB/qeYiljF0VUB4ZdaudYsO8jFrUb2lGwbd4eD3vzXawZOI3Aviia/rrlgcMtuAQvPEyfI7H/mM/RrEF2xZb0iZjCuAntYKQmm8ZJ5s9ykUoZDdyL60N7uWjkFO5o+bly4NOqHOtyw12ZPmFfhoM1Xe0fh3hKagNbXU+v/MJc1kl/in0qPYQeiKp0fyr2AqiKlXY4DKqIu6lQWA== 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:(13230001)(366004)(1076003)(26005)(186003)(66946007)(66556008)(66476007)(8676002)(4326008)(83380400001)(36756003)(316002)(6486002)(103116003)(508600001)(8936002)(2616005)(6666004)(5660300002)(38100700002)(38350700002)(86362001)(2906002)(6512007)(6506007)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?YZD04+x+Xm3m+8VGahhLv6P61AlS88cCGYjWDiQCd5CoInjhdJ4372GwNF/N?= =?us-ascii?Q?PTa+yJ3h/8hrdaEQct67riRoAhgsU9qVD2v5IH73w3YzsmIGJPdxvXA4Hd16?= =?us-ascii?Q?pf1vTx+wyfLmWkBN8IFy2eti2KBpQxM+7d3X/Uy7rrYr+LBSPj9f6K4kGocN?= =?us-ascii?Q?LOdnPupOndjkEvz3K2RFSWP7MEAb6VW8649hevptb2JyZ5CmTwwKJ0JabkA8?= =?us-ascii?Q?L0ipU5s/6Xxo3uied2zCFNxJ5Hy6c0vyAW2Nk78sZt+Ngq6ZF7sY2GucjPQ/?= =?us-ascii?Q?RFui4Ec3xiPFwYVu5h299gWXh4XNqEzdOurzBCEnJBg5kenf3i0sQjh66jL0?= =?us-ascii?Q?ALYObpK9JeMmpWHMwt8xA5Nks2VzBna+dR9lPJ1B/zgzPNBkjSdaSU9QrYfD?= =?us-ascii?Q?IgDAm3woMJW9duIT8ilmbtEFRX+knbFcJQ5MuhIds7O3lO40fUvpceYOao7A?= =?us-ascii?Q?/McYeNQW+UdeYM7Nu4iz8k22cHyRJQsi2tpg2PIEInDy2299GFktPD0Ej6/d?= =?us-ascii?Q?+3SupozWCJSSreeswhjG8v+CdW9D7PYbSR8Iqw4/1ss0c/nneRe30/0blD3C?= =?us-ascii?Q?5yCa5iPQ+QsUiTNS910Ca0FhsokDhzKBQFUGlsTy7wGwvTDkhVkUuUWJixlJ?= =?us-ascii?Q?LLb89ZP1r0DjibJ89L0SUDUzVHyR0Neemm3R4rC+oPHAWFsKsEkmipgFwtqL?= =?us-ascii?Q?JuoU+5lZMr8RQBsrDH2K6OnT5zllSpsoIDE0lrrRo/G83yXQY9j4B6uBUUei?= =?us-ascii?Q?GhNHSTFFgdNK6YRoo2n5yrn5hEejSXrILCqba+Y+hNe+CTdxWhPxlDS8+pZS?= =?us-ascii?Q?7FJTAJJsn4ULSv3h5v5GQDYljo87xppX+d2q8xGb2OPaO36HHues//h0ShUX?= =?us-ascii?Q?6B8B077QppVJe7zmsmjYRr72aYpLPyRhiAaYjK91zVlxJzyURRVEvZ5/3/Ei?= =?us-ascii?Q?rJydhgDud5w27rc0gDB6W8A6LRgsS20PbN64WdrYD045Yg3RspkNo8dPqJSU?= =?us-ascii?Q?eMMYD2BL/o++g5Y9pDwYYLs3wrJTOlWGnhJ3cu53iIj1DtAvS7YqOMf8uaxV?= =?us-ascii?Q?wh8LSBlrT6oWnzu1PnSwzfA+30NCgfWslBvK3jfk2Fal1MTlkSiBsb1Q9Fmk?= =?us-ascii?Q?dvpKoQ6NVfELA/SHOS3SB7dYloGvO0EwF5SZMmpPnTjZXxT47dgEVPRbowEC?= =?us-ascii?Q?1UvTOEinLpnuvk2/chTc0yk6EdfxpcxptoAzDSlKp/oXDrQvPEH4vS0hspCL?= =?us-ascii?Q?AHJo8vECZwyuSqlSuP0C+TWxAGok1BwvtK0u3bXg+rKR7lZgFiuKdpujOpIx?= =?us-ascii?Q?mAF23M9JK9d8HGOmu2GzQErpTaF1NETkIdHbmH9VsEdronUzbMQZCV15Ofj1?= =?us-ascii?Q?kG6PiXgwMqWU3j2C+cMu7BwnekVT+ipAUkjVyv21l5BZi5EnoTp7ijkWuH3q?= =?us-ascii?Q?BFuVscQom+2b0BLgGMf3KqufEaWMeImbG68MJRExnJ8S7LlGR72SpPYBZoUU?= =?us-ascii?Q?/j/z8EQaiHGX0kHLb7KOXpz89AVgNzvpBGn0ozaOhVQNZYFzXYGI8Ygu5oj3?= =?us-ascii?Q?d9R0AmW+Y3zQxCOlZTWNWlZLp2Hqd6a6mHszgdUQnVosTkXIqKR3bwIXfgxo?= =?us-ascii?Q?uRPDJaSK5QmHjIAF3E1HiOVkD7Iv+psiwcAyih6t4YQJEjY8GK6yk3vkCX3K?= =?us-ascii?Q?CyGVa+wcCYoeH4ixAig++qs5DzohjqNTstOh02cBzHhbehKi1jSYNBSqFjED?= =?us-ascii?Q?a0R8Nhvh+GOO8VFnWBOtRO7mWfMZTzI=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6e7e2195-7fc4-4841-4432-08da1a9b1c42 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Apr 2022 02:37:52.5372 (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: m7hyaKBs9eaKSiTPE93OZhS2DZRQY1R1RSEyRBiqXo/pSWqU9kVS8dJOG6EOFJwEB4FS8GSs4DxxqX1bEGywCA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR10MB1238 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.858 definitions=2022-04-09_25:2022-04-08,2022-04-09 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 bulkscore=0 adultscore=0 suspectscore=0 spamscore=0 mlxlogscore=999 phishscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204100014 X-Proofpoint-GUID: ixcy4LGxI4QGT3EtmVwzPmRwvvhLqa1p X-Proofpoint-ORIG-GUID: ixcy4LGxI4QGT3EtmVwzPmRwvvhLqa1p Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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/Documentat= ion/filesystems/kernfs-hashed-locks.rst new file mode 100644 index 000000000000..8c3542e38e04 --- /dev/null +++ b/Documentation/filesystems/kernfs-hashed-locks.rst @@ -0,0 +1,214 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +kernfs hashed locks +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +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 m= utex +(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 kernfs_open_file_mutex open_file_mutex[NR_KERNFS_LOCKS]; + struct rw_semaphore kernfs_rwsem[NR_KERNFS_LOCKS]; + }; + +The hashed mutexes is encapsulated in kernfs_open_file_mutex as shown belo= w: + +struct kernfs_open_file_mutex { + struct mutex lock; +} ____cacheline_aligned_in_smp; + +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 syst= em. +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 +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D + +(1) There's one kernfs_open_file for each open file and all kernfs_open_fi= le + instances corresponding to a kernfs_node are maintained in a list. + hashed mutexes or kernfs_global_locks.open_file_mutex[index].lock prot= ects + 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 depen= d on + the state of a node. + +(3) per-fs mutex (mentioned earlier) provides synchronization between look= up + and remove operations. + 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 ac= quire + corresponding rwsem and go ahead with node removal. So it may happen t= hat + 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, r= emoves + the node and hence makes its any future use wrong. + per-fs mutex ensures that for competing search and remove operations o= nly + one proceeds at a time and since object returned by search is pinned b= efore + releasing the per-fs mutex, it will be available for subsequent usage. + + +Lock usage and proof +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +(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_n= ode. + Also when a kernfs_node is removed, all of its kernfs_open_file instan= ces + 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. --=20 2.30.2