rcu-string.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2012 Red Hat. All rights reserved.
  4. */
  5. #ifndef BTRFS_RCU_STRING_H
  6. #define BTRFS_RCU_STRING_H
  7. #include <linux/types.h>
  8. #include <linux/string.h>
  9. #include <linux/slab.h>
  10. #include <linux/rcupdate.h>
  11. #include <linux/printk.h>
  12. struct rcu_string {
  13. struct rcu_head rcu;
  14. char str[];
  15. };
  16. static inline struct rcu_string *rcu_string_strdup(const char *src, gfp_t mask)
  17. {
  18. size_t len = strlen(src) + 1;
  19. struct rcu_string *ret = kzalloc(sizeof(struct rcu_string) +
  20. (len * sizeof(char)), mask);
  21. if (!ret)
  22. return ret;
  23. /* Warn if the source got unexpectedly truncated. */
  24. if (WARN_ON(strscpy(ret->str, src, len) < 0)) {
  25. kfree(ret);
  26. return NULL;
  27. }
  28. return ret;
  29. }
  30. static inline void rcu_string_free(struct rcu_string *str)
  31. {
  32. if (str)
  33. kfree_rcu(str, rcu);
  34. }
  35. #define printk_in_rcu(fmt, ...) do { \
  36. rcu_read_lock(); \
  37. printk(fmt, __VA_ARGS__); \
  38. rcu_read_unlock(); \
  39. } while (0)
  40. #define printk_ratelimited_in_rcu(fmt, ...) do { \
  41. rcu_read_lock(); \
  42. printk_ratelimited(fmt, __VA_ARGS__); \
  43. rcu_read_unlock(); \
  44. } while (0)
  45. #define rcu_str_deref(rcu_str) ({ \
  46. struct rcu_string *__str = rcu_dereference(rcu_str); \
  47. __str->str; \
  48. })
  49. #endif