storm-watch.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2017 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include "storm-watch.h"
  7. /**
  8. * is_storming(): Check if an event is storming
  9. *
  10. * @data: Data for tracking an event storm
  11. *
  12. * The return value will be true if a storm has been detected and
  13. * false if a storm was not detected.
  14. */
  15. bool is_storming(struct storm_watch *data)
  16. {
  17. ktime_t curr_kt, delta_kt;
  18. bool is_storming = false;
  19. if (!data)
  20. return false;
  21. if (!data->enabled)
  22. return false;
  23. /* max storm count must be greater than 0 */
  24. if (data->max_storm_count <= 0)
  25. return false;
  26. /* the period threshold must be greater than 0ms */
  27. if (data->storm_period_ms <= 0)
  28. return false;
  29. mutex_lock(&data->storm_lock);
  30. curr_kt = ktime_get_boottime();
  31. delta_kt = ktime_sub(curr_kt, data->last_kt);
  32. if (ktime_to_ms(delta_kt) < data->storm_period_ms)
  33. data->storm_count++;
  34. else
  35. data->storm_count = 0;
  36. if (data->storm_count > data->max_storm_count) {
  37. is_storming = true;
  38. data->storm_count = 0;
  39. }
  40. data->last_kt = curr_kt;
  41. mutex_unlock(&data->storm_lock);
  42. return is_storming;
  43. }
  44. void reset_storm_count(struct storm_watch *data)
  45. {
  46. mutex_lock(&data->storm_lock);
  47. data->storm_count = 0;
  48. mutex_unlock(&data->storm_lock);
  49. }
  50. void update_storm_count(struct storm_watch *data, int max_count)
  51. {
  52. if (!data)
  53. return;
  54. mutex_lock(&data->storm_lock);
  55. data->max_storm_count = max_count;
  56. mutex_unlock(&data->storm_lock);
  57. }