Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751357AbdLZTnK (ORCPT ); Tue, 26 Dec 2017 14:43:10 -0500 Received: from mail-sn1nam02on0108.outbound.protection.outlook.com ([104.47.36.108]:22857 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750822AbdLZTnH (ORCPT ); Tue, 26 Dec 2017 14:43:07 -0500 From: Trent Piepho To: "linux-mtd@lists.infradead.org" , "linux@armlinux.org.uk" , "broonie@kernel.org" , "cyrille.pitchen@wedev4u.fr" , "dwmw2@infradead.org" , "computersforpeace@gmail.com" , "boris.brezillon@free-electrons.com" , "vigneshr@ti.com" , "richard@nod.at" , "marek.vasut@gmail.com" CC: "linux-spi@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "nicolas.ferre@microchip.com" , "robh@kernel.org" , "radu.pirea@microchip.com" , "devicetree@vger.kernel.org" Subject: Re: [PATCH 1/3] mtd: spi-nor: add optional DMA-safe bounce buffer for data transfer Thread-Topic: [PATCH 1/3] mtd: spi-nor: add optional DMA-safe bounce buffer for data transfer Thread-Index: AQHTfHXE8/fvhJVkOkO7c5x52lKY/6NWCpOA Date: Tue, 26 Dec 2017 19:43:05 +0000 Message-ID: <1514317385.26695.39.camel@impinj.com> References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=tpiepho@impinj.com; x-originating-ip: [216.243.31.162] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;SN4PR0601MB3760;7:4uYqFVYnSsIDOhtkUBEVusgHJjiUNLh9jVHioyMsKjtEweBXU1FIpPeUywbZTZLVuBDMLUQLdT86hmC9DegG5b2OGUtaI3C4GcdI79WftuRPTl6ii8YMnre2/sfDYcPwoxl9452qaRJqdpYoO74t+W2sAKZGa8ZrrH33NTVrxwXb5f/DTlTAmme6KDOYoWM8F72TldRwmcBSGs7EhvrxvuYwMN39NUX0DvkXtCHe5HBITwxfLn8z8objhJWz3bWx x-ms-exchange-antispam-srfa-diagnostics: SSOS; x-ms-office365-filtering-correlation-id: 12c08888-e64d-4a6a-98af-08d54c98e244 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(3008032)(2017052603307)(7153060);SRVR:SN4PR0601MB3760; x-ms-traffictypediagnostic: SN4PR0601MB3760: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(17755550239193); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(6040470)(2401047)(5005006)(8121501046)(3002001)(10201501046)(3231023)(944501075)(93006095)(93001095)(6041268)(20161123562045)(20161123564045)(20161123558120)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:SN4PR0601MB3760;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:SN4PR0601MB3760; x-forefront-prvs: 053315510E x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39380400002)(39840400004)(366004)(376002)(396003)(346002)(24454002)(377424004)(189003)(199004)(316002)(478600001)(217423001)(14454004)(4326008)(6512007)(6246003)(8676002)(81156014)(81166006)(7416002)(2950100002)(110136005)(54906003)(5250100002)(2201001)(36756003)(86362001)(102836004)(2501003)(39060400002)(4001150100001)(68736007)(97736004)(8936002)(105586002)(106356001)(6116002)(3846002)(103116003)(25786009)(66066001)(2906002)(7736002)(2900100001)(305945005)(6486002)(6436002)(59450400001)(53936002)(229853002)(3280700002)(6506007)(99286004)(5660300001)(76176011)(3660700001)(921003)(1121003);DIR:OUT;SFP:1102;SCL:1;SRVR:SN4PR0601MB3760;H:SN4PR0601MB3759.namprd06.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; x-microsoft-antispam-message-info: 3nnOBX6Gxcvm8c1q0MdgPTYvE3jhojmWutcenXi1aqGVKqcwY/jcg6AyDw3doGSFjQmNzYOYlOKPzKhJVZThyg== spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="utf-8" Content-ID: MIME-Version: 1.0 X-OriginatorOrg: impinj.com X-MS-Exchange-CrossTenant-Network-Message-Id: 12c08888-e64d-4a6a-98af-08d54c98e244 X-MS-Exchange-CrossTenant-originalarrivaltime: 26 Dec 2017 19:43:05.6742 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 6de70f0f-7357-4529-a415-d8cbb7e93e5e X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN4PR0601MB3760 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by mail.home.local id vBQJhEL1019232 Content-Length: 2846 Lines: 81 On Sun, 2017-12-24 at 05:36 +0100, Cyrille Pitchen wrote: > > Then the patch adds two hardware capabilities for SPI flash controllers, > SNOR_HWCAPS_WR_BOUNCE and SNOR_HWCAPS_RD_BOUNCE. Are there any drivers for which a bounce buffer is NOT needed when the tx/rx buffer is not in DMA safe memory? Maybe it would make more sense to invert the sense of these flags, so that they indicate the driver does not need DMA safe buffers, if that is the uncommon/non-existent case, so that fewer drivers need to be modified to to be fixed? > +static bool spi_nor_is_dma_safe(const void *buf) > +{ > + if (is_vmalloc_addr(buf)) > + return false; > + > +#ifdef CONFIG_HIGHMEM > + if ((unsigned long)buf >= PKMAP_BASE && > + (unsigned long)buf < (PKMAP_BASE + (LAST_PKMAP * PAGE_SIZE))) > + return false; > +#endif It looks like: (unsigned long)addr >= PKMAP_ADDR(0) && (unsigned long)addr < PKMAP_ADDR(LAST_PKMAP) is the expression used in the highmem code. But really, isn't this begging for is_highmem_addr() in include/linux/mm.h that can always return false when highmem is not enabled? In order to be safe, this must be called when nor->lock is held, right? Otherwise it could race against two callers allocating the buffer at the same time. That should probably be noted in the kerneldoc comments for this function, which should also be written. > +static int spi_nor_get_bounce_buffer(struct spi_nor *nor, > + u_char **buffer, > + size_t *buffer_size) > +{ > + > + *buffer = nor->bounce_buffer; > + *buffer_size = size; So the buffer is returned via the parameter, and also via a field inside nor. Seems redundant. Consider address could be returned via the function return value coupled with PTR_ERR() for the error cases. Or not return address at all since it's available via nor- >bounce_buffer. > { > struct spi_nor *nor = mtd_to_spi_nor(mtd); > + bool use_bounce = (nor->flags & SNOR_F_USE_RD_BOUNCE) && > + !spi_nor_is_dma_safe(buf); > + u_char *buffer = buf; > + size_t buffer_size = 0; > int ret; > > dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len); > @@ -1268,13 +1324,23 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len, > if (ret) > return ret; > > + if (use_bounce) { > + ret = spi_nor_get_bounce_buffer(nor, &buffer, &buffer_size); > + if (ret < 0) > + goto read_err; > + } This pattern, check if bounce is enabled, check if address is dma- unsafe, get bounce buffer, seems to be very common. Could it be refactored into one helper? u_char *buffer = spi_nor_check_bounce(nor, buf, len, &buffer_size); if (IS_ERR(buffer)) return PTR_ERR(buffer); // buffer = nor->bounce_buffer or buf, whichever is correct // buffer_size = len or bounce buffer size, whichever is correct Could spi_nor_read_sfdp_dma_unsafe() also use this buffer?