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