ucma.c 44 KB

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