From nobody Sun May 10 12:50:05 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 50D10C433EF for ; Wed, 4 May 2022 09:52:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347817AbiEDJzg (ORCPT ); Wed, 4 May 2022 05:55:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347773AbiEDJz1 (ORCPT ); Wed, 4 May 2022 05:55:27 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C962275F9 for ; Wed, 4 May 2022 02:51:51 -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 2448TH9x013484; Wed, 4 May 2022 09:51:42 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=bZPGkS06yWX1KnFgQdPwzAo8WOEYAz5SGlexyy2l2dU=; b=oX7z5+BVTT7aGR4whAWaH5KilbkgC7hxNZdl3yv6wUrWC+NoklLVfim9AL4rjVVNdb3o eL0vYluymqUHg82wI+Ie7bDZheOUmSnDbb8IA9JnYqB3zRkn20yApm2sOj/JWs903/TC 7sf1zGDGHmYj9ah8YQAyhZPZRkMYjwHQcnkXU8x0+nHhpC3jeel+flA5Tlnd3eeK9JHT YoXxiqOxvv7F6b/T80eNQNFG0GIVuEkWbZyts/uKL/gN1eXyhNLeK/NacmvTxJKmpb2O EX+Gi1ccRHBowybabknbhKZiaadBvhCPkbTwwjPPBT9Pm2I7sNXG1VjEoQev9f1R8DA4 OA== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3frvqsg05r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 May 2022 09:51:42 +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 2449jM1w035760; Wed, 4 May 2022 09:51:41 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2169.outbound.protection.outlook.com [104.47.55.169]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3fs1a5jfn7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 May 2022 09:51:41 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XWbSn+QYxDycd2i+1elKXOWRwSYOMa7oyQF1m3XYkltAXA/Nk5ONAo7+O24epVexNpTSYXuSPPajU96esPiW7R6s3tyWEmLAk+ilE7C8IAv8XWaDlWWERU9ZlABYW1+2qiUguf14Xcqr8Rf+nwsKUrbSZnoYvttNLWLEvNI6Lp/QbG+5TVkiv7CCDtfaGhDOr68wBDkb47q05EZm1VYfoHiAGjs146bTPsKiwglC6UfKTfT/dqow+4DMwZb+bGNIHGPgBDkuZc69azgmVN+qJgw0hP66sof4xl2E5jCpQItBcYoTR1i6X24I1a7VZ076jmAufkRgZdMLQKZDX4PZ/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=bZPGkS06yWX1KnFgQdPwzAo8WOEYAz5SGlexyy2l2dU=; b=JVtTqJbC1ICp6V5M45/9MPjPiKTyOvvBj/johcO3RXZheSRdrkRpZN2siAq0CpoatI8lwHfYMEus54ccWuFwpHAakj9OtDzvJNVJ4RPGy8ArmDLPtuRTG2L88lK7+rnkVf8lW0Kw1a+G14wZ0WtFTHKq+HsJHR5vxdoRDlYf+bSlCeoU+BUio+ULLDDB3NdxDn3mV0oZqKd9oNqA12JkEN/oQVWR+04KzpU6GM8hAJHVBET/jSvzgiMZ299nbrkMn2anHhfdynSMA1XmIfQ6dZbd5q0qrHWOuJJOtdm/w0ewt5I6Z0pFx5I9NYYJUJGup0vI77qozkz6H+4n8FVqLA== 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=bZPGkS06yWX1KnFgQdPwzAo8WOEYAz5SGlexyy2l2dU=; b=w/mMv7WsmNSHZj2YVf/k3bp9qqF8J+uF26+H7qMOIrcSerVpCJTPTA5QDiQFBKQ2HaEZG68gHWLuhP7OjGYoTRICMLy3MO4goWYmoXmXUGQYOhv7iXhPCCBexbSM6LxgFfr8j61MdyylqrM74YrccO1BBSOGmd4hfbft8Qhq9Yw= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by SN6PR10MB2702.namprd10.prod.outlook.com (2603:10b6:805:48::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.28; Wed, 4 May 2022 09:51:39 +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.5206.025; Wed, 4 May 2022 09:51:39 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v2 1/5] kernfs: Rename kernfs_put_open_node to kernfs_unlink_open_file. Date: Wed, 4 May 2022 19:51:19 +1000 Message-Id: <20220504095123.295859-2-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220504095123.295859-1-imran.f.khan@oracle.com> References: <20220504095123.295859-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0203.ausprd01.prod.outlook.com (2603:10c6:10:16::23) To CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 55ef05bd-9e40-495a-6901-08da2db3af62 X-MS-TrafficTypeDiagnostic: SN6PR10MB2702: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: q9x8e7Niz50Ks3givBWn5s4XJepRuErS83mqM7Tww63uMZw7Nr20MrsLJRNnAU1DZ2LwOkQZ5oTnCMM/YGq21B9lats3/qH9cj3nTKUtUWW4KUYjCSZDDKb3lj4h19YHcH7gEPUMzpH5eheZ22Lhj8g1svmGx5SUficCskkeZgy5juBgpkVndsCsX6tY0g4mweIcmOfb//pSeu93Infy0ot9SohT49d3IAKgVu71RyLEM03NcVgnLNUBQmIgLq05ZyOQ3II2589f7XrJJfbo7pMvVm+RPlBucdq3Qn53q7W1qxbWFQOCUho6Xlbj0oevSwXS1BhJk6QzLtSHVJdzzDbPfnRRipAVdFAwvOF7j/DfxB80ruoiMxqIgvdsjONBGwQdtkXTnarDvOlIWEszXFSOPnUg8RjnyEhTJUXZMn14xQl1qZrhkiJgpleq+OG4gv6p/yvTbxTOqpEMD3fi8/MnA6Fy+pQ83Tic1jEUW4KJFdq+TqB/PzS5gOZWR58SRbZt/j/V3DjL7f0dllBqbNkMW8vWzChR5mGMwxxJIudvRpDtGCYFUJu+27ivZOFce+hnPt8/eRORCbk1wiKBK4bE4mIaI0q6QvkVdc5AlGeBnXGjIohI+biwPUj+6zostX8roCk22PFZe07lj+aPYhDJfrh/0iDg5pwGLSQFYdg/s8XUBoidbwGXP0CDrEWi+Ryqp3Kp4DJoxuFREI/h4g== 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)(38100700002)(66556008)(8676002)(6512007)(4326008)(66946007)(66476007)(8936002)(6666004)(5660300002)(316002)(508600001)(2906002)(6486002)(86362001)(83380400001)(2616005)(52116002)(103116003)(36756003)(186003)(26005)(1076003)(6506007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?k/+w2/7P4LojH50QBZqKkifP7mkNGEsxlVm1EIrE7fhIzGQRfciUrRjn192u?= =?us-ascii?Q?R2KgX4c4MLTjX2D0B8ZfmFGdaJ9CIyiqXGoEbkETMZhn0UPIiJrUU71O0LH1?= =?us-ascii?Q?BGPI4w+Vs9ucUaMrSs9ipvigS6h3L7INov/qHdP6y7ichYZw4PCJt+mauOoS?= =?us-ascii?Q?lYc/HXdlDI38RetNq0Lx7fgcia6VGmkIcfZcApsSicXokYiWN1KhnVXlvu8D?= =?us-ascii?Q?sdpkTpdlWbhWQ3iCcNLGtx8K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ascii?Q?Totzw42JO7tKNz35AVeiZBzVIHwYU1UbD0Nq1x3AdMQfc5RAmM8/d+kwpBtM?= =?us-ascii?Q?aeHOCL+UoRcn1XjgqmgDCZXWshw1vLpZpvS+GEjT0Znv66gCFeP5aIdEnY+l?= =?us-ascii?Q?rX1B+SQHB/EkGKYWDGWtPfz4XmWwyQMZSCbP9BI16dMTlfYRQuF2QnnULTW7?= =?us-ascii?Q?02iJMfslPkkDGVOcEosMH6jmMLXuTYITdKeYSDILIDhAaAd6JmLZIYBQrSxg?= =?us-ascii?Q?8So3vUBjE74+1mV9DTiVmqYsyyjTKOlkYGVlAx/EVT1bmrrbEadZM+VHi74v?= =?us-ascii?Q?NthKVBa++BAakNn8gVyCiGfFpk6g6r+gB6qJiOzl5CygjKcV/oXvLsD94p4k?= =?us-ascii?Q?1qw0UPRSz7NHL3up+nqJBX+XW64CRHFGiDj1cEFa7qKHlIfsXGNZe4WpxnLe?= =?us-ascii?Q?4JjVeCRhBe1kXPNCq7m8x2bKyAFHqnMGljVC5F6HfCY7QiOWsSUzxNUbhuWw?= =?us-ascii?Q?3MxRtW32DjAVLVjunSmv0XaqBJ2YrlI5Npjw6g/IXWIgVOEFSLbV1quPEZaI?= =?us-ascii?Q?HadXVNwxWXDjuuaVBAYS0GlQvSlJZNSjLSx4+lNksM9i3Z5JF1r1GWpQqJNu?= =?us-ascii?Q?hOLfh0Le6qY5BTeYl9KxV4tzI4xMC6E=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 55ef05bd-9e40-495a-6901-08da2db3af62 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 May 2022 09:51:39.3547 (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: yeEL7udoP8EjKy9yuyA+XK/N0tI3CiAKBHyohnH2wPsQ0ekc1YVz+foHq4ClksOrSQpJtZyc9aIO7GYRV7NL1g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR10MB2702 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-05-04_03:2022-05-02,2022-05-04 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 bulkscore=0 malwarescore=0 spamscore=0 mlxlogscore=999 mlxscore=0 suspectscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205040065 X-Proofpoint-GUID: tksyVwjIRgZYppqzmHOSh85KegIdLYgi X-Proofpoint-ORIG-GUID: tksyVwjIRgZYppqzmHOSh85KegIdLYgi Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Since we are no longer using refcnt for kernfs_open_node instances, rename kernfs_put_open_node to kernfs_unlink_open_file to reflect this change. Also update function description and inline comments accordingly. Signed-off-by: Imran Khan --- fs/kernfs/file.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index aea6968c979e..e3abfa843879 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -552,18 +552,19 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, } =20 /** - * kernfs_put_open_node - put kernfs_open_node + * kernfs_unlink_open_file - Unlink @of from @kn. + * * @kn: target kernfs_node * @of: associated kernfs_open_file * - * Put @kn->attr.open and unlink @of from the files list. If - * list of associated open files becomes empty, disassociate and - * free kernfs_open_node. + * Unlink @of from list of @kn's associated open files. If list of + * associated open files becomes empty, disassociate and free + * kernfs_open_node. * * LOCKING: * None. */ -static void kernfs_put_open_node(struct kernfs_node *kn, +static void kernfs_unlink_open_file(struct kernfs_node *kn, struct kernfs_open_file *of) { struct kernfs_open_node *on =3D kn->attr.open; @@ -703,7 +704,7 @@ static int kernfs_fop_open(struct inode *inode, struct = file *file) return 0; =20 err_put_node: - kernfs_put_open_node(kn, of); + kernfs_unlink_open_file(kn, of); err_seq_release: seq_release(inode, file); err_free: @@ -749,7 +750,7 @@ static int kernfs_fop_release(struct inode *inode, stru= ct file *filp) mutex_unlock(&kernfs_open_file_mutex); } =20 - kernfs_put_open_node(kn, of); + kernfs_unlink_open_file(kn, of); seq_release(inode, filp); kfree(of->prealloc_buf); kfree(of); @@ -765,8 +766,15 @@ void kernfs_drain_open_files(struct kernfs_node *kn) if (!(kn->flags & (KERNFS_HAS_MMAP | KERNFS_HAS_RELEASE))) return; =20 - on =3D kn->attr.open; - if (!on) + /* + * lockless opportunistic check is safe below because no one is adding to + * ->attr.open at this point of time. This check allows early bail out + * if ->attr.open is already NULL. kernfs_unlink_open_file makes + * ->attr.open NULL only while holding kernfs_open_file_mutex so below + * check under kernfs_open_file_mutex will ensure bailing out if + * ->attr.open became NULL while waiting for the mutex. + */ + if (!kn->attr.open) return; =20 mutex_lock(&kernfs_open_file_mutex); @@ -775,6 +783,8 @@ void kernfs_drain_open_files(struct kernfs_node *kn) return; } =20 + on =3D kn->attr.open; + list_for_each_entry(of, &on->files, list) { struct inode *inode =3D file_inode(of->file); =20 --=20 2.30.2 From nobody Sun May 10 12:50:05 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 7EF20C433F5 for ; Wed, 4 May 2022 09:52:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347798AbiEDJzc (ORCPT ); Wed, 4 May 2022 05:55:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236064AbiEDJzZ (ORCPT ); Wed, 4 May 2022 05:55:25 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F27D4275F9 for ; Wed, 4 May 2022 02:51:49 -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 24484mUr030007; Wed, 4 May 2022 09:51:46 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=dG7GcFp5EYK6YZJsbVee6Ls20dkAdgDheh3ibk5p4t0=; b=LS8fARNbrmjreIRIAweN+dk/mjwJvZqZ5g67GaKkOiOtqAuTSJNdceqkTqmYBM20+cgo aXrEzWLMsUIU2WbxbOiO1x6EjtxB1d55wo1LXRuvwwG1C0KgTz37+/51YZiCZOTVG2AF q/TnyGdMpz25ClV858KRQ5H9iXGBNF12Vbfr9LmQK7MzZcVyUkABgnuo4B8cmdxrL+Z9 bPFir9kFdlmLFAoAkhlXqT6MyH/xB9T9wP/rwd8wsbSHwOAD3xkIlMhxpe6gIIe/Eb1r oxqC9gcdr2UaplUUnoE4tMBpdaYebQZ5y/I9UDSyt5P64PizJIiXApVTkGR/7EW2uEOo Cw== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fruq0g0tf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 May 2022 09:51:45 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 2449jTbM018513; Wed, 4 May 2022 09:51:44 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2175.outbound.protection.outlook.com [104.47.57.175]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fsvbn6r2j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 May 2022 09:51:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EVyK9ocrmFqonnZq3HagoQYEwIGWgSUXKf1AduE8bgNd2LdGGZjqzPVleFIAV5Q2+U5nbDTGwrPo5qNB8y5si2a3ykJcuCcDXnWhzHgj9qknpKCVXDPj1ksMWIYOWXL6446jK2fEgZ1e/KFw0yV/O3c91z9bA1wGZEiuB3Fmiq3AIeUldxz32VRXTVx77czrSy9DKiOLdWwVmKMm8o6THUneFqfdxAfZIY86+NFr8ST+xMaSKgvk4MXUY7739FLPUaP3G04TKgh9G5aq3ci3U3gPs2I+Vqt/U60c4Dw2NjVvB1I3q9dqBzqPA8tB2jkPSSLePYl8uG8XXtcDITxW1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=dG7GcFp5EYK6YZJsbVee6Ls20dkAdgDheh3ibk5p4t0=; b=axEZ6XTS0JvF2xYyh96VuvFSY8fMxot9fQ5e32oGkmJLYSO5oOwhbek+V4xGZ9GtfEV82GlV2DlpCUPI6//AZu52aZeZFff7qySHrIXe06Hd6Ck1p8Q0BbegoiIDrWSAm7rRYaeb0LBQ6JFFTAHHePKmc/xvJrYqDMmm/wyG9ibbQDmrAIyrJYjhPHvJF7dOsqseqJXUZPY0scI4MtmJy4nZAfzbHavOv8M5mMamcmd0za0ubS0UWMTagsap10nUP7zY+7X202/p8JtGWJRNT9SkHJWf+uWAMDunPe64kNDm4GBoriJ2yniG6j85+k5o6KS3Oh7Z9fZPg63aO3AUhw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=dG7GcFp5EYK6YZJsbVee6Ls20dkAdgDheh3ibk5p4t0=; b=fNeCVYsTP6Y0uJxxkqUzNN7RlmismCCN5NfxFAHqYgQbnNjxTpqAkBsi9PoFo+4A+elrSq1RCxUgOyhJdBSwv/cnuhdxXtXOUDOO6L+tH/JhnPKzUIOCBSdKUEarOsN2k1PW1VVceSLFdCFuhMsFaON1lQRNdmSVQzIhupPbc6I= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by DM5PR1001MB2316.namprd10.prod.outlook.com (2603:10b6:4:2d::39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.28; Wed, 4 May 2022 09:51:42 +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.5206.025; Wed, 4 May 2022 09:51:42 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v2 2/5] kernfs: make ->attr.open RCU protected. Date: Wed, 4 May 2022 19:51:20 +1000 Message-Id: <20220504095123.295859-3-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220504095123.295859-1-imran.f.khan@oracle.com> References: <20220504095123.295859-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0203.ausprd01.prod.outlook.com (2603:10c6:10:16::23) To CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7d0dd33b-a723-49e6-e5cf-08da2db3b122 X-MS-TrafficTypeDiagnostic: DM5PR1001MB2316: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: 8yHNGlNmF4MYG181h0xdXscLxs/IbCBPJyGIb4IEwqNBYwFJM4kIBWPHoEQ5cjCU3FixNJhxxL7XoMOZZIiwQZdPeCQM+16Fucp/ZnVBSPHlH5LWvXZEZLCTFF0FULaGCgON3Dd2DQfIIGeVDMfeu/Jq5Ib/NRkI0Dq9IVbEZZ/MMm80JdKQj8OjWVQh6o/oWhmW0bX0xVmWqoi0l4MTHiB/7LyAcX9Vzpb+gZamjCh/lfmrQbTJvlO7NBNr/5Vr8cTcbzB9v03ZfDta+HHBgxzj786lr3UBGL5oXlY13U4C1xxnbzGzegV4m38p7eLS/SBf/j8JqRH/VGQ4n+GlajKZU7myxDWMgjWZPb4yimVlcE1ZghoBlPrPgUr26KRJN44jS6WEJpa4J4/00+3542BubThGyDJCgBrNY1udfzF6FkAfa7iQTHiS7uxVOMr7dRAEmm7hIrV1aiPGgZ2/uRwVEpl/mgeUgFJh0CnDagacoxirNG8prHXgTp1aU6VJAVJex/kXELbIiJIlPy5CsTHGMcz3fpBDCohIC+1Nts8BTUOgiqSA53JBXIHXYrQ81vxRum+FsRth4XlSSk4OEEzAAUoZL54N7k3bkbDKXuL7dv/jLFUPtyMAAreEGSjFnYlqd8nAr+25jjfq65B1D5KRr+JUqu0giMOhhg11zLgwJDsDhv0F2tnHHpYRQHwGgZ76j+u5FaPneng33T5YEw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4468.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(316002)(6506007)(2906002)(38100700002)(38350700002)(8936002)(186003)(36756003)(83380400001)(8676002)(6666004)(66476007)(66556008)(5660300002)(26005)(66946007)(1076003)(6512007)(2616005)(508600001)(52116002)(86362001)(103116003)(4326008)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?WRekcAJeJGMDQIkdEl9TK3xvj/I631a6nNWkaQSKz1h7bwm30WrkohThdsCJ?= =?us-ascii?Q?Ok0DCTnqMGevWVirkNPIC2pocvCNE1Snm/SdJMQXFiCwp5/teog4S9w5vwMi?= =?us-ascii?Q?dF+UWq8dd7EDOF6Dp3uoU71gWzRh8L7VSovQGq+tRE8GpPdvHj0oI4DScCqu?= =?us-ascii?Q?LJLbQTgJBtIk0ctReU/IEfhXanxapCMtUijph6FL44WzPCvkFDY82WdROHTW?= =?us-ascii?Q?KqrOgbIaOVLL/H5RRmcysBhyu+QXdfvWFZS6LxdQn2MeJVO5tJSsmHkdPm32?= =?us-ascii?Q?u5xOyCHFcDNW5v9Yo0Ff9+zGLkocyvB9nMAyYXmbfYntPQFa7aJ5BX/J67KK?= =?us-ascii?Q?IV6v1eqXfzzqf36Jsix6sJJMHwaUTG2wvElNw/x9P7j9ZIGXk9PWPzYdyXuZ?= =?us-ascii?Q?m5yYCCvDk9Ggfd1oYf5YfWhA15kCREEgRJBya2eGk04trXNp6vWfie7EJOLa?= =?us-ascii?Q?1gpLR7G2fEGPDwndNbAX2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=?us-ascii?Q?Hj0Q3WqfN3J8i+cvStsqCgmrIAMXmPZweDyUwldz09K90U3b+SkuEx75+CPR?= =?us-ascii?Q?zghPfrS6t9NokDzqMyvWIdONYwnXilTymmbQLLJR9Khjv/NKuVQ6xV1EfowT?= =?us-ascii?Q?uJuTEtIlLKi0NaD2b4lvNECr4/T7uIJ2jr6YvPeOgBxPpD34TFBgAuIykE+I?= =?us-ascii?Q?Z/mWNnYaQmT9uekjE6A1xVtBkEtB50ERQeba/5TabOLsmHzDEq1ngmx2gb8F?= =?us-ascii?Q?fDYPjt7OuiXe+cpVFxDvHUfEQjy6wORA2WAdfdKLQ1dqtMpM7uhyIxWXgNJD?= =?us-ascii?Q?oplJ3OiPENk08dLIVF9nJug/ww8GJWpnf0efkb67sLPO8FH43FgHAMHdtN+3?= =?us-ascii?Q?Cy+ht/bso/Q0YBTi2s6sYz0X0p8IwQA=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7d0dd33b-a723-49e6-e5cf-08da2db3b122 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 May 2022 09:51:42.3096 (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: 6WJkMvYfKsjVE0A6NK33tjl2swVfU/otVIO3Aq7LVeIlVsik+ywg3ZnZeixKGixwPq1vRv5yT5/Qx0H4+K5sqQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1001MB2316 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-05-04_03:2022-05-02,2022-05-04 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 bulkscore=0 spamscore=0 malwarescore=0 adultscore=0 suspectscore=0 mlxscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205040065 X-Proofpoint-ORIG-GUID: oU1O_JjiY0QM6WmtqVp-N2STes3Ke0E6 X-Proofpoint-GUID: oU1O_JjiY0QM6WmtqVp-N2STes3Ke0E6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" After removal of kernfs_open_node->refcnt in the previous patch, kernfs_open_node_lock can be removed as well by making ->attr.open RCU protected. kernfs_put_open_node can delegate freeing to ->attr.open to RCU and other readers of ->attr.open can do so under rcu_read_(un)lock. Suggested by: Al Viro Signed-off-by: Imran Khan --- fs/kernfs/file.c | 120 +++++++++++++++++++++++++---------------- include/linux/kernfs.h | 2 +- 2 files changed, 75 insertions(+), 47 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index e3abfa843879..36bff71ab263 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -23,16 +23,16 @@ * for each kernfs_node with one or more open files. * * kernfs_node->attr.open points to kernfs_open_node. attr.open is - * protected by kernfs_open_node_lock. + * RCU protected. * * filp->private_data points to seq_file whose ->private points to * kernfs_open_file. kernfs_open_files are chained at * kernfs_open_node->files, which is protected by kernfs_open_file_mutex. */ -static DEFINE_SPINLOCK(kernfs_open_node_lock); static DEFINE_MUTEX(kernfs_open_file_mutex); =20 struct kernfs_open_node { + struct rcu_head rcu_head; atomic_t event; wait_queue_head_t poll; struct list_head files; /* goes through kernfs_open_file.list */ @@ -51,6 +51,38 @@ struct kernfs_open_node { static DEFINE_SPINLOCK(kernfs_notify_lock); static struct kernfs_node *kernfs_notify_list =3D KERNFS_NOTIFY_EOL; =20 +/* + * Raw deref RCU protected kn->attr.open. + * The caller guarantees that @on will not vanish in the middle of this + * function and hence we can deref ->attr.open raw. + */ +static struct kernfs_open_node *kernfs_deref_on_raw(struct kernfs_node *kn) +{ + return rcu_dereference_raw(kn->attr.open); +} + +/* + * Deref ->attr.open corresponding to @kn while holding kernfs_open_file_m= utex. + * ->attr.open is modified under kernfs_open_file_mutex. So it can be safe= ly + * accessed outside RCU read-side critical section, while holding the mute= x. + */ +static struct kernfs_open_node *kernfs_deref_on_protected(struct kernfs_no= de *kn) +{ + return rcu_dereference_protected(kn->attr.open, + lockdep_is_held(&kernfs_open_file_mutex)); +} + +/* + * Check ->attr.open corresponding to @kn while holding kernfs_open_file_m= utex. + * ->attr.open is modified under kernfs_open_file_mutex. So it can be safe= ly + * accessed outside RCU read-side critical section, while holding the mute= x. + */ +static struct kernfs_open_node *kernfs_check_on_protected(struct kernfs_no= de *kn) +{ + return rcu_dereference_check(kn->attr.open, + lockdep_is_held(&kernfs_open_file_mutex)); +} + static struct kernfs_open_file *kernfs_of(struct file *file) { return ((struct seq_file *)file->private_data)->private; @@ -156,8 +188,9 @@ static void kernfs_seq_stop(struct seq_file *sf, void *= v) static int kernfs_seq_show(struct seq_file *sf, void *v) { struct kernfs_open_file *of =3D sf->private; + struct kernfs_open_node *on =3D kernfs_deref_on_raw(of->kn); =20 - of->event =3D atomic_read(&of->kn->attr.open->event); + of->event =3D atomic_read(&unrcu_pointer(on)->event); =20 return of->kn->attr.ops->seq_show(sf, v); } @@ -180,6 +213,7 @@ static ssize_t kernfs_file_read_iter(struct kiocb *iocb= , struct iov_iter *iter) struct kernfs_open_file *of =3D kernfs_of(iocb->ki_filp); ssize_t len =3D min_t(size_t, iov_iter_count(iter), PAGE_SIZE); const struct kernfs_ops *ops; + struct kernfs_open_node *on; char *buf; =20 buf =3D of->prealloc_buf; @@ -201,7 +235,8 @@ static ssize_t kernfs_file_read_iter(struct kiocb *iocb= , struct iov_iter *iter) goto out_free; } =20 - of->event =3D atomic_read(&of->kn->attr.open->event); + on =3D kernfs_deref_on_raw(of->kn); + of->event =3D atomic_read(&unrcu_pointer(on)->event); ops =3D kernfs_ops(of->kn); if (ops->read) len =3D ops->read(of, buf, len, iocb->ki_pos); @@ -519,36 +554,29 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, { struct kernfs_open_node *on, *new_on =3D NULL; =20 - retry: mutex_lock(&kernfs_open_file_mutex); - spin_lock_irq(&kernfs_open_node_lock); - - if (!kn->attr.open && new_on) { - kn->attr.open =3D new_on; - new_on =3D NULL; - } - - on =3D kn->attr.open; - if (on) - list_add_tail(&of->list, &on->files); - - spin_unlock_irq(&kernfs_open_node_lock); - mutex_unlock(&kernfs_open_file_mutex); + on =3D kernfs_deref_on_protected(kn); =20 if (on) { - kfree(new_on); + list_add_tail(&of->list, &on->files); + mutex_unlock(&kernfs_open_file_mutex); return 0; + } else { + /* not there, initialize a new one */ + new_on =3D kmalloc(sizeof(*new_on), GFP_KERNEL); + if (!new_on) { + mutex_unlock(&kernfs_open_file_mutex); + return -ENOMEM; + } + atomic_set(&new_on->event, 1); + init_waitqueue_head(&new_on->poll); + INIT_LIST_HEAD(&new_on->files); + list_add_tail(&of->list, &new_on->files); + rcu_assign_pointer(kn->attr.open, new_on); } + mutex_unlock(&kernfs_open_file_mutex); =20 - /* not there, initialize a new one and retry */ - new_on =3D kmalloc(sizeof(*new_on), GFP_KERNEL); - if (!new_on) - return -ENOMEM; - - atomic_set(&new_on->event, 1); - init_waitqueue_head(&new_on->poll); - INIT_LIST_HEAD(&new_on->files); - goto retry; + return 0; } =20 /** @@ -567,24 +595,25 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, static void kernfs_unlink_open_file(struct kernfs_node *kn, struct kernfs_open_file *of) { - struct kernfs_open_node *on =3D kn->attr.open; - unsigned long flags; + struct kernfs_open_node *on; =20 mutex_lock(&kernfs_open_file_mutex); - spin_lock_irqsave(&kernfs_open_node_lock, flags); + + on =3D kernfs_deref_on_protected(kn); + if (!on) { + mutex_unlock(&kernfs_open_file_mutex); + return; + } =20 if (of) list_del(&of->list); =20 - if (list_empty(&on->files)) - kn->attr.open =3D NULL; - else - on =3D NULL; + if (list_empty(&on->files)) { + rcu_assign_pointer(kn->attr.open, NULL); + kfree_rcu(on, rcu_head); + } =20 - spin_unlock_irqrestore(&kernfs_open_node_lock, flags); mutex_unlock(&kernfs_open_file_mutex); - - kfree(on); } =20 static int kernfs_fop_open(struct inode *inode, struct file *file) @@ -774,17 +803,16 @@ void kernfs_drain_open_files(struct kernfs_node *kn) * check under kernfs_open_file_mutex will ensure bailing out if * ->attr.open became NULL while waiting for the mutex. */ - if (!kn->attr.open) + if (!rcu_access_pointer(kn->attr.open)) return; =20 mutex_lock(&kernfs_open_file_mutex); - if (!kn->attr.open) { + on =3D kernfs_check_on_protected(kn); + if (!on) { mutex_unlock(&kernfs_open_file_mutex); return; } =20 - on =3D kn->attr.open; - list_for_each_entry(of, &on->files, list) { struct inode *inode =3D file_inode(of->file); =20 @@ -815,7 +843,7 @@ void kernfs_drain_open_files(struct kernfs_node *kn) __poll_t kernfs_generic_poll(struct kernfs_open_file *of, poll_table *wait) { struct kernfs_node *kn =3D kernfs_dentry_node(of->file->f_path.dentry); - struct kernfs_open_node *on =3D kn->attr.open; + struct kernfs_open_node *on =3D kernfs_deref_on_raw(kn); =20 poll_wait(of->file, &on->poll, wait); =20 @@ -922,13 +950,13 @@ void kernfs_notify(struct kernfs_node *kn) return; =20 /* kick poll immediately */ - spin_lock_irqsave(&kernfs_open_node_lock, flags); - on =3D kn->attr.open; + rcu_read_lock(); + on =3D rcu_dereference(kn->attr.open); if (on) { atomic_inc(&on->event); wake_up_interruptible(&on->poll); } - spin_unlock_irqrestore(&kernfs_open_node_lock, flags); + rcu_read_unlock(); =20 /* schedule work to kick fsnotify */ spin_lock_irqsave(&kernfs_notify_lock, flags); diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index e2ae15a6225e..13f54f078a52 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -114,7 +114,7 @@ struct kernfs_elem_symlink { =20 struct kernfs_elem_attr { const struct kernfs_ops *ops; - struct kernfs_open_node *open; + struct kernfs_open_node __rcu *open; loff_t size; struct kernfs_node *notify_next; /* for kernfs_notify() */ }; --=20 2.30.2 From nobody Sun May 10 12:50:05 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 C5620C433EF for ; Wed, 4 May 2022 09:52:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347831AbiEDJzm (ORCPT ); Wed, 4 May 2022 05:55:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347777AbiEDJz1 (ORCPT ); Wed, 4 May 2022 05:55:27 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D0FD27B2F for ; Wed, 4 May 2022 02:51:51 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2448qP0C026110; Wed, 4 May 2022 09:51:48 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=9OkcfwQVEoQcLOHcE1BNDaBawN3QOSQ7/Fx8akMzMYA=; b=F/S43S1G2f1y/B+jIrVqbDcNA1FpthwZZEurJ0fk4SNM2DVbhcL1pJQKZ3uRfRIKKT6N olU8kdYa6xoVO8YVO/1oOlaGheFzSb3DrceXTOFvQCQc1VtXV74JQ3OxtRCC4ioxKB+i 2pPOho7D489Zz77WH68SLWYPOojG6H0SvXlMzfoCxJdW1mPwJK/1UEoWgMNxwKywoUbH sq3PjYjfzykKrGzmgi09iRNQ6tpkeX20bd2oMfSN24V3/TJWn8tnZO4nALg0osd/FPj+ tur8tjH1Z/sSs8+BlsgJqnD6AlkNm9aYTRqQOzNRQxybZybqS7AgNzvGhNwW1pHOB81K AA== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fruhc7uyk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 May 2022 09:51:47 +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 2449iolm015704; Wed, 4 May 2022 09:51:46 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2172.outbound.protection.outlook.com [104.47.57.172]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3fruj37taw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 May 2022 09:51:46 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H+SG5uM4OqmrhAcPGt8oYCR0VPUCaOB5YimlBVo5FAX4b9uhsXcTX3cArgmPLHJLW4Mk2oFuFA/U/EKxnHbGi4JPbkEN606FJYo7jvf3TN148gVFaysrDlDexePIlGAWtswTrvJCKp90D2/epIMGP4tJcwpVMYwRTYYEMFhK35K9tmEGrOS4+A5TSvztIQBrjBoZW/x72Nw4yRFNjI/T7TlAxuzep0By6wEYto7wZqHYPc1Ak28lePVIFdPqmzxk2Dpnv3URRxnR1YcWW98WuePDeG1bCjCakElsD4B9KtmIFWfzCrR0yOdjJyENhy+VeO9uGt+Hg5E41h+ORpLL8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=9OkcfwQVEoQcLOHcE1BNDaBawN3QOSQ7/Fx8akMzMYA=; b=OzqOFx4Mx9DZWH4ZqXiRW/SnUIxX+1OvjVFFqrY3DqKRS0bmVYDC7fyhXZKIWTLCqZTPRpwvqoqGL3rCptp5tCnDN8TAj8Y7D5Ksn9dzArYcVugJIfUMVncllW/rxsyWPk3DbMvOWxGDvtluqD/inzet3r+VEEsirRPRvenqcDCgCKmcpLFWKsbSCcgwXl9AJ8Q1BuLfaZroMSPufi3z/0bRBWocp6pD75WcCJXrtii6kmOAFUmliVjGjoNjI8BjY444kjlY7/w501oWLcQZtlGpdG2mmdgHGj5MauidkVDG+GtrDqpkxHzVJm9rif6isGSD+JdCFIaQMLbxG9HKxA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9OkcfwQVEoQcLOHcE1BNDaBawN3QOSQ7/Fx8akMzMYA=; b=MOizWancv7+G3vAtT3PM02t2pLd3HOBWyMKqIt/tXVibgKV2/SefMNeD8VhW5sZrgLUjhUKJmji7GirCKctMTwaT0KqjsKC9sjSuQrdep9YdAaZS3856QMve7TAR1sM9z/KnC40GoOHXdUI+21gUg7N5UC0ijO4iguu4fVJOZIk= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by DM5PR1001MB2316.namprd10.prod.outlook.com (2603:10b6:4:2d::39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.28; Wed, 4 May 2022 09:51:45 +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.5206.025; Wed, 4 May 2022 09:51:45 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v2 3/5] kernfs: Change kernfs_notify_list to llist. Date: Wed, 4 May 2022 19:51:21 +1000 Message-Id: <20220504095123.295859-4-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220504095123.295859-1-imran.f.khan@oracle.com> References: <20220504095123.295859-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0203.ausprd01.prod.outlook.com (2603:10c6:10:16::23) To CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f910664f-7ae4-419c-5864-08da2db3b299 X-MS-TrafficTypeDiagnostic: DM5PR1001MB2316: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: GrcYIkJsHHF0S02LPuAM5WylHKZvPL8Z1X+JTAUaB2vx21rJL4yqHjtdQDDHBFMgStqQZ2s/6ajIV4od817WxiWnsvs8lCKm9rwWjTtLnMD5mnnlNnynq6cTn6r2Wq93V7p0YOvWjULBEkHehHLkIzE8LZjFXGsF4eV9ZgFoE26u1f0/M/FAG0bM0b9KGn/yORXLnhld+Af+FkjPBulJugGkp21hYutoc6jWyEwie2TSifhi0H/r84kcPbtFlACS9ip02ch9bcA0C2XcRL3nYV1l6dqtIAQE2HdyjSX7mAaBbpnE5CsqIQlCYIN9UwnsGkuhzWkQcP3RpDIIKoYoR3WodV+4wNygBnRTXdPn8aXCd1aqA7MnacgRC6vJVvh7CGG8G0Kb2IUHzuTU7FNQ8LZ3ebGMvNBcPVLeeiXfLc08a6e7zrQgqfh/7SvcN/SreG8QF6KD+M3A0OeUhJYj/CziMkTLzkA9H3qMsO4X3nax9Dy7mw3KWjBJMZ0O5qjsOcPBH2ggG9ppqmu0w5n6PPT0mUzuKXjGnIdpsBtyavJDQVLKBt1i0a/BxCjeQqFCQqVnuQUFbkV4qxDBJa/TvDlc5tARg8M6HU1OlEvt0ObQXW6AJ+IFDknpdjV3REdy61UX/B60dsqXCqPuwNolWC4mby9CQJTMRDu55Uc2FqG3WWqEvGOUnL0dRodSpoQBuoJt+NRj9oY58TwweIf4LQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO1PR10MB4468.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(316002)(6506007)(2906002)(38100700002)(38350700002)(8936002)(186003)(36756003)(83380400001)(8676002)(6666004)(66476007)(66556008)(5660300002)(26005)(66946007)(1076003)(6512007)(2616005)(508600001)(52116002)(86362001)(103116003)(4326008)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?4Da6H45pP5nE2pEh7pS1By4muGmPUyp7JGZSMvoV9f1tBUd7vBEn1StRn9CI?= =?us-ascii?Q?1LFERD1Lvd9No2ggV1P6szW28Stit0I2rwsDYqn8gwU1K7XY0joXtXU5Gb+l?= =?us-ascii?Q?uVQzV+0aKTp/q9CTfThm3e97DcfaMtEVfNiDXqYSiqKtGKXkXVbsjY9/vSQx?= =?us-ascii?Q?fncbk4aT9y9ESwOTcifGR1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hSdw?= =?us-ascii?Q?zgGTGa8EJSAWP0oiXOgpM3ZCR1UtwqxOadAcevt0eGSUCQx9Sh3s2eAwukFy?= =?us-ascii?Q?QUk8YtQV9YZO2SU4lMy+D0tYv3C8YDI3wD59VJLebZod7ntfFHkQJTH/Az/h?= =?us-ascii?Q?euiGQvvRP035XK9obI1sdFlV7Ik3EGAo7CRjnOo8lVLDQLeFpgC8fxJuq0UL?= =?us-ascii?Q?aDcF2sswwp180H/NbH6RK1fgmwPfio9bGnSIZji7922p6oSBBgteczgil86i?= =?us-ascii?Q?FNFDcESvT+N1WFCOqNeq4SYmCDyDfWrWDrknjCia23FQ1/UUGOTaoW7yrG4F?= =?us-ascii?Q?/77tdo/jybluNM0welrz/sNruejEA0r80NrF72OWWUV1qTyhxra1nMfyzqNd?= =?us-ascii?Q?LVZPVGdr5IVezbp5xw3ZpUBlZHgLxoIuDmWqlB3fTFAn/8F6fyGGjxl3hU8f?= =?us-ascii?Q?EiW6oj6h8fNildSlayHdl/3TuSoWcAfHbJrkK4EDotq5ucwJ43w85l9PyP9D?= =?us-ascii?Q?3d+DTB0H2GD8Oh2xOaVquO7D3DrT0tHD7V0cqp+TvUvkRm53uIH54mMQiVQl?= =?us-ascii?Q?0wuK2KFzrZrNVqPsoa82J//v2ZqjjmjB5KEVKVnP8iQGwv79PZtTonC0gQTL?= =?us-ascii?Q?qfTtDClnwDQpWPMtqhgWuPqjauFX6xlrVJX6bC2ESXl97cGmetK24uOd2eOk?= =?us-ascii?Q?/gckga/wUgnHojrFKLPHJPnTuNYv+5k=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f910664f-7ae4-419c-5864-08da2db3b299 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 May 2022 09:51:44.9510 (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: nL7ehQf1COOrTXze4bR4JksQwnGoEJzjZXKaUn4tWczHgS7J62BfFtB6NafMCu7jOVQ6Gc4olNLtK2NLrDGPEQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1001MB2316 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-05-04_03:2022-05-02,2022-05-04 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 mlxscore=0 mlxlogscore=999 adultscore=0 phishscore=0 suspectscore=0 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205040065 X-Proofpoint-GUID: EbI5eLTggQ0xkRjxhvaGBcTbh90qeo2U X-Proofpoint-ORIG-GUID: EbI5eLTggQ0xkRjxhvaGBcTbh90qeo2U 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 --- fs/kernfs/file.c | 47 ++++++++++++++++++------------------------ include/linux/kernfs.h | 2 +- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 36bff71ab263..96c8493003b6 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -38,18 +38,16 @@ struct kernfs_open_node { struct list_head files; /* goes through kernfs_open_file.list */ }; =20 -/* - * kernfs_notify() may be called from any context and bounces notifications - * through a work item. To minimize space overhead in kernfs_node, the - * pending queue is implemented as a singly linked list of kernfs_nodes. - * The list is terminated with the self pointer so that whether a - * kernfs_node is on the list or not can be determined by testing the next - * pointer for NULL. +/** + * attribute_to_node - get kernfs_node object corresponding to a kernfs at= tribute + * @ptr: &struct kernfs_elem_attr + * @type: struct kernfs_node + * @member: name of member (i.e attr) */ -#define KERNFS_NOTIFY_EOL ((void *)&kernfs_notify_list) +#define attribute_to_node(ptr, type, member) \ + container_of(ptr, type, member) =20 -static DEFINE_SPINLOCK(kernfs_notify_lock); -static struct kernfs_node *kernfs_notify_list =3D KERNFS_NOTIFY_EOL; +static LLIST_HEAD(kernfs_notify_list); =20 /* * Raw deref RCU protected kn->attr.open. @@ -876,18 +874,16 @@ static void kernfs_notify_workfn(struct work_struct *= work) struct kernfs_node *kn; struct kernfs_super_info *info; struct kernfs_root *root; + struct llist_node *free; + struct kernfs_elem_attr *attr; repeat: /* pop one off the notify_list */ - spin_lock_irq(&kernfs_notify_lock); - kn =3D kernfs_notify_list; - if (kn =3D=3D KERNFS_NOTIFY_EOL) { - spin_unlock_irq(&kernfs_notify_lock); + free =3D llist_del_first(&kernfs_notify_list); + if (free =3D=3D NULL) return; - } - kernfs_notify_list =3D kn->attr.notify_next; - kn->attr.notify_next =3D NULL; - spin_unlock_irq(&kernfs_notify_lock); =20 + attr =3D llist_entry(free, struct kernfs_elem_attr, notify_next); + kn =3D attribute_to_node(attr, struct kernfs_node, attr); root =3D kernfs_root(kn); /* kick fsnotify */ down_write(&root->kernfs_rwsem); @@ -943,12 +939,14 @@ static void kernfs_notify_workfn(struct work_struct *= work) void kernfs_notify(struct kernfs_node *kn) { static DECLARE_WORK(kernfs_notify_work, kernfs_notify_workfn); - unsigned long flags; struct kernfs_open_node *on; =20 if (WARN_ON(kernfs_type(kn) !=3D KERNFS_FILE)) return; =20 + /* Because we are using llist for kernfs_notify_list */ + WARN_ON_ONCE(in_nmi()); + /* kick poll immediately */ rcu_read_lock(); on =3D rcu_dereference(kn->attr.open); @@ -959,14 +957,9 @@ void kernfs_notify(struct kernfs_node *kn) rcu_read_unlock(); =20 /* schedule work to kick fsnotify */ - spin_lock_irqsave(&kernfs_notify_lock, flags); - if (!kn->attr.notify_next) { - kernfs_get(kn); - kn->attr.notify_next =3D kernfs_notify_list; - kernfs_notify_list =3D kn; - schedule_work(&kernfs_notify_work); - } - spin_unlock_irqrestore(&kernfs_notify_lock, flags); + kernfs_get(kn); + llist_add(&kn->attr.notify_next, &kernfs_notify_list); + schedule_work(&kernfs_notify_work); } EXPORT_SYMBOL_GPL(kernfs_notify); =20 diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 13f54f078a52..2dd9c8df0f4f 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -116,7 +116,7 @@ struct kernfs_elem_attr { const struct kernfs_ops *ops; struct kernfs_open_node __rcu *open; loff_t size; - struct kernfs_node *notify_next; /* for kernfs_notify() */ + struct llist_node notify_next; /* for kernfs_notify() */ }; =20 /* --=20 2.30.2 From nobody Sun May 10 12:50:05 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 60EEBC433EF for ; Wed, 4 May 2022 09:52:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347846AbiEDJzp (ORCPT ); Wed, 4 May 2022 05:55:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347791AbiEDJzb (ORCPT ); Wed, 4 May 2022 05:55:31 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93F8E27CC6 for ; Wed, 4 May 2022 02:51:55 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2448dCZ1026258; Wed, 4 May 2022 09:51:52 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=u8abtKVpp3wetyUrvExCT0QB/Yr4E5Fq3bnErhIbpIs=; b=gU6wVOYeq0L0zGpJVKU9iFYpmtKC2utA9UkY3KV16+0MgE32GZAk2xuRMhoH+Ie4Ukij exB4vTB7TRxJl6vvaJOmKIfy0pIrWVarvKvrgjpJl7y+iQmLV89DEL9ZsVcQCQUvUP2s JZO5XqVIsLGqsEEcY1BazHJwHipl0PgI+KOOWm139JHKTumEHo0iu5rVXhh1UQ8HUWnC GtXB8+nELGeCqrJ96KHXuNJUXZDu/y776XhyKllenfqPmPPxmcGKAs61Bk5AKkUZhLDu 6MGNcMvTGXY62as7P7nYJoRMGIIMbfJ7AHeYAJbWLOVSqPzt/ov5InDpy6oDrzECvawI +A== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fruhc7uyp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 May 2022 09:51:51 +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 2449iodH015629; Wed, 4 May 2022 09:51:50 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2107.outbound.protection.outlook.com [104.47.58.107]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3fruj37tbx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 May 2022 09:51:50 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C2HXyKzNf+POlFosJMsvgdyOo1v1L3lnqLUPKPmVYU0oROmL18g0HRx3pMBkvr8XSAFXYNsi9ViehDXs7+el4YO5/x212X+u/sbVhPmTBgNn5cH3jA2P2QzRfqV269C6biJXOwDKMNSlnDKFnkHmoBbLnxETRWD8C/W/jXiXh+Rey/OkVVAsIBpNdPSZkr1C0pCSDKv3XLD8ZCKyt55n/qoD6+ylOjn80EKSHQASlYSBMmOXZ0K5oVfLkUrr3WIvWGjQrajUNXj2r/nuL2dCVQdNo2Z/gOh73dT9Ffu2KOxmfTYQPsUW7Onq6c9oohZhamC3GUY6jEQbgkCJfnHZdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=u8abtKVpp3wetyUrvExCT0QB/Yr4E5Fq3bnErhIbpIs=; b=QOJWXv1NItbeMawGDJCf/INPvDUjdkEgwtTiJ/X0d2HwnwAo9YsBpTBW7frEO1qbBwhKPGw9eshfB/jvILLXLUHIv6/KSrl9WL0iqOpCHl0bZClm+GuDEiiSdJ4mEFuy30voBbPju6OzkaBUysOWofO7GGisBvIHoxpFOWhoHxf4FzCZnmZzIGJH6ZNhoum/65dhr7r/ntzXKxrFAFwDvuRcajCgcC0L4q8tywGyFl8RW0EUY/R4J41Z8xFzsICikPUuiOE+KTIXsrTy5AIB/tv5+ZzzEqGU0M6Fxcpm2zvKnZOLO9ZQ2f4MfBw1SNUuACaA12HrNBLpJ4rcUPtR3w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=u8abtKVpp3wetyUrvExCT0QB/Yr4E5Fq3bnErhIbpIs=; b=M8nLTRWmcLlbo6A0fF8Ve/rsDTI1EB5syXcGYU4nHbSB6BIFJXOVCGlOUuLrl4VJaXoVy6d/AUulfazomX0zhfLMWLJwD5MxpTt3l0hH2HyjDW3i/9ARlg9tPTCws5nv7qdWPVn5y+P6FZND8g4tvghx/tuNwAnCvDnUYSVW9hI= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by BLAPR10MB4899.namprd10.prod.outlook.com (2603:10b6:208:323::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5206.13; Wed, 4 May 2022 09:51:48 +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.5206.025; Wed, 4 May 2022 09:51:47 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v2 4/5] kernfs: Introduce interface to access global kernfs_open_file_mutex. Date: Wed, 4 May 2022 19:51:22 +1000 Message-Id: <20220504095123.295859-5-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220504095123.295859-1-imran.f.khan@oracle.com> References: <20220504095123.295859-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0203.ausprd01.prod.outlook.com (2603:10c6:10:16::23) To CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ee53f7d7-bd85-41f7-adfe-08da2db3b44d X-MS-TrafficTypeDiagnostic: BLAPR10MB4899: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: 9hjSBeR5nFGIyYTctKJpNgIX/Lg9YU+n7spX9quexnAqOTYMbtnC/PrdIU7R2UUkAydprkrY+0Zm7t3ILZIUpBX8tg7pH2UfxWoAA3kLbvpTISV03DjCxloP52A1FmxZyTa4jGTFQhbgqd3X/w+1EFYSJCYbNgUPQyuofp+zFB1KZwT+BdKq9P7R6bESzcrusypziCzUNLNGwaEeoYEAC5S/ClQ4pIQ2RifHqSdkFyvziCrdwnCzzpCBPJT4hTRUCuGOdjrOo9mG56wSFAN2sroEj3xp5WIKqsl4iZStvyVlQO3LIYKWloZpcNNyp4mSjWCipKkGRPGXstMZ3MMgTLnzMcpB6qRAprHQ6AO4NLgV55pQwGtR0a9RvkzSyTH+l+isrdnWb0Ete3bHjf4OORuLpAtOYZpXa8HIJ7KzKj5I7ZSWRY1ODa43hGNLo82k71O3kYdDBy7mNXPGIurwYBgqDEdv0IhliGicMcIMWzfEiqiwFMZty6f64+zuIBd5brGI31mNc+OEOqWiTI4ZVeEkUu3s1H6mpm/wIKqM8QP4S9Vk8TsOao0MS13mMmrSkGjUMFPKyEpZJCnIn6J/KAGFXcoq7FdrMWuLF0KrIoNzJrblgKJ47/hPbz8rjaagjgnHr5r+mZ01yS45Upbct/jPKm4TqiUC4m8FMBNjtUD9itYsaWBN0ZEr0JoIViKkk6T8RIPlrSqNlG7bmgO0OA== 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)(66946007)(26005)(316002)(66476007)(6512007)(52116002)(86362001)(66556008)(8676002)(4326008)(186003)(1076003)(2616005)(6666004)(36756003)(6486002)(38350700002)(508600001)(38100700002)(6506007)(2906002)(103116003)(83380400001)(5660300002)(8936002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?Cbr7Bc5zNhZW2rhTIY/VMv4QArgfjQY4oTTS8CcAl43dPHD5/eqmR7H69p8t?= =?us-ascii?Q?cHnxgQU+DpEM5dzEKoHDjK5dxnfo4TgQ/Yknzws3l5JAwGC1p7ww38q/4usd?= =?us-ascii?Q?KSx9R/GBjz4R81+w0js1fUn+DqvuysGITQ1v1YF16WG0Fi6MLjFxLfCfx14K?= =?us-ascii?Q?jVLA9NHSXPkSGU7wGPxX/vQ8zKPfXhj6hs4Q6zznyvCPzFj/0v7WJqJ8gIph?= =?us-ascii?Q?Jq5JGcMY26UV1/DfphCSNYpaj3P1ihU03YBKG8pK5OjeHbt8m8EkweLcrUjU?= =?us-ascii?Q?K5WjVsUHOhrvhKPDevu4PwRjqGF6uthd+en8i2aodz7bxoH7JnjERc8+1mdi?= =?us-ascii?Q?8ZpVyT0hWcJLLuG1ocbC3QyDF4j+Z2bZP8TXsoNc84jeobG8MQDjvf6UxoPv?= =?us-ascii?Q?5XHp33efeLC6mALTWiS6LpzhWnuzerv5IbzV8jwW9/kUXSms25jjUIsVrc+F?= =?us-ascii?Q?n/M8AAirCbjShUyNXzJWnoGPEApq3LnNnjsRnXhBQHhJKTvHXCKGMkaeb0iE?= =?us-ascii?Q?jFlRFP9blcFCSDXggb6T8djwiJ/PU1PL7ozBn2PThMe7Cuqtu5d1qouZC8kM?= =?us-ascii?Q?lD2TU9k2ZspJTB4x71gXKxzs4s5wSAdnRGPWUDIjYgKzmT4ZRyKx3ynbuJkS?= =?us-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gWNSaUUXu6XRqw7V3cOkTs1L2R/oexh2mW9MvYyXFntDa?= =?us-ascii?Q?80HN+XBK60yL/KRs7Os6RBTCYjpu33IBPAetJmCwY/Ix0R1K/wQnH5eRaViY?= =?us-ascii?Q?fSbP1vKlcjmR5HgW21Z9rzZZrRAEEk5AlfKGbQtI6wQy7VDNU3QM1nBLMutK?= =?us-ascii?Q?GttuGNyCpXAkBzgk6O7LpEb7Z830UV4MVs0fxgLt1H9KUGdF6B+D0RAkXajP?= =?us-ascii?Q?Bo+Ka+NwxTepm6t6jMYiRn5S7YBeykc=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: ee53f7d7-bd85-41f7-adfe-08da2db3b44d X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 May 2022 09:51:47.7651 (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: e05CNKefdqLrTBbtQK1qoSHuOc+4pQ8eNiVDByqPnLBeRZb4e9PHrFXijrc01AeddwlCAgljTx070mkZmrT9ng== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLAPR10MB4899 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-05-04_03:2022-05-02,2022-05-04 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 mlxscore=0 mlxlogscore=998 adultscore=0 phishscore=0 suspectscore=0 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205040065 X-Proofpoint-GUID: Wbg31iDQgwKpsMCWszaMTtYTIdbgVyjX X-Proofpoint-ORIG-GUID: Wbg31iDQgwKpsMCWszaMTtYTIdbgVyjX Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This allows to change underlying mutex locking, without needing to change the users of the lock. For example next patch modifies this interface to use hashed mutexes in place of a single global kernfs_open_file_mutex. Signed-off-by: Imran Khan --- fs/kernfs/file.c | 50 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 96c8493003b6..7a60074ec0a0 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -49,6 +49,22 @@ struct kernfs_open_node { =20 static LLIST_HEAD(kernfs_notify_list); =20 +static inline struct mutex *kernfs_open_file_mutex_ptr(struct kernfs_node = *kn) +{ + return &kernfs_open_file_mutex; +} + +static inline struct mutex *kernfs_open_file_mutex_lock(struct kernfs_node= *kn) +{ + struct mutex *lock; + + lock =3D kernfs_open_file_mutex_ptr(kn); + + mutex_lock(lock); + + return lock; +} + /* * Raw deref RCU protected kn->attr.open. * The caller guarantees that @on will not vanish in the middle of this @@ -67,7 +83,7 @@ static struct kernfs_open_node *kernfs_deref_on_raw(struc= t kernfs_node *kn) static struct kernfs_open_node *kernfs_deref_on_protected(struct kernfs_no= de *kn) { return rcu_dereference_protected(kn->attr.open, - lockdep_is_held(&kernfs_open_file_mutex)); + lockdep_is_held(kernfs_open_file_mutex_ptr(kn))); } =20 /* @@ -78,7 +94,7 @@ static struct kernfs_open_node *kernfs_deref_on_protected= (struct kernfs_node *kn static struct kernfs_open_node *kernfs_check_on_protected(struct kernfs_no= de *kn) { return rcu_dereference_check(kn->attr.open, - lockdep_is_held(&kernfs_open_file_mutex)); + lockdep_is_held(kernfs_open_file_mutex_ptr(kn))); } =20 static struct kernfs_open_file *kernfs_of(struct file *file) @@ -551,19 +567,20 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, struct kernfs_open_file *of) { struct kernfs_open_node *on, *new_on =3D NULL; + struct mutex *mutex =3D NULL; =20 - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); on =3D kernfs_deref_on_protected(kn); =20 if (on) { list_add_tail(&of->list, &on->files); - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return 0; } else { /* not there, initialize a new one */ new_on =3D kmalloc(sizeof(*new_on), GFP_KERNEL); if (!new_on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return -ENOMEM; } atomic_set(&new_on->event, 1); @@ -572,7 +589,7 @@ static int kernfs_get_open_node(struct kernfs_node *kn, list_add_tail(&of->list, &new_on->files); rcu_assign_pointer(kn->attr.open, new_on); } - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); =20 return 0; } @@ -594,12 +611,13 @@ static void kernfs_unlink_open_file(struct kernfs_nod= e *kn, struct kernfs_open_file *of) { struct kernfs_open_node *on; + struct mutex *mutex =3D NULL; =20 - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); =20 on =3D kernfs_deref_on_protected(kn); if (!on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return; } =20 @@ -611,7 +629,7 @@ static void kernfs_unlink_open_file(struct kernfs_node = *kn, kfree_rcu(on, rcu_head); } =20 - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); } =20 static int kernfs_fop_open(struct inode *inode, struct file *file) @@ -753,7 +771,7 @@ static void kernfs_release_file(struct kernfs_node *kn, * here because drain path may be called from places which can * cause circular dependency. */ - lockdep_assert_held(&kernfs_open_file_mutex); + lockdep_assert_held(kernfs_open_file_mutex_ptr(kn)); =20 if (!of->released) { /* @@ -770,11 +788,12 @@ static int kernfs_fop_release(struct inode *inode, st= ruct file *filp) { struct kernfs_node *kn =3D inode->i_private; struct kernfs_open_file *of =3D kernfs_of(filp); + struct mutex *mutex =3D NULL; =20 if (kn->flags & KERNFS_HAS_RELEASE) { - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); kernfs_release_file(kn, of); - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); } =20 kernfs_unlink_open_file(kn, of); @@ -789,6 +808,7 @@ void kernfs_drain_open_files(struct kernfs_node *kn) { struct kernfs_open_node *on; struct kernfs_open_file *of; + struct mutex *mutex =3D NULL; =20 if (!(kn->flags & (KERNFS_HAS_MMAP | KERNFS_HAS_RELEASE))) return; @@ -804,10 +824,10 @@ void kernfs_drain_open_files(struct kernfs_node *kn) if (!rcu_access_pointer(kn->attr.open)) return; =20 - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); on =3D kernfs_check_on_protected(kn); if (!on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return; } =20 @@ -821,7 +841,7 @@ void kernfs_drain_open_files(struct kernfs_node *kn) kernfs_release_file(kn, of); } =20 - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); } =20 /* --=20 2.30.2 From nobody Sun May 10 12:50:05 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 BD214C433F5 for ; Wed, 4 May 2022 09:52:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347849AbiEDJ4D (ORCPT ); Wed, 4 May 2022 05:56:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347338AbiEDJz5 (ORCPT ); Wed, 4 May 2022 05:55:57 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 608FD27FF4 for ; Wed, 4 May 2022 02:51:59 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2449a9Eq026114; Wed, 4 May 2022 09:51:53 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=aapRvOZQ687zTXBnoT4JyRmdwm8vA+JE1KLtnpL3jZ8=; b=gHrEYQp7oT8c3Np1gwixkAJ3bcrIo7+nRe3/vlnp95AyHxDrV/ECq5HYE8kVFCAf9tqh 2cpjLCgmRjWKwJJ9xq/78UOIKe/89E9vAjMu1+oSHbUO1A03MYoYtnyucFFZYqx+6auJ bgPl6d4ZsqxJ1fFMdWOZPdWxBRJj2+NFG7UFtJPv2CBWP3or3mVDeLE3IV0HFOpxp0b1 9Hilo/YzU+/5R0gn3uzMCOQsOea2kweMM85vRJJWxaX49hFopa8GhI4kFk652JAcZM5V Qwljn2pOQaLqAYJShDUPIclu9r1Ev5P8MybvZOeXlx4i5K9bn3C14dby1quPDBb69Ijd tA== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fruhc7uyq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 May 2022 09:51:52 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 2449kVg6025215; Wed, 4 May 2022 09:51:52 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2108.outbound.protection.outlook.com [104.47.58.108]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fruj9t30d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 May 2022 09:51:52 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jrRgwkU53bO/B6hDX9NH/8ha91yO/LqZ7MvkaAGUYmKekL+go/9sruFZDRgebKaSKr1aOmwsYOJMpgtgSpHuUO/lYPSn19kEG7DYuCuJ6wuxOvtyGeFTn9cci9+mYTg8ZI4ajrxrmOBD4B1M0g04DNq2vf8thM89rvci1XoJdq0vO5uoTlceVpsISkF04F4pSqXdzkM17Q97d1IARqT9rMGeG8epztCWJHrhDtgehkrFxRyqiArWCTH/zcryfKO2v8vm/ulXL6A5J0dYBN3V4x/XGEeG2mnyBonsgkKu6b1/gi0JXW0leUhOJdcMwVaFz5MDneEkdf5bNDvsF2jEfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=aapRvOZQ687zTXBnoT4JyRmdwm8vA+JE1KLtnpL3jZ8=; b=W9AZaO3fNCAzhCCLa6PrhskZ6YleVQDT1HjYOITBBTgQ82wVdRLPYlYzM5jVAxAQd68h/Esg4jg3YBz62jBBuGsA+jF6iiLnOat3y7U30p8JKFGa5xBgMkFvAkgxYa476c04t8Bx1jBuRAq9MjtzfUpgyM9Tr+bh6OQSFjcun5QiCc14jhybO62pfZpXWQQuyMIUK+AHG/haqxTuh9Axf1Nmom2hvA9mEoQ8ZJRXD8XYK5JVcrvblyodqQa4Vv9BytNPMnKT3ys10tv5SQFfIIGIrPkf9aNnWiAh/4puF2vP9iKb+hISNuzWe/6lblCp8VzaB/KIaucsDnlR1ep9jA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aapRvOZQ687zTXBnoT4JyRmdwm8vA+JE1KLtnpL3jZ8=; b=vylleIDlm/Lr12hYUVAavsLI2FcaseSr2VzdO4SXOEg9NFiEbggszm8kYvdaKaJU6nOkIL5nNRYyPKhS6xWrdXPhaL7MsDIYlle5Ek9iBmvB9T9jGO4avEjqtE+T+e+muWBPCfNv99EW/bWnRFq2VnyCkNobU4DGnU422A6uVso= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by BLAPR10MB4899.namprd10.prod.outlook.com (2603:10b6:208:323::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5206.13; Wed, 4 May 2022 09:51:50 +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.5206.025; Wed, 4 May 2022 09:51:50 +0000 From: Imran Khan To: tj@kernel.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v2 5/5] kernfs: Replace global kernfs_open_file_mutex with hashed mutexes. Date: Wed, 4 May 2022 19:51:23 +1000 Message-Id: <20220504095123.295859-6-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220504095123.295859-1-imran.f.khan@oracle.com> References: <20220504095123.295859-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYBPR01CA0203.ausprd01.prod.outlook.com (2603:10c6:10:16::23) To CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 16505ffa-a799-431f-85b9-08da2db3b5e5 X-MS-TrafficTypeDiagnostic: BLAPR10MB4899: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: rUM1HcB36c3KdDcxLWo3265cvcQ+hjF9uhPKvm2t3JLTBgUUxBYg87JfqhTMbX+88wGvQOGz2/UQL28448Ov94d8jwoyjpWi5ehx1tlrVkW7F51Kr4MTziL5Jd8XLcZzPi7C13lDlZHIBVXki7o+HN7bnHsqWd4FJiotX+GzCHxvll69d/e49pZocnBV+xvzG5JAnLvUHgsFFo+9pf7K+vWJWhlD0xcQQTFKZeJyGrtamrmZ6BAKnyf9hU4IUmFEWD+AutAv/un4wygo+j701Z1ME9weEz5XYbVwWut4bVZW1cp41UBge6ZF7esw6lN594pr9o4hRvOjo2L56rRHoXnzxODcJsCtjGXioK/QcPSPUOhWGzzD9gxYAPbAJL3AZ+6ho6qounFH5EJsBwJ2wO8JijjDMZbMVVm1/6NqY/ZTlCaB5wr9ms5LiiKN8I/v5/EIthzaLOQ1k+fT0ykPtomz9dQwoYy2hz6RzKSBFV7KjarJX365qcnhWSJTZc+32TQihe2HkAwO6fGSdYkaViY9LZTUEcd2eCZfHO1yOux5JCzCQKx997bKKj2eHPa7PLKZwfKD+fh998vOjkd1mOJxpp29TKz2h57QbOak4w4kUbj18T2X+vtm0BazbqDGE2dhz24ycmXC7DcBpSlsBc4Quacj70IdSfcGrC9qEXRTt4pZh5L6SA+SRUH+sK2X5FaMSgvPbn3zrEJuUGi+HQ== 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)(66946007)(26005)(316002)(66476007)(6512007)(52116002)(86362001)(66556008)(8676002)(4326008)(186003)(1076003)(2616005)(6666004)(36756003)(6486002)(38350700002)(508600001)(38100700002)(6506007)(2906002)(103116003)(83380400001)(5660300002)(8936002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?NuCRMAcC330IPTGmg5XLEp1cq2Ezjk8g+LNz359gAcoyXJJC9V2+2RZfCCYX?= =?us-ascii?Q?82Odlmb2+ZtiwG/huOhJxRb8fIPCn7eqK24fDZn/TtWMNA7PqK9bfMqfFiHN?= =?us-ascii?Q?stz85/LFEre9jGbJTbW0Ppe8o4jPAG3AodJzyUmASSMxVESY37eh4+ct9Kuc?= =?us-ascii?Q?wclC6ubleNh/Wr8cGJz+jFN6yoRSYFE0rNDv84Hty9/HvvVEawKuGe8vzHby?= =?us-ascii?Q?buTY0U2Q6f0G40BXlQhy06loixIJ1VC8E+kZE3P6erTSbYbCHSXzI0eOzNgG?= =?us-ascii?Q?hjgk10NDJyPUGCaK2DHVXaBIIls8icaCSkuN87iSE8o2PH+e3m6x+EMB4//B?= =?us-ascii?Q?243HrzY59F9M6ACLd9mtoJHXyGWf0ATk+VzzBgzEjYOPNBwjvTPsJxxXNq2z?= =?us-ascii?Q?wKc171ibECLCBI5lHhAXZ9U4juWRZpGvq05OpqCH7UJWn7gUJ8bcrwCYUdDb?= =?us-ascii?Q?Uznk7ZBcrkL2rL50K2kylVKlbAIPTmwIdM/JRadeVZoIfn0elTbPQyuWkEEK?= =?us-ascii?Q?7GmGkSWsvvlIAW9sK3b2KPyaMPE7VqSZihSaJGLQ2OoG25qddRhXMIJQgx0X?= =?us-ascii?Q?1rPeEuMOdv+A0ba0+fPw8VUgdyURMgtImKNSHcFjMK6W484D1I2cN0m0Fe2R?= =?us-ascii?Q?eEGzmTKHdiKIcuf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ascii?Q?CprUPYU1ooekH/xzxhMcSLpN63xvSS8U971re14yT2lYipMgo9G0EUnRpu1Y?= =?us-ascii?Q?HCtTgoABWQOUqTZPcjDZ09Ti/ajyXojeiEbaISa2E+rwnzI2LtXkHLsRWuzX?= =?us-ascii?Q?Qyred7MGTwDKCUFDXnLnOCUWOKNuXslNbQUEfYVpGr2K8NDa3HIUaqtPXnIP?= =?us-ascii?Q?RhEhvjFyrDQjooyyLXbEKZjcmxTjAOQ=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 16505ffa-a799-431f-85b9-08da2db3b5e5 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 May 2022 09:51:50.4210 (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: ddRO4lY2rVuk8Dot9NA387LY8zykC+v0iAqjpXQ12PTgAocqmTOzJJeK0B4J4rdh2Koc66kby6qREHRXPSkuIQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLAPR10MB4899 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.858 definitions=2022-05-04_03:2022-05-02,2022-05-04 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 phishscore=0 mlxlogscore=999 mlxscore=0 spamscore=0 suspectscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205040065 X-Proofpoint-GUID: bmILUFcD8A7FTwpgaP9BhshWULU0_bAe X-Proofpoint-ORIG-GUID: bmILUFcD8A7FTwpgaP9BhshWULU0_bAe 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 7a60074ec0a0..946a4a8d7e32 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -18,19 +18,6 @@ =20 #include "kernfs-internal.h" =20 -/* - * There's one kernfs_open_file for each open file and one kernfs_open_node - * for each kernfs_node with one or more open files. - * - * kernfs_node->attr.open points to kernfs_open_node. attr.open is - * RCU protected. - * - * filp->private_data points to seq_file whose ->private points to - * kernfs_open_file. kernfs_open_files are chained at - * kernfs_open_node->files, which is protected by kernfs_open_file_mutex. - */ -static DEFINE_MUTEX(kernfs_open_file_mutex); - struct kernfs_open_node { struct rcu_head rcu_head; atomic_t event; @@ -51,7 +38,9 @@ static LLIST_HEAD(kernfs_notify_list); =20 static inline struct mutex *kernfs_open_file_mutex_ptr(struct kernfs_node = *kn) { - return &kernfs_open_file_mutex; + int idx =3D hash_ptr(kn, NR_KERNFS_LOCK_BITS); + + return &kernfs_locks->open_file_mutex[idx]; } =20 static inline struct mutex *kernfs_open_file_mutex_lock(struct kernfs_node= *kn) diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index eeaa779b929c..3ae214d02d44 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -164,4 +164,8 @@ void kernfs_drain_open_files(struct kernfs_node *kn); */ extern const struct inode_operations kernfs_symlink_iops; =20 +/* + * kernfs locks + */ +extern struct kernfs_global_locks *kernfs_locks; #endif /* __KERNFS_INTERNAL_H */ diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index cfa79715fc1a..d0859f72d2d6 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -20,6 +20,7 @@ #include "kernfs-internal.h" =20 struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache; +struct kernfs_global_locks *kernfs_locks; =20 static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *den= try) { @@ -387,6 +388,22 @@ void kernfs_kill_sb(struct super_block *sb) kfree(info); } =20 +static void __init kernfs_mutex_init(void) +{ + int count; + + for (count =3D 0; count < NR_KERNFS_LOCKS; count++) + mutex_init(&kernfs_locks->open_file_mutex[count]); +} + +static void __init kernfs_lock_init(void) +{ + kernfs_locks =3D kmalloc(sizeof(struct kernfs_global_locks), GFP_KERNEL); + WARN_ON(!kernfs_locks); + + kernfs_mutex_init(); +} + void __init kernfs_init(void) { kernfs_node_cache =3D kmem_cache_create("kernfs_node_cache", @@ -397,4 +414,6 @@ void __init kernfs_init(void) kernfs_iattrs_cache =3D kmem_cache_create("kernfs_iattrs_cache", sizeof(struct kernfs_iattrs), 0, SLAB_PANIC, NULL); + + kernfs_lock_init(); } diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 2dd9c8df0f4f..13e703f615f7 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -18,6 +18,7 @@ #include #include #include +#include =20 struct file; struct dentry; @@ -34,6 +35,62 @@ struct kernfs_fs_context; struct kernfs_open_node; struct kernfs_iattrs; =20 +/* + * NR_KERNFS_LOCK_BITS determines size (NR_KERNFS_LOCKS) of hash + * table of locks. + * Having a small hash table would impact scalability, since + * more and more kernfs_node objects will end up using same lock + * and having a very large hash table would waste memory. + * + * At the moment size of hash table of locks is being set based on + * the number of CPUs as follows: + * + * NR_CPU NR_KERNFS_LOCK_BITS NR_KERNFS_LOCKS + * 1 1 2 + * 2-3 2 4 + * 4-7 4 16 + * 8-15 6 64 + * 16-31 8 256 + * 32 and more 10 1024 + * + * The above relation between NR_CPU and number of locks is based + * on some internal experimentation which involved booting qemu + * with different values of smp, performing some sysfs operations + * on all CPUs and observing how increase in number of locks impacts + * completion time of these sysfs operations on each CPU. + */ +#ifdef CONFIG_SMP +#define NR_KERNFS_LOCK_BITS (2 * (ilog2(NR_CPUS < 32 ? NR_CPUS : 32))) +#else +#define NR_KERNFS_LOCK_BITS 1 +#endif + +#define NR_KERNFS_LOCKS (1 << NR_KERNFS_LOCK_BITS) + +/* + * There's one kernfs_open_file for each open file and one kernfs_open_node + * for each kernfs_node with one or more open files. + * + * filp->private_data points to seq_file whose ->private points to + * kernfs_open_file. + * + * kernfs_open_files are chained at kernfs_open_node->files, which is + * protected by kernfs_global_locks.open_file_mutex[i]. + * + * To reduce possible contention in sysfs access, arising due to single + * locks, use an array of locks (e.g. open_file_mutex) and use kernfs_node + * object address as hash keys to get the index of these locks. + * + * Hashed mutexes are safe to use here because operations using these don't + * rely on global exclusion. + * + * In future we intend to replace other global locks with hashed ones as w= ell. + * kernfs_global_locks acts as a holder for all such hash tables. + */ +struct kernfs_global_locks { + struct mutex open_file_mutex[NR_KERNFS_LOCKS]; +}; + enum kernfs_node_type { KERNFS_DIR =3D 0x0001, KERNFS_FILE =3D 0x0002, --=20 2.30.2