303-add-devm_alloc_percpu-support.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. From d33bde3c487c722541ad359e1d22090a78df0c77 Mon Sep 17 00:00:00 2001
  2. From: Zhao Qiang <[email protected]>
  3. Date: Tue, 11 Jul 2017 16:47:18 +0800
  4. Subject: [PATCH] add devm_alloc_percpu support
  5. Signed-off-by: Zhao Qiang <[email protected]>
  6. ---
  7. drivers/base/devres.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++
  8. include/linux/device.h | 19 +++++++++++++++
  9. 2 files changed, 85 insertions(+)
  10. diff --git a/drivers/base/devres.c b/drivers/base/devres.c
  11. index 8fc654f0807b..71d577025285 100644
  12. --- a/drivers/base/devres.c
  13. +++ b/drivers/base/devres.c
  14. @@ -10,6 +10,7 @@
  15. #include <linux/device.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. +#include <linux/percpu.h>
  19. #include "base.h"
  20. @@ -985,3 +986,68 @@ void devm_free_pages(struct device *dev, unsigned long addr)
  21. &devres));
  22. }
  23. EXPORT_SYMBOL_GPL(devm_free_pages);
  24. +
  25. +static void devm_percpu_release(struct device *dev, void *pdata)
  26. +{
  27. + void __percpu *p;
  28. +
  29. + p = *(void __percpu **)pdata;
  30. + free_percpu(p);
  31. +}
  32. +
  33. +static int devm_percpu_match(struct device *dev, void *data, void *p)
  34. +{
  35. + struct devres *devr = container_of(data, struct devres, data);
  36. +
  37. + return *(void **)devr->data == p;
  38. +}
  39. +
  40. +/**
  41. + * __devm_alloc_percpu - Resource-managed alloc_percpu
  42. + * @dev: Device to allocate per-cpu memory for
  43. + * @size: Size of per-cpu memory to allocate
  44. + * @align: Alignment of per-cpu memory to allocate
  45. + *
  46. + * Managed alloc_percpu. Per-cpu memory allocated with this function is
  47. + * automatically freed on driver detach.
  48. + *
  49. + * RETURNS:
  50. + * Pointer to allocated memory on success, NULL on failure.
  51. + */
  52. +void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
  53. + size_t align)
  54. +{
  55. + void *p;
  56. + void __percpu *pcpu;
  57. +
  58. + pcpu = __alloc_percpu(size, align);
  59. + if (!pcpu)
  60. + return NULL;
  61. +
  62. + p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
  63. + if (!p) {
  64. + free_percpu(pcpu);
  65. + return NULL;
  66. + }
  67. +
  68. + *(void __percpu **)p = pcpu;
  69. +
  70. + devres_add(dev, p);
  71. +
  72. + return pcpu;
  73. +}
  74. +EXPORT_SYMBOL_GPL(__devm_alloc_percpu);
  75. +
  76. +/**
  77. + * devm_free_percpu - Resource-managed free_percpu
  78. + * @dev: Device this memory belongs to
  79. + * @pdata: Per-cpu memory to free
  80. + *
  81. + * Free memory allocated with devm_alloc_percpu().
  82. + */
  83. +void devm_free_percpu(struct device *dev, void __percpu *pdata)
  84. +{
  85. + WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match,
  86. + (void *)pdata));
  87. +}
  88. +EXPORT_SYMBOL_GPL(devm_free_percpu);
  89. diff --git a/include/linux/device.h b/include/linux/device.h
  90. index bc41e87a969b..0a2135cbddc9 100644
  91. --- a/include/linux/device.h
  92. +++ b/include/linux/device.h
  93. @@ -686,6 +686,25 @@ void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res);
  94. int devm_add_action(struct device *dev, void (*action)(void *), void *data);
  95. void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
  96. +/**
  97. + * devm_alloc_percpu - Resource-managed alloc_percpu
  98. + * @dev: Device to allocate per-cpu memory for
  99. + * @type: Type to allocate per-cpu memory for
  100. + *
  101. + * Managed alloc_percpu. Per-cpu memory allocated with this function is
  102. + * automatically freed on driver detach.
  103. + *
  104. + * RETURNS:
  105. + * Pointer to allocated memory on success, NULL on failure.
  106. + */
  107. +#define devm_alloc_percpu(dev, type) \
  108. + ((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
  109. + __alignof__(type)))
  110. +
  111. +void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
  112. + size_t align);
  113. +void devm_free_percpu(struct device *dev, void __percpu *pdata);
  114. +
  115. static inline int devm_add_action_or_reset(struct device *dev,
  116. void (*action)(void *), void *data)
  117. {
  118. --
  119. 2.11.1