svc.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/net/sunrpc/svc.c
  4. *
  5. * High-level RPC service routines
  6. *
  7. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  8. *
  9. * Multiple threads pools and NUMAisation
  10. * Copyright (c) 2006 Silicon Graphics, Inc.
  11. * by Greg Banks <gnb@melbourne.sgi.com>
  12. */
  13. #include <linux/linkage.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/errno.h>
  16. #include <linux/net.h>
  17. #include <linux/in.h>
  18. #include <linux/mm.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/kthread.h>
  22. #include <linux/slab.h>
  23. #include <linux/sunrpc/types.h>
  24. #include <linux/sunrpc/xdr.h>
  25. #include <linux/sunrpc/stats.h>
  26. #include <linux/sunrpc/svcsock.h>
  27. #include <linux/sunrpc/clnt.h>
  28. #include <linux/sunrpc/bc_xprt.h>
  29. #include <trace/events/sunrpc.h>
  30. #include "fail.h"
  31. #include "sunrpc.h"
  32. #define RPCDBG_FACILITY RPCDBG_SVCDSP
  33. static void svc_unregister(const struct svc_serv *serv, struct net *net);
  34. #define SVC_POOL_DEFAULT SVC_POOL_GLOBAL
  35. /*
  36. * Mode for mapping cpus to pools.
  37. */
  38. enum {
  39. SVC_POOL_AUTO = -1, /* choose one of the others */
  40. SVC_POOL_GLOBAL, /* no mapping, just a single global pool
  41. * (legacy & UP mode) */
  42. SVC_POOL_PERCPU, /* one pool per cpu */
  43. SVC_POOL_PERNODE /* one pool per numa node */
  44. };
  45. /*
  46. * Structure for mapping cpus to pools and vice versa.
  47. * Setup once during sunrpc initialisation.
  48. */
  49. struct svc_pool_map {
  50. int count; /* How many svc_servs use us */
  51. int mode; /* Note: int not enum to avoid
  52. * warnings about "enumeration value
  53. * not handled in switch" */
  54. unsigned int npools;
  55. unsigned int *pool_to; /* maps pool id to cpu or node */
  56. unsigned int *to_pool; /* maps cpu or node to pool id */
  57. };
  58. static struct svc_pool_map svc_pool_map = {
  59. .mode = SVC_POOL_DEFAULT
  60. };
  61. static DEFINE_MUTEX(svc_pool_map_mutex);/* protects svc_pool_map.count only */
  62. static int
  63. __param_set_pool_mode(const char *val, struct svc_pool_map *m)
  64. {
  65. int err, mode;
  66. mutex_lock(&svc_pool_map_mutex);
  67. err = 0;
  68. if (!strncmp(val, "auto", 4))
  69. mode = SVC_POOL_AUTO;
  70. else if (!strncmp(val, "global", 6))
  71. mode = SVC_POOL_GLOBAL;
  72. else if (!strncmp(val, "percpu", 6))
  73. mode = SVC_POOL_PERCPU;
  74. else if (!strncmp(val, "pernode", 7))
  75. mode = SVC_POOL_PERNODE;
  76. else
  77. err = -EINVAL;
  78. if (err)
  79. goto out;
  80. if (m->count == 0)
  81. m->mode = mode;
  82. else if (mode != m->mode)
  83. err = -EBUSY;
  84. out:
  85. mutex_unlock(&svc_pool_map_mutex);
  86. return err;
  87. }
  88. static int
  89. param_set_pool_mode(const char *val, const struct kernel_param *kp)
  90. {
  91. struct svc_pool_map *m = kp->arg;
  92. return __param_set_pool_mode(val, m);
  93. }
  94. int sunrpc_set_pool_mode(const char *val)
  95. {
  96. return __param_set_pool_mode(val, &svc_pool_map);
  97. }
  98. EXPORT_SYMBOL(sunrpc_set_pool_mode);
  99. /**
  100. * sunrpc_get_pool_mode - get the current pool_mode for the host
  101. * @buf: where to write the current pool_mode
  102. * @size: size of @buf
  103. *
  104. * Grab the current pool_mode from the svc_pool_map and write
  105. * the resulting string to @buf. Returns the number of characters
  106. * written to @buf (a'la snprintf()).
  107. */
  108. int
  109. sunrpc_get_pool_mode(char *buf, size_t size)
  110. {
  111. struct svc_pool_map *m = &svc_pool_map;
  112. switch (m->mode)
  113. {
  114. case SVC_POOL_AUTO:
  115. return snprintf(buf, size, "auto");
  116. case SVC_POOL_GLOBAL:
  117. return snprintf(buf, size, "global");
  118. case SVC_POOL_PERCPU:
  119. return snprintf(buf, size, "percpu");
  120. case SVC_POOL_PERNODE:
  121. return snprintf(buf, size, "pernode");
  122. default:
  123. return snprintf(buf, size, "%d", m->mode);
  124. }
  125. }
  126. EXPORT_SYMBOL(sunrpc_get_pool_mode);
  127. static int
  128. param_get_pool_mode(char *buf, const struct kernel_param *kp)
  129. {
  130. char str[16];
  131. int len;
  132. len = sunrpc_get_pool_mode(str, ARRAY_SIZE(str));
  133. /* Ensure we have room for newline and NUL */
  134. len = min_t(int, len, ARRAY_SIZE(str) - 2);
  135. /* tack on the newline */
  136. str[len] = '\n';
  137. str[len + 1] = '\0';
  138. return sysfs_emit(buf, "%s", str);
  139. }
  140. module_param_call(pool_mode, param_set_pool_mode, param_get_pool_mode,
  141. &svc_pool_map, 0644);
  142. /*
  143. * Detect best pool mapping mode heuristically,
  144. * according to the machine's topology.
  145. */
  146. static int
  147. svc_pool_map_choose_mode(void)
  148. {
  149. unsigned int node;
  150. if (nr_online_nodes > 1) {
  151. /*
  152. * Actually have multiple NUMA nodes,
  153. * so split pools on NUMA node boundaries
  154. */
  155. return SVC_POOL_PERNODE;
  156. }
  157. node = first_online_node;
  158. if (nr_cpus_node(node) > 2) {
  159. /*
  160. * Non-trivial SMP, or CONFIG_NUMA on
  161. * non-NUMA hardware, e.g. with a generic
  162. * x86_64 kernel on Xeons. In this case we
  163. * want to divide the pools on cpu boundaries.
  164. */
  165. return SVC_POOL_PERCPU;
  166. }
  167. /* default: one global pool */
  168. return SVC_POOL_GLOBAL;
  169. }
  170. /*
  171. * Allocate the to_pool[] and pool_to[] arrays.
  172. * Returns 0 on success or an errno.
  173. */
  174. static int
  175. svc_pool_map_alloc_arrays(struct svc_pool_map *m, unsigned int maxpools)
  176. {
  177. m->to_pool = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL);
  178. if (!m->to_pool)
  179. goto fail;
  180. m->pool_to = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL);
  181. if (!m->pool_to)
  182. goto fail_free;
  183. return 0;
  184. fail_free:
  185. kfree(m->to_pool);
  186. m->to_pool = NULL;
  187. fail:
  188. return -ENOMEM;
  189. }
  190. /*
  191. * Initialise the pool map for SVC_POOL_PERCPU mode.
  192. * Returns number of pools or <0 on error.
  193. */
  194. static int
  195. svc_pool_map_init_percpu(struct svc_pool_map *m)
  196. {
  197. unsigned int maxpools = nr_cpu_ids;
  198. unsigned int pidx = 0;
  199. unsigned int cpu;
  200. int err;
  201. err = svc_pool_map_alloc_arrays(m, maxpools);
  202. if (err)
  203. return err;
  204. for_each_online_cpu(cpu) {
  205. BUG_ON(pidx >= maxpools);
  206. m->to_pool[cpu] = pidx;
  207. m->pool_to[pidx] = cpu;
  208. pidx++;
  209. }
  210. /* cpus brought online later all get mapped to pool0, sorry */
  211. return pidx;
  212. };
  213. /*
  214. * Initialise the pool map for SVC_POOL_PERNODE mode.
  215. * Returns number of pools or <0 on error.
  216. */
  217. static int
  218. svc_pool_map_init_pernode(struct svc_pool_map *m)
  219. {
  220. unsigned int maxpools = nr_node_ids;
  221. unsigned int pidx = 0;
  222. unsigned int node;
  223. int err;
  224. err = svc_pool_map_alloc_arrays(m, maxpools);
  225. if (err)
  226. return err;
  227. for_each_node_with_cpus(node) {
  228. /* some architectures (e.g. SN2) have cpuless nodes */
  229. BUG_ON(pidx > maxpools);
  230. m->to_pool[node] = pidx;
  231. m->pool_to[pidx] = node;
  232. pidx++;
  233. }
  234. /* nodes brought online later all get mapped to pool0, sorry */
  235. return pidx;
  236. }
  237. /*
  238. * Add a reference to the global map of cpus to pools (and
  239. * vice versa) if pools are in use.
  240. * Initialise the map if we're the first user.
  241. * Returns the number of pools. If this is '1', no reference
  242. * was taken.
  243. */
  244. static unsigned int
  245. svc_pool_map_get(void)
  246. {
  247. struct svc_pool_map *m = &svc_pool_map;
  248. int npools = -1;
  249. mutex_lock(&svc_pool_map_mutex);
  250. if (m->count++) {
  251. mutex_unlock(&svc_pool_map_mutex);
  252. return m->npools;
  253. }
  254. if (m->mode == SVC_POOL_AUTO)
  255. m->mode = svc_pool_map_choose_mode();
  256. switch (m->mode) {
  257. case SVC_POOL_PERCPU:
  258. npools = svc_pool_map_init_percpu(m);
  259. break;
  260. case SVC_POOL_PERNODE:
  261. npools = svc_pool_map_init_pernode(m);
  262. break;
  263. }
  264. if (npools <= 0) {
  265. /* default, or memory allocation failure */
  266. npools = 1;
  267. m->mode = SVC_POOL_GLOBAL;
  268. }
  269. m->npools = npools;
  270. mutex_unlock(&svc_pool_map_mutex);
  271. return npools;
  272. }
  273. /*
  274. * Drop a reference to the global map of cpus to pools.
  275. * When the last reference is dropped, the map data is
  276. * freed; this allows the sysadmin to change the pool.
  277. */
  278. static void
  279. svc_pool_map_put(void)
  280. {
  281. struct svc_pool_map *m = &svc_pool_map;
  282. mutex_lock(&svc_pool_map_mutex);
  283. if (!--m->count) {
  284. kfree(m->to_pool);
  285. m->to_pool = NULL;
  286. kfree(m->pool_to);
  287. m->pool_to = NULL;
  288. m->npools = 0;
  289. }
  290. mutex_unlock(&svc_pool_map_mutex);
  291. }
  292. static int svc_pool_map_get_node(unsigned int pidx)
  293. {
  294. const struct svc_pool_map *m = &svc_pool_map;
  295. if (m->count) {
  296. if (m->mode == SVC_POOL_PERCPU)
  297. return cpu_to_node(m->pool_to[pidx]);
  298. if (m->mode == SVC_POOL_PERNODE)
  299. return m->pool_to[pidx];
  300. }
  301. return numa_mem_id();
  302. }
  303. /*
  304. * Set the given thread's cpus_allowed mask so that it
  305. * will only run on cpus in the given pool.
  306. */
  307. static inline void
  308. svc_pool_map_set_cpumask(struct task_struct *task, unsigned int pidx)
  309. {
  310. struct svc_pool_map *m = &svc_pool_map;
  311. unsigned int node = m->pool_to[pidx];
  312. /*
  313. * The caller checks for sv_nrpools > 1, which
  314. * implies that we've been initialized.
  315. */
  316. WARN_ON_ONCE(m->count == 0);
  317. if (m->count == 0)
  318. return;
  319. switch (m->mode) {
  320. case SVC_POOL_PERCPU:
  321. {
  322. set_cpus_allowed_ptr(task, cpumask_of(node));
  323. break;
  324. }
  325. case SVC_POOL_PERNODE:
  326. {
  327. set_cpus_allowed_ptr(task, cpumask_of_node(node));
  328. break;
  329. }
  330. }
  331. }
  332. /**
  333. * svc_pool_for_cpu - Select pool to run a thread on this cpu
  334. * @serv: An RPC service
  335. *
  336. * Use the active CPU and the svc_pool_map's mode setting to
  337. * select the svc thread pool to use. Once initialized, the
  338. * svc_pool_map does not change.
  339. *
  340. * Return value:
  341. * A pointer to an svc_pool
  342. */
  343. struct svc_pool *svc_pool_for_cpu(struct svc_serv *serv)
  344. {
  345. struct svc_pool_map *m = &svc_pool_map;
  346. int cpu = raw_smp_processor_id();
  347. unsigned int pidx = 0;
  348. if (serv->sv_nrpools <= 1)
  349. return serv->sv_pools;
  350. switch (m->mode) {
  351. case SVC_POOL_PERCPU:
  352. pidx = m->to_pool[cpu];
  353. break;
  354. case SVC_POOL_PERNODE:
  355. pidx = m->to_pool[cpu_to_node(cpu)];
  356. break;
  357. }
  358. return &serv->sv_pools[pidx % serv->sv_nrpools];
  359. }
  360. static int svc_rpcb_setup(struct svc_serv *serv, struct net *net)
  361. {
  362. int err;
  363. err = rpcb_create_local(net);
  364. if (err)
  365. return err;
  366. /* Remove any stale portmap registrations */
  367. svc_unregister(serv, net);
  368. return 0;
  369. }
  370. void svc_rpcb_cleanup(struct svc_serv *serv, struct net *net)
  371. {
  372. svc_unregister(serv, net);
  373. rpcb_put_local(net);
  374. }
  375. static int svc_uses_rpcbind(struct svc_serv *serv)
  376. {
  377. unsigned int p, i;
  378. for (p = 0; p < serv->sv_nprogs; p++) {
  379. struct svc_program *progp = &serv->sv_programs[p];
  380. for (i = 0; i < progp->pg_nvers; i++) {
  381. if (progp->pg_vers[i] == NULL)
  382. continue;
  383. if (!progp->pg_vers[i]->vs_hidden)
  384. return 1;
  385. }
  386. }
  387. return 0;
  388. }
  389. int svc_bind(struct svc_serv *serv, struct net *net)
  390. {
  391. if (!svc_uses_rpcbind(serv))
  392. return 0;
  393. return svc_rpcb_setup(serv, net);
  394. }
  395. EXPORT_SYMBOL_GPL(svc_bind);
  396. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  397. static void
  398. __svc_init_bc(struct svc_serv *serv)
  399. {
  400. lwq_init(&serv->sv_cb_list);
  401. }
  402. #else
  403. static void
  404. __svc_init_bc(struct svc_serv *serv)
  405. {
  406. }
  407. #endif
  408. /*
  409. * Create an RPC service
  410. */
  411. static struct svc_serv *
  412. __svc_create(struct svc_program *prog, int nprogs, struct svc_stat *stats,
  413. unsigned int bufsize, int npools, int (*threadfn)(void *data))
  414. {
  415. struct svc_serv *serv;
  416. unsigned int vers;
  417. unsigned int xdrsize;
  418. unsigned int i;
  419. if (!(serv = kzalloc_obj(*serv)))
  420. return NULL;
  421. serv->sv_name = prog->pg_name;
  422. serv->sv_programs = prog;
  423. serv->sv_nprogs = nprogs;
  424. serv->sv_stats = stats;
  425. if (bufsize > RPCSVC_MAXPAYLOAD)
  426. bufsize = RPCSVC_MAXPAYLOAD;
  427. serv->sv_max_payload = bufsize? bufsize : 4096;
  428. serv->sv_max_mesg = roundup(serv->sv_max_payload + PAGE_SIZE, PAGE_SIZE);
  429. serv->sv_threadfn = threadfn;
  430. xdrsize = 0;
  431. for (i = 0; i < nprogs; i++) {
  432. struct svc_program *progp = &prog[i];
  433. progp->pg_lovers = progp->pg_nvers-1;
  434. for (vers = 0; vers < progp->pg_nvers ; vers++)
  435. if (progp->pg_vers[vers]) {
  436. progp->pg_hivers = vers;
  437. if (progp->pg_lovers > vers)
  438. progp->pg_lovers = vers;
  439. if (progp->pg_vers[vers]->vs_xdrsize > xdrsize)
  440. xdrsize = progp->pg_vers[vers]->vs_xdrsize;
  441. }
  442. }
  443. serv->sv_xdrsize = xdrsize;
  444. INIT_LIST_HEAD(&serv->sv_tempsocks);
  445. INIT_LIST_HEAD(&serv->sv_permsocks);
  446. timer_setup(&serv->sv_temptimer, NULL, 0);
  447. spin_lock_init(&serv->sv_lock);
  448. __svc_init_bc(serv);
  449. serv->sv_nrpools = npools;
  450. serv->sv_pools =
  451. kzalloc_objs(struct svc_pool, serv->sv_nrpools);
  452. if (!serv->sv_pools) {
  453. kfree(serv);
  454. return NULL;
  455. }
  456. for (i = 0; i < serv->sv_nrpools; i++) {
  457. struct svc_pool *pool = &serv->sv_pools[i];
  458. dprintk("svc: initialising pool %u for %s\n",
  459. i, serv->sv_name);
  460. pool->sp_id = i;
  461. lwq_init(&pool->sp_xprts);
  462. INIT_LIST_HEAD(&pool->sp_all_threads);
  463. init_llist_head(&pool->sp_idle_threads);
  464. percpu_counter_init(&pool->sp_messages_arrived, 0, GFP_KERNEL);
  465. percpu_counter_init(&pool->sp_sockets_queued, 0, GFP_KERNEL);
  466. percpu_counter_init(&pool->sp_threads_woken, 0, GFP_KERNEL);
  467. }
  468. return serv;
  469. }
  470. /**
  471. * svc_create - Create an RPC service
  472. * @prog: the RPC program the new service will handle
  473. * @bufsize: maximum message size for @prog
  474. * @threadfn: a function to service RPC requests for @prog
  475. *
  476. * Returns an instantiated struct svc_serv object or NULL.
  477. */
  478. struct svc_serv *svc_create(struct svc_program *prog, unsigned int bufsize,
  479. int (*threadfn)(void *data))
  480. {
  481. return __svc_create(prog, 1, NULL, bufsize, 1, threadfn);
  482. }
  483. EXPORT_SYMBOL_GPL(svc_create);
  484. /**
  485. * svc_create_pooled - Create an RPC service with pooled threads
  486. * @prog: Array of RPC programs the new service will handle
  487. * @nprogs: Number of programs in the array
  488. * @stats: the stats struct if desired
  489. * @bufsize: maximum message size for @prog
  490. * @threadfn: a function to service RPC requests for @prog
  491. *
  492. * Returns an instantiated struct svc_serv object or NULL.
  493. */
  494. struct svc_serv *svc_create_pooled(struct svc_program *prog,
  495. unsigned int nprogs,
  496. struct svc_stat *stats,
  497. unsigned int bufsize,
  498. int (*threadfn)(void *data))
  499. {
  500. struct svc_serv *serv;
  501. unsigned int npools = svc_pool_map_get();
  502. serv = __svc_create(prog, nprogs, stats, bufsize, npools, threadfn);
  503. if (!serv)
  504. goto out_err;
  505. serv->sv_is_pooled = true;
  506. return serv;
  507. out_err:
  508. svc_pool_map_put();
  509. return NULL;
  510. }
  511. EXPORT_SYMBOL_GPL(svc_create_pooled);
  512. /*
  513. * Destroy an RPC service. Should be called with appropriate locking to
  514. * protect sv_permsocks and sv_tempsocks.
  515. */
  516. void
  517. svc_destroy(struct svc_serv **servp)
  518. {
  519. struct svc_serv *serv = *servp;
  520. unsigned int i;
  521. *servp = NULL;
  522. dprintk("svc: svc_destroy(%s)\n", serv->sv_programs->pg_name);
  523. timer_shutdown_sync(&serv->sv_temptimer);
  524. /*
  525. * Remaining transports at this point are not expected.
  526. */
  527. WARN_ONCE(!list_empty(&serv->sv_permsocks),
  528. "SVC: permsocks remain for %s\n", serv->sv_programs->pg_name);
  529. WARN_ONCE(!list_empty(&serv->sv_tempsocks),
  530. "SVC: tempsocks remain for %s\n", serv->sv_programs->pg_name);
  531. cache_clean_deferred(serv);
  532. if (serv->sv_is_pooled)
  533. svc_pool_map_put();
  534. for (i = 0; i < serv->sv_nrpools; i++) {
  535. struct svc_pool *pool = &serv->sv_pools[i];
  536. percpu_counter_destroy(&pool->sp_messages_arrived);
  537. percpu_counter_destroy(&pool->sp_sockets_queued);
  538. percpu_counter_destroy(&pool->sp_threads_woken);
  539. }
  540. kfree(serv->sv_pools);
  541. kfree(serv);
  542. }
  543. EXPORT_SYMBOL_GPL(svc_destroy);
  544. static bool
  545. svc_init_buffer(struct svc_rqst *rqstp, const struct svc_serv *serv, int node)
  546. {
  547. rqstp->rq_maxpages = svc_serv_maxpages(serv);
  548. /* rq_pages' last entry is NULL for historical reasons. */
  549. rqstp->rq_pages = kcalloc_node(rqstp->rq_maxpages + 1,
  550. sizeof(struct page *),
  551. GFP_KERNEL, node);
  552. if (!rqstp->rq_pages)
  553. return false;
  554. return true;
  555. }
  556. /*
  557. * Release an RPC server buffer
  558. */
  559. static void
  560. svc_release_buffer(struct svc_rqst *rqstp)
  561. {
  562. unsigned long i;
  563. for (i = 0; i < rqstp->rq_maxpages; i++)
  564. if (rqstp->rq_pages[i])
  565. put_page(rqstp->rq_pages[i]);
  566. kfree(rqstp->rq_pages);
  567. }
  568. static void
  569. svc_rqst_free(struct svc_rqst *rqstp)
  570. {
  571. folio_batch_release(&rqstp->rq_fbatch);
  572. kfree(rqstp->rq_bvec);
  573. svc_release_buffer(rqstp);
  574. if (rqstp->rq_scratch_folio)
  575. folio_put(rqstp->rq_scratch_folio);
  576. kfree(rqstp->rq_resp);
  577. kfree(rqstp->rq_argp);
  578. kfree(rqstp->rq_auth_data);
  579. kfree_rcu(rqstp, rq_rcu_head);
  580. }
  581. static struct svc_rqst *
  582. svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)
  583. {
  584. struct svc_rqst *rqstp;
  585. rqstp = kzalloc_node(sizeof(*rqstp), GFP_KERNEL, node);
  586. if (!rqstp)
  587. return rqstp;
  588. folio_batch_init(&rqstp->rq_fbatch);
  589. rqstp->rq_server = serv;
  590. rqstp->rq_pool = pool;
  591. rqstp->rq_scratch_folio = __folio_alloc_node(GFP_KERNEL, 0, node);
  592. if (!rqstp->rq_scratch_folio)
  593. goto out_enomem;
  594. rqstp->rq_argp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
  595. if (!rqstp->rq_argp)
  596. goto out_enomem;
  597. rqstp->rq_resp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
  598. if (!rqstp->rq_resp)
  599. goto out_enomem;
  600. if (!svc_init_buffer(rqstp, serv, node))
  601. goto out_enomem;
  602. rqstp->rq_bvec = kcalloc_node(rqstp->rq_maxpages,
  603. sizeof(struct bio_vec),
  604. GFP_KERNEL, node);
  605. if (!rqstp->rq_bvec)
  606. goto out_enomem;
  607. rqstp->rq_err = -EAGAIN; /* No error yet */
  608. serv->sv_nrthreads += 1;
  609. pool->sp_nrthreads += 1;
  610. /* Protected by whatever lock the service uses when calling
  611. * svc_set_num_threads()
  612. */
  613. list_add_rcu(&rqstp->rq_all, &pool->sp_all_threads);
  614. return rqstp;
  615. out_enomem:
  616. svc_rqst_free(rqstp);
  617. return NULL;
  618. }
  619. /**
  620. * svc_pool_wake_idle_thread - Awaken an idle thread in @pool
  621. * @pool: service thread pool
  622. *
  623. * Can be called from soft IRQ or process context. Finding an idle
  624. * service thread and marking it BUSY is atomic with respect to
  625. * other calls to svc_pool_wake_idle_thread().
  626. *
  627. */
  628. void svc_pool_wake_idle_thread(struct svc_pool *pool)
  629. {
  630. struct svc_rqst *rqstp;
  631. struct llist_node *ln;
  632. rcu_read_lock();
  633. ln = READ_ONCE(pool->sp_idle_threads.first);
  634. if (ln) {
  635. rqstp = llist_entry(ln, struct svc_rqst, rq_idle);
  636. WRITE_ONCE(rqstp->rq_qtime, ktime_get());
  637. if (!task_is_running(rqstp->rq_task)) {
  638. wake_up_process(rqstp->rq_task);
  639. trace_svc_pool_thread_wake(pool, rqstp->rq_task->pid);
  640. percpu_counter_inc(&pool->sp_threads_woken);
  641. } else {
  642. trace_svc_pool_thread_running(pool, rqstp->rq_task->pid);
  643. }
  644. rcu_read_unlock();
  645. return;
  646. }
  647. rcu_read_unlock();
  648. trace_svc_pool_thread_noidle(pool, 0);
  649. }
  650. EXPORT_SYMBOL_GPL(svc_pool_wake_idle_thread);
  651. /**
  652. * svc_new_thread - spawn a new thread in the given pool
  653. * @serv: the serv to which the pool belongs
  654. * @pool: pool in which thread should be spawned
  655. *
  656. * Create a new thread inside @pool, which is a part of @serv.
  657. * Caller must hold the service mutex.
  658. *
  659. * Returns 0 on success, or -errno on failure.
  660. */
  661. int svc_new_thread(struct svc_serv *serv, struct svc_pool *pool)
  662. {
  663. struct svc_rqst *rqstp;
  664. struct task_struct *task;
  665. int node;
  666. int err = 0;
  667. node = svc_pool_map_get_node(pool->sp_id);
  668. rqstp = svc_prepare_thread(serv, pool, node);
  669. if (!rqstp)
  670. return -ENOMEM;
  671. task = kthread_create_on_node(serv->sv_threadfn, rqstp,
  672. node, "%s", serv->sv_name);
  673. if (IS_ERR(task)) {
  674. err = PTR_ERR(task);
  675. goto out;
  676. }
  677. rqstp->rq_task = task;
  678. if (serv->sv_nrpools > 1)
  679. svc_pool_map_set_cpumask(task, pool->sp_id);
  680. svc_sock_update_bufs(serv);
  681. wake_up_process(task);
  682. /* Wait for the thread to signal initialization status */
  683. wait_var_event(&rqstp->rq_err, rqstp->rq_err != -EAGAIN);
  684. err = rqstp->rq_err;
  685. out:
  686. if (err)
  687. svc_exit_thread(rqstp);
  688. return err;
  689. }
  690. EXPORT_SYMBOL_GPL(svc_new_thread);
  691. static int
  692. svc_start_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
  693. {
  694. int err = 0;
  695. while (!err && nrservs--)
  696. err = svc_new_thread(serv, pool);
  697. return err;
  698. }
  699. static int
  700. svc_stop_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
  701. {
  702. do {
  703. set_bit(SP_VICTIM_REMAINS, &pool->sp_flags);
  704. set_bit(SP_NEED_VICTIM, &pool->sp_flags);
  705. svc_pool_wake_idle_thread(pool);
  706. wait_on_bit(&pool->sp_flags, SP_VICTIM_REMAINS, TASK_IDLE);
  707. nrservs++;
  708. } while (nrservs < 0);
  709. return 0;
  710. }
  711. /**
  712. * svc_set_pool_threads - adjust number of threads per pool
  713. * @serv: RPC service to adjust
  714. * @pool: Specific pool from which to choose threads
  715. * @min_threads: min number of threads to run in @pool
  716. * @max_threads: max number of threads in @pool (0 means kill all threads)
  717. *
  718. * Create or destroy threads in @pool to bring it into an acceptable range
  719. * between @min_threads and @max_threads.
  720. *
  721. * If @min_threads is 0 or larger than @max_threads, then it is ignored and
  722. * the pool will be set to run a static @max_threads number of threads.
  723. *
  724. * Caller must ensure mutual exclusion between this and server startup or
  725. * shutdown.
  726. *
  727. * Returns zero on success or a negative errno if an error occurred while
  728. * starting a thread.
  729. */
  730. int
  731. svc_set_pool_threads(struct svc_serv *serv, struct svc_pool *pool,
  732. unsigned int min_threads, unsigned int max_threads)
  733. {
  734. int delta;
  735. if (!pool)
  736. return -EINVAL;
  737. /* clamp min threads to the max */
  738. if (min_threads > max_threads)
  739. min_threads = max_threads;
  740. pool->sp_nrthrmin = min_threads;
  741. pool->sp_nrthrmax = max_threads;
  742. /*
  743. * When min_threads is set, then only change the number of
  744. * threads to bring it within an acceptable range.
  745. */
  746. if (min_threads) {
  747. if (pool->sp_nrthreads > max_threads)
  748. delta = max_threads;
  749. else if (pool->sp_nrthreads < min_threads)
  750. delta = min_threads;
  751. else
  752. return 0;
  753. } else {
  754. delta = max_threads;
  755. }
  756. delta -= pool->sp_nrthreads;
  757. if (delta > 0)
  758. return svc_start_kthreads(serv, pool, delta);
  759. if (delta < 0)
  760. return svc_stop_kthreads(serv, pool, delta);
  761. return 0;
  762. }
  763. EXPORT_SYMBOL_GPL(svc_set_pool_threads);
  764. /**
  765. * svc_set_num_threads - adjust number of threads in serv
  766. * @serv: RPC service to adjust
  767. * @min_threads: min number of threads to run per pool
  768. * @nrservs: New number of threads for @serv (0 means kill all threads)
  769. *
  770. * Create or destroy threads in @serv to bring it to @nrservs. If there
  771. * are multiple pools then the new threads or victims will be distributed
  772. * evenly among them.
  773. *
  774. * Caller must ensure mutual exclusion between this and server startup or
  775. * shutdown.
  776. *
  777. * Returns zero on success or a negative errno if an error occurred while
  778. * starting a thread. On failure, some pools may have already been
  779. * adjusted; the caller is responsible for recovery.
  780. */
  781. int
  782. svc_set_num_threads(struct svc_serv *serv, unsigned int min_threads,
  783. unsigned int nrservs)
  784. {
  785. unsigned int base = nrservs / serv->sv_nrpools;
  786. unsigned int remain = nrservs % serv->sv_nrpools;
  787. int i, err = 0;
  788. for (i = 0; i < serv->sv_nrpools; ++i) {
  789. struct svc_pool *pool = &serv->sv_pools[i];
  790. int threads = base;
  791. if (remain) {
  792. ++threads;
  793. --remain;
  794. }
  795. err = svc_set_pool_threads(serv, pool, min_threads, threads);
  796. if (err)
  797. break;
  798. }
  799. return err;
  800. }
  801. EXPORT_SYMBOL_GPL(svc_set_num_threads);
  802. /**
  803. * svc_rqst_replace_page - Replace one page in rq_pages[]
  804. * @rqstp: svc_rqst with pages to replace
  805. * @page: replacement page
  806. *
  807. * When replacing a page in rq_pages, batch the release of the
  808. * replaced pages to avoid hammering the page allocator.
  809. *
  810. * Return values:
  811. * %true: page replaced
  812. * %false: array bounds checking failed
  813. */
  814. bool svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)
  815. {
  816. struct page **begin = rqstp->rq_pages;
  817. struct page **end = &rqstp->rq_pages[rqstp->rq_maxpages];
  818. if (unlikely(rqstp->rq_next_page < begin || rqstp->rq_next_page > end)) {
  819. trace_svc_replace_page_err(rqstp);
  820. return false;
  821. }
  822. if (*rqstp->rq_next_page) {
  823. if (!folio_batch_add(&rqstp->rq_fbatch,
  824. page_folio(*rqstp->rq_next_page)))
  825. __folio_batch_release(&rqstp->rq_fbatch);
  826. }
  827. get_page(page);
  828. *(rqstp->rq_next_page++) = page;
  829. return true;
  830. }
  831. EXPORT_SYMBOL_GPL(svc_rqst_replace_page);
  832. /**
  833. * svc_rqst_release_pages - Release Reply buffer pages
  834. * @rqstp: RPC transaction context
  835. *
  836. * Release response pages that might still be in flight after
  837. * svc_send, and any spliced filesystem-owned pages.
  838. */
  839. void svc_rqst_release_pages(struct svc_rqst *rqstp)
  840. {
  841. int i, count = rqstp->rq_next_page - rqstp->rq_respages;
  842. if (count) {
  843. release_pages(rqstp->rq_respages, count);
  844. for (i = 0; i < count; i++)
  845. rqstp->rq_respages[i] = NULL;
  846. }
  847. }
  848. /**
  849. * svc_exit_thread - finalise the termination of a sunrpc server thread
  850. * @rqstp: the svc_rqst which represents the thread.
  851. *
  852. * When a thread started with svc_new_thread() exits it must call
  853. * svc_exit_thread() as its last act. This must be done with the
  854. * service mutex held. Normally this is held by a DIFFERENT thread, the
  855. * one that is calling svc_set_num_threads() and which will wait for
  856. * SP_VICTIM_REMAINS to be cleared before dropping the mutex. If the
  857. * thread exits for any reason other than svc_thread_should_stop()
  858. * returning %true (which indicated that svc_set_num_threads() is
  859. * waiting for it to exit), then it must take the service mutex itself,
  860. * which can only safely be done using mutex_try_lock().
  861. */
  862. void
  863. svc_exit_thread(struct svc_rqst *rqstp)
  864. {
  865. struct svc_serv *serv = rqstp->rq_server;
  866. struct svc_pool *pool = rqstp->rq_pool;
  867. list_del_rcu(&rqstp->rq_all);
  868. pool->sp_nrthreads -= 1;
  869. serv->sv_nrthreads -= 1;
  870. svc_sock_update_bufs(serv);
  871. svc_rqst_free(rqstp);
  872. clear_and_wake_up_bit(SP_VICTIM_REMAINS, &pool->sp_flags);
  873. }
  874. EXPORT_SYMBOL_GPL(svc_exit_thread);
  875. /*
  876. * Register an "inet" protocol family netid with the local
  877. * rpcbind daemon via an rpcbind v4 SET request.
  878. *
  879. * No netconfig infrastructure is available in the kernel, so
  880. * we map IP_ protocol numbers to netids by hand.
  881. *
  882. * Returns zero on success; a negative errno value is returned
  883. * if any error occurs.
  884. */
  885. static int __svc_rpcb_register4(struct net *net, const u32 program,
  886. const u32 version,
  887. const unsigned short protocol,
  888. const unsigned short port)
  889. {
  890. const struct sockaddr_in sin = {
  891. .sin_family = AF_INET,
  892. .sin_addr.s_addr = htonl(INADDR_ANY),
  893. .sin_port = htons(port),
  894. };
  895. const char *netid;
  896. int error;
  897. switch (protocol) {
  898. case IPPROTO_UDP:
  899. netid = RPCBIND_NETID_UDP;
  900. break;
  901. case IPPROTO_TCP:
  902. netid = RPCBIND_NETID_TCP;
  903. break;
  904. default:
  905. return -ENOPROTOOPT;
  906. }
  907. error = rpcb_v4_register(net, program, version,
  908. (const struct sockaddr *)&sin, netid);
  909. /*
  910. * User space didn't support rpcbind v4, so retry this
  911. * registration request with the legacy rpcbind v2 protocol.
  912. */
  913. if (error == -EPROTONOSUPPORT)
  914. error = rpcb_register(net, program, version, protocol, port);
  915. return error;
  916. }
  917. #if IS_ENABLED(CONFIG_IPV6)
  918. /*
  919. * Register an "inet6" protocol family netid with the local
  920. * rpcbind daemon via an rpcbind v4 SET request.
  921. *
  922. * No netconfig infrastructure is available in the kernel, so
  923. * we map IP_ protocol numbers to netids by hand.
  924. *
  925. * Returns zero on success; a negative errno value is returned
  926. * if any error occurs.
  927. */
  928. static int __svc_rpcb_register6(struct net *net, const u32 program,
  929. const u32 version,
  930. const unsigned short protocol,
  931. const unsigned short port)
  932. {
  933. const struct sockaddr_in6 sin6 = {
  934. .sin6_family = AF_INET6,
  935. .sin6_addr = IN6ADDR_ANY_INIT,
  936. .sin6_port = htons(port),
  937. };
  938. const char *netid;
  939. int error;
  940. switch (protocol) {
  941. case IPPROTO_UDP:
  942. netid = RPCBIND_NETID_UDP6;
  943. break;
  944. case IPPROTO_TCP:
  945. netid = RPCBIND_NETID_TCP6;
  946. break;
  947. default:
  948. return -ENOPROTOOPT;
  949. }
  950. error = rpcb_v4_register(net, program, version,
  951. (const struct sockaddr *)&sin6, netid);
  952. /*
  953. * User space didn't support rpcbind version 4, so we won't
  954. * use a PF_INET6 listener.
  955. */
  956. if (error == -EPROTONOSUPPORT)
  957. error = -EAFNOSUPPORT;
  958. return error;
  959. }
  960. #endif /* IS_ENABLED(CONFIG_IPV6) */
  961. /*
  962. * Register a kernel RPC service via rpcbind version 4.
  963. *
  964. * Returns zero on success; a negative errno value is returned
  965. * if any error occurs.
  966. */
  967. static int __svc_register(struct net *net, const char *progname,
  968. const u32 program, const u32 version,
  969. const int family,
  970. const unsigned short protocol,
  971. const unsigned short port)
  972. {
  973. int error = -EAFNOSUPPORT;
  974. switch (family) {
  975. case PF_INET:
  976. error = __svc_rpcb_register4(net, program, version,
  977. protocol, port);
  978. break;
  979. #if IS_ENABLED(CONFIG_IPV6)
  980. case PF_INET6:
  981. error = __svc_rpcb_register6(net, program, version,
  982. protocol, port);
  983. #endif
  984. }
  985. trace_svc_register(progname, version, family, protocol, port, error);
  986. return error;
  987. }
  988. static
  989. int svc_rpcbind_set_version(struct net *net,
  990. const struct svc_program *progp,
  991. u32 version, int family,
  992. unsigned short proto,
  993. unsigned short port)
  994. {
  995. return __svc_register(net, progp->pg_name, progp->pg_prog,
  996. version, family, proto, port);
  997. }
  998. int svc_generic_rpcbind_set(struct net *net,
  999. const struct svc_program *progp,
  1000. u32 version, int family,
  1001. unsigned short proto,
  1002. unsigned short port)
  1003. {
  1004. const struct svc_version *vers = progp->pg_vers[version];
  1005. int error;
  1006. if (vers == NULL)
  1007. return 0;
  1008. if (vers->vs_hidden) {
  1009. trace_svc_noregister(progp->pg_name, version, proto,
  1010. port, family, 0);
  1011. return 0;
  1012. }
  1013. /*
  1014. * Don't register a UDP port if we need congestion
  1015. * control.
  1016. */
  1017. if (vers->vs_need_cong_ctrl && proto == IPPROTO_UDP)
  1018. return 0;
  1019. error = svc_rpcbind_set_version(net, progp, version,
  1020. family, proto, port);
  1021. return (vers->vs_rpcb_optnl) ? 0 : error;
  1022. }
  1023. EXPORT_SYMBOL_GPL(svc_generic_rpcbind_set);
  1024. /**
  1025. * svc_register - register an RPC service with the local portmapper
  1026. * @serv: svc_serv struct for the service to register
  1027. * @net: net namespace for the service to register
  1028. * @family: protocol family of service's listener socket
  1029. * @proto: transport protocol number to advertise
  1030. * @port: port to advertise
  1031. *
  1032. * Service is registered for any address in the passed-in protocol family
  1033. */
  1034. int svc_register(const struct svc_serv *serv, struct net *net,
  1035. const int family, const unsigned short proto,
  1036. const unsigned short port)
  1037. {
  1038. unsigned int p, i;
  1039. int error = 0;
  1040. WARN_ON_ONCE(proto == 0 && port == 0);
  1041. if (proto == 0 && port == 0)
  1042. return -EINVAL;
  1043. for (p = 0; p < serv->sv_nprogs; p++) {
  1044. struct svc_program *progp = &serv->sv_programs[p];
  1045. for (i = 0; i < progp->pg_nvers; i++) {
  1046. error = progp->pg_rpcbind_set(net, progp, i,
  1047. family, proto, port);
  1048. if (error < 0) {
  1049. printk(KERN_WARNING "svc: failed to register "
  1050. "%sv%u RPC service (errno %d).\n",
  1051. progp->pg_name, i, -error);
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. return error;
  1057. }
  1058. /*
  1059. * If user space is running rpcbind, it should take the v4 UNSET
  1060. * and clear everything for this [program, version]. If user space
  1061. * is running portmap, it will reject the v4 UNSET, but won't have
  1062. * any "inet6" entries anyway. So a PMAP_UNSET should be sufficient
  1063. * in this case to clear all existing entries for [program, version].
  1064. */
  1065. static void __svc_unregister(struct net *net, const u32 program, const u32 version,
  1066. const char *progname)
  1067. {
  1068. int error;
  1069. error = rpcb_v4_register(net, program, version, NULL, "");
  1070. /*
  1071. * User space didn't support rpcbind v4, so retry this
  1072. * request with the legacy rpcbind v2 protocol.
  1073. */
  1074. if (error == -EPROTONOSUPPORT)
  1075. error = rpcb_register(net, program, version, 0, 0);
  1076. trace_svc_unregister(progname, version, error);
  1077. }
  1078. /*
  1079. * All netids, bind addresses and ports registered for [program, version]
  1080. * are removed from the local rpcbind database (if the service is not
  1081. * hidden) to make way for a new instance of the service.
  1082. *
  1083. * The result of unregistration is reported via dprintk for those who want
  1084. * verification of the result, but is otherwise not important.
  1085. */
  1086. static void svc_unregister(const struct svc_serv *serv, struct net *net)
  1087. {
  1088. struct sighand_struct *sighand;
  1089. unsigned long flags;
  1090. unsigned int p, i;
  1091. clear_thread_flag(TIF_SIGPENDING);
  1092. for (p = 0; p < serv->sv_nprogs; p++) {
  1093. struct svc_program *progp = &serv->sv_programs[p];
  1094. for (i = 0; i < progp->pg_nvers; i++) {
  1095. if (progp->pg_vers[i] == NULL)
  1096. continue;
  1097. if (progp->pg_vers[i]->vs_hidden)
  1098. continue;
  1099. __svc_unregister(net, progp->pg_prog, i, progp->pg_name);
  1100. }
  1101. }
  1102. rcu_read_lock();
  1103. sighand = rcu_dereference(current->sighand);
  1104. spin_lock_irqsave(&sighand->siglock, flags);
  1105. recalc_sigpending();
  1106. spin_unlock_irqrestore(&sighand->siglock, flags);
  1107. rcu_read_unlock();
  1108. }
  1109. /*
  1110. * dprintk the given error with the address of the client that caused it.
  1111. */
  1112. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  1113. static __printf(2, 3)
  1114. void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...)
  1115. {
  1116. struct va_format vaf;
  1117. va_list args;
  1118. char buf[RPC_MAX_ADDRBUFLEN];
  1119. va_start(args, fmt);
  1120. vaf.fmt = fmt;
  1121. vaf.va = &args;
  1122. dprintk("svc: %s: %pV", svc_print_addr(rqstp, buf, sizeof(buf)), &vaf);
  1123. va_end(args);
  1124. }
  1125. #else
  1126. static __printf(2,3) void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) {}
  1127. #endif
  1128. __be32
  1129. svc_generic_init_request(struct svc_rqst *rqstp,
  1130. const struct svc_program *progp,
  1131. struct svc_process_info *ret)
  1132. {
  1133. const struct svc_version *versp = NULL; /* compiler food */
  1134. const struct svc_procedure *procp = NULL;
  1135. if (rqstp->rq_vers >= progp->pg_nvers )
  1136. goto err_bad_vers;
  1137. versp = progp->pg_vers[rqstp->rq_vers];
  1138. if (!versp)
  1139. goto err_bad_vers;
  1140. /*
  1141. * Some protocol versions (namely NFSv4) require some form of
  1142. * congestion control. (See RFC 7530 section 3.1 paragraph 2)
  1143. * In other words, UDP is not allowed. We mark those when setting
  1144. * up the svc_xprt, and verify that here.
  1145. *
  1146. * The spec is not very clear about what error should be returned
  1147. * when someone tries to access a server that is listening on UDP
  1148. * for lower versions. RPC_PROG_MISMATCH seems to be the closest
  1149. * fit.
  1150. */
  1151. if (versp->vs_need_cong_ctrl && rqstp->rq_xprt &&
  1152. !test_bit(XPT_CONG_CTRL, &rqstp->rq_xprt->xpt_flags))
  1153. goto err_bad_vers;
  1154. if (rqstp->rq_proc >= versp->vs_nproc)
  1155. goto err_bad_proc;
  1156. rqstp->rq_procinfo = procp = &versp->vs_proc[rqstp->rq_proc];
  1157. /* Initialize storage for argp and resp */
  1158. memset(rqstp->rq_argp, 0, procp->pc_argzero);
  1159. memset(rqstp->rq_resp, 0, procp->pc_ressize);
  1160. /* Bump per-procedure stats counter */
  1161. this_cpu_inc(versp->vs_count[rqstp->rq_proc]);
  1162. ret->dispatch = versp->vs_dispatch;
  1163. return rpc_success;
  1164. err_bad_vers:
  1165. ret->mismatch.lovers = progp->pg_lovers;
  1166. ret->mismatch.hivers = progp->pg_hivers;
  1167. return rpc_prog_mismatch;
  1168. err_bad_proc:
  1169. return rpc_proc_unavail;
  1170. }
  1171. EXPORT_SYMBOL_GPL(svc_generic_init_request);
  1172. /*
  1173. * Common routine for processing the RPC request.
  1174. */
  1175. static int
  1176. svc_process_common(struct svc_rqst *rqstp)
  1177. {
  1178. struct xdr_stream *xdr = &rqstp->rq_res_stream;
  1179. struct svc_program *progp = NULL;
  1180. const struct svc_procedure *procp = NULL;
  1181. struct svc_serv *serv = rqstp->rq_server;
  1182. struct svc_process_info process;
  1183. enum svc_auth_status auth_res;
  1184. unsigned int aoffset;
  1185. int pr, rc;
  1186. __be32 *p;
  1187. /* Reset the accept_stat for the RPC */
  1188. rqstp->rq_accept_statp = NULL;
  1189. /* Will be turned off only when NFSv4 Sessions are used */
  1190. set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  1191. clear_bit(RQ_DROPME, &rqstp->rq_flags);
  1192. /* Construct the first words of the reply: */
  1193. svcxdr_init_encode(rqstp);
  1194. xdr_stream_encode_be32(xdr, rqstp->rq_xid);
  1195. xdr_stream_encode_be32(xdr, rpc_reply);
  1196. p = xdr_inline_decode(&rqstp->rq_arg_stream, XDR_UNIT * 4);
  1197. if (unlikely(!p))
  1198. goto err_short_len;
  1199. if (*p++ != cpu_to_be32(RPC_VERSION))
  1200. goto err_bad_rpc;
  1201. xdr_stream_encode_be32(xdr, rpc_msg_accepted);
  1202. rqstp->rq_prog = be32_to_cpup(p++);
  1203. rqstp->rq_vers = be32_to_cpup(p++);
  1204. rqstp->rq_proc = be32_to_cpup(p);
  1205. for (pr = 0; pr < serv->sv_nprogs; pr++)
  1206. if (rqstp->rq_prog == serv->sv_programs[pr].pg_prog)
  1207. progp = &serv->sv_programs[pr];
  1208. /*
  1209. * Decode auth data, and add verifier to reply buffer.
  1210. * We do this before anything else in order to get a decent
  1211. * auth verifier.
  1212. */
  1213. auth_res = svc_authenticate(rqstp);
  1214. /* Also give the program a chance to reject this call: */
  1215. if (auth_res == SVC_OK && progp)
  1216. auth_res = progp->pg_authenticate(rqstp);
  1217. trace_svc_authenticate(rqstp, auth_res);
  1218. switch (auth_res) {
  1219. case SVC_OK:
  1220. break;
  1221. case SVC_GARBAGE:
  1222. rqstp->rq_auth_stat = rpc_autherr_badcred;
  1223. goto err_bad_auth;
  1224. case SVC_DENIED:
  1225. goto err_bad_auth;
  1226. case SVC_CLOSE:
  1227. goto close;
  1228. case SVC_DROP:
  1229. goto dropit;
  1230. case SVC_COMPLETE:
  1231. goto sendit;
  1232. default:
  1233. pr_warn_once("Unexpected svc_auth_status (%d)\n", auth_res);
  1234. rqstp->rq_auth_stat = rpc_autherr_failed;
  1235. goto err_bad_auth;
  1236. }
  1237. if (progp == NULL)
  1238. goto err_bad_prog;
  1239. switch (progp->pg_init_request(rqstp, progp, &process)) {
  1240. case rpc_success:
  1241. break;
  1242. case rpc_prog_unavail:
  1243. goto err_bad_prog;
  1244. case rpc_prog_mismatch:
  1245. goto err_bad_vers;
  1246. case rpc_proc_unavail:
  1247. goto err_bad_proc;
  1248. }
  1249. procp = rqstp->rq_procinfo;
  1250. /* Should this check go into the dispatcher? */
  1251. if (!procp || !procp->pc_func)
  1252. goto err_bad_proc;
  1253. /* Syntactic check complete */
  1254. if (serv->sv_stats)
  1255. serv->sv_stats->rpccnt++;
  1256. trace_svc_process(rqstp, progp->pg_name);
  1257. aoffset = xdr_stream_pos(xdr);
  1258. /* un-reserve some of the out-queue now that we have a
  1259. * better idea of reply size
  1260. */
  1261. if (procp->pc_xdrressize)
  1262. svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
  1263. /* Call the function that processes the request. */
  1264. rc = process.dispatch(rqstp);
  1265. xdr_finish_decode(xdr);
  1266. if (!rc)
  1267. goto dropit;
  1268. if (rqstp->rq_auth_stat != rpc_auth_ok)
  1269. goto err_bad_auth;
  1270. if (*rqstp->rq_accept_statp != rpc_success)
  1271. xdr_truncate_encode(xdr, aoffset);
  1272. if (procp->pc_encode == NULL)
  1273. goto dropit;
  1274. sendit:
  1275. if (svc_authorise(rqstp))
  1276. goto close_xprt;
  1277. return 1; /* Caller can now send it */
  1278. dropit:
  1279. svc_authorise(rqstp); /* doesn't hurt to call this twice */
  1280. dprintk("svc: svc_process dropit\n");
  1281. return 0;
  1282. close:
  1283. svc_authorise(rqstp);
  1284. close_xprt:
  1285. if (rqstp->rq_xprt && test_bit(XPT_TEMP, &rqstp->rq_xprt->xpt_flags))
  1286. svc_xprt_close(rqstp->rq_xprt);
  1287. dprintk("svc: svc_process close\n");
  1288. return 0;
  1289. err_short_len:
  1290. svc_printk(rqstp, "short len %u, dropping request\n",
  1291. rqstp->rq_arg.len);
  1292. goto close_xprt;
  1293. err_bad_rpc:
  1294. if (serv->sv_stats)
  1295. serv->sv_stats->rpcbadfmt++;
  1296. xdr_stream_encode_u32(xdr, RPC_MSG_DENIED);
  1297. xdr_stream_encode_u32(xdr, RPC_MISMATCH);
  1298. /* Only RPCv2 supported */
  1299. xdr_stream_encode_u32(xdr, RPC_VERSION);
  1300. xdr_stream_encode_u32(xdr, RPC_VERSION);
  1301. return 1; /* don't wrap */
  1302. err_bad_auth:
  1303. dprintk("svc: authentication failed (%d)\n",
  1304. be32_to_cpu(rqstp->rq_auth_stat));
  1305. if (serv->sv_stats)
  1306. serv->sv_stats->rpcbadauth++;
  1307. /* Restore write pointer to location of reply status: */
  1308. xdr_truncate_encode(xdr, XDR_UNIT * 2);
  1309. xdr_stream_encode_u32(xdr, RPC_MSG_DENIED);
  1310. xdr_stream_encode_u32(xdr, RPC_AUTH_ERROR);
  1311. xdr_stream_encode_be32(xdr, rqstp->rq_auth_stat);
  1312. goto sendit;
  1313. err_bad_prog:
  1314. dprintk("svc: unknown program %d\n", rqstp->rq_prog);
  1315. if (serv->sv_stats)
  1316. serv->sv_stats->rpcbadfmt++;
  1317. *rqstp->rq_accept_statp = rpc_prog_unavail;
  1318. goto sendit;
  1319. err_bad_vers:
  1320. svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
  1321. rqstp->rq_vers, rqstp->rq_prog, progp->pg_name);
  1322. if (serv->sv_stats)
  1323. serv->sv_stats->rpcbadfmt++;
  1324. *rqstp->rq_accept_statp = rpc_prog_mismatch;
  1325. /*
  1326. * svc_authenticate() has already added the verifier and
  1327. * advanced the stream just past rq_accept_statp.
  1328. */
  1329. xdr_stream_encode_u32(xdr, process.mismatch.lovers);
  1330. xdr_stream_encode_u32(xdr, process.mismatch.hivers);
  1331. goto sendit;
  1332. err_bad_proc:
  1333. svc_printk(rqstp, "unknown procedure (%d)\n", rqstp->rq_proc);
  1334. if (serv->sv_stats)
  1335. serv->sv_stats->rpcbadfmt++;
  1336. *rqstp->rq_accept_statp = rpc_proc_unavail;
  1337. goto sendit;
  1338. }
  1339. /*
  1340. * Drop request
  1341. */
  1342. static void svc_drop(struct svc_rqst *rqstp)
  1343. {
  1344. trace_svc_drop(rqstp);
  1345. }
  1346. static void svc_release_rqst(struct svc_rqst *rqstp)
  1347. {
  1348. const struct svc_procedure *procp = rqstp->rq_procinfo;
  1349. if (procp && procp->pc_release)
  1350. procp->pc_release(rqstp);
  1351. }
  1352. /**
  1353. * svc_process - Execute one RPC transaction
  1354. * @rqstp: RPC transaction context
  1355. *
  1356. */
  1357. void svc_process(struct svc_rqst *rqstp)
  1358. {
  1359. struct kvec *resv = &rqstp->rq_res.head[0];
  1360. __be32 *p;
  1361. #if IS_ENABLED(CONFIG_FAIL_SUNRPC)
  1362. if (!fail_sunrpc.ignore_server_disconnect &&
  1363. should_fail(&fail_sunrpc.attr, 1))
  1364. svc_xprt_deferred_close(rqstp->rq_xprt);
  1365. #endif
  1366. /*
  1367. * Setup response xdr_buf.
  1368. * Initially it has just one page
  1369. */
  1370. rqstp->rq_next_page = &rqstp->rq_respages[1];
  1371. resv->iov_base = page_address(rqstp->rq_respages[0]);
  1372. resv->iov_len = 0;
  1373. rqstp->rq_res.pages = rqstp->rq_next_page;
  1374. rqstp->rq_res.len = 0;
  1375. rqstp->rq_res.page_base = 0;
  1376. rqstp->rq_res.page_len = 0;
  1377. rqstp->rq_res.buflen = PAGE_SIZE;
  1378. rqstp->rq_res.tail[0].iov_base = NULL;
  1379. rqstp->rq_res.tail[0].iov_len = 0;
  1380. svcxdr_init_decode(rqstp);
  1381. p = xdr_inline_decode(&rqstp->rq_arg_stream, XDR_UNIT * 2);
  1382. if (unlikely(!p))
  1383. goto out_drop;
  1384. rqstp->rq_xid = *p++;
  1385. if (unlikely(*p != rpc_call))
  1386. goto out_baddir;
  1387. if (!svc_process_common(rqstp)) {
  1388. svc_release_rqst(rqstp);
  1389. goto out_drop;
  1390. }
  1391. svc_send(rqstp);
  1392. svc_release_rqst(rqstp);
  1393. return;
  1394. out_baddir:
  1395. svc_printk(rqstp, "bad direction 0x%08x, dropping request\n",
  1396. be32_to_cpu(*p));
  1397. if (rqstp->rq_server->sv_stats)
  1398. rqstp->rq_server->sv_stats->rpcbadfmt++;
  1399. out_drop:
  1400. svc_drop(rqstp);
  1401. }
  1402. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1403. /**
  1404. * svc_process_bc - process a reverse-direction RPC request
  1405. * @req: RPC request to be used for client-side processing
  1406. * @rqstp: server-side execution context
  1407. *
  1408. */
  1409. void svc_process_bc(struct rpc_rqst *req, struct svc_rqst *rqstp)
  1410. {
  1411. struct rpc_timeout timeout = {
  1412. .to_increment = 0,
  1413. };
  1414. struct rpc_task *task;
  1415. int proc_error;
  1416. /* Build the svc_rqst used by the common processing routine */
  1417. rqstp->rq_xid = req->rq_xid;
  1418. rqstp->rq_prot = req->rq_xprt->prot;
  1419. rqstp->rq_bc_net = req->rq_xprt->xprt_net;
  1420. rqstp->rq_addrlen = sizeof(req->rq_xprt->addr);
  1421. memcpy(&rqstp->rq_addr, &req->rq_xprt->addr, rqstp->rq_addrlen);
  1422. memcpy(&rqstp->rq_arg, &req->rq_rcv_buf, sizeof(rqstp->rq_arg));
  1423. memcpy(&rqstp->rq_res, &req->rq_snd_buf, sizeof(rqstp->rq_res));
  1424. /* Adjust the argument buffer length */
  1425. rqstp->rq_arg.len = req->rq_private_buf.len;
  1426. if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
  1427. rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
  1428. rqstp->rq_arg.page_len = 0;
  1429. } else if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len +
  1430. rqstp->rq_arg.page_len)
  1431. rqstp->rq_arg.page_len = rqstp->rq_arg.len -
  1432. rqstp->rq_arg.head[0].iov_len;
  1433. else
  1434. rqstp->rq_arg.len = rqstp->rq_arg.head[0].iov_len +
  1435. rqstp->rq_arg.page_len;
  1436. /* Reset the response buffer */
  1437. rqstp->rq_res.head[0].iov_len = 0;
  1438. /*
  1439. * Skip the XID and calldir fields because they've already
  1440. * been processed by the caller.
  1441. */
  1442. svcxdr_init_decode(rqstp);
  1443. if (!xdr_inline_decode(&rqstp->rq_arg_stream, XDR_UNIT * 2))
  1444. return;
  1445. /* Parse and execute the bc call */
  1446. proc_error = svc_process_common(rqstp);
  1447. atomic_dec(&req->rq_xprt->bc_slot_count);
  1448. if (!proc_error) {
  1449. /* Processing error: drop the request */
  1450. xprt_free_bc_request(req);
  1451. svc_release_rqst(rqstp);
  1452. return;
  1453. }
  1454. /* Finally, send the reply synchronously */
  1455. if (rqstp->bc_to_initval > 0) {
  1456. timeout.to_initval = rqstp->bc_to_initval;
  1457. timeout.to_retries = rqstp->bc_to_retries;
  1458. } else {
  1459. timeout.to_initval = req->rq_xprt->timeout->to_initval;
  1460. timeout.to_retries = req->rq_xprt->timeout->to_retries;
  1461. }
  1462. timeout.to_maxval = timeout.to_initval;
  1463. memcpy(&req->rq_snd_buf, &rqstp->rq_res, sizeof(req->rq_snd_buf));
  1464. task = rpc_run_bc_task(req, &timeout);
  1465. svc_release_rqst(rqstp);
  1466. if (IS_ERR(task))
  1467. return;
  1468. WARN_ON_ONCE(atomic_read(&task->tk_count) != 1);
  1469. rpc_put_task(task);
  1470. }
  1471. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1472. /**
  1473. * svc_max_payload - Return transport-specific limit on the RPC payload
  1474. * @rqstp: RPC transaction context
  1475. *
  1476. * Returns the maximum number of payload bytes the current transport
  1477. * allows.
  1478. */
  1479. u32 svc_max_payload(const struct svc_rqst *rqstp)
  1480. {
  1481. u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload;
  1482. if (rqstp->rq_server->sv_max_payload < max)
  1483. max = rqstp->rq_server->sv_max_payload;
  1484. return max;
  1485. }
  1486. EXPORT_SYMBOL_GPL(svc_max_payload);
  1487. /**
  1488. * svc_proc_name - Return RPC procedure name in string form
  1489. * @rqstp: svc_rqst to operate on
  1490. *
  1491. * Return value:
  1492. * Pointer to a NUL-terminated string
  1493. */
  1494. const char *svc_proc_name(const struct svc_rqst *rqstp)
  1495. {
  1496. if (rqstp && rqstp->rq_procinfo)
  1497. return rqstp->rq_procinfo->pc_name;
  1498. return "unknown";
  1499. }
  1500. /**
  1501. * svc_encode_result_payload - mark a range of bytes as a result payload
  1502. * @rqstp: svc_rqst to operate on
  1503. * @offset: payload's byte offset in rqstp->rq_res
  1504. * @length: size of payload, in bytes
  1505. *
  1506. * Returns zero on success, or a negative errno if a permanent
  1507. * error occurred.
  1508. */
  1509. int svc_encode_result_payload(struct svc_rqst *rqstp, unsigned int offset,
  1510. unsigned int length)
  1511. {
  1512. return rqstp->rq_xprt->xpt_ops->xpo_result_payload(rqstp, offset,
  1513. length);
  1514. }
  1515. EXPORT_SYMBOL_GPL(svc_encode_result_payload);
  1516. /**
  1517. * svc_fill_symlink_pathname - Construct pathname argument for VFS symlink call
  1518. * @rqstp: svc_rqst to operate on
  1519. * @first: buffer containing first section of pathname
  1520. * @p: buffer containing remaining section of pathname
  1521. * @total: total length of the pathname argument
  1522. *
  1523. * The VFS symlink API demands a NUL-terminated pathname in mapped memory.
  1524. * Returns pointer to a NUL-terminated string, or an ERR_PTR. Caller must free
  1525. * the returned string.
  1526. */
  1527. char *svc_fill_symlink_pathname(struct svc_rqst *rqstp, struct kvec *first,
  1528. void *p, size_t total)
  1529. {
  1530. size_t len, remaining;
  1531. char *result, *dst;
  1532. result = kmalloc(total + 1, GFP_KERNEL);
  1533. if (!result)
  1534. return ERR_PTR(-ESERVERFAULT);
  1535. dst = result;
  1536. remaining = total;
  1537. len = min_t(size_t, total, first->iov_len);
  1538. if (len) {
  1539. memcpy(dst, first->iov_base, len);
  1540. dst += len;
  1541. remaining -= len;
  1542. }
  1543. if (remaining) {
  1544. len = min_t(size_t, remaining, PAGE_SIZE);
  1545. memcpy(dst, p, len);
  1546. dst += len;
  1547. }
  1548. *dst = '\0';
  1549. /* Sanity check: Linux doesn't allow the pathname argument to
  1550. * contain a NUL byte.
  1551. */
  1552. if (strlen(result) != total) {
  1553. kfree(result);
  1554. return ERR_PTR(-EINVAL);
  1555. }
  1556. return result;
  1557. }
  1558. EXPORT_SYMBOL_GPL(svc_fill_symlink_pathname);