ucma.c 43 KB

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