From patchwork Thu Feb 10 04:13:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Foster X-Patchwork-Id: 12741236 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 6156AC433EF for ; Thu, 10 Feb 2022 04:15:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233099AbiBJEPi (ORCPT ); Wed, 9 Feb 2022 23:15:38 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:58624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233087AbiBJEPg (ORCPT ); Wed, 9 Feb 2022 23:15:36 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2098.outbound.protection.outlook.com [40.107.93.98]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EAC05591; Wed, 9 Feb 2022 20:15:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=goBu0WqNtqRpRZIg53BYFwrETO5LgbvwHbSU1cDmvUSPgRjtKYtyo7Ifscd7i4j83Q+yFOi6EwwR6AXvgRHs2IT1R6wnICLfvlpER3wJot7gN78i5yPnR8b3L2dFoh/hnxnDBou2Pkq+eCv27sQw6TDInazzu0oRdU+Xxef0EV6sMx7q4eWhl9xXe9lV+euwZGPCW/IOk8PkjunDTTGk1VeG+I35laQyQ96/jWhduT/I06sjNarlAdVa1wYLR8P/tBPq4mRRJJbraBtS0fbqmCaQw+jjfALVMi6h2mAUYAywu/l3y2gTSBYlHp9eDfdC8CqDZVoTl1ZkUuav9x7oeQ== 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=4aG2oIUQ/mI7G40At3g+APWfeyXJe5mF7JaV+F+KQ6s=; b=kqNgkw1/7cacLN1DITOT0NBIcXm4pjOJuMLV2jBQQ2dzSh2kZSyH+zUMbtgx3n4g5cYTpfDav+aJ92D8ok1nX1VImTLqa+VFtpp19+kkJDWrSy4ujiG0DI5WGCOGYms1v2nOyCh/IwAfQe30xycN74g/0h0AxU+/t6yaZnoCRw4K59pgQvfrUZdC61kD56CAf1N0rVNLFzrJCTzVCwVY4MuGY4x5KhgK/R6kV89lH5+v2qJsi5LkjyX6zwt/4wb754xS7vG2H7TLwHKQCmLvKVFv3EGRwejMU8pxvb3XxWZL2J66HreND6IKoLWjsiOsh/UvIYodMWHYp134H+1vdw== 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=4aG2oIUQ/mI7G40At3g+APWfeyXJe5mF7JaV+F+KQ6s=; b=NsxGzcsNUT7nBJJHQrD9GhqdpajgXcq9Kl/9Ev0OSeZRroweuh4l/q2O45qmXMhxn/pnHZoD7z/rxYr1Ffha3t9KPsgGhB9ceTWw92oSPObLuA3U3fbBezz7/cm+V2++B1cJK6pthakInewnaUxOIc5x2+Wt08NKQkcosJScUJU= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=in-advantage.com; Received: from DM5PR1001MB2345.namprd10.prod.outlook.com (2603:10b6:4:2d::31) by BN7PR10MB2564.namprd10.prod.outlook.com (2603:10b6:406:c7::16) 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 04:15:34 +0000 Received: from DM5PR1001MB2345.namprd10.prod.outlook.com ([fe80::bcda:8606:7c7a:b1d4]) by DM5PR1001MB2345.namprd10.prod.outlook.com ([fe80::bcda:8606:7c7a:b1d4%3]) with mapi id 15.20.4951.017; Thu, 10 Feb 2022 04:15:34 +0000 From: Colin Foster To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: Jakub Kicinski , "David S. Miller" , UNGLinuxDriver@microchip.com, Alexandre Belloni , Claudiu Manoil , Vladimir Oltean , Vladimir Oltean Subject: [PATCH v6 net-next 1/5] net: mscc: ocelot: fix mutex lock error during ethtool stats read Date: Wed, 9 Feb 2022 20:13:41 -0800 Message-Id: <20220210041345.321216-2-colin.foster@in-advantage.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220210041345.321216-1-colin.foster@in-advantage.com> References: <20220210041345.321216-1-colin.foster@in-advantage.com> X-ClientProxiedBy: MW4PR04CA0084.namprd04.prod.outlook.com (2603:10b6:303:6b::29) To DM5PR1001MB2345.namprd10.prod.outlook.com (2603:10b6:4:2d::31) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4b184d77-46e9-4dcf-bdc1-08d9ec4bfbc6 X-MS-TrafficTypeDiagnostic: BN7PR10MB2564:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4714; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lm0nirG4HIOyP0crUpvJ7KsZb/k8KCQ/64J5RHIA/YCy5BN1UKrscBHo6bhEUXAx0slPl4bLId5xBmsEBpW9C1rQZ4ZHnp9rNg0EuXpDd2bpx+nTkn4nLu+VUT8MdcaEOK+rbDogEnIeAr46yQ9wMNjxanm+oY5YHJmNKj+QPE29jpSAZSYCYmCN3Wh+5N6xUrRWkA+aVUSDnU+AgDYJ7qHtYAl4FZyupEXFKHYGV1E2dOp80j0J5nszFGhOuG4CfgnW8L8sB8crhWfimqp8WveMtO9oymgMX4Pn0DiBg+f9Ywf0cj+anBLXGNdqZ+U1o9reZ/rq44tL2A8EE2wJhVNJL2u7VxofmXoJE+vQCxaz2up9X69M3/uJbRSFRuambnrrxGH+mFOEgWnXdJsg0WHFvyYPsbcuEMWwLUHjJpwz9su0hUGsQvowaTVGZA/jgjcb2YA2TLNOHJiT+5zo1+nAD5II1/toaL3jh1AmDKqy/B1o4gdORFwxfTxnY+NPmUQjd3OKllIhASLFoIMkskyz36+9yjsJx6fDFTdl6rDVPvX5nwNmdChqaEQ5zWekEFTqQ2B6yDYSl7ER20fIABUeUi8Yfm77kegDZzcivoZ/NEc3E9UOC8NudBTiG2HvEEQ6e/7dxg6+Q4GI9KA8YpQtfeGKop7TmEq/HOAl11t/5swPUUl2deqER1JQof3fi3mKIwgiRJ5ILwA0h8Ldjg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR1001MB2345.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(346002)(42606007)(366004)(39830400003)(396003)(376002)(136003)(316002)(66946007)(86362001)(508600001)(8676002)(38350700002)(8936002)(4326008)(38100700002)(2616005)(54906003)(66476007)(66556008)(5660300002)(26005)(44832011)(6486002)(6512007)(2906002)(6506007)(6666004)(83380400001)(36756003)(52116002)(186003)(1076003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: aVeDfyUUkcTkHkLUVkrijv6Nt98EHQUhUL8PjoMhRCqVxXSCP4LVAecYIWeykXn1ONT8Sc88swK/UYLGiOydF+RGCY6+KDLF8kctTii+efca2xhPCw/+pC1BZ9wNv/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3gCb4LBN4/wYGejMMjwEYoiar+vcOgcQICB8Brq1xPdm/BAlGMHRc/Z33Fvps/aCqI/GXsMLoXLadrpOP4Er7karRPjGbLKsWyT4VcYMjNs5pEhwMGuIlRIgvAi+igWtXdxVTQ8Y/J028eeYDwNhNW2qwOJaO2CXseeGQKZqg5sJeVttG6ns52W3t40v2eWgOSljb+9VylcGpUkdZDh5KPzXkYSehjbg+5bMQblkcVzzCqF6zdrtHY3li8woXMxk6F8O5BAUuLpfovj27pk/E6Op1lxJCXnZXWlgz2AinuJV4yWpoOJL0/YLmITg6J1DJ5zQP2oeG6pfMHGfXEDSqzs/S39f0ocU5tJY3JhELEX8lFOXBWHuMNNkTcCE4tvdChNeMe+wFmu0D4dufRbOwJJCoYVuNIFg4HdleeNN1a4ojbPRnVKOkBjYltemKCkZolIbYWVvSAWMdFwNveq+XzqeHQRCaVbngZa6pr3+575qQ8wtjyshYQ= X-OriginatorOrg: in-advantage.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4b184d77-46e9-4dcf-bdc1-08d9ec4bfbc6 X-MS-Exchange-CrossTenant-AuthSource: DM5PR1001MB2345.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Feb 2022 04:15:34.3100 (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: eegGckG9wKtdC4q6HJSw8wB0zEC3IPlVWSYojEtp/NQ2SgNj9CnkuDY6dNmBAv/jNVzKaApihLYyD2Hkqc5Ukqj5/KzCOhOdxQ/Y4VDk7pQ= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN7PR10MB2564 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. Signed-off-by: Colin Foster 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 455293aa6343..6933dff1dd37 100644 --- a/drivers/net/ethernet/mscc/ocelot.c +++ b/drivers/net/ethernet/mscc/ocelot.c @@ -1737,12 +1737,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); @@ -1761,8 +1760,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) @@ -1771,7 +1768,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); @@ -1781,12 +1780,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);