genpool.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * MCE event pool management in MCE context
  4. *
  5. * Copyright (C) 2015 Intel Corp.
  6. * Author: Chen, Gong <[email protected]>
  7. */
  8. #include <linux/smp.h>
  9. #include <linux/mm.h>
  10. #include <linux/genalloc.h>
  11. #include <linux/llist.h>
  12. #include "internal.h"
  13. /*
  14. * printk() is not safe in MCE context. This is a lock-less memory allocator
  15. * used to save error information organized in a lock-less list.
  16. *
  17. * This memory pool is only to be used to save MCE records in MCE context.
  18. * MCE events are rare, so a fixed size memory pool should be enough. Use
  19. * 2 pages to save MCE events for now (~80 MCE records at most).
  20. */
  21. #define MCE_POOLSZ (2 * PAGE_SIZE)
  22. static struct gen_pool *mce_evt_pool;
  23. static LLIST_HEAD(mce_event_llist);
  24. static char gen_pool_buf[MCE_POOLSZ];
  25. /*
  26. * Compare the record "t" with each of the records on list "l" to see if
  27. * an equivalent one is present in the list.
  28. */
  29. static bool is_duplicate_mce_record(struct mce_evt_llist *t, struct mce_evt_llist *l)
  30. {
  31. struct mce_evt_llist *node;
  32. struct mce *m1, *m2;
  33. m1 = &t->mce;
  34. llist_for_each_entry(node, &l->llnode, llnode) {
  35. m2 = &node->mce;
  36. if (!mce_cmp(m1, m2))
  37. return true;
  38. }
  39. return false;
  40. }
  41. /*
  42. * The system has panicked - we'd like to peruse the list of MCE records
  43. * that have been queued, but not seen by anyone yet. The list is in
  44. * reverse time order, so we need to reverse it. While doing that we can
  45. * also drop duplicate records (these were logged because some banks are
  46. * shared between cores or by all threads on a socket).
  47. */
  48. struct llist_node *mce_gen_pool_prepare_records(void)
  49. {
  50. struct llist_node *head;
  51. LLIST_HEAD(new_head);
  52. struct mce_evt_llist *node, *t;
  53. head = llist_del_all(&mce_event_llist);
  54. if (!head)
  55. return NULL;
  56. /* squeeze out duplicates while reversing order */
  57. llist_for_each_entry_safe(node, t, head, llnode) {
  58. if (!is_duplicate_mce_record(node, t))
  59. llist_add(&node->llnode, &new_head);
  60. }
  61. return new_head.first;
  62. }
  63. void mce_gen_pool_process(struct work_struct *__unused)
  64. {
  65. struct llist_node *head;
  66. struct mce_evt_llist *node, *tmp;
  67. struct mce *mce;
  68. head = llist_del_all(&mce_event_llist);
  69. if (!head)
  70. return;
  71. head = llist_reverse_order(head);
  72. llist_for_each_entry_safe(node, tmp, head, llnode) {
  73. mce = &node->mce;
  74. blocking_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
  75. gen_pool_free(mce_evt_pool, (unsigned long)node, sizeof(*node));
  76. }
  77. }
  78. bool mce_gen_pool_empty(void)
  79. {
  80. return llist_empty(&mce_event_llist);
  81. }
  82. int mce_gen_pool_add(struct mce *mce)
  83. {
  84. struct mce_evt_llist *node;
  85. if (filter_mce(mce))
  86. return -EINVAL;
  87. if (!mce_evt_pool)
  88. return -EINVAL;
  89. node = (void *)gen_pool_alloc(mce_evt_pool, sizeof(*node));
  90. if (!node) {
  91. pr_warn_ratelimited("MCE records pool full!\n");
  92. return -ENOMEM;
  93. }
  94. memcpy(&node->mce, mce, sizeof(*mce));
  95. llist_add(&node->llnode, &mce_event_llist);
  96. return 0;
  97. }
  98. static int mce_gen_pool_create(void)
  99. {
  100. struct gen_pool *tmpp;
  101. int ret = -ENOMEM;
  102. tmpp = gen_pool_create(ilog2(sizeof(struct mce_evt_llist)), -1);
  103. if (!tmpp)
  104. goto out;
  105. ret = gen_pool_add(tmpp, (unsigned long)gen_pool_buf, MCE_POOLSZ, -1);
  106. if (ret) {
  107. gen_pool_destroy(tmpp);
  108. goto out;
  109. }
  110. mce_evt_pool = tmpp;
  111. out:
  112. return ret;
  113. }
  114. int mce_gen_pool_init(void)
  115. {
  116. /* Just init mce_gen_pool once. */
  117. if (mce_evt_pool)
  118. return 0;
  119. return mce_gen_pool_create();
  120. }