of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / arch / x86 / kernel / cpu / mcheck / mce-genpool.c
blob0a850100c5944641717c797e5d4f129b2cf9a79f
1 /*
2 * MCE event pool management in MCE context
4 * Copyright (C) 2015 Intel Corp.
5 * Author: Chen, Gong <gong.chen@linux.intel.com>
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"
16 * printk() is not safe in MCE context. This is a lock-less memory allocator
17 * used to save error information organized in a lock-less list.
19 * This memory pool is only to be used to save MCE records in MCE context.
20 * MCE events are rare, so a fixed size memory pool should be enough. Use
21 * 2 pages to save MCE events for now (~80 MCE records at most).
23 #define MCE_POOLSZ (2 * PAGE_SIZE)
25 static struct gen_pool *mce_evt_pool;
26 static LLIST_HEAD(mce_event_llist);
27 static char gen_pool_buf[MCE_POOLSZ];
29 void mce_gen_pool_process(void)
31 struct llist_node *head;
32 struct mce_evt_llist *node;
33 struct mce *mce;
35 head = llist_del_all(&mce_event_llist);
36 if (!head)
37 return;
39 head = llist_reverse_order(head);
40 llist_for_each_entry(node, head, llnode) {
41 mce = &node->mce;
42 atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
43 gen_pool_free(mce_evt_pool, (unsigned long)node, sizeof(*node));
47 bool mce_gen_pool_empty(void)
49 return llist_empty(&mce_event_llist);
52 int mce_gen_pool_add(struct mce *mce)
54 struct mce_evt_llist *node;
56 if (!mce_evt_pool)
57 return -EINVAL;
59 node = (void *)gen_pool_alloc(mce_evt_pool, sizeof(*node));
60 if (!node) {
61 pr_warn_ratelimited("MCE records pool full!\n");
62 return -ENOMEM;
65 memcpy(&node->mce, mce, sizeof(*mce));
66 llist_add(&node->llnode, &mce_event_llist);
68 return 0;
71 static int mce_gen_pool_create(void)
73 struct gen_pool *tmpp;
74 int ret = -ENOMEM;
76 tmpp = gen_pool_create(ilog2(sizeof(struct mce_evt_llist)), -1);
77 if (!tmpp)
78 goto out;
80 ret = gen_pool_add(tmpp, (unsigned long)gen_pool_buf, MCE_POOLSZ, -1);
81 if (ret) {
82 gen_pool_destroy(tmpp);
83 goto out;
86 mce_evt_pool = tmpp;
88 out:
89 return ret;
92 int mce_gen_pool_init(void)
94 /* Just init mce_gen_pool once. */
95 if (mce_evt_pool)
96 return 0;
98 return mce_gen_pool_create();