relay.c 29 KB

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