relay.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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_online_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_online_cpu(i) {
  538. if (!chan->buf[i])
  539. break;
  540. relay_close_buf(chan->buf[i]);
  541. }
  542. kref_put(&chan->kref, relay_destroy_channel);
  543. mutex_unlock(&relay_channels_mutex);
  544. return NULL;
  545. }
  546. EXPORT_SYMBOL_GPL(relay_open);
  547. struct rchan_percpu_buf_dispatcher {
  548. struct rchan_buf *buf;
  549. struct dentry *dentry;
  550. };
  551. /* Called in atomic context. */
  552. static void __relay_set_buf_dentry(void *info)
  553. {
  554. struct rchan_percpu_buf_dispatcher *p = info;
  555. relay_set_buf_dentry(p->buf, p->dentry);
  556. }
  557. /**
  558. * relay_late_setup_files - triggers file creation
  559. * @chan: channel to operate on
  560. * @base_filename: base name of files to create
  561. * @parent: dentry of parent directory, %NULL for root directory
  562. *
  563. * Returns 0 if successful, non-zero otherwise.
  564. *
  565. * Use to setup files for a previously buffer-only channel.
  566. * Useful to do early tracing in kernel, before VFS is up, for example.
  567. */
  568. int relay_late_setup_files(struct rchan *chan,
  569. const char *base_filename,
  570. struct dentry *parent)
  571. {
  572. int err = 0;
  573. unsigned int i, curr_cpu;
  574. unsigned long flags;
  575. struct dentry *dentry;
  576. struct rchan_percpu_buf_dispatcher disp;
  577. if (!chan || !base_filename)
  578. return -EINVAL;
  579. strlcpy(chan->base_filename, base_filename, NAME_MAX);
  580. mutex_lock(&relay_channels_mutex);
  581. /* Is chan already set up? */
  582. if (unlikely(chan->has_base_filename))
  583. return -EEXIST;
  584. chan->has_base_filename = 1;
  585. chan->parent = parent;
  586. curr_cpu = get_cpu();
  587. /*
  588. * The CPU hotplug notifier ran before us and created buffers with
  589. * no files associated. So it's safe to call relay_setup_buf_file()
  590. * on all currently online CPUs.
  591. */
  592. for_each_online_cpu(i) {
  593. if (unlikely(!chan->buf[i])) {
  594. printk(KERN_ERR "relay_late_setup_files: CPU %u "
  595. "has no buffer, it must have!\n", i);
  596. BUG();
  597. err = -EINVAL;
  598. break;
  599. }
  600. dentry = relay_create_buf_file(chan, chan->buf[i], i);
  601. if (unlikely(!dentry)) {
  602. err = -EINVAL;
  603. break;
  604. }
  605. if (curr_cpu == i) {
  606. local_irq_save(flags);
  607. relay_set_buf_dentry(chan->buf[i], dentry);
  608. local_irq_restore(flags);
  609. } else {
  610. disp.buf = chan->buf[i];
  611. disp.dentry = dentry;
  612. smp_mb();
  613. /* relay_channels_mutex must be held, so wait. */
  614. err = smp_call_function_single(i,
  615. __relay_set_buf_dentry,
  616. &disp, 1);
  617. }
  618. if (unlikely(err))
  619. break;
  620. }
  621. put_cpu();
  622. mutex_unlock(&relay_channels_mutex);
  623. return err;
  624. }
  625. /**
  626. * relay_switch_subbuf - switch to a new sub-buffer
  627. * @buf: channel buffer
  628. * @length: size of current event
  629. *
  630. * Returns either the length passed in or 0 if full.
  631. *
  632. * Performs sub-buffer-switch tasks such as invoking callbacks,
  633. * updating padding counts, waking up readers, etc.
  634. */
  635. size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  636. {
  637. void *old, *new;
  638. size_t old_subbuf, new_subbuf;
  639. if (unlikely(length > buf->chan->subbuf_size))
  640. goto toobig;
  641. if (buf->offset != buf->chan->subbuf_size + 1) {
  642. buf->prev_padding = buf->chan->subbuf_size - buf->offset;
  643. old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  644. buf->padding[old_subbuf] = buf->prev_padding;
  645. buf->subbufs_produced++;
  646. if (buf->dentry)
  647. buf->dentry->d_inode->i_size +=
  648. buf->chan->subbuf_size -
  649. buf->padding[old_subbuf];
  650. else
  651. buf->early_bytes += buf->chan->subbuf_size -
  652. buf->padding[old_subbuf];
  653. smp_mb();
  654. if (waitqueue_active(&buf->read_wait))
  655. /*
  656. * Calling wake_up_interruptible() from here
  657. * will deadlock if we happen to be logging
  658. * from the scheduler (trying to re-grab
  659. * rq->lock), so defer it.
  660. */
  661. __mod_timer(&buf->timer, jiffies + 1);
  662. }
  663. old = buf->data;
  664. new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  665. new = buf->start + new_subbuf * buf->chan->subbuf_size;
  666. buf->offset = 0;
  667. if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
  668. buf->offset = buf->chan->subbuf_size + 1;
  669. return 0;
  670. }
  671. buf->data = new;
  672. buf->padding[new_subbuf] = 0;
  673. if (unlikely(length + buf->offset > buf->chan->subbuf_size))
  674. goto toobig;
  675. return length;
  676. toobig:
  677. buf->chan->last_toobig = length;
  678. return 0;
  679. }
  680. EXPORT_SYMBOL_GPL(relay_switch_subbuf);
  681. /**
  682. * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
  683. * @chan: the channel
  684. * @cpu: the cpu associated with the channel buffer to update
  685. * @subbufs_consumed: number of sub-buffers to add to current buf's count
  686. *
  687. * Adds to the channel buffer's consumed sub-buffer count.
  688. * subbufs_consumed should be the number of sub-buffers newly consumed,
  689. * not the total consumed.
  690. *
  691. * NOTE. Kernel clients don't need to call this function if the channel
  692. * mode is 'overwrite'.
  693. */
  694. void relay_subbufs_consumed(struct rchan *chan,
  695. unsigned int cpu,
  696. size_t subbufs_consumed)
  697. {
  698. struct rchan_buf *buf;
  699. if (!chan)
  700. return;
  701. if (cpu >= NR_CPUS || !chan->buf[cpu])
  702. return;
  703. buf = chan->buf[cpu];
  704. buf->subbufs_consumed += subbufs_consumed;
  705. if (buf->subbufs_consumed > buf->subbufs_produced)
  706. buf->subbufs_consumed = buf->subbufs_produced;
  707. }
  708. EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
  709. /**
  710. * relay_close - close the channel
  711. * @chan: the channel
  712. *
  713. * Closes all channel buffers and frees the channel.
  714. */
  715. void relay_close(struct rchan *chan)
  716. {
  717. unsigned int i;
  718. if (!chan)
  719. return;
  720. mutex_lock(&relay_channels_mutex);
  721. if (chan->is_global && chan->buf[0])
  722. relay_close_buf(chan->buf[0]);
  723. else
  724. for_each_possible_cpu(i)
  725. if (chan->buf[i])
  726. relay_close_buf(chan->buf[i]);
  727. if (chan->last_toobig)
  728. printk(KERN_WARNING "relay: one or more items not logged "
  729. "[item size (%Zd) > sub-buffer size (%Zd)]\n",
  730. chan->last_toobig, chan->subbuf_size);
  731. list_del(&chan->list);
  732. kref_put(&chan->kref, relay_destroy_channel);
  733. mutex_unlock(&relay_channels_mutex);
  734. }
  735. EXPORT_SYMBOL_GPL(relay_close);
  736. /**
  737. * relay_flush - close the channel
  738. * @chan: the channel
  739. *
  740. * Flushes all channel buffers, i.e. forces buffer switch.
  741. */
  742. void relay_flush(struct rchan *chan)
  743. {
  744. unsigned int i;
  745. if (!chan)
  746. return;
  747. if (chan->is_global && chan->buf[0]) {
  748. relay_switch_subbuf(chan->buf[0], 0);
  749. return;
  750. }
  751. mutex_lock(&relay_channels_mutex);
  752. for_each_possible_cpu(i)
  753. if (chan->buf[i])
  754. relay_switch_subbuf(chan->buf[i], 0);
  755. mutex_unlock(&relay_channels_mutex);
  756. }
  757. EXPORT_SYMBOL_GPL(relay_flush);
  758. /**
  759. * relay_file_open - open file op for relay files
  760. * @inode: the inode
  761. * @filp: the file
  762. *
  763. * Increments the channel buffer refcount.
  764. */
  765. static int relay_file_open(struct inode *inode, struct file *filp)
  766. {
  767. struct rchan_buf *buf = inode->i_private;
  768. kref_get(&buf->kref);
  769. filp->private_data = buf;
  770. return nonseekable_open(inode, filp);
  771. }
  772. /**
  773. * relay_file_mmap - mmap file op for relay files
  774. * @filp: the file
  775. * @vma: the vma describing what to map
  776. *
  777. * Calls upon relay_mmap_buf() to map the file into user space.
  778. */
  779. static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
  780. {
  781. struct rchan_buf *buf = filp->private_data;
  782. return relay_mmap_buf(buf, vma);
  783. }
  784. /**
  785. * relay_file_poll - poll file op for relay files
  786. * @filp: the file
  787. * @wait: poll table
  788. *
  789. * Poll implemention.
  790. */
  791. static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
  792. {
  793. unsigned int mask = 0;
  794. struct rchan_buf *buf = filp->private_data;
  795. if (buf->finalized)
  796. return POLLERR;
  797. if (filp->f_mode & FMODE_READ) {
  798. poll_wait(filp, &buf->read_wait, wait);
  799. if (!relay_buf_empty(buf))
  800. mask |= POLLIN | POLLRDNORM;
  801. }
  802. return mask;
  803. }
  804. /**
  805. * relay_file_release - release file op for relay files
  806. * @inode: the inode
  807. * @filp: the file
  808. *
  809. * Decrements the channel refcount, as the filesystem is
  810. * no longer using it.
  811. */
  812. static int relay_file_release(struct inode *inode, struct file *filp)
  813. {
  814. struct rchan_buf *buf = filp->private_data;
  815. kref_put(&buf->kref, relay_remove_buf);
  816. return 0;
  817. }
  818. /*
  819. * relay_file_read_consume - update the consumed count for the buffer
  820. */
  821. static void relay_file_read_consume(struct rchan_buf *buf,
  822. size_t read_pos,
  823. size_t bytes_consumed)
  824. {
  825. size_t subbuf_size = buf->chan->subbuf_size;
  826. size_t n_subbufs = buf->chan->n_subbufs;
  827. size_t read_subbuf;
  828. if (buf->subbufs_produced == buf->subbufs_consumed &&
  829. buf->offset == buf->bytes_consumed)
  830. return;
  831. if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
  832. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  833. buf->bytes_consumed = 0;
  834. }
  835. buf->bytes_consumed += bytes_consumed;
  836. if (!read_pos)
  837. read_subbuf = buf->subbufs_consumed % n_subbufs;
  838. else
  839. read_subbuf = read_pos / buf->chan->subbuf_size;
  840. if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
  841. if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
  842. (buf->offset == subbuf_size))
  843. return;
  844. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  845. buf->bytes_consumed = 0;
  846. }
  847. }
  848. /*
  849. * relay_file_read_avail - boolean, are there unconsumed bytes available?
  850. */
  851. static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
  852. {
  853. size_t subbuf_size = buf->chan->subbuf_size;
  854. size_t n_subbufs = buf->chan->n_subbufs;
  855. size_t produced = buf->subbufs_produced;
  856. size_t consumed = buf->subbufs_consumed;
  857. relay_file_read_consume(buf, read_pos, 0);
  858. consumed = buf->subbufs_consumed;
  859. if (unlikely(buf->offset > subbuf_size)) {
  860. if (produced == consumed)
  861. return 0;
  862. return 1;
  863. }
  864. if (unlikely(produced - consumed >= n_subbufs)) {
  865. consumed = produced - n_subbufs + 1;
  866. buf->subbufs_consumed = consumed;
  867. buf->bytes_consumed = 0;
  868. }
  869. produced = (produced % n_subbufs) * subbuf_size + buf->offset;
  870. consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
  871. if (consumed > produced)
  872. produced += n_subbufs * subbuf_size;
  873. if (consumed == produced) {
  874. if (buf->offset == subbuf_size &&
  875. buf->subbufs_produced > buf->subbufs_consumed)
  876. return 1;
  877. return 0;
  878. }
  879. return 1;
  880. }
  881. /**
  882. * relay_file_read_subbuf_avail - return bytes available in sub-buffer
  883. * @read_pos: file read position
  884. * @buf: relay channel buffer
  885. */
  886. static size_t relay_file_read_subbuf_avail(size_t read_pos,
  887. struct rchan_buf *buf)
  888. {
  889. size_t padding, avail = 0;
  890. size_t read_subbuf, read_offset, write_subbuf, write_offset;
  891. size_t subbuf_size = buf->chan->subbuf_size;
  892. write_subbuf = (buf->data - buf->start) / subbuf_size;
  893. write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
  894. read_subbuf = read_pos / subbuf_size;
  895. read_offset = read_pos % subbuf_size;
  896. padding = buf->padding[read_subbuf];
  897. if (read_subbuf == write_subbuf) {
  898. if (read_offset + padding < write_offset)
  899. avail = write_offset - (read_offset + padding);
  900. } else
  901. avail = (subbuf_size - padding) - read_offset;
  902. return avail;
  903. }
  904. /**
  905. * relay_file_read_start_pos - find the first available byte to read
  906. * @read_pos: file read position
  907. * @buf: relay channel buffer
  908. *
  909. * If the @read_pos is in the middle of padding, return the
  910. * position of the first actually available byte, otherwise
  911. * return the original value.
  912. */
  913. static size_t relay_file_read_start_pos(size_t read_pos,
  914. struct rchan_buf *buf)
  915. {
  916. size_t read_subbuf, padding, padding_start, padding_end;
  917. size_t subbuf_size = buf->chan->subbuf_size;
  918. size_t n_subbufs = buf->chan->n_subbufs;
  919. size_t consumed = buf->subbufs_consumed % n_subbufs;
  920. if (!read_pos)
  921. read_pos = consumed * subbuf_size + buf->bytes_consumed;
  922. read_subbuf = read_pos / subbuf_size;
  923. padding = buf->padding[read_subbuf];
  924. padding_start = (read_subbuf + 1) * subbuf_size - padding;
  925. padding_end = (read_subbuf + 1) * subbuf_size;
  926. if (read_pos >= padding_start && read_pos < padding_end) {
  927. read_subbuf = (read_subbuf + 1) % n_subbufs;
  928. read_pos = read_subbuf * subbuf_size;
  929. }
  930. return read_pos;
  931. }
  932. /**
  933. * relay_file_read_end_pos - return the new read position
  934. * @read_pos: file read position
  935. * @buf: relay channel buffer
  936. * @count: number of bytes to be read
  937. */
  938. static size_t relay_file_read_end_pos(struct rchan_buf *buf,
  939. size_t read_pos,
  940. size_t count)
  941. {
  942. size_t read_subbuf, padding, end_pos;
  943. size_t subbuf_size = buf->chan->subbuf_size;
  944. size_t n_subbufs = buf->chan->n_subbufs;
  945. read_subbuf = read_pos / subbuf_size;
  946. padding = buf->padding[read_subbuf];
  947. if (read_pos % subbuf_size + count + padding == subbuf_size)
  948. end_pos = (read_subbuf + 1) * subbuf_size;
  949. else
  950. end_pos = read_pos + count;
  951. if (end_pos >= subbuf_size * n_subbufs)
  952. end_pos = 0;
  953. return end_pos;
  954. }
  955. /*
  956. * subbuf_read_actor - read up to one subbuf's worth of data
  957. */
  958. static int subbuf_read_actor(size_t read_start,
  959. struct rchan_buf *buf,
  960. size_t avail,
  961. read_descriptor_t *desc,
  962. read_actor_t actor)
  963. {
  964. void *from;
  965. int ret = 0;
  966. from = buf->start + read_start;
  967. ret = avail;
  968. if (copy_to_user(desc->arg.buf, from, avail)) {
  969. desc->error = -EFAULT;
  970. ret = 0;
  971. }
  972. desc->arg.data += ret;
  973. desc->written += ret;
  974. desc->count -= ret;
  975. return ret;
  976. }
  977. typedef int (*subbuf_actor_t) (size_t read_start,
  978. struct rchan_buf *buf,
  979. size_t avail,
  980. read_descriptor_t *desc,
  981. read_actor_t actor);
  982. /*
  983. * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
  984. */
  985. static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,
  986. subbuf_actor_t subbuf_actor,
  987. read_actor_t actor,
  988. read_descriptor_t *desc)
  989. {
  990. struct rchan_buf *buf = filp->private_data;
  991. size_t read_start, avail;
  992. int ret;
  993. if (!desc->count)
  994. return 0;
  995. mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
  996. do {
  997. if (!relay_file_read_avail(buf, *ppos))
  998. break;
  999. read_start = relay_file_read_start_pos(*ppos, buf);
  1000. avail = relay_file_read_subbuf_avail(read_start, buf);
  1001. if (!avail)
  1002. break;
  1003. avail = min(desc->count, avail);
  1004. ret = subbuf_actor(read_start, buf, avail, desc, actor);
  1005. if (desc->error < 0)
  1006. break;
  1007. if (ret) {
  1008. relay_file_read_consume(buf, read_start, ret);
  1009. *ppos = relay_file_read_end_pos(buf, read_start, ret);
  1010. }
  1011. } while (desc->count && ret);
  1012. mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
  1013. return desc->written;
  1014. }
  1015. static ssize_t relay_file_read(struct file *filp,
  1016. char __user *buffer,
  1017. size_t count,
  1018. loff_t *ppos)
  1019. {
  1020. read_descriptor_t desc;
  1021. desc.written = 0;
  1022. desc.count = count;
  1023. desc.arg.buf = buffer;
  1024. desc.error = 0;
  1025. return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,
  1026. NULL, &desc);
  1027. }
  1028. static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
  1029. {
  1030. rbuf->bytes_consumed += bytes_consumed;
  1031. if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
  1032. relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
  1033. rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
  1034. }
  1035. }
  1036. static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
  1037. struct pipe_buffer *buf)
  1038. {
  1039. struct rchan_buf *rbuf;
  1040. rbuf = (struct rchan_buf *)page_private(buf->page);
  1041. relay_consume_bytes(rbuf, buf->private);
  1042. }
  1043. static struct pipe_buf_operations relay_pipe_buf_ops = {
  1044. .can_merge = 0,
  1045. .map = generic_pipe_buf_map,
  1046. .unmap = generic_pipe_buf_unmap,
  1047. .confirm = generic_pipe_buf_confirm,
  1048. .release = relay_pipe_buf_release,
  1049. .steal = generic_pipe_buf_steal,
  1050. .get = generic_pipe_buf_get,
  1051. };
  1052. static void relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
  1053. {
  1054. }
  1055. /*
  1056. * subbuf_splice_actor - splice up to one subbuf's worth of data
  1057. */
  1058. static int subbuf_splice_actor(struct file *in,
  1059. loff_t *ppos,
  1060. struct pipe_inode_info *pipe,
  1061. size_t len,
  1062. unsigned int flags,
  1063. int *nonpad_ret)
  1064. {
  1065. unsigned int pidx, poff, total_len, subbuf_pages, nr_pages, ret;
  1066. struct rchan_buf *rbuf = in->private_data;
  1067. unsigned int subbuf_size = rbuf->chan->subbuf_size;
  1068. uint64_t pos = (uint64_t) *ppos;
  1069. uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size;
  1070. size_t read_start = (size_t) do_div(pos, alloc_size);
  1071. size_t read_subbuf = read_start / subbuf_size;
  1072. size_t padding = rbuf->padding[read_subbuf];
  1073. size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
  1074. struct page *pages[PIPE_BUFFERS];
  1075. struct partial_page partial[PIPE_BUFFERS];
  1076. struct splice_pipe_desc spd = {
  1077. .pages = pages,
  1078. .nr_pages = 0,
  1079. .partial = partial,
  1080. .flags = flags,
  1081. .ops = &relay_pipe_buf_ops,
  1082. .spd_release = relay_page_release,
  1083. };
  1084. if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
  1085. return 0;
  1086. /*
  1087. * Adjust read len, if longer than what is available
  1088. */
  1089. if (len > (subbuf_size - read_start % subbuf_size))
  1090. len = subbuf_size - read_start % subbuf_size;
  1091. subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
  1092. pidx = (read_start / PAGE_SIZE) % subbuf_pages;
  1093. poff = read_start & ~PAGE_MASK;
  1094. nr_pages = min_t(unsigned int, subbuf_pages, PIPE_BUFFERS);
  1095. for (total_len = 0; spd.nr_pages < nr_pages; spd.nr_pages++) {
  1096. unsigned int this_len, this_end, private;
  1097. unsigned int cur_pos = read_start + total_len;
  1098. if (!len)
  1099. break;
  1100. this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
  1101. private = this_len;
  1102. spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
  1103. spd.partial[spd.nr_pages].offset = poff;
  1104. this_end = cur_pos + this_len;
  1105. if (this_end >= nonpad_end) {
  1106. this_len = nonpad_end - cur_pos;
  1107. private = this_len + padding;
  1108. }
  1109. spd.partial[spd.nr_pages].len = this_len;
  1110. spd.partial[spd.nr_pages].private = private;
  1111. len -= this_len;
  1112. total_len += this_len;
  1113. poff = 0;
  1114. pidx = (pidx + 1) % subbuf_pages;
  1115. if (this_end >= nonpad_end) {
  1116. spd.nr_pages++;
  1117. break;
  1118. }
  1119. }
  1120. if (!spd.nr_pages)
  1121. return 0;
  1122. ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
  1123. if (ret < 0 || ret < total_len)
  1124. return ret;
  1125. if (read_start + ret == nonpad_end)
  1126. ret += padding;
  1127. return ret;
  1128. }
  1129. static ssize_t relay_file_splice_read(struct file *in,
  1130. loff_t *ppos,
  1131. struct pipe_inode_info *pipe,
  1132. size_t len,
  1133. unsigned int flags)
  1134. {
  1135. ssize_t spliced;
  1136. int ret;
  1137. int nonpad_ret = 0;
  1138. ret = 0;
  1139. spliced = 0;
  1140. while (len && !spliced) {
  1141. ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret);
  1142. if (ret < 0)
  1143. break;
  1144. else if (!ret) {
  1145. if (spliced)
  1146. break;
  1147. if (flags & SPLICE_F_NONBLOCK) {
  1148. ret = -EAGAIN;
  1149. break;
  1150. }
  1151. }
  1152. *ppos += ret;
  1153. if (ret > len)
  1154. len = 0;
  1155. else
  1156. len -= ret;
  1157. spliced += nonpad_ret;
  1158. nonpad_ret = 0;
  1159. }
  1160. if (spliced)
  1161. return spliced;
  1162. return ret;
  1163. }
  1164. const struct file_operations relay_file_operations = {
  1165. .open = relay_file_open,
  1166. .poll = relay_file_poll,
  1167. .mmap = relay_file_mmap,
  1168. .read = relay_file_read,
  1169. .llseek = no_llseek,
  1170. .release = relay_file_release,
  1171. .splice_read = relay_file_splice_read,
  1172. };
  1173. EXPORT_SYMBOL_GPL(relay_file_operations);
  1174. static __init int relay_init(void)
  1175. {
  1176. hotcpu_notifier(relay_hotcpu_callback, 0);
  1177. return 0;
  1178. }
  1179. early_initcall(relay_init);