relay.c 29 KB

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