cpufreq.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <linux/cpufreq.h>
  12. #include "sched.h"
  13. DEFINE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
  14. /**
  15. * cpufreq_add_update_util_hook - Populate the CPU's update_util_data pointer.
  16. * @cpu: The CPU to set the pointer for.
  17. * @data: New pointer value.
  18. * @func: Callback function to set for the CPU.
  19. *
  20. * Set and publish the update_util_data pointer for the given CPU.
  21. *
  22. * The update_util_data pointer of @cpu is set to @data and the callback
  23. * function pointer in the target struct update_util_data is set to @func.
  24. * That function will be called by cpufreq_update_util() from RCU-sched
  25. * read-side critical sections, so it must not sleep. @data will always be
  26. * passed to it as the first argument which allows the function to get to the
  27. * target update_util_data structure and its container.
  28. *
  29. * The update_util_data pointer of @cpu must be NULL when this function is
  30. * called or it will WARN() and return with no effect.
  31. */
  32. void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data,
  33. void (*func)(struct update_util_data *data, u64 time,
  34. unsigned int flags))
  35. {
  36. if (WARN_ON(!data || !func))
  37. return;
  38. if (WARN_ON(per_cpu(cpufreq_update_util_data, cpu)))
  39. return;
  40. data->func = func;
  41. rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data);
  42. }
  43. EXPORT_SYMBOL_GPL(cpufreq_add_update_util_hook);
  44. /**
  45. * cpufreq_remove_update_util_hook - Clear the CPU's update_util_data pointer.
  46. * @cpu: The CPU to clear the pointer for.
  47. *
  48. * Clear the update_util_data pointer for the given CPU.
  49. *
  50. * Callers must use RCU-sched callbacks to free any memory that might be
  51. * accessed via the old update_util_data pointer or invoke synchronize_sched()
  52. * right after this function to avoid use-after-free.
  53. */
  54. void cpufreq_remove_update_util_hook(int cpu)
  55. {
  56. rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), NULL);
  57. }
  58. EXPORT_SYMBOL_GPL(cpufreq_remove_update_util_hook);
  59. /**
  60. * cpufreq_this_cpu_can_update - Check if cpufreq policy can be updated.
  61. * @policy: cpufreq policy to check.
  62. *
  63. * Return 'true' if:
  64. * - the local and remote CPUs share @policy,
  65. * - dvfs_possible_from_any_cpu is set in @policy and the local CPU is not going
  66. * offline (in which case it is not expected to run cpufreq updates any more).
  67. */
  68. bool cpufreq_this_cpu_can_update(struct cpufreq_policy *policy)
  69. {
  70. return cpumask_test_cpu(smp_processor_id(), policy->cpus) ||
  71. (policy->dvfs_possible_from_any_cpu &&
  72. rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data)));
  73. }