task_iter.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2020 Facebook */
  3. #include <linux/init.h>
  4. #include <linux/namei.h>
  5. #include <linux/pid_namespace.h>
  6. #include <linux/fs.h>
  7. #include <linux/fdtable.h>
  8. #include <linux/filter.h>
  9. #include <linux/bpf_mem_alloc.h>
  10. #include <linux/btf_ids.h>
  11. #include <linux/mm_types.h>
  12. #include "mmap_unlock_work.h"
  13. static const char * const iter_task_type_names[] = {
  14. "ALL",
  15. "TID",
  16. "PID",
  17. };
  18. struct bpf_iter_seq_task_common {
  19. struct pid_namespace *ns;
  20. enum bpf_iter_task_type type;
  21. u32 pid;
  22. u32 pid_visiting;
  23. };
  24. struct bpf_iter_seq_task_info {
  25. /* The first field must be struct bpf_iter_seq_task_common.
  26. * this is assumed by {init, fini}_seq_pidns() callback functions.
  27. */
  28. struct bpf_iter_seq_task_common common;
  29. u32 tid;
  30. };
  31. static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_common *common,
  32. u32 *tid,
  33. bool skip_if_dup_files)
  34. {
  35. struct task_struct *task;
  36. struct pid *pid;
  37. u32 next_tid;
  38. if (!*tid) {
  39. /* The first time, the iterator calls this function. */
  40. pid = find_pid_ns(common->pid, common->ns);
  41. task = get_pid_task(pid, PIDTYPE_TGID);
  42. if (!task)
  43. return NULL;
  44. *tid = common->pid;
  45. common->pid_visiting = common->pid;
  46. return task;
  47. }
  48. /* If the control returns to user space and comes back to the
  49. * kernel again, *tid and common->pid_visiting should be the
  50. * same for task_seq_start() to pick up the correct task.
  51. */
  52. if (*tid == common->pid_visiting) {
  53. pid = find_pid_ns(common->pid_visiting, common->ns);
  54. task = get_pid_task(pid, PIDTYPE_PID);
  55. return task;
  56. }
  57. task = find_task_by_pid_ns(common->pid_visiting, common->ns);
  58. if (!task)
  59. return NULL;
  60. retry:
  61. task = __next_thread(task);
  62. if (!task)
  63. return NULL;
  64. next_tid = __task_pid_nr_ns(task, PIDTYPE_PID, common->ns);
  65. if (!next_tid)
  66. goto retry;
  67. if (skip_if_dup_files && task->files == task->group_leader->files)
  68. goto retry;
  69. *tid = common->pid_visiting = next_tid;
  70. get_task_struct(task);
  71. return task;
  72. }
  73. static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
  74. u32 *tid,
  75. bool skip_if_dup_files)
  76. {
  77. struct task_struct *task = NULL;
  78. struct pid *pid;
  79. if (common->type == BPF_TASK_ITER_TID) {
  80. if (*tid && *tid != common->pid)
  81. return NULL;
  82. rcu_read_lock();
  83. pid = find_pid_ns(common->pid, common->ns);
  84. if (pid) {
  85. task = get_pid_task(pid, PIDTYPE_PID);
  86. *tid = common->pid;
  87. }
  88. rcu_read_unlock();
  89. return task;
  90. }
  91. if (common->type == BPF_TASK_ITER_TGID) {
  92. rcu_read_lock();
  93. task = task_group_seq_get_next(common, tid, skip_if_dup_files);
  94. rcu_read_unlock();
  95. return task;
  96. }
  97. rcu_read_lock();
  98. retry:
  99. pid = find_ge_pid(*tid, common->ns);
  100. if (pid) {
  101. *tid = pid_nr_ns(pid, common->ns);
  102. task = get_pid_task(pid, PIDTYPE_PID);
  103. if (!task) {
  104. ++*tid;
  105. goto retry;
  106. } else if (skip_if_dup_files && !thread_group_leader(task) &&
  107. task->files == task->group_leader->files) {
  108. put_task_struct(task);
  109. task = NULL;
  110. ++*tid;
  111. goto retry;
  112. }
  113. }
  114. rcu_read_unlock();
  115. return task;
  116. }
  117. static void *task_seq_start(struct seq_file *seq, loff_t *pos)
  118. {
  119. struct bpf_iter_seq_task_info *info = seq->private;
  120. struct task_struct *task;
  121. task = task_seq_get_next(&info->common, &info->tid, false);
  122. if (!task)
  123. return NULL;
  124. if (*pos == 0)
  125. ++*pos;
  126. return task;
  127. }
  128. static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  129. {
  130. struct bpf_iter_seq_task_info *info = seq->private;
  131. struct task_struct *task;
  132. ++*pos;
  133. ++info->tid;
  134. put_task_struct((struct task_struct *)v);
  135. task = task_seq_get_next(&info->common, &info->tid, false);
  136. if (!task)
  137. return NULL;
  138. return task;
  139. }
  140. struct bpf_iter__task {
  141. __bpf_md_ptr(struct bpf_iter_meta *, meta);
  142. __bpf_md_ptr(struct task_struct *, task);
  143. };
  144. DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
  145. static int __task_seq_show(struct seq_file *seq, struct task_struct *task,
  146. bool in_stop)
  147. {
  148. struct bpf_iter_meta meta;
  149. struct bpf_iter__task ctx;
  150. struct bpf_prog *prog;
  151. meta.seq = seq;
  152. prog = bpf_iter_get_info(&meta, in_stop);
  153. if (!prog)
  154. return 0;
  155. ctx.meta = &meta;
  156. ctx.task = task;
  157. return bpf_iter_run_prog(prog, &ctx);
  158. }
  159. static int task_seq_show(struct seq_file *seq, void *v)
  160. {
  161. return __task_seq_show(seq, v, false);
  162. }
  163. static void task_seq_stop(struct seq_file *seq, void *v)
  164. {
  165. if (!v)
  166. (void)__task_seq_show(seq, v, true);
  167. else
  168. put_task_struct((struct task_struct *)v);
  169. }
  170. static int bpf_iter_attach_task(struct bpf_prog *prog,
  171. union bpf_iter_link_info *linfo,
  172. struct bpf_iter_aux_info *aux)
  173. {
  174. unsigned int flags;
  175. struct pid *pid;
  176. pid_t tgid;
  177. if ((!!linfo->task.tid + !!linfo->task.pid + !!linfo->task.pid_fd) > 1)
  178. return -EINVAL;
  179. aux->task.type = BPF_TASK_ITER_ALL;
  180. if (linfo->task.tid != 0) {
  181. aux->task.type = BPF_TASK_ITER_TID;
  182. aux->task.pid = linfo->task.tid;
  183. }
  184. if (linfo->task.pid != 0) {
  185. aux->task.type = BPF_TASK_ITER_TGID;
  186. aux->task.pid = linfo->task.pid;
  187. }
  188. if (linfo->task.pid_fd != 0) {
  189. aux->task.type = BPF_TASK_ITER_TGID;
  190. pid = pidfd_get_pid(linfo->task.pid_fd, &flags);
  191. if (IS_ERR(pid))
  192. return PTR_ERR(pid);
  193. tgid = pid_nr_ns(pid, task_active_pid_ns(current));
  194. aux->task.pid = tgid;
  195. put_pid(pid);
  196. }
  197. return 0;
  198. }
  199. static const struct seq_operations task_seq_ops = {
  200. .start = task_seq_start,
  201. .next = task_seq_next,
  202. .stop = task_seq_stop,
  203. .show = task_seq_show,
  204. };
  205. struct bpf_iter_seq_task_file_info {
  206. /* The first field must be struct bpf_iter_seq_task_common.
  207. * this is assumed by {init, fini}_seq_pidns() callback functions.
  208. */
  209. struct bpf_iter_seq_task_common common;
  210. struct task_struct *task;
  211. u32 tid;
  212. u32 fd;
  213. };
  214. static struct file *
  215. task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
  216. {
  217. u32 saved_tid = info->tid;
  218. struct task_struct *curr_task;
  219. unsigned int curr_fd = info->fd;
  220. struct file *f;
  221. /* If this function returns a non-NULL file object,
  222. * it held a reference to the task/file.
  223. * Otherwise, it does not hold any reference.
  224. */
  225. again:
  226. if (info->task) {
  227. curr_task = info->task;
  228. curr_fd = info->fd;
  229. } else {
  230. curr_task = task_seq_get_next(&info->common, &info->tid, true);
  231. if (!curr_task) {
  232. info->task = NULL;
  233. return NULL;
  234. }
  235. /* set info->task */
  236. info->task = curr_task;
  237. if (saved_tid == info->tid)
  238. curr_fd = info->fd;
  239. else
  240. curr_fd = 0;
  241. }
  242. rcu_read_lock();
  243. f = task_lookup_next_fdget_rcu(curr_task, &curr_fd);
  244. if (f) {
  245. /* set info->fd */
  246. info->fd = curr_fd;
  247. rcu_read_unlock();
  248. return f;
  249. }
  250. /* the current task is done, go to the next task */
  251. rcu_read_unlock();
  252. put_task_struct(curr_task);
  253. if (info->common.type == BPF_TASK_ITER_TID) {
  254. info->task = NULL;
  255. return NULL;
  256. }
  257. info->task = NULL;
  258. info->fd = 0;
  259. saved_tid = ++(info->tid);
  260. goto again;
  261. }
  262. static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
  263. {
  264. struct bpf_iter_seq_task_file_info *info = seq->private;
  265. struct file *file;
  266. info->task = NULL;
  267. file = task_file_seq_get_next(info);
  268. if (file && *pos == 0)
  269. ++*pos;
  270. return file;
  271. }
  272. static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  273. {
  274. struct bpf_iter_seq_task_file_info *info = seq->private;
  275. ++*pos;
  276. ++info->fd;
  277. fput((struct file *)v);
  278. return task_file_seq_get_next(info);
  279. }
  280. struct bpf_iter__task_file {
  281. __bpf_md_ptr(struct bpf_iter_meta *, meta);
  282. __bpf_md_ptr(struct task_struct *, task);
  283. u32 fd __aligned(8);
  284. __bpf_md_ptr(struct file *, file);
  285. };
  286. DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
  287. struct task_struct *task, u32 fd,
  288. struct file *file)
  289. static int __task_file_seq_show(struct seq_file *seq, struct file *file,
  290. bool in_stop)
  291. {
  292. struct bpf_iter_seq_task_file_info *info = seq->private;
  293. struct bpf_iter__task_file ctx;
  294. struct bpf_iter_meta meta;
  295. struct bpf_prog *prog;
  296. meta.seq = seq;
  297. prog = bpf_iter_get_info(&meta, in_stop);
  298. if (!prog)
  299. return 0;
  300. ctx.meta = &meta;
  301. ctx.task = info->task;
  302. ctx.fd = info->fd;
  303. ctx.file = file;
  304. return bpf_iter_run_prog(prog, &ctx);
  305. }
  306. static int task_file_seq_show(struct seq_file *seq, void *v)
  307. {
  308. return __task_file_seq_show(seq, v, false);
  309. }
  310. static void task_file_seq_stop(struct seq_file *seq, void *v)
  311. {
  312. struct bpf_iter_seq_task_file_info *info = seq->private;
  313. if (!v) {
  314. (void)__task_file_seq_show(seq, v, true);
  315. } else {
  316. fput((struct file *)v);
  317. put_task_struct(info->task);
  318. info->task = NULL;
  319. }
  320. }
  321. static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
  322. {
  323. struct bpf_iter_seq_task_common *common = priv_data;
  324. common->ns = get_pid_ns(task_active_pid_ns(current));
  325. common->type = aux->task.type;
  326. common->pid = aux->task.pid;
  327. return 0;
  328. }
  329. static void fini_seq_pidns(void *priv_data)
  330. {
  331. struct bpf_iter_seq_task_common *common = priv_data;
  332. put_pid_ns(common->ns);
  333. }
  334. static const struct seq_operations task_file_seq_ops = {
  335. .start = task_file_seq_start,
  336. .next = task_file_seq_next,
  337. .stop = task_file_seq_stop,
  338. .show = task_file_seq_show,
  339. };
  340. struct bpf_iter_seq_task_vma_info {
  341. /* The first field must be struct bpf_iter_seq_task_common.
  342. * this is assumed by {init, fini}_seq_pidns() callback functions.
  343. */
  344. struct bpf_iter_seq_task_common common;
  345. struct task_struct *task;
  346. struct mm_struct *mm;
  347. struct vm_area_struct *vma;
  348. u32 tid;
  349. unsigned long prev_vm_start;
  350. unsigned long prev_vm_end;
  351. };
  352. enum bpf_task_vma_iter_find_op {
  353. task_vma_iter_first_vma, /* use find_vma() with addr 0 */
  354. task_vma_iter_next_vma, /* use vma_next() with curr_vma */
  355. task_vma_iter_find_vma, /* use find_vma() to find next vma */
  356. };
  357. static struct vm_area_struct *
  358. task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info *info)
  359. {
  360. enum bpf_task_vma_iter_find_op op;
  361. struct vm_area_struct *curr_vma;
  362. struct task_struct *curr_task;
  363. struct mm_struct *curr_mm;
  364. u32 saved_tid = info->tid;
  365. /* If this function returns a non-NULL vma, it holds a reference to
  366. * the task_struct, holds a refcount on mm->mm_users, and holds
  367. * read lock on vma->mm->mmap_lock.
  368. * If this function returns NULL, it does not hold any reference or
  369. * lock.
  370. */
  371. if (info->task) {
  372. curr_task = info->task;
  373. curr_vma = info->vma;
  374. curr_mm = info->mm;
  375. /* In case of lock contention, drop mmap_lock to unblock
  376. * the writer.
  377. *
  378. * After relock, call find(mm, prev_vm_end - 1) to find
  379. * new vma to process.
  380. *
  381. * +------+------+-----------+
  382. * | VMA1 | VMA2 | VMA3 |
  383. * +------+------+-----------+
  384. * | | | |
  385. * 4k 8k 16k 400k
  386. *
  387. * For example, curr_vma == VMA2. Before unlock, we set
  388. *
  389. * prev_vm_start = 8k
  390. * prev_vm_end = 16k
  391. *
  392. * There are a few cases:
  393. *
  394. * 1) VMA2 is freed, but VMA3 exists.
  395. *
  396. * find_vma() will return VMA3, just process VMA3.
  397. *
  398. * 2) VMA2 still exists.
  399. *
  400. * find_vma() will return VMA2, process VMA2->next.
  401. *
  402. * 3) no more vma in this mm.
  403. *
  404. * Process the next task.
  405. *
  406. * 4) find_vma() returns a different vma, VMA2'.
  407. *
  408. * 4.1) If VMA2 covers same range as VMA2', skip VMA2',
  409. * because we already covered the range;
  410. * 4.2) VMA2 and VMA2' covers different ranges, process
  411. * VMA2'.
  412. */
  413. if (mmap_lock_is_contended(curr_mm)) {
  414. info->prev_vm_start = curr_vma->vm_start;
  415. info->prev_vm_end = curr_vma->vm_end;
  416. op = task_vma_iter_find_vma;
  417. mmap_read_unlock(curr_mm);
  418. if (mmap_read_lock_killable(curr_mm)) {
  419. mmput(curr_mm);
  420. goto finish;
  421. }
  422. } else {
  423. op = task_vma_iter_next_vma;
  424. }
  425. } else {
  426. again:
  427. curr_task = task_seq_get_next(&info->common, &info->tid, true);
  428. if (!curr_task) {
  429. info->tid++;
  430. goto finish;
  431. }
  432. if (saved_tid != info->tid) {
  433. /* new task, process the first vma */
  434. op = task_vma_iter_first_vma;
  435. } else {
  436. /* Found the same tid, which means the user space
  437. * finished data in previous buffer and read more.
  438. * We dropped mmap_lock before returning to user
  439. * space, so it is necessary to use find_vma() to
  440. * find the next vma to process.
  441. */
  442. op = task_vma_iter_find_vma;
  443. }
  444. curr_mm = get_task_mm(curr_task);
  445. if (!curr_mm)
  446. goto next_task;
  447. if (mmap_read_lock_killable(curr_mm)) {
  448. mmput(curr_mm);
  449. goto finish;
  450. }
  451. }
  452. switch (op) {
  453. case task_vma_iter_first_vma:
  454. curr_vma = find_vma(curr_mm, 0);
  455. break;
  456. case task_vma_iter_next_vma:
  457. curr_vma = find_vma(curr_mm, curr_vma->vm_end);
  458. break;
  459. case task_vma_iter_find_vma:
  460. /* We dropped mmap_lock so it is necessary to use find_vma
  461. * to find the next vma. This is similar to the mechanism
  462. * in show_smaps_rollup().
  463. */
  464. curr_vma = find_vma(curr_mm, info->prev_vm_end - 1);
  465. /* case 1) and 4.2) above just use curr_vma */
  466. /* check for case 2) or case 4.1) above */
  467. if (curr_vma &&
  468. curr_vma->vm_start == info->prev_vm_start &&
  469. curr_vma->vm_end == info->prev_vm_end)
  470. curr_vma = find_vma(curr_mm, curr_vma->vm_end);
  471. break;
  472. }
  473. if (!curr_vma) {
  474. /* case 3) above, or case 2) 4.1) with vma->next == NULL */
  475. mmap_read_unlock(curr_mm);
  476. mmput(curr_mm);
  477. goto next_task;
  478. }
  479. info->task = curr_task;
  480. info->vma = curr_vma;
  481. info->mm = curr_mm;
  482. return curr_vma;
  483. next_task:
  484. if (info->common.type == BPF_TASK_ITER_TID)
  485. goto finish;
  486. put_task_struct(curr_task);
  487. info->task = NULL;
  488. info->mm = NULL;
  489. info->tid++;
  490. goto again;
  491. finish:
  492. if (curr_task)
  493. put_task_struct(curr_task);
  494. info->task = NULL;
  495. info->vma = NULL;
  496. info->mm = NULL;
  497. return NULL;
  498. }
  499. static void *task_vma_seq_start(struct seq_file *seq, loff_t *pos)
  500. {
  501. struct bpf_iter_seq_task_vma_info *info = seq->private;
  502. struct vm_area_struct *vma;
  503. vma = task_vma_seq_get_next(info);
  504. if (vma && *pos == 0)
  505. ++*pos;
  506. return vma;
  507. }
  508. static void *task_vma_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  509. {
  510. struct bpf_iter_seq_task_vma_info *info = seq->private;
  511. ++*pos;
  512. return task_vma_seq_get_next(info);
  513. }
  514. struct bpf_iter__task_vma {
  515. __bpf_md_ptr(struct bpf_iter_meta *, meta);
  516. __bpf_md_ptr(struct task_struct *, task);
  517. __bpf_md_ptr(struct vm_area_struct *, vma);
  518. };
  519. DEFINE_BPF_ITER_FUNC(task_vma, struct bpf_iter_meta *meta,
  520. struct task_struct *task, struct vm_area_struct *vma)
  521. static int __task_vma_seq_show(struct seq_file *seq, bool in_stop)
  522. {
  523. struct bpf_iter_seq_task_vma_info *info = seq->private;
  524. struct bpf_iter__task_vma ctx;
  525. struct bpf_iter_meta meta;
  526. struct bpf_prog *prog;
  527. meta.seq = seq;
  528. prog = bpf_iter_get_info(&meta, in_stop);
  529. if (!prog)
  530. return 0;
  531. ctx.meta = &meta;
  532. ctx.task = info->task;
  533. ctx.vma = info->vma;
  534. return bpf_iter_run_prog(prog, &ctx);
  535. }
  536. static int task_vma_seq_show(struct seq_file *seq, void *v)
  537. {
  538. return __task_vma_seq_show(seq, false);
  539. }
  540. static void task_vma_seq_stop(struct seq_file *seq, void *v)
  541. {
  542. struct bpf_iter_seq_task_vma_info *info = seq->private;
  543. if (!v) {
  544. (void)__task_vma_seq_show(seq, true);
  545. } else {
  546. /* info->vma has not been seen by the BPF program. If the
  547. * user space reads more, task_vma_seq_get_next should
  548. * return this vma again. Set prev_vm_start to ~0UL,
  549. * so that we don't skip the vma returned by the next
  550. * find_vma() (case task_vma_iter_find_vma in
  551. * task_vma_seq_get_next()).
  552. */
  553. info->prev_vm_start = ~0UL;
  554. info->prev_vm_end = info->vma->vm_end;
  555. mmap_read_unlock(info->mm);
  556. mmput(info->mm);
  557. info->mm = NULL;
  558. put_task_struct(info->task);
  559. info->task = NULL;
  560. }
  561. }
  562. static const struct seq_operations task_vma_seq_ops = {
  563. .start = task_vma_seq_start,
  564. .next = task_vma_seq_next,
  565. .stop = task_vma_seq_stop,
  566. .show = task_vma_seq_show,
  567. };
  568. static const struct bpf_iter_seq_info task_seq_info = {
  569. .seq_ops = &task_seq_ops,
  570. .init_seq_private = init_seq_pidns,
  571. .fini_seq_private = fini_seq_pidns,
  572. .seq_priv_size = sizeof(struct bpf_iter_seq_task_info),
  573. };
  574. static int bpf_iter_fill_link_info(const struct bpf_iter_aux_info *aux, struct bpf_link_info *info)
  575. {
  576. switch (aux->task.type) {
  577. case BPF_TASK_ITER_TID:
  578. info->iter.task.tid = aux->task.pid;
  579. break;
  580. case BPF_TASK_ITER_TGID:
  581. info->iter.task.pid = aux->task.pid;
  582. break;
  583. default:
  584. break;
  585. }
  586. return 0;
  587. }
  588. static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, struct seq_file *seq)
  589. {
  590. seq_printf(seq, "task_type:\t%s\n", iter_task_type_names[aux->task.type]);
  591. if (aux->task.type == BPF_TASK_ITER_TID)
  592. seq_printf(seq, "tid:\t%u\n", aux->task.pid);
  593. else if (aux->task.type == BPF_TASK_ITER_TGID)
  594. seq_printf(seq, "pid:\t%u\n", aux->task.pid);
  595. }
  596. static struct bpf_iter_reg task_reg_info = {
  597. .target = "task",
  598. .attach_target = bpf_iter_attach_task,
  599. .feature = BPF_ITER_RESCHED,
  600. .ctx_arg_info_size = 1,
  601. .ctx_arg_info = {
  602. { offsetof(struct bpf_iter__task, task),
  603. PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED },
  604. },
  605. .seq_info = &task_seq_info,
  606. .fill_link_info = bpf_iter_fill_link_info,
  607. .show_fdinfo = bpf_iter_task_show_fdinfo,
  608. };
  609. static const struct bpf_iter_seq_info task_file_seq_info = {
  610. .seq_ops = &task_file_seq_ops,
  611. .init_seq_private = init_seq_pidns,
  612. .fini_seq_private = fini_seq_pidns,
  613. .seq_priv_size = sizeof(struct bpf_iter_seq_task_file_info),
  614. };
  615. static struct bpf_iter_reg task_file_reg_info = {
  616. .target = "task_file",
  617. .attach_target = bpf_iter_attach_task,
  618. .feature = BPF_ITER_RESCHED,
  619. .ctx_arg_info_size = 2,
  620. .ctx_arg_info = {
  621. { offsetof(struct bpf_iter__task_file, task),
  622. PTR_TO_BTF_ID_OR_NULL },
  623. { offsetof(struct bpf_iter__task_file, file),
  624. PTR_TO_BTF_ID_OR_NULL },
  625. },
  626. .seq_info = &task_file_seq_info,
  627. .fill_link_info = bpf_iter_fill_link_info,
  628. .show_fdinfo = bpf_iter_task_show_fdinfo,
  629. };
  630. static const struct bpf_iter_seq_info task_vma_seq_info = {
  631. .seq_ops = &task_vma_seq_ops,
  632. .init_seq_private = init_seq_pidns,
  633. .fini_seq_private = fini_seq_pidns,
  634. .seq_priv_size = sizeof(struct bpf_iter_seq_task_vma_info),
  635. };
  636. static struct bpf_iter_reg task_vma_reg_info = {
  637. .target = "task_vma",
  638. .attach_target = bpf_iter_attach_task,
  639. .feature = BPF_ITER_RESCHED,
  640. .ctx_arg_info_size = 2,
  641. .ctx_arg_info = {
  642. { offsetof(struct bpf_iter__task_vma, task),
  643. PTR_TO_BTF_ID_OR_NULL },
  644. { offsetof(struct bpf_iter__task_vma, vma),
  645. PTR_TO_BTF_ID_OR_NULL },
  646. },
  647. .seq_info = &task_vma_seq_info,
  648. .fill_link_info = bpf_iter_fill_link_info,
  649. .show_fdinfo = bpf_iter_task_show_fdinfo,
  650. };
  651. BPF_CALL_5(bpf_find_vma, struct task_struct *, task, u64, start,
  652. bpf_callback_t, callback_fn, void *, callback_ctx, u64, flags)
  653. {
  654. struct mmap_unlock_irq_work *work = NULL;
  655. struct vm_area_struct *vma;
  656. bool irq_work_busy = false;
  657. struct mm_struct *mm;
  658. int ret = -ENOENT;
  659. if (flags)
  660. return -EINVAL;
  661. if (!task)
  662. return -ENOENT;
  663. mm = task->mm;
  664. if (!mm)
  665. return -ENOENT;
  666. irq_work_busy = bpf_mmap_unlock_get_irq_work(&work);
  667. if (irq_work_busy || !mmap_read_trylock(mm))
  668. return -EBUSY;
  669. vma = find_vma(mm, start);
  670. if (vma && vma->vm_start <= start && vma->vm_end > start) {
  671. callback_fn((u64)(long)task, (u64)(long)vma,
  672. (u64)(long)callback_ctx, 0, 0);
  673. ret = 0;
  674. }
  675. bpf_mmap_unlock_mm(work, mm);
  676. return ret;
  677. }
  678. const struct bpf_func_proto bpf_find_vma_proto = {
  679. .func = bpf_find_vma,
  680. .ret_type = RET_INTEGER,
  681. .arg1_type = ARG_PTR_TO_BTF_ID,
  682. .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
  683. .arg2_type = ARG_ANYTHING,
  684. .arg3_type = ARG_PTR_TO_FUNC,
  685. .arg4_type = ARG_PTR_TO_STACK_OR_NULL,
  686. .arg5_type = ARG_ANYTHING,
  687. };
  688. struct bpf_iter_task_vma_kern_data {
  689. struct task_struct *task;
  690. struct mm_struct *mm;
  691. struct mmap_unlock_irq_work *work;
  692. struct vma_iterator vmi;
  693. };
  694. struct bpf_iter_task_vma {
  695. /* opaque iterator state; having __u64 here allows to preserve correct
  696. * alignment requirements in vmlinux.h, generated from BTF
  697. */
  698. __u64 __opaque[1];
  699. } __attribute__((aligned(8)));
  700. /* Non-opaque version of bpf_iter_task_vma */
  701. struct bpf_iter_task_vma_kern {
  702. struct bpf_iter_task_vma_kern_data *data;
  703. } __attribute__((aligned(8)));
  704. __bpf_kfunc_start_defs();
  705. __bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,
  706. struct task_struct *task, u64 addr)
  707. {
  708. struct bpf_iter_task_vma_kern *kit = (void *)it;
  709. bool irq_work_busy = false;
  710. int err;
  711. BUILD_BUG_ON(sizeof(struct bpf_iter_task_vma_kern) != sizeof(struct bpf_iter_task_vma));
  712. BUILD_BUG_ON(__alignof__(struct bpf_iter_task_vma_kern) != __alignof__(struct bpf_iter_task_vma));
  713. /* is_iter_reg_valid_uninit guarantees that kit hasn't been initialized
  714. * before, so non-NULL kit->data doesn't point to previously
  715. * bpf_mem_alloc'd bpf_iter_task_vma_kern_data
  716. */
  717. kit->data = bpf_mem_alloc(&bpf_global_ma, sizeof(struct bpf_iter_task_vma_kern_data));
  718. if (!kit->data)
  719. return -ENOMEM;
  720. kit->data->task = get_task_struct(task);
  721. kit->data->mm = task->mm;
  722. if (!kit->data->mm) {
  723. err = -ENOENT;
  724. goto err_cleanup_iter;
  725. }
  726. /* kit->data->work == NULL is valid after bpf_mmap_unlock_get_irq_work */
  727. irq_work_busy = bpf_mmap_unlock_get_irq_work(&kit->data->work);
  728. if (irq_work_busy || !mmap_read_trylock(kit->data->mm)) {
  729. err = -EBUSY;
  730. goto err_cleanup_iter;
  731. }
  732. vma_iter_init(&kit->data->vmi, kit->data->mm, addr);
  733. return 0;
  734. err_cleanup_iter:
  735. if (kit->data->task)
  736. put_task_struct(kit->data->task);
  737. bpf_mem_free(&bpf_global_ma, kit->data);
  738. /* NULL kit->data signals failed bpf_iter_task_vma initialization */
  739. kit->data = NULL;
  740. return err;
  741. }
  742. __bpf_kfunc struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_vma *it)
  743. {
  744. struct bpf_iter_task_vma_kern *kit = (void *)it;
  745. if (!kit->data) /* bpf_iter_task_vma_new failed */
  746. return NULL;
  747. return vma_next(&kit->data->vmi);
  748. }
  749. __bpf_kfunc void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it)
  750. {
  751. struct bpf_iter_task_vma_kern *kit = (void *)it;
  752. if (kit->data) {
  753. bpf_mmap_unlock_mm(kit->data->work, kit->data->mm);
  754. put_task_struct(kit->data->task);
  755. bpf_mem_free(&bpf_global_ma, kit->data);
  756. }
  757. }
  758. __bpf_kfunc_end_defs();
  759. #ifdef CONFIG_CGROUPS
  760. struct bpf_iter_css_task {
  761. __u64 __opaque[1];
  762. } __attribute__((aligned(8)));
  763. struct bpf_iter_css_task_kern {
  764. struct css_task_iter *css_it;
  765. } __attribute__((aligned(8)));
  766. __bpf_kfunc_start_defs();
  767. __bpf_kfunc int bpf_iter_css_task_new(struct bpf_iter_css_task *it,
  768. struct cgroup_subsys_state *css, unsigned int flags)
  769. {
  770. struct bpf_iter_css_task_kern *kit = (void *)it;
  771. BUILD_BUG_ON(sizeof(struct bpf_iter_css_task_kern) != sizeof(struct bpf_iter_css_task));
  772. BUILD_BUG_ON(__alignof__(struct bpf_iter_css_task_kern) !=
  773. __alignof__(struct bpf_iter_css_task));
  774. kit->css_it = NULL;
  775. switch (flags) {
  776. case CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED:
  777. case CSS_TASK_ITER_PROCS:
  778. case 0:
  779. break;
  780. default:
  781. return -EINVAL;
  782. }
  783. kit->css_it = bpf_mem_alloc(&bpf_global_ma, sizeof(struct css_task_iter));
  784. if (!kit->css_it)
  785. return -ENOMEM;
  786. css_task_iter_start(css, flags, kit->css_it);
  787. return 0;
  788. }
  789. __bpf_kfunc struct task_struct *bpf_iter_css_task_next(struct bpf_iter_css_task *it)
  790. {
  791. struct bpf_iter_css_task_kern *kit = (void *)it;
  792. if (!kit->css_it)
  793. return NULL;
  794. return css_task_iter_next(kit->css_it);
  795. }
  796. __bpf_kfunc void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it)
  797. {
  798. struct bpf_iter_css_task_kern *kit = (void *)it;
  799. if (!kit->css_it)
  800. return;
  801. css_task_iter_end(kit->css_it);
  802. bpf_mem_free(&bpf_global_ma, kit->css_it);
  803. }
  804. __bpf_kfunc_end_defs();
  805. #endif /* CONFIG_CGROUPS */
  806. struct bpf_iter_task {
  807. __u64 __opaque[3];
  808. } __attribute__((aligned(8)));
  809. struct bpf_iter_task_kern {
  810. struct task_struct *task;
  811. struct task_struct *pos;
  812. unsigned int flags;
  813. } __attribute__((aligned(8)));
  814. enum {
  815. /* all process in the system */
  816. BPF_TASK_ITER_ALL_PROCS,
  817. /* all threads in the system */
  818. BPF_TASK_ITER_ALL_THREADS,
  819. /* all threads of a specific process */
  820. BPF_TASK_ITER_PROC_THREADS
  821. };
  822. __bpf_kfunc_start_defs();
  823. __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,
  824. struct task_struct *task__nullable, unsigned int flags)
  825. {
  826. struct bpf_iter_task_kern *kit = (void *)it;
  827. BUILD_BUG_ON(sizeof(struct bpf_iter_task_kern) > sizeof(struct bpf_iter_task));
  828. BUILD_BUG_ON(__alignof__(struct bpf_iter_task_kern) !=
  829. __alignof__(struct bpf_iter_task));
  830. kit->pos = NULL;
  831. switch (flags) {
  832. case BPF_TASK_ITER_ALL_THREADS:
  833. case BPF_TASK_ITER_ALL_PROCS:
  834. break;
  835. case BPF_TASK_ITER_PROC_THREADS:
  836. if (!task__nullable)
  837. return -EINVAL;
  838. break;
  839. default:
  840. return -EINVAL;
  841. }
  842. if (flags == BPF_TASK_ITER_PROC_THREADS)
  843. kit->task = task__nullable;
  844. else
  845. kit->task = &init_task;
  846. kit->pos = kit->task;
  847. kit->flags = flags;
  848. return 0;
  849. }
  850. __bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it)
  851. {
  852. struct bpf_iter_task_kern *kit = (void *)it;
  853. struct task_struct *pos;
  854. unsigned int flags;
  855. flags = kit->flags;
  856. pos = kit->pos;
  857. if (!pos)
  858. return pos;
  859. if (flags == BPF_TASK_ITER_ALL_PROCS)
  860. goto get_next_task;
  861. kit->pos = __next_thread(kit->pos);
  862. if (kit->pos || flags == BPF_TASK_ITER_PROC_THREADS)
  863. return pos;
  864. get_next_task:
  865. kit->task = next_task(kit->task);
  866. if (kit->task == &init_task)
  867. kit->pos = NULL;
  868. else
  869. kit->pos = kit->task;
  870. return pos;
  871. }
  872. __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it)
  873. {
  874. }
  875. __bpf_kfunc_end_defs();
  876. DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);
  877. static void do_mmap_read_unlock(struct irq_work *entry)
  878. {
  879. struct mmap_unlock_irq_work *work;
  880. if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
  881. return;
  882. work = container_of(entry, struct mmap_unlock_irq_work, irq_work);
  883. mmap_read_unlock_non_owner(work->mm);
  884. }
  885. static int __init task_iter_init(void)
  886. {
  887. struct mmap_unlock_irq_work *work;
  888. int ret, cpu;
  889. for_each_possible_cpu(cpu) {
  890. work = per_cpu_ptr(&mmap_unlock_work, cpu);
  891. init_irq_work(&work->irq_work, do_mmap_read_unlock);
  892. }
  893. task_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
  894. ret = bpf_iter_reg_target(&task_reg_info);
  895. if (ret)
  896. return ret;
  897. task_file_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
  898. task_file_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_FILE];
  899. ret = bpf_iter_reg_target(&task_file_reg_info);
  900. if (ret)
  901. return ret;
  902. task_vma_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
  903. task_vma_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA];
  904. return bpf_iter_reg_target(&task_vma_reg_info);
  905. }
  906. late_initcall(task_iter_init);