svc.c 24 KB

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