relay.c 33 KB

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