Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760010AbbLCLLg (ORCPT ); Thu, 3 Dec 2015 06:11:36 -0500 Received: from mail-bl2on0099.outbound.protection.outlook.com ([65.55.169.99]:53184 "EHLO na01-bl2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1759969AbbLCLLZ (ORCPT ); Thu, 3 Dec 2015 06:11:25 -0500 Authentication-Results: spf=none (sender IP is 192.88.168.50) smtp.helo=tx30smr01.am.freescale.net; freescale.mail.onmicrosoft.com; dkim=none (message not signed) header.d=none;freescale.mail.onmicrosoft.com; dmarc=none action=none header.from=; From: <> To: CC: , , , , , , , , , , Madalin Bucur Subject: [net-next v5 1/8] devres: add devm_alloc_percpu() Date: Thu, 3 Dec 2015 14:08:59 +0200 Message-ID: <1449144546-25438-2-git-send-email-madalin.bucur@freescale.com> X-Mailer: git-send-email 1.5.6.5 In-Reply-To: <1449144546-25438-1-git-send-email-madalin.bucur@freescale.com> References: <1449144546-25438-1-git-send-email-madalin.bucur@freescale.com> Reply-To: X-EOPAttributedMessage: 0 X-Matching-Connectors: 130936146830383369;(91ab9b29-cfa4-454e-5278-08d120cd25b8);() X-Microsoft-Exchange-Diagnostics: 1;BY2FFO11FD009;1:vFmhLkKFZ43x1CzcYrvqkr3WqY5sXKkB/hRhZxAn4VYP1kDFjUMN5sT4y7NkK3rEa2diSP4IPp9i2wbdKJ5sggEu/tX1Kg8VJPrcNwOUwZGb0neiuAMq0pVSZ69Eq8RkO3wYXmV/U17qBKbGFgW/s0Lyrz5BPIPSnEs+MTEwCXwYwOdlK/SUXaijrheE0iTLz6bs5FF8b5ZGoSTp+L86Etlrf9cJNR630ld7Kfu9TsMG45ODqaa2rnMKaR74xbwywQszg2SJQxcimrcb3M7hZZIxFPjkjTHqoM9YhBV5x3z/1no69yoRsN9F7xSGigWXCKtrFrxnk0yWG1fhjv7YptDmu6SAFPM1BnKDcDEQjfQDu8faUzyzJSjGsfbp2syNTndm8r2oJbwH8IAB0KTsOxaipBLOIAXLAqhR2Fugm6m1XYcva0j7jRnwweECi/0qnhkmqs1rjNp5PL6eihid7vIeaaobk332fmcehViRR2w= X-Forefront-Antispam-Report: CIP:192.88.168.50;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10009020)(6009001)(2970300002)(428002)(189002)(199003)(47776003)(6806005)(33646002)(87936001)(49486002)(960300001)(50986999)(5008740100001)(1096002)(2950100001)(48376002)(4001430100002)(50466002)(1220700001)(77096005)(104016004)(586003)(2351001)(229853001)(81156007)(97736004)(110136002)(19580395003)(19580405001)(36756003)(107886002)(189998001)(78992003)(5001960100002)(105586002)(5003940100001)(106466001)(5005630100001)(43066003)(76176999)(53806999)(101416001)(50226001)(60522002);DIR:OUT;SFP:1101;SCL:1;SRVR:BLUPR0301MB2116;H:tx30smr01.am.freescale.net;FPR:;SPF:None;PTR:InfoDomainNonexistent;MX:0;A:0;LANG:en; MIME-Version: 1.0 Content-Type: text/plain X-Microsoft-Exchange-Diagnostics: 1;BLUPR0301MB2116;2:zVEouwTgzOTTbS95N5Jddf8oip+M2KZzLn5QRvSTgCVRYJv8W3x7bd8/i2N7pKNw5M/z8I+rPGGW7LRNMk5x+dZeudoNpvz0EjhQSkn5XJI1kr0KFuoNjN6mSgf2GI/EBRN8NnNZsk6p6nMt9AIZRw==;3:S8jzx7UhAuB00s2anMVgYDRWjBFBaOpjl6toFPmXm/NZ/MKX7/EZ83+S4oAm5vg9BRty8uDPf8ASoBq35PirdSN2H9lHwWmz6t57v5aZijvoL+djHqeegc8hc0HiylHs6sjtB5qfhH7bV92Zg3pAg9XK4COaJxs6wf1eLK04bA/6qlBBJxzT3Mv4sMlb5MKJIBD4uHhWi6VE3semgk5xbd+wTsckR8K7Y7W1GGFUfgI=;25:jpjDm6zHg3FKG0PSlzKrFbwU76tJl61wemvAdY1rIqeav1I8RbTAGUsEFBJohlL6GsytCuQGuyPXocJdswXPVnfwUObQ82gEei0l6mMu1NL3vs5ptD/dZXZCaDirdqMoe1iIVCHOsboDqS+q+KMoQifxfX8neMudewZLbVwMR9IZ8xrAdm7AcKpe0BQ550UsDsiYNXu32DwNW2cjCrRSGQsL8aZ/nEVqv2F3K3OnKWj6UMapwR7GIdozSv8tUuI9TIdnhd4UdmWaWIMBs5x9uw== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BLUPR0301MB2116; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(101931422205132); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(2401047)(520078)(5005006)(8121501046)(3002001)(10201501046);SRVR:BLUPR0301MB2116;BCL:0;PCL:0;RULEID:(400006);SRVR:BLUPR0301MB2116; X-Microsoft-Exchange-Diagnostics: 1;BLUPR0301MB2116;4:GY1jLtiWhSTChbaNPWp7whtDBy2daW/oEGmujkvkW8++alG0V5E1X8O9XVqLND1LCc/im844bKxJNvICVpp2vur4A0TK85Yzj7VIJzGSsPaL2gchswdNIi92veiUDqtaf1QiEicQ7yiDjERCZLc5eYIaiqxmHEjM2umKjCHHoZJgZkozhpDMC1RBkNLTOD95I8fjlEZ+t0LEaQcEBXWXBSumopw74jLv+imAehzmaeLk4q4dv69LbY/xw85l9b29mkWLT7BHuhZ7AywJ+83upxLkIIkTYytvQOwr45xN8KYZBGyqWjitdaluTgQ3pOmt/Cd+DL3L4PYJB0o9f4m3UpgeOGULk+pLJOaZpQjekIQesiehDxksfXp1Y1eHCYLHYZqaJFn0ZwgezIcvUxDv2D3QrsZXlNpBHY0rlbL30vFsEd135fWvfXUFrD/OJ9jZ X-Forefront-PRVS: 077929D941 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;BLUPR0301MB2116;23:z3B8FUjXlAVu5HbXV0Y77EA0UFHIJBo72t3sKdv?= =?us-ascii?Q?i7trs/cpB91an1v28dg8Bd9//KNDLUAyr4E5l38YGtF3Qq+L2Mk2V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DiBj7hrLMPum8mcPsjT8rpJWTpdQRhDj2RO4O+jwvr8SMdsTk7?= =?us-ascii?Q?+GAqEYImur5/UV3I68r9E+MSTaoE4+CtNeHJe+7vvBGkiZ11jFBSLRUs2OM6?= =?us-ascii?Q?dKteMLwgsB2sP+hfufV047ECqyqbPQTAf0aNd+QWIiR4WpVE5H8kXTrNsPL7?= =?us-ascii?Q?11UsJwNpx5sRK78NW1tgOtKvauLDj1KC4lX2kmXDRtpabf6GQ9cgofA=3D?= =?us-ascii?Q?=3D?= X-Microsoft-Exchange-Diagnostics: 1;BLUPR0301MB2116;5:dakq949XHEuY2ZjIKblqdIkE8kx52s1a8Y7RZMLx0r3KPBOU6M2a2EYMBKYilSN0FjgoAEzNs84zEczpRtAefUV+LQdzrQ9bv7ZcrAE/pwNPItWHqycWZLu2svBm34+SoDb18CoO0CEbXOSKxOsh03b00C0vx5BIx0F0FnI3xBw=;24:UM0h/XMS37dakKn9ZBGoeRhYnBk4ZnJUHfWa9gj/gSkhnFOrTJuc02UhuUcEHHKKeO5u+rPusunJYv0j0ZwY2X54jbGhdZHgsZtRLtYg12U= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2015 11:11:22.8511 (UTC) X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e;Ip=[192.88.168.50];Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR0301MB2116 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4435 Lines: 146 From: Madalin Bucur 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 Tested-by: Madalin-Cristian 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 8fc654f..77e658f 100644 --- a/drivers/base/devres.c +++ b/drivers/base/devres.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "base.h" @@ -985,3 +986,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: Alignment 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 b8f411b..ef9a86a 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -681,6 +681,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/