From nobody Sun Jun 14 21:08:49 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 D4561C433F5 for ; Wed, 11 May 2022 09:55:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239150AbiEKJzN (ORCPT ); Wed, 11 May 2022 05:55:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238393AbiEKJxT (ORCPT ); Wed, 11 May 2022 05:53:19 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BD3F5EBDC for ; Wed, 11 May 2022 02:52:19 -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 24B8OGUK022574; Wed, 11 May 2022 09:52:15 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=MEryTXJE8CmdHi2K4qrJQ1EZMSc1ytvc1cHp7TCXjII=; b=fZuEzRhhXo4+C77KYPKGkqAtnDLUnSZzFIJN71aM7JHue4lB6RcZNcvo2D2uVNbjK/yU cKxKIWd4CaRm1h5cSVANjB8sk6GujSygdicoz54+HI6poDN97dIKc0pIPoqJTcw4YiSY LDAT0ymoGGD8I12l/ORMDLI53RtByX3f/NxC4NacAvY+jDheMtuxWzc4c5WEJ7IFcptI h3zCDeJ7IUYG36fq4hAOnFKM+xJAyh7h1XBVhSawn8+GiMVOur8jh5hiWcRu2gS7luu7 okmTErHT3GPfyuS/FYmyn7WtLPB3QCgPxUWc+QD+Ex75Mwhe10mZuYxktTHLZzLryTXV HQ== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fwhath828-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 May 2022 09:52:15 +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 24B9peBm010660; Wed, 11 May 2022 09:52:13 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2170.outbound.protection.outlook.com [104.47.56.170]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fwf7ad9vn-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 May 2022 09:52:13 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=A5BHQevo3huyU/nwTqXUuBHmUEI5EhJiFw93++0lpfOvkkdOdm478LWKM33CSeZe38INCDpO3f4O34FiHFkEL4W899G8h9RyrDl38q3iQcbP8ZqLd166okgWYiCDk1BEltXoXV7VihSlwXwb5xzUxCT4lsLl1gj7rLbgKdn4mLHftBt9CZLCA77Gp1qCAX0r9/se1bC5qAtazu//z42JMfth2Heu1GpO1TGFPDjS9grKZzBjwtzpyMFFy3KYnhL5xCANpwtuS7VkrsBkL2W4792pBQkDUi2AarwykC5IeFTywbthGk8vuchPgqAtjlz6wdg4+Dsmgab0S0VUs86V4Q== 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=MEryTXJE8CmdHi2K4qrJQ1EZMSc1ytvc1cHp7TCXjII=; b=fethLSs/sMCko+5ZUZwOr2Y11XMlwzCcH9TxOmtcODJxKGX3TDyIiukRcpEfOX0TMeOwDs4v2T7RQR044XkBN7kyPewx/m0mHmtReuUjFDMNtPZfhtkpEbAssnPp3qHFxPTRijPiHK6FPBRKh/gkHaC/OsL201K+OtdgT0IOZNkpVYkGCm+uJHNLg/qCMxcmqj1dppsjOCzaU0V+9+GS1TU+fGjGwJeXhdSM0CcIsolV62AR3QQVmCQSQP37p4bqA357+MNMATJ1o1YxLQSm44bZ4MSJXscvBaXicQ6vvivavLpAmno3hF+QG4q2wMWJj615A1J/rpiqG0VfBAT9nw== 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=MEryTXJE8CmdHi2K4qrJQ1EZMSc1ytvc1cHp7TCXjII=; b=cDsjQk28JyUt5/c6AegI7eaV4IVPledndUmYSustTcQXDWh9Ube9mx1iKOa3AW3kTiSAQBlG2BQgYhG0une3U6ptu5DprH/JCQkd3vHZiz7Ps8Pg4K6pT6qsrJLiaxyS+xUyowWGtp/troMUY8tR5Vp3hSb6JK2SbtYU0425X2g= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by BYAPR10MB3718.namprd10.prod.outlook.com (2603:10b6:a03:126::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13; Wed, 11 May 2022 09:52:12 +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.5250.013; Wed, 11 May 2022 09:52:12 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v3 1/4] kernfs: make ->attr.open RCU protected. Date: Wed, 11 May 2022 19:51:54 +1000 Message-Id: <20220511095157.478522-2-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220511095157.478522-1-imran.f.khan@oracle.com> References: <20220511095157.478522-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0011.ausprd01.prod.outlook.com (2603:10c6:10:e8::16) 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: 473eaaef-1c3a-438a-d69a-08da3333ebc7 X-MS-TrafficTypeDiagnostic: BYAPR10MB3718: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: JRiqou7mQg6oAZcDnDB+R9pbGjY0mVkXcUPYIDWIL1agINwOSYDyGCbotoK5z9c7wEE1oIeJtPh9v492wk4woilQ439p678tZMDD7zJmChkFH0jXtKBdARRg78hMI4EPygOFNsXRv2hauaw4uXZf7MeQKEQ3sb3yAI9iFkbxGjDZupR8mMkMpW34xSVS4JRe6k17hsPVEIIa8lFVsobAOTb/7QGCNcARd/zuA/Vsk+2lgcSl5r/0/ZkHgnPMOkEujS/YhPeuZ5TkshjGjKWKcEUv/BxRJk0p1YeEORYfEpGxxT0vS7Sed5GuueXxprcH+bpf6KiVqRf9upAFWC4NG0Kt4d/qeT7Zkk+fAo0CO6+gR7e6kicITdRg77blCb7rCtnleG40farxjNoQVhElZir+2IQt/68RJMDt1/psocUxhGBazKx5Zse3SlctevGrjQIFiA21ulio+WSKzZ6FTQJfxZI5RONwiwt11YEDUewSoq1HBGEWU0txULForLAiA93/dR69e2+rFTexxi8uWLBMN+epMRwnwhkVdVPJyaYLi0yPfqHOFckggjHC14HEbD1MViGA8c5+1a4nB2Rvag6x2wxBUXcapLnm/boMWtnx16pmN7x8jWV1KkphTwBYiHt3qPeCa83/1NYrhAIhtbX84MPFg48uPUThthSgmrisWS48pjchetwnGLR2gI1tiWq/F9FYqhqMgOhpOIMvCw== 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)(38350700002)(2616005)(6512007)(6506007)(26005)(52116002)(6666004)(5660300002)(8936002)(2906002)(103116003)(36756003)(86362001)(66556008)(66476007)(66946007)(6486002)(508600001)(8676002)(4326008)(316002)(1076003)(83380400001)(186003)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?tOEMvVIYZTJLWCvsaNPH4kaYxJTJwnBTeT0+qh1I3uvkapokE+ALbVRYcBF4?= =?us-ascii?Q?5xSu61aaVVwkO3B9avDr1KdWYdW/Azz5AmzLRh+KH/M4+8kUzG8M/ubcv23y?= =?us-ascii?Q?FvMf7HQweg6OOEVzgngqa2Y8eErDdb8iM9mDEt42ArrZBk5w6zGswUd9AyCZ?= =?us-ascii?Q?2B6o9YQxAG/B6KTp9Vn81jTT9vguHQXzxAK2+bssw7RtP0Rw0LsUm26bkIsu?= =?us-ascii?Q?GBTZE9ycfQFSLRF6PLD7LSgGK7A4B4KSB4WlBLuQpX6eJ1pfjWPH2IPDU9Yn?= =?us-ascii?Q?MvumJAeT6Ch8otdSSdKp9TC1RtTas4ANQY8/efYOxsFVE/jrAVMQhTiJD3IO?= =?us-ascii?Q?CJokTOaKvuj5dzxCyAHP952euZyCl4qlTQeOzZj0gc0AXglPGvkfRPhGsDjh?= =?us-ascii?Q?+Ze1B6TW7JAkJ6ExSlurvIOvyoMuFj1jrIjlyfWykcy5ceek9IE5/vie12eQ?= =?us-ascii?Q?8KabiVCRbgJPChk8EoIge8gCG6jKZ6miBLxfo//b9BTjCCMGscIk3wYxYNK0?= =?us-ascii?Q?jQHk7T8PnEM1vVJs2q+PVZUAa94/a1vRpLyipV6l3wxEXuq0V9aC9Ihl4G8s?= =?us-ascii?Q?Qtste8aOxEVz/P2jv5IMaMBR1zVoMbQxv0zmnBfHJyDcEkXzyPGP6FP04jCL?= =?us-ascii?Q?G9tw8Hx/NWvLMqop3EqGAeWfDtJ+BWLk8E3k5HqX4HY3O+7EbQnVhwDWefEW?= =?us-ascii?Q?dKTz0RYOJDW23glps2uZSA/iF7BD2bv+mS0wGODA1ioPJYMYI6rEHwgqmdYD?= =?us-ascii?Q?kHLkEbLc47SyX30xQz4MDIQqhSsV39EmrdVQflNb/TSO2mK9LbhNBGaAch7S?= =?us-ascii?Q?LeicTdIDkBJJboHbDOep06x9UIISbfJE+5DqTFGLdfrlqz5suYa+smhs6uAF?= =?us-ascii?Q?bxQCTtrC+M8p/WtXnEW6fulnTKIlq4rOpnzF0XraRo9WHkBHBvc5lbrDvYKE?= =?us-ascii?Q?WOsn0BEmLtvdk4QAD9ij359l2MmcSe/yYnVvrtseUCMWxuWMhKaJwTfptWTb?= =?us-ascii?Q?7tLagaBr3sYHIeu2ckTkmV9CWlnMQeHC0r5u/NYRMKZ6uwdLh5Yrj/yijVZk?= =?us-ascii?Q?Yf8hwmrCKBGWUsKrD/ER84MyD1d1Ul53vJFVN1Pguwm3xfMePMYYA0S/lvwF?= =?us-ascii?Q?VHSiCmxRLQKwjthC+olhfAmu7Ql9ZQYKnoxRAb9j2tn/SGswCE578HxE88IZ?= =?us-ascii?Q?w18izglcUay/Y8LfitS94JZ0Njv5e9vPlQ73wfcAL403/SXROIZRnCRw3QVt?= =?us-ascii?Q?QZUyI/Ezw1sZ2LAy+Hh2N1FuT5/czw+/l0X83Dvly3Gb0kZdtbj/AtpXgAPo?= =?us-ascii?Q?IlsM7O88ElXwX7YxF/2tGQV9hd8jb21mz4Ev7ySabRxTaOqclAHFHDASQD9v?= =?us-ascii?Q?h8p+dvTJ4sVbS8216nKCu13c7qcvaceb9pAvo8mPO3Prd9IirmbhcTyPkJ9+?= =?us-ascii?Q?OKfEBGVj/LQZI79naWmWtsVTPsIpAP5FmShUTQN1LOyPuZRS6PMvez/4zmgt?= =?us-ascii?Q?D9RC43FNDMB7827dSPvP2FKwUg1jKtvjDlCj8Ry1Nkzl8IVqllPo5GENVYcn?= =?us-ascii?Q?jhSrv59TTRV6dK0Q+Z7dEDHi82akNIXSf6trI649JhsmH89Q/bLw89QH8yJn?= =?us-ascii?Q?gMDyCmc7RDSVWs8Wcp4UjojKbqrUcnKjYVVThnVNcUHYSiXL8ZwwibVpFbeq?= =?us-ascii?Q?WGc2VuCAtkCY3sZDPQ9j5mYzBWke56HElJSahJ0cKah6YzCRLXH0iiM7Czpx?= =?us-ascii?Q?kjwp2ddNV7tzMMYpxkhbte9/MqfEl+w=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 473eaaef-1c3a-438a-d69a-08da3333ebc7 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 May 2022 09:52:11.9843 (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: o+qLZE3YBadgrnOZexdjJTU2jfaHeend+2EZxq3lqQ63+etLZMyI4G/Al7j41G/+koIdNT9WR1Q4D4cqZMelHQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR10MB3718 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-05-11_03:2022-05-09,2022-05-11 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 mlxlogscore=999 spamscore=0 bulkscore=0 adultscore=0 suspectscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205110044 X-Proofpoint-GUID: Ik3ukUpnngKJ5dt7OXEbEtXqm_pAANUh X-Proofpoint-ORIG-GUID: Ik3ukUpnngKJ5dt7OXEbEtXqm_pAANUh 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 | 141 +++++++++++++++++++++++++++-------------- include/linux/kernfs.h | 2 +- 2 files changed, 96 insertions(+), 47 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index e3abfa843879..796f27333846 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,50 @@ 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. + * If both @of->list and @kn->attr.open->files are non empty, we can safely + * assume that @of is on @kn->attr.open and hence @kn->attr.open will not = vanish + * and raw derefeencing is safe here. + */ +static struct kernfs_open_node * +kernfs_deref_on_raw(struct kernfs_open_file *of, struct kernfs_node *kn) +{ + struct kernfs_open_node *on; + + if (list_empty(&of->list)) + return NULL; + + on =3D rcu_dereference_raw(kn->attr.open); + + if (list_empty(&on->files)) + return NULL; + else + return on; +} + +/* + * 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 +200,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_on_raw(of, of->kn); + + if (!on) + return -EINVAL; =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 +228,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 +250,11 @@ 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_on_raw(of, of->kn); + if (!on) + return -EINVAL; + + 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 +572,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 +613,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 +821,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 +861,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_on_raw(of, kn); + + if (!on) + return EPOLLERR; =20 poll_wait(of->file, &on->poll, wait); =20 @@ -922,13 +971,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 Jun 14 21:08:49 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 9A1E8C433F5 for ; Wed, 11 May 2022 09:54:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239128AbiEKJy4 (ORCPT ); Wed, 11 May 2022 05:54:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238407AbiEKJxT (ORCPT ); Wed, 11 May 2022 05:53:19 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99F085F8C1 for ; Wed, 11 May 2022 02:52:19 -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 24B7W0UE013766; Wed, 11 May 2022 09:52:16 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=Dv6MdJQVF/iPf30dqjqKA26fuiNU9n9T7VLokmbH7wM=; b=LJMn6xW7Ejh4xnd2je37BUICqrsuINZX+t09v3pgEu2uTLQ9XxecYa0bIP0MgzAQtJ57 OoDIXxiZ7ZKz7w7s+sj0Kw1W2CE9VRAHfJgkCKrtYlOdzUpwM4vNuO2I1Yz9dAsd6k/T D/Pys+fGzh+RfDq+0231L5Lag7TtSCELvyvQsKCvk7765Pv3A49mEWjkgyWv85yebMzK m2U8tKNTwgwK615JSASfTyqAG/r94PfCPpMRwTMrTz83l2TLv21LeLGIod9LZEUXklrw waZWgWHH2Ujelt4vFxErK9ObaMAA1xT9Ax8WUbWHjTO+wNuvju9amQ5ditIBB+wENHlS Mg== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fwgn9rw1f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 May 2022 09:52:15 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 24B9pSlg030701; Wed, 11 May 2022 09:52:15 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2176.outbound.protection.outlook.com [104.47.56.176]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3fwf73dv9c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 May 2022 09:52:15 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=E76+pkIYCZXWvTLVtYk4JyGRukaHeFOwqlEV9vPbFWaaEHwz/1vQEXoFmJmye+xt+WQ2tT7XrM/koSajaCGiOVkukDALWo1y6weplD5iHrNpsgYywg6PUhdjzWkuQtr0WpPwn3FkMIhRQq+354o6r5vT1A2nd/Ag2ZshFdCGcZG09mbVS5CPzASOh7eHo/a7vDjKxXtImUpcJMu9QHYEr54cwlrPusa+Rkg39Mgh66mFLj4fPPqUXkuuyxqLwWmHZ4exrwECrQj1xlZVw3V/79NKZxlXDdkZmG1X70kHRErrHPP5OVldxk+iQx2UjWIddR/mspgu2kyOEOWilceO/w== 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=Dv6MdJQVF/iPf30dqjqKA26fuiNU9n9T7VLokmbH7wM=; b=ceYc0AlHIqauhr9ONGY8tEj023FXKY050ImCWzwY0d97Q5+EVdpKfyylvqumZn07NReGwlP3fikVpM0ihfVw8BiV1SnmGBMQertbBPTZohTHDF6T2yYpCTNHl2SrLEVneFmEJ3RALgS3SK87ALrCgEBjGmhxBoew8PtIGo63DE7r7GGZLb0SczVq8mSZmcIQl+1yi/drRsCp04rv37vLlA6bSlGh9R8X7u0CLG5VhOEaygCE/w13B/Mwc7zppH7aNP/8nGJT3IGYa1JKkhSVfUroqHLuyP2EcSz4HPN+Y9ph+13KjVYp5Vr1e4NpMNl6sMFKpM288sKCsVB1b5lQnw== 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=Dv6MdJQVF/iPf30dqjqKA26fuiNU9n9T7VLokmbH7wM=; b=yXJ6F4eVpF/NkWXxnniPALLqUTad//1SYyL4Aly5lV+6BBrRyYdHkTriVIOxN66ctwjTyo2hOJoOx7gMXtZ31lMKonlxiMAAtWyGJHGmWfhl5h/zAXVjhc2g1E26jN/uA706vMqIy7LPY/C7PFVCPg9TZRRRbgKobszp0f7eOGA= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by BYAPR10MB3718.namprd10.prod.outlook.com (2603:10b6:a03:126::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13; Wed, 11 May 2022 09:52:13 +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.5250.013; Wed, 11 May 2022 09:52:13 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v3 2/4] kernfs: Change kernfs_notify_list to llist. Date: Wed, 11 May 2022 19:51:55 +1000 Message-Id: <20220511095157.478522-3-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220511095157.478522-1-imran.f.khan@oracle.com> References: <20220511095157.478522-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0011.ausprd01.prod.outlook.com (2603:10c6:10:e8::16) 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: 27e9a72c-417d-4c4f-2b45-08da3333ecc2 X-MS-TrafficTypeDiagnostic: BYAPR10MB3718: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: oAXx0wUB57qTj8r9vfiJl0jbSH1Gfkivg0vTammOo12Er/MzXbr27U58C/aXs1lp3GFrs9RMggI2rBGSQ5qanQ/BgS6BCeZarziAgqm21P/aMNaH4xVctA/m637y0rXqwYV/kSFYw8eHdUSmGrRhWROsYlwoqh59wPS1XLxTGmz9itEIpapwgPwkWZiYzDBmxAypnAazjstoI73R+0sCmmNxDrXeCyIARY0O/kPfHJ//pxIMlFwzOXhKN9yVVh5PkYwFylyyNO+qdsn1N1kQyzVg14da3YHGlk4l9bVcIj9AM0RXENYC6QCGawH/FnF8tLruxC9eM/WuGWU/byM9oOkFJ7Q89NamZlliEGoQnKwxqWdFG+w5JMVQ9xP9KZPN1qf0Qkup2OCRjjGJRKr2k2KiwOYCor+QX/AlG1zWcAQCkgPnyJHOKww66u5t+9iqDA9XJyCHjyW2Qdd0CjE1TOWASCJT3JhmIUCIut26Eo/q6hjy587LxYBqIfUW3NuUb6dEaoHqnvTSHwokmIu4rj2wnT4+MV+yxtKuzXwClUxJrfBeQQ6gLula/BSMm5WO3CHSid4gub09xyrWiDGhKYEHf1dK9OfFviNyS7C/gcqvzwiKMhx4vrK8gapWYjZlpQh3hOE71MRAKTIVcsh853IZU/MNVuAwO2+s/oG8EtlH8S530Mb9jZkMcd/VRfbffq7b0OrMC6NcOtfnMb+yhg== 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)(38350700002)(2616005)(6512007)(6506007)(26005)(52116002)(6666004)(5660300002)(8936002)(2906002)(103116003)(36756003)(86362001)(66556008)(66476007)(66946007)(6486002)(508600001)(8676002)(4326008)(316002)(1076003)(83380400001)(186003)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?rJWr2rCp/6nbhxokw1fi3xTj3mKxFhkd1/I1mAHTHLQgobhhv5BCMtvecqlA?= =?us-ascii?Q?KOWRFfGnz6zpO6pr+G4QQLnjB6dT1T0azcSZEKs/exLwEexIidGi/LkRCEOP?= =?us-ascii?Q?QObfC0Lr121LMFT8iFtvGuihWWCImUitRBoi/IAt41rERC+55xM0ywqbuvJR?= =?us-ascii?Q?ctPecAKA3ZiNmlD5EIpHUW3KRsgZ2/tDt9dsWahgwgbQp0ddR/kqO4ILSasg?= =?us-ascii?Q?xcInmOrCRMWzkPZGQdgQXwkfC4v1Oqm4yaAE5OGwI5tYFTQOhmBz8cLfG7LE?= =?us-ascii?Q?2gRGofOyso/zZzK+IapaylCEK/1iDGVoz5le+htzWrE7wCa7nPkTj9yPD2XG?= =?us-ascii?Q?YaxohCa9vw5i7Nxn8InddddOLKNRAPoPZczFO9iHUzaukCfQZ4CDiiY9TO4P?= =?us-ascii?Q?9flRNgOMeT+30LsuerqipQOp/e6dOP5Oqrv+XcExNdhn8liZGP0wdLbvfYf7?= =?us-ascii?Q?nf9gSuC1YcfXToUpNZcgMfMI2RQyzyRZPbsh/3v+PdSLshm13glSe/1n4MSg?= =?us-ascii?Q?Z4y0I4pEuegEzrH51bSyHJKaoduHxnCCk2em2QzQ/HuJrFNDRxh1Ij0qRdsO?= =?us-ascii?Q?Px1mYm+P+84MroWYtIpMxNRh1GyKc1DkXGW79xmQA2cVUCqhIrgTbTKQW79E?= =?us-ascii?Q?ilj5BO3kG1u+Eqxqd/9RKNRi3FklKLyLF2E4kxrF07LtJGjaOyLijysO+etc?= =?us-ascii?Q?xxOvpLKnXJlR8dWPPGA7Y2mJqCMZ5au94TRKGC2rpu9PU0DEmZsBv/PnvzMN?= =?us-ascii?Q?hccRoEAE4j2yKbcVQuJ35+2EQ4ySFvAJWw4LqtDE1nfiSmdrw74Gr3puYB/z?= =?us-ascii?Q?kDUMi8/FyUBpHKaGpwKzdDR1DybO14X4nJUNH40OjlZrk6Qcikwg9Uzsfhs5?= =?us-ascii?Q?UNY/7tdnJ//jOM2+AVssLtBUW+0+p18//1p4CDCxHV7d9adwkUKMbpRDfYmT?= =?us-ascii?Q?wqpe8fWYct8zTNcWxHby2cjjTwuZlbXtdufBnNZGm6WjkD8Aa188aD4xu6WD?= =?us-ascii?Q?gYuwv4IsHYrmMwyUiMVj09fBJoRWiPskpmQX7XO9qlLY/xqZCL6B9tZAvRvT?= =?us-ascii?Q?jw8Q3BiFb9lcJyKg/dsRWCmLLE1foO3Hwv7gpB1Cw8QxTqNe1bH1+WoDjbI9?= =?us-ascii?Q?83qqynqWDPjTi4ypZp1mzRCyzD4J4Gi0LphhZ8ntoDE40Z3Oxdq0gf+otZRA?= =?us-ascii?Q?jQoDkE2dSepeI+JynUrPMovCHnUMiwdgyJSMEBAL/jax9ntLWLtAGx7HC/rV?= =?us-ascii?Q?qKR1PZafgxPnJVh1eONTDAh3e5YhqQcnH0Zifq2domLsKLw0euhaL2J3YiKa?= =?us-ascii?Q?UyF4ibeauBUYApfMrYbDY2A0mHo1FQ6jOHAuIsqKvgdzLXhK9cbS0CeGFNcD?= =?us-ascii?Q?3xn9vyHf7fiucUTOyWQYOp9b3dOSJaojJ7+scIHoQr3W+KR3GBxed2sMo4Pq?= =?us-ascii?Q?s127iSibLpZ3UEJ6O/D9x8shIaHwy7aqxIycnI+6KqTwTGrHqwRJ0o6rELhC?= =?us-ascii?Q?lFs3Q9STlFuEK5+DM6BePowT1AibhRYybX5agXWm+f/gMA+Lv0YRsbuME+Sh?= =?us-ascii?Q?Lp8zlnpin/PoXpORbX4jsCNMyemn92a3+BMNJy7pbvHgJZ5h2pZX4uJ9l5HY?= =?us-ascii?Q?htkn7aLEEgdKiMFb48qtrEFWJlqqc+iaZhnFtoCkzv/wbD9Ma8D075BvkBJW?= =?us-ascii?Q?ai+IK73DNgqIIRJHOwIos+nAMGmoAxGVpBJ3ktFH3ABrTRl2j0mQ9xoRqhDx?= =?us-ascii?Q?FPgfZI4E1YAFCIUVohRQSCC8hwL+EIk=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 27e9a72c-417d-4c4f-2b45-08da3333ecc2 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 May 2022 09:52:13.6404 (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: IQffhU8X6PhHJjtnY+MxlqA85xiLi5LSQh4kMKQZEb7lOWvG0LK6CBiAVfVgG38dTbkGQX2EOTQEfGLNxZ2TZg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR10MB3718 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-05-11_03:2022-05-09,2022-05-11 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 adultscore=0 bulkscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205110044 X-Proofpoint-GUID: VqOlTnQm8YrdRti7f9Vl4rkwz-awXyJg X-Proofpoint-ORIG-GUID: VqOlTnQm8YrdRti7f9Vl4rkwz-awXyJg 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 796f27333846..a8d8a9114b51 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. @@ -897,18 +895,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); @@ -964,12 +960,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); @@ -980,14 +978,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 Jun 14 21:08:49 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 7B9FAC433F5 for ; Wed, 11 May 2022 09:54:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231432AbiEKJyH (ORCPT ); Wed, 11 May 2022 05:54:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238744AbiEKJxW (ORCPT ); Wed, 11 May 2022 05:53:22 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9E395F25F for ; Wed, 11 May 2022 02:52:23 -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 24B8os36003160; Wed, 11 May 2022 09:52:19 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=vTf9fJxmP+bRMy17EqHu7rO0MwTklTjTk5ffFt5F7e8=; b=SF4p9H7YnF9edSU1gO6a8xW7ASmKJq6DZqKv0Zw2+9YCRuK9wj/snJuy2Vnupr+H6iNE ZHwfYLbRgJjZz73+aeg3grPo+ItTq7EicIee+3QK0kxcvh9wFwdBhlspZlGXwsavmX5Z 1aqctR1aQkeuG06g3tqitAQiGDeJRnft8PArxutq5cYamx7s2+qoXymJPI9vLUSkP/NH CCENkPeF/SD7DtLtYai4gKvT1d0diKnKq4zEAmZOTUjjINZI+yqMk1ZXnfpnUs2HcLgE 4kIopxy7QuWkgFSUNSbKamYUDs7FCTdH4BSXCSxBAPUm7SXqs0b91KVe94C2OHojMqNM VA== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3g0a04g5b0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 May 2022 09:52:19 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 24B9pRSX030682; Wed, 11 May 2022 09:52:18 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2103.outbound.protection.outlook.com [104.47.70.103]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3fwf73dvab-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 May 2022 09:52:18 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mAi9xCZUWGa842OWBCTVYY5HE1FRglAdt9RxACAujlKDT2EpfxSZqHQiUY4YAHCAxxRtj3N2tv7wOMdzPz0j7hfLGuVAT4qt9obE8iHV+Xq+zOoRLic8hVzmtgu0BDyLH5hvHR3axJj++xcbrhLOHErf2+c7olOF6Fz9dIjJMF9UgvD/F+vT0eYO9jMsk58ABUQGtHJTm0/DutXIcTdiO1NTBIClMlRDZllZeafD6A8w7rF7MwqapsLuuLjpGa+iz0GNJ7ppt1BZ+j0ddxeHA4mzY/56+FKlOigKwJI7wWobVdt9AQnDHRuqECOMkkFg70cVwbXnEfOtmVJZmrkZ4A== 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=vTf9fJxmP+bRMy17EqHu7rO0MwTklTjTk5ffFt5F7e8=; b=mpAie4CftIqKU5WZKNFeQt7mqtup/VVlcbeb8VPzegFohY5jWVTqcONKm2aKA2Zpf0nqnmm1jHx4MpQMQ2/8hiX8QVOMFGWgvlf1LFbGvxHgPhiv6AH36A7QaVoDh1s3kIItJd4qlJUNJNiGbSWjVbPS8gx34rLC0t8zi5X7G/EmJEe04SngrMNv77gg20fhYHj5K6HkjtlxClI41mPXCjjmFumW9nPYYpGAkfUAwrPqDrQEEg6yK2BCsaIxzimNaDKxUxgfdqshuOzoUo0OSKR4L6XEj2wnGDq5h8YFthKYo7QmOq+zuEt4B6IKvMguZ6PAn2Wpwgou6rUjbPFv0w== 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=vTf9fJxmP+bRMy17EqHu7rO0MwTklTjTk5ffFt5F7e8=; b=mzoSvYmPZ8WKHwoqGpZIoFvW4y8hGDdTsRhdXnHIcIYHq7prUWjNaU/+nhbHGBsxfox9y1xXxs+gNEQdAHlmONq/CDJqa78AgI7NWZlf4TJ3FmZ+ia7Y8zDQKSo+EYDTollDv9gDB+OsfxlQMux53wdJNsh6KtJXrWuq9TkD7Jc= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by PH7PR10MB6178.namprd10.prod.outlook.com (2603:10b6:510:1f2::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5227.21; Wed, 11 May 2022 09:52:16 +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.5250.013; Wed, 11 May 2022 09:52:15 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v3 3/4] kernfs: Introduce interface to access global kernfs_open_file_mutex. Date: Wed, 11 May 2022 19:51:56 +1000 Message-Id: <20220511095157.478522-4-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220511095157.478522-1-imran.f.khan@oracle.com> References: <20220511095157.478522-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0011.ausprd01.prod.outlook.com (2603:10c6:10:e8::16) 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: 3f65c4cf-ba66-4bc0-a147-08da3333eddb X-MS-TrafficTypeDiagnostic: PH7PR10MB6178: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: UyeimxmGbr9AATuO/LY05F8A7Pcf5HojpviSixf1kPKwSU9py4we1GsuJjn+yiI2eIkBNGA+pVdZL6gko0KX0xnhaTp/ME+r492uA1KyGodH+KXRIbyWf6n4GZGVt7KVZo/wLXAP67C4O7G4WA5YF9p72gwb6U45PN9qj4J6GamnIZQdf1FzwLb8qB0vZSwVYSAVt+TojrxtbZasW0SGZC+WbKJBFZ/jUtxJJ3/fk1nwTN5s9Q9byXFo2WKc/K/pYSgiv81zwczWKLzBuAXc8NoAqbOoCmbcAQMg+13taXzf+sBR8HElxRtGCbhrPF1YGlDPPsbFngwUaHljGPzozHuknbwIxlMDMmz9LFx9zxwYACBRKCvXQ7BssT0RSXYIgh0iJz1otMuFqNcAYsUIDy+zCZDoqFzXCF2VXz6lL8aZPIP8vMJnhy0/5M4fYlpQvBKjoWOXeyiYdCeiagIuYIP0uES8wTyunyQwufE3bKUTgkEFQwhQ4nzxIbl0DP7+Z/wKrh6BnQ0/ihro1sR8pDwbUlUlpnX+jiSi1Qrl429MBTj6hX22W0mZBGaNJ4wd6QOk06egkvOMyUYpW3j7jlLXIlO3XvE/Q0BMNWVxMxfnR/LAaAN1L75fl/Nj2bJUG6unOWWMb56r04J1wA+ODJLS0Yqyvy+fPnBgFJ8rK1gxHL+rT47cEh6ncCyFRLl8cC2L0ghm9pw9bphjfXkLiw== 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)(6666004)(6486002)(103116003)(86362001)(5660300002)(52116002)(508600001)(6506007)(2906002)(8936002)(38350700002)(83380400001)(6512007)(38100700002)(26005)(2616005)(1076003)(186003)(316002)(36756003)(66556008)(4326008)(8676002)(66946007)(66476007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?8SDNBSSXSYjDHkO/GGTgSJucS8PxB+c6hsA91MHwVdoGzkNl0+Xeax8m7Dq2?= =?us-ascii?Q?Z2WP2odQslLiXt75LrHU7XS5DNHkIsq1bBIFOhiBzMVI9eL8tPuYAUYpMmJb?= =?us-ascii?Q?hhwvMl2FY6Ri6d8crEWf5n9ERs5TYn8dtn/5BtSesN+K7nUALmZHn1GAwO6l?= =?us-ascii?Q?qh3epF5Dbz6oFDRBXM8Qf6Za2xaRV4Tygu+rjqqjiLc39dC+5RgtybXNS7jk?= =?us-ascii?Q?0cCF0ald3ZRwtFS6HCa5CW0mJBynWkRDCBtU/TrbADQjLksi6L/RuMEKOqff?= =?us-ascii?Q?O9cRpl1B2Pffp3KNpo3HzjrctltjrgTFkreZ5HLpRGsQ3JPs5ZER4wZTUtij?= =?us-ascii?Q?YmcGujSAiBLwk2R5QzrVSdzoCfyNU5iQMx2oI27C0v+i7CflQrozBG06aiDl?= =?us-ascii?Q?FuYLsBPOkzvpPXFdKT55OTxf8Sv+NNWCbGQIl5NTWbxk2bey9JgDpculluv2?= =?us-ascii?Q?Zqsz3k/JCFe/pClCLrr5ZOZ5wwu/HcbDOC0L9KS2AZj9UAA6WYgXkkyaylfY?= =?us-ascii?Q?o+fYgkzZpJHl518SKLbkpp6o+uKTJanA9niLa6nT8DfrekjQTxOJWldE+Jyr?= =?us-ascii?Q?LHp2/khuFrn3ZpA1Lf5N48AM66ovYNDuWJjl8TuJw+E0MSqqZNRpr+D4/9AS?= =?us-ascii?Q?VY1i5MFzSel76mSHUTGRgYdGJc7X+Dfpmj8uHTU+xIdor0m7yqdYEeAZYXA+?= =?us-ascii?Q?OwriOFtZgYvU8WpoYq47YfcwKgaZ7UPjzqrrVhm78cVazUQXwzKRAHJ3llik?= =?us-ascii?Q?AwWo35PCgXyhTFbNPS0OOYTHuEwVTdByfy4QXiN83TwrvNSnaIkO1zRlygRD?= =?us-ascii?Q?hSJJRl84DrHHMqZgfOrhTpkpVP7+7lyLvE/zy6jQ1rpypZTfjybRu816D61v?= =?us-ascii?Q?onx/hDWJsAEgv775F2ZNxsNpRE+LaQAaRe7bdrY6VdjdhZ0accsCxhX8KjQq?= =?us-ascii?Q?YNxUKuwD6KkxzdfR0sUdjAYxCItxLl+Arv99UI3VKFOo/3s6Gk6V9s6arUVx?= =?us-ascii?Q?R6yx81oo/vjxLYdb5rQQhKIRPjD4IAFjNUPLXmNo3czEfePSa+M5N8Lynuuj?= =?us-ascii?Q?k+mjN0dTJfYHWm8Y1+GH5MbV0LK/Tl2Tup1/Nm8WY6o8/tkUC0f5Hk0GjTJP?= =?us-ascii?Q?PimKbc58ULQhMGFzLsfltBz3zMwPbmzvYV/fPHNw0RwgVVviQi1dhCjnW4Wt?= =?us-ascii?Q?XSHmDef+btWWHYbaAHf3cbJMEcRoxgyCqSXIgV3BcP1PyPFpacMPxLG2mZbg?= =?us-ascii?Q?YCLeATp3WHcKtba6uQfIrLiD5MOTQ2b6ImquHGFDK+88uXABP1zUgcIaVN/4?= =?us-ascii?Q?7uOAAOvzHO0bUYhgge+82+qNp53QxgEhPbhMu9yqxh5FeSpDuLNT7RhTQUa2?= =?us-ascii?Q?IfiBEkuzlyKrX8/hy3c8rMkP/G1vMFb1gkOza9TjG2wwagOxErh9535J18GE?= =?us-ascii?Q?Ux+nzOpERgJVTM6sgA4Wu4+t3NPJSPd/1L8wDwagdLfdBQwfdO6NOsxZrLNb?= =?us-ascii?Q?ceknExD/UsVCKc84lOSrmEWF/5rh+BC7Lmt9CemXNMZZYmdDxajhNqpLbYRm?= =?us-ascii?Q?enJeKV77wHe1PaqUhhvp1GSvQFO20Fb2LiZmV5e1QoETDQsoFutoa6ipF/Fh?= =?us-ascii?Q?Jwxvi1zoda9oFx4Kl3hSI/LAhVcqotTZ46mGgban+PCQjVkIJ0bP6hi5dZ1r?= =?us-ascii?Q?Zlix5UpHZZBzJw2R9aIXRsGrS+Lr5Q6fcW8ooGObCxI7tmoGejMgYT8FQtgL?= =?us-ascii?Q?e2KV+AJLGj4o9r+62s2E3k6vVSjZ2gc=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3f65c4cf-ba66-4bc0-a147-08da3333eddb X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 May 2022 09:52:15.6412 (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: KlNX+f0ARvscWpaXNFNIGOmItcOZDEausDQuY6l3OHCHcWEfG60MGm/X8ImmFvijk4uiaifGat40BXW7LtPctw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR10MB6178 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-05-11_03:2022-05-09,2022-05-11 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 adultscore=0 bulkscore=0 malwarescore=0 mlxscore=0 mlxlogscore=963 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205110044 X-Proofpoint-GUID: Zu7m2nVPGmw5G1uN9f4w222JerbGIyX0 X-Proofpoint-ORIG-GUID: Zu7m2nVPGmw5G1uN9f4w222JerbGIyX0 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 a8d8a9114b51..9000c85ce1e1 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. * If both @of->list and @kn->attr.open->files are non empty, we can safely @@ -79,7 +95,7 @@ kernfs_deref_on_raw(struct kernfs_open_file *of, struct k= ernfs_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 /* @@ -90,7 +106,7 @@ static struct kernfs_open_node *kernfs_deref_on_protecte= d(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) @@ -569,19 +585,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); @@ -590,7 +607,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; } @@ -612,12 +629,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 @@ -629,7 +647,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) @@ -771,7 +789,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) { /* @@ -788,11 +806,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); @@ -807,6 +826,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,10 +842,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 @@ -839,7 +859,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 Jun 14 21:08:49 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 9334AC433F5 for ; Wed, 11 May 2022 09:54:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236943AbiEKJyt (ORCPT ); Wed, 11 May 2022 05:54:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232685AbiEKJx0 (ORCPT ); Wed, 11 May 2022 05:53:26 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EABCD60042 for ; Wed, 11 May 2022 02:52:24 -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 24B8QrAa024470; Wed, 11 May 2022 09:52:21 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=6E1gBTMkxYHgvMkRLoIkGcvNZJNeFZKDzjzIA9IBXsw=; b=nXnJMDBB6JCtWaHHF/V5k3/gGl1vH3c1fZY/04RfwLdXewqmPAkjRRE0OFyzXoFj9ai0 jensngIUkT/vecm3WSZpBOBppRQcey+6v01HVPeoqWTHZixmxhBqnwgxw83eiUWdScEY E/fOdK6J4H6y64KmLw4y4A0S3bsO9+9Sqt8raMEwayrImYcMMCCDuOXHySm5mdk8Z9bG TIEob8we9yVEGSN9DFzMtY2+3iVePLPAXzkX31hWBCx8Kqca1QuXRph+R+casKGbMath cqT1Y4op4VPSWCBhcUX/yMqr1cYvLkqZT/qIYmOIFbEvVbb6+yEBBgU/faRn3oowHQ2L rg== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fwfc0s7tp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 May 2022 09:52:20 +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 24B9oOaQ028160; Wed, 11 May 2022 09:52:19 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fwf73w0pc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 11 May 2022 09:52:19 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QjV4EEaK5LlS/+QBK4iCvnxbb/6MqRk8GcS5Sc7CFlj7uxo0CRTagWppIEQ1VdISNmmdu/hm5UMsMf7nBDxaf/PcfPnRY1Z6aIauzc8C2tqYtRK/dM5xnqpmd1zE9Riu5ACufayaczhiaLzIW8m/zPwlHcKgcf5c1NaJpVb0qxWhtHBeOSmdUcheCLAtpjQ6rnoxoG6UqnNj7HO2IdTzVS1aXv5UtLdLi0TzoyKtuyVNpkSJSCXqAUTC476W4MxT0V23yjpIfsoBKY9NhZDdTSnqTPRoDmjskyqtZv8mJTz6mGopEyx3iPCCzuXRkrZYn5/RelMSNx6sSA0rDBMjvQ== 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=6E1gBTMkxYHgvMkRLoIkGcvNZJNeFZKDzjzIA9IBXsw=; b=dLu0EkbHiZu/msrstInZ9jFXpbXlPgjR4GNtNLqKtn5vE0rj6ihv2/KF/DE68gilL9JwbfBunz2FGa8MHdQwlD0eLbCfi4EMuB4Ml0dKlzhnXv3fZ8MesuaDp56TXWYDip6x2e2IGZi1cG6Hg7Gbu4QWL6aa6+hsZEUx7I4r9hRpadtjU0E9b8AWOTnagCAs7DqWekxzV1pVEf8Fr8ad0jXY3jC2D/XElVhgnhlWuEYIo/7Vxn0fKMlOl5MFttz1+0707fjaowFpDbXTfjVmeqeGpxxOrHtMOB9QLHZzcFS/728Az8zaV6yqLdsgqGp4HhurN5ytXa1uxva+jVwBrw== 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=6E1gBTMkxYHgvMkRLoIkGcvNZJNeFZKDzjzIA9IBXsw=; b=Tr5jppErlcdL4aElTNMJ25XqPZWKPqu4FKdURxTXn+ks+0riKYDGDVEOAM76dhlOLGPYDAfQDuJhf3IkjxHp7mkL/XjSNDFuDQeKRHzOQtBuUR5EZ4c8FjqTsKfg3k0DGwCf584//h3wQMJVOOI5eFAI8YIL19XWH6mlJ82a7SU= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by CO1PR10MB4785.namprd10.prod.outlook.com (2603:10b6:303:95::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13; Wed, 11 May 2022 09:52:17 +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.5250.013; Wed, 11 May 2022 09:52:17 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v3 4/4] kernfs: Replace global kernfs_open_file_mutex with hashed mutexes. Date: Wed, 11 May 2022 19:51:57 +1000 Message-Id: <20220511095157.478522-5-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220511095157.478522-1-imran.f.khan@oracle.com> References: <20220511095157.478522-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0011.ausprd01.prod.outlook.com (2603:10c6:10:e8::16) 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: 9c6f727f-4fdb-4a07-1d51-08da3333eef5 X-MS-TrafficTypeDiagnostic: CO1PR10MB4785: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: 4+TMl8rTXLhSeq7tsixMaxo3cLYEVNaf4q1XnErYWcxquI0HVABLGn558rau6TI9SmM3fORaNba8YabiUMTJWftn2i822Mo4hnXFcr6qop9QBgzUAKICY72e0zyipBzvOcU7MOHmxc7GzezbJzBputpFwKJphhcFo/5eMOVB5dGXuW21UeTcXuIWnG5lnEloJcrOym9VCyg4kJXJNSy9vKvhBjCO7d5yHugHpH1JHGcnGIYJ0xd6MQpfErHEpPjS+NOplh4L5SFf8bWcRbYenAZZQfnLlPcuPRCbT8RR4xZj2HRJoKf9n8DVoqEnr+bDioYq1Dc9CPLIa6rX+0gcyi42OjgrBB0OnQWvrBKg3azEq9nRn66S1B1YG/NqSdI5oQiY4wi1MfOi98xtK4vPSGSAU6A36GE2Ns325/tPbI1PD2TARP9CAXtP58kFIs9bvGlM9TFUXPRRt4rn9k4ggL2UU3iy7DIJJdgTql/U8t0i0+EwDVExSZsC1EQ567MzmBARcfDRk0jET2tmR2gpqyTYydJgZhkM1Mg6ZIAnA8T9ZWCg1+ZGKPas4Zg1iNX+FOtPXSIjgkn8AU/4jCtU0cU6i0gKQjHX3SaxDNfNY8694RNqGAEGgdQpnSKpuE3QNCAc7g0ROFkK+nlShFio1Uf+/z0jlKzRg4vn6+gtKylRajU14ung6P8ikQVuBJBWB1KF2JEaVXW8WIcbWQygTg== 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)(38350700002)(66556008)(2616005)(8676002)(1076003)(66476007)(5660300002)(4326008)(38100700002)(8936002)(316002)(508600001)(36756003)(6486002)(103116003)(6506007)(86362001)(2906002)(83380400001)(26005)(6512007)(186003)(66946007)(52116002)(6666004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?Z+VQyOLTFwN0JSDOgkD8HSnIGPmlv7dB/rgaIkbm0IAIry+lyjzlYlus+yaz?= =?us-ascii?Q?w/c501x1L/ScJdXsBh0bt6xDDHJlHX7EL3RHsk/+EqsweJQ6xsHVCtfXx4Qg?= =?us-ascii?Q?UMHtjBEVbS1ESFmHmuUidQSnM9A7DhcDbrE/TMkpv8aL1xrzm1gCMIVgefQN?= =?us-ascii?Q?DWh2K3AcG33v7Rd2gZNQ9Jmkyer/nfF0dI5I90SEm+zOKicLLYvuclS2+wlw?= =?us-ascii?Q?/CvmGdVh9YwaMnGE9KQ2WMJTmxWDDXA7kHuHgRjoTNUBmEsKcxv/ORJA5bQi?= =?us-ascii?Q?Z5nHo2Yb0HP1ysSbOR8sjlF6/+UiYlIFeSPE4dPjBmRon4Ht4aFFXtihnlqO?= =?us-ascii?Q?oJrERAvG+ziNDu950+NHJu2j91FXuTs284P0Q4lWK2XhqBA/XTQgZT6rQTFw?= =?us-ascii?Q?RkEACUOfHqwMOUhtryIUY82VVoiOVNYrJWKTXS2dsy/9degVXu+LfL2z1IAY?= =?us-ascii?Q?OMC3qF+GvprxTQZCet6MTe6V7A0ivU4VjGjx9Xz2UJTJo1jzlD2P/IPb+YIU?= =?us-ascii?Q?0U5Dw3tCFQjtQbT7VlFrvZkE2iFNcPQ9WBx88uCNZYlDiN9XETikPvx5kkfX?= =?us-ascii?Q?ObxLEHqwrkA8xH8yRwdWqh6RLNA2+Q5majj2PXZD1oXlnTvCsdlX7jvdCvwU?= =?us-ascii?Q?ylSB7lk66JZbm2MzByeg8Xa2/B45jI/w3aPwc3qpLuN+EYWDopMMf4YcOoJZ?= =?us-ascii?Q?9b1aqw8+syseIdso6AXJfIiuOcYxS3daQ6r1O6OdsduylPfO+O1ne1qTk2dw?= =?us-ascii?Q?Mf6z37TtbyDvsUK/LU9c3VqtjbIUACuEfU/OZJ+OSFJOOKoPvl4t3tnma3OE?= =?us-ascii?Q?eAsLU0KJROGbKTrW9Hor7T7WNS0nex81BRfaSFfksi4u1rkQAMA2BM3dpHQ2?= =?us-ascii?Q?IcyPzCwtqpkCSWSHw1CCdLp30epEsPScMQKKP/q2kaIlQKXe8kKm+8ZtCG0C?= =?us-ascii?Q?BBTeVe/iNAguWcarjwtgWIN9sOSG/mTPDpZJI3+GNLqf7BxrIZFH7+/ng7mE?= =?us-ascii?Q?U1IjAP+GFZXxT3zpb8gKLW6wiRmajObF8tU7C0oWKGvMocn7qUY7vEOCA106?= =?us-ascii?Q?2V/aWJvY+QHbfTzvCTm3ajxIXqiVQEh4ojET5BQmXzW8soRU4F6QcrwFIF0T?= =?us-ascii?Q?5lxcUSXJir1r8ZnnMPvJXCs3ZLm19WO1VMAgA4LYqg76IyzQ1cUkALInXG/3?= =?us-ascii?Q?HYnxSQPx8o9/NXC+DqJmnRO9bdUid9dEv72oUCswNIVU+2kEvOYvbA8UYTId?= =?us-ascii?Q?c3YLFIOlWj+Ne2TD3ViTEg0EbD9cDQEEhaCMkvIyiGAkPophjyF0sUbwHbXB?= =?us-ascii?Q?l1CVIOiG6y+qJZdK6KvxiJKRZ0sIW3Qeed3jri4GG7qZ+pRTZrZXqOHeIBpn?= =?us-ascii?Q?9hK7Pn9vyNJQIIpfx7tubpbbKpEf9gsDSt7vNKL5n77SDSAV0+Q/90mV9SNA?= =?us-ascii?Q?ePt6ujkOBF/OwamUoNigoyTBRhfEf2WvIDS3Ee84UACr6mh7m2BPH9pLhh/e?= =?us-ascii?Q?L5G/PfkujrvMsAf1f+ZfSeekPlZw5AW44Fzyd4u8XpWcQWlzTrUeCMElnAg+?= =?us-ascii?Q?6IjDAISzryqQt4IfU3vvrjnQvfOkKbQXMw5oxIkNYXi+N6UgaB60+3OnHGM6?= =?us-ascii?Q?lJj6u6Uu/gZYcWaGCxV2VbRK0dLj+NChrcl2FCDxHB0cWOTJ0YF3N2WfquWZ?= =?us-ascii?Q?jvkgBKSqvSp5Rg881YRrw2u9N9jQdxOE6gK4EXkrBvKS5w33YMJSj0z0sNSJ?= =?us-ascii?Q?InZAlxS2Nq2E/5bfOV+X97I8V/X1e5U=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9c6f727f-4fdb-4a07-1d51-08da3333eef5 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 May 2022 09:52:17.3297 (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: cu+W6COpFQP8aJDsLuqLZfLVRBBZIkrxQm3by1vOoC5FD4kScmwazC2IXNtKtfwDBZL2NAVAlNB9d+0b9gn/kg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1PR10MB4785 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-05-11_03:2022-05-09,2022-05-11 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 adultscore=0 spamscore=0 phishscore=0 suspectscore=0 mlxscore=0 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205110044 X-Proofpoint-ORIG-GUID: ipi3VnwZeP1nj1ARIRKBakRdDVd6Nbd3 X-Proofpoint-GUID: ipi3VnwZeP1nj1ARIRKBakRdDVd6Nbd3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In current kernfs design a single mutex, kernfs_open_file_mutex, protects the list of kernfs_open_file instances corresponding to a sysfs attribute. So even if different tasks are opening or closing different sysfs files they can contend on osq_lock of this mutex. The contention is more apparent in large scale systems with few hundred CPUs where most of the CPUs have running tasks that are opening, accessing or closing sysfs files at any point of time. Using hashed mutexes in place of a single global mutex, can significantly reduce contention around global mutex and hence can provide better scalability. Moreover as these hashed mutexes are not part of kernfs_node objects we will not see any singnificant change in memory utilization of kernfs based file systems like sysfs, cgroupfs etc. Modify interface introduced in previous patch to make use of hashed mutexes. Use kernfs_node address as hashing key. Signed-off-by: Imran Khan --- fs/kernfs/file.c | 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 9000c85ce1e1..175c9f53284f 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