sched.h 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. #ifndef _LINUX_SCHED_H
  2. #define _LINUX_SCHED_H
  3. #include <uapi/linux/sched.h>
  4. #include <linux/sched/prio.h>
  5. #include <linux/capability.h>
  6. #include <linux/mutex.h>
  7. #include <linux/plist.h>
  8. #include <linux/mm_types_task.h>
  9. #include <asm/ptrace.h>
  10. #include <linux/sem.h>
  11. #include <linux/shm.h>
  12. #include <linux/signal.h>
  13. #include <linux/signal_types.h>
  14. #include <linux/pid.h>
  15. #include <linux/seccomp.h>
  16. #include <linux/rculist.h>
  17. #include <linux/rtmutex.h>
  18. #include <linux/resource.h>
  19. #include <linux/hrtimer.h>
  20. #include <linux/kcov.h>
  21. #include <linux/task_io_accounting.h>
  22. #include <linux/latencytop.h>
  23. #include <linux/cred.h>
  24. #include <linux/gfp.h>
  25. #include <linux/topology.h>
  26. #include <linux/magic.h>
  27. #include <linux/cgroup-defs.h>
  28. #include <asm/current.h>
  29. /* task_struct member predeclarations: */
  30. struct audit_context;
  31. struct autogroup;
  32. struct backing_dev_info;
  33. struct bio_list;
  34. struct blk_plug;
  35. struct cfs_rq;
  36. struct filename;
  37. struct fs_struct;
  38. struct futex_pi_state;
  39. struct io_context;
  40. struct mempolicy;
  41. struct nameidata;
  42. struct nsproxy;
  43. struct perf_event_context;
  44. struct pid_namespace;
  45. struct pipe_inode_info;
  46. struct rcu_node;
  47. struct reclaim_state;
  48. struct robust_list_head;
  49. struct sched_attr;
  50. struct sched_param;
  51. struct seq_file;
  52. struct sighand_struct;
  53. struct signal_struct;
  54. struct task_delay_info;
  55. struct task_group;
  56. struct task_struct;
  57. struct uts_namespace;
  58. /*
  59. * Task state bitmask. NOTE! These bits are also
  60. * encoded in fs/proc/array.c: get_task_state().
  61. *
  62. * We have two separate sets of flags: task->state
  63. * is about runnability, while task->exit_state are
  64. * about the task exiting. Confusing, but this way
  65. * modifying one set can't modify the other one by
  66. * mistake.
  67. */
  68. #define TASK_RUNNING 0
  69. #define TASK_INTERRUPTIBLE 1
  70. #define TASK_UNINTERRUPTIBLE 2
  71. #define __TASK_STOPPED 4
  72. #define __TASK_TRACED 8
  73. /* in tsk->exit_state */
  74. #define EXIT_DEAD 16
  75. #define EXIT_ZOMBIE 32
  76. #define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
  77. /* in tsk->state again */
  78. #define TASK_DEAD 64
  79. #define TASK_WAKEKILL 128
  80. #define TASK_WAKING 256
  81. #define TASK_PARKED 512
  82. #define TASK_NOLOAD 1024
  83. #define TASK_NEW 2048
  84. #define TASK_STATE_MAX 4096
  85. #define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPNn"
  86. /* Convenience macros for the sake of set_current_state */
  87. #define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
  88. #define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
  89. #define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
  90. #define TASK_IDLE (TASK_UNINTERRUPTIBLE | TASK_NOLOAD)
  91. /* Convenience macros for the sake of wake_up */
  92. #define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
  93. #define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
  94. /* get_task_state() */
  95. #define TASK_REPORT (TASK_RUNNING | TASK_INTERRUPTIBLE | \
  96. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  97. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  98. #define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  99. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  100. #define task_is_stopped_or_traced(task) \
  101. ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  102. #define task_contributes_to_load(task) \
  103. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  104. (task->flags & PF_FROZEN) == 0 && \
  105. (task->state & TASK_NOLOAD) == 0)
  106. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  107. #define __set_current_state(state_value) \
  108. do { \
  109. current->task_state_change = _THIS_IP_; \
  110. current->state = (state_value); \
  111. } while (0)
  112. #define set_current_state(state_value) \
  113. do { \
  114. current->task_state_change = _THIS_IP_; \
  115. smp_store_mb(current->state, (state_value)); \
  116. } while (0)
  117. #else
  118. /*
  119. * set_current_state() includes a barrier so that the write of current->state
  120. * is correctly serialised wrt the caller's subsequent test of whether to
  121. * actually sleep:
  122. *
  123. * for (;;) {
  124. * set_current_state(TASK_UNINTERRUPTIBLE);
  125. * if (!need_sleep)
  126. * break;
  127. *
  128. * schedule();
  129. * }
  130. * __set_current_state(TASK_RUNNING);
  131. *
  132. * If the caller does not need such serialisation (because, for instance, the
  133. * condition test and condition change and wakeup are under the same lock) then
  134. * use __set_current_state().
  135. *
  136. * The above is typically ordered against the wakeup, which does:
  137. *
  138. * need_sleep = false;
  139. * wake_up_state(p, TASK_UNINTERRUPTIBLE);
  140. *
  141. * Where wake_up_state() (and all other wakeup primitives) imply enough
  142. * barriers to order the store of the variable against wakeup.
  143. *
  144. * Wakeup will do: if (@state & p->state) p->state = TASK_RUNNING, that is,
  145. * once it observes the TASK_UNINTERRUPTIBLE store the waking CPU can issue a
  146. * TASK_RUNNING store which can collide with __set_current_state(TASK_RUNNING).
  147. *
  148. * This is obviously fine, since they both store the exact same value.
  149. *
  150. * Also see the comments of try_to_wake_up().
  151. */
  152. #define __set_current_state(state_value) \
  153. do { current->state = (state_value); } while (0)
  154. #define set_current_state(state_value) \
  155. smp_store_mb(current->state, (state_value))
  156. #endif
  157. /* Task command name length */
  158. #define TASK_COMM_LEN 16
  159. extern void sched_init(void);
  160. extern void sched_init_smp(void);
  161. extern cpumask_var_t cpu_isolated_map;
  162. extern int runqueue_is_locked(int cpu);
  163. extern void cpu_init (void);
  164. extern void trap_init(void);
  165. extern void update_process_times(int user);
  166. extern void scheduler_tick(void);
  167. #define MAX_SCHEDULE_TIMEOUT LONG_MAX
  168. extern signed long schedule_timeout(signed long timeout);
  169. extern signed long schedule_timeout_interruptible(signed long timeout);
  170. extern signed long schedule_timeout_killable(signed long timeout);
  171. extern signed long schedule_timeout_uninterruptible(signed long timeout);
  172. extern signed long schedule_timeout_idle(signed long timeout);
  173. asmlinkage void schedule(void);
  174. extern void schedule_preempt_disabled(void);
  175. extern int __must_check io_schedule_prepare(void);
  176. extern void io_schedule_finish(int token);
  177. extern long io_schedule_timeout(long timeout);
  178. extern void io_schedule(void);
  179. /**
  180. * struct prev_cputime - snaphsot of system and user cputime
  181. * @utime: time spent in user mode
  182. * @stime: time spent in system mode
  183. * @lock: protects the above two fields
  184. *
  185. * Stores previous user/system time values such that we can guarantee
  186. * monotonicity.
  187. */
  188. struct prev_cputime {
  189. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  190. u64 utime;
  191. u64 stime;
  192. raw_spinlock_t lock;
  193. #endif
  194. };
  195. /**
  196. * struct task_cputime - collected CPU time counts
  197. * @utime: time spent in user mode, in nanoseconds
  198. * @stime: time spent in kernel mode, in nanoseconds
  199. * @sum_exec_runtime: total time spent on the CPU, in nanoseconds
  200. *
  201. * This structure groups together three kinds of CPU time that are tracked for
  202. * threads and thread groups. Most things considering CPU time want to group
  203. * these counts together and treat all three of them in parallel.
  204. */
  205. struct task_cputime {
  206. u64 utime;
  207. u64 stime;
  208. unsigned long long sum_exec_runtime;
  209. };
  210. /* Alternate field names when used to cache expirations. */
  211. #define virt_exp utime
  212. #define prof_exp stime
  213. #define sched_exp sum_exec_runtime
  214. #include <linux/rwsem.h>
  215. #ifdef CONFIG_SCHED_INFO
  216. struct sched_info {
  217. /* cumulative counters */
  218. unsigned long pcount; /* # of times run on this cpu */
  219. unsigned long long run_delay; /* time spent waiting on a runqueue */
  220. /* timestamps */
  221. unsigned long long last_arrival,/* when we last ran on a cpu */
  222. last_queued; /* when we were last queued to run */
  223. };
  224. #endif /* CONFIG_SCHED_INFO */
  225. /*
  226. * Integer metrics need fixed point arithmetic, e.g., sched/fair
  227. * has a few: load, load_avg, util_avg, freq, and capacity.
  228. *
  229. * We define a basic fixed point arithmetic range, and then formalize
  230. * all these metrics based on that basic range.
  231. */
  232. # define SCHED_FIXEDPOINT_SHIFT 10
  233. # define SCHED_FIXEDPOINT_SCALE (1L << SCHED_FIXEDPOINT_SHIFT)
  234. #ifdef ARCH_HAS_PREFETCH_SWITCH_STACK
  235. extern void prefetch_stack(struct task_struct *t);
  236. #else
  237. static inline void prefetch_stack(struct task_struct *t) { }
  238. #endif
  239. struct load_weight {
  240. unsigned long weight;
  241. u32 inv_weight;
  242. };
  243. /*
  244. * The load_avg/util_avg accumulates an infinite geometric series
  245. * (see __update_load_avg() in kernel/sched/fair.c).
  246. *
  247. * [load_avg definition]
  248. *
  249. * load_avg = runnable% * scale_load_down(load)
  250. *
  251. * where runnable% is the time ratio that a sched_entity is runnable.
  252. * For cfs_rq, it is the aggregated load_avg of all runnable and
  253. * blocked sched_entities.
  254. *
  255. * load_avg may also take frequency scaling into account:
  256. *
  257. * load_avg = runnable% * scale_load_down(load) * freq%
  258. *
  259. * where freq% is the CPU frequency normalized to the highest frequency.
  260. *
  261. * [util_avg definition]
  262. *
  263. * util_avg = running% * SCHED_CAPACITY_SCALE
  264. *
  265. * where running% is the time ratio that a sched_entity is running on
  266. * a CPU. For cfs_rq, it is the aggregated util_avg of all runnable
  267. * and blocked sched_entities.
  268. *
  269. * util_avg may also factor frequency scaling and CPU capacity scaling:
  270. *
  271. * util_avg = running% * SCHED_CAPACITY_SCALE * freq% * capacity%
  272. *
  273. * where freq% is the same as above, and capacity% is the CPU capacity
  274. * normalized to the greatest capacity (due to uarch differences, etc).
  275. *
  276. * N.B., the above ratios (runnable%, running%, freq%, and capacity%)
  277. * themselves are in the range of [0, 1]. To do fixed point arithmetics,
  278. * we therefore scale them to as large a range as necessary. This is for
  279. * example reflected by util_avg's SCHED_CAPACITY_SCALE.
  280. *
  281. * [Overflow issue]
  282. *
  283. * The 64-bit load_sum can have 4353082796 (=2^64/47742/88761) entities
  284. * with the highest load (=88761), always runnable on a single cfs_rq,
  285. * and should not overflow as the number already hits PID_MAX_LIMIT.
  286. *
  287. * For all other cases (including 32-bit kernels), struct load_weight's
  288. * weight will overflow first before we do, because:
  289. *
  290. * Max(load_avg) <= Max(load.weight)
  291. *
  292. * Then it is the load_weight's responsibility to consider overflow
  293. * issues.
  294. */
  295. struct sched_avg {
  296. u64 last_update_time, load_sum;
  297. u32 util_sum, period_contrib;
  298. unsigned long load_avg, util_avg;
  299. };
  300. #ifdef CONFIG_SCHEDSTATS
  301. struct sched_statistics {
  302. u64 wait_start;
  303. u64 wait_max;
  304. u64 wait_count;
  305. u64 wait_sum;
  306. u64 iowait_count;
  307. u64 iowait_sum;
  308. u64 sleep_start;
  309. u64 sleep_max;
  310. s64 sum_sleep_runtime;
  311. u64 block_start;
  312. u64 block_max;
  313. u64 exec_max;
  314. u64 slice_max;
  315. u64 nr_migrations_cold;
  316. u64 nr_failed_migrations_affine;
  317. u64 nr_failed_migrations_running;
  318. u64 nr_failed_migrations_hot;
  319. u64 nr_forced_migrations;
  320. u64 nr_wakeups;
  321. u64 nr_wakeups_sync;
  322. u64 nr_wakeups_migrate;
  323. u64 nr_wakeups_local;
  324. u64 nr_wakeups_remote;
  325. u64 nr_wakeups_affine;
  326. u64 nr_wakeups_affine_attempts;
  327. u64 nr_wakeups_passive;
  328. u64 nr_wakeups_idle;
  329. };
  330. #endif
  331. struct sched_entity {
  332. struct load_weight load; /* for load-balancing */
  333. struct rb_node run_node;
  334. struct list_head group_node;
  335. unsigned int on_rq;
  336. u64 exec_start;
  337. u64 sum_exec_runtime;
  338. u64 vruntime;
  339. u64 prev_sum_exec_runtime;
  340. u64 nr_migrations;
  341. #ifdef CONFIG_SCHEDSTATS
  342. struct sched_statistics statistics;
  343. #endif
  344. #ifdef CONFIG_FAIR_GROUP_SCHED
  345. int depth;
  346. struct sched_entity *parent;
  347. /* rq on which this entity is (to be) queued: */
  348. struct cfs_rq *cfs_rq;
  349. /* rq "owned" by this entity/group: */
  350. struct cfs_rq *my_q;
  351. #endif
  352. #ifdef CONFIG_SMP
  353. /*
  354. * Per entity load average tracking.
  355. *
  356. * Put into separate cache line so it does not
  357. * collide with read-mostly values above.
  358. */
  359. struct sched_avg avg ____cacheline_aligned_in_smp;
  360. #endif
  361. };
  362. struct sched_rt_entity {
  363. struct list_head run_list;
  364. unsigned long timeout;
  365. unsigned long watchdog_stamp;
  366. unsigned int time_slice;
  367. unsigned short on_rq;
  368. unsigned short on_list;
  369. struct sched_rt_entity *back;
  370. #ifdef CONFIG_RT_GROUP_SCHED
  371. struct sched_rt_entity *parent;
  372. /* rq on which this entity is (to be) queued: */
  373. struct rt_rq *rt_rq;
  374. /* rq "owned" by this entity/group: */
  375. struct rt_rq *my_q;
  376. #endif
  377. };
  378. struct sched_dl_entity {
  379. struct rb_node rb_node;
  380. /*
  381. * Original scheduling parameters. Copied here from sched_attr
  382. * during sched_setattr(), they will remain the same until
  383. * the next sched_setattr().
  384. */
  385. u64 dl_runtime; /* maximum runtime for each instance */
  386. u64 dl_deadline; /* relative deadline of each instance */
  387. u64 dl_period; /* separation of two instances (period) */
  388. u64 dl_bw; /* dl_runtime / dl_deadline */
  389. /*
  390. * Actual scheduling parameters. Initialized with the values above,
  391. * they are continously updated during task execution. Note that
  392. * the remaining runtime could be < 0 in case we are in overrun.
  393. */
  394. s64 runtime; /* remaining runtime for this instance */
  395. u64 deadline; /* absolute deadline for this instance */
  396. unsigned int flags; /* specifying the scheduler behaviour */
  397. /*
  398. * Some bool flags:
  399. *
  400. * @dl_throttled tells if we exhausted the runtime. If so, the
  401. * task has to wait for a replenishment to be performed at the
  402. * next firing of dl_timer.
  403. *
  404. * @dl_boosted tells if we are boosted due to DI. If so we are
  405. * outside bandwidth enforcement mechanism (but only until we
  406. * exit the critical section);
  407. *
  408. * @dl_yielded tells if task gave up the cpu before consuming
  409. * all its available runtime during the last job.
  410. */
  411. int dl_throttled, dl_boosted, dl_yielded;
  412. /*
  413. * Bandwidth enforcement timer. Each -deadline task has its
  414. * own bandwidth to be enforced, thus we need one timer per task.
  415. */
  416. struct hrtimer dl_timer;
  417. };
  418. union rcu_special {
  419. struct {
  420. u8 blocked;
  421. u8 need_qs;
  422. u8 exp_need_qs;
  423. u8 pad; /* Otherwise the compiler can store garbage here. */
  424. } b; /* Bits. */
  425. u32 s; /* Set of bits. */
  426. };
  427. enum perf_event_task_context {
  428. perf_invalid_context = -1,
  429. perf_hw_context = 0,
  430. perf_sw_context,
  431. perf_nr_task_contexts,
  432. };
  433. struct wake_q_node {
  434. struct wake_q_node *next;
  435. };
  436. /* Track pages that require TLB flushes */
  437. struct tlbflush_unmap_batch {
  438. /*
  439. * Each bit set is a CPU that potentially has a TLB entry for one of
  440. * the PFNs being flushed. See set_tlb_ubc_flush_pending().
  441. */
  442. struct cpumask cpumask;
  443. /* True if any bit in cpumask is set */
  444. bool flush_required;
  445. /*
  446. * If true then the PTE was dirty when unmapped. The entry must be
  447. * flushed before IO is initiated or a stale TLB entry potentially
  448. * allows an update without redirtying the page.
  449. */
  450. bool writable;
  451. };
  452. struct task_struct {
  453. #ifdef CONFIG_THREAD_INFO_IN_TASK
  454. /*
  455. * For reasons of header soup (see current_thread_info()), this
  456. * must be the first element of task_struct.
  457. */
  458. struct thread_info thread_info;
  459. #endif
  460. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  461. void *stack;
  462. atomic_t usage;
  463. unsigned int flags; /* per process flags, defined below */
  464. unsigned int ptrace;
  465. #ifdef CONFIG_SMP
  466. struct llist_node wake_entry;
  467. int on_cpu;
  468. #ifdef CONFIG_THREAD_INFO_IN_TASK
  469. unsigned int cpu; /* current CPU */
  470. #endif
  471. unsigned int wakee_flips;
  472. unsigned long wakee_flip_decay_ts;
  473. struct task_struct *last_wakee;
  474. int wake_cpu;
  475. #endif
  476. int on_rq;
  477. int prio, static_prio, normal_prio;
  478. unsigned int rt_priority;
  479. const struct sched_class *sched_class;
  480. struct sched_entity se;
  481. struct sched_rt_entity rt;
  482. #ifdef CONFIG_CGROUP_SCHED
  483. struct task_group *sched_task_group;
  484. #endif
  485. struct sched_dl_entity dl;
  486. #ifdef CONFIG_PREEMPT_NOTIFIERS
  487. /* list of struct preempt_notifier: */
  488. struct hlist_head preempt_notifiers;
  489. #endif
  490. #ifdef CONFIG_BLK_DEV_IO_TRACE
  491. unsigned int btrace_seq;
  492. #endif
  493. unsigned int policy;
  494. int nr_cpus_allowed;
  495. cpumask_t cpus_allowed;
  496. #ifdef CONFIG_PREEMPT_RCU
  497. int rcu_read_lock_nesting;
  498. union rcu_special rcu_read_unlock_special;
  499. struct list_head rcu_node_entry;
  500. struct rcu_node *rcu_blocked_node;
  501. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  502. #ifdef CONFIG_TASKS_RCU
  503. unsigned long rcu_tasks_nvcsw;
  504. bool rcu_tasks_holdout;
  505. struct list_head rcu_tasks_holdout_list;
  506. int rcu_tasks_idle_cpu;
  507. #endif /* #ifdef CONFIG_TASKS_RCU */
  508. #ifdef CONFIG_SCHED_INFO
  509. struct sched_info sched_info;
  510. #endif
  511. struct list_head tasks;
  512. #ifdef CONFIG_SMP
  513. struct plist_node pushable_tasks;
  514. struct rb_node pushable_dl_tasks;
  515. #endif
  516. struct mm_struct *mm, *active_mm;
  517. /* Per-thread vma caching: */
  518. struct vmacache vmacache;
  519. #if defined(SPLIT_RSS_COUNTING)
  520. struct task_rss_stat rss_stat;
  521. #endif
  522. /* task state */
  523. int exit_state;
  524. int exit_code, exit_signal;
  525. int pdeath_signal; /* The signal sent when the parent dies */
  526. unsigned long jobctl; /* JOBCTL_*, siglock protected */
  527. /* Used for emulating ABI behavior of previous Linux versions */
  528. unsigned int personality;
  529. /* scheduler bits, serialized by scheduler locks */
  530. unsigned sched_reset_on_fork:1;
  531. unsigned sched_contributes_to_load:1;
  532. unsigned sched_migrated:1;
  533. unsigned sched_remote_wakeup:1;
  534. unsigned :0; /* force alignment to the next boundary */
  535. /* unserialized, strictly 'current' */
  536. unsigned in_execve:1; /* bit to tell LSMs we're in execve */
  537. unsigned in_iowait:1;
  538. #if !defined(TIF_RESTORE_SIGMASK)
  539. unsigned restore_sigmask:1;
  540. #endif
  541. #ifdef CONFIG_MEMCG
  542. unsigned memcg_may_oom:1;
  543. #ifndef CONFIG_SLOB
  544. unsigned memcg_kmem_skip_account:1;
  545. #endif
  546. #endif
  547. #ifdef CONFIG_COMPAT_BRK
  548. unsigned brk_randomized:1;
  549. #endif
  550. unsigned long atomic_flags; /* Flags needing atomic access. */
  551. struct restart_block restart_block;
  552. pid_t pid;
  553. pid_t tgid;
  554. #ifdef CONFIG_CC_STACKPROTECTOR
  555. /* Canary value for the -fstack-protector gcc feature */
  556. unsigned long stack_canary;
  557. #endif
  558. /*
  559. * pointers to (original) parent process, youngest child, younger sibling,
  560. * older sibling, respectively. (p->father can be replaced with
  561. * p->real_parent->pid)
  562. */
  563. struct task_struct __rcu *real_parent; /* real parent process */
  564. struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
  565. /*
  566. * children/sibling forms the list of my natural children
  567. */
  568. struct list_head children; /* list of my children */
  569. struct list_head sibling; /* linkage in my parent's children list */
  570. struct task_struct *group_leader; /* threadgroup leader */
  571. /*
  572. * ptraced is the list of tasks this task is using ptrace on.
  573. * This includes both natural children and PTRACE_ATTACH targets.
  574. * p->ptrace_entry is p's link on the p->parent->ptraced list.
  575. */
  576. struct list_head ptraced;
  577. struct list_head ptrace_entry;
  578. /* PID/PID hash table linkage. */
  579. struct pid_link pids[PIDTYPE_MAX];
  580. struct list_head thread_group;
  581. struct list_head thread_node;
  582. struct completion *vfork_done; /* for vfork() */
  583. int __user *set_child_tid; /* CLONE_CHILD_SETTID */
  584. int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
  585. u64 utime, stime;
  586. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  587. u64 utimescaled, stimescaled;
  588. #endif
  589. u64 gtime;
  590. struct prev_cputime prev_cputime;
  591. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  592. seqcount_t vtime_seqcount;
  593. unsigned long long vtime_snap;
  594. enum {
  595. /* Task is sleeping or running in a CPU with VTIME inactive */
  596. VTIME_INACTIVE = 0,
  597. /* Task runs in userspace in a CPU with VTIME active */
  598. VTIME_USER,
  599. /* Task runs in kernelspace in a CPU with VTIME active */
  600. VTIME_SYS,
  601. } vtime_snap_whence;
  602. #endif
  603. #ifdef CONFIG_NO_HZ_FULL
  604. atomic_t tick_dep_mask;
  605. #endif
  606. unsigned long nvcsw, nivcsw; /* context switch counts */
  607. u64 start_time; /* monotonic time in nsec */
  608. u64 real_start_time; /* boot based time in nsec */
  609. /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
  610. unsigned long min_flt, maj_flt;
  611. #ifdef CONFIG_POSIX_TIMERS
  612. struct task_cputime cputime_expires;
  613. struct list_head cpu_timers[3];
  614. #endif
  615. /* process credentials */
  616. const struct cred __rcu *ptracer_cred; /* Tracer's credentials at attach */
  617. const struct cred __rcu *real_cred; /* objective and real subjective task
  618. * credentials (COW) */
  619. const struct cred __rcu *cred; /* effective (overridable) subjective task
  620. * credentials (COW) */
  621. char comm[TASK_COMM_LEN]; /* executable name excluding path
  622. - access with [gs]et_task_comm (which lock
  623. it with task_lock())
  624. - initialized normally by setup_new_exec */
  625. /* file system info */
  626. struct nameidata *nameidata;
  627. #ifdef CONFIG_SYSVIPC
  628. /* ipc stuff */
  629. struct sysv_sem sysvsem;
  630. struct sysv_shm sysvshm;
  631. #endif
  632. #ifdef CONFIG_DETECT_HUNG_TASK
  633. /* hung task detection */
  634. unsigned long last_switch_count;
  635. #endif
  636. /* filesystem information */
  637. struct fs_struct *fs;
  638. /* open file information */
  639. struct files_struct *files;
  640. /* namespaces */
  641. struct nsproxy *nsproxy;
  642. /* signal handlers */
  643. struct signal_struct *signal;
  644. struct sighand_struct *sighand;
  645. sigset_t blocked, real_blocked;
  646. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  647. struct sigpending pending;
  648. unsigned long sas_ss_sp;
  649. size_t sas_ss_size;
  650. unsigned sas_ss_flags;
  651. struct callback_head *task_works;
  652. struct audit_context *audit_context;
  653. #ifdef CONFIG_AUDITSYSCALL
  654. kuid_t loginuid;
  655. unsigned int sessionid;
  656. #endif
  657. struct seccomp seccomp;
  658. /* Thread group tracking */
  659. u32 parent_exec_id;
  660. u32 self_exec_id;
  661. /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
  662. * mempolicy */
  663. spinlock_t alloc_lock;
  664. /* Protection of the PI data structures: */
  665. raw_spinlock_t pi_lock;
  666. struct wake_q_node wake_q;
  667. #ifdef CONFIG_RT_MUTEXES
  668. /* PI waiters blocked on a rt_mutex held by this task */
  669. struct rb_root pi_waiters;
  670. struct rb_node *pi_waiters_leftmost;
  671. /* Deadlock detection and priority inheritance handling */
  672. struct rt_mutex_waiter *pi_blocked_on;
  673. #endif
  674. #ifdef CONFIG_DEBUG_MUTEXES
  675. /* mutex deadlock detection */
  676. struct mutex_waiter *blocked_on;
  677. #endif
  678. #ifdef CONFIG_TRACE_IRQFLAGS
  679. unsigned int irq_events;
  680. unsigned long hardirq_enable_ip;
  681. unsigned long hardirq_disable_ip;
  682. unsigned int hardirq_enable_event;
  683. unsigned int hardirq_disable_event;
  684. int hardirqs_enabled;
  685. int hardirq_context;
  686. unsigned long softirq_disable_ip;
  687. unsigned long softirq_enable_ip;
  688. unsigned int softirq_disable_event;
  689. unsigned int softirq_enable_event;
  690. int softirqs_enabled;
  691. int softirq_context;
  692. #endif
  693. #ifdef CONFIG_LOCKDEP
  694. # define MAX_LOCK_DEPTH 48UL
  695. u64 curr_chain_key;
  696. int lockdep_depth;
  697. unsigned int lockdep_recursion;
  698. struct held_lock held_locks[MAX_LOCK_DEPTH];
  699. gfp_t lockdep_reclaim_gfp;
  700. #endif
  701. #ifdef CONFIG_UBSAN
  702. unsigned int in_ubsan;
  703. #endif
  704. /* journalling filesystem info */
  705. void *journal_info;
  706. /* stacked block device info */
  707. struct bio_list *bio_list;
  708. #ifdef CONFIG_BLOCK
  709. /* stack plugging */
  710. struct blk_plug *plug;
  711. #endif
  712. /* VM state */
  713. struct reclaim_state *reclaim_state;
  714. struct backing_dev_info *backing_dev_info;
  715. struct io_context *io_context;
  716. unsigned long ptrace_message;
  717. siginfo_t *last_siginfo; /* For ptrace use. */
  718. struct task_io_accounting ioac;
  719. #if defined(CONFIG_TASK_XACCT)
  720. u64 acct_rss_mem1; /* accumulated rss usage */
  721. u64 acct_vm_mem1; /* accumulated virtual memory usage */
  722. u64 acct_timexpd; /* stime + utime since last update */
  723. #endif
  724. #ifdef CONFIG_CPUSETS
  725. nodemask_t mems_allowed; /* Protected by alloc_lock */
  726. seqcount_t mems_allowed_seq; /* Seqence no to catch updates */
  727. int cpuset_mem_spread_rotor;
  728. int cpuset_slab_spread_rotor;
  729. #endif
  730. #ifdef CONFIG_CGROUPS
  731. /* Control Group info protected by css_set_lock */
  732. struct css_set __rcu *cgroups;
  733. /* cg_list protected by css_set_lock and tsk->alloc_lock */
  734. struct list_head cg_list;
  735. #endif
  736. #ifdef CONFIG_INTEL_RDT_A
  737. int closid;
  738. #endif
  739. #ifdef CONFIG_FUTEX
  740. struct robust_list_head __user *robust_list;
  741. #ifdef CONFIG_COMPAT
  742. struct compat_robust_list_head __user *compat_robust_list;
  743. #endif
  744. struct list_head pi_state_list;
  745. struct futex_pi_state *pi_state_cache;
  746. #endif
  747. #ifdef CONFIG_PERF_EVENTS
  748. struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
  749. struct mutex perf_event_mutex;
  750. struct list_head perf_event_list;
  751. #endif
  752. #ifdef CONFIG_DEBUG_PREEMPT
  753. unsigned long preempt_disable_ip;
  754. #endif
  755. #ifdef CONFIG_NUMA
  756. struct mempolicy *mempolicy; /* Protected by alloc_lock */
  757. short il_next;
  758. short pref_node_fork;
  759. #endif
  760. #ifdef CONFIG_NUMA_BALANCING
  761. int numa_scan_seq;
  762. unsigned int numa_scan_period;
  763. unsigned int numa_scan_period_max;
  764. int numa_preferred_nid;
  765. unsigned long numa_migrate_retry;
  766. u64 node_stamp; /* migration stamp */
  767. u64 last_task_numa_placement;
  768. u64 last_sum_exec_runtime;
  769. struct callback_head numa_work;
  770. struct list_head numa_entry;
  771. struct numa_group *numa_group;
  772. /*
  773. * numa_faults is an array split into four regions:
  774. * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
  775. * in this precise order.
  776. *
  777. * faults_memory: Exponential decaying average of faults on a per-node
  778. * basis. Scheduling placement decisions are made based on these
  779. * counts. The values remain static for the duration of a PTE scan.
  780. * faults_cpu: Track the nodes the process was running on when a NUMA
  781. * hinting fault was incurred.
  782. * faults_memory_buffer and faults_cpu_buffer: Record faults per node
  783. * during the current scan window. When the scan completes, the counts
  784. * in faults_memory and faults_cpu decay and these values are copied.
  785. */
  786. unsigned long *numa_faults;
  787. unsigned long total_numa_faults;
  788. /*
  789. * numa_faults_locality tracks if faults recorded during the last
  790. * scan window were remote/local or failed to migrate. The task scan
  791. * period is adapted based on the locality of the faults with different
  792. * weights depending on whether they were shared or private faults
  793. */
  794. unsigned long numa_faults_locality[3];
  795. unsigned long numa_pages_migrated;
  796. #endif /* CONFIG_NUMA_BALANCING */
  797. #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
  798. struct tlbflush_unmap_batch tlb_ubc;
  799. #endif
  800. struct rcu_head rcu;
  801. /*
  802. * cache last used pipe for splice
  803. */
  804. struct pipe_inode_info *splice_pipe;
  805. struct page_frag task_frag;
  806. #ifdef CONFIG_TASK_DELAY_ACCT
  807. struct task_delay_info *delays;
  808. #endif
  809. #ifdef CONFIG_FAULT_INJECTION
  810. int make_it_fail;
  811. #endif
  812. /*
  813. * when (nr_dirtied >= nr_dirtied_pause), it's time to call
  814. * balance_dirty_pages() for some dirty throttling pause
  815. */
  816. int nr_dirtied;
  817. int nr_dirtied_pause;
  818. unsigned long dirty_paused_when; /* start of a write-and-pause period */
  819. #ifdef CONFIG_LATENCYTOP
  820. int latency_record_count;
  821. struct latency_record latency_record[LT_SAVECOUNT];
  822. #endif
  823. /*
  824. * time slack values; these are used to round up poll() and
  825. * select() etc timeout values. These are in nanoseconds.
  826. */
  827. u64 timer_slack_ns;
  828. u64 default_timer_slack_ns;
  829. #ifdef CONFIG_KASAN
  830. unsigned int kasan_depth;
  831. #endif
  832. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  833. /* Index of current stored address in ret_stack */
  834. int curr_ret_stack;
  835. /* Stack of return addresses for return function tracing */
  836. struct ftrace_ret_stack *ret_stack;
  837. /* time stamp for last schedule */
  838. unsigned long long ftrace_timestamp;
  839. /*
  840. * Number of functions that haven't been traced
  841. * because of depth overrun.
  842. */
  843. atomic_t trace_overrun;
  844. /* Pause for the tracing */
  845. atomic_t tracing_graph_pause;
  846. #endif
  847. #ifdef CONFIG_TRACING
  848. /* state flags for use by tracers */
  849. unsigned long trace;
  850. /* bitmask and counter of trace recursion */
  851. unsigned long trace_recursion;
  852. #endif /* CONFIG_TRACING */
  853. #ifdef CONFIG_KCOV
  854. /* Coverage collection mode enabled for this task (0 if disabled). */
  855. enum kcov_mode kcov_mode;
  856. /* Size of the kcov_area. */
  857. unsigned kcov_size;
  858. /* Buffer for coverage collection. */
  859. void *kcov_area;
  860. /* kcov desciptor wired with this task or NULL. */
  861. struct kcov *kcov;
  862. #endif
  863. #ifdef CONFIG_MEMCG
  864. struct mem_cgroup *memcg_in_oom;
  865. gfp_t memcg_oom_gfp_mask;
  866. int memcg_oom_order;
  867. /* number of pages to reclaim on returning to userland */
  868. unsigned int memcg_nr_pages_over_high;
  869. #endif
  870. #ifdef CONFIG_UPROBES
  871. struct uprobe_task *utask;
  872. #endif
  873. #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
  874. unsigned int sequential_io;
  875. unsigned int sequential_io_avg;
  876. #endif
  877. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  878. unsigned long task_state_change;
  879. #endif
  880. int pagefault_disabled;
  881. #ifdef CONFIG_MMU
  882. struct task_struct *oom_reaper_list;
  883. #endif
  884. #ifdef CONFIG_VMAP_STACK
  885. struct vm_struct *stack_vm_area;
  886. #endif
  887. #ifdef CONFIG_THREAD_INFO_IN_TASK
  888. /* A live task holds one reference. */
  889. atomic_t stack_refcount;
  890. #endif
  891. /* CPU-specific state of this task */
  892. struct thread_struct thread;
  893. /*
  894. * WARNING: on x86, 'thread_struct' contains a variable-sized
  895. * structure. It *MUST* be at the end of 'task_struct'.
  896. *
  897. * Do not put anything below here!
  898. */
  899. };
  900. static inline struct pid *task_pid(struct task_struct *task)
  901. {
  902. return task->pids[PIDTYPE_PID].pid;
  903. }
  904. static inline struct pid *task_tgid(struct task_struct *task)
  905. {
  906. return task->group_leader->pids[PIDTYPE_PID].pid;
  907. }
  908. /*
  909. * Without tasklist or rcu lock it is not safe to dereference
  910. * the result of task_pgrp/task_session even if task == current,
  911. * we can race with another thread doing sys_setsid/sys_setpgid.
  912. */
  913. static inline struct pid *task_pgrp(struct task_struct *task)
  914. {
  915. return task->group_leader->pids[PIDTYPE_PGID].pid;
  916. }
  917. static inline struct pid *task_session(struct task_struct *task)
  918. {
  919. return task->group_leader->pids[PIDTYPE_SID].pid;
  920. }
  921. /*
  922. * the helpers to get the task's different pids as they are seen
  923. * from various namespaces
  924. *
  925. * task_xid_nr() : global id, i.e. the id seen from the init namespace;
  926. * task_xid_vnr() : virtual id, i.e. the id seen from the pid namespace of
  927. * current.
  928. * task_xid_nr_ns() : id seen from the ns specified;
  929. *
  930. * set_task_vxid() : assigns a virtual id to a task;
  931. *
  932. * see also pid_nr() etc in include/linux/pid.h
  933. */
  934. pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
  935. struct pid_namespace *ns);
  936. static inline pid_t task_pid_nr(struct task_struct *tsk)
  937. {
  938. return tsk->pid;
  939. }
  940. static inline pid_t task_pid_nr_ns(struct task_struct *tsk,
  941. struct pid_namespace *ns)
  942. {
  943. return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
  944. }
  945. static inline pid_t task_pid_vnr(struct task_struct *tsk)
  946. {
  947. return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
  948. }
  949. static inline pid_t task_tgid_nr(struct task_struct *tsk)
  950. {
  951. return tsk->tgid;
  952. }
  953. pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
  954. static inline pid_t task_tgid_vnr(struct task_struct *tsk)
  955. {
  956. return pid_vnr(task_tgid(tsk));
  957. }
  958. static inline int pid_alive(const struct task_struct *p);
  959. static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
  960. {
  961. pid_t pid = 0;
  962. rcu_read_lock();
  963. if (pid_alive(tsk))
  964. pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
  965. rcu_read_unlock();
  966. return pid;
  967. }
  968. static inline pid_t task_ppid_nr(const struct task_struct *tsk)
  969. {
  970. return task_ppid_nr_ns(tsk, &init_pid_ns);
  971. }
  972. static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
  973. struct pid_namespace *ns)
  974. {
  975. return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
  976. }
  977. static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
  978. {
  979. return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
  980. }
  981. static inline pid_t task_session_nr_ns(struct task_struct *tsk,
  982. struct pid_namespace *ns)
  983. {
  984. return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
  985. }
  986. static inline pid_t task_session_vnr(struct task_struct *tsk)
  987. {
  988. return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
  989. }
  990. /* obsolete, do not use */
  991. static inline pid_t task_pgrp_nr(struct task_struct *tsk)
  992. {
  993. return task_pgrp_nr_ns(tsk, &init_pid_ns);
  994. }
  995. /**
  996. * pid_alive - check that a task structure is not stale
  997. * @p: Task structure to be checked.
  998. *
  999. * Test if a process is not yet dead (at most zombie state)
  1000. * If pid_alive fails, then pointers within the task structure
  1001. * can be stale and must not be dereferenced.
  1002. *
  1003. * Return: 1 if the process is alive. 0 otherwise.
  1004. */
  1005. static inline int pid_alive(const struct task_struct *p)
  1006. {
  1007. return p->pids[PIDTYPE_PID].pid != NULL;
  1008. }
  1009. /**
  1010. * is_global_init - check if a task structure is init. Since init
  1011. * is free to have sub-threads we need to check tgid.
  1012. * @tsk: Task structure to be checked.
  1013. *
  1014. * Check if a task structure is the first user space task the kernel created.
  1015. *
  1016. * Return: 1 if the task structure is init. 0 otherwise.
  1017. */
  1018. static inline int is_global_init(struct task_struct *tsk)
  1019. {
  1020. return task_tgid_nr(tsk) == 1;
  1021. }
  1022. extern struct pid *cad_pid;
  1023. extern void free_task(struct task_struct *tsk);
  1024. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  1025. extern void __put_task_struct(struct task_struct *t);
  1026. static inline void put_task_struct(struct task_struct *t)
  1027. {
  1028. if (atomic_dec_and_test(&t->usage))
  1029. __put_task_struct(t);
  1030. }
  1031. struct task_struct *task_rcu_dereference(struct task_struct **ptask);
  1032. struct task_struct *try_get_task_struct(struct task_struct **ptask);
  1033. /*
  1034. * Per process flags
  1035. */
  1036. #define PF_IDLE 0x00000002 /* I am an IDLE thread */
  1037. #define PF_EXITING 0x00000004 /* getting shut down */
  1038. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  1039. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  1040. #define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
  1041. #define PF_FORKNOEXEC 0x00000040 /* forked but didn't exec */
  1042. #define PF_MCE_PROCESS 0x00000080 /* process policy on mce errors */
  1043. #define PF_SUPERPRIV 0x00000100 /* used super-user privileges */
  1044. #define PF_DUMPCORE 0x00000200 /* dumped core */
  1045. #define PF_SIGNALED 0x00000400 /* killed by a signal */
  1046. #define PF_MEMALLOC 0x00000800 /* Allocating memory */
  1047. #define PF_NPROC_EXCEEDED 0x00001000 /* set_user noticed that RLIMIT_NPROC was exceeded */
  1048. #define PF_USED_MATH 0x00002000 /* if unset the fpu must be initialized before use */
  1049. #define PF_USED_ASYNC 0x00004000 /* used async_schedule*(), used by module init */
  1050. #define PF_NOFREEZE 0x00008000 /* this thread should not be frozen */
  1051. #define PF_FROZEN 0x00010000 /* frozen for system suspend */
  1052. #define PF_FSTRANS 0x00020000 /* inside a filesystem transaction */
  1053. #define PF_KSWAPD 0x00040000 /* I am kswapd */
  1054. #define PF_MEMALLOC_NOIO 0x00080000 /* Allocating memory without IO involved */
  1055. #define PF_LESS_THROTTLE 0x00100000 /* Throttle me less: I clean memory */
  1056. #define PF_KTHREAD 0x00200000 /* I am a kernel thread */
  1057. #define PF_RANDOMIZE 0x00400000 /* randomize virtual address space */
  1058. #define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
  1059. #define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
  1060. #define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
  1061. #define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
  1062. #define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
  1063. #define PF_SUSPEND_TASK 0x80000000 /* this thread called freeze_processes and should not be frozen */
  1064. /*
  1065. * Only the _current_ task can read/write to tsk->flags, but other
  1066. * tasks can access tsk->flags in readonly mode for example
  1067. * with tsk_used_math (like during threaded core dumping).
  1068. * There is however an exception to this rule during ptrace
  1069. * or during fork: the ptracer task is allowed to write to the
  1070. * child->flags of its traced child (same goes for fork, the parent
  1071. * can write to the child->flags), because we're guaranteed the
  1072. * child is not running and in turn not changing child->flags
  1073. * at the same time the parent does it.
  1074. */
  1075. #define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
  1076. #define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
  1077. #define clear_used_math() clear_stopped_child_used_math(current)
  1078. #define set_used_math() set_stopped_child_used_math(current)
  1079. #define conditional_stopped_child_used_math(condition, child) \
  1080. do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
  1081. #define conditional_used_math(condition) \
  1082. conditional_stopped_child_used_math(condition, current)
  1083. #define copy_to_stopped_child_used_math(child) \
  1084. do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
  1085. /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
  1086. #define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
  1087. #define used_math() tsk_used_math(current)
  1088. /* Per-process atomic flags. */
  1089. #define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */
  1090. #define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */
  1091. #define PFA_SPREAD_SLAB 2 /* Spread some slab caches over cpuset */
  1092. #define PFA_LMK_WAITING 3 /* Lowmemorykiller is waiting */
  1093. #define TASK_PFA_TEST(name, func) \
  1094. static inline bool task_##func(struct task_struct *p) \
  1095. { return test_bit(PFA_##name, &p->atomic_flags); }
  1096. #define TASK_PFA_SET(name, func) \
  1097. static inline void task_set_##func(struct task_struct *p) \
  1098. { set_bit(PFA_##name, &p->atomic_flags); }
  1099. #define TASK_PFA_CLEAR(name, func) \
  1100. static inline void task_clear_##func(struct task_struct *p) \
  1101. { clear_bit(PFA_##name, &p->atomic_flags); }
  1102. TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
  1103. TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
  1104. TASK_PFA_TEST(SPREAD_PAGE, spread_page)
  1105. TASK_PFA_SET(SPREAD_PAGE, spread_page)
  1106. TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
  1107. TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
  1108. TASK_PFA_SET(SPREAD_SLAB, spread_slab)
  1109. TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
  1110. TASK_PFA_TEST(LMK_WAITING, lmk_waiting)
  1111. TASK_PFA_SET(LMK_WAITING, lmk_waiting)
  1112. static inline void tsk_restore_flags(struct task_struct *task,
  1113. unsigned long orig_flags, unsigned long flags)
  1114. {
  1115. task->flags &= ~flags;
  1116. task->flags |= orig_flags & flags;
  1117. }
  1118. extern int cpuset_cpumask_can_shrink(const struct cpumask *cur,
  1119. const struct cpumask *trial);
  1120. extern int task_can_attach(struct task_struct *p,
  1121. const struct cpumask *cs_cpus_allowed);
  1122. #ifdef CONFIG_SMP
  1123. extern void do_set_cpus_allowed(struct task_struct *p,
  1124. const struct cpumask *new_mask);
  1125. extern int set_cpus_allowed_ptr(struct task_struct *p,
  1126. const struct cpumask *new_mask);
  1127. #else
  1128. static inline void do_set_cpus_allowed(struct task_struct *p,
  1129. const struct cpumask *new_mask)
  1130. {
  1131. }
  1132. static inline int set_cpus_allowed_ptr(struct task_struct *p,
  1133. const struct cpumask *new_mask)
  1134. {
  1135. if (!cpumask_test_cpu(0, new_mask))
  1136. return -EINVAL;
  1137. return 0;
  1138. }
  1139. #endif
  1140. #ifndef cpu_relax_yield
  1141. #define cpu_relax_yield() cpu_relax()
  1142. #endif
  1143. /* sched_exec is called by processes performing an exec */
  1144. #ifdef CONFIG_SMP
  1145. extern void sched_exec(void);
  1146. #else
  1147. #define sched_exec() {}
  1148. #endif
  1149. extern int yield_to(struct task_struct *p, bool preempt);
  1150. extern void set_user_nice(struct task_struct *p, long nice);
  1151. extern int task_prio(const struct task_struct *p);
  1152. /**
  1153. * task_nice - return the nice value of a given task.
  1154. * @p: the task in question.
  1155. *
  1156. * Return: The nice value [ -20 ... 0 ... 19 ].
  1157. */
  1158. static inline int task_nice(const struct task_struct *p)
  1159. {
  1160. return PRIO_TO_NICE((p)->static_prio);
  1161. }
  1162. extern int can_nice(const struct task_struct *p, const int nice);
  1163. extern int task_curr(const struct task_struct *p);
  1164. extern int idle_cpu(int cpu);
  1165. extern int sched_setscheduler(struct task_struct *, int,
  1166. const struct sched_param *);
  1167. extern int sched_setscheduler_nocheck(struct task_struct *, int,
  1168. const struct sched_param *);
  1169. extern int sched_setattr(struct task_struct *,
  1170. const struct sched_attr *);
  1171. extern struct task_struct *idle_task(int cpu);
  1172. /**
  1173. * is_idle_task - is the specified task an idle task?
  1174. * @p: the task in question.
  1175. *
  1176. * Return: 1 if @p is an idle task. 0 otherwise.
  1177. */
  1178. static inline bool is_idle_task(const struct task_struct *p)
  1179. {
  1180. return !!(p->flags & PF_IDLE);
  1181. }
  1182. extern struct task_struct *curr_task(int cpu);
  1183. extern void ia64_set_curr_task(int cpu, struct task_struct *p);
  1184. void yield(void);
  1185. union thread_union {
  1186. #ifndef CONFIG_THREAD_INFO_IN_TASK
  1187. struct thread_info thread_info;
  1188. #endif
  1189. unsigned long stack[THREAD_SIZE/sizeof(long)];
  1190. };
  1191. #ifdef CONFIG_THREAD_INFO_IN_TASK
  1192. static inline struct thread_info *task_thread_info(struct task_struct *task)
  1193. {
  1194. return &task->thread_info;
  1195. }
  1196. #elif !defined(__HAVE_THREAD_FUNCTIONS)
  1197. # define task_thread_info(task) ((struct thread_info *)(task)->stack)
  1198. #endif
  1199. #ifndef __HAVE_ARCH_KSTACK_END
  1200. static inline int kstack_end(void *addr)
  1201. {
  1202. /* Reliable end of stack detection:
  1203. * Some APM bios versions misalign the stack
  1204. */
  1205. return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
  1206. }
  1207. #endif
  1208. extern struct pid_namespace init_pid_ns;
  1209. /*
  1210. * find a task by one of its numerical ids
  1211. *
  1212. * find_task_by_pid_ns():
  1213. * finds a task by its pid in the specified namespace
  1214. * find_task_by_vpid():
  1215. * finds a task by its virtual pid
  1216. *
  1217. * see also find_vpid() etc in include/linux/pid.h
  1218. */
  1219. extern struct task_struct *find_task_by_vpid(pid_t nr);
  1220. extern struct task_struct *find_task_by_pid_ns(pid_t nr,
  1221. struct pid_namespace *ns);
  1222. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  1223. extern int wake_up_process(struct task_struct *tsk);
  1224. extern void wake_up_new_task(struct task_struct *tsk);
  1225. #ifdef CONFIG_SMP
  1226. extern void kick_process(struct task_struct *tsk);
  1227. #else
  1228. static inline void kick_process(struct task_struct *tsk) { }
  1229. #endif
  1230. extern void exit_files(struct task_struct *);
  1231. extern void exit_itimers(struct signal_struct *);
  1232. extern int do_execve(struct filename *,
  1233. const char __user * const __user *,
  1234. const char __user * const __user *);
  1235. extern int do_execveat(int, struct filename *,
  1236. const char __user * const __user *,
  1237. const char __user * const __user *,
  1238. int);
  1239. extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
  1240. static inline void set_task_comm(struct task_struct *tsk, const char *from)
  1241. {
  1242. __set_task_comm(tsk, from, false);
  1243. }
  1244. extern char *get_task_comm(char *to, struct task_struct *tsk);
  1245. #ifdef CONFIG_SMP
  1246. void scheduler_ipi(void);
  1247. extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
  1248. #else
  1249. static inline void scheduler_ipi(void) { }
  1250. static inline unsigned long wait_task_inactive(struct task_struct *p,
  1251. long match_state)
  1252. {
  1253. return 1;
  1254. }
  1255. #endif
  1256. /* set thread flags in other task's structures
  1257. * - see asm/thread_info.h for TIF_xxxx flags available
  1258. */
  1259. static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
  1260. {
  1261. set_ti_thread_flag(task_thread_info(tsk), flag);
  1262. }
  1263. static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
  1264. {
  1265. clear_ti_thread_flag(task_thread_info(tsk), flag);
  1266. }
  1267. static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
  1268. {
  1269. return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
  1270. }
  1271. static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
  1272. {
  1273. return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
  1274. }
  1275. static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
  1276. {
  1277. return test_ti_thread_flag(task_thread_info(tsk), flag);
  1278. }
  1279. static inline void set_tsk_need_resched(struct task_struct *tsk)
  1280. {
  1281. set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
  1282. }
  1283. static inline void clear_tsk_need_resched(struct task_struct *tsk)
  1284. {
  1285. clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
  1286. }
  1287. static inline int test_tsk_need_resched(struct task_struct *tsk)
  1288. {
  1289. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  1290. }
  1291. /*
  1292. * cond_resched() and cond_resched_lock(): latency reduction via
  1293. * explicit rescheduling in places that are safe. The return
  1294. * value indicates whether a reschedule was done in fact.
  1295. * cond_resched_lock() will drop the spinlock before scheduling,
  1296. * cond_resched_softirq() will enable bhs before scheduling.
  1297. */
  1298. #ifndef CONFIG_PREEMPT
  1299. extern int _cond_resched(void);
  1300. #else
  1301. static inline int _cond_resched(void) { return 0; }
  1302. #endif
  1303. #define cond_resched() ({ \
  1304. ___might_sleep(__FILE__, __LINE__, 0); \
  1305. _cond_resched(); \
  1306. })
  1307. extern int __cond_resched_lock(spinlock_t *lock);
  1308. #define cond_resched_lock(lock) ({ \
  1309. ___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
  1310. __cond_resched_lock(lock); \
  1311. })
  1312. extern int __cond_resched_softirq(void);
  1313. #define cond_resched_softirq() ({ \
  1314. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  1315. __cond_resched_softirq(); \
  1316. })
  1317. static inline void cond_resched_rcu(void)
  1318. {
  1319. #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
  1320. rcu_read_unlock();
  1321. cond_resched();
  1322. rcu_read_lock();
  1323. #endif
  1324. }
  1325. /*
  1326. * Does a critical section need to be broken due to another
  1327. * task waiting?: (technically does not depend on CONFIG_PREEMPT,
  1328. * but a general need for low latency)
  1329. */
  1330. static inline int spin_needbreak(spinlock_t *lock)
  1331. {
  1332. #ifdef CONFIG_PREEMPT
  1333. return spin_is_contended(lock);
  1334. #else
  1335. return 0;
  1336. #endif
  1337. }
  1338. static __always_inline bool need_resched(void)
  1339. {
  1340. return unlikely(tif_need_resched());
  1341. }
  1342. /*
  1343. * Wrappers for p->thread_info->cpu access. No-op on UP.
  1344. */
  1345. #ifdef CONFIG_SMP
  1346. static inline unsigned int task_cpu(const struct task_struct *p)
  1347. {
  1348. #ifdef CONFIG_THREAD_INFO_IN_TASK
  1349. return p->cpu;
  1350. #else
  1351. return task_thread_info(p)->cpu;
  1352. #endif
  1353. }
  1354. static inline int task_node(const struct task_struct *p)
  1355. {
  1356. return cpu_to_node(task_cpu(p));
  1357. }
  1358. extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
  1359. #else
  1360. static inline unsigned int task_cpu(const struct task_struct *p)
  1361. {
  1362. return 0;
  1363. }
  1364. static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
  1365. {
  1366. }
  1367. #endif /* CONFIG_SMP */
  1368. /*
  1369. * In order to reduce various lock holder preemption latencies provide an
  1370. * interface to see if a vCPU is currently running or not.
  1371. *
  1372. * This allows us to terminate optimistic spin loops and block, analogous to
  1373. * the native optimistic spin heuristic of testing if the lock owner task is
  1374. * running or not.
  1375. */
  1376. #ifndef vcpu_is_preempted
  1377. # define vcpu_is_preempted(cpu) false
  1378. #endif
  1379. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  1380. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  1381. extern int task_can_switch_user(struct user_struct *up,
  1382. struct task_struct *tsk);
  1383. #ifndef TASK_SIZE_OF
  1384. #define TASK_SIZE_OF(tsk) TASK_SIZE
  1385. #endif
  1386. #endif