svc.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. /*
  2. * linux/net/sunrpc/svc.c
  3. *
  4. * High-level RPC service routines
  5. *
  6. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7. *
  8. * Multiple threads pools and NUMAisation
  9. * Copyright (c) 2006 Silicon Graphics, Inc.
  10. * by Greg Banks <gnb@melbourne.sgi.com>
  11. */
  12. #include <linux/linkage.h>
  13. #include <linux/sched.h>
  14. #include <linux/errno.h>
  15. #include <linux/net.h>
  16. #include <linux/in.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/sched.h>
  21. #include <linux/sunrpc/types.h>
  22. #include <linux/sunrpc/xdr.h>
  23. #include <linux/sunrpc/stats.h>
  24. #include <linux/sunrpc/svcsock.h>
  25. #include <linux/sunrpc/clnt.h>
  26. #define RPCDBG_FACILITY RPCDBG_SVCDSP
  27. #define svc_serv_is_pooled(serv) ((serv)->sv_function)
  28. /*
  29. * Mode for mapping cpus to pools.
  30. */
  31. enum {
  32. SVC_POOL_AUTO = -1, /* choose one of the others */
  33. SVC_POOL_GLOBAL, /* no mapping, just a single global pool
  34. * (legacy & UP mode) */
  35. SVC_POOL_PERCPU, /* one pool per cpu */
  36. SVC_POOL_PERNODE /* one pool per numa node */
  37. };
  38. #define SVC_POOL_DEFAULT SVC_POOL_GLOBAL
  39. /*
  40. * Structure for mapping cpus to pools and vice versa.
  41. * Setup once during sunrpc initialisation.
  42. */
  43. static struct svc_pool_map {
  44. int count; /* How many svc_servs use us */
  45. int mode; /* Note: int not enum to avoid
  46. * warnings about "enumeration value
  47. * not handled in switch" */
  48. unsigned int npools;
  49. unsigned int *pool_to; /* maps pool id to cpu or node */
  50. unsigned int *to_pool; /* maps cpu or node to pool id */
  51. } svc_pool_map = {
  52. .count = 0,
  53. .mode = SVC_POOL_DEFAULT
  54. };
  55. static DEFINE_MUTEX(svc_pool_map_mutex);/* protects svc_pool_map.count only */
  56. static int
  57. param_set_pool_mode(const char *val, struct kernel_param *kp)
  58. {
  59. int *ip = (int *)kp->arg;
  60. struct svc_pool_map *m = &svc_pool_map;
  61. int err;
  62. mutex_lock(&svc_pool_map_mutex);
  63. err = -EBUSY;
  64. if (m->count)
  65. goto out;
  66. err = 0;
  67. if (!strncmp(val, "auto", 4))
  68. *ip = SVC_POOL_AUTO;
  69. else if (!strncmp(val, "global", 6))
  70. *ip = SVC_POOL_GLOBAL;
  71. else if (!strncmp(val, "percpu", 6))
  72. *ip = SVC_POOL_PERCPU;
  73. else if (!strncmp(val, "pernode", 7))
  74. *ip = SVC_POOL_PERNODE;
  75. else
  76. err = -EINVAL;
  77. out:
  78. mutex_unlock(&svc_pool_map_mutex);
  79. return err;
  80. }
  81. static int
  82. param_get_pool_mode(char *buf, struct kernel_param *kp)
  83. {
  84. int *ip = (int *)kp->arg;
  85. switch (*ip)
  86. {
  87. case SVC_POOL_AUTO:
  88. return strlcpy(buf, "auto", 20);
  89. case SVC_POOL_GLOBAL:
  90. return strlcpy(buf, "global", 20);
  91. case SVC_POOL_PERCPU:
  92. return strlcpy(buf, "percpu", 20);
  93. case SVC_POOL_PERNODE:
  94. return strlcpy(buf, "pernode", 20);
  95. default:
  96. return sprintf(buf, "%d", *ip);
  97. }
  98. }
  99. module_param_call(pool_mode, param_set_pool_mode, param_get_pool_mode,
  100. &svc_pool_map.mode, 0644);
  101. /*
  102. * Detect best pool mapping mode heuristically,
  103. * according to the machine's topology.
  104. */
  105. static int
  106. svc_pool_map_choose_mode(void)
  107. {
  108. unsigned int node;
  109. if (num_online_nodes() > 1) {
  110. /*
  111. * Actually have multiple NUMA nodes,
  112. * so split pools on NUMA node boundaries
  113. */
  114. return SVC_POOL_PERNODE;
  115. }
  116. node = any_online_node(node_online_map);
  117. if (nr_cpus_node(node) > 2) {
  118. /*
  119. * Non-trivial SMP, or CONFIG_NUMA on
  120. * non-NUMA hardware, e.g. with a generic
  121. * x86_64 kernel on Xeons. In this case we
  122. * want to divide the pools on cpu boundaries.
  123. */
  124. return SVC_POOL_PERCPU;
  125. }
  126. /* default: one global pool */
  127. return SVC_POOL_GLOBAL;
  128. }
  129. /*
  130. * Allocate the to_pool[] and pool_to[] arrays.
  131. * Returns 0 on success or an errno.
  132. */
  133. static int
  134. svc_pool_map_alloc_arrays(struct svc_pool_map *m, unsigned int maxpools)
  135. {
  136. m->to_pool = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL);
  137. if (!m->to_pool)
  138. goto fail;
  139. m->pool_to = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL);
  140. if (!m->pool_to)
  141. goto fail_free;
  142. return 0;
  143. fail_free:
  144. kfree(m->to_pool);
  145. fail:
  146. return -ENOMEM;
  147. }
  148. /*
  149. * Initialise the pool map for SVC_POOL_PERCPU mode.
  150. * Returns number of pools or <0 on error.
  151. */
  152. static int
  153. svc_pool_map_init_percpu(struct svc_pool_map *m)
  154. {
  155. unsigned int maxpools = nr_cpu_ids;
  156. unsigned int pidx = 0;
  157. unsigned int cpu;
  158. int err;
  159. err = svc_pool_map_alloc_arrays(m, maxpools);
  160. if (err)
  161. return err;
  162. for_each_online_cpu(cpu) {
  163. BUG_ON(pidx > maxpools);
  164. m->to_pool[cpu] = pidx;
  165. m->pool_to[pidx] = cpu;
  166. pidx++;
  167. }
  168. /* cpus brought online later all get mapped to pool0, sorry */
  169. return pidx;
  170. };
  171. /*
  172. * Initialise the pool map for SVC_POOL_PERNODE mode.
  173. * Returns number of pools or <0 on error.
  174. */
  175. static int
  176. svc_pool_map_init_pernode(struct svc_pool_map *m)
  177. {
  178. unsigned int maxpools = nr_node_ids;
  179. unsigned int pidx = 0;
  180. unsigned int node;
  181. int err;
  182. err = svc_pool_map_alloc_arrays(m, maxpools);
  183. if (err)
  184. return err;
  185. for_each_node_with_cpus(node) {
  186. /* some architectures (e.g. SN2) have cpuless nodes */
  187. BUG_ON(pidx > maxpools);
  188. m->to_pool[node] = pidx;
  189. m->pool_to[pidx] = node;
  190. pidx++;
  191. }
  192. /* nodes brought online later all get mapped to pool0, sorry */
  193. return pidx;
  194. }
  195. /*
  196. * Add a reference to the global map of cpus to pools (and
  197. * vice versa). Initialise the map if we're the first user.
  198. * Returns the number of pools.
  199. */
  200. static unsigned int
  201. svc_pool_map_get(void)
  202. {
  203. struct svc_pool_map *m = &svc_pool_map;
  204. int npools = -1;
  205. mutex_lock(&svc_pool_map_mutex);
  206. if (m->count++) {
  207. mutex_unlock(&svc_pool_map_mutex);
  208. return m->npools;
  209. }
  210. if (m->mode == SVC_POOL_AUTO)
  211. m->mode = svc_pool_map_choose_mode();
  212. switch (m->mode) {
  213. case SVC_POOL_PERCPU:
  214. npools = svc_pool_map_init_percpu(m);
  215. break;
  216. case SVC_POOL_PERNODE:
  217. npools = svc_pool_map_init_pernode(m);
  218. break;
  219. }
  220. if (npools < 0) {
  221. /* default, or memory allocation failure */
  222. npools = 1;
  223. m->mode = SVC_POOL_GLOBAL;
  224. }
  225. m->npools = npools;
  226. mutex_unlock(&svc_pool_map_mutex);
  227. return m->npools;
  228. }
  229. /*
  230. * Drop a reference to the global map of cpus to pools.
  231. * When the last reference is dropped, the map data is
  232. * freed; this allows the sysadmin to change the pool
  233. * mode using the pool_mode module option without
  234. * rebooting or re-loading sunrpc.ko.
  235. */
  236. static void
  237. svc_pool_map_put(void)
  238. {
  239. struct svc_pool_map *m = &svc_pool_map;
  240. mutex_lock(&svc_pool_map_mutex);
  241. if (!--m->count) {
  242. m->mode = SVC_POOL_DEFAULT;
  243. kfree(m->to_pool);
  244. kfree(m->pool_to);
  245. m->npools = 0;
  246. }
  247. mutex_unlock(&svc_pool_map_mutex);
  248. }
  249. /*
  250. * Set the current thread's cpus_allowed mask so that it
  251. * will only run on cpus in the given pool.
  252. *
  253. * Returns 1 and fills in oldmask iff a cpumask was applied.
  254. */
  255. static inline int
  256. svc_pool_map_set_cpumask(unsigned int pidx, cpumask_t *oldmask)
  257. {
  258. struct svc_pool_map *m = &svc_pool_map;
  259. unsigned int node; /* or cpu */
  260. /*
  261. * The caller checks for sv_nrpools > 1, which
  262. * implies that we've been initialized.
  263. */
  264. BUG_ON(m->count == 0);
  265. switch (m->mode)
  266. {
  267. default:
  268. return 0;
  269. case SVC_POOL_PERCPU:
  270. node = m->pool_to[pidx];
  271. *oldmask = current->cpus_allowed;
  272. set_cpus_allowed(current, cpumask_of_cpu(node));
  273. return 1;
  274. case SVC_POOL_PERNODE:
  275. node = m->pool_to[pidx];
  276. *oldmask = current->cpus_allowed;
  277. set_cpus_allowed(current, node_to_cpumask(node));
  278. return 1;
  279. }
  280. }
  281. /*
  282. * Use the mapping mode to choose a pool for a given CPU.
  283. * Used when enqueueing an incoming RPC. Always returns
  284. * a non-NULL pool pointer.
  285. */
  286. struct svc_pool *
  287. svc_pool_for_cpu(struct svc_serv *serv, int cpu)
  288. {
  289. struct svc_pool_map *m = &svc_pool_map;
  290. unsigned int pidx = 0;
  291. /*
  292. * An uninitialised map happens in a pure client when
  293. * lockd is brought up, so silently treat it the
  294. * same as SVC_POOL_GLOBAL.
  295. */
  296. if (svc_serv_is_pooled(serv)) {
  297. switch (m->mode) {
  298. case SVC_POOL_PERCPU:
  299. pidx = m->to_pool[cpu];
  300. break;
  301. case SVC_POOL_PERNODE:
  302. pidx = m->to_pool[cpu_to_node(cpu)];
  303. break;
  304. }
  305. }
  306. return &serv->sv_pools[pidx % serv->sv_nrpools];
  307. }
  308. /*
  309. * Create an RPC service
  310. */
  311. static struct svc_serv *
  312. __svc_create(struct svc_program *prog, unsigned int bufsize, int npools,
  313. void (*shutdown)(struct svc_serv *serv))
  314. {
  315. struct svc_serv *serv;
  316. int vers;
  317. unsigned int xdrsize;
  318. unsigned int i;
  319. if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL)))
  320. return NULL;
  321. serv->sv_name = prog->pg_name;
  322. serv->sv_program = prog;
  323. serv->sv_nrthreads = 1;
  324. serv->sv_stats = prog->pg_stats;
  325. if (bufsize > RPCSVC_MAXPAYLOAD)
  326. bufsize = RPCSVC_MAXPAYLOAD;
  327. serv->sv_max_payload = bufsize? bufsize : 4096;
  328. serv->sv_max_mesg = roundup(serv->sv_max_payload + PAGE_SIZE, PAGE_SIZE);
  329. serv->sv_shutdown = shutdown;
  330. xdrsize = 0;
  331. while (prog) {
  332. prog->pg_lovers = prog->pg_nvers-1;
  333. for (vers=0; vers<prog->pg_nvers ; vers++)
  334. if (prog->pg_vers[vers]) {
  335. prog->pg_hivers = vers;
  336. if (prog->pg_lovers > vers)
  337. prog->pg_lovers = vers;
  338. if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
  339. xdrsize = prog->pg_vers[vers]->vs_xdrsize;
  340. }
  341. prog = prog->pg_next;
  342. }
  343. serv->sv_xdrsize = xdrsize;
  344. INIT_LIST_HEAD(&serv->sv_tempsocks);
  345. INIT_LIST_HEAD(&serv->sv_permsocks);
  346. init_timer(&serv->sv_temptimer);
  347. spin_lock_init(&serv->sv_lock);
  348. serv->sv_nrpools = npools;
  349. serv->sv_pools =
  350. kcalloc(serv->sv_nrpools, sizeof(struct svc_pool),
  351. GFP_KERNEL);
  352. if (!serv->sv_pools) {
  353. kfree(serv);
  354. return NULL;
  355. }
  356. for (i = 0; i < serv->sv_nrpools; i++) {
  357. struct svc_pool *pool = &serv->sv_pools[i];
  358. dprintk("svc: initialising pool %u for %s\n",
  359. i, serv->sv_name);
  360. pool->sp_id = i;
  361. INIT_LIST_HEAD(&pool->sp_threads);
  362. INIT_LIST_HEAD(&pool->sp_sockets);
  363. INIT_LIST_HEAD(&pool->sp_all_threads);
  364. spin_lock_init(&pool->sp_lock);
  365. }
  366. /* Remove any stale portmap registrations */
  367. svc_register(serv, 0, 0);
  368. return serv;
  369. }
  370. struct svc_serv *
  371. svc_create(struct svc_program *prog, unsigned int bufsize,
  372. void (*shutdown)(struct svc_serv *serv))
  373. {
  374. return __svc_create(prog, bufsize, /*npools*/1, shutdown);
  375. }
  376. EXPORT_SYMBOL(svc_create);
  377. struct svc_serv *
  378. svc_create_pooled(struct svc_program *prog, unsigned int bufsize,
  379. void (*shutdown)(struct svc_serv *serv),
  380. svc_thread_fn func, int sig, struct module *mod)
  381. {
  382. struct svc_serv *serv;
  383. unsigned int npools = svc_pool_map_get();
  384. serv = __svc_create(prog, bufsize, npools, shutdown);
  385. if (serv != NULL) {
  386. serv->sv_function = func;
  387. serv->sv_kill_signal = sig;
  388. serv->sv_module = mod;
  389. }
  390. return serv;
  391. }
  392. EXPORT_SYMBOL(svc_create_pooled);
  393. /*
  394. * Destroy an RPC service. Should be called with the BKL held
  395. */
  396. void
  397. svc_destroy(struct svc_serv *serv)
  398. {
  399. dprintk("svc: svc_destroy(%s, %d)\n",
  400. serv->sv_program->pg_name,
  401. serv->sv_nrthreads);
  402. if (serv->sv_nrthreads) {
  403. if (--(serv->sv_nrthreads) != 0) {
  404. svc_sock_update_bufs(serv);
  405. return;
  406. }
  407. } else
  408. printk("svc_destroy: no threads for serv=%p!\n", serv);
  409. del_timer_sync(&serv->sv_temptimer);
  410. svc_close_all(&serv->sv_tempsocks);
  411. if (serv->sv_shutdown)
  412. serv->sv_shutdown(serv);
  413. svc_close_all(&serv->sv_permsocks);
  414. BUG_ON(!list_empty(&serv->sv_permsocks));
  415. BUG_ON(!list_empty(&serv->sv_tempsocks));
  416. cache_clean_deferred(serv);
  417. if (svc_serv_is_pooled(serv))
  418. svc_pool_map_put();
  419. /* Unregister service with the portmapper */
  420. svc_register(serv, 0, 0);
  421. kfree(serv->sv_pools);
  422. kfree(serv);
  423. }
  424. EXPORT_SYMBOL(svc_destroy);
  425. /*
  426. * Allocate an RPC server's buffer space.
  427. * We allocate pages and place them in rq_argpages.
  428. */
  429. static int
  430. svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
  431. {
  432. int pages;
  433. int arghi;
  434. pages = size / PAGE_SIZE + 1; /* extra page as we hold both request and reply.
  435. * We assume one is at most one page
  436. */
  437. arghi = 0;
  438. BUG_ON(pages > RPCSVC_MAXPAGES);
  439. while (pages) {
  440. struct page *p = alloc_page(GFP_KERNEL);
  441. if (!p)
  442. break;
  443. rqstp->rq_pages[arghi++] = p;
  444. pages--;
  445. }
  446. return ! pages;
  447. }
  448. /*
  449. * Release an RPC server buffer
  450. */
  451. static void
  452. svc_release_buffer(struct svc_rqst *rqstp)
  453. {
  454. int i;
  455. for (i=0; i<ARRAY_SIZE(rqstp->rq_pages); i++)
  456. if (rqstp->rq_pages[i])
  457. put_page(rqstp->rq_pages[i]);
  458. }
  459. struct svc_rqst *
  460. svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool)
  461. {
  462. struct svc_rqst *rqstp;
  463. rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL);
  464. if (!rqstp)
  465. goto out_enomem;
  466. init_waitqueue_head(&rqstp->rq_wait);
  467. serv->sv_nrthreads++;
  468. spin_lock_bh(&pool->sp_lock);
  469. pool->sp_nrthreads++;
  470. list_add(&rqstp->rq_all, &pool->sp_all_threads);
  471. spin_unlock_bh(&pool->sp_lock);
  472. rqstp->rq_server = serv;
  473. rqstp->rq_pool = pool;
  474. rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL);
  475. if (!rqstp->rq_argp)
  476. goto out_thread;
  477. rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL);
  478. if (!rqstp->rq_resp)
  479. goto out_thread;
  480. if (!svc_init_buffer(rqstp, serv->sv_max_mesg))
  481. goto out_thread;
  482. return rqstp;
  483. out_thread:
  484. svc_exit_thread(rqstp);
  485. out_enomem:
  486. return ERR_PTR(-ENOMEM);
  487. }
  488. EXPORT_SYMBOL(svc_prepare_thread);
  489. /*
  490. * Create a thread in the given pool. Caller must hold BKL.
  491. * On a NUMA or SMP machine, with a multi-pool serv, the thread
  492. * will be restricted to run on the cpus belonging to the pool.
  493. */
  494. static int
  495. __svc_create_thread(svc_thread_fn func, struct svc_serv *serv,
  496. struct svc_pool *pool)
  497. {
  498. struct svc_rqst *rqstp;
  499. int error = -ENOMEM;
  500. int have_oldmask = 0;
  501. cpumask_t oldmask;
  502. rqstp = svc_prepare_thread(serv, pool);
  503. if (IS_ERR(rqstp)) {
  504. error = PTR_ERR(rqstp);
  505. goto out;
  506. }
  507. if (serv->sv_nrpools > 1)
  508. have_oldmask = svc_pool_map_set_cpumask(pool->sp_id, &oldmask);
  509. error = kernel_thread((int (*)(void *)) func, rqstp, 0);
  510. if (have_oldmask)
  511. set_cpus_allowed(current, oldmask);
  512. if (error < 0)
  513. goto out_thread;
  514. svc_sock_update_bufs(serv);
  515. error = 0;
  516. out:
  517. return error;
  518. out_thread:
  519. svc_exit_thread(rqstp);
  520. goto out;
  521. }
  522. /*
  523. * Create a thread in the default pool. Caller must hold BKL.
  524. */
  525. int
  526. svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
  527. {
  528. return __svc_create_thread(func, serv, &serv->sv_pools[0]);
  529. }
  530. EXPORT_SYMBOL(svc_create_thread);
  531. /*
  532. * Choose a pool in which to create a new thread, for svc_set_num_threads
  533. */
  534. static inline struct svc_pool *
  535. choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  536. {
  537. if (pool != NULL)
  538. return pool;
  539. return &serv->sv_pools[(*state)++ % serv->sv_nrpools];
  540. }
  541. /*
  542. * Choose a thread to kill, for svc_set_num_threads
  543. */
  544. static inline struct task_struct *
  545. choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  546. {
  547. unsigned int i;
  548. struct task_struct *task = NULL;
  549. if (pool != NULL) {
  550. spin_lock_bh(&pool->sp_lock);
  551. } else {
  552. /* choose a pool in round-robin fashion */
  553. for (i = 0; i < serv->sv_nrpools; i++) {
  554. pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
  555. spin_lock_bh(&pool->sp_lock);
  556. if (!list_empty(&pool->sp_all_threads))
  557. goto found_pool;
  558. spin_unlock_bh(&pool->sp_lock);
  559. }
  560. return NULL;
  561. }
  562. found_pool:
  563. if (!list_empty(&pool->sp_all_threads)) {
  564. struct svc_rqst *rqstp;
  565. /*
  566. * Remove from the pool->sp_all_threads list
  567. * so we don't try to kill it again.
  568. */
  569. rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all);
  570. list_del_init(&rqstp->rq_all);
  571. task = rqstp->rq_task;
  572. }
  573. spin_unlock_bh(&pool->sp_lock);
  574. return task;
  575. }
  576. /*
  577. * Create or destroy enough new threads to make the number
  578. * of threads the given number. If `pool' is non-NULL, applies
  579. * only to threads in that pool, otherwise round-robins between
  580. * all pools. Must be called with a svc_get() reference and
  581. * the BKL held.
  582. *
  583. * Destroying threads relies on the service threads filling in
  584. * rqstp->rq_task, which only the nfs ones do. Assumes the serv
  585. * has been created using svc_create_pooled().
  586. *
  587. * Based on code that used to be in nfsd_svc() but tweaked
  588. * to be pool-aware.
  589. */
  590. int
  591. svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
  592. {
  593. struct task_struct *victim;
  594. int error = 0;
  595. unsigned int state = serv->sv_nrthreads-1;
  596. if (pool == NULL) {
  597. /* The -1 assumes caller has done a svc_get() */
  598. nrservs -= (serv->sv_nrthreads-1);
  599. } else {
  600. spin_lock_bh(&pool->sp_lock);
  601. nrservs -= pool->sp_nrthreads;
  602. spin_unlock_bh(&pool->sp_lock);
  603. }
  604. /* create new threads */
  605. while (nrservs > 0) {
  606. nrservs--;
  607. __module_get(serv->sv_module);
  608. error = __svc_create_thread(serv->sv_function, serv,
  609. choose_pool(serv, pool, &state));
  610. if (error < 0) {
  611. module_put(serv->sv_module);
  612. break;
  613. }
  614. }
  615. /* destroy old threads */
  616. while (nrservs < 0 &&
  617. (victim = choose_victim(serv, pool, &state)) != NULL) {
  618. send_sig(serv->sv_kill_signal, victim, 1);
  619. nrservs++;
  620. }
  621. return error;
  622. }
  623. EXPORT_SYMBOL(svc_set_num_threads);
  624. /*
  625. * Called from a server thread as it's exiting. Caller must hold BKL.
  626. */
  627. void
  628. svc_exit_thread(struct svc_rqst *rqstp)
  629. {
  630. struct svc_serv *serv = rqstp->rq_server;
  631. struct svc_pool *pool = rqstp->rq_pool;
  632. svc_release_buffer(rqstp);
  633. kfree(rqstp->rq_resp);
  634. kfree(rqstp->rq_argp);
  635. kfree(rqstp->rq_auth_data);
  636. spin_lock_bh(&pool->sp_lock);
  637. pool->sp_nrthreads--;
  638. list_del(&rqstp->rq_all);
  639. spin_unlock_bh(&pool->sp_lock);
  640. kfree(rqstp);
  641. /* Release the server */
  642. if (serv)
  643. svc_destroy(serv);
  644. }
  645. EXPORT_SYMBOL(svc_exit_thread);
  646. /*
  647. * Register an RPC service with the local portmapper.
  648. * To unregister a service, call this routine with
  649. * proto and port == 0.
  650. */
  651. int
  652. svc_register(struct svc_serv *serv, int proto, unsigned short port)
  653. {
  654. struct svc_program *progp;
  655. unsigned long flags;
  656. int i, error = 0, dummy;
  657. if (!port)
  658. clear_thread_flag(TIF_SIGPENDING);
  659. for (progp = serv->sv_program; progp; progp = progp->pg_next) {
  660. for (i = 0; i < progp->pg_nvers; i++) {
  661. if (progp->pg_vers[i] == NULL)
  662. continue;
  663. dprintk("svc: svc_register(%s, %s, %d, %d)%s\n",
  664. progp->pg_name,
  665. proto == IPPROTO_UDP? "udp" : "tcp",
  666. port,
  667. i,
  668. progp->pg_vers[i]->vs_hidden?
  669. " (but not telling portmap)" : "");
  670. if (progp->pg_vers[i]->vs_hidden)
  671. continue;
  672. error = rpcb_register(progp->pg_prog, i, proto, port, &dummy);
  673. if (error < 0)
  674. break;
  675. if (port && !dummy) {
  676. error = -EACCES;
  677. break;
  678. }
  679. }
  680. }
  681. if (!port) {
  682. spin_lock_irqsave(&current->sighand->siglock, flags);
  683. recalc_sigpending();
  684. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  685. }
  686. return error;
  687. }
  688. /*
  689. * Printk the given error with the address of the client that caused it.
  690. */
  691. static int
  692. __attribute__ ((format (printf, 2, 3)))
  693. svc_printk(struct svc_rqst *rqstp, const char *fmt, ...)
  694. {
  695. va_list args;
  696. int r;
  697. char buf[RPC_MAX_ADDRBUFLEN];
  698. if (!net_ratelimit())
  699. return 0;
  700. printk(KERN_WARNING "svc: %s: ",
  701. svc_print_addr(rqstp, buf, sizeof(buf)));
  702. va_start(args, fmt);
  703. r = vprintk(fmt, args);
  704. va_end(args);
  705. return r;
  706. }
  707. /*
  708. * Process the RPC request.
  709. */
  710. int
  711. svc_process(struct svc_rqst *rqstp)
  712. {
  713. struct svc_program *progp;
  714. struct svc_version *versp = NULL; /* compiler food */
  715. struct svc_procedure *procp = NULL;
  716. struct kvec * argv = &rqstp->rq_arg.head[0];
  717. struct kvec * resv = &rqstp->rq_res.head[0];
  718. struct svc_serv *serv = rqstp->rq_server;
  719. kxdrproc_t xdr;
  720. __be32 *statp;
  721. u32 dir, prog, vers, proc;
  722. __be32 auth_stat, rpc_stat;
  723. int auth_res;
  724. __be32 *reply_statp;
  725. rpc_stat = rpc_success;
  726. if (argv->iov_len < 6*4)
  727. goto err_short_len;
  728. /* setup response xdr_buf.
  729. * Initially it has just one page
  730. */
  731. rqstp->rq_resused = 1;
  732. resv->iov_base = page_address(rqstp->rq_respages[0]);
  733. resv->iov_len = 0;
  734. rqstp->rq_res.pages = rqstp->rq_respages + 1;
  735. rqstp->rq_res.len = 0;
  736. rqstp->rq_res.page_base = 0;
  737. rqstp->rq_res.page_len = 0;
  738. rqstp->rq_res.buflen = PAGE_SIZE;
  739. rqstp->rq_res.tail[0].iov_base = NULL;
  740. rqstp->rq_res.tail[0].iov_len = 0;
  741. /* Will be turned off only in gss privacy case: */
  742. rqstp->rq_splice_ok = 1;
  743. /* Setup reply header */
  744. rqstp->rq_xprt->xpt_ops->xpo_prep_reply_hdr(rqstp);
  745. rqstp->rq_xid = svc_getu32(argv);
  746. svc_putu32(resv, rqstp->rq_xid);
  747. dir = svc_getnl(argv);
  748. vers = svc_getnl(argv);
  749. /* First words of reply: */
  750. svc_putnl(resv, 1); /* REPLY */
  751. if (dir != 0) /* direction != CALL */
  752. goto err_bad_dir;
  753. if (vers != 2) /* RPC version number */
  754. goto err_bad_rpc;
  755. /* Save position in case we later decide to reject: */
  756. reply_statp = resv->iov_base + resv->iov_len;
  757. svc_putnl(resv, 0); /* ACCEPT */
  758. rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
  759. rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
  760. rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
  761. progp = serv->sv_program;
  762. for (progp = serv->sv_program; progp; progp = progp->pg_next)
  763. if (prog == progp->pg_prog)
  764. break;
  765. /*
  766. * Decode auth data, and add verifier to reply buffer.
  767. * We do this before anything else in order to get a decent
  768. * auth verifier.
  769. */
  770. auth_res = svc_authenticate(rqstp, &auth_stat);
  771. /* Also give the program a chance to reject this call: */
  772. if (auth_res == SVC_OK && progp) {
  773. auth_stat = rpc_autherr_badcred;
  774. auth_res = progp->pg_authenticate(rqstp);
  775. }
  776. switch (auth_res) {
  777. case SVC_OK:
  778. break;
  779. case SVC_GARBAGE:
  780. rpc_stat = rpc_garbage_args;
  781. goto err_bad;
  782. case SVC_SYSERR:
  783. rpc_stat = rpc_system_err;
  784. goto err_bad;
  785. case SVC_DENIED:
  786. goto err_bad_auth;
  787. case SVC_DROP:
  788. goto dropit;
  789. case SVC_COMPLETE:
  790. goto sendit;
  791. }
  792. if (progp == NULL)
  793. goto err_bad_prog;
  794. if (vers >= progp->pg_nvers ||
  795. !(versp = progp->pg_vers[vers]))
  796. goto err_bad_vers;
  797. procp = versp->vs_proc + proc;
  798. if (proc >= versp->vs_nproc || !procp->pc_func)
  799. goto err_bad_proc;
  800. rqstp->rq_server = serv;
  801. rqstp->rq_procinfo = procp;
  802. /* Syntactic check complete */
  803. serv->sv_stats->rpccnt++;
  804. /* Build the reply header. */
  805. statp = resv->iov_base +resv->iov_len;
  806. svc_putnl(resv, RPC_SUCCESS);
  807. /* Bump per-procedure stats counter */
  808. procp->pc_count++;
  809. /* Initialize storage for argp and resp */
  810. memset(rqstp->rq_argp, 0, procp->pc_argsize);
  811. memset(rqstp->rq_resp, 0, procp->pc_ressize);
  812. /* un-reserve some of the out-queue now that we have a
  813. * better idea of reply size
  814. */
  815. if (procp->pc_xdrressize)
  816. svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
  817. /* Call the function that processes the request. */
  818. if (!versp->vs_dispatch) {
  819. /* Decode arguments */
  820. xdr = procp->pc_decode;
  821. if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
  822. goto err_garbage;
  823. *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  824. /* Encode reply */
  825. if (*statp == rpc_drop_reply) {
  826. if (procp->pc_release)
  827. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  828. goto dropit;
  829. }
  830. if (*statp == rpc_success && (xdr = procp->pc_encode)
  831. && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
  832. dprintk("svc: failed to encode reply\n");
  833. /* serv->sv_stats->rpcsystemerr++; */
  834. *statp = rpc_system_err;
  835. }
  836. } else {
  837. dprintk("svc: calling dispatcher\n");
  838. if (!versp->vs_dispatch(rqstp, statp)) {
  839. /* Release reply info */
  840. if (procp->pc_release)
  841. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  842. goto dropit;
  843. }
  844. }
  845. /* Check RPC status result */
  846. if (*statp != rpc_success)
  847. resv->iov_len = ((void*)statp) - resv->iov_base + 4;
  848. /* Release reply info */
  849. if (procp->pc_release)
  850. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  851. if (procp->pc_encode == NULL)
  852. goto dropit;
  853. sendit:
  854. if (svc_authorise(rqstp))
  855. goto dropit;
  856. return svc_send(rqstp);
  857. dropit:
  858. svc_authorise(rqstp); /* doesn't hurt to call this twice */
  859. dprintk("svc: svc_process dropit\n");
  860. svc_drop(rqstp);
  861. return 0;
  862. err_short_len:
  863. svc_printk(rqstp, "short len %Zd, dropping request\n",
  864. argv->iov_len);
  865. goto dropit; /* drop request */
  866. err_bad_dir:
  867. svc_printk(rqstp, "bad direction %d, dropping request\n", dir);
  868. serv->sv_stats->rpcbadfmt++;
  869. goto dropit; /* drop request */
  870. err_bad_rpc:
  871. serv->sv_stats->rpcbadfmt++;
  872. svc_putnl(resv, 1); /* REJECT */
  873. svc_putnl(resv, 0); /* RPC_MISMATCH */
  874. svc_putnl(resv, 2); /* Only RPCv2 supported */
  875. svc_putnl(resv, 2);
  876. goto sendit;
  877. err_bad_auth:
  878. dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
  879. serv->sv_stats->rpcbadauth++;
  880. /* Restore write pointer to location of accept status: */
  881. xdr_ressize_check(rqstp, reply_statp);
  882. svc_putnl(resv, 1); /* REJECT */
  883. svc_putnl(resv, 1); /* AUTH_ERROR */
  884. svc_putnl(resv, ntohl(auth_stat)); /* status */
  885. goto sendit;
  886. err_bad_prog:
  887. dprintk("svc: unknown program %d\n", prog);
  888. serv->sv_stats->rpcbadfmt++;
  889. svc_putnl(resv, RPC_PROG_UNAVAIL);
  890. goto sendit;
  891. err_bad_vers:
  892. svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
  893. vers, prog, progp->pg_name);
  894. serv->sv_stats->rpcbadfmt++;
  895. svc_putnl(resv, RPC_PROG_MISMATCH);
  896. svc_putnl(resv, progp->pg_lovers);
  897. svc_putnl(resv, progp->pg_hivers);
  898. goto sendit;
  899. err_bad_proc:
  900. svc_printk(rqstp, "unknown procedure (%d)\n", proc);
  901. serv->sv_stats->rpcbadfmt++;
  902. svc_putnl(resv, RPC_PROC_UNAVAIL);
  903. goto sendit;
  904. err_garbage:
  905. svc_printk(rqstp, "failed to decode args\n");
  906. rpc_stat = rpc_garbage_args;
  907. err_bad:
  908. serv->sv_stats->rpcbadfmt++;
  909. svc_putnl(resv, ntohl(rpc_stat));
  910. goto sendit;
  911. }
  912. EXPORT_SYMBOL(svc_process);
  913. /*
  914. * Return (transport-specific) limit on the rpc payload.
  915. */
  916. u32 svc_max_payload(const struct svc_rqst *rqstp)
  917. {
  918. u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload;
  919. if (rqstp->rq_server->sv_max_payload < max)
  920. max = rqstp->rq_server->sv_max_payload;
  921. return max;
  922. }
  923. EXPORT_SYMBOL_GPL(svc_max_payload);