Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3297963pxk; Mon, 21 Sep 2020 10:00:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfXzWVMDKIa9qErL1vd668uoi1U1061SVeRIP/h3psaAfrwxkzZ5Ts9l2atcY2whRpb41q X-Received: by 2002:a17:906:c55:: with SMTP id t21mr432243ejf.276.1600707641570; Mon, 21 Sep 2020 10:00:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600707641; cv=none; d=google.com; s=arc-20160816; b=q4SULeNG3itTVOLHV5mKnc9JJ8qs8jztbdYtE9e6b6JSIjqNuYPeCTADYc+VtjkGlS KV3N8w8Jx0jJXSF2h+EAZHNp88nQGy3Ark1sS3rYtO0tX3o9bo1Z586b9vs8satFZkyl 7GZxV/2QrCLHSSX40kcbxWIQOFQfQ1Q/D3YF3n678cahTfvy1MJrjb8N1NyEpvOMfE5g NgQk0VmuPrcPZnXITdOdimE+xhAqMOx+8/Yyyuk7NCg3XvgADRHAXhRN6ajEV+39S49j ulBb4y3ktUg8dzPs/1GZjxF3fK4e+zuF5tPdT/7DnPkYXNI2hzn527H7/APzHUPYEz80 FEYQ== 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=AzyA+nH3eNzGIHCwmXvBW8hJj0n37G6ikYFe/jdvHTA=; b=rH/SMEgIEeBRcICMs0YRBenTrKqoy/TZm0Z1j8uYNwWeTM9rV8GBI5jOYKxvOyi/T5 fieaXo303sCrILw9M2cxaT/QGQ/zNFYxuVJkSf/PKSQ34pjDxLWUufHp0xzEDyGuyC2k 0SiDdtUQUDg1b8f/gjOhFg+DcPTI13MZ2YluSFPATHWsf9Dxcv4u50Zuzrtyt1epvErZ Cs664D7DJdYNAtJasu/J7dY7oOul57UvoRDWLp18sMWEi1m2QNaez5H4aDe2Y7NQyEjm 2OF1K86w0Sc0Gnm/i4+Evl8YrEQ90O+nSInyxFQJowifvHdTbf15NUJWjdhEe2/tLXMM UKTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cK0jbY5J; 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 bu18si8909015ejb.324.2020.09.21.10.00.17; Mon, 21 Sep 2020 10:00: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=@kernel.org header.s=default header.b=cK0jbY5J; 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 S1730312AbgIUQ7R (ORCPT + 99 others); Mon, 21 Sep 2020 12:59:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:44968 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729137AbgIUQlZ (ORCPT ); Mon, 21 Sep 2020 12:41:25 -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 DAD662399A; Mon, 21 Sep 2020 16:41:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600706485; bh=1Pc+8kuK6JSFDckznXLJCEEkO2/fb968mCbD/xiDKZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cK0jbY5J8B5U1jV2Ju4y8VdH0vK4Gk3ClkAfcKgT4ULEV3C6xny6AwFz6dgCkMLD5 gnADzm14cSbSspOH2ykB9N/ErJ9RUNfHH5C7oEdcIMzwye7fqWXFL7Jja2s5qU0xHW wYlL0AWqR0LilsW/25CNk/AnSZQ7DFixx5jTxgdM= 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.19 24/49] spi: Fix memory leak on splited transfers Date: Mon, 21 Sep 2020 18:28:08 +0200 Message-Id: <20200921162035.721679705@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200921162034.660953761@linuxfoundation.org> References: <20200921162034.660953761@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 92e6b6774d98e..1fd529a2d2f6b 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1116,8 +1116,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; @@ -1375,6 +1373,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