relay.c 32 KB

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