cpufreq.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Scheduler code and data structures related to cpufreq.
  3. *
  4. * Copyright (C) 2016, Intel Corporation
  5. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include "sched.h"
  12. DEFINE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
  13. /**
  14. * cpufreq_add_update_util_hook - Populate the CPU's update_util_data pointer.
  15. * @cpu: The CPU to set the pointer for.
  16. * @data: New pointer value.
  17. * @func: Callback function to set for the CPU.
  18. *
  19. * Set and publish the update_util_data pointer for the given CPU.
  20. *
  21. * The update_util_data pointer of @cpu is set to @data and the callback
  22. * function pointer in the target struct update_util_data is set to @func.
  23. * That function will be called by cpufreq_update_util() from RCU-sched
  24. * read-side critical sections, so it must not sleep. @data will always be
  25. * passed to it as the first argument which allows the function to get to the
  26. * target update_util_data structure and its container.
  27. *
  28. * The update_util_data pointer of @cpu must be NULL when this function is
  29. * called or it will WARN() and return with no effect.
  30. */
  31. void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data,
  32. void (*func)(struct update_util_data *data, u64 time,
  33. unsigned int flags))
  34. {
  35. if (WARN_ON(!data || !func))
  36. return;
  37. if (WARN_ON(per_cpu(cpufreq_update_util_data, cpu)))
  38. return;
  39. data->func = func;
  40. rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data);
  41. }
  42. EXPORT_SYMBOL_GPL(cpufreq_add_update_util_hook);
  43. /**
  44. * cpufreq_remove_update_util_hook - Clear the CPU's update_util_data pointer.
  45. * @cpu: The CPU to clear the pointer for.
  46. *
  47. * Clear the update_util_data pointer for the given CPU.
  48. *
  49. * Callers must use RCU-sched callbacks to free any memory that might be
  50. * accessed via the old update_util_data pointer or invoke synchronize_sched()
  51. * right after this function to avoid use-after-free.
  52. */
  53. void cpufreq_remove_update_util_hook(int cpu)
  54. {
  55. rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), NULL);
  56. }
  57. EXPORT_SYMBOL_GPL(cpufreq_remove_update_util_hook);