Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964982AbbLCMwT (ORCPT ); Thu, 3 Dec 2015 07:52:19 -0500 Received: from mail-by2on0120.outbound.protection.outlook.com ([207.46.100.120]:56048 "EHLO na01-by2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751479AbbLCMwR (ORCPT ); Thu, 3 Dec 2015 07:52:17 -0500 Authentication-Results: spf=permerror (sender IP is 192.88.168.50) smtp.mailfrom=freescale.com; freescale.mail.onmicrosoft.com; dkim=none (message not signed) header.d=none;freescale.mail.onmicrosoft.com; dmarc=none action=none header.from=freescale.com; From: Madalin Bucur To: CC: , , , , , , , , , , Madalin Bucur Subject: [net-next v5 1/8] devres: add devm_alloc_percpu() Date: Thu, 3 Dec 2015 15:49:42 +0200 Message-ID: <1449150589-26253-2-git-send-email-madalin.bucur@freescale.com> X-Mailer: git-send-email 1.5.6.5 In-Reply-To: <1449150589-26253-1-git-send-email-madalin.bucur@freescale.com> References: <1449150589-26253-1-git-send-email-madalin.bucur@freescale.com> Reply-To: X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1;BN1BFFO11FD019;1:sxpvdJV1py1aVytK8q2tW6PYSOpHinCFPjdIKltUfF17Qtg9is0YaDU2ldrtWQxxXxER/967XUo+IMaXjL1LdP/nebw25KunIZeDt/LJ7dLbxouZbUoduFfi0YUnPnVv3SuKedCocZgLNE7FLruiy0tREG9/TsdbfKTyW7exlgwru7jmxEA39nZs2Wb/Y/BfxRyHbcP3nMpK37B2ZkOFXg8/iCuP1LpKqoNBS5ndiLeEA0/gVIPOCH2AHmWPjUmwxaCvm2ikbsC4hkmuAmomah/EBO7AQocmqS3LEnSBQ94WzbbSLMOTSgjyEEf5iJSgq9XquHUFkmV4ZA1Gi3Zrae0yvVDPK0RJCituJdQ2JFSXO64GDKBYlRTX0mubFRMtTW0vLCW1jiA+S+BNV6B0KcK0zOkueGEOvHh+7D7EkTo= X-Forefront-Antispam-Report: CIP:192.88.168.50;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10019020)(6009001)(2980300002)(448002)(199003)(189002)(229853001)(53806999)(47776003)(2950100001)(48376002)(86362001)(50466002)(2351001)(77096005)(33646002)(49486002)(87936001)(19580405001)(85326001)(19580395003)(106466001)(50226001)(107886002)(5003940100001)(586003)(5001960100002)(110136002)(5008740100001)(43066003)(76176999)(97736004)(50986999)(81156007)(189998001)(1220700001)(1096002)(4001430100002)(36756003)(104016004)(6806005);DIR:OUT;SFP:1102;SCL:1;SRVR:BLUPR03MB501;H:tx30smr01.am.freescale.net;FPR:;SPF:PermError;PTR:InfoDomainNonexistent;A:1;MX:1;LANG:en; MIME-Version: 1.0 Content-Type: text/plain X-Microsoft-Exchange-Diagnostics: 1;BLUPR03MB501;2:cob+4N9KSwsaxip2sCIdnzE7NIf7Vv7JezLAYCPCp/IdUZUQMSZJY7b5sG9WnH376wWRJ1zKuvM6ebxP/uGGKYoNXqxzjE8XB/Fezfbg7gjK8LcJ1jDqSymqaDMd6eCt+gdJl0ApDZTRFYxw/TAWcg==;3:s0C20lFOq3CMeOzstj0NY4GPUk/Sd/aR7w3BaO5z8iIBPhQRiCL9i7aKwv6f+JQA7wgORJaT8YJHIMl8FylVLzEaru1uv6NeWzjXG0G4lSC/YxpJ4LvXSbzyWn5WU94CsUqxFOC76mDie4qZrZ3xCPgPGlIyBBwamzFJP4jDzf9cvPmD3nCKoLU+RRyNBHDChFzfMyLKu0dvvnW95Wxsw6Ml4NMl/jX0CYj1Wuj1kas=;25:Cfbz3AMm368tbAK/vf7OzbJJfgaUe9rpYfANNy0p42AYgwGGbQiN9BLrT1+/Msmqhkf9CVIbWmqVXfaORllxArsjk8Hyhur686IEdxBGlWq6NJT9AJIolRZY+pnDaolVASP6Jkj41FijkGBfIPdLFiyuiUZndxl39n1QQzOH4d/n4iAZBXIBLbNc4jBWVfloQnEiM1+jzm9kW18gvfFPs1WmMXzPyk4cs0/7E9AZ53J+hHDZ4msLmihNz4j2ZSJ/evQHJPszWPjz9UVXk88VcQ== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BLUPR03MB501; X-Microsoft-Exchange-Diagnostics: 1;BLUPR03MB501;20:YBFlX1TNz3MG6uKzd2QGoKyhfXHSnkDr27xwxsV9rI2NKE4Nlm+7nss7ctKlwsqd8ZpO3xhRJUxlBFBpINT9bl2OGiqZ6bbtwu7RM737lGjVCLMbRI5tmPAsXcjVqs2xuWEa9OLzcArT+tEaLlcHzhJZ9NJl50wIF4EQANBXF2ZAiajekBCZcjb1TM3HQMNrrVkWpKXXLW24ARk5oyrzVZ4nDL/1oxbte7F/I7n8A3fv1dZxQwVM2M8pMBYJ9oj4qsmVv0jnCagbCpO+fx9vuf5+cCK1iG1POcfHUPGNlR3EF/VQzuK3ypyEf/KP5krJTut+HuCMyYUnxGsaJtiIBc2931Ghyl9Muz0SCyzE13s=;4:eO2GzboNOMHoXcZ/0pNpYMRLrFVr2rAF2w/0u2jZoCyCXyIA5nxLdzt30BI1/I8HQjVVfVz4XxTAxtkOfJkUjnmpbC+8KeZmnHVhfxrOJ5TWRzJodXrX8QDPzH33vq6in06CFqxnVaxn46+7giR28OsdfiOvPu1YbIsB8VvXxRES8vhlEnArsGynhLaaViIJS1aQBqIWBGjmSfKlXy74syWI8tsrG3BF41eNh5JXVa/AVOz9HXupG2TeYgkmc1BrkiLQqM/QfBz+DJ8/Qzr8InTzgYKMyPM/GYP+ngBxzDe05n/qaIW/STfN2VT/NOSGN8cka4hKFWn2rcc3Jq7tI3XcnfKwB5hWPIXcT66oESuvXXGjjAZVE4asm/Gmsn4nhZqYGqQE+JuiUUXPtqReKZ8dBp77I1r2iNQUaWzix1Y/eQ8CFWGjzdgBvA5K879A X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(101931422205132); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(2401047)(8121501046)(520078)(5005006)(10201501046)(3002001);SRVR:BLUPR03MB501;BCL:0;PCL:0;RULEID:;SRVR:BLUPR03MB501; X-Forefront-PRVS: 077929D941 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;BLUPR03MB501;23:l8a2Ur5Fe3IJ+iXth3yD5gEGPxsy1qACLvBEb2VCov?= =?us-ascii?Q?GccUoLtKnWIXmhQ89oxQVC1s0gFbeMayYPAa7W7LMjHBx5+/4AuFukS/Lx0b?= =?us-ascii?Q?bqqi8g6qT2PZjTqkiMKRIZPm6EXZrd1QQWR2MuFdn/tToR1ScqaSBoX4K8sC?= =?us-ascii?Q?dCqcf5T1Tx+qzN+1GwkdDJwJ1OaQVhGrHPPP7BK000OQ04y6yBjziMXl+La6?= =?us-ascii?Q?W9QDp/T8Y6kB8VmUfI1Ogt5WEUUabmN9ll0et8mXPTWebW38VG3GPuRuRwTq?= =?us-ascii?Q?EAYvJOnNZ46h3vky0gHTImFmaMCPhE3R4eRttg0xntLAwXN+rq3YVV2/Vv78?= =?us-ascii?Q?PdPb4xSoAK79apPB99GKAONAXxtjkLK585crS0IcZqu1N0/jR3wkxsZDULRI?= =?us-ascii?Q?e6OvkeynzKN7hc55O0dnWIGIKoNM/E6NMFAvFfTaaJdYvlGWeLZpILVQbjdV?= =?us-ascii?Q?+WD4eqDnwVwH/vr4hJJ5B6j1qWBu7Sk26G8tfyxz6oOjy0ZdFHYWgyRrPueU?= =?us-ascii?Q?QMIEdOK+evY5wqQ7n7uDdceATT2UDikrlt1RpMORUMPdZPrYL0PcnSCHqhEU?= =?us-ascii?Q?vwGWM6LvfU0ZoYfnTxkXFJZD5AuMt8+VMZfqZ6wh7yublcINZsCSbi9faKt3?= =?us-ascii?Q?DPok43Sr9ncHJB6BdiQL9Pf7FwGH9dftS8KP4ETVq+W/Hkgnvld5CsglcrBC?= =?us-ascii?Q?zIlaYBNfTqUsqocghCx2daslcWTNkJQEqPlfF8jSdOTFTTxTMid+qeQ2nU+N?= =?us-ascii?Q?BtqQKSfUnsq+ZH1ghSByCr9Cd2pNG2AlDLy5Gtvk2TnX9awVQRl9Mt++7GqH?= =?us-ascii?Q?/AUEptyvorlRV3DX1DoIoHUIW3Mlc3b0ysIxALDseP9lb6VLqBX3Oc2KK6g2?= =?us-ascii?Q?uu4+QglcGR8zKA8JKQO7NggxkEelOw9hGspElLkD1EpR7xrIuc2w1DhQZZcP?= =?us-ascii?Q?+35/YMesoPPw7UsaQfovCj148l5ugDJTkLeuBT0ywsL68308gCR28PqEpfeN?= =?us-ascii?Q?fl9Kr7zg9GVvpFSQLMu/KCcK4NH0x8hLtX7mOr7Uwqa+ud1A8+pkWTJMbKcJ?= =?us-ascii?Q?SjLUI=3D?= X-Microsoft-Exchange-Diagnostics: 1;BLUPR03MB501;5:M/nzJ/11yKimWCGN4Ow/7SQAIqttfHgGDo/Gri3Busbd6WoolLToSOAnWBVvWVloEPA+vwgCIbejEL7nZ14wPqcgECU4hRO5wIVRBpNuq0pR6DwqBlZ9zUGQO8JNXbhOyTWCNqnEG+SRxBFekwIIqw==;24:uEel52gMLL3rOmm1Lt/sUrOEJBkcp+/N/gsZ20lnuhxwpQzQKFi75nlxZwBOTGoU6JXYtayYyoqqBmrtbQU7aJNWv35n3cCNNLrpoYgy98A= X-OriginatorOrg: freescale.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2015 12:52:08.2363 (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.168.50];Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR03MB501 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4384 Lines: 144 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/