ucma.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  1. /*
  2. * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/completion.h>
  33. #include <linux/file.h>
  34. #include <linux/mutex.h>
  35. #include <linux/poll.h>
  36. #include <linux/sched.h>
  37. #include <linux/idr.h>
  38. #include <linux/in.h>
  39. #include <linux/in6.h>
  40. #include <linux/miscdevice.h>
  41. #include <linux/slab.h>
  42. #include <linux/sysctl.h>
  43. #include <linux/module.h>
  44. #include <linux/nsproxy.h>
  45. #include <linux/nospec.h>
  46. #include <rdma/rdma_user_cm.h>
  47. #include <rdma/ib_marshall.h>
  48. #include <rdma/rdma_cm.h>
  49. #include <rdma/rdma_cm_ib.h>
  50. #include <rdma/ib_addr.h>
  51. #include <rdma/ib.h>
  52. MODULE_AUTHOR("Sean Hefty");
  53. MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
  54. MODULE_LICENSE("Dual BSD/GPL");
  55. static unsigned int max_backlog = 1024;
  56. static struct ctl_table_header *ucma_ctl_table_hdr;
  57. static struct ctl_table ucma_ctl_table[] = {
  58. {
  59. .procname = "max_backlog",
  60. .data = &max_backlog,
  61. .maxlen = sizeof max_backlog,
  62. .mode = 0644,
  63. .proc_handler = proc_dointvec,
  64. },
  65. { }
  66. };
  67. struct ucma_file {
  68. struct mutex mut;
  69. struct file *filp;
  70. struct list_head ctx_list;
  71. struct list_head event_list;
  72. wait_queue_head_t poll_wait;
  73. struct workqueue_struct *close_wq;
  74. };
  75. struct ucma_context {
  76. int id;
  77. struct completion comp;
  78. atomic_t ref;
  79. int events_reported;
  80. int backlog;
  81. struct ucma_file *file;
  82. struct rdma_cm_id *cm_id;
  83. u64 uid;
  84. struct list_head list;
  85. struct list_head mc_list;
  86. /* mark that device is in process of destroying the internal HW
  87. * resources, protected by the global mut
  88. */
  89. int closing;
  90. /* sync between removal event and id destroy, protected by file mut */
  91. int destroying;
  92. struct work_struct close_work;
  93. };
  94. struct ucma_multicast {
  95. struct ucma_context *ctx;
  96. int id;
  97. int events_reported;
  98. u64 uid;
  99. struct list_head list;
  100. struct sockaddr_storage addr;
  101. };
  102. struct ucma_event {
  103. struct ucma_context *ctx;
  104. struct ucma_multicast *mc;
  105. struct list_head list;
  106. struct rdma_cm_id *cm_id;
  107. struct rdma_ucm_event_resp resp;
  108. struct work_struct close_work;
  109. };
  110. static DEFINE_MUTEX(mut);
  111. static DEFINE_IDR(ctx_idr);
  112. static DEFINE_IDR(multicast_idr);
  113. static const struct file_operations ucma_fops;
  114. static inline struct ucma_context *_ucma_find_context(int id,
  115. struct ucma_file *file)
  116. {
  117. struct ucma_context *ctx;
  118. ctx = idr_find(&ctx_idr, id);
  119. if (!ctx)
  120. ctx = ERR_PTR(-ENOENT);
  121. else if (ctx->file != file || !ctx->cm_id)
  122. ctx = ERR_PTR(-EINVAL);
  123. return ctx;
  124. }
  125. static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
  126. {
  127. struct ucma_context *ctx;
  128. mutex_lock(&mut);
  129. ctx = _ucma_find_context(id, file);
  130. if (!IS_ERR(ctx)) {
  131. if (ctx->closing)
  132. ctx = ERR_PTR(-EIO);
  133. else
  134. atomic_inc(&ctx->ref);
  135. }
  136. mutex_unlock(&mut);
  137. return ctx;
  138. }
  139. static void ucma_put_ctx(struct ucma_context *ctx)
  140. {
  141. if (atomic_dec_and_test(&ctx->ref))
  142. complete(&ctx->comp);
  143. }
  144. static void ucma_close_event_id(struct work_struct *work)
  145. {
  146. struct ucma_event *uevent_close = container_of(work, struct ucma_event, close_work);
  147. rdma_destroy_id(uevent_close->cm_id);
  148. kfree(uevent_close);
  149. }
  150. static void ucma_close_id(struct work_struct *work)
  151. {
  152. struct ucma_context *ctx = container_of(work, struct ucma_context, close_work);
  153. /* once all inflight tasks are finished, we close all underlying
  154. * resources. The context is still alive till its explicit destryoing
  155. * by its creator.
  156. */
  157. ucma_put_ctx(ctx);
  158. wait_for_completion(&ctx->comp);
  159. /* No new events will be generated after destroying the id. */
  160. rdma_destroy_id(ctx->cm_id);
  161. }
  162. static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
  163. {
  164. struct ucma_context *ctx;
  165. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  166. if (!ctx)
  167. return NULL;
  168. INIT_WORK(&ctx->close_work, ucma_close_id);
  169. atomic_set(&ctx->ref, 1);
  170. init_completion(&ctx->comp);
  171. INIT_LIST_HEAD(&ctx->mc_list);
  172. ctx->file = file;
  173. mutex_lock(&mut);
  174. ctx->id = idr_alloc(&ctx_idr, ctx, 0, 0, GFP_KERNEL);
  175. mutex_unlock(&mut);
  176. if (ctx->id < 0)
  177. goto error;
  178. list_add_tail(&ctx->list, &file->ctx_list);
  179. return ctx;
  180. error:
  181. kfree(ctx);
  182. return NULL;
  183. }
  184. static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
  185. {
  186. struct ucma_multicast *mc;
  187. mc = kzalloc(sizeof(*mc), GFP_KERNEL);
  188. if (!mc)
  189. return NULL;
  190. mutex_lock(&mut);
  191. mc->id = idr_alloc(&multicast_idr, NULL, 0, 0, GFP_KERNEL);
  192. mutex_unlock(&mut);
  193. if (mc->id < 0)
  194. goto error;
  195. mc->ctx = ctx;
  196. list_add_tail(&mc->list, &ctx->mc_list);
  197. return mc;
  198. error:
  199. kfree(mc);
  200. return NULL;
  201. }
  202. static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
  203. struct rdma_conn_param *src)
  204. {
  205. if (src->private_data_len)
  206. memcpy(dst->private_data, src->private_data,
  207. src->private_data_len);
  208. dst->private_data_len = src->private_data_len;
  209. dst->responder_resources =src->responder_resources;
  210. dst->initiator_depth = src->initiator_depth;
  211. dst->flow_control = src->flow_control;
  212. dst->retry_count = src->retry_count;
  213. dst->rnr_retry_count = src->rnr_retry_count;
  214. dst->srq = src->srq;
  215. dst->qp_num = src->qp_num;
  216. }
  217. static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
  218. struct rdma_ud_param *src)
  219. {
  220. if (src->private_data_len)
  221. memcpy(dst->private_data, src->private_data,
  222. src->private_data_len);
  223. dst->private_data_len = src->private_data_len;
  224. ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
  225. dst->qp_num = src->qp_num;
  226. dst->qkey = src->qkey;
  227. }
  228. static void ucma_set_event_context(struct ucma_context *ctx,
  229. struct rdma_cm_event *event,
  230. struct ucma_event *uevent)
  231. {
  232. uevent->ctx = ctx;
  233. switch (event->event) {
  234. case RDMA_CM_EVENT_MULTICAST_JOIN:
  235. case RDMA_CM_EVENT_MULTICAST_ERROR:
  236. uevent->mc = (struct ucma_multicast *)
  237. event->param.ud.private_data;
  238. uevent->resp.uid = uevent->mc->uid;
  239. uevent->resp.id = uevent->mc->id;
  240. break;
  241. default:
  242. uevent->resp.uid = ctx->uid;
  243. uevent->resp.id = ctx->id;
  244. break;
  245. }
  246. }
  247. /* Called with file->mut locked for the relevant context. */
  248. static void ucma_removal_event_handler(struct rdma_cm_id *cm_id)
  249. {
  250. struct ucma_context *ctx = cm_id->context;
  251. struct ucma_event *con_req_eve;
  252. int event_found = 0;
  253. if (ctx->destroying)
  254. return;
  255. /* only if context is pointing to cm_id that it owns it and can be
  256. * queued to be closed, otherwise that cm_id is an inflight one that
  257. * is part of that context event list pending to be detached and
  258. * reattached to its new context as part of ucma_get_event,
  259. * handled separately below.
  260. */
  261. if (ctx->cm_id == cm_id) {
  262. mutex_lock(&mut);
  263. ctx->closing = 1;
  264. mutex_unlock(&mut);
  265. queue_work(ctx->file->close_wq, &ctx->close_work);
  266. return;
  267. }
  268. list_for_each_entry(con_req_eve, &ctx->file->event_list, list) {
  269. if (con_req_eve->cm_id == cm_id &&
  270. con_req_eve->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  271. list_del(&con_req_eve->list);
  272. INIT_WORK(&con_req_eve->close_work, ucma_close_event_id);
  273. queue_work(ctx->file->close_wq, &con_req_eve->close_work);
  274. event_found = 1;
  275. break;
  276. }
  277. }
  278. if (!event_found)
  279. printk(KERN_ERR "ucma_removal_event_handler: warning: connect request event wasn't found\n");
  280. }
  281. static int ucma_event_handler(struct rdma_cm_id *cm_id,
  282. struct rdma_cm_event *event)
  283. {
  284. struct ucma_event *uevent;
  285. struct ucma_context *ctx = cm_id->context;
  286. int ret = 0;
  287. uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
  288. if (!uevent)
  289. return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
  290. mutex_lock(&ctx->file->mut);
  291. uevent->cm_id = cm_id;
  292. ucma_set_event_context(ctx, event, uevent);
  293. uevent->resp.event = event->event;
  294. uevent->resp.status = event->status;
  295. if (cm_id->qp_type == IB_QPT_UD)
  296. ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud);
  297. else
  298. ucma_copy_conn_event(&uevent->resp.param.conn,
  299. &event->param.conn);
  300. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  301. if (!ctx->backlog) {
  302. ret = -ENOMEM;
  303. kfree(uevent);
  304. goto out;
  305. }
  306. ctx->backlog--;
  307. } else if (!ctx->uid || ctx->cm_id != cm_id) {
  308. /*
  309. * We ignore events for new connections until userspace has set
  310. * their context. This can only happen if an error occurs on a
  311. * new connection before the user accepts it. This is okay,
  312. * since the accept will just fail later. However, we do need
  313. * to release the underlying HW resources in case of a device
  314. * removal event.
  315. */
  316. if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
  317. ucma_removal_event_handler(cm_id);
  318. kfree(uevent);
  319. goto out;
  320. }
  321. list_add_tail(&uevent->list, &ctx->file->event_list);
  322. wake_up_interruptible(&ctx->file->poll_wait);
  323. if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
  324. ucma_removal_event_handler(cm_id);
  325. out:
  326. mutex_unlock(&ctx->file->mut);
  327. return ret;
  328. }
  329. static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
  330. int in_len, int out_len)
  331. {
  332. struct ucma_context *ctx;
  333. struct rdma_ucm_get_event cmd;
  334. struct ucma_event *uevent;
  335. int ret = 0;
  336. if (out_len < sizeof uevent->resp)
  337. return -ENOSPC;
  338. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  339. return -EFAULT;
  340. mutex_lock(&file->mut);
  341. while (list_empty(&file->event_list)) {
  342. mutex_unlock(&file->mut);
  343. if (file->filp->f_flags & O_NONBLOCK)
  344. return -EAGAIN;
  345. if (wait_event_interruptible(file->poll_wait,
  346. !list_empty(&file->event_list)))
  347. return -ERESTARTSYS;
  348. mutex_lock(&file->mut);
  349. }
  350. uevent = list_entry(file->event_list.next, struct ucma_event, list);
  351. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  352. ctx = ucma_alloc_ctx(file);
  353. if (!ctx) {
  354. ret = -ENOMEM;
  355. goto done;
  356. }
  357. uevent->ctx->backlog++;
  358. ctx->cm_id = uevent->cm_id;
  359. ctx->cm_id->context = ctx;
  360. uevent->resp.id = ctx->id;
  361. }
  362. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  363. &uevent->resp, sizeof uevent->resp)) {
  364. ret = -EFAULT;
  365. goto done;
  366. }
  367. list_del(&uevent->list);
  368. uevent->ctx->events_reported++;
  369. if (uevent->mc)
  370. uevent->mc->events_reported++;
  371. kfree(uevent);
  372. done:
  373. mutex_unlock(&file->mut);
  374. return ret;
  375. }
  376. static int ucma_get_qp_type(struct rdma_ucm_create_id *cmd, enum ib_qp_type *qp_type)
  377. {
  378. switch (cmd->ps) {
  379. case RDMA_PS_TCP:
  380. *qp_type = IB_QPT_RC;
  381. return 0;
  382. case RDMA_PS_UDP:
  383. case RDMA_PS_IPOIB:
  384. *qp_type = IB_QPT_UD;
  385. return 0;
  386. case RDMA_PS_IB:
  387. *qp_type = cmd->qp_type;
  388. return 0;
  389. default:
  390. return -EINVAL;
  391. }
  392. }
  393. static ssize_t ucma_create_id(struct ucma_file *file, const char __user *inbuf,
  394. int in_len, int out_len)
  395. {
  396. struct rdma_ucm_create_id cmd;
  397. struct rdma_ucm_create_id_resp resp;
  398. struct ucma_context *ctx;
  399. struct rdma_cm_id *cm_id;
  400. enum ib_qp_type qp_type;
  401. int ret;
  402. if (out_len < sizeof(resp))
  403. return -ENOSPC;
  404. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  405. return -EFAULT;
  406. ret = ucma_get_qp_type(&cmd, &qp_type);
  407. if (ret)
  408. return ret;
  409. mutex_lock(&file->mut);
  410. ctx = ucma_alloc_ctx(file);
  411. mutex_unlock(&file->mut);
  412. if (!ctx)
  413. return -ENOMEM;
  414. ctx->uid = cmd.uid;
  415. cm_id = rdma_create_id(current->nsproxy->net_ns,
  416. ucma_event_handler, ctx, cmd.ps, qp_type);
  417. if (IS_ERR(cm_id)) {
  418. ret = PTR_ERR(cm_id);
  419. goto err1;
  420. }
  421. resp.id = ctx->id;
  422. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  423. &resp, sizeof(resp))) {
  424. ret = -EFAULT;
  425. goto err2;
  426. }
  427. ctx->cm_id = cm_id;
  428. return 0;
  429. err2:
  430. rdma_destroy_id(cm_id);
  431. err1:
  432. mutex_lock(&mut);
  433. idr_remove(&ctx_idr, ctx->id);
  434. mutex_unlock(&mut);
  435. mutex_lock(&file->mut);
  436. list_del(&ctx->list);
  437. mutex_unlock(&file->mut);
  438. kfree(ctx);
  439. return ret;
  440. }
  441. static void ucma_cleanup_multicast(struct ucma_context *ctx)
  442. {
  443. struct ucma_multicast *mc, *tmp;
  444. mutex_lock(&mut);
  445. list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
  446. list_del(&mc->list);
  447. idr_remove(&multicast_idr, mc->id);
  448. kfree(mc);
  449. }
  450. mutex_unlock(&mut);
  451. }
  452. static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
  453. {
  454. struct ucma_event *uevent, *tmp;
  455. list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
  456. if (uevent->mc != mc)
  457. continue;
  458. list_del(&uevent->list);
  459. kfree(uevent);
  460. }
  461. }
  462. /*
  463. * ucma_free_ctx is called after the underlying rdma CM-ID is destroyed. At
  464. * this point, no new events will be reported from the hardware. However, we
  465. * still need to cleanup the UCMA context for this ID. Specifically, there
  466. * might be events that have not yet been consumed by the user space software.
  467. * These might include pending connect requests which we have not completed
  468. * processing. We cannot call rdma_destroy_id while holding the lock of the
  469. * context (file->mut), as it might cause a deadlock. We therefore extract all
  470. * relevant events from the context pending events list while holding the
  471. * mutex. After that we release them as needed.
  472. */
  473. static int ucma_free_ctx(struct ucma_context *ctx)
  474. {
  475. int events_reported;
  476. struct ucma_event *uevent, *tmp;
  477. LIST_HEAD(list);
  478. ucma_cleanup_multicast(ctx);
  479. /* Cleanup events not yet reported to the user. */
  480. mutex_lock(&ctx->file->mut);
  481. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
  482. if (uevent->ctx == ctx)
  483. list_move_tail(&uevent->list, &list);
  484. }
  485. list_del(&ctx->list);
  486. mutex_unlock(&ctx->file->mut);
  487. list_for_each_entry_safe(uevent, tmp, &list, list) {
  488. list_del(&uevent->list);
  489. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
  490. rdma_destroy_id(uevent->cm_id);
  491. kfree(uevent);
  492. }
  493. events_reported = ctx->events_reported;
  494. kfree(ctx);
  495. return events_reported;
  496. }
  497. static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
  498. int in_len, int out_len)
  499. {
  500. struct rdma_ucm_destroy_id cmd;
  501. struct rdma_ucm_destroy_id_resp resp;
  502. struct ucma_context *ctx;
  503. int ret = 0;
  504. if (out_len < sizeof(resp))
  505. return -ENOSPC;
  506. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  507. return -EFAULT;
  508. mutex_lock(&mut);
  509. ctx = _ucma_find_context(cmd.id, file);
  510. if (!IS_ERR(ctx))
  511. idr_remove(&ctx_idr, ctx->id);
  512. mutex_unlock(&mut);
  513. if (IS_ERR(ctx))
  514. return PTR_ERR(ctx);
  515. mutex_lock(&ctx->file->mut);
  516. ctx->destroying = 1;
  517. mutex_unlock(&ctx->file->mut);
  518. flush_workqueue(ctx->file->close_wq);
  519. /* At this point it's guaranteed that there is no inflight
  520. * closing task */
  521. mutex_lock(&mut);
  522. if (!ctx->closing) {
  523. mutex_unlock(&mut);
  524. ucma_put_ctx(ctx);
  525. wait_for_completion(&ctx->comp);
  526. rdma_destroy_id(ctx->cm_id);
  527. } else {
  528. mutex_unlock(&mut);
  529. }
  530. resp.events_reported = ucma_free_ctx(ctx);
  531. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  532. &resp, sizeof(resp)))
  533. ret = -EFAULT;
  534. return ret;
  535. }
  536. static ssize_t ucma_bind_ip(struct ucma_file *file, const char __user *inbuf,
  537. int in_len, int out_len)
  538. {
  539. struct rdma_ucm_bind_ip cmd;
  540. struct ucma_context *ctx;
  541. int ret;
  542. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  543. return -EFAULT;
  544. if (!rdma_addr_size_in6(&cmd.addr))
  545. return -EINVAL;
  546. ctx = ucma_get_ctx(file, cmd.id);
  547. if (IS_ERR(ctx))
  548. return PTR_ERR(ctx);
  549. ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
  550. ucma_put_ctx(ctx);
  551. return ret;
  552. }
  553. static ssize_t ucma_bind(struct ucma_file *file, const char __user *inbuf,
  554. int in_len, int out_len)
  555. {
  556. struct rdma_ucm_bind cmd;
  557. struct ucma_context *ctx;
  558. int ret;
  559. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  560. return -EFAULT;
  561. if (cmd.reserved || !cmd.addr_size ||
  562. cmd.addr_size != rdma_addr_size_kss(&cmd.addr))
  563. return -EINVAL;
  564. ctx = ucma_get_ctx(file, cmd.id);
  565. if (IS_ERR(ctx))
  566. return PTR_ERR(ctx);
  567. ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
  568. ucma_put_ctx(ctx);
  569. return ret;
  570. }
  571. static ssize_t ucma_resolve_ip(struct ucma_file *file,
  572. const char __user *inbuf,
  573. int in_len, int out_len)
  574. {
  575. struct rdma_ucm_resolve_ip cmd;
  576. struct ucma_context *ctx;
  577. int ret;
  578. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  579. return -EFAULT;
  580. if ((cmd.src_addr.sin6_family && !rdma_addr_size_in6(&cmd.src_addr)) ||
  581. !rdma_addr_size_in6(&cmd.dst_addr))
  582. return -EINVAL;
  583. ctx = ucma_get_ctx(file, cmd.id);
  584. if (IS_ERR(ctx))
  585. return PTR_ERR(ctx);
  586. ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
  587. (struct sockaddr *) &cmd.dst_addr, cmd.timeout_ms);
  588. ucma_put_ctx(ctx);
  589. return ret;
  590. }
  591. static ssize_t ucma_resolve_addr(struct ucma_file *file,
  592. const char __user *inbuf,
  593. int in_len, int out_len)
  594. {
  595. struct rdma_ucm_resolve_addr cmd;
  596. struct ucma_context *ctx;
  597. int ret;
  598. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  599. return -EFAULT;
  600. if (cmd.reserved ||
  601. (cmd.src_size && (cmd.src_size != rdma_addr_size_kss(&cmd.src_addr))) ||
  602. !cmd.dst_size || (cmd.dst_size != rdma_addr_size_kss(&cmd.dst_addr)))
  603. return -EINVAL;
  604. ctx = ucma_get_ctx(file, cmd.id);
  605. if (IS_ERR(ctx))
  606. return PTR_ERR(ctx);
  607. ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
  608. (struct sockaddr *) &cmd.dst_addr, cmd.timeout_ms);
  609. ucma_put_ctx(ctx);
  610. return ret;
  611. }
  612. static ssize_t ucma_resolve_route(struct ucma_file *file,
  613. const char __user *inbuf,
  614. int in_len, int out_len)
  615. {
  616. struct rdma_ucm_resolve_route cmd;
  617. struct ucma_context *ctx;
  618. int ret;
  619. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  620. return -EFAULT;
  621. ctx = ucma_get_ctx(file, cmd.id);
  622. if (IS_ERR(ctx))
  623. return PTR_ERR(ctx);
  624. ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
  625. ucma_put_ctx(ctx);
  626. return ret;
  627. }
  628. static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
  629. struct rdma_route *route)
  630. {
  631. struct rdma_dev_addr *dev_addr;
  632. resp->num_paths = route->num_paths;
  633. switch (route->num_paths) {
  634. case 0:
  635. dev_addr = &route->addr.dev_addr;
  636. rdma_addr_get_dgid(dev_addr,
  637. (union ib_gid *) &resp->ib_route[0].dgid);
  638. rdma_addr_get_sgid(dev_addr,
  639. (union ib_gid *) &resp->ib_route[0].sgid);
  640. resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  641. break;
  642. case 2:
  643. ib_copy_path_rec_to_user(&resp->ib_route[1],
  644. &route->path_rec[1]);
  645. /* fall through */
  646. case 1:
  647. ib_copy_path_rec_to_user(&resp->ib_route[0],
  648. &route->path_rec[0]);
  649. break;
  650. default:
  651. break;
  652. }
  653. }
  654. static void ucma_copy_iboe_route(struct rdma_ucm_query_route_resp *resp,
  655. struct rdma_route *route)
  656. {
  657. resp->num_paths = route->num_paths;
  658. switch (route->num_paths) {
  659. case 0:
  660. rdma_ip2gid((struct sockaddr *)&route->addr.dst_addr,
  661. (union ib_gid *)&resp->ib_route[0].dgid);
  662. rdma_ip2gid((struct sockaddr *)&route->addr.src_addr,
  663. (union ib_gid *)&resp->ib_route[0].sgid);
  664. resp->ib_route[0].pkey = cpu_to_be16(0xffff);
  665. break;
  666. case 2:
  667. ib_copy_path_rec_to_user(&resp->ib_route[1],
  668. &route->path_rec[1]);
  669. /* fall through */
  670. case 1:
  671. ib_copy_path_rec_to_user(&resp->ib_route[0],
  672. &route->path_rec[0]);
  673. break;
  674. default:
  675. break;
  676. }
  677. }
  678. static void ucma_copy_iw_route(struct rdma_ucm_query_route_resp *resp,
  679. struct rdma_route *route)
  680. {
  681. struct rdma_dev_addr *dev_addr;
  682. dev_addr = &route->addr.dev_addr;
  683. rdma_addr_get_dgid(dev_addr, (union ib_gid *) &resp->ib_route[0].dgid);
  684. rdma_addr_get_sgid(dev_addr, (union ib_gid *) &resp->ib_route[0].sgid);
  685. }
  686. static ssize_t ucma_query_route(struct ucma_file *file,
  687. const char __user *inbuf,
  688. int in_len, int out_len)
  689. {
  690. struct rdma_ucm_query cmd;
  691. struct rdma_ucm_query_route_resp resp;
  692. struct ucma_context *ctx;
  693. struct sockaddr *addr;
  694. int ret = 0;
  695. if (out_len < sizeof(resp))
  696. return -ENOSPC;
  697. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  698. return -EFAULT;
  699. ctx = ucma_get_ctx(file, cmd.id);
  700. if (IS_ERR(ctx))
  701. return PTR_ERR(ctx);
  702. memset(&resp, 0, sizeof resp);
  703. addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
  704. memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
  705. sizeof(struct sockaddr_in) :
  706. sizeof(struct sockaddr_in6));
  707. addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
  708. memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
  709. sizeof(struct sockaddr_in) :
  710. sizeof(struct sockaddr_in6));
  711. if (!ctx->cm_id->device)
  712. goto out;
  713. resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid;
  714. resp.port_num = ctx->cm_id->port_num;
  715. if (rdma_cap_ib_sa(ctx->cm_id->device, ctx->cm_id->port_num))
  716. ucma_copy_ib_route(&resp, &ctx->cm_id->route);
  717. else if (rdma_protocol_roce(ctx->cm_id->device, ctx->cm_id->port_num))
  718. ucma_copy_iboe_route(&resp, &ctx->cm_id->route);
  719. else if (rdma_protocol_iwarp(ctx->cm_id->device, ctx->cm_id->port_num))
  720. ucma_copy_iw_route(&resp, &ctx->cm_id->route);
  721. out:
  722. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  723. &resp, sizeof(resp)))
  724. ret = -EFAULT;
  725. ucma_put_ctx(ctx);
  726. return ret;
  727. }
  728. static void ucma_query_device_addr(struct rdma_cm_id *cm_id,
  729. struct rdma_ucm_query_addr_resp *resp)
  730. {
  731. if (!cm_id->device)
  732. return;
  733. resp->node_guid = (__force __u64) cm_id->device->node_guid;
  734. resp->port_num = cm_id->port_num;
  735. resp->pkey = (__force __u16) cpu_to_be16(
  736. ib_addr_get_pkey(&cm_id->route.addr.dev_addr));
  737. }
  738. static ssize_t ucma_query_addr(struct ucma_context *ctx,
  739. void __user *response, int out_len)
  740. {
  741. struct rdma_ucm_query_addr_resp resp;
  742. struct sockaddr *addr;
  743. int ret = 0;
  744. if (out_len < sizeof(resp))
  745. return -ENOSPC;
  746. memset(&resp, 0, sizeof resp);
  747. addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
  748. resp.src_size = rdma_addr_size(addr);
  749. memcpy(&resp.src_addr, addr, resp.src_size);
  750. addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
  751. resp.dst_size = rdma_addr_size(addr);
  752. memcpy(&resp.dst_addr, addr, resp.dst_size);
  753. ucma_query_device_addr(ctx->cm_id, &resp);
  754. if (copy_to_user(response, &resp, sizeof(resp)))
  755. ret = -EFAULT;
  756. return ret;
  757. }
  758. static ssize_t ucma_query_path(struct ucma_context *ctx,
  759. void __user *response, int out_len)
  760. {
  761. struct rdma_ucm_query_path_resp *resp;
  762. int i, ret = 0;
  763. if (out_len < sizeof(*resp))
  764. return -ENOSPC;
  765. resp = kzalloc(out_len, GFP_KERNEL);
  766. if (!resp)
  767. return -ENOMEM;
  768. resp->num_paths = ctx->cm_id->route.num_paths;
  769. for (i = 0, out_len -= sizeof(*resp);
  770. i < resp->num_paths && out_len > sizeof(struct ib_path_rec_data);
  771. i++, out_len -= sizeof(struct ib_path_rec_data)) {
  772. resp->path_data[i].flags = IB_PATH_GMP | IB_PATH_PRIMARY |
  773. IB_PATH_BIDIRECTIONAL;
  774. ib_sa_pack_path(&ctx->cm_id->route.path_rec[i],
  775. &resp->path_data[i].path_rec);
  776. }
  777. if (copy_to_user(response, resp,
  778. sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))))
  779. ret = -EFAULT;
  780. kfree(resp);
  781. return ret;
  782. }
  783. static ssize_t ucma_query_gid(struct ucma_context *ctx,
  784. void __user *response, int out_len)
  785. {
  786. struct rdma_ucm_query_addr_resp resp;
  787. struct sockaddr_ib *addr;
  788. int ret = 0;
  789. if (out_len < sizeof(resp))
  790. return -ENOSPC;
  791. memset(&resp, 0, sizeof resp);
  792. ucma_query_device_addr(ctx->cm_id, &resp);
  793. addr = (struct sockaddr_ib *) &resp.src_addr;
  794. resp.src_size = sizeof(*addr);
  795. if (ctx->cm_id->route.addr.src_addr.ss_family == AF_IB) {
  796. memcpy(addr, &ctx->cm_id->route.addr.src_addr, resp.src_size);
  797. } else {
  798. addr->sib_family = AF_IB;
  799. addr->sib_pkey = (__force __be16) resp.pkey;
  800. rdma_addr_get_sgid(&ctx->cm_id->route.addr.dev_addr,
  801. (union ib_gid *) &addr->sib_addr);
  802. addr->sib_sid = rdma_get_service_id(ctx->cm_id, (struct sockaddr *)
  803. &ctx->cm_id->route.addr.src_addr);
  804. }
  805. addr = (struct sockaddr_ib *) &resp.dst_addr;
  806. resp.dst_size = sizeof(*addr);
  807. if (ctx->cm_id->route.addr.dst_addr.ss_family == AF_IB) {
  808. memcpy(addr, &ctx->cm_id->route.addr.dst_addr, resp.dst_size);
  809. } else {
  810. addr->sib_family = AF_IB;
  811. addr->sib_pkey = (__force __be16) resp.pkey;
  812. rdma_addr_get_dgid(&ctx->cm_id->route.addr.dev_addr,
  813. (union ib_gid *) &addr->sib_addr);
  814. addr->sib_sid = rdma_get_service_id(ctx->cm_id, (struct sockaddr *)
  815. &ctx->cm_id->route.addr.dst_addr);
  816. }
  817. if (copy_to_user(response, &resp, sizeof(resp)))
  818. ret = -EFAULT;
  819. return ret;
  820. }
  821. static ssize_t ucma_query(struct ucma_file *file,
  822. const char __user *inbuf,
  823. int in_len, int out_len)
  824. {
  825. struct rdma_ucm_query cmd;
  826. struct ucma_context *ctx;
  827. void __user *response;
  828. int ret;
  829. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  830. return -EFAULT;
  831. response = (void __user *)(unsigned long) cmd.response;
  832. ctx = ucma_get_ctx(file, cmd.id);
  833. if (IS_ERR(ctx))
  834. return PTR_ERR(ctx);
  835. switch (cmd.option) {
  836. case RDMA_USER_CM_QUERY_ADDR:
  837. ret = ucma_query_addr(ctx, response, out_len);
  838. break;
  839. case RDMA_USER_CM_QUERY_PATH:
  840. ret = ucma_query_path(ctx, response, out_len);
  841. break;
  842. case RDMA_USER_CM_QUERY_GID:
  843. ret = ucma_query_gid(ctx, response, out_len);
  844. break;
  845. default:
  846. ret = -ENOSYS;
  847. break;
  848. }
  849. ucma_put_ctx(ctx);
  850. return ret;
  851. }
  852. static void ucma_copy_conn_param(struct rdma_cm_id *id,
  853. struct rdma_conn_param *dst,
  854. struct rdma_ucm_conn_param *src)
  855. {
  856. dst->private_data = src->private_data;
  857. dst->private_data_len = src->private_data_len;
  858. dst->responder_resources =src->responder_resources;
  859. dst->initiator_depth = src->initiator_depth;
  860. dst->flow_control = src->flow_control;
  861. dst->retry_count = src->retry_count;
  862. dst->rnr_retry_count = src->rnr_retry_count;
  863. dst->srq = src->srq;
  864. dst->qp_num = src->qp_num;
  865. dst->qkey = (id->route.addr.src_addr.ss_family == AF_IB) ? src->qkey : 0;
  866. }
  867. static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
  868. int in_len, int out_len)
  869. {
  870. struct rdma_ucm_connect cmd;
  871. struct rdma_conn_param conn_param;
  872. struct ucma_context *ctx;
  873. int ret;
  874. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  875. return -EFAULT;
  876. if (!cmd.conn_param.valid)
  877. return -EINVAL;
  878. ctx = ucma_get_ctx(file, cmd.id);
  879. if (IS_ERR(ctx))
  880. return PTR_ERR(ctx);
  881. ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
  882. ret = rdma_connect(ctx->cm_id, &conn_param);
  883. ucma_put_ctx(ctx);
  884. return ret;
  885. }
  886. static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
  887. int in_len, int out_len)
  888. {
  889. struct rdma_ucm_listen cmd;
  890. struct ucma_context *ctx;
  891. int ret;
  892. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  893. return -EFAULT;
  894. ctx = ucma_get_ctx(file, cmd.id);
  895. if (IS_ERR(ctx))
  896. return PTR_ERR(ctx);
  897. ctx->backlog = cmd.backlog > 0 && cmd.backlog < max_backlog ?
  898. cmd.backlog : max_backlog;
  899. ret = rdma_listen(ctx->cm_id, ctx->backlog);
  900. ucma_put_ctx(ctx);
  901. return ret;
  902. }
  903. static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
  904. int in_len, int out_len)
  905. {
  906. struct rdma_ucm_accept cmd;
  907. struct rdma_conn_param conn_param;
  908. struct ucma_context *ctx;
  909. int ret;
  910. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  911. return -EFAULT;
  912. ctx = ucma_get_ctx(file, cmd.id);
  913. if (IS_ERR(ctx))
  914. return PTR_ERR(ctx);
  915. if (cmd.conn_param.valid) {
  916. ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
  917. mutex_lock(&file->mut);
  918. ret = rdma_accept(ctx->cm_id, &conn_param);
  919. if (!ret)
  920. ctx->uid = cmd.uid;
  921. mutex_unlock(&file->mut);
  922. } else
  923. ret = rdma_accept(ctx->cm_id, NULL);
  924. ucma_put_ctx(ctx);
  925. return ret;
  926. }
  927. static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
  928. int in_len, int out_len)
  929. {
  930. struct rdma_ucm_reject cmd;
  931. struct ucma_context *ctx;
  932. int ret;
  933. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  934. return -EFAULT;
  935. ctx = ucma_get_ctx(file, cmd.id);
  936. if (IS_ERR(ctx))
  937. return PTR_ERR(ctx);
  938. ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
  939. ucma_put_ctx(ctx);
  940. return ret;
  941. }
  942. static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
  943. int in_len, int out_len)
  944. {
  945. struct rdma_ucm_disconnect cmd;
  946. struct ucma_context *ctx;
  947. int ret;
  948. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  949. return -EFAULT;
  950. ctx = ucma_get_ctx(file, cmd.id);
  951. if (IS_ERR(ctx))
  952. return PTR_ERR(ctx);
  953. ret = rdma_disconnect(ctx->cm_id);
  954. ucma_put_ctx(ctx);
  955. return ret;
  956. }
  957. static ssize_t ucma_init_qp_attr(struct ucma_file *file,
  958. const char __user *inbuf,
  959. int in_len, int out_len)
  960. {
  961. struct rdma_ucm_init_qp_attr cmd;
  962. struct ib_uverbs_qp_attr resp;
  963. struct ucma_context *ctx;
  964. struct ib_qp_attr qp_attr;
  965. int ret;
  966. if (out_len < sizeof(resp))
  967. return -ENOSPC;
  968. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  969. return -EFAULT;
  970. if (cmd.qp_state > IB_QPS_ERR)
  971. return -EINVAL;
  972. ctx = ucma_get_ctx(file, cmd.id);
  973. if (IS_ERR(ctx))
  974. return PTR_ERR(ctx);
  975. if (!ctx->cm_id->device) {
  976. ret = -EINVAL;
  977. goto out;
  978. }
  979. resp.qp_attr_mask = 0;
  980. memset(&qp_attr, 0, sizeof qp_attr);
  981. qp_attr.qp_state = cmd.qp_state;
  982. ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  983. if (ret)
  984. goto out;
  985. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  986. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  987. &resp, sizeof(resp)))
  988. ret = -EFAULT;
  989. out:
  990. ucma_put_ctx(ctx);
  991. return ret;
  992. }
  993. static int ucma_set_option_id(struct ucma_context *ctx, int optname,
  994. void *optval, size_t optlen)
  995. {
  996. int ret = 0;
  997. switch (optname) {
  998. case RDMA_OPTION_ID_TOS:
  999. if (optlen != sizeof(u8)) {
  1000. ret = -EINVAL;
  1001. break;
  1002. }
  1003. rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
  1004. break;
  1005. case RDMA_OPTION_ID_REUSEADDR:
  1006. if (optlen != sizeof(int)) {
  1007. ret = -EINVAL;
  1008. break;
  1009. }
  1010. ret = rdma_set_reuseaddr(ctx->cm_id, *((int *) optval) ? 1 : 0);
  1011. break;
  1012. case RDMA_OPTION_ID_AFONLY:
  1013. if (optlen != sizeof(int)) {
  1014. ret = -EINVAL;
  1015. break;
  1016. }
  1017. ret = rdma_set_afonly(ctx->cm_id, *((int *) optval) ? 1 : 0);
  1018. break;
  1019. default:
  1020. ret = -ENOSYS;
  1021. }
  1022. return ret;
  1023. }
  1024. static int ucma_set_ib_path(struct ucma_context *ctx,
  1025. struct ib_path_rec_data *path_data, size_t optlen)
  1026. {
  1027. struct ib_sa_path_rec sa_path;
  1028. struct rdma_cm_event event;
  1029. int ret;
  1030. if (optlen % sizeof(*path_data))
  1031. return -EINVAL;
  1032. for (; optlen; optlen -= sizeof(*path_data), path_data++) {
  1033. if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY |
  1034. IB_PATH_BIDIRECTIONAL))
  1035. break;
  1036. }
  1037. if (!optlen)
  1038. return -EINVAL;
  1039. if (!ctx->cm_id->device)
  1040. return -EINVAL;
  1041. memset(&sa_path, 0, sizeof(sa_path));
  1042. ib_sa_unpack_path(path_data->path_rec, &sa_path);
  1043. ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1);
  1044. if (ret)
  1045. return ret;
  1046. memset(&event, 0, sizeof event);
  1047. event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  1048. return ucma_event_handler(ctx->cm_id, &event);
  1049. }
  1050. static int ucma_set_option_ib(struct ucma_context *ctx, int optname,
  1051. void *optval, size_t optlen)
  1052. {
  1053. int ret;
  1054. switch (optname) {
  1055. case RDMA_OPTION_IB_PATH:
  1056. ret = ucma_set_ib_path(ctx, optval, optlen);
  1057. break;
  1058. default:
  1059. ret = -ENOSYS;
  1060. }
  1061. return ret;
  1062. }
  1063. static int ucma_set_option_level(struct ucma_context *ctx, int level,
  1064. int optname, void *optval, size_t optlen)
  1065. {
  1066. int ret;
  1067. switch (level) {
  1068. case RDMA_OPTION_ID:
  1069. ret = ucma_set_option_id(ctx, optname, optval, optlen);
  1070. break;
  1071. case RDMA_OPTION_IB:
  1072. ret = ucma_set_option_ib(ctx, optname, optval, optlen);
  1073. break;
  1074. default:
  1075. ret = -ENOSYS;
  1076. }
  1077. return ret;
  1078. }
  1079. static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
  1080. int in_len, int out_len)
  1081. {
  1082. struct rdma_ucm_set_option cmd;
  1083. struct ucma_context *ctx;
  1084. void *optval;
  1085. int ret;
  1086. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1087. return -EFAULT;
  1088. ctx = ucma_get_ctx(file, cmd.id);
  1089. if (IS_ERR(ctx))
  1090. return PTR_ERR(ctx);
  1091. if (unlikely(cmd.optlen > KMALLOC_MAX_SIZE))
  1092. return -EINVAL;
  1093. optval = memdup_user((void __user *) (unsigned long) cmd.optval,
  1094. cmd.optlen);
  1095. if (IS_ERR(optval)) {
  1096. ret = PTR_ERR(optval);
  1097. goto out;
  1098. }
  1099. ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
  1100. cmd.optlen);
  1101. kfree(optval);
  1102. out:
  1103. ucma_put_ctx(ctx);
  1104. return ret;
  1105. }
  1106. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  1107. int in_len, int out_len)
  1108. {
  1109. struct rdma_ucm_notify cmd;
  1110. struct ucma_context *ctx;
  1111. int ret = -EINVAL;
  1112. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1113. return -EFAULT;
  1114. ctx = ucma_get_ctx(file, cmd.id);
  1115. if (IS_ERR(ctx))
  1116. return PTR_ERR(ctx);
  1117. if (ctx->cm_id->device)
  1118. ret = rdma_notify(ctx->cm_id, (enum ib_event_type)cmd.event);
  1119. ucma_put_ctx(ctx);
  1120. return ret;
  1121. }
  1122. static ssize_t ucma_process_join(struct ucma_file *file,
  1123. struct rdma_ucm_join_mcast *cmd, int out_len)
  1124. {
  1125. struct rdma_ucm_create_id_resp resp;
  1126. struct ucma_context *ctx;
  1127. struct ucma_multicast *mc;
  1128. struct sockaddr *addr;
  1129. int ret;
  1130. if (out_len < sizeof(resp))
  1131. return -ENOSPC;
  1132. addr = (struct sockaddr *) &cmd->addr;
  1133. if (cmd->reserved || (cmd->addr_size != rdma_addr_size(addr)))
  1134. return -EINVAL;
  1135. ctx = ucma_get_ctx(file, cmd->id);
  1136. if (IS_ERR(ctx))
  1137. return PTR_ERR(ctx);
  1138. mutex_lock(&file->mut);
  1139. mc = ucma_alloc_multicast(ctx);
  1140. if (!mc) {
  1141. ret = -ENOMEM;
  1142. goto err1;
  1143. }
  1144. mc->uid = cmd->uid;
  1145. memcpy(&mc->addr, addr, cmd->addr_size);
  1146. ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc);
  1147. if (ret)
  1148. goto err2;
  1149. resp.id = mc->id;
  1150. if (copy_to_user((void __user *)(unsigned long) cmd->response,
  1151. &resp, sizeof(resp))) {
  1152. ret = -EFAULT;
  1153. goto err3;
  1154. }
  1155. mutex_lock(&mut);
  1156. idr_replace(&multicast_idr, mc, mc->id);
  1157. mutex_unlock(&mut);
  1158. mutex_unlock(&file->mut);
  1159. ucma_put_ctx(ctx);
  1160. return 0;
  1161. err3:
  1162. rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
  1163. ucma_cleanup_mc_events(mc);
  1164. err2:
  1165. mutex_lock(&mut);
  1166. idr_remove(&multicast_idr, mc->id);
  1167. mutex_unlock(&mut);
  1168. list_del(&mc->list);
  1169. kfree(mc);
  1170. err1:
  1171. mutex_unlock(&file->mut);
  1172. ucma_put_ctx(ctx);
  1173. return ret;
  1174. }
  1175. static ssize_t ucma_join_ip_multicast(struct ucma_file *file,
  1176. const char __user *inbuf,
  1177. int in_len, int out_len)
  1178. {
  1179. struct rdma_ucm_join_ip_mcast cmd;
  1180. struct rdma_ucm_join_mcast join_cmd;
  1181. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1182. return -EFAULT;
  1183. join_cmd.response = cmd.response;
  1184. join_cmd.uid = cmd.uid;
  1185. join_cmd.id = cmd.id;
  1186. join_cmd.addr_size = rdma_addr_size_in6(&cmd.addr);
  1187. if (!join_cmd.addr_size)
  1188. return -EINVAL;
  1189. join_cmd.reserved = 0;
  1190. memcpy(&join_cmd.addr, &cmd.addr, join_cmd.addr_size);
  1191. return ucma_process_join(file, &join_cmd, out_len);
  1192. }
  1193. static ssize_t ucma_join_multicast(struct ucma_file *file,
  1194. const char __user *inbuf,
  1195. int in_len, int out_len)
  1196. {
  1197. struct rdma_ucm_join_mcast cmd;
  1198. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1199. return -EFAULT;
  1200. if (!rdma_addr_size_kss(&cmd.addr))
  1201. return -EINVAL;
  1202. return ucma_process_join(file, &cmd, out_len);
  1203. }
  1204. static ssize_t ucma_leave_multicast(struct ucma_file *file,
  1205. const char __user *inbuf,
  1206. int in_len, int out_len)
  1207. {
  1208. struct rdma_ucm_destroy_id cmd;
  1209. struct rdma_ucm_destroy_id_resp resp;
  1210. struct ucma_multicast *mc;
  1211. int ret = 0;
  1212. if (out_len < sizeof(resp))
  1213. return -ENOSPC;
  1214. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1215. return -EFAULT;
  1216. mutex_lock(&mut);
  1217. mc = idr_find(&multicast_idr, cmd.id);
  1218. if (!mc)
  1219. mc = ERR_PTR(-ENOENT);
  1220. else if (mc->ctx->file != file)
  1221. mc = ERR_PTR(-EINVAL);
  1222. else if (!atomic_inc_not_zero(&mc->ctx->ref))
  1223. mc = ERR_PTR(-ENXIO);
  1224. else
  1225. idr_remove(&multicast_idr, mc->id);
  1226. mutex_unlock(&mut);
  1227. if (IS_ERR(mc)) {
  1228. ret = PTR_ERR(mc);
  1229. goto out;
  1230. }
  1231. rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
  1232. mutex_lock(&mc->ctx->file->mut);
  1233. ucma_cleanup_mc_events(mc);
  1234. list_del(&mc->list);
  1235. mutex_unlock(&mc->ctx->file->mut);
  1236. ucma_put_ctx(mc->ctx);
  1237. resp.events_reported = mc->events_reported;
  1238. kfree(mc);
  1239. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  1240. &resp, sizeof(resp)))
  1241. ret = -EFAULT;
  1242. out:
  1243. return ret;
  1244. }
  1245. static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
  1246. {
  1247. /* Acquire mutex's based on pointer comparison to prevent deadlock. */
  1248. if (file1 < file2) {
  1249. mutex_lock(&file1->mut);
  1250. mutex_lock_nested(&file2->mut, SINGLE_DEPTH_NESTING);
  1251. } else {
  1252. mutex_lock(&file2->mut);
  1253. mutex_lock_nested(&file1->mut, SINGLE_DEPTH_NESTING);
  1254. }
  1255. }
  1256. static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
  1257. {
  1258. if (file1 < file2) {
  1259. mutex_unlock(&file2->mut);
  1260. mutex_unlock(&file1->mut);
  1261. } else {
  1262. mutex_unlock(&file1->mut);
  1263. mutex_unlock(&file2->mut);
  1264. }
  1265. }
  1266. static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
  1267. {
  1268. struct ucma_event *uevent, *tmp;
  1269. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
  1270. if (uevent->ctx == ctx)
  1271. list_move_tail(&uevent->list, &file->event_list);
  1272. }
  1273. static ssize_t ucma_migrate_id(struct ucma_file *new_file,
  1274. const char __user *inbuf,
  1275. int in_len, int out_len)
  1276. {
  1277. struct rdma_ucm_migrate_id cmd;
  1278. struct rdma_ucm_migrate_resp resp;
  1279. struct ucma_context *ctx;
  1280. struct fd f;
  1281. struct ucma_file *cur_file;
  1282. int ret = 0;
  1283. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1284. return -EFAULT;
  1285. /* Get current fd to protect against it being closed */
  1286. f = fdget(cmd.fd);
  1287. if (!f.file)
  1288. return -ENOENT;
  1289. if (f.file->f_op != &ucma_fops) {
  1290. ret = -EINVAL;
  1291. goto file_put;
  1292. }
  1293. /* Validate current fd and prevent destruction of id. */
  1294. ctx = ucma_get_ctx(f.file->private_data, cmd.id);
  1295. if (IS_ERR(ctx)) {
  1296. ret = PTR_ERR(ctx);
  1297. goto file_put;
  1298. }
  1299. cur_file = ctx->file;
  1300. if (cur_file == new_file) {
  1301. resp.events_reported = ctx->events_reported;
  1302. goto response;
  1303. }
  1304. /*
  1305. * Migrate events between fd's, maintaining order, and avoiding new
  1306. * events being added before existing events.
  1307. */
  1308. ucma_lock_files(cur_file, new_file);
  1309. mutex_lock(&mut);
  1310. list_move_tail(&ctx->list, &new_file->ctx_list);
  1311. ucma_move_events(ctx, new_file);
  1312. ctx->file = new_file;
  1313. resp.events_reported = ctx->events_reported;
  1314. mutex_unlock(&mut);
  1315. ucma_unlock_files(cur_file, new_file);
  1316. response:
  1317. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  1318. &resp, sizeof(resp)))
  1319. ret = -EFAULT;
  1320. ucma_put_ctx(ctx);
  1321. file_put:
  1322. fdput(f);
  1323. return ret;
  1324. }
  1325. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  1326. const char __user *inbuf,
  1327. int in_len, int out_len) = {
  1328. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  1329. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  1330. [RDMA_USER_CM_CMD_BIND_IP] = ucma_bind_ip,
  1331. [RDMA_USER_CM_CMD_RESOLVE_IP] = ucma_resolve_ip,
  1332. [RDMA_USER_CM_CMD_RESOLVE_ROUTE] = ucma_resolve_route,
  1333. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  1334. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  1335. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  1336. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  1337. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  1338. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  1339. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  1340. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  1341. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  1342. [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option,
  1343. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  1344. [RDMA_USER_CM_CMD_JOIN_IP_MCAST] = ucma_join_ip_multicast,
  1345. [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
  1346. [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id,
  1347. [RDMA_USER_CM_CMD_QUERY] = ucma_query,
  1348. [RDMA_USER_CM_CMD_BIND] = ucma_bind,
  1349. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  1350. [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast
  1351. };
  1352. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  1353. size_t len, loff_t *pos)
  1354. {
  1355. struct ucma_file *file = filp->private_data;
  1356. struct rdma_ucm_cmd_hdr hdr;
  1357. ssize_t ret;
  1358. if (WARN_ON_ONCE(!ib_safe_file_access(filp)))
  1359. return -EACCES;
  1360. if (len < sizeof(hdr))
  1361. return -EINVAL;
  1362. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  1363. return -EFAULT;
  1364. if (hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  1365. return -EINVAL;
  1366. hdr.cmd = array_index_nospec(hdr.cmd, ARRAY_SIZE(ucma_cmd_table));
  1367. if (hdr.in + sizeof(hdr) > len)
  1368. return -EINVAL;
  1369. if (!ucma_cmd_table[hdr.cmd])
  1370. return -ENOSYS;
  1371. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  1372. if (!ret)
  1373. ret = len;
  1374. return ret;
  1375. }
  1376. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  1377. {
  1378. struct ucma_file *file = filp->private_data;
  1379. unsigned int mask = 0;
  1380. poll_wait(filp, &file->poll_wait, wait);
  1381. if (!list_empty(&file->event_list))
  1382. mask = POLLIN | POLLRDNORM;
  1383. return mask;
  1384. }
  1385. /*
  1386. * ucma_open() does not need the BKL:
  1387. *
  1388. * - no global state is referred to;
  1389. * - there is no ioctl method to race against;
  1390. * - no further module initialization is required for open to work
  1391. * after the device is registered.
  1392. */
  1393. static int ucma_open(struct inode *inode, struct file *filp)
  1394. {
  1395. struct ucma_file *file;
  1396. file = kmalloc(sizeof *file, GFP_KERNEL);
  1397. if (!file)
  1398. return -ENOMEM;
  1399. file->close_wq = create_singlethread_workqueue("ucma_close_id");
  1400. if (!file->close_wq) {
  1401. kfree(file);
  1402. return -ENOMEM;
  1403. }
  1404. INIT_LIST_HEAD(&file->event_list);
  1405. INIT_LIST_HEAD(&file->ctx_list);
  1406. init_waitqueue_head(&file->poll_wait);
  1407. mutex_init(&file->mut);
  1408. filp->private_data = file;
  1409. file->filp = filp;
  1410. return nonseekable_open(inode, filp);
  1411. }
  1412. static int ucma_close(struct inode *inode, struct file *filp)
  1413. {
  1414. struct ucma_file *file = filp->private_data;
  1415. struct ucma_context *ctx, *tmp;
  1416. mutex_lock(&file->mut);
  1417. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  1418. ctx->destroying = 1;
  1419. mutex_unlock(&file->mut);
  1420. mutex_lock(&mut);
  1421. idr_remove(&ctx_idr, ctx->id);
  1422. mutex_unlock(&mut);
  1423. flush_workqueue(file->close_wq);
  1424. /* At that step once ctx was marked as destroying and workqueue
  1425. * was flushed we are safe from any inflights handlers that
  1426. * might put other closing task.
  1427. */
  1428. mutex_lock(&mut);
  1429. if (!ctx->closing) {
  1430. mutex_unlock(&mut);
  1431. ucma_put_ctx(ctx);
  1432. wait_for_completion(&ctx->comp);
  1433. /* rdma_destroy_id ensures that no event handlers are
  1434. * inflight for that id before releasing it.
  1435. */
  1436. rdma_destroy_id(ctx->cm_id);
  1437. } else {
  1438. mutex_unlock(&mut);
  1439. }
  1440. ucma_free_ctx(ctx);
  1441. mutex_lock(&file->mut);
  1442. }
  1443. mutex_unlock(&file->mut);
  1444. destroy_workqueue(file->close_wq);
  1445. kfree(file);
  1446. return 0;
  1447. }
  1448. static const struct file_operations ucma_fops = {
  1449. .owner = THIS_MODULE,
  1450. .open = ucma_open,
  1451. .release = ucma_close,
  1452. .write = ucma_write,
  1453. .poll = ucma_poll,
  1454. .llseek = no_llseek,
  1455. };
  1456. static struct miscdevice ucma_misc = {
  1457. .minor = MISC_DYNAMIC_MINOR,
  1458. .name = "rdma_cm",
  1459. .nodename = "infiniband/rdma_cm",
  1460. .mode = 0666,
  1461. .fops = &ucma_fops,
  1462. };
  1463. static ssize_t show_abi_version(struct device *dev,
  1464. struct device_attribute *attr,
  1465. char *buf)
  1466. {
  1467. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  1468. }
  1469. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1470. static int __init ucma_init(void)
  1471. {
  1472. int ret;
  1473. ret = misc_register(&ucma_misc);
  1474. if (ret)
  1475. return ret;
  1476. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  1477. if (ret) {
  1478. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  1479. goto err1;
  1480. }
  1481. ucma_ctl_table_hdr = register_net_sysctl(&init_net, "net/rdma_ucm", ucma_ctl_table);
  1482. if (!ucma_ctl_table_hdr) {
  1483. printk(KERN_ERR "rdma_ucm: couldn't register sysctl paths\n");
  1484. ret = -ENOMEM;
  1485. goto err2;
  1486. }
  1487. return 0;
  1488. err2:
  1489. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1490. err1:
  1491. misc_deregister(&ucma_misc);
  1492. return ret;
  1493. }
  1494. static void __exit ucma_cleanup(void)
  1495. {
  1496. unregister_net_sysctl_table(ucma_ctl_table_hdr);
  1497. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1498. misc_deregister(&ucma_misc);
  1499. idr_destroy(&ctx_idr);
  1500. idr_destroy(&multicast_idr);
  1501. }
  1502. module_init(ucma_init);
  1503. module_exit(ucma_cleanup);