From nobody Sun May 10 17:53:02 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 6CB58C433F5 for ; Thu, 28 Apr 2022 05:55:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243290AbiD1F6S (ORCPT ); Thu, 28 Apr 2022 01:58:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243261AbiD1F6I (ORCPT ); Thu, 28 Apr 2022 01:58:08 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D25B67C270 for ; Wed, 27 Apr 2022 22:54:52 -0700 (PDT) Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 23RMFeqt032115; Thu, 28 Apr 2022 05:54: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=/CmSLrqClYIE087SRXM6wfjlNuYfW1h0xiUukTQAZ0E=; b=OxBRaCeIo5HECWE4zj1rH45ytE/mhLJ2m6tnLnta0iosYLwk6bWu1u2FYUSHXmYiwR7t Cd+PXZG2mySajY/Rap+mliwdwSvBhXjTgECO64XS2isGM3wHxmpy7eWYq4h7cEmM5OBk 80qpzUjeCp+MaxdvecS83lvEzXXGZvtEkbsUSQB4g3iYoq2wHGvWM9iHljkkM67EJw5i 4N8Ug+/aur4GSYTH0YwkPqjYkDKuOcuVabAI1KdtSaXeyJ68DBykf+Eb1ifKiBTP15la Bqn2v/9D6kZG/ZAKyK1URxBF0G5Cfx1J6AxTkd+yP837k0b2gTJmawGuBQt2FzYG4z6B /w== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fmb102gqr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 28 Apr 2022 05:54:49 +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 23S5jAWA030614; Thu, 28 Apr 2022 05:54:47 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam08lp2043.outbound.protection.outlook.com [104.47.74.43]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fm7w60tuq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 28 Apr 2022 05:54:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EzoabQ7SYKczVc66kY7O5FMRnUGnXA4bmZn92poVkMy12pehYc0rJxmDjbnfGDDHYoUF//nkxVf+w5rMKUT/5jqeTArVSL7ZZmqcyunRHEngJZwaa6lRloMEyddqrp/Df8hkcjg5jm8F2mVsg70k7TmJK14fkHI5dd7MB8UHJRZUwWOsdBSxwcgsC1J06I7spfKGKe183aLlHTL1okP7yos5Hue7q4UIZaeavJCVWljpjr28noNlgobEhXrBYsLkBUaMjKvZTrAgFIcriyUnE5FMGqACGVjZ01Z/u8I1oCNV79vpKib81gzRlHaYRrKKntIm1MDcGFnvcrpEO5HiYQ== 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=/CmSLrqClYIE087SRXM6wfjlNuYfW1h0xiUukTQAZ0E=; b=FGepABEAxoKgIQEDKDstWFZ/SBR4PqBvR+TQ5bffm7ohOya25Ygg83cStKHS5BqdS+saIqQEta+Y0lqFEMr2WUrye1QdjDSpj8z/GUawh3kgziWo2BU7GD0U4bgXYS6ypsncCG1Bpmbg3lOwUpf2XMKOXK7Ts3lAVRx/tZAR1duPB9O9pzln0p4uVxB68sRNO3rg9c7n/20nO5i7JrUoAEI2UibJMcWZgzaGnPxRRYEdkQNJThWhxPSZsuQFBraopY04fiYLXOsfVb8/ZPRWwba6Kw9Amo8mEQTGEyB1AvDLsn4DsSxOFi3DL1iMMQ7oz2rr/rsa/tOI/YC4SDSxfQ== 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=/CmSLrqClYIE087SRXM6wfjlNuYfW1h0xiUukTQAZ0E=; b=eeZQn8753vnrzc56hpGwH8+Py5lo8aNYhfOvdF0lU9VpUqyQ088sc2d8UDnG44L5RHujOHEqlbpDpDMUHpSW1fJ6pkhO/sF4IdFFAHZtjIs8EJWBVtkEIMgNmT/UjDd1SwWQ34OOaSfICYlwjDKW1bb2061zcVaJkOcWkwOYvDs= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by MWHPR10MB1789.namprd10.prod.outlook.com (2603:10b6:301:8::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.15; Thu, 28 Apr 2022 05:54:46 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::14ef:8202:73ba:29ec]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::14ef:8202:73ba:29ec%3]) with mapi id 15.20.5186.021; Thu, 28 Apr 2022 05:54:45 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH 1/5] kernfs: Remove reference counting for kernfs_open_node. Date: Thu, 28 Apr 2022 15:54:27 +1000 Message-Id: <20220428055431.3826852-2-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220428055431.3826852-1-imran.f.khan@oracle.com> References: <20220428055431.3826852-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0016.ausprd01.prod.outlook.com (2603:10c6:10:e8::21) 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: 3a0a06f9-c705-4434-5e37-08da28db98f6 X-MS-TrafficTypeDiagnostic: MWHPR10MB1789: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: jG2qrDViny44wBdv+NjPVk4sXhOxOcXTygOfrCHfaEPTAnnlsHagixKGvqzNpjqALCiNoMVMI7xFfc0xiC/qpxFUc+KPZE5A/t131Scro7PH0cMLNFXzfTsCiDowMvKbWeo7iCkaO4eBbRg9AcQatDOTgZdOH37jwF2vBal/GQ05Thw2ci8BHfGiMSxUqpyR/Zs+DWV4N9ndtAMpOqgEbB/+rzaqqC6PexWXGXaAfUMBkpTJXcTmzNhk3Hp7nmq2KvokSkHJHKIw5Kr4JoYfw7NDH5kiEkqVjK58lHWLrs8szY5QuiQgQQJWn2lOopPtoYcnaik/Ctp/ptAzMS9FS+E6NmVrAn/sDWzfnnJtdp13qb5L9QTCg5m1fQdzvFI/pfL50v9IHPaZLTGpa9VWMKhlVpSc+RVajLzsXen5tfGCP4A4EAUMf27Gvj/oUKL2T7JJMkuJlaHj89M3ldRED9fmV2GsEasD3x4xUFJ44hdktptcaorLrrmGDzDFQMDQtKYrzN4o/gVab6BcQQdIglJI//OdcSiM1wGWL3QPPh94RIWgpUCJDE3q8CB1yVPxq3iWO9Qn76TjaXiz1h/wwRCwb56BieHA5NSPf9tiG8pMNama1cx0bx66kYny7jbntfEU5cCBCC7v3AEZ2Q+cVbzcJaGFxxJ+mDhnX7n81G2eOV1anIK88UF2YovIzSaC6Cz1PO7khHcGBQFNhvf+Kg== 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)(316002)(5660300002)(2616005)(6666004)(66556008)(26005)(8936002)(66946007)(6512007)(508600001)(2906002)(38100700002)(38350700002)(86362001)(1076003)(52116002)(6486002)(4326008)(83380400001)(66476007)(8676002)(186003)(6506007)(36756003)(103116003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?XUcq9j3/tGLNxD/W7po9VHdolRLiPC4NJINvR5eTLaO2Uk2gwevTXwNgokbX?= =?us-ascii?Q?+WGaHWl8swbQDs3VVAcbEFkcSAMXPhMp+4GUpRFtn6aDYeSPSZ1wHoKHc/Yj?= =?us-ascii?Q?rWoPQb2pDlwLBcdV+SdudFEJlQ/Wqla9/xTjXe11LtXF6ul1d8eGsGyRytVZ?= =?us-ascii?Q?PMWbCbuDF9CXLtRslBJ07cQ0I55Qda+dAm56ayYkpIqaFVJZ+JLhy2BmNxiA?= =?us-ascii?Q?HdLOTgJcp1ouZ1Qp3XDPEL4nNeJWQobmPwqvlaxkOUskBk0+RD1Cob6T+FIb?= =?us-ascii?Q?Ff7XOLTAI3fMQOUGkgAusvOQ8pnPvn9XR1tfGDWGIWNFCOc3Lv9Uvj1dOPXR?= =?us-ascii?Q?fdFjAzT4iLLoeUT27KzkjZ6bFJMt2PxpFBiRQ07Muv5nLXp8pjRoS8k2B4iB?= =?us-ascii?Q?SgHZoXljIzAAQDgXaRwFVctHU/ZIbsdD8x119YZ48BgXYAjcVLPlJttBeECH?= =?us-ascii?Q?zIu98EInytG+7igxcSeDxsoUPyWGtzFBmcXgQeIn0uKriv5BlXfhe+HlAwqa?= =?us-ascii?Q?vgJlzoVs/GlBQSrsd+CUCL2Icp9+fRmeQmUQuy1aoqkvQ3GXUXWFXrGimgoY?= =?us-ascii?Q?OmOS9TBg1DcAcQUP5RpGD5IKvO5FMk7/0ch3yaaUYM+Pdt/ygkZlT1t6wBN/?= =?us-ascii?Q?twMYZp5l48DXoeHee6HC3Atattj9YiayyjOQpgBCQgItBeJ7rBlbgj+pO4UJ?= =?us-ascii?Q?TO93wagee+4HDC9ShN2Kg2xwM7rvfJpWWmtebR7sILEsq1X24geExc4bQNOj?= =?us-ascii?Q?9jVxcV/34OKywtINTjI6TbtaHhT6wSUxCD33IihCpmJLv9sFmkVa5s8X8/x0?= =?us-ascii?Q?Y2NS/JKIi0jysCX3wZxVW/7lCZKfDBb9PqFC+/1Z1RiQPRU7jdqI8X5I7mT5?= =?us-ascii?Q?V57TNbQysvhO5WxhjnwyJ0mChJOrDXFvTPVqc3q+2YfvzYiBoFHf1bGUagGI?= =?us-ascii?Q?nQ9e7TA7J4+aL0INmnts/oeZcjfEsxcjpTzYGPLJrPHHnti8B2QGSSgDxWXe?= =?us-ascii?Q?zAT14V+3Da1a/Iwsh+WTv80fvV6OVE2oxTKqzoEjuYXFQ5hgg4vueVTP/N5K?= =?us-ascii?Q?UdPavwX7FlZ9RSC8O1ItcflmuvBv7SMN/3g2IwkawUTVcXNFG9GbbHmeNXZ+?= =?us-ascii?Q?HspNWzEG8n/rXeIQX5Bq8VRc4CPzAsw/ABYm0fyearwhQQ2XFn5RWV8TnlPw?= =?us-ascii?Q?9+unDrMRu35uTURzAOR0PO6qTIrWUHZCeUlScut2xkLFGJ9yab9po4uL3Mph?= =?us-ascii?Q?s/lLpgJFxFXVRIzl8vpSjJf4x26QCC1sPxmGHQcPY/WREJjBJQ+QITbDj5uA?= =?us-ascii?Q?2zsBDekr2p9QY7p71pYkwgG/5WDk0bfiZvCgjecRx8TRVF3fGhMKn2D18PIU?= =?us-ascii?Q?1bkexNjiSTDZg/gK1vU46dWBZpIJA+YzVV91OC0JVhtMErLn+0PZ2YwYiFFc?= =?us-ascii?Q?tN9OWyeFnEhIceUNiYwvuxEyDdx6BzzePG+R25xZYs2NwTK+feNNhFf9lltO?= =?us-ascii?Q?8chcswGGCGH2Th5Jr09kCRhuh+4FRunBKcwSduuFz4urVskGTwlMMeSbO8iM?= =?us-ascii?Q?pP35iA0Ns6wZySZ6sxIp+67pg2bOj1KXGTFgdjPrNl7lbpscGbJ3G9SkQUey?= =?us-ascii?Q?TxteoB7nZLD2bbWLqGOYk/2/a3335UKFMudj6EX/FfR8Jz2Gf/al0WR4Njiz?= =?us-ascii?Q?EAjXUYWcoFHlI/7mGzRSUO8eQPGZcdvkIYNoSHb4LLXnAoRkd/CXPj+Pujzs?= =?us-ascii?Q?N9AoCUkSXBZnI2JLVJ2JGRlpyu30T74=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3a0a06f9-c705-4434-5e37-08da28db98f6 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Apr 2022 05:54:45.7933 (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: qVJWso5/4X2/NxY6VnUe7ueZ6DCC4dQYYmOQts/VVg5OeNWY+CSByVL5Ty2lD2FbLWEvQWcxUcWOAVlFjN5GYw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR10MB1789 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-04-27_04:2022-04-27,2022-04-27 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 spamscore=0 adultscore=0 mlxscore=0 bulkscore=0 suspectscore=0 malwarescore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204280035 X-Proofpoint-ORIG-GUID: DQ1RZSFSzCPejkhH8vEYZmWnJvh75F9Y X-Proofpoint-GUID: DQ1RZSFSzCPejkhH8vEYZmWnJvh75F9Y 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 Acked-by: Tejun Heo --- fs/kernfs/file.c | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 88423069407c..e3abfa843879 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); @@ -556,17 +552,19 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, } =20 /** - * kernfs_put_open_node - put kernfs_open_node - * @kn: target kernfs_nodet + * kernfs_unlink_open_file - Unlink @of from @kn. + * + * @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. + * Unlink @of from list of @kn's associated open files. If list of + * associated open files becomes empty, disassociate and free + * kernfs_open_node. * * LOCKING: * None. */ -static void kernfs_put_open_node(struct kernfs_node *kn, +static void kernfs_unlink_open_file(struct kernfs_node *kn, struct kernfs_open_file *of) { struct kernfs_open_node *on =3D kn->attr.open; @@ -578,7 +576,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; @@ -706,7 +704,7 @@ static int kernfs_fop_open(struct inode *inode, struct = file *file) return 0; =20 err_put_node: - kernfs_put_open_node(kn, of); + kernfs_unlink_open_file(kn, of); err_seq_release: seq_release(inode, file); err_free: @@ -752,7 +750,7 @@ static int kernfs_fop_release(struct inode *inode, stru= ct file *filp) mutex_unlock(&kernfs_open_file_mutex); } =20 - kernfs_put_open_node(kn, of); + kernfs_unlink_open_file(kn, of); seq_release(inode, filp); kfree(of->prealloc_buf); kfree(of); @@ -768,15 +766,24 @@ 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) + /* + * lockless opportunistic check is safe below because no one is adding to + * ->attr.open at this point of time. This check allows early bail out + * if ->attr.open is already NULL. kernfs_unlink_open_file makes + * ->attr.open NULL only while holding kernfs_open_file_mutex so below + * check under kernfs_open_file_mutex will ensure bailing out if + * ->attr.open became NULL while waiting for the mutex. + */ + if (!kn->attr.open) return; =20 mutex_lock(&kernfs_open_file_mutex); + if (!kn->attr.open) { + mutex_unlock(&kernfs_open_file_mutex); + return; + } + + on =3D kn->attr.open; =20 list_for_each_entry(of, &on->files, list) { struct inode *inode =3D file_inode(of->file); @@ -789,8 +796,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:02 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 053E9C433EF for ; Thu, 28 Apr 2022 05:55:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243382AbiD1F6d (ORCPT ); Thu, 28 Apr 2022 01:58:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243315AbiD1F6Q (ORCPT ); Thu, 28 Apr 2022 01:58:16 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C90547C780 for ; Wed, 27 Apr 2022 22:54:59 -0700 (PDT) Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 23S3Wv97018608; Thu, 28 Apr 2022 05:54:50 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=dG7GcFp5EYK6YZJsbVee6Ls20dkAdgDheh3ibk5p4t0=; b=0V5LbXevmZJ8Tfmskwx93h2nXCrCU0aSMS7XRW6QDyQh/cWDACBkQjDiEuOTMdaOM4Vp MX0pdqSz39lAUB1cjI40EbhSATI8zetK6U+EqTwO8snI0yOmSuyAIHA/cazAJGVu0sQA 8E6Dnl8pKlKtLlOlZrRFNMu3AHm3yFTgwIfVofBPX5vEhcWKFeckItzRfgHVtQ2PuoWe +pZJh+usD86yWvgUdozip0nvLTIm4bkzqUewNaevFeUTr8aUHngG3Cy1gG0qD41ZyeCH xDpB0aaj7atBn6pPDqSSZmabGIrkAiJW/CNa1bVBovhzHbPA4OMT/ye3MsYJFRZGxfRC CA== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fmb5k300g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 28 Apr 2022 05:54:49 +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 23S5kbh9033686; Thu, 28 Apr 2022 05:54:49 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam08lp2047.outbound.protection.outlook.com [104.47.74.47]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fp5yn5ad0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 28 Apr 2022 05:54:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YBVFBRpLcHmxm7RlGBxh3dDM6btzshcqVrfUiyFZi5nTNxYvvVUQrNsIyK+023skqO1E80ceiLaES1+5hFztC+AgUe7AGvZgIDLP96rv0nqsAXAv/FC4qaOX5N4JS3sCz+Ls1XOpiYCDU8GlBXeT8WssbfDAhQYjYKPSkhWSlCu3aqipOteyv+wBt5hUG7YPJ2wmac6TuFJ/swzx348FCKnkUk9eV8gixdona7IuphsQZJt4ySMpHPO50BlAu1YNG9lEFfDig698BeWjUOjwrrFBTc4D6jFJQtIGlLSbNLtQoD/B2n+tpoCRNMTNFwxYh4OkLmb/omIMUaqDivKHdg== 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=dG7GcFp5EYK6YZJsbVee6Ls20dkAdgDheh3ibk5p4t0=; b=VXQoycgNfa9XWIEV5GBIC1Z6FDqRyRPEfErNXY1BHtBhxQdvQSypEqBQcWFqMD68+sw/nLhOawswIGCcsgKP2eFX8fLQBS0ZChjjFZvWEyeLvB6PO+Smb+VrSMov5Jp/0yk3QERrW93gHACsUqxvuWysbA5lNs8JT1gy+bm24ROp4I12b/LfsSmaNlCiRl7oP0V50UP5O8UN2GU4iCa6d2C+4UA5S3G2oJU7KvP+24b3IpVpp+m/nw67jePmvXsMOs20zyONIe9guStcZeBAWkgJrmzKDMOQFhG9hfzak1udltrvhhtWM8P4q21BSOj2eRWfZJMaPSWwpWiYCY8laQ== 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=dG7GcFp5EYK6YZJsbVee6Ls20dkAdgDheh3ibk5p4t0=; b=iIaTQ4gZDj06LnNeLapTqP2XTLpGv2dl7KK2Age7LUZoGf5xak6dgDKsg24IFtQDTFHeEMy/GQqz0xDXlpL75SrxrksmnKrnSDnC3I5uCO0l5B3KIsM5gt5QCi+ujDxqAHcXi5bqHTPwf2ODpszFrMARX0RRywpbiEMoWbArnUY= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by MWHPR10MB1789.namprd10.prod.outlook.com (2603:10b6:301:8::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.15; Thu, 28 Apr 2022 05:54:47 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::14ef:8202:73ba:29ec]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::14ef:8202:73ba:29ec%3]) with mapi id 15.20.5186.021; Thu, 28 Apr 2022 05:54:47 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH 2/5] kernfs: make ->attr.open RCU protected. Date: Thu, 28 Apr 2022 15:54:28 +1000 Message-Id: <20220428055431.3826852-3-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220428055431.3826852-1-imran.f.khan@oracle.com> References: <20220428055431.3826852-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0016.ausprd01.prod.outlook.com (2603:10c6:10:e8::21) 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: 2fb502f4-98e1-4f68-d68b-08da28db9a0f X-MS-TrafficTypeDiagnostic: MWHPR10MB1789: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: q1t/DBHiSA+n3IpjEu2RSVX3Ziz/9bbMS2nxVzFnJVO2wc6DJisx7H/9Uh2KO6pBMZgSlhvjmjWqZWxekS2RaSGwqU0K7lXf81V5hWrdFTCf1wqQsRtKowXEIrEn/rs27regPOrgYSRsrMdZlavNbRhOFm/A7MecV1ZkfRbSY6I9XpHSYWtuJJxglz8JY1RN/x+hWcx/WSQIT47/W+HHcuawUHdA6swC/J4AuTUE6qLKbtLaWfSBFgB2IUdMAgEfNZRoMHwrno4NuaOkY/DB6RxIPKEzZjOSwATsg1Du6uowjjKTxS46qothK3kZC/HzwE+7m09LjTFxQQqAMwH3bGDbjEJvcMn3u4B8sf87Sh5x4hEA0hf17dsJuIVw5TZ36Bh2Rt1/sV8UQZQ+x4OQTGIgsusH7Ra0m8mabdh7cl+aXbSx/NLJodbqY2PNJJ3vJubQaSqEwgM8xbhNOY02x/f5emuIgb/ESARSqGYo4zxZydF14N3GkRbqqnL3ogt+O+yGp/dXuycoEqut0SZMKmN0uB19ZmH/o9zgUfW2MFxZKFMokMLBckyDkUt01j6C6pW5ThBvRh57qO7PDXDowkk5N3M7qki/bjvS/epaatXud1QrG3I5K1BJpaeTNaK52ympfafKh2Bjs244ZYMb+d89o/gBTJXCNrZaoTIcZUaPRgQKe1ND7FrZSJypUSTOzFYyIrfGejgPVvMwo3GX+g== 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)(316002)(5660300002)(2616005)(6666004)(66556008)(26005)(8936002)(66946007)(6512007)(508600001)(2906002)(38100700002)(38350700002)(86362001)(1076003)(52116002)(6486002)(4326008)(83380400001)(66476007)(8676002)(186003)(6506007)(36756003)(103116003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?H3SSBWVcghLF20EJVXQ0edqQn9c38GMu/POKzZKsjSiqZr25lTmN4HHCoR7+?= =?us-ascii?Q?Mq6BEiU0wkFd8xaPoIYVHXobErkgQW4IvrAS2RMcPxXsz8zPegHAQpwPqcH7?= =?us-ascii?Q?t9wmxTA1LKkGlHrNxHT3Mm+OfRcZK0wT/+Ho/p5ma+Mx4yZj4VBBXQEnaQxi?= =?us-ascii?Q?n810o89X5UwjodMcLWgpmmzX1I9c9SwTFm0S0vLDEa5AIxLRvON1JNpC7L0F?= =?us-ascii?Q?ZSIU/cx7YOVCRso0JQ9t8c1ixo+C+5gccP8FLorNoMyGmiTb6LNM3pssLNbR?= =?us-ascii?Q?G8i9Hreh8Nndzzw2JcYfQ1xOID9mG6CGy368yz6oc6VDPk7kbU5Mm8Vrgnnf?= =?us-ascii?Q?IKhzL3Kq/BokICZ33IZ1wo6cbBMs4RXZ9BKx3lPDo7ZfSL0K0ZEMYkP1zTiD?= =?us-ascii?Q?OlTZdaOpQHWSEX6yfnrokL8GJD9AcDKYni65wjKTid+fswjVtMgy5iYbsOA7?= =?us-ascii?Q?/ISKnYbkqiB118blntURY7gcMjCVugXS6UtTzXMLvfn9fDrBvzorhULc+TTf?= =?us-ascii?Q?wwTgKBjvvEe4xEK4PE+SvxvZ4SxhdNdQ2ysG5t1aX3L/omvLlH2NCwds+gPB?= =?us-ascii?Q?dMBIWcdMEfr3hFxzUK4xBTjE3l/vIubnpdnklksNlTUFYYj4O/azbilzlkPs?= =?us-ascii?Q?8DZDrgCBNZSmGhF5Hdu442YIYY8PYeTiEuYbPGAtnAplW9KxkQspSQamkUQg?= =?us-ascii?Q?Fpc8+gVFFrmD99aZkUDg75TruFvRfGWL27iukIxH3MdKkE31tC17vhTJrXLw?= =?us-ascii?Q?nxeZOcu5w35UPPKkwa9mIIv0e7lJrfz/8cdHfldVrhdYAEgt3q2YmDqg+X2R?= =?us-ascii?Q?IfAT86mN4PChKnnTVA/wwuv5D+zesAqWXWVZNzcKGWy8NTxhOkAw5xtfy2t6?= =?us-ascii?Q?f892Srj74uWPqRmaeWnHuNk394gviecKS7NkIPkz1SL+REHe0L2rVa0WRNnf?= =?us-ascii?Q?NO0HD1KB38bxQ6RrghOwFn+IEVbIwyWAzL1/j9dqMc3262YvUg9xX8ceCiUv?= =?us-ascii?Q?qiLx5OKJ8Nhyc65sZl7qi4F8y+CvptTFKDP2DwUBKBMtRW0QsWg3X2PTREeC?= =?us-ascii?Q?ViKaIuLTUzjwDDyJ0TnTeqITSj72xR3bCC9rAnC+G+AJLhqlFIkpEACcT4qq?= =?us-ascii?Q?3K3hIDQ4AgEMc1ST9VyqGuiuxylhABbjy3OGxkbegXfOFD91EhOr4uzkZC/x?= =?us-ascii?Q?xnxT3L0lZz3BYapHcogJSd71wdqIojDYK9ZkjaROGoo4bHw6s8PmnOa4JNpF?= =?us-ascii?Q?IhKgBrqW9zkZN5HAxySJ1IK0XlLo2X4Enxi8vfAtyZ4VW8SZ3Mw7honN70oV?= =?us-ascii?Q?Axk1Dx9Ho0DFXllz/RNpNllBnxrTRhtBQGNNHVTwP4uYkComw1JSIoloS1uI?= =?us-ascii?Q?c8CP/X/4fPguL4iBI5Srm+5n7q6XqP6L09uOV6MSvfdDAbwfMo6zdhqgp9zO?= =?us-ascii?Q?mIDRf1VdpuhmuEdx0s4zynXXdkrxALwUPDok3atfT4wUl+ZSh6oi1LTqFnFP?= =?us-ascii?Q?8X2kMXeItdJwSKCxw9/cDaZuPOVBYHgRm+nnC5d51q2qbI4AhKTYg90/haQR?= =?us-ascii?Q?kt6y2nELEHM09DX8efWCch3I6jUI+eUrG8D3QSWh57s+K41jY8F71gCKmiKN?= =?us-ascii?Q?shXY4BgDOz/NPyJaHcubvwVtTWl9T9Yhp0dEtiddJRhjzyAMmgHW1G9TkxWB?= =?us-ascii?Q?rG3Ma3xfW8u/m74hM24uND8a3D0cOJ7LUJs5FYJChhJdZ5QBtI4XBCAO2/2u?= =?us-ascii?Q?2i/yHdKDewb4A2NQIV9eYGYhu9XXnUM=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2fb502f4-98e1-4f68-d68b-08da28db9a0f X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Apr 2022 05:54:47.7629 (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: HXnMmYAtNzkGHcDQWqCeBwG1ovKVHSL+zLTVHnuILAnWfRINijKhU+T1Ir3fDnX7YjyDjRpyVodEbPKrk9Ss0Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR10MB1789 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-04-27_04:2022-04-27,2022-04-27 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 mlxlogscore=999 mlxscore=0 suspectscore=0 malwarescore=0 spamscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204280035 X-Proofpoint-GUID: -4siOx-vEFCyAL9f4u_JmyKb613j5wmi X-Proofpoint-ORIG-GUID: -4siOx-vEFCyAL9f4u_JmyKb613j5wmi 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. Suggested by: Al Viro Signed-off-by: Imran Khan --- fs/kernfs/file.c | 120 +++++++++++++++++++++++++---------------- include/linux/kernfs.h | 2 +- 2 files changed, 75 insertions(+), 47 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index e3abfa843879..36bff71ab263 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 */ @@ -51,6 +51,38 @@ struct kernfs_open_node { static DEFINE_SPINLOCK(kernfs_notify_lock); static struct kernfs_node *kernfs_notify_list =3D KERNFS_NOTIFY_EOL; =20 +/* + * Raw deref RCU protected kn->attr.open. + * The caller guarantees that @on will not vanish in the middle of this + * function and hence we can deref ->attr.open raw. + */ +static struct kernfs_open_node *kernfs_deref_on_raw(struct kernfs_node *kn) +{ + return rcu_dereference_raw(kn->attr.open); +} + +/* + * Deref ->attr.open corresponding to @kn while holding kernfs_open_file_m= utex. + * ->attr.open is modified under kernfs_open_file_mutex. So it can be safe= ly + * accessed outside RCU read-side critical section, while holding the mute= x. + */ +static struct kernfs_open_node *kernfs_deref_on_protected(struct kernfs_no= de *kn) +{ + return rcu_dereference_protected(kn->attr.open, + lockdep_is_held(&kernfs_open_file_mutex)); +} + +/* + * Check ->attr.open corresponding to @kn while holding kernfs_open_file_m= utex. + * ->attr.open is modified under kernfs_open_file_mutex. So it can be safe= ly + * accessed outside RCU read-side critical section, while holding the mute= x. + */ +static struct kernfs_open_node *kernfs_check_on_protected(struct kernfs_no= de *kn) +{ + return rcu_dereference_check(kn->attr.open, + lockdep_is_held(&kernfs_open_file_mutex)); +} + static struct kernfs_open_file *kernfs_of(struct file *file) { return ((struct seq_file *)file->private_data)->private; @@ -156,8 +188,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 kernfs_deref_on_raw(of->kn); =20 - of->event =3D atomic_read(&of->kn->attr.open->event); + of->event =3D atomic_read(&unrcu_pointer(on)->event); =20 return of->kn->attr.ops->seq_show(sf, v); } @@ -180,6 +213,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 +235,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 kernfs_deref_on_raw(of->kn); + of->event =3D atomic_read(&unrcu_pointer(on)->event); ops =3D kernfs_ops(of->kn); if (ops->read) len =3D ops->read(of, buf, len, iocb->ki_pos); @@ -519,36 +554,29 @@ 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); + on =3D kernfs_deref_on_protected(kn); =20 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 */ + 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 /** @@ -567,24 +595,25 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, static void kernfs_unlink_open_file(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; =20 mutex_lock(&kernfs_open_file_mutex); - spin_lock_irqsave(&kernfs_open_node_lock, flags); + + on =3D kernfs_deref_on_protected(kn); + 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; + if (list_empty(&on->files)) { + rcu_assign_pointer(kn->attr.open, NULL); + kfree_rcu(on, rcu_head); + } =20 - spin_unlock_irqrestore(&kernfs_open_node_lock, flags); mutex_unlock(&kernfs_open_file_mutex); - - kfree(on); } =20 static int kernfs_fop_open(struct inode *inode, struct file *file) @@ -774,17 +803,16 @@ void kernfs_drain_open_files(struct kernfs_node *kn) * check under kernfs_open_file_mutex will ensure bailing out if * ->attr.open became NULL while waiting for the mutex. */ - if (!kn->attr.open) + if (!rcu_access_pointer(kn->attr.open)) return; =20 mutex_lock(&kernfs_open_file_mutex); - if (!kn->attr.open) { + on =3D kernfs_check_on_protected(kn); + if (!on) { mutex_unlock(&kernfs_open_file_mutex); return; } =20 - on =3D kn->attr.open; - list_for_each_entry(of, &on->files, list) { struct inode *inode =3D file_inode(of->file); =20 @@ -815,7 +843,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 kernfs_deref_on_raw(kn); =20 poll_wait(of->file, &on->poll, wait); =20 @@ -922,13 +950,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(); =20 /* schedule work to kick fsnotify */ spin_lock_irqsave(&kernfs_notify_lock, flags); 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:02 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 25670C433EF for ; Thu, 28 Apr 2022 05:55:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243356AbiD1F61 (ORCPT ); Thu, 28 Apr 2022 01:58:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243284AbiD1F6J (ORCPT ); Thu, 28 Apr 2022 01:58:09 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 991C27C273 for ; Wed, 27 Apr 2022 22:54:56 -0700 (PDT) Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 23S2gEI8015530; Thu, 28 Apr 2022 05:54: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=9OkcfwQVEoQcLOHcE1BNDaBawN3QOSQ7/Fx8akMzMYA=; b=NYUqU2H+Hyl+Y5Q2QXyluITx0VTEcn5KVonecGouIAzNaYi1xSVN4IGXYrB9jK1XvM8F xWdRzDSXFFuCkeqFr550KECsbGvngMpDtaPPMNV1qwYqeVJgtAwYFYk6q/kwsKN8yGEC QbtVU1aqpkYDMrYpuGGn8x6amdDvZdSELPS+hCV+isiyjTGh5wDVOLTkO1annCD6Bg2K rLOJOcBO6R8/2LSsfYhhOoI2X46tao5Ch79v+Lt+dSJldGHOhB+n1ClNmKBKEUwQiXZX jkM0yReBvZEPg/OSgVido/Ga8WsOhKp64NmaZl3UEEmY7lG2dzjzedR+WIfvxfjyZQnz EQ== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fmb9atvx0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 28 Apr 2022 05:54:52 +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 23S5kABF029716; Thu, 28 Apr 2022 05:54:51 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam08lp2048.outbound.protection.outlook.com [104.47.74.48]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fm7w60aww-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 28 Apr 2022 05:54:51 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MDA0Bz6Ku4bho2GBGPjh5/vHBgWgjSU3VTYn2AwTRSwTvj9DY9Aw10pp3vEJX6BX/HH66U0a+wAsl/Tj5iW2I4tjcVndtAM+q/K53R4llif7kbpdgjHLxp3JIkLr4W/cVT8oEqP6mg5XmMYGScSFvAxf1q57c3n22NsUWYmECAs6p6ZzJK0vmYu8AYORV0gy3iBkkkAF1kYkBsHZ7kdcAPCMlVgawsL5ueziAbs5bZjVQtDLfdeXaKqllWXsln9FLw6lDAXtZZKa5q6Y/xRmf2XzRQxZ7br36dUp6HrNh3PCr1pzNrMzJ9gsM3UH3LZgz1HjN9e/TBEYpb7xvpjXEw== 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=9OkcfwQVEoQcLOHcE1BNDaBawN3QOSQ7/Fx8akMzMYA=; b=RlX0pdiszpnA4nmvTEY+D58m0KHhLfOx9FUnc8nXwEVeZLt3BAxiqlFgnbn1r1evSnXEiGV1VHLwtJSkQQzn+7rI/zOsEE9jNQSoygsltXLYhDqrFErJ8LmXYQXyIU3LVfC45yTLIWa0RBIfMof+BZSA3As02pZSMNBbVGLpJicv35xnt7ruNPZbr5qmnD7VPGc3EPNrBG6/Ttkem3CYpssJVZNt3jaMVR2Js+i4JpnmoKfhjEpdkbxl2CUIe9bFVSqbTixQH6xG9HTfCm/LhsrgT2XycLFO0jr+5YZMz2yw8w50vK8dQBUwO7cJWXQqvWpwl9MDjemzU3KSfidhwA== 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=9OkcfwQVEoQcLOHcE1BNDaBawN3QOSQ7/Fx8akMzMYA=; b=y024ULWJWFFgYrMEaNlIn7gArDnvIZRnB9lhqc5OzjUpfnnDo+HRORoKrBqssk4nelF7l8u9fofD+Em4QyBzKknascOzK/CeBtgZzGtU0WFx0U7a9UFRRljDd5nvZJ6nm8VkGe507LlZvlUAx0sbxOb2rDJ9FNhmydHb7qhklbs= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by MWHPR10MB1789.namprd10.prod.outlook.com (2603:10b6:301:8::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.15; Thu, 28 Apr 2022 05:54:49 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::14ef:8202:73ba:29ec]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::14ef:8202:73ba:29ec%3]) with mapi id 15.20.5186.021; Thu, 28 Apr 2022 05:54:49 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH 3/5] kernfs: Change kernfs_notify_list to llist. Date: Thu, 28 Apr 2022 15:54:29 +1000 Message-Id: <20220428055431.3826852-4-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220428055431.3826852-1-imran.f.khan@oracle.com> References: <20220428055431.3826852-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0016.ausprd01.prod.outlook.com (2603:10c6:10:e8::21) 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: 4149e507-cd2e-412b-1dd7-08da28db9b48 X-MS-TrafficTypeDiagnostic: MWHPR10MB1789: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: kxa2b0NKT2jtdpJat6Di9/D74Zt9WGrUumNBFDShEV8472Cf5ZR5QsQZOGlUe8IT2KiQ2O5B4DOVi/siMwaSRFWrkZ7+2Ts/JtaAuHcxjy9CDiojdQYW0Uc6t1i2FQwX2U3h4TSoewlqHdRhevM7Vrt6whho8wUkQElSBlV1oACGMhsYM5VEak6m5EzXtTUoS+ezsBgFlBSYg75nL1Z+tJLCV5yCGF+BamrpYrflcef7hMXljNJk89IWxnwtZAf8G5FnZsgcje8XD2sEK+z6Ff150PPz6wXrtpOznAWaOM9IsqbCSUGze6OsF+Pwia0JzP78B1e3oUhzJyvp5wDFKv1eKvlM0FlZ+PSis2Abgno7a9uzrWy6eSSQiIA7dcHVoNnHYLmD8UWIyuWhFWZMLhqWhAi0P0tRur1EjiAfzBeElkbYYF4jphjDNS79bYMHh379e6MsxpJ5SB5KEmTY1cX85s0Syf/kUL6NhL6W+EDoVYt62YA3ixxtu/hZ5LlUdVWtpduUL5JLd34Cnx1oUjADT3PjiklTzaF1GvqLhB/GrUn+3bGdQdFJw8qKPKoTwALHE0H2X7KjtdPSh7jAKyeHw/2dWv5rtpAmEucgCXDYWGBWJ0C9rRBbSHcX0ipHizLsLC3mT+YfiSIfC1DX+jcRRRvTkcGM09xh/OBjU3p33YL0iyiA0X9MzxxHUVBpnRoiRi/4Q0IONIO+3rk+eg== 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)(316002)(5660300002)(2616005)(6666004)(66556008)(26005)(8936002)(66946007)(6512007)(508600001)(2906002)(38100700002)(38350700002)(86362001)(1076003)(52116002)(6486002)(4326008)(83380400001)(66476007)(8676002)(186003)(6506007)(36756003)(103116003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?sfKnpUzBaDSfbx+TPaJQ/RgVAriqz/pCwNmHUtL/GAtV494+P+xnr2U50LSM?= =?us-ascii?Q?odnzrSyEsWtunYmvzjVCx99diLyWNRS+7z2T/Oa3a1yuVnXGgtWG6aYIgiy6?= =?us-ascii?Q?9vehqA/LXHuJI3IFEAJgXF4Q2abt7hqI5PMsov3TjOUfa+CcwXpwzX1kYi8o?= =?us-ascii?Q?88HdaSqqOTdyDHNQmYbVLWL4EJyD+UhmlVJUpCblyZOUNR80cpk8SCFKWoCg?= =?us-ascii?Q?18OBZGT76D5QRvRGBNEYT+/Fi5t9N1VpFpAhotRMnNNFFl+hsi1FtRVQLWlM?= =?us-ascii?Q?Vf+xe9vclAhwJQhsyBEBaYBkhkfKS/rIks0myDp/biHIF5GNLkp3GS6o3Mrj?= =?us-ascii?Q?dX0jyJ2FJaDUW05JTuVWrKd6fu+M+9Tw5E2jjIZ2udPOG2mdFgz5Rx84xmqp?= =?us-ascii?Q?TtEUIg5yRnXmandZSLytnKS5OfAt8Is5RgxsLafJo1TmAwCM2ur3lBsiJagT?= =?us-ascii?Q?DjeIaAw6ckZXnF+6qyNKciIK8zOTOdspWLzpN2lfqu4E5b4l5izrTz6fcXXO?= =?us-ascii?Q?WVkE5A72kcpuvCCjZDzRK9Lp3MIzKdgCbWzGdG9v84TN6b6agZYZqgriCv4h?= =?us-ascii?Q?O1PIl1TLE9c8JhSaHSdkwtsZND7Lm5ayG1x/YR3EPbB01CGoSUIrEUx83R3V?= =?us-ascii?Q?kqv3fL+tdFXnyU3dZTfY5S8QntU2V7GAAI6aqPA4RA9DrSzwm8ZseiD+AIyH?= =?us-ascii?Q?YxLnq9MCG0jQ+XD5bfn6D2dCAvCWsJQxmQALYPbKmyZxUSarHD3Z7bWDCwkc?= =?us-ascii?Q?WhD+o4SJXGDgi1Rn4IrhPvmEv1KpmxjaOLBuuHzC8zY2NEueeJmrz+Fl4YT0?= =?us-ascii?Q?yE417sMc/8pp3K5XA0L5ONRwtoPlTozp9M7L3FeA6KpvYrJj0uSJdev0yRjo?= =?us-ascii?Q?Oh/V1efxr7ukUz3fzOjbJxssU1Fp78A5egTH62Zir6BFdKWrJN+SxqpvoRZ1?= =?us-ascii?Q?JC9FUqiyU9ub0Hd4BIIn1NyDXK8LuMZS4TFQ+0NRC6e+pkTzszHmyFhqWeKv?= =?us-ascii?Q?rSSEIzaZcN5HW8ZdlFAZBx1X8W2oteMdNo5M7ve3pAzlMJATX0RRI456rBC7?= =?us-ascii?Q?sNb5CXIPgl1SEY0JvIt1Zr4to+bTxZEV1ltIwDYN1T5FEUY+n+RGfdhqZ8Vp?= =?us-ascii?Q?l3PePbjGMRUXNuzAl8ugT5myzscl67DxSo+8nieaFamKS84UNxfCDHKnPN7F?= =?us-ascii?Q?DI1rUtjdWEmU93Atg8YY8Tbjvcuawf530C3dN+sBY9N235V918JCrPT0/EUP?= =?us-ascii?Q?zLNqKd61YuhafdLG+s0wWa8PMQ2y/zpob5p43w/Urm/HE066ijauPxEGmhCQ?= =?us-ascii?Q?SRDyHRXo7VKJzGcFottFDvGX9JWA8QGF/AlVkAT5L0OITOvXxZNelQ1YdliK?= =?us-ascii?Q?Byj3jXT4mgo7KzMUr+0MC6M/uf7WlnbOIFpCNTvv3mf8762NsFIZ7sBd3+Cv?= =?us-ascii?Q?r3A6kVOaz6aDXTRCXPNJo2UO6hqRJY+oEGGHsS0dZF3NoDcVXDIJdOf3MRBC?= =?us-ascii?Q?ELX1BzP3WTu2wlHjfQZLUwMXrsorwSrcN+O3jbJpgQmsXW4904g5ZyFwwzS1?= =?us-ascii?Q?J8LsolYTkDI7WcvmYt11/M2AVuPlf33jWY1GaKZBoaC7QNG65Jc74rmGYQsR?= =?us-ascii?Q?zfFO5Zx+k74p3lNDpd/DFptd6bayTHmIi7ucQuHpRTmNQ8/kn0rIrRyg0yaQ?= =?us-ascii?Q?Wur1YALl9WpIrxrrO0o5N0VcMV0OYPcVkRPZeNQomiSwiLe+2wrHeB88E5gn?= =?us-ascii?Q?x4t0sqPB04fjN+P6KTshpTDD0wyKkXg=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4149e507-cd2e-412b-1dd7-08da28db9b48 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Apr 2022 05:54:49.5583 (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: lkzsaGMlNaXCL0jdgAPXgfYSUA2uktKkAJlQmrqtivcvZzSs0dme8Y8YnamBy9C89wyZ8ZBm6Tc+dEMxJVH93A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR10MB1789 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-04-27_04:2022-04-27,2022-04-27 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 malwarescore=0 mlxscore=0 phishscore=0 bulkscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204280035 X-Proofpoint-ORIG-GUID: upHP8-1U7KmGUEWVmrFw_2SfCnzhIKW_ X-Proofpoint-GUID: upHP8-1U7KmGUEWVmrFw_2SfCnzhIKW_ 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. Suggested by: Al Viro Signed-off-by: Imran Khan Acked-by: Tejun Heo --- fs/kernfs/file.c | 47 ++++++++++++++++++------------------------ include/linux/kernfs.h | 2 +- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 36bff71ab263..96c8493003b6 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -38,18 +38,16 @@ 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 /* * Raw deref RCU protected kn->attr.open. @@ -876,18 +874,16 @@ 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 */ - spin_lock_irq(&kernfs_notify_lock); - kn =3D kernfs_notify_list; - if (kn =3D=3D KERNFS_NOTIFY_EOL) { - spin_unlock_irq(&kernfs_notify_lock); + free =3D llist_del_first(&kernfs_notify_list); + if (free =3D=3D NULL) return; - } - kernfs_notify_list =3D kn->attr.notify_next; - kn->attr.notify_next =3D NULL; - 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); @@ -943,12 +939,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); @@ -959,14 +957,9 @@ void kernfs_notify(struct kernfs_node *kn) rcu_read_unlock(); =20 /* 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:02 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 E60FCC433F5 for ; Thu, 28 Apr 2022 05:55:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243341AbiD1F6W (ORCPT ); Thu, 28 Apr 2022 01:58:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243287AbiD1F6O (ORCPT ); Thu, 28 Apr 2022 01:58:14 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 344F37C786 for ; Wed, 27 Apr 2022 22:54:58 -0700 (PDT) Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 23S0pDSL025790; Thu, 28 Apr 2022 05:54:55 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=u8abtKVpp3wetyUrvExCT0QB/Yr4E5Fq3bnErhIbpIs=; b=TWjTkAyqh8D/jGb7Uha1gkRa7xW1RWkY4fM0ZaZNWeKtid52PzdvcHG1zpUkF/4mP6TU 4gtgbgJPZ0snlaZJb0NvlvE1VrZsUiq5JelJPh5CQ+vUlrbPLt2S192PnL0SGUSpB6UX Ek8SmUPsmfWdPHZM9tX2i9Mtahs8m28DXGZbnQr/RjDn8Bsyc9BJ6oYkL3q6RS4p6tBm rpRIbkMMKoOpa+xtN4vDe5MSU5IZ0t2AaSqQG1dZxJbmwlQL86H6UPACJk6Eip1lfF00 PyfXmFgk60uaEuhjgxltL5ri23NRYPTItOvvvmwJ+PKhPP2zRVpR2Fk3CsreGXciZHoN gQ== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fmb1mu1k4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 28 Apr 2022 05:54:54 +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 23S5jAKA030640; Thu, 28 Apr 2022 05:54:53 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam08lp2040.outbound.protection.outlook.com [104.47.74.40]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fm7w60tx6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 28 Apr 2022 05:54:53 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BSadmBVDLFk6T3k/cshNVKolhTIdkkvEnCedmTB+padtBzOCWIhVO+dHGSaA3w/EeBcy0FEKkxcSqiLkgaTKr8YMg7DotX6TMo1aqo1/DgjI2hdLUng4S99RIsCHo3uCwOrDMuBG2IGd1RfiVbxOfThusljqV4UHqNW+Gav/z9w3zJArnq6bIepVu9viGWBhnEc6PGGFPvmZTjFk+rfsw8pwt+C0xzi+TGzqMNg6YbJ7X5NzBdNGT1ZPyyFskGSAcuo4OtUq++cU80bnVnH6dSh5Sg8NYvV63gBkN6D3BWm9W5tv8FgqLWdLmdBZbB0TQ9R2Ow4398Pf4zTYngyCLA== 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=u8abtKVpp3wetyUrvExCT0QB/Yr4E5Fq3bnErhIbpIs=; b=NdMmVFSwOFknhBcHLsBDAMmlbaGxAO3hi3Esq3thilhIIvWM0BraZPpN36sFh/R6AqDRi2r/gJEWEKODQzuJHmSSdqENsNFGc2SaLUtKe/dnMUFuYakuSk6qR0k9CfJBQ6k0yflHM3gt0yI3R1MIncsIxkG7pLYD/aeu75NmY7WUbv9lgDfXGGparerZ2Gwb0EkX/vJA/s/RjNqMFvGqRaDm3dDK7tSwlplPYn4AvDAWgi+XmVy5D1kLrBXXYA0atjN/dhWWbcTz+l3fNAZfkRkTB+Y45+arh3rUQTAFO0CIeR0QEX3kWLgMfxJiZ9w/0CiLh+6TWuoJwYcUqMnJmA== 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=u8abtKVpp3wetyUrvExCT0QB/Yr4E5Fq3bnErhIbpIs=; b=fxtTijxIauLitoW1jRgbtSU9PJpf6MjA101mehSOnFxwoCeS+kCk4FyTR6uALvj3TaRm+uBXlW3ijgpgSeKusOiBkVMrBl/xrRYTnHO5ifpx3hYy1ZO8jcBo4+RH0wlm2JTzglAFspvcueZn9eJF7UNlU+DdoIT7MFe503AgQ6c= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by MWHPR10MB1789.namprd10.prod.outlook.com (2603:10b6:301:8::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.15; Thu, 28 Apr 2022 05:54:51 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::14ef:8202:73ba:29ec]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::14ef:8202:73ba:29ec%3]) with mapi id 15.20.5186.021; Thu, 28 Apr 2022 05:54:51 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH 4/5] kernfs: Introduce interface to access global kernfs_open_file_mutex. Date: Thu, 28 Apr 2022 15:54:30 +1000 Message-Id: <20220428055431.3826852-5-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220428055431.3826852-1-imran.f.khan@oracle.com> References: <20220428055431.3826852-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0016.ausprd01.prod.outlook.com (2603:10c6:10:e8::21) 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: f6d4024e-b5e4-4769-94b7-08da28db9c61 X-MS-TrafficTypeDiagnostic: MWHPR10MB1789: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: TmW7J7OmSXSxAPThkKTbtB2jdhStu/akpN2i7xFvAq3z101ajfNkQ2ilHBYAA1sRemMBV7bIhT0FOT8OFdiJL3v9eEoHkxIDspIemZDxvjWC7jr11r1ITdA9aCL//WZUY7k3E9B45ElAWOzDTiv4Ij7oK6D4wS72nEGTVlvkkkxKtKZgZKuNg7GEBc4RzrffgcriP5lX4Vswe+ZYHw+OZBYG2xbnLMGBPyzPCl53kGCaIcBPnljlyxK9TpfIiBR0efaOoiX2oNNTlBOaz8FnFa7AUWhzGx3wHaatxmLzu5+0gkAwHEhmOz/shOjnv9noDO7umWNwDM2+TWWu1XH9eVVl0eRLaR9nUKhgnoXQz3PS/EtASqe3TGVcN8z0lYOxAHFZKths1VAwIfemAdme55GYm17H6LFrl+JURCDJsxM42WgjrybhrP5WqmAwz5xp/Im8iPnnK/HTeJvbhJaHcYey507p8LWbN/Qo7t1PxqPN/BOlnlznvgjZrD1tlL+X6eCavT5Y3CEGv+6m0pE0+FN6anXWe7ceainEswHPEH3yj1CiILI15FqJCjOlrKqfJiSaDq0AN+Ij6ZYpa76habdWQeqLLoUzLWcyKGSY3u+78a/eaQdJm3fwEMLFIMnvFOWFfefpfgIGtlIcH6hvVga3C5Xtwv39sib8U6DdZfmNFCMCnn/QnnuEL/r/Cy0grPZfUY2FcCZS+qyXY5cSnw== 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)(316002)(5660300002)(2616005)(6666004)(66556008)(26005)(8936002)(66946007)(6512007)(508600001)(2906002)(38100700002)(38350700002)(86362001)(1076003)(52116002)(6486002)(4326008)(83380400001)(66476007)(8676002)(186003)(6506007)(36756003)(103116003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?uVAyDoVrS2rzjnPHXcOWOidi+ZXgRqWK66Njl3PhQoVANydldeQFrRzSw78J?= =?us-ascii?Q?orVKBPq+2UO7TE9T9BCtDM4MVl7tu6TRD+iksN+Hpz5SUmsVhsGaaHIR6Xbv?= =?us-ascii?Q?O0+bov9HOYfLBEX4O3V8LugSz+JTYp2/97s1Wbp+ixLKOSWzAkWUblO4nPV+?= =?us-ascii?Q?6xnLLXX25eh6+TRQrWthV3qOplpBklzcJZmgxmyFiIhsnIL4Owv2q2YCgI+0?= =?us-ascii?Q?pgKr063+TJ3lk/O0UwIFcGL2a0SYJO/Tof7crgAtbjUQaQMUyJlUlCMsiR6m?= =?us-ascii?Q?LJkBIUudTIQ5Tv+f0d/8FGa4QbkbsrzNfFHJPnraJ7ElfiLs2MC8egI3GJez?= =?us-ascii?Q?xXgEokOqNu5m7ZAEIL5gY7JITM+pSNZlQ48XyOXYYuwJwGE4DtjBSeyCTPC/?= =?us-ascii?Q?XIRk6bRCrl/6PXY6at397AJl9YGe7HutmtAMY4xvEiOnq8cBJ5ORMIGLWomI?= =?us-ascii?Q?E0tL2SBXBC4Drvif2msim2VW3Q8k3YYvDdOet0qYO3yfF21VSDAmL8IS68T3?= =?us-ascii?Q?fvfWlrO/DVd7U4fDUdGlsLMAaJo5cR/j/paf0w/yqK+OfvAjAbsjtes8/FDL?= =?us-ascii?Q?ElGfoX98PY3Z+1rqqH4dyNtNauzN0FDK6ggviBKl4EPAMPN+RC1xp6qejMQ7?= =?us-ascii?Q?aNiLmnT11ztCUT/qb0yanH9G4yW3073I2zabvVG6qUD8wCr6cGDUCiGkoqR2?= =?us-ascii?Q?H6VZPSE5TfDfuzXKlngknez0SbmrlIxKnNjrHl2Z2UxlYISMqlSQuDpMPihB?= =?us-ascii?Q?vJHQI8aGFY97n6AgUIBDllfueo7xWGFAIRaKGgm8d6pjLgcyE50hABGYZsnn?= =?us-ascii?Q?Pjg8K8lixwNS518fGG1l8jZtAW8HApCnQOdvRN9QGNWHu/X8PfF8/SjHVCw6?= =?us-ascii?Q?/v3FFOI3hiZZ+SYOaJPocBVJrIvWlxbAMqnMnHvkolkR6wLVju4q8apS1B9C?= =?us-ascii?Q?SJxGiBcjJFg9Gaezn1LOY5IE1RgIOcSjJRAFzxo0newGhvddSk9wJfPMdy5v?= =?us-ascii?Q?NA4FvgNqxfpDv9k8zlBbBVoGiJHYJ6kUUkXCZqLxpbqMWhcyjNq+GDmhZtC4?= =?us-ascii?Q?ZazGGk/qeQc+1Xg48RCdmHgVDGCtrjVejAqKeZTqt4aeo6FP3fc9lgV/H2/h?= =?us-ascii?Q?cgbPNFVz0sb8WA7WiwPe8GSU6s/nU94CwolUUzqs8S8h3Z8s9YY344bPfdjx?= =?us-ascii?Q?wsrVMoBYsXcyqI5cJCoMc31wqWHxfzmSvW4la2kNvlydz8Qu7KmarEntwkCu?= =?us-ascii?Q?mTvJ7iGLv4IVYRggX+iCtheg0VBoeLOWun0WnsipPWWOiJc6HG/ayMyRVDWs?= =?us-ascii?Q?Ns8A99RJRSJ9qduI1kurx5vh+9ZgXsQ9m2Y8D36mtJv9jUj34CfJA6+c4LbI?= =?us-ascii?Q?gO/WRoKJMCd+N0HsJ8JD69Qo+vBgtIhbUH9/jBKxRSpLHj9OghBIdhZVVNj5?= =?us-ascii?Q?nA4gbn8UwITSkpdds9fJRyLPHoQCCGA52rVHsQF6e04WbNmMChEvlGyCf9NZ?= =?us-ascii?Q?ek02TFUr73HPwC1x/FbP0UsEqfS6t2B2FAGjFenO7qGGdwHffdyFR/3j+IlN?= =?us-ascii?Q?yOFV5dgx/WQmsAW/hhgTTzhM1eRu0hkbFsoJNYFGYegQRr8TH8Tkf1ZKG1NE?= =?us-ascii?Q?vVBFY1NKF2c5XI5900aZsb+vPWb+ZcVOBTCMTchZ0h5ikEUOA2gZpUKEZwlY?= =?us-ascii?Q?ZIohzvF6ATuxuIBvOU4TTLu4HONNGNRC3AtdabiMsmtA6xseU7TLUkUJ+RNl?= =?us-ascii?Q?wLO1HFHGMJ+a8JX/Kmr5JCme9gMnO8M=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f6d4024e-b5e4-4769-94b7-08da28db9c61 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Apr 2022 05:54:51.4810 (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: TDoYE9dTsABDrCPyXBED7M/Xo6dHmwYWj8MWOOveWt1sqi+J0uNXpyi5FL6qBrEDs1EsYffc8AIJnc326CvyFA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR10MB1789 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-04-27_04:2022-04-27,2022-04-27 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 spamscore=0 adultscore=0 mlxscore=0 bulkscore=0 suspectscore=0 malwarescore=0 mlxlogscore=994 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204280035 X-Proofpoint-GUID: vc7PMfLYo3lUbZWX0xdsNu4rgjvmpdVE X-Proofpoint-ORIG-GUID: vc7PMfLYo3lUbZWX0xdsNu4rgjvmpdVE 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 Acked-by: Tejun Heo --- fs/kernfs/file.c | 50 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 96c8493003b6..7a60074ec0a0 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -49,6 +49,22 @@ struct kernfs_open_node { =20 static LLIST_HEAD(kernfs_notify_list); =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; +} + /* * Raw deref RCU protected kn->attr.open. * The caller guarantees that @on will not vanish in the middle of this @@ -67,7 +83,7 @@ static struct kernfs_open_node *kernfs_deref_on_raw(struc= t kernfs_node *kn) static struct kernfs_open_node *kernfs_deref_on_protected(struct kernfs_no= de *kn) { return rcu_dereference_protected(kn->attr.open, - lockdep_is_held(&kernfs_open_file_mutex)); + lockdep_is_held(kernfs_open_file_mutex_ptr(kn))); } =20 /* @@ -78,7 +94,7 @@ static struct kernfs_open_node *kernfs_deref_on_protected= (struct kernfs_node *kn static struct kernfs_open_node *kernfs_check_on_protected(struct kernfs_no= de *kn) { return rcu_dereference_check(kn->attr.open, - lockdep_is_held(&kernfs_open_file_mutex)); + lockdep_is_held(kernfs_open_file_mutex_ptr(kn))); } =20 static struct kernfs_open_file *kernfs_of(struct file *file) @@ -551,19 +567,20 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, 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); on =3D kernfs_deref_on_protected(kn); =20 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 */ 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); @@ -572,7 +589,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; } @@ -594,12 +611,13 @@ static void kernfs_unlink_open_file(struct kernfs_nod= e *kn, struct kernfs_open_file *of) { struct kernfs_open_node *on; + struct mutex *mutex =3D NULL; =20 - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); =20 on =3D kernfs_deref_on_protected(kn); if (!on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return; } =20 @@ -611,7 +629,7 @@ static void kernfs_unlink_open_file(struct kernfs_node = *kn, kfree_rcu(on, rcu_head); } =20 - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); } =20 static int kernfs_fop_open(struct inode *inode, struct file *file) @@ -753,7 +771,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) { /* @@ -770,11 +788,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_unlink_open_file(kn, of); @@ -789,6 +808,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; @@ -804,10 +824,10 @@ void kernfs_drain_open_files(struct kernfs_node *kn) if (!rcu_access_pointer(kn->attr.open)) return; =20 - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); on =3D kernfs_check_on_protected(kn); if (!on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return; } =20 @@ -821,7 +841,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:02 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 E1946C433F5 for ; Thu, 28 Apr 2022 05:55:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243403AbiD1F6l (ORCPT ); Thu, 28 Apr 2022 01:58:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243319AbiD1F6Q (ORCPT ); Thu, 28 Apr 2022 01:58:16 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A17BF267 for ; Wed, 27 Apr 2022 22:55:00 -0700 (PDT) Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 23RMRBOs032176; Thu, 28 Apr 2022 05:54:57 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=aapRvOZQ687zTXBnoT4JyRmdwm8vA+JE1KLtnpL3jZ8=; b=gFXllb1u+cOMmS9EqvacHtap/V/6DblKykHlebcPCIY9zzqmxRlPhy9ykqUedvDMAQG6 HwGjdcSri48gLAcBrgIMfSho1cuR1Qrc7njmVlWXOUj2JH9VAPHifQEpxR9KlnjTVyHK Tn1gcmBTG2SxDw+NOCziOddJJmeVc4Bf4AiQp/aPM0ajmr0KL5aunjZi1cscl+W25nVv Myccvbo5zCVb1X2VrKRvRFGUO4s/Dw51n9Gx1DaZ+XGJpE/8ZVFwsAJBCs4r5Zp9VOzP NlhVcSODBQVhzIqCZiv6IsLjxKtus+pzya7nKpEvKHTQfiBS4Et7D1tr+SKgLum3b73A ug== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fmb102gr0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 28 Apr 2022 05:54:56 +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 23S5kb37033657; Thu, 28 Apr 2022 05:54:55 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam08lp2045.outbound.protection.outlook.com [104.47.74.45]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fp5yn5agh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 28 Apr 2022 05:54:55 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LiT5KMlkJ6qQ/mlhRo/5Lk4XS67cU5bL/1zlYhjimsfklsKQucLE1/WtF8p/VJlYkh8wZSBD32JLVFmwhDuNOg/GtYe1forQ1aWqwaHYdm92ecuyW090CLaJDs3z2ZZY70A7m+V7YJPilumsZAcf2dJEuAc7mEDPg64bPb++LeiFAah+xxEzLHI47uHuo31UQyjSIZOfP1mGVAi5+y3dhN7Jpqv2/76oO0yITZXh3qp9UXW2yCQQQhNaKahJmLngtieDtC2Je1b87m3GjplzMLxH1Yce38at+MejYEcS3mBX3gINQR8pMsIqQ9dkhbMTaopN4zGnp4yBBf2E7YVTHg== 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=aapRvOZQ687zTXBnoT4JyRmdwm8vA+JE1KLtnpL3jZ8=; b=U9I16kCQRTfJC/aE2Q809IMftU1/nY+nVGA5vDgnD6uEqEWIML4v+BqYMLK7Cyo+P+lJHy1PoODpkTyMPPdjKCTmEk1Dv6bHramCNDEfFkP4CoyBqk2IshZSefVWwU94dZmV9w3uNn5pVwRTaD/PTLqkgdb9ZkeP1Re8syVxLii3lmIOeXY2hHExKDlkcThjoBgrqhSLa3/dE1AUV62NR0NTNNcu5CXkkyeck7jym7tUxUcvh1o+DhEpmKiZJitH8OjsQD99Ou9s1rdV0aTB6D32xy/U2X62BnZx5Y1ygEpyKIxkl5NQXBscGlwTnsQQg3FaRstref4QTejLi5GdZQ== 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=aapRvOZQ687zTXBnoT4JyRmdwm8vA+JE1KLtnpL3jZ8=; b=tcVPJVdvDqTJ0gTrQyO8RCmcwm1rH1uMSlcCK7vMAA5xM5NaJOYLxTVZBTrM3e08zNeWOGX9dEuKvEA4JRQrBQrFhCE+BWO2kmcZBHXkzMw9hwku3BP/HZ9xx/Q8dnebyWjUAe/gzw0jZk0iO1+hWzWWYUUn2AqlsMo99+fAoKw= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by MWHPR10MB1789.namprd10.prod.outlook.com (2603:10b6:301:8::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.15; Thu, 28 Apr 2022 05:54:53 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::14ef:8202:73ba:29ec]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::14ef:8202:73ba:29ec%3]) with mapi id 15.20.5186.021; Thu, 28 Apr 2022 05:54:53 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH 5/5] kernfs: Replace global kernfs_open_file_mutex with hashed mutexes. Date: Thu, 28 Apr 2022 15:54:31 +1000 Message-Id: <20220428055431.3826852-6-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220428055431.3826852-1-imran.f.khan@oracle.com> References: <20220428055431.3826852-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0016.ausprd01.prod.outlook.com (2603:10c6:10:e8::21) 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: 6f123612-a594-4ee9-8ed8-08da28db9d6c X-MS-TrafficTypeDiagnostic: MWHPR10MB1789: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: hfe+NO0i8VCNHWxW3G72ZWz0gphf7zGjcFM6OR00gEZq5KtlTifTQscYPrUQTshO19ttspNP939hqyb375d6z6QLbyHgti3i1+ZxEDBcSu6r7uf7CsFnUroBKg/qiJ6nGbPSQQ0RMlae4id1FR2POcmrMQcaz3SDDH/fwEFjpUL6ICv74A8wNMQeSMddHVuv8wBpy1qPJSrUPpK8pGpGXaPN5GdfXA4f5PzL81HEf7PxyJm1AyG9/kBl9Yai4VIxyECr0ExcEa0HfpQfFP16smxvmcaoqF5qWSmVEZ5Kcq4J95CHnVeZnC3BYQlXYYJI9ILzv5gGG99jYuoRGsySJlxz3iqXXiCI3YvhrB6ZRHRMYpdCvEQqgVqq4e37nae7uYqY/wafl/bj9/2HzGiaO021qB8kQ924bRKjBjf8u9QkLSXLe8FZY2SoqyMxP2iVNZP8r7T2cynmFKqJAdmaTucBwVyEqGVJ56EQu//Ligh8WvuNqM+f5aowctoFt3Nb/1aO0ytOyPldgl3/4c7u2GMrOED1jPr3Sk8ASIpErCaLzbwv/nw9t8D5Ku08FFm7q5nr/rYvXr8119PbQBn2J6qZIXUK/PHQ91uD/aMGORrNG51ZDlqaUtmNKBk1N6w7yMeOMosb+OyLNj+XFwBtn7gHBAOyJxltNLaE2pT9O4jPWnBvdKV+s6nkKC1YYCjjmTIfi2ecX3BJee2mjzRMNg== 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)(316002)(5660300002)(2616005)(6666004)(66556008)(26005)(8936002)(66946007)(6512007)(508600001)(2906002)(38100700002)(38350700002)(86362001)(1076003)(52116002)(6486002)(4326008)(83380400001)(66476007)(8676002)(186003)(6506007)(36756003)(103116003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?n7eHkxn7bETBz7kfWa4BDgyL1loq2kLVtdDOjWESEVG/CFSL/0MjIHT6hBdA?= =?us-ascii?Q?/JzC0ZYUfGQS6upp9hYATO9Iz6Wi20+Jjt7G6cXeTOPr1X6/T+2vu6nVZPPq?= =?us-ascii?Q?40eh9PX6dRgyFiCjUpjD+tgeJm3UrdhsJ9PRyy5ckrieVboA/v1Y2gwx4ccT?= =?us-ascii?Q?VkMAT2lj2qgOHUvMgG+nvQ/75egfFB96wGyT82G19XNjO8PWjMz/ACRZ7GKk?= =?us-ascii?Q?4K5sHXPsbBVpt6Dtla2IEMLvVUBSp4IY0iXwL/naBYT4FuklMi0sZ+eyGI+6?= =?us-ascii?Q?nSa0q3jFe7BtgOl9OQkPd3nP0TZCVdmpp1UobVKCgP87RfCvhhxxdk5KODhC?= =?us-ascii?Q?TQ1SXTkyMVihzJzFRNO0rcw5fAlbFM9a/A2TMmraqBlzy6RXlfjmFbP6oXSd?= =?us-ascii?Q?qnGEhCJ57LOEpS8W1f4EXkN80ADFInik+ARQ2QnVUQ+z77K5+XMRRz3LuwQb?= =?us-ascii?Q?btEv3xh2Y06/ihDc2xS6/6H9tM7wGn1WHc55kYGJyekImliLStKxm7AkeQ8l?= =?us-ascii?Q?iPpuqSZ2VXRbLKwWdLiSOp3i0hr+Mw9hMnnDUrciIbMBrYvJUtWwEN5EgUnM?= =?us-ascii?Q?4fr+oXx3SRH7StQqbTjD3y9kbvvOlsq77U7nSbiAMQGbznYbwt1kF8VJ1h1/?= =?us-ascii?Q?SoOyrLTjmPBFEU/LIp1wJpRLKEALFdwdR/8apL6aw3Ak0bikBDe+kp5Ckyyy?= =?us-ascii?Q?Cc3dHab1S/3R6KxiMHk2U7AUzEYVaYo2mjNSXU4j4ChMittjwhsu3JWDds3E?= =?us-ascii?Q?4mJH207PiL27ntulAvSP6pDubObKvWGreX2AgbgO5j6SD8GFTRHZSfu2Oaq/?= =?us-ascii?Q?X60UtR3KpQ+jS1c0kAAkiIyh5v6QAAEONJ1c2XmBHWnQmQdW4dqXBGRW+HNi?= =?us-ascii?Q?EXFlYU3kQu6kB3YkgvDkAgTL7aWelwlOQp0TG0JMY6fS7dX9NG4r/Uy1/Bti?= =?us-ascii?Q?Lu9eEwN053r0DBN1cWWJXCxmmaiTpRqIqf2PdX2AmfrY+BScG9Dce7RsgBbM?= =?us-ascii?Q?XGJjB689DcP1O/xepAK+l9RXh2Zf2U4TqpHu1zkbO4gdny9oBrRZhUup0ibY?= =?us-ascii?Q?wyqC6gXwmRAU8OKgKZlQhiDbUesQPcDmnBmyGvfAC6GttZg54YtDlPdufgJK?= =?us-ascii?Q?TI1jY0qamVwTNFl32g5JsWt64qUz79r5k5+qOWPg9iSI0VNUUr9UV64kbi2l?= =?us-ascii?Q?UvpUpLrbdtVRdxLNb4OxhdjCaCrsBefaSbBmJEWfx7ElSaaGLGYBtOiZJ8kZ?= =?us-ascii?Q?IvcNik50DtSG2t1FOBZuo9UaUtZKizfx0mJRY70fQ90vpmfBAhHawgh18B7l?= =?us-ascii?Q?PXD9bAiV4LgY7wWLkf+vRq2IEjiJbjOQkAX2nvJh/TuYRvrii0nziiYUpDvv?= =?us-ascii?Q?WEDcaxo8kgnctqMUJYRXyUbc2TL7exMp31jjUx/AGjFzuF81JKxaKXiubZ4e?= =?us-ascii?Q?GJe4s74lKcmYbAFmh405JX4/Lfem6ghE6KK+PBj69tGRJXc94aZXHb+5AtGt?= =?us-ascii?Q?TAk+8QEo0ESGUYeJ8KVjtS0zuT4CtwxUFlw9sOutWvMQUfb5ZwOGp/oYJ8E9?= =?us-ascii?Q?4vwtTeZH2tlpTdPvMZeT9rhicK481PDu3FP1aHKly+kpFXcGmwqIhurRDLqd?= =?us-ascii?Q?d3qsrutZQGzGzDYDHBZc0aWJT9B4nimQewLXVoB36JN5i8yS8zb8HrM0M9Vt?= =?us-ascii?Q?tFGzyk+EDggCoV7vCgkVHMrpmBCjstyhz31SyaAX88c9cxBm35IMIncIiu4F?= =?us-ascii?Q?Ambl1l7DNs7LV0hoEtMJ/+IDD88fI98=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6f123612-a594-4ee9-8ed8-08da28db9d6c X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Apr 2022 05:54:53.3102 (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: AZTz7n/2cpwjHaxGBelEo/gJifgZfA7+6h8ZZiWheL7Gvt0pysC5cCu5rsraeiFA9Cr0MQyXj/RnDrIUzKQSlA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR10MB1789 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-04-27_04:2022-04-27,2022-04-27 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 mlxlogscore=999 mlxscore=0 suspectscore=0 malwarescore=0 spamscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204280035 X-Proofpoint-ORIG-GUID: ivmp_A-DIYe3L4wpzsfyGgoCDm4ckYSg X-Proofpoint-GUID: ivmp_A-DIYe3L4wpzsfyGgoCDm4ckYSg 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 Acked-by: Tejun Heo --- fs/kernfs/file.c | 17 ++--------- fs/kernfs/kernfs-internal.h | 4 +++ fs/kernfs/mount.c | 19 +++++++++++++ include/linux/kernfs.h | 57 +++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 14 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 7a60074ec0a0..946a4a8d7e32 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -18,19 +18,6 @@ =20 #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_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; @@ -51,7 +38,9 @@ static LLIST_HEAD(kernfs_notify_list); =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]; } =20 static inline struct mutex *kernfs_open_file_mutex_lock(struct kernfs_node= *kn) diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index eeaa779b929c..3ae214d02d44 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -164,4 +164,8 @@ 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; #endif /* __KERNFS_INTERNAL_H */ diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index cfa79715fc1a..d0859f72d2d6 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,22 @@ void kernfs_kill_sb(struct super_block *sb) kfree(info); } =20 +static void __init kernfs_mutex_init(void) +{ + int count; + + for (count =3D 0; count < NR_KERNFS_LOCKS; count++) + mutex_init(&kernfs_locks->open_file_mutex[count]); +} + +static void __init kernfs_lock_init(void) +{ + kernfs_locks =3D kmalloc(sizeof(struct kernfs_global_locks), GFP_KERNEL); + WARN_ON(!kernfs_locks); + + kernfs_mutex_init(); +} + void __init kernfs_init(void) { kernfs_node_cache =3D kmem_cache_create("kernfs_node_cache", @@ -397,4 +414,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..13e703f615f7 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,62 @@ 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_global_locks.open_file_mutex[i]. + * + * To reduce possible contention in sysfs access, arising due to single + * locks, use an array of locks (e.g. open_file_mutex) and use kernfs_node + * object address as hash keys to get the index of these locks. + * + * Hashed mutexes are safe to use here because operations using these don't + * rely on global exclusion. + * + * In future we intend to replace other global locks with hashed ones as w= ell. + * kernfs_global_locks acts as a holder for all such hash tables. + */ +struct kernfs_global_locks { + struct mutex open_file_mutex[NR_KERNFS_LOCKS]; +}; + enum kernfs_node_type { KERNFS_DIR =3D 0x0001, KERNFS_FILE =3D 0x0002, --=20 2.30.2