Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp189962pxt; Wed, 4 Aug 2021 19:51:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzAbwXamwzOsNebmT8M6zcu/dci4IWfN0RpO0T9VXwlX2EW6UGf1WqTtQwdWbR6/fgoCyUo X-Received: by 2002:a5d:8484:: with SMTP id t4mr388199iom.126.1628131901569; Wed, 04 Aug 2021 19:51:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628131901; cv=none; d=google.com; s=arc-20160816; b=xg3827THuUmu0q2PYOBaXn1EFQVr3Cu2lxrp7jHk//kTpwb2HCC6SuhEXIaRqEVgaM NecEpQyvdQbfYbgk4BsZcssrkQNYquySbJdLM1B1QIoRjtBWBIrZiysUwZdUUueEmyG1 /TkFi3cr7KkUiRI78tZoE88LOG+9YAB4W2IOfr4r1YVt+NxMJiaHr3oS1MgdC3F+wLFV Pp8a6/ilqNGAUHjB+c6dlCkaerhR+5/kSMH5woQwo0UKIxo5gRmxWFaO+BefZtFL0D9F UlPAHTVrGG2hy5AH+sBM8fwEiZq2k2spZwzGBLmY6ihg71cHkd3lhLvCgQrj/v5hzRzC TxRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from:dmarc-filter :sender:dkim-signature; bh=uCGICzJv+N4Cscfb4VHB7mulhy2uYSQDb+HMunR6BxM=; b=sJxjCwDbJxtJUSbKZ+YhtI30hv2K0rqEzc2x2KzFnQvgde8vNm3qiRJRtxHsAvJrum /ysQOnCZe6BAMKWzRYTHw78/NlRWHnxcY8NJoqqcnTBo0faN0zo4ebNP4xxNtV9UERPa o95lx7G0+qH9JyiF1mlqf0zK7PzscDZ4w3T7rmhax8P2ZBsM2gxHyyGwKxBapvgvNXZP +QJV9UxK+hayVstAQ7UPjQ4fwACNr79zyDl1yuCgEx1Se9IL2RWElp3unUhPwpDzM9iD oQvEtwTdgRVTM5IJzFjtgPgi8k6kbhL233YfT6WoxSHLYa2Z/+brnH1WthkhYMGj7cdb hJzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=XIK5OQYM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v8si4203928jas.68.2021.08.04.19.50.56; Wed, 04 Aug 2021 19:51:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=XIK5OQYM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230209AbhHDVUa (ORCPT + 99 others); Wed, 4 Aug 2021 17:20:30 -0400 Received: from m43-7.mailgun.net ([69.72.43.7]:54646 "EHLO m43-7.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230014AbhHDVU3 (ORCPT ); Wed, 4 Aug 2021 17:20:29 -0400 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1628112016; h=Message-Id: Date: Subject: Cc: To: From: Sender; bh=uCGICzJv+N4Cscfb4VHB7mulhy2uYSQDb+HMunR6BxM=; b=XIK5OQYMw71APGXxJOxI66ANO93SDOgAp0P1zh72nBRL0Eo+dq+1imKx9Xlyf6fguYtD/Dlk mPjNriCbZjel3DkIRAgX5hToxARy/yF8QreMKFmUppRDkCbjjjfjn2/Sb7uaWKYLdWJgtfTY mA/r9ajP6IAAwm5xB4XBjLhrAKA= X-Mailgun-Sending-Ip: 69.72.43.7 X-Mailgun-Sid: WyI0MWYwYSIsICJsaW51eC1rZXJuZWxAdmdlci5rZXJuZWwub3JnIiwgImJlOWU0YSJd Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by smtp-out-n06.prod.us-east-1.postgun.com with SMTP id 610b0487041a739c46c1a6ae (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Wed, 04 Aug 2021 21:20:07 GMT Sender: rishabhb=codeaurora.org@mg.codeaurora.org Received: by smtp.codeaurora.org (Postfix, from userid 1001) id C823AC4338A; Wed, 4 Aug 2021 21:20:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=ALL_TRUSTED,BAYES_00,SPF_FAIL, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from rishabhb-linux.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: rishabhb) by smtp.codeaurora.org (Postfix) with ESMTPSA id 78CBDC433D3; Wed, 4 Aug 2021 21:20:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 78CBDC433D3 Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; spf=fail smtp.mailfrom=rishabhb@codeaurora.org From: Rishabh Bhatnagar To: sudeep.holla@arm.com, cristian.marussi@arm.com Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, avajid@codeaurora.org, adharmap@codeaurora.org, Rishabh Bhatnagar Subject: [PATCH v3] firmware: arm_scmi: Free mailbox channels if probe fails Date: Wed, 4 Aug 2021 14:19:59 -0700 Message-Id: <1628111999-21595-1-git-send-email-rishabhb@codeaurora.org> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Mailbox channels for the base protocol are setup during probe. There can be a scenario where probe fails to acquire the base protocol due to a timeout leading to cleaning up of all device managed memory including the scmi_mailbox structure setup during mailbox_chan_setup function. [ 12.735104]arm-scmi soc:qcom,scmi: timed out in resp(caller: version_get+0x84/0x140) [ 12.735224]arm-scmi soc:qcom,scmi: unable to communicate with SCMI [ 12.735947]arm-scmi: probe of soc:qcom,scmi failed with error -110 Now when a message arrives at cpu slightly after the timeout, the mailbox controller will try to call the rx_callback of the client and might end up accessing freed memory. [ 12.758363][ C0] Call trace: [ 12.758367][ C0] rx_callback+0x24/0x160 [ 12.758372][ C0] mbox_chan_received_data+0x44/0x94 [ 12.758386][ C0] __handle_irq_event_percpu+0xd4/0x240 This patch frees the mailbox channels setup during probe and adds some more error handling in case the probe fails. Signed-off-by: Rishabh Bhatnagar --- drivers/firmware/arm_scmi/driver.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c index 9b2e8d4..ead3bd3 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -1390,6 +1390,21 @@ void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table) mutex_unlock(&scmi_requested_devices_mtx); } +static int scmi_cleanup_txrx_channels(struct scmi_info *info) +{ + int ret; + struct idr *idr = &info->tx_idr; + + ret = idr_for_each(idr, info->desc->ops->chan_free, idr); + idr_destroy(&info->tx_idr); + + idr = &info->rx_idr; + ret = idr_for_each(idr, info->desc->ops->chan_free, idr); + idr_destroy(&info->rx_idr); + + return ret; +} + static int scmi_probe(struct platform_device *pdev) { int ret; @@ -1430,7 +1445,7 @@ static int scmi_probe(struct platform_device *pdev) ret = scmi_xfer_info_init(info); if (ret) - return ret; + goto clear_txrx_setup; if (scmi_notification_init(handle)) dev_err(dev, "SCMI Notifications NOT available.\n"); @@ -1443,7 +1458,7 @@ static int scmi_probe(struct platform_device *pdev) ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE); if (ret) { dev_err(dev, "unable to communicate with SCMI\n"); - return ret; + goto notification_exit; } mutex_lock(&scmi_list_mutex); @@ -1482,6 +1497,12 @@ static int scmi_probe(struct platform_device *pdev) } return 0; + +notification_exit: + scmi_notification_exit(&info->handle); +clear_txrx_setup: + scmi_cleanup_txrx_channels(info); + return ret; } void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id) @@ -1493,7 +1514,6 @@ static int scmi_remove(struct platform_device *pdev) { int ret = 0, id; struct scmi_info *info = platform_get_drvdata(pdev); - struct idr *idr = &info->tx_idr; struct device_node *child; mutex_lock(&scmi_list_mutex); @@ -1517,14 +1537,7 @@ static int scmi_remove(struct platform_device *pdev) idr_destroy(&info->active_protocols); /* Safe to free channels since no more users */ - ret = idr_for_each(idr, info->desc->ops->chan_free, idr); - idr_destroy(&info->tx_idr); - - idr = &info->rx_idr; - ret = idr_for_each(idr, info->desc->ops->chan_free, idr); - idr_destroy(&info->rx_idr); - - return ret; + return scmi_cleanup_txrx_channels(info); } static ssize_t protocol_version_show(struct device *dev, -- The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project