fgraph.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Infrastructure to took into function calls and returns.
  4. * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
  5. * Mostly borrowed from function tracer which
  6. * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
  7. *
  8. * Highly modified by Steven Rostedt (VMware).
  9. */
  10. #include <linux/bits.h>
  11. #include <linux/jump_label.h>
  12. #include <linux/suspend.h>
  13. #include <linux/ftrace.h>
  14. #include <linux/static_call.h>
  15. #include <linux/slab.h>
  16. #include <trace/events/sched.h>
  17. #include "ftrace_internal.h"
  18. #include "trace.h"
  19. /*
  20. * FGRAPH_FRAME_SIZE: Size in bytes of the meta data on the shadow stack
  21. * FGRAPH_FRAME_OFFSET: Size in long words of the meta data frame
  22. */
  23. #define FGRAPH_FRAME_SIZE sizeof(struct ftrace_ret_stack)
  24. #define FGRAPH_FRAME_OFFSET DIV_ROUND_UP(FGRAPH_FRAME_SIZE, sizeof(long))
  25. /*
  26. * On entry to a function (via function_graph_enter()), a new fgraph frame
  27. * (ftrace_ret_stack) is pushed onto the stack as well as a word that
  28. * holds a bitmask and a type (called "bitmap"). The bitmap is defined as:
  29. *
  30. * bits: 0 - 9 offset in words from the previous ftrace_ret_stack
  31. *
  32. * bits: 10 - 11 Type of storage
  33. * 0 - reserved
  34. * 1 - bitmap of fgraph_array index
  35. * 2 - reserved data
  36. *
  37. * For type with "bitmap of fgraph_array index" (FGRAPH_TYPE_BITMAP):
  38. * bits: 12 - 27 The bitmap of fgraph_ops fgraph_array index
  39. * That is, it's a bitmask of 0-15 (16 bits)
  40. * where if a corresponding ops in the fgraph_array[]
  41. * expects a callback from the return of the function
  42. * it's corresponding bit will be set.
  43. *
  44. *
  45. * The top of the ret_stack (when not empty) will always have a reference
  46. * word that points to the last fgraph frame that was saved.
  47. *
  48. * For reserved data:
  49. * bits: 12 - 17 The size in words that is stored
  50. * bits: 18 - 23 The index of fgraph_array, which shows who is stored
  51. *
  52. * That is, at the end of function_graph_enter, if the first and forth
  53. * fgraph_ops on the fgraph_array[] (index 0 and 3) needs their retfunc called
  54. * on the return of the function being traced, and the forth fgraph_ops
  55. * stored two words of data, this is what will be on the task's shadow
  56. * ret_stack: (the stack grows upward)
  57. *
  58. * ret_stack[SHADOW_STACK_OFFSET]
  59. * | SHADOW_STACK_TASK_VARS(ret_stack)[15] |
  60. * ...
  61. * | SHADOW_STACK_TASK_VARS(ret_stack)[0] |
  62. * ret_stack[SHADOW_STACK_MAX_OFFSET]
  63. * ...
  64. * | | <- task->curr_ret_stack
  65. * +--------------------------------------------+
  66. * | (3 << 12) | (3 << 10) | FGRAPH_FRAME_OFFSET|
  67. * | *or put another way* |
  68. * | (3 << FGRAPH_DATA_INDEX_SHIFT)| \ | This is for fgraph_ops[3].
  69. * | ((2 - 1) << FGRAPH_DATA_SHIFT)| \ | The data size is 2 words.
  70. * | (FGRAPH_TYPE_DATA << FGRAPH_TYPE_SHIFT)| \ |
  71. * | (offset2:FGRAPH_FRAME_OFFSET+3) | <- the offset2 is from here
  72. * +--------------------------------------------+ ( It is 4 words from the ret_stack)
  73. * | STORED DATA WORD 2 |
  74. * | STORED DATA WORD 1 |
  75. * +--------------------------------------------+
  76. * | (9 << 12) | (1 << 10) | FGRAPH_FRAME_OFFSET|
  77. * | *or put another way* |
  78. * | (BIT(3)|BIT(0)) << FGRAPH_INDEX_SHIFT | \ |
  79. * | FGRAPH_TYPE_BITMAP << FGRAPH_TYPE_SHIFT| \ |
  80. * | (offset1:FGRAPH_FRAME_OFFSET) | <- the offset1 is from here
  81. * +--------------------------------------------+
  82. * | struct ftrace_ret_stack |
  83. * | (stores the saved ret pointer) | <- the offset points here
  84. * +--------------------------------------------+
  85. * | (X) | (N) | ( N words away from
  86. * | | previous ret_stack)
  87. * ...
  88. * ret_stack[0]
  89. *
  90. * If a backtrace is required, and the real return pointer needs to be
  91. * fetched, then it looks at the task's curr_ret_stack offset, if it
  92. * is greater than zero (reserved, or right before popped), it would mask
  93. * the value by FGRAPH_FRAME_OFFSET_MASK to get the offset of the
  94. * ftrace_ret_stack structure stored on the shadow stack.
  95. */
  96. /*
  97. * The following is for the top word on the stack:
  98. *
  99. * FGRAPH_FRAME_OFFSET (0-9) holds the offset delta to the fgraph frame
  100. * FGRAPH_TYPE (10-11) holds the type of word this is.
  101. * (RESERVED or BITMAP)
  102. */
  103. #define FGRAPH_FRAME_OFFSET_BITS 10
  104. #define FGRAPH_FRAME_OFFSET_MASK GENMASK(FGRAPH_FRAME_OFFSET_BITS - 1, 0)
  105. #define FGRAPH_TYPE_BITS 2
  106. #define FGRAPH_TYPE_MASK GENMASK(FGRAPH_TYPE_BITS - 1, 0)
  107. #define FGRAPH_TYPE_SHIFT FGRAPH_FRAME_OFFSET_BITS
  108. enum {
  109. FGRAPH_TYPE_RESERVED = 0,
  110. FGRAPH_TYPE_BITMAP = 1,
  111. FGRAPH_TYPE_DATA = 2,
  112. };
  113. /*
  114. * For BITMAP type:
  115. * FGRAPH_INDEX (12-27) bits holding the gops index wanting return callback called
  116. */
  117. #define FGRAPH_INDEX_BITS 16
  118. #define FGRAPH_INDEX_MASK GENMASK(FGRAPH_INDEX_BITS - 1, 0)
  119. #define FGRAPH_INDEX_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_BITS)
  120. /*
  121. * For DATA type:
  122. * FGRAPH_DATA (12-17) bits hold the size of data (in words)
  123. * FGRAPH_INDEX (18-23) bits hold the index for which gops->idx the data is for
  124. *
  125. * Note:
  126. * data_size == 0 means 1 word, and 31 (=2^5 - 1) means 32 words.
  127. */
  128. #define FGRAPH_DATA_BITS 5
  129. #define FGRAPH_DATA_MASK GENMASK(FGRAPH_DATA_BITS - 1, 0)
  130. #define FGRAPH_DATA_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_BITS)
  131. #define FGRAPH_MAX_DATA_SIZE (sizeof(long) * (1 << FGRAPH_DATA_BITS))
  132. #define FGRAPH_DATA_INDEX_BITS 4
  133. #define FGRAPH_DATA_INDEX_MASK GENMASK(FGRAPH_DATA_INDEX_BITS - 1, 0)
  134. #define FGRAPH_DATA_INDEX_SHIFT (FGRAPH_DATA_SHIFT + FGRAPH_DATA_BITS)
  135. #define FGRAPH_MAX_INDEX \
  136. ((FGRAPH_INDEX_SIZE << FGRAPH_DATA_BITS) + FGRAPH_RET_INDEX)
  137. #define FGRAPH_ARRAY_SIZE FGRAPH_INDEX_BITS
  138. /*
  139. * SHADOW_STACK_SIZE: The size in bytes of the entire shadow stack
  140. * SHADOW_STACK_OFFSET: The size in long words of the shadow stack
  141. * SHADOW_STACK_MAX_OFFSET: The max offset of the stack for a new frame to be added
  142. */
  143. #define SHADOW_STACK_SIZE (PAGE_SIZE)
  144. #define SHADOW_STACK_OFFSET (SHADOW_STACK_SIZE / sizeof(long))
  145. /* Leave on a buffer at the end */
  146. #define SHADOW_STACK_MAX_OFFSET \
  147. (SHADOW_STACK_OFFSET - (FGRAPH_FRAME_OFFSET + 1 + FGRAPH_ARRAY_SIZE))
  148. /* RET_STACK(): Return the frame from a given @offset from task @t */
  149. #define RET_STACK(t, offset) ((struct ftrace_ret_stack *)(&(t)->ret_stack[offset]))
  150. /*
  151. * Each fgraph_ops has a reservered unsigned long at the end (top) of the
  152. * ret_stack to store task specific state.
  153. */
  154. #define SHADOW_STACK_TASK_VARS(ret_stack) \
  155. ((unsigned long *)(&(ret_stack)[SHADOW_STACK_OFFSET - FGRAPH_ARRAY_SIZE]))
  156. DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph);
  157. int ftrace_graph_active;
  158. static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE];
  159. static unsigned long fgraph_array_bitmask;
  160. /* LRU index table for fgraph_array */
  161. static int fgraph_lru_table[FGRAPH_ARRAY_SIZE];
  162. static int fgraph_lru_next;
  163. static int fgraph_lru_last;
  164. /* Initialize fgraph_lru_table with unused index */
  165. static void fgraph_lru_init(void)
  166. {
  167. int i;
  168. for (i = 0; i < FGRAPH_ARRAY_SIZE; i++)
  169. fgraph_lru_table[i] = i;
  170. }
  171. /* Release the used index to the LRU table */
  172. static int fgraph_lru_release_index(int idx)
  173. {
  174. if (idx < 0 || idx >= FGRAPH_ARRAY_SIZE ||
  175. WARN_ON_ONCE(fgraph_lru_table[fgraph_lru_last] != -1))
  176. return -1;
  177. fgraph_lru_table[fgraph_lru_last] = idx;
  178. fgraph_lru_last = (fgraph_lru_last + 1) % FGRAPH_ARRAY_SIZE;
  179. clear_bit(idx, &fgraph_array_bitmask);
  180. return 0;
  181. }
  182. /* Allocate a new index from LRU table */
  183. static int fgraph_lru_alloc_index(void)
  184. {
  185. int idx = fgraph_lru_table[fgraph_lru_next];
  186. /* No id is available */
  187. if (idx == -1)
  188. return -1;
  189. fgraph_lru_table[fgraph_lru_next] = -1;
  190. fgraph_lru_next = (fgraph_lru_next + 1) % FGRAPH_ARRAY_SIZE;
  191. set_bit(idx, &fgraph_array_bitmask);
  192. return idx;
  193. }
  194. /* Get the offset to the fgraph frame from a ret_stack value */
  195. static inline int __get_offset(unsigned long val)
  196. {
  197. return val & FGRAPH_FRAME_OFFSET_MASK;
  198. }
  199. /* Get the type of word from a ret_stack value */
  200. static inline int __get_type(unsigned long val)
  201. {
  202. return (val >> FGRAPH_TYPE_SHIFT) & FGRAPH_TYPE_MASK;
  203. }
  204. /* Get the data_index for a DATA type ret_stack word */
  205. static inline int __get_data_index(unsigned long val)
  206. {
  207. return (val >> FGRAPH_DATA_INDEX_SHIFT) & FGRAPH_DATA_INDEX_MASK;
  208. }
  209. /* Get the data_size for a DATA type ret_stack word */
  210. static inline int __get_data_size(unsigned long val)
  211. {
  212. return ((val >> FGRAPH_DATA_SHIFT) & FGRAPH_DATA_MASK) + 1;
  213. }
  214. /* Get the word from the ret_stack at @offset */
  215. static inline unsigned long get_fgraph_entry(struct task_struct *t, int offset)
  216. {
  217. return t->ret_stack[offset];
  218. }
  219. /* Get the FRAME_OFFSET from the word from the @offset on ret_stack */
  220. static inline int get_frame_offset(struct task_struct *t, int offset)
  221. {
  222. return __get_offset(t->ret_stack[offset]);
  223. }
  224. /* For BITMAP type: get the bitmask from the @offset at ret_stack */
  225. static inline unsigned long
  226. get_bitmap_bits(struct task_struct *t, int offset)
  227. {
  228. return (t->ret_stack[offset] >> FGRAPH_INDEX_SHIFT) & FGRAPH_INDEX_MASK;
  229. }
  230. /* Write the bitmap to the ret_stack at @offset (does index, offset and bitmask) */
  231. static inline void
  232. set_bitmap(struct task_struct *t, int offset, unsigned long bitmap)
  233. {
  234. t->ret_stack[offset] = (bitmap << FGRAPH_INDEX_SHIFT) |
  235. (FGRAPH_TYPE_BITMAP << FGRAPH_TYPE_SHIFT) | FGRAPH_FRAME_OFFSET;
  236. }
  237. /* For DATA type: get the data saved under the ret_stack word at @offset */
  238. static inline void *get_data_type_data(struct task_struct *t, int offset)
  239. {
  240. unsigned long val = t->ret_stack[offset];
  241. if (__get_type(val) != FGRAPH_TYPE_DATA)
  242. return NULL;
  243. offset -= __get_data_size(val);
  244. return (void *)&t->ret_stack[offset];
  245. }
  246. /* Create the ret_stack word for a DATA type */
  247. static inline unsigned long make_data_type_val(int idx, int size, int offset)
  248. {
  249. return (idx << FGRAPH_DATA_INDEX_SHIFT) |
  250. ((size - 1) << FGRAPH_DATA_SHIFT) |
  251. (FGRAPH_TYPE_DATA << FGRAPH_TYPE_SHIFT) | offset;
  252. }
  253. /* ftrace_graph_entry set to this to tell some archs to run function graph */
  254. static int entry_run(struct ftrace_graph_ent *trace, struct fgraph_ops *ops)
  255. {
  256. return 0;
  257. }
  258. /* ftrace_graph_return set to this to tell some archs to run function graph */
  259. static void return_run(struct ftrace_graph_ret *trace, struct fgraph_ops *ops)
  260. {
  261. }
  262. static void ret_stack_set_task_var(struct task_struct *t, int idx, long val)
  263. {
  264. unsigned long *gvals = SHADOW_STACK_TASK_VARS(t->ret_stack);
  265. gvals[idx] = val;
  266. }
  267. static unsigned long *
  268. ret_stack_get_task_var(struct task_struct *t, int idx)
  269. {
  270. unsigned long *gvals = SHADOW_STACK_TASK_VARS(t->ret_stack);
  271. return &gvals[idx];
  272. }
  273. static void ret_stack_init_task_vars(unsigned long *ret_stack)
  274. {
  275. unsigned long *gvals = SHADOW_STACK_TASK_VARS(ret_stack);
  276. memset(gvals, 0, sizeof(*gvals) * FGRAPH_ARRAY_SIZE);
  277. }
  278. /**
  279. * fgraph_reserve_data - Reserve storage on the task's ret_stack
  280. * @idx: The index of fgraph_array
  281. * @size_bytes: The size in bytes to reserve
  282. *
  283. * Reserves space of up to FGRAPH_MAX_DATA_SIZE bytes on the
  284. * task's ret_stack shadow stack, for a given fgraph_ops during
  285. * the entryfunc() call. If entryfunc() returns zero, the storage
  286. * is discarded. An entryfunc() can only call this once per iteration.
  287. * The fgraph_ops retfunc() can retrieve this stored data with
  288. * fgraph_retrieve_data().
  289. *
  290. * Returns: On success, a pointer to the data on the stack.
  291. * Otherwise, NULL if there's not enough space left on the
  292. * ret_stack for the data, or if fgraph_reserve_data() was called
  293. * more than once for a single entryfunc() call.
  294. */
  295. void *fgraph_reserve_data(int idx, int size_bytes)
  296. {
  297. unsigned long val;
  298. void *data;
  299. int curr_ret_stack = current->curr_ret_stack;
  300. int data_size;
  301. if (size_bytes > FGRAPH_MAX_DATA_SIZE)
  302. return NULL;
  303. /* Convert the data size to number of longs. */
  304. data_size = (size_bytes + sizeof(long) - 1) >> (sizeof(long) == 4 ? 2 : 3);
  305. val = get_fgraph_entry(current, curr_ret_stack - 1);
  306. data = &current->ret_stack[curr_ret_stack];
  307. curr_ret_stack += data_size + 1;
  308. if (unlikely(curr_ret_stack >= SHADOW_STACK_MAX_OFFSET))
  309. return NULL;
  310. val = make_data_type_val(idx, data_size, __get_offset(val) + data_size + 1);
  311. /* Set the last word to be reserved */
  312. current->ret_stack[curr_ret_stack - 1] = val;
  313. /* Make sure interrupts see this */
  314. barrier();
  315. current->curr_ret_stack = curr_ret_stack;
  316. /* Again sync with interrupts, and reset reserve */
  317. current->ret_stack[curr_ret_stack - 1] = val;
  318. return data;
  319. }
  320. /**
  321. * fgraph_retrieve_data - Retrieve stored data from fgraph_reserve_data()
  322. * @idx: the index of fgraph_array (fgraph_ops::idx)
  323. * @size_bytes: pointer to retrieved data size.
  324. *
  325. * This is to be called by a fgraph_ops retfunc(), to retrieve data that
  326. * was stored by the fgraph_ops entryfunc() on the function entry.
  327. * That is, this will retrieve the data that was reserved on the
  328. * entry of the function that corresponds to the exit of the function
  329. * that the fgraph_ops retfunc() is called on.
  330. *
  331. * Returns: The stored data from fgraph_reserve_data() called by the
  332. * matching entryfunc() for the retfunc() this is called from.
  333. * Or NULL if there was nothing stored.
  334. */
  335. void *fgraph_retrieve_data(int idx, int *size_bytes)
  336. {
  337. int offset = current->curr_ret_stack - 1;
  338. unsigned long val;
  339. val = get_fgraph_entry(current, offset);
  340. while (__get_type(val) == FGRAPH_TYPE_DATA) {
  341. if (__get_data_index(val) == idx)
  342. goto found;
  343. offset -= __get_data_size(val) + 1;
  344. val = get_fgraph_entry(current, offset);
  345. }
  346. return NULL;
  347. found:
  348. if (size_bytes)
  349. *size_bytes = __get_data_size(val) * sizeof(long);
  350. return get_data_type_data(current, offset);
  351. }
  352. /**
  353. * fgraph_get_task_var - retrieve a task specific state variable
  354. * @gops: The ftrace_ops that owns the task specific variable
  355. *
  356. * Every registered fgraph_ops has a task state variable
  357. * reserved on the task's ret_stack. This function returns the
  358. * address to that variable.
  359. *
  360. * Returns the address to the fgraph_ops @gops tasks specific
  361. * unsigned long variable.
  362. */
  363. unsigned long *fgraph_get_task_var(struct fgraph_ops *gops)
  364. {
  365. return ret_stack_get_task_var(current, gops->idx);
  366. }
  367. /*
  368. * @offset: The offset into @t->ret_stack to find the ret_stack entry
  369. * @frame_offset: Where to place the offset into @t->ret_stack of that entry
  370. *
  371. * Returns a pointer to the previous ret_stack below @offset or NULL
  372. * when it reaches the bottom of the stack.
  373. *
  374. * Calling this with:
  375. *
  376. * offset = task->curr_ret_stack;
  377. * do {
  378. * ret_stack = get_ret_stack(task, offset, &offset);
  379. * } while (ret_stack);
  380. *
  381. * Will iterate through all the ret_stack entries from curr_ret_stack
  382. * down to the first one.
  383. */
  384. static inline struct ftrace_ret_stack *
  385. get_ret_stack(struct task_struct *t, int offset, int *frame_offset)
  386. {
  387. int offs;
  388. BUILD_BUG_ON(FGRAPH_FRAME_SIZE % sizeof(long));
  389. if (unlikely(offset <= 0))
  390. return NULL;
  391. offs = get_frame_offset(t, --offset);
  392. if (WARN_ON_ONCE(offs <= 0 || offs > offset))
  393. return NULL;
  394. offset -= offs;
  395. *frame_offset = offset;
  396. return RET_STACK(t, offset);
  397. }
  398. /* Both enabled by default (can be cleared by function_graph tracer flags */
  399. static bool fgraph_sleep_time = true;
  400. #ifdef CONFIG_DYNAMIC_FTRACE
  401. /*
  402. * archs can override this function if they must do something
  403. * to enable hook for graph tracer.
  404. */
  405. int __weak ftrace_enable_ftrace_graph_caller(void)
  406. {
  407. return 0;
  408. }
  409. /*
  410. * archs can override this function if they must do something
  411. * to disable hook for graph tracer.
  412. */
  413. int __weak ftrace_disable_ftrace_graph_caller(void)
  414. {
  415. return 0;
  416. }
  417. #endif
  418. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace,
  419. struct fgraph_ops *gops)
  420. {
  421. return 0;
  422. }
  423. static void ftrace_graph_ret_stub(struct ftrace_graph_ret *trace,
  424. struct fgraph_ops *gops)
  425. {
  426. }
  427. static struct fgraph_ops fgraph_stub = {
  428. .entryfunc = ftrace_graph_entry_stub,
  429. .retfunc = ftrace_graph_ret_stub,
  430. };
  431. static struct fgraph_ops *fgraph_direct_gops = &fgraph_stub;
  432. DEFINE_STATIC_CALL(fgraph_func, ftrace_graph_entry_stub);
  433. DEFINE_STATIC_CALL(fgraph_retfunc, ftrace_graph_ret_stub);
  434. static DEFINE_STATIC_KEY_TRUE(fgraph_do_direct);
  435. /**
  436. * ftrace_graph_stop - set to permanently disable function graph tracing
  437. *
  438. * In case of an error int function graph tracing, this is called
  439. * to try to keep function graph tracing from causing any more harm.
  440. * Usually this is pretty severe and this is called to try to at least
  441. * get a warning out to the user.
  442. */
  443. void ftrace_graph_stop(void)
  444. {
  445. static_branch_enable(&kill_ftrace_graph);
  446. }
  447. /* Add a function return address to the trace stack on thread info.*/
  448. static int
  449. ftrace_push_return_trace(unsigned long ret, unsigned long func,
  450. unsigned long frame_pointer, unsigned long *retp,
  451. int fgraph_idx)
  452. {
  453. struct ftrace_ret_stack *ret_stack;
  454. unsigned long long calltime;
  455. unsigned long val;
  456. int offset;
  457. if (unlikely(ftrace_graph_is_dead()))
  458. return -EBUSY;
  459. if (!current->ret_stack)
  460. return -EBUSY;
  461. BUILD_BUG_ON(SHADOW_STACK_SIZE % sizeof(long));
  462. /* Set val to "reserved" with the delta to the new fgraph frame */
  463. val = (FGRAPH_TYPE_RESERVED << FGRAPH_TYPE_SHIFT) | FGRAPH_FRAME_OFFSET;
  464. /*
  465. * We must make sure the ret_stack is tested before we read
  466. * anything else.
  467. */
  468. smp_rmb();
  469. /*
  470. * Check if there's room on the shadow stack to fit a fraph frame
  471. * and a bitmap word.
  472. */
  473. if (current->curr_ret_stack + FGRAPH_FRAME_OFFSET + 1 >= SHADOW_STACK_MAX_OFFSET) {
  474. atomic_inc(&current->trace_overrun);
  475. return -EBUSY;
  476. }
  477. calltime = trace_clock_local();
  478. offset = READ_ONCE(current->curr_ret_stack);
  479. ret_stack = RET_STACK(current, offset);
  480. offset += FGRAPH_FRAME_OFFSET;
  481. /* ret offset = FGRAPH_FRAME_OFFSET ; type = reserved */
  482. current->ret_stack[offset] = val;
  483. ret_stack->ret = ret;
  484. /*
  485. * The unwinders expect curr_ret_stack to point to either zero
  486. * or an offset where to find the next ret_stack. Even though the
  487. * ret stack might be bogus, we want to write the ret and the
  488. * offset to find the ret_stack before we increment the stack point.
  489. * If an interrupt comes in now before we increment the curr_ret_stack
  490. * it may blow away what we wrote. But that's fine, because the
  491. * offset will still be correct (even though the 'ret' won't be).
  492. * What we worry about is the offset being correct after we increment
  493. * the curr_ret_stack and before we update that offset, as if an
  494. * interrupt comes in and does an unwind stack dump, it will need
  495. * at least a correct offset!
  496. */
  497. barrier();
  498. WRITE_ONCE(current->curr_ret_stack, offset + 1);
  499. /*
  500. * This next barrier is to ensure that an interrupt coming in
  501. * will not corrupt what we are about to write.
  502. */
  503. barrier();
  504. /* Still keep it reserved even if an interrupt came in */
  505. current->ret_stack[offset] = val;
  506. ret_stack->ret = ret;
  507. ret_stack->func = func;
  508. ret_stack->calltime = calltime;
  509. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  510. ret_stack->fp = frame_pointer;
  511. #endif
  512. ret_stack->retp = retp;
  513. return offset;
  514. }
  515. /*
  516. * Not all archs define MCOUNT_INSN_SIZE which is used to look for direct
  517. * functions. But those archs currently don't support direct functions
  518. * anyway, and ftrace_find_rec_direct() is just a stub for them.
  519. * Define MCOUNT_INSN_SIZE to keep those archs compiling.
  520. */
  521. #ifndef MCOUNT_INSN_SIZE
  522. /* Make sure this only works without direct calls */
  523. # ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  524. # error MCOUNT_INSN_SIZE not defined with direct calls enabled
  525. # endif
  526. # define MCOUNT_INSN_SIZE 0
  527. #endif
  528. /* If the caller does not use ftrace, call this function. */
  529. int function_graph_enter(unsigned long ret, unsigned long func,
  530. unsigned long frame_pointer, unsigned long *retp)
  531. {
  532. struct ftrace_graph_ent trace;
  533. unsigned long bitmap = 0;
  534. int offset;
  535. int i;
  536. trace.func = func;
  537. trace.depth = ++current->curr_ret_depth;
  538. offset = ftrace_push_return_trace(ret, func, frame_pointer, retp, 0);
  539. if (offset < 0)
  540. goto out;
  541. #ifdef CONFIG_HAVE_STATIC_CALL
  542. if (static_branch_likely(&fgraph_do_direct)) {
  543. int save_curr_ret_stack = current->curr_ret_stack;
  544. if (static_call(fgraph_func)(&trace, fgraph_direct_gops))
  545. bitmap |= BIT(fgraph_direct_gops->idx);
  546. else
  547. /* Clear out any saved storage */
  548. current->curr_ret_stack = save_curr_ret_stack;
  549. } else
  550. #endif
  551. {
  552. for_each_set_bit(i, &fgraph_array_bitmask,
  553. sizeof(fgraph_array_bitmask) * BITS_PER_BYTE) {
  554. struct fgraph_ops *gops = READ_ONCE(fgraph_array[i]);
  555. int save_curr_ret_stack;
  556. if (gops == &fgraph_stub)
  557. continue;
  558. save_curr_ret_stack = current->curr_ret_stack;
  559. if (ftrace_ops_test(&gops->ops, func, NULL) &&
  560. gops->entryfunc(&trace, gops))
  561. bitmap |= BIT(i);
  562. else
  563. /* Clear out any saved storage */
  564. current->curr_ret_stack = save_curr_ret_stack;
  565. }
  566. }
  567. if (!bitmap)
  568. goto out_ret;
  569. /*
  570. * Since this function uses fgraph_idx = 0 as a tail-call checking
  571. * flag, set that bit always.
  572. */
  573. set_bitmap(current, offset, bitmap | BIT(0));
  574. return 0;
  575. out_ret:
  576. current->curr_ret_stack -= FGRAPH_FRAME_OFFSET + 1;
  577. out:
  578. current->curr_ret_depth--;
  579. return -EBUSY;
  580. }
  581. /* Retrieve a function return address to the trace stack on thread info.*/
  582. static struct ftrace_ret_stack *
  583. ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
  584. unsigned long frame_pointer, int *offset)
  585. {
  586. struct ftrace_ret_stack *ret_stack;
  587. ret_stack = get_ret_stack(current, current->curr_ret_stack, offset);
  588. if (unlikely(!ret_stack)) {
  589. ftrace_graph_stop();
  590. WARN(1, "Bad function graph ret_stack pointer: %d",
  591. current->curr_ret_stack);
  592. /* Might as well panic, otherwise we have no where to go */
  593. *ret = (unsigned long)panic;
  594. return NULL;
  595. }
  596. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  597. /*
  598. * The arch may choose to record the frame pointer used
  599. * and check it here to make sure that it is what we expect it
  600. * to be. If gcc does not set the place holder of the return
  601. * address in the frame pointer, and does a copy instead, then
  602. * the function graph trace will fail. This test detects this
  603. * case.
  604. *
  605. * Currently, x86_32 with optimize for size (-Os) makes the latest
  606. * gcc do the above.
  607. *
  608. * Note, -mfentry does not use frame pointers, and this test
  609. * is not needed if CC_USING_FENTRY is set.
  610. */
  611. if (unlikely(ret_stack->fp != frame_pointer)) {
  612. ftrace_graph_stop();
  613. WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
  614. " from func %ps return to %lx\n",
  615. ret_stack->fp,
  616. frame_pointer,
  617. (void *)ret_stack->func,
  618. ret_stack->ret);
  619. *ret = (unsigned long)panic;
  620. return NULL;
  621. }
  622. #endif
  623. *offset += FGRAPH_FRAME_OFFSET;
  624. *ret = ret_stack->ret;
  625. trace->func = ret_stack->func;
  626. trace->calltime = ret_stack->calltime;
  627. trace->overrun = atomic_read(&current->trace_overrun);
  628. trace->depth = current->curr_ret_depth;
  629. /*
  630. * We still want to trace interrupts coming in if
  631. * max_depth is set to 1. Make sure the decrement is
  632. * seen before ftrace_graph_return.
  633. */
  634. barrier();
  635. return ret_stack;
  636. }
  637. /*
  638. * Hibernation protection.
  639. * The state of the current task is too much unstable during
  640. * suspend/restore to disk. We want to protect against that.
  641. */
  642. static int
  643. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  644. void *unused)
  645. {
  646. switch (state) {
  647. case PM_HIBERNATION_PREPARE:
  648. pause_graph_tracing();
  649. break;
  650. case PM_POST_HIBERNATION:
  651. unpause_graph_tracing();
  652. break;
  653. }
  654. return NOTIFY_DONE;
  655. }
  656. static struct notifier_block ftrace_suspend_notifier = {
  657. .notifier_call = ftrace_suspend_notifier_call,
  658. };
  659. /* fgraph_ret_regs is not defined without CONFIG_FUNCTION_GRAPH_RETVAL */
  660. struct fgraph_ret_regs;
  661. /*
  662. * Send the trace to the ring-buffer.
  663. * @return the original return address.
  664. */
  665. static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs,
  666. unsigned long frame_pointer)
  667. {
  668. struct ftrace_ret_stack *ret_stack;
  669. struct ftrace_graph_ret trace;
  670. unsigned long bitmap;
  671. unsigned long ret;
  672. int offset;
  673. int i;
  674. ret_stack = ftrace_pop_return_trace(&trace, &ret, frame_pointer, &offset);
  675. if (unlikely(!ret_stack)) {
  676. ftrace_graph_stop();
  677. WARN_ON(1);
  678. /* Might as well panic. What else to do? */
  679. return (unsigned long)panic;
  680. }
  681. trace.rettime = trace_clock_local();
  682. #ifdef CONFIG_FUNCTION_GRAPH_RETVAL
  683. trace.retval = fgraph_ret_regs_return_value(ret_regs);
  684. #endif
  685. bitmap = get_bitmap_bits(current, offset);
  686. #ifdef CONFIG_HAVE_STATIC_CALL
  687. if (static_branch_likely(&fgraph_do_direct)) {
  688. if (test_bit(fgraph_direct_gops->idx, &bitmap))
  689. static_call(fgraph_retfunc)(&trace, fgraph_direct_gops);
  690. } else
  691. #endif
  692. {
  693. for_each_set_bit(i, &bitmap, sizeof(bitmap) * BITS_PER_BYTE) {
  694. struct fgraph_ops *gops = READ_ONCE(fgraph_array[i]);
  695. if (gops == &fgraph_stub)
  696. continue;
  697. gops->retfunc(&trace, gops);
  698. }
  699. }
  700. /*
  701. * The ftrace_graph_return() may still access the current
  702. * ret_stack structure, we need to make sure the update of
  703. * curr_ret_stack is after that.
  704. */
  705. barrier();
  706. current->curr_ret_stack = offset - FGRAPH_FRAME_OFFSET;
  707. current->curr_ret_depth--;
  708. return ret;
  709. }
  710. /*
  711. * After all architecures have selected HAVE_FUNCTION_GRAPH_RETVAL, we can
  712. * leave only ftrace_return_to_handler(ret_regs).
  713. */
  714. #ifdef CONFIG_HAVE_FUNCTION_GRAPH_RETVAL
  715. unsigned long ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs)
  716. {
  717. return __ftrace_return_to_handler(ret_regs,
  718. fgraph_ret_regs_frame_pointer(ret_regs));
  719. }
  720. #else
  721. unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
  722. {
  723. return __ftrace_return_to_handler(NULL, frame_pointer);
  724. }
  725. #endif
  726. /**
  727. * ftrace_graph_get_ret_stack - return the entry of the shadow stack
  728. * @task: The task to read the shadow stack from.
  729. * @idx: Index down the shadow stack
  730. *
  731. * Return the ret_struct on the shadow stack of the @task at the
  732. * call graph at @idx starting with zero. If @idx is zero, it
  733. * will return the last saved ret_stack entry. If it is greater than
  734. * zero, it will return the corresponding ret_stack for the depth
  735. * of saved return addresses.
  736. */
  737. struct ftrace_ret_stack *
  738. ftrace_graph_get_ret_stack(struct task_struct *task, int idx)
  739. {
  740. struct ftrace_ret_stack *ret_stack = NULL;
  741. int offset = task->curr_ret_stack;
  742. if (offset < 0)
  743. return NULL;
  744. do {
  745. ret_stack = get_ret_stack(task, offset, &offset);
  746. } while (ret_stack && --idx >= 0);
  747. return ret_stack;
  748. }
  749. /**
  750. * ftrace_graph_ret_addr - return the original value of the return address
  751. * @task: The task the unwinder is being executed on
  752. * @idx: An initialized pointer to the next stack index to use
  753. * @ret: The current return address (likely pointing to return_handler)
  754. * @retp: The address on the stack of the current return location
  755. *
  756. * This function can be called by stack unwinding code to convert a found stack
  757. * return address (@ret) to its original value, in case the function graph
  758. * tracer has modified it to be 'return_to_handler'. If the address hasn't
  759. * been modified, the unchanged value of @ret is returned.
  760. *
  761. * @idx holds the last index used to know where to start from. It should be
  762. * initialized to zero for the first iteration as that will mean to start
  763. * at the top of the shadow stack. If the location is found, this pointer
  764. * will be assigned that location so that if called again, it will continue
  765. * where it left off.
  766. *
  767. * @retp is a pointer to the return address on the stack.
  768. */
  769. unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
  770. unsigned long ret, unsigned long *retp)
  771. {
  772. struct ftrace_ret_stack *ret_stack;
  773. unsigned long return_handler = (unsigned long)dereference_kernel_function_descriptor(return_to_handler);
  774. int i = task->curr_ret_stack;
  775. if (ret != return_handler)
  776. return ret;
  777. if (!idx)
  778. return ret;
  779. i = *idx ? : task->curr_ret_stack;
  780. while (i > 0) {
  781. ret_stack = get_ret_stack(task, i, &i);
  782. if (!ret_stack)
  783. break;
  784. /*
  785. * For the tail-call, there would be 2 or more ftrace_ret_stacks on
  786. * the ret_stack, which records "return_to_handler" as the return
  787. * address except for the last one.
  788. * But on the real stack, there should be 1 entry because tail-call
  789. * reuses the return address on the stack and jump to the next function.
  790. * Thus we will continue to find real return address.
  791. */
  792. if (ret_stack->retp == retp &&
  793. ret_stack->ret != return_handler) {
  794. *idx = i;
  795. return ret_stack->ret;
  796. }
  797. }
  798. return ret;
  799. }
  800. static struct ftrace_ops graph_ops = {
  801. .func = ftrace_graph_func,
  802. .flags = FTRACE_OPS_GRAPH_STUB,
  803. #ifdef FTRACE_GRAPH_TRAMP_ADDR
  804. .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
  805. /* trampoline_size is only needed for dynamically allocated tramps */
  806. #endif
  807. };
  808. void fgraph_init_ops(struct ftrace_ops *dst_ops,
  809. struct ftrace_ops *src_ops)
  810. {
  811. dst_ops->flags = FTRACE_OPS_FL_PID | FTRACE_OPS_GRAPH_STUB;
  812. #ifdef CONFIG_DYNAMIC_FTRACE
  813. if (src_ops) {
  814. dst_ops->func_hash = &src_ops->local_hash;
  815. mutex_init(&dst_ops->local_hash.regex_lock);
  816. INIT_LIST_HEAD(&dst_ops->subop_list);
  817. dst_ops->flags |= FTRACE_OPS_FL_INITIALIZED;
  818. }
  819. #endif
  820. }
  821. void ftrace_graph_sleep_time_control(bool enable)
  822. {
  823. fgraph_sleep_time = enable;
  824. }
  825. /*
  826. * Simply points to ftrace_stub, but with the proper protocol.
  827. * Defined by the linker script in linux/vmlinux.lds.h
  828. */
  829. void ftrace_stub_graph(struct ftrace_graph_ret *trace, struct fgraph_ops *gops);
  830. /* The callbacks that hook a function */
  831. trace_func_graph_ret_t ftrace_graph_return = ftrace_stub_graph;
  832. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  833. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  834. static int alloc_retstack_tasklist(unsigned long **ret_stack_list)
  835. {
  836. int i;
  837. int ret = 0;
  838. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  839. struct task_struct *g, *t;
  840. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  841. ret_stack_list[i] = kmalloc(SHADOW_STACK_SIZE, GFP_KERNEL);
  842. if (!ret_stack_list[i]) {
  843. start = 0;
  844. end = i;
  845. ret = -ENOMEM;
  846. goto free;
  847. }
  848. }
  849. rcu_read_lock();
  850. for_each_process_thread(g, t) {
  851. if (start == end) {
  852. ret = -EAGAIN;
  853. goto unlock;
  854. }
  855. if (t->ret_stack == NULL) {
  856. atomic_set(&t->trace_overrun, 0);
  857. ret_stack_init_task_vars(ret_stack_list[start]);
  858. t->curr_ret_stack = 0;
  859. t->curr_ret_depth = -1;
  860. /* Make sure the tasks see the 0 first: */
  861. smp_wmb();
  862. t->ret_stack = ret_stack_list[start++];
  863. }
  864. }
  865. unlock:
  866. rcu_read_unlock();
  867. free:
  868. for (i = start; i < end; i++)
  869. kfree(ret_stack_list[i]);
  870. return ret;
  871. }
  872. static void
  873. ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
  874. struct task_struct *prev,
  875. struct task_struct *next,
  876. unsigned int prev_state)
  877. {
  878. struct ftrace_ret_stack *ret_stack;
  879. unsigned long long timestamp;
  880. int offset;
  881. /*
  882. * Does the user want to count the time a function was asleep.
  883. * If so, do not update the time stamps.
  884. */
  885. if (fgraph_sleep_time)
  886. return;
  887. timestamp = trace_clock_local();
  888. prev->ftrace_timestamp = timestamp;
  889. /* only process tasks that we timestamped */
  890. if (!next->ftrace_timestamp)
  891. return;
  892. /*
  893. * Update all the counters in next to make up for the
  894. * time next was sleeping.
  895. */
  896. timestamp -= next->ftrace_timestamp;
  897. for (offset = next->curr_ret_stack; offset > 0; ) {
  898. ret_stack = get_ret_stack(next, offset, &offset);
  899. if (ret_stack)
  900. ret_stack->calltime += timestamp;
  901. }
  902. }
  903. static DEFINE_PER_CPU(unsigned long *, idle_ret_stack);
  904. static void
  905. graph_init_task(struct task_struct *t, unsigned long *ret_stack)
  906. {
  907. atomic_set(&t->trace_overrun, 0);
  908. ret_stack_init_task_vars(ret_stack);
  909. t->ftrace_timestamp = 0;
  910. t->curr_ret_stack = 0;
  911. t->curr_ret_depth = -1;
  912. /* make curr_ret_stack visible before we add the ret_stack */
  913. smp_wmb();
  914. t->ret_stack = ret_stack;
  915. }
  916. /*
  917. * Allocate a return stack for the idle task. May be the first
  918. * time through, or it may be done by CPU hotplug online.
  919. */
  920. void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
  921. {
  922. t->curr_ret_stack = 0;
  923. t->curr_ret_depth = -1;
  924. /*
  925. * The idle task has no parent, it either has its own
  926. * stack or no stack at all.
  927. */
  928. if (t->ret_stack)
  929. WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
  930. if (ftrace_graph_active) {
  931. unsigned long *ret_stack;
  932. ret_stack = per_cpu(idle_ret_stack, cpu);
  933. if (!ret_stack) {
  934. ret_stack = kmalloc(SHADOW_STACK_SIZE, GFP_KERNEL);
  935. if (!ret_stack)
  936. return;
  937. per_cpu(idle_ret_stack, cpu) = ret_stack;
  938. }
  939. graph_init_task(t, ret_stack);
  940. }
  941. }
  942. /* Allocate a return stack for newly created task */
  943. void ftrace_graph_init_task(struct task_struct *t)
  944. {
  945. /* Make sure we do not use the parent ret_stack */
  946. t->ret_stack = NULL;
  947. t->curr_ret_stack = 0;
  948. t->curr_ret_depth = -1;
  949. if (ftrace_graph_active) {
  950. unsigned long *ret_stack;
  951. ret_stack = kmalloc(SHADOW_STACK_SIZE, GFP_KERNEL);
  952. if (!ret_stack)
  953. return;
  954. graph_init_task(t, ret_stack);
  955. }
  956. }
  957. void ftrace_graph_exit_task(struct task_struct *t)
  958. {
  959. unsigned long *ret_stack = t->ret_stack;
  960. t->ret_stack = NULL;
  961. /* NULL must become visible to IRQs before we free it: */
  962. barrier();
  963. kfree(ret_stack);
  964. }
  965. #ifdef CONFIG_DYNAMIC_FTRACE
  966. static int fgraph_pid_func(struct ftrace_graph_ent *trace,
  967. struct fgraph_ops *gops)
  968. {
  969. struct trace_array *tr = gops->ops.private;
  970. int pid;
  971. if (tr) {
  972. pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid);
  973. if (pid == FTRACE_PID_IGNORE)
  974. return 0;
  975. if (pid != FTRACE_PID_TRACE &&
  976. pid != current->pid)
  977. return 0;
  978. }
  979. return gops->saved_func(trace, gops);
  980. }
  981. void fgraph_update_pid_func(void)
  982. {
  983. struct fgraph_ops *gops;
  984. struct ftrace_ops *op;
  985. if (!(graph_ops.flags & FTRACE_OPS_FL_INITIALIZED))
  986. return;
  987. list_for_each_entry(op, &graph_ops.subop_list, list) {
  988. if (op->flags & FTRACE_OPS_FL_PID) {
  989. gops = container_of(op, struct fgraph_ops, ops);
  990. gops->entryfunc = ftrace_pids_enabled(op) ?
  991. fgraph_pid_func : gops->saved_func;
  992. if (ftrace_graph_active == 1)
  993. static_call_update(fgraph_func, gops->entryfunc);
  994. }
  995. }
  996. }
  997. #endif
  998. /* Allocate a return stack for each task */
  999. static int start_graph_tracing(void)
  1000. {
  1001. unsigned long **ret_stack_list;
  1002. int ret, cpu;
  1003. ret_stack_list = kcalloc(FTRACE_RETSTACK_ALLOC_SIZE,
  1004. sizeof(*ret_stack_list), GFP_KERNEL);
  1005. if (!ret_stack_list)
  1006. return -ENOMEM;
  1007. /* The cpu_boot init_task->ret_stack will never be freed */
  1008. for_each_online_cpu(cpu) {
  1009. if (!idle_task(cpu)->ret_stack)
  1010. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  1011. }
  1012. do {
  1013. ret = alloc_retstack_tasklist(ret_stack_list);
  1014. } while (ret == -EAGAIN);
  1015. if (!ret) {
  1016. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  1017. if (ret)
  1018. pr_info("ftrace_graph: Couldn't activate tracepoint"
  1019. " probe to kernel_sched_switch\n");
  1020. }
  1021. kfree(ret_stack_list);
  1022. return ret;
  1023. }
  1024. static void init_task_vars(int idx)
  1025. {
  1026. struct task_struct *g, *t;
  1027. int cpu;
  1028. for_each_online_cpu(cpu) {
  1029. if (idle_task(cpu)->ret_stack)
  1030. ret_stack_set_task_var(idle_task(cpu), idx, 0);
  1031. }
  1032. read_lock(&tasklist_lock);
  1033. for_each_process_thread(g, t) {
  1034. if (t->ret_stack)
  1035. ret_stack_set_task_var(t, idx, 0);
  1036. }
  1037. read_unlock(&tasklist_lock);
  1038. }
  1039. static void ftrace_graph_enable_direct(bool enable_branch, struct fgraph_ops *gops)
  1040. {
  1041. trace_func_graph_ent_t func = NULL;
  1042. trace_func_graph_ret_t retfunc = NULL;
  1043. int i;
  1044. if (gops) {
  1045. func = gops->entryfunc;
  1046. retfunc = gops->retfunc;
  1047. fgraph_direct_gops = gops;
  1048. } else {
  1049. for_each_set_bit(i, &fgraph_array_bitmask,
  1050. sizeof(fgraph_array_bitmask) * BITS_PER_BYTE) {
  1051. func = fgraph_array[i]->entryfunc;
  1052. retfunc = fgraph_array[i]->retfunc;
  1053. fgraph_direct_gops = fgraph_array[i];
  1054. }
  1055. }
  1056. if (WARN_ON_ONCE(!func))
  1057. return;
  1058. static_call_update(fgraph_func, func);
  1059. static_call_update(fgraph_retfunc, retfunc);
  1060. if (enable_branch)
  1061. static_branch_disable(&fgraph_do_direct);
  1062. }
  1063. static void ftrace_graph_disable_direct(bool disable_branch)
  1064. {
  1065. if (disable_branch)
  1066. static_branch_disable(&fgraph_do_direct);
  1067. static_call_update(fgraph_func, ftrace_graph_entry_stub);
  1068. static_call_update(fgraph_retfunc, ftrace_graph_ret_stub);
  1069. fgraph_direct_gops = &fgraph_stub;
  1070. }
  1071. /* The cpu_boot init_task->ret_stack will never be freed */
  1072. static int fgraph_cpu_init(unsigned int cpu)
  1073. {
  1074. if (!idle_task(cpu)->ret_stack)
  1075. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  1076. return 0;
  1077. }
  1078. int register_ftrace_graph(struct fgraph_ops *gops)
  1079. {
  1080. static bool fgraph_initialized;
  1081. int command = 0;
  1082. int ret = 0;
  1083. int i = -1;
  1084. guard(mutex)(&ftrace_lock);
  1085. if (!fgraph_initialized) {
  1086. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "fgraph:online",
  1087. fgraph_cpu_init, NULL);
  1088. if (ret < 0) {
  1089. pr_warn("fgraph: Error to init cpu hotplug support\n");
  1090. return ret;
  1091. }
  1092. fgraph_initialized = true;
  1093. ret = 0;
  1094. }
  1095. if (!fgraph_array[0]) {
  1096. /* The array must always have real data on it */
  1097. for (i = 0; i < FGRAPH_ARRAY_SIZE; i++)
  1098. fgraph_array[i] = &fgraph_stub;
  1099. fgraph_lru_init();
  1100. }
  1101. i = fgraph_lru_alloc_index();
  1102. if (i < 0 || WARN_ON_ONCE(fgraph_array[i] != &fgraph_stub))
  1103. return -ENOSPC;
  1104. gops->idx = i;
  1105. ftrace_graph_active++;
  1106. if (ftrace_graph_active == 2)
  1107. ftrace_graph_disable_direct(true);
  1108. if (ftrace_graph_active == 1) {
  1109. ftrace_graph_enable_direct(false, gops);
  1110. register_pm_notifier(&ftrace_suspend_notifier);
  1111. ret = start_graph_tracing();
  1112. if (ret)
  1113. goto error;
  1114. /*
  1115. * Some archs just test to see if these are not
  1116. * the default function
  1117. */
  1118. ftrace_graph_return = return_run;
  1119. ftrace_graph_entry = entry_run;
  1120. command = FTRACE_START_FUNC_RET;
  1121. } else {
  1122. init_task_vars(gops->idx);
  1123. }
  1124. /* Always save the function, and reset at unregistering */
  1125. gops->saved_func = gops->entryfunc;
  1126. ret = ftrace_startup_subops(&graph_ops, &gops->ops, command);
  1127. if (!ret)
  1128. fgraph_array[i] = gops;
  1129. error:
  1130. if (ret) {
  1131. ftrace_graph_active--;
  1132. gops->saved_func = NULL;
  1133. fgraph_lru_release_index(i);
  1134. if (!ftrace_graph_active)
  1135. unregister_pm_notifier(&ftrace_suspend_notifier);
  1136. }
  1137. return ret;
  1138. }
  1139. void unregister_ftrace_graph(struct fgraph_ops *gops)
  1140. {
  1141. int command = 0;
  1142. mutex_lock(&ftrace_lock);
  1143. if (unlikely(!ftrace_graph_active))
  1144. goto out;
  1145. if (unlikely(gops->idx < 0 || gops->idx >= FGRAPH_ARRAY_SIZE ||
  1146. fgraph_array[gops->idx] != gops))
  1147. goto out;
  1148. if (fgraph_lru_release_index(gops->idx) < 0)
  1149. goto out;
  1150. fgraph_array[gops->idx] = &fgraph_stub;
  1151. ftrace_graph_active--;
  1152. if (!ftrace_graph_active)
  1153. command = FTRACE_STOP_FUNC_RET;
  1154. ftrace_shutdown_subops(&graph_ops, &gops->ops, command);
  1155. if (ftrace_graph_active == 1)
  1156. ftrace_graph_enable_direct(true, NULL);
  1157. else if (!ftrace_graph_active)
  1158. ftrace_graph_disable_direct(false);
  1159. if (!ftrace_graph_active) {
  1160. ftrace_graph_return = ftrace_stub_graph;
  1161. ftrace_graph_entry = ftrace_graph_entry_stub;
  1162. unregister_pm_notifier(&ftrace_suspend_notifier);
  1163. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  1164. }
  1165. out:
  1166. gops->saved_func = NULL;
  1167. mutex_unlock(&ftrace_lock);
  1168. }