relay.c 34 KB

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