From nobody Tue Apr 28 09:05:07 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 425FDC43334 for ; Thu, 2 Jun 2022 06:39:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231310AbiFBGjr (ORCPT ); Thu, 2 Jun 2022 02:39:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231285AbiFBGjf (ORCPT ); Thu, 2 Jun 2022 02:39:35 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D48929C128 for ; Wed, 1 Jun 2022 23:39:34 -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 2526Hx3k001846; Thu, 2 Jun 2022 06:39:26 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=UxIYfUMvr/J7q4ABiB5tYZRWOTo71XmD91Jlc8nERYI=; b=EYyIEXxQkBn+hMzblMOAcpSoF0QQeQFyQj/1MkxPc4KL4gJYBwlmipnvxL4O/wO9Wsvg PP3JdS/Knrt+Wk8kyO51WY13iqTND6Xw5srmCcPgAaIJFHHdW0BUqwMF79JGs9A3kNMe h2WrMhFwuW0IqtDYuDQqisWODTBKz4N7LLE4zNI31QVM58RYF7/fsSte40LlbX6WS8XC jo6Hd3/psFTFVvWzt56t1kR8m4X79i1KsqEGcpT2YwYnXbdUFfLvcltt5Z8hQIAaK/oS 3o8xIuCi9K2qL4aHbFEoRGdQqMyNbfNpBGFRhtls0rWVqA6bHJMkWBXnCHn+Wzroddo4 ng== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3gbcahskwv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 02 Jun 2022 06:39:25 +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 2526KAFM001123; Thu, 2 Jun 2022 06:39:25 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2101.outbound.protection.outlook.com [104.47.55.101]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3gc8p4krau-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 02 Jun 2022 06:39:25 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UjtqFf7zbjbST1ikhOBxN4jUTzl8GVX9wRonet4M71L7IRsQxKYCSjP+a41c9QlgfHwyWv5p1YXAJsZ42bR2SjW+n27n3zetOQIWHdxKYKzS7ucTzIqfIv1G42CDcShTDL9bYffD1yjbF063PIFRfR642Y+PZ972KZQfHOfhNft/X8JMrBdbQu6lswsHGHCM2trYkveFw27HNNI5nZPD88hMRjWcA1ze+z3R3FOdGdqNLPrJ27p9FBCjj101Z58d7rX5JwRUHyhlKxBmQYIEmrmv57C557U1f5DjEcLJzW5dVR+rVduLyl8NNe16GX5vaVxLYgzxu5OeE6N3uaGx8w== 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=UxIYfUMvr/J7q4ABiB5tYZRWOTo71XmD91Jlc8nERYI=; b=mpwDmImwvjrZCq+tUAFdFYkw+O8mBXruMY+OHdN8qeEOV/1rSIZsqYSGkaSHEBrvT/vwozGqFhNngb96o/U+D6GFIh6I6yVpaHLfZOVXsIChQIQiIOA8/uIu/AcU8RvUKDkpn/S+58A3pmTj+G4oM73FnZAbabWPE1pNhadL/v6ZYoMY49ARFUYwwsKSRtA3t6zT+kZPHYWCf9jRZbnDyMvymDMkuWRjfIRz2dZptmdZYzklMzSttAAf7gV/snlFajFI3C0XV+OCGD065HDyabpgGJpxGVR6n5iiYnJgEEaKz+XTyVWo974ORm6bzxG510hq9ms6qJ+BttnqIQ3c2A== 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=UxIYfUMvr/J7q4ABiB5tYZRWOTo71XmD91Jlc8nERYI=; b=ket1CPf81rK2Ne9V6SjD1KVB3P5lVFUizRBnhMc3t65Ow2KKTZ7ClwsTh8mJIi2YE1F/k7eFmoRfYGz545EHZ2wvmEg1KzkZnUePVYwYZD+9ojtdAKwQkVd9Q2q2sPWDrjWz8m/OwpdN1EuIthDQhlQdO/4cuCVV6kvSXgLW7Bw= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by PH0PR10MB5845.namprd10.prod.outlook.com (2603:10b6:510:14b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5314.13; Thu, 2 Jun 2022 06:39:23 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0%8]) with mapi id 15.20.5314.013; Thu, 2 Jun 2022 06:39:23 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: matthew.wilcox@oracle.com, konrad.wilk@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH RESEND v4 1/4] kernfs: make ->attr.open RCU protected. Date: Thu, 2 Jun 2022 16:39:04 +1000 Message-Id: <20220602063907.136569-2-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220602063907.136569-1-imran.f.khan@oracle.com> References: <20220602063907.136569-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0014.ausprd01.prod.outlook.com (2603:10c6:10:e8::19) 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: 8d745bc9-929b-4789-39a6-08da4462a158 X-MS-TrafficTypeDiagnostic: PH0PR10MB5845: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: tZLSUV/NW9S+r8NQAbP1BryjHT77VuBpSc4rhuegSUk5MxP1QjfALao1SFtMibXYnllHG+XeJAFG6p0+dhe2DYrQUUMY5abQPFzHyiHiokHgZoUC6egpP948LcqtCUiBVtoOH1yj2oTDO2gQ6lkBCjYyGJiZGnsV2g0mJO0kXl/bvIa35SytBQXRQv/qyUVOk9JFOYh2wpBQNRxtbtdNJvATAFogeMfBkdGlHGCA7O4ag/PIdHJEm2uBbr5TIAqRuxBvFOvjR+6VstW9vmmtn8JTvS4tt0Fjv1ZdjbuOVcdy5QiHVzrhCdOnoo/JUnGmlE3pUzTA9FxLq1nSKLjCJiHbenBWtaYWTFlFk3Chr/w0CfrV3h5bn5QxkRzZbzn3baTV3509vjbHQrtDltDv/s4C5rLKwGLCZOuJUTRTom5glDYbeyUnMbmoaL+ETcp6TPRYUuOA+pfXCoWGSYJuyD0wrtwhwSzWxmVhIRejV615VF6VlVfZK8earYfvgW1Rd6fc43L75ZHm34wZUJx8jylAxcyPgh+ZzXEw2hS9uKZRRzMprif/IgOgDP4qFRfyDRYe4yUYjfChzLuCbuAR6SZ/qoaDGGaq+6f2vDC7g63tXP9Kih+3vGGdGdOBu24LPnyd7MOs23alRQSpKJETIn5KZHKcZTT1OOb/sHAnx3Ppy504eeJPMVevStkZasaeoIKklYXNVS945BsE/9tlSg== 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)(186003)(83380400001)(38100700002)(1076003)(2616005)(5660300002)(103116003)(36756003)(8936002)(2906002)(4326008)(66946007)(86362001)(6486002)(66476007)(8676002)(6506007)(6666004)(6512007)(66556008)(52116002)(26005)(38350700002)(316002)(508600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?M/i6g7+j2QheIor6aHW5BQJws7guGTd0nEbiUHnvm1rU6zen8V2uZrPshutR?= =?us-ascii?Q?+MvrqR2nuD2AdpGvwufDBDlDGt648Wpfnbp4OZcxNd99PJH/mBjrLcDlQq3t?= =?us-ascii?Q?oxMji6HB7NkdQHybQm8gh4zj9W5mWNrlc+JAWZPtlJKAczCK2pOGRIWOYhr1?= =?us-ascii?Q?vpUBSkopqaAU6aLMnIdS5qB24Z0vIQ/kRRCAoHRfsIh4cbUfJ7g92cAmnJYg?= =?us-ascii?Q?YRfE2fYyB1zcwDRf3BdHl/xcYga8oZieOupMF2xgVTGwMGrI2z9nKG/7yDia?= =?us-ascii?Q?IqDPuCarSQWmr9vp2OhDEBpFBKBDkd126+VvrMSl8UvnbmKCCoq2K71XXNp1?= =?us-ascii?Q?3jUnGtmgy64BUrlJIkVsu/+bvJKNfOEiF+C3F9/+Eakvzpk9A4/s1uJW1xJ2?= =?us-ascii?Q?hBHISdtciiF+PFgI8UZ1u5vbHi0/nluv/T0J8xHwKer3f1KDqElpDFPS/JZA?= =?us-ascii?Q?bPHAtLGoQ9TlUY76/PJWr5r7m41ZTsh4MsQIxtEjbJFKnIzlt+6m0pIuee5p?= =?us-ascii?Q?d4p4Pxg20z2wVYyhIR/dlD9chrv2EDrPzZnZguOJkMpCwOh1KTzRMckH7Iua?= =?us-ascii?Q?kSh74Y6ajCVwruWqmjiLYH6E05jtj87XjMclhvK9Pe+6w26a3XavBrM9AhhL?= =?us-ascii?Q?sDNU9NZEzqXZO7RsMtYtI0TrnLDW/f18HY8uhG7Aeon/GTpheosusdocMBnj?= =?us-ascii?Q?lHLy4VPzghdTMRazRA+cuFcycAYyLVPmYzoLBBBFUJ90ni9p9cxS28MEIKb8?= =?us-ascii?Q?SU+7Xyi4lJwURdqfQRoFMwfFiCzFiiCg1E38LHp7IbMiYKfloOBL1BTr32o8?= =?us-ascii?Q?A0bIZaHGfVCUZtl3eNn5oitDKWAmJkNqo+Ai4JjJbkYlVcjIkcrZZqS+6FKh?= =?us-ascii?Q?fCOZy3BOWaznZK/yimfrOzsMgDcyNl+AmESlP21Fr7CUIeMsCSlBM9g1zmzt?= =?us-ascii?Q?bx2UVOmF9MfXCY4BPlN7L8kG6MSPSbTkjkpaZoDBHTZI1Sy4L6Au0pcwnd87?= =?us-ascii?Q?+xJOYuSg/RLe9PO1h/k6niDsRsjdgN+xekcDap8EtYITaBZu1FpyO0jH684Y?= =?us-ascii?Q?nBaMiwH2P5pZHgQRi6IqUl+pKdbenYb1H8si5OEUqGuUvMtaN4ivBSzm7Vei?= =?us-ascii?Q?41TTZDqjTNjnsaf2T9RuIeRIkr+8HxNwaZmSD1fAZnzhsJ6p+/YMLGLvN3yK?= =?us-ascii?Q?cYUtX27nilhBPZuBog9cUdhU+ZD6sHFGdL+6u4Hw2p5/39s7gjemyihqqEuA?= =?us-ascii?Q?SNfTacT/BZwb3v7Zmi91eWCNyrlPHoA0ECvl9dsL7nj48gdHfWaHEO7CXqC6?= =?us-ascii?Q?L4aov3pB8QYgTobBF6dgxeSfxHQ0bCLWzGpwkP8Epzm4d2HKi2cZkdQamSEp?= =?us-ascii?Q?J1igoj7i+RvDs0CC9hKD9xABzwUeP9gZWi8qcGBWL1Yy7RdpaMSkxO80AbKs?= =?us-ascii?Q?4EuQRmaGgqtUcN4y0yedaguVnuHi0SBoK3SO9Wk3aGde83+CLthycB4WsdYO?= =?us-ascii?Q?zD8mP0IBw29vZv1Tvo5Biz6SEvbfTHAU4wbmAOo1iObnDHP75CFt8WeOBnGB?= =?us-ascii?Q?SlLT6xX5janTiiTcCgpMEWxpeU2PT8ku0iWl1h9IAlJHB9VPor9NKZ6+gNra?= =?us-ascii?Q?8vfgocvML/jmfkEBpGO/YlSmZlCEKJCPR6vDBgUVqddQXgs8OOwFOKT/C8oX?= =?us-ascii?Q?CBQTPdSvqXhgt6NDwmgID+J5sb0sgPPmLJvQlf3hzsWA5X6xvwxuKRXvHkMF?= =?us-ascii?Q?YlbFZJ2XDUXaSnuT1UFUbwy9OGZPrw4=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8d745bc9-929b-4789-39a6-08da4462a158 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2022 06:39:23.2629 (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: iUV7yx+bE/XTawP/IL8JqB/NhGN9eJ2I3FI07yvri0MnF7ZZkHudlSayOEJe1Tn6G1ndcO79o4xdGpgqtyDsPw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5845 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.874 definitions=2022-06-02_01:2022-06-01,2022-06-02 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 phishscore=0 suspectscore=0 malwarescore=0 mlxscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206020024 X-Proofpoint-ORIG-GUID: OddiBEL0kSJZqGgKsB5Cpk6l7cxXIPwT X-Proofpoint-GUID: OddiBEL0kSJZqGgKsB5Cpk6l7cxXIPwT 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..63f1a8f3efb6 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 Tue Apr 28 09:05:07 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 C3AB1C433EF for ; Thu, 2 Jun 2022 06:39:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231297AbiFBGjn (ORCPT ); Thu, 2 Jun 2022 02:39:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231284AbiFBGjf (ORCPT ); Thu, 2 Jun 2022 02:39:35 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D4CD2A1412 for ; Wed, 1 Jun 2022 23:39:34 -0700 (PDT) Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2526Nmiv001753; Thu, 2 Jun 2022 06:39:28 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=bl60fQmGMwt5ZLhB73rldYocSX3/75s4t4xJYNn5IVo=; b=iv/VmSMvtxNgnNuIXkpIWxg2lKuIDK0UarT3MOrktUJPYREPNv7y4XJj9adMnHTNpLda kFxB48Dd0FBghETnpQTGNuiZtga/tAlKsvqpV7b/9t0aoADKJZeVi7OUhbLLekeCQhq+ pTvwaR9IYB+IX3umzrUMDkFNR3fG1906l8PvrKvfSn8DWdUexKt9+ryChF3ikq2UMIC6 IfkJBqcDBJXoQ6GemHKnqVIyDQt2bVwkYcwNCa6Mwg1FUPK9A3k6bP40kpqUsnNPs7g2 DsCDYBNrwOe9U2qHbyPAlUuCfgv9U4i/EeHMkxJMD5hj/ti+IzlkjpiIkLW9mB+jJ66Z Uw== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3gbcausnr2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 02 Jun 2022 06:39:28 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 2526M5ZF039572; Thu, 2 Jun 2022 06:39:28 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2105.outbound.protection.outlook.com [104.47.55.105]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3gc8hutb75-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 02 Jun 2022 06:39:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VvWZr6XJP/dw4ELZZBhkTUcYk5YZvkYSioy7kksGoQpVgEsYlfQr9TtIbx5WuDlcSI+pP+NidT6H2s+Kchq0+I2tq4/Jn1QCGF3EgFJCG665wlaW5xMBwTOk8yRrU3AG6aNflozLt2s8yx/6Jd8p1koUBTTpxDPMcM08VRBUwFDDyWa59GIop8u7oCSBQuZI4JYJefJm8RmoLhE/MzFLt0JGQD+9jBeYzAtYJ7qhEktjqCi+esEmHACssAfDZS/URwBAKiGoAG8G0FTC2dBWBAYWHw3sJHvr1GNCTa64tntzFfvx/0+COW6vKvRGyK5g7aScK1Bfk6RgEvM5iw6sSQ== 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=bl60fQmGMwt5ZLhB73rldYocSX3/75s4t4xJYNn5IVo=; b=DaP7AX+LvuNna1WgAn+Fb8iiGnNwv4qq+pq5VGY/+g3sEUqWK4UhGvjuKmz3xL0QBfGIIbHHeietF6SYJ9L3fU7fk8JRMtc0CQH/XWZhUeOHzr+jCBzpQGDvNeQuxxmhj3Ap8is4EkQb20n7LoyZe5xTay0oQSAOiWSsJUOaCifQKO/U5y0OsGIF9jmID/PHGxt7QAvUrV+7JSqPBXO5e5FVw0IQ3Gqf2YticaqVr9/P2rKN5yXKE4vDx0IrTYMza5bCblckstCneejlHTi+S3RJa6H3IsOK9sKbrdd9OnsPkILn5D9UNgju8ZcOb9snhSWFd3ALx4Hhd0R5iWBTLQ== 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=bl60fQmGMwt5ZLhB73rldYocSX3/75s4t4xJYNn5IVo=; b=AZEV31hYNvppcKagqE+5SWJb1B/TgvfKRe5NLxNtE4b/4Om58aq0lI4Yf2frxllQnl7QlFTw2pV3D28pZj5dFb89a/MY+rqEvp9oNcyB9UMw4txml4escvmWA/fTEjSRXn8ZRE23G6BrLH5vUNCbPnx9bg4FYDdq418rXL3SEYg= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by PH0PR10MB5845.namprd10.prod.outlook.com (2603:10b6:510:14b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5314.13; Thu, 2 Jun 2022 06:39:25 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0%8]) with mapi id 15.20.5314.013; Thu, 2 Jun 2022 06:39:25 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: matthew.wilcox@oracle.com, konrad.wilk@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH RESEND v4 2/4] kernfs: Change kernfs_notify_list to llist. Date: Thu, 2 Jun 2022 16:39:05 +1000 Message-Id: <20220602063907.136569-3-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220602063907.136569-1-imran.f.khan@oracle.com> References: <20220602063907.136569-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0014.ausprd01.prod.outlook.com (2603:10c6:10:e8::19) 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: 975cfaab-bb2e-427e-3d7e-08da4462a2c0 X-MS-TrafficTypeDiagnostic: PH0PR10MB5845: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: DPP0MgusXJCfrqtfQrIl8Gn3gL7Px+MajRO4rxlgrr0CTH9KVHRYv3pKPM68bXWVC4lQn+oHjBhvl43kNCpx6bNDYUmTdQWZnVuwNsPoCgIVbPmylwHO/iLdt4C+d6iYUWL+6+LAb8yA/XbUchLBjEXLjp5P7PQQAZS8CDMNvwdqGjFkQ/1MOlFLnjrPymFs37WRBn/HtjIkK99dv/0dBzEQu5i59KKu0ZjyRrXT0t1WGOuOKfxAGhCTT7hu9QdE2SDzL7rOyd4tT1XevUj6yAfekkZff8Xjhh8KpLbSlXHOuilBQc5KhJSIWOLtNL4o68bsq5F5gt4pJCkz0FKhHC9cdF3ng4JDAA5Dol+WnC9uxyB83qDSXOcYwzZL4e8RtejgFNu3JRDzMLSbbEIUpNphWpQ+45ZiUyMf4SXsd0tp12ComVkwPk9rL1FXnWDJfikaOzwmKPTq6KhBq0nKTU1CqDlXIucl4zIYWMuWqlZ3YmAEgebisEvqOi6QQdelPZNPXCtazLjL9hBlVVxi8MU0EAREtHdGN6+K9nQLfPBsvuzxbS7W5ze+aZ5ivXIf9Z8E8tULnJWs19IMX2QsGAetcqHvOYWVUpEALRnMIcP6xLvmJ48Iki+g9X1h9IAwH+q0oh87saM4rm1JYm2KOPkl6q74ilYtyFBwmbOF4Qk1Rz7sLSaFKf7I1Q0ra/lvw74UGpDnQOEORcscQBlWTA== 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)(186003)(83380400001)(38100700002)(1076003)(2616005)(5660300002)(103116003)(36756003)(8936002)(2906002)(4326008)(66946007)(86362001)(6486002)(66476007)(8676002)(6506007)(6666004)(6512007)(66556008)(52116002)(26005)(38350700002)(316002)(508600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?7wHkL/XWqs8BDNql3F40cwrsIjpEx3RTrs1GT+4glF1c6/c1xaXAZ+o64kx/?= =?us-ascii?Q?I+XYF11MX92ceyQ0M2zbgSnBf0uyzRFQ6slseTc7mIqeM7VdcCVxLXQA69p4?= =?us-ascii?Q?qaFONBsmF3nUadMfszNh0FJgZQMQ7slP8azbh3ZtVK6TAF+xUpZ5z9BPxXHe?= =?us-ascii?Q?Kre9nG/zEZn8Uzpg48qnAV47qrVrdQAcfwKpe23wYENcbCaOWjWR5uFu1zDP?= =?us-ascii?Q?zzqFhvJWZYwd7qeVTeLa31ly97jz+CsB9/2qesLcO1c6UYL7khUakJpS80md?= =?us-ascii?Q?VQae6IQ/ihETwh8wv0LKDah2CduJgdEx0GDS1p/xbyvbffLnqXQ6RCYG2wGo?= =?us-ascii?Q?FknMgZRbEMF2rvbJRzkdkGBN3zgBmTTi7EmicDSJKDwMTV7gn5gcUPorEkJu?= =?us-ascii?Q?XMyfsph7yzz29aqoPqbNSD2E+fBU1JodObAQhevQBQPxoq0TCSWg2RipB3z+?= =?us-ascii?Q?AYDLqNE84INS0DgDbL+Etpi63pq1nBdm+XUKzlaN4/S/5g5FED1Ae8WFmJOh?= =?us-ascii?Q?G2w/2ZOPuztsRrQziKkpPR9BhlF43FI1oggv6/4fd3zBsfwvPqiS+lB/5j2s?= =?us-ascii?Q?rFa4nwl9Kl344dDSARQISbcZglD6QviO+GnG9Qr1/fu7FaydKtfLkvi9LZ6k?= =?us-ascii?Q?Gu2u7b50vBUzQUH4ggLzpBN8wrQAXuxeQ3urTOzTPv5Jk8wUOu3/xIDuDRXB?= =?us-ascii?Q?XLZnilK6Qm9Uhn2t29D3sKHq7Zs0lWkP2fZ4kBY0L98kZBUiPH9Ka+YKW5tD?= =?us-ascii?Q?qZT8NcexhjQS85kLU72I0EsFNnuWHAmfwQHjLdaATGpOLfEwxQSZXaLk4tbz?= =?us-ascii?Q?WSJ0SZ1rGepzXOzVs5VAWF5gb5ORTM/tLMG+abLt4rjnNSkTq9aMBAY6EHPy?= =?us-ascii?Q?NiwAd5T2XGYF98/HKqJO80aZokMbccti+unhFHp8KwdvjvPX69c/TDl6i7j/?= =?us-ascii?Q?TqSU1hr/vdJDlhUTDyiQ5KQAfOMckIO5IUndyW28G/wvsB2UEzdNhA20gs93?= =?us-ascii?Q?7zR8hUsM51RpmSl1Ij+5liFP+2w6zqlubYweYH39UP58tcBtfECG6E1wFg3d?= =?us-ascii?Q?bcn2QSUUPqSGraOsYvYE8BvhU16MoqGRXfVl3E3eItpYbnK2LlzEG848QQnq?= =?us-ascii?Q?lU37jwWcumbJzmjv2QKzulYQYjvlazoQZ/+hAayLSJgeGrVEURgAPF7TCD/x?= =?us-ascii?Q?QBngQ99+7XIww0XQmmUdFuCiPMbHfkUUXBWwZmdJx1dSNwQFaVejrQ40/n8e?= =?us-ascii?Q?XID+plhm1lf6drojjkMAMclt7QPE4ApMFapZ6EiZCalFbh5W5bQDNHlNqrnc?= =?us-ascii?Q?EaG67y1WpoTL5voDsv3pZDOqJmxM0V7SR/MSbrhnQFRJuDkan1K09ZoozdZx?= =?us-ascii?Q?pt8knzEcRsxARY6UydMvMGNQ8J9Qf6yq0ZEm/EsXllmWSyn1ZKmiYYgMGntS?= =?us-ascii?Q?d3/RjC89GdaH/yIFJ6K6WnnOVG0lVqTpQOkJXgXALIHP4YPU6nWs+xXiTMLX?= =?us-ascii?Q?QlDdLWOMPJW2tYPBw2muJ/cER+dnLea5N51/2dUOeQpMwSepIdpWQiTFAHgZ?= =?us-ascii?Q?KUFw4rv8923milcTr9PPgyy//UJXyD7UuQO0oYQgjKCYYtV58L1twVsu+jhg?= =?us-ascii?Q?n3Rp3gvRh9HOyan0XvLJ+ot8C7XjTjdP0mz3XCDc6EAPywTP+VVbLWcl4Xzd?= =?us-ascii?Q?5Gog7qizkOftf6zzYMVMw7h+TZB8sLss7e+Rry0ZV5MjZ5xBE/In2DjaFY4Q?= =?us-ascii?Q?ik8Mt9CZTk1E9fP8cv2Ag9dCWk/IOyY=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 975cfaab-bb2e-427e-3d7e-08da4462a2c0 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2022 06:39:25.6382 (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: 6Tfjb0aGVphkKsxuW2dxlq/H67d7Qn9U7Bi0q3TPb3vrbc8kVlv4XsqZo1V8ZAm+T5+9oMaJVY+h6WgrmTzb/Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5845 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.874 definitions=2022-06-02_01:2022-06-01,2022-06-02 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 bulkscore=0 mlxlogscore=999 malwarescore=0 mlxscore=0 spamscore=0 phishscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206020024 X-Proofpoint-ORIG-GUID: g7ncAuIb5h74LUzEv8SsMQiguc4qsphs X-Proofpoint-GUID: g7ncAuIb5h74LUzEv8SsMQiguc4qsphs 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 63f1a8f3efb6..b21c8e3b6a8d 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 Tue Apr 28 09:05:07 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 39456C43334 for ; Thu, 2 Jun 2022 06:40:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231328AbiFBGjx (ORCPT ); Thu, 2 Jun 2022 02:39:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230487AbiFBGjh (ORCPT ); Thu, 2 Jun 2022 02:39:37 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B2A82A1427 for ; Wed, 1 Jun 2022 23:39:35 -0700 (PDT) Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2526RFwp021216; Thu, 2 Jun 2022 06:39:31 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=m1iSjamDqyqxMu9SXCCs8oNabaTWPDZfM9wumRhiAB0=; b=DygSoAi6QkgeVkbnY3+Eh+W37C+v/8pRPLrvztILvOcnalh0q+Nq7TnnMo4w8BCjuXzQ +a0COknP5kAP+cqnwDbN08/VO2jZ/vvrEITvkWXfd8qSy9eJB4h+XtGbPOTVX3KxxH4I +t3zqbYZ4UPJ5MP5OSRDU8+jAho8mVlR0WYH5jGjGLneXyG8Ik3BiyM7JFI9RI7TupF1 OH+8gqGJSKS2Dj2yPW1+FMB/NCV+n16yjoDuj+sQDuK0CfwjPkkNAWeeO0MsFb6or/uG Ci7Yc8lAvwl88y4AUyUjgdLV9U3i9Ffqu5e0SCWFNoRuPj8VASbJ30gVLbAGmVp1vqR8 qg== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3gbgwm96f6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 02 Jun 2022 06:39:31 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 2526M0MQ039391; Thu, 2 Jun 2022 06:39:30 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2102.outbound.protection.outlook.com [104.47.55.102]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3gc8hutb83-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 02 Jun 2022 06:39:29 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LAwhbzZTVtZeRA4YzzO1soErUwEgwSI+q7s5ndHJKaXa+793T3sN/yNMFakeEYJoXeOQ4ZlYq9WJqE7Dyo7IIF8sKFxyr2PCKvYpnjUQA1XyKBYN+kUZ1/5T1+tnV6FlG9yMiePil9LxfiHfj6SnlhKVqH6wnJYGlYyG09PVR3DF2aQu8JYEQpM4qkbaPRfr3ACYR/+6v3Oi6fcxzOduofSnp53ZxCVq6PNKArddXYWhlsVwwf7xDt9UgTxTeMbp+7Xb0lMS55fC7zwTq3bNZCePA6wbmj+o+uLlZ8YfpGCxoH4MaTTR+pxteJGt2aHvxMlgp1F9ZxzE4ArC5MNSOg== 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=m1iSjamDqyqxMu9SXCCs8oNabaTWPDZfM9wumRhiAB0=; b=eBTH1yk9JWhAXHfU3ZjDed+lnpRHS3CWhjIh0WCBjEvFFVMAgcAr4CR3TXy19Z54bBZ97yxTGavs0kW6pjJ/sRiZQOYs0Nqyhvon4rx0P8De+M3+ITNVIrs/yHWZLvu8tbmELYdHHKMjOf1ZFryFdFN4lSeEfvbE1dllaxsxtSk6530GnG0pLG4mId59tbW9tzDCIwEXrLVqDLwg+QbsBmB2QkRZhlbhIDe04zjPCntBGXMVPqbxdNtwcQ7CGzBAnCxo2rL1cz7AlnEDHe7YdHYjHnL3DacyD10QHljnzM8iEzPNiE5CHIv/TxwIM/p07mL32aWKU+0Pc6UMT2pvtA== 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=m1iSjamDqyqxMu9SXCCs8oNabaTWPDZfM9wumRhiAB0=; b=h13zBAcmJexGLLOqgT5TjPyo3e4nME2iUxsaeh15yt4Hw0KstfHr+11O2wmfJ46S5EWn+5yzzh/1yOhyYtGxoJFQu28cMv6dNWFzLhFfDVysgux37/TYF6BOSCfayEZrQpvqkCG4LugLydzaaRxrUXkRLGWt14U71oal8B6ZO6I= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by PH0PR10MB5845.namprd10.prod.outlook.com (2603:10b6:510:14b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5314.13; Thu, 2 Jun 2022 06:39:28 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0%8]) with mapi id 15.20.5314.013; Thu, 2 Jun 2022 06:39:28 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: matthew.wilcox@oracle.com, konrad.wilk@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH RESEND v4 3/4] kernfs: Introduce interface to access global kernfs_open_file_mutex. Date: Thu, 2 Jun 2022 16:39:06 +1000 Message-Id: <20220602063907.136569-4-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220602063907.136569-1-imran.f.khan@oracle.com> References: <20220602063907.136569-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0014.ausprd01.prod.outlook.com (2603:10c6:10:e8::19) 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: 972b2b8f-ff2b-4307-e39d-08da4462a417 X-MS-TrafficTypeDiagnostic: PH0PR10MB5845: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: vMT7edIbYen4FecYs4XjbRwOdr//85hPQ9G3xKbYPZymT6dm6QHApTxpFkMFjCvjKZjSgBwu/w2WHIUPR19JOSTTLti96Vy9x7tw9NMc+8SH1UJJHFK6OmwtsvHTvVNwjBwz0/+vrCOjxaDkeF3POY5FpSaBcMQx/tmuMsi+godp+oh40S8XXefb+GM5lPdjnBtVkNW7f1nDh+XEBCa6/YgOREua/sYyT6MHji19fv62eO2i1GtflhYoeJRTEsOtdyFWeDyPEbtveOQm/YkAMQfCUOSmT0z5XZmp0RXyzelBaO2mVsFehmd3aoDa9fm5R8t/ueFg94Nc0NZPeTbDq0Wpn3DhgJhAyLR2rCew8ygVoJRCQHti4FyauFwTyNUXOTInn6CboBmN5a9s+m/PBJNALOu3Bp3nDEVBGAU9LQzCZ8lkI1X3n4hDZeDbpavdQKsP4a6YQU9XIIaTMlFP2MjbDqxRGIJXvzpNFzi6uIB6ggaNT2Jbo79FiOcYU+INd6X1zek5XrdBRog+V62duREfgNUyN+mSV9tzQiX5OvMhVjFqJpkUL65gmzQYhOvMiPZTdh7Op5iEdICdWHSyzrgzuEQt/aLgYCc1lIQ/N6UHhWHsBZQkmFpb7LHB/156pECMh/d4sB5ZWPeDAfqsa1ai5D/Hw1rgBpjkqrvfLLh/yve6liGaSTSC68/RI06jafYlU+ZRIr/iSsPOYKRSOQ== 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)(186003)(83380400001)(38100700002)(1076003)(2616005)(5660300002)(103116003)(36756003)(8936002)(2906002)(4326008)(66946007)(86362001)(6486002)(66476007)(8676002)(6506007)(6666004)(6512007)(66556008)(52116002)(26005)(38350700002)(316002)(508600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?4m8WFYF/WqrZKmxsUDkToLqV4x2snpiy9RPn7nCuIJIysIp8Q1bLkeBZyJOW?= =?us-ascii?Q?zcLGKBD2cWXX55jMUA+YMCUehVS4wHvfmokQ7lI5wcOCHFxMypKtF5t5Xpef?= =?us-ascii?Q?HOJqi+SEQw4Oiy/H92zeWykqaTT5x7Mt1gRtd9VZd5bA30nQ02+XSWyoNH8N?= =?us-ascii?Q?sB+ckwKD5ylbh9hW0AA3hniVu8wISlLHdxLoFSo+XjZqPKiV0+bnFBhsMdnS?= =?us-ascii?Q?2RnGFAGgNrr11WzOmjCvpWQp05Vh9+UeRCqDmbWo3vB338Vf6qumAyrmq6ui?= =?us-ascii?Q?KDMPIaH9OCPUM+HTIqsxTjX1Rnatk+iQV1HAeh/bb0Ia9e2kOM46iNcMpOk8?= =?us-ascii?Q?eZATCmojjul4jMKna2fkCr3xcBfty0XVCMZfqLUR9GXVLvozb4lODVRDB3f5?= =?us-ascii?Q?IJu7GIC9aQiAWxQjcVuxI36m53AmNBizNzze0A3Z/ChVvYCwTBwNvCxXaH/H?= =?us-ascii?Q?TMvKCG6NamLHSKHgzEN0MHxlfqG/tWLFboBjtDQhywlNqvmsHGGlKJwLduII?= =?us-ascii?Q?fpyEs8qfGDHwRxIUmeYt7PcfBUPp0BQ4mHVmL0O5uzPP11o1MFSV/hFwvR9W?= =?us-ascii?Q?FlkE6lA4Y7BpSc0+2p7e/sXDJCO2sPCt38aUHjB2KRcoe8j0mUWISnfK59ED?= =?us-ascii?Q?cu/U0/eesFGhbLynSW+OaSOF0OP+IdpZLjgllnAhwH9/TVnFdubamkZtcMo7?= =?us-ascii?Q?1i/jfsjcg67UYMvJNe2DStCp9WDL4J1MnxwTDH2R6z89UYoasvT99kpCTPjT?= =?us-ascii?Q?zTnFhqhB2u90Gw7Z4Jhu9D1p50lrtpAqBDHFmvJawN+SOn9ZOr2E46fxLFf7?= =?us-ascii?Q?krpjhqlbpO98DH5jPeEOS2QLSahag/gGb/lmm0Lcd6O8f3im8013cjkjMez+?= =?us-ascii?Q?fAkHRWwu/kGEGkXaklWUqpGpTMS0m3Y68mi60StepF/DAdk6AHohkBYhsTve?= =?us-ascii?Q?VuK2yxXQCCjLr9nqSvwPcdfxgmNndo/CSMZdk0CII710AMQk6PlT0BisxhLF?= =?us-ascii?Q?As78aa7kmFtF4NOT2ukoUtaov30P0Whp0DSoZbT6s6gMm/FUKKcU/nGHnCNT?= =?us-ascii?Q?hETJnFN6IICdSwPh6k+TWPZF5xIDYQGc2qOz/iQseLzA0QQEmL3bF6xLfRDJ?= =?us-ascii?Q?0Ur4MtxMmEMN7c0rsaV/7algjCxVgXibFwfXgXw/hs+JUU6XhIdsRe13Kvrr?= =?us-ascii?Q?PF/yx4aL3jki4VXH359FSEiU311MbdyzAEoQquMEQgMtOHWBpx5tDgMzhEfM?= =?us-ascii?Q?I167NYbpFtPpIq1Atx+A69VU+U2vprWx14Y9HLme8S/vzTLlGnNPFEtuUTaG?= =?us-ascii?Q?b1igjqScuyhXK1RaaN96hagPBJpT+tx3kjfYxnil5UotUPKhwXtb/FuWaF/R?= =?us-ascii?Q?82riBvqrH4DjMiJAuGeqXHdaDOjufJbzKwld2rxbsJQwli3lLgQVM48Oli48?= =?us-ascii?Q?k+exP0G1pvu0bdYOdwZ5sjp8zZBYgGgs2h3A8R1UJ/D1N6FRO4aT5gSFqcNv?= =?us-ascii?Q?GtfYI8/18nFCrj0TTZHYpolCUXYeoahSCrwNzFLS8JQHV1/FaLMfSIVRz3D7?= =?us-ascii?Q?X4GQfF/iQaREkfwSEddiwQyqpBX/K/3rRETB0HupwFjtcrP1+3yTRKEMXUMz?= =?us-ascii?Q?5teuQpDRQsx4uYp3w9DAytpNRK7N2CwA+TkDT6VEkZqODmYZQE8EJvUPCdHo?= =?us-ascii?Q?XebVswRN31jHUn6vI4ZGkI1HY6/WQL6re9uzikfPGot45rEx29TO77DbhM/9?= =?us-ascii?Q?MK1FAgAnwvQqI74GQF23IrwkZSm21q4=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 972b2b8f-ff2b-4307-e39d-08da4462a417 X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2022 06:39:28.0608 (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: zvHAqTW+VTUvGCfCbNdYXhxRptb3+DVfaBBLDvezBwUf4w2tfr0vVLNB9w3MA9eZ5nn82mpsXRB+0yUKBfiH0w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5845 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.874 definitions=2022-06-02_01:2022-06-01,2022-06-02 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 bulkscore=0 mlxlogscore=992 malwarescore=0 mlxscore=0 spamscore=0 phishscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206020024 X-Proofpoint-GUID: 7LZKAyl96yGcGiI2LfoaPU1crghaBqAR X-Proofpoint-ORIG-GUID: 7LZKAyl96yGcGiI2LfoaPU1crghaBqAR 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 b21c8e3b6a8d..d35d01d30fa0 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 Tue Apr 28 09:05:07 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 89FD5C433EF for ; Thu, 2 Jun 2022 06:40:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231360AbiFBGjz (ORCPT ); Thu, 2 Jun 2022 02:39:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230421AbiFBGjj (ORCPT ); Thu, 2 Jun 2022 02:39:39 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 009672A142D for ; Wed, 1 Jun 2022 23:39:37 -0700 (PDT) Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2526Pv3h021233; Thu, 2 Jun 2022 06:39:35 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=uOb/SSpfVdSHz1Z+PNlXCOer19/Qt50UtIJ4Zu2ET/M=; b=luKZBJIXxuKqMqcBLpm+1YiOjOhKzn9oWzu3heYMWhdNcnfs4vGJzznezhd+P/FUT+Mu fP1BMWP8uhYs9XsSf5KfnhEL9q/lrzCWB04KkG2kBwZ+t+15cuNysgxzkVDRu2AINwdB wyPqB2KEwvgwcdrNAkU1T+vRWQPnu7E4toG8lUFdkelakECejEnTdv93yIwoWohy856/ 6izcn5o6yVyd61oo8CHwY0v96SP2Bggz3FMFRkq715C+bj5KvZk/5eXtbUq41n+3WM0u ndmGN3F0+lcBbz3cmz3QYH38jE3FB1Fuq0/nSu2VhX0igZwRQYrphiETXgqi/AmAQfp7 uA== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3gbgwm96fa-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 02 Jun 2022 06:39:34 +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 2526KpWs005235; Thu, 2 Jun 2022 06:39:33 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2173.outbound.protection.outlook.com [104.47.73.173]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3gc8kmccxg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 02 Jun 2022 06:39:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ne1a7fz4UUL3ZBBhT8bZe0N8bn9hACbWHwxujYaUGSkTqBOQypN+RxqWgueMXmF6IiS4A6tQn2CCdJO82dq7manoiD9wA+vsTpiEVPz2xAmCeiau5BXrMSYB563guukKM6MNtP+jYBphm+jgEuMKnClGJEFcfHDJti9chuNUUbycgKInSsUBxdIxoo6EemS1faHibq9+6Z+dq671A1yUaBUwYSX1L353GtEAgghjXonaDW0ZznBoH8JO+GRE+ZvbpD9k6rD2omRqSfnwrgCiCjCnkTuKwBWIx+9ftfQZ73gbuX8baUQUEhjlxcmFyQqkRSM5O/3rd5WAnAUsqP8GoA== 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=uOb/SSpfVdSHz1Z+PNlXCOer19/Qt50UtIJ4Zu2ET/M=; b=PMsm5h02R9Ih6Cnwa8Be1yl24uTHbUXu28Pyc/nH7lFn9F9dXtDK7XhTUAHIsais6a1AZitW02B+GpQrOAt1kkb/LOrKCMBqrqTXzIcbsgvxixV6AG/H65nJ2djXUB9Jk8dJjqQ2Fzfoz4XJuevdqY3tG0V+TMBWexlmFVz8rCQztfTGtslQ01IjS97PW/jQB/ZSkUcXvqQw9ECoC9BxRzhXGJQvItAamJK27lmnQqBa806LTJS3hzqTQMM0hc3pBA3Jwo7OgwAjSTL3vIhcZ9hwjnS814kAQQxdkwZHhFIwxbUM/pPN1PQS/ilc2CcNaQhUgvvqSDg3x5VAKGEFUw== 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=uOb/SSpfVdSHz1Z+PNlXCOer19/Qt50UtIJ4Zu2ET/M=; b=YpBsUcx8XlpaYMHURWznloHJpnrFQUyAPV2u3YeYIigr3U84PYoYO/NH3j1MhRYY6vWMoDafqS+Tn1/8Nua+uM/c3SFR59aqA98ff6cPleJO5wqkPyL3xcL2C0A88aWsQ8caR+lgJCiUtmX8eyfuoXZemP/IHmAhOySk5NRURS4= Received: from CO1PR10MB4468.namprd10.prod.outlook.com (2603:10b6:303:6c::24) by PH0PR10MB5845.namprd10.prod.outlook.com (2603:10b6:510:14b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5314.13; Thu, 2 Jun 2022 06:39:30 +0000 Received: from CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0]) by CO1PR10MB4468.namprd10.prod.outlook.com ([fe80::e9bd:ba52:c1c2:11e0%8]) with mapi id 15.20.5314.013; Thu, 2 Jun 2022 06:39:30 +0000 From: Imran Khan To: tj@kernel.org, viro@zeniv.linux.org.uk, gregkh@linuxfoundation.org Cc: matthew.wilcox@oracle.com, konrad.wilk@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH RESEND v4 4/4] kernfs: Replace global kernfs_open_file_mutex with hashed mutexes. Date: Thu, 2 Jun 2022 16:39:07 +1000 Message-Id: <20220602063907.136569-5-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220602063907.136569-1-imran.f.khan@oracle.com> References: <20220602063907.136569-1-imran.f.khan@oracle.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SY6PR01CA0014.ausprd01.prod.outlook.com (2603:10c6:10:e8::19) 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: 26f8fdb1-33b1-48b9-f5ad-08da4462a59e X-MS-TrafficTypeDiagnostic: PH0PR10MB5845: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: es0Y57+mE0PRx6HAMf0iLmwb3Dz7yB2rtENtFr5EDthEvTYQT3HtXSRYDL4Dv2l5/K8dBg0YM4L/X0Tr/rHSbnTRDEf8pJjgdh0hR5aqu8pUDhpjyIgmBvWlJfiVN0uMCWNqe+pvA5rARgZCRA5Ed9Y6EJoxzZwQRWFUoJiYNQew7Dt9+VX7MpFYVJRxLi51H18Z/o1p1qT24cIkuPfRgvDwnjZr/bTBpO3LolTy4rhFD+66V/K552rMbVEDRugUZbbwZyCqCCVIUDZrBcMp3ifxyOJfqeDIXdtQfVKl6BNzPUWRgZmibBYI1fxgM6kWdt4fKb1NWCLPSrraYNHljcIVBiMbHRRfPapK+YmWsKZEztdzmXveH8VZs/NLA81Vmn3WLDCPJUJcTZbWOA+lH5/jpsLsiOLxMpur7JyQMLuHX+bNMiQNT12WHUcqjIC0MdB6Hw6z0U8iZ/dhAmYgQw2gd+MMMIFfPNiOLkRsO/YLjBKCAusBcVX3D2tzkzFinvE7E2QO4t92hqeh0rLFYiXALKgJC9yEb6fx0TAJGyMU0c7BxkaXcS143kPjUB7TsbIDNrlMih7/a6IYMtO6yzeoJeIGOCwRHb2ZQy6XS0Wpy7j2lhSrPrBRPB/Jkgi95qm41dQYO445ZRGv5mxb/Mq2PilLQE906aAkNzUqxLPFSdtcafojLZp5y5oNxWGCtC4lA/3UlyBxLXGSEx/EzA== 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)(186003)(83380400001)(38100700002)(1076003)(2616005)(5660300002)(103116003)(36756003)(8936002)(2906002)(4326008)(66946007)(86362001)(6486002)(66476007)(8676002)(6506007)(6666004)(6512007)(66556008)(52116002)(26005)(38350700002)(316002)(508600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?neqH/diUXa42mJMGrqmqzmgdpjgeIsUaILRx7j4X3ekQ/aiGk8phwBP9V0ds?= =?us-ascii?Q?+eYNaidwMEM6wI2AVM6uj8Tg8kqlXvep6gHe8UAzS/Gh/7eEoLDZmHtwZg24?= =?us-ascii?Q?F2npzFsQClZXyoFDO0A6bDmiipgxYO8ZuC1RATH2qRZ5XYVhxcjB+sUVRZst?= =?us-ascii?Q?FQx+traAOHc1ffkIFwYRwhoxNW7DrkrafHqDRYdLRnzk6FH46PdpkrDiy6CY?= =?us-ascii?Q?NASx0pdWrMEcgT/wUtiT0LBJjRWy4rKLjvPLrnikpU0+C9FFdNfPW7piMGTH?= =?us-ascii?Q?ga6DspsUFeWxXWAmJmcjYao4RYPQKELpXqIxy6hxM2Uf1kL/2zEnc/Rcvhe/?= =?us-ascii?Q?TLW1cj/texunwvneeyqr6Pu1mIHTYJT6/xRK0DbeRrCfLIhMoBPgyGTHUE9r?= =?us-ascii?Q?kHAjPo82fyLoTSRyiOhJNvIE1OjO9OVg/Vyd08OPjA+OEw9StIByuI76I31M?= =?us-ascii?Q?GrbFDyzSouus0y69fzqevLrTE/GKMxtvpKDSy9UM+KHBXCBjlmzmKwIs9X7b?= =?us-ascii?Q?TMUzzhPQJOh0gRLF06Bnx8q8Qi9LFdAJha/7G4suD4gVeWLYo7zYl49R7ndD?= =?us-ascii?Q?sueRo+aVEFHB2IdidNqYiM9oetqVTVsPjAoEZpngyAyB/wVNvwIU+z28zNDc?= =?us-ascii?Q?IWXkcoUSUnvDCdZTC9JIetB3fqwDkZI+q0D4a8BYQjhAN2IPiRJQfv3zmbab?= =?us-ascii?Q?vW+Wl4dp96o4umwnVoDktwtyKOIg+BWqTra5v8YWPcODZ1aqJeTaSOXWsZOo?= =?us-ascii?Q?5ISsFQRQRzc1vTOfeerQ4Lr/EU2ziuDsPYNS6HWB1pmtR3SVg//NGfTeE2Qn?= =?us-ascii?Q?Nk2ngzMWKTXU0ScdPg6yHPhVMp9+Dd4r01O630L2qZBvqn7T9ivcJ3axmpyO?= =?us-ascii?Q?M46D6V8hvhvTnCxsKSAMMEIThCxVTqTxc6e9ghKQ6sQ7C7a9Ln/GynpMqXa0?= =?us-ascii?Q?QtU0pveWlRRTJvztMyOMAr1IfdV4qzMhtR9fWday6rZEyfvW6feDG3Wl2VMl?= =?us-ascii?Q?A2+Iyl3Hj1vv/r7+tB+BgJFq85kEahWgMviMRAr2e/EFItFgwbzWDcd9HnNR?= =?us-ascii?Q?TqAGGZRPCdGHFbekRJo4fgoOXXXjavU5tycb4rTxXDIhe12MzyU3nmnBuC/3?= =?us-ascii?Q?0p3IPA5QglHWyyFKtldnh3gmYSzLguoyPfpW6Yl/iDZmcUtt+HLNRDV4xmty?= =?us-ascii?Q?hirjclWIwybgdRJLGBN5VJfc/hcEJXjakM8FYlA8JgYcf4Hr0/zAxbNFlqZ2?= =?us-ascii?Q?gdpGEzie61V9TEx+h4IkXH+DG+eViyZdZSJMstM/fOA4ayCxfmWQJN20xP0h?= =?us-ascii?Q?f+dlcdlkMwArcVzpJWT5SReP6TnT41W6U1dH2aWVA4+EMHSQLrGm92tCb4Tz?= =?us-ascii?Q?je1f3NucR9yT8Tv+30pY7t5MvnZ7fKQBvPQSoQTtttwWO1Eb+gtJSdKe4hFU?= =?us-ascii?Q?8c2cxOG+LRY8IedWdO9m6qRfUok1dc2U1jxmFdCCK4FpbXAuEop78qc08t7x?= =?us-ascii?Q?k1KD/zz3sAyVj5D3K5iviAMFFU9uMiKbGdtREQvH8batSKLPu18uSxi9b2op?= =?us-ascii?Q?k53C4dpSVF2rfD3mCcZMP2bgrurXhsm9ZhxdNpr56bh/mt2nibkGmWyG9ghz?= =?us-ascii?Q?3hA4sAoLKy5ghGc7HuZVsBkO8bfr6vMdMnFLZ9E8UOgbRrhaK1cawmdToMON?= =?us-ascii?Q?RGBXqOtVrZScYTzEDHvIB5FvIaSH4iaMYtfwWOU5hUTh2uwQ6g7R40IvdGSo?= =?us-ascii?Q?45TsgEduCSp9Sk/2gkjzP3rcVAHg4M4=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 26f8fdb1-33b1-48b9-f5ad-08da4462a59e X-MS-Exchange-CrossTenant-AuthSource: CO1PR10MB4468.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2022 06:39:30.7019 (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: PLsF8Cmhnx9A4syYANa6Xkqcxm5S8/RT8pDuRWyhLbMYg3/8Ysfx02v/OjCmqTVOGGwQ2UCHZZRezK+G77BBaw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5845 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.874 definitions=2022-06-02_01:2022-06-01,2022-06-02 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 suspectscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 adultscore=0 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206020024 X-Proofpoint-GUID: j-JHfHAlThFJeXEcL16yZe4gjSTJdiUx X-Proofpoint-ORIG-GUID: j-JHfHAlThFJeXEcL16yZe4gjSTJdiUx 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 d35d01d30fa0..987ef7165acc 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