ucma.c 46 KB

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