svc.c 31 KB

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