zone.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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 conpressed
  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 to damage 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 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 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 work if actual data length lesser than buffer size.
  746. * If data length greater than buffer size, pmsg will rewrite to
  747. * beginning of zone, which make buffer->datalen wrongly.
  748. * So we should reset datalen as buffer size once actual data length
  749. * 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 except panic records
  765. * Fix case that panic_write prints log which wakes up console backend.
  766. */
  767. if (is_on_panic() && record->type != PSTORE_TYPE_DMESG)
  768. return -EBUSY;
  769. switch (record->type) {
  770. case PSTORE_TYPE_DMESG:
  771. return psz_kmsg_write(cxt, record);
  772. case PSTORE_TYPE_CONSOLE:
  773. return psz_record_write(cxt->cpsz, record);
  774. case PSTORE_TYPE_PMSG:
  775. return psz_record_write(cxt->ppsz, record);
  776. case PSTORE_TYPE_FTRACE: {
  777. int zonenum = smp_processor_id();
  778. if (!cxt->fpszs)
  779. return -ENOSPC;
  780. return psz_record_write(cxt->fpszs[zonenum], record);
  781. }
  782. default:
  783. return -EINVAL;
  784. }
  785. }
  786. static struct pstore_zone *psz_read_next_zone(struct psz_context *cxt)
  787. {
  788. struct pstore_zone *zone = NULL;
  789. while (cxt->kmsg_read_cnt < cxt->kmsg_max_cnt) {
  790. zone = cxt->kpszs[cxt->kmsg_read_cnt++];
  791. if (psz_ok(zone))
  792. return zone;
  793. }
  794. if (cxt->ftrace_read_cnt < cxt->ftrace_max_cnt)
  795. /*
  796. * No need psz_old_ok(). Let psz_ftrace_read() do so for
  797. * combination. psz_ftrace_read() should traverse over
  798. * all zones in case of some zone without data.
  799. */
  800. return cxt->fpszs[cxt->ftrace_read_cnt++];
  801. if (cxt->pmsg_read_cnt == 0) {
  802. cxt->pmsg_read_cnt++;
  803. zone = cxt->ppsz;
  804. if (psz_old_ok(zone))
  805. return zone;
  806. }
  807. if (cxt->console_read_cnt == 0) {
  808. cxt->console_read_cnt++;
  809. zone = cxt->cpsz;
  810. if (psz_old_ok(zone))
  811. return zone;
  812. }
  813. return NULL;
  814. }
  815. static int psz_kmsg_read_hdr(struct pstore_zone *zone,
  816. struct pstore_record *record)
  817. {
  818. struct psz_buffer *buffer = zone->buffer;
  819. struct psz_kmsg_header *hdr =
  820. (struct psz_kmsg_header *)buffer->data;
  821. if (hdr->magic != PSTORE_KMSG_HEADER_MAGIC)
  822. return -EINVAL;
  823. record->compressed = hdr->compressed;
  824. record->time.tv_sec = hdr->time.tv_sec;
  825. record->time.tv_nsec = hdr->time.tv_nsec;
  826. record->reason = hdr->reason;
  827. record->count = hdr->counter;
  828. return 0;
  829. }
  830. static ssize_t psz_kmsg_read(struct pstore_zone *zone,
  831. struct pstore_record *record)
  832. {
  833. ssize_t size, hlen = 0;
  834. size = buffer_datalen(zone);
  835. /* Clear and skip this kmsg dump record if it has no valid header */
  836. if (psz_kmsg_read_hdr(zone, record)) {
  837. atomic_set(&zone->buffer->datalen, 0);
  838. atomic_set(&zone->dirty, 0);
  839. return -ENOMSG;
  840. }
  841. size -= sizeof(struct psz_kmsg_header);
  842. if (!record->compressed) {
  843. char *buf = kasprintf(GFP_KERNEL, "%s: Total %d times\n",
  844. kmsg_dump_reason_str(record->reason),
  845. record->count);
  846. if (!buf)
  847. return -ENOMEM;
  848. hlen = strlen(buf);
  849. record->buf = krealloc(buf, hlen + size, GFP_KERNEL);
  850. if (!record->buf) {
  851. kfree(buf);
  852. return -ENOMEM;
  853. }
  854. } else {
  855. record->buf = kmalloc(size, GFP_KERNEL);
  856. if (!record->buf)
  857. return -ENOMEM;
  858. }
  859. size = psz_zone_read_buffer(zone, record->buf + hlen, size,
  860. sizeof(struct psz_kmsg_header));
  861. if (unlikely(size < 0)) {
  862. kfree(record->buf);
  863. return -ENOMSG;
  864. }
  865. return size + hlen;
  866. }
  867. /* try to combine all ftrace zones */
  868. static ssize_t psz_ftrace_read(struct pstore_zone *zone,
  869. struct pstore_record *record)
  870. {
  871. struct psz_context *cxt;
  872. struct psz_buffer *buf;
  873. int ret;
  874. if (!zone || !record)
  875. return -ENOSPC;
  876. if (!psz_old_ok(zone))
  877. goto out;
  878. buf = (struct psz_buffer *)zone->oldbuf;
  879. if (!buf)
  880. return -ENOMSG;
  881. ret = pstore_ftrace_combine_log(&record->buf, &record->size,
  882. (char *)buf->data, atomic_read(&buf->datalen));
  883. if (unlikely(ret))
  884. return ret;
  885. out:
  886. cxt = record->psi->data;
  887. if (cxt->ftrace_read_cnt < cxt->ftrace_max_cnt)
  888. /* then, read next ftrace zone */
  889. return -ENOMSG;
  890. record->id = 0;
  891. return record->size ? record->size : -ENOMSG;
  892. }
  893. static ssize_t psz_record_read(struct pstore_zone *zone,
  894. struct pstore_record *record)
  895. {
  896. size_t len;
  897. struct psz_buffer *buf;
  898. if (!zone || !record)
  899. return -ENOSPC;
  900. buf = (struct psz_buffer *)zone->oldbuf;
  901. if (!buf)
  902. return -ENOMSG;
  903. len = atomic_read(&buf->datalen);
  904. record->buf = kmalloc(len, GFP_KERNEL);
  905. if (!record->buf)
  906. return -ENOMEM;
  907. if (unlikely(psz_zone_read_oldbuf(zone, record->buf, len, 0))) {
  908. kfree(record->buf);
  909. return -ENOMSG;
  910. }
  911. return len;
  912. }
  913. static ssize_t psz_pstore_read(struct pstore_record *record)
  914. {
  915. struct psz_context *cxt = record->psi->data;
  916. ssize_t (*readop)(struct pstore_zone *zone,
  917. struct pstore_record *record);
  918. struct pstore_zone *zone;
  919. ssize_t ret;
  920. /* before read, we must recover from storage */
  921. ret = psz_recovery(cxt);
  922. if (ret)
  923. return ret;
  924. next_zone:
  925. zone = psz_read_next_zone(cxt);
  926. if (!zone)
  927. return 0;
  928. record->type = zone->type;
  929. switch (record->type) {
  930. case PSTORE_TYPE_DMESG:
  931. readop = psz_kmsg_read;
  932. record->id = cxt->kmsg_read_cnt - 1;
  933. break;
  934. case PSTORE_TYPE_FTRACE:
  935. readop = psz_ftrace_read;
  936. break;
  937. case PSTORE_TYPE_CONSOLE:
  938. case PSTORE_TYPE_PMSG:
  939. readop = psz_record_read;
  940. break;
  941. default:
  942. goto next_zone;
  943. }
  944. ret = readop(zone, record);
  945. if (ret == -ENOMSG)
  946. goto next_zone;
  947. return ret;
  948. }
  949. static struct psz_context pstore_zone_cxt = {
  950. .pstore_zone_info_lock =
  951. __MUTEX_INITIALIZER(pstore_zone_cxt.pstore_zone_info_lock),
  952. .recovered = ATOMIC_INIT(0),
  953. .on_panic = ATOMIC_INIT(0),
  954. .pstore = {
  955. .owner = THIS_MODULE,
  956. .open = psz_pstore_open,
  957. .read = psz_pstore_read,
  958. .write = psz_pstore_write,
  959. .erase = psz_pstore_erase,
  960. },
  961. };
  962. static void psz_free_zone(struct pstore_zone **pszone)
  963. {
  964. struct pstore_zone *zone = *pszone;
  965. if (!zone)
  966. return;
  967. kfree(zone->buffer);
  968. kfree(zone);
  969. *pszone = NULL;
  970. }
  971. static void psz_free_zones(struct pstore_zone ***pszones, unsigned int *cnt)
  972. {
  973. struct pstore_zone **zones = *pszones;
  974. if (!zones)
  975. return;
  976. while (*cnt > 0) {
  977. (*cnt)--;
  978. psz_free_zone(&(zones[*cnt]));
  979. }
  980. kfree(zones);
  981. *pszones = NULL;
  982. }
  983. static void psz_free_all_zones(struct psz_context *cxt)
  984. {
  985. if (cxt->kpszs)
  986. psz_free_zones(&cxt->kpszs, &cxt->kmsg_max_cnt);
  987. if (cxt->ppsz)
  988. psz_free_zone(&cxt->ppsz);
  989. if (cxt->cpsz)
  990. psz_free_zone(&cxt->cpsz);
  991. if (cxt->fpszs)
  992. psz_free_zones(&cxt->fpszs, &cxt->ftrace_max_cnt);
  993. }
  994. static struct pstore_zone *psz_init_zone(enum pstore_type_id type,
  995. loff_t *off, size_t size)
  996. {
  997. struct pstore_zone_info *info = pstore_zone_cxt.pstore_zone_info;
  998. struct pstore_zone *zone;
  999. const char *name = pstore_type_to_name(type);
  1000. if (!size)
  1001. return NULL;
  1002. if (*off + size > info->total_size) {
  1003. pr_err("no room for %s (0x%zx@0x%llx over 0x%lx)\n",
  1004. name, size, *off, info->total_size);
  1005. return ERR_PTR(-ENOMEM);
  1006. }
  1007. zone = kzalloc(sizeof(struct pstore_zone), GFP_KERNEL);
  1008. if (!zone)
  1009. return ERR_PTR(-ENOMEM);
  1010. zone->buffer = kmalloc(size, GFP_KERNEL);
  1011. if (!zone->buffer) {
  1012. kfree(zone);
  1013. return ERR_PTR(-ENOMEM);
  1014. }
  1015. memset(zone->buffer, 0xFF, size);
  1016. zone->off = *off;
  1017. zone->name = name;
  1018. zone->type = type;
  1019. zone->buffer_size = size - sizeof(struct psz_buffer);
  1020. zone->buffer->sig = type ^ PSZ_SIG;
  1021. zone->oldbuf = NULL;
  1022. atomic_set(&zone->dirty, 0);
  1023. atomic_set(&zone->buffer->datalen, 0);
  1024. atomic_set(&zone->buffer->start, 0);
  1025. *off += size;
  1026. pr_debug("pszone %s: off 0x%llx, %zu header, %zu data\n", zone->name,
  1027. zone->off, sizeof(*zone->buffer), zone->buffer_size);
  1028. return zone;
  1029. }
  1030. static struct pstore_zone **psz_init_zones(enum pstore_type_id type,
  1031. loff_t *off, size_t total_size, ssize_t record_size,
  1032. unsigned int *cnt)
  1033. {
  1034. struct pstore_zone_info *info = pstore_zone_cxt.pstore_zone_info;
  1035. struct pstore_zone **zones, *zone;
  1036. const char *name = pstore_type_to_name(type);
  1037. int c, i;
  1038. *cnt = 0;
  1039. if (!total_size || !record_size)
  1040. return NULL;
  1041. if (*off + total_size > info->total_size) {
  1042. pr_err("no room for zones %s (0x%zx@0x%llx over 0x%lx)\n",
  1043. name, total_size, *off, info->total_size);
  1044. return ERR_PTR(-ENOMEM);
  1045. }
  1046. c = total_size / record_size;
  1047. zones = kcalloc(c, sizeof(*zones), GFP_KERNEL);
  1048. if (!zones) {
  1049. pr_err("allocate for zones %s failed\n", name);
  1050. return ERR_PTR(-ENOMEM);
  1051. }
  1052. for (i = 0; i < c; i++) {
  1053. zone = psz_init_zone(type, off, record_size);
  1054. if (!zone || IS_ERR(zone)) {
  1055. pr_err("initialize zones %s failed\n", name);
  1056. psz_free_zones(&zones, &i);
  1057. return (void *)zone;
  1058. }
  1059. zones[i] = zone;
  1060. }
  1061. *cnt = c;
  1062. return zones;
  1063. }
  1064. static int psz_alloc_zones(struct psz_context *cxt)
  1065. {
  1066. struct pstore_zone_info *info = cxt->pstore_zone_info;
  1067. loff_t off = 0;
  1068. int err;
  1069. size_t off_size = 0;
  1070. off_size += info->pmsg_size;
  1071. cxt->ppsz = psz_init_zone(PSTORE_TYPE_PMSG, &off, info->pmsg_size);
  1072. if (IS_ERR(cxt->ppsz)) {
  1073. err = PTR_ERR(cxt->ppsz);
  1074. cxt->ppsz = NULL;
  1075. goto free_out;
  1076. }
  1077. off_size += info->console_size;
  1078. cxt->cpsz = psz_init_zone(PSTORE_TYPE_CONSOLE, &off,
  1079. info->console_size);
  1080. if (IS_ERR(cxt->cpsz)) {
  1081. err = PTR_ERR(cxt->cpsz);
  1082. cxt->cpsz = NULL;
  1083. goto free_out;
  1084. }
  1085. off_size += info->ftrace_size;
  1086. cxt->fpszs = psz_init_zones(PSTORE_TYPE_FTRACE, &off,
  1087. info->ftrace_size,
  1088. info->ftrace_size / nr_cpu_ids,
  1089. &cxt->ftrace_max_cnt);
  1090. if (IS_ERR(cxt->fpszs)) {
  1091. err = PTR_ERR(cxt->fpszs);
  1092. cxt->fpszs = NULL;
  1093. goto free_out;
  1094. }
  1095. cxt->kpszs = psz_init_zones(PSTORE_TYPE_DMESG, &off,
  1096. info->total_size - off_size,
  1097. info->kmsg_size, &cxt->kmsg_max_cnt);
  1098. if (IS_ERR(cxt->kpszs)) {
  1099. err = PTR_ERR(cxt->kpszs);
  1100. cxt->kpszs = NULL;
  1101. goto free_out;
  1102. }
  1103. return 0;
  1104. free_out:
  1105. psz_free_all_zones(cxt);
  1106. return err;
  1107. }
  1108. /**
  1109. * register_pstore_zone() - register to pstore/zone
  1110. *
  1111. * @info: back-end driver information. See &struct pstore_zone_info.
  1112. *
  1113. * Only one back-end at one time.
  1114. *
  1115. * Return: 0 on success, others on failure.
  1116. */
  1117. int register_pstore_zone(struct pstore_zone_info *info)
  1118. {
  1119. int err = -EINVAL;
  1120. struct psz_context *cxt = &pstore_zone_cxt;
  1121. if (info->total_size < 4096) {
  1122. pr_warn("total_size must be >= 4096\n");
  1123. return -EINVAL;
  1124. }
  1125. if (info->total_size > SZ_128M) {
  1126. pr_warn("capping size to 128MiB\n");
  1127. info->total_size = SZ_128M;
  1128. }
  1129. if (!info->kmsg_size && !info->pmsg_size && !info->console_size &&
  1130. !info->ftrace_size) {
  1131. pr_warn("at least one record size must be non-zero\n");
  1132. return -EINVAL;
  1133. }
  1134. if (!info->name || !info->name[0])
  1135. return -EINVAL;
  1136. #define check_size(name, size) { \
  1137. if (info->name > 0 && info->name < (size)) { \
  1138. pr_err(#name " must be over %d\n", (size)); \
  1139. return -EINVAL; \
  1140. } \
  1141. if (info->name & (size - 1)) { \
  1142. pr_err(#name " must be a multiple of %d\n", \
  1143. (size)); \
  1144. return -EINVAL; \
  1145. } \
  1146. }
  1147. check_size(total_size, 4096);
  1148. check_size(kmsg_size, SECTOR_SIZE);
  1149. check_size(pmsg_size, SECTOR_SIZE);
  1150. check_size(console_size, SECTOR_SIZE);
  1151. check_size(ftrace_size, SECTOR_SIZE);
  1152. #undef check_size
  1153. /*
  1154. * the @read and @write must be applied.
  1155. * if no @read, pstore may mount failed.
  1156. * if no @write, pstore do not support to remove record file.
  1157. */
  1158. if (!info->read || !info->write) {
  1159. pr_err("no valid general read/write interface\n");
  1160. return -EINVAL;
  1161. }
  1162. mutex_lock(&cxt->pstore_zone_info_lock);
  1163. if (cxt->pstore_zone_info) {
  1164. pr_warn("'%s' already loaded: ignoring '%s'\n",
  1165. cxt->pstore_zone_info->name, info->name);
  1166. mutex_unlock(&cxt->pstore_zone_info_lock);
  1167. return -EBUSY;
  1168. }
  1169. cxt->pstore_zone_info = info;
  1170. pr_debug("register %s with properties:\n", info->name);
  1171. pr_debug("\ttotal size : %ld Bytes\n", info->total_size);
  1172. pr_debug("\tkmsg size : %ld Bytes\n", info->kmsg_size);
  1173. pr_debug("\tpmsg size : %ld Bytes\n", info->pmsg_size);
  1174. pr_debug("\tconsole size : %ld Bytes\n", info->console_size);
  1175. pr_debug("\tftrace size : %ld Bytes\n", info->ftrace_size);
  1176. err = psz_alloc_zones(cxt);
  1177. if (err) {
  1178. pr_err("alloc zones failed\n");
  1179. goto fail_out;
  1180. }
  1181. if (info->kmsg_size) {
  1182. cxt->pstore.bufsize = cxt->kpszs[0]->buffer_size -
  1183. sizeof(struct psz_kmsg_header);
  1184. cxt->pstore.buf = kzalloc(cxt->pstore.bufsize, GFP_KERNEL);
  1185. if (!cxt->pstore.buf) {
  1186. err = -ENOMEM;
  1187. goto fail_free;
  1188. }
  1189. }
  1190. cxt->pstore.data = cxt;
  1191. pr_info("registered %s as backend for", info->name);
  1192. cxt->pstore.max_reason = info->max_reason;
  1193. cxt->pstore.name = info->name;
  1194. if (info->kmsg_size) {
  1195. cxt->pstore.flags |= PSTORE_FLAGS_DMESG;
  1196. pr_cont(" kmsg(%s",
  1197. kmsg_dump_reason_str(cxt->pstore.max_reason));
  1198. if (cxt->pstore_zone_info->panic_write)
  1199. pr_cont(",panic_write");
  1200. pr_cont(")");
  1201. }
  1202. if (info->pmsg_size) {
  1203. cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
  1204. pr_cont(" pmsg");
  1205. }
  1206. if (info->console_size) {
  1207. cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
  1208. pr_cont(" console");
  1209. }
  1210. if (info->ftrace_size) {
  1211. cxt->pstore.flags |= PSTORE_FLAGS_FTRACE;
  1212. pr_cont(" ftrace");
  1213. }
  1214. pr_cont("\n");
  1215. err = pstore_register(&cxt->pstore);
  1216. if (err) {
  1217. pr_err("registering with pstore failed\n");
  1218. goto fail_free;
  1219. }
  1220. mutex_unlock(&pstore_zone_cxt.pstore_zone_info_lock);
  1221. return 0;
  1222. fail_free:
  1223. kfree(cxt->pstore.buf);
  1224. cxt->pstore.buf = NULL;
  1225. cxt->pstore.bufsize = 0;
  1226. psz_free_all_zones(cxt);
  1227. fail_out:
  1228. pstore_zone_cxt.pstore_zone_info = NULL;
  1229. mutex_unlock(&pstore_zone_cxt.pstore_zone_info_lock);
  1230. return err;
  1231. }
  1232. EXPORT_SYMBOL_GPL(register_pstore_zone);
  1233. /**
  1234. * unregister_pstore_zone() - unregister to pstore/zone
  1235. *
  1236. * @info: back-end driver information. See struct pstore_zone_info.
  1237. */
  1238. void unregister_pstore_zone(struct pstore_zone_info *info)
  1239. {
  1240. struct psz_context *cxt = &pstore_zone_cxt;
  1241. mutex_lock(&cxt->pstore_zone_info_lock);
  1242. if (!cxt->pstore_zone_info) {
  1243. mutex_unlock(&cxt->pstore_zone_info_lock);
  1244. return;
  1245. }
  1246. /* Stop incoming writes from pstore. */
  1247. pstore_unregister(&cxt->pstore);
  1248. /* Flush any pending writes. */
  1249. psz_flush_all_dirty_zones(NULL);
  1250. flush_delayed_work(&psz_cleaner);
  1251. /* Clean up allocations. */
  1252. kfree(cxt->pstore.buf);
  1253. cxt->pstore.buf = NULL;
  1254. cxt->pstore.bufsize = 0;
  1255. cxt->pstore_zone_info = NULL;
  1256. psz_free_all_zones(cxt);
  1257. /* Clear counters and zone state. */
  1258. cxt->oops_counter = 0;
  1259. cxt->panic_counter = 0;
  1260. atomic_set(&cxt->recovered, 0);
  1261. atomic_set(&cxt->on_panic, 0);
  1262. mutex_unlock(&cxt->pstore_zone_info_lock);
  1263. }
  1264. EXPORT_SYMBOL_GPL(unregister_pstore_zone);
  1265. MODULE_LICENSE("GPL");
  1266. MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
  1267. MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
  1268. MODULE_DESCRIPTION("Storage Manager for pstore/blk");