xpc_main.c 34 KB

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