alloc_cache.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef IOU_ALLOC_CACHE_H
  3. #define IOU_ALLOC_CACHE_H
  4. #include <linux/io_uring_types.h>
  5. #include <linux/kasan.h>
  6. /*
  7. * Don't allow the cache to grow beyond this size.
  8. */
  9. #define IO_ALLOC_CACHE_MAX 128
  10. void io_alloc_cache_free(struct io_alloc_cache *cache,
  11. void (*free)(const void *));
  12. bool io_alloc_cache_init(struct io_alloc_cache *cache,
  13. unsigned max_nr, unsigned int size,
  14. unsigned int init_bytes);
  15. void *io_cache_alloc_new(struct io_alloc_cache *cache, gfp_t gfp);
  16. static inline bool io_alloc_cache_put(struct io_alloc_cache *cache,
  17. void *entry)
  18. {
  19. if (cache->nr_cached < cache->max_cached) {
  20. if (!kasan_mempool_poison_object(entry))
  21. return false;
  22. cache->entries[cache->nr_cached++] = entry;
  23. return true;
  24. }
  25. return false;
  26. }
  27. static inline void *io_alloc_cache_get(struct io_alloc_cache *cache)
  28. {
  29. if (cache->nr_cached) {
  30. void *entry = cache->entries[--cache->nr_cached];
  31. /*
  32. * If KASAN is enabled, always clear the initial bytes that
  33. * must be zeroed post alloc, in case any of them overlap
  34. * with KASAN storage.
  35. */
  36. #if defined(CONFIG_KASAN)
  37. kasan_mempool_unpoison_object(entry, cache->elem_size);
  38. if (cache->init_clear)
  39. memset(entry, 0, cache->init_clear);
  40. #endif
  41. return entry;
  42. }
  43. return NULL;
  44. }
  45. static inline void *io_cache_alloc(struct io_alloc_cache *cache, gfp_t gfp)
  46. {
  47. void *obj;
  48. obj = io_alloc_cache_get(cache);
  49. if (obj)
  50. return obj;
  51. return io_cache_alloc_new(cache, gfp);
  52. }
  53. static inline void io_cache_free(struct io_alloc_cache *cache, void *obj)
  54. {
  55. if (!io_alloc_cache_put(cache, obj))
  56. kfree(obj);
  57. }
  58. #endif