Skip to content

Commit a21cad9

Browse files
committed
driver core: Split devres APIs to device/devres.h
device.h is a huge header which is hard to follow and easy to miss something. Improve that by splitting devres APIs to device/devres.h. In particular this helps to speedup the build of the code that includes device.h solely for a devres APIs. While at it, cast the error pointers to __iomem using IOMEM_ERR_PTR() and fix sparse warnings. Signed-off-by: Raag Jadav <[email protected]> Acked-by: Arnd Bergmann <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Andy Shevchenko <[email protected]>
1 parent 18311a7 commit a21cad9

File tree

2 files changed

+125
-118
lines changed

2 files changed

+125
-118
lines changed

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: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
83+
char * __malloc
84+
devm_kstrdup(struct device *dev, const char *s, gfp_t gfp);
85+
const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
86+
char * __printf(3, 0) __malloc
87+
devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap);
88+
char * __printf(3, 4) __malloc
89+
devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...);
90+
91+
unsigned long devm_get_free_pages(struct device *dev, gfp_t gfp_mask, unsigned int order);
92+
void devm_free_pages(struct device *dev, unsigned long addr);
93+
94+
#ifdef CONFIG_HAS_IOMEM
95+
96+
void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res);
97+
void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res);
98+
99+
void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
100+
resource_size_t *size);
101+
#else
102+
103+
static inline
104+
void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res)
105+
{
106+
return IOMEM_ERR_PTR(-EINVAL);
107+
}
108+
109+
static inline
110+
void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res)
111+
{
112+
return IOMEM_ERR_PTR(-EINVAL);
113+
}
114+
115+
static inline
116+
void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
117+
resource_size_t *size)
118+
{
119+
return IOMEM_ERR_PTR(-EINVAL);
120+
}
121+
122+
#endif
123+
124+
#endif /* _DEVICE_DEVRES_H_ */

0 commit comments

Comments
 (0)