svc.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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/kthread.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 given thread's cpus_allowed mask so that it
  251. * will only run on cpus in the given pool.
  252. */
  253. static inline void
  254. svc_pool_map_set_cpumask(struct task_struct *task, unsigned int pidx)
  255. {
  256. struct svc_pool_map *m = &svc_pool_map;
  257. unsigned int node = m->pool_to[pidx];
  258. /*
  259. * The caller checks for sv_nrpools > 1, which
  260. * implies that we've been initialized.
  261. */
  262. BUG_ON(m->count == 0);
  263. switch (m->mode) {
  264. case SVC_POOL_PERCPU:
  265. {
  266. cpumask_of_cpu_ptr(cpumask, node);
  267. set_cpus_allowed_ptr(task, cpumask);
  268. break;
  269. }
  270. case SVC_POOL_PERNODE:
  271. {
  272. node_to_cpumask_ptr(nodecpumask, node);
  273. set_cpus_allowed_ptr(task, nodecpumask);
  274. break;
  275. }
  276. }
  277. }
  278. /*
  279. * Use the mapping mode to choose a pool for a given CPU.
  280. * Used when enqueueing an incoming RPC. Always returns
  281. * a non-NULL pool pointer.
  282. */
  283. struct svc_pool *
  284. svc_pool_for_cpu(struct svc_serv *serv, int cpu)
  285. {
  286. struct svc_pool_map *m = &svc_pool_map;
  287. unsigned int pidx = 0;
  288. /*
  289. * An uninitialised map happens in a pure client when
  290. * lockd is brought up, so silently treat it the
  291. * same as SVC_POOL_GLOBAL.
  292. */
  293. if (svc_serv_is_pooled(serv)) {
  294. switch (m->mode) {
  295. case SVC_POOL_PERCPU:
  296. pidx = m->to_pool[cpu];
  297. break;
  298. case SVC_POOL_PERNODE:
  299. pidx = m->to_pool[cpu_to_node(cpu)];
  300. break;
  301. }
  302. }
  303. return &serv->sv_pools[pidx % serv->sv_nrpools];
  304. }
  305. /*
  306. * Create an RPC service
  307. */
  308. static struct svc_serv *
  309. __svc_create(struct svc_program *prog, unsigned int bufsize, int npools,
  310. void (*shutdown)(struct svc_serv *serv))
  311. {
  312. struct svc_serv *serv;
  313. unsigned int vers;
  314. unsigned int xdrsize;
  315. unsigned int i;
  316. if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL)))
  317. return NULL;
  318. serv->sv_name = prog->pg_name;
  319. serv->sv_program = prog;
  320. serv->sv_nrthreads = 1;
  321. serv->sv_stats = prog->pg_stats;
  322. if (bufsize > RPCSVC_MAXPAYLOAD)
  323. bufsize = RPCSVC_MAXPAYLOAD;
  324. serv->sv_max_payload = bufsize? bufsize : 4096;
  325. serv->sv_max_mesg = roundup(serv->sv_max_payload + PAGE_SIZE, PAGE_SIZE);
  326. serv->sv_shutdown = shutdown;
  327. xdrsize = 0;
  328. while (prog) {
  329. prog->pg_lovers = prog->pg_nvers-1;
  330. for (vers=0; vers<prog->pg_nvers ; vers++)
  331. if (prog->pg_vers[vers]) {
  332. prog->pg_hivers = vers;
  333. if (prog->pg_lovers > vers)
  334. prog->pg_lovers = vers;
  335. if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
  336. xdrsize = prog->pg_vers[vers]->vs_xdrsize;
  337. }
  338. prog = prog->pg_next;
  339. }
  340. serv->sv_xdrsize = xdrsize;
  341. INIT_LIST_HEAD(&serv->sv_tempsocks);
  342. INIT_LIST_HEAD(&serv->sv_permsocks);
  343. init_timer(&serv->sv_temptimer);
  344. spin_lock_init(&serv->sv_lock);
  345. serv->sv_nrpools = npools;
  346. serv->sv_pools =
  347. kcalloc(serv->sv_nrpools, sizeof(struct svc_pool),
  348. GFP_KERNEL);
  349. if (!serv->sv_pools) {
  350. kfree(serv);
  351. return NULL;
  352. }
  353. for (i = 0; i < serv->sv_nrpools; i++) {
  354. struct svc_pool *pool = &serv->sv_pools[i];
  355. dprintk("svc: initialising pool %u for %s\n",
  356. i, serv->sv_name);
  357. pool->sp_id = i;
  358. INIT_LIST_HEAD(&pool->sp_threads);
  359. INIT_LIST_HEAD(&pool->sp_sockets);
  360. INIT_LIST_HEAD(&pool->sp_all_threads);
  361. spin_lock_init(&pool->sp_lock);
  362. }
  363. /* Remove any stale portmap registrations */
  364. svc_register(serv, 0, 0);
  365. return serv;
  366. }
  367. struct svc_serv *
  368. svc_create(struct svc_program *prog, unsigned int bufsize,
  369. void (*shutdown)(struct svc_serv *serv))
  370. {
  371. return __svc_create(prog, bufsize, /*npools*/1, shutdown);
  372. }
  373. EXPORT_SYMBOL(svc_create);
  374. struct svc_serv *
  375. svc_create_pooled(struct svc_program *prog, unsigned int bufsize,
  376. void (*shutdown)(struct svc_serv *serv),
  377. svc_thread_fn func, struct module *mod)
  378. {
  379. struct svc_serv *serv;
  380. unsigned int npools = svc_pool_map_get();
  381. serv = __svc_create(prog, bufsize, npools, shutdown);
  382. if (serv != NULL) {
  383. serv->sv_function = func;
  384. serv->sv_module = mod;
  385. }
  386. return serv;
  387. }
  388. EXPORT_SYMBOL(svc_create_pooled);
  389. /*
  390. * Destroy an RPC service. Should be called with appropriate locking to
  391. * protect the sv_nrthreads, sv_permsocks and sv_tempsocks.
  392. */
  393. void
  394. svc_destroy(struct svc_serv *serv)
  395. {
  396. dprintk("svc: svc_destroy(%s, %d)\n",
  397. serv->sv_program->pg_name,
  398. serv->sv_nrthreads);
  399. if (serv->sv_nrthreads) {
  400. if (--(serv->sv_nrthreads) != 0) {
  401. svc_sock_update_bufs(serv);
  402. return;
  403. }
  404. } else
  405. printk("svc_destroy: no threads for serv=%p!\n", serv);
  406. del_timer_sync(&serv->sv_temptimer);
  407. svc_close_all(&serv->sv_tempsocks);
  408. if (serv->sv_shutdown)
  409. serv->sv_shutdown(serv);
  410. svc_close_all(&serv->sv_permsocks);
  411. BUG_ON(!list_empty(&serv->sv_permsocks));
  412. BUG_ON(!list_empty(&serv->sv_tempsocks));
  413. cache_clean_deferred(serv);
  414. if (svc_serv_is_pooled(serv))
  415. svc_pool_map_put();
  416. /* Unregister service with the portmapper */
  417. svc_register(serv, 0, 0);
  418. kfree(serv->sv_pools);
  419. kfree(serv);
  420. }
  421. EXPORT_SYMBOL(svc_destroy);
  422. /*
  423. * Allocate an RPC server's buffer space.
  424. * We allocate pages and place them in rq_argpages.
  425. */
  426. static int
  427. svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
  428. {
  429. unsigned int pages, arghi;
  430. pages = size / PAGE_SIZE + 1; /* extra page as we hold both request and reply.
  431. * We assume one is at most one page
  432. */
  433. arghi = 0;
  434. BUG_ON(pages > RPCSVC_MAXPAGES);
  435. while (pages) {
  436. struct page *p = alloc_page(GFP_KERNEL);
  437. if (!p)
  438. break;
  439. rqstp->rq_pages[arghi++] = p;
  440. pages--;
  441. }
  442. return pages == 0;
  443. }
  444. /*
  445. * Release an RPC server buffer
  446. */
  447. static void
  448. svc_release_buffer(struct svc_rqst *rqstp)
  449. {
  450. unsigned int i;
  451. for (i = 0; i < ARRAY_SIZE(rqstp->rq_pages); i++)
  452. if (rqstp->rq_pages[i])
  453. put_page(rqstp->rq_pages[i]);
  454. }
  455. struct svc_rqst *
  456. svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool)
  457. {
  458. struct svc_rqst *rqstp;
  459. rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL);
  460. if (!rqstp)
  461. goto out_enomem;
  462. init_waitqueue_head(&rqstp->rq_wait);
  463. serv->sv_nrthreads++;
  464. spin_lock_bh(&pool->sp_lock);
  465. pool->sp_nrthreads++;
  466. list_add(&rqstp->rq_all, &pool->sp_all_threads);
  467. spin_unlock_bh(&pool->sp_lock);
  468. rqstp->rq_server = serv;
  469. rqstp->rq_pool = pool;
  470. rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL);
  471. if (!rqstp->rq_argp)
  472. goto out_thread;
  473. rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL);
  474. if (!rqstp->rq_resp)
  475. goto out_thread;
  476. if (!svc_init_buffer(rqstp, serv->sv_max_mesg))
  477. goto out_thread;
  478. return rqstp;
  479. out_thread:
  480. svc_exit_thread(rqstp);
  481. out_enomem:
  482. return ERR_PTR(-ENOMEM);
  483. }
  484. EXPORT_SYMBOL(svc_prepare_thread);
  485. /*
  486. * Choose a pool in which to create a new thread, for svc_set_num_threads
  487. */
  488. static inline struct svc_pool *
  489. choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  490. {
  491. if (pool != NULL)
  492. return pool;
  493. return &serv->sv_pools[(*state)++ % serv->sv_nrpools];
  494. }
  495. /*
  496. * Choose a thread to kill, for svc_set_num_threads
  497. */
  498. static inline struct task_struct *
  499. choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  500. {
  501. unsigned int i;
  502. struct task_struct *task = NULL;
  503. if (pool != NULL) {
  504. spin_lock_bh(&pool->sp_lock);
  505. } else {
  506. /* choose a pool in round-robin fashion */
  507. for (i = 0; i < serv->sv_nrpools; i++) {
  508. pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
  509. spin_lock_bh(&pool->sp_lock);
  510. if (!list_empty(&pool->sp_all_threads))
  511. goto found_pool;
  512. spin_unlock_bh(&pool->sp_lock);
  513. }
  514. return NULL;
  515. }
  516. found_pool:
  517. if (!list_empty(&pool->sp_all_threads)) {
  518. struct svc_rqst *rqstp;
  519. /*
  520. * Remove from the pool->sp_all_threads list
  521. * so we don't try to kill it again.
  522. */
  523. rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all);
  524. list_del_init(&rqstp->rq_all);
  525. task = rqstp->rq_task;
  526. }
  527. spin_unlock_bh(&pool->sp_lock);
  528. return task;
  529. }
  530. /*
  531. * Create or destroy enough new threads to make the number
  532. * of threads the given number. If `pool' is non-NULL, applies
  533. * only to threads in that pool, otherwise round-robins between
  534. * all pools. Must be called with a svc_get() reference and
  535. * the BKL or another lock to protect access to svc_serv fields.
  536. *
  537. * Destroying threads relies on the service threads filling in
  538. * rqstp->rq_task, which only the nfs ones do. Assumes the serv
  539. * has been created using svc_create_pooled().
  540. *
  541. * Based on code that used to be in nfsd_svc() but tweaked
  542. * to be pool-aware.
  543. */
  544. int
  545. svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
  546. {
  547. struct svc_rqst *rqstp;
  548. struct task_struct *task;
  549. struct svc_pool *chosen_pool;
  550. int error = 0;
  551. unsigned int state = serv->sv_nrthreads-1;
  552. if (pool == NULL) {
  553. /* The -1 assumes caller has done a svc_get() */
  554. nrservs -= (serv->sv_nrthreads-1);
  555. } else {
  556. spin_lock_bh(&pool->sp_lock);
  557. nrservs -= pool->sp_nrthreads;
  558. spin_unlock_bh(&pool->sp_lock);
  559. }
  560. /* create new threads */
  561. while (nrservs > 0) {
  562. nrservs--;
  563. chosen_pool = choose_pool(serv, pool, &state);
  564. rqstp = svc_prepare_thread(serv, chosen_pool);
  565. if (IS_ERR(rqstp)) {
  566. error = PTR_ERR(rqstp);
  567. break;
  568. }
  569. __module_get(serv->sv_module);
  570. task = kthread_create(serv->sv_function, rqstp, serv->sv_name);
  571. if (IS_ERR(task)) {
  572. error = PTR_ERR(task);
  573. module_put(serv->sv_module);
  574. svc_exit_thread(rqstp);
  575. break;
  576. }
  577. rqstp->rq_task = task;
  578. if (serv->sv_nrpools > 1)
  579. svc_pool_map_set_cpumask(task, chosen_pool->sp_id);
  580. svc_sock_update_bufs(serv);
  581. wake_up_process(task);
  582. }
  583. /* destroy old threads */
  584. while (nrservs < 0 &&
  585. (task = choose_victim(serv, pool, &state)) != NULL) {
  586. send_sig(SIGINT, task, 1);
  587. nrservs++;
  588. }
  589. return error;
  590. }
  591. EXPORT_SYMBOL(svc_set_num_threads);
  592. /*
  593. * Called from a server thread as it's exiting. Caller must hold the BKL or
  594. * the "service mutex", whichever is appropriate for the service.
  595. */
  596. void
  597. svc_exit_thread(struct svc_rqst *rqstp)
  598. {
  599. struct svc_serv *serv = rqstp->rq_server;
  600. struct svc_pool *pool = rqstp->rq_pool;
  601. svc_release_buffer(rqstp);
  602. kfree(rqstp->rq_resp);
  603. kfree(rqstp->rq_argp);
  604. kfree(rqstp->rq_auth_data);
  605. spin_lock_bh(&pool->sp_lock);
  606. pool->sp_nrthreads--;
  607. list_del(&rqstp->rq_all);
  608. spin_unlock_bh(&pool->sp_lock);
  609. kfree(rqstp);
  610. /* Release the server */
  611. if (serv)
  612. svc_destroy(serv);
  613. }
  614. EXPORT_SYMBOL(svc_exit_thread);
  615. /*
  616. * Register an RPC service with the local portmapper.
  617. * To unregister a service, call this routine with
  618. * proto and port == 0.
  619. */
  620. int
  621. svc_register(struct svc_serv *serv, int proto, unsigned short port)
  622. {
  623. struct svc_program *progp;
  624. unsigned long flags;
  625. unsigned int i;
  626. int error = 0, dummy;
  627. if (!port)
  628. clear_thread_flag(TIF_SIGPENDING);
  629. for (progp = serv->sv_program; progp; progp = progp->pg_next) {
  630. for (i = 0; i < progp->pg_nvers; i++) {
  631. if (progp->pg_vers[i] == NULL)
  632. continue;
  633. dprintk("svc: svc_register(%s, %s, %d, %d)%s\n",
  634. progp->pg_name,
  635. proto == IPPROTO_UDP? "udp" : "tcp",
  636. port,
  637. i,
  638. progp->pg_vers[i]->vs_hidden?
  639. " (but not telling portmap)" : "");
  640. if (progp->pg_vers[i]->vs_hidden)
  641. continue;
  642. error = rpcb_register(progp->pg_prog, i, proto, port, &dummy);
  643. if (error < 0)
  644. break;
  645. if (port && !dummy) {
  646. error = -EACCES;
  647. break;
  648. }
  649. }
  650. }
  651. if (!port) {
  652. spin_lock_irqsave(&current->sighand->siglock, flags);
  653. recalc_sigpending();
  654. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  655. }
  656. return error;
  657. }
  658. /*
  659. * Printk the given error with the address of the client that caused it.
  660. */
  661. static int
  662. __attribute__ ((format (printf, 2, 3)))
  663. svc_printk(struct svc_rqst *rqstp, const char *fmt, ...)
  664. {
  665. va_list args;
  666. int r;
  667. char buf[RPC_MAX_ADDRBUFLEN];
  668. if (!net_ratelimit())
  669. return 0;
  670. printk(KERN_WARNING "svc: %s: ",
  671. svc_print_addr(rqstp, buf, sizeof(buf)));
  672. va_start(args, fmt);
  673. r = vprintk(fmt, args);
  674. va_end(args);
  675. return r;
  676. }
  677. /*
  678. * Process the RPC request.
  679. */
  680. int
  681. svc_process(struct svc_rqst *rqstp)
  682. {
  683. struct svc_program *progp;
  684. struct svc_version *versp = NULL; /* compiler food */
  685. struct svc_procedure *procp = NULL;
  686. struct kvec * argv = &rqstp->rq_arg.head[0];
  687. struct kvec * resv = &rqstp->rq_res.head[0];
  688. struct svc_serv *serv = rqstp->rq_server;
  689. kxdrproc_t xdr;
  690. __be32 *statp;
  691. u32 dir, prog, vers, proc;
  692. __be32 auth_stat, rpc_stat;
  693. int auth_res;
  694. __be32 *reply_statp;
  695. rpc_stat = rpc_success;
  696. if (argv->iov_len < 6*4)
  697. goto err_short_len;
  698. /* setup response xdr_buf.
  699. * Initially it has just one page
  700. */
  701. rqstp->rq_resused = 1;
  702. resv->iov_base = page_address(rqstp->rq_respages[0]);
  703. resv->iov_len = 0;
  704. rqstp->rq_res.pages = rqstp->rq_respages + 1;
  705. rqstp->rq_res.len = 0;
  706. rqstp->rq_res.page_base = 0;
  707. rqstp->rq_res.page_len = 0;
  708. rqstp->rq_res.buflen = PAGE_SIZE;
  709. rqstp->rq_res.tail[0].iov_base = NULL;
  710. rqstp->rq_res.tail[0].iov_len = 0;
  711. /* Will be turned off only in gss privacy case: */
  712. rqstp->rq_splice_ok = 1;
  713. /* Setup reply header */
  714. rqstp->rq_xprt->xpt_ops->xpo_prep_reply_hdr(rqstp);
  715. rqstp->rq_xid = svc_getu32(argv);
  716. svc_putu32(resv, rqstp->rq_xid);
  717. dir = svc_getnl(argv);
  718. vers = svc_getnl(argv);
  719. /* First words of reply: */
  720. svc_putnl(resv, 1); /* REPLY */
  721. if (dir != 0) /* direction != CALL */
  722. goto err_bad_dir;
  723. if (vers != 2) /* RPC version number */
  724. goto err_bad_rpc;
  725. /* Save position in case we later decide to reject: */
  726. reply_statp = resv->iov_base + resv->iov_len;
  727. svc_putnl(resv, 0); /* ACCEPT */
  728. rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
  729. rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
  730. rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
  731. progp = serv->sv_program;
  732. for (progp = serv->sv_program; progp; progp = progp->pg_next)
  733. if (prog == progp->pg_prog)
  734. break;
  735. /*
  736. * Decode auth data, and add verifier to reply buffer.
  737. * We do this before anything else in order to get a decent
  738. * auth verifier.
  739. */
  740. auth_res = svc_authenticate(rqstp, &auth_stat);
  741. /* Also give the program a chance to reject this call: */
  742. if (auth_res == SVC_OK && progp) {
  743. auth_stat = rpc_autherr_badcred;
  744. auth_res = progp->pg_authenticate(rqstp);
  745. }
  746. switch (auth_res) {
  747. case SVC_OK:
  748. break;
  749. case SVC_GARBAGE:
  750. goto err_garbage;
  751. case SVC_SYSERR:
  752. rpc_stat = rpc_system_err;
  753. goto err_bad;
  754. case SVC_DENIED:
  755. goto err_bad_auth;
  756. case SVC_DROP:
  757. goto dropit;
  758. case SVC_COMPLETE:
  759. goto sendit;
  760. }
  761. if (progp == NULL)
  762. goto err_bad_prog;
  763. if (vers >= progp->pg_nvers ||
  764. !(versp = progp->pg_vers[vers]))
  765. goto err_bad_vers;
  766. procp = versp->vs_proc + proc;
  767. if (proc >= versp->vs_nproc || !procp->pc_func)
  768. goto err_bad_proc;
  769. rqstp->rq_server = serv;
  770. rqstp->rq_procinfo = procp;
  771. /* Syntactic check complete */
  772. serv->sv_stats->rpccnt++;
  773. /* Build the reply header. */
  774. statp = resv->iov_base +resv->iov_len;
  775. svc_putnl(resv, RPC_SUCCESS);
  776. /* Bump per-procedure stats counter */
  777. procp->pc_count++;
  778. /* Initialize storage for argp and resp */
  779. memset(rqstp->rq_argp, 0, procp->pc_argsize);
  780. memset(rqstp->rq_resp, 0, procp->pc_ressize);
  781. /* un-reserve some of the out-queue now that we have a
  782. * better idea of reply size
  783. */
  784. if (procp->pc_xdrressize)
  785. svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
  786. /* Call the function that processes the request. */
  787. if (!versp->vs_dispatch) {
  788. /* Decode arguments */
  789. xdr = procp->pc_decode;
  790. if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
  791. goto err_garbage;
  792. *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  793. /* Encode reply */
  794. if (*statp == rpc_drop_reply) {
  795. if (procp->pc_release)
  796. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  797. goto dropit;
  798. }
  799. if (*statp == rpc_success && (xdr = procp->pc_encode)
  800. && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
  801. dprintk("svc: failed to encode reply\n");
  802. /* serv->sv_stats->rpcsystemerr++; */
  803. *statp = rpc_system_err;
  804. }
  805. } else {
  806. dprintk("svc: calling dispatcher\n");
  807. if (!versp->vs_dispatch(rqstp, statp)) {
  808. /* Release reply info */
  809. if (procp->pc_release)
  810. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  811. goto dropit;
  812. }
  813. }
  814. /* Check RPC status result */
  815. if (*statp != rpc_success)
  816. resv->iov_len = ((void*)statp) - resv->iov_base + 4;
  817. /* Release reply info */
  818. if (procp->pc_release)
  819. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  820. if (procp->pc_encode == NULL)
  821. goto dropit;
  822. sendit:
  823. if (svc_authorise(rqstp))
  824. goto dropit;
  825. return svc_send(rqstp);
  826. dropit:
  827. svc_authorise(rqstp); /* doesn't hurt to call this twice */
  828. dprintk("svc: svc_process dropit\n");
  829. svc_drop(rqstp);
  830. return 0;
  831. err_short_len:
  832. svc_printk(rqstp, "short len %Zd, dropping request\n",
  833. argv->iov_len);
  834. goto dropit; /* drop request */
  835. err_bad_dir:
  836. svc_printk(rqstp, "bad direction %d, dropping request\n", dir);
  837. serv->sv_stats->rpcbadfmt++;
  838. goto dropit; /* drop request */
  839. err_bad_rpc:
  840. serv->sv_stats->rpcbadfmt++;
  841. svc_putnl(resv, 1); /* REJECT */
  842. svc_putnl(resv, 0); /* RPC_MISMATCH */
  843. svc_putnl(resv, 2); /* Only RPCv2 supported */
  844. svc_putnl(resv, 2);
  845. goto sendit;
  846. err_bad_auth:
  847. dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
  848. serv->sv_stats->rpcbadauth++;
  849. /* Restore write pointer to location of accept status: */
  850. xdr_ressize_check(rqstp, reply_statp);
  851. svc_putnl(resv, 1); /* REJECT */
  852. svc_putnl(resv, 1); /* AUTH_ERROR */
  853. svc_putnl(resv, ntohl(auth_stat)); /* status */
  854. goto sendit;
  855. err_bad_prog:
  856. dprintk("svc: unknown program %d\n", prog);
  857. serv->sv_stats->rpcbadfmt++;
  858. svc_putnl(resv, RPC_PROG_UNAVAIL);
  859. goto sendit;
  860. err_bad_vers:
  861. svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
  862. vers, prog, progp->pg_name);
  863. serv->sv_stats->rpcbadfmt++;
  864. svc_putnl(resv, RPC_PROG_MISMATCH);
  865. svc_putnl(resv, progp->pg_lovers);
  866. svc_putnl(resv, progp->pg_hivers);
  867. goto sendit;
  868. err_bad_proc:
  869. svc_printk(rqstp, "unknown procedure (%d)\n", proc);
  870. serv->sv_stats->rpcbadfmt++;
  871. svc_putnl(resv, RPC_PROC_UNAVAIL);
  872. goto sendit;
  873. err_garbage:
  874. svc_printk(rqstp, "failed to decode args\n");
  875. rpc_stat = rpc_garbage_args;
  876. err_bad:
  877. serv->sv_stats->rpcbadfmt++;
  878. svc_putnl(resv, ntohl(rpc_stat));
  879. goto sendit;
  880. }
  881. EXPORT_SYMBOL(svc_process);
  882. /*
  883. * Return (transport-specific) limit on the rpc payload.
  884. */
  885. u32 svc_max_payload(const struct svc_rqst *rqstp)
  886. {
  887. u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload;
  888. if (rqstp->rq_server->sv_max_payload < max)
  889. max = rqstp->rq_server->sv_max_payload;
  890. return max;
  891. }
  892. EXPORT_SYMBOL_GPL(svc_max_payload);