Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp5736080pxb; Mon, 14 Feb 2022 06:29:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJyXW3jH06WSxcATYEYMWvDdHlH068+GrXCjIYjETN054GCen2guIQ8W99BFlwD8+6ocmime X-Received: by 2002:a05:6402:90b:: with SMTP id g11mr15442227edz.73.1644848997905; Mon, 14 Feb 2022 06:29:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644848997; cv=none; d=google.com; s=arc-20160816; b=Xf2Mucz5tcApdQLmxfp5uY2DnnMvrneS3iP+ogf9R8uO7Str5L+kj+DZs5TlrLSCVb X3+tMxWK3BN7UKgE76WZgnmLPMsq1I/kL9weecvhdpbPIgHgt5Df0FJSTBgOxay4MLgF aVbVk8hKEAhX7CnafoV6hvqHjEH2np2/G75SpZ2FyTZsAY3w5h3qDb8M/ECzfd8LzEGt h4ZBNTjTXZikXrEOSq+mASoduTxHqy7zvJbRWiftePP7u7bqkrRAmh8nS3rbkSX8jNeO lWEdbteI5Zu6mWUSylNYDH0jkjUzKEP8YWi+j0LpDcjKVrxmJ/AyvQ34qgCTgVETzSGc pPlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=xAaVKN1IY9EGvT9xKECTyX+Ky1Wwa57l7EKaQ+/aQUs=; b=h/oHUqItXdtNfX8AL9SAFuyinnxxazgCRdrI/DeeVJ7XSDzSxOROGQr/3AwLHW1mmn UFky2MjO/Hnx/u8rldUWDu6QFgR/SNachaArsPBOwqFzw2pyrfQMRQg72FBInpGfHYhM 0kvFya24jg3X63SvK7WG2NMCSDcG+lhWbs2tLS7ZJ5tntT6J26eUneXMYasF2fv2nVgd ID8MyQ+nYYmEIuIG/OxQwp6MrGx5aHHHxQHT/qR9FIlkT6+j8/2yu+3RWZZDLH4vhcF/ GMBzWU7NNcSi76B97mbi2bEMuUB+PNDC9gTN8IOp8NWoUZjfHVeEtMKHYhv8hhvmm2PQ rW3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LdKsReFV; 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=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dd20si8592003ejc.943.2022.02.14.06.29.31; Mon, 14 Feb 2022 06:29:57 -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=@linuxfoundation.org header.s=korg header.b=LdKsReFV; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344157AbiBNJz6 (ORCPT + 99 others); Mon, 14 Feb 2022 04:55:58 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:33318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344151AbiBNJvd (ORCPT ); Mon, 14 Feb 2022 04:51:33 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1C6377A8E; Mon, 14 Feb 2022 01:42:32 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8ED34611F9; Mon, 14 Feb 2022 09:42:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 732FAC340E9; Mon, 14 Feb 2022 09:42:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1644831752; bh=0v9TIcwA/Vbn20Rlsqb/6ZLsZbXcSZQqHmpK8wJkvJY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LdKsReFVJl3zuN3WsUOdsh93KzRD2M+NUOMqGHvKZ430WRRa4bpcrqV5iPK4jMFqf S5wEepyZ1sTQXsUiFbkqY8UdSdJTFxCYdkNibUBUxw2sslTtFdlXjtg93kaavRwWFD J5H9T+Q1y1UmOad+j2likw/nJ9L11pS8mba7w4Pw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Colin Foster , Vladimir Oltean , Jakub Kicinski , Sasha Levin Subject: [PATCH 5.10 085/116] net: mscc: ocelot: fix mutex lock error during ethtool stats read Date: Mon, 14 Feb 2022 10:26:24 +0100 Message-Id: <20220214092501.704177712@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220214092458.668376521@linuxfoundation.org> References: <20220214092458.668376521@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,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 From: Colin Foster [ Upstream commit 7fbf6795d127a3b1bb39b0e42579904cf6db1624 ] 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 Link: https://lore.kernel.org/all/20220210150451.416845-2-colin.foster@in-advantage.com/ Signed-off-by: Jakub Kicinski Signed-off-by: Sasha Levin --- 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 52401915828a1..a06466ecca12a 100644 --- a/drivers/net/ethernet/mscc/ocelot.c +++ b/drivers/net/ethernet/mscc/ocelot.c @@ -848,12 +848,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); @@ -872,8 +871,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) @@ -882,7 +879,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); @@ -892,12 +891,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.34.1