ucma.c 44 KB

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