svc.c 24 KB

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