Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp1686706rwb; Sun, 15 Jan 2023 02:34:51 -0800 (PST) X-Google-Smtp-Source: AMrXdXsZ8J3NDaPjbngl5VX+Xk1+A1dH80i4P8kKfmEEkqlmitwwXKAlpcS8dWrb89d6cEweHphN X-Received: by 2002:a17:907:a70b:b0:7c1:98d:a8a3 with SMTP id vw11-20020a170907a70b00b007c1098da8a3mr67956652ejc.7.1673778891216; Sun, 15 Jan 2023 02:34:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1673778891; cv=pass; d=google.com; s=arc-20160816; b=Iz1I1DQYiiOeHmOVbNTVWJDn2oboWKkJODGglEqjt+43GAgwa0q7L09zwRZBa4/iWe 5G6SVkdIEK55BSKmOIEQkFU1BegPj0WKeLQNtffiNAJhA+p4UFJslNGD8siaCPB/S8g4 p9pYRGMe7LpsVLKD9ioDIboZLfVvhLW4N8Eim+d6scVFLisw/dK7yLAeX01qsarUDShk GiBidU+hSmxEs+n2cRtzNtRm2uymRnxyLq0ZNjUPZ6qfxZx53nqAOlG4vjjRqcacI2zf QHSneNjoYagnhxdA3XKe+aBZdKzbdGFTIZU4Eu09BvRriqDFSkhqKnEVW8O/OFCZM1F3 Hg7Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tLVW6cpIN92RszWQGZf+N4nrJfbYAPQMEODlCfdC350=; b=wOe9mefpBrgO5Z98kASnPyWtSzv4X8AEpgKKNOFp+4VEnfbaFrztorLz0Y74HInfB4 xG4CYUIUVg23ToqpM0NKhZ9Ry9oiVEbpqNCGVnksBn/GymhD+UgCLsJ6dubwrjRHible U0g+JwFgzViWwNPkyzoXG3vCGpGsQxHiOU6JQreO38tKOzFlA2gKyWuTLSGibOUZ6y+N 783KnzCyqHmGIBeen9GOlo9BbNqzB39tgtgjNRQBCuV/nBz/1OXDAUKOBWwLEsrsSUil ueZYWE/pR74QYOIHp9GwLmklqxdoVPLeO05ptroxG/uFGimqW1tuUJV510mQZu2NSKtJ 06WQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=XB6O5oay; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id js22-20020a17090797d600b00860c51f7df4si13786721ejc.769.2023.01.15.02.34.38; Sun, 15 Jan 2023 02:34:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=XB6O5oay; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231197AbjAOKcf (ORCPT + 52 others); Sun, 15 Jan 2023 05:32:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230456AbjAOKcY (ORCPT ); Sun, 15 Jan 2023 05:32:24 -0500 Received: from JPN01-OS0-obe.outbound.protection.outlook.com (mail-os0jpn01olkn2097.outbound.protection.outlook.com [40.92.98.97]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98580CDC2; Sun, 15 Jan 2023 02:32:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZZ8HtHvdENvKMc5J2uIOfj4Kp6Y9foBxjCSsnlC+z5YkiZM474EfhC4ynpDqOfwt6LoxEtaMfgVN4foiwMLkR8ABS9xuZK2mIkEIpvhHIJl3N94/E66CHB6rp/ykaI4DFcbrYopML/eBt8bEAezOXg9fmviGBI4j5BHcjKYaC0EH8F/VH+cXtIMDHGuOGUtHbBxfqE1+iZfYMFrEdJm2nUTRyj1+YDd7yyDs3sJjEAeh64AzFtR1qglUpve4Fgp7k5A+ibRUjrAiktLnvxgV5LMF0eHXKwkkexksaJ2yUjUYtIOch3GCScFtJFW2HFS3G6gzqhv9vOUXblcaTFrZUw== 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=tLVW6cpIN92RszWQGZf+N4nrJfbYAPQMEODlCfdC350=; b=LjxVsH0SpBJqfAMMHL59vCXKJk3432EvrjPZXM6v80cIVJj5BnqySzsDsuDeN98CtyI+a0DgK82z1xZx741FMU2C0MfAmLly5cGWv7bF51LtCPb75yc9JLgkTpwiqnJWVfMhwhqXvWCW4LneXWY01GUwwj0+XteXSTmpyJZSLFs6PFEwsZ4IsNWU5GpjaudyZklUBzlg9tS7xewXbRz6Yta0TkDiOnHCVCuK7MLT9Ru8qR8vKLpqGHw/4iWsJ8pOgDJRBBRvqPvYPm/2qA+9OfQLsu9AhBxSQ/ReGzJOeYrRrPuTNKz4zxlRzQ8BVvfyP/AUWJCWCOIyZfbZNGtf7g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=tLVW6cpIN92RszWQGZf+N4nrJfbYAPQMEODlCfdC350=; b=XB6O5oayfLt/aCyhfvENUfXRRs5UXjrQhNIOoPIl2Ve771LGme7axgopdlHe+b7GcidylXFFuiHNgIuk+KyUaqLwx23U66ef4pJSttngh6J03L9kVgpf8DMWVF6vS3g123ugZn4cY9/hEa3LQLa1fgri7fIAUtc20KLwMX+pd0ZJGvvwUzckegoSHB7hRBghycr00mAMy5jsryus/WQXaiGOKT+mut+i5LWzS8k7NH4ZbiyltGhKk237CKW2JKEpSNS86sYwFMbqjcq7jvOmeGYBV1URn8kRPrFFbFWqK3GEOh1FOmdIzjOzrn92AxQuhnzcxLBRqqzcbdJG/nielQ== Received: from TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:152::9) by OS3P286MB2025.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:1a0::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.12; Sun, 15 Jan 2023 10:32:21 +0000 Received: from TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM ([fe80::ff96:9cb6:e047:c605]) by TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM ([fe80::ff96:9cb6:e047:c605%4]) with mapi id 15.20.6002.012; Sun, 15 Jan 2023 10:32:21 +0000 From: Dawei Li To: linkinjeon@kernel.org, sfrench@samba.org Cc: senozhatsky@chromium.org, tom@talpey.com, hyc.lee@gmail.com, linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/6] ksmbd: replace rwlock with rcu for concurrenct access on conn list Date: Sun, 15 Jan 2023 18:32:06 +0800 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230115103209.146002-1-set_pte_at@outlook.com> References: <20230115103209.146002-1-set_pte_at@outlook.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-TMN: [Ra10at4WNJIhZRyiO61Rfjy0xkteVbPIxa/XrE9qXao=] X-ClientProxiedBy: TYAPR01CA0210.jpnprd01.prod.outlook.com (2603:1096:404:29::30) To TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:152::9) X-Microsoft-Original-Message-ID: <20230115103209.146002-4-set_pte_at@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYCP286MB2323:EE_|OS3P286MB2025:EE_ X-MS-Office365-Filtering-Correlation-Id: b32847a3-374f-40b3-3b1c-08daf6e3c8bc X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mth2RSi214nePp7p+aChpl0eZQuO7BbteM2cExknauR0Ff27xfg8RHoRF3rc9Wza1H7Cerp1DK71TKlWOowM9vm4KX7kTDvlpzxJaFJd8wtk6ZBKk28M4397dNs2SVJPidGWnpD4QR9sjmO3SchxlX/OWFtejsNqjW0vo5uGVZrsARSbYr+sNCkHe1q2wQpO/VA4870587HNs03agII+ku/WVUv38XsoyrGclsOL+FkQ6C8Yv2GvHzZNHTRrlq53IIEIb4Y6aZj2l+NPIEfzoqY2jUDMgShGZRJCtEIJWNOVQKIK6Keji+FD4olZfyQocz56g6UhBO+e6o0Q3pQLvS7FDvStdFp979WK7h76oJMQ4cd45Yt60QTyN5GkBXWHGomtqUh39E/JNrF4Pcocq2JnGuznk85Vunu6s58Ce16+0soIomdd/42RX58ioLXX49NrcfPP2ogUEccTQZNPEcjstyUa61ehKErcV9zi2dO042NH9KM57PI1v0WcmsAKV+gA1a2Azl4zcUH8sMcd0SizhTw9irl1Lo5iQBDWHMZZqwxefOahLgfMaHiG4ehcWMRSS9eZYGs86OAg/8pSCdnN42j45/WojbX1UAJYLhk= X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?SIY1ldUY1Fpn35roPD/jBPhD9BjxbpTGS8nSLe2pEb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B8ojphupyM8M+8n4N/sv7VPihydGRvMRj?= =?us-ascii?Q?iKT8F7x5kQW591B0YiUViN+ZUlODJKZp3r+zql8XYcPQmwGk45A87RUyI7Lp?= =?us-ascii?Q?ndTBav31jEbvsyRwZ8t/dMOOwCS8v1XIdhRZ5X5lm8LSaNmhuXmxrXwQsA4T?= =?us-ascii?Q?yDuyGxpx3aVlkPPUm7ISKlC9+57LUQfccG/peMbEnsjy/LdKofFmWoYhfwRo?= =?us-ascii?Q?44WipRd0wCl/v9CsBOg+fJAIOxUMC2JvLAKx0wsgkLNqndZ9aiz8fFz6epE2?= =?us-ascii?Q?JqKRmC9SclVK0ZQag1qiapVDb857y9U+GXSR7nPi3ufJxGF4copogKoIsSSF?= =?us-ascii?Q?AjEHLtOPcVAX1+ErcnKt1V2qsgnMXRB4BKU48gn7tkpTsVlk1zEmFZiFlbYT?= =?us-ascii?Q?PT4=3D?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b32847a3-374f-40b3-3b1c-08daf6e3c8bc X-MS-Exchange-CrossTenant-AuthSource: TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Jan 2023 10:32:21.4490 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: OS3P286MB2025 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently, racing protection on conn list is implemented as rwlock, improve it by rcu primitive for its better performance. Signed-off-by: Dawei Li --- fs/ksmbd/connection.c | 30 +++++++++++++++--------------- fs/ksmbd/connection.h | 1 - fs/ksmbd/smb2pdu.c | 14 +++++++------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/fs/ksmbd/connection.c b/fs/ksmbd/connection.c index fd0a288af299..36d1da273edd 100644 --- a/fs/ksmbd/connection.c +++ b/fs/ksmbd/connection.c @@ -20,7 +20,7 @@ static DEFINE_MUTEX(init_lock); static struct ksmbd_conn_ops default_conn_ops; LIST_HEAD(conn_list); -DEFINE_RWLOCK(conn_list_lock); +DEFINE_SPINLOCK(conn_list_lock); /** * ksmbd_conn_free() - free resources of the connection instance @@ -32,9 +32,9 @@ DEFINE_RWLOCK(conn_list_lock); */ void ksmbd_conn_free(struct ksmbd_conn *conn) { - write_lock(&conn_list_lock); - list_del(&conn->conns_list); - write_unlock(&conn_list_lock); + spin_lock(&conn_list_lock); + list_del_rcu(&conn->conns_list); + spin_unlock(&conn_list_lock); xa_destroy(&conn->sessions); kvfree(conn->request_buf); @@ -84,9 +84,9 @@ struct ksmbd_conn *ksmbd_conn_alloc(void) spin_lock_init(&conn->llist_lock); INIT_LIST_HEAD(&conn->lock_list); - write_lock(&conn_list_lock); - list_add(&conn->conns_list, &conn_list); - write_unlock(&conn_list_lock); + spin_lock(&conn_list_lock); + list_add_rcu(&conn->conns_list, &conn_list); + spin_unlock(&conn_list_lock); return conn; } @@ -95,15 +95,15 @@ bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c) struct ksmbd_conn *t; bool ret = false; - read_lock(&conn_list_lock); - list_for_each_entry(t, &conn_list, conns_list) { + rcu_read_lock(); + list_for_each_entry_rcu(t, &conn_list, conns_list) { if (memcmp(t->ClientGUID, c->ClientGUID, SMB2_CLIENT_GUID_SIZE)) continue; ret = true; break; } - read_unlock(&conn_list_lock); + rcu_read_unlock(); return ret; } @@ -402,8 +402,8 @@ static void stop_sessions(void) struct ksmbd_transport *t; again: - read_lock(&conn_list_lock); - list_for_each_entry(conn, &conn_list, conns_list) { + rcu_read_lock(); + list_for_each_entry_rcu(conn, &conn_list, conns_list) { struct task_struct *task; t = conn->transport; @@ -413,12 +413,12 @@ static void stop_sessions(void) task->comm, task_pid_nr(task)); conn->status = KSMBD_SESS_EXITING; if (t->ops->shutdown) { - read_unlock(&conn_list_lock); + rcu_read_unlock(); t->ops->shutdown(t); - read_lock(&conn_list_lock); + rcu_read_lock(); } } - read_unlock(&conn_list_lock); + rcu_read_unlock(); if (!list_empty(&conn_list)) { schedule_timeout_interruptible(HZ / 10); /* 100ms */ diff --git a/fs/ksmbd/connection.h b/fs/ksmbd/connection.h index 3643354a3fa7..e41f33a2da7c 100644 --- a/fs/ksmbd/connection.h +++ b/fs/ksmbd/connection.h @@ -139,7 +139,6 @@ struct ksmbd_transport { #define KSMBD_TCP_PEER_SOCKADDR(c) ((struct sockaddr *)&((c)->peer_addr)) extern struct list_head conn_list; -extern rwlock_t conn_list_lock; bool ksmbd_conn_alive(struct ksmbd_conn *conn); void ksmbd_conn_wait_idle(struct ksmbd_conn *conn); diff --git a/fs/ksmbd/smb2pdu.c b/fs/ksmbd/smb2pdu.c index 771e045c8d4a..c60cfb360f42 100644 --- a/fs/ksmbd/smb2pdu.c +++ b/fs/ksmbd/smb2pdu.c @@ -6915,8 +6915,8 @@ int smb2_lock(struct ksmbd_work *work) nolock = 1; /* check locks in connection list */ - read_lock(&conn_list_lock); - list_for_each_entry(conn, &conn_list, conns_list) { + rcu_read_lock(); + list_for_each_entry_rcu(conn, &conn_list, conns_list) { spin_lock(&conn->llist_lock); list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) { if (file_inode(cmp_lock->fl->fl_file) != @@ -6932,7 +6932,7 @@ int smb2_lock(struct ksmbd_work *work) list_del(&cmp_lock->flist); list_del(&cmp_lock->clist); spin_unlock(&conn->llist_lock); - read_unlock(&conn_list_lock); + rcu_read_unlock(); locks_free_lock(cmp_lock->fl); kfree(cmp_lock); @@ -6954,7 +6954,7 @@ int smb2_lock(struct ksmbd_work *work) cmp_lock->start > smb_lock->start && cmp_lock->start < smb_lock->end) { spin_unlock(&conn->llist_lock); - read_unlock(&conn_list_lock); + rcu_read_unlock(); pr_err("previous lock conflict with zero byte lock range\n"); goto out; } @@ -6963,7 +6963,7 @@ int smb2_lock(struct ksmbd_work *work) smb_lock->start > cmp_lock->start && smb_lock->start < cmp_lock->end) { spin_unlock(&conn->llist_lock); - read_unlock(&conn_list_lock); + rcu_read_unlock(); pr_err("current lock conflict with zero byte lock range\n"); goto out; } @@ -6974,14 +6974,14 @@ int smb2_lock(struct ksmbd_work *work) cmp_lock->end >= smb_lock->end)) && !cmp_lock->zero_len && !smb_lock->zero_len) { spin_unlock(&conn->llist_lock); - read_unlock(&conn_list_lock); + rcu_read_unlock(); pr_err("Not allow lock operation on exclusive lock range\n"); goto out; } } spin_unlock(&conn->llist_lock); } - read_unlock(&conn_list_lock); + rcu_read_unlock(); out_check_cl: if (smb_lock->fl->fl_type == F_UNLCK && nolock) { pr_err("Try to unlock nolocked range\n"); -- 2.25.1