Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3300199pxk; Mon, 21 Sep 2020 10:03:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdF8VXQYq995VeaDrilUls8T037PuTQDDFrilIy3NPenxcCScJ0rfZdf0icxBXeH3m9Bre X-Received: by 2002:a17:906:cb92:: with SMTP id mf18mr379941ejb.485.1600707792009; Mon, 21 Sep 2020 10:03:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600707792; cv=none; d=google.com; s=arc-20160816; b=02bopWURA7tNOispeQj7gWFKmSNGj/VQ23Z1T49lbUv7t28alsfuQ1WmUy5X22mQOB MpDUGU+GPqe/AzpQ55juFgCsdqDfl49mlvYhEhz0gnkgYuLsVvaTApYf1XRsW+7B90Z2 jNXgn2KOHHVGBqwEsWsxTX5a2EVWufqomR4UrEYPBps+Bue5uP9QVq44vdscr1KJEIm/ WCkWdNTH/X+cUlMt7J9HUlBGUUxytIC8rNiKPUtgxEYCSThdndNZOVOuQrRKGAG5z/Su QL6h0Dg8eZhqBg5xHzJpPs2VGFjawNrHMidNZ9eME7XOsERxSb6T9L/GezflMgg0nixG E5PQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Qb6iRbdiZSMAOq6q9QV6ivFdpSXHD82HTod9eYoYpp4=; b=flZGGx/F+Do4J+/7zC/xsqxAslvCdif6N6v66MM8S5TmCQ0U1Fb11lm6ZdT5BFpbik YAp1DOSpGWOLvXtQXctxUPneJl9V69I+pIP2x/JUm0T6oFGhUJ69pwU5jb9fLEqppPMd UcSdsfTsCGQWAQkrbdw+b1rM09pNndQrTEKmCrElFXHawMtf7skZhC5eS27Vb8pEzYpm gNef/tZCxGs4+W3byo8BFjOn7RewLGEuZ5/Lisn6q6ZlswxH1FOr4CYZk45nnBZr0iDr +QMKJDzhbErQPQTdyZu32sg/hSQzUDpo3D+6Q/bdMZf5M7bUIajfdVweuH/v9VavPH/9 HTTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cQCslyK7; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a42si11791907edf.535.2020.09.21.10.02.45; Mon, 21 Sep 2020 10:03:11 -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=@kernel.org header.s=default header.b=cQCslyK7; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730371AbgIURAe (ORCPT + 99 others); Mon, 21 Sep 2020 13:00:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:41742 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728260AbgIUQjf (ORCPT ); Mon, 21 Sep 2020 12:39:35 -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 D6E6A239D1; Mon, 21 Sep 2020 16:39:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600706375; bh=hoiopHNqBwCuXdRZ+e3IEFnmXrmN6DQJtXVKc2zkBnE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cQCslyK7E349bcie7A29O3RQcV2kOjhyBxsqn78+Dq9LVYk0czpvG6cElQ6vXRZ+9 L3fi5t/CZBgyNUndbs75NHAUbdu+WT5haXBXCulTqXgVqxXfpvyrUOZo4rDyO+Z8KQ Gs9YtfLG2lKEe0biaYsAr7fkHtF3rVcG5Ej/uAN0= 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 4.14 74/94] spi: Fix memory leak on splited transfers Date: Mon, 21 Sep 2020 18:28:01 +0200 Message-Id: <20200921162038.931588593@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200921162035.541285330@linuxfoundation.org> References: <20200921162035.541285330@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 ab6a4f85bcde7..acc8eeed73f07 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1125,8 +1125,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; @@ -1384,6 +1382,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) { -- 2.25.1