xpc_main.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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-2005 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. * . We currently have no way to determine which nasid an IPI came
  28. * from. Thus, xpc_IPI_send() does a remote AMO write followed by
  29. * an IPI. The AMO indicates where data is to be pulled from, so
  30. * after the IPI arrives, the remote partition checks the AMO word.
  31. * The IPI can actually arrive before the AMO however, so other code
  32. * must periodically check for this case. Also, remote AMO operations
  33. * do not reliably time out. Thus we do a remote PIO read solely to
  34. * know whether the remote partition is down and whether we should
  35. * stop sending IPIs to it. This remote PIO read operation is set up
  36. * in a special nofault region so SAL knows to ignore (and cleanup)
  37. * any errors due to the remote AMO write, PIO read, and/or PIO
  38. * write operations.
  39. *
  40. * If/when new hardware solves this IPI problem, we should abandon
  41. * the current approach.
  42. *
  43. */
  44. #include <linux/kernel.h>
  45. #include <linux/module.h>
  46. #include <linux/init.h>
  47. #include <linux/sched.h>
  48. #include <linux/syscalls.h>
  49. #include <linux/cache.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/slab.h>
  52. #include <asm/sn/intr.h>
  53. #include <asm/sn/sn_sal.h>
  54. #include <asm/uaccess.h>
  55. #include "xpc.h"
  56. /* define two XPC debug device structures to be used with dev_dbg() et al */
  57. struct device_driver xpc_dbg_name = {
  58. .name = "xpc"
  59. };
  60. struct device xpc_part_dbg_subname = {
  61. .bus_id = {0}, /* set to "part" at xpc_init() time */
  62. .driver = &xpc_dbg_name
  63. };
  64. struct device xpc_chan_dbg_subname = {
  65. .bus_id = {0}, /* set to "chan" at xpc_init() time */
  66. .driver = &xpc_dbg_name
  67. };
  68. struct device *xpc_part = &xpc_part_dbg_subname;
  69. struct device *xpc_chan = &xpc_chan_dbg_subname;
  70. /* systune related variables for /proc/sys directories */
  71. static int xpc_hb_min = 1;
  72. static int xpc_hb_max = 10;
  73. static int xpc_hb_check_min = 10;
  74. static int xpc_hb_check_max = 120;
  75. static ctl_table xpc_sys_xpc_hb_dir[] = {
  76. {
  77. 1,
  78. "hb_interval",
  79. &xpc_hb_interval,
  80. sizeof(int),
  81. 0644,
  82. NULL,
  83. &proc_dointvec_minmax,
  84. &sysctl_intvec,
  85. NULL,
  86. &xpc_hb_min, &xpc_hb_max
  87. },
  88. {
  89. 2,
  90. "hb_check_interval",
  91. &xpc_hb_check_interval,
  92. sizeof(int),
  93. 0644,
  94. NULL,
  95. &proc_dointvec_minmax,
  96. &sysctl_intvec,
  97. NULL,
  98. &xpc_hb_check_min, &xpc_hb_check_max
  99. },
  100. {0}
  101. };
  102. static ctl_table xpc_sys_xpc_dir[] = {
  103. {
  104. 1,
  105. "hb",
  106. NULL,
  107. 0,
  108. 0555,
  109. xpc_sys_xpc_hb_dir
  110. },
  111. {0}
  112. };
  113. static ctl_table xpc_sys_dir[] = {
  114. {
  115. 1,
  116. "xpc",
  117. NULL,
  118. 0,
  119. 0555,
  120. xpc_sys_xpc_dir
  121. },
  122. {0}
  123. };
  124. static struct ctl_table_header *xpc_sysctl;
  125. /* #of IRQs received */
  126. static atomic_t xpc_act_IRQ_rcvd;
  127. /* IRQ handler notifies this wait queue on receipt of an IRQ */
  128. static DECLARE_WAIT_QUEUE_HEAD(xpc_act_IRQ_wq);
  129. static unsigned long xpc_hb_check_timeout;
  130. /* xpc_hb_checker thread exited notification */
  131. static DECLARE_MUTEX_LOCKED(xpc_hb_checker_exited);
  132. /* xpc_discovery thread exited notification */
  133. static DECLARE_MUTEX_LOCKED(xpc_discovery_exited);
  134. static struct timer_list xpc_hb_timer;
  135. static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
  136. /*
  137. * Notify the heartbeat check thread that an IRQ has been received.
  138. */
  139. static irqreturn_t
  140. xpc_act_IRQ_handler(int irq, void *dev_id, struct pt_regs *regs)
  141. {
  142. atomic_inc(&xpc_act_IRQ_rcvd);
  143. wake_up_interruptible(&xpc_act_IRQ_wq);
  144. return IRQ_HANDLED;
  145. }
  146. /*
  147. * Timer to produce the heartbeat. The timer structures function is
  148. * already set when this is initially called. A tunable is used to
  149. * specify when the next timeout should occur.
  150. */
  151. static void
  152. xpc_hb_beater(unsigned long dummy)
  153. {
  154. xpc_vars->heartbeat++;
  155. if (jiffies >= xpc_hb_check_timeout) {
  156. wake_up_interruptible(&xpc_act_IRQ_wq);
  157. }
  158. xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
  159. add_timer(&xpc_hb_timer);
  160. }
  161. /*
  162. * This thread is responsible for nearly all of the partition
  163. * activation/deactivation.
  164. */
  165. static int
  166. xpc_hb_checker(void *ignore)
  167. {
  168. int last_IRQ_count = 0;
  169. int new_IRQ_count;
  170. int force_IRQ=0;
  171. /* this thread was marked active by xpc_hb_init() */
  172. daemonize(XPC_HB_CHECK_THREAD_NAME);
  173. set_cpus_allowed(current, cpumask_of_cpu(XPC_HB_CHECK_CPU));
  174. xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
  175. while (!(volatile int) xpc_exiting) {
  176. /* wait for IRQ or timeout */
  177. (void) wait_event_interruptible(xpc_act_IRQ_wq,
  178. (last_IRQ_count < atomic_read(&xpc_act_IRQ_rcvd) ||
  179. jiffies >= xpc_hb_check_timeout ||
  180. (volatile int) xpc_exiting));
  181. dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
  182. "been received\n",
  183. (int) (xpc_hb_check_timeout - jiffies),
  184. atomic_read(&xpc_act_IRQ_rcvd) - last_IRQ_count);
  185. /* checking of remote heartbeats is skewed by IRQ handling */
  186. if (jiffies >= xpc_hb_check_timeout) {
  187. dev_dbg(xpc_part, "checking remote heartbeats\n");
  188. xpc_check_remote_hb();
  189. /*
  190. * We need to periodically recheck to ensure no
  191. * IPI/AMO pairs have been missed. That check
  192. * must always reset xpc_hb_check_timeout.
  193. */
  194. force_IRQ = 1;
  195. }
  196. new_IRQ_count = atomic_read(&xpc_act_IRQ_rcvd);
  197. if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
  198. force_IRQ = 0;
  199. dev_dbg(xpc_part, "found an IRQ to process; will be "
  200. "resetting xpc_hb_check_timeout\n");
  201. last_IRQ_count += xpc_identify_act_IRQ_sender();
  202. if (last_IRQ_count < new_IRQ_count) {
  203. /* retry once to help avoid missing AMO */
  204. (void) xpc_identify_act_IRQ_sender();
  205. }
  206. last_IRQ_count = new_IRQ_count;
  207. xpc_hb_check_timeout = jiffies +
  208. (xpc_hb_check_interval * HZ);
  209. }
  210. }
  211. dev_dbg(xpc_part, "heartbeat checker is exiting\n");
  212. /* mark this thread as inactive */
  213. up(&xpc_hb_checker_exited);
  214. return 0;
  215. }
  216. /*
  217. * This thread will attempt to discover other partitions to activate
  218. * based on info provided by SAL. This new thread is short lived and
  219. * will exit once discovery is complete.
  220. */
  221. static int
  222. xpc_initiate_discovery(void *ignore)
  223. {
  224. daemonize(XPC_DISCOVERY_THREAD_NAME);
  225. xpc_discovery();
  226. dev_dbg(xpc_part, "discovery thread is exiting\n");
  227. /* mark this thread as inactive */
  228. up(&xpc_discovery_exited);
  229. return 0;
  230. }
  231. /*
  232. * Establish first contact with the remote partititon. This involves pulling
  233. * the XPC per partition variables from the remote partition and waiting for
  234. * the remote partition to pull ours.
  235. */
  236. static enum xpc_retval
  237. xpc_make_first_contact(struct xpc_partition *part)
  238. {
  239. enum xpc_retval ret;
  240. while ((ret = xpc_pull_remote_vars_part(part)) != xpcSuccess) {
  241. if (ret != xpcRetry) {
  242. XPC_DEACTIVATE_PARTITION(part, ret);
  243. return ret;
  244. }
  245. dev_dbg(xpc_chan, "waiting to make first contact with "
  246. "partition %d\n", XPC_PARTID(part));
  247. /* wait a 1/4 of a second or so */
  248. set_current_state(TASK_INTERRUPTIBLE);
  249. (void) schedule_timeout(0.25 * HZ);
  250. if (part->act_state == XPC_P_DEACTIVATING) {
  251. return part->reason;
  252. }
  253. }
  254. return xpc_mark_partition_active(part);
  255. }
  256. /*
  257. * The first kthread assigned to a newly activated partition is the one
  258. * created by XPC HB with which it calls xpc_partition_up(). XPC hangs on to
  259. * that kthread until the partition is brought down, at which time that kthread
  260. * returns back to XPC HB. (The return of that kthread will signify to XPC HB
  261. * that XPC has dismantled all communication infrastructure for the associated
  262. * partition.) This kthread becomes the channel manager for that partition.
  263. *
  264. * Each active partition has a channel manager, who, besides connecting and
  265. * disconnecting channels, will ensure that each of the partition's connected
  266. * channels has the required number of assigned kthreads to get the work done.
  267. */
  268. static void
  269. xpc_channel_mgr(struct xpc_partition *part)
  270. {
  271. while (part->act_state != XPC_P_DEACTIVATING ||
  272. atomic_read(&part->nchannels_active) > 0) {
  273. xpc_process_channel_activity(part);
  274. /*
  275. * Wait until we've been requested to activate kthreads or
  276. * all of the channel's message queues have been torn down or
  277. * a signal is pending.
  278. *
  279. * The channel_mgr_requests is set to 1 after being awakened,
  280. * This is done to prevent the channel mgr from making one pass
  281. * through the loop for each request, since he will
  282. * be servicing all the requests in one pass. The reason it's
  283. * set to 1 instead of 0 is so that other kthreads will know
  284. * that the channel mgr is running and won't bother trying to
  285. * wake him up.
  286. */
  287. atomic_dec(&part->channel_mgr_requests);
  288. (void) wait_event_interruptible(part->channel_mgr_wq,
  289. (atomic_read(&part->channel_mgr_requests) > 0 ||
  290. (volatile u64) part->local_IPI_amo != 0 ||
  291. ((volatile u8) part->act_state ==
  292. XPC_P_DEACTIVATING &&
  293. atomic_read(&part->nchannels_active) == 0)));
  294. atomic_set(&part->channel_mgr_requests, 1);
  295. // >>> Does it need to wakeup periodically as well? In case we
  296. // >>> miscalculated the #of kthreads to wakeup or create?
  297. }
  298. }
  299. /*
  300. * When XPC HB determines that a partition has come up, it will create a new
  301. * kthread and that kthread will call this function to attempt to set up the
  302. * basic infrastructure used for Cross Partition Communication with the newly
  303. * upped partition.
  304. *
  305. * The kthread that was created by XPC HB and which setup the XPC
  306. * infrastructure will remain assigned to the partition until the partition
  307. * goes down. At which time the kthread will teardown the XPC infrastructure
  308. * and then exit.
  309. *
  310. * XPC HB will put the remote partition's XPC per partition specific variables
  311. * physical address into xpc_partitions[partid].remote_vars_part_pa prior to
  312. * calling xpc_partition_up().
  313. */
  314. static void
  315. xpc_partition_up(struct xpc_partition *part)
  316. {
  317. DBUG_ON(part->channels != NULL);
  318. dev_dbg(xpc_chan, "activating partition %d\n", XPC_PARTID(part));
  319. if (xpc_setup_infrastructure(part) != xpcSuccess) {
  320. return;
  321. }
  322. /*
  323. * The kthread that XPC HB called us with will become the
  324. * channel manager for this partition. It will not return
  325. * back to XPC HB until the partition's XPC infrastructure
  326. * has been dismantled.
  327. */
  328. (void) xpc_part_ref(part); /* this will always succeed */
  329. if (xpc_make_first_contact(part) == xpcSuccess) {
  330. xpc_channel_mgr(part);
  331. }
  332. xpc_part_deref(part);
  333. xpc_teardown_infrastructure(part);
  334. }
  335. static int
  336. xpc_activating(void *__partid)
  337. {
  338. partid_t partid = (u64) __partid;
  339. struct xpc_partition *part = &xpc_partitions[partid];
  340. unsigned long irq_flags;
  341. struct sched_param param = { sched_priority: MAX_USER_RT_PRIO - 1 };
  342. int ret;
  343. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  344. spin_lock_irqsave(&part->act_lock, irq_flags);
  345. if (part->act_state == XPC_P_DEACTIVATING) {
  346. part->act_state = XPC_P_INACTIVE;
  347. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  348. part->remote_rp_pa = 0;
  349. return 0;
  350. }
  351. /* indicate the thread is activating */
  352. DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
  353. part->act_state = XPC_P_ACTIVATING;
  354. XPC_SET_REASON(part, 0, 0);
  355. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  356. dev_dbg(xpc_part, "bringing partition %d up\n", partid);
  357. daemonize("xpc%02d", partid);
  358. /*
  359. * This thread needs to run at a realtime priority to prevent a
  360. * significant performance degradation.
  361. */
  362. ret = sched_setscheduler(current, SCHED_FIFO, &param);
  363. if (ret != 0) {
  364. dev_warn(xpc_part, "unable to set pid %d to a realtime "
  365. "priority, ret=%d\n", current->pid, ret);
  366. }
  367. /* allow this thread and its children to run on any CPU */
  368. set_cpus_allowed(current, CPU_MASK_ALL);
  369. /*
  370. * Register the remote partition's AMOs with SAL so it can handle
  371. * and cleanup errors within that address range should the remote
  372. * partition go down. We don't unregister this range because it is
  373. * difficult to tell when outstanding writes to the remote partition
  374. * are finished and thus when it is safe to unregister. This should
  375. * not result in wasted space in the SAL xp_addr_region table because
  376. * we should get the same page for remote_amos_page_pa after module
  377. * reloads and system reboots.
  378. */
  379. if (sn_register_xp_addr_region(part->remote_amos_page_pa,
  380. PAGE_SIZE, 1) < 0) {
  381. dev_warn(xpc_part, "xpc_partition_up(%d) failed to register "
  382. "xp_addr region\n", partid);
  383. spin_lock_irqsave(&part->act_lock, irq_flags);
  384. part->act_state = XPC_P_INACTIVE;
  385. XPC_SET_REASON(part, xpcPhysAddrRegFailed, __LINE__);
  386. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  387. part->remote_rp_pa = 0;
  388. return 0;
  389. }
  390. XPC_ALLOW_HB(partid, xpc_vars);
  391. xpc_IPI_send_activated(part);
  392. /*
  393. * xpc_partition_up() holds this thread and marks this partition as
  394. * XPC_P_ACTIVE by calling xpc_hb_mark_active().
  395. */
  396. (void) xpc_partition_up(part);
  397. xpc_mark_partition_inactive(part);
  398. if (part->reason == xpcReactivating) {
  399. /* interrupting ourselves results in activating partition */
  400. xpc_IPI_send_reactivate(part);
  401. }
  402. return 0;
  403. }
  404. void
  405. xpc_activate_partition(struct xpc_partition *part)
  406. {
  407. partid_t partid = XPC_PARTID(part);
  408. unsigned long irq_flags;
  409. pid_t pid;
  410. spin_lock_irqsave(&part->act_lock, irq_flags);
  411. pid = kernel_thread(xpc_activating, (void *) ((u64) partid), 0);
  412. DBUG_ON(part->act_state != XPC_P_INACTIVE);
  413. if (pid > 0) {
  414. part->act_state = XPC_P_ACTIVATION_REQ;
  415. XPC_SET_REASON(part, xpcCloneKThread, __LINE__);
  416. } else {
  417. XPC_SET_REASON(part, xpcCloneKThreadFailed, __LINE__);
  418. }
  419. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  420. }
  421. /*
  422. * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
  423. * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
  424. * than one partition, we use an AMO_t structure per partition to indicate
  425. * whether a partition has sent an IPI or not. >>> If it has, then wake up the
  426. * associated kthread to handle it.
  427. *
  428. * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IPIs sent by XPC
  429. * running on other partitions.
  430. *
  431. * Noteworthy Arguments:
  432. *
  433. * irq - Interrupt ReQuest number. NOT USED.
  434. *
  435. * dev_id - partid of IPI's potential sender.
  436. *
  437. * regs - processor's context before the processor entered
  438. * interrupt code. NOT USED.
  439. */
  440. irqreturn_t
  441. xpc_notify_IRQ_handler(int irq, void *dev_id, struct pt_regs *regs)
  442. {
  443. partid_t partid = (partid_t) (u64) dev_id;
  444. struct xpc_partition *part = &xpc_partitions[partid];
  445. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  446. if (xpc_part_ref(part)) {
  447. xpc_check_for_channel_activity(part);
  448. xpc_part_deref(part);
  449. }
  450. return IRQ_HANDLED;
  451. }
  452. /*
  453. * Check to see if xpc_notify_IRQ_handler() dropped any IPIs on the floor
  454. * because the write to their associated IPI amo completed after the IRQ/IPI
  455. * was received.
  456. */
  457. void
  458. xpc_dropped_IPI_check(struct xpc_partition *part)
  459. {
  460. if (xpc_part_ref(part)) {
  461. xpc_check_for_channel_activity(part);
  462. part->dropped_IPI_timer.expires = jiffies +
  463. XPC_P_DROPPED_IPI_WAIT;
  464. add_timer(&part->dropped_IPI_timer);
  465. xpc_part_deref(part);
  466. }
  467. }
  468. void
  469. xpc_activate_kthreads(struct xpc_channel *ch, int needed)
  470. {
  471. int idle = atomic_read(&ch->kthreads_idle);
  472. int assigned = atomic_read(&ch->kthreads_assigned);
  473. int wakeup;
  474. DBUG_ON(needed <= 0);
  475. if (idle > 0) {
  476. wakeup = (needed > idle) ? idle : needed;
  477. needed -= wakeup;
  478. dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
  479. "channel=%d\n", wakeup, ch->partid, ch->number);
  480. /* only wakeup the requested number of kthreads */
  481. wake_up_nr(&ch->idle_wq, wakeup);
  482. }
  483. if (needed <= 0) {
  484. return;
  485. }
  486. if (needed + assigned > ch->kthreads_assigned_limit) {
  487. needed = ch->kthreads_assigned_limit - assigned;
  488. // >>>should never be less than 0
  489. if (needed <= 0) {
  490. return;
  491. }
  492. }
  493. dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
  494. needed, ch->partid, ch->number);
  495. xpc_create_kthreads(ch, needed);
  496. }
  497. /*
  498. * This function is where XPC's kthreads wait for messages to deliver.
  499. */
  500. static void
  501. xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
  502. {
  503. do {
  504. /* deliver messages to their intended recipients */
  505. while ((volatile s64) ch->w_local_GP.get <
  506. (volatile s64) ch->w_remote_GP.put &&
  507. !((volatile u32) ch->flags &
  508. XPC_C_DISCONNECTING)) {
  509. xpc_deliver_msg(ch);
  510. }
  511. if (atomic_inc_return(&ch->kthreads_idle) >
  512. ch->kthreads_idle_limit) {
  513. /* too many idle kthreads on this channel */
  514. atomic_dec(&ch->kthreads_idle);
  515. break;
  516. }
  517. dev_dbg(xpc_chan, "idle kthread calling "
  518. "wait_event_interruptible_exclusive()\n");
  519. (void) wait_event_interruptible_exclusive(ch->idle_wq,
  520. ((volatile s64) ch->w_local_GP.get <
  521. (volatile s64) ch->w_remote_GP.put ||
  522. ((volatile u32) ch->flags &
  523. XPC_C_DISCONNECTING)));
  524. atomic_dec(&ch->kthreads_idle);
  525. } while (!((volatile u32) ch->flags & XPC_C_DISCONNECTING));
  526. }
  527. static int
  528. xpc_daemonize_kthread(void *args)
  529. {
  530. partid_t partid = XPC_UNPACK_ARG1(args);
  531. u16 ch_number = XPC_UNPACK_ARG2(args);
  532. struct xpc_partition *part = &xpc_partitions[partid];
  533. struct xpc_channel *ch;
  534. int n_needed;
  535. daemonize("xpc%02dc%d", partid, ch_number);
  536. dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
  537. partid, ch_number);
  538. ch = &part->channels[ch_number];
  539. if (!(ch->flags & XPC_C_DISCONNECTING)) {
  540. DBUG_ON(!(ch->flags & XPC_C_CONNECTED));
  541. /* let registerer know that connection has been established */
  542. if (atomic_read(&ch->kthreads_assigned) == 1) {
  543. xpc_connected_callout(ch);
  544. /*
  545. * It is possible that while the callout was being
  546. * made that the remote partition sent some messages.
  547. * If that is the case, we may need to activate
  548. * additional kthreads to help deliver them. We only
  549. * need one less than total #of messages to deliver.
  550. */
  551. n_needed = ch->w_remote_GP.put - ch->w_local_GP.get - 1;
  552. if (n_needed > 0 &&
  553. !(ch->flags & XPC_C_DISCONNECTING)) {
  554. xpc_activate_kthreads(ch, n_needed);
  555. }
  556. }
  557. xpc_kthread_waitmsgs(part, ch);
  558. }
  559. if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
  560. ((ch->flags & XPC_C_CONNECTCALLOUT) ||
  561. (ch->reason != xpcUnregistering &&
  562. ch->reason != xpcOtherUnregistering))) {
  563. xpc_disconnected_callout(ch);
  564. }
  565. xpc_msgqueue_deref(ch);
  566. dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
  567. partid, ch_number);
  568. xpc_part_deref(part);
  569. return 0;
  570. }
  571. /*
  572. * For each partition that XPC has established communications with, there is
  573. * a minimum of one kernel thread assigned to perform any operation that
  574. * may potentially sleep or block (basically the callouts to the asynchronous
  575. * functions registered via xpc_connect()).
  576. *
  577. * Additional kthreads are created and destroyed by XPC as the workload
  578. * demands.
  579. *
  580. * A kthread is assigned to one of the active channels that exists for a given
  581. * partition.
  582. */
  583. void
  584. xpc_create_kthreads(struct xpc_channel *ch, int needed)
  585. {
  586. unsigned long irq_flags;
  587. pid_t pid;
  588. u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
  589. while (needed-- > 0) {
  590. pid = kernel_thread(xpc_daemonize_kthread, (void *) args, 0);
  591. if (pid < 0) {
  592. /* the fork failed */
  593. if (atomic_read(&ch->kthreads_assigned) <
  594. ch->kthreads_idle_limit) {
  595. /*
  596. * Flag this as an error only if we have an
  597. * insufficient #of kthreads for the channel
  598. * to function.
  599. *
  600. * No xpc_msgqueue_ref() is needed here since
  601. * the channel mgr is doing this.
  602. */
  603. spin_lock_irqsave(&ch->lock, irq_flags);
  604. XPC_DISCONNECT_CHANNEL(ch, xpcLackOfResources,
  605. &irq_flags);
  606. spin_unlock_irqrestore(&ch->lock, irq_flags);
  607. }
  608. break;
  609. }
  610. /*
  611. * The following is done on behalf of the newly created
  612. * kthread. That kthread is responsible for doing the
  613. * counterpart to the following before it exits.
  614. */
  615. (void) xpc_part_ref(&xpc_partitions[ch->partid]);
  616. xpc_msgqueue_ref(ch);
  617. atomic_inc(&ch->kthreads_assigned);
  618. ch->kthreads_created++; // >>> temporary debug only!!!
  619. }
  620. }
  621. void
  622. xpc_disconnect_wait(int ch_number)
  623. {
  624. partid_t partid;
  625. struct xpc_partition *part;
  626. struct xpc_channel *ch;
  627. /* now wait for all callouts to the caller's function to cease */
  628. for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
  629. part = &xpc_partitions[partid];
  630. if (xpc_part_ref(part)) {
  631. ch = &part->channels[ch_number];
  632. // >>> how do we keep from falling into the window between our check and going
  633. // >>> down and coming back up where sema is re-inited?
  634. if (ch->flags & XPC_C_SETUP) {
  635. (void) down(&ch->teardown_sema);
  636. }
  637. xpc_part_deref(part);
  638. }
  639. }
  640. }
  641. static void
  642. xpc_do_exit(void)
  643. {
  644. partid_t partid;
  645. int active_part_count;
  646. struct xpc_partition *part;
  647. /* now it's time to eliminate our heartbeat */
  648. del_timer_sync(&xpc_hb_timer);
  649. xpc_vars->heartbeating_to_mask = 0;
  650. /* indicate to others that our reserved page is uninitialized */
  651. xpc_rsvd_page->vars_pa = 0;
  652. /*
  653. * Ignore all incoming interrupts. Without interupts the heartbeat
  654. * checker won't activate any new partitions that may come up.
  655. */
  656. free_irq(SGI_XPC_ACTIVATE, NULL);
  657. /*
  658. * Cause the heartbeat checker and the discovery threads to exit.
  659. * We don't want them attempting to activate new partitions as we
  660. * try to deactivate the existing ones.
  661. */
  662. xpc_exiting = 1;
  663. wake_up_interruptible(&xpc_act_IRQ_wq);
  664. /* wait for the heartbeat checker thread to mark itself inactive */
  665. down(&xpc_hb_checker_exited);
  666. /* wait for the discovery thread to mark itself inactive */
  667. down(&xpc_discovery_exited);
  668. set_current_state(TASK_INTERRUPTIBLE);
  669. schedule_timeout(0.3 * HZ);
  670. set_current_state(TASK_RUNNING);
  671. /* wait for all partitions to become inactive */
  672. do {
  673. active_part_count = 0;
  674. for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
  675. part = &xpc_partitions[partid];
  676. if (part->act_state != XPC_P_INACTIVE) {
  677. active_part_count++;
  678. XPC_DEACTIVATE_PARTITION(part, xpcUnloading);
  679. }
  680. }
  681. if (active_part_count) {
  682. set_current_state(TASK_INTERRUPTIBLE);
  683. schedule_timeout(0.3 * HZ);
  684. set_current_state(TASK_RUNNING);
  685. }
  686. } while (active_part_count > 0);
  687. /* close down protections for IPI operations */
  688. xpc_restrict_IPI_ops();
  689. /* clear the interface to XPC's functions */
  690. xpc_clear_interface();
  691. if (xpc_sysctl) {
  692. unregister_sysctl_table(xpc_sysctl);
  693. }
  694. }
  695. int __init
  696. xpc_init(void)
  697. {
  698. int ret;
  699. partid_t partid;
  700. struct xpc_partition *part;
  701. pid_t pid;
  702. /*
  703. * xpc_remote_copy_buffer is used as a temporary buffer for bte_copy'ng
  704. * both a partition's reserved page and its XPC variables. Its size was
  705. * based on the size of a reserved page. So we need to ensure that the
  706. * XPC variables will fit as well.
  707. */
  708. if (XPC_VARS_ALIGNED_SIZE > XPC_RSVD_PAGE_ALIGNED_SIZE) {
  709. dev_err(xpc_part, "xpc_remote_copy_buffer is not big enough\n");
  710. return -EPERM;
  711. }
  712. DBUG_ON((u64) xpc_remote_copy_buffer !=
  713. L1_CACHE_ALIGN((u64) xpc_remote_copy_buffer));
  714. snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
  715. snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
  716. xpc_sysctl = register_sysctl_table(xpc_sys_dir, 1);
  717. /*
  718. * The first few fields of each entry of xpc_partitions[] need to
  719. * be initialized now so that calls to xpc_connect() and
  720. * xpc_disconnect() can be made prior to the activation of any remote
  721. * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
  722. * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
  723. * PARTITION HAS BEEN ACTIVATED.
  724. */
  725. for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
  726. part = &xpc_partitions[partid];
  727. DBUG_ON((u64) part != L1_CACHE_ALIGN((u64) part));
  728. part->act_IRQ_rcvd = 0;
  729. spin_lock_init(&part->act_lock);
  730. part->act_state = XPC_P_INACTIVE;
  731. XPC_SET_REASON(part, 0, 0);
  732. part->setup_state = XPC_P_UNSET;
  733. init_waitqueue_head(&part->teardown_wq);
  734. atomic_set(&part->references, 0);
  735. }
  736. /*
  737. * Open up protections for IPI operations (and AMO operations on
  738. * Shub 1.1 systems).
  739. */
  740. xpc_allow_IPI_ops();
  741. /*
  742. * Interrupts being processed will increment this atomic variable and
  743. * awaken the heartbeat thread which will process the interrupts.
  744. */
  745. atomic_set(&xpc_act_IRQ_rcvd, 0);
  746. /*
  747. * This is safe to do before the xpc_hb_checker thread has started
  748. * because the handler releases a wait queue. If an interrupt is
  749. * received before the thread is waiting, it will not go to sleep,
  750. * but rather immediately process the interrupt.
  751. */
  752. ret = request_irq(SGI_XPC_ACTIVATE, xpc_act_IRQ_handler, 0,
  753. "xpc hb", NULL);
  754. if (ret != 0) {
  755. dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
  756. "errno=%d\n", -ret);
  757. xpc_restrict_IPI_ops();
  758. if (xpc_sysctl) {
  759. unregister_sysctl_table(xpc_sysctl);
  760. }
  761. return -EBUSY;
  762. }
  763. /*
  764. * Fill the partition reserved page with the information needed by
  765. * other partitions to discover we are alive and establish initial
  766. * communications.
  767. */
  768. xpc_rsvd_page = xpc_rsvd_page_init();
  769. if (xpc_rsvd_page == NULL) {
  770. dev_err(xpc_part, "could not setup our reserved page\n");
  771. free_irq(SGI_XPC_ACTIVATE, NULL);
  772. xpc_restrict_IPI_ops();
  773. if (xpc_sysctl) {
  774. unregister_sysctl_table(xpc_sysctl);
  775. }
  776. return -EBUSY;
  777. }
  778. /*
  779. * Set the beating to other partitions into motion. This is
  780. * the last requirement for other partitions' discovery to
  781. * initiate communications with us.
  782. */
  783. init_timer(&xpc_hb_timer);
  784. xpc_hb_timer.function = xpc_hb_beater;
  785. xpc_hb_beater(0);
  786. /*
  787. * The real work-horse behind xpc. This processes incoming
  788. * interrupts and monitors remote heartbeats.
  789. */
  790. pid = kernel_thread(xpc_hb_checker, NULL, 0);
  791. if (pid < 0) {
  792. dev_err(xpc_part, "failed while forking hb check thread\n");
  793. /* indicate to others that our reserved page is uninitialized */
  794. xpc_rsvd_page->vars_pa = 0;
  795. del_timer_sync(&xpc_hb_timer);
  796. free_irq(SGI_XPC_ACTIVATE, NULL);
  797. xpc_restrict_IPI_ops();
  798. if (xpc_sysctl) {
  799. unregister_sysctl_table(xpc_sysctl);
  800. }
  801. return -EBUSY;
  802. }
  803. /*
  804. * Startup a thread that will attempt to discover other partitions to
  805. * activate based on info provided by SAL. This new thread is short
  806. * lived and will exit once discovery is complete.
  807. */
  808. pid = kernel_thread(xpc_initiate_discovery, NULL, 0);
  809. if (pid < 0) {
  810. dev_err(xpc_part, "failed while forking discovery thread\n");
  811. /* mark this new thread as a non-starter */
  812. up(&xpc_discovery_exited);
  813. xpc_do_exit();
  814. return -EBUSY;
  815. }
  816. /* set the interface to point at XPC's functions */
  817. xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
  818. xpc_initiate_allocate, xpc_initiate_send,
  819. xpc_initiate_send_notify, xpc_initiate_received,
  820. xpc_initiate_partid_to_nasids);
  821. return 0;
  822. }
  823. module_init(xpc_init);
  824. void __exit
  825. xpc_exit(void)
  826. {
  827. xpc_do_exit();
  828. }
  829. module_exit(xpc_exit);
  830. MODULE_AUTHOR("Silicon Graphics, Inc.");
  831. MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
  832. MODULE_LICENSE("GPL");
  833. module_param(xpc_hb_interval, int, 0);
  834. MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
  835. "heartbeat increments.");
  836. module_param(xpc_hb_check_interval, int, 0);
  837. MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
  838. "heartbeat checks.");