From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.2 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A386AC4338F for ; Thu, 5 Aug 2021 10:54:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7F96D61108 for ; Thu, 5 Aug 2021 10:54:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240554AbhHEKys (ORCPT ); Thu, 5 Aug 2021 06:54:48 -0400 Received: from foss.arm.com ([217.140.110.172]:42760 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240406AbhHEKyp (ORCPT ); Thu, 5 Aug 2021 06:54:45 -0400 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 271D41FB; Thu, 5 Aug 2021 03:54:31 -0700 (PDT) Received: from e120937-lin (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2A4973F719; Thu, 5 Aug 2021 03:54:30 -0700 (PDT) Date: Thu, 5 Aug 2021 11:54:27 +0100 From: Cristian Marussi To: Rishabh Bhatnagar Cc: sudeep.holla@arm.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, avajid@codeaurora.org, adharmap@codeaurora.org Subject: Re: [PATCH v3] firmware: arm_scmi: Free mailbox channels if probe fails Message-ID: <20210805105427.GU6592@e120937-lin> References: <1628111999-21595-1-git-send-email-rishabhb@codeaurora.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1628111999-21595-1-git-send-email-rishabhb@codeaurora.org> User-Agent: Mutt/1.9.4 (2018-02-28) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 04, 2021 at 02:19:59PM -0700, Rishabh Bhatnagar wrote: > 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); > } > Hi, > +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); > } > Looks good to me. Reviewed-by: Cristian Marussi Tested-by: Cristian Marussi (Re-tested on for-next/scmi on top of virtio-scmi series) Thanks, Cristian From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71A73C4338F for ; Thu, 5 Aug 2021 10:56:10 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 39D5660F25 for ; Thu, 5 Aug 2021 10:56:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 39D5660F25 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=WZtMer2IP1f3LeCbTekOMeQVoxDGlQvwrxuaf0BcCec=; b=NKZxmlDnDS3AaP xWhNXmElQ94pif49IfdcTZr9t+iVaUJTlKZSwxemupIUswLKpNZChOOar1NiwieEBsZXHVWE4Njbl JmQAukpnKzJVCSN9uIs8Xrc3O6OLV2gcqlIr4xTkU4smy9sfuq+CNdXi+6NdrXeOFbQS/vA8YZXxO gyTlvU26FXhR0clB4m3sTqQKlogIQt9oKKLX/dnEXT5550O5ReMR2K0pXy5g8xN5EvqgPS5zZOCO1 QeFe6idlKgl86RF5UqQqNd7cNK8k269Y3qYvGvyX5xU9x6EI7JYUFO97tIn/mqm3eD8/JMq/bNYrh tovT+ggn7IZs3cvraE7Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mBb1M-009B3k-CP; Thu, 05 Aug 2021 10:54:40 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mBb1H-009B31-Sr for linux-arm-kernel@lists.infradead.org; Thu, 05 Aug 2021 10:54:37 +0000 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 271D41FB; Thu, 5 Aug 2021 03:54:31 -0700 (PDT) Received: from e120937-lin (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2A4973F719; Thu, 5 Aug 2021 03:54:30 -0700 (PDT) Date: Thu, 5 Aug 2021 11:54:27 +0100 From: Cristian Marussi To: Rishabh Bhatnagar Cc: sudeep.holla@arm.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, avajid@codeaurora.org, adharmap@codeaurora.org Subject: Re: [PATCH v3] firmware: arm_scmi: Free mailbox channels if probe fails Message-ID: <20210805105427.GU6592@e120937-lin> References: <1628111999-21595-1-git-send-email-rishabhb@codeaurora.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1628111999-21595-1-git-send-email-rishabhb@codeaurora.org> User-Agent: Mutt/1.9.4 (2018-02-28) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210805_035436_078388_B2D6FE19 X-CRM114-Status: GOOD ( 26.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, Aug 04, 2021 at 02:19:59PM -0700, Rishabh Bhatnagar wrote: > 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); > } > Hi, > +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); > } > Looks good to me. Reviewed-by: Cristian Marussi Tested-by: Cristian Marussi (Re-tested on for-next/scmi on top of virtio-scmi series) Thanks, Cristian _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel