svc.c 24 KB

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