relay.c 32 KB

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