From patchwork Mon Sep 21 16:27:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "gregkh@linuxfoundation.org" X-Patchwork-Id: 309433 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=-14.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT 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 7136AC4346A for ; Mon, 21 Sep 2020 16:44:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3FE00239D1 for ; Mon, 21 Sep 2020 16:44:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600706654; bh=bzNEqt+/cIQCKugUryp8HxCjv2Ca+Nsz4OsbFTa/h/c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=FiG0XCaLF989+xhIV5t72UpbVBKvRRGylhPd6ra9p661m+Id9/kjWNrkVYY6qCcpx CvQSf37smH8FEdpi4lWAkW+kg7uM/0LeoeGm8XoMHxXktL70Y97HDjiBYFgreVSwvi MysWmmdCkkBsdQRv9D+VeMuYs/jpKCZxeAqg+a0g= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729157AbgIUQoN (ORCPT ); Mon, 21 Sep 2020 12:44:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:48920 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729046AbgIUQn5 (ORCPT ); Mon, 21 Sep 2020 12:43:57 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6F3E82399A; Mon, 21 Sep 2020 16:43:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600706636; bh=bzNEqt+/cIQCKugUryp8HxCjv2Ca+Nsz4OsbFTa/h/c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qRaPzPOzc+RyaEbbHQ+1jt4W6Gz1UT2MQUvd6vkbIXr0hguYV4/xpRw7SIg969pN5 wv8lQiOXMWCtJmwqLkw1gDZpHcVBBD7q2HURCqnANFkIZBAK/ELF6eo+10Skv3P//R By+rR/Jv9YXNkMGVyQpTo5G25Cb1ZSeJeO+1++Cs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Gustav Wiklander , Mark Brown , Sasha Levin Subject: [PATCH 5.8 033/118] spi: Fix memory leak on splited transfers Date: Mon, 21 Sep 2020 18:27:25 +0200 Message-Id: <20200921162037.843357734@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200921162036.324813383@linuxfoundation.org> References: <20200921162036.324813383@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Gustav Wiklander [ Upstream commit b59a7ca15464c78ea1ba3b280cfc5ac5ece11ade ] In the prepare_message callback the bus driver has the opportunity to split a transfer into smaller chunks. spi_map_msg is done after prepare_message. Function spi_res_release releases the splited transfers in the message. Therefore spi_res_release should be called after spi_map_msg. The previous try at this was commit c9ba7a16d0f1 which released the splited transfers after spi_finalize_current_message had been called. This introduced a race since the message struct could be out of scope because the spi_sync call got completed. Fixes this leak on spi bus driver spi-bcm2835.c when transfer size is greater than 65532: Kmemleak: sg_alloc_table+0x28/0xc8 spi_map_buf+0xa4/0x300 __spi_pump_messages+0x370/0x748 __spi_sync+0x1d4/0x270 spi_sync+0x34/0x58 spi_test_execute_msg+0x60/0x340 [spi_loopback_test] spi_test_run_iter+0x548/0x578 [spi_loopback_test] spi_test_run_test+0x94/0x140 [spi_loopback_test] spi_test_run_tests+0x150/0x180 [spi_loopback_test] spi_loopback_test_probe+0x50/0xd0 [spi_loopback_test] spi_drv_probe+0x84/0xe0 Signed-off-by: Gustav Wiklander Link: https://lore.kernel.org/r/20200908151129.15915-1-gustav.wiklander@axis.com Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- drivers/spi/spi.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 5c5a95792c0d3..65ca552654794 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1305,8 +1305,6 @@ out: if (msg->status && ctlr->handle_err) ctlr->handle_err(ctlr, msg); - spi_res_release(ctlr, msg); - spi_finalize_current_message(ctlr); return ret; @@ -1694,6 +1692,13 @@ void spi_finalize_current_message(struct spi_controller *ctlr) spi_unmap_msg(ctlr, mesg); + /* In the prepare_messages callback the spi bus has the opportunity to + * split a transfer to smaller chunks. + * Release splited transfers here since spi_map_msg is done on the + * splited transfers. + */ + spi_res_release(ctlr, mesg); + if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { ret = ctlr->unprepare_message(ctlr, mesg); if (ret) {