mce-genpool.c 3.3 KB

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