relay.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*
  2. * Public API and common code for kernel->userspace relay file support.
  3. *
  4. * See Documentation/filesystems/relay.txt for an overview.
  5. *
  6. * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
  7. * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
  8. *
  9. * Moved to kernel/relay.c by Paul Mundt, 2006.
  10. * November 2006 - CPU hotplug support by Mathieu Desnoyers
  11. * (mathieu.desnoyers@polymtl.ca)
  12. *
  13. * This file is released under the GPL.
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/stddef.h>
  17. #include <linux/slab.h>
  18. #include <linux/export.h>
  19. #include <linux/string.h>
  20. #include <linux/relay.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/mm.h>
  23. #include <linux/cpu.h>
  24. #include <linux/splice.h>
  25. /* list of open channels, for cpu hotplug */
  26. static DEFINE_MUTEX(relay_channels_mutex);
  27. static LIST_HEAD(relay_channels);
  28. /*
  29. * close() vm_op implementation for relay file mapping.
  30. */
  31. static void relay_file_mmap_close(struct vm_area_struct *vma)
  32. {
  33. struct rchan_buf *buf = vma->vm_private_data;
  34. buf->chan->cb->buf_unmapped(buf, vma->vm_file);
  35. }
  36. /*
  37. * fault() vm_op implementation for relay file mapping.
  38. */
  39. static vm_fault_t relay_buf_fault(struct vm_fault *vmf)
  40. {
  41. struct page *page;
  42. struct rchan_buf *buf = vmf->vma->vm_private_data;
  43. pgoff_t pgoff = vmf->pgoff;
  44. if (!buf)
  45. return VM_FAULT_OOM;
  46. page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT));
  47. if (!page)
  48. return VM_FAULT_SIGBUS;
  49. get_page(page);
  50. vmf->page = page;
  51. return 0;
  52. }
  53. /*
  54. * vm_ops for relay file mappings.
  55. */
  56. static const struct vm_operations_struct relay_file_mmap_ops = {
  57. .fault = relay_buf_fault,
  58. .close = relay_file_mmap_close,
  59. };
  60. /*
  61. * allocate an array of pointers of struct page
  62. */
  63. static struct page **relay_alloc_page_array(unsigned int n_pages)
  64. {
  65. const size_t pa_size = n_pages * sizeof(struct page *);
  66. if (pa_size > PAGE_SIZE)
  67. return vzalloc(pa_size);
  68. return kzalloc(pa_size, GFP_KERNEL);
  69. }
  70. /*
  71. * free an array of pointers of struct page
  72. */
  73. static void relay_free_page_array(struct page **array)
  74. {
  75. kvfree(array);
  76. }
  77. /**
  78. * relay_mmap_buf: - mmap channel buffer to process address space
  79. * @buf: relay channel buffer
  80. * @vma: vm_area_struct describing memory to be mapped
  81. *
  82. * Returns 0 if ok, negative on error
  83. *
  84. * Caller should already have grabbed mmap_sem.
  85. */
  86. static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
  87. {
  88. unsigned long length = vma->vm_end - vma->vm_start;
  89. struct file *filp = vma->vm_file;
  90. if (!buf)
  91. return -EBADF;
  92. if (length != (unsigned long)buf->chan->alloc_size)
  93. return -EINVAL;
  94. vma->vm_ops = &relay_file_mmap_ops;
  95. vma->vm_flags |= VM_DONTEXPAND;
  96. vma->vm_private_data = buf;
  97. buf->chan->cb->buf_mapped(buf, filp);
  98. return 0;
  99. }
  100. /**
  101. * relay_alloc_buf - allocate a channel buffer
  102. * @buf: the buffer struct
  103. * @size: total size of the buffer
  104. *
  105. * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
  106. * passed in size will get page aligned, if it isn't already.
  107. */
  108. static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
  109. {
  110. void *mem;
  111. unsigned int i, j, n_pages;
  112. *size = PAGE_ALIGN(*size);
  113. n_pages = *size >> PAGE_SHIFT;
  114. buf->page_array = relay_alloc_page_array(n_pages);
  115. if (!buf->page_array)
  116. return NULL;
  117. for (i = 0; i < n_pages; i++) {
  118. buf->page_array[i] = alloc_page(GFP_KERNEL);
  119. if (unlikely(!buf->page_array[i]))
  120. goto depopulate;
  121. set_page_private(buf->page_array[i], (unsigned long)buf);
  122. }
  123. mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
  124. if (!mem)
  125. goto depopulate;
  126. memset(mem, 0, *size);
  127. buf->page_count = n_pages;
  128. return mem;
  129. depopulate:
  130. for (j = 0; j < i; j++)
  131. __free_page(buf->page_array[j]);
  132. relay_free_page_array(buf->page_array);
  133. return NULL;
  134. }
  135. /**
  136. * relay_create_buf - allocate and initialize a channel buffer
  137. * @chan: the relay channel
  138. *
  139. * Returns channel buffer if successful, %NULL otherwise.
  140. */
  141. static struct rchan_buf *relay_create_buf(struct rchan *chan)
  142. {
  143. struct rchan_buf *buf;
  144. if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t *))
  145. return NULL;
  146. buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
  147. if (!buf)
  148. return NULL;
  149. buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t *),
  150. GFP_KERNEL);
  151. if (!buf->padding)
  152. goto free_buf;
  153. buf->start = relay_alloc_buf(buf, &chan->alloc_size);
  154. if (!buf->start)
  155. goto free_buf;
  156. buf->chan = chan;
  157. kref_get(&buf->chan->kref);
  158. return buf;
  159. free_buf:
  160. kfree(buf->padding);
  161. kfree(buf);
  162. return NULL;
  163. }
  164. /**
  165. * relay_destroy_channel - free the channel struct
  166. * @kref: target kernel reference that contains the relay channel
  167. *
  168. * Should only be called from kref_put().
  169. */
  170. static void relay_destroy_channel(struct kref *kref)
  171. {
  172. struct rchan *chan = container_of(kref, struct rchan, kref);
  173. free_percpu(chan->buf);
  174. kfree(chan);
  175. }
  176. /**
  177. * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
  178. * @buf: the buffer struct
  179. */
  180. static void relay_destroy_buf(struct rchan_buf *buf)
  181. {
  182. struct rchan *chan = buf->chan;
  183. unsigned int i;
  184. if (likely(buf->start)) {
  185. vunmap(buf->start);
  186. for (i = 0; i < buf->page_count; i++)
  187. __free_page(buf->page_array[i]);
  188. relay_free_page_array(buf->page_array);
  189. }
  190. *per_cpu_ptr(chan->buf, buf->cpu) = NULL;
  191. kfree(buf->padding);
  192. kfree(buf);
  193. kref_put(&chan->kref, relay_destroy_channel);
  194. }
  195. /**
  196. * relay_remove_buf - remove a channel buffer
  197. * @kref: target kernel reference that contains the relay buffer
  198. *
  199. * Removes the file from the filesystem, which also frees the
  200. * rchan_buf_struct and the channel buffer. Should only be called from
  201. * kref_put().
  202. */
  203. static void relay_remove_buf(struct kref *kref)
  204. {
  205. struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
  206. relay_destroy_buf(buf);
  207. }
  208. /**
  209. * relay_buf_empty - boolean, is the channel buffer empty?
  210. * @buf: channel buffer
  211. *
  212. * Returns 1 if the buffer is empty, 0 otherwise.
  213. */
  214. static int relay_buf_empty(struct rchan_buf *buf)
  215. {
  216. return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
  217. }
  218. /**
  219. * relay_buf_full - boolean, is the channel buffer full?
  220. * @buf: channel buffer
  221. *
  222. * Returns 1 if the buffer is full, 0 otherwise.
  223. */
  224. int relay_buf_full(struct rchan_buf *buf)
  225. {
  226. size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
  227. return (ready >= buf->chan->n_subbufs) ? 1 : 0;
  228. }
  229. EXPORT_SYMBOL_GPL(relay_buf_full);
  230. /*
  231. * High-level relay kernel API and associated functions.
  232. */
  233. /*
  234. * rchan_callback implementations defining default channel behavior. Used
  235. * in place of corresponding NULL values in client callback struct.
  236. */
  237. /*
  238. * subbuf_start() default callback. Does nothing.
  239. */
  240. static int subbuf_start_default_callback (struct rchan_buf *buf,
  241. void *subbuf,
  242. void *prev_subbuf,
  243. size_t prev_padding)
  244. {
  245. if (relay_buf_full(buf))
  246. return 0;
  247. return 1;
  248. }
  249. /*
  250. * buf_mapped() default callback. Does nothing.
  251. */
  252. static void buf_mapped_default_callback(struct rchan_buf *buf,
  253. struct file *filp)
  254. {
  255. }
  256. /*
  257. * buf_unmapped() default callback. Does nothing.
  258. */
  259. static void buf_unmapped_default_callback(struct rchan_buf *buf,
  260. struct file *filp)
  261. {
  262. }
  263. /*
  264. * create_buf_file_create() default callback. Does nothing.
  265. */
  266. static struct dentry *create_buf_file_default_callback(const char *filename,
  267. struct dentry *parent,
  268. umode_t mode,
  269. struct rchan_buf *buf,
  270. int *is_global)
  271. {
  272. return NULL;
  273. }
  274. /*
  275. * remove_buf_file() default callback. Does nothing.
  276. */
  277. static int remove_buf_file_default_callback(struct dentry *dentry)
  278. {
  279. return -EINVAL;
  280. }
  281. /* relay channel default callbacks */
  282. static struct rchan_callbacks default_channel_callbacks = {
  283. .subbuf_start = subbuf_start_default_callback,
  284. .buf_mapped = buf_mapped_default_callback,
  285. .buf_unmapped = buf_unmapped_default_callback,
  286. .create_buf_file = create_buf_file_default_callback,
  287. .remove_buf_file = remove_buf_file_default_callback,
  288. };
  289. /**
  290. * wakeup_readers - wake up readers waiting on a channel
  291. * @work: contains the channel buffer
  292. *
  293. * This is the function used to defer reader waking
  294. */
  295. static void wakeup_readers(struct irq_work *work)
  296. {
  297. struct rchan_buf *buf;
  298. buf = container_of(work, struct rchan_buf, wakeup_work);
  299. wake_up_interruptible(&buf->read_wait);
  300. }
  301. /**
  302. * __relay_reset - reset a channel buffer
  303. * @buf: the channel buffer
  304. * @init: 1 if this is a first-time initialization
  305. *
  306. * See relay_reset() for description of effect.
  307. */
  308. static void __relay_reset(struct rchan_buf *buf, unsigned int init)
  309. {
  310. size_t i;
  311. if (init) {
  312. init_waitqueue_head(&buf->read_wait);
  313. kref_init(&buf->kref);
  314. init_irq_work(&buf->wakeup_work, wakeup_readers);
  315. } else {
  316. irq_work_sync(&buf->wakeup_work);
  317. }
  318. buf->subbufs_produced = 0;
  319. buf->subbufs_consumed = 0;
  320. buf->bytes_consumed = 0;
  321. buf->finalized = 0;
  322. buf->data = buf->start;
  323. buf->offset = 0;
  324. for (i = 0; i < buf->chan->n_subbufs; i++)
  325. buf->padding[i] = 0;
  326. buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
  327. }
  328. /**
  329. * relay_reset - reset the channel
  330. * @chan: the channel
  331. *
  332. * This has the effect of erasing all data from all channel buffers
  333. * and restarting the channel in its initial state. The buffers
  334. * are not freed, so any mappings are still in effect.
  335. *
  336. * NOTE. Care should be taken that the channel isn't actually
  337. * being used by anything when this call is made.
  338. */
  339. void relay_reset(struct rchan *chan)
  340. {
  341. struct rchan_buf *buf;
  342. unsigned int i;
  343. if (!chan)
  344. return;
  345. if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
  346. __relay_reset(buf, 0);
  347. return;
  348. }
  349. mutex_lock(&relay_channels_mutex);
  350. for_each_possible_cpu(i)
  351. if ((buf = *per_cpu_ptr(chan->buf, i)))
  352. __relay_reset(buf, 0);
  353. mutex_unlock(&relay_channels_mutex);
  354. }
  355. EXPORT_SYMBOL_GPL(relay_reset);
  356. static inline void relay_set_buf_dentry(struct rchan_buf *buf,
  357. struct dentry *dentry)
  358. {
  359. buf->dentry = dentry;
  360. d_inode(buf->dentry)->i_size = buf->early_bytes;
  361. }
  362. static struct dentry *relay_create_buf_file(struct rchan *chan,
  363. struct rchan_buf *buf,
  364. unsigned int cpu)
  365. {
  366. struct dentry *dentry;
  367. char *tmpname;
  368. tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
  369. if (!tmpname)
  370. return NULL;
  371. snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
  372. /* Create file in fs */
  373. dentry = chan->cb->create_buf_file(tmpname, chan->parent,
  374. S_IRUSR, buf,
  375. &chan->is_global);
  376. if (IS_ERR(dentry))
  377. dentry = NULL;
  378. kfree(tmpname);
  379. return dentry;
  380. }
  381. /*
  382. * relay_open_buf - create a new relay channel buffer
  383. *
  384. * used by relay_open() and CPU hotplug.
  385. */
  386. static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
  387. {
  388. struct rchan_buf *buf = NULL;
  389. struct dentry *dentry;
  390. if (chan->is_global)
  391. return *per_cpu_ptr(chan->buf, 0);
  392. buf = relay_create_buf(chan);
  393. if (!buf)
  394. return NULL;
  395. if (chan->has_base_filename) {
  396. dentry = relay_create_buf_file(chan, buf, cpu);
  397. if (!dentry)
  398. goto free_buf;
  399. relay_set_buf_dentry(buf, dentry);
  400. } else {
  401. /* Only retrieve global info, nothing more, nothing less */
  402. dentry = chan->cb->create_buf_file(NULL, NULL,
  403. S_IRUSR, buf,
  404. &chan->is_global);
  405. if (IS_ERR_OR_NULL(dentry))
  406. goto free_buf;
  407. }
  408. buf->cpu = cpu;
  409. __relay_reset(buf, 1);
  410. if(chan->is_global) {
  411. *per_cpu_ptr(chan->buf, 0) = buf;
  412. buf->cpu = 0;
  413. }
  414. return buf;
  415. free_buf:
  416. relay_destroy_buf(buf);
  417. return NULL;
  418. }
  419. /**
  420. * relay_close_buf - close a channel buffer
  421. * @buf: channel buffer
  422. *
  423. * Marks the buffer finalized and restores the default callbacks.
  424. * The channel buffer and channel buffer data structure are then freed
  425. * automatically when the last reference is given up.
  426. */
  427. static void relay_close_buf(struct rchan_buf *buf)
  428. {
  429. buf->finalized = 1;
  430. irq_work_sync(&buf->wakeup_work);
  431. buf->chan->cb->remove_buf_file(buf->dentry);
  432. kref_put(&buf->kref, relay_remove_buf);
  433. }
  434. static void setup_callbacks(struct rchan *chan,
  435. struct rchan_callbacks *cb)
  436. {
  437. if (!cb) {
  438. chan->cb = &default_channel_callbacks;
  439. return;
  440. }
  441. if (!cb->subbuf_start)
  442. cb->subbuf_start = subbuf_start_default_callback;
  443. if (!cb->buf_mapped)
  444. cb->buf_mapped = buf_mapped_default_callback;
  445. if (!cb->buf_unmapped)
  446. cb->buf_unmapped = buf_unmapped_default_callback;
  447. if (!cb->create_buf_file)
  448. cb->create_buf_file = create_buf_file_default_callback;
  449. if (!cb->remove_buf_file)
  450. cb->remove_buf_file = remove_buf_file_default_callback;
  451. chan->cb = cb;
  452. }
  453. int relay_prepare_cpu(unsigned int cpu)
  454. {
  455. struct rchan *chan;
  456. struct rchan_buf *buf;
  457. mutex_lock(&relay_channels_mutex);
  458. list_for_each_entry(chan, &relay_channels, list) {
  459. if ((buf = *per_cpu_ptr(chan->buf, cpu)))
  460. continue;
  461. buf = relay_open_buf(chan, cpu);
  462. if (!buf) {
  463. pr_err("relay: cpu %d buffer creation failed\n", cpu);
  464. mutex_unlock(&relay_channels_mutex);
  465. return -ENOMEM;
  466. }
  467. *per_cpu_ptr(chan->buf, cpu) = buf;
  468. }
  469. mutex_unlock(&relay_channels_mutex);
  470. return 0;
  471. }
  472. /**
  473. * relay_open - create a new relay channel
  474. * @base_filename: base name of files to create, %NULL for buffering only
  475. * @parent: dentry of parent directory, %NULL for root directory or buffer
  476. * @subbuf_size: size of sub-buffers
  477. * @n_subbufs: number of sub-buffers
  478. * @cb: client callback functions
  479. * @private_data: user-defined data
  480. *
  481. * Returns channel pointer if successful, %NULL otherwise.
  482. *
  483. * Creates a channel buffer for each cpu using the sizes and
  484. * attributes specified. The created channel buffer files
  485. * will be named base_filename0...base_filenameN-1. File
  486. * permissions will be %S_IRUSR.
  487. *
  488. * If opening a buffer (@parent = NULL) that you later wish to register
  489. * in a filesystem, call relay_late_setup_files() once the @parent dentry
  490. * is available.
  491. */
  492. struct rchan *relay_open(const char *base_filename,
  493. struct dentry *parent,
  494. size_t subbuf_size,
  495. size_t n_subbufs,
  496. struct rchan_callbacks *cb,
  497. void *private_data)
  498. {
  499. unsigned int i;
  500. struct rchan *chan;
  501. struct rchan_buf *buf;
  502. if (!(subbuf_size && n_subbufs))
  503. return NULL;
  504. if (subbuf_size > UINT_MAX / n_subbufs)
  505. return NULL;
  506. chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
  507. if (!chan)
  508. return NULL;
  509. chan->buf = alloc_percpu(struct rchan_buf *);
  510. if (!chan->buf) {
  511. kfree(chan);
  512. return NULL;
  513. }
  514. chan->version = RELAYFS_CHANNEL_VERSION;
  515. chan->n_subbufs = n_subbufs;
  516. chan->subbuf_size = subbuf_size;
  517. chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs);
  518. chan->parent = parent;
  519. chan->private_data = private_data;
  520. if (base_filename) {
  521. chan->has_base_filename = 1;
  522. strlcpy(chan->base_filename, base_filename, NAME_MAX);
  523. }
  524. setup_callbacks(chan, cb);
  525. kref_init(&chan->kref);
  526. mutex_lock(&relay_channels_mutex);
  527. for_each_online_cpu(i) {
  528. buf = relay_open_buf(chan, i);
  529. if (!buf)
  530. goto free_bufs;
  531. *per_cpu_ptr(chan->buf, i) = buf;
  532. }
  533. list_add(&chan->list, &relay_channels);
  534. mutex_unlock(&relay_channels_mutex);
  535. return chan;
  536. free_bufs:
  537. for_each_possible_cpu(i) {
  538. if ((buf = *per_cpu_ptr(chan->buf, i)))
  539. relay_close_buf(buf);
  540. }
  541. kref_put(&chan->kref, relay_destroy_channel);
  542. mutex_unlock(&relay_channels_mutex);
  543. return NULL;
  544. }
  545. EXPORT_SYMBOL_GPL(relay_open);
  546. struct rchan_percpu_buf_dispatcher {
  547. struct rchan_buf *buf;
  548. struct dentry *dentry;
  549. };
  550. /* Called in atomic context. */
  551. static void __relay_set_buf_dentry(void *info)
  552. {
  553. struct rchan_percpu_buf_dispatcher *p = info;
  554. relay_set_buf_dentry(p->buf, p->dentry);
  555. }
  556. /**
  557. * relay_late_setup_files - triggers file creation
  558. * @chan: channel to operate on
  559. * @base_filename: base name of files to create
  560. * @parent: dentry of parent directory, %NULL for root directory
  561. *
  562. * Returns 0 if successful, non-zero otherwise.
  563. *
  564. * Use to setup files for a previously buffer-only channel created
  565. * by relay_open() with a NULL parent dentry.
  566. *
  567. * For example, this is useful for perfomring early tracing in kernel,
  568. * before VFS is up and then exposing the early results once the dentry
  569. * is available.
  570. */
  571. int relay_late_setup_files(struct rchan *chan,
  572. const char *base_filename,
  573. struct dentry *parent)
  574. {
  575. int err = 0;
  576. unsigned int i, curr_cpu;
  577. unsigned long flags;
  578. struct dentry *dentry;
  579. struct rchan_buf *buf;
  580. struct rchan_percpu_buf_dispatcher disp;
  581. if (!chan || !base_filename)
  582. return -EINVAL;
  583. strlcpy(chan->base_filename, base_filename, NAME_MAX);
  584. mutex_lock(&relay_channels_mutex);
  585. /* Is chan already set up? */
  586. if (unlikely(chan->has_base_filename)) {
  587. mutex_unlock(&relay_channels_mutex);
  588. return -EEXIST;
  589. }
  590. chan->has_base_filename = 1;
  591. chan->parent = parent;
  592. if (chan->is_global) {
  593. err = -EINVAL;
  594. buf = *per_cpu_ptr(chan->buf, 0);
  595. if (!WARN_ON_ONCE(!buf)) {
  596. dentry = relay_create_buf_file(chan, buf, 0);
  597. if (dentry && !WARN_ON_ONCE(!chan->is_global)) {
  598. relay_set_buf_dentry(buf, dentry);
  599. err = 0;
  600. }
  601. }
  602. mutex_unlock(&relay_channels_mutex);
  603. return err;
  604. }
  605. curr_cpu = get_cpu();
  606. /*
  607. * The CPU hotplug notifier ran before us and created buffers with
  608. * no files associated. So it's safe to call relay_setup_buf_file()
  609. * on all currently online CPUs.
  610. */
  611. for_each_online_cpu(i) {
  612. buf = *per_cpu_ptr(chan->buf, i);
  613. if (unlikely(!buf)) {
  614. WARN_ONCE(1, KERN_ERR "CPU has no buffer!\n");
  615. err = -EINVAL;
  616. break;
  617. }
  618. dentry = relay_create_buf_file(chan, buf, i);
  619. if (unlikely(!dentry)) {
  620. err = -EINVAL;
  621. break;
  622. }
  623. if (curr_cpu == i) {
  624. local_irq_save(flags);
  625. relay_set_buf_dentry(buf, dentry);
  626. local_irq_restore(flags);
  627. } else {
  628. disp.buf = buf;
  629. disp.dentry = dentry;
  630. smp_mb();
  631. /* relay_channels_mutex must be held, so wait. */
  632. err = smp_call_function_single(i,
  633. __relay_set_buf_dentry,
  634. &disp, 1);
  635. }
  636. if (unlikely(err))
  637. break;
  638. }
  639. put_cpu();
  640. mutex_unlock(&relay_channels_mutex);
  641. return err;
  642. }
  643. EXPORT_SYMBOL_GPL(relay_late_setup_files);
  644. /**
  645. * relay_switch_subbuf - switch to a new sub-buffer
  646. * @buf: channel buffer
  647. * @length: size of current event
  648. *
  649. * Returns either the length passed in or 0 if full.
  650. *
  651. * Performs sub-buffer-switch tasks such as invoking callbacks,
  652. * updating padding counts, waking up readers, etc.
  653. */
  654. size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  655. {
  656. void *old, *new;
  657. size_t old_subbuf, new_subbuf;
  658. if (unlikely(length > buf->chan->subbuf_size))
  659. goto toobig;
  660. if (buf->offset != buf->chan->subbuf_size + 1) {
  661. buf->prev_padding = buf->chan->subbuf_size - buf->offset;
  662. old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  663. buf->padding[old_subbuf] = buf->prev_padding;
  664. buf->subbufs_produced++;
  665. if (buf->dentry)
  666. d_inode(buf->dentry)->i_size +=
  667. buf->chan->subbuf_size -
  668. buf->padding[old_subbuf];
  669. else
  670. buf->early_bytes += buf->chan->subbuf_size -
  671. buf->padding[old_subbuf];
  672. smp_mb();
  673. if (waitqueue_active(&buf->read_wait)) {
  674. /*
  675. * Calling wake_up_interruptible() from here
  676. * will deadlock if we happen to be logging
  677. * from the scheduler (trying to re-grab
  678. * rq->lock), so defer it.
  679. */
  680. irq_work_queue(&buf->wakeup_work);
  681. }
  682. }
  683. old = buf->data;
  684. new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  685. new = buf->start + new_subbuf * buf->chan->subbuf_size;
  686. buf->offset = 0;
  687. if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
  688. buf->offset = buf->chan->subbuf_size + 1;
  689. return 0;
  690. }
  691. buf->data = new;
  692. buf->padding[new_subbuf] = 0;
  693. if (unlikely(length + buf->offset > buf->chan->subbuf_size))
  694. goto toobig;
  695. return length;
  696. toobig:
  697. buf->chan->last_toobig = length;
  698. return 0;
  699. }
  700. EXPORT_SYMBOL_GPL(relay_switch_subbuf);
  701. /**
  702. * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
  703. * @chan: the channel
  704. * @cpu: the cpu associated with the channel buffer to update
  705. * @subbufs_consumed: number of sub-buffers to add to current buf's count
  706. *
  707. * Adds to the channel buffer's consumed sub-buffer count.
  708. * subbufs_consumed should be the number of sub-buffers newly consumed,
  709. * not the total consumed.
  710. *
  711. * NOTE. Kernel clients don't need to call this function if the channel
  712. * mode is 'overwrite'.
  713. */
  714. void relay_subbufs_consumed(struct rchan *chan,
  715. unsigned int cpu,
  716. size_t subbufs_consumed)
  717. {
  718. struct rchan_buf *buf;
  719. if (!chan || cpu >= NR_CPUS)
  720. return;
  721. buf = *per_cpu_ptr(chan->buf, cpu);
  722. if (!buf || subbufs_consumed > chan->n_subbufs)
  723. return;
  724. if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed)
  725. buf->subbufs_consumed = buf->subbufs_produced;
  726. else
  727. buf->subbufs_consumed += subbufs_consumed;
  728. }
  729. EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
  730. /**
  731. * relay_close - close the channel
  732. * @chan: the channel
  733. *
  734. * Closes all channel buffers and frees the channel.
  735. */
  736. void relay_close(struct rchan *chan)
  737. {
  738. struct rchan_buf *buf;
  739. unsigned int i;
  740. if (!chan)
  741. return;
  742. mutex_lock(&relay_channels_mutex);
  743. if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0)))
  744. relay_close_buf(buf);
  745. else
  746. for_each_possible_cpu(i)
  747. if ((buf = *per_cpu_ptr(chan->buf, i)))
  748. relay_close_buf(buf);
  749. if (chan->last_toobig)
  750. printk(KERN_WARNING "relay: one or more items not logged "
  751. "[item size (%zd) > sub-buffer size (%zd)]\n",
  752. chan->last_toobig, chan->subbuf_size);
  753. list_del(&chan->list);
  754. kref_put(&chan->kref, relay_destroy_channel);
  755. mutex_unlock(&relay_channels_mutex);
  756. }
  757. EXPORT_SYMBOL_GPL(relay_close);
  758. /**
  759. * relay_flush - close the channel
  760. * @chan: the channel
  761. *
  762. * Flushes all channel buffers, i.e. forces buffer switch.
  763. */
  764. void relay_flush(struct rchan *chan)
  765. {
  766. struct rchan_buf *buf;
  767. unsigned int i;
  768. if (!chan)
  769. return;
  770. if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
  771. relay_switch_subbuf(buf, 0);
  772. return;
  773. }
  774. mutex_lock(&relay_channels_mutex);
  775. for_each_possible_cpu(i)
  776. if ((buf = *per_cpu_ptr(chan->buf, i)))
  777. relay_switch_subbuf(buf, 0);
  778. mutex_unlock(&relay_channels_mutex);
  779. }
  780. EXPORT_SYMBOL_GPL(relay_flush);
  781. /**
  782. * relay_file_open - open file op for relay files
  783. * @inode: the inode
  784. * @filp: the file
  785. *
  786. * Increments the channel buffer refcount.
  787. */
  788. static int relay_file_open(struct inode *inode, struct file *filp)
  789. {
  790. struct rchan_buf *buf = inode->i_private;
  791. kref_get(&buf->kref);
  792. filp->private_data = buf;
  793. return nonseekable_open(inode, filp);
  794. }
  795. /**
  796. * relay_file_mmap - mmap file op for relay files
  797. * @filp: the file
  798. * @vma: the vma describing what to map
  799. *
  800. * Calls upon relay_mmap_buf() to map the file into user space.
  801. */
  802. static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
  803. {
  804. struct rchan_buf *buf = filp->private_data;
  805. return relay_mmap_buf(buf, vma);
  806. }
  807. /**
  808. * relay_file_poll - poll file op for relay files
  809. * @filp: the file
  810. * @wait: poll table
  811. *
  812. * Poll implemention.
  813. */
  814. static __poll_t relay_file_poll(struct file *filp, poll_table *wait)
  815. {
  816. __poll_t mask = 0;
  817. struct rchan_buf *buf = filp->private_data;
  818. if (buf->finalized)
  819. return EPOLLERR;
  820. if (filp->f_mode & FMODE_READ) {
  821. poll_wait(filp, &buf->read_wait, wait);
  822. if (!relay_buf_empty(buf))
  823. mask |= EPOLLIN | EPOLLRDNORM;
  824. }
  825. return mask;
  826. }
  827. /**
  828. * relay_file_release - release file op for relay files
  829. * @inode: the inode
  830. * @filp: the file
  831. *
  832. * Decrements the channel refcount, as the filesystem is
  833. * no longer using it.
  834. */
  835. static int relay_file_release(struct inode *inode, struct file *filp)
  836. {
  837. struct rchan_buf *buf = filp->private_data;
  838. kref_put(&buf->kref, relay_remove_buf);
  839. return 0;
  840. }
  841. /*
  842. * relay_file_read_consume - update the consumed count for the buffer
  843. */
  844. static void relay_file_read_consume(struct rchan_buf *buf,
  845. size_t read_pos,
  846. size_t bytes_consumed)
  847. {
  848. size_t subbuf_size = buf->chan->subbuf_size;
  849. size_t n_subbufs = buf->chan->n_subbufs;
  850. size_t read_subbuf;
  851. if (buf->subbufs_produced == buf->subbufs_consumed &&
  852. buf->offset == buf->bytes_consumed)
  853. return;
  854. if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
  855. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  856. buf->bytes_consumed = 0;
  857. }
  858. buf->bytes_consumed += bytes_consumed;
  859. if (!read_pos)
  860. read_subbuf = buf->subbufs_consumed % n_subbufs;
  861. else
  862. read_subbuf = read_pos / buf->chan->subbuf_size;
  863. if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
  864. if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
  865. (buf->offset == subbuf_size))
  866. return;
  867. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  868. buf->bytes_consumed = 0;
  869. }
  870. }
  871. /*
  872. * relay_file_read_avail - boolean, are there unconsumed bytes available?
  873. */
  874. static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
  875. {
  876. size_t subbuf_size = buf->chan->subbuf_size;
  877. size_t n_subbufs = buf->chan->n_subbufs;
  878. size_t produced = buf->subbufs_produced;
  879. size_t consumed = buf->subbufs_consumed;
  880. relay_file_read_consume(buf, read_pos, 0);
  881. consumed = buf->subbufs_consumed;
  882. if (unlikely(buf->offset > subbuf_size)) {
  883. if (produced == consumed)
  884. return 0;
  885. return 1;
  886. }
  887. if (unlikely(produced - consumed >= n_subbufs)) {
  888. consumed = produced - n_subbufs + 1;
  889. buf->subbufs_consumed = consumed;
  890. buf->bytes_consumed = 0;
  891. }
  892. produced = (produced % n_subbufs) * subbuf_size + buf->offset;
  893. consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
  894. if (consumed > produced)
  895. produced += n_subbufs * subbuf_size;
  896. if (consumed == produced) {
  897. if (buf->offset == subbuf_size &&
  898. buf->subbufs_produced > buf->subbufs_consumed)
  899. return 1;
  900. return 0;
  901. }
  902. return 1;
  903. }
  904. /**
  905. * relay_file_read_subbuf_avail - return bytes available in sub-buffer
  906. * @read_pos: file read position
  907. * @buf: relay channel buffer
  908. */
  909. static size_t relay_file_read_subbuf_avail(size_t read_pos,
  910. struct rchan_buf *buf)
  911. {
  912. size_t padding, avail = 0;
  913. size_t read_subbuf, read_offset, write_subbuf, write_offset;
  914. size_t subbuf_size = buf->chan->subbuf_size;
  915. write_subbuf = (buf->data - buf->start) / subbuf_size;
  916. write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
  917. read_subbuf = read_pos / subbuf_size;
  918. read_offset = read_pos % subbuf_size;
  919. padding = buf->padding[read_subbuf];
  920. if (read_subbuf == write_subbuf) {
  921. if (read_offset + padding < write_offset)
  922. avail = write_offset - (read_offset + padding);
  923. } else
  924. avail = (subbuf_size - padding) - read_offset;
  925. return avail;
  926. }
  927. /**
  928. * relay_file_read_start_pos - find the first available byte to read
  929. * @read_pos: file read position
  930. * @buf: relay channel buffer
  931. *
  932. * If the @read_pos is in the middle of padding, return the
  933. * position of the first actually available byte, otherwise
  934. * return the original value.
  935. */
  936. static size_t relay_file_read_start_pos(size_t read_pos,
  937. struct rchan_buf *buf)
  938. {
  939. size_t read_subbuf, padding, padding_start, padding_end;
  940. size_t subbuf_size = buf->chan->subbuf_size;
  941. size_t n_subbufs = buf->chan->n_subbufs;
  942. size_t consumed = buf->subbufs_consumed % n_subbufs;
  943. if (!read_pos)
  944. read_pos = consumed * subbuf_size + buf->bytes_consumed;
  945. read_subbuf = read_pos / subbuf_size;
  946. padding = buf->padding[read_subbuf];
  947. padding_start = (read_subbuf + 1) * subbuf_size - padding;
  948. padding_end = (read_subbuf + 1) * subbuf_size;
  949. if (read_pos >= padding_start && read_pos < padding_end) {
  950. read_subbuf = (read_subbuf + 1) % n_subbufs;
  951. read_pos = read_subbuf * subbuf_size;
  952. }
  953. return read_pos;
  954. }
  955. /**
  956. * relay_file_read_end_pos - return the new read position
  957. * @read_pos: file read position
  958. * @buf: relay channel buffer
  959. * @count: number of bytes to be read
  960. */
  961. static size_t relay_file_read_end_pos(struct rchan_buf *buf,
  962. size_t read_pos,
  963. size_t count)
  964. {
  965. size_t read_subbuf, padding, end_pos;
  966. size_t subbuf_size = buf->chan->subbuf_size;
  967. size_t n_subbufs = buf->chan->n_subbufs;
  968. read_subbuf = read_pos / subbuf_size;
  969. padding = buf->padding[read_subbuf];
  970. if (read_pos % subbuf_size + count + padding == subbuf_size)
  971. end_pos = (read_subbuf + 1) * subbuf_size;
  972. else
  973. end_pos = read_pos + count;
  974. if (end_pos >= subbuf_size * n_subbufs)
  975. end_pos = 0;
  976. return end_pos;
  977. }
  978. static ssize_t relay_file_read(struct file *filp,
  979. char __user *buffer,
  980. size_t count,
  981. loff_t *ppos)
  982. {
  983. struct rchan_buf *buf = filp->private_data;
  984. size_t read_start, avail;
  985. size_t written = 0;
  986. int ret;
  987. if (!count)
  988. return 0;
  989. inode_lock(file_inode(filp));
  990. do {
  991. void *from;
  992. if (!relay_file_read_avail(buf, *ppos))
  993. break;
  994. read_start = relay_file_read_start_pos(*ppos, buf);
  995. avail = relay_file_read_subbuf_avail(read_start, buf);
  996. if (!avail)
  997. break;
  998. avail = min(count, avail);
  999. from = buf->start + read_start;
  1000. ret = avail;
  1001. if (copy_to_user(buffer, from, avail))
  1002. break;
  1003. buffer += ret;
  1004. written += ret;
  1005. count -= ret;
  1006. relay_file_read_consume(buf, read_start, ret);
  1007. *ppos = relay_file_read_end_pos(buf, read_start, ret);
  1008. } while (count);
  1009. inode_unlock(file_inode(filp));
  1010. return written;
  1011. }
  1012. static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
  1013. {
  1014. rbuf->bytes_consumed += bytes_consumed;
  1015. if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
  1016. relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
  1017. rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
  1018. }
  1019. }
  1020. static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
  1021. struct pipe_buffer *buf)
  1022. {
  1023. struct rchan_buf *rbuf;
  1024. rbuf = (struct rchan_buf *)page_private(buf->page);
  1025. relay_consume_bytes(rbuf, buf->private);
  1026. }
  1027. static const struct pipe_buf_operations relay_pipe_buf_ops = {
  1028. .can_merge = 0,
  1029. .confirm = generic_pipe_buf_confirm,
  1030. .release = relay_pipe_buf_release,
  1031. .steal = generic_pipe_buf_steal,
  1032. .get = generic_pipe_buf_get,
  1033. };
  1034. static void relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
  1035. {
  1036. }
  1037. /*
  1038. * subbuf_splice_actor - splice up to one subbuf's worth of data
  1039. */
  1040. static ssize_t subbuf_splice_actor(struct file *in,
  1041. loff_t *ppos,
  1042. struct pipe_inode_info *pipe,
  1043. size_t len,
  1044. unsigned int flags,
  1045. int *nonpad_ret)
  1046. {
  1047. unsigned int pidx, poff, total_len, subbuf_pages, nr_pages;
  1048. struct rchan_buf *rbuf = in->private_data;
  1049. unsigned int subbuf_size = rbuf->chan->subbuf_size;
  1050. uint64_t pos = (uint64_t) *ppos;
  1051. uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size;
  1052. size_t read_start = (size_t) do_div(pos, alloc_size);
  1053. size_t read_subbuf = read_start / subbuf_size;
  1054. size_t padding = rbuf->padding[read_subbuf];
  1055. size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
  1056. struct page *pages[PIPE_DEF_BUFFERS];
  1057. struct partial_page partial[PIPE_DEF_BUFFERS];
  1058. struct splice_pipe_desc spd = {
  1059. .pages = pages,
  1060. .nr_pages = 0,
  1061. .nr_pages_max = PIPE_DEF_BUFFERS,
  1062. .partial = partial,
  1063. .ops = &relay_pipe_buf_ops,
  1064. .spd_release = relay_page_release,
  1065. };
  1066. ssize_t ret;
  1067. if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
  1068. return 0;
  1069. if (splice_grow_spd(pipe, &spd))
  1070. return -ENOMEM;
  1071. /*
  1072. * Adjust read len, if longer than what is available
  1073. */
  1074. if (len > (subbuf_size - read_start % subbuf_size))
  1075. len = subbuf_size - read_start % subbuf_size;
  1076. subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
  1077. pidx = (read_start / PAGE_SIZE) % subbuf_pages;
  1078. poff = read_start & ~PAGE_MASK;
  1079. nr_pages = min_t(unsigned int, subbuf_pages, spd.nr_pages_max);
  1080. for (total_len = 0; spd.nr_pages < nr_pages; spd.nr_pages++) {
  1081. unsigned int this_len, this_end, private;
  1082. unsigned int cur_pos = read_start + total_len;
  1083. if (!len)
  1084. break;
  1085. this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
  1086. private = this_len;
  1087. spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
  1088. spd.partial[spd.nr_pages].offset = poff;
  1089. this_end = cur_pos + this_len;
  1090. if (this_end >= nonpad_end) {
  1091. this_len = nonpad_end - cur_pos;
  1092. private = this_len + padding;
  1093. }
  1094. spd.partial[spd.nr_pages].len = this_len;
  1095. spd.partial[spd.nr_pages].private = private;
  1096. len -= this_len;
  1097. total_len += this_len;
  1098. poff = 0;
  1099. pidx = (pidx + 1) % subbuf_pages;
  1100. if (this_end >= nonpad_end) {
  1101. spd.nr_pages++;
  1102. break;
  1103. }
  1104. }
  1105. ret = 0;
  1106. if (!spd.nr_pages)
  1107. goto out;
  1108. ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
  1109. if (ret < 0 || ret < total_len)
  1110. goto out;
  1111. if (read_start + ret == nonpad_end)
  1112. ret += padding;
  1113. out:
  1114. splice_shrink_spd(&spd);
  1115. return ret;
  1116. }
  1117. static ssize_t relay_file_splice_read(struct file *in,
  1118. loff_t *ppos,
  1119. struct pipe_inode_info *pipe,
  1120. size_t len,
  1121. unsigned int flags)
  1122. {
  1123. ssize_t spliced;
  1124. int ret;
  1125. int nonpad_ret = 0;
  1126. ret = 0;
  1127. spliced = 0;
  1128. while (len && !spliced) {
  1129. ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret);
  1130. if (ret < 0)
  1131. break;
  1132. else if (!ret) {
  1133. if (flags & SPLICE_F_NONBLOCK)
  1134. ret = -EAGAIN;
  1135. break;
  1136. }
  1137. *ppos += ret;
  1138. if (ret > len)
  1139. len = 0;
  1140. else
  1141. len -= ret;
  1142. spliced += nonpad_ret;
  1143. nonpad_ret = 0;
  1144. }
  1145. if (spliced)
  1146. return spliced;
  1147. return ret;
  1148. }
  1149. const struct file_operations relay_file_operations = {
  1150. .open = relay_file_open,
  1151. .poll = relay_file_poll,
  1152. .mmap = relay_file_mmap,
  1153. .read = relay_file_read,
  1154. .llseek = no_llseek,
  1155. .release = relay_file_release,
  1156. .splice_read = relay_file_splice_read,
  1157. };
  1158. EXPORT_SYMBOL_GPL(relay_file_operations);