Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2546078pxb; Wed, 9 Feb 2022 23:21:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJzf3+w5D8SxlwuoEq/cHl7KHORMbYgeXJuXT+WkJR33aIcMzWnpdrHz83V9Sf6loPdzo5Sw X-Received: by 2002:a17:907:6293:: with SMTP id nd19mr5238042ejc.64.1644477672088; Wed, 09 Feb 2022 23:21:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1644477672; cv=pass; d=google.com; s=arc-20160816; b=XIYZFJUWWJzlhjX3OZ1uyfMmbB6JYSzGcicfa5IRqhD4LZGyWbbspYuR1X5TNQhwz9 jaY3vvrAVaL9z1mB0hYpcUyEr44dNZrlC3xq9e0pikLJPrhgL3608ghqCb/k2pq87aG9 0It/zVs1qi+SOEw7UXtuPbEaJmSDqSvV4GRcMozqdphPgWuViEVhet5ylWgRNdBkyjnj 4Hwg/GLQFDBZH0VjrmqoMKAjLvaprfUSW4RVlL7AwVenZILJrGwL1tHIZLT/JU2K51QI nFJ+y5SJUoJzk8sd2EZ84kWl6I8RhrWdmgFVi2VKoErlwWhpkL1+MuaI8x1dqoqsKPWV hcfQ== 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=4aG2oIUQ/mI7G40At3g+APWfeyXJe5mF7JaV+F+KQ6s=; b=tDT1VZEtCP/TgdNMFtNzgPdSjjISXyq8Vk/KUeknR99lQa+fnwlxS3HUHT3LE21Bfc R5QFzZSVCRn/8EqrshgO2/mSJMrDDvVQNsCMYY6Wh9F7nEvdVuCS1RWvEwus+ydRIHQg 3CWCa3ktNkni7TdCO4pj/ugWM9Vhw+/oOCrh2LMOzGKv+ie8KXZ7j+LNCf71AfLWg1R2 V9l4kcTHzOmcxak45o7XkEzF4ACq+FgFQyZte2ChnIhcfCiH1eeXi2V/lhpIQRqKuyEk 534mnRrfVCQUuRDc0epaV4YSqrtUCgBVsnt6LsVxLg2Dd+MDgEocbXw6MTcfVRQ9Ak4j 29CA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@inadvantage.onmicrosoft.com header.s=selector2-inadvantage-onmicrosoft-com header.b=NsxGzcsN; 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=fail (p=NONE sp=NONE dis=NONE) header.from=in-advantage.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r10si2034536edd.608.2022.02.09.23.20.37; Wed, 09 Feb 2022 23:21:12 -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=@inadvantage.onmicrosoft.com header.s=selector2-inadvantage-onmicrosoft-com header.b=NsxGzcsN; 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=fail (p=NONE sp=NONE dis=NONE) header.from=in-advantage.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233218AbiBJEPq (ORCPT + 99 others); Wed, 9 Feb 2022 23:15:46 -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> Content-Transfer-Encoding: 8bit Content-Type: text/plain 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: =?us-ascii?Q?aVeDfyUUkcTkHkLUVkrijv6Nt98EHQUhUL8PjoMhRCqVxXSCP4LVAecYIWey?= =?us-ascii?Q?kXn1ONT8Sc88swK/UYLGiOydF+RGCY6+KDLF8kctTii+efca2xhPCw/+pC1B?= =?us-ascii?Q?Z9wNv/onzfYtQevmEXLKuzmLy0v5gJrVlI3Fpz95sygxJRVLx+6NgpU0ll3C?= =?us-ascii?Q?Bju8tK05Bz2fxU5NzK+Iu46AjAPP9Ae06eSusUtLtAcuKpHXG2DgHhV9JLHF?= =?us-ascii?Q?lZ72FcHQWTQF0wF1wQUlYNbR+s1uBozRmUnChrx/bsh0haQuOoMqLvqVNVqA?= =?us-ascii?Q?BB/Z3NNHh94UEqtSM1aJsQsHkWJoFRNo4l+URazDWTNY7aE00wwvz5IyuJJg?= =?us-ascii?Q?ucO9t1zVEzMkhRGi9D4u25VUQMM6tyc2iMGple5kuom5bhh9vsUOYA08yCxH?= =?us-ascii?Q?hxLs5LBdL2OtPO3awwHM6bP6Pg9mJ4rWjbBLwr+yqRAj07n41gX+WEwMRmZ/?= =?us-ascii?Q?bkMdOeqL/SBVUbxDebfMgN2EI7kXBLIw5JJvw4FVAb1t6UtKZk5bmgd50wHe?= =?us-ascii?Q?5usjSMf3coVob6ZSz2nOv7A3lRslbteibfGARTwszwwWjJuk0dynQFit0CG1?= =?us-ascii?Q?lAXCLa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ZXWlgz2A?= =?us-ascii?Q?inuJV4yWpoOJL0/YLmITg6J1DJ5zQP2oeG6pfMHGfXEDSqzs/S39f0ocU5tJ?= =?us-ascii?Q?Y3JhELEX8lFOXBWHuMNNkTcCE4tvdChNeMe+wFmu0D4dufRbOwJJCoYVuNIF?= =?us-ascii?Q?g4HdleeNN1a4ojbPRnVKOkBjYltemKCkZolIbYWVvSAWMdFwNveq+XzqeHQR?= =?us-ascii?Q?CaVbngZa6pr3+575qQ8wtjyshYQ=3D?= 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 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE 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 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 --- 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); -- 2.25.1