svc.c 29 KB

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