relay.c 29 KB

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