relay.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. * @work: work struct that contains the the channel buffer
  274. *
  275. * This is the work function used to defer reader waking. The
  276. * reason waking is deferred is that calling directly from write
  277. * causes problems if you're writing from say the scheduler.
  278. */
  279. static void wakeup_readers(struct work_struct *work)
  280. {
  281. struct rchan_buf *buf =
  282. container_of(work, struct rchan_buf, wake_readers.work);
  283. wake_up_interruptible(&buf->read_wait);
  284. }
  285. /**
  286. * __relay_reset - reset a channel buffer
  287. * @buf: the channel buffer
  288. * @init: 1 if this is a first-time initialization
  289. *
  290. * See relay_reset() for description of effect.
  291. */
  292. static void __relay_reset(struct rchan_buf *buf, unsigned int init)
  293. {
  294. size_t i;
  295. if (init) {
  296. init_waitqueue_head(&buf->read_wait);
  297. kref_init(&buf->kref);
  298. INIT_DELAYED_WORK(&buf->wake_readers, NULL);
  299. } else {
  300. cancel_delayed_work(&buf->wake_readers);
  301. flush_scheduled_work();
  302. }
  303. buf->subbufs_produced = 0;
  304. buf->subbufs_consumed = 0;
  305. buf->bytes_consumed = 0;
  306. buf->finalized = 0;
  307. buf->data = buf->start;
  308. buf->offset = 0;
  309. for (i = 0; i < buf->chan->n_subbufs; i++)
  310. buf->padding[i] = 0;
  311. buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
  312. }
  313. /**
  314. * relay_reset - reset the channel
  315. * @chan: the channel
  316. *
  317. * This has the effect of erasing all data from all channel buffers
  318. * and restarting the channel in its initial state. The buffers
  319. * are not freed, so any mappings are still in effect.
  320. *
  321. * NOTE. Care should be taken that the channel isn't actually
  322. * being used by anything when this call is made.
  323. */
  324. void relay_reset(struct rchan *chan)
  325. {
  326. unsigned int i;
  327. if (!chan)
  328. return;
  329. if (chan->is_global && chan->buf[0]) {
  330. __relay_reset(chan->buf[0], 0);
  331. return;
  332. }
  333. mutex_lock(&relay_channels_mutex);
  334. for_each_online_cpu(i)
  335. if (chan->buf[i])
  336. __relay_reset(chan->buf[i], 0);
  337. mutex_unlock(&relay_channels_mutex);
  338. }
  339. EXPORT_SYMBOL_GPL(relay_reset);
  340. /*
  341. * relay_open_buf - create a new relay channel buffer
  342. *
  343. * used by relay_open() and CPU hotplug.
  344. */
  345. static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
  346. {
  347. struct rchan_buf *buf = NULL;
  348. struct dentry *dentry;
  349. char *tmpname;
  350. if (chan->is_global)
  351. return chan->buf[0];
  352. tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
  353. if (!tmpname)
  354. goto end;
  355. snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
  356. buf = relay_create_buf(chan);
  357. if (!buf)
  358. goto free_name;
  359. buf->cpu = cpu;
  360. __relay_reset(buf, 1);
  361. /* Create file in fs */
  362. dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
  363. buf, &chan->is_global);
  364. if (!dentry)
  365. goto free_buf;
  366. buf->dentry = dentry;
  367. if(chan->is_global) {
  368. chan->buf[0] = buf;
  369. buf->cpu = 0;
  370. }
  371. goto free_name;
  372. free_buf:
  373. relay_destroy_buf(buf);
  374. free_name:
  375. kfree(tmpname);
  376. end:
  377. return buf;
  378. }
  379. /**
  380. * relay_close_buf - close a channel buffer
  381. * @buf: channel buffer
  382. *
  383. * Marks the buffer finalized and restores the default callbacks.
  384. * The channel buffer and channel buffer data structure are then freed
  385. * automatically when the last reference is given up.
  386. */
  387. static void relay_close_buf(struct rchan_buf *buf)
  388. {
  389. buf->finalized = 1;
  390. cancel_delayed_work(&buf->wake_readers);
  391. flush_scheduled_work();
  392. kref_put(&buf->kref, relay_remove_buf);
  393. }
  394. static void setup_callbacks(struct rchan *chan,
  395. struct rchan_callbacks *cb)
  396. {
  397. if (!cb) {
  398. chan->cb = &default_channel_callbacks;
  399. return;
  400. }
  401. if (!cb->subbuf_start)
  402. cb->subbuf_start = subbuf_start_default_callback;
  403. if (!cb->buf_mapped)
  404. cb->buf_mapped = buf_mapped_default_callback;
  405. if (!cb->buf_unmapped)
  406. cb->buf_unmapped = buf_unmapped_default_callback;
  407. if (!cb->create_buf_file)
  408. cb->create_buf_file = create_buf_file_default_callback;
  409. if (!cb->remove_buf_file)
  410. cb->remove_buf_file = remove_buf_file_default_callback;
  411. chan->cb = cb;
  412. }
  413. /**
  414. *
  415. * relay_hotcpu_callback - CPU hotplug callback
  416. * @nb: notifier block
  417. * @action: hotplug action to take
  418. * @hcpu: CPU number
  419. *
  420. * Returns the success/failure of the operation. (NOTIFY_OK, NOTIFY_BAD)
  421. */
  422. static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
  423. unsigned long action,
  424. void *hcpu)
  425. {
  426. unsigned int hotcpu = (unsigned long)hcpu;
  427. struct rchan *chan;
  428. switch(action) {
  429. case CPU_UP_PREPARE:
  430. mutex_lock(&relay_channels_mutex);
  431. list_for_each_entry(chan, &relay_channels, list) {
  432. if (chan->buf[hotcpu])
  433. continue;
  434. chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
  435. if(!chan->buf[hotcpu]) {
  436. printk(KERN_ERR
  437. "relay_hotcpu_callback: cpu %d buffer "
  438. "creation failed\n", hotcpu);
  439. mutex_unlock(&relay_channels_mutex);
  440. return NOTIFY_BAD;
  441. }
  442. }
  443. mutex_unlock(&relay_channels_mutex);
  444. break;
  445. case CPU_DEAD:
  446. /* No need to flush the cpu : will be flushed upon
  447. * final relay_flush() call. */
  448. break;
  449. }
  450. return NOTIFY_OK;
  451. }
  452. /**
  453. * relay_open - create a new relay channel
  454. * @base_filename: base name of files to create
  455. * @parent: dentry of parent directory, %NULL for root directory
  456. * @subbuf_size: size of sub-buffers
  457. * @n_subbufs: number of sub-buffers
  458. * @cb: client callback functions
  459. * @private_data: user-defined data
  460. *
  461. * Returns channel pointer if successful, %NULL otherwise.
  462. *
  463. * Creates a channel buffer for each cpu using the sizes and
  464. * attributes specified. The created channel buffer files
  465. * will be named base_filename0...base_filenameN-1. File
  466. * permissions will be %S_IRUSR.
  467. */
  468. struct rchan *relay_open(const char *base_filename,
  469. struct dentry *parent,
  470. size_t subbuf_size,
  471. size_t n_subbufs,
  472. struct rchan_callbacks *cb,
  473. void *private_data)
  474. {
  475. unsigned int i;
  476. struct rchan *chan;
  477. if (!base_filename)
  478. return NULL;
  479. if (!(subbuf_size && n_subbufs))
  480. return NULL;
  481. chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
  482. if (!chan)
  483. return NULL;
  484. chan->version = RELAYFS_CHANNEL_VERSION;
  485. chan->n_subbufs = n_subbufs;
  486. chan->subbuf_size = subbuf_size;
  487. chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
  488. chan->parent = parent;
  489. chan->private_data = private_data;
  490. strlcpy(chan->base_filename, base_filename, NAME_MAX);
  491. setup_callbacks(chan, cb);
  492. kref_init(&chan->kref);
  493. mutex_lock(&relay_channels_mutex);
  494. for_each_online_cpu(i) {
  495. chan->buf[i] = relay_open_buf(chan, i);
  496. if (!chan->buf[i])
  497. goto free_bufs;
  498. }
  499. list_add(&chan->list, &relay_channels);
  500. mutex_unlock(&relay_channels_mutex);
  501. return chan;
  502. free_bufs:
  503. for_each_online_cpu(i) {
  504. if (!chan->buf[i])
  505. break;
  506. relay_close_buf(chan->buf[i]);
  507. }
  508. kref_put(&chan->kref, relay_destroy_channel);
  509. mutex_unlock(&relay_channels_mutex);
  510. return NULL;
  511. }
  512. EXPORT_SYMBOL_GPL(relay_open);
  513. /**
  514. * relay_switch_subbuf - switch to a new sub-buffer
  515. * @buf: channel buffer
  516. * @length: size of current event
  517. *
  518. * Returns either the length passed in or 0 if full.
  519. *
  520. * Performs sub-buffer-switch tasks such as invoking callbacks,
  521. * updating padding counts, waking up readers, etc.
  522. */
  523. size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  524. {
  525. void *old, *new;
  526. size_t old_subbuf, new_subbuf;
  527. if (unlikely(length > buf->chan->subbuf_size))
  528. goto toobig;
  529. if (buf->offset != buf->chan->subbuf_size + 1) {
  530. buf->prev_padding = buf->chan->subbuf_size - buf->offset;
  531. old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  532. buf->padding[old_subbuf] = buf->prev_padding;
  533. buf->subbufs_produced++;
  534. buf->dentry->d_inode->i_size += buf->chan->subbuf_size -
  535. buf->padding[old_subbuf];
  536. smp_mb();
  537. if (waitqueue_active(&buf->read_wait)) {
  538. PREPARE_DELAYED_WORK(&buf->wake_readers,
  539. wakeup_readers);
  540. schedule_delayed_work(&buf->wake_readers, 1);
  541. }
  542. }
  543. old = buf->data;
  544. new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  545. new = buf->start + new_subbuf * buf->chan->subbuf_size;
  546. buf->offset = 0;
  547. if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
  548. buf->offset = buf->chan->subbuf_size + 1;
  549. return 0;
  550. }
  551. buf->data = new;
  552. buf->padding[new_subbuf] = 0;
  553. if (unlikely(length + buf->offset > buf->chan->subbuf_size))
  554. goto toobig;
  555. return length;
  556. toobig:
  557. buf->chan->last_toobig = length;
  558. return 0;
  559. }
  560. EXPORT_SYMBOL_GPL(relay_switch_subbuf);
  561. /**
  562. * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
  563. * @chan: the channel
  564. * @cpu: the cpu associated with the channel buffer to update
  565. * @subbufs_consumed: number of sub-buffers to add to current buf's count
  566. *
  567. * Adds to the channel buffer's consumed sub-buffer count.
  568. * subbufs_consumed should be the number of sub-buffers newly consumed,
  569. * not the total consumed.
  570. *
  571. * NOTE. Kernel clients don't need to call this function if the channel
  572. * mode is 'overwrite'.
  573. */
  574. void relay_subbufs_consumed(struct rchan *chan,
  575. unsigned int cpu,
  576. size_t subbufs_consumed)
  577. {
  578. struct rchan_buf *buf;
  579. if (!chan)
  580. return;
  581. if (cpu >= NR_CPUS || !chan->buf[cpu])
  582. return;
  583. buf = chan->buf[cpu];
  584. buf->subbufs_consumed += subbufs_consumed;
  585. if (buf->subbufs_consumed > buf->subbufs_produced)
  586. buf->subbufs_consumed = buf->subbufs_produced;
  587. }
  588. EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
  589. /**
  590. * relay_close - close the channel
  591. * @chan: the channel
  592. *
  593. * Closes all channel buffers and frees the channel.
  594. */
  595. void relay_close(struct rchan *chan)
  596. {
  597. unsigned int i;
  598. if (!chan)
  599. return;
  600. mutex_lock(&relay_channels_mutex);
  601. if (chan->is_global && chan->buf[0])
  602. relay_close_buf(chan->buf[0]);
  603. else
  604. for_each_possible_cpu(i)
  605. if (chan->buf[i])
  606. relay_close_buf(chan->buf[i]);
  607. if (chan->last_toobig)
  608. printk(KERN_WARNING "relay: one or more items not logged "
  609. "[item size (%Zd) > sub-buffer size (%Zd)]\n",
  610. chan->last_toobig, chan->subbuf_size);
  611. list_del(&chan->list);
  612. kref_put(&chan->kref, relay_destroy_channel);
  613. mutex_unlock(&relay_channels_mutex);
  614. }
  615. EXPORT_SYMBOL_GPL(relay_close);
  616. /**
  617. * relay_flush - close the channel
  618. * @chan: the channel
  619. *
  620. * Flushes all channel buffers, i.e. forces buffer switch.
  621. */
  622. void relay_flush(struct rchan *chan)
  623. {
  624. unsigned int i;
  625. if (!chan)
  626. return;
  627. if (chan->is_global && chan->buf[0]) {
  628. relay_switch_subbuf(chan->buf[0], 0);
  629. return;
  630. }
  631. mutex_lock(&relay_channels_mutex);
  632. for_each_possible_cpu(i)
  633. if (chan->buf[i])
  634. relay_switch_subbuf(chan->buf[i], 0);
  635. mutex_unlock(&relay_channels_mutex);
  636. }
  637. EXPORT_SYMBOL_GPL(relay_flush);
  638. /**
  639. * relay_file_open - open file op for relay files
  640. * @inode: the inode
  641. * @filp: the file
  642. *
  643. * Increments the channel buffer refcount.
  644. */
  645. static int relay_file_open(struct inode *inode, struct file *filp)
  646. {
  647. struct rchan_buf *buf = inode->i_private;
  648. kref_get(&buf->kref);
  649. filp->private_data = buf;
  650. return 0;
  651. }
  652. /**
  653. * relay_file_mmap - mmap file op for relay files
  654. * @filp: the file
  655. * @vma: the vma describing what to map
  656. *
  657. * Calls upon relay_mmap_buf() to map the file into user space.
  658. */
  659. static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
  660. {
  661. struct rchan_buf *buf = filp->private_data;
  662. return relay_mmap_buf(buf, vma);
  663. }
  664. /**
  665. * relay_file_poll - poll file op for relay files
  666. * @filp: the file
  667. * @wait: poll table
  668. *
  669. * Poll implemention.
  670. */
  671. static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
  672. {
  673. unsigned int mask = 0;
  674. struct rchan_buf *buf = filp->private_data;
  675. if (buf->finalized)
  676. return POLLERR;
  677. if (filp->f_mode & FMODE_READ) {
  678. poll_wait(filp, &buf->read_wait, wait);
  679. if (!relay_buf_empty(buf))
  680. mask |= POLLIN | POLLRDNORM;
  681. }
  682. return mask;
  683. }
  684. /**
  685. * relay_file_release - release file op for relay files
  686. * @inode: the inode
  687. * @filp: the file
  688. *
  689. * Decrements the channel refcount, as the filesystem is
  690. * no longer using it.
  691. */
  692. static int relay_file_release(struct inode *inode, struct file *filp)
  693. {
  694. struct rchan_buf *buf = filp->private_data;
  695. kref_put(&buf->kref, relay_remove_buf);
  696. return 0;
  697. }
  698. /*
  699. * relay_file_read_consume - update the consumed count for the buffer
  700. */
  701. static void relay_file_read_consume(struct rchan_buf *buf,
  702. size_t read_pos,
  703. size_t bytes_consumed)
  704. {
  705. size_t subbuf_size = buf->chan->subbuf_size;
  706. size_t n_subbufs = buf->chan->n_subbufs;
  707. size_t read_subbuf;
  708. if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
  709. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  710. buf->bytes_consumed = 0;
  711. }
  712. buf->bytes_consumed += bytes_consumed;
  713. read_subbuf = read_pos / buf->chan->subbuf_size;
  714. if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
  715. if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
  716. (buf->offset == subbuf_size))
  717. return;
  718. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  719. buf->bytes_consumed = 0;
  720. }
  721. }
  722. /*
  723. * relay_file_read_avail - boolean, are there unconsumed bytes available?
  724. */
  725. static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
  726. {
  727. size_t subbuf_size = buf->chan->subbuf_size;
  728. size_t n_subbufs = buf->chan->n_subbufs;
  729. size_t produced = buf->subbufs_produced;
  730. size_t consumed = buf->subbufs_consumed;
  731. relay_file_read_consume(buf, read_pos, 0);
  732. if (unlikely(buf->offset > subbuf_size)) {
  733. if (produced == consumed)
  734. return 0;
  735. return 1;
  736. }
  737. if (unlikely(produced - consumed >= n_subbufs)) {
  738. consumed = (produced / n_subbufs) * n_subbufs;
  739. buf->subbufs_consumed = consumed;
  740. }
  741. produced = (produced % n_subbufs) * subbuf_size + buf->offset;
  742. consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
  743. if (consumed > produced)
  744. produced += n_subbufs * subbuf_size;
  745. if (consumed == produced)
  746. return 0;
  747. return 1;
  748. }
  749. /**
  750. * relay_file_read_subbuf_avail - return bytes available in sub-buffer
  751. * @read_pos: file read position
  752. * @buf: relay channel buffer
  753. */
  754. static size_t relay_file_read_subbuf_avail(size_t read_pos,
  755. struct rchan_buf *buf)
  756. {
  757. size_t padding, avail = 0;
  758. size_t read_subbuf, read_offset, write_subbuf, write_offset;
  759. size_t subbuf_size = buf->chan->subbuf_size;
  760. write_subbuf = (buf->data - buf->start) / subbuf_size;
  761. write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
  762. read_subbuf = read_pos / subbuf_size;
  763. read_offset = read_pos % subbuf_size;
  764. padding = buf->padding[read_subbuf];
  765. if (read_subbuf == write_subbuf) {
  766. if (read_offset + padding < write_offset)
  767. avail = write_offset - (read_offset + padding);
  768. } else
  769. avail = (subbuf_size - padding) - read_offset;
  770. return avail;
  771. }
  772. /**
  773. * relay_file_read_start_pos - find the first available byte to read
  774. * @read_pos: file read position
  775. * @buf: relay channel buffer
  776. *
  777. * If the @read_pos is in the middle of padding, return the
  778. * position of the first actually available byte, otherwise
  779. * return the original value.
  780. */
  781. static size_t relay_file_read_start_pos(size_t read_pos,
  782. struct rchan_buf *buf)
  783. {
  784. size_t read_subbuf, padding, padding_start, padding_end;
  785. size_t subbuf_size = buf->chan->subbuf_size;
  786. size_t n_subbufs = buf->chan->n_subbufs;
  787. read_subbuf = read_pos / subbuf_size;
  788. padding = buf->padding[read_subbuf];
  789. padding_start = (read_subbuf + 1) * subbuf_size - padding;
  790. padding_end = (read_subbuf + 1) * subbuf_size;
  791. if (read_pos >= padding_start && read_pos < padding_end) {
  792. read_subbuf = (read_subbuf + 1) % n_subbufs;
  793. read_pos = read_subbuf * subbuf_size;
  794. }
  795. return read_pos;
  796. }
  797. /**
  798. * relay_file_read_end_pos - return the new read position
  799. * @read_pos: file read position
  800. * @buf: relay channel buffer
  801. * @count: number of bytes to be read
  802. */
  803. static size_t relay_file_read_end_pos(struct rchan_buf *buf,
  804. size_t read_pos,
  805. size_t count)
  806. {
  807. size_t read_subbuf, padding, end_pos;
  808. size_t subbuf_size = buf->chan->subbuf_size;
  809. size_t n_subbufs = buf->chan->n_subbufs;
  810. read_subbuf = read_pos / subbuf_size;
  811. padding = buf->padding[read_subbuf];
  812. if (read_pos % subbuf_size + count + padding == subbuf_size)
  813. end_pos = (read_subbuf + 1) * subbuf_size;
  814. else
  815. end_pos = read_pos + count;
  816. if (end_pos >= subbuf_size * n_subbufs)
  817. end_pos = 0;
  818. return end_pos;
  819. }
  820. /*
  821. * subbuf_read_actor - read up to one subbuf's worth of data
  822. */
  823. static int subbuf_read_actor(size_t read_start,
  824. struct rchan_buf *buf,
  825. size_t avail,
  826. read_descriptor_t *desc,
  827. read_actor_t actor)
  828. {
  829. void *from;
  830. int ret = 0;
  831. from = buf->start + read_start;
  832. ret = avail;
  833. if (copy_to_user(desc->arg.buf, from, avail)) {
  834. desc->error = -EFAULT;
  835. ret = 0;
  836. }
  837. desc->arg.data += ret;
  838. desc->written += ret;
  839. desc->count -= ret;
  840. return ret;
  841. }
  842. /*
  843. * subbuf_send_actor - send up to one subbuf's worth of data
  844. */
  845. static int subbuf_send_actor(size_t read_start,
  846. struct rchan_buf *buf,
  847. size_t avail,
  848. read_descriptor_t *desc,
  849. read_actor_t actor)
  850. {
  851. unsigned long pidx, poff;
  852. unsigned int subbuf_pages;
  853. int ret = 0;
  854. subbuf_pages = buf->chan->alloc_size >> PAGE_SHIFT;
  855. pidx = (read_start / PAGE_SIZE) % subbuf_pages;
  856. poff = read_start & ~PAGE_MASK;
  857. while (avail) {
  858. struct page *p = buf->page_array[pidx];
  859. unsigned int len;
  860. len = PAGE_SIZE - poff;
  861. if (len > avail)
  862. len = avail;
  863. len = actor(desc, p, poff, len);
  864. if (desc->error)
  865. break;
  866. avail -= len;
  867. ret += len;
  868. poff = 0;
  869. pidx = (pidx + 1) % subbuf_pages;
  870. }
  871. return ret;
  872. }
  873. typedef int (*subbuf_actor_t) (size_t read_start,
  874. struct rchan_buf *buf,
  875. size_t avail,
  876. read_descriptor_t *desc,
  877. read_actor_t actor);
  878. /*
  879. * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
  880. */
  881. static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,
  882. subbuf_actor_t subbuf_actor,
  883. read_actor_t actor,
  884. read_descriptor_t *desc)
  885. {
  886. struct rchan_buf *buf = filp->private_data;
  887. size_t read_start, avail;
  888. int ret;
  889. if (!desc->count)
  890. return 0;
  891. mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
  892. do {
  893. if (!relay_file_read_avail(buf, *ppos))
  894. break;
  895. read_start = relay_file_read_start_pos(*ppos, buf);
  896. avail = relay_file_read_subbuf_avail(read_start, buf);
  897. if (!avail)
  898. break;
  899. avail = min(desc->count, avail);
  900. ret = subbuf_actor(read_start, buf, avail, desc, actor);
  901. if (desc->error < 0)
  902. break;
  903. if (ret) {
  904. relay_file_read_consume(buf, read_start, ret);
  905. *ppos = relay_file_read_end_pos(buf, read_start, ret);
  906. }
  907. } while (desc->count && ret);
  908. mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
  909. return desc->written;
  910. }
  911. static ssize_t relay_file_read(struct file *filp,
  912. char __user *buffer,
  913. size_t count,
  914. loff_t *ppos)
  915. {
  916. read_descriptor_t desc;
  917. desc.written = 0;
  918. desc.count = count;
  919. desc.arg.buf = buffer;
  920. desc.error = 0;
  921. return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,
  922. NULL, &desc);
  923. }
  924. static ssize_t relay_file_sendfile(struct file *filp,
  925. loff_t *ppos,
  926. size_t count,
  927. read_actor_t actor,
  928. void *target)
  929. {
  930. read_descriptor_t desc;
  931. desc.written = 0;
  932. desc.count = count;
  933. desc.arg.data = target;
  934. desc.error = 0;
  935. return relay_file_read_subbufs(filp, ppos, subbuf_send_actor,
  936. actor, &desc);
  937. }
  938. const struct file_operations relay_file_operations = {
  939. .open = relay_file_open,
  940. .poll = relay_file_poll,
  941. .mmap = relay_file_mmap,
  942. .read = relay_file_read,
  943. .llseek = no_llseek,
  944. .release = relay_file_release,
  945. .sendfile = relay_file_sendfile,
  946. };
  947. EXPORT_SYMBOL_GPL(relay_file_operations);
  948. static __init int relay_init(void)
  949. {
  950. hotcpu_notifier(relay_hotcpu_callback, 0);
  951. return 0;
  952. }
  953. module_init(relay_init);