ucma.c 43 KB

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