From patchwork Tue Mar 20 07:28:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Bloch X-Patchwork-Id: 10296601 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BE6DD602C2 for ; Tue, 20 Mar 2018 07:28:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A6897290CA for ; Tue, 20 Mar 2018 07:28:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9AB102923E; Tue, 20 Mar 2018 07:28:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 064F8290CA for ; Tue, 20 Mar 2018 07:28:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751424AbeCTH2b (ORCPT ); Tue, 20 Mar 2018 03:28:31 -0400 Received: from mail-eopbgr10085.outbound.protection.outlook.com ([40.107.1.85]:6425 "EHLO EUR02-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751304AbeCTH2a (ORCPT ); Tue, 20 Mar 2018 03:28:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Mellanox.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=ICgKEN8j1Q3sx9SHFzVUs7+oKljjRKs/Pzyc1MAwLPI=; b=P3jHjDlDTcOBOe21pRQM6mYj+Nos71U+SUSC7+gY1Mn7Yj5Ds63JnTcLpU+z7r67YrsSu1I91sRXV6C0oB+9DnvNVI0hgMF9i6r3BTkxRQb3s4wvCwaUCGIqT+7gzuNV8zK+Alo9IVjzJqet9BqgZh+GgFfbS6zMuMuR0WAX61g= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=markb@mellanox.com; Received: from r-vrt-26-1.mtr.labs.mlnx (37.142.13.130) by HE1PR05MB1340.eurprd05.prod.outlook.com (2a01:111:e400:5889::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.588.14; Tue, 20 Mar 2018 07:28:26 +0000 From: Mark Bloch To: jgg@mellanox.com, dledford@redhat.com Cc: majd@mellanox.com, leonro@mellanox.com, linux-rdma@vger.kernel.org, clameter@jumptrading.com Subject: [rdma-next v1 1/1] IB/core: Protect against concurrent access to hardware stats Date: Tue, 20 Mar 2018 07:28:19 +0000 Message-Id: <1521530899-9581-1-git-send-email-markb@mellanox.com> X-Mailer: git-send-email 1.8.4.3 MIME-Version: 1.0 X-Originating-IP: [37.142.13.130] X-ClientProxiedBy: DB6P193CA0002.EURP193.PROD.OUTLOOK.COM (2603:10a6:6:29::12) To HE1PR05MB1340.eurprd05.prod.outlook.com (2a01:111:e400:5889::18) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: d33c2a50-bf39-4212-3254-08d58e342c4c X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(2017052603328)(7153060)(7193020); SRVR:HE1PR05MB1340; X-Microsoft-Exchange-Diagnostics: 1; HE1PR05MB1340; 3:GBRM8W+mjzUCvZuRag/5huf0Zj4h7+mzJCk63B9SfHAnDaYEoNESNVx9G6An/Z/9LLVYy5i9I4cvT7TvZ9tS3+veDKsIJ+hW5C/hVvzPOB0FrhiZ86un4fdFujdmI8nKajFB3D+h0aWpEAt5fmvP9oSNbYXsUquisBIDe8bL0pD6F1s+qROksXHCPnEn3uEdLEj8FPJYFZhwfQnhCdWG5L4EQ014yvPF46WpgOlJhanQA3eqqq5qNDyxrqNz3Kao; 25:Csspwp/boF2soX+rva/TYLwKNw4AnknoL+2yIyA0QJ1BQyAgac8q2aobwAtP5qvf9bhjtbdPnTNVaK4julh1Zci3GuqWWPoikszmlTLuyDMXMTvbaE79JJSYCd5GNVZ4JqjfmDkjas6qYQUHkwXqOXsyBYbEKut87dPunIgC9nGfftHMTWvfpTgBqBFd0JFWhpbiFHO96XiQjnxTBbZpg9J1xmhplrFvPEDTbOU9Zi2o7TP518vnkuLRRmbgWA04p8V9Cq2NfkGS8eXXYYItunFB5SUlJkkEmegS1uEyu32s2l1d8kLkZaqkPU4YFHgz2WBMx9bVjyrWnzy1Kwj9hQ==; 31:bzYA4K7+omCTDjRQzSAa7uZCcxQ/h/ju9KGerHqIhOVYnCFf6CC9V72VbpB4e3kxPmJnJfTKyshkzylHFqqMotInb9090umC8a8KSWzdjL090K16Q7iRpjnbFS0kBscOAgkEJ7+VQVLeKPJXQzsJZHymfptgF6+AVBT74mMo2WzLb9C333r+ipJTf6wT1CdGmS+I4rXPEZJYjjthT4y/ErcADqaRWJMur/w2wCFMDuc= X-MS-TrafficTypeDiagnostic: HE1PR05MB1340: X-Microsoft-Exchange-Diagnostics: 1; HE1PR05MB1340; 20:kSUT9/qH/VDEEt3k14mb4HWz4gE0K/NlgpAQKM6iJyBDwJ9EOt0WLKcFlcAyfdPBueCYceC04uVt25W4YjVUzilsq10GM30ND3XKI922vZnts5eb1m4u52PdtG0+/I3uD+H9BQwXtEwvSU+pgXQTNFYdHptdQoEXFkyq1WOepr/UAhm04F0e75BStYbrKh7x67I8w9LChifCZzmoaqfI9Zq1SL78Fyi05y518TpmG2a5F7pL2M2XMD+vXy0eLBumft4tf1fAbyO2ybO6BGRdojVGVOtyj3tU1L8E+gkit5D41it/ZyoiJCjMqbPsdy9cs/r+BDL+HkvzgC1KH+lCAYfNHemRjmMkQAyfL/kAEub/btWlIiXxsM1r0rp30p3eVHpqSAEcceB5OXUn5nnw1eUws7CX8UNQwS2WuHZzknyBXzRzOOv8Fyg3xOUzFpw/EQ5UZISTvV6PNx8eBdWRamCm2MRWSt5bxuBMdd1RQVtogPyhXNgW2XyaVJxnQlPI; 4:Bw7hzun2jFUWzqndRQz/p2SQWiunZFVvdLUAnM6oBEz/TzI4vPMpph1z6QKrnht6XNfXBd0uZvz6GuTEf0EuA1iK1ETSFAPbagGS8a0RYhyYoOpK+kAQNmtlLvVgnfbMq1Myzit+SvY5QifOUxQ9CLs1lQ0jllVmYc/OKPTYqPHe2u9ISG0QzOxm18BXin74bU6mHlCJpQvNL2CunLHZ2FGFGSgt9hhDdcRBs8qTD+jqKvNL2PhHZk0rtlV0IHwzkxzakbFWQ2hIhdJSfeTcaA== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(10201501046)(3231221)(944501308)(52105095)(3002001)(93006095)(93001095)(6055026)(6041310)(20161123558120)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123562045)(6072148)(201708071742011); SRVR:HE1PR05MB1340; BCL:0; PCL:0; RULEID:; SRVR:HE1PR05MB1340; X-Forefront-PRVS: 061725F016 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(366004)(376002)(39380400002)(346002)(396003)(39860400002)(199004)(189003)(8936002)(5660300001)(97736004)(106356001)(16526019)(7736002)(316002)(26005)(2906002)(305945005)(4720700003)(48376002)(52116002)(6506007)(59450400001)(478600001)(16586007)(386003)(51416003)(6666003)(6512007)(81156014)(8676002)(3846002)(50226002)(66066001)(47776003)(50466002)(53936002)(68736007)(6116002)(25786009)(105586002)(36756003)(86362001)(6486002)(4326008)(81166006); DIR:OUT; SFP:1101; SCL:1; SRVR:HE1PR05MB1340; H:r-vrt-26-1.mtr.labs.mlnx; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: mellanox.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; HE1PR05MB1340; 23:3oaCt3oDa7CsTaowRnT9l28TwOaNEc8D8oD2pPHtw?= =?us-ascii?Q?xcHNNBe67djOkkXciv/NN47b8X4cXc8XP7Qv/D57nr9SGyTjq5wYWR6Q7h+e?= =?us-ascii?Q?HRsIgcNDEJTmOnr0aXDBut8YYGavaqgH7Zdzq9+G0Xkw6GRvrKKZraedUWFa?= =?us-ascii?Q?Ak0f7lFsYw1NX7bWkkmnVIXCuAz24PFIyYIlp1QhEEE0srJa8rOLdW5xD4pQ?= =?us-ascii?Q?ztHl9d2jBEmtbf94CUlQrIHKCCSGlsHp3dNlXJycZKZbn6alKhqAA4QQJECJ?= =?us-ascii?Q?WuHoPrhDO8I2oiZUVN6kUwpR0w5obg8cr6Yp8iBX4ln1Q3gSiZOPUn7QKrs+?= =?us-ascii?Q?NJRCIFUlt2GQh4FVeaIIvTTjPLhqTMn1j3a9ntlXrRtpJKQjcloS+gfX049t?= =?us-ascii?Q?aqQ76JTXj0zZgO6ILhraYXN2TAiSXoTGfeYBMffcnrp/aqP9Nxt7gCSjHMBX?= =?us-ascii?Q?wpkLwLJudD4SvnglyYBSaf5xvi2uKm3hd3+4bqM7yrP/tIuL4Gpwvpjmju1q?= =?us-ascii?Q?fzEE7m8+0DuoHrYt2bQ6dCuX7NS0itoeHmf/gp07vkc52Vd7iL2DziVmzAl2?= =?us-ascii?Q?+gmrfBr/hOdx3+wdRw/zUYJtZclpX2QBS4UKNQn+KFdUuTgYlISvh2ueMGcm?= =?us-ascii?Q?wGIUMZ8wLrJz70//bPnz5bA6tCNVZCYyih0vBAEpvssKqdjYsjbV0yNo8ukO?= =?us-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9CqEowDXEf/k2qa3alFpk8=; 5:MreGuqVSzmRUrqDuaJJZrIgABOI5RZYQ4EP9Qy3SDn+giDSzJfZsm1tHYWLaLZz7fQ1yd4ec2KltG6w7A2R3AzT9XMirF60BiQgcP5ZVkPFs/Vd33YuyOnpkc965BenholzpXiqxEo82I+qVixn2q7HP3TFi8vTI3rtVB+CvEPg=; 24:HkEIFDPLNkLgMQ2oyalUosvxnIX4kGhrcVYZpvtx+g72KsUp+I4LmJiz1vXEOMM93NY/QbVcaYnzMOIPfzm7nkU4xhHtQbqGI4+r2Xf3ugA=; 7:bfCSiHpjTS1m9znh/HUGFk8jRA9Kn6q3WQnED6jgX3A3zgl4LC7pPjWEBQLUPivhEG8KT7j82lxFvDt7bYeWlWr6pg90lWgRDNxEKYCz2PiSjWjq9LBFUcoBvnlSFNJydoFj3xlwftSY+DcAQvoiiZnf8I37lQKQxi+HxXnec4v0dm2GVq1+2OIiFHgoSpJJGEO9Sm/Mlc2WfdXqqhJNLwapS8H08ajT04eTLSDUq0rAYz/CTICmn1UJESlPbWat SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: Mellanox.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Mar 2018 07:28:26.2146 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d33c2a50-bf39-4212-3254-08d58e342c4c X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: a652971c-7d2e-4d9b-a6a4-d149256f461b X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR05MB1340 Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently access to hardware stats buffer isn't protected, this can result in multiple writes and reads at the same time to the same memory location. This can lead to providing an incorrect value to the user. Add a mutex to protect against it. Fixes: b40f4757daa1 ("IB/core: Make device counter infrastructure dynamic") Signed-off-by: Mark Bloch --- We might want to protect setting/reading lifespan as well, but it seems not needed as it shouldn't be changed too often, and even if it does not by multiple threads at the same time. v0 -> v1: Removed RFC tag and added a Fixes line. --- drivers/infiniband/core/sysfs.c | 10 ++++++++-- include/rdma/ib_verbs.h | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/infiniband/core/sysfs.c b/drivers/infiniband/core/sysfs.c index 8ae1308e..b769d61 100644 --- a/drivers/infiniband/core/sysfs.c +++ b/drivers/infiniband/core/sysfs.c @@ -810,10 +810,15 @@ static ssize_t show_hw_stats(struct kobject *kobj, struct attribute *attr, dev = port->ibdev; stats = port->hw_stats; } + mutex_lock(&stats->lock); ret = update_hw_stats(dev, stats, hsa->port_num, hsa->index); if (ret) - return ret; - return print_hw_stat(stats, hsa->index, buf); + goto unlock; + ret = print_hw_stat(stats, hsa->index, buf); +unlock: + mutex_unlock(&stats->lock); + + return ret; } static ssize_t show_stats_lifespan(struct kobject *kobj, @@ -951,6 +956,7 @@ static void setup_hw_stats(struct ib_device *device, struct ib_port *port, sysfs_attr_init(hsag->attrs[i]); } + mutex_init(&stats->lock); /* treat an error here as non-fatal */ hsag->attrs[i] = alloc_hsa_lifespan("lifespan", port_num); if (hsag->attrs[i]) diff --git a/include/rdma/ib_verbs.h b/include/rdma/ib_verbs.h index 73b2387..33fbccb 100644 --- a/include/rdma/ib_verbs.h +++ b/include/rdma/ib_verbs.h @@ -484,6 +484,7 @@ enum ib_port_speed { * filled in by the drivers get_stats routine */ struct rdma_hw_stats { + struct mutex lock; unsigned long timestamp; unsigned long lifespan; const char * const *names;