xpc_main.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /*
  9. * Cross Partition Communication (XPC) support - standard version.
  10. *
  11. * XPC provides a message passing capability that crosses partition
  12. * boundaries. This module is made up of two parts:
  13. *
  14. * partition This part detects the presence/absence of other
  15. * partitions. It provides a heartbeat and monitors
  16. * the heartbeats of other partitions.
  17. *
  18. * channel This part manages the channels and sends/receives
  19. * messages across them to/from other partitions.
  20. *
  21. * There are a couple of additional functions residing in XP, which
  22. * provide an interface to XPC for its users.
  23. *
  24. *
  25. * Caveats:
  26. *
  27. * . Currently on sn2, we have no way to determine which nasid an IRQ
  28. * came from. Thus, xpc_send_IRQ_sn2() does a remote amo write
  29. * followed by an IPI. The amo indicates where data is to be pulled
  30. * from, so after the IPI arrives, the remote partition checks the amo
  31. * word. The IPI can actually arrive before the amo however, so other
  32. * code must periodically check for this case. Also, remote amo
  33. * operations do not reliably time out. Thus we do a remote PIO read
  34. * solely to know whether the remote partition is down and whether we
  35. * should stop sending IPIs to it. This remote PIO read operation is
  36. * set up in a special nofault region so SAL knows to ignore (and
  37. * cleanup) any errors due to the remote amo write, PIO read, and/or
  38. * PIO write operations.
  39. *
  40. * If/when new hardware solves this IPI problem, we should abandon
  41. * the current approach.
  42. *
  43. */
  44. #include <linux/module.h>
  45. #include <linux/sysctl.h>
  46. #include <linux/device.h>
  47. #include <linux/delay.h>
  48. #include <linux/reboot.h>
  49. #include <linux/kdebug.h>
  50. #include <linux/kthread.h>
  51. #include "xpc.h"
  52. /* define two XPC debug device structures to be used with dev_dbg() et al */
  53. struct device_driver xpc_dbg_name = {
  54. .name = "xpc"
  55. };
  56. struct device xpc_part_dbg_subname = {
  57. .bus_id = {0}, /* set to "part" at xpc_init() time */
  58. .driver = &xpc_dbg_name
  59. };
  60. struct device xpc_chan_dbg_subname = {
  61. .bus_id = {0}, /* set to "chan" at xpc_init() time */
  62. .driver = &xpc_dbg_name
  63. };
  64. struct device *xpc_part = &xpc_part_dbg_subname;
  65. struct device *xpc_chan = &xpc_chan_dbg_subname;
  66. static int xpc_kdebug_ignore;
  67. /* systune related variables for /proc/sys directories */
  68. static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
  69. static int xpc_hb_min_interval = 1;
  70. static int xpc_hb_max_interval = 10;
  71. static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
  72. static int xpc_hb_check_min_interval = 10;
  73. static int xpc_hb_check_max_interval = 120;
  74. int xpc_disengage_timelimit = XPC_DISENGAGE_DEFAULT_TIMELIMIT;
  75. static int xpc_disengage_min_timelimit; /* = 0 */
  76. static int xpc_disengage_max_timelimit = 120;
  77. static ctl_table xpc_sys_xpc_hb_dir[] = {
  78. {
  79. .ctl_name = CTL_UNNUMBERED,
  80. .procname = "hb_interval",
  81. .data = &xpc_hb_interval,
  82. .maxlen = sizeof(int),
  83. .mode = 0644,
  84. .proc_handler = &proc_dointvec_minmax,
  85. .strategy = &sysctl_intvec,
  86. .extra1 = &xpc_hb_min_interval,
  87. .extra2 = &xpc_hb_max_interval},
  88. {
  89. .ctl_name = CTL_UNNUMBERED,
  90. .procname = "hb_check_interval",
  91. .data = &xpc_hb_check_interval,
  92. .maxlen = sizeof(int),
  93. .mode = 0644,
  94. .proc_handler = &proc_dointvec_minmax,
  95. .strategy = &sysctl_intvec,
  96. .extra1 = &xpc_hb_check_min_interval,
  97. .extra2 = &xpc_hb_check_max_interval},
  98. {}
  99. };
  100. static ctl_table xpc_sys_xpc_dir[] = {
  101. {
  102. .ctl_name = CTL_UNNUMBERED,
  103. .procname = "hb",
  104. .mode = 0555,
  105. .child = xpc_sys_xpc_hb_dir},
  106. {
  107. .ctl_name = CTL_UNNUMBERED,
  108. .procname = "disengage_timelimit",
  109. .data = &xpc_disengage_timelimit,
  110. .maxlen = sizeof(int),
  111. .mode = 0644,
  112. .proc_handler = &proc_dointvec_minmax,
  113. .strategy = &sysctl_intvec,
  114. .extra1 = &xpc_disengage_min_timelimit,
  115. .extra2 = &xpc_disengage_max_timelimit},
  116. {}
  117. };
  118. static ctl_table xpc_sys_dir[] = {
  119. {
  120. .ctl_name = CTL_UNNUMBERED,
  121. .procname = "xpc",
  122. .mode = 0555,
  123. .child = xpc_sys_xpc_dir},
  124. {}
  125. };
  126. static struct ctl_table_header *xpc_sysctl;
  127. /* non-zero if any remote partition disengage was timed out */
  128. int xpc_disengage_timedout;
  129. /* #of activate IRQs received */
  130. atomic_t xpc_activate_IRQ_rcvd = ATOMIC_INIT(0);
  131. /* IRQ handler notifies this wait queue on receipt of an IRQ */
  132. DECLARE_WAIT_QUEUE_HEAD(xpc_activate_IRQ_wq);
  133. static unsigned long xpc_hb_check_timeout;
  134. static struct timer_list xpc_hb_timer;
  135. void *xpc_heartbeating_to_mask;
  136. /* notification that the xpc_hb_checker thread has exited */
  137. static DECLARE_COMPLETION(xpc_hb_checker_exited);
  138. /* notification that the xpc_discovery thread has exited */
  139. static DECLARE_COMPLETION(xpc_discovery_exited);
  140. static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
  141. static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
  142. static struct notifier_block xpc_reboot_notifier = {
  143. .notifier_call = xpc_system_reboot,
  144. };
  145. static int xpc_system_die(struct notifier_block *, unsigned long, void *);
  146. static struct notifier_block xpc_die_notifier = {
  147. .notifier_call = xpc_system_die,
  148. };
  149. enum xp_retval (*xpc_get_partition_rsvd_page_pa) (u64 buf, u64 *cookie,
  150. u64 *paddr, size_t *len);
  151. enum xp_retval (*xpc_rsvd_page_init) (struct xpc_rsvd_page *rp);
  152. void (*xpc_heartbeat_init) (void);
  153. void (*xpc_heartbeat_exit) (void);
  154. void (*xpc_increment_heartbeat) (void);
  155. void (*xpc_offline_heartbeat) (void);
  156. void (*xpc_online_heartbeat) (void);
  157. void (*xpc_check_remote_hb) (void);
  158. enum xp_retval (*xpc_make_first_contact) (struct xpc_partition *part);
  159. void (*xpc_notify_senders_of_disconnect) (struct xpc_channel *ch);
  160. u64 (*xpc_get_chctl_all_flags) (struct xpc_partition *part);
  161. enum xp_retval (*xpc_allocate_msgqueues) (struct xpc_channel *ch);
  162. void (*xpc_free_msgqueues) (struct xpc_channel *ch);
  163. void (*xpc_process_msg_chctl_flags) (struct xpc_partition *part, int ch_number);
  164. int (*xpc_n_of_deliverable_msgs) (struct xpc_channel *ch);
  165. struct xpc_msg *(*xpc_get_deliverable_msg) (struct xpc_channel *ch);
  166. void (*xpc_request_partition_activation) (struct xpc_rsvd_page *remote_rp,
  167. u64 remote_rp_pa, int nasid);
  168. void (*xpc_request_partition_reactivation) (struct xpc_partition *part);
  169. void (*xpc_request_partition_deactivation) (struct xpc_partition *part);
  170. void (*xpc_cancel_partition_deactivation_request) (struct xpc_partition *part);
  171. void (*xpc_process_activate_IRQ_rcvd) (int n_IRQs_expected);
  172. enum xp_retval (*xpc_setup_infrastructure) (struct xpc_partition *part);
  173. void (*xpc_teardown_infrastructure) (struct xpc_partition *part);
  174. void (*xpc_indicate_partition_engaged) (struct xpc_partition *part);
  175. int (*xpc_partition_engaged) (short partid);
  176. int (*xpc_any_partition_engaged) (void);
  177. void (*xpc_indicate_partition_disengaged) (struct xpc_partition *part);
  178. void (*xpc_assume_partition_disengaged) (short partid);
  179. void (*xpc_send_chctl_closerequest) (struct xpc_channel *ch,
  180. unsigned long *irq_flags);
  181. void (*xpc_send_chctl_closereply) (struct xpc_channel *ch,
  182. unsigned long *irq_flags);
  183. void (*xpc_send_chctl_openrequest) (struct xpc_channel *ch,
  184. unsigned long *irq_flags);
  185. void (*xpc_send_chctl_openreply) (struct xpc_channel *ch,
  186. unsigned long *irq_flags);
  187. enum xp_retval (*xpc_send_msg) (struct xpc_channel *ch, u32 flags,
  188. void *payload, u16 payload_size, u8 notify_type,
  189. xpc_notify_func func, void *key);
  190. void (*xpc_received_msg) (struct xpc_channel *ch, struct xpc_msg *msg);
  191. /*
  192. * Timer function to enforce the timelimit on the partition disengage.
  193. */
  194. static void
  195. xpc_timeout_partition_disengage(unsigned long data)
  196. {
  197. struct xpc_partition *part = (struct xpc_partition *)data;
  198. DBUG_ON(time_is_after_jiffies(part->disengage_timeout));
  199. (void)xpc_partition_disengaged(part);
  200. DBUG_ON(part->disengage_timeout != 0);
  201. DBUG_ON(xpc_partition_engaged(XPC_PARTID(part)));
  202. }
  203. /*
  204. * Timer to produce the heartbeat. The timer structures function is
  205. * already set when this is initially called. A tunable is used to
  206. * specify when the next timeout should occur.
  207. */
  208. static void
  209. xpc_hb_beater(unsigned long dummy)
  210. {
  211. xpc_increment_heartbeat();
  212. if (time_is_before_eq_jiffies(xpc_hb_check_timeout))
  213. wake_up_interruptible(&xpc_activate_IRQ_wq);
  214. xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
  215. add_timer(&xpc_hb_timer);
  216. }
  217. static void
  218. xpc_start_hb_beater(void)
  219. {
  220. xpc_heartbeat_init();
  221. init_timer(&xpc_hb_timer);
  222. xpc_hb_timer.function = xpc_hb_beater;
  223. xpc_hb_beater(0);
  224. }
  225. static void
  226. xpc_stop_hb_beater(void)
  227. {
  228. del_timer_sync(&xpc_hb_timer);
  229. xpc_heartbeat_exit();
  230. }
  231. /*
  232. * This thread is responsible for nearly all of the partition
  233. * activation/deactivation.
  234. */
  235. static int
  236. xpc_hb_checker(void *ignore)
  237. {
  238. int last_IRQ_count = 0;
  239. int new_IRQ_count;
  240. int force_IRQ = 0;
  241. /* this thread was marked active by xpc_hb_init() */
  242. set_cpus_allowed_ptr(current, &cpumask_of_cpu(XPC_HB_CHECK_CPU));
  243. /* set our heartbeating to other partitions into motion */
  244. xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
  245. xpc_start_hb_beater();
  246. while (!xpc_exiting) {
  247. dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
  248. "been received\n",
  249. (int)(xpc_hb_check_timeout - jiffies),
  250. atomic_read(&xpc_activate_IRQ_rcvd) - last_IRQ_count);
  251. /* checking of remote heartbeats is skewed by IRQ handling */
  252. if (time_is_before_eq_jiffies(xpc_hb_check_timeout)) {
  253. dev_dbg(xpc_part, "checking remote heartbeats\n");
  254. xpc_check_remote_hb();
  255. /*
  256. * We need to periodically recheck to ensure no
  257. * IRQ/amo pairs have been missed. That check
  258. * must always reset xpc_hb_check_timeout.
  259. */
  260. force_IRQ = 1;
  261. }
  262. /* check for outstanding IRQs */
  263. new_IRQ_count = atomic_read(&xpc_activate_IRQ_rcvd);
  264. if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
  265. force_IRQ = 0;
  266. dev_dbg(xpc_part, "found an IRQ to process; will be "
  267. "resetting xpc_hb_check_timeout\n");
  268. xpc_process_activate_IRQ_rcvd(new_IRQ_count -
  269. last_IRQ_count);
  270. last_IRQ_count = new_IRQ_count;
  271. xpc_hb_check_timeout = jiffies +
  272. (xpc_hb_check_interval * HZ);
  273. }
  274. /* wait for IRQ or timeout */
  275. (void)wait_event_interruptible(xpc_activate_IRQ_wq,
  276. (last_IRQ_count < atomic_read(
  277. &xpc_activate_IRQ_rcvd)
  278. || time_is_before_eq_jiffies(
  279. xpc_hb_check_timeout) ||
  280. xpc_exiting));
  281. }
  282. xpc_stop_hb_beater();
  283. dev_dbg(xpc_part, "heartbeat checker is exiting\n");
  284. /* mark this thread as having exited */
  285. complete(&xpc_hb_checker_exited);
  286. return 0;
  287. }
  288. /*
  289. * This thread will attempt to discover other partitions to activate
  290. * based on info provided by SAL. This new thread is short lived and
  291. * will exit once discovery is complete.
  292. */
  293. static int
  294. xpc_initiate_discovery(void *ignore)
  295. {
  296. xpc_discovery();
  297. dev_dbg(xpc_part, "discovery thread is exiting\n");
  298. /* mark this thread as having exited */
  299. complete(&xpc_discovery_exited);
  300. return 0;
  301. }
  302. /*
  303. * The first kthread assigned to a newly activated partition is the one
  304. * created by XPC HB with which it calls xpc_activating(). XPC hangs on to
  305. * that kthread until the partition is brought down, at which time that kthread
  306. * returns back to XPC HB. (The return of that kthread will signify to XPC HB
  307. * that XPC has dismantled all communication infrastructure for the associated
  308. * partition.) This kthread becomes the channel manager for that partition.
  309. *
  310. * Each active partition has a channel manager, who, besides connecting and
  311. * disconnecting channels, will ensure that each of the partition's connected
  312. * channels has the required number of assigned kthreads to get the work done.
  313. */
  314. static void
  315. xpc_channel_mgr(struct xpc_partition *part)
  316. {
  317. while (part->act_state != XPC_P_DEACTIVATING ||
  318. atomic_read(&part->nchannels_active) > 0 ||
  319. !xpc_partition_disengaged(part)) {
  320. xpc_process_sent_chctl_flags(part);
  321. /*
  322. * Wait until we've been requested to activate kthreads or
  323. * all of the channel's message queues have been torn down or
  324. * a signal is pending.
  325. *
  326. * The channel_mgr_requests is set to 1 after being awakened,
  327. * This is done to prevent the channel mgr from making one pass
  328. * through the loop for each request, since he will
  329. * be servicing all the requests in one pass. The reason it's
  330. * set to 1 instead of 0 is so that other kthreads will know
  331. * that the channel mgr is running and won't bother trying to
  332. * wake him up.
  333. */
  334. atomic_dec(&part->channel_mgr_requests);
  335. (void)wait_event_interruptible(part->channel_mgr_wq,
  336. (atomic_read(&part->channel_mgr_requests) > 0 ||
  337. part->chctl.all_flags != 0 ||
  338. (part->act_state == XPC_P_DEACTIVATING &&
  339. atomic_read(&part->nchannels_active) == 0 &&
  340. xpc_partition_disengaged(part))));
  341. atomic_set(&part->channel_mgr_requests, 1);
  342. }
  343. }
  344. /*
  345. * When XPC HB determines that a partition has come up, it will create a new
  346. * kthread and that kthread will call this function to attempt to set up the
  347. * basic infrastructure used for Cross Partition Communication with the newly
  348. * upped partition.
  349. *
  350. * The kthread that was created by XPC HB and which setup the XPC
  351. * infrastructure will remain assigned to the partition becoming the channel
  352. * manager for that partition until the partition is deactivating, at which
  353. * time the kthread will teardown the XPC infrastructure and then exit.
  354. */
  355. static int
  356. xpc_activating(void *__partid)
  357. {
  358. short partid = (u64)__partid;
  359. struct xpc_partition *part = &xpc_partitions[partid];
  360. unsigned long irq_flags;
  361. DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
  362. spin_lock_irqsave(&part->act_lock, irq_flags);
  363. if (part->act_state == XPC_P_DEACTIVATING) {
  364. part->act_state = XPC_P_INACTIVE;
  365. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  366. part->remote_rp_pa = 0;
  367. return 0;
  368. }
  369. /* indicate the thread is activating */
  370. DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
  371. part->act_state = XPC_P_ACTIVATING;
  372. XPC_SET_REASON(part, 0, 0);
  373. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  374. dev_dbg(xpc_part, "activating partition %d\n", partid);
  375. xpc_allow_hb(partid);
  376. if (xpc_setup_infrastructure(part) == xpSuccess) {
  377. (void)xpc_part_ref(part); /* this will always succeed */
  378. if (xpc_make_first_contact(part) == xpSuccess) {
  379. xpc_mark_partition_active(part);
  380. xpc_channel_mgr(part);
  381. /* won't return until partition is deactivating */
  382. }
  383. xpc_part_deref(part);
  384. xpc_teardown_infrastructure(part);
  385. }
  386. xpc_disallow_hb(partid);
  387. xpc_mark_partition_inactive(part);
  388. if (part->reason == xpReactivating) {
  389. /* interrupting ourselves results in activating partition */
  390. xpc_request_partition_reactivation(part);
  391. }
  392. return 0;
  393. }
  394. void
  395. xpc_activate_partition(struct xpc_partition *part)
  396. {
  397. short partid = XPC_PARTID(part);
  398. unsigned long irq_flags;
  399. struct task_struct *kthread;
  400. spin_lock_irqsave(&part->act_lock, irq_flags);
  401. DBUG_ON(part->act_state != XPC_P_INACTIVE);
  402. part->act_state = XPC_P_ACTIVATION_REQ;
  403. XPC_SET_REASON(part, xpCloneKThread, __LINE__);
  404. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  405. kthread = kthread_run(xpc_activating, (void *)((u64)partid), "xpc%02d",
  406. partid);
  407. if (IS_ERR(kthread)) {
  408. spin_lock_irqsave(&part->act_lock, irq_flags);
  409. part->act_state = XPC_P_INACTIVE;
  410. XPC_SET_REASON(part, xpCloneKThreadFailed, __LINE__);
  411. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  412. }
  413. }
  414. void
  415. xpc_activate_kthreads(struct xpc_channel *ch, int needed)
  416. {
  417. int idle = atomic_read(&ch->kthreads_idle);
  418. int assigned = atomic_read(&ch->kthreads_assigned);
  419. int wakeup;
  420. DBUG_ON(needed <= 0);
  421. if (idle > 0) {
  422. wakeup = (needed > idle) ? idle : needed;
  423. needed -= wakeup;
  424. dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
  425. "channel=%d\n", wakeup, ch->partid, ch->number);
  426. /* only wakeup the requested number of kthreads */
  427. wake_up_nr(&ch->idle_wq, wakeup);
  428. }
  429. if (needed <= 0)
  430. return;
  431. if (needed + assigned > ch->kthreads_assigned_limit) {
  432. needed = ch->kthreads_assigned_limit - assigned;
  433. if (needed <= 0)
  434. return;
  435. }
  436. dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
  437. needed, ch->partid, ch->number);
  438. xpc_create_kthreads(ch, needed, 0);
  439. }
  440. /*
  441. * This function is where XPC's kthreads wait for messages to deliver.
  442. */
  443. static void
  444. xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
  445. {
  446. do {
  447. /* deliver messages to their intended recipients */
  448. while (xpc_n_of_deliverable_msgs(ch) > 0 &&
  449. !(ch->flags & XPC_C_DISCONNECTING)) {
  450. xpc_deliver_msg(ch);
  451. }
  452. if (atomic_inc_return(&ch->kthreads_idle) >
  453. ch->kthreads_idle_limit) {
  454. /* too many idle kthreads on this channel */
  455. atomic_dec(&ch->kthreads_idle);
  456. break;
  457. }
  458. dev_dbg(xpc_chan, "idle kthread calling "
  459. "wait_event_interruptible_exclusive()\n");
  460. (void)wait_event_interruptible_exclusive(ch->idle_wq,
  461. (xpc_n_of_deliverable_msgs(ch) > 0 ||
  462. (ch->flags & XPC_C_DISCONNECTING)));
  463. atomic_dec(&ch->kthreads_idle);
  464. } while (!(ch->flags & XPC_C_DISCONNECTING));
  465. }
  466. static int
  467. xpc_kthread_start(void *args)
  468. {
  469. short partid = XPC_UNPACK_ARG1(args);
  470. u16 ch_number = XPC_UNPACK_ARG2(args);
  471. struct xpc_partition *part = &xpc_partitions[partid];
  472. struct xpc_channel *ch;
  473. int n_needed;
  474. unsigned long irq_flags;
  475. dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
  476. partid, ch_number);
  477. ch = &part->channels[ch_number];
  478. if (!(ch->flags & XPC_C_DISCONNECTING)) {
  479. /* let registerer know that connection has been established */
  480. spin_lock_irqsave(&ch->lock, irq_flags);
  481. if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
  482. ch->flags |= XPC_C_CONNECTEDCALLOUT;
  483. spin_unlock_irqrestore(&ch->lock, irq_flags);
  484. xpc_connected_callout(ch);
  485. spin_lock_irqsave(&ch->lock, irq_flags);
  486. ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
  487. spin_unlock_irqrestore(&ch->lock, irq_flags);
  488. /*
  489. * It is possible that while the callout was being
  490. * made that the remote partition sent some messages.
  491. * If that is the case, we may need to activate
  492. * additional kthreads to help deliver them. We only
  493. * need one less than total #of messages to deliver.
  494. */
  495. n_needed = xpc_n_of_deliverable_msgs(ch) - 1;
  496. if (n_needed > 0 && !(ch->flags & XPC_C_DISCONNECTING))
  497. xpc_activate_kthreads(ch, n_needed);
  498. } else {
  499. spin_unlock_irqrestore(&ch->lock, irq_flags);
  500. }
  501. xpc_kthread_waitmsgs(part, ch);
  502. }
  503. /* let registerer know that connection is disconnecting */
  504. spin_lock_irqsave(&ch->lock, irq_flags);
  505. if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
  506. !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
  507. ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
  508. spin_unlock_irqrestore(&ch->lock, irq_flags);
  509. xpc_disconnect_callout(ch, xpDisconnecting);
  510. spin_lock_irqsave(&ch->lock, irq_flags);
  511. ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
  512. }
  513. spin_unlock_irqrestore(&ch->lock, irq_flags);
  514. if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
  515. atomic_dec_return(&part->nchannels_engaged) == 0) {
  516. xpc_indicate_partition_disengaged(part);
  517. }
  518. xpc_msgqueue_deref(ch);
  519. dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
  520. partid, ch_number);
  521. xpc_part_deref(part);
  522. return 0;
  523. }
  524. /*
  525. * For each partition that XPC has established communications with, there is
  526. * a minimum of one kernel thread assigned to perform any operation that
  527. * may potentially sleep or block (basically the callouts to the asynchronous
  528. * functions registered via xpc_connect()).
  529. *
  530. * Additional kthreads are created and destroyed by XPC as the workload
  531. * demands.
  532. *
  533. * A kthread is assigned to one of the active channels that exists for a given
  534. * partition.
  535. */
  536. void
  537. xpc_create_kthreads(struct xpc_channel *ch, int needed,
  538. int ignore_disconnecting)
  539. {
  540. unsigned long irq_flags;
  541. u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
  542. struct xpc_partition *part = &xpc_partitions[ch->partid];
  543. struct task_struct *kthread;
  544. while (needed-- > 0) {
  545. /*
  546. * The following is done on behalf of the newly created
  547. * kthread. That kthread is responsible for doing the
  548. * counterpart to the following before it exits.
  549. */
  550. if (ignore_disconnecting) {
  551. if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
  552. /* kthreads assigned had gone to zero */
  553. BUG_ON(!(ch->flags &
  554. XPC_C_DISCONNECTINGCALLOUT_MADE));
  555. break;
  556. }
  557. } else if (ch->flags & XPC_C_DISCONNECTING) {
  558. break;
  559. } else if (atomic_inc_return(&ch->kthreads_assigned) == 1 &&
  560. atomic_inc_return(&part->nchannels_engaged) == 1) {
  561. xpc_indicate_partition_engaged(part);
  562. }
  563. (void)xpc_part_ref(part);
  564. xpc_msgqueue_ref(ch);
  565. kthread = kthread_run(xpc_kthread_start, (void *)args,
  566. "xpc%02dc%d", ch->partid, ch->number);
  567. if (IS_ERR(kthread)) {
  568. /* the fork failed */
  569. /*
  570. * NOTE: if (ignore_disconnecting &&
  571. * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
  572. * then we'll deadlock if all other kthreads assigned
  573. * to this channel are blocked in the channel's
  574. * registerer, because the only thing that will unblock
  575. * them is the xpDisconnecting callout that this
  576. * failed kthread_run() would have made.
  577. */
  578. if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
  579. atomic_dec_return(&part->nchannels_engaged) == 0) {
  580. xpc_indicate_partition_disengaged(part);
  581. }
  582. xpc_msgqueue_deref(ch);
  583. xpc_part_deref(part);
  584. if (atomic_read(&ch->kthreads_assigned) <
  585. ch->kthreads_idle_limit) {
  586. /*
  587. * Flag this as an error only if we have an
  588. * insufficient #of kthreads for the channel
  589. * to function.
  590. */
  591. spin_lock_irqsave(&ch->lock, irq_flags);
  592. XPC_DISCONNECT_CHANNEL(ch, xpLackOfResources,
  593. &irq_flags);
  594. spin_unlock_irqrestore(&ch->lock, irq_flags);
  595. }
  596. break;
  597. }
  598. }
  599. }
  600. void
  601. xpc_disconnect_wait(int ch_number)
  602. {
  603. unsigned long irq_flags;
  604. short partid;
  605. struct xpc_partition *part;
  606. struct xpc_channel *ch;
  607. int wakeup_channel_mgr;
  608. /* now wait for all callouts to the caller's function to cease */
  609. for (partid = 0; partid < xp_max_npartitions; partid++) {
  610. part = &xpc_partitions[partid];
  611. if (!xpc_part_ref(part))
  612. continue;
  613. ch = &part->channels[ch_number];
  614. if (!(ch->flags & XPC_C_WDISCONNECT)) {
  615. xpc_part_deref(part);
  616. continue;
  617. }
  618. wait_for_completion(&ch->wdisconnect_wait);
  619. spin_lock_irqsave(&ch->lock, irq_flags);
  620. DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
  621. wakeup_channel_mgr = 0;
  622. if (ch->delayed_chctl_flags) {
  623. if (part->act_state != XPC_P_DEACTIVATING) {
  624. spin_lock(&part->chctl_lock);
  625. part->chctl.flags[ch->number] |=
  626. ch->delayed_chctl_flags;
  627. spin_unlock(&part->chctl_lock);
  628. wakeup_channel_mgr = 1;
  629. }
  630. ch->delayed_chctl_flags = 0;
  631. }
  632. ch->flags &= ~XPC_C_WDISCONNECT;
  633. spin_unlock_irqrestore(&ch->lock, irq_flags);
  634. if (wakeup_channel_mgr)
  635. xpc_wakeup_channel_mgr(part);
  636. xpc_part_deref(part);
  637. }
  638. }
  639. static void
  640. xpc_do_exit(enum xp_retval reason)
  641. {
  642. short partid;
  643. int active_part_count, printed_waiting_msg = 0;
  644. struct xpc_partition *part;
  645. unsigned long printmsg_time, disengage_timeout = 0;
  646. /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
  647. DBUG_ON(xpc_exiting == 1);
  648. /*
  649. * Let the heartbeat checker thread and the discovery thread
  650. * (if one is running) know that they should exit. Also wake up
  651. * the heartbeat checker thread in case it's sleeping.
  652. */
  653. xpc_exiting = 1;
  654. wake_up_interruptible(&xpc_activate_IRQ_wq);
  655. /* wait for the discovery thread to exit */
  656. wait_for_completion(&xpc_discovery_exited);
  657. /* wait for the heartbeat checker thread to exit */
  658. wait_for_completion(&xpc_hb_checker_exited);
  659. /* sleep for a 1/3 of a second or so */
  660. (void)msleep_interruptible(300);
  661. /* wait for all partitions to become inactive */
  662. printmsg_time = jiffies + (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
  663. xpc_disengage_timedout = 0;
  664. do {
  665. active_part_count = 0;
  666. for (partid = 0; partid < xp_max_npartitions; partid++) {
  667. part = &xpc_partitions[partid];
  668. if (xpc_partition_disengaged(part) &&
  669. part->act_state == XPC_P_INACTIVE) {
  670. continue;
  671. }
  672. active_part_count++;
  673. XPC_DEACTIVATE_PARTITION(part, reason);
  674. if (part->disengage_timeout > disengage_timeout)
  675. disengage_timeout = part->disengage_timeout;
  676. }
  677. if (xpc_any_partition_engaged()) {
  678. if (time_is_before_jiffies(printmsg_time)) {
  679. dev_info(xpc_part, "waiting for remote "
  680. "partitions to deactivate, timeout in "
  681. "%ld seconds\n", (disengage_timeout -
  682. jiffies) / HZ);
  683. printmsg_time = jiffies +
  684. (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
  685. printed_waiting_msg = 1;
  686. }
  687. } else if (active_part_count > 0) {
  688. if (printed_waiting_msg) {
  689. dev_info(xpc_part, "waiting for local partition"
  690. " to deactivate\n");
  691. printed_waiting_msg = 0;
  692. }
  693. } else {
  694. if (!xpc_disengage_timedout) {
  695. dev_info(xpc_part, "all partitions have "
  696. "deactivated\n");
  697. }
  698. break;
  699. }
  700. /* sleep for a 1/3 of a second or so */
  701. (void)msleep_interruptible(300);
  702. } while (1);
  703. DBUG_ON(xpc_any_partition_engaged());
  704. DBUG_ON(xpc_any_hbs_allowed() != 0);
  705. /* a zero timestamp indicates our rsvd page is not initialized */
  706. xpc_rsvd_page->ts_jiffies = 0;
  707. if (reason == xpUnloading) {
  708. (void)unregister_die_notifier(&xpc_die_notifier);
  709. (void)unregister_reboot_notifier(&xpc_reboot_notifier);
  710. }
  711. /* clear the interface to XPC's functions */
  712. xpc_clear_interface();
  713. if (xpc_sysctl)
  714. unregister_sysctl_table(xpc_sysctl);
  715. kfree(xpc_partitions);
  716. if (is_shub())
  717. xpc_exit_sn2();
  718. else
  719. xpc_exit_uv();
  720. }
  721. /*
  722. * This function is called when the system is being rebooted.
  723. */
  724. static int
  725. xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
  726. {
  727. enum xp_retval reason;
  728. switch (event) {
  729. case SYS_RESTART:
  730. reason = xpSystemReboot;
  731. break;
  732. case SYS_HALT:
  733. reason = xpSystemHalt;
  734. break;
  735. case SYS_POWER_OFF:
  736. reason = xpSystemPoweroff;
  737. break;
  738. default:
  739. reason = xpSystemGoingDown;
  740. }
  741. xpc_do_exit(reason);
  742. return NOTIFY_DONE;
  743. }
  744. /*
  745. * Notify other partitions to deactivate from us by first disengaging from all
  746. * references to our memory.
  747. */
  748. static void
  749. xpc_die_deactivate(void)
  750. {
  751. struct xpc_partition *part;
  752. short partid;
  753. int any_engaged;
  754. long keep_waiting;
  755. long wait_to_print;
  756. /* keep xpc_hb_checker thread from doing anything (just in case) */
  757. xpc_exiting = 1;
  758. xpc_disallow_all_hbs(); /*indicate we're deactivated */
  759. for (partid = 0; partid < xp_max_npartitions; partid++) {
  760. part = &xpc_partitions[partid];
  761. if (xpc_partition_engaged(partid) ||
  762. part->act_state != XPC_P_INACTIVE) {
  763. xpc_request_partition_deactivation(part);
  764. xpc_indicate_partition_disengaged(part);
  765. }
  766. }
  767. /*
  768. * Though we requested that all other partitions deactivate from us,
  769. * we only wait until they've all disengaged or we've reached the
  770. * defined timelimit.
  771. *
  772. * Given that one iteration through the following while-loop takes
  773. * approximately 200 microseconds, calculate the #of loops to take
  774. * before bailing and the #of loops before printing a waiting message.
  775. */
  776. keep_waiting = xpc_disengage_timelimit * 1000 * 5;
  777. wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL * 1000 * 5;
  778. while (1) {
  779. any_engaged = xpc_any_partition_engaged();
  780. if (!any_engaged) {
  781. dev_info(xpc_part, "all partitions have deactivated\n");
  782. break;
  783. }
  784. if (!keep_waiting--) {
  785. for (partid = 0; partid < xp_max_npartitions;
  786. partid++) {
  787. if (xpc_partition_engaged(partid)) {
  788. dev_info(xpc_part, "deactivate from "
  789. "remote partition %d timed "
  790. "out\n", partid);
  791. }
  792. }
  793. break;
  794. }
  795. if (!wait_to_print--) {
  796. dev_info(xpc_part, "waiting for remote partitions to "
  797. "deactivate, timeout in %ld seconds\n",
  798. keep_waiting / (1000 * 5));
  799. wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL *
  800. 1000 * 5;
  801. }
  802. udelay(200);
  803. }
  804. }
  805. /*
  806. * This function is called when the system is being restarted or halted due
  807. * to some sort of system failure. If this is the case we need to notify the
  808. * other partitions to disengage from all references to our memory.
  809. * This function can also be called when our heartbeater could be offlined
  810. * for a time. In this case we need to notify other partitions to not worry
  811. * about the lack of a heartbeat.
  812. */
  813. static int
  814. xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
  815. {
  816. #ifdef CONFIG_IA64 /* !!! temporary kludge */
  817. switch (event) {
  818. case DIE_MACHINE_RESTART:
  819. case DIE_MACHINE_HALT:
  820. xpc_die_deactivate();
  821. break;
  822. case DIE_KDEBUG_ENTER:
  823. /* Should lack of heartbeat be ignored by other partitions? */
  824. if (!xpc_kdebug_ignore)
  825. break;
  826. /* fall through */
  827. case DIE_MCA_MONARCH_ENTER:
  828. case DIE_INIT_MONARCH_ENTER:
  829. xpc_offline_heartbeat();
  830. break;
  831. case DIE_KDEBUG_LEAVE:
  832. /* Is lack of heartbeat being ignored by other partitions? */
  833. if (!xpc_kdebug_ignore)
  834. break;
  835. /* fall through */
  836. case DIE_MCA_MONARCH_LEAVE:
  837. case DIE_INIT_MONARCH_LEAVE:
  838. xpc_online_heartbeat();
  839. break;
  840. }
  841. #else
  842. xpc_die_deactivate();
  843. #endif
  844. return NOTIFY_DONE;
  845. }
  846. int __init
  847. xpc_init(void)
  848. {
  849. int ret;
  850. short partid;
  851. struct xpc_partition *part;
  852. struct task_struct *kthread;
  853. snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
  854. snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
  855. if (is_shub()) {
  856. /*
  857. * The ia64-sn2 architecture supports at most 64 partitions.
  858. * And the inability to unregister remote amos restricts us
  859. * further to only support exactly 64 partitions on this
  860. * architecture, no less.
  861. */
  862. if (xp_max_npartitions != 64)
  863. return -EINVAL;
  864. ret = xpc_init_sn2();
  865. if (ret != 0)
  866. return ret;
  867. } else if (is_uv()) {
  868. xpc_init_uv();
  869. } else {
  870. return -ENODEV;
  871. }
  872. xpc_partitions = kzalloc(sizeof(struct xpc_partition) *
  873. xp_max_npartitions, GFP_KERNEL);
  874. if (xpc_partitions == NULL) {
  875. dev_err(xpc_part, "can't get memory for partition structure\n");
  876. ret = -ENOMEM;
  877. goto out_1;
  878. }
  879. /*
  880. * The first few fields of each entry of xpc_partitions[] need to
  881. * be initialized now so that calls to xpc_connect() and
  882. * xpc_disconnect() can be made prior to the activation of any remote
  883. * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
  884. * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
  885. * PARTITION HAS BEEN ACTIVATED.
  886. */
  887. for (partid = 0; partid < xp_max_npartitions; partid++) {
  888. part = &xpc_partitions[partid];
  889. DBUG_ON((u64)part != L1_CACHE_ALIGN((u64)part));
  890. part->activate_IRQ_rcvd = 0;
  891. spin_lock_init(&part->act_lock);
  892. part->act_state = XPC_P_INACTIVE;
  893. XPC_SET_REASON(part, 0, 0);
  894. init_timer(&part->disengage_timer);
  895. part->disengage_timer.function =
  896. xpc_timeout_partition_disengage;
  897. part->disengage_timer.data = (unsigned long)part;
  898. part->setup_state = XPC_P_UNSET;
  899. init_waitqueue_head(&part->teardown_wq);
  900. atomic_set(&part->references, 0);
  901. }
  902. xpc_sysctl = register_sysctl_table(xpc_sys_dir);
  903. /*
  904. * Fill the partition reserved page with the information needed by
  905. * other partitions to discover we are alive and establish initial
  906. * communications.
  907. */
  908. xpc_rsvd_page = xpc_setup_rsvd_page();
  909. if (xpc_rsvd_page == NULL) {
  910. dev_err(xpc_part, "can't setup our reserved page\n");
  911. ret = -EBUSY;
  912. goto out_2;
  913. }
  914. /* add ourselves to the reboot_notifier_list */
  915. ret = register_reboot_notifier(&xpc_reboot_notifier);
  916. if (ret != 0)
  917. dev_warn(xpc_part, "can't register reboot notifier\n");
  918. /* add ourselves to the die_notifier list */
  919. ret = register_die_notifier(&xpc_die_notifier);
  920. if (ret != 0)
  921. dev_warn(xpc_part, "can't register die notifier\n");
  922. /*
  923. * The real work-horse behind xpc. This processes incoming
  924. * interrupts and monitors remote heartbeats.
  925. */
  926. kthread = kthread_run(xpc_hb_checker, NULL, XPC_HB_CHECK_THREAD_NAME);
  927. if (IS_ERR(kthread)) {
  928. dev_err(xpc_part, "failed while forking hb check thread\n");
  929. ret = -EBUSY;
  930. goto out_3;
  931. }
  932. /*
  933. * Startup a thread that will attempt to discover other partitions to
  934. * activate based on info provided by SAL. This new thread is short
  935. * lived and will exit once discovery is complete.
  936. */
  937. kthread = kthread_run(xpc_initiate_discovery, NULL,
  938. XPC_DISCOVERY_THREAD_NAME);
  939. if (IS_ERR(kthread)) {
  940. dev_err(xpc_part, "failed while forking discovery thread\n");
  941. /* mark this new thread as a non-starter */
  942. complete(&xpc_discovery_exited);
  943. xpc_do_exit(xpUnloading);
  944. return -EBUSY;
  945. }
  946. /* set the interface to point at XPC's functions */
  947. xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
  948. xpc_initiate_send, xpc_initiate_send_notify,
  949. xpc_initiate_received, xpc_initiate_partid_to_nasids);
  950. return 0;
  951. /* initialization was not successful */
  952. out_3:
  953. /* a zero timestamp indicates our rsvd page is not initialized */
  954. xpc_rsvd_page->ts_jiffies = 0;
  955. (void)unregister_die_notifier(&xpc_die_notifier);
  956. (void)unregister_reboot_notifier(&xpc_reboot_notifier);
  957. out_2:
  958. if (xpc_sysctl)
  959. unregister_sysctl_table(xpc_sysctl);
  960. kfree(xpc_partitions);
  961. out_1:
  962. if (is_shub())
  963. xpc_exit_sn2();
  964. else
  965. xpc_exit_uv();
  966. return ret;
  967. }
  968. module_init(xpc_init);
  969. void __exit
  970. xpc_exit(void)
  971. {
  972. xpc_do_exit(xpUnloading);
  973. }
  974. module_exit(xpc_exit);
  975. MODULE_AUTHOR("Silicon Graphics, Inc.");
  976. MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
  977. MODULE_LICENSE("GPL");
  978. module_param(xpc_hb_interval, int, 0);
  979. MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
  980. "heartbeat increments.");
  981. module_param(xpc_hb_check_interval, int, 0);
  982. MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
  983. "heartbeat checks.");
  984. module_param(xpc_disengage_timelimit, int, 0);
  985. MODULE_PARM_DESC(xpc_disengage_timelimit, "Number of seconds to wait "
  986. "for disengage to complete.");
  987. module_param(xpc_kdebug_ignore, int, 0);
  988. MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
  989. "other partitions when dropping into kdebug.");