nbcon.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2022 Linutronix GmbH, John Ogness
  3. // Copyright (C) 2022 Intel, Thomas Gleixner
  4. #include <linux/atomic.h>
  5. #include <linux/bug.h>
  6. #include <linux/console.h>
  7. #include <linux/delay.h>
  8. #include <linux/errno.h>
  9. #include <linux/export.h>
  10. #include <linux/init.h>
  11. #include <linux/irqflags.h>
  12. #include <linux/kthread.h>
  13. #include <linux/minmax.h>
  14. #include <linux/percpu.h>
  15. #include <linux/preempt.h>
  16. #include <linux/slab.h>
  17. #include <linux/smp.h>
  18. #include <linux/stddef.h>
  19. #include <linux/string.h>
  20. #include <linux/types.h>
  21. #include "internal.h"
  22. #include "printk_ringbuffer.h"
  23. /*
  24. * Printk console printing implementation for consoles which does not depend
  25. * on the legacy style console_lock mechanism.
  26. *
  27. * The state of the console is maintained in the "nbcon_state" atomic
  28. * variable.
  29. *
  30. * The console is locked when:
  31. *
  32. * - The 'prio' field contains the priority of the context that owns the
  33. * console. Only higher priority contexts are allowed to take over the
  34. * lock. A value of 0 (NBCON_PRIO_NONE) means the console is not locked.
  35. *
  36. * - The 'cpu' field denotes on which CPU the console is locked. It is used
  37. * to prevent busy waiting on the same CPU. Also it informs the lock owner
  38. * that it has lost the lock in a more complex scenario when the lock was
  39. * taken over by a higher priority context, released, and taken on another
  40. * CPU with the same priority as the interrupted owner.
  41. *
  42. * The acquire mechanism uses a few more fields:
  43. *
  44. * - The 'req_prio' field is used by the handover approach to make the
  45. * current owner aware that there is a context with a higher priority
  46. * waiting for the friendly handover.
  47. *
  48. * - The 'unsafe' field allows to take over the console in a safe way in the
  49. * middle of emitting a message. The field is set only when accessing some
  50. * shared resources or when the console device is manipulated. It can be
  51. * cleared, for example, after emitting one character when the console
  52. * device is in a consistent state.
  53. *
  54. * - The 'unsafe_takeover' field is set when a hostile takeover took the
  55. * console in an unsafe state. The console will stay in the unsafe state
  56. * until re-initialized.
  57. *
  58. * The acquire mechanism uses three approaches:
  59. *
  60. * 1) Direct acquire when the console is not owned or is owned by a lower
  61. * priority context and is in a safe state.
  62. *
  63. * 2) Friendly handover mechanism uses a request/grant handshake. It is used
  64. * when the current owner has lower priority and the console is in an
  65. * unsafe state.
  66. *
  67. * The requesting context:
  68. *
  69. * a) Sets its priority into the 'req_prio' field.
  70. *
  71. * b) Waits (with a timeout) for the owning context to unlock the
  72. * console.
  73. *
  74. * c) Takes the lock and clears the 'req_prio' field.
  75. *
  76. * The owning context:
  77. *
  78. * a) Observes the 'req_prio' field set on exit from the unsafe
  79. * console state.
  80. *
  81. * b) Gives up console ownership by clearing the 'prio' field.
  82. *
  83. * 3) Unsafe hostile takeover allows to take over the lock even when the
  84. * console is an unsafe state. It is used only in panic() by the final
  85. * attempt to flush consoles in a try and hope mode.
  86. *
  87. * Note that separate record buffers are used in panic(). As a result,
  88. * the messages can be read and formatted without any risk even after
  89. * using the hostile takeover in unsafe state.
  90. *
  91. * The release function simply clears the 'prio' field.
  92. *
  93. * All operations on @console::nbcon_state are atomic cmpxchg based to
  94. * handle concurrency.
  95. *
  96. * The acquire/release functions implement only minimal policies:
  97. *
  98. * - Preference for higher priority contexts.
  99. * - Protection of the panic CPU.
  100. *
  101. * All other policy decisions must be made at the call sites:
  102. *
  103. * - What is marked as an unsafe section.
  104. * - Whether to spin-wait if there is already an owner and the console is
  105. * in an unsafe state.
  106. * - Whether to attempt an unsafe hostile takeover.
  107. *
  108. * The design allows to implement the well known:
  109. *
  110. * acquire()
  111. * output_one_printk_record()
  112. * release()
  113. *
  114. * The output of one printk record might be interrupted with a higher priority
  115. * context. The new owner is supposed to reprint the entire interrupted record
  116. * from scratch.
  117. */
  118. /**
  119. * nbcon_state_set - Helper function to set the console state
  120. * @con: Console to update
  121. * @new: The new state to write
  122. *
  123. * Only to be used when the console is not yet or no longer visible in the
  124. * system. Otherwise use nbcon_state_try_cmpxchg().
  125. */
  126. static inline void nbcon_state_set(struct console *con, struct nbcon_state *new)
  127. {
  128. atomic_set(&ACCESS_PRIVATE(con, nbcon_state), new->atom);
  129. }
  130. /**
  131. * nbcon_state_read - Helper function to read the console state
  132. * @con: Console to read
  133. * @state: The state to store the result
  134. */
  135. static inline void nbcon_state_read(struct console *con, struct nbcon_state *state)
  136. {
  137. state->atom = atomic_read(&ACCESS_PRIVATE(con, nbcon_state));
  138. }
  139. /**
  140. * nbcon_state_try_cmpxchg() - Helper function for atomic_try_cmpxchg() on console state
  141. * @con: Console to update
  142. * @cur: Old/expected state
  143. * @new: New state
  144. *
  145. * Return: True on success. False on fail and @cur is updated.
  146. */
  147. static inline bool nbcon_state_try_cmpxchg(struct console *con, struct nbcon_state *cur,
  148. struct nbcon_state *new)
  149. {
  150. return atomic_try_cmpxchg(&ACCESS_PRIVATE(con, nbcon_state), &cur->atom, new->atom);
  151. }
  152. /**
  153. * nbcon_seq_read - Read the current console sequence
  154. * @con: Console to read the sequence of
  155. *
  156. * Return: Sequence number of the next record to print on @con.
  157. */
  158. u64 nbcon_seq_read(struct console *con)
  159. {
  160. unsigned long nbcon_seq = atomic_long_read(&ACCESS_PRIVATE(con, nbcon_seq));
  161. return __ulseq_to_u64seq(prb, nbcon_seq);
  162. }
  163. /**
  164. * nbcon_seq_force - Force console sequence to a specific value
  165. * @con: Console to work on
  166. * @seq: Sequence number value to set
  167. *
  168. * Only to be used during init (before registration) or in extreme situations
  169. * (such as panic with CONSOLE_REPLAY_ALL).
  170. */
  171. void nbcon_seq_force(struct console *con, u64 seq)
  172. {
  173. /*
  174. * If the specified record no longer exists, the oldest available record
  175. * is chosen. This is especially important on 32bit systems because only
  176. * the lower 32 bits of the sequence number are stored. The upper 32 bits
  177. * are derived from the sequence numbers available in the ringbuffer.
  178. */
  179. u64 valid_seq = max_t(u64, seq, prb_first_valid_seq(prb));
  180. atomic_long_set(&ACCESS_PRIVATE(con, nbcon_seq), __u64seq_to_ulseq(valid_seq));
  181. }
  182. /**
  183. * nbcon_seq_try_update - Try to update the console sequence number
  184. * @ctxt: Pointer to an acquire context that contains
  185. * all information about the acquire mode
  186. * @new_seq: The new sequence number to set
  187. *
  188. * @ctxt->seq is updated to the new value of @con::nbcon_seq (expanded to
  189. * the 64bit value). This could be a different value than @new_seq if
  190. * nbcon_seq_force() was used or the current context no longer owns the
  191. * console. In the later case, it will stop printing anyway.
  192. */
  193. static void nbcon_seq_try_update(struct nbcon_context *ctxt, u64 new_seq)
  194. {
  195. unsigned long nbcon_seq = __u64seq_to_ulseq(ctxt->seq);
  196. struct console *con = ctxt->console;
  197. if (atomic_long_try_cmpxchg(&ACCESS_PRIVATE(con, nbcon_seq), &nbcon_seq,
  198. __u64seq_to_ulseq(new_seq))) {
  199. ctxt->seq = new_seq;
  200. } else {
  201. ctxt->seq = nbcon_seq_read(con);
  202. }
  203. }
  204. /**
  205. * nbcon_context_try_acquire_direct - Try to acquire directly
  206. * @ctxt: The context of the caller
  207. * @cur: The current console state
  208. * @is_reacquire: This acquire is a reacquire
  209. *
  210. * Acquire the console when it is released. Also acquire the console when
  211. * the current owner has a lower priority and the console is in a safe state.
  212. *
  213. * Return: 0 on success. Otherwise, an error code on failure. Also @cur
  214. * is updated to the latest state when failed to modify it.
  215. *
  216. * Errors:
  217. *
  218. * -EPERM: A panic is in progress and this is neither the panic
  219. * CPU nor is this a reacquire. Or the current owner or
  220. * waiter has the same or higher priority. No acquire
  221. * method can be successful in these cases.
  222. *
  223. * -EBUSY: The current owner has a lower priority but the console
  224. * in an unsafe state. The caller should try using
  225. * the handover acquire method.
  226. */
  227. static int nbcon_context_try_acquire_direct(struct nbcon_context *ctxt,
  228. struct nbcon_state *cur, bool is_reacquire)
  229. {
  230. unsigned int cpu = smp_processor_id();
  231. struct console *con = ctxt->console;
  232. struct nbcon_state new;
  233. do {
  234. /*
  235. * Panic does not imply that the console is owned. However,
  236. * since all non-panic CPUs are stopped during panic(), it
  237. * is safer to have them avoid gaining console ownership.
  238. *
  239. * If this acquire is a reacquire (and an unsafe takeover
  240. * has not previously occurred) then it is allowed to attempt
  241. * a direct acquire in panic. This gives console drivers an
  242. * opportunity to perform any necessary cleanup if they were
  243. * interrupted by the panic CPU while printing.
  244. */
  245. if (other_cpu_in_panic() &&
  246. (!is_reacquire || cur->unsafe_takeover)) {
  247. return -EPERM;
  248. }
  249. if (ctxt->prio <= cur->prio || ctxt->prio <= cur->req_prio)
  250. return -EPERM;
  251. if (cur->unsafe)
  252. return -EBUSY;
  253. /*
  254. * The console should never be safe for a direct acquire
  255. * if an unsafe hostile takeover has ever happened.
  256. */
  257. WARN_ON_ONCE(cur->unsafe_takeover);
  258. new.atom = cur->atom;
  259. new.prio = ctxt->prio;
  260. new.req_prio = NBCON_PRIO_NONE;
  261. new.unsafe = cur->unsafe_takeover;
  262. new.cpu = cpu;
  263. } while (!nbcon_state_try_cmpxchg(con, cur, &new));
  264. return 0;
  265. }
  266. static bool nbcon_waiter_matches(struct nbcon_state *cur, int expected_prio)
  267. {
  268. /*
  269. * The request context is well defined by the @req_prio because:
  270. *
  271. * - Only a context with a priority higher than the owner can become
  272. * a waiter.
  273. * - Only a context with a priority higher than the waiter can
  274. * directly take over the request.
  275. * - There are only three priorities.
  276. * - Only one CPU is allowed to request PANIC priority.
  277. * - Lower priorities are ignored during panic() until reboot.
  278. *
  279. * As a result, the following scenario is *not* possible:
  280. *
  281. * 1. This context is currently a waiter.
  282. * 2. Another context with a higher priority than this context
  283. * directly takes ownership.
  284. * 3. The higher priority context releases the ownership.
  285. * 4. Another lower priority context takes the ownership.
  286. * 5. Another context with the same priority as this context
  287. * creates a request and starts waiting.
  288. *
  289. * Event #1 implies this context is EMERGENCY.
  290. * Event #2 implies the new context is PANIC.
  291. * Event #3 occurs when panic() has flushed the console.
  292. * Event #4 occurs when a non-panic CPU reacquires.
  293. * Event #5 is not possible due to the other_cpu_in_panic() check
  294. * in nbcon_context_try_acquire_handover().
  295. */
  296. return (cur->req_prio == expected_prio);
  297. }
  298. /**
  299. * nbcon_context_try_acquire_requested - Try to acquire after having
  300. * requested a handover
  301. * @ctxt: The context of the caller
  302. * @cur: The current console state
  303. *
  304. * This is a helper function for nbcon_context_try_acquire_handover().
  305. * It is called when the console is in an unsafe state. The current
  306. * owner will release the console on exit from the unsafe region.
  307. *
  308. * Return: 0 on success and @cur is updated to the new console state.
  309. * Otherwise an error code on failure.
  310. *
  311. * Errors:
  312. *
  313. * -EPERM: A panic is in progress and this is not the panic CPU
  314. * or this context is no longer the waiter.
  315. *
  316. * -EBUSY: The console is still locked. The caller should
  317. * continue waiting.
  318. *
  319. * Note: The caller must still remove the request when an error has occurred
  320. * except when this context is no longer the waiter.
  321. */
  322. static int nbcon_context_try_acquire_requested(struct nbcon_context *ctxt,
  323. struct nbcon_state *cur)
  324. {
  325. unsigned int cpu = smp_processor_id();
  326. struct console *con = ctxt->console;
  327. struct nbcon_state new;
  328. /* Note that the caller must still remove the request! */
  329. if (other_cpu_in_panic())
  330. return -EPERM;
  331. /*
  332. * Note that the waiter will also change if there was an unsafe
  333. * hostile takeover.
  334. */
  335. if (!nbcon_waiter_matches(cur, ctxt->prio))
  336. return -EPERM;
  337. /* If still locked, caller should continue waiting. */
  338. if (cur->prio != NBCON_PRIO_NONE)
  339. return -EBUSY;
  340. /*
  341. * The previous owner should have never released ownership
  342. * in an unsafe region.
  343. */
  344. WARN_ON_ONCE(cur->unsafe);
  345. new.atom = cur->atom;
  346. new.prio = ctxt->prio;
  347. new.req_prio = NBCON_PRIO_NONE;
  348. new.unsafe = cur->unsafe_takeover;
  349. new.cpu = cpu;
  350. if (!nbcon_state_try_cmpxchg(con, cur, &new)) {
  351. /*
  352. * The acquire could fail only when it has been taken
  353. * over by a higher priority context.
  354. */
  355. WARN_ON_ONCE(nbcon_waiter_matches(cur, ctxt->prio));
  356. return -EPERM;
  357. }
  358. /* Handover success. This context now owns the console. */
  359. return 0;
  360. }
  361. /**
  362. * nbcon_context_try_acquire_handover - Try to acquire via handover
  363. * @ctxt: The context of the caller
  364. * @cur: The current console state
  365. *
  366. * The function must be called only when the context has higher priority
  367. * than the current owner and the console is in an unsafe state.
  368. * It is the case when nbcon_context_try_acquire_direct() returns -EBUSY.
  369. *
  370. * The function sets "req_prio" field to make the current owner aware of
  371. * the request. Then it waits until the current owner releases the console,
  372. * or an even higher context takes over the request, or timeout expires.
  373. *
  374. * The current owner checks the "req_prio" field on exit from the unsafe
  375. * region and releases the console. It does not touch the "req_prio" field
  376. * so that the console stays reserved for the waiter.
  377. *
  378. * Return: 0 on success. Otherwise, an error code on failure. Also @cur
  379. * is updated to the latest state when failed to modify it.
  380. *
  381. * Errors:
  382. *
  383. * -EPERM: A panic is in progress and this is not the panic CPU.
  384. * Or a higher priority context has taken over the
  385. * console or the handover request.
  386. *
  387. * -EBUSY: The current owner is on the same CPU so that the hand
  388. * shake could not work. Or the current owner is not
  389. * willing to wait (zero timeout). Or the console does
  390. * not enter the safe state before timeout passed. The
  391. * caller might still use the unsafe hostile takeover
  392. * when allowed.
  393. *
  394. * -EAGAIN: @cur has changed when creating the handover request.
  395. * The caller should retry with direct acquire.
  396. */
  397. static int nbcon_context_try_acquire_handover(struct nbcon_context *ctxt,
  398. struct nbcon_state *cur)
  399. {
  400. unsigned int cpu = smp_processor_id();
  401. struct console *con = ctxt->console;
  402. struct nbcon_state new;
  403. int timeout;
  404. int request_err = -EBUSY;
  405. /*
  406. * Check that the handover is called when the direct acquire failed
  407. * with -EBUSY.
  408. */
  409. WARN_ON_ONCE(ctxt->prio <= cur->prio || ctxt->prio <= cur->req_prio);
  410. WARN_ON_ONCE(!cur->unsafe);
  411. /*
  412. * Panic does not imply that the console is owned. However, it
  413. * is critical that non-panic CPUs during panic are unable to
  414. * wait for a handover in order to satisfy the assumptions of
  415. * nbcon_waiter_matches(). In particular, the assumption that
  416. * lower priorities are ignored during panic.
  417. */
  418. if (other_cpu_in_panic())
  419. return -EPERM;
  420. /* Handover is not possible on the same CPU. */
  421. if (cur->cpu == cpu)
  422. return -EBUSY;
  423. /*
  424. * Console stays unsafe after an unsafe takeover until re-initialized.
  425. * Waiting is not going to help in this case.
  426. */
  427. if (cur->unsafe_takeover)
  428. return -EBUSY;
  429. /* Is the caller willing to wait? */
  430. if (ctxt->spinwait_max_us == 0)
  431. return -EBUSY;
  432. /*
  433. * Setup a request for the handover. The caller should try to acquire
  434. * the console directly when the current state has been modified.
  435. */
  436. new.atom = cur->atom;
  437. new.req_prio = ctxt->prio;
  438. if (!nbcon_state_try_cmpxchg(con, cur, &new))
  439. return -EAGAIN;
  440. cur->atom = new.atom;
  441. /* Wait until there is no owner and then acquire the console. */
  442. for (timeout = ctxt->spinwait_max_us; timeout >= 0; timeout--) {
  443. /* On successful acquire, this request is cleared. */
  444. request_err = nbcon_context_try_acquire_requested(ctxt, cur);
  445. if (!request_err)
  446. return 0;
  447. /*
  448. * If the acquire should be aborted, it must be ensured
  449. * that the request is removed before returning to caller.
  450. */
  451. if (request_err == -EPERM)
  452. break;
  453. udelay(1);
  454. /* Re-read the state because some time has passed. */
  455. nbcon_state_read(con, cur);
  456. }
  457. /* Timed out or aborted. Carefully remove handover request. */
  458. do {
  459. /*
  460. * No need to remove request if there is a new waiter. This
  461. * can only happen if a higher priority context has taken over
  462. * the console or the handover request.
  463. */
  464. if (!nbcon_waiter_matches(cur, ctxt->prio))
  465. return -EPERM;
  466. /* Unset request for handover. */
  467. new.atom = cur->atom;
  468. new.req_prio = NBCON_PRIO_NONE;
  469. if (nbcon_state_try_cmpxchg(con, cur, &new)) {
  470. /*
  471. * Request successfully unset. Report failure of
  472. * acquiring via handover.
  473. */
  474. cur->atom = new.atom;
  475. return request_err;
  476. }
  477. /*
  478. * Unable to remove request. Try to acquire in case
  479. * the owner has released the lock.
  480. */
  481. } while (nbcon_context_try_acquire_requested(ctxt, cur));
  482. /* Lucky timing. The acquire succeeded while removing the request. */
  483. return 0;
  484. }
  485. /**
  486. * nbcon_context_try_acquire_hostile - Acquire via unsafe hostile takeover
  487. * @ctxt: The context of the caller
  488. * @cur: The current console state
  489. *
  490. * Acquire the console even in the unsafe state.
  491. *
  492. * It can be permitted by setting the 'allow_unsafe_takeover' field only
  493. * by the final attempt to flush messages in panic().
  494. *
  495. * Return: 0 on success. -EPERM when not allowed by the context.
  496. */
  497. static int nbcon_context_try_acquire_hostile(struct nbcon_context *ctxt,
  498. struct nbcon_state *cur)
  499. {
  500. unsigned int cpu = smp_processor_id();
  501. struct console *con = ctxt->console;
  502. struct nbcon_state new;
  503. if (!ctxt->allow_unsafe_takeover)
  504. return -EPERM;
  505. /* Ensure caller is allowed to perform unsafe hostile takeovers. */
  506. if (WARN_ON_ONCE(ctxt->prio != NBCON_PRIO_PANIC))
  507. return -EPERM;
  508. /*
  509. * Check that try_acquire_direct() and try_acquire_handover() returned
  510. * -EBUSY in the right situation.
  511. */
  512. WARN_ON_ONCE(ctxt->prio <= cur->prio || ctxt->prio <= cur->req_prio);
  513. WARN_ON_ONCE(cur->unsafe != true);
  514. do {
  515. new.atom = cur->atom;
  516. new.cpu = cpu;
  517. new.prio = ctxt->prio;
  518. new.unsafe |= cur->unsafe_takeover;
  519. new.unsafe_takeover |= cur->unsafe;
  520. } while (!nbcon_state_try_cmpxchg(con, cur, &new));
  521. return 0;
  522. }
  523. static struct printk_buffers panic_nbcon_pbufs;
  524. /**
  525. * nbcon_context_try_acquire - Try to acquire nbcon console
  526. * @ctxt: The context of the caller
  527. * @is_reacquire: This acquire is a reacquire
  528. *
  529. * Context: Under @ctxt->con->device_lock() or local_irq_save().
  530. * Return: True if the console was acquired. False otherwise.
  531. *
  532. * If the caller allowed an unsafe hostile takeover, on success the
  533. * caller should check the current console state to see if it is
  534. * in an unsafe state. Otherwise, on success the caller may assume
  535. * the console is not in an unsafe state.
  536. */
  537. static bool nbcon_context_try_acquire(struct nbcon_context *ctxt, bool is_reacquire)
  538. {
  539. unsigned int cpu = smp_processor_id();
  540. struct console *con = ctxt->console;
  541. struct nbcon_state cur;
  542. int err;
  543. nbcon_state_read(con, &cur);
  544. try_again:
  545. err = nbcon_context_try_acquire_direct(ctxt, &cur, is_reacquire);
  546. if (err != -EBUSY)
  547. goto out;
  548. err = nbcon_context_try_acquire_handover(ctxt, &cur);
  549. if (err == -EAGAIN)
  550. goto try_again;
  551. if (err != -EBUSY)
  552. goto out;
  553. err = nbcon_context_try_acquire_hostile(ctxt, &cur);
  554. out:
  555. if (err)
  556. return false;
  557. /* Acquire succeeded. */
  558. /* Assign the appropriate buffer for this context. */
  559. if (atomic_read(&panic_cpu) == cpu)
  560. ctxt->pbufs = &panic_nbcon_pbufs;
  561. else
  562. ctxt->pbufs = con->pbufs;
  563. /* Set the record sequence for this context to print. */
  564. ctxt->seq = nbcon_seq_read(ctxt->console);
  565. return true;
  566. }
  567. static bool nbcon_owner_matches(struct nbcon_state *cur, int expected_cpu,
  568. int expected_prio)
  569. {
  570. /*
  571. * A similar function, nbcon_waiter_matches(), only deals with
  572. * EMERGENCY and PANIC priorities. However, this function must also
  573. * deal with the NORMAL priority, which requires additional checks
  574. * and constraints.
  575. *
  576. * For the case where preemption and interrupts are disabled, it is
  577. * enough to also verify that the owning CPU has not changed.
  578. *
  579. * For the case where preemption or interrupts are enabled, an
  580. * external synchronization method *must* be used. In particular,
  581. * the driver-specific locking mechanism used in device_lock()
  582. * (including disabling migration) should be used. It prevents
  583. * scenarios such as:
  584. *
  585. * 1. [Task A] owns a context with NBCON_PRIO_NORMAL on [CPU X] and
  586. * is scheduled out.
  587. * 2. Another context takes over the lock with NBCON_PRIO_EMERGENCY
  588. * and releases it.
  589. * 3. [Task B] acquires a context with NBCON_PRIO_NORMAL on [CPU X]
  590. * and is scheduled out.
  591. * 4. [Task A] gets running on [CPU X] and sees that the console is
  592. * still owned by a task on [CPU X] with NBON_PRIO_NORMAL. Thus
  593. * [Task A] thinks it is the owner when it is not.
  594. */
  595. if (cur->prio != expected_prio)
  596. return false;
  597. if (cur->cpu != expected_cpu)
  598. return false;
  599. return true;
  600. }
  601. /**
  602. * nbcon_context_release - Release the console
  603. * @ctxt: The nbcon context from nbcon_context_try_acquire()
  604. */
  605. static void nbcon_context_release(struct nbcon_context *ctxt)
  606. {
  607. unsigned int cpu = smp_processor_id();
  608. struct console *con = ctxt->console;
  609. struct nbcon_state cur;
  610. struct nbcon_state new;
  611. nbcon_state_read(con, &cur);
  612. do {
  613. if (!nbcon_owner_matches(&cur, cpu, ctxt->prio))
  614. break;
  615. new.atom = cur.atom;
  616. new.prio = NBCON_PRIO_NONE;
  617. /*
  618. * If @unsafe_takeover is set, it is kept set so that
  619. * the state remains permanently unsafe.
  620. */
  621. new.unsafe |= cur.unsafe_takeover;
  622. } while (!nbcon_state_try_cmpxchg(con, &cur, &new));
  623. ctxt->pbufs = NULL;
  624. }
  625. /**
  626. * nbcon_context_can_proceed - Check whether ownership can proceed
  627. * @ctxt: The nbcon context from nbcon_context_try_acquire()
  628. * @cur: The current console state
  629. *
  630. * Return: True if this context still owns the console. False if
  631. * ownership was handed over or taken.
  632. *
  633. * Must be invoked when entering the unsafe state to make sure that it still
  634. * owns the lock. Also must be invoked when exiting the unsafe context
  635. * to eventually free the lock for a higher priority context which asked
  636. * for the friendly handover.
  637. *
  638. * It can be called inside an unsafe section when the console is just
  639. * temporary in safe state instead of exiting and entering the unsafe
  640. * state.
  641. *
  642. * Also it can be called in the safe context before doing an expensive
  643. * safe operation. It does not make sense to do the operation when
  644. * a higher priority context took the lock.
  645. *
  646. * When this function returns false then the calling context no longer owns
  647. * the console and is no longer allowed to go forward. In this case it must
  648. * back out immediately and carefully. The buffer content is also no longer
  649. * trusted since it no longer belongs to the calling context.
  650. */
  651. static bool nbcon_context_can_proceed(struct nbcon_context *ctxt, struct nbcon_state *cur)
  652. {
  653. unsigned int cpu = smp_processor_id();
  654. /* Make sure this context still owns the console. */
  655. if (!nbcon_owner_matches(cur, cpu, ctxt->prio))
  656. return false;
  657. /* The console owner can proceed if there is no waiter. */
  658. if (cur->req_prio == NBCON_PRIO_NONE)
  659. return true;
  660. /*
  661. * A console owner within an unsafe region is always allowed to
  662. * proceed, even if there are waiters. It can perform a handover
  663. * when exiting the unsafe region. Otherwise the waiter will
  664. * need to perform an unsafe hostile takeover.
  665. */
  666. if (cur->unsafe)
  667. return true;
  668. /* Waiters always have higher priorities than owners. */
  669. WARN_ON_ONCE(cur->req_prio <= cur->prio);
  670. /*
  671. * Having a safe point for take over and eventually a few
  672. * duplicated characters or a full line is way better than a
  673. * hostile takeover. Post processing can take care of the garbage.
  674. * Release and hand over.
  675. */
  676. nbcon_context_release(ctxt);
  677. /*
  678. * It is not clear whether the waiter really took over ownership. The
  679. * outermost callsite must make the final decision whether console
  680. * ownership is needed for it to proceed. If yes, it must reacquire
  681. * ownership (possibly hostile) before carefully proceeding.
  682. *
  683. * The calling context no longer owns the console so go back all the
  684. * way instead of trying to implement reacquire heuristics in tons of
  685. * places.
  686. */
  687. return false;
  688. }
  689. /**
  690. * nbcon_can_proceed - Check whether ownership can proceed
  691. * @wctxt: The write context that was handed to the write function
  692. *
  693. * Return: True if this context still owns the console. False if
  694. * ownership was handed over or taken.
  695. *
  696. * It is used in nbcon_enter_unsafe() to make sure that it still owns the
  697. * lock. Also it is used in nbcon_exit_unsafe() to eventually free the lock
  698. * for a higher priority context which asked for the friendly handover.
  699. *
  700. * It can be called inside an unsafe section when the console is just
  701. * temporary in safe state instead of exiting and entering the unsafe state.
  702. *
  703. * Also it can be called in the safe context before doing an expensive safe
  704. * operation. It does not make sense to do the operation when a higher
  705. * priority context took the lock.
  706. *
  707. * When this function returns false then the calling context no longer owns
  708. * the console and is no longer allowed to go forward. In this case it must
  709. * back out immediately and carefully. The buffer content is also no longer
  710. * trusted since it no longer belongs to the calling context.
  711. */
  712. bool nbcon_can_proceed(struct nbcon_write_context *wctxt)
  713. {
  714. struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
  715. struct console *con = ctxt->console;
  716. struct nbcon_state cur;
  717. nbcon_state_read(con, &cur);
  718. return nbcon_context_can_proceed(ctxt, &cur);
  719. }
  720. EXPORT_SYMBOL_GPL(nbcon_can_proceed);
  721. #define nbcon_context_enter_unsafe(c) __nbcon_context_update_unsafe(c, true)
  722. #define nbcon_context_exit_unsafe(c) __nbcon_context_update_unsafe(c, false)
  723. /**
  724. * __nbcon_context_update_unsafe - Update the unsafe bit in @con->nbcon_state
  725. * @ctxt: The nbcon context from nbcon_context_try_acquire()
  726. * @unsafe: The new value for the unsafe bit
  727. *
  728. * Return: True if the unsafe state was updated and this context still
  729. * owns the console. Otherwise false if ownership was handed
  730. * over or taken.
  731. *
  732. * This function allows console owners to modify the unsafe status of the
  733. * console.
  734. *
  735. * When this function returns false then the calling context no longer owns
  736. * the console and is no longer allowed to go forward. In this case it must
  737. * back out immediately and carefully. The buffer content is also no longer
  738. * trusted since it no longer belongs to the calling context.
  739. *
  740. * Internal helper to avoid duplicated code.
  741. */
  742. static bool __nbcon_context_update_unsafe(struct nbcon_context *ctxt, bool unsafe)
  743. {
  744. struct console *con = ctxt->console;
  745. struct nbcon_state cur;
  746. struct nbcon_state new;
  747. nbcon_state_read(con, &cur);
  748. do {
  749. /*
  750. * The unsafe bit must not be cleared if an
  751. * unsafe hostile takeover has occurred.
  752. */
  753. if (!unsafe && cur.unsafe_takeover)
  754. goto out;
  755. if (!nbcon_context_can_proceed(ctxt, &cur))
  756. return false;
  757. new.atom = cur.atom;
  758. new.unsafe = unsafe;
  759. } while (!nbcon_state_try_cmpxchg(con, &cur, &new));
  760. cur.atom = new.atom;
  761. out:
  762. return nbcon_context_can_proceed(ctxt, &cur);
  763. }
  764. static void nbcon_write_context_set_buf(struct nbcon_write_context *wctxt,
  765. char *buf, unsigned int len)
  766. {
  767. struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
  768. struct console *con = ctxt->console;
  769. struct nbcon_state cur;
  770. wctxt->outbuf = buf;
  771. wctxt->len = len;
  772. nbcon_state_read(con, &cur);
  773. wctxt->unsafe_takeover = cur.unsafe_takeover;
  774. }
  775. /**
  776. * nbcon_enter_unsafe - Enter an unsafe region in the driver
  777. * @wctxt: The write context that was handed to the write function
  778. *
  779. * Return: True if this context still owns the console. False if
  780. * ownership was handed over or taken.
  781. *
  782. * When this function returns false then the calling context no longer owns
  783. * the console and is no longer allowed to go forward. In this case it must
  784. * back out immediately and carefully. The buffer content is also no longer
  785. * trusted since it no longer belongs to the calling context.
  786. */
  787. bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt)
  788. {
  789. struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
  790. bool is_owner;
  791. is_owner = nbcon_context_enter_unsafe(ctxt);
  792. if (!is_owner)
  793. nbcon_write_context_set_buf(wctxt, NULL, 0);
  794. return is_owner;
  795. }
  796. EXPORT_SYMBOL_GPL(nbcon_enter_unsafe);
  797. /**
  798. * nbcon_exit_unsafe - Exit an unsafe region in the driver
  799. * @wctxt: The write context that was handed to the write function
  800. *
  801. * Return: True if this context still owns the console. False if
  802. * ownership was handed over or taken.
  803. *
  804. * When this function returns false then the calling context no longer owns
  805. * the console and is no longer allowed to go forward. In this case it must
  806. * back out immediately and carefully. The buffer content is also no longer
  807. * trusted since it no longer belongs to the calling context.
  808. */
  809. bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt)
  810. {
  811. struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
  812. bool ret;
  813. ret = nbcon_context_exit_unsafe(ctxt);
  814. if (!ret)
  815. nbcon_write_context_set_buf(wctxt, NULL, 0);
  816. return ret;
  817. }
  818. EXPORT_SYMBOL_GPL(nbcon_exit_unsafe);
  819. /**
  820. * nbcon_reacquire_nobuf - Reacquire a console after losing ownership
  821. * while printing
  822. * @wctxt: The write context that was handed to the write callback
  823. *
  824. * Since ownership can be lost at any time due to handover or takeover, a
  825. * printing context _must_ be prepared to back out immediately and
  826. * carefully. However, there are scenarios where the printing context must
  827. * reacquire ownership in order to finalize or revert hardware changes.
  828. *
  829. * This function allows a printing context to reacquire ownership using the
  830. * same priority as its previous ownership.
  831. *
  832. * Note that after a successful reacquire the printing context will have no
  833. * output buffer because that has been lost. This function cannot be used to
  834. * resume printing.
  835. */
  836. void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt)
  837. {
  838. struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
  839. while (!nbcon_context_try_acquire(ctxt, true))
  840. cpu_relax();
  841. nbcon_write_context_set_buf(wctxt, NULL, 0);
  842. }
  843. EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf);
  844. /**
  845. * nbcon_emit_next_record - Emit a record in the acquired context
  846. * @wctxt: The write context that will be handed to the write function
  847. * @use_atomic: True if the write_atomic() callback is to be used
  848. *
  849. * Return: True if this context still owns the console. False if
  850. * ownership was handed over or taken.
  851. *
  852. * When this function returns false then the calling context no longer owns
  853. * the console and is no longer allowed to go forward. In this case it must
  854. * back out immediately and carefully. The buffer content is also no longer
  855. * trusted since it no longer belongs to the calling context. If the caller
  856. * wants to do more it must reacquire the console first.
  857. *
  858. * When true is returned, @wctxt->ctxt.backlog indicates whether there are
  859. * still records pending in the ringbuffer,
  860. */
  861. static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt, bool use_atomic)
  862. {
  863. struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
  864. struct console *con = ctxt->console;
  865. bool is_extended = console_srcu_read_flags(con) & CON_EXTENDED;
  866. struct printk_message pmsg = {
  867. .pbufs = ctxt->pbufs,
  868. };
  869. unsigned long con_dropped;
  870. struct nbcon_state cur;
  871. unsigned long dropped;
  872. unsigned long ulseq;
  873. /*
  874. * This function should never be called for consoles that have not
  875. * implemented the necessary callback for writing: i.e. legacy
  876. * consoles and, when atomic, nbcon consoles with no write_atomic().
  877. * Handle it as if ownership was lost and try to continue.
  878. *
  879. * Note that for nbcon consoles the write_thread() callback is
  880. * mandatory and was already checked in nbcon_alloc().
  881. */
  882. if (WARN_ON_ONCE((use_atomic && !con->write_atomic) ||
  883. !(console_srcu_read_flags(con) & CON_NBCON))) {
  884. nbcon_context_release(ctxt);
  885. return false;
  886. }
  887. /*
  888. * The printk buffers are filled within an unsafe section. This
  889. * prevents NBCON_PRIO_NORMAL and NBCON_PRIO_EMERGENCY from
  890. * clobbering each other.
  891. */
  892. if (!nbcon_context_enter_unsafe(ctxt))
  893. return false;
  894. ctxt->backlog = printk_get_next_message(&pmsg, ctxt->seq, is_extended, true);
  895. if (!ctxt->backlog)
  896. return nbcon_context_exit_unsafe(ctxt);
  897. /*
  898. * @con->dropped is not protected in case of an unsafe hostile
  899. * takeover. In that situation the update can be racy so
  900. * annotate it accordingly.
  901. */
  902. con_dropped = data_race(READ_ONCE(con->dropped));
  903. dropped = con_dropped + pmsg.dropped;
  904. if (dropped && !is_extended)
  905. console_prepend_dropped(&pmsg, dropped);
  906. /*
  907. * If the previous owner was assigned the same record, this context
  908. * has taken over ownership and is replaying the record. Prepend a
  909. * message to let the user know the record is replayed.
  910. */
  911. ulseq = atomic_long_read(&ACCESS_PRIVATE(con, nbcon_prev_seq));
  912. if (__ulseq_to_u64seq(prb, ulseq) == pmsg.seq) {
  913. console_prepend_replay(&pmsg);
  914. } else {
  915. /*
  916. * Ensure this context is still the owner before trying to
  917. * update @nbcon_prev_seq. Otherwise the value in @ulseq may
  918. * not be from the previous owner and instead be some later
  919. * value from the context that took over ownership.
  920. */
  921. nbcon_state_read(con, &cur);
  922. if (!nbcon_context_can_proceed(ctxt, &cur))
  923. return false;
  924. atomic_long_try_cmpxchg(&ACCESS_PRIVATE(con, nbcon_prev_seq), &ulseq,
  925. __u64seq_to_ulseq(pmsg.seq));
  926. }
  927. if (!nbcon_context_exit_unsafe(ctxt))
  928. return false;
  929. /* For skipped records just update seq/dropped in @con. */
  930. if (pmsg.outbuf_len == 0)
  931. goto update_con;
  932. /* Initialize the write context for driver callbacks. */
  933. nbcon_write_context_set_buf(wctxt, &pmsg.pbufs->outbuf[0], pmsg.outbuf_len);
  934. if (use_atomic)
  935. con->write_atomic(con, wctxt);
  936. else
  937. con->write_thread(con, wctxt);
  938. if (!wctxt->outbuf) {
  939. /*
  940. * Ownership was lost and reacquired by the driver. Handle it
  941. * as if ownership was lost.
  942. */
  943. nbcon_context_release(ctxt);
  944. return false;
  945. }
  946. /*
  947. * Ownership may have been lost but _not_ reacquired by the driver.
  948. * This case is detected and handled when entering unsafe to update
  949. * dropped/seq values.
  950. */
  951. /*
  952. * Since any dropped message was successfully output, reset the
  953. * dropped count for the console.
  954. */
  955. dropped = 0;
  956. update_con:
  957. /*
  958. * The dropped count and the sequence number are updated within an
  959. * unsafe section. This limits update races to the panic context and
  960. * allows the panic context to win.
  961. */
  962. if (!nbcon_context_enter_unsafe(ctxt))
  963. return false;
  964. if (dropped != con_dropped) {
  965. /* Counterpart to the READ_ONCE() above. */
  966. WRITE_ONCE(con->dropped, dropped);
  967. }
  968. nbcon_seq_try_update(ctxt, pmsg.seq + 1);
  969. return nbcon_context_exit_unsafe(ctxt);
  970. }
  971. /*
  972. * nbcon_emit_one - Print one record for an nbcon console using the
  973. * specified callback
  974. * @wctxt: An initialized write context struct to use for this context
  975. * @use_atomic: True if the write_atomic() callback is to be used
  976. *
  977. * Return: True, when a record has been printed and there are still
  978. * pending records. The caller might want to continue flushing.
  979. *
  980. * False, when there is no pending record, or when the console
  981. * context cannot be acquired, or the ownership has been lost.
  982. * The caller should give up. Either the job is done, cannot be
  983. * done, or will be handled by the owning context.
  984. *
  985. * This is an internal helper to handle the locking of the console before
  986. * calling nbcon_emit_next_record().
  987. */
  988. static bool nbcon_emit_one(struct nbcon_write_context *wctxt, bool use_atomic)
  989. {
  990. struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
  991. struct console *con = ctxt->console;
  992. unsigned long flags;
  993. bool ret = false;
  994. if (!use_atomic) {
  995. con->device_lock(con, &flags);
  996. /*
  997. * Ensure this stays on the CPU to make handover and
  998. * takeover possible.
  999. */
  1000. cant_migrate();
  1001. }
  1002. if (!nbcon_context_try_acquire(ctxt, false))
  1003. goto out;
  1004. /*
  1005. * nbcon_emit_next_record() returns false when the console was
  1006. * handed over or taken over. In both cases the context is no
  1007. * longer valid.
  1008. *
  1009. * The higher priority printing context takes over responsibility
  1010. * to print the pending records.
  1011. */
  1012. if (!nbcon_emit_next_record(wctxt, use_atomic))
  1013. goto out;
  1014. nbcon_context_release(ctxt);
  1015. ret = ctxt->backlog;
  1016. out:
  1017. if (!use_atomic)
  1018. con->device_unlock(con, flags);
  1019. return ret;
  1020. }
  1021. /**
  1022. * nbcon_kthread_should_wakeup - Check whether a printer thread should wakeup
  1023. * @con: Console to operate on
  1024. * @ctxt: The nbcon context from nbcon_context_try_acquire()
  1025. *
  1026. * Return: True if the thread should shutdown or if the console is
  1027. * allowed to print and a record is available. False otherwise.
  1028. *
  1029. * After the thread wakes up, it must first check if it should shutdown before
  1030. * attempting any printing.
  1031. */
  1032. static bool nbcon_kthread_should_wakeup(struct console *con, struct nbcon_context *ctxt)
  1033. {
  1034. bool ret = false;
  1035. short flags;
  1036. int cookie;
  1037. if (kthread_should_stop())
  1038. return true;
  1039. cookie = console_srcu_read_lock();
  1040. flags = console_srcu_read_flags(con);
  1041. if (console_is_usable(con, flags, false)) {
  1042. /* Bring the sequence in @ctxt up to date */
  1043. ctxt->seq = nbcon_seq_read(con);
  1044. ret = prb_read_valid(prb, ctxt->seq, NULL);
  1045. }
  1046. console_srcu_read_unlock(cookie);
  1047. return ret;
  1048. }
  1049. /**
  1050. * nbcon_kthread_func - The printer thread function
  1051. * @__console: Console to operate on
  1052. *
  1053. * Return: 0
  1054. */
  1055. static int nbcon_kthread_func(void *__console)
  1056. {
  1057. struct console *con = __console;
  1058. struct nbcon_write_context wctxt = {
  1059. .ctxt.console = con,
  1060. .ctxt.prio = NBCON_PRIO_NORMAL,
  1061. };
  1062. struct nbcon_context *ctxt = &ACCESS_PRIVATE(&wctxt, ctxt);
  1063. short con_flags;
  1064. bool backlog;
  1065. int cookie;
  1066. wait_for_event:
  1067. /*
  1068. * Guarantee this task is visible on the rcuwait before
  1069. * checking the wake condition.
  1070. *
  1071. * The full memory barrier within set_current_state() of
  1072. * ___rcuwait_wait_event() pairs with the full memory
  1073. * barrier within rcuwait_has_sleeper().
  1074. *
  1075. * This pairs with rcuwait_has_sleeper:A and nbcon_kthread_wake:A.
  1076. */
  1077. rcuwait_wait_event(&con->rcuwait,
  1078. nbcon_kthread_should_wakeup(con, ctxt),
  1079. TASK_INTERRUPTIBLE); /* LMM(nbcon_kthread_func:A) */
  1080. do {
  1081. if (kthread_should_stop())
  1082. return 0;
  1083. backlog = false;
  1084. /*
  1085. * Keep the srcu read lock around the entire operation so that
  1086. * synchronize_srcu() can guarantee that the kthread stopped
  1087. * or suspended printing.
  1088. */
  1089. cookie = console_srcu_read_lock();
  1090. con_flags = console_srcu_read_flags(con);
  1091. if (console_is_usable(con, con_flags, false))
  1092. backlog = nbcon_emit_one(&wctxt, false);
  1093. console_srcu_read_unlock(cookie);
  1094. cond_resched();
  1095. } while (backlog);
  1096. goto wait_for_event;
  1097. }
  1098. /**
  1099. * nbcon_irq_work - irq work to wake console printer thread
  1100. * @irq_work: The irq work to operate on
  1101. */
  1102. static void nbcon_irq_work(struct irq_work *irq_work)
  1103. {
  1104. struct console *con = container_of(irq_work, struct console, irq_work);
  1105. nbcon_kthread_wake(con);
  1106. }
  1107. static inline bool rcuwait_has_sleeper(struct rcuwait *w)
  1108. {
  1109. /*
  1110. * Guarantee any new records can be seen by tasks preparing to wait
  1111. * before this context checks if the rcuwait is empty.
  1112. *
  1113. * This full memory barrier pairs with the full memory barrier within
  1114. * set_current_state() of ___rcuwait_wait_event(), which is called
  1115. * after prepare_to_rcuwait() adds the waiter but before it has
  1116. * checked the wait condition.
  1117. *
  1118. * This pairs with nbcon_kthread_func:A.
  1119. */
  1120. smp_mb(); /* LMM(rcuwait_has_sleeper:A) */
  1121. return rcuwait_active(w);
  1122. }
  1123. /**
  1124. * nbcon_kthreads_wake - Wake up printing threads using irq_work
  1125. */
  1126. void nbcon_kthreads_wake(void)
  1127. {
  1128. struct console *con;
  1129. int cookie;
  1130. if (!printk_kthreads_running)
  1131. return;
  1132. cookie = console_srcu_read_lock();
  1133. for_each_console_srcu(con) {
  1134. if (!(console_srcu_read_flags(con) & CON_NBCON))
  1135. continue;
  1136. /*
  1137. * Only schedule irq_work if the printing thread is
  1138. * actively waiting. If not waiting, the thread will
  1139. * notice by itself that it has work to do.
  1140. */
  1141. if (rcuwait_has_sleeper(&con->rcuwait))
  1142. irq_work_queue(&con->irq_work);
  1143. }
  1144. console_srcu_read_unlock(cookie);
  1145. }
  1146. /*
  1147. * nbcon_kthread_stop - Stop a console printer thread
  1148. * @con: Console to operate on
  1149. */
  1150. void nbcon_kthread_stop(struct console *con)
  1151. {
  1152. lockdep_assert_console_list_lock_held();
  1153. if (!con->kthread)
  1154. return;
  1155. kthread_stop(con->kthread);
  1156. con->kthread = NULL;
  1157. }
  1158. /**
  1159. * nbcon_kthread_create - Create a console printer thread
  1160. * @con: Console to operate on
  1161. *
  1162. * Return: True if the kthread was started or already exists.
  1163. * Otherwise false and @con must not be registered.
  1164. *
  1165. * This function is called when it will be expected that nbcon consoles are
  1166. * flushed using the kthread. The messages printed with NBCON_PRIO_NORMAL
  1167. * will be no longer flushed by the legacy loop. This is why failure must
  1168. * be fatal for console registration.
  1169. *
  1170. * If @con was already registered and this function fails, @con must be
  1171. * unregistered before the global state variable @printk_kthreads_running
  1172. * can be set.
  1173. */
  1174. bool nbcon_kthread_create(struct console *con)
  1175. {
  1176. struct task_struct *kt;
  1177. lockdep_assert_console_list_lock_held();
  1178. if (con->kthread)
  1179. return true;
  1180. kt = kthread_run(nbcon_kthread_func, con, "pr/%s%d", con->name, con->index);
  1181. if (WARN_ON(IS_ERR(kt))) {
  1182. con_printk(KERN_ERR, con, "failed to start printing thread\n");
  1183. return false;
  1184. }
  1185. con->kthread = kt;
  1186. /*
  1187. * It is important that console printing threads are scheduled
  1188. * shortly after a printk call and with generous runtime budgets.
  1189. */
  1190. sched_set_normal(con->kthread, -20);
  1191. return true;
  1192. }
  1193. /* Track the nbcon emergency nesting per CPU. */
  1194. static DEFINE_PER_CPU(unsigned int, nbcon_pcpu_emergency_nesting);
  1195. static unsigned int early_nbcon_pcpu_emergency_nesting __initdata;
  1196. /**
  1197. * nbcon_get_cpu_emergency_nesting - Get the per CPU emergency nesting pointer
  1198. *
  1199. * Context: For reading, any context. For writing, any context which could
  1200. * not be migrated to another CPU.
  1201. * Return: Either a pointer to the per CPU emergency nesting counter of
  1202. * the current CPU or to the init data during early boot.
  1203. *
  1204. * The function is safe for reading per-CPU variables in any context because
  1205. * preemption is disabled if the current CPU is in the emergency state. See
  1206. * also nbcon_cpu_emergency_enter().
  1207. */
  1208. static __ref unsigned int *nbcon_get_cpu_emergency_nesting(void)
  1209. {
  1210. /*
  1211. * The value of __printk_percpu_data_ready gets set in normal
  1212. * context and before SMP initialization. As a result it could
  1213. * never change while inside an nbcon emergency section.
  1214. */
  1215. if (!printk_percpu_data_ready())
  1216. return &early_nbcon_pcpu_emergency_nesting;
  1217. return raw_cpu_ptr(&nbcon_pcpu_emergency_nesting);
  1218. }
  1219. /**
  1220. * nbcon_get_default_prio - The appropriate nbcon priority to use for nbcon
  1221. * printing on the current CPU
  1222. *
  1223. * Context: Any context.
  1224. * Return: The nbcon_prio to use for acquiring an nbcon console in this
  1225. * context for printing.
  1226. *
  1227. * The function is safe for reading per-CPU data in any context because
  1228. * preemption is disabled if the current CPU is in the emergency or panic
  1229. * state.
  1230. */
  1231. enum nbcon_prio nbcon_get_default_prio(void)
  1232. {
  1233. unsigned int *cpu_emergency_nesting;
  1234. if (this_cpu_in_panic())
  1235. return NBCON_PRIO_PANIC;
  1236. cpu_emergency_nesting = nbcon_get_cpu_emergency_nesting();
  1237. if (*cpu_emergency_nesting)
  1238. return NBCON_PRIO_EMERGENCY;
  1239. return NBCON_PRIO_NORMAL;
  1240. }
  1241. /**
  1242. * nbcon_legacy_emit_next_record - Print one record for an nbcon console
  1243. * in legacy contexts
  1244. * @con: The console to print on
  1245. * @handover: Will be set to true if a printk waiter has taken over the
  1246. * console_lock, in which case the caller is no longer holding
  1247. * both the console_lock and the SRCU read lock. Otherwise it
  1248. * is set to false.
  1249. * @cookie: The cookie from the SRCU read lock.
  1250. * @use_atomic: Set true when called in an atomic or unknown context.
  1251. * It affects which nbcon callback will be used: write_atomic()
  1252. * or write_thread().
  1253. *
  1254. * When false, the write_thread() callback is used and would be
  1255. * called in a preemtible context unless disabled by the
  1256. * device_lock. The legacy handover is not allowed in this mode.
  1257. *
  1258. * Context: Any context except NMI.
  1259. * Return: True, when a record has been printed and there are still
  1260. * pending records. The caller might want to continue flushing.
  1261. *
  1262. * False, when there is no pending record, or when the console
  1263. * context cannot be acquired, or the ownership has been lost.
  1264. * The caller should give up. Either the job is done, cannot be
  1265. * done, or will be handled by the owning context.
  1266. *
  1267. * This function is meant to be called by console_flush_all() to print records
  1268. * on nbcon consoles from legacy context (printing via console unlocking).
  1269. * Essentially it is the nbcon version of console_emit_next_record().
  1270. */
  1271. bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
  1272. int cookie, bool use_atomic)
  1273. {
  1274. struct nbcon_write_context wctxt = { };
  1275. struct nbcon_context *ctxt = &ACCESS_PRIVATE(&wctxt, ctxt);
  1276. unsigned long flags;
  1277. bool progress;
  1278. ctxt->console = con;
  1279. ctxt->prio = nbcon_get_default_prio();
  1280. if (use_atomic) {
  1281. /*
  1282. * In an atomic or unknown context, use the same procedure as
  1283. * in console_emit_next_record(). It allows to handover.
  1284. */
  1285. printk_safe_enter_irqsave(flags);
  1286. console_lock_spinning_enable();
  1287. stop_critical_timings();
  1288. }
  1289. progress = nbcon_emit_one(&wctxt, use_atomic);
  1290. if (use_atomic) {
  1291. start_critical_timings();
  1292. *handover = console_lock_spinning_disable_and_check(cookie);
  1293. printk_safe_exit_irqrestore(flags);
  1294. } else {
  1295. /* Non-atomic does not perform legacy spinning handovers. */
  1296. *handover = false;
  1297. }
  1298. return progress;
  1299. }
  1300. /**
  1301. * __nbcon_atomic_flush_pending_con - Flush specified nbcon console using its
  1302. * write_atomic() callback
  1303. * @con: The nbcon console to flush
  1304. * @stop_seq: Flush up until this record
  1305. * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers
  1306. *
  1307. * Return: 0 if @con was flushed up to @stop_seq Otherwise, error code on
  1308. * failure.
  1309. *
  1310. * Errors:
  1311. *
  1312. * -EPERM: Unable to acquire console ownership.
  1313. *
  1314. * -EAGAIN: Another context took over ownership while printing.
  1315. *
  1316. * -ENOENT: A record before @stop_seq is not available.
  1317. *
  1318. * If flushing up to @stop_seq was not successful, it only makes sense for the
  1319. * caller to try again when -EAGAIN was returned. When -EPERM is returned,
  1320. * this context is not allowed to acquire the console. When -ENOENT is
  1321. * returned, it cannot be expected that the unfinalized record will become
  1322. * available.
  1323. */
  1324. static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq,
  1325. bool allow_unsafe_takeover)
  1326. {
  1327. struct nbcon_write_context wctxt = { };
  1328. struct nbcon_context *ctxt = &ACCESS_PRIVATE(&wctxt, ctxt);
  1329. int err = 0;
  1330. ctxt->console = con;
  1331. ctxt->spinwait_max_us = 2000;
  1332. ctxt->prio = nbcon_get_default_prio();
  1333. ctxt->allow_unsafe_takeover = allow_unsafe_takeover;
  1334. if (!nbcon_context_try_acquire(ctxt, false))
  1335. return -EPERM;
  1336. while (nbcon_seq_read(con) < stop_seq) {
  1337. /*
  1338. * nbcon_emit_next_record() returns false when the console was
  1339. * handed over or taken over. In both cases the context is no
  1340. * longer valid.
  1341. */
  1342. if (!nbcon_emit_next_record(&wctxt, true))
  1343. return -EAGAIN;
  1344. if (!ctxt->backlog) {
  1345. /* Are there reserved but not yet finalized records? */
  1346. if (nbcon_seq_read(con) < stop_seq)
  1347. err = -ENOENT;
  1348. break;
  1349. }
  1350. }
  1351. nbcon_context_release(ctxt);
  1352. return err;
  1353. }
  1354. /**
  1355. * nbcon_atomic_flush_pending_con - Flush specified nbcon console using its
  1356. * write_atomic() callback
  1357. * @con: The nbcon console to flush
  1358. * @stop_seq: Flush up until this record
  1359. * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers
  1360. *
  1361. * This will stop flushing before @stop_seq if another context has ownership.
  1362. * That context is then responsible for the flushing. Likewise, if new records
  1363. * are added while this context was flushing and there is no other context
  1364. * to handle the printing, this context must also flush those records.
  1365. */
  1366. static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq,
  1367. bool allow_unsafe_takeover)
  1368. {
  1369. struct console_flush_type ft;
  1370. unsigned long flags;
  1371. int err;
  1372. again:
  1373. /*
  1374. * Atomic flushing does not use console driver synchronization (i.e.
  1375. * it does not hold the port lock for uart consoles). Therefore IRQs
  1376. * must be disabled to avoid being interrupted and then calling into
  1377. * a driver that will deadlock trying to acquire console ownership.
  1378. */
  1379. local_irq_save(flags);
  1380. err = __nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_takeover);
  1381. local_irq_restore(flags);
  1382. /*
  1383. * If there was a new owner (-EPERM, -EAGAIN), that context is
  1384. * responsible for completing.
  1385. *
  1386. * Do not wait for records not yet finalized (-ENOENT) to avoid a
  1387. * possible deadlock. They will either get flushed by the writer or
  1388. * eventually skipped on panic CPU.
  1389. */
  1390. if (err)
  1391. return;
  1392. /*
  1393. * If flushing was successful but more records are available, this
  1394. * context must flush those remaining records if the printer thread
  1395. * is not available do it.
  1396. */
  1397. printk_get_console_flush_type(&ft);
  1398. if (!ft.nbcon_offload &&
  1399. prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
  1400. stop_seq = prb_next_reserve_seq(prb);
  1401. goto again;
  1402. }
  1403. }
  1404. /**
  1405. * __nbcon_atomic_flush_pending - Flush all nbcon consoles using their
  1406. * write_atomic() callback
  1407. * @stop_seq: Flush up until this record
  1408. * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers
  1409. */
  1410. static void __nbcon_atomic_flush_pending(u64 stop_seq, bool allow_unsafe_takeover)
  1411. {
  1412. struct console *con;
  1413. int cookie;
  1414. cookie = console_srcu_read_lock();
  1415. for_each_console_srcu(con) {
  1416. short flags = console_srcu_read_flags(con);
  1417. if (!(flags & CON_NBCON))
  1418. continue;
  1419. if (!console_is_usable(con, flags, true))
  1420. continue;
  1421. if (nbcon_seq_read(con) >= stop_seq)
  1422. continue;
  1423. nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_takeover);
  1424. }
  1425. console_srcu_read_unlock(cookie);
  1426. }
  1427. /**
  1428. * nbcon_atomic_flush_pending - Flush all nbcon consoles using their
  1429. * write_atomic() callback
  1430. *
  1431. * Flush the backlog up through the currently newest record. Any new
  1432. * records added while flushing will not be flushed if there is another
  1433. * context available to handle the flushing. This is to avoid one CPU
  1434. * printing unbounded because other CPUs continue to add records.
  1435. */
  1436. void nbcon_atomic_flush_pending(void)
  1437. {
  1438. __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb), false);
  1439. }
  1440. /**
  1441. * nbcon_atomic_flush_unsafe - Flush all nbcon consoles using their
  1442. * write_atomic() callback and allowing unsafe hostile takeovers
  1443. *
  1444. * Flush the backlog up through the currently newest record. Unsafe hostile
  1445. * takeovers will be performed, if necessary.
  1446. */
  1447. void nbcon_atomic_flush_unsafe(void)
  1448. {
  1449. __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb), true);
  1450. }
  1451. /**
  1452. * nbcon_cpu_emergency_enter - Enter an emergency section where printk()
  1453. * messages for that CPU are flushed directly
  1454. *
  1455. * Context: Any context. Disables preemption.
  1456. *
  1457. * When within an emergency section, printk() calls will attempt to flush any
  1458. * pending messages in the ringbuffer.
  1459. */
  1460. void nbcon_cpu_emergency_enter(void)
  1461. {
  1462. unsigned int *cpu_emergency_nesting;
  1463. preempt_disable();
  1464. cpu_emergency_nesting = nbcon_get_cpu_emergency_nesting();
  1465. (*cpu_emergency_nesting)++;
  1466. }
  1467. /**
  1468. * nbcon_cpu_emergency_exit - Exit an emergency section
  1469. *
  1470. * Context: Within an emergency section. Enables preemption.
  1471. */
  1472. void nbcon_cpu_emergency_exit(void)
  1473. {
  1474. unsigned int *cpu_emergency_nesting;
  1475. cpu_emergency_nesting = nbcon_get_cpu_emergency_nesting();
  1476. if (!WARN_ON_ONCE(*cpu_emergency_nesting == 0))
  1477. (*cpu_emergency_nesting)--;
  1478. preempt_enable();
  1479. }
  1480. /**
  1481. * nbcon_alloc - Allocate and init the nbcon console specific data
  1482. * @con: Console to initialize
  1483. *
  1484. * Return: True if the console was fully allocated and initialized.
  1485. * Otherwise @con must not be registered.
  1486. *
  1487. * When allocation and init was successful, the console must be properly
  1488. * freed using nbcon_free() once it is no longer needed.
  1489. */
  1490. bool nbcon_alloc(struct console *con)
  1491. {
  1492. struct nbcon_state state = { };
  1493. /* The write_thread() callback is mandatory. */
  1494. if (WARN_ON(!con->write_thread))
  1495. return false;
  1496. rcuwait_init(&con->rcuwait);
  1497. init_irq_work(&con->irq_work, nbcon_irq_work);
  1498. atomic_long_set(&ACCESS_PRIVATE(con, nbcon_prev_seq), -1UL);
  1499. nbcon_state_set(con, &state);
  1500. /*
  1501. * Initialize @nbcon_seq to the highest possible sequence number so
  1502. * that practically speaking it will have nothing to print until a
  1503. * desired initial sequence number has been set via nbcon_seq_force().
  1504. */
  1505. atomic_long_set(&ACCESS_PRIVATE(con, nbcon_seq), ULSEQ_MAX(prb));
  1506. if (con->flags & CON_BOOT) {
  1507. /*
  1508. * Boot console printing is synchronized with legacy console
  1509. * printing, so boot consoles can share the same global printk
  1510. * buffers.
  1511. */
  1512. con->pbufs = &printk_shared_pbufs;
  1513. } else {
  1514. con->pbufs = kmalloc(sizeof(*con->pbufs), GFP_KERNEL);
  1515. if (!con->pbufs) {
  1516. con_printk(KERN_ERR, con, "failed to allocate printing buffer\n");
  1517. return false;
  1518. }
  1519. if (printk_kthreads_running) {
  1520. if (!nbcon_kthread_create(con)) {
  1521. kfree(con->pbufs);
  1522. con->pbufs = NULL;
  1523. return false;
  1524. }
  1525. }
  1526. }
  1527. return true;
  1528. }
  1529. /**
  1530. * nbcon_free - Free and cleanup the nbcon console specific data
  1531. * @con: Console to free/cleanup nbcon data
  1532. */
  1533. void nbcon_free(struct console *con)
  1534. {
  1535. struct nbcon_state state = { };
  1536. if (printk_kthreads_running)
  1537. nbcon_kthread_stop(con);
  1538. nbcon_state_set(con, &state);
  1539. /* Boot consoles share global printk buffers. */
  1540. if (!(con->flags & CON_BOOT))
  1541. kfree(con->pbufs);
  1542. con->pbufs = NULL;
  1543. }
  1544. /**
  1545. * nbcon_device_try_acquire - Try to acquire nbcon console and enter unsafe
  1546. * section
  1547. * @con: The nbcon console to acquire
  1548. *
  1549. * Context: Under the locking mechanism implemented in
  1550. * @con->device_lock() including disabling migration.
  1551. * Return: True if the console was acquired. False otherwise.
  1552. *
  1553. * Console drivers will usually use their own internal synchronization
  1554. * mechasism to synchronize between console printing and non-printing
  1555. * activities (such as setting baud rates). However, nbcon console drivers
  1556. * supporting atomic consoles may also want to mark unsafe sections when
  1557. * performing non-printing activities in order to synchronize against their
  1558. * atomic_write() callback.
  1559. *
  1560. * This function acquires the nbcon console using priority NBCON_PRIO_NORMAL
  1561. * and marks it unsafe for handover/takeover.
  1562. */
  1563. bool nbcon_device_try_acquire(struct console *con)
  1564. {
  1565. struct nbcon_context *ctxt = &ACCESS_PRIVATE(con, nbcon_device_ctxt);
  1566. cant_migrate();
  1567. memset(ctxt, 0, sizeof(*ctxt));
  1568. ctxt->console = con;
  1569. ctxt->prio = NBCON_PRIO_NORMAL;
  1570. if (!nbcon_context_try_acquire(ctxt, false))
  1571. return false;
  1572. if (!nbcon_context_enter_unsafe(ctxt))
  1573. return false;
  1574. return true;
  1575. }
  1576. EXPORT_SYMBOL_GPL(nbcon_device_try_acquire);
  1577. /**
  1578. * nbcon_device_release - Exit unsafe section and release the nbcon console
  1579. * @con: The nbcon console acquired in nbcon_device_try_acquire()
  1580. */
  1581. void nbcon_device_release(struct console *con)
  1582. {
  1583. struct nbcon_context *ctxt = &ACCESS_PRIVATE(con, nbcon_device_ctxt);
  1584. struct console_flush_type ft;
  1585. int cookie;
  1586. if (!nbcon_context_exit_unsafe(ctxt))
  1587. return;
  1588. nbcon_context_release(ctxt);
  1589. /*
  1590. * This context must flush any new records added while the console
  1591. * was locked if the printer thread is not available to do it. The
  1592. * console_srcu_read_lock must be taken to ensure the console is
  1593. * usable throughout flushing.
  1594. */
  1595. cookie = console_srcu_read_lock();
  1596. printk_get_console_flush_type(&ft);
  1597. if (console_is_usable(con, console_srcu_read_flags(con), true) &&
  1598. !ft.nbcon_offload &&
  1599. prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
  1600. /*
  1601. * If nbcon_atomic flushing is not available, fallback to
  1602. * using the legacy loop.
  1603. */
  1604. if (ft.nbcon_atomic) {
  1605. __nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb), false);
  1606. } else if (ft.legacy_direct) {
  1607. if (console_trylock())
  1608. console_unlock();
  1609. } else if (ft.legacy_offload) {
  1610. printk_trigger_flush();
  1611. }
  1612. }
  1613. console_srcu_read_unlock(cookie);
  1614. }
  1615. EXPORT_SYMBOL_GPL(nbcon_device_release);