relay.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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_area_struct *vma, struct vm_fault *vmf)
  40. {
  41. struct page *page;
  42. struct rchan_buf *buf = vma->vm_private_data;
  43. pgoff_t pgoff = vmf->pgoff;
  44. if (!buf)
  45. return VM_FAULT_OOM;
  46. page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT));
  47. if (!page)
  48. return VM_FAULT_SIGBUS;
  49. get_page(page);
  50. vmf->page = page;
  51. return 0;
  52. }
  53. /*
  54. * vm_ops for relay file mappings.
  55. */
  56. static 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(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. return NULL;
  536. }
  537. EXPORT_SYMBOL_GPL(relay_open);
  538. struct rchan_percpu_buf_dispatcher {
  539. struct rchan_buf *buf;
  540. struct dentry *dentry;
  541. };
  542. /* Called in atomic context. */
  543. static void __relay_set_buf_dentry(void *info)
  544. {
  545. struct rchan_percpu_buf_dispatcher *p = info;
  546. relay_set_buf_dentry(p->buf, p->dentry);
  547. }
  548. /**
  549. * relay_late_setup_files - triggers file creation
  550. * @chan: channel to operate on
  551. * @base_filename: base name of files to create
  552. * @parent: dentry of parent directory, %NULL for root directory
  553. *
  554. * Returns 0 if successful, non-zero otherwise.
  555. *
  556. * Use to setup files for a previously buffer-only channel created
  557. * by relay_open() with a NULL parent dentry.
  558. *
  559. * For example, this is useful for perfomring early tracing in kernel,
  560. * before VFS is up and then exposing the early results once the dentry
  561. * is available.
  562. */
  563. int relay_late_setup_files(struct rchan *chan,
  564. const char *base_filename,
  565. struct dentry *parent)
  566. {
  567. int err = 0;
  568. unsigned int i, curr_cpu;
  569. unsigned long flags;
  570. struct dentry *dentry;
  571. struct rchan_buf *buf;
  572. struct rchan_percpu_buf_dispatcher disp;
  573. if (!chan || !base_filename)
  574. return -EINVAL;
  575. strlcpy(chan->base_filename, base_filename, NAME_MAX);
  576. mutex_lock(&relay_channels_mutex);
  577. /* Is chan already set up? */
  578. if (unlikely(chan->has_base_filename)) {
  579. mutex_unlock(&relay_channels_mutex);
  580. return -EEXIST;
  581. }
  582. chan->has_base_filename = 1;
  583. chan->parent = parent;
  584. if (chan->is_global) {
  585. err = -EINVAL;
  586. buf = *per_cpu_ptr(chan->buf, 0);
  587. if (!WARN_ON_ONCE(!buf)) {
  588. dentry = relay_create_buf_file(chan, buf, 0);
  589. if (dentry && !WARN_ON_ONCE(!chan->is_global)) {
  590. relay_set_buf_dentry(buf, dentry);
  591. err = 0;
  592. }
  593. }
  594. mutex_unlock(&relay_channels_mutex);
  595. return err;
  596. }
  597. curr_cpu = get_cpu();
  598. /*
  599. * The CPU hotplug notifier ran before us and created buffers with
  600. * no files associated. So it's safe to call relay_setup_buf_file()
  601. * on all currently online CPUs.
  602. */
  603. for_each_online_cpu(i) {
  604. buf = *per_cpu_ptr(chan->buf, i);
  605. if (unlikely(!buf)) {
  606. WARN_ONCE(1, KERN_ERR "CPU has no buffer!\n");
  607. err = -EINVAL;
  608. break;
  609. }
  610. dentry = relay_create_buf_file(chan, buf, i);
  611. if (unlikely(!dentry)) {
  612. err = -EINVAL;
  613. break;
  614. }
  615. if (curr_cpu == i) {
  616. local_irq_save(flags);
  617. relay_set_buf_dentry(buf, dentry);
  618. local_irq_restore(flags);
  619. } else {
  620. disp.buf = buf;
  621. disp.dentry = dentry;
  622. smp_mb();
  623. /* relay_channels_mutex must be held, so wait. */
  624. err = smp_call_function_single(i,
  625. __relay_set_buf_dentry,
  626. &disp, 1);
  627. }
  628. if (unlikely(err))
  629. break;
  630. }
  631. put_cpu();
  632. mutex_unlock(&relay_channels_mutex);
  633. return err;
  634. }
  635. EXPORT_SYMBOL_GPL(relay_late_setup_files);
  636. /**
  637. * relay_switch_subbuf - switch to a new sub-buffer
  638. * @buf: channel buffer
  639. * @length: size of current event
  640. *
  641. * Returns either the length passed in or 0 if full.
  642. *
  643. * Performs sub-buffer-switch tasks such as invoking callbacks,
  644. * updating padding counts, waking up readers, etc.
  645. */
  646. size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  647. {
  648. void *old, *new;
  649. size_t old_subbuf, new_subbuf;
  650. if (unlikely(length > buf->chan->subbuf_size))
  651. goto toobig;
  652. if (buf->offset != buf->chan->subbuf_size + 1) {
  653. buf->prev_padding = buf->chan->subbuf_size - buf->offset;
  654. old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  655. buf->padding[old_subbuf] = buf->prev_padding;
  656. buf->subbufs_produced++;
  657. if (buf->dentry)
  658. d_inode(buf->dentry)->i_size +=
  659. buf->chan->subbuf_size -
  660. buf->padding[old_subbuf];
  661. else
  662. buf->early_bytes += buf->chan->subbuf_size -
  663. buf->padding[old_subbuf];
  664. smp_mb();
  665. if (waitqueue_active(&buf->read_wait)) {
  666. /*
  667. * Calling wake_up_interruptible() from here
  668. * will deadlock if we happen to be logging
  669. * from the scheduler (trying to re-grab
  670. * rq->lock), so defer it.
  671. */
  672. irq_work_queue(&buf->wakeup_work);
  673. }
  674. }
  675. old = buf->data;
  676. new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  677. new = buf->start + new_subbuf * buf->chan->subbuf_size;
  678. buf->offset = 0;
  679. if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
  680. buf->offset = buf->chan->subbuf_size + 1;
  681. return 0;
  682. }
  683. buf->data = new;
  684. buf->padding[new_subbuf] = 0;
  685. if (unlikely(length + buf->offset > buf->chan->subbuf_size))
  686. goto toobig;
  687. return length;
  688. toobig:
  689. buf->chan->last_toobig = length;
  690. return 0;
  691. }
  692. EXPORT_SYMBOL_GPL(relay_switch_subbuf);
  693. /**
  694. * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
  695. * @chan: the channel
  696. * @cpu: the cpu associated with the channel buffer to update
  697. * @subbufs_consumed: number of sub-buffers to add to current buf's count
  698. *
  699. * Adds to the channel buffer's consumed sub-buffer count.
  700. * subbufs_consumed should be the number of sub-buffers newly consumed,
  701. * not the total consumed.
  702. *
  703. * NOTE. Kernel clients don't need to call this function if the channel
  704. * mode is 'overwrite'.
  705. */
  706. void relay_subbufs_consumed(struct rchan *chan,
  707. unsigned int cpu,
  708. size_t subbufs_consumed)
  709. {
  710. struct rchan_buf *buf;
  711. if (!chan || cpu >= NR_CPUS)
  712. return;
  713. buf = *per_cpu_ptr(chan->buf, cpu);
  714. if (!buf || subbufs_consumed > chan->n_subbufs)
  715. return;
  716. if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed)
  717. buf->subbufs_consumed = buf->subbufs_produced;
  718. else
  719. buf->subbufs_consumed += subbufs_consumed;
  720. }
  721. EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
  722. /**
  723. * relay_close - close the channel
  724. * @chan: the channel
  725. *
  726. * Closes all channel buffers and frees the channel.
  727. */
  728. void relay_close(struct rchan *chan)
  729. {
  730. struct rchan_buf *buf;
  731. unsigned int i;
  732. if (!chan)
  733. return;
  734. mutex_lock(&relay_channels_mutex);
  735. if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0)))
  736. relay_close_buf(buf);
  737. else
  738. for_each_possible_cpu(i)
  739. if ((buf = *per_cpu_ptr(chan->buf, i)))
  740. relay_close_buf(buf);
  741. if (chan->last_toobig)
  742. printk(KERN_WARNING "relay: one or more items not logged "
  743. "[item size (%Zd) > sub-buffer size (%Zd)]\n",
  744. chan->last_toobig, chan->subbuf_size);
  745. list_del(&chan->list);
  746. kref_put(&chan->kref, relay_destroy_channel);
  747. mutex_unlock(&relay_channels_mutex);
  748. }
  749. EXPORT_SYMBOL_GPL(relay_close);
  750. /**
  751. * relay_flush - close the channel
  752. * @chan: the channel
  753. *
  754. * Flushes all channel buffers, i.e. forces buffer switch.
  755. */
  756. void relay_flush(struct rchan *chan)
  757. {
  758. struct rchan_buf *buf;
  759. unsigned int i;
  760. if (!chan)
  761. return;
  762. if (chan->is_global && (buf = *per_cpu_ptr(chan->buf, 0))) {
  763. relay_switch_subbuf(buf, 0);
  764. return;
  765. }
  766. mutex_lock(&relay_channels_mutex);
  767. for_each_possible_cpu(i)
  768. if ((buf = *per_cpu_ptr(chan->buf, i)))
  769. relay_switch_subbuf(buf, 0);
  770. mutex_unlock(&relay_channels_mutex);
  771. }
  772. EXPORT_SYMBOL_GPL(relay_flush);
  773. /**
  774. * relay_file_open - open file op for relay files
  775. * @inode: the inode
  776. * @filp: the file
  777. *
  778. * Increments the channel buffer refcount.
  779. */
  780. static int relay_file_open(struct inode *inode, struct file *filp)
  781. {
  782. struct rchan_buf *buf = inode->i_private;
  783. kref_get(&buf->kref);
  784. filp->private_data = buf;
  785. return nonseekable_open(inode, filp);
  786. }
  787. /**
  788. * relay_file_mmap - mmap file op for relay files
  789. * @filp: the file
  790. * @vma: the vma describing what to map
  791. *
  792. * Calls upon relay_mmap_buf() to map the file into user space.
  793. */
  794. static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
  795. {
  796. struct rchan_buf *buf = filp->private_data;
  797. return relay_mmap_buf(buf, vma);
  798. }
  799. /**
  800. * relay_file_poll - poll file op for relay files
  801. * @filp: the file
  802. * @wait: poll table
  803. *
  804. * Poll implemention.
  805. */
  806. static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
  807. {
  808. unsigned int mask = 0;
  809. struct rchan_buf *buf = filp->private_data;
  810. if (buf->finalized)
  811. return POLLERR;
  812. if (filp->f_mode & FMODE_READ) {
  813. poll_wait(filp, &buf->read_wait, wait);
  814. if (!relay_buf_empty(buf))
  815. mask |= POLLIN | POLLRDNORM;
  816. }
  817. return mask;
  818. }
  819. /**
  820. * relay_file_release - release file op for relay files
  821. * @inode: the inode
  822. * @filp: the file
  823. *
  824. * Decrements the channel refcount, as the filesystem is
  825. * no longer using it.
  826. */
  827. static int relay_file_release(struct inode *inode, struct file *filp)
  828. {
  829. struct rchan_buf *buf = filp->private_data;
  830. kref_put(&buf->kref, relay_remove_buf);
  831. return 0;
  832. }
  833. /*
  834. * relay_file_read_consume - update the consumed count for the buffer
  835. */
  836. static void relay_file_read_consume(struct rchan_buf *buf,
  837. size_t read_pos,
  838. size_t bytes_consumed)
  839. {
  840. size_t subbuf_size = buf->chan->subbuf_size;
  841. size_t n_subbufs = buf->chan->n_subbufs;
  842. size_t read_subbuf;
  843. if (buf->subbufs_produced == buf->subbufs_consumed &&
  844. buf->offset == buf->bytes_consumed)
  845. return;
  846. if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
  847. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  848. buf->bytes_consumed = 0;
  849. }
  850. buf->bytes_consumed += bytes_consumed;
  851. if (!read_pos)
  852. read_subbuf = buf->subbufs_consumed % n_subbufs;
  853. else
  854. read_subbuf = read_pos / buf->chan->subbuf_size;
  855. if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
  856. if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
  857. (buf->offset == subbuf_size))
  858. return;
  859. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  860. buf->bytes_consumed = 0;
  861. }
  862. }
  863. /*
  864. * relay_file_read_avail - boolean, are there unconsumed bytes available?
  865. */
  866. static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
  867. {
  868. size_t subbuf_size = buf->chan->subbuf_size;
  869. size_t n_subbufs = buf->chan->n_subbufs;
  870. size_t produced = buf->subbufs_produced;
  871. size_t consumed = buf->subbufs_consumed;
  872. relay_file_read_consume(buf, read_pos, 0);
  873. consumed = buf->subbufs_consumed;
  874. if (unlikely(buf->offset > subbuf_size)) {
  875. if (produced == consumed)
  876. return 0;
  877. return 1;
  878. }
  879. if (unlikely(produced - consumed >= n_subbufs)) {
  880. consumed = produced - n_subbufs + 1;
  881. buf->subbufs_consumed = consumed;
  882. buf->bytes_consumed = 0;
  883. }
  884. produced = (produced % n_subbufs) * subbuf_size + buf->offset;
  885. consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
  886. if (consumed > produced)
  887. produced += n_subbufs * subbuf_size;
  888. if (consumed == produced) {
  889. if (buf->offset == subbuf_size &&
  890. buf->subbufs_produced > buf->subbufs_consumed)
  891. return 1;
  892. return 0;
  893. }
  894. return 1;
  895. }
  896. /**
  897. * relay_file_read_subbuf_avail - return bytes available in sub-buffer
  898. * @read_pos: file read position
  899. * @buf: relay channel buffer
  900. */
  901. static size_t relay_file_read_subbuf_avail(size_t read_pos,
  902. struct rchan_buf *buf)
  903. {
  904. size_t padding, avail = 0;
  905. size_t read_subbuf, read_offset, write_subbuf, write_offset;
  906. size_t subbuf_size = buf->chan->subbuf_size;
  907. write_subbuf = (buf->data - buf->start) / subbuf_size;
  908. write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
  909. read_subbuf = read_pos / subbuf_size;
  910. read_offset = read_pos % subbuf_size;
  911. padding = buf->padding[read_subbuf];
  912. if (read_subbuf == write_subbuf) {
  913. if (read_offset + padding < write_offset)
  914. avail = write_offset - (read_offset + padding);
  915. } else
  916. avail = (subbuf_size - padding) - read_offset;
  917. return avail;
  918. }
  919. /**
  920. * relay_file_read_start_pos - find the first available byte to read
  921. * @read_pos: file read position
  922. * @buf: relay channel buffer
  923. *
  924. * If the @read_pos is in the middle of padding, return the
  925. * position of the first actually available byte, otherwise
  926. * return the original value.
  927. */
  928. static size_t relay_file_read_start_pos(size_t read_pos,
  929. struct rchan_buf *buf)
  930. {
  931. size_t read_subbuf, padding, padding_start, padding_end;
  932. size_t subbuf_size = buf->chan->subbuf_size;
  933. size_t n_subbufs = buf->chan->n_subbufs;
  934. size_t consumed = buf->subbufs_consumed % n_subbufs;
  935. if (!read_pos)
  936. read_pos = consumed * subbuf_size + buf->bytes_consumed;
  937. read_subbuf = read_pos / subbuf_size;
  938. padding = buf->padding[read_subbuf];
  939. padding_start = (read_subbuf + 1) * subbuf_size - padding;
  940. padding_end = (read_subbuf + 1) * subbuf_size;
  941. if (read_pos >= padding_start && read_pos < padding_end) {
  942. read_subbuf = (read_subbuf + 1) % n_subbufs;
  943. read_pos = read_subbuf * subbuf_size;
  944. }
  945. return read_pos;
  946. }
  947. /**
  948. * relay_file_read_end_pos - return the new read position
  949. * @read_pos: file read position
  950. * @buf: relay channel buffer
  951. * @count: number of bytes to be read
  952. */
  953. static size_t relay_file_read_end_pos(struct rchan_buf *buf,
  954. size_t read_pos,
  955. size_t count)
  956. {
  957. size_t read_subbuf, padding, end_pos;
  958. size_t subbuf_size = buf->chan->subbuf_size;
  959. size_t n_subbufs = buf->chan->n_subbufs;
  960. read_subbuf = read_pos / subbuf_size;
  961. padding = buf->padding[read_subbuf];
  962. if (read_pos % subbuf_size + count + padding == subbuf_size)
  963. end_pos = (read_subbuf + 1) * subbuf_size;
  964. else
  965. end_pos = read_pos + count;
  966. if (end_pos >= subbuf_size * n_subbufs)
  967. end_pos = 0;
  968. return end_pos;
  969. }
  970. static ssize_t relay_file_read(struct file *filp,
  971. char __user *buffer,
  972. size_t count,
  973. loff_t *ppos)
  974. {
  975. struct rchan_buf *buf = filp->private_data;
  976. size_t read_start, avail;
  977. size_t written = 0;
  978. int ret;
  979. if (!count)
  980. return 0;
  981. inode_lock(file_inode(filp));
  982. do {
  983. void *from;
  984. if (!relay_file_read_avail(buf, *ppos))
  985. break;
  986. read_start = relay_file_read_start_pos(*ppos, buf);
  987. avail = relay_file_read_subbuf_avail(read_start, buf);
  988. if (!avail)
  989. break;
  990. avail = min(count, avail);
  991. from = buf->start + read_start;
  992. ret = avail;
  993. if (copy_to_user(buffer, from, avail))
  994. break;
  995. buffer += ret;
  996. written += ret;
  997. count -= ret;
  998. relay_file_read_consume(buf, read_start, ret);
  999. *ppos = relay_file_read_end_pos(buf, read_start, ret);
  1000. } while (count);
  1001. inode_unlock(file_inode(filp));
  1002. return written;
  1003. }
  1004. static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
  1005. {
  1006. rbuf->bytes_consumed += bytes_consumed;
  1007. if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
  1008. relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
  1009. rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
  1010. }
  1011. }
  1012. static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
  1013. struct pipe_buffer *buf)
  1014. {
  1015. struct rchan_buf *rbuf;
  1016. rbuf = (struct rchan_buf *)page_private(buf->page);
  1017. relay_consume_bytes(rbuf, buf->private);
  1018. }
  1019. static const struct pipe_buf_operations relay_pipe_buf_ops = {
  1020. .can_merge = 0,
  1021. .confirm = generic_pipe_buf_confirm,
  1022. .release = relay_pipe_buf_release,
  1023. .steal = generic_pipe_buf_steal,
  1024. .get = generic_pipe_buf_get,
  1025. };
  1026. static void relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
  1027. {
  1028. }
  1029. /*
  1030. * subbuf_splice_actor - splice up to one subbuf's worth of data
  1031. */
  1032. static ssize_t subbuf_splice_actor(struct file *in,
  1033. loff_t *ppos,
  1034. struct pipe_inode_info *pipe,
  1035. size_t len,
  1036. unsigned int flags,
  1037. int *nonpad_ret)
  1038. {
  1039. unsigned int pidx, poff, total_len, subbuf_pages, nr_pages;
  1040. struct rchan_buf *rbuf = in->private_data;
  1041. unsigned int subbuf_size = rbuf->chan->subbuf_size;
  1042. uint64_t pos = (uint64_t) *ppos;
  1043. uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size;
  1044. size_t read_start = (size_t) do_div(pos, alloc_size);
  1045. size_t read_subbuf = read_start / subbuf_size;
  1046. size_t padding = rbuf->padding[read_subbuf];
  1047. size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
  1048. struct page *pages[PIPE_DEF_BUFFERS];
  1049. struct partial_page partial[PIPE_DEF_BUFFERS];
  1050. struct splice_pipe_desc spd = {
  1051. .pages = pages,
  1052. .nr_pages = 0,
  1053. .nr_pages_max = PIPE_DEF_BUFFERS,
  1054. .partial = partial,
  1055. .flags = flags,
  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);