forked from mchehab/rasdaemon
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ras-page-isolation.c
340 lines (293 loc) · 8.16 KB
/
ras-page-isolation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
*/
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "ras-logger.h"
#include "ras-page-isolation.h"
#define PARSED_ENV_LEN 50
static const struct config threshold_units[] = {
{ "m", 1000 },
{ "k", 1000 },
{ "", 1 },
{}
};
static const struct config cycle_units[] = {
{ "d", 24 },
{ "h", 60 },
{ "m", 60 },
{ "s", 1 },
{}
};
static struct isolation threshold = {
.name = "PAGE_CE_THRESHOLD",
.units = threshold_units,
.env = "50",
.unit = "",
};
static struct isolation cycle = {
.name = "PAGE_CE_REFRESH_CYCLE",
.units = cycle_units,
.env = "24h",
.unit = "h",
};
static const char * const kernel_offline[] = {
[OFFLINE_SOFT] = "/sys/devices/system/memory/soft_offline_page",
[OFFLINE_HARD] = "/sys/devices/system/memory/hard_offline_page",
[OFFLINE_SOFT_THEN_HARD] = "/sys/devices/system/memory/soft_offline_page",
};
static const struct config offline_choice[] = {
{ "off", OFFLINE_OFF },
{ "account", OFFLINE_ACCOUNT },
{ "soft", OFFLINE_SOFT },
{ "hard", OFFLINE_HARD },
{ "soft-then-hard", OFFLINE_SOFT_THEN_HARD },
{}
};
static const char * const page_state[] = {
[PAGE_ONLINE] = "online",
[PAGE_OFFLINE] = "offlined",
[PAGE_OFFLINE_FAILED] = "offline-failed",
};
static enum otype offline = OFFLINE_SOFT;
static struct rb_root page_records;
static void page_offline_init(void)
{
const char *env = "PAGE_CE_ACTION";
char *choice = getenv(env);
const struct config *c = NULL;
int matched = 0;
if (choice) {
for (c = offline_choice; c->name; c++) {
if (!strcasecmp(choice, c->name)) {
offline = c->val;
matched = 1;
break;
}
}
}
if (!matched)
log(TERM, LOG_INFO, "Improper %s, set to default soft\n", env);
if (offline > OFFLINE_ACCOUNT && access(kernel_offline[offline], W_OK)) {
log(TERM, LOG_INFO, "Kernel does not support page offline interface\n");
offline = OFFLINE_ACCOUNT;
}
log(TERM, LOG_INFO, "Page offline choice on Corrected Errors is %s\n",
offline_choice[offline].name);
}
static void parse_isolation_env(struct isolation *config)
{
char *env = getenv(config->name);
char *unit = NULL;
const struct config *units = NULL;
int i, no_unit;
int valid = 0;
int unit_matched = 0;
unsigned long value, tmp;
/* check if env is valid */
if (env && strlen(env)) {
/* All the character before unit must be digit */
for (i = 0; i < strlen(env) - 1; i++) {
if (!isdigit(env[i]))
goto parse;
}
if (sscanf(env, "%lu", &value) < 1 || !value)
goto parse;
/* check if the unit is valid */
unit = env + strlen(env) - 1;
/* no unit, all the character are value character */
if (isdigit(*unit)) {
valid = 1;
no_unit = 1;
goto parse;
}
for (units = config->units; units->name; units++) {
/* value character and unit character are both valid */
if (!strcasecmp(unit, units->name)) {
valid = 1;
no_unit = 0;
break;
}
}
}
parse:
/* if invalid, use default env */
if (valid) {
config->env = env;
if (!no_unit)
config->unit = unit;
} else {
log(TERM, LOG_INFO, "Improper %s, set to default %s.\n",
config->name, config->env);
}
/* if env value string is greater than ulong_max, truncate the last digit */
sscanf(config->env, "%lu", &value);
for (units = config->units; units->name; units++) {
if (!strcasecmp(config->unit, units->name))
unit_matched = 1;
if (unit_matched) {
tmp = value;
value *= units->val;
if (tmp != 0 && value / tmp != units->val)
config->overflow = true;
}
}
config->val = value;
/* In order to output value and unit perfectly */
config->unit = no_unit ? config->unit : "";
}
static void parse_env_string(struct isolation *config, char *str, unsigned int size)
{
int i;
if (config->overflow) {
/* when overflow, use basic unit */
for (i = 0; config->units[i].name; i++)
;
snprintf(str, size, "%lu%s", config->val, config->units[i - 1].name);
log(TERM, LOG_INFO, "%s is set overflow(%s), truncate it\n",
config->name, config->env);
} else {
snprintf(str, size, "%s%s", config->env, config->unit);
}
}
static void page_isolation_init(void)
{
char threshold_string[PARSED_ENV_LEN];
char cycle_string[PARSED_ENV_LEN];
/**
* It's unnecessary to parse threshold configuration when offline
* choice is off.
*/
if (offline == OFFLINE_OFF)
return;
parse_isolation_env(&threshold);
parse_isolation_env(&cycle);
parse_env_string(&threshold, threshold_string, sizeof(threshold_string));
parse_env_string(&cycle, cycle_string, sizeof(cycle_string));
log(TERM, LOG_INFO, "Threshold of memory Corrected Errors is %s / %s\n",
threshold_string, cycle_string);
}
void ras_page_account_init(void)
{
page_offline_init();
page_isolation_init();
}
static int do_page_offline(unsigned long long addr, enum otype type)
{
int fd, rc;
char buf[20];
fd = open(kernel_offline[type], O_WRONLY);
if (fd == -1) {
log(TERM, LOG_ERR, "[%s]:open file: %s failed\n", __func__,
kernel_offline[type]);
return -1;
}
snprintf(buf, sizeof(buf), "%#llx", addr);
rc = write(fd, buf, strlen(buf));
if (rc < 0)
log(TERM, LOG_ERR,
"page offline addr(%s) by %s failed, errno:%d\n",
buf, kernel_offline[type], errno);
close(fd);
return rc;
}
static void page_offline(struct page_record *pr)
{
unsigned long long addr = pr->addr;
int ret;
/* Offlining page is not required */
if (offline <= OFFLINE_ACCOUNT) {
log(TERM, LOG_INFO, "PAGE_CE_ACTION=%s, ignore to offline page at %#llx\n",
offline_choice[offline].name, addr);
return;
}
/* Ignore offlined pages */
if (pr->offlined == PAGE_OFFLINE) {
log(TERM, LOG_INFO, "page at %#llx is already offlined, ignore\n", addr);
return;
}
/* Time to silence this noisy page */
if (offline == OFFLINE_SOFT_THEN_HARD) {
ret = do_page_offline(addr, OFFLINE_SOFT);
if (ret < 0)
ret = do_page_offline(addr, OFFLINE_HARD);
} else {
ret = do_page_offline(addr, offline);
}
pr->offlined = ret < 0 ? PAGE_OFFLINE_FAILED : PAGE_OFFLINE;
log(TERM, LOG_INFO, "Result of offlining page at %#llx: %s\n",
addr, page_state[pr->offlined]);
}
static void page_record(struct page_record *pr, unsigned int count, time_t time)
{
unsigned long period = time - pr->start;
unsigned long tolerate;
if (period >= cycle.val) {
/**
* Since we don't refresh automatically, it is possible that the period
* between two occurrences will be longer than the pre-configured refresh cycle.
* In this case, we tolerate the frequency of the whole period up to
* the pre-configured threshold.
*/
tolerate = (period / (double)cycle.val) * threshold.val;
pr->count -= (tolerate > pr->count) ? pr->count : tolerate;
pr->start = time;
pr->excess = 0;
}
pr->count += count;
if (pr->count >= threshold.val) {
log(TERM, LOG_INFO, "Corrected Errors at %#llx exceeded threshold\n", pr->addr);
/**
* Backup ce count of current cycle to enable next round, which actually
* should never happen if we can disable overflow completely in the same
* time unit (but sadly we can't).
*/
pr->excess += pr->count;
pr->count = 0;
page_offline(pr);
}
}
static struct page_record *page_lookup_insert(unsigned long long addr)
{
struct rb_node **entry = &page_records.rb_node;
struct rb_node *parent = NULL;
struct page_record *pr = NULL, *find = NULL;
while (*entry) {
parent = *entry;
pr = rb_entry(parent, struct page_record, entry);
if (addr == pr->addr)
return pr;
else if (addr < pr->addr)
entry = &(*entry)->rb_left;
else
entry = &(*entry)->rb_right;
}
find = calloc(1, sizeof(struct page_record));
if (!find) {
log(TERM, LOG_ERR, "No memory for page records\n");
return NULL;
}
find->addr = addr;
rb_link_node(&find->entry, parent, entry);
rb_insert_color(&find->entry, &page_records);
return find;
}
void ras_record_page_error(unsigned long long addr, unsigned int count, time_t time)
{
struct page_record *pr = NULL;
if (offline == OFFLINE_OFF)
return;
pr = page_lookup_insert(addr & PAGE_MASK);
if (pr) {
if (!pr->start)
pr->start = time;
page_record(pr, count, time);
}
}