From nobody Thu May 7 20:26:41 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 97359C433FE for ; Thu, 19 May 2022 07:28:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232640AbiESH2a (ORCPT ); Thu, 19 May 2022 03:28:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235074AbiESH1b (ORCPT ); Thu, 19 May 2022 03:27:31 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63B6387238 for ; Thu, 19 May 2022 00:27:24 -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 24J5x4XM027473; Thu, 19 May 2022 07:27: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=JRIL6/CfQA8Kjswn0c+rGygeg7AQia10G1qLQf+r7zA=; b=g8ThFICwIwWzIolpCs5dFd5+YOgkBWBd5NFWUBRD5PtP63mEzHapxU19nd/wwRzrwt2N OEFsc8+/KM/pW23nBZTK8cEgDGiR+8NS4ZoGzDHlbaCpqPDgL0hzPB0tAhqJrk3F50/i vYx0LV8hC8Y2J7L4I1flCz21+kyn2RZ6J4X+7tmHIWVFuS/ohdT5h9VGn44g3XBnlwXk GXUVUxdD5a3OxXE/QUtJnXxwGtoBkxiB6nSFUT05KqPslkDYMaaQsiPCJPOXvJ0eJFmc ht4sV/nBWsFR68MAN/h6YWIuAS39jv/hFA6gEUN7H1Mh0r1jH1F55k9oGFdrY+JfVjgb vg== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3g241sbcry-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 May 2022 07:27:15 +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 24J7F7l5023368; Thu, 19 May 2022 07:27:14 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2168.outbound.protection.outlook.com [104.47.55.168]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3g22v50ur8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 May 2022 07:27:14 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=M/FlOPqB5KRHLE5Kxy0Y20POISnHABWN/N+nfKvdwfUtgekWMnfOcR3mGtOdsNOl7hOzF1ffI2FzxwFXT1MkxEd0iok5dm9WnDSVpr/yan4mdcDZ4gqD1/egx+29OBQwBpG3vRKR9jnxo51QX9mk2R3zNFIdMjBSHMjmlh5iKpfsz7G+fspGtyol9p57qZJuwMg4AP4PnBywp4bHpzFI0Ixc5ZErtkUkznEjJUi6+GZTB+buhfIbXAby/zAvhzuBByChatXREZvvjn6HQtH88FTifXNypLQUNCiKGyPZB/dZlw+JSO3k8KJt7BvUgBGwMiDLE2KgIyWv+Tk0HTx36w== 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=JRIL6/CfQA8Kjswn0c+rGygeg7AQia10G1qLQf+r7zA=; b=G4BZJThEhM4KHtLPWV1vV0nlGqP+/sGeaPqiieKeKzIjF3HZvTABbHOTGux9O4eU5G+IAsGyd8NXFmeN5lqR7IuF6SvGahTmX4axXW9SWlJ6hHJJ8WWuO0APZ31hN2apVS/25gle85Buzcp4j/ampR2wH7bvUUBfQH2TPO4aqt0nuMgVnfn4Q92TljwIbkFegKV34ddJs62lmNHh0h6zsRTQ9YSZsplJHubBHqGm6N1ZsoDsEtG9ADC7vKEz5zK16RZG+zQ5oBPWttYW0G6vhP1o/mOf3fZMl0lJoWzj9jGbzn2S2jt79gGLFQnwLjF6+K4xOnQ7poVlvLFhp7yT6w== 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=JRIL6/CfQA8Kjswn0c+rGygeg7AQia10G1qLQf+r7zA=; b=vKBjaxHI1+wz2TQHKUiVYi8jUUof3poI7y7O0KnqsS4BuJDRW1BGY4EGguwCqAMyIxzA50sZqPwPma1uYrrqTGNX9WG8KI1ZN1lCKBOFr7SMVc6CfJeZst9Z8IKgKX15MPB0CA8eAQM9PRIEm4we/MpSa9U85/NAoVUh01KVDcE= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by DM5PR1001MB2154.namprd10.prod.outlook.com (2603:10b6:4:2e::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Thu, 19 May 2022 07:27: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.5273.016; Thu, 19 May 2022 07:27:13 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH v4 1/4] kernfs: make ->attr.open RCU protected. Date: Thu, 19 May 2022 17:26:55 +1000 Message-Id: <20220519072658.736370-2-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220519072658.736370-1-imran.f.khan@oracle.com> References: <20220519072658.736370-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYXPR01CA0079.ausprd01.prod.outlook.com (2603:10c6:0:2e::12) 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: 195d5650-ec31-43b5-fb05-08da3968fe1f X-MS-TrafficTypeDiagnostic: DM5PR1001MB2154: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: xPP+hlC/P3fX7UxwSlnqMF7lfOjr/OwcSaXIiaYM3mlTHlB80YU5jMg8Rcu4NJOOat472esh9rlAywOLlDaxYHj9l3tLBCznxHrTTL4G1mHkof+IdiqaX2cj9rlVPtdLQbV+HFNTZmG1iSUoNqi5KHGNa0sqqhIDWyurqhDyvpwgpeick/C9kpsBVRIi+zG6dWV3bDK9yCfsuJqydJzHSsxTWKTxwTAEEPhtHejsFw86Epn5JY98VKg9U0xnNW5U1BekqMf551UyuuyDxUUm++ltTrDAl1AEhajzG1N5KhUBeS2acAWvcC5j7h/udbkOHZicT268KfDxh/wpaSDR7M5XmcMo8NpC8t0zQoSg4rqiseOFwJqiuVoCSLR/wyiHNMFvflWAf9qGU0JXCYzT/QHn5NiQNuJQKkrzQSngYzOFJkwiv8tI0fCcBkYgAM8Np/iAf4BK3mU4QOTcl0aLeqIkEQgLUFXSawl60iojQdLqQ6EtMfyPhOnbVhzt9k1Ku5pVeeK3rGvojBT61SY0vE7cdhh4FC4RoocxeQOA8FhG1OFX/Pl3t1O3LQ5JrXoxpLoUK0QLeTi3M7viVyjzdOqI6GK8QKcXy5S7bn3KPp3iyEzjCnXfVXoWe99082GREF5j455qlihRdnsQCVskPLD8eLGCICo2TuwMt/c4a7eAUuzkIcpppfTm7SEPxA6kWpLZdV1SHkIecdCnCrQgRg== 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)(103116003)(8676002)(83380400001)(86362001)(186003)(66556008)(6666004)(38350700002)(4326008)(66946007)(36756003)(66476007)(38100700002)(1076003)(6486002)(8936002)(2616005)(508600001)(52116002)(2906002)(26005)(6512007)(6506007)(5660300002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?HSg5vDU1Ojwt4wEuB1lJXjClCwUJAkAq6+ScTQximxibKh+T3e4B+BMyW4D3?= =?us-ascii?Q?VCDGjCzyb44TNP1WrdEE2/+e9j/dLdqolhSOHmL0I+DLyVA3vamOVdi4h0gY?= =?us-ascii?Q?uZjqc4hPhXJAq7n5Wi3UvOYFZtLWB2/qTmV3Rpkzlgwwu1Os9Yf+L7iEqWpd?= =?us-ascii?Q?CuW3YpfhpFpqEG9IFyfLi4oEmXMPDb7ak06qsfxShtA3T71LlxkZ9wXlr1Sh?= =?us-ascii?Q?K1QbwMr14OhRIuemqviegJ9wVyaRxs1k2G9wii+zqmFQGXDiWoN9nYINsAIY?= =?us-ascii?Q?sXT24DBzDctMVtg0GUrJ5X8X2VPj8qPzrXiGu5OT+jlS+KQ6X24uLqhBO8as?= =?us-ascii?Q?0dD72Wf2uEzK/0MWbrGv2JeQxx1ASxrd4EpH7PawIG/KawLJlpVp4U/lr/R9?= =?us-ascii?Q?WI9/lCBqRoXK3z3PiU3l7zo0o+5QUg/0S8ne07RccDRVPHyv7xxxXhhGiA80?= =?us-ascii?Q?+V6gff3oGxfddIQTmkgb2BGnzjrPkYFfZ2u1a4YRTGj+etsM80j5jL3+AJLL?= =?us-ascii?Q?U/otk0zv6pGZDPuKZOm8+dx4y60CWGuQyh7TmcFHasl+bl4OfMxD4eg7j1Db?= =?us-ascii?Q?kG1+1q4aAh1KlKTJQdJJmhWcMWVx3TGQ0ivdwRm3QrlAexYSRX6fhTxlyIDI?= =?us-ascii?Q?am0KjfEWK13rMRTvHCy4gXQzyGlX2VdjA5s1e5X2Gjos3ZJj1hWa9KjLLqg8?= =?us-ascii?Q?heOSbm/q8rju1FEq7ElpGmCWRpec6Z5ztZd6XJ3AirpXiVR9Qs3/dgOElPp+?= =?us-ascii?Q?J3LS209bpIf/Yu65ph3u66eDdTp9wJAi4Nd52V0hna5p2vPlQKntbg2GLQU/?= =?us-ascii?Q?uj8NdLm+XR4fcSBBH8RM2YyzK8kHOY+e0KqVd0x7E7/Zw3Wpv0VnKNI95TCB?= =?us-ascii?Q?CS0nQBRYAN1kW8uaOasOqpPDMIu81gJOLSD5fgBEO0kpsefdZiD4NW3JaRUU?= =?us-ascii?Q?MpG/LSBoGZtaJ2+6JziNl/sAbAQEZX+QoTXpThzEYl2AS2kCQdtbI8ApgpyH?= =?us-ascii?Q?LtX8FPhIjh2F1TVg8QL/6BCL/d1JYd3JIVzQmnHwnjpV889Yv1w57tif/1Rb?= =?us-ascii?Q?11QOWB98XqY4IScDmc3fo3F/jdOGjCgA/cXa+0VHFuZGAYsobrRPMCHIhIGw?= =?us-ascii?Q?UnEjEICRf3FJ3V/rdpAE73g52aInVzT1OL296fVMqZ/V3Srt6UsJ3Uhcydse?= =?us-ascii?Q?phgoekMit2aVs3bpAgEpCoBdusjHpNFnZkt1YBNqVsdAbVXPvieG0d0fg0Nu?= =?us-ascii?Q?Oz4Yf6d7duGvdNw6XMilrdnzOsvNGQhE7rA8QQCgoL7zrBpD0+Y56yPiGhJo?= =?us-ascii?Q?wo92DlyB1ZkG2Qm8pCehP9qmlTneCrsvBN1eAyHlwkoMat2TDjkbjJ0QEdmJ?= =?us-ascii?Q?fvhMeutSvHpCxVd8ELlSABlCXPThZXSd0zIosJtb8diQobQ43O+CUdQWeF1Y?= =?us-ascii?Q?coxkXL/R98qYkSdrlq2L7RCslokLEZWyDo8QOJxGR96mwe7+Z7lDXjkNKvX7?= =?us-ascii?Q?LifPflcLmUSR92wbyVrw93npZZTHXi9qOg8RCIS2miUPXXk7mcQZ6XZEbQZW?= =?us-ascii?Q?ni/mUXKIzFiYSTYs5P0y00UUuOc3oHRRtuqg/NYQ1ke6w7a6fmDOUni7QCMg?= =?us-ascii?Q?YZwUEAK8M8LbFyG7F16pMOgYVIT21SZ2+viol7yRkKzkWknPCQTahNJgBS66?= =?us-ascii?Q?IvVAw850wdOcN9DdtGGHFrxZmoWmIJU4qCa43eElsy1X1VKFk09qegBMzDsU?= =?us-ascii?Q?oCvj6jFWknTNH8zNT6NjsQJ6vzq0hIQ=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 195d5650-ec31-43b5-fb05-08da3968fe1f X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2022 07:27:13.0174 (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: VuHxmUTcWilo0FwdgxrOM8AVrxlhf4KSAzf+8EeiYAMNkfh8ce56dA33C3Vlog57cbftK+gc/5RxRQcgyrRXMg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1001MB2154 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.874 definitions=2022-05-19_01:2022-05-17,2022-05-19 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 malwarescore=0 suspectscore=0 mlxlogscore=999 adultscore=0 phishscore=0 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205190045 X-Proofpoint-GUID: LNPm0jE7gLE-cUmd2nFtDqVDw8edkBoK X-Proofpoint-ORIG-GUID: LNPm0jE7gLE-cUmd2nFtDqVDw8edkBoK 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 | 172 ++++++++++++++++++++++++++++++----------- include/linux/kernfs.h | 2 +- 2 files changed, 127 insertions(+), 47 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index e3abfa843879..8264e28f9098 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,77 @@ struct kernfs_open_node { static DEFINE_SPINLOCK(kernfs_notify_lock); static struct kernfs_node *kernfs_notify_list =3D KERNFS_NOTIFY_EOL; =20 +/** + * kernfs_deref_open_node - Get kernfs_open_node corresponding to @kn. + * + * @of: associated kernfs_open_file instance. + * @kn: target kernfs_node. + * + * Fetch and return ->attr.open of @kn if @of->list is non empty. + * If @of->list is not empty we can safely assume that @of is on + * @kn->attr.open->files list and this guarantees that @kn->attr.open + * will not vanish i.e. dereferencing outside RCU read-side critical + * section is safe here. + * + * This should ONLY be used by readers of ->attr.open and caller needs + * to make sure that @of->list is not empty. + */ +static struct kernfs_open_node * +kernfs_deref_open_node(struct kernfs_open_file *of, struct kernfs_node *kn) +{ + struct kernfs_open_node *on; + + on =3D rcu_dereference_check(kn->attr.open, !list_empty(&of->list)); + + return on; +} + +/** + * kernfs_deref_open_node_protected - Get kernfs_open_node corresponding t= o @kn + * + * @kn: target kernfs_node. + * + * Fetch and return ->attr.open of @kn when caller(writer) holds + * kernfs_open_file_mutex. + * + * Update of ->attr.open happens under kernfs_open_file_mutex. So as long = as + * the current updater (caller) is holding this mutex, other updaters will= not + * be able to change ->attr.open and this means that we can safely deref + * ->attr.open outside RCU read-side critical section. + * + * This should ONLY be used by updaters of ->attr.open and caller needs to= make + * sure that kernfs_open_file_mutex is held. + */ +static struct kernfs_open_node * +kernfs_deref_open_node_protected(struct kernfs_node *kn) +{ + return rcu_dereference_protected(kn->attr.open, + lockdep_is_held(&kernfs_open_file_mutex)); +} + +/** + * kernfs_check_open_node_protected - Get kernfs_open_node corresponding t= o @kn + * + * @kn: target kernfs_node. + * + * Fetch and return ->attr.open of @kn when caller(reader) holds + * kernfs_open_file_mutex. + * + * Update of ->attr.open happens under kernfs_open_file_mutex. So as long = as + * the current reader (caller) is holding this mutex, updaters will not be + * able to change ->attr.open and this means that we can safely deref + * ->attr.open outside RCU read-side critical section. + * + * This should ONLY be used by readers of ->attr.open and caller needs to = make + * sure that kernfs_open_file_mutex is held. + */ +static struct kernfs_open_node * +kernfs_check_open_node_protected(struct kernfs_node *kn) +{ + return rcu_dereference_check(kn->attr.open, + lockdep_is_held(&kernfs_open_file_mutex)); +} + static struct kernfs_open_file *kernfs_of(struct file *file) { return ((struct seq_file *)file->private_data)->private; @@ -156,8 +227,12 @@ static void kernfs_seq_stop(struct seq_file *sf, void = *v) static int kernfs_seq_show(struct seq_file *sf, void *v) { struct kernfs_open_file *of =3D sf->private; + struct kernfs_open_node *on =3D kernfs_deref_open_node(of, of->kn); + + if (!on) + return -EINVAL; =20 - of->event =3D atomic_read(&of->kn->attr.open->event); + of->event =3D atomic_read(&on->event); =20 return of->kn->attr.ops->seq_show(sf, v); } @@ -180,6 +255,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 +277,15 @@ static ssize_t kernfs_file_read_iter(struct kiocb *ioc= b, struct iov_iter *iter) goto out_free; } =20 - of->event =3D atomic_read(&of->kn->attr.open->event); + on =3D kernfs_deref_open_node(of, of->kn); + if (!on) { + len =3D -EINVAL; + mutex_unlock(&of->mutex); + goto out_free; + } + + of->event =3D atomic_read(&on->event); + ops =3D kernfs_ops(of->kn); if (ops->read) len =3D ops->read(of, buf, len, iocb->ki_pos); @@ -519,36 +603,29 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, { struct kernfs_open_node *on, *new_on =3D NULL; =20 - retry: mutex_lock(&kernfs_open_file_mutex); - spin_lock_irq(&kernfs_open_node_lock); - - if (!kn->attr.open && new_on) { - kn->attr.open =3D new_on; - new_on =3D NULL; - } - - on =3D kn->attr.open; - if (on) - list_add_tail(&of->list, &on->files); - - spin_unlock_irq(&kernfs_open_node_lock); - mutex_unlock(&kernfs_open_file_mutex); + on =3D kernfs_deref_open_node_protected(kn); =20 if (on) { - kfree(new_on); + list_add_tail(&of->list, &on->files); + mutex_unlock(&kernfs_open_file_mutex); return 0; + } else { + /* not there, initialize a new one */ + new_on =3D kmalloc(sizeof(*new_on), GFP_KERNEL); + if (!new_on) { + mutex_unlock(&kernfs_open_file_mutex); + return -ENOMEM; + } + atomic_set(&new_on->event, 1); + init_waitqueue_head(&new_on->poll); + INIT_LIST_HEAD(&new_on->files); + list_add_tail(&of->list, &new_on->files); + rcu_assign_pointer(kn->attr.open, new_on); } + mutex_unlock(&kernfs_open_file_mutex); =20 - /* not there, initialize a new one and retry */ - new_on =3D kmalloc(sizeof(*new_on), GFP_KERNEL); - if (!new_on) - return -ENOMEM; - - atomic_set(&new_on->event, 1); - init_waitqueue_head(&new_on->poll); - INIT_LIST_HEAD(&new_on->files); - goto retry; + return 0; } =20 /** @@ -567,24 +644,25 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, static void kernfs_unlink_open_file(struct kernfs_node *kn, struct kernfs_open_file *of) { - struct kernfs_open_node *on =3D kn->attr.open; - unsigned long flags; + struct kernfs_open_node *on; =20 mutex_lock(&kernfs_open_file_mutex); - spin_lock_irqsave(&kernfs_open_node_lock, flags); + + on =3D kernfs_deref_open_node_protected(kn); + if (!on) { + mutex_unlock(&kernfs_open_file_mutex); + return; + } =20 if (of) list_del(&of->list); =20 - if (list_empty(&on->files)) - kn->attr.open =3D NULL; - else - on =3D NULL; + if (list_empty(&on->files)) { + rcu_assign_pointer(kn->attr.open, NULL); + kfree_rcu(on, rcu_head); + } =20 - spin_unlock_irqrestore(&kernfs_open_node_lock, flags); mutex_unlock(&kernfs_open_file_mutex); - - kfree(on); } =20 static int kernfs_fop_open(struct inode *inode, struct file *file) @@ -774,17 +852,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_open_node_protected(kn); + if (!on) { mutex_unlock(&kernfs_open_file_mutex); return; } =20 - on =3D kn->attr.open; - list_for_each_entry(of, &on->files, list) { struct inode *inode =3D file_inode(of->file); =20 @@ -815,7 +892,10 @@ void kernfs_drain_open_files(struct kernfs_node *kn) __poll_t kernfs_generic_poll(struct kernfs_open_file *of, poll_table *wait) { struct kernfs_node *kn =3D kernfs_dentry_node(of->file->f_path.dentry); - struct kernfs_open_node *on =3D kn->attr.open; + struct kernfs_open_node *on =3D kernfs_deref_open_node(of, kn); + + if (!on) + return EPOLLERR; =20 poll_wait(of->file, &on->poll, wait); =20 @@ -922,13 +1002,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 Thu May 7 20:26:41 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 A1B86C433F5 for ; Thu, 19 May 2022 07:28:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229824AbiESH2W (ORCPT ); Thu, 19 May 2022 03:28:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235072AbiESH1b (ORCPT ); Thu, 19 May 2022 03:27:31 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8270B8A323 for ; Thu, 19 May 2022 00:27:27 -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 24J63xoA007800; Thu, 19 May 2022 07:27:18 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=hdcN2DxHu0DQ013HIm/YggatbrfmzDjJg/4IfCBTWbk=; b=ktEOJxgdxZh4gTtZrFyZsEjIJ0rHi4DrrH1m0y568syAZqsnbQIe/jbAccyhKGpeZ4s7 3QJMTlSYIKoqFJmJpCSM/zB5KsQAlobYVsXjRfRuWHrcvqJkJFMwQn0+yZw5HDE7NGVq puwQZ2zboYg3xmQ5hwOBXo10CFCI/lRJDgbwCwy0tmnk/DUne56E2lQKMnFJKMwi+qrr 5YOOQmgHg2h3iB3EdWAyHPtdfr3PHs1BHEK0NFOutlCB+hz4gx7ZrLeg42HirrE4LYoP cWbZsuQvkWOOzxjvVwWyCw2G2+mkKnGI8a7IZ6e0UGDkr/TYpGPqXC9f2A1zaslZAJ7i ng== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3g22ucb3aj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 May 2022 07:27:17 +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 24J7G5Aq005601; Thu, 19 May 2022 07:27:16 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2174.outbound.protection.outlook.com [104.47.58.174]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3g22v51a8r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 May 2022 07:27:16 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KMqztEP6vUK46uBbJvaAiB+cGTNT2FzCkI5rMybNZ4xqjdahvZ6VgLW4McYDi582hO0znrcc4x/19cEtqdfqxV93SpDInFfOzCkPWHiIQpxjPgGUzn3Uvq5ikAiVY9cljNDNifFE24h6FDnWili9RUkbuWnU498i36BfHs1v+qGWleOwyDGPqZUL0qK+QxYll6i50latZaR/kQyjXfPxu9n4dCiYcQ4GtvVoKIkUfM9FpokvbdGXz6Hw8KC5ZI7UjjEE1JtHWRWW8KLVxMuah8Ub1xvaRcWp+Z2A6LmA7e8+ST/BKVIHoU/6U4doCyEyDpRPmzuc9DTRRibdYB3L8A== 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=hdcN2DxHu0DQ013HIm/YggatbrfmzDjJg/4IfCBTWbk=; b=k69tedihOKYcjYHxvNXLookUAvNX/omN3qM5Jf5xWAAMXXyHySI3guii3mF0X+V5YOGrLIyjMyoBoY4yysYzNpsbcxGeeI9iwJY9eNYvZ+NmLs9iVAZadhbwpDHm0jn4DBUOMWkHJyvMicVs7x1JrzVcF6emblg9Z2xwN6VeK3bK2PMLVVUBT5c5QIGaIluikzEBmEmly+DKHRLrAZjHzQUCHWSSKK6lXY2rqU1dwu79umhG0CCmqivZHkmCM0GGOaNV0U9Ij+vkqFUIU7RDc3y61PHOHD2ZPjnvTz8OFrQPvgCytq1b+Cg/M0aQm4aFUVeWt8iuMPeT5mSZ0uUq7w== 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=hdcN2DxHu0DQ013HIm/YggatbrfmzDjJg/4IfCBTWbk=; b=YHHrv7sZzYcvAUSAUmEYaQXN1ZwYp0cUlyt3G4eaPamKEJVQMVqsgl9NBHKdeuF701Qyxn/PkSbMwg93kTygfP6+DXskQlWaW/D68/jI4aC29dymEm2Ru0A24tyDok7rQe0n7ilavA/OfvMiCKNmxCcaZUpCcaNRMVgODrnI0tg= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by DM5PR1001MB2154.namprd10.prod.outlook.com (2603:10b6:4:2e::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Thu, 19 May 2022 07:27:14 +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.5273.016; Thu, 19 May 2022 07:27:14 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH v4 2/4] kernfs: Change kernfs_notify_list to llist. Date: Thu, 19 May 2022 17:26:56 +1000 Message-Id: <20220519072658.736370-3-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220519072658.736370-1-imran.f.khan@oracle.com> References: <20220519072658.736370-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYXPR01CA0079.ausprd01.prod.outlook.com (2603:10c6:0:2e::12) 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: 1ff5846b-65a3-4395-43f7-08da3968ff1a X-MS-TrafficTypeDiagnostic: DM5PR1001MB2154: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: eCRTObryh9NxDZowpgX1G+Ga6UjJXvdaG04DEsW9omRNIMkHFlSUSrx4gFsswiTYkRBvEmmOoKdvplW7CVDfSKu6VurgEtNY5VeMG6cfkSvqKx1ZnTARZG3howdHkwOxrnSg+k9lC0FBx0FdVgShfKHesUjChA2fDjmfgJIKH7wGsb3ezOB0CNWCxZCzVebbzdz0j7RjOPszmPYNJkep5jItdXBVT6mgV9wc4SF4AS+fWDggoB7lIEx2QoW7Atx31ClR+I5a6yAA1en1v6aihYevVOOSKqp+rUT+RqfUkeq9E7RWhf///Wjy66qxkozXgsGbqA7PJHb4VWEKo+/SRpeBJGnrQDKszDgMajQHWOITjgv1HXZvM1KrH7serCbCLiARa3hqbAcdKcdIi86hF3/ISyHB43RxOlhhYWMu7iELXR4Pw5nQd0GDbTx0KDggpbzbInkmNimBzJvUdB8pU6eTSFO/eE4jMYDtvQhrv9HacGmeEAsedzSty5m9uV5RIkSU1v5fW4CjJVDvZ9SNrNrasm9fwNxWoc3YP0dW/jqnPstOocCuNmEoahlItTo68K1diwocxrwy1LpqxKRwW/p2gN2S3aRNNtZAXIYvwI9Upyv7WY6ZMlNE7GRZcH4ceoVMjNpMHhBGclF8KL8ybfUB4Ym7JnclapKSuYmeAusjD7JyL72OeG1jZ8voSosU73wQ2J6CCM4O7iNhpyUcBg== 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)(103116003)(8676002)(83380400001)(86362001)(186003)(66556008)(6666004)(38350700002)(4326008)(66946007)(36756003)(66476007)(38100700002)(1076003)(6486002)(8936002)(2616005)(508600001)(52116002)(2906002)(26005)(6512007)(6506007)(5660300002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?U58cVmQblJNTd5q2vU/of5ZXY6eVCyU3M056LUlM20K8XEBO+uYx2BPa9iAM?= =?us-ascii?Q?plOkuZeDHmzHOQNICMqBynmb5YEYSmWjXtWmyrga3mkxVpXm6nTJYy8ZTphL?= =?us-ascii?Q?VDsXMnB2e+kN7dnKSHzGauCY2+zg46jIgZFn+JukfSAny8PNoFZTL/Yr/Lfo?= =?us-ascii?Q?hjpzlKfJ9GtOgDa8O5jYe0QqgpctJlTQQPkp6YgJXcwnYMLqfQaCvWvjzDfh?= =?us-ascii?Q?EhAKH7vVLTTiF5BwF4AHzoAz/ga93Tyf55mo6qvluEJ1iu72yMRUuukRC58D?= =?us-ascii?Q?yGPej3J6THCuEFwqeNxzzppFLr/df4u4Zl/54FC/D5vipN+KTFm2DpETO2S4?= =?us-ascii?Q?ZQMWVun4/34pmbv/8RQXBj1vUNFQ0weyZaukvr4kw9ZOECR+6xbDUGmSvYvh?= =?us-ascii?Q?OC1EUMV2z4BpOoCHj9EyhLG9aZQtZIXcQ8ozfJzT6R0unBjdLjVM3HZvAcEI?= =?us-ascii?Q?UbKwxWGKJPe8XK3j1h5vPU5kaC4ViBxUxn9U+2nq2GObT0WbAzpK8ARwyjhJ?= =?us-ascii?Q?YvT51DS7l7B9UKN7oR+Ms5jF0g+OqRMDcMU6LaML/ynjuaRqUul3lQFW5YCx?= =?us-ascii?Q?UVyNA/2erSLJ/hoJsAr2BK7Uf80ZGxe9QDNtT7g2e70MFoJPTeLomOVv5Yto?= =?us-ascii?Q?HdoUmKjHt7z8ZsdFZVSURQIDd+HIZb/pa9CB9/vZ42iNCraU8qDzN/U8xjbl?= =?us-ascii?Q?854OeXZEhqMvMs9OtJkjdYShWYeD1AOL4bwxwIPOLpppRsLI6tca4m0x+25J?= =?us-ascii?Q?GhRkL2hN6qCpJ2EcoLdDSHvGfu3fHi2VWeICtikq4mcEK1AzaVJUeL54k4Pb?= =?us-ascii?Q?u8RCt5KboqY75y5w6FpCqUGfqaIxBAhZBcqTXABCznykLR0Cp941BHxsj3hG?= =?us-ascii?Q?Kts3bDxK3l/dBdjH5/nCBGQ6nUxjVXk3adJo7DOkcw1DoaptWaHzYl2jm5QS?= =?us-ascii?Q?xl6bCvT3ekOYoA14KQL2giE60tObHTqh6iLdY3K+lRVvmKkyN7XB9QEQRhAP?= =?us-ascii?Q?Zfk/F5/ndj5ESVDtYAXn3RLlI7FK5HUpRo1jtD2e/1zDPirSfd6BxDbJTG/7?= =?us-ascii?Q?yWDe3YF+DI9M62jrtWmnvNo+FE8S8mXwUV6U7lp08li9a/mO50wk/iQ4Fwl3?= =?us-ascii?Q?PkZ1POCFg71AB0QWvZ1Mg9AYSROzqHJ9f1+UOJHk7xMmTNLFFRw/DBvNGdJx?= =?us-ascii?Q?v0z6o77U609hb7Pfby/MVyEJ22ze4CcA8uUtI6Rf0jxhdVA/tMtgV2+LLsza?= =?us-ascii?Q?NagAcmX/0TIat2PVvnSvJsaYorZ/stt2xMoWIC/WDT5IKcCaPahwcuHv8IpJ?= =?us-ascii?Q?Tv+MiG/Fu3My0QJfmPR5RAmBlXlvPGPCqwM3ew/fcMCEl0Eukl1jZ3ZuImlq?= =?us-ascii?Q?Zqfomifv4mIfRru0QZt6Z+akU6pjE5MT3R6wSg7KWFN6OEN9ZfZWbyMogIfW?= =?us-ascii?Q?YbFDQHEdwi3ttt0itiDCoVe5uas9w3cnVgK2wfUHCK4y/hobudD1gnrUf5zL?= =?us-ascii?Q?CRT/xF68gUSvAHXI7mt2zIiaAEGiLb65DsT8Ruyu59IHifS6wis8UUTvtK5s?= =?us-ascii?Q?fyA1zo90msbqMb5P5HMqw3I9+4QXkjgIBAwfNMHvMDo75fiQQRE2qGyduzEr?= =?us-ascii?Q?vKrwja+Sp+PiYIqcDA/ZkN6ZbPsXERtU0VyGlOVXq0C63TNTwk3IWj/ksEAy?= =?us-ascii?Q?mmcsdBPyPu1o0/BbsnUlocY3HCejVgIUgEDPTEqahs0KyOFLSXX6b68sZ/2Q?= =?us-ascii?Q?xH7ailxSJbYoh7pRYbA+oywuStwQMy4=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1ff5846b-65a3-4395-43f7-08da3968ff1a X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2022 07:27:14.6721 (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: GagWBv3r9nprIoxwMpj0Ulas2iVcpDqHHRFzA6h45cVQn70eRr6rp0o6jmiCRvQlfnenbUPTNImR6vULT37cAg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1001MB2154 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.874 definitions=2022-05-19_01:2022-05-17,2022-05-19 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 mlxlogscore=999 spamscore=0 bulkscore=0 malwarescore=0 adultscore=0 suspectscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205190045 X-Proofpoint-GUID: F4AmjUkmB3SWdE0AY7av6mXXW0gfJoLA X-Proofpoint-ORIG-GUID: F4AmjUkmB3SWdE0AY7av6mXXW0gfJoLA 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 8264e28f9098..3f883601c5ec 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -38,18 +38,16 @@ struct kernfs_open_node { struct list_head files; /* goes through kernfs_open_file.list */ }; =20 -/* - * kernfs_notify() may be called from any context and bounces notifications - * through a work item. To minimize space overhead in kernfs_node, the - * pending queue is implemented as a singly linked list of kernfs_nodes. - * The list is terminated with the self pointer so that whether a - * kernfs_node is on the list or not can be determined by testing the next - * pointer for NULL. +/** + * attribute_to_node - get kernfs_node object corresponding to a kernfs at= tribute + * @ptr: &struct kernfs_elem_attr + * @type: struct kernfs_node + * @member: name of member (i.e attr) */ -#define KERNFS_NOTIFY_EOL ((void *)&kernfs_notify_list) +#define attribute_to_node(ptr, type, member) \ + container_of(ptr, type, member) =20 -static DEFINE_SPINLOCK(kernfs_notify_lock); -static struct kernfs_node *kernfs_notify_list =3D KERNFS_NOTIFY_EOL; +static LLIST_HEAD(kernfs_notify_list); =20 /** * kernfs_deref_open_node - Get kernfs_open_node corresponding to @kn. @@ -928,18 +926,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); @@ -995,12 +991,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); @@ -1011,14 +1009,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 Thu May 7 20:26:41 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 3728BC433EF for ; Thu, 19 May 2022 07:28:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234873AbiESH2C (ORCPT ); Thu, 19 May 2022 03:28:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235071AbiESH1b (ORCPT ); Thu, 19 May 2022 03:27:31 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8391E8A33A for ; Thu, 19 May 2022 00:27:28 -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 24J5wx1t027449; Thu, 19 May 2022 07:27:18 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=/deI2k3jrtHe6bz5npsCsAFZC0VFGCAKb/rvsti9RCE=; b=dFYTHNsrqxi2A7TfkrYXdURVxIWG6VlKusflxBWPMlnAkxSlU6w5cQhxSGUJ7eG02/np +c09VBWcGruIqKc12wTNExYctImsbDAR/ofVcfYxj0JbhdmN4kRmvzZ1aL/IHwgniFJJ PDdKsL1/KyL7iL/73vpSVsGIgQk7WREvvyBhPyeDevVKTX7tY+WCtVu8jFlu03KzZGSu 3nheD0+hFeZ/uO9GsBoq1CD94PrVkaOGBNd6zSgwMK5Jd7HjLZtx+R0eIXDs+s+8j/xm M9wDMTFsfmMB8KH9iAkM+8IlUSWKwFnOyH+7mEsDG4VuDkxN7IjXvWfRYHa1OZEPHgIK +w== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3g241sbcs7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 May 2022 07:27:18 +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 24J7FaUR020863; Thu, 19 May 2022 07:27:18 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2176.outbound.protection.outlook.com [104.47.58.176]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3g22v4rajd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 May 2022 07:27:17 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nV870Z5HV9hcTqKXhqb4mcT6epxlCxn2JsY3VUH92oQsIOujax94b/dTG+qfivGK0eSPSIW2g7P62lOL4nx4Q+AF/PrT6+DdyoMFkErM5lME28ajTon2S+SDnGk3yIj9IsvWnBpp9oWymNYbyuB5qHDUW0coFpRhDbNrXLJEt/LPutUS5ntRk5ZiuBFKtj17tEB2rcsAg+n8Y8ISiQ5aAyS6X1IQvdRANSulhlme4+6onJp2ZTGNPvJ0sDjeDi9wAs3vM/nG9Dj3aOAzeUHnVoPkOtPRpmmP5dvdx2A10TA9f7YuAfLcxWQXp2xFx439JQpkpd3LZDDJYjhvc928ww== 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=/deI2k3jrtHe6bz5npsCsAFZC0VFGCAKb/rvsti9RCE=; b=Bxn4F/on1N/heBPfJkmwcTdg0JL3H/dQJb1pmEULfgoQr1ERoFxjh3VEVTL5ylIsnzQNamxaPf1/jks20Vs1f5FuwcYD9YvAwYECvF2FPU7uWFCXINsOU3/JIU7V7a9AQcEArQAYcdI7+KZ9YSIBJ8OLU23Eigh+WIXWNKy8S5UCu2It7KgELe6SX4+ZFW7YJRTTXASt8jYRMPCIR2byZ2wsN38KELu4fv8TB4s/GJKeDqcIBvefGG77iLty/rR71IK3DSxKIIKxof3DYhOUOy3kejj+MCIJU1htBL/GWK++Gxr24dAoDtAuuubpV37xNFN66eH157RoO2tJo9DvHg== 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=/deI2k3jrtHe6bz5npsCsAFZC0VFGCAKb/rvsti9RCE=; b=S/nlvk8ElV4ifN02t53ibdbSfydYdFSRwk4/1+SlFKFQdv2DIEDLdFpdSwvpQGK1K7CI+YBF/j0qe8sW77n5UicngoWTYkuEPnftoRHK9BlabNSoyFeFx80FUuinuY44FgjhtSwYlx1xYrF2uy+iP96v5cbDOP1CNrdxivy+QrA= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by DM5PR1001MB2154.namprd10.prod.outlook.com (2603:10b6:4:2e::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Thu, 19 May 2022 07:27: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.5273.016; Thu, 19 May 2022 07:27:16 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH v4 3/4] kernfs: Introduce interface to access global kernfs_open_file_mutex. Date: Thu, 19 May 2022 17:26:57 +1000 Message-Id: <20220519072658.736370-4-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220519072658.736370-1-imran.f.khan@oracle.com> References: <20220519072658.736370-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYXPR01CA0079.ausprd01.prod.outlook.com (2603:10c6:0:2e::12) 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: 5f9c0ca6-d16c-49f7-3050-08da39690012 X-MS-TrafficTypeDiagnostic: DM5PR1001MB2154: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: 1Vt2cKwQVRBcqN5971cAMJaZ0OLuChsgUNRmUg2kll+2ODuWoqVPxGBW/g/59LwEpNC+CppVMTFJIpB/C+RIOhL2t1LfNHilKtcXuJTWQkcZs6d6qxs5Vw1HjU7xVt2haTH1vG431n/EHFfDL7ReKKY7EpiSgNOVwple/He6/TgVsWiucFmQxZDYAuzxZuBd0lRD+SQtLQ8IlSf5bQJfPxkNZgLaAVrHWMeB3rUFchDzAAazhmssidZYiWR4QwZ16IfRHI2/Of/gYpTfvFXk7Et7u5Mo9Dy7hkkvmBqxIw3IcihMZPQdenXnDeajEX5MWmAUS8yaOwCztU4YbasQRDv8Ze6dXP+co4pVaAga074to4DY9huNajG02/eURTnU8xihvuDd2rqXBqXors6GuI4DChjh1x0+hUfD7qU3hLuqFZy3ohZedxgHvcHGNy0fg/rRPGpT1FXh+xnfuJtOvSpzD/tczfOwT/v2W5OCvDVcsZS5eOPwMhKS6rLOgocrraMkCiZv0gC4qEGWBR2oZe6YQyW7BYj8sFwS8qE7tLZHS1JOU+JAKHZPuAvdHXduOPl7AixtO/HnHdACmbU57Y8QVGGFiJUOHpcBOE6WEt/PgwtGTOPjAzbR7sXA0yKtAdG3akAlYiqD/KMtbPaouCuZu0y8OZM340Om9vx9v9k47KRzW6SVtlhtieCNKjFB33NJ0rOOof437jRX0n93iA== 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)(103116003)(8676002)(83380400001)(86362001)(186003)(66556008)(6666004)(38350700002)(4326008)(66946007)(36756003)(66476007)(38100700002)(1076003)(6486002)(8936002)(2616005)(508600001)(52116002)(2906002)(26005)(6512007)(6506007)(5660300002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?qAsLn5uftDdUhlrEI60f+ExfIKccIfEgATETmgym3GHc4/0miklP11u2rcKX?= =?us-ascii?Q?brc8zB3YuyqeasLjYNbRzuFub9t3ZsKTM5PHM4C9BRUa5K6DTyiaUj6KLY/4?= =?us-ascii?Q?kU7tcPyVB70MpcHOe2skd4gwJiHMfBkcSKNw4px0P84NPA+74v0wWvQFFIA/?= =?us-ascii?Q?Kd/Z30JqRGu6uBJoS59d0ztYKOR3kzHqFuOpSeLiU04rtsDr1xAHo2iv3oHR?= =?us-ascii?Q?b4r2SlpCDJH7uAEHNH1EZljJV6NgiXHwItQ2Oq3yAMa8FP1/A87YwuwRl2Aq?= =?us-ascii?Q?wCoirVCtTmqYE+zthXl8dl1aCKius/mNyI7Id/EVh8XYGVYh5mHycYA0610U?= =?us-ascii?Q?ztR9kzj5GVLfk9B7NubP3/Jcr+XwVY2/sPHNbi0ANvwKGSMkEPUKhzWQRZoz?= =?us-ascii?Q?SiyI9mwWJzI5LeCSIvOqLFP03PDVq3z+oC0ebLFfoFDR5Jc8WRMHOjYTrLhq?= =?us-ascii?Q?cvAzHvTU5in+NzOzmaP+yBmVzT/OPsBXClOpMtZnsi4cwmkGPXeMbiHJtCW1?= =?us-ascii?Q?MMixx4My2fToiMVoNRuSCSVq4oZJVdLY58faJxZkYTIqu/SaTwdoEMJA0RuU?= =?us-ascii?Q?GKG7AV/ekgNWZHi1yVwA+YuPwSt1YSvB4ymAFLVGdaZmN9yYs0FmduuHxvtY?= =?us-ascii?Q?ZNRzkXL8O8kmenmEaF1Wc4fN6WrPaLMMuXvrRmoWC/MpchyqCY36meTdBjsf?= =?us-ascii?Q?2XRP9/80aMAieIEi2eJUKGXJA4BeS1WjpN3Y/xbPppSuR48BUvQfFbqzxSEM?= =?us-ascii?Q?hVcp9FGFQho8zeBlLtWOnj9N/Z/yQsje4HL8TuglS7pJzOzmMKRfTNKVOsUN?= =?us-ascii?Q?enYwS7AOaiAXPMgIXVwSDlyaItf8Nir5EpYacMbNBDiICEvz7fu3BxqekXU9?= =?us-ascii?Q?3XXLTVFGObhwn+uwkzcjYTRZ7V3kdl1mdqUICCnftXIATgNIuh04JfvJS96v?= =?us-ascii?Q?jlK9XYLbTQ4haSxyaZe89CtKbhO6mAPbk6qQjsuLvoxifRUpINfrQzyaihuc?= =?us-ascii?Q?lAq1iu4NQSJi1BtE1xp0OJeqlPOFumf+FKEg/owmLKFq+pFyGyJaSQsHVGrA?= =?us-ascii?Q?IcQngnGGhT8wBYWWV79UEI5ozlW3NxEAu7mMPgd0P7J5qnyNMdyqHEOiKNZX?= =?us-ascii?Q?lQGoKlg/7gRP+Cjev91GQMYK/wWjmbOYglsP/3DJl1qm0EqUTIaGKt+8oiOQ?= =?us-ascii?Q?5b3mCPUm8nnAO9Hgu2U4U0+Oz0LOxstJD1aVNmXlF1k6BNptvR6nMC1LsXtW?= =?us-ascii?Q?9x7w//lV6emVLo4UOCoJpIj8QTiZnttWwz1vI90BsIxqOrPD2NJEGJS/r1Gv?= =?us-ascii?Q?0vfYJpsgEREw3SDxzn6WQUzzJlCEg+S5luwKlCicKlodK6epAVpGiViOkTDH?= =?us-ascii?Q?EjVyc7+enFTuWkDmrJzNUrlGAvnzhTw8jXcfa8PMmiGyxWu8me+D8CpXqaUg?= =?us-ascii?Q?mGCIC432K/1ngef0MMGh4EIoueD7qHI1Tn2uJSg9l3k81W1Z1XGWmHrkD+4Z?= =?us-ascii?Q?VpzCXwe8KCQ19Wk/lQ1uprq5IdyShN+RAJo7Ao+ZCr2Cvh4PXk18W4izmUsR?= =?us-ascii?Q?MGObaBMEOJTDD2nVdboXI0TrWYZi1mRLaQKv5oZFBaq88JbsdH8tbodbp7Qp?= =?us-ascii?Q?tzB7U843CB0vs3qx90tFSb1ZR1mclYXr+GzEYsKnIX0N9rs9vNoSmQ4arzCC?= =?us-ascii?Q?kheyeCJfa8TwD9DBFFy9BKOHnoTdLAF96DIWsNPyZKFoEVV0WrZPmwPBPGQS?= =?us-ascii?Q?x8o4wCGISq7IL0qYehXOF26VxBMBc1w=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5f9c0ca6-d16c-49f7-3050-08da39690012 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2022 07:27:16.3294 (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: ZEOj36HBFjRMLWLbI/h0JuFQ8P8RJ3T1DDrxmqbh1MlqYiEBTcih/jPJdiThYZss90xptJamBv5l4C74hxeb5Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1001MB2154 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.874 definitions=2022-05-19_01:2022-05-17,2022-05-19 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxscore=0 suspectscore=0 phishscore=0 bulkscore=0 adultscore=0 mlxlogscore=999 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205190045 X-Proofpoint-GUID: V4Xh8p_4xqYfEW2z9YJvRNTf8EBbxT1- X-Proofpoint-ORIG-GUID: V4Xh8p_4xqYfEW2z9YJvRNTf8EBbxT1- 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 | 72 +++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 3f883601c5ec..87ea8127c98c 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -49,6 +49,22 @@ struct kernfs_open_node { =20 static LLIST_HEAD(kernfs_notify_list); =20 +static inline struct mutex *kernfs_open_file_mutex_ptr(struct kernfs_node = *kn) +{ + return &kernfs_open_file_mutex; +} + +static inline struct mutex *kernfs_open_file_mutex_lock(struct kernfs_node= *kn) +{ + struct mutex *lock; + + lock =3D kernfs_open_file_mutex_ptr(kn); + + mutex_lock(lock); + + return lock; +} + /** * kernfs_deref_open_node - Get kernfs_open_node corresponding to @kn. * @@ -80,21 +96,21 @@ kernfs_deref_open_node(struct kernfs_open_file *of, str= uct kernfs_node *kn) * @kn: target kernfs_node. * * Fetch and return ->attr.open of @kn when caller(writer) holds - * kernfs_open_file_mutex. + * kernfs_open_file_mutex_ptr(kn). * - * Update of ->attr.open happens under kernfs_open_file_mutex. So as long = as - * the current updater (caller) is holding this mutex, other updaters will= not - * be able to change ->attr.open and this means that we can safely deref - * ->attr.open outside RCU read-side critical section. + * Update of ->attr.open happens under kernfs_open_file_mutex_ptr(kn). So = as + * long as the current updater (caller) is holding this mutex, other updat= ers + * will not be able to change ->attr.open and this means that we can safely + * deref ->attr.open outside RCU read-side critical section. * * This should ONLY be used by updaters of ->attr.open and caller needs to= make - * sure that kernfs_open_file_mutex is held. + * sure that kernfs_open_file_mutex_ptr(kn) is held. */ static struct kernfs_open_node * kernfs_deref_open_node_protected(struct kernfs_node *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 /** @@ -103,21 +119,21 @@ kernfs_deref_open_node_protected(struct kernfs_node *= kn) * @kn: target kernfs_node. * * Fetch and return ->attr.open of @kn when caller(reader) holds - * kernfs_open_file_mutex. + * kernfs_open_file_mutex_ptr(kn). * - * Update of ->attr.open happens under kernfs_open_file_mutex. So as long = as - * the current reader (caller) is holding this mutex, updaters will not be - * able to change ->attr.open and this means that we can safely deref + * Update of ->attr.open happens under kernfs_open_file_mutex_ptr(kn). So = as + * long as the current reader (caller) is holding this mutex, updaters will + * not be able to change ->attr.open and this means that we can safely der= ef * ->attr.open outside RCU read-side critical section. * * This should ONLY be used by readers of ->attr.open and caller needs to = make - * sure that kernfs_open_file_mutex is held. + * sure that kernfs_open_file_mutex_ptr(kn) is held. */ static struct kernfs_open_node * kernfs_check_open_node_protected(struct kernfs_node *kn) { return rcu_dereference_check(kn->attr.open, - lockdep_is_held(&kernfs_open_file_mutex)); + lockdep_is_held(kernfs_open_file_mutex_ptr(kn))); } =20 static struct kernfs_open_file *kernfs_of(struct file *file) @@ -600,19 +616,20 @@ static int kernfs_get_open_node(struct kernfs_node *k= n, struct kernfs_open_file *of) { struct kernfs_open_node *on, *new_on =3D NULL; + struct mutex *mutex =3D NULL; =20 - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); on =3D kernfs_deref_open_node_protected(kn); =20 if (on) { list_add_tail(&of->list, &on->files); - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return 0; } else { /* not there, initialize a new one */ new_on =3D kmalloc(sizeof(*new_on), GFP_KERNEL); if (!new_on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return -ENOMEM; } atomic_set(&new_on->event, 1); @@ -621,7 +638,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; } @@ -643,12 +660,13 @@ static void kernfs_unlink_open_file(struct kernfs_nod= e *kn, struct kernfs_open_file *of) { struct kernfs_open_node *on; + struct mutex *mutex =3D NULL; =20 - mutex_lock(&kernfs_open_file_mutex); + mutex =3D kernfs_open_file_mutex_lock(kn); =20 on =3D kernfs_deref_open_node_protected(kn); if (!on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return; } =20 @@ -660,7 +678,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) @@ -802,7 +820,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) { /* @@ -819,11 +837,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); @@ -838,6 +857,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; @@ -853,10 +873,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_open_node_protected(kn); if (!on) { - mutex_unlock(&kernfs_open_file_mutex); + mutex_unlock(mutex); return; } =20 @@ -870,7 +890,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 Thu May 7 20:26:41 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 6D083C433EF for ; Thu, 19 May 2022 07:28:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234841AbiESH2O (ORCPT ); Thu, 19 May 2022 03:28:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235075AbiESH1b (ORCPT ); Thu, 19 May 2022 03:27:31 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63BAE87A2F for ; Thu, 19 May 2022 00:27:25 -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 24J62Y1Z027463; Thu, 19 May 2022 07:27: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=7dHR2zvKmrKYyFDljtZZJB03REXmU/C0GxUdKURGTHs=; b=h3+YirsokaNzHrqqYDlYE1WQ1M0ZwpntI8kM86Aoi9jtkqCy+WTKazAqW+3j26bZoH06 ixFwhOpNmcGRAhOG1NcltHEkOazqZ+WaSOtxr/M2mpBTj38Klv+KOa/1pFVN8GBxnKyO 8DcIQTL0qZIL+qg1cnzzIO1q/5rAyD05N87HBXeOvGBo55mE5S8vX/SHpT4s8DAGafep M6l8m2a6SIS/h9RlL86ae9xfnmHqe53b9N5uWWOOIqskhIFlKlnx5ua1gS6IwdHHxREh SJQnIK9JnYjbdNu3pZdLMlmTiOjm356KpiHR9B+OvPrwWH1zQVIw4LmOIkbPOjFzikjL 5g== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3g241sbcsc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 May 2022 07:27:20 +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 24J7G5VD005600; Thu, 19 May 2022 07:27:20 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2174.outbound.protection.outlook.com [104.47.58.174]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3g22v51aae-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 May 2022 07:27:19 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AEORVCaDrnf3nCVd2YgZlj7nZjgGYjmATqWb9ANWaFMWkUBZA/9dRjZxA80SjvuteD/ELCorNq8ZLFJ5RuSi9XR6oi1BFtE7Hxz9FGPOtru0Sw9NrVd9gqjij8XMD1WFNs/MaB5TK83N03CAu88VuNleg7VY7FiKfd8CsL/EHgsI4SE/Ca/eHRSDVNQV7kg39w5aS2acriPQD8g2FovoNU1vMh8BpRWV8unRZbiiFW/1tOEf3RhloH93UF8woQ9LKW2ZY1SWbtQMI+99iS71P7hIvHpYxxusTeapF8AGX0oGeVCzpAk0rOBDa/qTekgcOzAF53zIVKemwslOyB9HZg== 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=7dHR2zvKmrKYyFDljtZZJB03REXmU/C0GxUdKURGTHs=; b=LAv/Ih6tvoOpGzbZLB8qlNGvdti3/iJirGmDuDhJO6BVA1j/sfJXiso+SxQuTxXmyNFD/GOS7dOsN69kx1odc0mLOCaL+u8sY/9ZZTbcsWXdaSkTbt8LyIDIVAAmHBQNEvtcu3SM2qQ93R+5eEsVN7928LN9lnTX5W8DzMqqovWiKWGfag19Z741CppR7Qu6ODkb9e3XR7VpR0LnHNT4VBZzqBwJhaLmX5lQRE59G/JUlMV3vkdZwml+Lej0JhnLJfT5gYaInO82QcGdp3w15ARevMXzr9SOjLC74uaHOSF+/tF5pULHkU9Lrj9+mx6HU2oG4g/2pkdW7g8DdfiN6w== 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=7dHR2zvKmrKYyFDljtZZJB03REXmU/C0GxUdKURGTHs=; b=a9hlYIjI27lZfn4w/7bra5aBTyRrnhmqKgWxlemjx+2DeEJEnfAytmKTS29GiCo7CirQtGHwgY4beypRE4R2dU0VTc09XM9zoeKUjbJDId93s4NdMuPgiGXSW1UDbdZLCbnSbb5qc3BScpAjJwB6ex0Ypu1MegumhDhZswstBu4= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by DM5PR1001MB2154.namprd10.prod.outlook.com (2603:10b6:4:2e::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Thu, 19 May 2022 07:27:18 +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.5273.016; Thu, 19 May 2022 07:27:17 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH v4 4/4] kernfs: Replace global kernfs_open_file_mutex with hashed mutexes. Date: Thu, 19 May 2022 17:26:58 +1000 Message-Id: <20220519072658.736370-5-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220519072658.736370-1-imran.f.khan@oracle.com> References: <20220519072658.736370-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SYXPR01CA0079.ausprd01.prod.outlook.com (2603:10c6:0:2e::12) 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: babf5724-6ff3-4473-ae30-08da3969010c X-MS-TrafficTypeDiagnostic: DM5PR1001MB2154: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: Imt7c/v8OK1vdHJSZa4Ik0HR1WqFcataWtfO7gd88sxg/NYb5MDYap621QES6t7CGIZlIxm93T3CvI9Vhw3fi4h1+QY9AJrJrC9PNChO8eDYdveo/W2VNRCHY9NyITASw2d+VfPbcRHhfkZoS9MfcPE3xRUwSOqUTvo/EkIVjJXzZGDdfDiCLyeJCK1FWn8MLNoDqG3v8wRovNHgkQgrgriBq40fmbrFAn3Cj1vao8xLg51GYUyj4q4c6ZUOe9HvonPJ76/ePIBeFu45RUe8RxRHKA84nyu0MfLYgh7C4PBydEVz1IpzT/yFwxB6Rfq0+md+92SpaAu/5MgaS/7VB1dOfdfPWeIjARmgkH18FudrU++UdmJt1I4pw5Wy8DDzt9nYJN5Ln4PppoLgk/8m4M+LxrrwvsjzYhyn/s3lgzThH+sD7pMpZjEFDK4Ra3p0C/bWfJ5y3AMp2xFK5y+N2LW4Ux46pql3YN2cm42Pr/09aDSEs5PR04P4e1+Vfqn7prnGtsR40fB14aGv+XI3cBJBQ2Fp/8smrgEDo3MgZGS/y+Yy+o5JWNPJo42XOppxFIhuYZMsDr04Toq62wgnLh3IXMXu5sLvRgzG7aMj6kTHbruaIJCQe+ajXxBioWyPeUA9XfqeL0zt6vXnGWjySGAZ/UMKVjBNI/hdL+gPqqB4PgtQ631/X181Kg8QIIGzjeuL+j9101uHljy2TYMYzw== 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)(103116003)(8676002)(83380400001)(86362001)(186003)(66556008)(6666004)(38350700002)(4326008)(66946007)(36756003)(66476007)(38100700002)(1076003)(6486002)(8936002)(2616005)(508600001)(52116002)(2906002)(26005)(6512007)(6506007)(5660300002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?XPWKdW3QoILG14jxMK1Cer2HNTwOq7hkfIm4ERTEiHnrRicJX7LFAOS6n3PQ?= =?us-ascii?Q?D2Hki5S41sgDWsCjbgI1tljwRK41rRF69zFV04KloB3GF5JA00v1ebYz14UM?= =?us-ascii?Q?Sy0QTkBsWW0fY94yGypjgZaA60i3TXprwEq8Ta+ArjAyTHu6UFKC3ZZ4h4EI?= =?us-ascii?Q?1lTvbdpNkz8tqKIUszvIFpsUOJuOKeEJx4XnFLHjyQwztTI70nx3iIXANbdA?= =?us-ascii?Q?9NgUtA1MD5gaa3mE5gIoI3rM8qE6O0AXEVuTPg8MbGXrbNZUrcmhep9XDFyb?= =?us-ascii?Q?+pw+AFho3fs+C/PRR/7LsG+JcZn/HB3Fc9GC8pbhxyNFQ5DWFI11223eVlj6?= =?us-ascii?Q?FEw3Q4SA4+UnKVNGu7SwJrdWbCPz7Rw9aHDydY00Op/IGFpBgmg5AHqkvJA2?= =?us-ascii?Q?vIqkqZt2M/OUbsZ/kXvcbHfiu6EMO4X0TSjEnpUNUHyZHupyTnMXiWfNVba5?= =?us-ascii?Q?q2UPhin90VFiaBSp+6Ko2n3oXZ00c/QFMFN3QvNGgXBQQ1sPyxqTIeQLfxS/?= =?us-ascii?Q?J8Jn3tVVTDGqdJVSkrCE8u03AzsQsT6vQNnSZCjEmkMi8f9mXwngrMS/3c2K?= =?us-ascii?Q?g/3Yg+f9VEETcXfgSAiNI6yR7vx8B8NMP/SloZ1ExNrPxzX7s2Gb1CsK3SRa?= =?us-ascii?Q?fn0gWQerohCC1X0RgA2wSIErk50xaDIUNdvB3lq71/JOPSP5POtpek7oL8p4?= =?us-ascii?Q?cg8hjywgA1Mr5mG855BFPDQejnJK+3c548zwhd+YxYRHZixln19jDea2I6XP?= =?us-ascii?Q?qcsFrn4818Xnjh1tOwsdz9uIRNyAt2EzZKJfcU2d7SZ+bsF9Z6GAttRSNhlQ?= =?us-ascii?Q?nwTLtqPSf4OplTIB68fYFYbyMCDkTkt0RFufiYlPFXuWy1rIM2/oHZghd+lq?= =?us-ascii?Q?I5bNKzpL7Fy4dj0wWK/0VaFAUdU/BT8u8bFaoG9LROENmsjl2g2JLgbSKDer?= =?us-ascii?Q?j8vtwnX8t0WlymXhXTJeVxoksjJqse95VnXP3oM6FLUj5Ogrn75UBmkp94om?= =?us-ascii?Q?7onQSpxf1lu4GlEnMyFkrsWV8h5QLCX+kHdV66HpeNuZSkq7050pqaLhUisA?= =?us-ascii?Q?25k/B6Ajq/LLrtUPX1LYSCORav2oUR327HSr3mlHzetqET99g/blCpOWemik?= =?us-ascii?Q?Zsv6je1FGAhwsPhtLdtwtlSGw79t02cfz5sYSlwO336KjQL0gKponNRsV0T4?= =?us-ascii?Q?NTNh71aI0fRsJmimP6Ph33aQ4RVnv0EgYbwxPsJP7cGmD+mpc4hRizCs/hF0?= =?us-ascii?Q?6Y5R0cMIPYoIxwohsUxzXxjJefsNrLWjd8STIKErY9VhskClr+wHoTED9p72?= =?us-ascii?Q?W+IglXmXlTOiQIOWleLjYWJG7V1t+hZpUOHvdXT8x8edNXHy4f8I707VOAbM?= =?us-ascii?Q?NakgOapmYexTS6RYy+d2ugsgidahbCArqkVG+l/B5qqlDiFZW89CqZ1VlaGH?= =?us-ascii?Q?wsIiRijNHyzEDKgHM4lH8ZFKA9M2BXhWDNxRrHDH9TMxj3xlNkjCAfi23UyM?= =?us-ascii?Q?4x1R97H6Jbn7ribv661rCKJaK7Iuz2GaJx2qmDIvmZPLDHoqnrKRcLLyC8Ny?= =?us-ascii?Q?daa5hSI2jD0fgNZ7C58O7qtuDETECI2+iwurBi3q4JLnGg4Z4FmxENGk0SWU?= =?us-ascii?Q?eBrZNTdODKdShIDCNTaj2cYn7Mbx2zOGatOyU/xFaElb7AUjQrTBkzrPvN3B?= =?us-ascii?Q?animmQq0WnVQXQWp3FiRQ/yZluv5Ul0pAioKFJPQ2zvglHQyRwFn1sxeCrod?= =?us-ascii?Q?WeIXil7ugaqO2sBcm6/tltZPCEMDtEM=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: babf5724-6ff3-4473-ae30-08da3969010c X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2022 07:27:17.9240 (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: qrhShRR5Z/TO75TJgV5ImeIB9s5Sg9IMDl+zlIlqQ5CzDxGhO3OKNA9fT7asQ+jTLbWq/bjytvOVlFDkAo7A3Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1001MB2154 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.874 definitions=2022-05-19_01:2022-05-17,2022-05-19 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 mlxlogscore=999 spamscore=0 bulkscore=0 malwarescore=0 adultscore=0 suspectscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205190045 X-Proofpoint-GUID: cIGDHlSoPM0qMJoftmoudxH9qwK1-o1Q X-Proofpoint-ORIG-GUID: cIGDHlSoPM0qMJoftmoudxH9qwK1-o1Q Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In current kernfs design a single mutex, kernfs_open_file_mutex, protects the list of kernfs_open_file instances corresponding to a sysfs attribute. So even if different tasks are opening or closing different sysfs files they can contend on osq_lock of this mutex. The contention is more apparent in large scale systems with few hundred CPUs where most of the CPUs have running tasks that are opening, accessing or closing sysfs files at any point of time. Using hashed mutexes in place of a single global mutex, can significantly reduce contention around global mutex and hence can provide better scalability. Moreover as these hashed mutexes are not part of kernfs_node objects we will not see any singnificant change in memory utilization of kernfs based file systems like sysfs, cgroupfs etc. Modify interface introduced in previous patch to make use of hashed mutexes. Use kernfs_node address as hashing key. Signed-off-by: Imran Khan Acked-by: Tejun Heo --- fs/kernfs/file.c | 17 ++--------- fs/kernfs/kernfs-internal.h | 4 +++ fs/kernfs/mount.c | 19 +++++++++++++ include/linux/kernfs.h | 57 +++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 14 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 87ea8127c98c..2f7325352e43 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