Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965625AbbGVQvi (ORCPT ); Wed, 22 Jul 2015 12:51:38 -0400 Received: from mail-bn1on0147.outbound.protection.outlook.com ([157.56.110.147]:50790 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S965573AbbGVQvg (ORCPT ); Wed, 22 Jul 2015 12:51:36 -0400 X-Greylist: delayed 1077 seconds by postgrey-1.27 at vger.kernel.org; Wed, 22 Jul 2015 12:51:36 EDT Authentication-Results: spf=fail (sender IP is 192.88.158.2) smtp.mailfrom=freescale.com; freescale.mail.onmicrosoft.com; dkim=none (message not signed) header.d=none; From: Madalin Bucur To: CC: , , , , , , , , Madalin Bucur Subject: [PATCH 01/10] devres: add devm_alloc_percpu() Date: Wed, 22 Jul 2015 19:16:37 +0300 Message-ID: <1437581806-17420-1-git-send-email-madalin.bucur@freescale.com> X-Mailer: git-send-email 1.5.6.5 Reply-To: X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1;BL2FFO11FD005;1:2KX6j0kWICpkNpEqz2si+dos7W4VLpP63Ivictlj/Q3uBaw+Y2aVTKv8IIYtM0FwOhW91ZcJV3rF+x+msfSZZ7zJLGuGx4lIfFaHv4IOJMj0bRoQoJCERrcmEGS6cMuFVyy086KAmjztpAEGdXs+owUje5wN/NoR9pTS3HIFg+7/JzLAmPMVPp3nbtxMDywtcU0e8E+kabyS50Yq41vZcmvcl/9OhmNUHIPNDSTfU3/iXA3atIcvf/h2X3cEdT+LTxD07df+X65Jjp9CxDlPTQMsnGCm6Sz2RTLHaIJz/xLgjEyib4y5PoqfkZNUGrJlskOAFPdVJP+dJgfy4BhW4jEnzUoUGu95o6xrIZmswTOg4TDSzc4m9Khk/ayUmIEli8VW6Hz2RIQ+EAxr7yRkiwXY0EuwObJWYZFddbWwbqH7zrwC2XAWWOkHDx8G6e3S X-Forefront-Antispam-Report: CIP:192.88.158.2;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10019020)(6009001)(2980300002)(339900001)(189002)(199003)(77156002)(47776003)(19580395003)(105606002)(50986999)(87936001)(49486002)(6806004)(46102003)(62966003)(53806999)(33646002)(106466001)(104016003)(2351001)(229853001)(77096005)(189998001)(19580405001)(85426001)(50226001)(36756003)(86362001)(50466002)(5001960100002)(110136002)(107886002)(43066003)(5003940100001)(48376002)(4001430100001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM2PR03MB557;H:az84smr01.freescale.net;FPR:;SPF:Fail;MLV:sfv;MX:1;A:1;LANG:en; MIME-Version: 1.0 Content-Type: text/plain X-Microsoft-Exchange-Diagnostics: 1;DM2PR03MB557;2:B0UM5GQ4+esiyDFtx+2+hsY1QkDSXaBX0FiNk2FHv7L1QJR/xVr/Qar5BzlU4dvi;3:BtkXAco5cTlSyGqJGmDyEkEBxgRKhV5wxfUKCN1IPUtFRT3r8LP/dE074qlWeCy921uuaRlI2V57H9Xb82l/o8o5gkCumy+tQYsc0jCLvyKCO0qr/tZHUwmtYiCmiw37FFdy+55DgM/Sf/VDR4MUhVup4V3e13W/BCdkSW4sJ6sBsQ69jmAAxRTkt+I5NYuYvBnZ1csNRQ3bBzbXHDWeqpZz5X7mb6WtyEMf9YeusAQ=;25:aO8+4z0VwxD4VQwrlHCSaQX4zQ5RiYU/9a4BCoR89LWuDFTN+Y0RDeZHFqmaphBzzfM0NrkiuEOk/RcqXK7KLmsGkmBXx6YXie6Vr4lo4/DcT7NM29lUQ1+cZus/Hi1At5vcPIxyQCSK8bNR9YdjPJW0tavVnDjauUiJ+H7Fcv0gSh10J7U589civEkksUFIER3TikHVecIx9f9yOw7JRhIqj2Ehu3BLnZwWoK5HoXWT9HB/RShhFyoPAom7TWi6uc8j/bcSxI0z2ohwbawhPw==;20:3eERmW1hHl+oOaVgZtgD2fU1oEySZcBlpvqbVpQ78gvdQ3XePINadR7tJyZMjHrBddyhZCWgHpkDwAj3UqVRQ2BzvMVrqM9W0739sFO32TOfqi6hOhdMsuZbX1jULfz9ZtpfW2NeH4ovolIqMSunhjnIgo3yqMTa2y6IlKb3ZqdjUcImG8PRswy6cHsEJNk4CJv2bJTk8yMSe/Tz0pmANyoLak/vokSaBMXK5CqXN6qUi/beZFvL0MDVooLwTAK+xpBRwMFMo6XhUDTqqnYrBk4LTHw8ZT7vee99AdiDRzGkwnkO1N6x3YWb+rCc2n0BPisGc186WQZdYQeFMKvepQqSS6pThyWQVmrxQ480TDU= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DM2PR03MB557; DM2PR03MB557: X-MS-Exchange-Organization-RulesExecuted X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(5005006)(3002001);SRVR:DM2PR03MB557;BCL:0;PCL:0;RULEID:;SRVR:DM2PR03MB557; X-Microsoft-Exchange-Diagnostics: 1;DM2PR03MB557;4:jd09lo6JhedBP7ME8VBpRyNJoT4Lg1t19aS3xm7nx2u6zdHDacZ18N6hmxk/VC9H1HQ/PM1EzfWCC1EkhMTfzHHCPbMUsR8/6YL6HAJk9g+6/n+VsbzzXouJ6dkDNBSYBeVMlQW/SybBKhr+eN8lZklOw5JgAMrQc/rBp+oIUfS3BQQqwcBoz8naZcFUM3obwVzEWvSd1fZFA7j1BoWoYlebG0pvrCe8X9lFrFQ8ZdqViOicvUSgeoVHs3G3CbN0WXHoizpRxKGlU3KTfD0C0wlrirw4UFkfngvNa+Cq0IM= X-Forefront-PRVS: 0645BEB7AA X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;DM2PR03MB557;23:ySFKH0ET7IW4XVzHARDmq60ObC2lZe5doQTJnUmbWk?= =?us-ascii?Q?I+fg6BkveNt2Fh33BUIjFbLVCvjG8wP191JIJnVrqCj989QKoW2Zp60JYbz0?= =?us-ascii?Q?7hSv3gPErO/L1HN1SSsnZXfuqa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Exchange-Diagnostics: 1;DM2PR03MB557;5:ZhZVneKdWzvxBytE4xaICDKn7n3B+VPDGkphycdc8L8e/D83fzN9/4jsOvCqQcGSCavuUGMXLp2/8aSxlokm5qvNRsZhbLc3nTCypZdMij7vjMuQxWxm1kvh/52Y9udzrPlVWEgHhWPwkNfypJWMgA==;24:dS3NHgZ1V5U4Ss0vb/cvrY/KJYTVK8WBv36XvwaClktVlxt821Et+hMTSFXgoj5XhHHCkb3CYWypj0L//tptmhWHR8w+AuAYxclkyUzoq1s=;20:HWCaKN85/uA30mV/nEgwjhx0/lgO/82aCXUfFWWMY6w5GTozARnPR2z1D+48KKLdYorYidsmJhqF3twOKtjKiA== X-OriginatorOrg: freescale.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2015 16:18:01.2209 (UTC) X-MS-Exchange-CrossTenant-Id: 710a03f5-10f6-4d38-9ff4-a80b81da590d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=710a03f5-10f6-4d38-9ff4-a80b81da590d;Ip=[192.88.158.2];Helo=[az84smr01.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR03MB557 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4321 Lines: 143 Introduce managed counterparts for alloc_percpu() and free_percpu(). Add devm_alloc_percpu() and devm_free_percpu() into the managed interfaces list. Signed-off-by: Madalin Bucur --- Documentation/driver-model/devres.txt | 4 +++ drivers/base/devres.c | 64 +++++++++++++++++++++++++++++++++++ include/linux/device.h | 19 +++++++++++ 3 files changed, 87 insertions(+) diff --git a/Documentation/driver-model/devres.txt b/Documentation/driver-model/devres.txt index 831a536..595fd1b 100644 --- a/Documentation/driver-model/devres.txt +++ b/Documentation/driver-model/devres.txt @@ -312,6 +312,10 @@ MEM devm_kvasprintf() devm_kzalloc() +PER-CPU MEM + devm_alloc_percpu() + devm_free_percpu() + PCI pcim_enable_device() : after success, all PCI ops become managed pcim_pin_device() : keep PCI device enabled after release diff --git a/drivers/base/devres.c b/drivers/base/devres.c index c8a53d1..deb2ea0 100644 --- a/drivers/base/devres.c +++ b/drivers/base/devres.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "base.h" @@ -984,3 +985,66 @@ void devm_free_pages(struct device *dev, unsigned long addr) &devres)); } EXPORT_SYMBOL_GPL(devm_free_pages); + +static void devm_percpu_release(struct device *dev, void *pdata) +{ + void __percpu *p; + + p = *(void __percpu **)pdata; + free_percpu(p); +} + +static int devm_percpu_match(struct device *dev, void *data, void *p) +{ + struct devres *devr = container_of(data, struct devres, data); + + return *(void **)devr->data == p; +} + +/** + * __devm_alloc_percpu - Resource-managed alloc_percpu + * @dev: Device to allocate per-cpu memory for + * @size: Size of per-cpu memory to allocate + * @align: Alignement of per-cpu memory to allocate + * + * Managed alloc_percpu. Per-cpu memory allocated with this function is + * automatically freed on driver detach. + * + * RETURNS: + * Pointer to allocated memory on success, NULL on failure. + */ +void __percpu *__devm_alloc_percpu(struct device *dev, size_t size, + size_t align) +{ + void *p; + void __percpu *pcpu; + + pcpu = __alloc_percpu(size, align); + if (!pcpu) + return NULL; + + p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL); + if (!p) + return NULL; + + *(void __percpu **)p = pcpu; + + devres_add(dev, p); + + return pcpu; +} +EXPORT_SYMBOL_GPL(__devm_alloc_percpu); + +/** + * devm_free_percpu - Resource-managed free_percpu + * @dev: Device this memory belongs to + * @pdata: Per-cpu memory to free + * + * Free memory allocated with devm_alloc_percpu(). + */ +void devm_free_percpu(struct device *dev, void __percpu *pdata) +{ + WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match, + (void *)pdata)); +} +EXPORT_SYMBOL_GPL(devm_free_percpu); diff --git a/include/linux/device.h b/include/linux/device.h index 5a31bf3..f623b55 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -672,6 +672,25 @@ void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res); int devm_add_action(struct device *dev, void (*action)(void *), void *data); void devm_remove_action(struct device *dev, void (*action)(void *), void *data); +/** + * devm_alloc_percpu - Resource-managed alloc_percpu + * @dev: Device to allocate per-cpu memory for + * @type: Type to allocate per-cpu memory for + * + * Managed alloc_percpu. Per-cpu memory allocated with this function is + * automatically freed on driver detach. + * + * RETURNS: + * Pointer to allocated memory on success, NULL on failure. + */ +#define devm_alloc_percpu(dev, type) \ + (typeof(type) __percpu *)__devm_alloc_percpu(dev, sizeof(type), \ + __alignof__(type)) + +void __percpu *__devm_alloc_percpu(struct device *dev, size_t size, + size_t align); +void devm_free_percpu(struct device *dev, void __percpu *pdata); + struct device_dma_parameters { /* * a low level driver may set these to teach IOMMU code about -- 1.7.11.7 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/