mce-genpool.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. void mce_gen_pool_process(void)
  27. {
  28. struct llist_node *head;
  29. struct mce_evt_llist *node;
  30. struct mce *mce;
  31. head = llist_del_all(&mce_event_llist);
  32. if (!head)
  33. return;
  34. head = llist_reverse_order(head);
  35. llist_for_each_entry(node, head, llnode) {
  36. mce = &node->mce;
  37. atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
  38. gen_pool_free(mce_evt_pool, (unsigned long)node, sizeof(*node));
  39. }
  40. }
  41. bool mce_gen_pool_empty(void)
  42. {
  43. return llist_empty(&mce_event_llist);
  44. }
  45. int mce_gen_pool_add(struct mce *mce)
  46. {
  47. struct mce_evt_llist *node;
  48. if (!mce_evt_pool)
  49. return -EINVAL;
  50. node = (void *)gen_pool_alloc(mce_evt_pool, sizeof(*node));
  51. if (!node) {
  52. pr_warn_ratelimited("MCE records pool full!\n");
  53. return -ENOMEM;
  54. }
  55. memcpy(&node->mce, mce, sizeof(*mce));
  56. llist_add(&node->llnode, &mce_event_llist);
  57. return 0;
  58. }
  59. static int mce_gen_pool_create(void)
  60. {
  61. struct gen_pool *tmpp;
  62. int ret = -ENOMEM;
  63. tmpp = gen_pool_create(ilog2(sizeof(struct mce_evt_llist)), -1);
  64. if (!tmpp)
  65. goto out;
  66. ret = gen_pool_add(tmpp, (unsigned long)gen_pool_buf, MCE_POOLSZ, -1);
  67. if (ret) {
  68. gen_pool_destroy(tmpp);
  69. goto out;
  70. }
  71. mce_evt_pool = tmpp;
  72. out:
  73. return ret;
  74. }
  75. int mce_gen_pool_init(void)
  76. {
  77. /* Just init mce_gen_pool once. */
  78. if (mce_evt_pool)
  79. return 0;
  80. return mce_gen_pool_create();
  81. }