svc.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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_NO_NODE;
  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. EXPORT_SYMBOL_GPL(svc_rpcb_cleanup);
  376. static int svc_uses_rpcbind(struct svc_serv *serv)
  377. {
  378. unsigned int p, i;
  379. for (p = 0; p < serv->sv_nprogs; p++) {
  380. struct svc_program *progp = &serv->sv_programs[p];
  381. for (i = 0; i < progp->pg_nvers; i++) {
  382. if (progp->pg_vers[i] == NULL)
  383. continue;
  384. if (!progp->pg_vers[i]->vs_hidden)
  385. return 1;
  386. }
  387. }
  388. return 0;
  389. }
  390. int svc_bind(struct svc_serv *serv, struct net *net)
  391. {
  392. if (!svc_uses_rpcbind(serv))
  393. return 0;
  394. return svc_rpcb_setup(serv, net);
  395. }
  396. EXPORT_SYMBOL_GPL(svc_bind);
  397. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  398. static void
  399. __svc_init_bc(struct svc_serv *serv)
  400. {
  401. lwq_init(&serv->sv_cb_list);
  402. }
  403. #else
  404. static void
  405. __svc_init_bc(struct svc_serv *serv)
  406. {
  407. }
  408. #endif
  409. /*
  410. * Create an RPC service
  411. */
  412. static struct svc_serv *
  413. __svc_create(struct svc_program *prog, int nprogs, struct svc_stat *stats,
  414. unsigned int bufsize, int npools, int (*threadfn)(void *data))
  415. {
  416. struct svc_serv *serv;
  417. unsigned int vers;
  418. unsigned int xdrsize;
  419. unsigned int i;
  420. if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL)))
  421. return NULL;
  422. serv->sv_name = prog->pg_name;
  423. serv->sv_programs = prog;
  424. serv->sv_nprogs = nprogs;
  425. serv->sv_stats = stats;
  426. if (bufsize > RPCSVC_MAXPAYLOAD)
  427. bufsize = RPCSVC_MAXPAYLOAD;
  428. serv->sv_max_payload = bufsize? bufsize : 4096;
  429. serv->sv_max_mesg = roundup(serv->sv_max_payload + PAGE_SIZE, PAGE_SIZE);
  430. serv->sv_threadfn = threadfn;
  431. xdrsize = 0;
  432. for (i = 0; i < nprogs; i++) {
  433. struct svc_program *progp = &prog[i];
  434. progp->pg_lovers = progp->pg_nvers-1;
  435. for (vers = 0; vers < progp->pg_nvers ; vers++)
  436. if (progp->pg_vers[vers]) {
  437. progp->pg_hivers = vers;
  438. if (progp->pg_lovers > vers)
  439. progp->pg_lovers = vers;
  440. if (progp->pg_vers[vers]->vs_xdrsize > xdrsize)
  441. xdrsize = progp->pg_vers[vers]->vs_xdrsize;
  442. }
  443. }
  444. serv->sv_xdrsize = xdrsize;
  445. INIT_LIST_HEAD(&serv->sv_tempsocks);
  446. INIT_LIST_HEAD(&serv->sv_permsocks);
  447. timer_setup(&serv->sv_temptimer, NULL, 0);
  448. spin_lock_init(&serv->sv_lock);
  449. __svc_init_bc(serv);
  450. serv->sv_nrpools = npools;
  451. serv->sv_pools =
  452. kcalloc(serv->sv_nrpools, sizeof(struct svc_pool),
  453. GFP_KERNEL);
  454. if (!serv->sv_pools) {
  455. kfree(serv);
  456. return NULL;
  457. }
  458. for (i = 0; i < serv->sv_nrpools; i++) {
  459. struct svc_pool *pool = &serv->sv_pools[i];
  460. dprintk("svc: initialising pool %u for %s\n",
  461. i, serv->sv_name);
  462. pool->sp_id = i;
  463. lwq_init(&pool->sp_xprts);
  464. INIT_LIST_HEAD(&pool->sp_all_threads);
  465. init_llist_head(&pool->sp_idle_threads);
  466. percpu_counter_init(&pool->sp_messages_arrived, 0, GFP_KERNEL);
  467. percpu_counter_init(&pool->sp_sockets_queued, 0, GFP_KERNEL);
  468. percpu_counter_init(&pool->sp_threads_woken, 0, GFP_KERNEL);
  469. }
  470. return serv;
  471. }
  472. /**
  473. * svc_create - Create an RPC service
  474. * @prog: the RPC program the new service will handle
  475. * @bufsize: maximum message size for @prog
  476. * @threadfn: a function to service RPC requests for @prog
  477. *
  478. * Returns an instantiated struct svc_serv object or NULL.
  479. */
  480. struct svc_serv *svc_create(struct svc_program *prog, unsigned int bufsize,
  481. int (*threadfn)(void *data))
  482. {
  483. return __svc_create(prog, 1, NULL, bufsize, 1, threadfn);
  484. }
  485. EXPORT_SYMBOL_GPL(svc_create);
  486. /**
  487. * svc_create_pooled - Create an RPC service with pooled threads
  488. * @prog: Array of RPC programs the new service will handle
  489. * @nprogs: Number of programs in the array
  490. * @stats: the stats struct if desired
  491. * @bufsize: maximum message size for @prog
  492. * @threadfn: a function to service RPC requests for @prog
  493. *
  494. * Returns an instantiated struct svc_serv object or NULL.
  495. */
  496. struct svc_serv *svc_create_pooled(struct svc_program *prog,
  497. unsigned int nprogs,
  498. struct svc_stat *stats,
  499. unsigned int bufsize,
  500. int (*threadfn)(void *data))
  501. {
  502. struct svc_serv *serv;
  503. unsigned int npools = svc_pool_map_get();
  504. serv = __svc_create(prog, nprogs, stats, bufsize, npools, threadfn);
  505. if (!serv)
  506. goto out_err;
  507. serv->sv_is_pooled = true;
  508. return serv;
  509. out_err:
  510. svc_pool_map_put();
  511. return NULL;
  512. }
  513. EXPORT_SYMBOL_GPL(svc_create_pooled);
  514. /*
  515. * Destroy an RPC service. Should be called with appropriate locking to
  516. * protect sv_permsocks and sv_tempsocks.
  517. */
  518. void
  519. svc_destroy(struct svc_serv **servp)
  520. {
  521. struct svc_serv *serv = *servp;
  522. unsigned int i;
  523. *servp = NULL;
  524. dprintk("svc: svc_destroy(%s)\n", serv->sv_programs->pg_name);
  525. timer_shutdown_sync(&serv->sv_temptimer);
  526. /*
  527. * Remaining transports at this point are not expected.
  528. */
  529. WARN_ONCE(!list_empty(&serv->sv_permsocks),
  530. "SVC: permsocks remain for %s\n", serv->sv_programs->pg_name);
  531. WARN_ONCE(!list_empty(&serv->sv_tempsocks),
  532. "SVC: tempsocks remain for %s\n", serv->sv_programs->pg_name);
  533. cache_clean_deferred(serv);
  534. if (serv->sv_is_pooled)
  535. svc_pool_map_put();
  536. for (i = 0; i < serv->sv_nrpools; i++) {
  537. struct svc_pool *pool = &serv->sv_pools[i];
  538. percpu_counter_destroy(&pool->sp_messages_arrived);
  539. percpu_counter_destroy(&pool->sp_sockets_queued);
  540. percpu_counter_destroy(&pool->sp_threads_woken);
  541. }
  542. kfree(serv->sv_pools);
  543. kfree(serv);
  544. }
  545. EXPORT_SYMBOL_GPL(svc_destroy);
  546. static bool
  547. svc_init_buffer(struct svc_rqst *rqstp, unsigned int size, int node)
  548. {
  549. unsigned long pages, ret;
  550. /* bc_xprt uses fore channel allocated buffers */
  551. if (svc_is_backchannel(rqstp))
  552. return true;
  553. pages = size / PAGE_SIZE + 1; /* extra page as we hold both request and reply.
  554. * We assume one is at most one page
  555. */
  556. WARN_ON_ONCE(pages > RPCSVC_MAXPAGES);
  557. if (pages > RPCSVC_MAXPAGES)
  558. pages = RPCSVC_MAXPAGES;
  559. ret = alloc_pages_bulk_array_node(GFP_KERNEL, node, pages,
  560. rqstp->rq_pages);
  561. return ret == pages;
  562. }
  563. /*
  564. * Release an RPC server buffer
  565. */
  566. static void
  567. svc_release_buffer(struct svc_rqst *rqstp)
  568. {
  569. unsigned int i;
  570. for (i = 0; i < ARRAY_SIZE(rqstp->rq_pages); i++)
  571. if (rqstp->rq_pages[i])
  572. put_page(rqstp->rq_pages[i]);
  573. }
  574. static void
  575. svc_rqst_free(struct svc_rqst *rqstp)
  576. {
  577. folio_batch_release(&rqstp->rq_fbatch);
  578. svc_release_buffer(rqstp);
  579. if (rqstp->rq_scratch_page)
  580. put_page(rqstp->rq_scratch_page);
  581. kfree(rqstp->rq_resp);
  582. kfree(rqstp->rq_argp);
  583. kfree(rqstp->rq_auth_data);
  584. kfree_rcu(rqstp, rq_rcu_head);
  585. }
  586. static struct svc_rqst *
  587. svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)
  588. {
  589. struct svc_rqst *rqstp;
  590. rqstp = kzalloc_node(sizeof(*rqstp), GFP_KERNEL, node);
  591. if (!rqstp)
  592. return rqstp;
  593. folio_batch_init(&rqstp->rq_fbatch);
  594. rqstp->rq_server = serv;
  595. rqstp->rq_pool = pool;
  596. rqstp->rq_scratch_page = alloc_pages_node(node, GFP_KERNEL, 0);
  597. if (!rqstp->rq_scratch_page)
  598. goto out_enomem;
  599. rqstp->rq_argp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
  600. if (!rqstp->rq_argp)
  601. goto out_enomem;
  602. rqstp->rq_resp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
  603. if (!rqstp->rq_resp)
  604. goto out_enomem;
  605. if (!svc_init_buffer(rqstp, serv->sv_max_mesg, node))
  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_wake_up(rqstp->rq_task->pid);
  640. percpu_counter_inc(&pool->sp_threads_woken);
  641. }
  642. rcu_read_unlock();
  643. return;
  644. }
  645. rcu_read_unlock();
  646. }
  647. EXPORT_SYMBOL_GPL(svc_pool_wake_idle_thread);
  648. static struct svc_pool *
  649. svc_pool_next(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
  650. {
  651. return pool ? pool : &serv->sv_pools[(*state)++ % serv->sv_nrpools];
  652. }
  653. static struct svc_pool *
  654. svc_pool_victim(struct svc_serv *serv, struct svc_pool *target_pool,
  655. unsigned int *state)
  656. {
  657. struct svc_pool *pool;
  658. unsigned int i;
  659. pool = target_pool;
  660. if (!pool) {
  661. for (i = 0; i < serv->sv_nrpools; i++) {
  662. pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
  663. if (pool->sp_nrthreads)
  664. break;
  665. }
  666. }
  667. if (pool && pool->sp_nrthreads) {
  668. set_bit(SP_VICTIM_REMAINS, &pool->sp_flags);
  669. set_bit(SP_NEED_VICTIM, &pool->sp_flags);
  670. return pool;
  671. }
  672. return NULL;
  673. }
  674. static int
  675. svc_start_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
  676. {
  677. struct svc_rqst *rqstp;
  678. struct task_struct *task;
  679. struct svc_pool *chosen_pool;
  680. unsigned int state = serv->sv_nrthreads-1;
  681. int node;
  682. int err;
  683. do {
  684. nrservs--;
  685. chosen_pool = svc_pool_next(serv, pool, &state);
  686. node = svc_pool_map_get_node(chosen_pool->sp_id);
  687. rqstp = svc_prepare_thread(serv, chosen_pool, node);
  688. if (!rqstp)
  689. return -ENOMEM;
  690. task = kthread_create_on_node(serv->sv_threadfn, rqstp,
  691. node, "%s", serv->sv_name);
  692. if (IS_ERR(task)) {
  693. svc_exit_thread(rqstp);
  694. return PTR_ERR(task);
  695. }
  696. rqstp->rq_task = task;
  697. if (serv->sv_nrpools > 1)
  698. svc_pool_map_set_cpumask(task, chosen_pool->sp_id);
  699. svc_sock_update_bufs(serv);
  700. wake_up_process(task);
  701. wait_var_event(&rqstp->rq_err, rqstp->rq_err != -EAGAIN);
  702. err = rqstp->rq_err;
  703. if (err) {
  704. svc_exit_thread(rqstp);
  705. return err;
  706. }
  707. } while (nrservs > 0);
  708. return 0;
  709. }
  710. static int
  711. svc_stop_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
  712. {
  713. unsigned int state = serv->sv_nrthreads-1;
  714. struct svc_pool *victim;
  715. do {
  716. victim = svc_pool_victim(serv, pool, &state);
  717. if (!victim)
  718. break;
  719. svc_pool_wake_idle_thread(victim);
  720. wait_on_bit(&victim->sp_flags, SP_VICTIM_REMAINS,
  721. TASK_IDLE);
  722. nrservs++;
  723. } while (nrservs < 0);
  724. return 0;
  725. }
  726. /**
  727. * svc_set_num_threads - adjust number of threads per RPC service
  728. * @serv: RPC service to adjust
  729. * @pool: Specific pool from which to choose threads, or NULL
  730. * @nrservs: New number of threads for @serv (0 or less means kill all threads)
  731. *
  732. * Create or destroy threads to make the number of threads for @serv the
  733. * given number. If @pool is non-NULL, change only threads in that pool;
  734. * otherwise, round-robin between all pools for @serv. @serv's
  735. * sv_nrthreads is adjusted for each thread created or destroyed.
  736. *
  737. * Caller must ensure mutual exclusion between this and server startup or
  738. * shutdown.
  739. *
  740. * Returns zero on success or a negative errno if an error occurred while
  741. * starting a thread.
  742. */
  743. int
  744. svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
  745. {
  746. if (!pool)
  747. nrservs -= serv->sv_nrthreads;
  748. else
  749. nrservs -= pool->sp_nrthreads;
  750. if (nrservs > 0)
  751. return svc_start_kthreads(serv, pool, nrservs);
  752. if (nrservs < 0)
  753. return svc_stop_kthreads(serv, pool, nrservs);
  754. return 0;
  755. }
  756. EXPORT_SYMBOL_GPL(svc_set_num_threads);
  757. /**
  758. * svc_rqst_replace_page - Replace one page in rq_pages[]
  759. * @rqstp: svc_rqst with pages to replace
  760. * @page: replacement page
  761. *
  762. * When replacing a page in rq_pages, batch the release of the
  763. * replaced pages to avoid hammering the page allocator.
  764. *
  765. * Return values:
  766. * %true: page replaced
  767. * %false: array bounds checking failed
  768. */
  769. bool svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)
  770. {
  771. struct page **begin = rqstp->rq_pages;
  772. struct page **end = &rqstp->rq_pages[RPCSVC_MAXPAGES];
  773. if (unlikely(rqstp->rq_next_page < begin || rqstp->rq_next_page > end)) {
  774. trace_svc_replace_page_err(rqstp);
  775. return false;
  776. }
  777. if (*rqstp->rq_next_page) {
  778. if (!folio_batch_add(&rqstp->rq_fbatch,
  779. page_folio(*rqstp->rq_next_page)))
  780. __folio_batch_release(&rqstp->rq_fbatch);
  781. }
  782. get_page(page);
  783. *(rqstp->rq_next_page++) = page;
  784. return true;
  785. }
  786. EXPORT_SYMBOL_GPL(svc_rqst_replace_page);
  787. /**
  788. * svc_rqst_release_pages - Release Reply buffer pages
  789. * @rqstp: RPC transaction context
  790. *
  791. * Release response pages that might still be in flight after
  792. * svc_send, and any spliced filesystem-owned pages.
  793. */
  794. void svc_rqst_release_pages(struct svc_rqst *rqstp)
  795. {
  796. int i, count = rqstp->rq_next_page - rqstp->rq_respages;
  797. if (count) {
  798. release_pages(rqstp->rq_respages, count);
  799. for (i = 0; i < count; i++)
  800. rqstp->rq_respages[i] = NULL;
  801. }
  802. }
  803. /**
  804. * svc_exit_thread - finalise the termination of a sunrpc server thread
  805. * @rqstp: the svc_rqst which represents the thread.
  806. *
  807. * When a thread started with svc_new_thread() exits it must call
  808. * svc_exit_thread() as its last act. This must be done with the
  809. * service mutex held. Normally this is held by a DIFFERENT thread, the
  810. * one that is calling svc_set_num_threads() and which will wait for
  811. * SP_VICTIM_REMAINS to be cleared before dropping the mutex. If the
  812. * thread exits for any reason other than svc_thread_should_stop()
  813. * returning %true (which indicated that svc_set_num_threads() is
  814. * waiting for it to exit), then it must take the service mutex itself,
  815. * which can only safely be done using mutex_try_lock().
  816. */
  817. void
  818. svc_exit_thread(struct svc_rqst *rqstp)
  819. {
  820. struct svc_serv *serv = rqstp->rq_server;
  821. struct svc_pool *pool = rqstp->rq_pool;
  822. list_del_rcu(&rqstp->rq_all);
  823. pool->sp_nrthreads -= 1;
  824. serv->sv_nrthreads -= 1;
  825. svc_sock_update_bufs(serv);
  826. svc_rqst_free(rqstp);
  827. clear_and_wake_up_bit(SP_VICTIM_REMAINS, &pool->sp_flags);
  828. }
  829. EXPORT_SYMBOL_GPL(svc_exit_thread);
  830. /*
  831. * Register an "inet" protocol family netid with the local
  832. * rpcbind daemon via an rpcbind v4 SET request.
  833. *
  834. * No netconfig infrastructure is available in the kernel, so
  835. * we map IP_ protocol numbers to netids by hand.
  836. *
  837. * Returns zero on success; a negative errno value is returned
  838. * if any error occurs.
  839. */
  840. static int __svc_rpcb_register4(struct net *net, const u32 program,
  841. const u32 version,
  842. const unsigned short protocol,
  843. const unsigned short port)
  844. {
  845. const struct sockaddr_in sin = {
  846. .sin_family = AF_INET,
  847. .sin_addr.s_addr = htonl(INADDR_ANY),
  848. .sin_port = htons(port),
  849. };
  850. const char *netid;
  851. int error;
  852. switch (protocol) {
  853. case IPPROTO_UDP:
  854. netid = RPCBIND_NETID_UDP;
  855. break;
  856. case IPPROTO_TCP:
  857. netid = RPCBIND_NETID_TCP;
  858. break;
  859. default:
  860. return -ENOPROTOOPT;
  861. }
  862. error = rpcb_v4_register(net, program, version,
  863. (const struct sockaddr *)&sin, netid);
  864. /*
  865. * User space didn't support rpcbind v4, so retry this
  866. * registration request with the legacy rpcbind v2 protocol.
  867. */
  868. if (error == -EPROTONOSUPPORT)
  869. error = rpcb_register(net, program, version, protocol, port);
  870. return error;
  871. }
  872. #if IS_ENABLED(CONFIG_IPV6)
  873. /*
  874. * Register an "inet6" protocol family netid with the local
  875. * rpcbind daemon via an rpcbind v4 SET request.
  876. *
  877. * No netconfig infrastructure is available in the kernel, so
  878. * we map IP_ protocol numbers to netids by hand.
  879. *
  880. * Returns zero on success; a negative errno value is returned
  881. * if any error occurs.
  882. */
  883. static int __svc_rpcb_register6(struct net *net, const u32 program,
  884. const u32 version,
  885. const unsigned short protocol,
  886. const unsigned short port)
  887. {
  888. const struct sockaddr_in6 sin6 = {
  889. .sin6_family = AF_INET6,
  890. .sin6_addr = IN6ADDR_ANY_INIT,
  891. .sin6_port = htons(port),
  892. };
  893. const char *netid;
  894. int error;
  895. switch (protocol) {
  896. case IPPROTO_UDP:
  897. netid = RPCBIND_NETID_UDP6;
  898. break;
  899. case IPPROTO_TCP:
  900. netid = RPCBIND_NETID_TCP6;
  901. break;
  902. default:
  903. return -ENOPROTOOPT;
  904. }
  905. error = rpcb_v4_register(net, program, version,
  906. (const struct sockaddr *)&sin6, netid);
  907. /*
  908. * User space didn't support rpcbind version 4, so we won't
  909. * use a PF_INET6 listener.
  910. */
  911. if (error == -EPROTONOSUPPORT)
  912. error = -EAFNOSUPPORT;
  913. return error;
  914. }
  915. #endif /* IS_ENABLED(CONFIG_IPV6) */
  916. /*
  917. * Register a kernel RPC service via rpcbind version 4.
  918. *
  919. * Returns zero on success; a negative errno value is returned
  920. * if any error occurs.
  921. */
  922. static int __svc_register(struct net *net, const char *progname,
  923. const u32 program, const u32 version,
  924. const int family,
  925. const unsigned short protocol,
  926. const unsigned short port)
  927. {
  928. int error = -EAFNOSUPPORT;
  929. switch (family) {
  930. case PF_INET:
  931. error = __svc_rpcb_register4(net, program, version,
  932. protocol, port);
  933. break;
  934. #if IS_ENABLED(CONFIG_IPV6)
  935. case PF_INET6:
  936. error = __svc_rpcb_register6(net, program, version,
  937. protocol, port);
  938. #endif
  939. }
  940. trace_svc_register(progname, version, family, protocol, port, error);
  941. return error;
  942. }
  943. static
  944. int svc_rpcbind_set_version(struct net *net,
  945. const struct svc_program *progp,
  946. u32 version, int family,
  947. unsigned short proto,
  948. unsigned short port)
  949. {
  950. return __svc_register(net, progp->pg_name, progp->pg_prog,
  951. version, family, proto, port);
  952. }
  953. int svc_generic_rpcbind_set(struct net *net,
  954. const struct svc_program *progp,
  955. u32 version, int family,
  956. unsigned short proto,
  957. unsigned short port)
  958. {
  959. const struct svc_version *vers = progp->pg_vers[version];
  960. int error;
  961. if (vers == NULL)
  962. return 0;
  963. if (vers->vs_hidden) {
  964. trace_svc_noregister(progp->pg_name, version, proto,
  965. port, family, 0);
  966. return 0;
  967. }
  968. /*
  969. * Don't register a UDP port if we need congestion
  970. * control.
  971. */
  972. if (vers->vs_need_cong_ctrl && proto == IPPROTO_UDP)
  973. return 0;
  974. error = svc_rpcbind_set_version(net, progp, version,
  975. family, proto, port);
  976. return (vers->vs_rpcb_optnl) ? 0 : error;
  977. }
  978. EXPORT_SYMBOL_GPL(svc_generic_rpcbind_set);
  979. /**
  980. * svc_register - register an RPC service with the local portmapper
  981. * @serv: svc_serv struct for the service to register
  982. * @net: net namespace for the service to register
  983. * @family: protocol family of service's listener socket
  984. * @proto: transport protocol number to advertise
  985. * @port: port to advertise
  986. *
  987. * Service is registered for any address in the passed-in protocol family
  988. */
  989. int svc_register(const struct svc_serv *serv, struct net *net,
  990. const int family, const unsigned short proto,
  991. const unsigned short port)
  992. {
  993. unsigned int p, i;
  994. int error = 0;
  995. WARN_ON_ONCE(proto == 0 && port == 0);
  996. if (proto == 0 && port == 0)
  997. return -EINVAL;
  998. for (p = 0; p < serv->sv_nprogs; p++) {
  999. struct svc_program *progp = &serv->sv_programs[p];
  1000. for (i = 0; i < progp->pg_nvers; i++) {
  1001. error = progp->pg_rpcbind_set(net, progp, i,
  1002. family, proto, port);
  1003. if (error < 0) {
  1004. printk(KERN_WARNING "svc: failed to register "
  1005. "%sv%u RPC service (errno %d).\n",
  1006. progp->pg_name, i, -error);
  1007. break;
  1008. }
  1009. }
  1010. }
  1011. return error;
  1012. }
  1013. /*
  1014. * If user space is running rpcbind, it should take the v4 UNSET
  1015. * and clear everything for this [program, version]. If user space
  1016. * is running portmap, it will reject the v4 UNSET, but won't have
  1017. * any "inet6" entries anyway. So a PMAP_UNSET should be sufficient
  1018. * in this case to clear all existing entries for [program, version].
  1019. */
  1020. static void __svc_unregister(struct net *net, const u32 program, const u32 version,
  1021. const char *progname)
  1022. {
  1023. int error;
  1024. error = rpcb_v4_register(net, program, version, NULL, "");
  1025. /*
  1026. * User space didn't support rpcbind v4, so retry this
  1027. * request with the legacy rpcbind v2 protocol.
  1028. */
  1029. if (error == -EPROTONOSUPPORT)
  1030. error = rpcb_register(net, program, version, 0, 0);
  1031. trace_svc_unregister(progname, version, error);
  1032. }
  1033. /*
  1034. * All netids, bind addresses and ports registered for [program, version]
  1035. * are removed from the local rpcbind database (if the service is not
  1036. * hidden) to make way for a new instance of the service.
  1037. *
  1038. * The result of unregistration is reported via dprintk for those who want
  1039. * verification of the result, but is otherwise not important.
  1040. */
  1041. static void svc_unregister(const struct svc_serv *serv, struct net *net)
  1042. {
  1043. struct sighand_struct *sighand;
  1044. unsigned long flags;
  1045. unsigned int p, i;
  1046. clear_thread_flag(TIF_SIGPENDING);
  1047. for (p = 0; p < serv->sv_nprogs; p++) {
  1048. struct svc_program *progp = &serv->sv_programs[p];
  1049. for (i = 0; i < progp->pg_nvers; i++) {
  1050. if (progp->pg_vers[i] == NULL)
  1051. continue;
  1052. if (progp->pg_vers[i]->vs_hidden)
  1053. continue;
  1054. __svc_unregister(net, progp->pg_prog, i, progp->pg_name);
  1055. }
  1056. }
  1057. rcu_read_lock();
  1058. sighand = rcu_dereference(current->sighand);
  1059. spin_lock_irqsave(&sighand->siglock, flags);
  1060. recalc_sigpending();
  1061. spin_unlock_irqrestore(&sighand->siglock, flags);
  1062. rcu_read_unlock();
  1063. }
  1064. /*
  1065. * dprintk the given error with the address of the client that caused it.
  1066. */
  1067. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  1068. static __printf(2, 3)
  1069. void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...)
  1070. {
  1071. struct va_format vaf;
  1072. va_list args;
  1073. char buf[RPC_MAX_ADDRBUFLEN];
  1074. va_start(args, fmt);
  1075. vaf.fmt = fmt;
  1076. vaf.va = &args;
  1077. dprintk("svc: %s: %pV", svc_print_addr(rqstp, buf, sizeof(buf)), &vaf);
  1078. va_end(args);
  1079. }
  1080. #else
  1081. static __printf(2,3) void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) {}
  1082. #endif
  1083. __be32
  1084. svc_generic_init_request(struct svc_rqst *rqstp,
  1085. const struct svc_program *progp,
  1086. struct svc_process_info *ret)
  1087. {
  1088. const struct svc_version *versp = NULL; /* compiler food */
  1089. const struct svc_procedure *procp = NULL;
  1090. if (rqstp->rq_vers >= progp->pg_nvers )
  1091. goto err_bad_vers;
  1092. versp = progp->pg_vers[rqstp->rq_vers];
  1093. if (!versp)
  1094. goto err_bad_vers;
  1095. /*
  1096. * Some protocol versions (namely NFSv4) require some form of
  1097. * congestion control. (See RFC 7530 section 3.1 paragraph 2)
  1098. * In other words, UDP is not allowed. We mark those when setting
  1099. * up the svc_xprt, and verify that here.
  1100. *
  1101. * The spec is not very clear about what error should be returned
  1102. * when someone tries to access a server that is listening on UDP
  1103. * for lower versions. RPC_PROG_MISMATCH seems to be the closest
  1104. * fit.
  1105. */
  1106. if (versp->vs_need_cong_ctrl && rqstp->rq_xprt &&
  1107. !test_bit(XPT_CONG_CTRL, &rqstp->rq_xprt->xpt_flags))
  1108. goto err_bad_vers;
  1109. if (rqstp->rq_proc >= versp->vs_nproc)
  1110. goto err_bad_proc;
  1111. rqstp->rq_procinfo = procp = &versp->vs_proc[rqstp->rq_proc];
  1112. /* Initialize storage for argp and resp */
  1113. memset(rqstp->rq_argp, 0, procp->pc_argzero);
  1114. memset(rqstp->rq_resp, 0, procp->pc_ressize);
  1115. /* Bump per-procedure stats counter */
  1116. this_cpu_inc(versp->vs_count[rqstp->rq_proc]);
  1117. ret->dispatch = versp->vs_dispatch;
  1118. return rpc_success;
  1119. err_bad_vers:
  1120. ret->mismatch.lovers = progp->pg_lovers;
  1121. ret->mismatch.hivers = progp->pg_hivers;
  1122. return rpc_prog_mismatch;
  1123. err_bad_proc:
  1124. return rpc_proc_unavail;
  1125. }
  1126. EXPORT_SYMBOL_GPL(svc_generic_init_request);
  1127. /*
  1128. * Common routine for processing the RPC request.
  1129. */
  1130. static int
  1131. svc_process_common(struct svc_rqst *rqstp)
  1132. {
  1133. struct xdr_stream *xdr = &rqstp->rq_res_stream;
  1134. struct svc_program *progp = NULL;
  1135. const struct svc_procedure *procp = NULL;
  1136. struct svc_serv *serv = rqstp->rq_server;
  1137. struct svc_process_info process;
  1138. enum svc_auth_status auth_res;
  1139. unsigned int aoffset;
  1140. int pr, rc;
  1141. __be32 *p;
  1142. /* Will be turned off only when NFSv4 Sessions are used */
  1143. set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  1144. clear_bit(RQ_DROPME, &rqstp->rq_flags);
  1145. /* Construct the first words of the reply: */
  1146. svcxdr_init_encode(rqstp);
  1147. xdr_stream_encode_be32(xdr, rqstp->rq_xid);
  1148. xdr_stream_encode_be32(xdr, rpc_reply);
  1149. p = xdr_inline_decode(&rqstp->rq_arg_stream, XDR_UNIT * 4);
  1150. if (unlikely(!p))
  1151. goto err_short_len;
  1152. if (*p++ != cpu_to_be32(RPC_VERSION))
  1153. goto err_bad_rpc;
  1154. xdr_stream_encode_be32(xdr, rpc_msg_accepted);
  1155. rqstp->rq_prog = be32_to_cpup(p++);
  1156. rqstp->rq_vers = be32_to_cpup(p++);
  1157. rqstp->rq_proc = be32_to_cpup(p);
  1158. for (pr = 0; pr < serv->sv_nprogs; pr++)
  1159. if (rqstp->rq_prog == serv->sv_programs[pr].pg_prog)
  1160. progp = &serv->sv_programs[pr];
  1161. /*
  1162. * Decode auth data, and add verifier to reply buffer.
  1163. * We do this before anything else in order to get a decent
  1164. * auth verifier.
  1165. */
  1166. auth_res = svc_authenticate(rqstp);
  1167. /* Also give the program a chance to reject this call: */
  1168. if (auth_res == SVC_OK && progp)
  1169. auth_res = progp->pg_authenticate(rqstp);
  1170. trace_svc_authenticate(rqstp, auth_res);
  1171. switch (auth_res) {
  1172. case SVC_OK:
  1173. break;
  1174. case SVC_GARBAGE:
  1175. rqstp->rq_auth_stat = rpc_autherr_badcred;
  1176. goto err_bad_auth;
  1177. case SVC_SYSERR:
  1178. goto err_system_err;
  1179. case SVC_DENIED:
  1180. goto err_bad_auth;
  1181. case SVC_CLOSE:
  1182. goto close;
  1183. case SVC_DROP:
  1184. goto dropit;
  1185. case SVC_COMPLETE:
  1186. goto sendit;
  1187. default:
  1188. pr_warn_once("Unexpected svc_auth_status (%d)\n", auth_res);
  1189. goto err_system_err;
  1190. }
  1191. if (progp == NULL)
  1192. goto err_bad_prog;
  1193. switch (progp->pg_init_request(rqstp, progp, &process)) {
  1194. case rpc_success:
  1195. break;
  1196. case rpc_prog_unavail:
  1197. goto err_bad_prog;
  1198. case rpc_prog_mismatch:
  1199. goto err_bad_vers;
  1200. case rpc_proc_unavail:
  1201. goto err_bad_proc;
  1202. }
  1203. procp = rqstp->rq_procinfo;
  1204. /* Should this check go into the dispatcher? */
  1205. if (!procp || !procp->pc_func)
  1206. goto err_bad_proc;
  1207. /* Syntactic check complete */
  1208. if (serv->sv_stats)
  1209. serv->sv_stats->rpccnt++;
  1210. trace_svc_process(rqstp, progp->pg_name);
  1211. aoffset = xdr_stream_pos(xdr);
  1212. /* un-reserve some of the out-queue now that we have a
  1213. * better idea of reply size
  1214. */
  1215. if (procp->pc_xdrressize)
  1216. svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
  1217. /* Call the function that processes the request. */
  1218. rc = process.dispatch(rqstp);
  1219. if (procp->pc_release)
  1220. procp->pc_release(rqstp);
  1221. xdr_finish_decode(xdr);
  1222. if (!rc)
  1223. goto dropit;
  1224. if (rqstp->rq_auth_stat != rpc_auth_ok)
  1225. goto err_bad_auth;
  1226. if (*rqstp->rq_accept_statp != rpc_success)
  1227. xdr_truncate_encode(xdr, aoffset);
  1228. if (procp->pc_encode == NULL)
  1229. goto dropit;
  1230. sendit:
  1231. if (svc_authorise(rqstp))
  1232. goto close_xprt;
  1233. return 1; /* Caller can now send it */
  1234. dropit:
  1235. svc_authorise(rqstp); /* doesn't hurt to call this twice */
  1236. dprintk("svc: svc_process dropit\n");
  1237. return 0;
  1238. close:
  1239. svc_authorise(rqstp);
  1240. close_xprt:
  1241. if (rqstp->rq_xprt && test_bit(XPT_TEMP, &rqstp->rq_xprt->xpt_flags))
  1242. svc_xprt_close(rqstp->rq_xprt);
  1243. dprintk("svc: svc_process close\n");
  1244. return 0;
  1245. err_short_len:
  1246. svc_printk(rqstp, "short len %u, dropping request\n",
  1247. rqstp->rq_arg.len);
  1248. goto close_xprt;
  1249. err_bad_rpc:
  1250. if (serv->sv_stats)
  1251. serv->sv_stats->rpcbadfmt++;
  1252. xdr_stream_encode_u32(xdr, RPC_MSG_DENIED);
  1253. xdr_stream_encode_u32(xdr, RPC_MISMATCH);
  1254. /* Only RPCv2 supported */
  1255. xdr_stream_encode_u32(xdr, RPC_VERSION);
  1256. xdr_stream_encode_u32(xdr, RPC_VERSION);
  1257. return 1; /* don't wrap */
  1258. err_bad_auth:
  1259. dprintk("svc: authentication failed (%d)\n",
  1260. be32_to_cpu(rqstp->rq_auth_stat));
  1261. if (serv->sv_stats)
  1262. serv->sv_stats->rpcbadauth++;
  1263. /* Restore write pointer to location of reply status: */
  1264. xdr_truncate_encode(xdr, XDR_UNIT * 2);
  1265. xdr_stream_encode_u32(xdr, RPC_MSG_DENIED);
  1266. xdr_stream_encode_u32(xdr, RPC_AUTH_ERROR);
  1267. xdr_stream_encode_be32(xdr, rqstp->rq_auth_stat);
  1268. goto sendit;
  1269. err_bad_prog:
  1270. dprintk("svc: unknown program %d\n", rqstp->rq_prog);
  1271. if (serv->sv_stats)
  1272. serv->sv_stats->rpcbadfmt++;
  1273. *rqstp->rq_accept_statp = rpc_prog_unavail;
  1274. goto sendit;
  1275. err_bad_vers:
  1276. svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
  1277. rqstp->rq_vers, rqstp->rq_prog, progp->pg_name);
  1278. if (serv->sv_stats)
  1279. serv->sv_stats->rpcbadfmt++;
  1280. *rqstp->rq_accept_statp = rpc_prog_mismatch;
  1281. /*
  1282. * svc_authenticate() has already added the verifier and
  1283. * advanced the stream just past rq_accept_statp.
  1284. */
  1285. xdr_stream_encode_u32(xdr, process.mismatch.lovers);
  1286. xdr_stream_encode_u32(xdr, process.mismatch.hivers);
  1287. goto sendit;
  1288. err_bad_proc:
  1289. svc_printk(rqstp, "unknown procedure (%d)\n", rqstp->rq_proc);
  1290. if (serv->sv_stats)
  1291. serv->sv_stats->rpcbadfmt++;
  1292. *rqstp->rq_accept_statp = rpc_proc_unavail;
  1293. goto sendit;
  1294. err_system_err:
  1295. if (serv->sv_stats)
  1296. serv->sv_stats->rpcbadfmt++;
  1297. *rqstp->rq_accept_statp = rpc_system_err;
  1298. goto sendit;
  1299. }
  1300. /*
  1301. * Drop request
  1302. */
  1303. static void svc_drop(struct svc_rqst *rqstp)
  1304. {
  1305. trace_svc_drop(rqstp);
  1306. }
  1307. /**
  1308. * svc_process - Execute one RPC transaction
  1309. * @rqstp: RPC transaction context
  1310. *
  1311. */
  1312. void svc_process(struct svc_rqst *rqstp)
  1313. {
  1314. struct kvec *resv = &rqstp->rq_res.head[0];
  1315. __be32 *p;
  1316. #if IS_ENABLED(CONFIG_FAIL_SUNRPC)
  1317. if (!fail_sunrpc.ignore_server_disconnect &&
  1318. should_fail(&fail_sunrpc.attr, 1))
  1319. svc_xprt_deferred_close(rqstp->rq_xprt);
  1320. #endif
  1321. /*
  1322. * Setup response xdr_buf.
  1323. * Initially it has just one page
  1324. */
  1325. rqstp->rq_next_page = &rqstp->rq_respages[1];
  1326. resv->iov_base = page_address(rqstp->rq_respages[0]);
  1327. resv->iov_len = 0;
  1328. rqstp->rq_res.pages = rqstp->rq_next_page;
  1329. rqstp->rq_res.len = 0;
  1330. rqstp->rq_res.page_base = 0;
  1331. rqstp->rq_res.page_len = 0;
  1332. rqstp->rq_res.buflen = PAGE_SIZE;
  1333. rqstp->rq_res.tail[0].iov_base = NULL;
  1334. rqstp->rq_res.tail[0].iov_len = 0;
  1335. svcxdr_init_decode(rqstp);
  1336. p = xdr_inline_decode(&rqstp->rq_arg_stream, XDR_UNIT * 2);
  1337. if (unlikely(!p))
  1338. goto out_drop;
  1339. rqstp->rq_xid = *p++;
  1340. if (unlikely(*p != rpc_call))
  1341. goto out_baddir;
  1342. if (!svc_process_common(rqstp))
  1343. goto out_drop;
  1344. svc_send(rqstp);
  1345. return;
  1346. out_baddir:
  1347. svc_printk(rqstp, "bad direction 0x%08x, dropping request\n",
  1348. be32_to_cpu(*p));
  1349. if (rqstp->rq_server->sv_stats)
  1350. rqstp->rq_server->sv_stats->rpcbadfmt++;
  1351. out_drop:
  1352. svc_drop(rqstp);
  1353. }
  1354. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1355. /**
  1356. * svc_process_bc - process a reverse-direction RPC request
  1357. * @req: RPC request to be used for client-side processing
  1358. * @rqstp: server-side execution context
  1359. *
  1360. */
  1361. void svc_process_bc(struct rpc_rqst *req, struct svc_rqst *rqstp)
  1362. {
  1363. struct rpc_timeout timeout = {
  1364. .to_increment = 0,
  1365. };
  1366. struct rpc_task *task;
  1367. int proc_error;
  1368. /* Build the svc_rqst used by the common processing routine */
  1369. rqstp->rq_xid = req->rq_xid;
  1370. rqstp->rq_prot = req->rq_xprt->prot;
  1371. rqstp->rq_bc_net = req->rq_xprt->xprt_net;
  1372. rqstp->rq_addrlen = sizeof(req->rq_xprt->addr);
  1373. memcpy(&rqstp->rq_addr, &req->rq_xprt->addr, rqstp->rq_addrlen);
  1374. memcpy(&rqstp->rq_arg, &req->rq_rcv_buf, sizeof(rqstp->rq_arg));
  1375. memcpy(&rqstp->rq_res, &req->rq_snd_buf, sizeof(rqstp->rq_res));
  1376. /* Adjust the argument buffer length */
  1377. rqstp->rq_arg.len = req->rq_private_buf.len;
  1378. if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
  1379. rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
  1380. rqstp->rq_arg.page_len = 0;
  1381. } else if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len +
  1382. rqstp->rq_arg.page_len)
  1383. rqstp->rq_arg.page_len = rqstp->rq_arg.len -
  1384. rqstp->rq_arg.head[0].iov_len;
  1385. else
  1386. rqstp->rq_arg.len = rqstp->rq_arg.head[0].iov_len +
  1387. rqstp->rq_arg.page_len;
  1388. /* Reset the response buffer */
  1389. rqstp->rq_res.head[0].iov_len = 0;
  1390. /*
  1391. * Skip the XID and calldir fields because they've already
  1392. * been processed by the caller.
  1393. */
  1394. svcxdr_init_decode(rqstp);
  1395. if (!xdr_inline_decode(&rqstp->rq_arg_stream, XDR_UNIT * 2))
  1396. return;
  1397. /* Parse and execute the bc call */
  1398. proc_error = svc_process_common(rqstp);
  1399. atomic_dec(&req->rq_xprt->bc_slot_count);
  1400. if (!proc_error) {
  1401. /* Processing error: drop the request */
  1402. xprt_free_bc_request(req);
  1403. return;
  1404. }
  1405. /* Finally, send the reply synchronously */
  1406. if (rqstp->bc_to_initval > 0) {
  1407. timeout.to_initval = rqstp->bc_to_initval;
  1408. timeout.to_retries = rqstp->bc_to_retries;
  1409. } else {
  1410. timeout.to_initval = req->rq_xprt->timeout->to_initval;
  1411. timeout.to_retries = req->rq_xprt->timeout->to_retries;
  1412. }
  1413. timeout.to_maxval = timeout.to_initval;
  1414. memcpy(&req->rq_snd_buf, &rqstp->rq_res, sizeof(req->rq_snd_buf));
  1415. task = rpc_run_bc_task(req, &timeout);
  1416. if (IS_ERR(task))
  1417. return;
  1418. WARN_ON_ONCE(atomic_read(&task->tk_count) != 1);
  1419. rpc_put_task(task);
  1420. }
  1421. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1422. /**
  1423. * svc_max_payload - Return transport-specific limit on the RPC payload
  1424. * @rqstp: RPC transaction context
  1425. *
  1426. * Returns the maximum number of payload bytes the current transport
  1427. * allows.
  1428. */
  1429. u32 svc_max_payload(const struct svc_rqst *rqstp)
  1430. {
  1431. u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload;
  1432. if (rqstp->rq_server->sv_max_payload < max)
  1433. max = rqstp->rq_server->sv_max_payload;
  1434. return max;
  1435. }
  1436. EXPORT_SYMBOL_GPL(svc_max_payload);
  1437. /**
  1438. * svc_proc_name - Return RPC procedure name in string form
  1439. * @rqstp: svc_rqst to operate on
  1440. *
  1441. * Return value:
  1442. * Pointer to a NUL-terminated string
  1443. */
  1444. const char *svc_proc_name(const struct svc_rqst *rqstp)
  1445. {
  1446. if (rqstp && rqstp->rq_procinfo)
  1447. return rqstp->rq_procinfo->pc_name;
  1448. return "unknown";
  1449. }
  1450. /**
  1451. * svc_encode_result_payload - mark a range of bytes as a result payload
  1452. * @rqstp: svc_rqst to operate on
  1453. * @offset: payload's byte offset in rqstp->rq_res
  1454. * @length: size of payload, in bytes
  1455. *
  1456. * Returns zero on success, or a negative errno if a permanent
  1457. * error occurred.
  1458. */
  1459. int svc_encode_result_payload(struct svc_rqst *rqstp, unsigned int offset,
  1460. unsigned int length)
  1461. {
  1462. return rqstp->rq_xprt->xpt_ops->xpo_result_payload(rqstp, offset,
  1463. length);
  1464. }
  1465. EXPORT_SYMBOL_GPL(svc_encode_result_payload);
  1466. /**
  1467. * svc_fill_write_vector - Construct data argument for VFS write call
  1468. * @rqstp: svc_rqst to operate on
  1469. * @payload: xdr_buf containing only the write data payload
  1470. *
  1471. * Fills in rqstp::rq_vec, and returns the number of elements.
  1472. */
  1473. unsigned int svc_fill_write_vector(struct svc_rqst *rqstp,
  1474. struct xdr_buf *payload)
  1475. {
  1476. struct page **pages = payload->pages;
  1477. struct kvec *first = payload->head;
  1478. struct kvec *vec = rqstp->rq_vec;
  1479. size_t total = payload->len;
  1480. unsigned int i;
  1481. /* Some types of transport can present the write payload
  1482. * entirely in rq_arg.pages. In this case, @first is empty.
  1483. */
  1484. i = 0;
  1485. if (first->iov_len) {
  1486. vec[i].iov_base = first->iov_base;
  1487. vec[i].iov_len = min_t(size_t, total, first->iov_len);
  1488. total -= vec[i].iov_len;
  1489. ++i;
  1490. }
  1491. while (total) {
  1492. vec[i].iov_base = page_address(*pages);
  1493. vec[i].iov_len = min_t(size_t, total, PAGE_SIZE);
  1494. total -= vec[i].iov_len;
  1495. ++i;
  1496. ++pages;
  1497. }
  1498. WARN_ON_ONCE(i > ARRAY_SIZE(rqstp->rq_vec));
  1499. return i;
  1500. }
  1501. EXPORT_SYMBOL_GPL(svc_fill_write_vector);
  1502. /**
  1503. * svc_fill_symlink_pathname - Construct pathname argument for VFS symlink call
  1504. * @rqstp: svc_rqst to operate on
  1505. * @first: buffer containing first section of pathname
  1506. * @p: buffer containing remaining section of pathname
  1507. * @total: total length of the pathname argument
  1508. *
  1509. * The VFS symlink API demands a NUL-terminated pathname in mapped memory.
  1510. * Returns pointer to a NUL-terminated string, or an ERR_PTR. Caller must free
  1511. * the returned string.
  1512. */
  1513. char *svc_fill_symlink_pathname(struct svc_rqst *rqstp, struct kvec *first,
  1514. void *p, size_t total)
  1515. {
  1516. size_t len, remaining;
  1517. char *result, *dst;
  1518. result = kmalloc(total + 1, GFP_KERNEL);
  1519. if (!result)
  1520. return ERR_PTR(-ESERVERFAULT);
  1521. dst = result;
  1522. remaining = total;
  1523. len = min_t(size_t, total, first->iov_len);
  1524. if (len) {
  1525. memcpy(dst, first->iov_base, len);
  1526. dst += len;
  1527. remaining -= len;
  1528. }
  1529. if (remaining) {
  1530. len = min_t(size_t, remaining, PAGE_SIZE);
  1531. memcpy(dst, p, len);
  1532. dst += len;
  1533. }
  1534. *dst = '\0';
  1535. /* Sanity check: Linux doesn't allow the pathname argument to
  1536. * contain a NUL byte.
  1537. */
  1538. if (strlen(result) != total) {
  1539. kfree(result);
  1540. return ERR_PTR(-EINVAL);
  1541. }
  1542. return result;
  1543. }
  1544. EXPORT_SYMBOL_GPL(svc_fill_symlink_pathname);