tasks.h 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Task-based RCU implementations.
  4. *
  5. * Copyright (C) 2020 Paul E. McKenney
  6. */
  7. #ifdef CONFIG_TASKS_RCU_GENERIC
  8. #include "rcu_segcblist.h"
  9. ////////////////////////////////////////////////////////////////////////
  10. //
  11. // Generic data structures.
  12. struct rcu_tasks;
  13. typedef void (*rcu_tasks_gp_func_t)(struct rcu_tasks *rtp);
  14. typedef void (*pregp_func_t)(struct list_head *hop);
  15. typedef void (*pertask_func_t)(struct task_struct *t, struct list_head *hop);
  16. typedef void (*postscan_func_t)(struct list_head *hop);
  17. typedef void (*holdouts_func_t)(struct list_head *hop, bool ndrpt, bool *frptp);
  18. typedef void (*postgp_func_t)(struct rcu_tasks *rtp);
  19. /**
  20. * struct rcu_tasks_percpu - Per-CPU component of definition for a Tasks-RCU-like mechanism.
  21. * @cblist: Callback list.
  22. * @lock: Lock protecting per-CPU callback list.
  23. * @rtp_jiffies: Jiffies counter value for statistics.
  24. * @lazy_timer: Timer to unlazify callbacks.
  25. * @urgent_gp: Number of additional non-lazy grace periods.
  26. * @rtp_n_lock_retries: Rough lock-contention statistic.
  27. * @rtp_work: Work queue for invoking callbacks.
  28. * @rtp_irq_work: IRQ work queue for deferred wakeups.
  29. * @barrier_q_head: RCU callback for barrier operation.
  30. * @rtp_blkd_tasks: List of tasks blocked as readers.
  31. * @rtp_exit_list: List of tasks in the latter portion of do_exit().
  32. * @cpu: CPU number corresponding to this entry.
  33. * @index: Index of this CPU in rtpcp_array of the rcu_tasks structure.
  34. * @rtpp: Pointer to the rcu_tasks structure.
  35. */
  36. struct rcu_tasks_percpu {
  37. struct rcu_segcblist cblist;
  38. raw_spinlock_t __private lock;
  39. unsigned long rtp_jiffies;
  40. unsigned long rtp_n_lock_retries;
  41. struct timer_list lazy_timer;
  42. unsigned int urgent_gp;
  43. struct work_struct rtp_work;
  44. struct irq_work rtp_irq_work;
  45. struct rcu_head barrier_q_head;
  46. struct list_head rtp_blkd_tasks;
  47. struct list_head rtp_exit_list;
  48. int cpu;
  49. int index;
  50. struct rcu_tasks *rtpp;
  51. };
  52. /**
  53. * struct rcu_tasks - Definition for a Tasks-RCU-like mechanism.
  54. * @cbs_wait: RCU wait allowing a new callback to get kthread's attention.
  55. * @cbs_gbl_lock: Lock protecting callback list.
  56. * @tasks_gp_mutex: Mutex protecting grace period, needed during mid-boot dead zone.
  57. * @gp_func: This flavor's grace-period-wait function.
  58. * @gp_state: Grace period's most recent state transition (debugging).
  59. * @gp_sleep: Per-grace-period sleep to prevent CPU-bound looping.
  60. * @init_fract: Initial backoff sleep interval.
  61. * @gp_jiffies: Time of last @gp_state transition.
  62. * @gp_start: Most recent grace-period start in jiffies.
  63. * @tasks_gp_seq: Number of grace periods completed since boot in upper bits.
  64. * @n_ipis: Number of IPIs sent to encourage grace periods to end.
  65. * @n_ipis_fails: Number of IPI-send failures.
  66. * @kthread_ptr: This flavor's grace-period/callback-invocation kthread.
  67. * @lazy_jiffies: Number of jiffies to allow callbacks to be lazy.
  68. * @pregp_func: This flavor's pre-grace-period function (optional).
  69. * @pertask_func: This flavor's per-task scan function (optional).
  70. * @postscan_func: This flavor's post-task scan function (optional).
  71. * @holdouts_func: This flavor's holdout-list scan function (optional).
  72. * @postgp_func: This flavor's post-grace-period function (optional).
  73. * @call_func: This flavor's call_rcu()-equivalent function.
  74. * @wait_state: Task state for synchronous grace-period waits (default TASK_UNINTERRUPTIBLE).
  75. * @rtpcpu: This flavor's rcu_tasks_percpu structure.
  76. * @rtpcp_array: Array of pointers to rcu_tasks_percpu structure of CPUs in cpu_possible_mask.
  77. * @percpu_enqueue_shift: Shift down CPU ID this much when enqueuing callbacks.
  78. * @percpu_enqueue_lim: Number of per-CPU callback queues in use for enqueuing.
  79. * @percpu_dequeue_lim: Number of per-CPU callback queues in use for dequeuing.
  80. * @percpu_dequeue_gpseq: RCU grace-period number to propagate enqueue limit to dequeuers.
  81. * @barrier_q_mutex: Serialize barrier operations.
  82. * @barrier_q_count: Number of queues being waited on.
  83. * @barrier_q_completion: Barrier wait/wakeup mechanism.
  84. * @barrier_q_seq: Sequence number for barrier operations.
  85. * @barrier_q_start: Most recent barrier start in jiffies.
  86. * @name: This flavor's textual name.
  87. * @kname: This flavor's kthread name.
  88. */
  89. struct rcu_tasks {
  90. struct rcuwait cbs_wait;
  91. raw_spinlock_t cbs_gbl_lock;
  92. struct mutex tasks_gp_mutex;
  93. int gp_state;
  94. int gp_sleep;
  95. int init_fract;
  96. unsigned long gp_jiffies;
  97. unsigned long gp_start;
  98. unsigned long tasks_gp_seq;
  99. unsigned long n_ipis;
  100. unsigned long n_ipis_fails;
  101. struct task_struct *kthread_ptr;
  102. unsigned long lazy_jiffies;
  103. rcu_tasks_gp_func_t gp_func;
  104. pregp_func_t pregp_func;
  105. pertask_func_t pertask_func;
  106. postscan_func_t postscan_func;
  107. holdouts_func_t holdouts_func;
  108. postgp_func_t postgp_func;
  109. call_rcu_func_t call_func;
  110. unsigned int wait_state;
  111. struct rcu_tasks_percpu __percpu *rtpcpu;
  112. struct rcu_tasks_percpu **rtpcp_array;
  113. int percpu_enqueue_shift;
  114. int percpu_enqueue_lim;
  115. int percpu_dequeue_lim;
  116. unsigned long percpu_dequeue_gpseq;
  117. struct mutex barrier_q_mutex;
  118. atomic_t barrier_q_count;
  119. struct completion barrier_q_completion;
  120. unsigned long barrier_q_seq;
  121. unsigned long barrier_q_start;
  122. char *name;
  123. char *kname;
  124. };
  125. static void call_rcu_tasks_iw_wakeup(struct irq_work *iwp);
  126. #define DEFINE_RCU_TASKS(rt_name, gp, call, n) \
  127. static DEFINE_PER_CPU(struct rcu_tasks_percpu, rt_name ## __percpu) = { \
  128. .lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name ## __percpu.cbs_pcpu_lock), \
  129. .rtp_irq_work = IRQ_WORK_INIT_HARD(call_rcu_tasks_iw_wakeup), \
  130. }; \
  131. static struct rcu_tasks rt_name = \
  132. { \
  133. .cbs_wait = __RCUWAIT_INITIALIZER(rt_name.wait), \
  134. .cbs_gbl_lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name.cbs_gbl_lock), \
  135. .tasks_gp_mutex = __MUTEX_INITIALIZER(rt_name.tasks_gp_mutex), \
  136. .gp_func = gp, \
  137. .call_func = call, \
  138. .wait_state = TASK_UNINTERRUPTIBLE, \
  139. .rtpcpu = &rt_name ## __percpu, \
  140. .lazy_jiffies = DIV_ROUND_UP(HZ, 4), \
  141. .name = n, \
  142. .percpu_enqueue_shift = order_base_2(CONFIG_NR_CPUS), \
  143. .percpu_enqueue_lim = 1, \
  144. .percpu_dequeue_lim = 1, \
  145. .barrier_q_mutex = __MUTEX_INITIALIZER(rt_name.barrier_q_mutex), \
  146. .barrier_q_seq = (0UL - 50UL) << RCU_SEQ_CTR_SHIFT, \
  147. .kname = #rt_name, \
  148. }
  149. #ifdef CONFIG_TASKS_RCU
  150. /* Report delay of scan exiting tasklist in rcu_tasks_postscan(). */
  151. static void tasks_rcu_exit_srcu_stall(struct timer_list *unused);
  152. static DEFINE_TIMER(tasks_rcu_exit_srcu_stall_timer, tasks_rcu_exit_srcu_stall);
  153. #endif
  154. /* Avoid IPIing CPUs early in the grace period. */
  155. #define RCU_TASK_IPI_DELAY (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) ? HZ / 2 : 0)
  156. static int rcu_task_ipi_delay __read_mostly = RCU_TASK_IPI_DELAY;
  157. module_param(rcu_task_ipi_delay, int, 0644);
  158. /* Control stall timeouts. Disable with <= 0, otherwise jiffies till stall. */
  159. #define RCU_TASK_BOOT_STALL_TIMEOUT (HZ * 30)
  160. #define RCU_TASK_STALL_TIMEOUT (HZ * 60 * 10)
  161. static int rcu_task_stall_timeout __read_mostly = RCU_TASK_STALL_TIMEOUT;
  162. module_param(rcu_task_stall_timeout, int, 0644);
  163. #define RCU_TASK_STALL_INFO (HZ * 10)
  164. static int rcu_task_stall_info __read_mostly = RCU_TASK_STALL_INFO;
  165. module_param(rcu_task_stall_info, int, 0644);
  166. static int rcu_task_stall_info_mult __read_mostly = 3;
  167. module_param(rcu_task_stall_info_mult, int, 0444);
  168. static int rcu_task_enqueue_lim __read_mostly = -1;
  169. module_param(rcu_task_enqueue_lim, int, 0444);
  170. static bool rcu_task_cb_adjust;
  171. static int rcu_task_contend_lim __read_mostly = 100;
  172. module_param(rcu_task_contend_lim, int, 0444);
  173. static int rcu_task_collapse_lim __read_mostly = 10;
  174. module_param(rcu_task_collapse_lim, int, 0444);
  175. static int rcu_task_lazy_lim __read_mostly = 32;
  176. module_param(rcu_task_lazy_lim, int, 0444);
  177. static int rcu_task_cpu_ids;
  178. /* RCU tasks grace-period state for debugging. */
  179. #define RTGS_INIT 0
  180. #define RTGS_WAIT_WAIT_CBS 1
  181. #define RTGS_WAIT_GP 2
  182. #define RTGS_PRE_WAIT_GP 3
  183. #define RTGS_SCAN_TASKLIST 4
  184. #define RTGS_POST_SCAN_TASKLIST 5
  185. #define RTGS_WAIT_SCAN_HOLDOUTS 6
  186. #define RTGS_SCAN_HOLDOUTS 7
  187. #define RTGS_POST_GP 8
  188. #define RTGS_WAIT_READERS 9
  189. #define RTGS_INVOKE_CBS 10
  190. #define RTGS_WAIT_CBS 11
  191. #ifndef CONFIG_TINY_RCU
  192. static const char * const rcu_tasks_gp_state_names[] = {
  193. "RTGS_INIT",
  194. "RTGS_WAIT_WAIT_CBS",
  195. "RTGS_WAIT_GP",
  196. "RTGS_PRE_WAIT_GP",
  197. "RTGS_SCAN_TASKLIST",
  198. "RTGS_POST_SCAN_TASKLIST",
  199. "RTGS_WAIT_SCAN_HOLDOUTS",
  200. "RTGS_SCAN_HOLDOUTS",
  201. "RTGS_POST_GP",
  202. "RTGS_WAIT_READERS",
  203. "RTGS_INVOKE_CBS",
  204. "RTGS_WAIT_CBS",
  205. };
  206. #endif /* #ifndef CONFIG_TINY_RCU */
  207. ////////////////////////////////////////////////////////////////////////
  208. //
  209. // Generic code.
  210. static void rcu_tasks_invoke_cbs_wq(struct work_struct *wp);
  211. /* Record grace-period phase and time. */
  212. static void set_tasks_gp_state(struct rcu_tasks *rtp, int newstate)
  213. {
  214. rtp->gp_state = newstate;
  215. rtp->gp_jiffies = jiffies;
  216. }
  217. #ifndef CONFIG_TINY_RCU
  218. /* Return state name. */
  219. static const char *tasks_gp_state_getname(struct rcu_tasks *rtp)
  220. {
  221. int i = data_race(rtp->gp_state); // Let KCSAN detect update races
  222. int j = READ_ONCE(i); // Prevent the compiler from reading twice
  223. if (j >= ARRAY_SIZE(rcu_tasks_gp_state_names))
  224. return "???";
  225. return rcu_tasks_gp_state_names[j];
  226. }
  227. #endif /* #ifndef CONFIG_TINY_RCU */
  228. // Initialize per-CPU callback lists for the specified flavor of
  229. // Tasks RCU. Do not enqueue callbacks before this function is invoked.
  230. static void cblist_init_generic(struct rcu_tasks *rtp)
  231. {
  232. int cpu;
  233. int lim;
  234. int shift;
  235. int maxcpu;
  236. int index = 0;
  237. if (rcu_task_enqueue_lim < 0) {
  238. rcu_task_enqueue_lim = 1;
  239. rcu_task_cb_adjust = true;
  240. } else if (rcu_task_enqueue_lim == 0) {
  241. rcu_task_enqueue_lim = 1;
  242. }
  243. lim = rcu_task_enqueue_lim;
  244. rtp->rtpcp_array = kcalloc(num_possible_cpus(), sizeof(struct rcu_tasks_percpu *), GFP_KERNEL);
  245. BUG_ON(!rtp->rtpcp_array);
  246. for_each_possible_cpu(cpu) {
  247. struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
  248. WARN_ON_ONCE(!rtpcp);
  249. if (cpu)
  250. raw_spin_lock_init(&ACCESS_PRIVATE(rtpcp, lock));
  251. if (rcu_segcblist_empty(&rtpcp->cblist))
  252. rcu_segcblist_init(&rtpcp->cblist);
  253. INIT_WORK(&rtpcp->rtp_work, rcu_tasks_invoke_cbs_wq);
  254. rtpcp->cpu = cpu;
  255. rtpcp->rtpp = rtp;
  256. rtpcp->index = index;
  257. rtp->rtpcp_array[index] = rtpcp;
  258. index++;
  259. if (!rtpcp->rtp_blkd_tasks.next)
  260. INIT_LIST_HEAD(&rtpcp->rtp_blkd_tasks);
  261. if (!rtpcp->rtp_exit_list.next)
  262. INIT_LIST_HEAD(&rtpcp->rtp_exit_list);
  263. rtpcp->barrier_q_head.next = &rtpcp->barrier_q_head;
  264. maxcpu = cpu;
  265. }
  266. rcu_task_cpu_ids = maxcpu + 1;
  267. if (lim > rcu_task_cpu_ids)
  268. lim = rcu_task_cpu_ids;
  269. shift = ilog2(rcu_task_cpu_ids / lim);
  270. if (((rcu_task_cpu_ids - 1) >> shift) >= lim)
  271. shift++;
  272. WRITE_ONCE(rtp->percpu_enqueue_shift, shift);
  273. WRITE_ONCE(rtp->percpu_dequeue_lim, lim);
  274. smp_store_release(&rtp->percpu_enqueue_lim, lim);
  275. pr_info("%s: Setting shift to %d and lim to %d rcu_task_cb_adjust=%d rcu_task_cpu_ids=%d.\n",
  276. rtp->name, data_race(rtp->percpu_enqueue_shift), data_race(rtp->percpu_enqueue_lim),
  277. rcu_task_cb_adjust, rcu_task_cpu_ids);
  278. }
  279. // Compute wakeup time for lazy callback timer.
  280. static unsigned long rcu_tasks_lazy_time(struct rcu_tasks *rtp)
  281. {
  282. return jiffies + rtp->lazy_jiffies;
  283. }
  284. // Timer handler that unlazifies lazy callbacks.
  285. static void call_rcu_tasks_generic_timer(struct timer_list *tlp)
  286. {
  287. unsigned long flags;
  288. bool needwake = false;
  289. struct rcu_tasks *rtp;
  290. struct rcu_tasks_percpu *rtpcp = from_timer(rtpcp, tlp, lazy_timer);
  291. rtp = rtpcp->rtpp;
  292. raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
  293. if (!rcu_segcblist_empty(&rtpcp->cblist) && rtp->lazy_jiffies) {
  294. if (!rtpcp->urgent_gp)
  295. rtpcp->urgent_gp = 1;
  296. needwake = true;
  297. mod_timer(&rtpcp->lazy_timer, rcu_tasks_lazy_time(rtp));
  298. }
  299. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  300. if (needwake)
  301. rcuwait_wake_up(&rtp->cbs_wait);
  302. }
  303. // IRQ-work handler that does deferred wakeup for call_rcu_tasks_generic().
  304. static void call_rcu_tasks_iw_wakeup(struct irq_work *iwp)
  305. {
  306. struct rcu_tasks *rtp;
  307. struct rcu_tasks_percpu *rtpcp = container_of(iwp, struct rcu_tasks_percpu, rtp_irq_work);
  308. rtp = rtpcp->rtpp;
  309. rcuwait_wake_up(&rtp->cbs_wait);
  310. }
  311. // Enqueue a callback for the specified flavor of Tasks RCU.
  312. static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
  313. struct rcu_tasks *rtp)
  314. {
  315. int chosen_cpu;
  316. unsigned long flags;
  317. bool havekthread = smp_load_acquire(&rtp->kthread_ptr);
  318. int ideal_cpu;
  319. unsigned long j;
  320. bool needadjust = false;
  321. bool needwake;
  322. struct rcu_tasks_percpu *rtpcp;
  323. rhp->next = NULL;
  324. rhp->func = func;
  325. local_irq_save(flags);
  326. rcu_read_lock();
  327. ideal_cpu = smp_processor_id() >> READ_ONCE(rtp->percpu_enqueue_shift);
  328. chosen_cpu = cpumask_next(ideal_cpu - 1, cpu_possible_mask);
  329. WARN_ON_ONCE(chosen_cpu >= rcu_task_cpu_ids);
  330. rtpcp = per_cpu_ptr(rtp->rtpcpu, chosen_cpu);
  331. if (!raw_spin_trylock_rcu_node(rtpcp)) { // irqs already disabled.
  332. raw_spin_lock_rcu_node(rtpcp); // irqs already disabled.
  333. j = jiffies;
  334. if (rtpcp->rtp_jiffies != j) {
  335. rtpcp->rtp_jiffies = j;
  336. rtpcp->rtp_n_lock_retries = 0;
  337. }
  338. if (rcu_task_cb_adjust && ++rtpcp->rtp_n_lock_retries > rcu_task_contend_lim &&
  339. READ_ONCE(rtp->percpu_enqueue_lim) != rcu_task_cpu_ids)
  340. needadjust = true; // Defer adjustment to avoid deadlock.
  341. }
  342. // Queuing callbacks before initialization not yet supported.
  343. if (WARN_ON_ONCE(!rcu_segcblist_is_enabled(&rtpcp->cblist)))
  344. rcu_segcblist_init(&rtpcp->cblist);
  345. needwake = (func == wakeme_after_rcu) ||
  346. (rcu_segcblist_n_cbs(&rtpcp->cblist) == rcu_task_lazy_lim);
  347. if (havekthread && !needwake && !timer_pending(&rtpcp->lazy_timer)) {
  348. if (rtp->lazy_jiffies)
  349. mod_timer(&rtpcp->lazy_timer, rcu_tasks_lazy_time(rtp));
  350. else
  351. needwake = rcu_segcblist_empty(&rtpcp->cblist);
  352. }
  353. if (needwake)
  354. rtpcp->urgent_gp = 3;
  355. rcu_segcblist_enqueue(&rtpcp->cblist, rhp);
  356. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  357. if (unlikely(needadjust)) {
  358. raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
  359. if (rtp->percpu_enqueue_lim != rcu_task_cpu_ids) {
  360. WRITE_ONCE(rtp->percpu_enqueue_shift, 0);
  361. WRITE_ONCE(rtp->percpu_dequeue_lim, rcu_task_cpu_ids);
  362. smp_store_release(&rtp->percpu_enqueue_lim, rcu_task_cpu_ids);
  363. pr_info("Switching %s to per-CPU callback queuing.\n", rtp->name);
  364. }
  365. raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
  366. }
  367. rcu_read_unlock();
  368. /* We can't create the thread unless interrupts are enabled. */
  369. if (needwake && READ_ONCE(rtp->kthread_ptr))
  370. irq_work_queue(&rtpcp->rtp_irq_work);
  371. }
  372. // RCU callback function for rcu_barrier_tasks_generic().
  373. static void rcu_barrier_tasks_generic_cb(struct rcu_head *rhp)
  374. {
  375. struct rcu_tasks *rtp;
  376. struct rcu_tasks_percpu *rtpcp;
  377. rhp->next = rhp; // Mark the callback as having been invoked.
  378. rtpcp = container_of(rhp, struct rcu_tasks_percpu, barrier_q_head);
  379. rtp = rtpcp->rtpp;
  380. if (atomic_dec_and_test(&rtp->barrier_q_count))
  381. complete(&rtp->barrier_q_completion);
  382. }
  383. // Wait for all in-flight callbacks for the specified RCU Tasks flavor.
  384. // Operates in a manner similar to rcu_barrier().
  385. static void __maybe_unused rcu_barrier_tasks_generic(struct rcu_tasks *rtp)
  386. {
  387. int cpu;
  388. unsigned long flags;
  389. struct rcu_tasks_percpu *rtpcp;
  390. unsigned long s = rcu_seq_snap(&rtp->barrier_q_seq);
  391. mutex_lock(&rtp->barrier_q_mutex);
  392. if (rcu_seq_done(&rtp->barrier_q_seq, s)) {
  393. smp_mb();
  394. mutex_unlock(&rtp->barrier_q_mutex);
  395. return;
  396. }
  397. rtp->barrier_q_start = jiffies;
  398. rcu_seq_start(&rtp->barrier_q_seq);
  399. init_completion(&rtp->barrier_q_completion);
  400. atomic_set(&rtp->barrier_q_count, 2);
  401. for_each_possible_cpu(cpu) {
  402. if (cpu >= smp_load_acquire(&rtp->percpu_dequeue_lim))
  403. break;
  404. rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
  405. rtpcp->barrier_q_head.func = rcu_barrier_tasks_generic_cb;
  406. raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
  407. if (rcu_segcblist_entrain(&rtpcp->cblist, &rtpcp->barrier_q_head))
  408. atomic_inc(&rtp->barrier_q_count);
  409. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  410. }
  411. if (atomic_sub_and_test(2, &rtp->barrier_q_count))
  412. complete(&rtp->barrier_q_completion);
  413. wait_for_completion(&rtp->barrier_q_completion);
  414. rcu_seq_end(&rtp->barrier_q_seq);
  415. mutex_unlock(&rtp->barrier_q_mutex);
  416. }
  417. // Advance callbacks and indicate whether either a grace period or
  418. // callback invocation is needed.
  419. static int rcu_tasks_need_gpcb(struct rcu_tasks *rtp)
  420. {
  421. int cpu;
  422. int dequeue_limit;
  423. unsigned long flags;
  424. bool gpdone = poll_state_synchronize_rcu(rtp->percpu_dequeue_gpseq);
  425. long n;
  426. long ncbs = 0;
  427. long ncbsnz = 0;
  428. int needgpcb = 0;
  429. dequeue_limit = smp_load_acquire(&rtp->percpu_dequeue_lim);
  430. for (cpu = 0; cpu < dequeue_limit; cpu++) {
  431. if (!cpu_possible(cpu))
  432. continue;
  433. struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
  434. /* Advance and accelerate any new callbacks. */
  435. if (!rcu_segcblist_n_cbs(&rtpcp->cblist))
  436. continue;
  437. raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
  438. // Should we shrink down to a single callback queue?
  439. n = rcu_segcblist_n_cbs(&rtpcp->cblist);
  440. if (n) {
  441. ncbs += n;
  442. if (cpu > 0)
  443. ncbsnz += n;
  444. }
  445. rcu_segcblist_advance(&rtpcp->cblist, rcu_seq_current(&rtp->tasks_gp_seq));
  446. (void)rcu_segcblist_accelerate(&rtpcp->cblist, rcu_seq_snap(&rtp->tasks_gp_seq));
  447. if (rtpcp->urgent_gp > 0 && rcu_segcblist_pend_cbs(&rtpcp->cblist)) {
  448. if (rtp->lazy_jiffies)
  449. rtpcp->urgent_gp--;
  450. needgpcb |= 0x3;
  451. } else if (rcu_segcblist_empty(&rtpcp->cblist)) {
  452. rtpcp->urgent_gp = 0;
  453. }
  454. if (rcu_segcblist_ready_cbs(&rtpcp->cblist))
  455. needgpcb |= 0x1;
  456. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  457. }
  458. // Shrink down to a single callback queue if appropriate.
  459. // This is done in two stages: (1) If there are no more than
  460. // rcu_task_collapse_lim callbacks on CPU 0 and none on any other
  461. // CPU, limit enqueueing to CPU 0. (2) After an RCU grace period,
  462. // if there has not been an increase in callbacks, limit dequeuing
  463. // to CPU 0. Note the matching RCU read-side critical section in
  464. // call_rcu_tasks_generic().
  465. if (rcu_task_cb_adjust && ncbs <= rcu_task_collapse_lim) {
  466. raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
  467. if (rtp->percpu_enqueue_lim > 1) {
  468. WRITE_ONCE(rtp->percpu_enqueue_shift, order_base_2(rcu_task_cpu_ids));
  469. smp_store_release(&rtp->percpu_enqueue_lim, 1);
  470. rtp->percpu_dequeue_gpseq = get_state_synchronize_rcu();
  471. gpdone = false;
  472. pr_info("Starting switch %s to CPU-0 callback queuing.\n", rtp->name);
  473. }
  474. raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
  475. }
  476. if (rcu_task_cb_adjust && !ncbsnz && gpdone) {
  477. raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
  478. if (rtp->percpu_enqueue_lim < rtp->percpu_dequeue_lim) {
  479. WRITE_ONCE(rtp->percpu_dequeue_lim, 1);
  480. pr_info("Completing switch %s to CPU-0 callback queuing.\n", rtp->name);
  481. }
  482. if (rtp->percpu_dequeue_lim == 1) {
  483. for (cpu = rtp->percpu_dequeue_lim; cpu < rcu_task_cpu_ids; cpu++) {
  484. if (!cpu_possible(cpu))
  485. continue;
  486. struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
  487. WARN_ON_ONCE(rcu_segcblist_n_cbs(&rtpcp->cblist));
  488. }
  489. }
  490. raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
  491. }
  492. return needgpcb;
  493. }
  494. // Advance callbacks and invoke any that are ready.
  495. static void rcu_tasks_invoke_cbs(struct rcu_tasks *rtp, struct rcu_tasks_percpu *rtpcp)
  496. {
  497. int cpuwq;
  498. unsigned long flags;
  499. int len;
  500. int index;
  501. struct rcu_head *rhp;
  502. struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
  503. struct rcu_tasks_percpu *rtpcp_next;
  504. index = rtpcp->index * 2 + 1;
  505. if (index < num_possible_cpus()) {
  506. rtpcp_next = rtp->rtpcp_array[index];
  507. if (rtpcp_next->cpu < smp_load_acquire(&rtp->percpu_dequeue_lim)) {
  508. cpuwq = rcu_cpu_beenfullyonline(rtpcp_next->cpu) ? rtpcp_next->cpu : WORK_CPU_UNBOUND;
  509. queue_work_on(cpuwq, system_wq, &rtpcp_next->rtp_work);
  510. index++;
  511. if (index < num_possible_cpus()) {
  512. rtpcp_next = rtp->rtpcp_array[index];
  513. if (rtpcp_next->cpu < smp_load_acquire(&rtp->percpu_dequeue_lim)) {
  514. cpuwq = rcu_cpu_beenfullyonline(rtpcp_next->cpu) ? rtpcp_next->cpu : WORK_CPU_UNBOUND;
  515. queue_work_on(cpuwq, system_wq, &rtpcp_next->rtp_work);
  516. }
  517. }
  518. }
  519. }
  520. if (rcu_segcblist_empty(&rtpcp->cblist))
  521. return;
  522. raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
  523. rcu_segcblist_advance(&rtpcp->cblist, rcu_seq_current(&rtp->tasks_gp_seq));
  524. rcu_segcblist_extract_done_cbs(&rtpcp->cblist, &rcl);
  525. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  526. len = rcl.len;
  527. for (rhp = rcu_cblist_dequeue(&rcl); rhp; rhp = rcu_cblist_dequeue(&rcl)) {
  528. debug_rcu_head_callback(rhp);
  529. local_bh_disable();
  530. rhp->func(rhp);
  531. local_bh_enable();
  532. cond_resched();
  533. }
  534. raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
  535. rcu_segcblist_add_len(&rtpcp->cblist, -len);
  536. (void)rcu_segcblist_accelerate(&rtpcp->cblist, rcu_seq_snap(&rtp->tasks_gp_seq));
  537. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  538. }
  539. // Workqueue flood to advance callbacks and invoke any that are ready.
  540. static void rcu_tasks_invoke_cbs_wq(struct work_struct *wp)
  541. {
  542. struct rcu_tasks *rtp;
  543. struct rcu_tasks_percpu *rtpcp = container_of(wp, struct rcu_tasks_percpu, rtp_work);
  544. rtp = rtpcp->rtpp;
  545. rcu_tasks_invoke_cbs(rtp, rtpcp);
  546. }
  547. // Wait for one grace period.
  548. static void rcu_tasks_one_gp(struct rcu_tasks *rtp, bool midboot)
  549. {
  550. int needgpcb;
  551. mutex_lock(&rtp->tasks_gp_mutex);
  552. // If there were none, wait a bit and start over.
  553. if (unlikely(midboot)) {
  554. needgpcb = 0x2;
  555. } else {
  556. mutex_unlock(&rtp->tasks_gp_mutex);
  557. set_tasks_gp_state(rtp, RTGS_WAIT_CBS);
  558. rcuwait_wait_event(&rtp->cbs_wait,
  559. (needgpcb = rcu_tasks_need_gpcb(rtp)),
  560. TASK_IDLE);
  561. mutex_lock(&rtp->tasks_gp_mutex);
  562. }
  563. if (needgpcb & 0x2) {
  564. // Wait for one grace period.
  565. set_tasks_gp_state(rtp, RTGS_WAIT_GP);
  566. rtp->gp_start = jiffies;
  567. rcu_seq_start(&rtp->tasks_gp_seq);
  568. rtp->gp_func(rtp);
  569. rcu_seq_end(&rtp->tasks_gp_seq);
  570. }
  571. // Invoke callbacks.
  572. set_tasks_gp_state(rtp, RTGS_INVOKE_CBS);
  573. rcu_tasks_invoke_cbs(rtp, per_cpu_ptr(rtp->rtpcpu, 0));
  574. mutex_unlock(&rtp->tasks_gp_mutex);
  575. }
  576. // RCU-tasks kthread that detects grace periods and invokes callbacks.
  577. static int __noreturn rcu_tasks_kthread(void *arg)
  578. {
  579. int cpu;
  580. struct rcu_tasks *rtp = arg;
  581. for_each_possible_cpu(cpu) {
  582. struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
  583. timer_setup(&rtpcp->lazy_timer, call_rcu_tasks_generic_timer, 0);
  584. rtpcp->urgent_gp = 1;
  585. }
  586. /* Run on housekeeping CPUs by default. Sysadm can move if desired. */
  587. housekeeping_affine(current, HK_TYPE_RCU);
  588. smp_store_release(&rtp->kthread_ptr, current); // Let GPs start!
  589. /*
  590. * Each pass through the following loop makes one check for
  591. * newly arrived callbacks, and, if there are some, waits for
  592. * one RCU-tasks grace period and then invokes the callbacks.
  593. * This loop is terminated by the system going down. ;-)
  594. */
  595. for (;;) {
  596. // Wait for one grace period and invoke any callbacks
  597. // that are ready.
  598. rcu_tasks_one_gp(rtp, false);
  599. // Paranoid sleep to keep this from entering a tight loop.
  600. schedule_timeout_idle(rtp->gp_sleep);
  601. }
  602. }
  603. // Wait for a grace period for the specified flavor of Tasks RCU.
  604. static void synchronize_rcu_tasks_generic(struct rcu_tasks *rtp)
  605. {
  606. /* Complain if the scheduler has not started. */
  607. if (WARN_ONCE(rcu_scheduler_active == RCU_SCHEDULER_INACTIVE,
  608. "synchronize_%s() called too soon", rtp->name))
  609. return;
  610. // If the grace-period kthread is running, use it.
  611. if (READ_ONCE(rtp->kthread_ptr)) {
  612. wait_rcu_gp_state(rtp->wait_state, rtp->call_func);
  613. return;
  614. }
  615. rcu_tasks_one_gp(rtp, true);
  616. }
  617. /* Spawn RCU-tasks grace-period kthread. */
  618. static void __init rcu_spawn_tasks_kthread_generic(struct rcu_tasks *rtp)
  619. {
  620. struct task_struct *t;
  621. t = kthread_run(rcu_tasks_kthread, rtp, "%s_kthread", rtp->kname);
  622. if (WARN_ONCE(IS_ERR(t), "%s: Could not start %s grace-period kthread, OOM is now expected behavior\n", __func__, rtp->name))
  623. return;
  624. smp_mb(); /* Ensure others see full kthread. */
  625. }
  626. #ifndef CONFIG_TINY_RCU
  627. /*
  628. * Print any non-default Tasks RCU settings.
  629. */
  630. static void __init rcu_tasks_bootup_oddness(void)
  631. {
  632. #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
  633. int rtsimc;
  634. if (rcu_task_stall_timeout != RCU_TASK_STALL_TIMEOUT)
  635. pr_info("\tTasks-RCU CPU stall warnings timeout set to %d (rcu_task_stall_timeout).\n", rcu_task_stall_timeout);
  636. rtsimc = clamp(rcu_task_stall_info_mult, 1, 10);
  637. if (rtsimc != rcu_task_stall_info_mult) {
  638. pr_info("\tTasks-RCU CPU stall info multiplier clamped to %d (rcu_task_stall_info_mult).\n", rtsimc);
  639. rcu_task_stall_info_mult = rtsimc;
  640. }
  641. #endif /* #ifdef CONFIG_TASKS_RCU */
  642. #ifdef CONFIG_TASKS_RCU
  643. pr_info("\tTrampoline variant of Tasks RCU enabled.\n");
  644. #endif /* #ifdef CONFIG_TASKS_RCU */
  645. #ifdef CONFIG_TASKS_RUDE_RCU
  646. pr_info("\tRude variant of Tasks RCU enabled.\n");
  647. #endif /* #ifdef CONFIG_TASKS_RUDE_RCU */
  648. #ifdef CONFIG_TASKS_TRACE_RCU
  649. pr_info("\tTracing variant of Tasks RCU enabled.\n");
  650. #endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
  651. }
  652. /* Dump out rcutorture-relevant state common to all RCU-tasks flavors. */
  653. static void show_rcu_tasks_generic_gp_kthread(struct rcu_tasks *rtp, char *s)
  654. {
  655. int cpu;
  656. bool havecbs = false;
  657. bool haveurgent = false;
  658. bool haveurgentcbs = false;
  659. for_each_possible_cpu(cpu) {
  660. struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
  661. if (!data_race(rcu_segcblist_empty(&rtpcp->cblist)))
  662. havecbs = true;
  663. if (data_race(rtpcp->urgent_gp))
  664. haveurgent = true;
  665. if (!data_race(rcu_segcblist_empty(&rtpcp->cblist)) && data_race(rtpcp->urgent_gp))
  666. haveurgentcbs = true;
  667. if (havecbs && haveurgent && haveurgentcbs)
  668. break;
  669. }
  670. pr_info("%s: %s(%d) since %lu g:%lu i:%lu/%lu %c%c%c%c l:%lu %s\n",
  671. rtp->kname,
  672. tasks_gp_state_getname(rtp), data_race(rtp->gp_state),
  673. jiffies - data_race(rtp->gp_jiffies),
  674. data_race(rcu_seq_current(&rtp->tasks_gp_seq)),
  675. data_race(rtp->n_ipis_fails), data_race(rtp->n_ipis),
  676. ".k"[!!data_race(rtp->kthread_ptr)],
  677. ".C"[havecbs],
  678. ".u"[haveurgent],
  679. ".U"[haveurgentcbs],
  680. rtp->lazy_jiffies,
  681. s);
  682. }
  683. /* Dump out more rcutorture-relevant state common to all RCU-tasks flavors. */
  684. static void rcu_tasks_torture_stats_print_generic(struct rcu_tasks *rtp, char *tt,
  685. char *tf, char *tst)
  686. {
  687. cpumask_var_t cm;
  688. int cpu;
  689. bool gotcb = false;
  690. unsigned long j = jiffies;
  691. pr_alert("%s%s Tasks%s RCU g%ld gp_start %lu gp_jiffies %lu gp_state %d (%s).\n",
  692. tt, tf, tst, data_race(rtp->tasks_gp_seq),
  693. j - data_race(rtp->gp_start), j - data_race(rtp->gp_jiffies),
  694. data_race(rtp->gp_state), tasks_gp_state_getname(rtp));
  695. pr_alert("\tEnqueue shift %d limit %d Dequeue limit %d gpseq %lu.\n",
  696. data_race(rtp->percpu_enqueue_shift),
  697. data_race(rtp->percpu_enqueue_lim),
  698. data_race(rtp->percpu_dequeue_lim),
  699. data_race(rtp->percpu_dequeue_gpseq));
  700. (void)zalloc_cpumask_var(&cm, GFP_KERNEL);
  701. pr_alert("\tCallback counts:");
  702. for_each_possible_cpu(cpu) {
  703. long n;
  704. struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
  705. if (cpumask_available(cm) && !rcu_barrier_cb_is_done(&rtpcp->barrier_q_head))
  706. cpumask_set_cpu(cpu, cm);
  707. n = rcu_segcblist_n_cbs(&rtpcp->cblist);
  708. if (!n)
  709. continue;
  710. pr_cont(" %d:%ld", cpu, n);
  711. gotcb = true;
  712. }
  713. if (gotcb)
  714. pr_cont(".\n");
  715. else
  716. pr_cont(" (none).\n");
  717. pr_alert("\tBarrier seq %lu start %lu count %d holdout CPUs ",
  718. data_race(rtp->barrier_q_seq), j - data_race(rtp->barrier_q_start),
  719. atomic_read(&rtp->barrier_q_count));
  720. if (cpumask_available(cm) && !cpumask_empty(cm))
  721. pr_cont(" %*pbl.\n", cpumask_pr_args(cm));
  722. else
  723. pr_cont("(none).\n");
  724. free_cpumask_var(cm);
  725. }
  726. #endif // #ifndef CONFIG_TINY_RCU
  727. static void exit_tasks_rcu_finish_trace(struct task_struct *t);
  728. #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
  729. ////////////////////////////////////////////////////////////////////////
  730. //
  731. // Shared code between task-list-scanning variants of Tasks RCU.
  732. /* Wait for one RCU-tasks grace period. */
  733. static void rcu_tasks_wait_gp(struct rcu_tasks *rtp)
  734. {
  735. struct task_struct *g;
  736. int fract;
  737. LIST_HEAD(holdouts);
  738. unsigned long j;
  739. unsigned long lastinfo;
  740. unsigned long lastreport;
  741. bool reported = false;
  742. int rtsi;
  743. struct task_struct *t;
  744. set_tasks_gp_state(rtp, RTGS_PRE_WAIT_GP);
  745. rtp->pregp_func(&holdouts);
  746. /*
  747. * There were callbacks, so we need to wait for an RCU-tasks
  748. * grace period. Start off by scanning the task list for tasks
  749. * that are not already voluntarily blocked. Mark these tasks
  750. * and make a list of them in holdouts.
  751. */
  752. set_tasks_gp_state(rtp, RTGS_SCAN_TASKLIST);
  753. if (rtp->pertask_func) {
  754. rcu_read_lock();
  755. for_each_process_thread(g, t)
  756. rtp->pertask_func(t, &holdouts);
  757. rcu_read_unlock();
  758. }
  759. set_tasks_gp_state(rtp, RTGS_POST_SCAN_TASKLIST);
  760. rtp->postscan_func(&holdouts);
  761. /*
  762. * Each pass through the following loop scans the list of holdout
  763. * tasks, removing any that are no longer holdouts. When the list
  764. * is empty, we are done.
  765. */
  766. lastreport = jiffies;
  767. lastinfo = lastreport;
  768. rtsi = READ_ONCE(rcu_task_stall_info);
  769. // Start off with initial wait and slowly back off to 1 HZ wait.
  770. fract = rtp->init_fract;
  771. while (!list_empty(&holdouts)) {
  772. ktime_t exp;
  773. bool firstreport;
  774. bool needreport;
  775. int rtst;
  776. // Slowly back off waiting for holdouts
  777. set_tasks_gp_state(rtp, RTGS_WAIT_SCAN_HOLDOUTS);
  778. if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
  779. schedule_timeout_idle(fract);
  780. } else {
  781. exp = jiffies_to_nsecs(fract);
  782. __set_current_state(TASK_IDLE);
  783. schedule_hrtimeout_range(&exp, jiffies_to_nsecs(HZ / 2), HRTIMER_MODE_REL_HARD);
  784. }
  785. if (fract < HZ)
  786. fract++;
  787. rtst = READ_ONCE(rcu_task_stall_timeout);
  788. needreport = rtst > 0 && time_after(jiffies, lastreport + rtst);
  789. if (needreport) {
  790. lastreport = jiffies;
  791. reported = true;
  792. }
  793. firstreport = true;
  794. WARN_ON(signal_pending(current));
  795. set_tasks_gp_state(rtp, RTGS_SCAN_HOLDOUTS);
  796. rtp->holdouts_func(&holdouts, needreport, &firstreport);
  797. // Print pre-stall informational messages if needed.
  798. j = jiffies;
  799. if (rtsi > 0 && !reported && time_after(j, lastinfo + rtsi)) {
  800. lastinfo = j;
  801. rtsi = rtsi * rcu_task_stall_info_mult;
  802. pr_info("%s: %s grace period number %lu (since boot) is %lu jiffies old.\n",
  803. __func__, rtp->kname, rtp->tasks_gp_seq, j - rtp->gp_start);
  804. }
  805. }
  806. set_tasks_gp_state(rtp, RTGS_POST_GP);
  807. rtp->postgp_func(rtp);
  808. }
  809. #endif /* #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU) */
  810. #ifdef CONFIG_TASKS_RCU
  811. ////////////////////////////////////////////////////////////////////////
  812. //
  813. // Simple variant of RCU whose quiescent states are voluntary context
  814. // switch, cond_resched_tasks_rcu_qs(), user-space execution, and idle.
  815. // As such, grace periods can take one good long time. There are no
  816. // read-side primitives similar to rcu_read_lock() and rcu_read_unlock()
  817. // because this implementation is intended to get the system into a safe
  818. // state for some of the manipulations involved in tracing and the like.
  819. // Finally, this implementation does not support high call_rcu_tasks()
  820. // rates from multiple CPUs. If this is required, per-CPU callback lists
  821. // will be needed.
  822. //
  823. // The implementation uses rcu_tasks_wait_gp(), which relies on function
  824. // pointers in the rcu_tasks structure. The rcu_spawn_tasks_kthread()
  825. // function sets these function pointers up so that rcu_tasks_wait_gp()
  826. // invokes these functions in this order:
  827. //
  828. // rcu_tasks_pregp_step():
  829. // Invokes synchronize_rcu() in order to wait for all in-flight
  830. // t->on_rq and t->nvcsw transitions to complete. This works because
  831. // all such transitions are carried out with interrupts disabled.
  832. // rcu_tasks_pertask(), invoked on every non-idle task:
  833. // For every runnable non-idle task other than the current one, use
  834. // get_task_struct() to pin down that task, snapshot that task's
  835. // number of voluntary context switches, and add that task to the
  836. // holdout list.
  837. // rcu_tasks_postscan():
  838. // Gather per-CPU lists of tasks in do_exit() to ensure that all
  839. // tasks that were in the process of exiting (and which thus might
  840. // not know to synchronize with this RCU Tasks grace period) have
  841. // completed exiting. The synchronize_rcu() in rcu_tasks_postgp()
  842. // will take care of any tasks stuck in the non-preemptible region
  843. // of do_exit() following its call to exit_tasks_rcu_finish().
  844. // check_all_holdout_tasks(), repeatedly until holdout list is empty:
  845. // Scans the holdout list, attempting to identify a quiescent state
  846. // for each task on the list. If there is a quiescent state, the
  847. // corresponding task is removed from the holdout list.
  848. // rcu_tasks_postgp():
  849. // Invokes synchronize_rcu() in order to ensure that all prior
  850. // t->on_rq and t->nvcsw transitions are seen by all CPUs and tasks
  851. // to have happened before the end of this RCU Tasks grace period.
  852. // Again, this works because all such transitions are carried out
  853. // with interrupts disabled.
  854. //
  855. // For each exiting task, the exit_tasks_rcu_start() and
  856. // exit_tasks_rcu_finish() functions add and remove, respectively, the
  857. // current task to a per-CPU list of tasks that rcu_tasks_postscan() must
  858. // wait on. This is necessary because rcu_tasks_postscan() must wait on
  859. // tasks that have already been removed from the global list of tasks.
  860. //
  861. // Pre-grace-period update-side code is ordered before the grace
  862. // via the raw_spin_lock.*rcu_node(). Pre-grace-period read-side code
  863. // is ordered before the grace period via synchronize_rcu() call in
  864. // rcu_tasks_pregp_step() and by the scheduler's locks and interrupt
  865. // disabling.
  866. /* Pre-grace-period preparation. */
  867. static void rcu_tasks_pregp_step(struct list_head *hop)
  868. {
  869. /*
  870. * Wait for all pre-existing t->on_rq and t->nvcsw transitions
  871. * to complete. Invoking synchronize_rcu() suffices because all
  872. * these transitions occur with interrupts disabled. Without this
  873. * synchronize_rcu(), a read-side critical section that started
  874. * before the grace period might be incorrectly seen as having
  875. * started after the grace period.
  876. *
  877. * This synchronize_rcu() also dispenses with the need for a
  878. * memory barrier on the first store to t->rcu_tasks_holdout,
  879. * as it forces the store to happen after the beginning of the
  880. * grace period.
  881. */
  882. synchronize_rcu();
  883. }
  884. /* Check for quiescent states since the pregp's synchronize_rcu() */
  885. static bool rcu_tasks_is_holdout(struct task_struct *t)
  886. {
  887. int cpu;
  888. /* Has the task been seen voluntarily sleeping? */
  889. if (!READ_ONCE(t->on_rq))
  890. return false;
  891. /*
  892. * t->on_rq && !t->se.sched_delayed *could* be considered sleeping but
  893. * since it is a spurious state (it will transition into the
  894. * traditional blocked state or get woken up without outside
  895. * dependencies), not considering it such should only affect timing.
  896. *
  897. * Be conservative for now and not include it.
  898. */
  899. /*
  900. * Idle tasks (or idle injection) within the idle loop are RCU-tasks
  901. * quiescent states. But CPU boot code performed by the idle task
  902. * isn't a quiescent state.
  903. */
  904. if (is_idle_task(t))
  905. return false;
  906. cpu = task_cpu(t);
  907. /* Idle tasks on offline CPUs are RCU-tasks quiescent states. */
  908. if (t == idle_task(cpu) && !rcu_cpu_online(cpu))
  909. return false;
  910. return true;
  911. }
  912. /* Per-task initial processing. */
  913. static void rcu_tasks_pertask(struct task_struct *t, struct list_head *hop)
  914. {
  915. if (t != current && rcu_tasks_is_holdout(t)) {
  916. get_task_struct(t);
  917. t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
  918. WRITE_ONCE(t->rcu_tasks_holdout, true);
  919. list_add(&t->rcu_tasks_holdout_list, hop);
  920. }
  921. }
  922. void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func);
  923. DEFINE_RCU_TASKS(rcu_tasks, rcu_tasks_wait_gp, call_rcu_tasks, "RCU Tasks");
  924. /* Processing between scanning taskslist and draining the holdout list. */
  925. static void rcu_tasks_postscan(struct list_head *hop)
  926. {
  927. int cpu;
  928. int rtsi = READ_ONCE(rcu_task_stall_info);
  929. if (!IS_ENABLED(CONFIG_TINY_RCU)) {
  930. tasks_rcu_exit_srcu_stall_timer.expires = jiffies + rtsi;
  931. add_timer(&tasks_rcu_exit_srcu_stall_timer);
  932. }
  933. /*
  934. * Exiting tasks may escape the tasklist scan. Those are vulnerable
  935. * until their final schedule() with TASK_DEAD state. To cope with
  936. * this, divide the fragile exit path part in two intersecting
  937. * read side critical sections:
  938. *
  939. * 1) A task_struct list addition before calling exit_notify(),
  940. * which may remove the task from the tasklist, with the
  941. * removal after the final preempt_disable() call in do_exit().
  942. *
  943. * 2) An _RCU_ read side starting with the final preempt_disable()
  944. * call in do_exit() and ending with the final call to schedule()
  945. * with TASK_DEAD state.
  946. *
  947. * This handles the part 1). And postgp will handle part 2) with a
  948. * call to synchronize_rcu().
  949. */
  950. for_each_possible_cpu(cpu) {
  951. unsigned long j = jiffies + 1;
  952. struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu);
  953. struct task_struct *t;
  954. struct task_struct *t1;
  955. struct list_head tmp;
  956. raw_spin_lock_irq_rcu_node(rtpcp);
  957. list_for_each_entry_safe(t, t1, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) {
  958. if (list_empty(&t->rcu_tasks_holdout_list))
  959. rcu_tasks_pertask(t, hop);
  960. // RT kernels need frequent pauses, otherwise
  961. // pause at least once per pair of jiffies.
  962. if (!IS_ENABLED(CONFIG_PREEMPT_RT) && time_before(jiffies, j))
  963. continue;
  964. // Keep our place in the list while pausing.
  965. // Nothing else traverses this list, so adding a
  966. // bare list_head is OK.
  967. list_add(&tmp, &t->rcu_tasks_exit_list);
  968. raw_spin_unlock_irq_rcu_node(rtpcp);
  969. cond_resched(); // For CONFIG_PREEMPT=n kernels
  970. raw_spin_lock_irq_rcu_node(rtpcp);
  971. t1 = list_entry(tmp.next, struct task_struct, rcu_tasks_exit_list);
  972. list_del(&tmp);
  973. j = jiffies + 1;
  974. }
  975. raw_spin_unlock_irq_rcu_node(rtpcp);
  976. }
  977. if (!IS_ENABLED(CONFIG_TINY_RCU))
  978. del_timer_sync(&tasks_rcu_exit_srcu_stall_timer);
  979. }
  980. /* See if tasks are still holding out, complain if so. */
  981. static void check_holdout_task(struct task_struct *t,
  982. bool needreport, bool *firstreport)
  983. {
  984. int cpu;
  985. if (!READ_ONCE(t->rcu_tasks_holdout) ||
  986. t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
  987. !rcu_tasks_is_holdout(t) ||
  988. (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
  989. !is_idle_task(t) && READ_ONCE(t->rcu_tasks_idle_cpu) >= 0)) {
  990. WRITE_ONCE(t->rcu_tasks_holdout, false);
  991. list_del_init(&t->rcu_tasks_holdout_list);
  992. put_task_struct(t);
  993. return;
  994. }
  995. rcu_request_urgent_qs_task(t);
  996. if (!needreport)
  997. return;
  998. if (*firstreport) {
  999. pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
  1000. *firstreport = false;
  1001. }
  1002. cpu = task_cpu(t);
  1003. pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
  1004. t, ".I"[is_idle_task(t)],
  1005. "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
  1006. t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
  1007. data_race(t->rcu_tasks_idle_cpu), cpu);
  1008. sched_show_task(t);
  1009. }
  1010. /* Scan the holdout lists for tasks no longer holding out. */
  1011. static void check_all_holdout_tasks(struct list_head *hop,
  1012. bool needreport, bool *firstreport)
  1013. {
  1014. struct task_struct *t, *t1;
  1015. list_for_each_entry_safe(t, t1, hop, rcu_tasks_holdout_list) {
  1016. check_holdout_task(t, needreport, firstreport);
  1017. cond_resched();
  1018. }
  1019. }
  1020. /* Finish off the Tasks-RCU grace period. */
  1021. static void rcu_tasks_postgp(struct rcu_tasks *rtp)
  1022. {
  1023. /*
  1024. * Because ->on_rq and ->nvcsw are not guaranteed to have a full
  1025. * memory barriers prior to them in the schedule() path, memory
  1026. * reordering on other CPUs could cause their RCU-tasks read-side
  1027. * critical sections to extend past the end of the grace period.
  1028. * However, because these ->nvcsw updates are carried out with
  1029. * interrupts disabled, we can use synchronize_rcu() to force the
  1030. * needed ordering on all such CPUs.
  1031. *
  1032. * This synchronize_rcu() also confines all ->rcu_tasks_holdout
  1033. * accesses to be within the grace period, avoiding the need for
  1034. * memory barriers for ->rcu_tasks_holdout accesses.
  1035. *
  1036. * In addition, this synchronize_rcu() waits for exiting tasks
  1037. * to complete their final preempt_disable() region of execution,
  1038. * enforcing the whole region before tasklist removal until
  1039. * the final schedule() with TASK_DEAD state to be an RCU TASKS
  1040. * read side critical section.
  1041. */
  1042. synchronize_rcu();
  1043. }
  1044. static void tasks_rcu_exit_srcu_stall(struct timer_list *unused)
  1045. {
  1046. #ifndef CONFIG_TINY_RCU
  1047. int rtsi;
  1048. rtsi = READ_ONCE(rcu_task_stall_info);
  1049. pr_info("%s: %s grace period number %lu (since boot) gp_state: %s is %lu jiffies old.\n",
  1050. __func__, rcu_tasks.kname, rcu_tasks.tasks_gp_seq,
  1051. tasks_gp_state_getname(&rcu_tasks), jiffies - rcu_tasks.gp_jiffies);
  1052. pr_info("Please check any exiting tasks stuck between calls to exit_tasks_rcu_start() and exit_tasks_rcu_finish()\n");
  1053. tasks_rcu_exit_srcu_stall_timer.expires = jiffies + rtsi;
  1054. add_timer(&tasks_rcu_exit_srcu_stall_timer);
  1055. #endif // #ifndef CONFIG_TINY_RCU
  1056. }
  1057. /**
  1058. * call_rcu_tasks() - Queue an RCU for invocation task-based grace period
  1059. * @rhp: structure to be used for queueing the RCU updates.
  1060. * @func: actual callback function to be invoked after the grace period
  1061. *
  1062. * The callback function will be invoked some time after a full grace
  1063. * period elapses, in other words after all currently executing RCU
  1064. * read-side critical sections have completed. call_rcu_tasks() assumes
  1065. * that the read-side critical sections end at a voluntary context
  1066. * switch (not a preemption!), cond_resched_tasks_rcu_qs(), entry into idle,
  1067. * or transition to usermode execution. As such, there are no read-side
  1068. * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
  1069. * this primitive is intended to determine that all tasks have passed
  1070. * through a safe state, not so much for data-structure synchronization.
  1071. *
  1072. * See the description of call_rcu() for more detailed information on
  1073. * memory ordering guarantees.
  1074. */
  1075. void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
  1076. {
  1077. call_rcu_tasks_generic(rhp, func, &rcu_tasks);
  1078. }
  1079. EXPORT_SYMBOL_GPL(call_rcu_tasks);
  1080. /**
  1081. * synchronize_rcu_tasks - wait until an rcu-tasks grace period has elapsed.
  1082. *
  1083. * Control will return to the caller some time after a full rcu-tasks
  1084. * grace period has elapsed, in other words after all currently
  1085. * executing rcu-tasks read-side critical sections have elapsed. These
  1086. * read-side critical sections are delimited by calls to schedule(),
  1087. * cond_resched_tasks_rcu_qs(), idle execution, userspace execution, calls
  1088. * to synchronize_rcu_tasks(), and (in theory, anyway) cond_resched().
  1089. *
  1090. * This is a very specialized primitive, intended only for a few uses in
  1091. * tracing and other situations requiring manipulation of function
  1092. * preambles and profiling hooks. The synchronize_rcu_tasks() function
  1093. * is not (yet) intended for heavy use from multiple CPUs.
  1094. *
  1095. * See the description of synchronize_rcu() for more detailed information
  1096. * on memory ordering guarantees.
  1097. */
  1098. void synchronize_rcu_tasks(void)
  1099. {
  1100. synchronize_rcu_tasks_generic(&rcu_tasks);
  1101. }
  1102. EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
  1103. /**
  1104. * rcu_barrier_tasks - Wait for in-flight call_rcu_tasks() callbacks.
  1105. *
  1106. * Although the current implementation is guaranteed to wait, it is not
  1107. * obligated to, for example, if there are no pending callbacks.
  1108. */
  1109. void rcu_barrier_tasks(void)
  1110. {
  1111. rcu_barrier_tasks_generic(&rcu_tasks);
  1112. }
  1113. EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
  1114. static int rcu_tasks_lazy_ms = -1;
  1115. module_param(rcu_tasks_lazy_ms, int, 0444);
  1116. static int __init rcu_spawn_tasks_kthread(void)
  1117. {
  1118. rcu_tasks.gp_sleep = HZ / 10;
  1119. rcu_tasks.init_fract = HZ / 10;
  1120. if (rcu_tasks_lazy_ms >= 0)
  1121. rcu_tasks.lazy_jiffies = msecs_to_jiffies(rcu_tasks_lazy_ms);
  1122. rcu_tasks.pregp_func = rcu_tasks_pregp_step;
  1123. rcu_tasks.pertask_func = rcu_tasks_pertask;
  1124. rcu_tasks.postscan_func = rcu_tasks_postscan;
  1125. rcu_tasks.holdouts_func = check_all_holdout_tasks;
  1126. rcu_tasks.postgp_func = rcu_tasks_postgp;
  1127. rcu_tasks.wait_state = TASK_IDLE;
  1128. rcu_spawn_tasks_kthread_generic(&rcu_tasks);
  1129. return 0;
  1130. }
  1131. #if !defined(CONFIG_TINY_RCU)
  1132. void show_rcu_tasks_classic_gp_kthread(void)
  1133. {
  1134. show_rcu_tasks_generic_gp_kthread(&rcu_tasks, "");
  1135. }
  1136. EXPORT_SYMBOL_GPL(show_rcu_tasks_classic_gp_kthread);
  1137. void rcu_tasks_torture_stats_print(char *tt, char *tf)
  1138. {
  1139. rcu_tasks_torture_stats_print_generic(&rcu_tasks, tt, tf, "");
  1140. }
  1141. EXPORT_SYMBOL_GPL(rcu_tasks_torture_stats_print);
  1142. #endif // !defined(CONFIG_TINY_RCU)
  1143. struct task_struct *get_rcu_tasks_gp_kthread(void)
  1144. {
  1145. return rcu_tasks.kthread_ptr;
  1146. }
  1147. EXPORT_SYMBOL_GPL(get_rcu_tasks_gp_kthread);
  1148. void rcu_tasks_get_gp_data(int *flags, unsigned long *gp_seq)
  1149. {
  1150. *flags = 0;
  1151. *gp_seq = rcu_seq_current(&rcu_tasks.tasks_gp_seq);
  1152. }
  1153. EXPORT_SYMBOL_GPL(rcu_tasks_get_gp_data);
  1154. /*
  1155. * Protect against tasklist scan blind spot while the task is exiting and
  1156. * may be removed from the tasklist. Do this by adding the task to yet
  1157. * another list.
  1158. *
  1159. * Note that the task will remove itself from this list, so there is no
  1160. * need for get_task_struct(), except in the case where rcu_tasks_pertask()
  1161. * adds it to the holdout list, in which case rcu_tasks_pertask() supplies
  1162. * the needed get_task_struct().
  1163. */
  1164. void exit_tasks_rcu_start(void)
  1165. {
  1166. unsigned long flags;
  1167. struct rcu_tasks_percpu *rtpcp;
  1168. struct task_struct *t = current;
  1169. WARN_ON_ONCE(!list_empty(&t->rcu_tasks_exit_list));
  1170. preempt_disable();
  1171. rtpcp = this_cpu_ptr(rcu_tasks.rtpcpu);
  1172. t->rcu_tasks_exit_cpu = smp_processor_id();
  1173. raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
  1174. WARN_ON_ONCE(!rtpcp->rtp_exit_list.next);
  1175. list_add(&t->rcu_tasks_exit_list, &rtpcp->rtp_exit_list);
  1176. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  1177. preempt_enable();
  1178. }
  1179. /*
  1180. * Remove the task from the "yet another list" because do_exit() is now
  1181. * non-preemptible, allowing synchronize_rcu() to wait beyond this point.
  1182. */
  1183. void exit_tasks_rcu_finish(void)
  1184. {
  1185. unsigned long flags;
  1186. struct rcu_tasks_percpu *rtpcp;
  1187. struct task_struct *t = current;
  1188. WARN_ON_ONCE(list_empty(&t->rcu_tasks_exit_list));
  1189. rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, t->rcu_tasks_exit_cpu);
  1190. raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
  1191. list_del_init(&t->rcu_tasks_exit_list);
  1192. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  1193. exit_tasks_rcu_finish_trace(t);
  1194. }
  1195. #else /* #ifdef CONFIG_TASKS_RCU */
  1196. void exit_tasks_rcu_start(void) { }
  1197. void exit_tasks_rcu_finish(void) { exit_tasks_rcu_finish_trace(current); }
  1198. #endif /* #else #ifdef CONFIG_TASKS_RCU */
  1199. #ifdef CONFIG_TASKS_RUDE_RCU
  1200. ////////////////////////////////////////////////////////////////////////
  1201. //
  1202. // "Rude" variant of Tasks RCU, inspired by Steve Rostedt's
  1203. // trick of passing an empty function to schedule_on_each_cpu().
  1204. // This approach provides batching of concurrent calls to the synchronous
  1205. // synchronize_rcu_tasks_rude() API. This invokes schedule_on_each_cpu()
  1206. // in order to send IPIs far and wide and induces otherwise unnecessary
  1207. // context switches on all online CPUs, whether idle or not.
  1208. //
  1209. // Callback handling is provided by the rcu_tasks_kthread() function.
  1210. //
  1211. // Ordering is provided by the scheduler's context-switch code.
  1212. // Empty function to allow workqueues to force a context switch.
  1213. static void rcu_tasks_be_rude(struct work_struct *work)
  1214. {
  1215. }
  1216. // Wait for one rude RCU-tasks grace period.
  1217. static void rcu_tasks_rude_wait_gp(struct rcu_tasks *rtp)
  1218. {
  1219. rtp->n_ipis += cpumask_weight(cpu_online_mask);
  1220. schedule_on_each_cpu(rcu_tasks_be_rude);
  1221. }
  1222. static void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func);
  1223. DEFINE_RCU_TASKS(rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude,
  1224. "RCU Tasks Rude");
  1225. /*
  1226. * call_rcu_tasks_rude() - Queue a callback rude task-based grace period
  1227. * @rhp: structure to be used for queueing the RCU updates.
  1228. * @func: actual callback function to be invoked after the grace period
  1229. *
  1230. * The callback function will be invoked some time after a full grace
  1231. * period elapses, in other words after all currently executing RCU
  1232. * read-side critical sections have completed. call_rcu_tasks_rude()
  1233. * assumes that the read-side critical sections end at context switch,
  1234. * cond_resched_tasks_rcu_qs(), or transition to usermode execution (as
  1235. * usermode execution is schedulable). As such, there are no read-side
  1236. * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
  1237. * this primitive is intended to determine that all tasks have passed
  1238. * through a safe state, not so much for data-structure synchronization.
  1239. *
  1240. * See the description of call_rcu() for more detailed information on
  1241. * memory ordering guarantees.
  1242. *
  1243. * This is no longer exported, and is instead reserved for use by
  1244. * synchronize_rcu_tasks_rude().
  1245. */
  1246. static void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func)
  1247. {
  1248. call_rcu_tasks_generic(rhp, func, &rcu_tasks_rude);
  1249. }
  1250. /**
  1251. * synchronize_rcu_tasks_rude - wait for a rude rcu-tasks grace period
  1252. *
  1253. * Control will return to the caller some time after a rude rcu-tasks
  1254. * grace period has elapsed, in other words after all currently
  1255. * executing rcu-tasks read-side critical sections have elapsed. These
  1256. * read-side critical sections are delimited by calls to schedule(),
  1257. * cond_resched_tasks_rcu_qs(), userspace execution (which is a schedulable
  1258. * context), and (in theory, anyway) cond_resched().
  1259. *
  1260. * This is a very specialized primitive, intended only for a few uses in
  1261. * tracing and other situations requiring manipulation of function preambles
  1262. * and profiling hooks. The synchronize_rcu_tasks_rude() function is not
  1263. * (yet) intended for heavy use from multiple CPUs.
  1264. *
  1265. * See the description of synchronize_rcu() for more detailed information
  1266. * on memory ordering guarantees.
  1267. */
  1268. void synchronize_rcu_tasks_rude(void)
  1269. {
  1270. synchronize_rcu_tasks_generic(&rcu_tasks_rude);
  1271. }
  1272. EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_rude);
  1273. static int __init rcu_spawn_tasks_rude_kthread(void)
  1274. {
  1275. rcu_tasks_rude.gp_sleep = HZ / 10;
  1276. rcu_spawn_tasks_kthread_generic(&rcu_tasks_rude);
  1277. return 0;
  1278. }
  1279. #if !defined(CONFIG_TINY_RCU)
  1280. void show_rcu_tasks_rude_gp_kthread(void)
  1281. {
  1282. show_rcu_tasks_generic_gp_kthread(&rcu_tasks_rude, "");
  1283. }
  1284. EXPORT_SYMBOL_GPL(show_rcu_tasks_rude_gp_kthread);
  1285. void rcu_tasks_rude_torture_stats_print(char *tt, char *tf)
  1286. {
  1287. rcu_tasks_torture_stats_print_generic(&rcu_tasks_rude, tt, tf, "");
  1288. }
  1289. EXPORT_SYMBOL_GPL(rcu_tasks_rude_torture_stats_print);
  1290. #endif // !defined(CONFIG_TINY_RCU)
  1291. struct task_struct *get_rcu_tasks_rude_gp_kthread(void)
  1292. {
  1293. return rcu_tasks_rude.kthread_ptr;
  1294. }
  1295. EXPORT_SYMBOL_GPL(get_rcu_tasks_rude_gp_kthread);
  1296. void rcu_tasks_rude_get_gp_data(int *flags, unsigned long *gp_seq)
  1297. {
  1298. *flags = 0;
  1299. *gp_seq = rcu_seq_current(&rcu_tasks_rude.tasks_gp_seq);
  1300. }
  1301. EXPORT_SYMBOL_GPL(rcu_tasks_rude_get_gp_data);
  1302. #endif /* #ifdef CONFIG_TASKS_RUDE_RCU */
  1303. ////////////////////////////////////////////////////////////////////////
  1304. //
  1305. // Tracing variant of Tasks RCU. This variant is designed to be used
  1306. // to protect tracing hooks, including those of BPF. This variant
  1307. // therefore:
  1308. //
  1309. // 1. Has explicit read-side markers to allow finite grace periods
  1310. // in the face of in-kernel loops for PREEMPT=n builds.
  1311. //
  1312. // 2. Protects code in the idle loop, exception entry/exit, and
  1313. // CPU-hotplug code paths, similar to the capabilities of SRCU.
  1314. //
  1315. // 3. Avoids expensive read-side instructions, having overhead similar
  1316. // to that of Preemptible RCU.
  1317. //
  1318. // There are of course downsides. For example, the grace-period code
  1319. // can send IPIs to CPUs, even when those CPUs are in the idle loop or
  1320. // in nohz_full userspace. If needed, these downsides can be at least
  1321. // partially remedied.
  1322. //
  1323. // Perhaps most important, this variant of RCU does not affect the vanilla
  1324. // flavors, rcu_preempt and rcu_sched. The fact that RCU Tasks Trace
  1325. // readers can operate from idle, offline, and exception entry/exit in no
  1326. // way allows rcu_preempt and rcu_sched readers to also do so.
  1327. //
  1328. // The implementation uses rcu_tasks_wait_gp(), which relies on function
  1329. // pointers in the rcu_tasks structure. The rcu_spawn_tasks_trace_kthread()
  1330. // function sets these function pointers up so that rcu_tasks_wait_gp()
  1331. // invokes these functions in this order:
  1332. //
  1333. // rcu_tasks_trace_pregp_step():
  1334. // Disables CPU hotplug, adds all currently executing tasks to the
  1335. // holdout list, then checks the state of all tasks that blocked
  1336. // or were preempted within their current RCU Tasks Trace read-side
  1337. // critical section, adding them to the holdout list if appropriate.
  1338. // Finally, this function re-enables CPU hotplug.
  1339. // The ->pertask_func() pointer is NULL, so there is no per-task processing.
  1340. // rcu_tasks_trace_postscan():
  1341. // Invokes synchronize_rcu() to wait for late-stage exiting tasks
  1342. // to finish exiting.
  1343. // check_all_holdout_tasks_trace(), repeatedly until holdout list is empty:
  1344. // Scans the holdout list, attempting to identify a quiescent state
  1345. // for each task on the list. If there is a quiescent state, the
  1346. // corresponding task is removed from the holdout list. Once this
  1347. // list is empty, the grace period has completed.
  1348. // rcu_tasks_trace_postgp():
  1349. // Provides the needed full memory barrier and does debug checks.
  1350. //
  1351. // The exit_tasks_rcu_finish_trace() synchronizes with exiting tasks.
  1352. //
  1353. // Pre-grace-period update-side code is ordered before the grace period
  1354. // via the ->cbs_lock and barriers in rcu_tasks_kthread(). Pre-grace-period
  1355. // read-side code is ordered before the grace period by atomic operations
  1356. // on .b.need_qs flag of each task involved in this process, or by scheduler
  1357. // context-switch ordering (for locked-down non-running readers).
  1358. // The lockdep state must be outside of #ifdef to be useful.
  1359. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1360. static struct lock_class_key rcu_lock_trace_key;
  1361. struct lockdep_map rcu_trace_lock_map =
  1362. STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_trace", &rcu_lock_trace_key);
  1363. EXPORT_SYMBOL_GPL(rcu_trace_lock_map);
  1364. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  1365. #ifdef CONFIG_TASKS_TRACE_RCU
  1366. // Record outstanding IPIs to each CPU. No point in sending two...
  1367. static DEFINE_PER_CPU(bool, trc_ipi_to_cpu);
  1368. // The number of detections of task quiescent state relying on
  1369. // heavyweight readers executing explicit memory barriers.
  1370. static unsigned long n_heavy_reader_attempts;
  1371. static unsigned long n_heavy_reader_updates;
  1372. static unsigned long n_heavy_reader_ofl_updates;
  1373. static unsigned long n_trc_holdouts;
  1374. void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func);
  1375. DEFINE_RCU_TASKS(rcu_tasks_trace, rcu_tasks_wait_gp, call_rcu_tasks_trace,
  1376. "RCU Tasks Trace");
  1377. /* Load from ->trc_reader_special.b.need_qs with proper ordering. */
  1378. static u8 rcu_ld_need_qs(struct task_struct *t)
  1379. {
  1380. smp_mb(); // Enforce full grace-period ordering.
  1381. return smp_load_acquire(&t->trc_reader_special.b.need_qs);
  1382. }
  1383. /* Store to ->trc_reader_special.b.need_qs with proper ordering. */
  1384. static void rcu_st_need_qs(struct task_struct *t, u8 v)
  1385. {
  1386. smp_store_release(&t->trc_reader_special.b.need_qs, v);
  1387. smp_mb(); // Enforce full grace-period ordering.
  1388. }
  1389. /*
  1390. * Do a cmpxchg() on ->trc_reader_special.b.need_qs, allowing for
  1391. * the four-byte operand-size restriction of some platforms.
  1392. *
  1393. * Returns the old value, which is often ignored.
  1394. */
  1395. u8 rcu_trc_cmpxchg_need_qs(struct task_struct *t, u8 old, u8 new)
  1396. {
  1397. union rcu_special ret;
  1398. union rcu_special trs_old = READ_ONCE(t->trc_reader_special);
  1399. union rcu_special trs_new = trs_old;
  1400. if (trs_old.b.need_qs != old)
  1401. return trs_old.b.need_qs;
  1402. trs_new.b.need_qs = new;
  1403. // Although cmpxchg() appears to KCSAN to update all four bytes,
  1404. // only the .b.need_qs byte actually changes.
  1405. instrument_atomic_read_write(&t->trc_reader_special.b.need_qs,
  1406. sizeof(t->trc_reader_special.b.need_qs));
  1407. // Avoid false-positive KCSAN failures.
  1408. ret.s = data_race(cmpxchg(&t->trc_reader_special.s, trs_old.s, trs_new.s));
  1409. return ret.b.need_qs;
  1410. }
  1411. EXPORT_SYMBOL_GPL(rcu_trc_cmpxchg_need_qs);
  1412. /*
  1413. * If we are the last reader, signal the grace-period kthread.
  1414. * Also remove from the per-CPU list of blocked tasks.
  1415. */
  1416. void rcu_read_unlock_trace_special(struct task_struct *t)
  1417. {
  1418. unsigned long flags;
  1419. struct rcu_tasks_percpu *rtpcp;
  1420. union rcu_special trs;
  1421. // Open-coded full-word version of rcu_ld_need_qs().
  1422. smp_mb(); // Enforce full grace-period ordering.
  1423. trs = smp_load_acquire(&t->trc_reader_special);
  1424. if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) && t->trc_reader_special.b.need_mb)
  1425. smp_mb(); // Pairs with update-side barriers.
  1426. // Update .need_qs before ->trc_reader_nesting for irq/NMI handlers.
  1427. if (trs.b.need_qs == (TRC_NEED_QS_CHECKED | TRC_NEED_QS)) {
  1428. u8 result = rcu_trc_cmpxchg_need_qs(t, TRC_NEED_QS_CHECKED | TRC_NEED_QS,
  1429. TRC_NEED_QS_CHECKED);
  1430. WARN_ONCE(result != trs.b.need_qs, "%s: result = %d", __func__, result);
  1431. }
  1432. if (trs.b.blocked) {
  1433. rtpcp = per_cpu_ptr(rcu_tasks_trace.rtpcpu, t->trc_blkd_cpu);
  1434. raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
  1435. list_del_init(&t->trc_blkd_node);
  1436. WRITE_ONCE(t->trc_reader_special.b.blocked, false);
  1437. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  1438. }
  1439. WRITE_ONCE(t->trc_reader_nesting, 0);
  1440. }
  1441. EXPORT_SYMBOL_GPL(rcu_read_unlock_trace_special);
  1442. /* Add a newly blocked reader task to its CPU's list. */
  1443. void rcu_tasks_trace_qs_blkd(struct task_struct *t)
  1444. {
  1445. unsigned long flags;
  1446. struct rcu_tasks_percpu *rtpcp;
  1447. local_irq_save(flags);
  1448. rtpcp = this_cpu_ptr(rcu_tasks_trace.rtpcpu);
  1449. raw_spin_lock_rcu_node(rtpcp); // irqs already disabled
  1450. t->trc_blkd_cpu = smp_processor_id();
  1451. if (!rtpcp->rtp_blkd_tasks.next)
  1452. INIT_LIST_HEAD(&rtpcp->rtp_blkd_tasks);
  1453. list_add(&t->trc_blkd_node, &rtpcp->rtp_blkd_tasks);
  1454. WRITE_ONCE(t->trc_reader_special.b.blocked, true);
  1455. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  1456. }
  1457. EXPORT_SYMBOL_GPL(rcu_tasks_trace_qs_blkd);
  1458. /* Add a task to the holdout list, if it is not already on the list. */
  1459. static void trc_add_holdout(struct task_struct *t, struct list_head *bhp)
  1460. {
  1461. if (list_empty(&t->trc_holdout_list)) {
  1462. get_task_struct(t);
  1463. list_add(&t->trc_holdout_list, bhp);
  1464. n_trc_holdouts++;
  1465. }
  1466. }
  1467. /* Remove a task from the holdout list, if it is in fact present. */
  1468. static void trc_del_holdout(struct task_struct *t)
  1469. {
  1470. if (!list_empty(&t->trc_holdout_list)) {
  1471. list_del_init(&t->trc_holdout_list);
  1472. put_task_struct(t);
  1473. n_trc_holdouts--;
  1474. }
  1475. }
  1476. /* IPI handler to check task state. */
  1477. static void trc_read_check_handler(void *t_in)
  1478. {
  1479. int nesting;
  1480. struct task_struct *t = current;
  1481. struct task_struct *texp = t_in;
  1482. // If the task is no longer running on this CPU, leave.
  1483. if (unlikely(texp != t))
  1484. goto reset_ipi; // Already on holdout list, so will check later.
  1485. // If the task is not in a read-side critical section, and
  1486. // if this is the last reader, awaken the grace-period kthread.
  1487. nesting = READ_ONCE(t->trc_reader_nesting);
  1488. if (likely(!nesting)) {
  1489. rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
  1490. goto reset_ipi;
  1491. }
  1492. // If we are racing with an rcu_read_unlock_trace(), try again later.
  1493. if (unlikely(nesting < 0))
  1494. goto reset_ipi;
  1495. // Get here if the task is in a read-side critical section.
  1496. // Set its state so that it will update state for the grace-period
  1497. // kthread upon exit from that critical section.
  1498. rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS | TRC_NEED_QS_CHECKED);
  1499. reset_ipi:
  1500. // Allow future IPIs to be sent on CPU and for task.
  1501. // Also order this IPI handler against any later manipulations of
  1502. // the intended task.
  1503. smp_store_release(per_cpu_ptr(&trc_ipi_to_cpu, smp_processor_id()), false); // ^^^
  1504. smp_store_release(&texp->trc_ipi_to_cpu, -1); // ^^^
  1505. }
  1506. /* Callback function for scheduler to check locked-down task. */
  1507. static int trc_inspect_reader(struct task_struct *t, void *bhp_in)
  1508. {
  1509. struct list_head *bhp = bhp_in;
  1510. int cpu = task_cpu(t);
  1511. int nesting;
  1512. bool ofl = cpu_is_offline(cpu);
  1513. if (task_curr(t) && !ofl) {
  1514. // If no chance of heavyweight readers, do it the hard way.
  1515. if (!IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB))
  1516. return -EINVAL;
  1517. // If heavyweight readers are enabled on the remote task,
  1518. // we can inspect its state despite its currently running.
  1519. // However, we cannot safely change its state.
  1520. n_heavy_reader_attempts++;
  1521. // Check for "running" idle tasks on offline CPUs.
  1522. if (!rcu_watching_zero_in_eqs(cpu, &t->trc_reader_nesting))
  1523. return -EINVAL; // No quiescent state, do it the hard way.
  1524. n_heavy_reader_updates++;
  1525. nesting = 0;
  1526. } else {
  1527. // The task is not running, so C-language access is safe.
  1528. nesting = t->trc_reader_nesting;
  1529. WARN_ON_ONCE(ofl && task_curr(t) && (t != idle_task(task_cpu(t))));
  1530. if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) && ofl)
  1531. n_heavy_reader_ofl_updates++;
  1532. }
  1533. // If not exiting a read-side critical section, mark as checked
  1534. // so that the grace-period kthread will remove it from the
  1535. // holdout list.
  1536. if (!nesting) {
  1537. rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
  1538. return 0; // In QS, so done.
  1539. }
  1540. if (nesting < 0)
  1541. return -EINVAL; // Reader transitioning, try again later.
  1542. // The task is in a read-side critical section, so set up its
  1543. // state so that it will update state upon exit from that critical
  1544. // section.
  1545. if (!rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS | TRC_NEED_QS_CHECKED))
  1546. trc_add_holdout(t, bhp);
  1547. return 0;
  1548. }
  1549. /* Attempt to extract the state for the specified task. */
  1550. static void trc_wait_for_one_reader(struct task_struct *t,
  1551. struct list_head *bhp)
  1552. {
  1553. int cpu;
  1554. // If a previous IPI is still in flight, let it complete.
  1555. if (smp_load_acquire(&t->trc_ipi_to_cpu) != -1) // Order IPI
  1556. return;
  1557. // The current task had better be in a quiescent state.
  1558. if (t == current) {
  1559. rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
  1560. WARN_ON_ONCE(READ_ONCE(t->trc_reader_nesting));
  1561. return;
  1562. }
  1563. // Attempt to nail down the task for inspection.
  1564. get_task_struct(t);
  1565. if (!task_call_func(t, trc_inspect_reader, bhp)) {
  1566. put_task_struct(t);
  1567. return;
  1568. }
  1569. put_task_struct(t);
  1570. // If this task is not yet on the holdout list, then we are in
  1571. // an RCU read-side critical section. Otherwise, the invocation of
  1572. // trc_add_holdout() that added it to the list did the necessary
  1573. // get_task_struct(). Either way, the task cannot be freed out
  1574. // from under this code.
  1575. // If currently running, send an IPI, either way, add to list.
  1576. trc_add_holdout(t, bhp);
  1577. if (task_curr(t) &&
  1578. time_after(jiffies + 1, rcu_tasks_trace.gp_start + rcu_task_ipi_delay)) {
  1579. // The task is currently running, so try IPIing it.
  1580. cpu = task_cpu(t);
  1581. // If there is already an IPI outstanding, let it happen.
  1582. if (per_cpu(trc_ipi_to_cpu, cpu) || t->trc_ipi_to_cpu >= 0)
  1583. return;
  1584. per_cpu(trc_ipi_to_cpu, cpu) = true;
  1585. t->trc_ipi_to_cpu = cpu;
  1586. rcu_tasks_trace.n_ipis++;
  1587. if (smp_call_function_single(cpu, trc_read_check_handler, t, 0)) {
  1588. // Just in case there is some other reason for
  1589. // failure than the target CPU being offline.
  1590. WARN_ONCE(1, "%s(): smp_call_function_single() failed for CPU: %d\n",
  1591. __func__, cpu);
  1592. rcu_tasks_trace.n_ipis_fails++;
  1593. per_cpu(trc_ipi_to_cpu, cpu) = false;
  1594. t->trc_ipi_to_cpu = -1;
  1595. }
  1596. }
  1597. }
  1598. /*
  1599. * Initialize for first-round processing for the specified task.
  1600. * Return false if task is NULL or already taken care of, true otherwise.
  1601. */
  1602. static bool rcu_tasks_trace_pertask_prep(struct task_struct *t, bool notself)
  1603. {
  1604. // During early boot when there is only the one boot CPU, there
  1605. // is no idle task for the other CPUs. Also, the grace-period
  1606. // kthread is always in a quiescent state. In addition, just return
  1607. // if this task is already on the list.
  1608. if (unlikely(t == NULL) || (t == current && notself) || !list_empty(&t->trc_holdout_list))
  1609. return false;
  1610. rcu_st_need_qs(t, 0);
  1611. t->trc_ipi_to_cpu = -1;
  1612. return true;
  1613. }
  1614. /* Do first-round processing for the specified task. */
  1615. static void rcu_tasks_trace_pertask(struct task_struct *t, struct list_head *hop)
  1616. {
  1617. if (rcu_tasks_trace_pertask_prep(t, true))
  1618. trc_wait_for_one_reader(t, hop);
  1619. }
  1620. /* Initialize for a new RCU-tasks-trace grace period. */
  1621. static void rcu_tasks_trace_pregp_step(struct list_head *hop)
  1622. {
  1623. LIST_HEAD(blkd_tasks);
  1624. int cpu;
  1625. unsigned long flags;
  1626. struct rcu_tasks_percpu *rtpcp;
  1627. struct task_struct *t;
  1628. // There shouldn't be any old IPIs, but...
  1629. for_each_possible_cpu(cpu)
  1630. WARN_ON_ONCE(per_cpu(trc_ipi_to_cpu, cpu));
  1631. // Disable CPU hotplug across the CPU scan for the benefit of
  1632. // any IPIs that might be needed. This also waits for all readers
  1633. // in CPU-hotplug code paths.
  1634. cpus_read_lock();
  1635. // These rcu_tasks_trace_pertask_prep() calls are serialized to
  1636. // allow safe access to the hop list.
  1637. for_each_online_cpu(cpu) {
  1638. rcu_read_lock();
  1639. // Note that cpu_curr_snapshot() picks up the target
  1640. // CPU's current task while its runqueue is locked with
  1641. // an smp_mb__after_spinlock(). This ensures that either
  1642. // the grace-period kthread will see that task's read-side
  1643. // critical section or the task will see the updater's pre-GP
  1644. // accesses. The trailing smp_mb() in cpu_curr_snapshot()
  1645. // does not currently play a role other than simplify
  1646. // that function's ordering semantics. If these simplified
  1647. // ordering semantics continue to be redundant, that smp_mb()
  1648. // might be removed.
  1649. t = cpu_curr_snapshot(cpu);
  1650. if (rcu_tasks_trace_pertask_prep(t, true))
  1651. trc_add_holdout(t, hop);
  1652. rcu_read_unlock();
  1653. cond_resched_tasks_rcu_qs();
  1654. }
  1655. // Only after all running tasks have been accounted for is it
  1656. // safe to take care of the tasks that have blocked within their
  1657. // current RCU tasks trace read-side critical section.
  1658. for_each_possible_cpu(cpu) {
  1659. rtpcp = per_cpu_ptr(rcu_tasks_trace.rtpcpu, cpu);
  1660. raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
  1661. list_splice_init(&rtpcp->rtp_blkd_tasks, &blkd_tasks);
  1662. while (!list_empty(&blkd_tasks)) {
  1663. rcu_read_lock();
  1664. t = list_first_entry(&blkd_tasks, struct task_struct, trc_blkd_node);
  1665. list_del_init(&t->trc_blkd_node);
  1666. list_add(&t->trc_blkd_node, &rtpcp->rtp_blkd_tasks);
  1667. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  1668. rcu_tasks_trace_pertask(t, hop);
  1669. rcu_read_unlock();
  1670. raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
  1671. }
  1672. raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
  1673. cond_resched_tasks_rcu_qs();
  1674. }
  1675. // Re-enable CPU hotplug now that the holdout list is populated.
  1676. cpus_read_unlock();
  1677. }
  1678. /*
  1679. * Do intermediate processing between task and holdout scans.
  1680. */
  1681. static void rcu_tasks_trace_postscan(struct list_head *hop)
  1682. {
  1683. // Wait for late-stage exiting tasks to finish exiting.
  1684. // These might have passed the call to exit_tasks_rcu_finish().
  1685. // If you remove the following line, update rcu_trace_implies_rcu_gp()!!!
  1686. synchronize_rcu();
  1687. // Any tasks that exit after this point will set
  1688. // TRC_NEED_QS_CHECKED in ->trc_reader_special.b.need_qs.
  1689. }
  1690. /* Communicate task state back to the RCU tasks trace stall warning request. */
  1691. struct trc_stall_chk_rdr {
  1692. int nesting;
  1693. int ipi_to_cpu;
  1694. u8 needqs;
  1695. };
  1696. static int trc_check_slow_task(struct task_struct *t, void *arg)
  1697. {
  1698. struct trc_stall_chk_rdr *trc_rdrp = arg;
  1699. if (task_curr(t) && cpu_online(task_cpu(t)))
  1700. return false; // It is running, so decline to inspect it.
  1701. trc_rdrp->nesting = READ_ONCE(t->trc_reader_nesting);
  1702. trc_rdrp->ipi_to_cpu = READ_ONCE(t->trc_ipi_to_cpu);
  1703. trc_rdrp->needqs = rcu_ld_need_qs(t);
  1704. return true;
  1705. }
  1706. /* Show the state of a task stalling the current RCU tasks trace GP. */
  1707. static void show_stalled_task_trace(struct task_struct *t, bool *firstreport)
  1708. {
  1709. int cpu;
  1710. struct trc_stall_chk_rdr trc_rdr;
  1711. bool is_idle_tsk = is_idle_task(t);
  1712. if (*firstreport) {
  1713. pr_err("INFO: rcu_tasks_trace detected stalls on tasks:\n");
  1714. *firstreport = false;
  1715. }
  1716. cpu = task_cpu(t);
  1717. if (!task_call_func(t, trc_check_slow_task, &trc_rdr))
  1718. pr_alert("P%d: %c%c\n",
  1719. t->pid,
  1720. ".I"[t->trc_ipi_to_cpu >= 0],
  1721. ".i"[is_idle_tsk]);
  1722. else
  1723. pr_alert("P%d: %c%c%c%c nesting: %d%c%c cpu: %d%s\n",
  1724. t->pid,
  1725. ".I"[trc_rdr.ipi_to_cpu >= 0],
  1726. ".i"[is_idle_tsk],
  1727. ".N"[cpu >= 0 && tick_nohz_full_cpu(cpu)],
  1728. ".B"[!!data_race(t->trc_reader_special.b.blocked)],
  1729. trc_rdr.nesting,
  1730. " !CN"[trc_rdr.needqs & 0x3],
  1731. " ?"[trc_rdr.needqs > 0x3],
  1732. cpu, cpu_online(cpu) ? "" : "(offline)");
  1733. sched_show_task(t);
  1734. }
  1735. /* List stalled IPIs for RCU tasks trace. */
  1736. static void show_stalled_ipi_trace(void)
  1737. {
  1738. int cpu;
  1739. for_each_possible_cpu(cpu)
  1740. if (per_cpu(trc_ipi_to_cpu, cpu))
  1741. pr_alert("\tIPI outstanding to CPU %d\n", cpu);
  1742. }
  1743. /* Do one scan of the holdout list. */
  1744. static void check_all_holdout_tasks_trace(struct list_head *hop,
  1745. bool needreport, bool *firstreport)
  1746. {
  1747. struct task_struct *g, *t;
  1748. // Disable CPU hotplug across the holdout list scan for IPIs.
  1749. cpus_read_lock();
  1750. list_for_each_entry_safe(t, g, hop, trc_holdout_list) {
  1751. // If safe and needed, try to check the current task.
  1752. if (READ_ONCE(t->trc_ipi_to_cpu) == -1 &&
  1753. !(rcu_ld_need_qs(t) & TRC_NEED_QS_CHECKED))
  1754. trc_wait_for_one_reader(t, hop);
  1755. // If check succeeded, remove this task from the list.
  1756. if (smp_load_acquire(&t->trc_ipi_to_cpu) == -1 &&
  1757. rcu_ld_need_qs(t) == TRC_NEED_QS_CHECKED)
  1758. trc_del_holdout(t);
  1759. else if (needreport)
  1760. show_stalled_task_trace(t, firstreport);
  1761. cond_resched_tasks_rcu_qs();
  1762. }
  1763. // Re-enable CPU hotplug now that the holdout list scan has completed.
  1764. cpus_read_unlock();
  1765. if (needreport) {
  1766. if (*firstreport)
  1767. pr_err("INFO: rcu_tasks_trace detected stalls? (Late IPI?)\n");
  1768. show_stalled_ipi_trace();
  1769. }
  1770. }
  1771. static void rcu_tasks_trace_empty_fn(void *unused)
  1772. {
  1773. }
  1774. /* Wait for grace period to complete and provide ordering. */
  1775. static void rcu_tasks_trace_postgp(struct rcu_tasks *rtp)
  1776. {
  1777. int cpu;
  1778. // Wait for any lingering IPI handlers to complete. Note that
  1779. // if a CPU has gone offline or transitioned to userspace in the
  1780. // meantime, all IPI handlers should have been drained beforehand.
  1781. // Yes, this assumes that CPUs process IPIs in order. If that ever
  1782. // changes, there will need to be a recheck and/or timed wait.
  1783. for_each_online_cpu(cpu)
  1784. if (WARN_ON_ONCE(smp_load_acquire(per_cpu_ptr(&trc_ipi_to_cpu, cpu))))
  1785. smp_call_function_single(cpu, rcu_tasks_trace_empty_fn, NULL, 1);
  1786. smp_mb(); // Caller's code must be ordered after wakeup.
  1787. // Pairs with pretty much every ordering primitive.
  1788. }
  1789. /* Report any needed quiescent state for this exiting task. */
  1790. static void exit_tasks_rcu_finish_trace(struct task_struct *t)
  1791. {
  1792. union rcu_special trs = READ_ONCE(t->trc_reader_special);
  1793. rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
  1794. WARN_ON_ONCE(READ_ONCE(t->trc_reader_nesting));
  1795. if (WARN_ON_ONCE(rcu_ld_need_qs(t) & TRC_NEED_QS || trs.b.blocked))
  1796. rcu_read_unlock_trace_special(t);
  1797. else
  1798. WRITE_ONCE(t->trc_reader_nesting, 0);
  1799. }
  1800. /**
  1801. * call_rcu_tasks_trace() - Queue a callback trace task-based grace period
  1802. * @rhp: structure to be used for queueing the RCU updates.
  1803. * @func: actual callback function to be invoked after the grace period
  1804. *
  1805. * The callback function will be invoked some time after a trace rcu-tasks
  1806. * grace period elapses, in other words after all currently executing
  1807. * trace rcu-tasks read-side critical sections have completed. These
  1808. * read-side critical sections are delimited by calls to rcu_read_lock_trace()
  1809. * and rcu_read_unlock_trace().
  1810. *
  1811. * See the description of call_rcu() for more detailed information on
  1812. * memory ordering guarantees.
  1813. */
  1814. void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func)
  1815. {
  1816. call_rcu_tasks_generic(rhp, func, &rcu_tasks_trace);
  1817. }
  1818. EXPORT_SYMBOL_GPL(call_rcu_tasks_trace);
  1819. /**
  1820. * synchronize_rcu_tasks_trace - wait for a trace rcu-tasks grace period
  1821. *
  1822. * Control will return to the caller some time after a trace rcu-tasks
  1823. * grace period has elapsed, in other words after all currently executing
  1824. * trace rcu-tasks read-side critical sections have elapsed. These read-side
  1825. * critical sections are delimited by calls to rcu_read_lock_trace()
  1826. * and rcu_read_unlock_trace().
  1827. *
  1828. * This is a very specialized primitive, intended only for a few uses in
  1829. * tracing and other situations requiring manipulation of function preambles
  1830. * and profiling hooks. The synchronize_rcu_tasks_trace() function is not
  1831. * (yet) intended for heavy use from multiple CPUs.
  1832. *
  1833. * See the description of synchronize_rcu() for more detailed information
  1834. * on memory ordering guarantees.
  1835. */
  1836. void synchronize_rcu_tasks_trace(void)
  1837. {
  1838. RCU_LOCKDEP_WARN(lock_is_held(&rcu_trace_lock_map), "Illegal synchronize_rcu_tasks_trace() in RCU Tasks Trace read-side critical section");
  1839. synchronize_rcu_tasks_generic(&rcu_tasks_trace);
  1840. }
  1841. EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_trace);
  1842. /**
  1843. * rcu_barrier_tasks_trace - Wait for in-flight call_rcu_tasks_trace() callbacks.
  1844. *
  1845. * Although the current implementation is guaranteed to wait, it is not
  1846. * obligated to, for example, if there are no pending callbacks.
  1847. */
  1848. void rcu_barrier_tasks_trace(void)
  1849. {
  1850. rcu_barrier_tasks_generic(&rcu_tasks_trace);
  1851. }
  1852. EXPORT_SYMBOL_GPL(rcu_barrier_tasks_trace);
  1853. int rcu_tasks_trace_lazy_ms = -1;
  1854. module_param(rcu_tasks_trace_lazy_ms, int, 0444);
  1855. static int __init rcu_spawn_tasks_trace_kthread(void)
  1856. {
  1857. if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB)) {
  1858. rcu_tasks_trace.gp_sleep = HZ / 10;
  1859. rcu_tasks_trace.init_fract = HZ / 10;
  1860. } else {
  1861. rcu_tasks_trace.gp_sleep = HZ / 200;
  1862. if (rcu_tasks_trace.gp_sleep <= 0)
  1863. rcu_tasks_trace.gp_sleep = 1;
  1864. rcu_tasks_trace.init_fract = HZ / 200;
  1865. if (rcu_tasks_trace.init_fract <= 0)
  1866. rcu_tasks_trace.init_fract = 1;
  1867. }
  1868. if (rcu_tasks_trace_lazy_ms >= 0)
  1869. rcu_tasks_trace.lazy_jiffies = msecs_to_jiffies(rcu_tasks_trace_lazy_ms);
  1870. rcu_tasks_trace.pregp_func = rcu_tasks_trace_pregp_step;
  1871. rcu_tasks_trace.postscan_func = rcu_tasks_trace_postscan;
  1872. rcu_tasks_trace.holdouts_func = check_all_holdout_tasks_trace;
  1873. rcu_tasks_trace.postgp_func = rcu_tasks_trace_postgp;
  1874. rcu_spawn_tasks_kthread_generic(&rcu_tasks_trace);
  1875. return 0;
  1876. }
  1877. #if !defined(CONFIG_TINY_RCU)
  1878. void show_rcu_tasks_trace_gp_kthread(void)
  1879. {
  1880. char buf[64];
  1881. snprintf(buf, sizeof(buf), "N%lu h:%lu/%lu/%lu",
  1882. data_race(n_trc_holdouts),
  1883. data_race(n_heavy_reader_ofl_updates),
  1884. data_race(n_heavy_reader_updates),
  1885. data_race(n_heavy_reader_attempts));
  1886. show_rcu_tasks_generic_gp_kthread(&rcu_tasks_trace, buf);
  1887. }
  1888. EXPORT_SYMBOL_GPL(show_rcu_tasks_trace_gp_kthread);
  1889. void rcu_tasks_trace_torture_stats_print(char *tt, char *tf)
  1890. {
  1891. rcu_tasks_torture_stats_print_generic(&rcu_tasks_trace, tt, tf, "");
  1892. }
  1893. EXPORT_SYMBOL_GPL(rcu_tasks_trace_torture_stats_print);
  1894. #endif // !defined(CONFIG_TINY_RCU)
  1895. struct task_struct *get_rcu_tasks_trace_gp_kthread(void)
  1896. {
  1897. return rcu_tasks_trace.kthread_ptr;
  1898. }
  1899. EXPORT_SYMBOL_GPL(get_rcu_tasks_trace_gp_kthread);
  1900. void rcu_tasks_trace_get_gp_data(int *flags, unsigned long *gp_seq)
  1901. {
  1902. *flags = 0;
  1903. *gp_seq = rcu_seq_current(&rcu_tasks_trace.tasks_gp_seq);
  1904. }
  1905. EXPORT_SYMBOL_GPL(rcu_tasks_trace_get_gp_data);
  1906. #else /* #ifdef CONFIG_TASKS_TRACE_RCU */
  1907. static void exit_tasks_rcu_finish_trace(struct task_struct *t) { }
  1908. #endif /* #else #ifdef CONFIG_TASKS_TRACE_RCU */
  1909. #ifndef CONFIG_TINY_RCU
  1910. void show_rcu_tasks_gp_kthreads(void)
  1911. {
  1912. show_rcu_tasks_classic_gp_kthread();
  1913. show_rcu_tasks_rude_gp_kthread();
  1914. show_rcu_tasks_trace_gp_kthread();
  1915. }
  1916. #endif /* #ifndef CONFIG_TINY_RCU */
  1917. #ifdef CONFIG_PROVE_RCU
  1918. struct rcu_tasks_test_desc {
  1919. struct rcu_head rh;
  1920. const char *name;
  1921. bool notrun;
  1922. unsigned long runstart;
  1923. };
  1924. static struct rcu_tasks_test_desc tests[] = {
  1925. {
  1926. .name = "call_rcu_tasks()",
  1927. /* If not defined, the test is skipped. */
  1928. .notrun = IS_ENABLED(CONFIG_TASKS_RCU),
  1929. },
  1930. {
  1931. .name = "call_rcu_tasks_trace()",
  1932. /* If not defined, the test is skipped. */
  1933. .notrun = IS_ENABLED(CONFIG_TASKS_TRACE_RCU)
  1934. }
  1935. };
  1936. #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
  1937. static void test_rcu_tasks_callback(struct rcu_head *rhp)
  1938. {
  1939. struct rcu_tasks_test_desc *rttd =
  1940. container_of(rhp, struct rcu_tasks_test_desc, rh);
  1941. pr_info("Callback from %s invoked.\n", rttd->name);
  1942. rttd->notrun = false;
  1943. }
  1944. #endif // #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
  1945. static void rcu_tasks_initiate_self_tests(void)
  1946. {
  1947. #ifdef CONFIG_TASKS_RCU
  1948. pr_info("Running RCU Tasks wait API self tests\n");
  1949. tests[0].runstart = jiffies;
  1950. synchronize_rcu_tasks();
  1951. call_rcu_tasks(&tests[0].rh, test_rcu_tasks_callback);
  1952. #endif
  1953. #ifdef CONFIG_TASKS_RUDE_RCU
  1954. pr_info("Running RCU Tasks Rude wait API self tests\n");
  1955. synchronize_rcu_tasks_rude();
  1956. #endif
  1957. #ifdef CONFIG_TASKS_TRACE_RCU
  1958. pr_info("Running RCU Tasks Trace wait API self tests\n");
  1959. tests[1].runstart = jiffies;
  1960. synchronize_rcu_tasks_trace();
  1961. call_rcu_tasks_trace(&tests[1].rh, test_rcu_tasks_callback);
  1962. #endif
  1963. }
  1964. /*
  1965. * Return: 0 - test passed
  1966. * 1 - test failed, but have not timed out yet
  1967. * -1 - test failed and timed out
  1968. */
  1969. static int rcu_tasks_verify_self_tests(void)
  1970. {
  1971. int ret = 0;
  1972. int i;
  1973. unsigned long bst = rcu_task_stall_timeout;
  1974. if (bst <= 0 || bst > RCU_TASK_BOOT_STALL_TIMEOUT)
  1975. bst = RCU_TASK_BOOT_STALL_TIMEOUT;
  1976. for (i = 0; i < ARRAY_SIZE(tests); i++) {
  1977. while (tests[i].notrun) { // still hanging.
  1978. if (time_after(jiffies, tests[i].runstart + bst)) {
  1979. pr_err("%s has failed boot-time tests.\n", tests[i].name);
  1980. ret = -1;
  1981. break;
  1982. }
  1983. ret = 1;
  1984. break;
  1985. }
  1986. }
  1987. WARN_ON(ret < 0);
  1988. return ret;
  1989. }
  1990. /*
  1991. * Repeat the rcu_tasks_verify_self_tests() call once every second until the
  1992. * test passes or has timed out.
  1993. */
  1994. static struct delayed_work rcu_tasks_verify_work;
  1995. static void rcu_tasks_verify_work_fn(struct work_struct *work __maybe_unused)
  1996. {
  1997. int ret = rcu_tasks_verify_self_tests();
  1998. if (ret <= 0)
  1999. return;
  2000. /* Test fails but not timed out yet, reschedule another check */
  2001. schedule_delayed_work(&rcu_tasks_verify_work, HZ);
  2002. }
  2003. static int rcu_tasks_verify_schedule_work(void)
  2004. {
  2005. INIT_DELAYED_WORK(&rcu_tasks_verify_work, rcu_tasks_verify_work_fn);
  2006. rcu_tasks_verify_work_fn(NULL);
  2007. return 0;
  2008. }
  2009. late_initcall(rcu_tasks_verify_schedule_work);
  2010. #else /* #ifdef CONFIG_PROVE_RCU */
  2011. static void rcu_tasks_initiate_self_tests(void) { }
  2012. #endif /* #else #ifdef CONFIG_PROVE_RCU */
  2013. void __init tasks_cblist_init_generic(void)
  2014. {
  2015. lockdep_assert_irqs_disabled();
  2016. WARN_ON(num_online_cpus() > 1);
  2017. #ifdef CONFIG_TASKS_RCU
  2018. cblist_init_generic(&rcu_tasks);
  2019. #endif
  2020. #ifdef CONFIG_TASKS_RUDE_RCU
  2021. cblist_init_generic(&rcu_tasks_rude);
  2022. #endif
  2023. #ifdef CONFIG_TASKS_TRACE_RCU
  2024. cblist_init_generic(&rcu_tasks_trace);
  2025. #endif
  2026. }
  2027. void __init rcu_init_tasks_generic(void)
  2028. {
  2029. #ifdef CONFIG_TASKS_RCU
  2030. rcu_spawn_tasks_kthread();
  2031. #endif
  2032. #ifdef CONFIG_TASKS_RUDE_RCU
  2033. rcu_spawn_tasks_rude_kthread();
  2034. #endif
  2035. #ifdef CONFIG_TASKS_TRACE_RCU
  2036. rcu_spawn_tasks_trace_kthread();
  2037. #endif
  2038. // Run the self-tests.
  2039. rcu_tasks_initiate_self_tests();
  2040. }
  2041. #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
  2042. static inline void rcu_tasks_bootup_oddness(void) {}
  2043. #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */