From patchwork Mon Nov 21 22:50:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juan Gutierrez X-Patchwork-Id: 9441317 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1294D60237 for ; Tue, 22 Nov 2016 14:48:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 04EA52841E for ; Tue, 22 Nov 2016 14:48:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EDB142858D; Tue, 22 Nov 2016 14:48:27 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 63C672841E for ; Tue, 22 Nov 2016 14:48:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933565AbcKVOs0 (ORCPT ); Tue, 22 Nov 2016 09:48:26 -0500 Received: from mail-he1eur01on0053.outbound.protection.outlook.com ([104.47.0.53]:22464 "EHLO EUR01-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932949AbcKVOsY (ORCPT ); Tue, 22 Nov 2016 09:48:24 -0500 X-Greylist: delayed 44682 seconds by postgrey-1.27 at vger.kernel.org; Tue, 22 Nov 2016 09:48:23 EST DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=Y3EPGPvpLoyCGurzH3jvP+/UAIPaHP5qYjY4sCx7QAc=; b=mH0Q6UPXhO6Q/2AZxvkANgiF43KuxKa3Qe+NC1vTdtcpoM9keo1WNdDEdTlbls2XYeNvnAQ21bz2UZ2Zd+s3/gjHZNA/JRxylThHcNPpWx2roGUvVkIWJxSlVI1HEeuYszScw6xLJsamja15MceGfYPSX5RBZeh7Ba3GGwvHv9Y= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=juan.gutierrez@nxp.com; Received: from kakaroto.am.freescale.net (192.88.168.1) by DB3PR04MB0764.eurprd04.prod.outlook.com (10.160.51.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.734.8; Mon, 21 Nov 2016 22:51:04 +0000 From: Juan Gutierrez To: , , CC: , , Subject: [PATCH] spi: use sg_next for walking through the allocated scatterlist table Date: Mon, 21 Nov 2016 16:50:03 -0600 Message-ID: <1479768603-15583-1-git-send-email-juan.gutierrez@nxp.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 X-Originating-IP: [192.88.168.1] X-ClientProxiedBy: MWHPR21CA0017.namprd21.prod.outlook.com (10.173.47.27) To DB3PR04MB0764.eurprd04.prod.outlook.com (10.160.51.155) X-Microsoft-Exchange-Diagnostics: 1; DB3PR04MB0764; 2:56nNjijtwxAu0ujzbW6HQ2qsxP4Apy6VKbsoQf152EK1aFjmDNfP9IGhhjv7SA22jywxk6onF416tHPQiYGaHiVp9VB8wT+Jd2+L2u53IxC3LpTgjfaq82WCaT0Z5J6QTZexNRYXlmI1UMpQS6yOes03oR7SBBWdUcS+KQUORcc=; 3:alYDe8vrrPNXmSmsUSOrVGg1FGDsW///Oj8Strq1jRgnlcxKZySQcVDdC/ByabtZJIvYTQY2hR+pA2aHGnNsk2W3r+KOqfboBqw4oXJDtRHaaBHI9fIYOpRwvijFdppQ2vggtH4fh1dcmpxPpbIYpapfMve6hXLYJafhukCf9lE=; 25:v1slOqV9KznJXdKZM3l7rf80uNGg+hHRNMJK2fMIU78+Ez/B4kLKyTHZL4YVX1ayRQw9E3r9LM9mP6JwU+Fsb3MjPOf3TMjYL/BhUepF3Rvw3WdZBi4nHcAJRiThakXukByD1wUGliAi0eG65zThqjxI81i5UcGL6wrA7IOld/oRBceJBhYzHwtqF4BppSFspaUO1hF/O/z22Qw1MuQQDokp3lsQdNL6vj46rZn9Rfi7K9a6L5sg+ESXR5w84bYwzwdBWErVnpBaS1CF1F5lB3/4+AISS9VgiQXzwss5iPe4MeVu/lnZYFW8/iYbYoIc+Tm6A/8YR1xnHQcnxnZt/k4Zf1/BFHbe1ZJMeerdyayzb3DIuxQhxp6raCQ24cQAki1HhByLG98g2NoA9YMbfD5SPBGgOpcR8SmHzKJ9Q3chd/5pKEBijNlKUonq7zbmbGs2K6hwnuGe+QR2HWuhfw== X-MS-Office365-Filtering-Correlation-Id: f57a0e68-5ae5-4cda-a83c-08d41260e11b X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:DB3PR04MB0764; X-Microsoft-Exchange-Diagnostics: 1; DB3PR04MB0764; 31:Yv105XzJE4Cem+5v/u3jo8VtRF36kLZQFMKg68dizXeF0KKCD9P9M3mMJdVzRVooZIs9+zn6vP71t8PIZ4/2WoUc+OZjJuytDEZAnRk8+ePQ8WbVLxALXbHpQk2MvrItQfuU4X9+D2pQlQu/tilhEUlCABYJoRySYdUDICCLJM8hGxCW3MSM4YFPQuH9lC5SPHqBXF/kekZ5Gi68JCk6Jqar+G8kcg7txI7PWg9EIVJDnzuQw4CmXkM1U8LVnPEnJSEwXbql3NeixWy92gAx2w==; 20:QJfcVlMtaQEsGNnLFxDwd9w3/18tiuf1FbJXlOMhy6yOeEOOo2HZ2HdhdvGAjwS71koYl7bptRxzLazNOP5odxQBxoBFAvLs/ieE8vtkG3IGYWZ+yHH0Of+kXBplr16ZnyKPgh+PFb8u0YVqPDGjnvXdldyyG0v9BFnfAwZnzcKmvP4GumgJocfnpyw6/Cq/U6DU6Z951dcqRv5nF4Q6BMvS0YryoIaiDpaAQM/vRyHCZ+acfQg0IrCaOtB6ni21crpLFs3E0caUB6n91/DNCk3Ah/wDqq3nah0xoP2/V91ruW1P0TzTqAyzj/aZIITM94wJwI9m7fTaOjbUiiUQf9C7iQk+chRCQibRi6V2aVWCrywQKaiq15NA5Q0Mn3FhOwFSt3oWa/cNjhGslyCnfNx3wbQq4fCrEfuEGUwofBU4y3mSo3KAscwZDServH9NgjzPfC6MvnLoXZmsiT6QlNDp9SLs6+AJWEsW4gfuziCopB0dHIdNiI8OaT2zp80x X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(131327999870524)(185117386973197); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040307)(6060326)(6045199)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6055026)(6061324)(6041248)(6046074); SRVR:DB3PR04MB0764; BCL:0; PCL:0; RULEID:; SRVR:DB3PR04MB0764; X-Microsoft-Exchange-Diagnostics: 1; DB3PR04MB0764; 4:6TvFEsZaNJZCyFsCf2FnY/fiR2D9WRgQeGZxWb4ux3g/8O0iMy0w14r+65S4gXGTMdLeZITLVLbAaj5gQddLOnHzPKUYa1WqWGFv5Ow4aEH+I9+nQRL623glP56UiGK5eTmtSrMaIPPnkhcikV1CkgKIBDVzCXctGq8xT/IqMk2PI4LBQACXic504HfbGROb27+HjNzQ3PdsFd4v8dPYy/M7vqa2ngts+fVXRw1H9Y3uSq7h4FXm8oMPfZMxgHAIc1XmthhVr4tkotdYpptlsufDE0VV2hqiHVWybt1PFHIVJMeD3Bydb/qEmU0cVQ8Ajl3KBDeP16dLsdK2MNeNzIGUSsNHmOD2kSt9LVETqOWQEiofMNdOkTaaIfSkgdNs867Tm6XHRrC3BCtdqDJiai5waXraq249Z88L/CqtYA8FFKENjNdHbvx0uUkB9Vx9DFdzcBnL4eRAElaTX4yAy4wzgT0uQ0kVry/UDTCHg7JZcQkCYgT4GGkEa9i6fyiZBUp+Pf9RVyCI4KFuR0nnTVh2k6BIjX8iG7y3zG+imH6Xqjj0uoWJwT/4Fxw/uK30zcNEmhNtPWF2udj/RL/2/g== X-Forefront-PRVS: 01334458E5 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(189002)(199003)(8676002)(36756003)(6116002)(4326007)(33646002)(3846002)(97736004)(5001770100001)(106356001)(305945005)(2201001)(101416001)(86362001)(50986999)(105586002)(92566002)(50466002)(5003940100001)(42186005)(77096005)(66066001)(68736007)(2906002)(7736002)(7846002)(50226002)(189998001)(81166006)(47776003)(48376002)(81156014)(38730400001)(5660300001); DIR:OUT; SFP:1101; SCL:1; SRVR:DB3PR04MB0764; H:kakaroto.am.freescale.net; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: nxp.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; DB3PR04MB0764; 23:WAQyh9a6A4LOBBLwSHACCQpS+/WB2zDZW9yhq/WBE?= =?us-ascii?Q?CTahf2IOUF55pIjsTPckAKIA8xOKTH0gZi2B968n2tcz6rRmSDo6wHumdheW?= =?us-ascii?Q?PP8c+DZZWnF2GKwN/UekCQmLseTF2p5kXY+g80Sy2nfqxh2XzNOC9BDFLRcV?= =?us-ascii?Q?3S276KYNUKO2WrbIIprgffTRm1aE8lzE9/vR9ZkeKLy9jmdYldSsSZuuss4B?= =?us-ascii?Q?kjKu9c66+L5ieSDj2gfos3wcAEFoXGGj9PQS7MPXeH7iZ0RzjLPkhUAjapg/?= =?us-ascii?Q?9jizLChK4jVkRirIqMzrbOatMnRe1kek2TQq4bSgyYQOsFgL8fimbOIiuNp6?= =?us-ascii?Q?ujcww9qIi1I8UyuTFxuKRET+aauXmpq1VBbIGBBptZ7UQUw8EcV2x2tbOKMC?= =?us-ascii?Q?y9zaatUNBOqpqt0L/k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q/Ux+qRM6rpYs7/WN73lrp+FDJ3FoFNW86ernb0Ifvp7DgobZrpK9XE0vMBt0x1khD56H+CCp; 5:5VyccC00rpGgIPysPopPwVn9ebK7+zfc8R8c9JN8AVsNlLAdIy73D6Mo8DKJ9F0x8NsAn5Z9lnWsbfQaycsiD7X5kFneTft1aziAQotiOwvd6uxNdjWKLDiIZuX3zlGgOKKK+z/dU2SlVjwYZ07dcQ==; 24:x/2dc26zkz+ZwAydrTxqfRnNMZH1gcVe24CAFsmP7P7pLU4gyatmeblfvvHVfRBp+vOd8/+qBch960GH3rhY1qufBw13/iraVitzqu5ipxo= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DB3PR04MB0764; 7:fEYklOCQ0HkxhqZRmCgOHfvbP8soaVPX+RDW7qpPn+NzorK/9/ajWoRiUpC7CZQVyB7pXZGf0/pR6dPDrE8+jy+ELUMrxg8mxNoiI86ohLCCdREgweSpRQSovNtZWllMzMYhM5b+igykLLy3N7lMfr8UR4619j19cRhQC2b2jeRHXhPyWSJSNGjgXDRR6Pqf/rgRcCkwmB0Sq7/JQGj3w1dUJe+hbsYB3dOgnaWJEuJGtEKytoa7X800py226nXb6g5IIO23C6NOWOQQWNnvj8vJssCuh2FNZE41mBTRBJFA4rOkD6r4rlGUcHNiAYXNXrTOTlSoLRtnqmWpSaLg7Iqh5gINDckQybqQa6PVqjU=; 23:A7pR1NzwfISGmz5RsKT7/wB/zPF3fti/4M/NQ/0DK2HjWoW6Pij7k4c0ulf+1ZT9VpXjBf4jRKN8r8/SSV2DuSpzOgOOfgfKP0Q/i2CL5eHmV9S19K/zGwiumA4hnN/A3GaJ7uQUTZVvpR+Tk08WzzhZCw2Je5ZIGXewnv+jSvhtMwLL4z0xpwhCz4uA3B2w X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Nov 2016 22:51:04.6281 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB3PR04MB0764 X-OriginatorOrg: nxp.com 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 A null dereference or Oops exception might occurs when reading at once the whole content of an spi-nor of big enough size that requires an scatterlist table that does not fit into one single page. The spi_map_buf function is ignoring the chained sg case by dereferenceing the scatterlist elements in an array fashion. This wrongly assumes that the allocation of the scatterlist elements are contiguous. This is true as long as the scatterlist table fits within a PAGE_SIZE. However, for allocation where the scatter table is bigger than that, the pages allocated by sg_alloc might not be contigous. The sg table can be properly walked by sg_next instead of using an array. Signed-off-by: Juan Gutierrez --- drivers/spi/spi.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 838783c..6435548 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -720,6 +720,7 @@ static int spi_map_buf(struct spi_master *master, struct device *dev, int desc_len; int sgs; struct page *vm_page; + struct scatterlist *sg; void *sg_buf; size_t min; int i, ret; @@ -738,6 +739,7 @@ static int spi_map_buf(struct spi_master *master, struct device *dev, if (ret != 0) return ret; + sg = &sgt->sgl[0]; for (i = 0; i < sgs; i++) { if (vmalloced_buf || kmap_buf) { @@ -751,16 +753,17 @@ static int spi_map_buf(struct spi_master *master, struct device *dev, sg_free_table(sgt); return -ENOMEM; } - sg_set_page(&sgt->sgl[i], vm_page, + sg_set_page(sg, vm_page, min, offset_in_page(buf)); } else { min = min_t(size_t, len, desc_len); sg_buf = buf; - sg_set_buf(&sgt->sgl[i], sg_buf, min); + sg_set_buf(sg, sg_buf, min); } buf += min; len -= min; + sg = sg_next(sg); } ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);