relay.c 30 KB

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