Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp2164495rdb; Tue, 5 Sep 2023 17:10:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEdgFb+B+ZOf5mVdZ9msxIxIbIVPhKbZhVgLwZ/y4Bk/orHSTlgaqNZgZ4mbNsaS37jVi2d X-Received: by 2002:a05:6a00:812:b0:68b:dfef:de77 with SMTP id m18-20020a056a00081200b0068bdfefde77mr14908271pfk.25.1693959052403; Tue, 05 Sep 2023 17:10:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693959052; cv=none; d=google.com; s=arc-20160816; b=FGR0vZhkK5Un4wvdjrwy+M6QL+hWCHjBapVJr9ocR7DSqOmpCyzU032rRsI6rtG1z+ rR3BVb+IxqUBtIZTp/RP38xHxx2MkTI3meGGDHA0vmoOrPr2lV/m2T9mCtJL0Sw7BGSG mp6IDk2ZzrL7HmPPaTB2sih7aq0V9Au3ve3PnNMIxTusmEfYiKjpo56eKdmHxs455wGm pEmoh+HEpk3coofUs94V1Gy4cHNSITq2sMycD/vVuNq2OtPdLwpBLzyNP0lBFmcmbAWB hqMARy+EnWYOubnzJ9fUBlRExttyl2l2bRMZEV4TbvzrDuJpeEhwkg+3KHLpYpobk2Kg E8Fw== 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 :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=b7d3ZV+ACax4ughh31YhO2JaquD1xWJ7TYZjLY8jlhQ=; fh=aOmhpQMMgxKPHGsvz3Es952ofF1ZDlafv0Gzf0qZcIw=; b=yRX+LsPhfkD6JW2+LNA8gNnwQSasiQaq8TW6+QEPEN0m68oH0kkoJf5Nm0OQa1WpSR J1UWrkSUspFKhAJqtNC8xsZZx4Rt6Ji58vFWLZFuMqgpwltWTosgyOI55CLjaGImVFng kA/QADJByfI6486A6Boz/tXNlArthwit40GhKpNRkTs1zp0LnFJWyd/6OQSl6J0JTM5W jU6t6zWU3HX4jMMb8i/vxOa12J2//YfGwSGYNG8V3ppoMuP1jhRV2BRGCx+QNOSti8lm kbfE7b/Lbkq4/QC7ipUydqwdugh7bTe+xa9AnaggRRVTN6PZs03swjXsVxZ4C6j49C9p 9ADA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=T5jnBzgv; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e18-20020a633712000000b005503f266742si686776pga.647.2023.09.05.17.10.38; Tue, 05 Sep 2023 17:10:52 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=T5jnBzgv; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239348AbjIEWPO (ORCPT + 99 others); Tue, 5 Sep 2023 18:15:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbjIEWPO (ORCPT ); Tue, 5 Sep 2023 18:15:14 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F351CF4 for ; Tue, 5 Sep 2023 15:15:10 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A97CC433CA; Tue, 5 Sep 2023 22:15:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1693952110; bh=K7DoqPi5LWEMcJo8SKG1rmXcL9ak2qlpFw7Z0l+lF3I=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=T5jnBzgvd+7vurX4i5cqqzDRUyJQboLYrKaawD0ClNWOuMD3gu6HHwwBXLJqG8vez +y1Av3v6SmQmUFuM8SZB02YzZEnGakwmvz5JTzdiYeFCCAwl08lLAZ2B4vypE3tgLu jIck7CHKmeNIT7VYPNlWm0Rci++Y8cT+2y0UwDL3TR8OpP2J/V0hITgFGi2JdRVHBS vyLc2kReEzNEhYC3kiueTh933NcsiAyP+TxZhrqGquh308ptCuGhrjLPx3diruuqnh oGTRTQjwG/TApZKRl4knfH7TD0JFcZw7oy+L8+Hi3SOwhgybpkBK2qu75DqH0Wmbve JMI8+6PfGFZYQ== Date: Tue, 5 Sep 2023 15:15:09 -0700 From: Jakub Kicinski To: Parthiban Veerasooran Cc: , , , , , , , , , Subject: Re: [PATCH net-next] ethtool: plca: fix plca enable data type while parsing the value Message-ID: <20230905151509.5208ee63@kernel.org> In-Reply-To: <20230831104523.7178-1-Parthiban.Veerasooran@microchip.com> References: <20230831104523.7178-1-Parthiban.Veerasooran@microchip.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 On Thu, 31 Aug 2023 16:15:23 +0530 Parthiban Veerasooran wrote: > Subject: [PATCH net-next] ethtool: plca: fix plca enable data type while parsing the value Since this is a fix the subject should say net instead of net-next. [PATCH net v2] for the next revision. > The ETHTOOL_A_PLCA_ENABLED data type is u8. But while parsing the > value from the attribute, nla_get_u32() is used in the plca_update_sint() > function instead of nla_get_u8(). So plca_cfg.enabled variable is updated > with some garbage value instead of 0 or 1 and always enables plca even > though plca is disabled through ethtool application. This bug has been > fixed by implementing plca_update_sint_from_u8() function which uses > nla_get_u8() function to extract the plca_cfg.enabled value and the > function plca_update_sint_from_u32() is used for extracting the other > values using nla_get_u32() function. Hm, yes, that seems like the best of the available options. > Fixes: 8580e16c28f3 ("net/ethtool: add netlink interface for the PLCA RS") Please make sure you CC all the people who signed off the commit under Fixes. You missed pabeni@redhat.com and andrew@lunn.ch > Signed-off-by: Parthiban Veerasooran > --- > net/ethtool/plca.c | 34 ++++++++++++++++++++++++---------- > 1 file changed, 24 insertions(+), 10 deletions(-) > > diff --git a/net/ethtool/plca.c b/net/ethtool/plca.c > index b238a1afe9ae..b4e80dd33590 100644 > --- a/net/ethtool/plca.c > +++ b/net/ethtool/plca.c > @@ -21,8 +21,8 @@ struct plca_reply_data { > #define PLCA_REPDATA(__reply_base) \ > container_of(__reply_base, struct plca_reply_data, base) > > -static void plca_update_sint(int *dst, const struct nlattr *attr, > - bool *mod) > +static void plca_update_sint_from_u32(int *dst, const struct nlattr *attr, > + bool *mod) > { > if (!attr) > return; > @@ -31,6 +31,16 @@ static void plca_update_sint(int *dst, const struct nlattr *attr, > *mod = true; > } > > +static void plca_update_sint_from_u8(int *dst, const struct nlattr *attr, > + bool *mod) > +{ > + if (!attr) > + return; > + > + *dst = nla_get_u8(attr); > + *mod = true; > +} > + > // PLCA get configuration message ------------------------------------------- // > > const struct nla_policy ethnl_plca_get_cfg_policy[] = { > @@ -144,14 +154,18 @@ ethnl_set_plca(struct ethnl_req_info *req_info, struct genl_info *info) > return -EOPNOTSUPP; > > memset(&plca_cfg, 0xff, sizeof(plca_cfg)); > - plca_update_sint(&plca_cfg.enabled, tb[ETHTOOL_A_PLCA_ENABLED], &mod); > - plca_update_sint(&plca_cfg.node_id, tb[ETHTOOL_A_PLCA_NODE_ID], &mod); > - plca_update_sint(&plca_cfg.node_cnt, tb[ETHTOOL_A_PLCA_NODE_CNT], &mod); > - plca_update_sint(&plca_cfg.to_tmr, tb[ETHTOOL_A_PLCA_TO_TMR], &mod); > - plca_update_sint(&plca_cfg.burst_cnt, tb[ETHTOOL_A_PLCA_BURST_CNT], > - &mod); > - plca_update_sint(&plca_cfg.burst_tmr, tb[ETHTOOL_A_PLCA_BURST_TMR], > - &mod); > + plca_update_sint_from_u8(&plca_cfg.enabled, tb[ETHTOOL_A_PLCA_ENABLED], > + &mod); > + plca_update_sint_from_u32(&plca_cfg.node_id, tb[ETHTOOL_A_PLCA_NODE_ID], > + &mod); > + plca_update_sint_from_u32(&plca_cfg.node_cnt, > + tb[ETHTOOL_A_PLCA_NODE_CNT], &mod); > + plca_update_sint_from_u32(&plca_cfg.to_tmr, tb[ETHTOOL_A_PLCA_TO_TMR], > + &mod); > + plca_update_sint_from_u32(&plca_cfg.burst_cnt, > + tb[ETHTOOL_A_PLCA_BURST_CNT], &mod); > + plca_update_sint_from_u32(&plca_cfg.burst_tmr, > + tb[ETHTOOL_A_PLCA_BURST_TMR], &mod); This looks error prone. We still need to maintain the types in multiple places. How about we use the policy to decide the type? Something along the lines of (untested): diff --git a/net/ethtool/plca.c b/net/ethtool/plca.c index b238a1afe9ae..aed30665e9c0 100644 --- a/net/ethtool/plca.c +++ b/net/ethtool/plca.c @@ -21,16 +21,6 @@ struct plca_reply_data { #define PLCA_REPDATA(__reply_base) \ container_of(__reply_base, struct plca_reply_data, base) -static void plca_update_sint(int *dst, const struct nlattr *attr, - bool *mod) -{ - if (!attr) - return; - - *dst = nla_get_u32(attr); - *mod = true; -} - // PLCA get configuration message ------------------------------------------- // const struct nla_policy ethnl_plca_get_cfg_policy[] = { @@ -125,6 +115,29 @@ const struct nla_policy ethnl_plca_set_cfg_policy[] = { [ETHTOOL_A_PLCA_BURST_TMR] = NLA_POLICY_MAX(NLA_U32, 255), }; +static void +plca_update_sint(int *dst, const struct nlattr **tb, u32 attrid, bool *mod) +{ + const struct nlattr *attr = tb[attrid]; + + if (!attr || + WARN_ON_ONCE(attrid >= ARRAY_SIZE(ethnl_plca_set_cfg_policy))) + return; + + switch (ethnl_plca_set_cfg_policy[attrid].type) { + case NLA_U8: + *dst = nla_get_u8(attr); + break; + case NLA_U32: + *dst = nla_get_u32(attr); + break; + default: + WARN_ON_ONCE(1); + } + + *mod = true; +} + static int ethnl_set_plca(struct ethnl_req_info *req_info, struct genl_info *info) { @@ -144,13 +157,13 @@ ethnl_set_plca(struct ethnl_req_info *req_info, struct genl_info *info) return -EOPNOTSUPP; memset(&plca_cfg, 0xff, sizeof(plca_cfg)); - plca_update_sint(&plca_cfg.enabled, tb[ETHTOOL_A_PLCA_ENABLED], &mod); - plca_update_sint(&plca_cfg.node_id, tb[ETHTOOL_A_PLCA_NODE_ID], &mod); - plca_update_sint(&plca_cfg.node_cnt, tb[ETHTOOL_A_PLCA_NODE_CNT], &mod); - plca_update_sint(&plca_cfg.to_tmr, tb[ETHTOOL_A_PLCA_TO_TMR], &mod); - plca_update_sint(&plca_cfg.burst_cnt, tb[ETHTOOL_A_PLCA_BURST_CNT], + plca_update_sint(&plca_cfg.enabled, tb, ETHTOOL_A_PLCA_ENABLED, &mod); + plca_update_sint(&plca_cfg.node_id, tb, ETHTOOL_A_PLCA_NODE_ID, &mod); + plca_update_sint(&plca_cfg.node_cnt, tb, ETHTOOL_A_PLCA_NODE_CNT, &mod); + plca_update_sint(&plca_cfg.to_tmr, tb, ETHTOOL_A_PLCA_TO_TMR, &mod); + plca_update_sint(&plca_cfg.burst_cnt, tb, ETHTOOL_A_PLCA_BURST_CNT, &mod); - plca_update_sint(&plca_cfg.burst_tmr, tb[ETHTOOL_A_PLCA_BURST_TMR], + plca_update_sint(&plca_cfg.burst_tmr, tb, ETHTOOL_A_PLCA_BURST_TMR, &mod); if (!mod) return 0;