| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* RAM Oops/Panic logger |
| 4 |
* |
| 5 |
* Copyright (C) 2010 Marco Stornelli <[email protected]> |
| 6 |
* Copyright (C) 2011 Kees Cook <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 |
|
| 11 |
#include <linux/kernel.h> |
| 12 |
#include <linux/err.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/version.h> |
| 15 |
#include <linux/pstore.h> |
| 16 |
#include <linux/io.h> |
| 17 |
#include <linux/ioport.h> |
| 18 |
#include <linux/platform_device.h> |
| 19 |
#include <linux/slab.h> |
| 20 |
#include <linux/compiler.h> |
| 21 |
#include <linux/of.h> |
| 22 |
#include <linux/of_address.h> |
| 23 |
#include <linux/mm.h> |
| 24 |
|
| 25 |
#include "internal.h" |
| 26 |
#include "ram_internal.h" |
| 27 |
|
| 28 |
#define RAMOOPS_KERNMSG_HDR "====" |
| 29 |
#define MIN_MEM_SIZE 4096UL |
| 30 |
|
| 31 |
static ulong record_size = MIN_MEM_SIZE; |
| 32 |
module_param(record_size, ulong, 0400); |
| 33 |
MODULE_PARM_DESC(record_size, |
| 34 |
"size of each dump done on oops/panic"); |
| 35 |
|
| 36 |
static ulong ramoops_console_size = MIN_MEM_SIZE; |
| 37 |
module_param_named(console_size, ramoops_console_size, ulong, 0400); |
| 38 |
MODULE_PARM_DESC(console_size, "size of kernel console log"); |
| 39 |
|
| 40 |
static ulong ramoops_ftrace_size = MIN_MEM_SIZE; |
| 41 |
module_param_named(ftrace_size, ramoops_ftrace_size, ulong, 0400); |
| 42 |
MODULE_PARM_DESC(ftrace_size, "size of ftrace log"); |
| 43 |
|
| 44 |
static ulong ramoops_pmsg_size = MIN_MEM_SIZE; |
| 45 |
module_param_named(pmsg_size, ramoops_pmsg_size, ulong, 0400); |
| 46 |
MODULE_PARM_DESC(pmsg_size, "size of user space message log"); |
| 47 |
|
| 48 |
static unsigned long long mem_address; |
| 49 |
module_param_hw(mem_address, ullong, other, 0400); |
| 50 |
MODULE_PARM_DESC(mem_address, |
| 51 |
"start of reserved RAM used to store oops/panic logs"); |
| 52 |
|
| 53 |
static char *mem_name; |
| 54 |
module_param_named(mem_name, mem_name, charp, 0400); |
| 55 |
MODULE_PARM_DESC(mem_name, "name of kernel param that holds addr"); |
| 56 |
|
| 57 |
static ulong mem_size; |
| 58 |
module_param(mem_size, ulong, 0400); |
| 59 |
MODULE_PARM_DESC(mem_size, |
| 60 |
"size of reserved RAM used to store oops/panic logs"); |
| 61 |
|
| 62 |
static unsigned int mem_type; |
| 63 |
module_param(mem_type, uint, 0400); |
| 64 |
MODULE_PARM_DESC(mem_type, |
| 65 |
"memory type: 0=write-combined (default), 1=unbuffered, 2=cached"); |
| 66 |
|
| 67 |
static int ramoops_max_reason = -1; |
| 68 |
module_param_named(max_reason, ramoops_max_reason, int, 0400); |
| 69 |
MODULE_PARM_DESC(max_reason, |
| 70 |
"maximum reason for kmsg dump (default 2: Oops and Panic) "); |
| 71 |
|
| 72 |
static int ramoops_ecc; |
| 73 |
module_param_named(ecc, ramoops_ecc, int, 0400); |
| 74 |
MODULE_PARM_DESC(ramoops_ecc, |
| 75 |
"if non-zero, the option enables ECC support and specifies " |
| 76 |
"ECC buffer size in bytes (1 is a special value, means 16 " |
| 77 |
"bytes ECC)"); |
| 78 |
|
| 79 |
static int ramoops_dump_oops = -1; |
| 80 |
module_param_named(dump_oops, ramoops_dump_oops, int, 0400); |
| 81 |
MODULE_PARM_DESC(dump_oops, |
| 82 |
"(deprecated: use max_reason instead) set to 1 to dump oopses & panics, 0 to only dump panics"); |
| 83 |
|
| 84 |
struct ramoops_context { |
| 85 |
struct persistent_ram_zone **dprzs; /* Oops dump zones */ |
| 86 |
struct persistent_ram_zone *cprz; /* Console zone */ |
| 87 |
struct persistent_ram_zone **fprzs; /* Ftrace zones */ |
| 88 |
struct persistent_ram_zone *mprz; /* PMSG zone */ |
| 89 |
phys_addr_t phys_addr; |
| 90 |
unsigned long size; |
| 91 |
unsigned int memtype; |
| 92 |
size_t record_size; |
| 93 |
size_t console_size; |
| 94 |
size_t ftrace_size; |
| 95 |
size_t pmsg_size; |
| 96 |
u32 flags; |
| 97 |
struct persistent_ram_ecc_info ecc_info; |
| 98 |
unsigned int max_dump_cnt; |
| 99 |
unsigned int dump_write_cnt; |
| 100 |
/* _read_cnt need clear on ramoops_pstore_open */ |
| 101 |
unsigned int dump_read_cnt; |
| 102 |
unsigned int console_read_cnt; |
| 103 |
unsigned int max_ftrace_cnt; |
| 104 |
unsigned int ftrace_read_cnt; |
| 105 |
unsigned int pmsg_read_cnt; |
| 106 |
struct pstore_info pstore; |
| 107 |
}; |
| 108 |
|
| 109 |
static struct platform_device *dummy; |
| 110 |
|
| 111 |
static int ramoops_pstore_open(struct pstore_info *psi) |
| 112 |
{ |
| 113 |
struct ramoops_context *cxt = psi->data; |
| 114 |
|
| 115 |
cxt->dump_read_cnt = 0; |
| 116 |
cxt->console_read_cnt = 0; |
| 117 |
cxt->ftrace_read_cnt = 0; |
| 118 |
cxt->pmsg_read_cnt = 0; |
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
static struct persistent_ram_zone * |
| 123 |
ramoops_get_next_prz(struct persistent_ram_zone *przs[], int id, |
| 124 |
struct pstore_record *record) |
| 125 |
{ |
| 126 |
struct persistent_ram_zone *prz; |
| 127 |
|
| 128 |
/* Give up if we never existed or have hit the end. */ |
| 129 |
if (!przs) |
| 130 |
return NULL; |
| 131 |
|
| 132 |
prz = przs[id]; |
| 133 |
if (!prz) |
| 134 |
return NULL; |
| 135 |
|
| 136 |
/* Update old/shadowed buffer. */ |
| 137 |
if (prz->type == PSTORE_TYPE_DMESG) |
| 138 |
persistent_ram_save_old(prz); |
| 139 |
|
| 140 |
if (!persistent_ram_old_size(prz)) |
| 141 |
return NULL; |
| 142 |
|
| 143 |
record->type = prz->type; |
| 144 |
record->id = id; |
| 145 |
|
| 146 |
return prz; |
| 147 |
} |
| 148 |
|
| 149 |
static int ramoops_read_kmsg_hdr(char *buffer, struct timespec64 *time, |
| 150 |
bool *compressed) |
| 151 |
{ |
| 152 |
char data_type; |
| 153 |
int header_length = 0; |
| 154 |
|
| 155 |
if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lld.%lu-%c\n%n", |
| 156 |
(time64_t *)&time->tv_sec, &time->tv_nsec, &data_type, |
| 157 |
&header_length) == 3) { |
| 158 |
time->tv_nsec *= 1000; |
| 159 |
if (data_type == 'C') |
| 160 |
*compressed = true; |
| 161 |
else |
| 162 |
*compressed = false; |
| 163 |
} else if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lld.%lu\n%n", |
| 164 |
(time64_t *)&time->tv_sec, &time->tv_nsec, |
| 165 |
&header_length) == 2) { |
| 166 |
time->tv_nsec *= 1000; |
| 167 |
*compressed = false; |
| 168 |
} else { |
| 169 |
time->tv_sec = 0; |
| 170 |
time->tv_nsec = 0; |
| 171 |
*compressed = false; |
| 172 |
} |
| 173 |
return header_length; |
| 174 |
} |
| 175 |
|
| 176 |
static bool prz_ok(struct persistent_ram_zone *prz) |
| 177 |
{ |
| 178 |
return !!prz && !!(persistent_ram_old_size(prz) + |
| 179 |
persistent_ram_ecc_string(prz, NULL, 0)); |
| 180 |
} |
| 181 |
|
| 182 |
static ssize_t ramoops_pstore_read(struct pstore_record *record) |
| 183 |
{ |
| 184 |
ssize_t size = 0; |
| 185 |
struct ramoops_context *cxt = record->psi->data; |
| 186 |
struct persistent_ram_zone *prz = NULL; |
| 187 |
int header_length = 0; |
| 188 |
bool free_prz = false; |
| 189 |
|
| 190 |
/* |
| 191 |
* Ramoops headers provide time stamps for PSTORE_TYPE_DMESG, but |
| 192 |
* PSTORE_TYPE_CONSOLE and PSTORE_TYPE_FTRACE don't currently have |
| 193 |
* valid time stamps, so it is initialized to zero. |
| 194 |
*/ |
| 195 |
record->time.tv_sec = 0; |
| 196 |
record->time.tv_nsec = 0; |
| 197 |
record->compressed = false; |
| 198 |
|
| 199 |
/* Find the next valid persistent_ram_zone for DMESG */ |
| 200 |
while (cxt->dump_read_cnt < cxt->max_dump_cnt && !prz) { |
| 201 |
prz = ramoops_get_next_prz(cxt->dprzs, cxt->dump_read_cnt++, |
| 202 |
record); |
| 203 |
if (!prz_ok(prz)) |
| 204 |
continue; |
| 205 |
header_length = ramoops_read_kmsg_hdr(persistent_ram_old(prz), |
| 206 |
&record->time, |
| 207 |
&record->compressed); |
| 208 |
/* Clear and skip this DMESG record if it has no valid header */ |
| 209 |
if (!header_length) { |
| 210 |
persistent_ram_free_old(prz); |
| 211 |
persistent_ram_zap(prz); |
| 212 |
prz = NULL; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
if (!prz_ok(prz) && !cxt->console_read_cnt++) |
| 217 |
prz = ramoops_get_next_prz(&cxt->cprz, 0 /* single */, record); |
| 218 |
|
| 219 |
if (!prz_ok(prz) && !cxt->pmsg_read_cnt++) |
| 220 |
prz = ramoops_get_next_prz(&cxt->mprz, 0 /* single */, record); |
| 221 |
|
| 222 |
/* ftrace is last since it may want to dynamically allocate memory. */ |
| 223 |
if (!prz_ok(prz)) { |
| 224 |
if (!(cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU) && |
| 225 |
!cxt->ftrace_read_cnt++) { |
| 226 |
prz = ramoops_get_next_prz(cxt->fprzs, 0 /* single */, |
| 227 |
record); |
| 228 |
} else { |
| 229 |
/* |
| 230 |
* Build a new dummy record which combines all the |
| 231 |
* per-cpu records including metadata and ecc info. |
| 232 |
*/ |
| 233 |
struct persistent_ram_zone *tmp_prz, *prz_next; |
| 234 |
|
| 235 |
tmp_prz = kzalloc_obj(struct persistent_ram_zone); |
| 236 |
if (!tmp_prz) |
| 237 |
return -ENOMEM; |
| 238 |
prz = tmp_prz; |
| 239 |
free_prz = true; |
| 240 |
|
| 241 |
while (cxt->ftrace_read_cnt < cxt->max_ftrace_cnt) { |
| 242 |
prz_next = ramoops_get_next_prz(cxt->fprzs, |
| 243 |
cxt->ftrace_read_cnt++, record); |
| 244 |
|
| 245 |
if (!prz_ok(prz_next)) |
| 246 |
continue; |
| 247 |
|
| 248 |
tmp_prz->ecc_info = prz_next->ecc_info; |
| 249 |
tmp_prz->corrected_bytes += |
| 250 |
prz_next->corrected_bytes; |
| 251 |
tmp_prz->bad_blocks += prz_next->bad_blocks; |
| 252 |
|
| 253 |
size = pstore_ftrace_combine_log( |
| 254 |
&tmp_prz->old_log, |
| 255 |
&tmp_prz->old_log_size, |
| 256 |
prz_next->old_log, |
| 257 |
prz_next->old_log_size); |
| 258 |
if (size) |
| 259 |
goto out; |
| 260 |
} |
| 261 |
record->id = 0; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if (!prz_ok(prz)) { |
| 266 |
size = 0; |
| 267 |
goto out; |
| 268 |
} |
| 269 |
|
| 270 |
size = persistent_ram_old_size(prz) - header_length; |
| 271 |
|
| 272 |
/* ECC correction notice */ |
| 273 |
record->ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0); |
| 274 |
|
| 275 |
record->buf = kvzalloc(size + record->ecc_notice_size + 1, GFP_KERNEL); |
| 276 |
if (record->buf == NULL) { |
| 277 |
size = -ENOMEM; |
| 278 |
goto out; |
| 279 |
} |
| 280 |
|
| 281 |
memcpy(record->buf, (char *)persistent_ram_old(prz) + header_length, |
| 282 |
size); |
| 283 |
|
| 284 |
persistent_ram_ecc_string(prz, record->buf + size, |
| 285 |
record->ecc_notice_size + 1); |
| 286 |
|
| 287 |
out: |
| 288 |
if (free_prz) { |
| 289 |
kvfree(prz->old_log); |
| 290 |
kfree(prz); |
| 291 |
} |
| 292 |
|
| 293 |
return size; |
| 294 |
} |
| 295 |
|
| 296 |
static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz, |
| 297 |
struct pstore_record *record) |
| 298 |
{ |
| 299 |
char hdr[36]; /* "===="(4), %lld(20), "."(1), %06lu(6), "-%c\n"(3) */ |
| 300 |
size_t len; |
| 301 |
|
| 302 |
len = scnprintf(hdr, sizeof(hdr), |
| 303 |
RAMOOPS_KERNMSG_HDR "%lld.%06lu-%c\n", |
| 304 |
(time64_t)record->time.tv_sec, |
| 305 |
record->time.tv_nsec / 1000, |
| 306 |
record->compressed ? 'C' : 'D'); |
| 307 |
persistent_ram_write(prz, hdr, len); |
| 308 |
|
| 309 |
return len; |
| 310 |
} |
| 311 |
|
| 312 |
static int notrace ramoops_pstore_write(struct pstore_record *record) |
| 313 |
{ |
| 314 |
struct ramoops_context *cxt = record->psi->data; |
| 315 |
struct persistent_ram_zone *prz; |
| 316 |
size_t size, hlen; |
| 317 |
|
| 318 |
if (record->type == PSTORE_TYPE_CONSOLE) { |
| 319 |
if (!cxt->cprz) |
| 320 |
return -ENOMEM; |
| 321 |
persistent_ram_write(cxt->cprz, record->buf, record->size); |
| 322 |
return 0; |
| 323 |
} else if (record->type == PSTORE_TYPE_FTRACE) { |
| 324 |
int zonenum; |
| 325 |
|
| 326 |
if (!cxt->fprzs) |
| 327 |
return -ENOMEM; |
| 328 |
/* |
| 329 |
* Choose zone by if we're using per-cpu buffers. |
| 330 |
*/ |
| 331 |
if (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU) |
| 332 |
zonenum = smp_processor_id(); |
| 333 |
else |
| 334 |
zonenum = 0; |
| 335 |
|
| 336 |
persistent_ram_write(cxt->fprzs[zonenum], record->buf, |
| 337 |
record->size); |
| 338 |
return 0; |
| 339 |
} else if (record->type == PSTORE_TYPE_PMSG) { |
| 340 |
pr_warn_ratelimited("PMSG shouldn't call %s\n", __func__); |
| 341 |
return -EINVAL; |
| 342 |
} |
| 343 |
|
| 344 |
if (record->type != PSTORE_TYPE_DMESG) |
| 345 |
return -EINVAL; |
| 346 |
|
| 347 |
/* |
| 348 |
* We could filter on record->reason here if we wanted to (which |
| 349 |
* would duplicate what happened before the "max_reason" setting |
| 350 |
* was added), but that would defeat the purpose of a system |
| 351 |
* changing printk.always_kmsg_dump, so instead log everything that |
| 352 |
* the kmsg dumper sends us, since it should be doing the filtering |
| 353 |
* based on the combination of printk.always_kmsg_dump and our |
| 354 |
* requested "max_reason". |
| 355 |
*/ |
| 356 |
|
| 357 |
/* |
| 358 |
* Explicitly only take the first part of any new crash. |
| 359 |
* If our buffer is larger than kmsg_bytes, this can never happen, |
| 360 |
* and if our buffer is smaller than kmsg_bytes, we don't want the |
| 361 |
* report split across multiple records. |
| 362 |
*/ |
| 363 |
if (record->part != 1) |
| 364 |
return -ENOSPC; |
| 365 |
|
| 366 |
if (!cxt->dprzs) |
| 367 |
return -ENOSPC; |
| 368 |
|
| 369 |
prz = cxt->dprzs[cxt->dump_write_cnt]; |
| 370 |
|
| 371 |
/* |
| 372 |
* Since this is a new crash dump, we need to reset the buffer in |
| 373 |
* case it still has an old dump present. Without this, the new dump |
| 374 |
* will get appended, which would seriously confuse anything trying |
| 375 |
* to check dump file contents. Specifically, ramoops_read_kmsg_hdr() |
| 376 |
* expects to find a dump header in the beginning of buffer data, so |
| 377 |
* we must to reset the buffer values, in order to ensure that the |
| 378 |
* header will be written to the beginning of the buffer. |
| 379 |
*/ |
| 380 |
persistent_ram_zap(prz); |
| 381 |
|
| 382 |
/* Build header and append record contents. */ |
| 383 |
hlen = ramoops_write_kmsg_hdr(prz, record); |
| 384 |
if (!hlen) |
| 385 |
return -ENOMEM; |
| 386 |
|
| 387 |
size = record->size; |
| 388 |
if (size + hlen > prz->buffer_size) |
| 389 |
size = prz->buffer_size - hlen; |
| 390 |
persistent_ram_write(prz, record->buf, size); |
| 391 |
|
| 392 |
cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt; |
| 393 |
|
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
static int notrace ramoops_pstore_write_user(struct pstore_record *record, |
| 398 |
const char __user *buf) |
| 399 |
{ |
| 400 |
if (record->type == PSTORE_TYPE_PMSG) { |
| 401 |
struct ramoops_context *cxt = record->psi->data; |
| 402 |
|
| 403 |
if (!cxt->mprz) |
| 404 |
return -ENOMEM; |
| 405 |
return persistent_ram_write_user(cxt->mprz, buf, record->size); |
| 406 |
} |
| 407 |
|
| 408 |
return -EINVAL; |
| 409 |
} |
| 410 |
|
| 411 |
static int ramoops_pstore_erase(struct pstore_record *record) |
| 412 |
{ |
| 413 |
struct ramoops_context *cxt = record->psi->data; |
| 414 |
struct persistent_ram_zone *prz; |
| 415 |
|
| 416 |
switch (record->type) { |
| 417 |
case PSTORE_TYPE_DMESG: |
| 418 |
if (record->id >= cxt->max_dump_cnt) |
| 419 |
return -EINVAL; |
| 420 |
prz = cxt->dprzs[record->id]; |
| 421 |
break; |
| 422 |
case PSTORE_TYPE_CONSOLE: |
| 423 |
prz = cxt->cprz; |
| 424 |
break; |
| 425 |
case PSTORE_TYPE_FTRACE: |
| 426 |
if (record->id >= cxt->max_ftrace_cnt) |
| 427 |
return -EINVAL; |
| 428 |
prz = cxt->fprzs[record->id]; |
| 429 |
break; |
| 430 |
case PSTORE_TYPE_PMSG: |
| 431 |
prz = cxt->mprz; |
| 432 |
break; |
| 433 |
default: |
| 434 |
return -EINVAL; |
| 435 |
} |
| 436 |
|
| 437 |
persistent_ram_free_old(prz); |
| 438 |
persistent_ram_zap(prz); |
| 439 |
|
| 440 |
return 0; |
| 441 |
} |
| 442 |
|
| 443 |
static struct ramoops_context oops_cxt = { |
| 444 |
.pstore = { |
| 445 |
.owner = THIS_MODULE, |
| 446 |
.name = "ramoops", |
| 447 |
.open = ramoops_pstore_open, |
| 448 |
.read = ramoops_pstore_read, |
| 449 |
.write = ramoops_pstore_write, |
| 450 |
.write_user = ramoops_pstore_write_user, |
| 451 |
.erase = ramoops_pstore_erase, |
| 452 |
}, |
| 453 |
}; |
| 454 |
|
| 455 |
static void ramoops_free_przs(struct ramoops_context *cxt) |
| 456 |
{ |
| 457 |
int i; |
| 458 |
|
| 459 |
/* Free pmsg PRZ */ |
| 460 |
persistent_ram_free(&cxt->mprz); |
| 461 |
|
| 462 |
/* Free console PRZ */ |
| 463 |
persistent_ram_free(&cxt->cprz); |
| 464 |
|
| 465 |
/* Free dump PRZs */ |
| 466 |
if (cxt->dprzs) { |
| 467 |
for (i = 0; i < cxt->max_dump_cnt; i++) |
| 468 |
persistent_ram_free(&cxt->dprzs[i]); |
| 469 |
|
| 470 |
kfree(cxt->dprzs); |
| 471 |
cxt->dprzs = NULL; |
| 472 |
cxt->max_dump_cnt = 0; |
| 473 |
} |
| 474 |
|
| 475 |
/* Free ftrace PRZs */ |
| 476 |
if (cxt->fprzs) { |
| 477 |
for (i = 0; i < cxt->max_ftrace_cnt; i++) |
| 478 |
persistent_ram_free(&cxt->fprzs[i]); |
| 479 |
kfree(cxt->fprzs); |
| 480 |
cxt->fprzs = NULL; |
| 481 |
cxt->max_ftrace_cnt = 0; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
static int ramoops_init_przs(const char *name, |
| 486 |
struct device *dev, struct ramoops_context *cxt, |
| 487 |
struct persistent_ram_zone ***przs, |
| 488 |
phys_addr_t *paddr, size_t mem_sz, |
| 489 |
ssize_t record_size, |
| 490 |
unsigned int *cnt, u32 sig, u32 flags) |
| 491 |
{ |
| 492 |
int err = -ENOMEM; |
| 493 |
int i; |
| 494 |
size_t zone_sz; |
| 495 |
struct persistent_ram_zone **prz_ar; |
| 496 |
|
| 497 |
/* Allocate nothing for 0 mem_sz or 0 record_size. */ |
| 498 |
if (mem_sz == 0 || record_size == 0) { |
| 499 |
*cnt = 0; |
| 500 |
return 0; |
| 501 |
} |
| 502 |
|
| 503 |
/* |
| 504 |
* If we have a negative record size, calculate it based on |
| 505 |
* mem_sz / *cnt. If we have a positive record size, calculate |
| 506 |
* cnt from mem_sz / record_size. |
| 507 |
*/ |
| 508 |
if (record_size < 0) { |
| 509 |
if (*cnt == 0) |
| 510 |
return 0; |
| 511 |
record_size = mem_sz / *cnt; |
| 512 |
if (record_size == 0) { |
| 513 |
dev_err(dev, "%s record size == 0 (%zu / %u)\n", |
| 514 |
name, mem_sz, *cnt); |
| 515 |
goto fail; |
| 516 |
} |
| 517 |
} else { |
| 518 |
*cnt = mem_sz / record_size; |
| 519 |
if (*cnt == 0) { |
| 520 |
dev_err(dev, "%s record count == 0 (%zu / %zu)\n", |
| 521 |
name, mem_sz, record_size); |
| 522 |
goto fail; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
if (*paddr + mem_sz - cxt->phys_addr > cxt->size) { |
| 527 |
dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n", |
| 528 |
name, |
| 529 |
mem_sz, (unsigned long long)*paddr, |
| 530 |
cxt->size, (unsigned long long)cxt->phys_addr); |
| 531 |
goto fail; |
| 532 |
} |
| 533 |
|
| 534 |
zone_sz = mem_sz / *cnt; |
| 535 |
zone_sz = ALIGN_DOWN(zone_sz, 2); |
| 536 |
if (!zone_sz) { |
| 537 |
dev_err(dev, "%s zone size == 0\n", name); |
| 538 |
goto fail; |
| 539 |
} |
| 540 |
|
| 541 |
prz_ar = kzalloc_objs(**przs, *cnt); |
| 542 |
if (!prz_ar) |
| 543 |
goto fail; |
| 544 |
|
| 545 |
for (i = 0; i < *cnt; i++) { |
| 546 |
char *label; |
| 547 |
|
| 548 |
if (*cnt == 1) |
| 549 |
label = kasprintf(GFP_KERNEL, "ramoops:%s", name); |
| 550 |
else |
| 551 |
label = kasprintf(GFP_KERNEL, "ramoops:%s(%d/%d)", |
| 552 |
name, i, *cnt - 1); |
| 553 |
prz_ar[i] = persistent_ram_new(*paddr, zone_sz, sig, |
| 554 |
&cxt->ecc_info, |
| 555 |
cxt->memtype, flags, label); |
| 556 |
kfree(label); |
| 557 |
if (IS_ERR(prz_ar[i])) { |
| 558 |
err = PTR_ERR(prz_ar[i]); |
| 559 |
dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n", |
| 560 |
name, record_size, |
| 561 |
(unsigned long long)*paddr, err); |
| 562 |
|
| 563 |
while (i > 0) { |
| 564 |
i--; |
| 565 |
persistent_ram_free(&prz_ar[i]); |
| 566 |
} |
| 567 |
kfree(prz_ar); |
| 568 |
prz_ar = NULL; |
| 569 |
goto fail; |
| 570 |
} |
| 571 |
*paddr += zone_sz; |
| 572 |
prz_ar[i]->type = pstore_name_to_type(name); |
| 573 |
} |
| 574 |
|
| 575 |
*przs = prz_ar; |
| 576 |
return 0; |
| 577 |
|
| 578 |
fail: |
| 579 |
*cnt = 0; |
| 580 |
return err; |
| 581 |
} |
| 582 |
|
| 583 |
static int ramoops_init_prz(const char *name, |
| 584 |
struct device *dev, struct ramoops_context *cxt, |
| 585 |
struct persistent_ram_zone **prz, |
| 586 |
phys_addr_t *paddr, size_t sz, u32 sig) |
| 587 |
{ |
| 588 |
char *label; |
| 589 |
|
| 590 |
if (!sz) |
| 591 |
return 0; |
| 592 |
|
| 593 |
if (*paddr + sz - cxt->phys_addr > cxt->size) { |
| 594 |
dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n", |
| 595 |
name, sz, (unsigned long long)*paddr, |
| 596 |
cxt->size, (unsigned long long)cxt->phys_addr); |
| 597 |
return -ENOMEM; |
| 598 |
} |
| 599 |
|
| 600 |
label = kasprintf(GFP_KERNEL, "ramoops:%s", name); |
| 601 |
*prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info, |
| 602 |
cxt->memtype, PRZ_FLAG_ZAP_OLD, label); |
| 603 |
kfree(label); |
| 604 |
if (IS_ERR(*prz)) { |
| 605 |
int err = PTR_ERR(*prz); |
| 606 |
|
| 607 |
dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n", |
| 608 |
name, sz, (unsigned long long)*paddr, err); |
| 609 |
return err; |
| 610 |
} |
| 611 |
|
| 612 |
*paddr += sz; |
| 613 |
(*prz)->type = pstore_name_to_type(name); |
| 614 |
|
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
/* Read a u32 from a dt property and make sure it's safe for an int. */ |
| 619 |
static int ramoops_parse_dt_u32(struct platform_device *pdev, |
| 620 |
const char *propname, |
| 621 |
u32 default_value, u32 *value) |
| 622 |
{ |
| 623 |
u32 val32 = 0; |
| 624 |
int ret; |
| 625 |
|
| 626 |
ret = of_property_read_u32(pdev->dev.of_node, propname, &val32); |
| 627 |
if (ret == -EINVAL) { |
| 628 |
/* field is missing, use default value. */ |
| 629 |
val32 = default_value; |
| 630 |
} else if (ret < 0) { |
| 631 |
dev_err(&pdev->dev, "failed to parse property %s: %d\n", |
| 632 |
propname, ret); |
| 633 |
return ret; |
| 634 |
} |
| 635 |
|
| 636 |
/* Sanity check our results. */ |
| 637 |
if (val32 > INT_MAX) { |
| 638 |
dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32); |
| 639 |
return -EOVERFLOW; |
| 640 |
} |
| 641 |
|
| 642 |
*value = val32; |
| 643 |
return 0; |
| 644 |
} |
| 645 |
|
| 646 |
static int ramoops_parse_dt(struct platform_device *pdev, |
| 647 |
struct ramoops_platform_data *pdata) |
| 648 |
{ |
| 649 |
struct device_node *of_node = pdev->dev.of_node; |
| 650 |
struct device_node *parent_node; |
| 651 |
struct resource *res; |
| 652 |
u32 value; |
| 653 |
int ret; |
| 654 |
|
| 655 |
dev_dbg(&pdev->dev, "using Device Tree\n"); |
| 656 |
|
| 657 |
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 658 |
if (!res) { |
| 659 |
dev_err(&pdev->dev, |
| 660 |
"failed to locate DT /reserved-memory resource\n"); |
| 661 |
return -EINVAL; |
| 662 |
} |
| 663 |
|
| 664 |
pdata->mem_size = resource_size(res); |
| 665 |
pdata->mem_address = res->start; |
| 666 |
/* |
| 667 |
* Setting "unbuffered" is deprecated and will be ignored if |
| 668 |
* "mem_type" is also specified. |
| 669 |
*/ |
| 670 |
pdata->mem_type = of_property_read_bool(of_node, "unbuffered"); |
| 671 |
/* |
| 672 |
* Setting "no-dump-oops" is deprecated and will be ignored if |
| 673 |
* "max_reason" is also specified. |
| 674 |
*/ |
| 675 |
if (of_property_read_bool(of_node, "no-dump-oops")) |
| 676 |
pdata->max_reason = KMSG_DUMP_PANIC; |
| 677 |
else |
| 678 |
pdata->max_reason = KMSG_DUMP_OOPS; |
| 679 |
|
| 680 |
#define parse_u32(name, field, default_value) { \ |
| 681 |
ret = ramoops_parse_dt_u32(pdev, name, default_value, \ |
| 682 |
&value); \ |
| 683 |
if (ret < 0) \ |
| 684 |
return ret; \ |
| 685 |
field = value; \ |
| 686 |
} |
| 687 |
|
| 688 |
parse_u32("mem-type", pdata->mem_type, pdata->mem_type); |
| 689 |
parse_u32("record-size", pdata->record_size, 0); |
| 690 |
parse_u32("console-size", pdata->console_size, 0); |
| 691 |
parse_u32("ftrace-size", pdata->ftrace_size, 0); |
| 692 |
parse_u32("pmsg-size", pdata->pmsg_size, 0); |
| 693 |
parse_u32("ecc-size", pdata->ecc_info.ecc_size, 0); |
| 694 |
parse_u32("flags", pdata->flags, 0); |
| 695 |
parse_u32("max-reason", pdata->max_reason, pdata->max_reason); |
| 696 |
|
| 697 |
#undef parse_u32 |
| 698 |
|
| 699 |
/* |
| 700 |
* Some old Chromebooks relied on the kernel setting the |
| 701 |
* console_size and pmsg_size to the record size since that's |
| 702 |
* what the downstream kernel did. These same Chromebooks had |
| 703 |
* "ramoops" straight under the root node which isn't |
| 704 |
* according to the current upstream bindings (though it was |
| 705 |
* arguably acceptable under a prior version of the bindings). |
| 706 |
* Let's make those old Chromebooks work by detecting that |
| 707 |
* we're not a child of "reserved-memory" and mimicking the |
| 708 |
* expected behavior. |
| 709 |
*/ |
| 710 |
parent_node = of_get_parent(of_node); |
| 711 |
if (!of_node_name_eq(parent_node, "reserved-memory") && |
| 712 |
!pdata->console_size && !pdata->ftrace_size && |
| 713 |
!pdata->pmsg_size && !pdata->ecc_info.ecc_size) { |
| 714 |
pdata->console_size = pdata->record_size; |
| 715 |
pdata->pmsg_size = pdata->record_size; |
| 716 |
} |
| 717 |
of_node_put(parent_node); |
| 718 |
|
| 719 |
return 0; |
| 720 |
} |
| 721 |
|
| 722 |
static int ramoops_probe(struct platform_device *pdev) |
| 723 |
{ |
| 724 |
struct device *dev = &pdev->dev; |
| 725 |
struct ramoops_platform_data *pdata = dev->platform_data; |
| 726 |
struct ramoops_platform_data pdata_local; |
| 727 |
struct ramoops_context *cxt = &oops_cxt; |
| 728 |
size_t dump_mem_sz; |
| 729 |
phys_addr_t paddr; |
| 730 |
int err = -EINVAL; |
| 731 |
|
| 732 |
/* |
| 733 |
* Only a single ramoops area allowed at a time, so fail extra |
| 734 |
* probes. |
| 735 |
*/ |
| 736 |
if (cxt->max_dump_cnt) { |
| 737 |
pr_err("already initialized\n"); |
| 738 |
goto fail_out; |
| 739 |
} |
| 740 |
|
| 741 |
if (dev_of_node(dev) && !pdata) { |
| 742 |
pdata = &pdata_local; |
| 743 |
memset(pdata, 0, sizeof(*pdata)); |
| 744 |
|
| 745 |
err = ramoops_parse_dt(pdev, pdata); |
| 746 |
if (err < 0) |
| 747 |
goto fail_out; |
| 748 |
} |
| 749 |
|
| 750 |
/* Make sure we didn't get bogus platform data pointer. */ |
| 751 |
if (!pdata) { |
| 752 |
pr_err("NULL platform data\n"); |
| 753 |
err = -EINVAL; |
| 754 |
goto fail_out; |
| 755 |
} |
| 756 |
|
| 757 |
if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size && |
| 758 |
!pdata->ftrace_size && !pdata->pmsg_size)) { |
| 759 |
pr_err("The memory size and the record/console size must be " |
| 760 |
"non-zero\n"); |
| 761 |
err = -EINVAL; |
| 762 |
goto fail_out; |
| 763 |
} |
| 764 |
|
| 765 |
if (pdata->record_size && !is_power_of_2(pdata->record_size)) |
| 766 |
pdata->record_size = rounddown_pow_of_two(pdata->record_size); |
| 767 |
if (pdata->console_size && !is_power_of_2(pdata->console_size)) |
| 768 |
pdata->console_size = rounddown_pow_of_two(pdata->console_size); |
| 769 |
if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size)) |
| 770 |
pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size); |
| 771 |
if (pdata->pmsg_size && !is_power_of_2(pdata->pmsg_size)) |
| 772 |
pdata->pmsg_size = rounddown_pow_of_two(pdata->pmsg_size); |
| 773 |
|
| 774 |
cxt->size = pdata->mem_size; |
| 775 |
cxt->phys_addr = pdata->mem_address; |
| 776 |
cxt->memtype = pdata->mem_type; |
| 777 |
cxt->record_size = pdata->record_size; |
| 778 |
cxt->console_size = pdata->console_size; |
| 779 |
cxt->ftrace_size = pdata->ftrace_size; |
| 780 |
cxt->pmsg_size = pdata->pmsg_size; |
| 781 |
cxt->flags = pdata->flags; |
| 782 |
cxt->ecc_info = pdata->ecc_info; |
| 783 |
|
| 784 |
paddr = cxt->phys_addr; |
| 785 |
|
| 786 |
dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size |
| 787 |
- cxt->pmsg_size; |
| 788 |
err = ramoops_init_przs("dmesg", dev, cxt, &cxt->dprzs, &paddr, |
| 789 |
dump_mem_sz, cxt->record_size, |
| 790 |
&cxt->max_dump_cnt, 0, 0); |
| 791 |
if (err) |
| 792 |
goto fail_init; |
| 793 |
|
| 794 |
err = ramoops_init_prz("console", dev, cxt, &cxt->cprz, &paddr, |
| 795 |
cxt->console_size, 0); |
| 796 |
if (err) |
| 797 |
goto fail_init; |
| 798 |
|
| 799 |
err = ramoops_init_prz("pmsg", dev, cxt, &cxt->mprz, &paddr, |
| 800 |
cxt->pmsg_size, 0); |
| 801 |
if (err) |
| 802 |
goto fail_init; |
| 803 |
|
| 804 |
cxt->max_ftrace_cnt = (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU) |
| 805 |
? nr_cpu_ids |
| 806 |
: 1; |
| 807 |
err = ramoops_init_przs("ftrace", dev, cxt, &cxt->fprzs, &paddr, |
| 808 |
cxt->ftrace_size, -1, |
| 809 |
&cxt->max_ftrace_cnt, LINUX_VERSION_CODE, |
| 810 |
(cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU) |
| 811 |
? PRZ_FLAG_NO_LOCK : 0); |
| 812 |
if (err) |
| 813 |
goto fail_init; |
| 814 |
|
| 815 |
cxt->pstore.data = cxt; |
| 816 |
/* |
| 817 |
* Prepare frontend flags based on which areas are initialized. |
| 818 |
* For ramoops_init_przs() cases, the "max count" variable tells |
| 819 |
* if there are regions present. For ramoops_init_prz() cases, |
| 820 |
* the single region size is how to check. |
| 821 |
*/ |
| 822 |
cxt->pstore.flags = 0; |
| 823 |
if (cxt->max_dump_cnt) { |
| 824 |
cxt->pstore.flags |= PSTORE_FLAGS_DMESG; |
| 825 |
cxt->pstore.max_reason = pdata->max_reason; |
| 826 |
} |
| 827 |
if (cxt->console_size) |
| 828 |
cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE; |
| 829 |
if (cxt->max_ftrace_cnt) |
| 830 |
cxt->pstore.flags |= PSTORE_FLAGS_FTRACE; |
| 831 |
if (cxt->pmsg_size) |
| 832 |
cxt->pstore.flags |= PSTORE_FLAGS_PMSG; |
| 833 |
|
| 834 |
/* |
| 835 |
* Since bufsize is only used for dmesg crash dumps, it |
| 836 |
* must match the size of the dprz record (after PRZ header |
| 837 |
* and ECC bytes have been accounted for). |
| 838 |
*/ |
| 839 |
if (cxt->pstore.flags & PSTORE_FLAGS_DMESG) { |
| 840 |
cxt->pstore.bufsize = cxt->dprzs[0]->buffer_size; |
| 841 |
cxt->pstore.buf = kvzalloc(cxt->pstore.bufsize, GFP_KERNEL); |
| 842 |
if (!cxt->pstore.buf) { |
| 843 |
pr_err("cannot allocate pstore crash dump buffer\n"); |
| 844 |
err = -ENOMEM; |
| 845 |
goto fail_clear; |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
err = pstore_register(&cxt->pstore); |
| 850 |
if (err) { |
| 851 |
pr_err("registering with pstore failed\n"); |
| 852 |
goto fail_buf; |
| 853 |
} |
| 854 |
|
| 855 |
/* |
| 856 |
* Update the module parameter variables as well so they are visible |
| 857 |
* through /sys/module/ramoops/parameters/ |
| 858 |
*/ |
| 859 |
mem_size = pdata->mem_size; |
| 860 |
mem_address = pdata->mem_address; |
| 861 |
record_size = pdata->record_size; |
| 862 |
ramoops_max_reason = pdata->max_reason; |
| 863 |
ramoops_console_size = pdata->console_size; |
| 864 |
ramoops_pmsg_size = pdata->pmsg_size; |
| 865 |
ramoops_ftrace_size = pdata->ftrace_size; |
| 866 |
mem_type = pdata->mem_type; |
| 867 |
ramoops_ecc = pdata->ecc_info.ecc_size; |
| 868 |
|
| 869 |
pr_info("using 0x%lx@0x%llx, ecc: %d\n", |
| 870 |
cxt->size, (unsigned long long)cxt->phys_addr, |
| 871 |
cxt->ecc_info.ecc_size); |
| 872 |
|
| 873 |
return 0; |
| 874 |
|
| 875 |
fail_buf: |
| 876 |
kvfree(cxt->pstore.buf); |
| 877 |
fail_clear: |
| 878 |
cxt->pstore.bufsize = 0; |
| 879 |
fail_init: |
| 880 |
ramoops_free_przs(cxt); |
| 881 |
fail_out: |
| 882 |
return err; |
| 883 |
} |
| 884 |
|
| 885 |
static void ramoops_remove(struct platform_device *pdev) |
| 886 |
{ |
| 887 |
struct ramoops_context *cxt = &oops_cxt; |
| 888 |
|
| 889 |
pstore_unregister(&cxt->pstore); |
| 890 |
|
| 891 |
kvfree(cxt->pstore.buf); |
| 892 |
cxt->pstore.bufsize = 0; |
| 893 |
|
| 894 |
ramoops_free_przs(cxt); |
| 895 |
} |
| 896 |
|
| 897 |
static const struct of_device_id dt_match[] = { |
| 898 |
{ .compatible = "ramoops" }, |
| 899 |
{} |
| 900 |
}; |
| 901 |
MODULE_DEVICE_TABLE(of, dt_match); |
| 902 |
|
| 903 |
static struct platform_driver ramoops_driver = { |
| 904 |
.probe = ramoops_probe, |
| 905 |
.remove = ramoops_remove, |
| 906 |
.driver = { |
| 907 |
.name = "ramoops", |
| 908 |
.of_match_table = dt_match, |
| 909 |
}, |
| 910 |
}; |
| 911 |
|
| 912 |
static inline void ramoops_unregister_dummy(void) |
| 913 |
{ |
| 914 |
platform_device_unregister(dummy); |
| 915 |
dummy = NULL; |
| 916 |
} |
| 917 |
|
| 918 |
static void __init ramoops_register_dummy(void) |
| 919 |
{ |
| 920 |
struct ramoops_platform_data pdata; |
| 921 |
|
| 922 |
if (mem_name) { |
| 923 |
phys_addr_t start; |
| 924 |
phys_addr_t size; |
| 925 |
|
| 926 |
if (reserve_mem_find_by_name(mem_name, &start, &size)) { |
| 927 |
mem_address = start; |
| 928 |
mem_size = size; |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
/* |
| 933 |
* Prepare a dummy platform data structure to carry the module |
| 934 |
* parameters. If mem_size isn't set, then there are no module |
| 935 |
* parameters, and we can skip this. |
| 936 |
*/ |
| 937 |
if (!mem_size) |
| 938 |
return; |
| 939 |
|
| 940 |
pr_info("using module parameters\n"); |
| 941 |
|
| 942 |
memset(&pdata, 0, sizeof(pdata)); |
| 943 |
pdata.mem_size = mem_size; |
| 944 |
pdata.mem_address = mem_address; |
| 945 |
pdata.mem_type = mem_type; |
| 946 |
pdata.record_size = record_size; |
| 947 |
pdata.console_size = ramoops_console_size; |
| 948 |
pdata.ftrace_size = ramoops_ftrace_size; |
| 949 |
pdata.pmsg_size = ramoops_pmsg_size; |
| 950 |
/* If "max_reason" is set, its value has priority over "dump_oops". */ |
| 951 |
if (ramoops_max_reason >= 0) |
| 952 |
pdata.max_reason = ramoops_max_reason; |
| 953 |
/* Otherwise, if "dump_oops" is set, parse it into "max_reason". */ |
| 954 |
else if (ramoops_dump_oops != -1) |
| 955 |
pdata.max_reason = ramoops_dump_oops ? KMSG_DUMP_OOPS |
| 956 |
: KMSG_DUMP_PANIC; |
| 957 |
/* And if neither are explicitly set, use the default. */ |
| 958 |
else |
| 959 |
pdata.max_reason = KMSG_DUMP_OOPS; |
| 960 |
pdata.flags = RAMOOPS_FLAG_FTRACE_PER_CPU; |
| 961 |
|
| 962 |
/* |
| 963 |
* For backwards compatibility ramoops.ecc=1 means 16 bytes ECC |
| 964 |
* (using 1 byte for ECC isn't much of use anyway). |
| 965 |
*/ |
| 966 |
pdata.ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc; |
| 967 |
|
| 968 |
dummy = platform_device_register_data(NULL, "ramoops", -1, |
| 969 |
&pdata, sizeof(pdata)); |
| 970 |
if (IS_ERR(dummy)) { |
| 971 |
pr_info("could not create platform device: %ld\n", |
| 972 |
PTR_ERR(dummy)); |
| 973 |
dummy = NULL; |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
static int __init ramoops_init(void) |
| 978 |
{ |
| 979 |
int ret; |
| 980 |
|
| 981 |
ramoops_register_dummy(); |
| 982 |
ret = platform_driver_register(&ramoops_driver); |
| 983 |
if (ret != 0) |
| 984 |
ramoops_unregister_dummy(); |
| 985 |
|
| 986 |
return ret; |
| 987 |
} |
| 988 |
postcore_initcall(ramoops_init); |
| 989 |
|
| 990 |
static void __exit ramoops_exit(void) |
| 991 |
{ |
| 992 |
platform_driver_unregister(&ramoops_driver); |
| 993 |
ramoops_unregister_dummy(); |
| 994 |
} |
| 995 |
module_exit(ramoops_exit); |
| 996 |
|
| 997 |
MODULE_LICENSE("GPL"); |
| 998 |
MODULE_AUTHOR("Marco Stornelli <[email protected]>"); |
| 999 |
MODULE_DESCRIPTION("RAM Oops/Panic logger/driver"); |