From nobody Wed Nov 27 15:36:41 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AB02C127E18; Wed, 9 Oct 2024 14:39:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728484772; cv=none; b=fOWaSPVO9gFW4QRRC8kdVmB2t3wi6eW0JkF7qCQzMCjzrLFzvSIgbFP/NUbwbBfCIrWiSTjOW2GZb//GKYQonylobZ7dotTkDJwnKdzl6kr/XhU0qoQt7gGjpZmic5Eq794GMYcFpE9j9PZxMVCbOGbyVIBT6C7B+BrOpNTz+0Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728484772; c=relaxed/simple; bh=E2Q/Z9HmKJzZCPdr6+3P8OqkCoRO4OHda0l6tZENExs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=pZGmbs7ysrua32L2QgphZxoSGlrtwlE5B9wZ6llmgquE8HOHjNFkFBIKI0ZEKO+mW5jZ52Iwe9D78n46aiFw21vSeFqVgVrYCMm4RWK38dbpIwplFIA8u/SZAXEWLwRz1BArf6HeWers8EpKUSqAS9aKWdAtl4CDTwzw049fsaI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9CB79FEC; Wed, 9 Oct 2024 07:39:59 -0700 (PDT) Received: from pluto.guestnet.cambridge.arm.com (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 94B053F58B; Wed, 9 Oct 2024 07:39:28 -0700 (PDT) From: Cristian Marussi To: linux-kernel@vger.kernel.org, arm-scmi@vger.kernel.org Cc: sudeep.holla@arm.com, cristian.marussi@arm.com, quic_sibis@quicinc.com, johan@kernel.org, konradybcio@kernel.org, johan+linaro@kernel.org Subject: [PATCH] firmware: arm_scmi: Skip opp duplicates Date: Wed, 9 Oct 2024 15:39:05 +0100 Message-ID: <20241009143905.2440438-1-cristian.marussi@arm.com> X-Mailer: git-send-email 2.46.1 Reply-To: 20241007060642.1978049-3-quic_sibis@quicinc.com Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Buggy firmware can reply with duplicated PERF opps descriptors. Ensure that the bad duplicates reported by the platform firmware doesn't get added to the opp-tables. Reported-by: Johan Hovold Closes: https://lore.kernel.org/lkml/ZoQjAWse2YxwyRJv@hovoldconsulting.com/ Signed-off-by: Cristian Marussi Reviewed-by: Sudeep Holla Tested-by: Johan Hovold --- A new version to include in this series that should address the limit case described by Sibi...not tested, of course :P --- drivers/firmware/arm_scmi/perf.c | 40 ++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/drivers/firmware/arm_scmi/perf.c b/drivers/firmware/arm_scmi/p= erf.c index 2d77b5f40ca7..32f9a9acd3e9 100644 --- a/drivers/firmware/arm_scmi/perf.c +++ b/drivers/firmware/arm_scmi/perf.c @@ -373,7 +373,7 @@ static int iter_perf_levels_update_state(struct scmi_it= erator_state *st, return 0; } =20 -static inline void +static inline int process_response_opp(struct device *dev, struct perf_dom_info *dom, struct scmi_opp *opp, unsigned int loop_idx, const struct scmi_msg_resp_perf_describe_levels *r) @@ -386,12 +386,16 @@ process_response_opp(struct device *dev, struct perf_= dom_info *dom, le16_to_cpu(r->opp[loop_idx].transition_latency_us); =20 ret =3D xa_insert(&dom->opps_by_lvl, opp->perf, opp, GFP_KERNEL); - if (ret) + if (ret) { dev_warn(dev, "Failed to add opps_by_lvl at %d for %s - ret:%d\n", opp->perf, dom->info.name, ret); + return ret; + } + + return 0; } =20 -static inline void +static inline int process_response_opp_v4(struct device *dev, struct perf_dom_info *dom, struct scmi_opp *opp, unsigned int loop_idx, const struct scmi_msg_resp_perf_describe_levels_v4 *r) @@ -404,9 +408,11 @@ process_response_opp_v4(struct device *dev, struct per= f_dom_info *dom, le16_to_cpu(r->opp[loop_idx].transition_latency_us); =20 ret =3D xa_insert(&dom->opps_by_lvl, opp->perf, opp, GFP_KERNEL); - if (ret) + if (ret) { dev_warn(dev, "Failed to add opps_by_lvl at %d for %s - ret:%d\n", opp->perf, dom->info.name, ret); + return ret; + } =20 /* Note that PERF v4 reports always five 32-bit words */ opp->indicative_freq =3D le32_to_cpu(r->opp[loop_idx].indicative_freq); @@ -415,13 +421,21 @@ process_response_opp_v4(struct device *dev, struct pe= rf_dom_info *dom, =20 ret =3D xa_insert(&dom->opps_by_idx, opp->level_index, opp, GFP_KERNEL); - if (ret) + if (ret) { dev_warn(dev, "Failed to add opps_by_idx at %d for %s - ret:%d\n", opp->level_index, dom->info.name, ret); =20 + /* Cleanup by_lvl too */ + xa_erase(&dom->opps_by_lvl, opp->perf); + + return ret; + } + hash_add(dom->opps_by_freq, &opp->hash, opp->indicative_freq); } + + return 0; } =20 static int @@ -429,16 +443,22 @@ iter_perf_levels_process_response(const struct scmi_p= rotocol_handle *ph, const void *response, struct scmi_iterator_state *st, void *priv) { + int ret; struct scmi_opp *opp; struct scmi_perf_ipriv *p =3D priv; =20 - opp =3D &p->perf_dom->opp[st->desc_index + st->loop_idx]; + opp =3D &p->perf_dom->opp[p->perf_dom->opp_count]; if (PROTOCOL_REV_MAJOR(p->version) <=3D 0x3) - process_response_opp(ph->dev, p->perf_dom, opp, st->loop_idx, - response); + ret =3D process_response_opp(ph->dev, p->perf_dom, opp, + st->loop_idx, response); else - process_response_opp_v4(ph->dev, p->perf_dom, opp, st->loop_idx, - response); + ret =3D process_response_opp_v4(ph->dev, p->perf_dom, opp, + st->loop_idx, response); + + /* Skip BAD duplicates received from firmware */ + if (ret) + return ret =3D=3D -EBUSY ? 0 : ret; + p->perf_dom->opp_count++; =20 dev_dbg(ph->dev, "Level %d Power %d Latency %dus Ifreq %d Index %d\n", --=20 2.46.1