Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754056AbbHEPrp (ORCPT ); Wed, 5 Aug 2015 11:47:45 -0400 Received: from mail-bl2on0123.outbound.protection.outlook.com ([65.55.169.123]:27874 "EHLO na01-bl2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753135AbbHEPni (ORCPT ); Wed, 5 Aug 2015 11:43:38 -0400 Authentication-Results: spf=temperror (sender IP is 192.88.168.50) smtp.mailfrom=freescale.com; freescale.mail.onmicrosoft.com; dkim=none (message not signed) header.d=none; From: Madalin Bucur To: CC: , , , , , , , , , Madalin Bucur Subject: [v2 1/9] devres: add devm_alloc_percpu() Date: Wed, 5 Aug 2015 18:41:21 +0300 Message-ID: <1438789289-16304-2-git-send-email-madalin.bucur@freescale.com> X-Mailer: git-send-email 1.8.2.3 In-Reply-To: <1438789289-16304-1-git-send-email-madalin.bucur@freescale.com> References: <1438789289-16304-1-git-send-email-madalin.bucur@freescale.com> Reply-To: X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1;BN1AFFO11OLC002;1:F09UiJBfWqPwZoK5nnz0/DQ5BIc593zAOHfGixmgGFr9Xnb8ugr9x4m9yDEuW8ZFb8By/3VjC9+EbbT8mvdGILcvPibMLo2b+yWmlCV7abkSL0I/ZGmUikXlg0VBwGTTnNFBJj0MpiMEIzZmNSQen+tjvj4qg7+XoJPMcGjxJZ/m/HKq7Tc9TpYQbHiVzgHZFSD09lXcdUXT8TVxfCtQ5Dddj5xno9e8opc+hwaKe26aZEc8KzCW4KDNUIyjWclzCIICpqam6MI7rFM/4Z569O7kg8qOOBIC2tSfz/15OGPBNWMC7UjIR2P1X/hWlNACrPjK0senillzw+JCFeKfzQ== X-Forefront-Antispam-Report: CIP:192.88.168.50;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10019020)(6009001)(2980300002)(199003)(189002)(2950100001)(6806004)(81156007)(5001830100001)(19580395003)(62966003)(50466002)(47776003)(97736004)(68736005)(19580405001)(4001540100001)(105606002)(64706001)(106466001)(50226001)(77156002)(5001860100001)(87936001)(229853001)(48376002)(77096005)(189998001)(104016003)(5001960100002)(5001920100001)(110136002)(5003940100001)(46102003)(36756003)(50986999)(33646002)(53806999)(2351001)(43066003)(86362001)(107886002)(76176999)(4001430100001);DIR:OUT;SFP:1102;SCL:1;SRVR:BL2PR03MB498;H:tx30smr01.am.freescale.net;FPR:;SPF:TempError;PTR:InfoDomainNonexistent;A:3;MX:1;LANG:en; MIME-Version: 1.0 Content-Type: text/plain X-Microsoft-Exchange-Diagnostics: 1;BL2PR03MB498;2:SWP8/DJf3Wok+Xq9j6hbCxuMNV81tdCvLM5Jwv+2eD39aNaJYvh7N4ppSw7Hak3xjHI8aKdxQ5OAOAWgRs9FFjQD5zPketZxFC0w+LnK+IWXmIeCDcUq3t1I58fjOsW0A+PYEdUXdD5LNWDXXVUE5ZS1CUjo4hvui/rOlAuZLpo=;3:2oeCuQ91YdKkaeJidoJ6SynsbmRwiMSUhuQb6pynMXzVd/Rt+Tfx34yJ6XPr9DCelBDkdxqcaGogrKHecRVUTGZTo1V2dQKFwiJ3dZU54IydlGP76z8pk0Z1fgh3k5AnaeQtxYq6U5H6e0E5WZPb4eMMs/avSNHa+8k8yEQ0gRzD9MtEdHyYUlAGczwrjJiVaaA96Qk/yXUUjnnzRoyF54z5ziW45Vxm+fyniqTu5WI=;25:6Mg8k7/5tLmDtGZjG/AMeMGPFJLH+kStQxtYPjn56lt7/2PQjyq3NOnAENT4/x+ht3XKGA0A7RYH5qbdI3eDaoF4dMthgues6fK+Zj6Pn1EOdZ9YVOLe98DFuqbNOAYhCgb9HGo9Hq6fm3D1lFdCtWfx8zAgQampZ7h4nqNgxP4N24rzTCcsiwY6CZPLmqE2ZCkzRXtB+6dlifK1VSiV7b9QhNxP7Vj5XBQqVXFOju2TFp1ZeP1ELyh6v0NJKFw2AEYevumnZ3l136T9vCO84w== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BL2PR03MB498; X-Microsoft-Exchange-Diagnostics: 1;BL2PR03MB498;20:IzwI4niOIZ19bEQM33xYQiNzB9Rcim0WP0PMdABWV3inBuRCn7fRdHQaoYlIQ8X8MZJMt7Qo7VkFzJJ0jcIQ4gz26WZqpTK7MsXhqNspjcrGSYFNjPLhgDXN+IZBRkEupbKJ+sHSej6cMR6pZ2qsZrXGkT0bWgOjzG9oV4jIBOaWQssZkxTuFhKpdykx8d8CwWh8RRsOXLVgeP9GBi6/C3v5nDLkoxvKDlh+cmD9HhB4k0fhi9VwkTz5OtHdbZYZmnU0XH7fwvxYp4MfPCPW5r7O/wT5o2gTzxdZGoO4J9zzdYcyCAXq2lieKjJ/STHwVLBolKIGGckaNaY6JvjvDB4slJOX/957PJ+H7snTnjo=;4:zCDC+FuLlHWexOw5LwycKmEd3kquti5NBpFCYA9IQedTWaNugrkY8LSsJjl/VCUC7pA+EFCkoTTyFfeHf8YsshXhxCLSYm71mO/VY3PPCmpL3vzXhslmA+KPcGVxX+m7rrYhjJh/W0HQ7Ai3IKrxkz+Bcs2xTHWite0joyqhvX5i3xJS2DPI/BbVk9TkH6xjGNY3NeW7FedjblQQRM1vWugeihHmT973t5rINhzghj+Oc/gFySAaa58z+hgu06SkOxWpEh5OwzuyW8O2p1SIPtf+/LFI1axAFy52pWJxKqo= 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:BL2PR03MB498;BCL:0;PCL:0;RULEID:;SRVR:BL2PR03MB498; X-Forefront-PRVS: 06592CCE58 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;BL2PR03MB498;23:e6CldUWfWaqIbiPCGjpDnwCvpNqqV5gg2U7EKFNX+h?= =?us-ascii?Q?WBrwZGeHSDkubTSgDcTl5Rce4dS5tohTq70/GQBvvoNPiUp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NOsdZD8/kSnf7NwbXBogZW+eOPy50wn9z68v4F4OF3/otwBUShx?= =?us-ascii?Q?gzD6/W75mE7YB9w7SK93qs+7yMeBlrw82P9IoqayyOUh4NmRe8RPOyPchg0B?= =?us-ascii?Q?PRBpWFWqdw6nLW1fvXDcsPS+dL8cYfT3DqV84iaz1jxlS4hcdQYrsvr7rNrB?= =?us-ascii?Q?37QHknk/Uce5XPZ0jh5YMrQdC2Xk+d2NFTzD3yMkTsfhOWke1XIQ=3D=3D?= X-Microsoft-Exchange-Diagnostics: 1;BL2PR03MB498;5:PO6usylOjHLWtnRoI7WfZOICCpf2sD9KFPSTI8gX6V+bAFj1oBUpI2qXcUzMbW7vl7bpdGrVfstkOze/XPlOG2ZGdRXvZRpMZung0O9Az3nwedD3otLzw7/g0tTfeWNxY7+qzUhZ15vaFqbZUV0b5A==;24:egQ7ejzWDRTa6Cw9/zrwXpyBUUMcsdP7nPEfVfilXfn8B/8oU1M2WwlRDUwK2XMrI+0Fi7NHeWVJMPkwGHz9z8IXFqVeN6Ob8XZpDZ868FU=;20:6MWtpQJPagWCuv/owrj4wKFjbmWMsFeUzrmCBQ8710TQacQ9sGOvqIKCSHySyIWIu+4V3i0R8tVkZKTVoK7YkA== X-OriginatorOrg: freescale.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Aug 2015 15:43:34.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.168.50];Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL2PR03MB498 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 a2b4ea7..126c25b 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -673,6 +673,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/