ucma.c 42 KB

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