rcu-string.h 997 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. struct rcu_string {
  8. struct rcu_head rcu;
  9. char str[];
  10. };
  11. static inline struct rcu_string *rcu_string_strdup(const char *src, gfp_t mask)
  12. {
  13. size_t len = strlen(src) + 1;
  14. struct rcu_string *ret = kzalloc(sizeof(struct rcu_string) +
  15. (len * sizeof(char)), mask);
  16. if (!ret)
  17. return ret;
  18. strncpy(ret->str, src, len);
  19. return ret;
  20. }
  21. static inline void rcu_string_free(struct rcu_string *str)
  22. {
  23. if (str)
  24. kfree_rcu(str, rcu);
  25. }
  26. #define printk_in_rcu(fmt, ...) do { \
  27. rcu_read_lock(); \
  28. printk(fmt, __VA_ARGS__); \
  29. rcu_read_unlock(); \
  30. } while (0)
  31. #define printk_ratelimited_in_rcu(fmt, ...) do { \
  32. rcu_read_lock(); \
  33. printk_ratelimited(fmt, __VA_ARGS__); \
  34. rcu_read_unlock(); \
  35. } while (0)
  36. #define rcu_str_deref(rcu_str) ({ \
  37. struct rcu_string *__str = rcu_dereference(rcu_str); \
  38. __str->str; \
  39. })
  40. #endif