1 From d33bde3c487c722541ad359e1d22090a78df0c77 Mon Sep 17 00:00:00 2001
2 From: Zhao Qiang <qiang.zhao@nxp.com>
3 Date: Tue, 11 Jul 2017 16:47:18 +0800
4 Subject: [PATCH] add devm_alloc_percpu support
6 Signed-off-by: Zhao Qiang <qiang.zhao@nxp.com>
8 drivers/base/devres.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++
9 include/linux/device.h | 19 +++++++++++++++
10 2 files changed, 85 insertions(+)
12 diff --git a/drivers/base/devres.c b/drivers/base/devres.c
13 index 8fc654f0807b..71d577025285 100644
14 --- a/drivers/base/devres.c
15 +++ b/drivers/base/devres.c
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 +#include <linux/percpu.h>
24 @@ -985,3 +986,68 @@ void devm_free_pages(struct device *dev, unsigned long addr)
27 EXPORT_SYMBOL_GPL(devm_free_pages);
29 +static void devm_percpu_release(struct device *dev, void *pdata)
33 + p = *(void __percpu **)pdata;
37 +static int devm_percpu_match(struct device *dev, void *data, void *p)
39 + struct devres *devr = container_of(data, struct devres, data);
41 + return *(void **)devr->data == p;
45 + * __devm_alloc_percpu - Resource-managed alloc_percpu
46 + * @dev: Device to allocate per-cpu memory for
47 + * @size: Size of per-cpu memory to allocate
48 + * @align: Alignment of per-cpu memory to allocate
50 + * Managed alloc_percpu. Per-cpu memory allocated with this function is
51 + * automatically freed on driver detach.
54 + * Pointer to allocated memory on success, NULL on failure.
56 +void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
60 + void __percpu *pcpu;
62 + pcpu = __alloc_percpu(size, align);
66 + p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
72 + *(void __percpu **)p = pcpu;
78 +EXPORT_SYMBOL_GPL(__devm_alloc_percpu);
81 + * devm_free_percpu - Resource-managed free_percpu
82 + * @dev: Device this memory belongs to
83 + * @pdata: Per-cpu memory to free
85 + * Free memory allocated with devm_alloc_percpu().
87 +void devm_free_percpu(struct device *dev, void __percpu *pdata)
89 + WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match,
92 +EXPORT_SYMBOL_GPL(devm_free_percpu);
93 diff --git a/include/linux/device.h b/include/linux/device.h
94 index bc41e87a969b..0a2135cbddc9 100644
95 --- a/include/linux/device.h
96 +++ b/include/linux/device.h
97 @@ -686,6 +686,25 @@ void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res);
98 int devm_add_action(struct device *dev, void (*action)(void *), void *data);
99 void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
102 + * devm_alloc_percpu - Resource-managed alloc_percpu
103 + * @dev: Device to allocate per-cpu memory for
104 + * @type: Type to allocate per-cpu memory for
106 + * Managed alloc_percpu. Per-cpu memory allocated with this function is
107 + * automatically freed on driver detach.
110 + * Pointer to allocated memory on success, NULL on failure.
112 +#define devm_alloc_percpu(dev, type) \
113 + ((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
114 + __alignof__(type)))
116 +void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
118 +void devm_free_percpu(struct device *dev, void __percpu *pdata);
120 static inline int devm_add_action_or_reset(struct device *dev,
121 void (*action)(void *), void *data)