relay.c 26 KB

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