From nobody Mon Apr 27 14:54:10 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 EC9AFC43334 for ; Tue, 14 Jun 2022 07:04:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352803AbiFNHES (ORCPT ); Tue, 14 Jun 2022 03:04:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243010AbiFNHEL (ORCPT ); Tue, 14 Jun 2022 03:04:11 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0F44220DD for ; Tue, 14 Jun 2022 00:04:08 -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 25E72N5I004107; Tue, 14 Jun 2022 07:04:04 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=SnFRT+9RBSdsMbWgtWy7GtR7gaB+2Xl3FfnHME5XIXA=; b=0oJ2Ef5sQ3yuYf4xofhwR4EhH/q+uT6+P3ajeh0l+p8/vq2khUBKrKE29dZi//QvlCOK IXu7d++vk/gyDsIbUGPdINQ9u/m3fuLJcpQ+i+kbX6xCu19xUJ4b0RpwbAMH8ZPlnYwX ojmGVYJweShY6511Ko7UK0dYM5u8YjsAU7c4kOOmIN9QGAZ/B5SnU8Y22URrZR46zXMC Nmm1nEl6nLXSX6RlVKgcLG0bhdsvC23rT7ZbVi5y+9nxKR0jhBKLbeyUdPCwVfn6NFEN B9JzcLT7SEWcj8pTGZcT4vXP00Ouk70/sHGaV8u8hKQMY2oSoaayEwX522YIxcKUskKq Tw== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3gmhu2n1cn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Jun 2022 07:04:03 +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 25E6tXIf012502; Tue, 14 Jun 2022 07:04:03 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2104.outbound.protection.outlook.com [104.47.55.104]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3gpn7j8phd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Jun 2022 07:04:02 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EZVIFFetpJhf17qiPnGYkNKJyEelqyM6xJFxjcUm8Ipw+UNIm47Bs+IqFoCC/y/jOCbFHxLMuXXSv7Zrh+oryfsaqaInlljx6pwK12EWfnWumn4D3r91kbi9nkeiJQqqdi6d/5R5BydC1gc8pAoMrG9tbMwbplycePm8XcHY1KsLherjfbKVCuzIoFk1aqCSwoe5z7fTUe+e8dQ7iKYYePAtBxALv5SHbxAfmzgOaL18yGJMFx9wt1cOO4OKp66rv9NOyI8qGpebz5TUsF1HEwIQkOr3Jxe8y2x4mXsJmJVbtvrbTqxhk8C3ECH6KSffuHm4DFwGscf0cVa3zkPb9Q== 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=SnFRT+9RBSdsMbWgtWy7GtR7gaB+2Xl3FfnHME5XIXA=; b=ZcLd7E5mDljHrmTonoQhLZireT6Cvqp3PJCvr9fNfnqrfhf0O8qbB6Dzq40b1wLVe6bItFLHptGtHmzkmDytpeZvhol9zzEy15iyP0CgOIPTpqA4db2p2EMu+MoFldhkxdilMjsXzIbLVejNxIn3bx2qAAaK+qs8GYjNB31JrMMoICsA1Uzbsfm0dH7kBIQndq/yFeamBOF3Ag5t5meBINyUrL9c7e74EE67WPnbGy0qPsJP+e4QYQDRmeIqRLa1mVdW9wUUl5acylz7I8oDp7b6L11Ccen6ekJULuVvfF+rF/vX+ZkXgyYNSlksH5AguygZTimeyunEdDC/NVkfmw== 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=SnFRT+9RBSdsMbWgtWy7GtR7gaB+2Xl3FfnHME5XIXA=; b=JVIcHXF62c8LYCFnDwnhE8sVzEGfmMmDCy6jVwfU3r4/1YpqALEw7bW8mnsF2EmLF4vH0LM+D/tbXavfHcS7yfuHpODsZcuG/AgqFmjtfhvaXL0lOZdePz8lP6pr++CmcaXTVD5jsSVT/Onr5iXeRVWFan0AYeN+curCjWgY+hc= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by DM8PR10MB5414.namprd10.prod.outlook.com (2603:10b6:8:32::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Tue, 14 Jun 2022 07:04:00 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0%9]) with mapi id 15.20.5332.022; Tue, 14 Jun 2022 07:04:00 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v6 1/4] kernfs: make ->attr.open RCU protected. Date: Tue, 14 Jun 2022 17:03:43 +1000 Message-Id: <20220614070346.810168-2-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220614070346.810168-1-imran.f.khan@oracle.com> References: <20220614070346.810168-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY2PR01CA0036.ausprd01.prod.outlook.com (2603:10c6:1:15::24) 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: a5340e65-dd80-475d-1ae5-08da4dd40edd X-MS-TrafficTypeDiagnostic: DM8PR10MB5414: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: kIhc8x2FsPEYimBcNnSZkG4VUYvMb5wssbHdZ+hDpB7HvbtI2ZBN2nZNIfztC+TTAq76/u024RmxqeS9qN6suaeV3TVVBHpJ4NHQlDPDqcJt/DhYCoI2Y9MtyxBz2hWEwQyOoPHREkpiL+nY3IcnaMNd8MOwO5DUifvPpLGn0zRLKiCw2ZNgoMudHUrpY/CLj4OdF7TlWTeGsYHxGo+eDhiTPP4GpIC3QldTiNWyhEa9fo42bEr7RnIhzRATNFU2njuaPizbrNzDPZGoR5CIMgw7D7+PmVVChkWPOAjjYZcb31vLR3uEITI88Tjd6K1tfndJBGBCCK5yw+XshGOoSxK6rWQJBaLYYZBPiflUSQ7dbhxTwXzfAi7QfqcZp9g6PkOu6JlQg+oqEZ+boPxXoa8kbfFS14FIb+MJgxWmeXrseKAGu9m45wGnzLtZs5SqeCMj6IOe/MJ61fwpPC7byuQMdHEkod6wfuQ0My6AsKA7D7cPZKn3PnMhEYo4uK8lFcF435j0oT09h0aB903cJTwYZPXVAXxxF3n9umUTJGbuF5/fuWwk4Ivoa9RJEkwieS0BRA0zcKczh21iR8HlNo6ZG/gF25+9JxwA5oMHyX34eSJB1Mxj/vTFdZItsveu8CcNPD5+7lxuV1nUUC0SoBHfX5AQ4/+/rcN3iVtatas5VlmNS0FISw5EMPMcJjdeDhrm33Ne0neGqAAsk2c12w== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4468.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(366004)(6486002)(186003)(2906002)(8936002)(38350700002)(6506007)(83380400001)(103116003)(508600001)(4326008)(1076003)(316002)(66476007)(6666004)(38100700002)(26005)(6512007)(52116002)(5660300002)(66946007)(66556008)(36756003)(8676002)(86362001)(2616005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?LIJ9yOyU9OmqMFtp3Xq+oaAaK10WuTlKsN9vi+Rte9L1QLVyjd3m0GM5jCYt?= =?us-ascii?Q?lK+KERHpJokkdMhxgH63Q3N1iwxtp2Zb6T14//lsS3mXM4NYqtALa9CcJsLM?= =?us-ascii?Q?mPSRaKS0FeOgJbQ2Pxl3NwsHcNuacmTD+bylkq5kJqWC5y13w0AS7UrwNJ75?= =?us-ascii?Q?efpYJADC4NSjBgvhMDZWF7yQ/dYPTiMi98WDTDJNa//xeMSGxdf9cRg547k5?= =?us-ascii?Q?wb4RW5QJp/FXIllqAyYyzbhvMNIL0edILCqjur7moBjCV8DxlSQdLbknGWBq?= =?us-ascii?Q?Q2qEAclC1eLwWNo9KsCUJroRi2WJr2jAXZr+7j1+e04wgsyTiP5DJ6Ez+fO1?= =?us-ascii?Q?Z84RDzGaNiweECyaYet7d4jCWPd0pC14hBm3M/F5FFu+SvhVBYvahOb/Bip5?= =?us-ascii?Q?sHcm5nOfFI9csFvcrlvOZCuCvBzj966vyaRv81Od/0LkkKs5ObM1vFL8rezc?= =?us-ascii?Q?zNl/vx8tcSkt3YG3SDu+jHpSQGOt6NDHX4QTbemx4YP+O9t2BsCP7AaNluon?= =?us-ascii?Q?Rkml7GbfU8/UKhQWLsXOJD3iLskmg1B2e3Kwqag8Hp8h/LFXf5GcTivP4zrr?= =?us-ascii?Q?6eMjnXj8WlVmXaE8Fttv3zR1bar6XEGeZ9xPKQ+k5Mqa6v3Q7I9/b0NfnoG2?= =?us-ascii?Q?UT3tgxwA5SlPwr8ZhVd1VA9vGEX6X5qQJ/dG79JkJsVpqDdnUIRpPrcVWHKS?= =?us-ascii?Q?1EbGH/k3Np1yR6SlWdcbdpGjcgHXbZyM6i/p4V90SYeJWxD6rl8Yi65G61ds?= =?us-ascii?Q?UPYPhy6lZjUsk0Z2g+I1fAgC8cberj/wdUQdzvv9eYxRWnqGBPlZfLuElU5y?= =?us-ascii?Q?3JvpbtLFY3BgczrmJ53G9ssb3ej9PCfYrMv/7YKdMpOVYxZXJR6TNRmfP2cm?= =?us-ascii?Q?Xiz3zqKTLwGv0GDBN/+ZPNxgn+zsMoO1OF4w+mO0+XPvo6/NTT4ehzm4V22m?= =?us-ascii?Q?7gpSuJ9m7E71VzYJVeb84iHXtX0IWdPPMNGZ+U1vjrR99i+hxzd4L5uuEy+Z?= =?us-ascii?Q?CB2ja5WxsKUJMPL1GazswG9iWoqxa55FPDFg0I+IxJV+IVSeCDKIM0znt5yg?= =?us-ascii?Q?/UckYHnQVpKqpSZ7ijZGSBiBGrBeQq1vfMoRhvZjmy0/uhLewQq1RX+1mwMU?= =?us-ascii?Q?XovGwKLq+txyt2cb9jrAqsUgXel1uP3nA75Ral5AwlIuV8yNvRWZPszgJqGq?= =?us-ascii?Q?KD66bzcwfHiH4qfZIrsx1Zf4MfrSA5mP0cbLNtuaYOtU+WVwJD3B4cGJdPoe?= =?us-ascii?Q?lRtCxx6nzWapYu+m/gA6tsQJ5dFbOkhBx73cdnqyHwFxQp8tKMiSSGs/9HFm?= =?us-ascii?Q?Bjf07s8pAQbDeS/2he4Bh+xPSh6wKws2TqK5ADk6ViVFKQZobg/l1OELE9rE?= =?us-ascii?Q?4g63txNUmFaPwvFaynvc7tnajdCukF5Zk1HfQQ6NefeVWB2pC6Uw+79yiJb+?= =?us-ascii?Q?GJ9IN8n+Sea4oQuY/vH3lf9/Goj+G0HdpOUuk/VV/BLPb099cz+4ODh684qg?= =?us-ascii?Q?UcTQigIQaCntXOEqLp7XE1LmMd8mHn+HMPmThfqWRd27fzLBL8Sz0rpCerc2?= =?us-ascii?Q?CdSyHCF/kb3tRITLezUYY/7CTyqS12e9HnZx0NJyCkEW75BxDcYG48rE8rNK?= =?us-ascii?Q?HlrLOoRDBv7HeY6nJ4g3hiSL7SQs5qbcVscl45/ugTXcxQcSyPBsTh8Z0Hna?= =?us-ascii?Q?D9GdOTgiI3uDP7SY2PuPOMS93DZ8v7Dth/lY9ZCKGFmk+ZhIXt8IQQjULiXL?= =?us-ascii?Q?4aRjZ8FgURLiB8/946V8lIhmDoSCjWk=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: a5340e65-dd80-475d-1ae5-08da4dd40edd X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jun 2022 07:04:00.5530 (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: /p67+Fcoh5zE+qqKB9sPjOMxRq/sH+FdBRbFSDSBTGO1TDjNVZN4gPQVVUIKOwdDQCvDueW8uX/tlHM6nTQ0aA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM8PR10MB5414 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.874 definitions=2022-06-14_02:2022-06-13,2022-06-14 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 spamscore=0 adultscore=0 mlxscore=0 suspectscore=0 mlxlogscore=999 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206140026 X-Proofpoint-GUID: Qdg1eKIfwdyxj-9NBF1D0n9THYVUe95U X-Proofpoint-ORIG-GUID: Qdg1eKIfwdyxj-9NBF1D0n9THYVUe95U 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 | 147 ++++++++++++++++++++++++++++------------- include/linux/kernfs.h | 2 +- 2 files changed, 102 insertions(+), 47 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index e3abfa843879c..da70f00a59c17 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,52 @@ struct kernfs_open_node { static DEFINE_SPINLOCK(kernfs_notify_lock); static struct kernfs_node *kernfs_notify_list =3D KERNFS_NOTIFY_EOL; =20 +/** + * kernfs_deref_open_node - Get kernfs_open_node corresponding to @kn. + * + * @of: associated kernfs_open_file instance. + * @kn: target kernfs_node. + * + * Fetch and return ->attr.open of @kn if @of->list is non empty. + * If @of->list is not empty we can safely assume that @of is on + * @kn->attr.open->files list and this guarantees that @kn->attr.open + * will not vanish i.e. dereferencing outside RCU read-side critical + * section is safe here. + * + * The caller needs to make sure that @of->list is not empty. + */ +static struct kernfs_open_node * +kernfs_deref_open_node(struct kernfs_open_file *of, struct kernfs_node *kn) +{ + struct kernfs_open_node *on; + + on =3D rcu_dereference_check(kn->attr.open, !list_empty(&of->list)); + + return on; +} + +/** + * kernfs_deref_open_node_protected - Get kernfs_open_node corresponding t= o @kn + * + * @kn: target kernfs_node. + * + * Fetch and return ->attr.open of @kn when caller holds the + * kernfs_open_file_mutex. + * + * Update of ->attr.open happens under kernfs_open_file_mutex. So when + * the caller guarantees that this mutex is being held, other updaters can= 't + * change ->attr.open and this means that we can safely deref ->attr.open + * outside RCU read-side critical section. + * + * The caller needs to make sure that kernfs_open_file_mutex is held. + */ +static struct kernfs_open_node * +kernfs_deref_open_node_protected(struct kernfs_node *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 +202,12 @@ 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_open_node(of, of->kn); =20 - of->event =3D atomic_read(&of->kn->attr.open->event); + if (!on) + return -EINVAL; + + of->event =3D atomic_read(&on->event); =20 return of->kn->attr.ops->seq_show(sf, v); } @@ -180,6 +230,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 +252,15 @@ static ssize_t kernfs_file_read_iter(struct kiocb *ioc= b, struct iov_iter *iter) goto out_free; } =20 - of->event =3D atomic_read(&of->kn->attr.open->event); + on =3D kernfs_deref_open_node(of, of->kn); + if (!on) { + len =3D -EINVAL; + mutex_unlock(&of->mutex); + goto out_free; + } + + 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 +578,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_open_node_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 +619,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_open_node_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 +827,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_deref_open_node_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 +867,10 @@ 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_open_node(of, kn); + + if (!on) + return EPOLLERR; =20 poll_wait(of->file, &on->poll, wait); =20 @@ -922,13 +977,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 e2ae15a6225e8..13f54f078a52a 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 Mon Apr 27 14:54:10 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 13300C43334 for ; Tue, 14 Jun 2022 07:04:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350054AbiFNHE1 (ORCPT ); Tue, 14 Jun 2022 03:04:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244778AbiFNHEM (ORCPT ); Tue, 14 Jun 2022 03:04:12 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78B4227FF6 for ; Tue, 14 Jun 2022 00:04:09 -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 25E1MonJ025280; Tue, 14 Jun 2022 07:04:05 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=btQThpfR7zsWYmnPqUVcJdnmvjwhHEoj9zDkG06cBeY=; b=RvsJtUyiRVBTmgBKCXP5CVhYmyzbiGILxHda8BJspnuGLSlng3EcM8b0ZETFCvdsy1UD dV+QacyQetA5ja68f+ABloNA+gwX2x/lK78lwYTPjt2D0CKIRoopZfHm7V3/eJdlViaE 2+1dolQa2iHwD3ZjAbcDgkffUltPogExTtEqvY6Uywxyjjl0joH4FPaP1hqqHtazWkTv ckfDFsynVsRIiYxa42WDfDhnAguyMRRuWTWaU+LVsP3sB/i7MYiztouyY3eev6pSL1AR kr1flVMR0OQII+TnX6O2ITBcsf+HD4Bw7O8EN5/tNtRf0YLIDZo1IN5BfhmFQ4Pj2oQh RA== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3gmhn0d0ff-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Jun 2022 07:04:05 +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 25E6tXIh012502; Tue, 14 Jun 2022 07:04:03 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2104.outbound.protection.outlook.com [104.47.55.104]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3gpn7j8phd-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Jun 2022 07:04:03 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Lqpun1h/5a6OSxueq+kMwIS5TnSb6imS7KfmnB9frIcJBZB95iwSHrxUsi6le+nhOGLymts1kY+i8osHTuht5VCYWixc6xWmj6VE7Liwz/flzXsjB6WsH6fQiUNHANTlprc3lbxsFhrTTPvKeKa+zZcUq0+LQPG7QZGeumROhhrk1kuypuJEdU95iFNSDcv2B6Sa2Zco9ToWCtCYnFTb5s7skYxfbRMXAlt1NoUblP5QJ18HDwswpc46Hz2nVT7imI2oWTbi0BHnjmsgL678x9KuxQFTyeoYjV3DKyrIuwkODS7juwAlCW9ksYbH73WOIUxSZOLfz3uwenU52U08nQ== 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=btQThpfR7zsWYmnPqUVcJdnmvjwhHEoj9zDkG06cBeY=; b=XjanbJZgywhMw43fCccAtwzbPvpB2H/gMR7k2lhkbBL5ftC3F+GsS8nMEYLjlsTeSISrorvdgUrU8+qub+bpkIuQWZADC2dMFUYhF+iS91DaY9NwRSb37a9RCQVDmrWXoGwHcRHLQ3clnfhOMf1NzgyUsXIjyomlLAtEDau/2qEtzmZa14AliCET4sulGQBGRy54Bh87jSYBg9m4h+n2fXa5hqQCCY4G4FY4SM3x1neOlfxfMlvYBc89uQPwG6vVlOQ8Azn1tyujzOLcnLYkM/4uK7cRiVDM9oJLP12ukPP0m/Wwz7X4dRuIS8QPQ53S2pyCMhUF7skuuQdOFNi9ug== 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=btQThpfR7zsWYmnPqUVcJdnmvjwhHEoj9zDkG06cBeY=; b=m1M17dx78NvQ558ozTJq44cqG1+/gYb0u8pBY9jkWEeCJLlASiD9eRk+A1nvEaO27re/QOfPfWy0kthaKDubBcJBR7aWXEqJcThP0QSc9vxm5+bxk2uVnkkM1WpJH9Bs9o5cNrgXXQHQNm4ZLwWhZG02vET96hpXgfhpoyqNKnY= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by DM8PR10MB5414.namprd10.prod.outlook.com (2603:10b6:8:32::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Tue, 14 Jun 2022 07:04:02 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0%9]) with mapi id 15.20.5332.022; Tue, 14 Jun 2022 07:04:02 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v6 2/4] kernfs: Change kernfs_notify_list to llist. Date: Tue, 14 Jun 2022 17:03:44 +1000 Message-Id: <20220614070346.810168-3-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220614070346.810168-1-imran.f.khan@oracle.com> References: <20220614070346.810168-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY2PR01CA0036.ausprd01.prod.outlook.com (2603:10c6:1:15::24) 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: fcd2e510-a5cf-4f33-65fd-08da4dd40fd7 X-MS-TrafficTypeDiagnostic: DM8PR10MB5414: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: h9Qe4RUAm2UqwEsRDBtiv+JsumSRzwh904EKNGjMOuvQMTFjlRx1ZzXE+V6agoDssJco/9nrApoOD03ficpj6Ft5KtrTrwo4MBvZX6yCi3pwVcm7F2LACejjX966zWgtMICJrDYVf0KqK4rdea/9NgAV0jZWSZONbOkxSLCN+zw09aLnYxTJBTOxSCOY40lIr75cEWuNP9y52JQ0fcjQVMwgfl5BWmjnLvWaGPv5qrpOdiDEu0ETqyVYcSTlbuAD4S35E+Emq7mnky0fdL05txNxqxfMcEEDcCglHTUnSEoUmrzvBk6YGVyFAeCZ1S8kJDzeu3s+lV4qm8vbJQisUQnSUilptfA1OCzuvk6wnp1k6OmOh0IWCGqJ+DUv9eb/Ov3DIUrj/Y8z+LEcZYUhJ026qIza8tBg2wLYHME99jbiQdfbmR0H6xsuz6iiWuR0C47expna55ZguQvQsACIGc16TnhVD613FKHKa4ADguOPzRtGuc2W+uDIR/e3/ek3dgwCrh40ECBcdinYuruOiy4y+hUcy1ujBm0ixfJZl/IdRLMNJW+UxJxEn9SnKSA2GLQtw9gMh3d5wLUSeJex+eHygGTFQy1hD80h2aA4cd4OXDLz194ky2xnEUSpQTLRUnJWjpPHoAt4RczJRBJUqzSJPFcY03ker5XVksNzjxYcWu+rBMBD5DqGpK32u2wz9HBTgOW8s/LNRNxULkoSTA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4468.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(366004)(6486002)(186003)(2906002)(8936002)(38350700002)(6506007)(83380400001)(103116003)(508600001)(4326008)(1076003)(316002)(66476007)(6666004)(38100700002)(26005)(6512007)(52116002)(5660300002)(66946007)(66556008)(36756003)(8676002)(86362001)(2616005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?TCE1BIHR5EClhxY3dotPW6eOPaRrqoJ8o2vqz5LqXWQGfci39u3Rclhe7efg?= =?us-ascii?Q?Mph+jISHAjuDebSzmsU8GffeaB1L+6cS6dl2Ypqaiie1u8VoZozUMdJg+4EO?= =?us-ascii?Q?r7o58YTuVjvYkA8aRNfDjvOwqHfw+mk6GFel9QYtsY9wUwwxLm6BqFspNRhU?= =?us-ascii?Q?nkpI2YolhhYep8sQqzSbHO73L40IQ0oCn5AUSKTv40cjf7l0tBGYgKiAjhPc?= =?us-ascii?Q?zgPTNAqQGWsPROOyeyxRL3zcg7Z+LprfHfyW1FCmRbPIdOmM3SuM7QztvZdp?= =?us-ascii?Q?g9+jf1HQ0xIzOmbQ1kTRMXO2ojcFQdQ3zWssE+TRLaNWvuTYVswYJCZvsGgj?= =?us-ascii?Q?4iTjRn7flnpKvZ1jpAi3lCxUQVKZwfayaRtn8jPgNxCeY3pHdKx4Y9BzOX2h?= =?us-ascii?Q?q3TxhkvIkHVhbMdZYDjeDJNoI/4DGxhdyNCGw6YxA/rhoM03D9GcWTjpGZCb?= =?us-ascii?Q?vTwiVHxh5CTLD1MXjyO4AfG4q449a+qBFI7IteR3L2Xne+yeTe12XTjcUSkB?= =?us-ascii?Q?Itg/fGLl2p/no/DZQvjXO2MHgkGqfQaPLypdVgWFMJYdXuWTioYh/cE+Lqa/?= =?us-ascii?Q?y/3Sxg25p/U7l/P9MncKdQ3MzlmWburhqx2DLpuUN8dXPS5F5pVCVBiMWCW/?= =?us-ascii?Q?vflyade4XbRsVOHq65aJbvrp1onr+pgHQBC8JLGDmNwjueM9JIw7vZPy62iA?= =?us-ascii?Q?CHklJzeBgSq8idZMZPy5GmEFBXQBj1arBqQDXiqPV8BkYP1cHzGZV0y99dWS?= =?us-ascii?Q?SJDCBO6EqvZwyNsjvcm8M+CuYs2KCKGyr4r8hWP3jKVGW+1Masn61/JJ9BEP?= =?us-ascii?Q?hOlsTonxnOe/k5i1nLOIKBl68wUaltHZzuGliEcmOznLUZhuSC7C/HQZ1LxD?= =?us-ascii?Q?Eins3QfbmJFoxf9aH4AjIkpPhNzbjYOzQBjupyR9nNXkdtoaHuFFuXTreZWF?= =?us-ascii?Q?/2LV9Q0QhtZIvV4xx1SmIRiR63AeQLugxxdF313KQ31+WxGzy203wg7TMv3r?= =?us-ascii?Q?qw69lPbeLluIhzEOoUKCCI0JAND+fKkg5wS59w9Nr1hQlc9m4vHuyz5vC7Kr?= =?us-ascii?Q?C02WQQbb0bmaeyKqxsBVgxudlvCxDmIE1J/KaJGJojDNBOEhgPCZHl+8R5Ns?= =?us-ascii?Q?tKNBkX+vCR8XV0fv8RLlMWZRD9sIN8fxqLZyezeroplhxmf9bnr2cjAh3HZB?= =?us-ascii?Q?bQGgHlDK0FOCHd5GJ63eqom8h/3rjCUqyKmwBb4ga0xFfamxBE5bngo5xbr+?= =?us-ascii?Q?gTtCtgYvAsYt8mtM4g3qrIeTBytUZelPoI9NVraBSVYuBz9PamCmd7yDmD2G?= =?us-ascii?Q?9weDtIgYcw1OUvo02cbYh6jhg9Khn/PimdFjGmn+r+ZH3Xuqg/3on6buKjAu?= =?us-ascii?Q?7lU33K2dKIUGzLmqfKLqb0a3/zBzUBXraQinp2NTuNHTfIcz/bM33as3imqY?= =?us-ascii?Q?cBU2hdGiKWKRjNd5GYWt/mEOyiY7bu251eOxE/GTFqHvQhPQcp9eTg3f+iSy?= =?us-ascii?Q?nT83fWOwSNrT3OGzgmN9SYjzDo4Tw28frvW6cmVNLb38fH9GxqKXQBaFCze7?= =?us-ascii?Q?ujOy13PDw5FCWhlkYqbiPYA6ahKiNDFz2c0RyHstsw9mPrP+M8eNR4WZNqwV?= =?us-ascii?Q?7PAaDG2xWXgmnSo3iUAu+AwQcdaRqgLnm3Ep7LGpJeu79EeV4uOc2+r+qIUZ?= =?us-ascii?Q?YLYKYvHI35F5cczGgsQ3wkvGFpQTt3EwgZgP8iGra7vNBQtpQRzMBaN2nSGr?= =?us-ascii?Q?xJch8/0RB72agx+YNoyMnA5UYvYUJss=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: fcd2e510-a5cf-4f33-65fd-08da4dd40fd7 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jun 2022 07:04:02.1932 (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: 1UfKuSYMec/z+d9YYW4r81QImWSheljNKqi/jijhGyAk5NGAADnM+obc2ZXvTW1RmXSNSEj54ZJIOCuSyv01ZA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM8PR10MB5414 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.874 definitions=2022-06-14_02:2022-06-13,2022-06-14 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 spamscore=0 adultscore=0 mlxscore=0 suspectscore=0 mlxlogscore=999 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206140026 X-Proofpoint-GUID: RoLB98GU4eylunY7S6Wclguv99Ww2gss X-Proofpoint-ORIG-GUID: RoLB98GU4eylunY7S6Wclguv99Ww2gss 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. 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 da70f00a59c17..77aeb0b6f992b 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 /** * kernfs_deref_open_node - Get kernfs_open_node corresponding to @kn. @@ -903,18 +901,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); @@ -970,12 +966,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); @@ -986,14 +984,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 13f54f078a52a..2dd9c8df0f4f6 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 Mon Apr 27 14:54:10 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 B074DC43334 for ; Tue, 14 Jun 2022 07:04:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352847AbiFNHEd (ORCPT ); Tue, 14 Jun 2022 03:04:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348730AbiFNHEN (ORCPT ); Tue, 14 Jun 2022 03:04:13 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51782252B0 for ; Tue, 14 Jun 2022 00:04:10 -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 25E1CYSC025268; Tue, 14 Jun 2022 07:04:06 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=gdHLfjg4pPkAcdiHPFHHk4jqZpKS6AtEQrF+Jm8La/8=; b=WU1Ed4X79NwmbiCO4zwcHwvCE3zMzllBeKtGelwU5dxeS1JhLirfH2GNo/wyIvHgu1PY zgjMZ+2RQ3aPvOTXBQ0WEARdyX4lHbAM/a5QVRV+/GWZZQ06VBCr/Bj10e7bdBmS6bA4 dY4Ydh0VaYsmxq9ZkgEPMbkC3Dq1T5CXXGaGLYJQjHxb+Z5qAI2jOWzp33fEe0HxCLMs Jv4ydAIqqWZZiZXv+uFubxSSEOV2uaWRpyQNlKSHYImj0/U8CAbE062thpl+rsdeb30p rTalhQ++7o645zwD33eWDgGuKA86L1fX6EzKDcR850Ixw9w79QtOWxE5FZLiA3X5bYi4 gQ== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3gmhn0d0fg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Jun 2022 07:04:06 +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 25E6uEZX028696; Tue, 14 Jun 2022 07:04:05 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2105.outbound.protection.outlook.com [104.47.55.105]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3gpngj89ax-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Jun 2022 07:04:05 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=d4zswYycEmBZepZP+fLvz2QXJ8qTSg3/QF+TJFTT2bCSfxzjnEdNfmjucULyp1+5lTeIyvkuDk7K9+x6cr3clfzyZAhYxhebRi/w/lc3CcmV7PdXy53WAY2ZR9M53qiTTu91wA2AZ9mqTVyHso6KHh8NXi4FhDzMkCbbt3pNq7+hQUaro8tEQvKUfAg0G/vWG7ucsjzFbcVQdGRKfTR78dgkSkyh6ok87gXbt9rseZiEL4WvqGUBdcz/saW79+OA8lAnlPlcllepfIB5KBnklN3dK9JAhudo7mapa+1YLZMYYsuFR9/N1XXZ7DF1LarsPKeg1/iTCMLgikDeICvA9Q== 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=gdHLfjg4pPkAcdiHPFHHk4jqZpKS6AtEQrF+Jm8La/8=; b=eUId9sQZ0x6rVT0sWFm/k1fr3RVm/9UzH+3x6UlwEHuio9ppmZA6hiXWVN7Q/M6JE1I1DgNKugBQJjVbfOEEXAX20VBInWrMLxwQUrfLafWR+JRkbcPFYfrB9XEDEZb7GAmbgaZMeENscrBUPvKhgwV/wPRBjs9E34Vdwopke9CfpQzW1XyfXNf8aB1c2F45Ny5egtApUTpa0FFfb+0k21qYOEb4VCFjT6JUcZnbly5kqWFwqilQB0ZjuhyFrRSCfxih3d1fpxpg3P0OQ+mVwq+rYjYD+iDbouk5AhWDzKXJDg0iR8XbufcrynDvb8nQzpq6WajB+4lPYX3b1MNFsg== 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=gdHLfjg4pPkAcdiHPFHHk4jqZpKS6AtEQrF+Jm8La/8=; b=iNiNNDlKkuo+oZUPWHgodHQ3FKzOBoJfSFB5iireu1BnoCWHPuqrXL1k/b5AcP/7xsZeU0zyIV0Z5nMYYZo+3zwvFMkTZqxD+KEStiDepY9edA9+IxMiswY9xXojS79MgQ1r6BS++/VxlpIqe2lpGIV1JWDtU39bvtkEocZvWwI= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by DM8PR10MB5414.namprd10.prod.outlook.com (2603:10b6:8:32::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Tue, 14 Jun 2022 07:04:04 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0%9]) with mapi id 15.20.5332.022; Tue, 14 Jun 2022 07:04:03 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v6 3/4] kernfs: Introduce interface to access global kernfs_open_file_mutex. Date: Tue, 14 Jun 2022 17:03:45 +1000 Message-Id: <20220614070346.810168-4-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220614070346.810168-1-imran.f.khan@oracle.com> References: <20220614070346.810168-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY2PR01CA0036.ausprd01.prod.outlook.com (2603:10c6:1:15::24) 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: 64b8c0fb-0960-4dfa-e1e2-08da4dd410d0 X-MS-TrafficTypeDiagnostic: DM8PR10MB5414: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: 36R6Yt+voPOf/4beI20OaHqdnrir67TkRquLc8GjSte66TJF3s9wqTLMkgp9W78yFpnGMSUBEVot8+MtuDqpRIKS9RSDqX07APl4bc8YRVTfN/Bpl6thyaofDZ4r4JuiEbw5TcholOY7wbuisIB2mtfgM8dC8h0h+v0LYLiY+BA9BY2Z9m+u/7waHiql10ixOi9Qu3dQw13g3xBfFBh1opeD8Cpw/ZseQArIDFhA+bBPGmXt64xe7P9edRo5F6vDm8arUecknS6/0Arko8pBmIzVs1irEqWvf/GYNQiEL+uk4HkHhpH4YtCp1avhX5Ci5MxgRhw2KB0XWAMeSgngR/vKAB1ACHYv6in2GHH5TTCbrEeDMKBZqlcAd9D7HcbsmkXep8iqLp0lIivJ0RuSMNXSVVKKwWrt7aF62Ozin36tW8X7vK2Q9qhXRk8ZGbX7xUucYyPU2c09D9KKDqffrM14ExYPiOq1K6S+zOnj+6QaK+/FJrZax9TvH5nvolw3UuWz7iqAoWv2osRsjQfA3zLeVo+LbM078+x2QNM7sfr9shKkg9qNtEl48NTYdY+9Ix95c0yk/EZk3l5oZV/I95032WEgXyfkPebPeadAg9K7M+qlpGSyqe9u7O/8LBJXUr+iuC/dd621Pw9VPC+vMteEYdL9zA5cB7FxxV/CV91mUs/bUkutHqzT3KrC6eC/5XQeMXj3xzLEHolWWDYVdA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4468.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(366004)(6486002)(186003)(2906002)(8936002)(38350700002)(6506007)(83380400001)(103116003)(508600001)(4326008)(1076003)(316002)(66476007)(6666004)(38100700002)(26005)(6512007)(52116002)(5660300002)(66946007)(66556008)(36756003)(8676002)(86362001)(2616005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?X9KHyY7QCPnHGbjdESyRDNLQ5bLFWRJ72mZtDgiNpB5j4ALaqT5rKZCM6p0j?= =?us-ascii?Q?/Vv7MuERp92RccT+itAU+RHx33AsSDJbudOzL0tn17frVcmOUriaHsqo9ErU?= =?us-ascii?Q?hWsUNe7H+SiLY0hZxiEM9bYs+Ub5TngYCmrA0eSk1Sa9xi9j99LanCyC49id?= =?us-ascii?Q?XbyUSerwBxM701Lt3UyLaEJlA8VTGmj1CMGeDJwqe54zqW6L+ddMQ3mFukX4?= =?us-ascii?Q?4OliDKdtJx/U9n6HoxmnGY1oHV3VMaexuD8LaNBMp4SkfpMLzmWPOd5E9wsK?= =?us-ascii?Q?Ys58TFfeMimzlQFYTnUrTDtlqrKayptf8bGmJk9icRi+brEHy/hL5RdbJYct?= =?us-ascii?Q?qzcKeW3p+Wx9cDDI4hUjlws6Dxm7u9DQJnjQqIYB9vP4/s1qhcu81zaJjYSC?= =?us-ascii?Q?BlOfg3T3vxtFF7k34V1mwt9iZ1buWFaLrejf/2d2ZSxcHFcurExGVUZiUQyA?= =?us-ascii?Q?T/W7QmZYu1Zlgfs8kBSbhH61c7iEPYwRJgGXHK3BDYiglKZ3jvOYMk9Mw2ww?= =?us-ascii?Q?K6+3jL0gPmCC6n2Hlu0rmrIL1St3V8rmyfts+SIU/4zGkEdW4HNAdpfCFWFF?= =?us-ascii?Q?k0SjkzkeEeieGy9QGJENCZgfRNhbFDX/6TWj3E6D0bnByJopP+hw9QU85Eq3?= =?us-ascii?Q?EZ1q3RXYi6t2lU300Su04JQarQGvv2r9WrjGnWu3HF2MHm4MU+0WELutvcVm?= =?us-ascii?Q?yPsZOyI6oi+wm+H3y9TkFYC9YRjBOeh1w4vSDqcx5wuiYnA8rWoiN3/iuchP?= =?us-ascii?Q?if/S4tYFovPn51AxCgHlihPulrdtRbNHwvmXnXgdmaxMAbhKap9CRUXn20wl?= =?us-ascii?Q?D1upzxHaMr/BRRxEvH8meUdRsHBYApAMMc6hpAtLftPl5lsKCdjDJqmupSNB?= =?us-ascii?Q?0t36MgFp0oa8WN5vweJUFzMjwY9EeRUpCTGzIjxz4G34/f/zwuTgBIXhrG/y?= =?us-ascii?Q?aAH1Eiq7iSznyKVPpdhIQN2Bm+LXmczsgmqM5IpdRupUQsrWWoVIgncwngCQ?= =?us-ascii?Q?pR3R2TxPRE6Zm5Ks+SEcOMuBaUtiro30GZqiAjqRi4aHTkgxFbECM810tDgG?= =?us-ascii?Q?Q378F2ZscfUhSfgi1XA0pFuhu1R+Tn31uwYfaE24nQE2FMUmeoEymDwRo2gm?= =?us-ascii?Q?paNqqO1qVQEDN45dVv5YrlF+PxA4lJ2Z3RuP68lmA9WChu5GIRsHrp5ewOu8?= =?us-ascii?Q?lnZr8M2d+F+K4FMQKRjMx1wgKAPbSp92kob+u/5jT52KsHQsVs3UbPnj2zUJ?= =?us-ascii?Q?zg90JxMgS3MzvKdC/h6jxhrpx0VXqjBXUmXdoo7myHkVyp7LzTsQP0dFv3To?= =?us-ascii?Q?wHwwz7J77jTdwig7/4vjjo4cJwg++WxG2CdL7O5R2Bi9ZyKjT+ARH9gCLJ1F?= =?us-ascii?Q?CPUBMW528yOteYf2+B8cml4xhPNH0Lr9kfuIEEj+P55zgQqzwUobiXheDy16?= =?us-ascii?Q?l+pfM1zB2Twhvc3QjXWv0ErHiiQP0gwG+Z+BDZ45/zcrOKWgH6ZELZwjU7eI?= =?us-ascii?Q?MDqu2rG/SWn/+D9hdmk+Kc7vXkOI6zodmKhKLT3fjlH0qbMHpL50bmarX55f?= =?us-ascii?Q?oP2yupJ0JS0qEtBRtuIV2ATRQoqkzITG8C3+t/ctmZIVJE1LuOL5q+BFOpgB?= =?us-ascii?Q?Fe+yJlFryllZINi759RsNm9VZ9zaR/bHSUM2cZkQqpMquZO4P0BPftReR8aR?= =?us-ascii?Q?Orv+XoRcTkhEB/I3dtgMLa1ayhgHP9cfShc6DYhVZonrpX1m1fj1FP3SHJ+/?= =?us-ascii?Q?zR2TNmg12wCMk9Snk8DWelO+88YGmp4=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 64b8c0fb-0960-4dfa-e1e2-08da4dd410d0 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jun 2022 07:04:03.8126 (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: gHsaE85xZETepmXNIC9n5Xwf5++Bak+eaKQ+b71V370c7xb+5cPwr877Wnrg7rAuXNkQqN7ISPIqUIzGqAj6qw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM8PR10MB5414 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.874 definitions=2022-06-14_02:2022-06-13,2022-06-14 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 phishscore=0 mlxscore=0 mlxlogscore=999 suspectscore=0 malwarescore=0 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206140026 X-Proofpoint-GUID: B2MuJ0zO4JQGo00jxQMYR4ZRcpPb-C-I X-Proofpoint-ORIG-GUID: B2MuJ0zO4JQGo00jxQMYR4ZRcpPb-C-I 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 | 56 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 77aeb0b6f992b..38fb71b2c671e 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; +} + /** * kernfs_deref_open_node - Get kernfs_open_node corresponding to @kn. * @@ -79,9 +95,9 @@ kernfs_deref_open_node(struct kernfs_open_file *of, struc= t kernfs_node *kn) * @kn: target kernfs_node. * * Fetch and return ->attr.open of @kn when caller holds the - * kernfs_open_file_mutex. + * kernfs_open_file_mutex_ptr(kn). * - * Update of ->attr.open happens under kernfs_open_file_mutex. So when + * Update of ->attr.open happens under kernfs_open_file_mutex_ptr(kn). So = when * the caller guarantees that this mutex is being held, other updaters can= 't * change ->attr.open and this means that we can safely deref ->attr.open * outside RCU read-side critical section. @@ -92,7 +108,7 @@ static struct kernfs_open_node * kernfs_deref_open_node_protected(struct kernfs_node *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) @@ -575,19 +591,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_open_node_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); @@ -596,7 +613,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; } @@ -618,12 +635,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_open_node_protected(kn); if (!on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return; } =20 @@ -635,7 +653,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) @@ -773,11 +791,11 @@ static void kernfs_release_file(struct kernfs_node *k= n, /* * @of is guaranteed to have no other file operations in flight and * we just want to synchronize release and drain paths. - * @kernfs_open_file_mutex is enough. @of->mutex can't be used + * @kernfs_open_file_mutex_ptr(kn) is enough. @of->mutex can't be used * 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) { /* @@ -794,11 +812,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); @@ -813,6 +832,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; @@ -822,16 +842,16 @@ void kernfs_drain_open_files(struct kernfs_node *kn) * ->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 + * check under kernfs_open_file_mutex_ptr(kn) will ensure bailing out if * ->attr.open became NULL while waiting for the mutex. */ 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_deref_open_node_protected(kn); if (!on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return; } =20 @@ -845,7 +865,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 Mon Apr 27 14:54:10 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 24530C43334 for ; Tue, 14 Jun 2022 07:04:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352909AbiFNHEg (ORCPT ); Tue, 14 Jun 2022 03:04:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352817AbiFNHES (ORCPT ); Tue, 14 Jun 2022 03:04:18 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADE27252B0 for ; Tue, 14 Jun 2022 00:04:16 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 25E0o9KH027265; Tue, 14 Jun 2022 07:04:10 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=f3cGwwvC9rHiGqS70G11BCfnoztMDwbpLZfF6qsiarI=; b=CymPJsby+Cs8ouwChjIS/K5kdNrciDduj2b3p459ft2R07CDUuddxyZHXK9PFgwJEpAg BHRbuqBQu+G9OEONbJZPFVKSWBSiwl4z+WDQnBU/iXBj4KNE48eGnWC2+Qyqrx5SrS+s yjVx8BrBwgDxA2WYYMERfK9Dn/qof1tS3L9wKooRIXK3P0T6qiqIDYUeCzOSfshPKGtx gvgXP8sbYLadUwNlvn7BKHonf8RoBtZllP+oD3XUd0CNL+Nvoim8Yq+dMnsc9cclyJEz zUqP+ftaRCPVZQVlMHIsC/gESxyNPajGenU2kB4MtPcm1WEpd8UlB85kKcaQ815uLk38 BA== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3gmjns4xf2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Jun 2022 07:04:10 +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 25E6u0t2033151; Tue, 14 Jun 2022 07:04:09 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 3gpngeg731-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Jun 2022 07:04:09 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Te8/Raf1vzFWk4gaEiOsQKQnswUV717VR/0o9FnO4pybG1+nmNAvtUXNGS+W0y5oIHG8S5JHGmGQ32v9r6jEGV0q5aBQjQRbgKI6rFjh2YG8rWIdOR5RpUwMW8s+o6OEytTx9TW/FdgxMhPD6IeY+jIS/I8NEi1QDVJEOc8tec8+xcDSZko0P2t9lOz7sWgNNjH0u4L2ikiFQYJj3gjQcxD6H+7R56Pis1IfIrsg5KzCVS1S0A5z5pO6Q+cDXYN6xZiBrpePZp8VDLG/OJf1ZF5GYBIoSYH0Gs+2FHQxFbw/dhVAguPaApyYzgBjh6GNdketm4NgaAqHQVFnIo0JOw== 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=f3cGwwvC9rHiGqS70G11BCfnoztMDwbpLZfF6qsiarI=; b=lKKLIYj+I/OOS/TwF6WuEQhTq1IMF/o4TiDsBZUGliSHP5aKWSo7o+FjvuA6yx0E7dzBA66pCdllXNIMJl/z+7YzJIck9uaUN3QejkPoZMf5gqk2p6wKvy53dVj57mx0tGEaZ310rP1bvU6p9c0u87+YLBk85HPNsSlcktXh2ADZBCAKZ6k9QnyX+5UIslplfJeoZfnu4DsnRwTF3x1Roc64FYdVsJ4iBHoKUlBCkwm/uBauEXf6CFDqjcf4tH9o+MBKM7Pu0FPXpJARQp8bh3Ek68jsuwDk1SI5wpBBOK4d9tc8dgMGx6wgJ3pZp7T1/lXvI4OHpTgjbSquJzfrNA== 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=f3cGwwvC9rHiGqS70G11BCfnoztMDwbpLZfF6qsiarI=; b=aA5pDUg8hNtfVm9Ot3S7YOCRMnm1uu+MJnnyJR5VInnd4/2c1VmVdD1M1yiQ0EGZ9yp8XGLeMiq34rwWeMIipnD4p7T4W4qOO0jeMl1R29UWWELirECc3jM/I6c6LT551r5JJdcJLqnVdfgTe6lOsdEsclLVcECEDb81gcDI0j4= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by PH0PR10MB5530.namprd10.prod.outlook.com (2603:10b6:510:10c::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Tue, 14 Jun 2022 07:04:07 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0%9]) with mapi id 15.20.5332.022; Tue, 14 Jun 2022 07:04:05 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v6 4/4] kernfs: Replace global kernfs_open_file_mutex with hashed mutexes. Date: Tue, 14 Jun 2022 17:03:46 +1000 Message-Id: <20220614070346.810168-5-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220614070346.810168-1-imran.f.khan@oracle.com> References: <20220614070346.810168-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY2PR01CA0036.ausprd01.prod.outlook.com (2603:10c6:1:15::24) 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: ed3546ba-e54e-456d-62dc-08da4dd411cb X-MS-TrafficTypeDiagnostic: PH0PR10MB5530: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: ynVqDtvzWWwhXvlOr4WmsCI6cPN7MkXmh3ftu8EY7P6NIP/OjFiCIw5Zllk2gZi6PXMKBzU7+ajVb9ab0106dpQ3s0elU0h96Pynt94wLEIbA/oE5P7raOTK+ZvbMQpXbZVDHgpKiF8YSF5PKrXrdlGkSbowkTiEAWajjh3iGDjmfqPPUiz2w9Rp6OvyCD2t005Ue+MNQIyyOUSZW+TvRfrVrKBLADyNJmhlq3E2KAfRJPCMKJc45+9fgELnfPfP65PgJ43NSTInCSpYUWtsexrE4tHlhSrvDGWhOu2ed+wM6AafmSLRt0sdzyW7dTNcYJamMnE0tHSkyT2Kvq2cTHEZ+aJ8iCICX5BB7voJkuUzOYiskzZP4EbjORg4iVX2Khl09AW84AbEbcH/5yugeDilbVmwyOEGusaHHUhRHbEisW0eYNf11apZhMna7C3xRbauL5Wv9nvUXDyUbfkL2mz4YLJajHU31ueB//ab5tOJvsZ+UZt/D2z/SQOkqfWV4DjNfZGYBH7eyUCxxuwzUzCGj+cdk2ro98CdXpgpzbpGMku7PTPPjZ0rxD+WBi7pcq46ZNL4ULKL9MV3nlMXzYeERsTS/d01ixFdaFdQiDinbyCeBvLcO44uMNX48pZp2JiigqGH1prDI9T9Tyq0Wy2AE7BK9PWnyJA4lcwcFi/YbItb4rgRWxBAQUhONjWuXgeA1iHwceP+aFmNDqp2Cw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4468.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(366004)(83380400001)(508600001)(6486002)(36756003)(5660300002)(186003)(2906002)(103116003)(66946007)(66476007)(2616005)(316002)(86362001)(1076003)(8676002)(8936002)(4326008)(6506007)(52116002)(26005)(38100700002)(6666004)(38350700002)(6512007)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?xeFJ4+OnA7D6Rw7+ljnrm+s03TyGs1FpxNrTwsrHrTQDw9PgAh2SY416fiGB?= =?us-ascii?Q?G+p2oSGKRdTsJ2PjgIYxkiSwfh/3uuUHWM4ICb7R5LGWOKyGcovubBEoB3l1?= =?us-ascii?Q?/xUqEOrYv4NNfyyaSQaX1Wcw3NbME/cBcWdX/6H3MpUrcbodp9zM6Vd6So2M?= =?us-ascii?Q?cT8GeCkpsf9xD/EkGmOXdURL/IPqUaIz2ULpez1vPVSpiJgG4fj27LJNqTcA?= =?us-ascii?Q?yQ49WB3u5oNhh+V3Iwgg2P25iw9zV4vp/kdxXxioSbwyMojq/4urt/5/IMEd?= =?us-ascii?Q?tMa4lvfbPyz42VEe/VJOKMOmf5bktKhC6qzIG/WbIv4Pi7m/Xcs9Jdzg51Bj?= =?us-ascii?Q?FjFTu6aLDoR9G/5NT5ZXezuMnGvWYvEwt0dUuC9kFuv/FnNoMdo8UxH6lOMT?= =?us-ascii?Q?5Lvg+ish64LqSkH3ByuQfBHRZdcDtHLUO4Gn7gEsr2j8mvxcLYuirzOg/I4F?= =?us-ascii?Q?nzblDGAMVK8Zw2hDyjUX09BG4KlzYyoWjUTz059j3OneY4r3rJjIzOeD+hUH?= =?us-ascii?Q?SRkFkMEkDjrG6inNPUnstZaqmvn418UGvLdWfOEW1c7WoBtFXof9DqkIvCGJ?= =?us-ascii?Q?ykVPKOE99Uv4vJJjGIhKpndNUxMbm0X13aEw8SsPLMBunYEM48/rMe1khPIe?= =?us-ascii?Q?gSXDberSENeELAOzviGna+xO9eDrEYoMbN55sbDz5EYJepdTSUgqGH+FbGL/?= =?us-ascii?Q?xXoZUbcYiZHYk53kZwAPtN1e/UTKPRIdH+kSAyypcoLJzVDWNSgyPJJra2TV?= =?us-ascii?Q?PcwHwkOJ730a1kG8PL93IdYfJSS792QTGTmpdJRF5AAFWhfUzoOk8g+3C9W4?= =?us-ascii?Q?j0gKcPJqbwqBOIBvvsDOIMWbKq/rG9jAQHaoi8Wthz+dH0x20KvunZj+xs1g?= =?us-ascii?Q?m4JZ/pdv/9vS55ThqRkLBfUuizBlI+zR7GpOcvLF7f8EK2L/5auQ/dGPuXvS?= =?us-ascii?Q?jHvBGhdaF/Czi88X/x/a4HIOOuH/3nsNDRbgxPp69FUCdyyRn3Cc2CRSeQ6S?= =?us-ascii?Q?TufSJ14caODWb3yUsgb19POk1iWtVuYEyr06Hp0Ie0w+8CHNd0sIhZSYAJUp?= =?us-ascii?Q?ytCjFJMhlPueA+eRl8OJLie3qEl2Pc1XE8olNSUABjcXwaMH2LcGN0rkQl78?= =?us-ascii?Q?0B1laNJYFN4yKjscxMhg8CgwmUbsQSQJbqOa0gEOS+boVbUtf3rXst6HJ45x?= =?us-ascii?Q?/A/c1r6Q+fwF+CTSWDZ0gMuxUqXTxWrL0tE4PDgsWfi1Dr9gl6L3XCszQiDI?= =?us-ascii?Q?Ofz83cusW3YsTN31+TdhyGH2jaZuxQb8fZYMNb+twAwEmvN0TBCpXjTmJOC0?= =?us-ascii?Q?LCzioFYnqoIQJkBIT9he1tHv+CwWhbvxlL5FryIF9THPBwj9mc3DniRhLdUi?= =?us-ascii?Q?XB2F9SvLJTVQf0xSSGlCZlcPU4IWuVN9GRUoZCtrmZvJinKRDKG2hsP92iSz?= =?us-ascii?Q?69ITI/jC2bTQsMre/0tSkQSNHIKWsjbrW+oRH/XYXxZja1SB7r6NECklSti6?= =?us-ascii?Q?T2vW4NPJ06PO2Sw9pfEwsqxmExOmyZiRGkgFL4b8obx9+pCKc9Mqbu2vrDni?= =?us-ascii?Q?7p3fiXd1qL11ntYLCSojXGVo0bTUwbWUf94ev3h4AbBuQ9NT/lyvpX/s2Bn/?= =?us-ascii?Q?YaMhZryKfED8RWUjNTrrG9L5HA9hlUmC2S2MeoSmiWcLFT9E/1nBbvC5FE/1?= =?us-ascii?Q?rTpvJurFYBqidyrtlFAN+mSNlR9zFhyLhd01LFihM3bYdvRvJre0KhJ+p+8o?= =?us-ascii?Q?q2lvLJWwpjijeB4J+oCxlkYCWylzb/Y=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: ed3546ba-e54e-456d-62dc-08da4dd411cb X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jun 2022 07:04:05.4699 (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: mv7teWZURVavaTebyMhWj4pQW5hMr8iORhO9c75V6xlkubJbR2kcuNeWfsDsEh6LZaxdkidVf2Lxmdfg2HBUDA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5530 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.874 definitions=2022-06-14_02:2022-06-13,2022-06-14 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 adultscore=0 mlxscore=0 spamscore=0 malwarescore=0 phishscore=0 suspectscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206140026 X-Proofpoint-GUID: v_HaHnp379SkwB76tlc7oZt4yGoHuiAv X-Proofpoint-ORIG-GUID: v_HaHnp379SkwB76tlc7oZt4yGoHuiAv 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 38fb71b2c671e..66bf838cbf9b7 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 eeaa779b929c7..3ae214d02d441 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 cfa79715fc1a7..d0859f72d2d64 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 2dd9c8df0f4f6..13e703f615f79 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