a8573cce00de0488550586e70762d323833063eb
[openwrt/staging/wigyori.git] / target / linux / layerscape / patches-4.9 / 303-add-devm_alloc_percpu-support.patch
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
5
6 Signed-off-by: Zhao Qiang <qiang.zhao@nxp.com>
7 ---
8 drivers/base/devres.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++
9 include/linux/device.h | 19 +++++++++++++++
10 2 files changed, 85 insertions(+)
11
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
16 @@ -10,6 +10,7 @@
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 +#include <linux/percpu.h>
21
22 #include "base.h"
23
24 @@ -985,3 +986,68 @@ void devm_free_pages(struct device *dev, unsigned long addr)
25 &devres));
26 }
27 EXPORT_SYMBOL_GPL(devm_free_pages);
28 +
29 +static void devm_percpu_release(struct device *dev, void *pdata)
30 +{
31 + void __percpu *p;
32 +
33 + p = *(void __percpu **)pdata;
34 + free_percpu(p);
35 +}
36 +
37 +static int devm_percpu_match(struct device *dev, void *data, void *p)
38 +{
39 + struct devres *devr = container_of(data, struct devres, data);
40 +
41 + return *(void **)devr->data == p;
42 +}
43 +
44 +/**
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
49 + *
50 + * Managed alloc_percpu. Per-cpu memory allocated with this function is
51 + * automatically freed on driver detach.
52 + *
53 + * RETURNS:
54 + * Pointer to allocated memory on success, NULL on failure.
55 + */
56 +void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
57 + size_t align)
58 +{
59 + void *p;
60 + void __percpu *pcpu;
61 +
62 + pcpu = __alloc_percpu(size, align);
63 + if (!pcpu)
64 + return NULL;
65 +
66 + p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
67 + if (!p) {
68 + free_percpu(pcpu);
69 + return NULL;
70 + }
71 +
72 + *(void __percpu **)p = pcpu;
73 +
74 + devres_add(dev, p);
75 +
76 + return pcpu;
77 +}
78 +EXPORT_SYMBOL_GPL(__devm_alloc_percpu);
79 +
80 +/**
81 + * devm_free_percpu - Resource-managed free_percpu
82 + * @dev: Device this memory belongs to
83 + * @pdata: Per-cpu memory to free
84 + *
85 + * Free memory allocated with devm_alloc_percpu().
86 + */
87 +void devm_free_percpu(struct device *dev, void __percpu *pdata)
88 +{
89 + WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match,
90 + (void *)pdata));
91 +}
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);
100
101 +/**
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
105 + *
106 + * Managed alloc_percpu. Per-cpu memory allocated with this function is
107 + * automatically freed on driver detach.
108 + *
109 + * RETURNS:
110 + * Pointer to allocated memory on success, NULL on failure.
111 + */
112 +#define devm_alloc_percpu(dev, type) \
113 + ((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
114 + __alignof__(type)))
115 +
116 +void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
117 + size_t align);
118 +void devm_free_percpu(struct device *dev, void __percpu *pdata);
119 +
120 static inline int devm_add_action_or_reset(struct device *dev,
121 void (*action)(void *), void *data)
122 {
123 --
124 2.11.1
125