zone.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Provide a pstore intermediate backend, organized into kernel memory
  4. * allocated zones that are then mapped and flushed into a single
  5. * contiguous region on a storage backend of some kind (block, mtd, etc).
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/mount.h>
  12. #include <linux/printk.h>
  13. #include <linux/fs.h>
  14. #include <linux/pstore_zone.h>
  15. #include <linux/kdev_t.h>
  16. #include <linux/device.h>
  17. #include <linux/namei.h>
  18. #include <linux/fcntl.h>
  19. #include <linux/uio.h>
  20. #include <linux/writeback.h>
  21. #include "internal.h"
  22. /**
  23. * struct psz_buffer - header of zone to flush to storage
  24. *
  25. * @sig: signature to indicate header (PSZ_SIG xor PSZONE-type value)
  26. * @datalen: length of data in @data
  27. * @start: offset into @data where the beginning of the stored bytes begin
  28. * @data: zone data.
  29. */
  30. struct psz_buffer {
  31. #define PSZ_SIG (0x43474244) /* DBGC */
  32. uint32_t sig;
  33. atomic_t datalen;
  34. atomic_t start;
  35. uint8_t data[];
  36. };
  37. /**
  38. * struct psz_kmsg_header - kmsg dump-specific header to flush to storage
  39. *
  40. * @magic: magic num for kmsg dump header
  41. * @time: kmsg dump trigger time
  42. * @compressed: whether compressed
  43. * @counter: kmsg dump counter
  44. * @reason: the kmsg dump reason (e.g. oops, panic, etc)
  45. * @data: pointer to log data
  46. *
  47. * This is a sub-header for a kmsg dump, trailing after &psz_buffer.
  48. */
  49. struct psz_kmsg_header {
  50. #define PSTORE_KMSG_HEADER_MAGIC 0x4dfc3ae5 /* Just a random number */
  51. uint32_t magic;
  52. struct timespec64 time;
  53. bool compressed;
  54. uint32_t counter;
  55. enum kmsg_dump_reason reason;
  56. uint8_t data[];
  57. };
  58. /**
  59. * struct pstore_zone - single stored buffer
  60. *
  61. * @off: zone offset of storage
  62. * @type: front-end type for this zone
  63. * @name: front-end name for this zone
  64. * @buffer: pointer to data buffer managed by this zone
  65. * @oldbuf: pointer to old data buffer
  66. * @buffer_size: bytes in @buffer->data
  67. * @should_recover: whether this zone should recover from storage
  68. * @dirty: whether the data in @buffer dirty
  69. *
  70. * zone structure in memory.
  71. */
  72. struct pstore_zone {
  73. loff_t off;
  74. const char *name;
  75. enum pstore_type_id type;
  76. struct psz_buffer *buffer;
  77. struct psz_buffer *oldbuf;
  78. size_t buffer_size;
  79. bool should_recover;
  80. atomic_t dirty;
  81. };
  82. /**
  83. * struct psz_context - all about running state of pstore/zone
  84. *
  85. * @kpszs: kmsg dump storage zones
  86. * @ppsz: pmsg storage zone
  87. * @cpsz: console storage zone
  88. * @fpszs: ftrace storage zones
  89. * @kmsg_max_cnt: max count of @kpszs
  90. * @kmsg_read_cnt: counter of total read kmsg dumps
  91. * @kmsg_write_cnt: counter of total kmsg dump writes
  92. * @pmsg_read_cnt: counter of total read pmsg zone
  93. * @console_read_cnt: counter of total read console zone
  94. * @ftrace_max_cnt: max count of @fpszs
  95. * @ftrace_read_cnt: counter of max read ftrace zone
  96. * @oops_counter: counter of oops dumps
  97. * @panic_counter: counter of panic dumps
  98. * @recovered: whether finished recovering data from storage
  99. * @on_panic: whether panic is happening
  100. * @pstore_zone_info_lock: lock to @pstore_zone_info
  101. * @pstore_zone_info: information from backend
  102. * @pstore: structure for pstore
  103. */
  104. struct psz_context {
  105. struct pstore_zone **kpszs;
  106. struct pstore_zone *ppsz;
  107. struct pstore_zone *cpsz;
  108. struct pstore_zone **fpszs;
  109. unsigned int kmsg_max_cnt;
  110. unsigned int kmsg_read_cnt;
  111. unsigned int kmsg_write_cnt;
  112. unsigned int pmsg_read_cnt;
  113. unsigned int console_read_cnt;
  114. unsigned int ftrace_max_cnt;
  115. unsigned int ftrace_read_cnt;
  116. /*
  117. * These counters should be calculated during recovery.
  118. * It records the oops/panic times after crashes rather than boots.
  119. */
  120. unsigned int oops_counter;
  121. unsigned int panic_counter;
  122. atomic_t recovered;
  123. atomic_t on_panic;
  124. /*
  125. * pstore_zone_info_lock protects this entire structure during calls
  126. * to register_pstore_zone()/unregister_pstore_zone().
  127. */
  128. struct mutex pstore_zone_info_lock;
  129. struct pstore_zone_info *pstore_zone_info;
  130. struct pstore_info pstore;
  131. };
  132. static struct psz_context pstore_zone_cxt;
  133. static void psz_flush_all_dirty_zones(struct work_struct *);
  134. static DECLARE_DELAYED_WORK(psz_cleaner, psz_flush_all_dirty_zones);
  135. /**
  136. * enum psz_flush_mode - flush mode for psz_zone_write()
  137. *
  138. * @FLUSH_NONE: do not flush to storage but update data on memory
  139. * @FLUSH_PART: just flush part of data including meta data to storage
  140. * @FLUSH_META: just flush meta data of zone to storage
  141. * @FLUSH_ALL: flush all of zone
  142. */
  143. enum psz_flush_mode {
  144. FLUSH_NONE = 0,
  145. FLUSH_PART,
  146. FLUSH_META,
  147. FLUSH_ALL,
  148. };
  149. static inline int buffer_datalen(struct pstore_zone *zone)
  150. {
  151. return atomic_read(&zone->buffer->datalen);
  152. }
  153. static inline int buffer_start(struct pstore_zone *zone)
  154. {
  155. return atomic_read(&zone->buffer->start);
  156. }
  157. static inline bool is_on_panic(void)
  158. {
  159. return atomic_read(&pstore_zone_cxt.on_panic);
  160. }
  161. static ssize_t psz_zone_read_buffer(struct pstore_zone *zone, char *buf,
  162. size_t len, unsigned long off)
  163. {
  164. if (!buf || !zone || !zone->buffer)
  165. return -EINVAL;
  166. if (off > zone->buffer_size)
  167. return -EINVAL;
  168. len = min_t(size_t, len, zone->buffer_size - off);
  169. memcpy(buf, zone->buffer->data + off, len);
  170. return len;
  171. }
  172. static int psz_zone_read_oldbuf(struct pstore_zone *zone, char *buf,
  173. size_t len, unsigned long off)
  174. {
  175. if (!buf || !zone || !zone->oldbuf)
  176. return -EINVAL;
  177. if (off > zone->buffer_size)
  178. return -EINVAL;
  179. len = min_t(size_t, len, zone->buffer_size - off);
  180. memcpy(buf, zone->oldbuf->data + off, len);
  181. return 0;
  182. }
  183. static int psz_zone_write(struct pstore_zone *zone,
  184. enum psz_flush_mode flush_mode, const char *buf,
  185. size_t len, unsigned long off)
  186. {
  187. struct pstore_zone_info *info = pstore_zone_cxt.pstore_zone_info;
  188. ssize_t wcnt = 0;
  189. ssize_t (*writeop)(const char *buf, size_t bytes, loff_t pos);
  190. size_t wlen;
  191. if (off > zone->buffer_size)
  192. return -EINVAL;
  193. wlen = min_t(size_t, len, zone->buffer_size - off);
  194. if (buf && wlen) {
  195. memcpy(zone->buffer->data + off, buf, wlen);
  196. atomic_set(&zone->buffer->datalen, wlen + off);
  197. }
  198. /* avoid damaging old records */
  199. if (!is_on_panic() && !atomic_read(&pstore_zone_cxt.recovered))
  200. goto dirty;
  201. writeop = is_on_panic() ? info->panic_write : info->write;
  202. if (!writeop)
  203. goto dirty;
  204. switch (flush_mode) {
  205. case FLUSH_NONE:
  206. if (unlikely(buf && wlen))
  207. goto dirty;
  208. return 0;
  209. case FLUSH_PART:
  210. wcnt = writeop((const char *)zone->buffer->data + off, wlen,
  211. zone->off + sizeof(*zone->buffer) + off);
  212. if (wcnt != wlen)
  213. goto dirty;
  214. fallthrough;
  215. case FLUSH_META:
  216. wlen = sizeof(struct psz_buffer);
  217. wcnt = writeop((const char *)zone->buffer, wlen, zone->off);
  218. if (wcnt != wlen)
  219. goto dirty;
  220. break;
  221. case FLUSH_ALL:
  222. wlen = zone->buffer_size + sizeof(*zone->buffer);
  223. wcnt = writeop((const char *)zone->buffer, wlen, zone->off);
  224. if (wcnt != wlen)
  225. goto dirty;
  226. break;
  227. }
  228. return 0;
  229. dirty:
  230. /* no need to mark it dirty if going to try next zone */
  231. if (wcnt == -ENOMSG)
  232. return -ENOMSG;
  233. atomic_set(&zone->dirty, true);
  234. /* flush dirty zones nicely */
  235. if (wcnt == -EBUSY && !is_on_panic())
  236. schedule_delayed_work(&psz_cleaner, msecs_to_jiffies(500));
  237. return -EBUSY;
  238. }
  239. static int psz_flush_dirty_zone(struct pstore_zone *zone)
  240. {
  241. int ret;
  242. if (unlikely(!zone))
  243. return -EINVAL;
  244. if (unlikely(!atomic_read(&pstore_zone_cxt.recovered)))
  245. return -EBUSY;
  246. if (!atomic_xchg(&zone->dirty, false))
  247. return 0;
  248. ret = psz_zone_write(zone, FLUSH_ALL, NULL, 0, 0);
  249. if (ret)
  250. atomic_set(&zone->dirty, true);
  251. return ret;
  252. }
  253. static int psz_flush_dirty_zones(struct pstore_zone **zones, unsigned int cnt)
  254. {
  255. int i, ret;
  256. struct pstore_zone *zone;
  257. if (!zones)
  258. return -EINVAL;
  259. for (i = 0; i < cnt; i++) {
  260. zone = zones[i];
  261. if (!zone)
  262. return -EINVAL;
  263. ret = psz_flush_dirty_zone(zone);
  264. if (ret)
  265. return ret;
  266. }
  267. return 0;
  268. }
  269. static int psz_move_zone(struct pstore_zone *old, struct pstore_zone *new)
  270. {
  271. const char *data = (const char *)old->buffer->data;
  272. int ret;
  273. ret = psz_zone_write(new, FLUSH_ALL, data, buffer_datalen(old), 0);
  274. if (ret) {
  275. atomic_set(&new->buffer->datalen, 0);
  276. atomic_set(&new->dirty, false);
  277. return ret;
  278. }
  279. atomic_set(&old->buffer->datalen, 0);
  280. return 0;
  281. }
  282. static void psz_flush_all_dirty_zones(struct work_struct *work)
  283. {
  284. struct psz_context *cxt = &pstore_zone_cxt;
  285. int ret = 0;
  286. if (cxt->ppsz)
  287. ret |= psz_flush_dirty_zone(cxt->ppsz);
  288. if (cxt->cpsz)
  289. ret |= psz_flush_dirty_zone(cxt->cpsz);
  290. if (cxt->kpszs)
  291. ret |= psz_flush_dirty_zones(cxt->kpszs, cxt->kmsg_max_cnt);
  292. if (cxt->fpszs)
  293. ret |= psz_flush_dirty_zones(cxt->fpszs, cxt->ftrace_max_cnt);
  294. if (ret && cxt->pstore_zone_info)
  295. schedule_delayed_work(&psz_cleaner, msecs_to_jiffies(1000));
  296. }
  297. static int psz_kmsg_recover_data(struct psz_context *cxt)
  298. {
  299. struct pstore_zone_info *info = cxt->pstore_zone_info;
  300. struct pstore_zone *zone = NULL;
  301. struct psz_buffer *buf;
  302. unsigned long i;
  303. ssize_t rcnt;
  304. if (!info->read)
  305. return -EINVAL;
  306. for (i = 0; i < cxt->kmsg_max_cnt; i++) {
  307. zone = cxt->kpszs[i];
  308. if (unlikely(!zone))
  309. return -EINVAL;
  310. if (atomic_read(&zone->dirty)) {
  311. unsigned int wcnt = cxt->kmsg_write_cnt;
  312. struct pstore_zone *new = cxt->kpszs[wcnt];
  313. int ret;
  314. ret = psz_move_zone(zone, new);
  315. if (ret) {
  316. pr_err("move zone from %lu to %d failed\n",
  317. i, wcnt);
  318. return ret;
  319. }
  320. cxt->kmsg_write_cnt = (wcnt + 1) % cxt->kmsg_max_cnt;
  321. }
  322. if (!zone->should_recover)
  323. continue;
  324. buf = zone->buffer;
  325. rcnt = info->read((char *)buf, zone->buffer_size + sizeof(*buf),
  326. zone->off);
  327. if (rcnt != zone->buffer_size + sizeof(*buf))
  328. return rcnt < 0 ? rcnt : -EIO;
  329. }
  330. return 0;
  331. }
  332. static int psz_kmsg_recover_meta(struct psz_context *cxt)
  333. {
  334. struct pstore_zone_info *info = cxt->pstore_zone_info;
  335. struct pstore_zone *zone;
  336. ssize_t rcnt, len;
  337. struct psz_buffer *buf;
  338. struct psz_kmsg_header *hdr;
  339. struct timespec64 time = { };
  340. unsigned long i;
  341. /*
  342. * Recover may happen on panic, we can't allocate any memory by kmalloc.
  343. * So, we use local array instead.
  344. */
  345. char buffer_header[sizeof(*buf) + sizeof(*hdr)] = {0};
  346. if (!info->read)
  347. return -EINVAL;
  348. len = sizeof(*buf) + sizeof(*hdr);
  349. buf = (struct psz_buffer *)buffer_header;
  350. for (i = 0; i < cxt->kmsg_max_cnt; i++) {
  351. zone = cxt->kpszs[i];
  352. if (unlikely(!zone))
  353. return -EINVAL;
  354. rcnt = info->read((char *)buf, len, zone->off);
  355. if (rcnt == -ENOMSG) {
  356. pr_debug("%s with id %lu may be broken, skip\n",
  357. zone->name, i);
  358. continue;
  359. } else if (rcnt != len) {
  360. pr_err("read %s with id %lu failed\n", zone->name, i);
  361. return rcnt < 0 ? rcnt : -EIO;
  362. }
  363. if (buf->sig != zone->buffer->sig) {
  364. pr_debug("no valid data in kmsg dump zone %lu\n", i);
  365. continue;
  366. }
  367. if (zone->buffer_size < atomic_read(&buf->datalen)) {
  368. pr_info("found overtop zone: %s: id %lu, off %lld, size %zu\n",
  369. zone->name, i, zone->off,
  370. zone->buffer_size);
  371. continue;
  372. }
  373. hdr = (struct psz_kmsg_header *)buf->data;
  374. if (hdr->magic != PSTORE_KMSG_HEADER_MAGIC) {
  375. pr_info("found invalid zone: %s: id %lu, off %lld, size %zu\n",
  376. zone->name, i, zone->off,
  377. zone->buffer_size);
  378. continue;
  379. }
  380. /*
  381. * we get the newest zone, and the next one must be the oldest
  382. * or unused zone, because we do write one by one like a circle.
  383. */
  384. if (hdr->time.tv_sec >= time.tv_sec) {
  385. time.tv_sec = hdr->time.tv_sec;
  386. cxt->kmsg_write_cnt = (i + 1) % cxt->kmsg_max_cnt;
  387. }
  388. if (hdr->reason == KMSG_DUMP_OOPS)
  389. cxt->oops_counter =
  390. max(cxt->oops_counter, hdr->counter);
  391. else if (hdr->reason == KMSG_DUMP_PANIC)
  392. cxt->panic_counter =
  393. max(cxt->panic_counter, hdr->counter);
  394. if (!atomic_read(&buf->datalen)) {
  395. pr_debug("found erased zone: %s: id %lu, off %lld, size %zu, datalen %d\n",
  396. zone->name, i, zone->off,
  397. zone->buffer_size,
  398. atomic_read(&buf->datalen));
  399. continue;
  400. }
  401. if (!is_on_panic())
  402. zone->should_recover = true;
  403. pr_debug("found nice zone: %s: id %lu, off %lld, size %zu, datalen %d\n",
  404. zone->name, i, zone->off,
  405. zone->buffer_size, atomic_read(&buf->datalen));
  406. }
  407. return 0;
  408. }
  409. static int psz_kmsg_recover(struct psz_context *cxt)
  410. {
  411. int ret;
  412. if (!cxt->kpszs)
  413. return 0;
  414. ret = psz_kmsg_recover_meta(cxt);
  415. if (ret)
  416. goto recover_fail;
  417. ret = psz_kmsg_recover_data(cxt);
  418. if (ret)
  419. goto recover_fail;
  420. return 0;
  421. recover_fail:
  422. pr_debug("psz_recover_kmsg failed\n");
  423. return ret;
  424. }
  425. static int psz_recover_zone(struct psz_context *cxt, struct pstore_zone *zone)
  426. {
  427. struct pstore_zone_info *info = cxt->pstore_zone_info;
  428. struct psz_buffer *oldbuf, tmpbuf;
  429. int ret = 0;
  430. char *buf;
  431. ssize_t rcnt, len, start, off;
  432. if (!zone || zone->oldbuf)
  433. return 0;
  434. if (is_on_panic()) {
  435. /* save data as much as possible */
  436. psz_flush_dirty_zone(zone);
  437. return 0;
  438. }
  439. if (unlikely(!info->read))
  440. return -EINVAL;
  441. len = sizeof(struct psz_buffer);
  442. rcnt = info->read((char *)&tmpbuf, len, zone->off);
  443. if (rcnt != len) {
  444. pr_debug("read zone %s failed\n", zone->name);
  445. return rcnt < 0 ? rcnt : -EIO;
  446. }
  447. if (tmpbuf.sig != zone->buffer->sig) {
  448. pr_debug("no valid data in zone %s\n", zone->name);
  449. return 0;
  450. }
  451. if (zone->buffer_size < atomic_read(&tmpbuf.datalen) ||
  452. zone->buffer_size < atomic_read(&tmpbuf.start)) {
  453. pr_info("found overtop zone: %s: off %lld, size %zu\n",
  454. zone->name, zone->off, zone->buffer_size);
  455. /* just keep going */
  456. return 0;
  457. }
  458. if (!atomic_read(&tmpbuf.datalen)) {
  459. pr_debug("found erased zone: %s: off %lld, size %zu, datalen %d\n",
  460. zone->name, zone->off, zone->buffer_size,
  461. atomic_read(&tmpbuf.datalen));
  462. return 0;
  463. }
  464. pr_debug("found nice zone: %s: off %lld, size %zu, datalen %d\n",
  465. zone->name, zone->off, zone->buffer_size,
  466. atomic_read(&tmpbuf.datalen));
  467. len = atomic_read(&tmpbuf.datalen) + sizeof(*oldbuf);
  468. oldbuf = kzalloc(len, GFP_KERNEL);
  469. if (!oldbuf)
  470. return -ENOMEM;
  471. memcpy(oldbuf, &tmpbuf, sizeof(*oldbuf));
  472. buf = (char *)oldbuf + sizeof(*oldbuf);
  473. len = atomic_read(&oldbuf->datalen);
  474. start = atomic_read(&oldbuf->start);
  475. off = zone->off + sizeof(*oldbuf);
  476. /* get part of data */
  477. rcnt = info->read(buf, len - start, off + start);
  478. if (rcnt != len - start) {
  479. pr_err("read zone %s failed\n", zone->name);
  480. ret = rcnt < 0 ? rcnt : -EIO;
  481. goto free_oldbuf;
  482. }
  483. /* get the rest of data */
  484. rcnt = info->read(buf + len - start, start, off);
  485. if (rcnt != start) {
  486. pr_err("read zone %s failed\n", zone->name);
  487. ret = rcnt < 0 ? rcnt : -EIO;
  488. goto free_oldbuf;
  489. }
  490. zone->oldbuf = oldbuf;
  491. psz_flush_dirty_zone(zone);
  492. return 0;
  493. free_oldbuf:
  494. kfree(oldbuf);
  495. return ret;
  496. }
  497. static int psz_recover_zones(struct psz_context *cxt,
  498. struct pstore_zone **zones, unsigned int cnt)
  499. {
  500. int ret;
  501. unsigned int i;
  502. struct pstore_zone *zone;
  503. if (!zones)
  504. return 0;
  505. for (i = 0; i < cnt; i++) {
  506. zone = zones[i];
  507. if (unlikely(!zone))
  508. continue;
  509. ret = psz_recover_zone(cxt, zone);
  510. if (ret)
  511. goto recover_fail;
  512. }
  513. return 0;
  514. recover_fail:
  515. pr_debug("recover %s[%u] failed\n", zone->name, i);
  516. return ret;
  517. }
  518. /**
  519. * psz_recovery() - recover data from storage
  520. * @cxt: the context of pstore/zone
  521. *
  522. * recovery means reading data back from storage after rebooting
  523. *
  524. * Return: 0 on success, others on failure.
  525. */
  526. static inline int psz_recovery(struct psz_context *cxt)
  527. {
  528. int ret;
  529. if (atomic_read(&cxt->recovered))
  530. return 0;
  531. ret = psz_kmsg_recover(cxt);
  532. if (ret)
  533. goto out;
  534. ret = psz_recover_zone(cxt, cxt->ppsz);
  535. if (ret)
  536. goto out;
  537. ret = psz_recover_zone(cxt, cxt->cpsz);
  538. if (ret)
  539. goto out;
  540. ret = psz_recover_zones(cxt, cxt->fpszs, cxt->ftrace_max_cnt);
  541. out:
  542. if (unlikely(ret))
  543. pr_err("recover failed\n");
  544. else {
  545. pr_debug("recover end!\n");
  546. atomic_set(&cxt->recovered, 1);
  547. }
  548. return ret;
  549. }
  550. static int psz_pstore_open(struct pstore_info *psi)
  551. {
  552. struct psz_context *cxt = psi->data;
  553. cxt->kmsg_read_cnt = 0;
  554. cxt->pmsg_read_cnt = 0;
  555. cxt->console_read_cnt = 0;
  556. cxt->ftrace_read_cnt = 0;
  557. return 0;
  558. }
  559. static inline bool psz_old_ok(struct pstore_zone *zone)
  560. {
  561. if (zone && zone->oldbuf && atomic_read(&zone->oldbuf->datalen))
  562. return true;
  563. return false;
  564. }
  565. static inline bool psz_ok(struct pstore_zone *zone)
  566. {
  567. if (zone && zone->buffer && buffer_datalen(zone))
  568. return true;
  569. return false;
  570. }
  571. static inline int psz_kmsg_erase(struct psz_context *cxt,
  572. struct pstore_zone *zone, struct pstore_record *record)
  573. {
  574. struct psz_buffer *buffer = zone->buffer;
  575. struct psz_kmsg_header *hdr =
  576. (struct psz_kmsg_header *)buffer->data;
  577. size_t size;
  578. if (unlikely(!psz_ok(zone)))
  579. return 0;
  580. /* this zone is already updated, no need to erase */
  581. if (record->count != hdr->counter)
  582. return 0;
  583. size = buffer_datalen(zone) + sizeof(*zone->buffer);
  584. atomic_set(&zone->buffer->datalen, 0);
  585. if (cxt->pstore_zone_info->erase)
  586. return cxt->pstore_zone_info->erase(size, zone->off);
  587. else
  588. return psz_zone_write(zone, FLUSH_META, NULL, 0, 0);
  589. }
  590. static inline int psz_record_erase(struct psz_context *cxt,
  591. struct pstore_zone *zone)
  592. {
  593. if (unlikely(!psz_old_ok(zone)))
  594. return 0;
  595. kfree(zone->oldbuf);
  596. zone->oldbuf = NULL;
  597. /*
  598. * if there are new data in zone buffer, that means the old data
  599. * are already invalid. It is no need to flush 0 (erase) to
  600. * block device.
  601. */
  602. if (!buffer_datalen(zone))
  603. return psz_zone_write(zone, FLUSH_META, NULL, 0, 0);
  604. psz_flush_dirty_zone(zone);
  605. return 0;
  606. }
  607. static int psz_pstore_erase(struct pstore_record *record)
  608. {
  609. struct psz_context *cxt = record->psi->data;
  610. switch (record->type) {
  611. case PSTORE_TYPE_DMESG:
  612. if (record->id >= cxt->kmsg_max_cnt)
  613. return -EINVAL;
  614. return psz_kmsg_erase(cxt, cxt->kpszs[record->id], record);
  615. case PSTORE_TYPE_PMSG:
  616. return psz_record_erase(cxt, cxt->ppsz);
  617. case PSTORE_TYPE_CONSOLE:
  618. return psz_record_erase(cxt, cxt->cpsz);
  619. case PSTORE_TYPE_FTRACE:
  620. if (record->id >= cxt->ftrace_max_cnt)
  621. return -EINVAL;
  622. return psz_record_erase(cxt, cxt->fpszs[record->id]);
  623. default: return -EINVAL;
  624. }
  625. }
  626. static void psz_write_kmsg_hdr(struct pstore_zone *zone,
  627. struct pstore_record *record)
  628. {
  629. struct psz_context *cxt = record->psi->data;
  630. struct psz_buffer *buffer = zone->buffer;
  631. struct psz_kmsg_header *hdr =
  632. (struct psz_kmsg_header *)buffer->data;
  633. hdr->magic = PSTORE_KMSG_HEADER_MAGIC;
  634. hdr->compressed = record->compressed;
  635. hdr->time.tv_sec = record->time.tv_sec;
  636. hdr->time.tv_nsec = record->time.tv_nsec;
  637. hdr->reason = record->reason;
  638. if (hdr->reason == KMSG_DUMP_OOPS)
  639. hdr->counter = ++cxt->oops_counter;
  640. else if (hdr->reason == KMSG_DUMP_PANIC)
  641. hdr->counter = ++cxt->panic_counter;
  642. else
  643. hdr->counter = 0;
  644. }
  645. /*
  646. * In case zone is broken, which may occur to MTD device, we try each zones,
  647. * start at cxt->kmsg_write_cnt.
  648. */
  649. static inline int notrace psz_kmsg_write_record(struct psz_context *cxt,
  650. struct pstore_record *record)
  651. {
  652. size_t size, hlen;
  653. struct pstore_zone *zone;
  654. unsigned int i;
  655. for (i = 0; i < cxt->kmsg_max_cnt; i++) {
  656. unsigned int zonenum, len;
  657. int ret;
  658. zonenum = (cxt->kmsg_write_cnt + i) % cxt->kmsg_max_cnt;
  659. zone = cxt->kpszs[zonenum];
  660. if (unlikely(!zone))
  661. return -ENOSPC;
  662. /* avoid destroying old data, allocate a new one */
  663. len = zone->buffer_size + sizeof(*zone->buffer);
  664. zone->oldbuf = zone->buffer;
  665. zone->buffer = kzalloc(len, GFP_ATOMIC);
  666. if (!zone->buffer) {
  667. zone->buffer = zone->oldbuf;
  668. return -ENOMEM;
  669. }
  670. zone->buffer->sig = zone->oldbuf->sig;
  671. pr_debug("write %s to zone id %d\n", zone->name, zonenum);
  672. psz_write_kmsg_hdr(zone, record);
  673. hlen = sizeof(struct psz_kmsg_header);
  674. size = min_t(size_t, record->size, zone->buffer_size - hlen);
  675. ret = psz_zone_write(zone, FLUSH_ALL, record->buf, size, hlen);
  676. if (likely(!ret || ret != -ENOMSG)) {
  677. cxt->kmsg_write_cnt = zonenum + 1;
  678. cxt->kmsg_write_cnt %= cxt->kmsg_max_cnt;
  679. /* no need to try next zone, free last zone buffer */
  680. kfree(zone->oldbuf);
  681. zone->oldbuf = NULL;
  682. return ret;
  683. }
  684. pr_debug("zone %u may be broken, try next dmesg zone\n",
  685. zonenum);
  686. kfree(zone->buffer);
  687. zone->buffer = zone->oldbuf;
  688. zone->oldbuf = NULL;
  689. }
  690. return -EBUSY;
  691. }
  692. static int notrace psz_kmsg_write(struct psz_context *cxt,
  693. struct pstore_record *record)
  694. {
  695. int ret;
  696. /*
  697. * Explicitly only take the first part of any new crash.
  698. * If our buffer is larger than kmsg_bytes, this can never happen,
  699. * and if our buffer is smaller than kmsg_bytes, we don't want the
  700. * report split across multiple records.
  701. */
  702. if (record->part != 1)
  703. return -ENOSPC;
  704. if (!cxt->kpszs)
  705. return -ENOSPC;
  706. ret = psz_kmsg_write_record(cxt, record);
  707. if (!ret && is_on_panic()) {
  708. /* ensure all data are flushed to storage when panic */
  709. pr_debug("try to flush other dirty zones\n");
  710. psz_flush_all_dirty_zones(NULL);
  711. }
  712. /* always return 0 as we had handled it on buffer */
  713. return 0;
  714. }
  715. static int notrace psz_record_write(struct pstore_zone *zone,
  716. struct pstore_record *record)
  717. {
  718. size_t start, rem;
  719. bool is_full_data = false;
  720. char *buf;
  721. int cnt;
  722. if (!zone || !record)
  723. return -ENOSPC;
  724. if (atomic_read(&zone->buffer->datalen) >= zone->buffer_size)
  725. is_full_data = true;
  726. cnt = record->size;
  727. buf = record->buf;
  728. if (unlikely(cnt > zone->buffer_size)) {
  729. buf += cnt - zone->buffer_size;
  730. cnt = zone->buffer_size;
  731. }
  732. start = buffer_start(zone);
  733. rem = zone->buffer_size - start;
  734. if (unlikely(rem < cnt)) {
  735. psz_zone_write(zone, FLUSH_PART, buf, rem, start);
  736. buf += rem;
  737. cnt -= rem;
  738. start = 0;
  739. is_full_data = true;
  740. }
  741. atomic_set(&zone->buffer->start, cnt + start);
  742. psz_zone_write(zone, FLUSH_PART, buf, cnt, start);
  743. /**
  744. * psz_zone_write will set datalen as start + cnt.
  745. * It works if actual data length is lesser than buffer size.
  746. * If data length is greater than buffer size, pmsg will rewrite to
  747. * the beginning of the zone, which makes buffer->datalen wrong.
  748. * So we should reset datalen as buffer size once actual data length
  749. * is greater than buffer size.
  750. */
  751. if (is_full_data) {
  752. atomic_set(&zone->buffer->datalen, zone->buffer_size);
  753. psz_zone_write(zone, FLUSH_META, NULL, 0, 0);
  754. }
  755. return 0;
  756. }
  757. static int notrace psz_pstore_write(struct pstore_record *record)
  758. {
  759. struct psz_context *cxt = record->psi->data;
  760. if (record->type == PSTORE_TYPE_DMESG &&
  761. record->reason == KMSG_DUMP_PANIC)
  762. atomic_set(&cxt->on_panic, 1);
  763. /*
  764. * If on panic, do not write anything except panic records.
  765. * Fix the case when panic_write prints log that wakes up
  766. * console backend.
  767. */
  768. if (is_on_panic() && record->type != PSTORE_TYPE_DMESG)
  769. return -EBUSY;
  770. switch (record->type) {
  771. case PSTORE_TYPE_DMESG:
  772. return psz_kmsg_write(cxt, record);
  773. case PSTORE_TYPE_CONSOLE:
  774. return psz_record_write(cxt->cpsz, record);
  775. case PSTORE_TYPE_PMSG:
  776. return psz_record_write(cxt->ppsz, record);
  777. case PSTORE_TYPE_FTRACE: {
  778. int zonenum = smp_processor_id();
  779. if (!cxt->fpszs)
  780. return -ENOSPC;
  781. return psz_record_write(cxt->fpszs[zonenum], record);
  782. }
  783. default:
  784. return -EINVAL;
  785. }
  786. }
  787. static struct pstore_zone *psz_read_next_zone(struct psz_context *cxt)
  788. {
  789. struct pstore_zone *zone = NULL;
  790. while (cxt->kmsg_read_cnt < cxt->kmsg_max_cnt) {
  791. zone = cxt->kpszs[cxt->kmsg_read_cnt++];
  792. if (psz_ok(zone))
  793. return zone;
  794. }
  795. if (cxt->ftrace_read_cnt < cxt->ftrace_max_cnt)
  796. /*
  797. * No need psz_old_ok(). Let psz_ftrace_read() do so for
  798. * combination. psz_ftrace_read() should traverse over
  799. * all zones in case of some zone without data.
  800. */
  801. return cxt->fpszs[cxt->ftrace_read_cnt++];
  802. if (cxt->pmsg_read_cnt == 0) {
  803. cxt->pmsg_read_cnt++;
  804. zone = cxt->ppsz;
  805. if (psz_old_ok(zone))
  806. return zone;
  807. }
  808. if (cxt->console_read_cnt == 0) {
  809. cxt->console_read_cnt++;
  810. zone = cxt->cpsz;
  811. if (psz_old_ok(zone))
  812. return zone;
  813. }
  814. return NULL;
  815. }
  816. static int psz_kmsg_read_hdr(struct pstore_zone *zone,
  817. struct pstore_record *record)
  818. {
  819. struct psz_buffer *buffer = zone->buffer;
  820. struct psz_kmsg_header *hdr =
  821. (struct psz_kmsg_header *)buffer->data;
  822. if (hdr->magic != PSTORE_KMSG_HEADER_MAGIC)
  823. return -EINVAL;
  824. record->compressed = hdr->compressed;
  825. record->time.tv_sec = hdr->time.tv_sec;
  826. record->time.tv_nsec = hdr->time.tv_nsec;
  827. record->reason = hdr->reason;
  828. record->count = hdr->counter;
  829. return 0;
  830. }
  831. static ssize_t psz_kmsg_read(struct pstore_zone *zone,
  832. struct pstore_record *record)
  833. {
  834. ssize_t size, hlen = 0;
  835. size = buffer_datalen(zone);
  836. /* Clear and skip this kmsg dump record if it has no valid header */
  837. if (psz_kmsg_read_hdr(zone, record)) {
  838. atomic_set(&zone->buffer->datalen, 0);
  839. atomic_set(&zone->dirty, 0);
  840. return -ENOMSG;
  841. }
  842. size -= sizeof(struct psz_kmsg_header);
  843. if (!record->compressed) {
  844. char *buf = kasprintf(GFP_KERNEL, "%s: Total %d times\n",
  845. kmsg_dump_reason_str(record->reason),
  846. record->count);
  847. if (!buf)
  848. return -ENOMEM;
  849. hlen = strlen(buf);
  850. record->buf = krealloc(buf, hlen + size, GFP_KERNEL);
  851. if (!record->buf) {
  852. kfree(buf);
  853. return -ENOMEM;
  854. }
  855. } else {
  856. record->buf = kmalloc(size, GFP_KERNEL);
  857. if (!record->buf)
  858. return -ENOMEM;
  859. }
  860. size = psz_zone_read_buffer(zone, record->buf + hlen, size,
  861. sizeof(struct psz_kmsg_header));
  862. if (unlikely(size < 0)) {
  863. kfree(record->buf);
  864. return -ENOMSG;
  865. }
  866. return size + hlen;
  867. }
  868. /* try to combine all ftrace zones */
  869. static ssize_t psz_ftrace_read(struct pstore_zone *zone,
  870. struct pstore_record *record)
  871. {
  872. struct psz_context *cxt;
  873. struct psz_buffer *buf;
  874. int ret;
  875. if (!zone || !record)
  876. return -ENOSPC;
  877. if (!psz_old_ok(zone))
  878. goto out;
  879. buf = (struct psz_buffer *)zone->oldbuf;
  880. if (!buf)
  881. return -ENOMSG;
  882. ret = pstore_ftrace_combine_log(&record->buf, &record->size,
  883. (char *)buf->data, atomic_read(&buf->datalen));
  884. if (unlikely(ret))
  885. return ret;
  886. out:
  887. cxt = record->psi->data;
  888. if (cxt->ftrace_read_cnt < cxt->ftrace_max_cnt)
  889. /* then, read next ftrace zone */
  890. return -ENOMSG;
  891. record->id = 0;
  892. return record->size ? record->size : -ENOMSG;
  893. }
  894. static ssize_t psz_record_read(struct pstore_zone *zone,
  895. struct pstore_record *record)
  896. {
  897. size_t len;
  898. struct psz_buffer *buf;
  899. if (!zone || !record)
  900. return -ENOSPC;
  901. buf = (struct psz_buffer *)zone->oldbuf;
  902. if (!buf)
  903. return -ENOMSG;
  904. len = atomic_read(&buf->datalen);
  905. record->buf = kmalloc(len, GFP_KERNEL);
  906. if (!record->buf)
  907. return -ENOMEM;
  908. if (unlikely(psz_zone_read_oldbuf(zone, record->buf, len, 0))) {
  909. kfree(record->buf);
  910. return -ENOMSG;
  911. }
  912. return len;
  913. }
  914. static ssize_t psz_pstore_read(struct pstore_record *record)
  915. {
  916. struct psz_context *cxt = record->psi->data;
  917. ssize_t (*readop)(struct pstore_zone *zone,
  918. struct pstore_record *record);
  919. struct pstore_zone *zone;
  920. ssize_t ret;
  921. /* before read, we must recover from storage */
  922. ret = psz_recovery(cxt);
  923. if (ret)
  924. return ret;
  925. next_zone:
  926. zone = psz_read_next_zone(cxt);
  927. if (!zone)
  928. return 0;
  929. record->type = zone->type;
  930. switch (record->type) {
  931. case PSTORE_TYPE_DMESG:
  932. readop = psz_kmsg_read;
  933. record->id = cxt->kmsg_read_cnt - 1;
  934. break;
  935. case PSTORE_TYPE_FTRACE:
  936. readop = psz_ftrace_read;
  937. break;
  938. case PSTORE_TYPE_CONSOLE:
  939. case PSTORE_TYPE_PMSG:
  940. readop = psz_record_read;
  941. break;
  942. default:
  943. goto next_zone;
  944. }
  945. ret = readop(zone, record);
  946. if (ret == -ENOMSG)
  947. goto next_zone;
  948. return ret;
  949. }
  950. static struct psz_context pstore_zone_cxt = {
  951. .pstore_zone_info_lock =
  952. __MUTEX_INITIALIZER(pstore_zone_cxt.pstore_zone_info_lock),
  953. .recovered = ATOMIC_INIT(0),
  954. .on_panic = ATOMIC_INIT(0),
  955. .pstore = {
  956. .owner = THIS_MODULE,
  957. .open = psz_pstore_open,
  958. .read = psz_pstore_read,
  959. .write = psz_pstore_write,
  960. .erase = psz_pstore_erase,
  961. },
  962. };
  963. static void psz_free_zone(struct pstore_zone **pszone)
  964. {
  965. struct pstore_zone *zone = *pszone;
  966. if (!zone)
  967. return;
  968. kfree(zone->buffer);
  969. kfree(zone);
  970. *pszone = NULL;
  971. }
  972. static void psz_free_zones(struct pstore_zone ***pszones, unsigned int *cnt)
  973. {
  974. struct pstore_zone **zones = *pszones;
  975. if (!zones)
  976. return;
  977. while (*cnt > 0) {
  978. (*cnt)--;
  979. psz_free_zone(&(zones[*cnt]));
  980. }
  981. kfree(zones);
  982. *pszones = NULL;
  983. }
  984. static void psz_free_all_zones(struct psz_context *cxt)
  985. {
  986. if (cxt->kpszs)
  987. psz_free_zones(&cxt->kpszs, &cxt->kmsg_max_cnt);
  988. if (cxt->ppsz)
  989. psz_free_zone(&cxt->ppsz);
  990. if (cxt->cpsz)
  991. psz_free_zone(&cxt->cpsz);
  992. if (cxt->fpszs)
  993. psz_free_zones(&cxt->fpszs, &cxt->ftrace_max_cnt);
  994. }
  995. static struct pstore_zone *psz_init_zone(enum pstore_type_id type,
  996. loff_t *off, size_t size)
  997. {
  998. struct pstore_zone_info *info = pstore_zone_cxt.pstore_zone_info;
  999. struct pstore_zone *zone;
  1000. const char *name = pstore_type_to_name(type);
  1001. if (!size)
  1002. return NULL;
  1003. if (*off + size > info->total_size) {
  1004. pr_err("no room for %s (0x%zx@0x%llx over 0x%lx)\n",
  1005. name, size, *off, info->total_size);
  1006. return ERR_PTR(-ENOMEM);
  1007. }
  1008. zone = kzalloc_obj(struct pstore_zone);
  1009. if (!zone)
  1010. return ERR_PTR(-ENOMEM);
  1011. zone->buffer = kmalloc(size, GFP_KERNEL);
  1012. if (!zone->buffer) {
  1013. kfree(zone);
  1014. return ERR_PTR(-ENOMEM);
  1015. }
  1016. memset(zone->buffer, 0xFF, size);
  1017. zone->off = *off;
  1018. zone->name = name;
  1019. zone->type = type;
  1020. zone->buffer_size = size - sizeof(struct psz_buffer);
  1021. zone->buffer->sig = type ^ PSZ_SIG;
  1022. zone->oldbuf = NULL;
  1023. atomic_set(&zone->dirty, 0);
  1024. atomic_set(&zone->buffer->datalen, 0);
  1025. atomic_set(&zone->buffer->start, 0);
  1026. *off += size;
  1027. pr_debug("pszone %s: off 0x%llx, %zu header, %zu data\n", zone->name,
  1028. zone->off, sizeof(*zone->buffer), zone->buffer_size);
  1029. return zone;
  1030. }
  1031. static struct pstore_zone **psz_init_zones(enum pstore_type_id type,
  1032. loff_t *off, size_t total_size, ssize_t record_size,
  1033. unsigned int *cnt)
  1034. {
  1035. struct pstore_zone_info *info = pstore_zone_cxt.pstore_zone_info;
  1036. struct pstore_zone **zones, *zone;
  1037. const char *name = pstore_type_to_name(type);
  1038. int c, i;
  1039. *cnt = 0;
  1040. if (!total_size || !record_size)
  1041. return NULL;
  1042. if (*off + total_size > info->total_size) {
  1043. pr_err("no room for zones %s (0x%zx@0x%llx over 0x%lx)\n",
  1044. name, total_size, *off, info->total_size);
  1045. return ERR_PTR(-ENOMEM);
  1046. }
  1047. c = total_size / record_size;
  1048. if (unlikely(!c)) {
  1049. pr_err("zone %s total_size too small\n", name);
  1050. return ERR_PTR(-EINVAL);
  1051. }
  1052. zones = kzalloc_objs(*zones, c);
  1053. if (!zones) {
  1054. pr_err("allocate for zones %s failed\n", name);
  1055. return ERR_PTR(-ENOMEM);
  1056. }
  1057. for (i = 0; i < c; i++) {
  1058. zone = psz_init_zone(type, off, record_size);
  1059. if (!zone || IS_ERR(zone)) {
  1060. pr_err("initialize zones %s failed\n", name);
  1061. psz_free_zones(&zones, &i);
  1062. return (void *)zone;
  1063. }
  1064. zones[i] = zone;
  1065. }
  1066. *cnt = c;
  1067. return zones;
  1068. }
  1069. static int psz_alloc_zones(struct psz_context *cxt)
  1070. {
  1071. struct pstore_zone_info *info = cxt->pstore_zone_info;
  1072. loff_t off = 0;
  1073. int err;
  1074. size_t off_size = 0;
  1075. off_size += info->pmsg_size;
  1076. cxt->ppsz = psz_init_zone(PSTORE_TYPE_PMSG, &off, info->pmsg_size);
  1077. if (IS_ERR(cxt->ppsz)) {
  1078. err = PTR_ERR(cxt->ppsz);
  1079. cxt->ppsz = NULL;
  1080. goto free_out;
  1081. }
  1082. off_size += info->console_size;
  1083. cxt->cpsz = psz_init_zone(PSTORE_TYPE_CONSOLE, &off,
  1084. info->console_size);
  1085. if (IS_ERR(cxt->cpsz)) {
  1086. err = PTR_ERR(cxt->cpsz);
  1087. cxt->cpsz = NULL;
  1088. goto free_out;
  1089. }
  1090. off_size += info->ftrace_size;
  1091. cxt->fpszs = psz_init_zones(PSTORE_TYPE_FTRACE, &off,
  1092. info->ftrace_size,
  1093. info->ftrace_size / nr_cpu_ids,
  1094. &cxt->ftrace_max_cnt);
  1095. if (IS_ERR(cxt->fpszs)) {
  1096. err = PTR_ERR(cxt->fpszs);
  1097. cxt->fpszs = NULL;
  1098. goto free_out;
  1099. }
  1100. cxt->kpszs = psz_init_zones(PSTORE_TYPE_DMESG, &off,
  1101. info->total_size - off_size,
  1102. info->kmsg_size, &cxt->kmsg_max_cnt);
  1103. if (IS_ERR(cxt->kpszs)) {
  1104. err = PTR_ERR(cxt->kpszs);
  1105. cxt->kpszs = NULL;
  1106. goto free_out;
  1107. }
  1108. return 0;
  1109. free_out:
  1110. psz_free_all_zones(cxt);
  1111. return err;
  1112. }
  1113. /**
  1114. * register_pstore_zone() - register to pstore/zone
  1115. *
  1116. * @info: back-end driver information. See &struct pstore_zone_info.
  1117. *
  1118. * Only one back-end at one time.
  1119. *
  1120. * Return: 0 on success, others on failure.
  1121. */
  1122. int register_pstore_zone(struct pstore_zone_info *info)
  1123. {
  1124. int err = -EINVAL;
  1125. struct psz_context *cxt = &pstore_zone_cxt;
  1126. if (info->total_size < 4096) {
  1127. pr_warn("total_size must be >= 4096\n");
  1128. return -EINVAL;
  1129. }
  1130. if (info->total_size > SZ_128M) {
  1131. pr_warn("capping size to 128MiB\n");
  1132. info->total_size = SZ_128M;
  1133. }
  1134. if (!info->kmsg_size && !info->pmsg_size && !info->console_size &&
  1135. !info->ftrace_size) {
  1136. pr_warn("at least one record size must be non-zero\n");
  1137. return -EINVAL;
  1138. }
  1139. if (!info->name || !info->name[0])
  1140. return -EINVAL;
  1141. #define check_size(name, size) { \
  1142. if (info->name > 0 && info->name < (size)) { \
  1143. pr_err(#name " must be over %d\n", (size)); \
  1144. return -EINVAL; \
  1145. } \
  1146. if (info->name & (size - 1)) { \
  1147. pr_err(#name " must be a multiple of %d\n", \
  1148. (size)); \
  1149. return -EINVAL; \
  1150. } \
  1151. }
  1152. check_size(total_size, 4096);
  1153. check_size(kmsg_size, SECTOR_SIZE);
  1154. check_size(pmsg_size, SECTOR_SIZE);
  1155. check_size(console_size, SECTOR_SIZE);
  1156. check_size(ftrace_size, SECTOR_SIZE);
  1157. #undef check_size
  1158. /*
  1159. * the @read and @write must be applied.
  1160. * if no @read, pstore may mount failed.
  1161. * if no @write, pstore do not support to remove record file.
  1162. */
  1163. if (!info->read || !info->write) {
  1164. pr_err("no valid general read/write interface\n");
  1165. return -EINVAL;
  1166. }
  1167. mutex_lock(&cxt->pstore_zone_info_lock);
  1168. if (cxt->pstore_zone_info) {
  1169. pr_warn("'%s' already loaded: ignoring '%s'\n",
  1170. cxt->pstore_zone_info->name, info->name);
  1171. mutex_unlock(&cxt->pstore_zone_info_lock);
  1172. return -EBUSY;
  1173. }
  1174. cxt->pstore_zone_info = info;
  1175. pr_debug("register %s with properties:\n", info->name);
  1176. pr_debug("\ttotal size : %ld Bytes\n", info->total_size);
  1177. pr_debug("\tkmsg size : %ld Bytes\n", info->kmsg_size);
  1178. pr_debug("\tpmsg size : %ld Bytes\n", info->pmsg_size);
  1179. pr_debug("\tconsole size : %ld Bytes\n", info->console_size);
  1180. pr_debug("\tftrace size : %ld Bytes\n", info->ftrace_size);
  1181. err = psz_alloc_zones(cxt);
  1182. if (err) {
  1183. pr_err("alloc zones failed\n");
  1184. goto fail_out;
  1185. }
  1186. if (info->kmsg_size) {
  1187. cxt->pstore.bufsize = cxt->kpszs[0]->buffer_size -
  1188. sizeof(struct psz_kmsg_header);
  1189. cxt->pstore.buf = kzalloc(cxt->pstore.bufsize, GFP_KERNEL);
  1190. if (!cxt->pstore.buf) {
  1191. err = -ENOMEM;
  1192. goto fail_free;
  1193. }
  1194. }
  1195. cxt->pstore.data = cxt;
  1196. pr_info("registered %s as backend for", info->name);
  1197. cxt->pstore.max_reason = info->max_reason;
  1198. cxt->pstore.name = info->name;
  1199. if (info->kmsg_size) {
  1200. cxt->pstore.flags |= PSTORE_FLAGS_DMESG;
  1201. pr_cont(" kmsg(%s",
  1202. kmsg_dump_reason_str(cxt->pstore.max_reason));
  1203. if (cxt->pstore_zone_info->panic_write)
  1204. pr_cont(",panic_write");
  1205. pr_cont(")");
  1206. }
  1207. if (info->pmsg_size) {
  1208. cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
  1209. pr_cont(" pmsg");
  1210. }
  1211. if (info->console_size) {
  1212. cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
  1213. pr_cont(" console");
  1214. }
  1215. if (info->ftrace_size) {
  1216. cxt->pstore.flags |= PSTORE_FLAGS_FTRACE;
  1217. pr_cont(" ftrace");
  1218. }
  1219. pr_cont("\n");
  1220. err = pstore_register(&cxt->pstore);
  1221. if (err) {
  1222. pr_err("registering with pstore failed\n");
  1223. goto fail_free;
  1224. }
  1225. mutex_unlock(&pstore_zone_cxt.pstore_zone_info_lock);
  1226. return 0;
  1227. fail_free:
  1228. kfree(cxt->pstore.buf);
  1229. cxt->pstore.buf = NULL;
  1230. cxt->pstore.bufsize = 0;
  1231. psz_free_all_zones(cxt);
  1232. fail_out:
  1233. pstore_zone_cxt.pstore_zone_info = NULL;
  1234. mutex_unlock(&pstore_zone_cxt.pstore_zone_info_lock);
  1235. return err;
  1236. }
  1237. EXPORT_SYMBOL_GPL(register_pstore_zone);
  1238. /**
  1239. * unregister_pstore_zone() - unregister to pstore/zone
  1240. *
  1241. * @info: back-end driver information. See struct pstore_zone_info.
  1242. */
  1243. void unregister_pstore_zone(struct pstore_zone_info *info)
  1244. {
  1245. struct psz_context *cxt = &pstore_zone_cxt;
  1246. mutex_lock(&cxt->pstore_zone_info_lock);
  1247. if (!cxt->pstore_zone_info) {
  1248. mutex_unlock(&cxt->pstore_zone_info_lock);
  1249. return;
  1250. }
  1251. /* Stop incoming writes from pstore. */
  1252. pstore_unregister(&cxt->pstore);
  1253. /* Flush any pending writes. */
  1254. psz_flush_all_dirty_zones(NULL);
  1255. flush_delayed_work(&psz_cleaner);
  1256. /* Clean up allocations. */
  1257. kfree(cxt->pstore.buf);
  1258. cxt->pstore.buf = NULL;
  1259. cxt->pstore.bufsize = 0;
  1260. cxt->pstore_zone_info = NULL;
  1261. psz_free_all_zones(cxt);
  1262. /* Clear counters and zone state. */
  1263. cxt->oops_counter = 0;
  1264. cxt->panic_counter = 0;
  1265. atomic_set(&cxt->recovered, 0);
  1266. atomic_set(&cxt->on_panic, 0);
  1267. mutex_unlock(&cxt->pstore_zone_info_lock);
  1268. }
  1269. EXPORT_SYMBOL_GPL(unregister_pstore_zone);
  1270. MODULE_LICENSE("GPL");
  1271. MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
  1272. MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
  1273. MODULE_DESCRIPTION("Storage Manager for pstore/blk");