xfs_hooks.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2022-2024 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <djwong@kernel.org>
  5. */
  6. #ifndef XFS_HOOKS_H_
  7. #define XFS_HOOKS_H_
  8. #ifdef CONFIG_XFS_LIVE_HOOKS
  9. struct xfs_hooks {
  10. struct blocking_notifier_head head;
  11. };
  12. /*
  13. * If jump labels are enabled in Kconfig, the static key uses nop sleds and
  14. * code patching to eliminate the overhead of taking the rwsem in
  15. * blocking_notifier_call_chain when there are no hooks configured. If not,
  16. * the static key per-call overhead is an atomic read. Most arches that can
  17. * handle XFS also support jump labels.
  18. *
  19. * Note: Patching the kernel code requires taking the cpu hotplug lock. Other
  20. * parts of the kernel allocate memory with that lock held, which means that
  21. * XFS callers cannot hold any locks that might be used by memory reclaim or
  22. * writeback when calling the static_branch_{inc,dec} functions.
  23. */
  24. # define DEFINE_STATIC_XFS_HOOK_SWITCH(name) \
  25. static DEFINE_STATIC_KEY_FALSE(name)
  26. # define xfs_hooks_switch_on(name) static_branch_inc(name)
  27. # define xfs_hooks_switch_off(name) static_branch_dec(name)
  28. # define xfs_hooks_switched_on(name) static_branch_unlikely(name)
  29. struct xfs_hook {
  30. /* This must come at the start of the structure. */
  31. struct notifier_block nb;
  32. };
  33. typedef int (*xfs_hook_fn_t)(struct xfs_hook *hook, unsigned long action,
  34. void *data);
  35. void xfs_hooks_init(struct xfs_hooks *chain);
  36. int xfs_hooks_add(struct xfs_hooks *chain, struct xfs_hook *hook);
  37. void xfs_hooks_del(struct xfs_hooks *chain, struct xfs_hook *hook);
  38. int xfs_hooks_call(struct xfs_hooks *chain, unsigned long action,
  39. void *priv);
  40. static inline void xfs_hook_setup(struct xfs_hook *hook, notifier_fn_t fn)
  41. {
  42. hook->nb.notifier_call = fn;
  43. hook->nb.priority = 0;
  44. }
  45. #else
  46. struct xfs_hooks { /* empty */ };
  47. # define DEFINE_STATIC_XFS_HOOK_SWITCH(name)
  48. # define xfs_hooks_switch_on(name) ((void)0)
  49. # define xfs_hooks_switch_off(name) ((void)0)
  50. # define xfs_hooks_switched_on(name) (false)
  51. # define xfs_hooks_init(chain) ((void)0)
  52. # define xfs_hooks_call(chain, val, priv) (NOTIFY_DONE)
  53. #endif
  54. #endif /* XFS_HOOKS_H_ */