relay.c 32 KB

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