relay.c 29 KB

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