Skip to content

Commit 1455f0b

Browse files
committed
Convert regulator drivers to use
Merge series from Raag Jadav <[email protected]>: This series converts regulator drivers to use the newly introduced[1] devm_kmemdup_array() helper. [1] https://lore.kernel.org/r/[email protected]
2 parents b80fd34 + c5c4ce6 commit 1455f0b

File tree

8 files changed

+139
-127
lines changed

8 files changed

+139
-127
lines changed

drivers/iio/imu/st_lsm9ds0/st_lsm9ds0_i2c.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Author: Andy Shevchenko <[email protected]>
88
*/
99

10-
#include <linux/device.h>
10+
#include <linux/device/devres.h>
1111
#include <linux/err.h>
1212
#include <linux/gfp_types.h>
1313
#include <linux/i2c.h>

drivers/iio/imu/st_lsm9ds0/st_lsm9ds0_spi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Author: Andy Shevchenko <[email protected]>
88
*/
99

10-
#include <linux/device.h>
10+
#include <linux/device/devres.h>
1111
#include <linux/err.h>
1212
#include <linux/gfp_types.h>
1313
#include <linux/module.h>

drivers/regulator/cros-ec-regulator.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ static int cros_ec_regulator_init_info(struct device *dev,
138138
data->num_voltages =
139139
min_t(u16, ARRAY_SIZE(resp.voltages_mv), resp.num_voltages);
140140
data->voltages_mV =
141-
devm_kmemdup(dev, resp.voltages_mv,
142-
sizeof(u16) * data->num_voltages, GFP_KERNEL);
141+
devm_kmemdup_array(dev, resp.voltages_mv, data->num_voltages,
142+
sizeof(resp.voltages_mv[0]), GFP_KERNEL);
143143
if (!data->voltages_mV)
144144
return -ENOMEM;
145145

drivers/regulator/devres.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,8 @@ int devm_regulator_bulk_get_const(struct device *dev, int num_consumers,
332332
const struct regulator_bulk_data *in_consumers,
333333
struct regulator_bulk_data **out_consumers)
334334
{
335-
*out_consumers = devm_kmemdup(dev, in_consumers,
336-
num_consumers * sizeof(*in_consumers),
337-
GFP_KERNEL);
335+
*out_consumers = devm_kmemdup_array(dev, in_consumers, num_consumers,
336+
sizeof(*in_consumers), GFP_KERNEL);
338337
if (*out_consumers == NULL)
339338
return -ENOMEM;
340339

include/linux/device.h

Lines changed: 1 addition & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
#include <linux/atomic.h>
2727
#include <linux/uidgid.h>
2828
#include <linux/gfp.h>
29-
#include <linux/overflow.h>
3029
#include <linux/device/bus.h>
3130
#include <linux/device/class.h>
31+
#include <linux/device/devres.h>
3232
#include <linux/device/driver.h>
3333
#include <linux/cleanup.h>
3434
#include <asm/device.h>
@@ -281,123 +281,6 @@ int __must_check device_create_bin_file(struct device *dev,
281281
void device_remove_bin_file(struct device *dev,
282282
const struct bin_attribute *attr);
283283

284-
/* device resource management */
285-
typedef void (*dr_release_t)(struct device *dev, void *res);
286-
typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
287-
288-
void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
289-
int nid, const char *name) __malloc;
290-
#define devres_alloc(release, size, gfp) \
291-
__devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
292-
#define devres_alloc_node(release, size, gfp, nid) \
293-
__devres_alloc_node(release, size, gfp, nid, #release)
294-
295-
void devres_for_each_res(struct device *dev, dr_release_t release,
296-
dr_match_t match, void *match_data,
297-
void (*fn)(struct device *, void *, void *),
298-
void *data);
299-
void devres_free(void *res);
300-
void devres_add(struct device *dev, void *res);
301-
void *devres_find(struct device *dev, dr_release_t release,
302-
dr_match_t match, void *match_data);
303-
void *devres_get(struct device *dev, void *new_res,
304-
dr_match_t match, void *match_data);
305-
void *devres_remove(struct device *dev, dr_release_t release,
306-
dr_match_t match, void *match_data);
307-
int devres_destroy(struct device *dev, dr_release_t release,
308-
dr_match_t match, void *match_data);
309-
int devres_release(struct device *dev, dr_release_t release,
310-
dr_match_t match, void *match_data);
311-
312-
/* devres group */
313-
void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp);
314-
void devres_close_group(struct device *dev, void *id);
315-
void devres_remove_group(struct device *dev, void *id);
316-
int devres_release_group(struct device *dev, void *id);
317-
318-
/* managed devm_k.alloc/kfree for device drivers */
319-
void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __alloc_size(2);
320-
void *devm_krealloc(struct device *dev, void *ptr, size_t size,
321-
gfp_t gfp) __must_check __realloc_size(3);
322-
__printf(3, 0) char *devm_kvasprintf(struct device *dev, gfp_t gfp,
323-
const char *fmt, va_list ap) __malloc;
324-
__printf(3, 4) char *devm_kasprintf(struct device *dev, gfp_t gfp,
325-
const char *fmt, ...) __malloc;
326-
static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
327-
{
328-
return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
329-
}
330-
static inline void *devm_kmalloc_array(struct device *dev,
331-
size_t n, size_t size, gfp_t flags)
332-
{
333-
size_t bytes;
334-
335-
if (unlikely(check_mul_overflow(n, size, &bytes)))
336-
return NULL;
337-
338-
return devm_kmalloc(dev, bytes, flags);
339-
}
340-
static inline void *devm_kcalloc(struct device *dev,
341-
size_t n, size_t size, gfp_t flags)
342-
{
343-
return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
344-
}
345-
static inline __realloc_size(3, 4) void * __must_check
346-
devm_krealloc_array(struct device *dev, void *p, size_t new_n, size_t new_size, gfp_t flags)
347-
{
348-
size_t bytes;
349-
350-
if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
351-
return NULL;
352-
353-
return devm_krealloc(dev, p, bytes, flags);
354-
}
355-
356-
void devm_kfree(struct device *dev, const void *p);
357-
char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
358-
const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
359-
void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
360-
__realloc_size(3);
361-
362-
unsigned long devm_get_free_pages(struct device *dev,
363-
gfp_t gfp_mask, unsigned int order);
364-
void devm_free_pages(struct device *dev, unsigned long addr);
365-
366-
#ifdef CONFIG_HAS_IOMEM
367-
void __iomem *devm_ioremap_resource(struct device *dev,
368-
const struct resource *res);
369-
void __iomem *devm_ioremap_resource_wc(struct device *dev,
370-
const struct resource *res);
371-
372-
void __iomem *devm_of_iomap(struct device *dev,
373-
struct device_node *node, int index,
374-
resource_size_t *size);
375-
#else
376-
377-
static inline
378-
void __iomem *devm_ioremap_resource(struct device *dev,
379-
const struct resource *res)
380-
{
381-
return ERR_PTR(-EINVAL);
382-
}
383-
384-
static inline
385-
void __iomem *devm_ioremap_resource_wc(struct device *dev,
386-
const struct resource *res)
387-
{
388-
return ERR_PTR(-EINVAL);
389-
}
390-
391-
static inline
392-
void __iomem *devm_of_iomap(struct device *dev,
393-
struct device_node *node, int index,
394-
resource_size_t *size)
395-
{
396-
return ERR_PTR(-EINVAL);
397-
}
398-
399-
#endif
400-
401284
/* allows to add/remove a custom action to devres stack */
402285
int devm_remove_action_nowarn(struct device *dev, void (*action)(void *), void *data);
403286

include/linux/device/devres.h

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _DEVICE_DEVRES_H_
3+
#define _DEVICE_DEVRES_H_
4+
5+
#include <linux/err.h>
6+
#include <linux/gfp_types.h>
7+
#include <linux/numa.h>
8+
#include <linux/overflow.h>
9+
#include <linux/stdarg.h>
10+
#include <linux/types.h>
11+
12+
struct device;
13+
struct device_node;
14+
struct resource;
15+
16+
/* device resource management */
17+
typedef void (*dr_release_t)(struct device *dev, void *res);
18+
typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
19+
20+
void * __malloc
21+
__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid, const char *name);
22+
#define devres_alloc(release, size, gfp) \
23+
__devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
24+
#define devres_alloc_node(release, size, gfp, nid) \
25+
__devres_alloc_node(release, size, gfp, nid, #release)
26+
27+
void devres_for_each_res(struct device *dev, dr_release_t release,
28+
dr_match_t match, void *match_data,
29+
void (*fn)(struct device *, void *, void *),
30+
void *data);
31+
void devres_free(void *res);
32+
void devres_add(struct device *dev, void *res);
33+
void *devres_find(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
34+
void *devres_get(struct device *dev, void *new_res, dr_match_t match, void *match_data);
35+
void *devres_remove(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
36+
int devres_destroy(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
37+
int devres_release(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
38+
39+
/* devres group */
40+
void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp);
41+
void devres_close_group(struct device *dev, void *id);
42+
void devres_remove_group(struct device *dev, void *id);
43+
int devres_release_group(struct device *dev, void *id);
44+
45+
/* managed devm_k.alloc/kfree for device drivers */
46+
void * __alloc_size(2)
47+
devm_kmalloc(struct device *dev, size_t size, gfp_t gfp);
48+
void * __must_check __realloc_size(3)
49+
devm_krealloc(struct device *dev, void *ptr, size_t size, gfp_t gfp);
50+
static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
51+
{
52+
return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
53+
}
54+
static inline void *devm_kmalloc_array(struct device *dev, size_t n, size_t size, gfp_t flags)
55+
{
56+
size_t bytes;
57+
58+
if (unlikely(check_mul_overflow(n, size, &bytes)))
59+
return NULL;
60+
61+
return devm_kmalloc(dev, bytes, flags);
62+
}
63+
static inline void *devm_kcalloc(struct device *dev, size_t n, size_t size, gfp_t flags)
64+
{
65+
return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
66+
}
67+
static inline __realloc_size(3, 4) void * __must_check
68+
devm_krealloc_array(struct device *dev, void *p, size_t new_n, size_t new_size, gfp_t flags)
69+
{
70+
size_t bytes;
71+
72+
if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
73+
return NULL;
74+
75+
return devm_krealloc(dev, p, bytes, flags);
76+
}
77+
78+
void devm_kfree(struct device *dev, const void *p);
79+
80+
void * __realloc_size(3)
81+
devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp);
82+
static inline void *devm_kmemdup_array(struct device *dev, const void *src,
83+
size_t n, size_t size, gfp_t flags)
84+
{
85+
return devm_kmemdup(dev, src, size_mul(size, n), flags);
86+
}
87+
88+
char * __malloc
89+
devm_kstrdup(struct device *dev, const char *s, gfp_t gfp);
90+
const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
91+
char * __printf(3, 0) __malloc
92+
devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap);
93+
char * __printf(3, 4) __malloc
94+
devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...);
95+
96+
unsigned long devm_get_free_pages(struct device *dev, gfp_t gfp_mask, unsigned int order);
97+
void devm_free_pages(struct device *dev, unsigned long addr);
98+
99+
#ifdef CONFIG_HAS_IOMEM
100+
101+
void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res);
102+
void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res);
103+
104+
void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
105+
resource_size_t *size);
106+
#else
107+
108+
static inline
109+
void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res)
110+
{
111+
return IOMEM_ERR_PTR(-EINVAL);
112+
}
113+
114+
static inline
115+
void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res)
116+
{
117+
return IOMEM_ERR_PTR(-EINVAL);
118+
}
119+
120+
static inline
121+
void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
122+
resource_size_t *size)
123+
{
124+
return IOMEM_ERR_PTR(-EINVAL);
125+
}
126+
127+
#endif
128+
129+
#endif /* _DEVICE_DEVRES_H_ */

include/linux/err.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ static inline void * __must_check ERR_PTR(long error)
4444
/* Return the pointer in the percpu address space. */
4545
#define ERR_PTR_PCPU(error) ((void __percpu *)(unsigned long)ERR_PTR(error))
4646

47+
/* Cast an error pointer to __iomem. */
48+
#define IOMEM_ERR_PTR(error) (__force void __iomem *)ERR_PTR(error)
49+
4750
/**
4851
* PTR_ERR - Extract the error code from an error pointer.
4952
* @ptr: An error pointer.

include/linux/io.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ static inline void devm_ioport_unmap(struct device *dev, void __iomem *addr)
6565
}
6666
#endif
6767

68-
#define IOMEM_ERR_PTR(err) (__force void __iomem *)ERR_PTR(err)
69-
7068
void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
7169
resource_size_t size);
7270
void __iomem *devm_ioremap_uc(struct device *dev, resource_size_t offset,

0 commit comments

Comments
 (0)