1 // SPDX-License-Identifier: GPL-2.0-only
3 * MCE event pool management in MCE context
5 * Copyright (C) 2015 Intel Corp.
6 * Author: Chen, Gong <gong.chen@linux.intel.com>
10 #include <linux/genalloc.h>
11 #include <linux/llist.h>
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.
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).
22 #define MCE_POOLSZ (2 * PAGE_SIZE)
24 static struct gen_pool
*mce_evt_pool
;
25 static LLIST_HEAD(mce_event_llist
);
26 static char gen_pool_buf
[MCE_POOLSZ
];
29 * Compare the record "t" with each of the records on list "l" to see if
30 * an equivalent one is present in the list.
32 static bool is_duplicate_mce_record(struct mce_evt_llist
*t
, struct mce_evt_llist
*l
)
34 struct mce_evt_llist
*node
;
39 llist_for_each_entry(node
, &l
->llnode
, llnode
) {
49 * The system has panicked - we'd like to peruse the list of MCE records
50 * that have been queued, but not seen by anyone yet. The list is in
51 * reverse time order, so we need to reverse it. While doing that we can
52 * also drop duplicate records (these were logged because some banks are
53 * shared between cores or by all threads on a socket).
55 struct llist_node
*mce_gen_pool_prepare_records(void)
57 struct llist_node
*head
;
59 struct mce_evt_llist
*node
, *t
;
61 head
= llist_del_all(&mce_event_llist
);
65 /* squeeze out duplicates while reversing order */
66 llist_for_each_entry_safe(node
, t
, head
, llnode
) {
67 if (!is_duplicate_mce_record(node
, t
))
68 llist_add(&node
->llnode
, &new_head
);
71 return new_head
.first
;
74 void mce_gen_pool_process(struct work_struct
*__unused
)
76 struct llist_node
*head
;
77 struct mce_evt_llist
*node
, *tmp
;
80 head
= llist_del_all(&mce_event_llist
);
84 head
= llist_reverse_order(head
);
85 llist_for_each_entry_safe(node
, tmp
, head
, llnode
) {
87 blocking_notifier_call_chain(&x86_mce_decoder_chain
, 0, mce
);
88 gen_pool_free(mce_evt_pool
, (unsigned long)node
, sizeof(*node
));
92 bool mce_gen_pool_empty(void)
94 return llist_empty(&mce_event_llist
);
97 int mce_gen_pool_add(struct mce
*mce
)
99 struct mce_evt_llist
*node
;
107 node
= (void *)gen_pool_alloc(mce_evt_pool
, sizeof(*node
));
109 pr_warn_ratelimited("MCE records pool full!\n");
113 memcpy(&node
->mce
, mce
, sizeof(*mce
));
114 llist_add(&node
->llnode
, &mce_event_llist
);
119 static int mce_gen_pool_create(void)
121 struct gen_pool
*tmpp
;
124 tmpp
= gen_pool_create(ilog2(sizeof(struct mce_evt_llist
)), -1);
128 ret
= gen_pool_add(tmpp
, (unsigned long)gen_pool_buf
, MCE_POOLSZ
, -1);
130 gen_pool_destroy(tmpp
);
140 int mce_gen_pool_init(void)
142 /* Just init mce_gen_pool once. */
146 return mce_gen_pool_create();