Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1956238AbdDZJBX (ORCPT ); Wed, 26 Apr 2017 05:01:23 -0400 Received: from mail-dm3nam03on0069.outbound.protection.outlook.com ([104.47.41.69]:17566 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1434645AbdDZI7k (ORCPT ); Wed, 26 Apr 2017 04:59:40 -0400 Authentication-Results: lst.de; dkim=none (message not signed) header.d=none;lst.de; dmarc=none action=none header.from=amd.com; Subject: Re: [PATCH v2 01/21] scatterlist: Introduce sg_map helper functions To: Logan Gunthorpe , , , , , , , , , , , , , , , , , References: <1493144468-22493-1-git-send-email-logang@deltatee.com> <1493144468-22493-2-git-send-email-logang@deltatee.com> CC: Jens Axboe , "James E.J. Bottomley" , "Martin K. Petersen" , Matthew Wilcox , Greg Kroah-Hartman , Ross Zwisler , Dan Williams , Stephen Bates , Christoph Hellwig From: =?UTF-8?Q?Christian_K=c3=b6nig?= Message-ID: <5dfc5bf5-482c-b5bb-029c-7cee80925f37@amd.com> Date: Wed, 26 Apr 2017 10:59:17 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <1493144468-22493-2-git-send-email-logang@deltatee.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [2a02:908:1251:7981:75b7:572:11f:8bb7] X-ClientProxiedBy: HE1PR09CA0049.eurprd09.prod.outlook.com (10.174.50.17) To BN6PR12MB1297.namprd12.prod.outlook.com (10.168.227.147) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d47bb110-7fdb-43ec-c734-08d48c828e59 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081)(201703131423075)(201703031133081);SRVR:BN6PR12MB1297; X-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4gXau8GDHBd1QNPQFr0uTfLYVtYyTiTIEcEok/wYoKuGNXy/zKDQma17VcOGZJv8jPzMtrCQoKAcJFA8tFzZtWY95Xjr1QLEvBdpMF4Nlw5kg==;20:Dyd5hdi5gzcED0ZQ6z0BTFnC+ec8Oih2H844nnGQEnp6hgsrg2wdDI3ZwXRdwHljjREgEAwm2Wqpclfzj2Judv13EkWUL+IzPq3h2ymLFs8OWapIHCm2fvHWiTKmdRalRARcg629jEu62J3JkXf4iHkt0xkbNEQWbNafbrY3JLcJyYmxkadwal0ZS1kuMFKYPiyNh/TYhnIhFZgejZCLvxrfg/d00KrbiaaZA71OIW14PU9Yg5EfimWEsmS7LXLQvGtAPHqN14KKOTgiMP9sxpizBXBpY28OInYfSyXEAABTOILVqO3bvw0v7AmqIwVTtdSLtv1NDqQy3v/I1mVE36PY0eJXbc3RcYeVUcVnMMRPyag0tvbuL8McCD7Yve9oBGBBHbw+c/99D/yWMFIXFWlbBEfm4U9wcqLx03yB/M/I9qi6WFqgQ9wSls/KNL0z6KrjPTIIdh+EaFY721t2ge7aEirtymZ6K5RWHhUyiJmACAwm98BHGq332pvBNdW+ X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110)(788757137089)(17755550239193); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(601004)(2401047)(8121501046)(5005006)(10201501046)(93006095)(93001095)(3002001)(6055026)(6041248)(20161123564025)(20161123560025)(20161123562025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(6072148);SRVR:BN6PR12MB1297;BCL:0;PCL:0;RULEID:;SRVR:BN6PR12MB1297; X-Microsoft-Exchange-Diagnostics: 1;BN6PR12MB1297;4:hUNkNAqdu9EWqsClFrR8q5qUjT/cdOGYYQRTZpWTuWpAYu3/SPUhipELso6vrSHuvNSCJP0fGBKcvEXAgj4D4+2xrlYgRdhw8fs0VrfHdu6LjnqHVrfKroICfGQhdeWxmvwcEADHqYGyzRq0OXFsx6YyCsN9mmXtJSP0/ODh9tex8ropIX520MClBQBZsWFw5peJXee52npBd/NkkM0YmGPBdf7bGVNXXf00i5dJlxVeqaZyQoSru7cRBSyi4f9ysSv5Vh2QZzcrj4r+MwjOIb/SR2RS5wyBxWnYobtAGgCPcZ5ZB/blm6Czi7/ZVkUfhcvJujD7rvNO5x6ptV0YZNwIhPwPYGXE9bWsdqfVzNbwf/uEotcshkk2ezOxfO1OTDQFVM39AogsfYoa3ojEfbazUwbk9I31hV393Gtxv8YqeYpG+fXVKtK7lRdXKEwAQ5OWPMaJ5X3yETdZty+brsj6LeqZq0HIsQaCqMHVkXAN8Lwz2S/gNKqUYt9ZW+AvYGSWh5YWpode7CGci8xSUkdC5s19w7XlvnEFPkslDGxByrhXp1VgNIydH2KPBVZG/zahPWPizRv0y0f5i+cLBKZXpsuNkNVt/oYX8pDI+CEMSZa+O6OyP1AzwUCrbD5/vDR9XXGtX0J0xy/WcaoSMtgsr2RSo0Psk13DYlrYVBilY6Cq46bDiGUtWOUalAnPVVZ5XCTe6xTbBdTc/H7WXGBIc43V2oq6A4vC58vHn6xDs7KVWj0ewQHmwNSEkTcROJ//V4HIgwhSPR1c+Ulimfz3toxGlnJfjOD7ZzLPavn8V0+mu5C41vmMH0SN6iYwi1DHuvoeUUcfpbTELGlTug== X-Forefront-PRVS: 0289B6431E X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(39450400003)(39860400002)(39840400002)(39400400002)(39850400002)(39410400002)(50944005)(54356999)(64126003)(4326008)(33646002)(38730400002)(6666003)(2950100002)(7416002)(76176999)(6486002)(50986999)(305945005)(25786009)(36756003)(7736002)(4001350100001)(229853002)(50466002)(23676002)(31686004)(6116002)(42186005)(65826007)(5660300001)(31696002)(47776003)(2906002)(65956001)(54906002)(2201001)(86362001)(83506001)(2870700001)(6246003)(8676002)(53936002)(81166006)(8666007)(189998001)(65806001)(921003)(1121003)(83996005)(2101003);DIR:OUT;SFP:1101;SCL:1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NVY2RrQUJMNzl5MFpwV1VsMmV0d0JnV1B4M2lsbmtjR1Zn?= =?utf-8?B?WHo4V1YvY25Td1pNQmRsUT09?= X-Microsoft-Exchange-Diagnostics: 1;BN6PR12MB1297;6:XASPga7w6Qx5P7zFWBLLh3vw2SNqoOwMbFCQmNRrTe/YDN8mP9hLOCaThaPQaYpnV7r+YSwYvI/5rYrhuw8LO6W2xGDp+G8zSamDbtnq7PttmuoAgn1BW0KfXDF3KijhlRboAzJzLPUem41AxrmtlL6lUCmhZJ2crBTQVCJyy88WqdERtPFKHKUF5Nab6mwbRhhupvxkgYUlbhATuUEQtU54NKVeRzsnTOliw5Pwbh9IovYxFzBIL6UGQb0jyBJlnAp37y/U+O+VcS2yGygtiEt5rP+vYi0eu+B4lGy/d1goUBbgUl5GOWJ246PBOoiitW6Ld79H8nvT07cyZkSH3ydDbho8wwqrhXeXylWyI5tiKtD3saZJCX0QA4BNEXYzoa4yYtCJ1aC3MDneeWqDGWPF1N8F3twH7uoytUKhI7GbHh2xyZaO08fE3WPzgyG7jCf999S10EwAART4EPtLgCy99U3DTyz2IpEr2AExHWF3GJH5XNxSQ6jnFR+ARCGsXAtZ01VgdUHkULlQ3OvszepD+ua71Za6BZNbI7E3+LE=;5:Jc8cHkAeB1NRjTGayNI/orQFvuirlNwW/qvHyL7cncJO+R5ZNZr1chB7XHkptR6JRR1sXJD1cpPXs9cV/rflrcfi7WgJve0xAK42BA3g6YMRnctm/Of2WD0i5cy00wBrMjf6dj+YidQ1nv+vIcHg9Q==;24:AosVpXF4PTelqj4UyQzpIZWKVGTuVv7fH4KmAeqW5BBtnULB/owkqpy4Z3yAJxFbyCYxuyuT2L9EtFhXcpPB9xBgFtxjAxFoncjYRPkX3fY= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;BN6PR12MB1297;7:NguflZEKMv/OlZyChTkeyALrKimSTQ0dkbZ05n+kax4/7PLphbWbi+6IjYWEm7BXmyZA9oxM1WwFbJTLRafb65U5IT7cWpPqFk+6XyFE4xjdzlxqXi1o5Mse+GQgK70DH+ZPwpAEVzf0tQhQZDwvSvSXtZI/DyAZvWDci9JCgk69HNlW+Y0oisucHJi7/uXRicS6n8oNCm9uIhacPMHYl7RlXi4KXtjwe00WFfCUS6fQknzX5WiyjJYa4idNcfq4xbngKESgDA9oE42WZ2v9i0/Zx857u30ZMZoeeDW5uCIK64bSVOvzxghS24piakYR9j8q1fDB3CMh6E7VTWnsyA==;20:JK4Yc8cX6jDga21fiv+wnHm4QMuYAroIs5hc4rdqfg+TNO/k7+NxmZ6NPL0sgE64Pfo0iR/QY7iBbgmB9Xp62SxAbfXTVBoB6IcTvieCBJOqsgJpwcnkhWGAxHBJB74JC25gDc2roZ9r2wgwy0nNzDzkgEmgM9ZlWNq6TyeiIru1rVStfvmYT+V2ryIIPeBziKFqJJagqw8KjLfvw6EJ/Y/UcuTNuvHckTzw2jgcFmr8m6Yg59LLOpqqW3LKsQGf X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Apr 2017 08:59:27.6284 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR12MB1297 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5920 Lines: 150 Am 25.04.2017 um 20:20 schrieb Logan Gunthorpe: > This patch introduces functions which kmap the pages inside an sgl. > These functions replace a common pattern of kmap(sg_page(sg)) that is > used in more than 50 places within the kernel. > > The motivation for this work is to eventually safely support sgls that > contain io memory. In order for that to work, any access to the contents > of an iomem SGL will need to be done with iomemcpy or hit some warning. > (The exact details of how this will work have yet to be worked out.) > Having all the kmaps in one place is just a first step in that > direction. Additionally, seeing this helps cut down the users of sg_page, > it should make any effort to go to struct-page-less DMAs a little > easier (should that idea ever swing back into favour again). > > A flags option is added to select between a regular or atomic mapping so > these functions can replace kmap(sg_page or kmap_atomic(sg_page. > Future work may expand this to have flags for using page_address or > vmap. We include a flag to require the function not to fail to > support legacy code that has no easy error path. Much further in the > future, there may be a flag to allocate memory and copy the data > from/to iomem. > > We also add the semantic that sg_map can fail to create a mapping, > despite the fact that the current code this is replacing is assumed to > never fail and the current version of these functions cannot fail. This > is to support iomem which may either have to fail to create the mapping or > allocate memory as a bounce buffer which itself can fail. > > Also, in terms of cleanup, a few of the existing kmap(sg_page) users > play things a bit loose in terms of whether they apply sg->offset > so using these helper functions should help avoid such issues. > > Signed-off-by: Logan Gunthorpe > --- Good to know that somebody is working on this. Those problems troubled us as well. Patch is Acked-by: Christian König . Regards, Christian. > include/linux/scatterlist.h | 85 +++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 85 insertions(+) > > diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h > index cb3c8fe..fad170b 100644 > --- a/include/linux/scatterlist.h > +++ b/include/linux/scatterlist.h > @@ -5,6 +5,7 @@ > #include > #include > #include > +#include > #include > > struct scatterlist { > @@ -126,6 +127,90 @@ static inline struct page *sg_page(struct scatterlist *sg) > return (struct page *)((sg)->page_link & ~0x3); > } > > +#define SG_KMAP (1 << 0) /* create a mapping with kmap */ > +#define SG_KMAP_ATOMIC (1 << 1) /* create a mapping with kmap_atomic */ > +#define SG_MAP_MUST_NOT_FAIL (1 << 2) /* indicate sg_map should not fail */ > + > +/** > + * sg_map - kmap a page inside an sgl > + * @sg: SG entry > + * @offset: Offset into entry > + * @flags: Flags for creating the mapping > + * > + * Description: > + * Use this function to map a page in the scatterlist at the specified > + * offset. sg->offset is already added for you. Note: the semantics of > + * this function are that it may fail. Thus, its output should be checked > + * with IS_ERR and PTR_ERR. Otherwise, a pointer to the specified offset > + * in the mapped page is returned. > + * > + * Flags can be any of: > + * * SG_KMAP - Use kmap to create the mapping > + * * SG_KMAP_ATOMIC - Use kmap_atomic to map the page atommically. > + * Thus, the rules of that function apply: the > + * cpu may not sleep until it is unmaped. > + * * SG_MAP_MUST_NOT_FAIL - Indicate that sg_map must not fail. > + * If it does, it will issue a BUG_ON instead. > + * This is intended for legacy code only, it > + * is not to be used in new code. > + * > + * Also, consider carefully whether this function is appropriate. It is > + * largely not recommended for new code and if the sgl came from another > + * subsystem and you don't know what kind of memory might be in the list > + * then you definitely should not call it. Non-mappable memory may be in > + * the sgl and thus this function may fail unexpectedly. Consider using > + * sg_copy_to_buffer instead. > + **/ > +static inline void *sg_map(struct scatterlist *sg, size_t offset, int flags) > +{ > + struct page *pg; > + unsigned int pg_off; > + void *ret; > + > + offset += sg->offset; > + pg = nth_page(sg_page(sg), offset >> PAGE_SHIFT); > + pg_off = offset_in_page(offset); > + > + if (flags & SG_KMAP_ATOMIC) > + ret = kmap_atomic(pg) + pg_off; > + else if (flags & SG_KMAP) > + ret = kmap(pg) + pg_off; > + else > + ret = ERR_PTR(-EINVAL); > + > + /* > + * In theory, this can't happen yet. Once we start adding > + * unmapable memory, it also shouldn't happen unless developers > + * start putting unmappable struct pages in sgls and passing > + * it to code that doesn't support it. > + */ > + BUG_ON(flags & SG_MAP_MUST_NOT_FAIL && IS_ERR(ret)); > + > + return ret; > +} > + > +/** > + * sg_unmap - unmap a page that was mapped with sg_map_offset > + * @sg: SG entry > + * @addr: address returned by sg_map_offset > + * @offset: Offset into entry (same as specified for sg_map) > + * @flags: Flags, which are the same specified for sg_map > + * > + * Description: > + * Unmap the page that was mapped with sg_map_offset > + **/ > +static inline void sg_unmap(struct scatterlist *sg, void *addr, > + size_t offset, int flags) > +{ > + struct page *pg = nth_page(sg_page(sg), offset >> PAGE_SHIFT); > + unsigned int pg_off = offset_in_page(offset); > + > + if (flags & SG_KMAP_ATOMIC) > + kunmap_atomic(addr - sg->offset - pg_off); > + else if (flags & SG_KMAP) > + kunmap(pg); > +} > + > /** > * sg_set_buf - Set sg entry to point at given data > * @sg: SG entry