From patchwork Thu May 9 10:55:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Sperl X-Patchwork-Id: 10936985 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C838C1390 for ; Thu, 9 May 2019 10:55:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B476E2874A for ; Thu, 9 May 2019 10:55:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A518E287C2; Thu, 9 May 2019 10:55:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 426752874A for ; Thu, 9 May 2019 10:55:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726415AbfEIKzz (ORCPT ); Thu, 9 May 2019 06:55:55 -0400 Received: from 212-186-180-163.static.upcbusiness.at ([212.186.180.163]:36350 "EHLO cgate.sperl.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725892AbfEIKzy (ORCPT ); Thu, 9 May 2019 06:55:54 -0400 Received: from hc1.intern.sperl.org (account martin@sperl.org [10.10.10.59] verified) by sperl.org (CommuniGate Pro SMTP 6.2.1 _community_) with ESMTPSA id 7764482; Thu, 09 May 2019 10:55:46 +0000 From: kernel@martin.sperl.org To: Nicolas Saenz Julienne , Meghana Madhyastha , =?utf-8?q?Noralf_Tr?= =?utf-8?q?=C3=B8nnes?= , Mark Brown , Stefan Wahren , linux-spi@vger.kernel.org Cc: Martin Sperl Subject: [PATCH V1 1/3] spi: core: resource: fix memory leak on error and place in "correct" sequence Date: Thu, 9 May 2019 10:55:31 +0000 Message-Id: <20190509105533.24275-2-kernel@martin.sperl.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190509105533.24275-1-kernel@martin.sperl.org> References: <20190509105533.24275-1-kernel@martin.sperl.org> Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Martin Sperl When an error occurs in ctlr->prepare_message or spi_map_msg then spi_resources is not cleared leaving unexpected entries and memory. Also there is an ordering issue: On ititialization: * prepare_message * spi_map_msg and when releasing: * spi_res_release (outside of finalize) -> this restores the spi structures * spi_unmap_msg * unprepare_message So the ordering is wrong in the case that prepare_message is modifying the spi_message and spi_message.resources. Especially the dma unmapping of all the translations are not done. There is still an ambiguity where is the "best" place where to place spi_res_release - it definitely has to be after spi_unmap_msg, but if it should be before or after unprepare message is not well defined. Ideally this dma unmap and unprepare_message would be executed by spi_res_release and thus in the guaranteed order they were applied. This incidently implements a better way for the reverted commit c9ba7a16d0f1 ("spi: Release spi_res after finalizing message") Signed-off-by: Martin Sperl Tested-by: Nicolas Saenz Julienne Tested-by: Noralf Trønnes --- drivers/spi/spi.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) -- 2.11.0 diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 8eb7460dd744..1dfb19140bbe 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1181,8 +1181,6 @@ static int spi_transfer_one_message(struct spi_controller *ctlr, if (msg->status && ctlr->handle_err) ctlr->handle_err(ctlr, msg); - spi_res_release(ctlr, msg); - spi_finalize_current_message(ctlr); return ret; @@ -1448,6 +1446,15 @@ void spi_finalize_current_message(struct spi_controller *ctlr) } } + /* where to put the release is a slight nightmare because + * ctlr->prepare_message may add to resources as well. + * so the question is: call it before unprepare or after? + * for now leave it after - the asumption here is that + * if prepare_message is using spi_res for callbacks, + * then no unprepare_message is used + */ + spi_res_release(ctlr, mesg); + spin_lock_irqsave(&ctlr->queue_lock, flags); ctlr->cur_msg = NULL; ctlr->cur_msg_prepared = false; From patchwork Thu May 9 10:55:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sperl X-Patchwork-Id: 10936987 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 372101390 for ; Thu, 9 May 2019 10:55:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 269AE2874A for ; Thu, 9 May 2019 10:55:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 19786287C2; Thu, 9 May 2019 10:55:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B99BA2874A for ; Thu, 9 May 2019 10:55:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726426AbfEIKz4 (ORCPT ); Thu, 9 May 2019 06:55:56 -0400 Received: from 212-186-180-163.static.upcbusiness.at ([212.186.180.163]:36350 "EHLO cgate.sperl.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725892AbfEIKz4 (ORCPT ); Thu, 9 May 2019 06:55:56 -0400 Received: from hc1.intern.sperl.org (account martin@sperl.org [10.10.10.59] verified) by sperl.org (CommuniGate Pro SMTP 6.2.1 _community_) with ESMTPSA id 7764483; Thu, 09 May 2019 10:55:46 +0000 From: kernel@martin.sperl.org To: Nicolas Saenz Julienne , Meghana Madhyastha , =?utf-8?q?Noralf_Tr?= =?utf-8?q?=C3=B8nnes?= , Mark Brown , Stefan Wahren , linux-spi@vger.kernel.org Cc: Martin Sperl Subject: [PATCH V1 2/3] spi: core: add a warning when prepare_message uses spi_res and unprepare_message Date: Thu, 9 May 2019 10:55:32 +0000 Message-Id: <20190509105533.24275-3-kernel@martin.sperl.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190509105533.24275-1-kernel@martin.sperl.org> References: <20190509105533.24275-1-kernel@martin.sperl.org> Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Martin Sperl Add a warning about a potential cleanup inconsistency when both prepare_message and unprepare_message are defined by the bus driver and prepare_message is using spi_res_add. Signed-off-by: Martin Sperl --- drivers/spi/spi.c | 6 ++++++ 1 file changed, 6 insertions(+) -- 2.11.0 diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 1dfb19140bbe..78fe46bf6f41 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1317,6 +1317,7 @@ static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) trace_spi_message_start(ctlr->cur_msg); if (ctlr->prepare_message) { + struct list_head *tail = ctlr->cur_msg->resources.prev; ret = ctlr->prepare_message(ctlr, ctlr->cur_msg); if (ret) { dev_err(&ctlr->dev, "failed to prepare message: %d\n", @@ -1325,6 +1326,11 @@ static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) spi_finalize_current_message(ctlr); goto out; } + /* warn about possible issues on finalize */ + if (ctlr->unprepare_message && + tail != ctlr->cur_msg->resources.prev) + dev_warn_once(&ctlr->dev, + "spi_message.resource is modified and unprepare_message is defined - this can give unexpected results during finalize\n"); ctlr->cur_msg_prepared = true; } From patchwork Thu May 9 10:55:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sperl X-Patchwork-Id: 10936989 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 32E991390 for ; Thu, 9 May 2019 10:55:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 22B3C2874A for ; Thu, 9 May 2019 10:55:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 16F51287C2; Thu, 9 May 2019 10:55:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A708C2874A for ; Thu, 9 May 2019 10:55:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726558AbfEIKz6 (ORCPT ); Thu, 9 May 2019 06:55:58 -0400 Received: from 212-186-180-163.static.upcbusiness.at ([212.186.180.163]:36350 "EHLO cgate.sperl.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725892AbfEIKz6 (ORCPT ); Thu, 9 May 2019 06:55:58 -0400 Received: from hc1.intern.sperl.org (account martin@sperl.org [10.10.10.59] verified) by sperl.org (CommuniGate Pro SMTP 6.2.1 _community_) with ESMTPSA id 7764484; Thu, 09 May 2019 10:55:46 +0000 From: kernel@martin.sperl.org To: Nicolas Saenz Julienne , Meghana Madhyastha , =?utf-8?q?Noralf_Tr?= =?utf-8?q?=C3=B8nnes?= , Mark Brown , Stefan Wahren , linux-spi@vger.kernel.org Cc: Martin Sperl Subject: [PATCH V1 3/3] spi: core: make unprepare_message part of spi_message.resource process Date: Thu, 9 May 2019 10:55:33 +0000 Message-Id: <20190509105533.24275-4-kernel@martin.sperl.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190509105533.24275-1-kernel@martin.sperl.org> References: <20190509105533.24275-1-kernel@martin.sperl.org> Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Martin Sperl Move unprepare_message to use the spi_message.resource cleanup process for invocation with consistent sequence of execution. Signed-off-by: Martin Sperl --- drivers/spi/spi.c | 52 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) -- 2.11.0 diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 78fe46bf6f41..24a4e605448a 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1201,6 +1201,23 @@ void spi_finalize_current_transfer(struct spi_controller *ctlr) EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); /** + * __spi_res_unprepare_message - resource management function which + * calls unprepare_message + * @ctlr: controller for + * @msg: the message to unprepare + * @res: extra data - ignored + */ +static void __spi_res_unprepare_message(struct spi_controller *ctlr, + struct spi_message *msg, + void *res) +{ + int ret = ctlr->unprepare_message(ctlr, msg); + + if (ret) + dev_err(&ctlr->dev, "failed to unprepare message: %d\n", ret); +} + +/** * __spi_pump_messages - function which processes spi message queue * @ctlr: controller to process queue for * @in_kthread: true if we are in the context of the message pump thread @@ -1318,6 +1335,19 @@ static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) if (ctlr->prepare_message) { struct list_head *tail = ctlr->cur_msg->resources.prev; + void *res = NULL; + + if (ctlr->unprepare_message) { + res = spi_res_alloc(ctlr->cur_msg->spi, + __spi_res_unprepare_message, 0, + GFP_KERNEL); + if (!res) { + ret = -ENOMEM; + ctlr->cur_msg->status = ret; + spi_finalize_current_message(ctlr); + goto out; + } + } ret = ctlr->prepare_message(ctlr, ctlr->cur_msg); if (ret) { dev_err(&ctlr->dev, "failed to prepare message: %d\n", @@ -1331,6 +1361,10 @@ static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) tail != ctlr->cur_msg->resources.prev) dev_warn_once(&ctlr->dev, "spi_message.resource is modified and unprepare_message is defined - this can give unexpected results during finalize\n"); + /* we only add the resource late to keep the expected order */ + if (res) + spi_res_add(ctlr->cur_msg, res); + ctlr->cur_msg_prepared = true; } @@ -1436,29 +1470,12 @@ void spi_finalize_current_message(struct spi_controller *ctlr) { struct spi_message *mesg; unsigned long flags; - int ret; spin_lock_irqsave(&ctlr->queue_lock, flags); mesg = ctlr->cur_msg; spin_unlock_irqrestore(&ctlr->queue_lock, flags); spi_unmap_msg(ctlr, mesg); - - if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { - ret = ctlr->unprepare_message(ctlr, mesg); - if (ret) { - dev_err(&ctlr->dev, "failed to unprepare message: %d\n", - ret); - } - } - - /* where to put the release is a slight nightmare because - * ctlr->prepare_message may add to resources as well. - * so the question is: call it before unprepare or after? - * for now leave it after - the asumption here is that - * if prepare_message is using spi_res for callbacks, - * then no unprepare_message is used - */ spi_res_release(ctlr, mesg); spin_lock_irqsave(&ctlr->queue_lock, flags); @@ -3770,4 +3787,3 @@ static int __init spi_init(void) * include needing to have boardinfo data structures be much more public. */ postcore_initcall(spi_init); -