From patchwork Thu Feb 10 15:04:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Foster X-Patchwork-Id: 12742042 X-Patchwork-Delegate: kuba@kernel.org 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 8CD76C4332F for ; Thu, 10 Feb 2022 15:05:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243353AbiBJPFD (ORCPT ); Thu, 10 Feb 2022 10:05:03 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:45514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243332AbiBJPFB (ORCPT ); Thu, 10 Feb 2022 10:05:01 -0500 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2127.outbound.protection.outlook.com [40.107.223.127]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54BD3BD2; Thu, 10 Feb 2022 07:05:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mc/Z3wknmSGOCelx0tepgEY9SKi5jYEcs9g91nV69lYrLjxs7mpB7FZMluM0Kj8V4rdFYYBH9nBFrsjOrbGC1fXa6FczYokXOUO6aSUOl0G3usUQdMpPtwameoJ6KGoiJfV5smTPbTgY1ccIRuiFOPYId35drgszOrqOd8z01oJkdh7JtntPNuy3Ki0W5hdjAxj9sY7G+m4WPpdjTD2ttOaYlvE5JNaWpKez+1q29/aYzyErzKysJtILYLqb8Ovt9FLAPeQH2bN+UTbQYuCsAgD5JSqNeOH8lyPZNE9ExiEEhophRjfuAw413R7F7C6wZLAxrySbsdDL6FyoGUzDiQ== 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=ASc3zgnMq9X2Pk+LcocD5lIAwlmiNV8x7v/Sm4LEpdc=; b=NJDwiciI0gjMu5YXQZ/fLO2E0xJPjjBtCQG8qJJ5ngkZ3J2qxYW7M9DtWPNvKA/S0QvDnaq6Q44W9t/RU0Yb4aAWzeQsO6WKtvmLpthdmcjU50rQ17RYHiiZD7y/iOM1ZDixxALxhBWI2Qs+A9BhLlbTjcd5BZV+GvY8WAB/148D/HWBi17rTcS9/6cmZ8ipnAtAXqvafwPmkIYKn5OBI8/dTAczh9AN0ksbrh9O+OxoCUUkGh89U5a5CwcmI8W+z+GctNjqJadSGfpLJJJPCcKgpyOMDwf0mdjWmj4cXp2UIdysX7B6cTHSkvQfbgffaQ7u6AqH4CKYDU8h7RGJmw== 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=inadvantage.onmicrosoft.com; s=selector2-inadvantage-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ASc3zgnMq9X2Pk+LcocD5lIAwlmiNV8x7v/Sm4LEpdc=; b=cBPs+MjP+xYHvkuM2NT7RZlEjouYLP5mqcrIYlfXgGshG7ZAlckGNzxQMv5p5zDz95b44GXzQeBYEBBHic22HpDjLaU1+wkjKORRlPAyQmUcPQy527v3+Z5bIxZDGJr8ZPKDBDBn5gTOp8S/AGFaOU+ftYBJ4GDPQ4Y++LoOSv8= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=in-advantage.com; Received: from MWHPR1001MB2351.namprd10.prod.outlook.com (2603:10b6:301:35::37) by DM6PR10MB3420.namprd10.prod.outlook.com (2603:10b6:5:1b1::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.12; Thu, 10 Feb 2022 15:05:01 +0000 Received: from MWHPR1001MB2351.namprd10.prod.outlook.com ([fe80::7c:dc80:7f24:67c5]) by MWHPR1001MB2351.namprd10.prod.outlook.com ([fe80::7c:dc80:7f24:67c5%6]) with mapi id 15.20.4975.012; Thu, 10 Feb 2022 15:05:01 +0000 From: Colin Foster To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: Andrew Lunn , Jakub Kicinski , "David S. Miller" , UNGLinuxDriver@microchip.com, Alexandre Belloni , Claudiu Manoil , Vladimir Oltean Subject: [PATCH v1 net 1/1] net: mscc: ocelot: fix mutex lock error during ethtool stats read Date: Thu, 10 Feb 2022 07:04:51 -0800 Message-Id: <20220210150451.416845-2-colin.foster@in-advantage.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220210150451.416845-1-colin.foster@in-advantage.com> References: <20220210150451.416845-1-colin.foster@in-advantage.com> X-ClientProxiedBy: MWHPR13CA0001.namprd13.prod.outlook.com (2603:10b6:300:16::11) To MWHPR1001MB2351.namprd10.prod.outlook.com (2603:10b6:301:35::37) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 8ad88271-abe5-45a8-2379-08d9eca6b5a5 X-MS-TrafficTypeDiagnostic: DM6PR10MB3420:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:983; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TNlimXYmI+HqjCE+6reZwu8OkbOyAfP5//Z3tl6Ymt+KkxDw50/kfp6MYXpg0M0ZzprNwGUPNWJHzBLc2Dx68okdI/NjhFXBEgiY1KxgXqllfmGSA7lvF2aX0Cf5nBEwWvYlmmBGzQ3DmmB6OdbcZZBXkKtf/bLpwJbU73g+PmuSbr6zr0eqsAW6qSFnM6bzdp7lnkAWoB7mDGZG4lgSN83xGZGgwY8+iQhOmB6Y9ZJvBB1iyOX0RAb4KIBexmEHqa+YBRPEgr90+437wcNCRyGDppqSBV2OfdXn/k/OwgfjAGHM5t0KeAKiDAxyQwoN6+7xBrmglFvjUOhjHu4AWVG0hqDEJDdIv+9xwh4as8bbFHsY5V8Hou1m3EwfzWpwtzAQb1v9LYsnZgJrGNgS37FooPgeE4ajP58VAU/YuHnXP8CIi7Diowr6IEx+ZacXC5lSl3ZuQKLksRiIKGSFOfza2D2cUD6myxVynW7Z6+6Eh4eXz6ja+SEOEBUCINqxtc5Ro1E5Ry25sP4jgUHMBwmPhF4RZ5aieWYGX6ZMLFsU2RVLEtOwz3w1aeHmzY2Kd4cNews9IpdbjABF2R8uuwInduc7YxUbTIY0yyuaVL6ba5I5RhPyRpS4yz7cVUpR3zHTdcvJnIe7n6poG37AjwFRc2hQaH+BeTexHCcabKVWzH4g2+wX4KgJwYGyv9CBt6tcZvHEZqRlREwomTeeYw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MWHPR1001MB2351.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(376002)(366004)(396003)(346002)(39830400003)(136003)(42606007)(8676002)(66946007)(316002)(508600001)(5660300002)(52116002)(6506007)(6666004)(66556008)(2616005)(4326008)(66476007)(6512007)(8936002)(54906003)(86362001)(1076003)(83380400001)(44832011)(6486002)(38100700002)(26005)(186003)(36756003)(38350700002)(2906002);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: xyYlT/FQ467thr03C7sBkXXjBIXNL0Iq/D8ZTpqu+VBS+Sz/wjfyWwSUpTniNoxNmNoO0RVcgvg3CQ3tqOSrk5xaesL77o1P6nl+Pc012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Ph8+8ecs8OaxiuDAo50Z8uy9XHJtPWrQIO0M8M0ZVPTWWcvjXu70tAQdgDJSnQmUQfGXE2qlMfo5kttEnmyeSGTGKWfcP7DwqZpXHEMtgzUMTRwSMd8twSciZNWeZj8+W//gAwuhHLAdl3ELLI2spYgNKqhXZg6gUmoV8G+m1vPJ4xAaZXl0bfVLS26XcVimseDocGdELYKKPrQT0A6p9o7ee1LD6Ozz+OA48jRYiUQToSVFD7aKjOkV0n+8Wp6FWqIlQ4UjhlWMWsOpYenNmwgTGgrvE1zwhfZ+/S333rFPNv+pZQFUrXsQYU17RttulfzD99nMJgbtJ49OGQGFya4lfmIII+GCpkGwj/GdH+FDLo5MXBhNwKCAZv/39IFtxxonIkZPoc3CfVPM9hivMDT3gkF9FDtKsjFhcK1uzF6OrzNCTTCSUWJMX13pwPWOb23KBlufS5XlY0iCY8tEzCif37HMdMS4kgtK0btc9Y2kqQwy3exGqjP+PAxqRvM= X-OriginatorOrg: in-advantage.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8ad88271-abe5-45a8-2379-08d9eca6b5a5 X-MS-Exchange-CrossTenant-AuthSource: MWHPR1001MB2351.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Feb 2022 15:05:00.7423 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 48e842ca-fbd8-4633-a79d-0c955a7d3aae X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: J43FyFBUJSTKySyymChlEWGQ9CXg0BjG3LiKUvuIEC6RRpC50Vo5RxhSWU365PwDJDUbHNgZf3F15NEjIu5NKiPB42NY9CZOSVUHJ43fGbI= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB3420 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org An ongoing workqueue populates the stats buffer. At the same time, a user might query the statistics. While writing to the buffer is mutex-locked, reading from the buffer wasn't. This could lead to buggy reads by ethtool. This patch fixes the former blamed commit, but the bug was introduced in the latter. Signed-off-by: Colin Foster Fixes: 1e1caa9735f90 ("ocelot: Clean up stats update deferred work") Fixes: a556c76adc052 ("net: mscc: Add initial Ocelot switch support") Reported-by: Vladimir Oltean Reviewed-by: Vladimir Oltean --- drivers/net/ethernet/mscc/ocelot.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/net/ethernet/mscc/ocelot.c b/drivers/net/ethernet/mscc/ocelot.c index 354e4474bcc3..e6de86552df0 100644 --- a/drivers/net/ethernet/mscc/ocelot.c +++ b/drivers/net/ethernet/mscc/ocelot.c @@ -1745,12 +1745,11 @@ void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data) } EXPORT_SYMBOL(ocelot_get_strings); +/* Caller must hold &ocelot->stats_lock */ static void ocelot_update_stats(struct ocelot *ocelot) { int i, j; - mutex_lock(&ocelot->stats_lock); - for (i = 0; i < ocelot->num_phys_ports; i++) { /* Configure the port to read the stats from */ ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG); @@ -1769,8 +1768,6 @@ static void ocelot_update_stats(struct ocelot *ocelot) ~(u64)U32_MAX) + val; } } - - mutex_unlock(&ocelot->stats_lock); } static void ocelot_check_stats_work(struct work_struct *work) @@ -1779,7 +1776,9 @@ static void ocelot_check_stats_work(struct work_struct *work) struct ocelot *ocelot = container_of(del_work, struct ocelot, stats_work); + mutex_lock(&ocelot->stats_lock); ocelot_update_stats(ocelot); + mutex_unlock(&ocelot->stats_lock); queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, OCELOT_STATS_CHECK_DELAY); @@ -1789,12 +1788,16 @@ void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) { int i; + mutex_lock(&ocelot->stats_lock); + /* check and update now */ ocelot_update_stats(ocelot); /* Copy all counters */ for (i = 0; i < ocelot->num_stats; i++) *data++ = ocelot->stats[port * ocelot->num_stats + i]; + + mutex_unlock(&ocelot->stats_lock); } EXPORT_SYMBOL(ocelot_get_ethtool_stats);