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