region.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
  5. */
  6. #include "devl_internal.h"
  7. struct devlink_region {
  8. struct devlink *devlink;
  9. struct devlink_port *port;
  10. struct list_head list;
  11. union {
  12. const struct devlink_region_ops *ops;
  13. const struct devlink_port_region_ops *port_ops;
  14. };
  15. struct mutex snapshot_lock; /* protects snapshot_list,
  16. * max_snapshots and cur_snapshots
  17. * consistency.
  18. */
  19. struct list_head snapshot_list;
  20. u32 max_snapshots;
  21. u32 cur_snapshots;
  22. u64 size;
  23. };
  24. struct devlink_snapshot {
  25. struct list_head list;
  26. struct devlink_region *region;
  27. u8 *data;
  28. u32 id;
  29. };
  30. static struct devlink_region *
  31. devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
  32. {
  33. struct devlink_region *region;
  34. list_for_each_entry(region, &devlink->region_list, list)
  35. if (!strcmp(region->ops->name, region_name))
  36. return region;
  37. return NULL;
  38. }
  39. static struct devlink_region *
  40. devlink_port_region_get_by_name(struct devlink_port *port,
  41. const char *region_name)
  42. {
  43. struct devlink_region *region;
  44. list_for_each_entry(region, &port->region_list, list)
  45. if (!strcmp(region->ops->name, region_name))
  46. return region;
  47. return NULL;
  48. }
  49. static struct devlink_snapshot *
  50. devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
  51. {
  52. struct devlink_snapshot *snapshot;
  53. list_for_each_entry(snapshot, &region->snapshot_list, list)
  54. if (snapshot->id == id)
  55. return snapshot;
  56. return NULL;
  57. }
  58. static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
  59. struct devlink *devlink,
  60. struct devlink_snapshot *snapshot)
  61. {
  62. struct nlattr *snap_attr;
  63. int err;
  64. snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
  65. if (!snap_attr)
  66. return -EINVAL;
  67. err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
  68. if (err)
  69. goto nla_put_failure;
  70. nla_nest_end(msg, snap_attr);
  71. return 0;
  72. nla_put_failure:
  73. nla_nest_cancel(msg, snap_attr);
  74. return err;
  75. }
  76. static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
  77. struct devlink *devlink,
  78. struct devlink_region *region)
  79. {
  80. struct devlink_snapshot *snapshot;
  81. struct nlattr *snapshots_attr;
  82. int err;
  83. snapshots_attr = nla_nest_start_noflag(msg,
  84. DEVLINK_ATTR_REGION_SNAPSHOTS);
  85. if (!snapshots_attr)
  86. return -EINVAL;
  87. list_for_each_entry(snapshot, &region->snapshot_list, list) {
  88. err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
  89. if (err)
  90. goto nla_put_failure;
  91. }
  92. nla_nest_end(msg, snapshots_attr);
  93. return 0;
  94. nla_put_failure:
  95. nla_nest_cancel(msg, snapshots_attr);
  96. return err;
  97. }
  98. static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
  99. enum devlink_command cmd, u32 portid,
  100. u32 seq, int flags,
  101. struct devlink_region *region)
  102. {
  103. void *hdr;
  104. int err;
  105. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  106. if (!hdr)
  107. return -EMSGSIZE;
  108. err = devlink_nl_put_handle(msg, devlink);
  109. if (err)
  110. goto nla_put_failure;
  111. if (region->port) {
  112. err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
  113. region->port->index);
  114. if (err)
  115. goto nla_put_failure;
  116. }
  117. err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
  118. if (err)
  119. goto nla_put_failure;
  120. err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
  121. region->size,
  122. DEVLINK_ATTR_PAD);
  123. if (err)
  124. goto nla_put_failure;
  125. err = nla_put_u32(msg, DEVLINK_ATTR_REGION_MAX_SNAPSHOTS,
  126. region->max_snapshots);
  127. if (err)
  128. goto nla_put_failure;
  129. err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
  130. if (err)
  131. goto nla_put_failure;
  132. genlmsg_end(msg, hdr);
  133. return 0;
  134. nla_put_failure:
  135. genlmsg_cancel(msg, hdr);
  136. return err;
  137. }
  138. static struct sk_buff *
  139. devlink_nl_region_notify_build(struct devlink_region *region,
  140. struct devlink_snapshot *snapshot,
  141. enum devlink_command cmd, u32 portid, u32 seq)
  142. {
  143. struct devlink *devlink = region->devlink;
  144. struct sk_buff *msg;
  145. void *hdr;
  146. int err;
  147. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  148. if (!msg)
  149. return ERR_PTR(-ENOMEM);
  150. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
  151. if (!hdr) {
  152. err = -EMSGSIZE;
  153. goto out_free_msg;
  154. }
  155. err = devlink_nl_put_handle(msg, devlink);
  156. if (err)
  157. goto out_cancel_msg;
  158. if (region->port) {
  159. err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
  160. region->port->index);
  161. if (err)
  162. goto out_cancel_msg;
  163. }
  164. err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
  165. region->ops->name);
  166. if (err)
  167. goto out_cancel_msg;
  168. if (snapshot) {
  169. err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
  170. snapshot->id);
  171. if (err)
  172. goto out_cancel_msg;
  173. } else {
  174. err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
  175. region->size, DEVLINK_ATTR_PAD);
  176. if (err)
  177. goto out_cancel_msg;
  178. }
  179. genlmsg_end(msg, hdr);
  180. return msg;
  181. out_cancel_msg:
  182. genlmsg_cancel(msg, hdr);
  183. out_free_msg:
  184. nlmsg_free(msg);
  185. return ERR_PTR(err);
  186. }
  187. static void devlink_nl_region_notify(struct devlink_region *region,
  188. struct devlink_snapshot *snapshot,
  189. enum devlink_command cmd)
  190. {
  191. struct devlink *devlink = region->devlink;
  192. struct sk_buff *msg;
  193. WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
  194. if (!__devl_is_registered(devlink) || !devlink_nl_notify_need(devlink))
  195. return;
  196. msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
  197. if (IS_ERR(msg))
  198. return;
  199. devlink_nl_notify_send(devlink, msg);
  200. }
  201. void devlink_regions_notify_register(struct devlink *devlink)
  202. {
  203. struct devlink_region *region;
  204. list_for_each_entry(region, &devlink->region_list, list)
  205. devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
  206. }
  207. void devlink_regions_notify_unregister(struct devlink *devlink)
  208. {
  209. struct devlink_region *region;
  210. list_for_each_entry_reverse(region, &devlink->region_list, list)
  211. devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
  212. }
  213. /**
  214. * __devlink_snapshot_id_increment - Increment number of snapshots using an id
  215. * @devlink: devlink instance
  216. * @id: the snapshot id
  217. *
  218. * Track when a new snapshot begins using an id. Load the count for the
  219. * given id from the snapshot xarray, increment it, and store it back.
  220. *
  221. * Called when a new snapshot is created with the given id.
  222. *
  223. * The id *must* have been previously allocated by
  224. * devlink_region_snapshot_id_get().
  225. *
  226. * Returns 0 on success, or an error on failure.
  227. */
  228. static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
  229. {
  230. unsigned long count;
  231. void *p;
  232. int err;
  233. xa_lock(&devlink->snapshot_ids);
  234. p = xa_load(&devlink->snapshot_ids, id);
  235. if (WARN_ON(!p)) {
  236. err = -EINVAL;
  237. goto unlock;
  238. }
  239. if (WARN_ON(!xa_is_value(p))) {
  240. err = -EINVAL;
  241. goto unlock;
  242. }
  243. count = xa_to_value(p);
  244. count++;
  245. err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
  246. GFP_ATOMIC));
  247. unlock:
  248. xa_unlock(&devlink->snapshot_ids);
  249. return err;
  250. }
  251. /**
  252. * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
  253. * @devlink: devlink instance
  254. * @id: the snapshot id
  255. *
  256. * Track when a snapshot is deleted and stops using an id. Load the count
  257. * for the given id from the snapshot xarray, decrement it, and store it
  258. * back.
  259. *
  260. * If the count reaches zero, erase this id from the xarray, freeing it
  261. * up for future re-use by devlink_region_snapshot_id_get().
  262. *
  263. * Called when a snapshot using the given id is deleted, and when the
  264. * initial allocator of the id is finished using it.
  265. */
  266. static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
  267. {
  268. unsigned long count;
  269. void *p;
  270. xa_lock(&devlink->snapshot_ids);
  271. p = xa_load(&devlink->snapshot_ids, id);
  272. if (WARN_ON(!p))
  273. goto unlock;
  274. if (WARN_ON(!xa_is_value(p)))
  275. goto unlock;
  276. count = xa_to_value(p);
  277. if (count > 1) {
  278. count--;
  279. __xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
  280. GFP_ATOMIC);
  281. } else {
  282. /* If this was the last user, we can erase this id */
  283. __xa_erase(&devlink->snapshot_ids, id);
  284. }
  285. unlock:
  286. xa_unlock(&devlink->snapshot_ids);
  287. }
  288. /**
  289. * __devlink_snapshot_id_insert - Insert a specific snapshot ID
  290. * @devlink: devlink instance
  291. * @id: the snapshot id
  292. *
  293. * Mark the given snapshot id as used by inserting a zero value into the
  294. * snapshot xarray.
  295. *
  296. * This must be called while holding the devlink instance lock. Unlike
  297. * devlink_snapshot_id_get, the initial reference count is zero, not one.
  298. * It is expected that the id will immediately be used before
  299. * releasing the devlink instance lock.
  300. *
  301. * Returns zero on success, or an error code if the snapshot id could not
  302. * be inserted.
  303. */
  304. static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
  305. {
  306. int err;
  307. xa_lock(&devlink->snapshot_ids);
  308. if (xa_load(&devlink->snapshot_ids, id)) {
  309. xa_unlock(&devlink->snapshot_ids);
  310. return -EEXIST;
  311. }
  312. err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
  313. GFP_ATOMIC));
  314. xa_unlock(&devlink->snapshot_ids);
  315. return err;
  316. }
  317. /**
  318. * __devlink_region_snapshot_id_get - get snapshot ID
  319. * @devlink: devlink instance
  320. * @id: storage to return snapshot id
  321. *
  322. * Allocates a new snapshot id. Returns zero on success, or a negative
  323. * error on failure. Must be called while holding the devlink instance
  324. * lock.
  325. *
  326. * Snapshot IDs are tracked using an xarray which stores the number of
  327. * users of the snapshot id.
  328. *
  329. * Note that the caller of this function counts as a 'user', in order to
  330. * avoid race conditions. The caller must release its hold on the
  331. * snapshot by using devlink_region_snapshot_id_put.
  332. */
  333. static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
  334. {
  335. return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
  336. xa_limit_32b, GFP_KERNEL);
  337. }
  338. /**
  339. * __devlink_region_snapshot_create - create a new snapshot
  340. * This will add a new snapshot of a region. The snapshot
  341. * will be stored on the region struct and can be accessed
  342. * from devlink. This is useful for future analyses of snapshots.
  343. * Multiple snapshots can be created on a region.
  344. * The @snapshot_id should be obtained using the getter function.
  345. *
  346. * Must be called only while holding the region snapshot lock.
  347. *
  348. * @region: devlink region of the snapshot
  349. * @data: snapshot data
  350. * @snapshot_id: snapshot id to be created
  351. */
  352. static int
  353. __devlink_region_snapshot_create(struct devlink_region *region,
  354. u8 *data, u32 snapshot_id)
  355. {
  356. struct devlink *devlink = region->devlink;
  357. struct devlink_snapshot *snapshot;
  358. int err;
  359. lockdep_assert_held(&region->snapshot_lock);
  360. /* check if region can hold one more snapshot */
  361. if (region->cur_snapshots == region->max_snapshots)
  362. return -ENOSPC;
  363. if (devlink_region_snapshot_get_by_id(region, snapshot_id))
  364. return -EEXIST;
  365. snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
  366. if (!snapshot)
  367. return -ENOMEM;
  368. err = __devlink_snapshot_id_increment(devlink, snapshot_id);
  369. if (err)
  370. goto err_snapshot_id_increment;
  371. snapshot->id = snapshot_id;
  372. snapshot->region = region;
  373. snapshot->data = data;
  374. list_add_tail(&snapshot->list, &region->snapshot_list);
  375. region->cur_snapshots++;
  376. devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
  377. return 0;
  378. err_snapshot_id_increment:
  379. kfree(snapshot);
  380. return err;
  381. }
  382. static void devlink_region_snapshot_del(struct devlink_region *region,
  383. struct devlink_snapshot *snapshot)
  384. {
  385. struct devlink *devlink = region->devlink;
  386. lockdep_assert_held(&region->snapshot_lock);
  387. devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
  388. region->cur_snapshots--;
  389. list_del(&snapshot->list);
  390. region->ops->destructor(snapshot->data);
  391. __devlink_snapshot_id_decrement(devlink, snapshot->id);
  392. kfree(snapshot);
  393. }
  394. int devlink_nl_region_get_doit(struct sk_buff *skb, struct genl_info *info)
  395. {
  396. struct devlink *devlink = info->user_ptr[0];
  397. struct devlink_port *port = NULL;
  398. struct devlink_region *region;
  399. const char *region_name;
  400. struct sk_buff *msg;
  401. unsigned int index;
  402. int err;
  403. if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME))
  404. return -EINVAL;
  405. if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
  406. index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  407. port = devlink_port_get_by_index(devlink, index);
  408. if (!port)
  409. return -ENODEV;
  410. }
  411. region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
  412. if (port)
  413. region = devlink_port_region_get_by_name(port, region_name);
  414. else
  415. region = devlink_region_get_by_name(devlink, region_name);
  416. if (!region)
  417. return -EINVAL;
  418. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  419. if (!msg)
  420. return -ENOMEM;
  421. err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
  422. info->snd_portid, info->snd_seq, 0,
  423. region);
  424. if (err) {
  425. nlmsg_free(msg);
  426. return err;
  427. }
  428. return genlmsg_reply(msg, info);
  429. }
  430. static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
  431. struct netlink_callback *cb,
  432. struct devlink_port *port,
  433. int *idx, int start, int flags)
  434. {
  435. struct devlink_region *region;
  436. int err = 0;
  437. list_for_each_entry(region, &port->region_list, list) {
  438. if (*idx < start) {
  439. (*idx)++;
  440. continue;
  441. }
  442. err = devlink_nl_region_fill(msg, port->devlink,
  443. DEVLINK_CMD_REGION_GET,
  444. NETLINK_CB(cb->skb).portid,
  445. cb->nlh->nlmsg_seq,
  446. flags, region);
  447. if (err)
  448. goto out;
  449. (*idx)++;
  450. }
  451. out:
  452. return err;
  453. }
  454. static int devlink_nl_region_get_dump_one(struct sk_buff *msg,
  455. struct devlink *devlink,
  456. struct netlink_callback *cb,
  457. int flags)
  458. {
  459. struct devlink_nl_dump_state *state = devlink_dump_state(cb);
  460. struct devlink_region *region;
  461. struct devlink_port *port;
  462. unsigned long port_index;
  463. int idx = 0;
  464. int err;
  465. list_for_each_entry(region, &devlink->region_list, list) {
  466. if (idx < state->idx) {
  467. idx++;
  468. continue;
  469. }
  470. err = devlink_nl_region_fill(msg, devlink,
  471. DEVLINK_CMD_REGION_GET,
  472. NETLINK_CB(cb->skb).portid,
  473. cb->nlh->nlmsg_seq, flags,
  474. region);
  475. if (err) {
  476. state->idx = idx;
  477. return err;
  478. }
  479. idx++;
  480. }
  481. xa_for_each(&devlink->ports, port_index, port) {
  482. err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, &idx,
  483. state->idx, flags);
  484. if (err) {
  485. state->idx = idx;
  486. return err;
  487. }
  488. }
  489. return 0;
  490. }
  491. int devlink_nl_region_get_dumpit(struct sk_buff *skb,
  492. struct netlink_callback *cb)
  493. {
  494. return devlink_nl_dumpit(skb, cb, devlink_nl_region_get_dump_one);
  495. }
  496. int devlink_nl_region_del_doit(struct sk_buff *skb, struct genl_info *info)
  497. {
  498. struct devlink *devlink = info->user_ptr[0];
  499. struct devlink_snapshot *snapshot;
  500. struct devlink_port *port = NULL;
  501. struct devlink_region *region;
  502. const char *region_name;
  503. unsigned int index;
  504. u32 snapshot_id;
  505. if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME) ||
  506. GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_SNAPSHOT_ID))
  507. return -EINVAL;
  508. region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
  509. snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
  510. if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
  511. index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  512. port = devlink_port_get_by_index(devlink, index);
  513. if (!port)
  514. return -ENODEV;
  515. }
  516. if (port)
  517. region = devlink_port_region_get_by_name(port, region_name);
  518. else
  519. region = devlink_region_get_by_name(devlink, region_name);
  520. if (!region)
  521. return -EINVAL;
  522. mutex_lock(&region->snapshot_lock);
  523. snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
  524. if (!snapshot) {
  525. mutex_unlock(&region->snapshot_lock);
  526. return -EINVAL;
  527. }
  528. devlink_region_snapshot_del(region, snapshot);
  529. mutex_unlock(&region->snapshot_lock);
  530. return 0;
  531. }
  532. int devlink_nl_region_new_doit(struct sk_buff *skb, struct genl_info *info)
  533. {
  534. struct devlink *devlink = info->user_ptr[0];
  535. struct devlink_snapshot *snapshot;
  536. struct devlink_port *port = NULL;
  537. struct nlattr *snapshot_id_attr;
  538. struct devlink_region *region;
  539. const char *region_name;
  540. unsigned int index;
  541. u32 snapshot_id;
  542. u8 *data;
  543. int err;
  544. if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME)) {
  545. NL_SET_ERR_MSG(info->extack, "No region name provided");
  546. return -EINVAL;
  547. }
  548. region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
  549. if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
  550. index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  551. port = devlink_port_get_by_index(devlink, index);
  552. if (!port)
  553. return -ENODEV;
  554. }
  555. if (port)
  556. region = devlink_port_region_get_by_name(port, region_name);
  557. else
  558. region = devlink_region_get_by_name(devlink, region_name);
  559. if (!region) {
  560. NL_SET_ERR_MSG(info->extack, "The requested region does not exist");
  561. return -EINVAL;
  562. }
  563. if (!region->ops->snapshot) {
  564. NL_SET_ERR_MSG(info->extack, "The requested region does not support taking an immediate snapshot");
  565. return -EOPNOTSUPP;
  566. }
  567. mutex_lock(&region->snapshot_lock);
  568. if (region->cur_snapshots == region->max_snapshots) {
  569. NL_SET_ERR_MSG(info->extack, "The region has reached the maximum number of stored snapshots");
  570. err = -ENOSPC;
  571. goto unlock;
  572. }
  573. snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
  574. if (snapshot_id_attr) {
  575. snapshot_id = nla_get_u32(snapshot_id_attr);
  576. if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
  577. NL_SET_ERR_MSG(info->extack, "The requested snapshot id is already in use");
  578. err = -EEXIST;
  579. goto unlock;
  580. }
  581. err = __devlink_snapshot_id_insert(devlink, snapshot_id);
  582. if (err)
  583. goto unlock;
  584. } else {
  585. err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
  586. if (err) {
  587. NL_SET_ERR_MSG(info->extack, "Failed to allocate a new snapshot id");
  588. goto unlock;
  589. }
  590. }
  591. if (port)
  592. err = region->port_ops->snapshot(port, region->port_ops,
  593. info->extack, &data);
  594. else
  595. err = region->ops->snapshot(devlink, region->ops,
  596. info->extack, &data);
  597. if (err)
  598. goto err_snapshot_capture;
  599. err = __devlink_region_snapshot_create(region, data, snapshot_id);
  600. if (err)
  601. goto err_snapshot_create;
  602. if (!snapshot_id_attr) {
  603. struct sk_buff *msg;
  604. snapshot = devlink_region_snapshot_get_by_id(region,
  605. snapshot_id);
  606. if (WARN_ON(!snapshot)) {
  607. err = -EINVAL;
  608. goto unlock;
  609. }
  610. msg = devlink_nl_region_notify_build(region, snapshot,
  611. DEVLINK_CMD_REGION_NEW,
  612. info->snd_portid,
  613. info->snd_seq);
  614. err = PTR_ERR_OR_ZERO(msg);
  615. if (err)
  616. goto err_notify;
  617. err = genlmsg_reply(msg, info);
  618. if (err)
  619. goto err_notify;
  620. }
  621. mutex_unlock(&region->snapshot_lock);
  622. return 0;
  623. err_snapshot_create:
  624. region->ops->destructor(data);
  625. err_snapshot_capture:
  626. __devlink_snapshot_id_decrement(devlink, snapshot_id);
  627. mutex_unlock(&region->snapshot_lock);
  628. return err;
  629. err_notify:
  630. devlink_region_snapshot_del(region, snapshot);
  631. unlock:
  632. mutex_unlock(&region->snapshot_lock);
  633. return err;
  634. }
  635. static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
  636. u8 *chunk, u32 chunk_size,
  637. u64 addr)
  638. {
  639. struct nlattr *chunk_attr;
  640. int err;
  641. chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
  642. if (!chunk_attr)
  643. return -EINVAL;
  644. err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
  645. if (err)
  646. goto nla_put_failure;
  647. err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
  648. DEVLINK_ATTR_PAD);
  649. if (err)
  650. goto nla_put_failure;
  651. nla_nest_end(msg, chunk_attr);
  652. return 0;
  653. nla_put_failure:
  654. nla_nest_cancel(msg, chunk_attr);
  655. return err;
  656. }
  657. #define DEVLINK_REGION_READ_CHUNK_SIZE 256
  658. typedef int devlink_chunk_fill_t(void *cb_priv, u8 *chunk, u32 chunk_size,
  659. u64 curr_offset,
  660. struct netlink_ext_ack *extack);
  661. static int
  662. devlink_nl_region_read_fill(struct sk_buff *skb, devlink_chunk_fill_t *cb,
  663. void *cb_priv, u64 start_offset, u64 end_offset,
  664. u64 *new_offset, struct netlink_ext_ack *extack)
  665. {
  666. u64 curr_offset = start_offset;
  667. int err = 0;
  668. u8 *data;
  669. /* Allocate and re-use a single buffer */
  670. data = kmalloc(DEVLINK_REGION_READ_CHUNK_SIZE, GFP_KERNEL);
  671. if (!data)
  672. return -ENOMEM;
  673. *new_offset = start_offset;
  674. while (curr_offset < end_offset) {
  675. u32 data_size;
  676. data_size = min_t(u32, end_offset - curr_offset,
  677. DEVLINK_REGION_READ_CHUNK_SIZE);
  678. err = cb(cb_priv, data, data_size, curr_offset, extack);
  679. if (err)
  680. break;
  681. err = devlink_nl_cmd_region_read_chunk_fill(skb, data, data_size, curr_offset);
  682. if (err)
  683. break;
  684. curr_offset += data_size;
  685. }
  686. *new_offset = curr_offset;
  687. kfree(data);
  688. return err;
  689. }
  690. static int
  691. devlink_region_snapshot_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
  692. u64 curr_offset,
  693. struct netlink_ext_ack __always_unused *extack)
  694. {
  695. struct devlink_snapshot *snapshot = cb_priv;
  696. memcpy(chunk, &snapshot->data[curr_offset], chunk_size);
  697. return 0;
  698. }
  699. static int
  700. devlink_region_port_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
  701. u64 curr_offset, struct netlink_ext_ack *extack)
  702. {
  703. struct devlink_region *region = cb_priv;
  704. return region->port_ops->read(region->port, region->port_ops, extack,
  705. curr_offset, chunk_size, chunk);
  706. }
  707. static int
  708. devlink_region_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
  709. u64 curr_offset, struct netlink_ext_ack *extack)
  710. {
  711. struct devlink_region *region = cb_priv;
  712. return region->ops->read(region->devlink, region->ops, extack,
  713. curr_offset, chunk_size, chunk);
  714. }
  715. int devlink_nl_region_read_dumpit(struct sk_buff *skb,
  716. struct netlink_callback *cb)
  717. {
  718. const struct genl_dumpit_info *info = genl_dumpit_info(cb);
  719. struct devlink_nl_dump_state *state = devlink_dump_state(cb);
  720. struct nlattr *chunks_attr, *region_attr, *snapshot_attr;
  721. u64 ret_offset, start_offset, end_offset = U64_MAX;
  722. struct nlattr **attrs = info->info.attrs;
  723. struct devlink_port *port = NULL;
  724. devlink_chunk_fill_t *region_cb;
  725. struct devlink_region *region;
  726. const char *region_name;
  727. struct devlink *devlink;
  728. unsigned int index;
  729. void *region_cb_priv;
  730. void *hdr;
  731. int err;
  732. start_offset = state->start_offset;
  733. devlink = devlink_get_from_attrs_lock(sock_net(cb->skb->sk), attrs,
  734. false);
  735. if (IS_ERR(devlink))
  736. return PTR_ERR(devlink);
  737. if (!attrs[DEVLINK_ATTR_REGION_NAME]) {
  738. NL_SET_ERR_MSG(cb->extack, "No region name provided");
  739. err = -EINVAL;
  740. goto out_unlock;
  741. }
  742. if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
  743. index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
  744. port = devlink_port_get_by_index(devlink, index);
  745. if (!port) {
  746. err = -ENODEV;
  747. goto out_unlock;
  748. }
  749. }
  750. region_attr = attrs[DEVLINK_ATTR_REGION_NAME];
  751. region_name = nla_data(region_attr);
  752. if (port)
  753. region = devlink_port_region_get_by_name(port, region_name);
  754. else
  755. region = devlink_region_get_by_name(devlink, region_name);
  756. if (!region) {
  757. NL_SET_ERR_MSG_ATTR(cb->extack, region_attr, "Requested region does not exist");
  758. err = -EINVAL;
  759. goto out_unlock;
  760. }
  761. snapshot_attr = attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
  762. if (!snapshot_attr) {
  763. if (!nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) {
  764. NL_SET_ERR_MSG(cb->extack, "No snapshot id provided");
  765. err = -EINVAL;
  766. goto out_unlock;
  767. }
  768. if (!region->ops->read) {
  769. NL_SET_ERR_MSG(cb->extack, "Requested region does not support direct read");
  770. err = -EOPNOTSUPP;
  771. goto out_unlock;
  772. }
  773. if (port)
  774. region_cb = &devlink_region_port_direct_fill;
  775. else
  776. region_cb = &devlink_region_direct_fill;
  777. region_cb_priv = region;
  778. } else {
  779. struct devlink_snapshot *snapshot;
  780. u32 snapshot_id;
  781. if (nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) {
  782. NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Direct region read does not use snapshot");
  783. err = -EINVAL;
  784. goto out_unlock;
  785. }
  786. snapshot_id = nla_get_u32(snapshot_attr);
  787. snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
  788. if (!snapshot) {
  789. NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Requested snapshot does not exist");
  790. err = -EINVAL;
  791. goto out_unlock;
  792. }
  793. region_cb = &devlink_region_snapshot_fill;
  794. region_cb_priv = snapshot;
  795. }
  796. if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
  797. attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
  798. if (!start_offset)
  799. start_offset =
  800. nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
  801. end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
  802. end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
  803. }
  804. if (end_offset > region->size)
  805. end_offset = region->size;
  806. /* return 0 if there is no further data to read */
  807. if (start_offset == end_offset) {
  808. err = 0;
  809. goto out_unlock;
  810. }
  811. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  812. &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
  813. DEVLINK_CMD_REGION_READ);
  814. if (!hdr) {
  815. err = -EMSGSIZE;
  816. goto out_unlock;
  817. }
  818. err = devlink_nl_put_handle(skb, devlink);
  819. if (err)
  820. goto nla_put_failure;
  821. if (region->port) {
  822. err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
  823. region->port->index);
  824. if (err)
  825. goto nla_put_failure;
  826. }
  827. err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
  828. if (err)
  829. goto nla_put_failure;
  830. chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
  831. if (!chunks_attr) {
  832. err = -EMSGSIZE;
  833. goto nla_put_failure;
  834. }
  835. err = devlink_nl_region_read_fill(skb, region_cb, region_cb_priv,
  836. start_offset, end_offset, &ret_offset,
  837. cb->extack);
  838. if (err && err != -EMSGSIZE)
  839. goto nla_put_failure;
  840. /* Check if there was any progress done to prevent infinite loop */
  841. if (ret_offset == start_offset) {
  842. err = -EINVAL;
  843. goto nla_put_failure;
  844. }
  845. state->start_offset = ret_offset;
  846. nla_nest_end(skb, chunks_attr);
  847. genlmsg_end(skb, hdr);
  848. devl_unlock(devlink);
  849. devlink_put(devlink);
  850. return skb->len;
  851. nla_put_failure:
  852. genlmsg_cancel(skb, hdr);
  853. out_unlock:
  854. devl_unlock(devlink);
  855. devlink_put(devlink);
  856. return err;
  857. }
  858. /**
  859. * devl_region_create - create a new address region
  860. *
  861. * @devlink: devlink
  862. * @ops: region operations and name
  863. * @region_max_snapshots: Maximum supported number of snapshots for region
  864. * @region_size: size of region
  865. */
  866. struct devlink_region *devl_region_create(struct devlink *devlink,
  867. const struct devlink_region_ops *ops,
  868. u32 region_max_snapshots,
  869. u64 region_size)
  870. {
  871. struct devlink_region *region;
  872. devl_assert_locked(devlink);
  873. if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
  874. return ERR_PTR(-EINVAL);
  875. if (devlink_region_get_by_name(devlink, ops->name))
  876. return ERR_PTR(-EEXIST);
  877. region = kzalloc(sizeof(*region), GFP_KERNEL);
  878. if (!region)
  879. return ERR_PTR(-ENOMEM);
  880. region->devlink = devlink;
  881. region->max_snapshots = region_max_snapshots;
  882. region->ops = ops;
  883. region->size = region_size;
  884. INIT_LIST_HEAD(&region->snapshot_list);
  885. mutex_init(&region->snapshot_lock);
  886. list_add_tail(&region->list, &devlink->region_list);
  887. devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
  888. return region;
  889. }
  890. EXPORT_SYMBOL_GPL(devl_region_create);
  891. /**
  892. * devlink_region_create - create a new address region
  893. *
  894. * @devlink: devlink
  895. * @ops: region operations and name
  896. * @region_max_snapshots: Maximum supported number of snapshots for region
  897. * @region_size: size of region
  898. *
  899. * Context: Takes and release devlink->lock <mutex>.
  900. */
  901. struct devlink_region *
  902. devlink_region_create(struct devlink *devlink,
  903. const struct devlink_region_ops *ops,
  904. u32 region_max_snapshots, u64 region_size)
  905. {
  906. struct devlink_region *region;
  907. devl_lock(devlink);
  908. region = devl_region_create(devlink, ops, region_max_snapshots,
  909. region_size);
  910. devl_unlock(devlink);
  911. return region;
  912. }
  913. EXPORT_SYMBOL_GPL(devlink_region_create);
  914. /**
  915. * devlink_port_region_create - create a new address region for a port
  916. *
  917. * @port: devlink port
  918. * @ops: region operations and name
  919. * @region_max_snapshots: Maximum supported number of snapshots for region
  920. * @region_size: size of region
  921. *
  922. * Context: Takes and release devlink->lock <mutex>.
  923. */
  924. struct devlink_region *
  925. devlink_port_region_create(struct devlink_port *port,
  926. const struct devlink_port_region_ops *ops,
  927. u32 region_max_snapshots, u64 region_size)
  928. {
  929. struct devlink *devlink = port->devlink;
  930. struct devlink_region *region;
  931. int err = 0;
  932. ASSERT_DEVLINK_PORT_INITIALIZED(port);
  933. if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
  934. return ERR_PTR(-EINVAL);
  935. devl_lock(devlink);
  936. if (devlink_port_region_get_by_name(port, ops->name)) {
  937. err = -EEXIST;
  938. goto unlock;
  939. }
  940. region = kzalloc(sizeof(*region), GFP_KERNEL);
  941. if (!region) {
  942. err = -ENOMEM;
  943. goto unlock;
  944. }
  945. region->devlink = devlink;
  946. region->port = port;
  947. region->max_snapshots = region_max_snapshots;
  948. region->port_ops = ops;
  949. region->size = region_size;
  950. INIT_LIST_HEAD(&region->snapshot_list);
  951. mutex_init(&region->snapshot_lock);
  952. list_add_tail(&region->list, &port->region_list);
  953. devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
  954. devl_unlock(devlink);
  955. return region;
  956. unlock:
  957. devl_unlock(devlink);
  958. return ERR_PTR(err);
  959. }
  960. EXPORT_SYMBOL_GPL(devlink_port_region_create);
  961. /**
  962. * devl_region_destroy - destroy address region
  963. *
  964. * @region: devlink region to destroy
  965. */
  966. void devl_region_destroy(struct devlink_region *region)
  967. {
  968. struct devlink *devlink = region->devlink;
  969. struct devlink_snapshot *snapshot, *ts;
  970. devl_assert_locked(devlink);
  971. /* Free all snapshots of region */
  972. mutex_lock(&region->snapshot_lock);
  973. list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
  974. devlink_region_snapshot_del(region, snapshot);
  975. mutex_unlock(&region->snapshot_lock);
  976. list_del(&region->list);
  977. mutex_destroy(&region->snapshot_lock);
  978. devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
  979. kfree(region);
  980. }
  981. EXPORT_SYMBOL_GPL(devl_region_destroy);
  982. /**
  983. * devlink_region_destroy - destroy address region
  984. *
  985. * @region: devlink region to destroy
  986. *
  987. * Context: Takes and release devlink->lock <mutex>.
  988. */
  989. void devlink_region_destroy(struct devlink_region *region)
  990. {
  991. struct devlink *devlink = region->devlink;
  992. devl_lock(devlink);
  993. devl_region_destroy(region);
  994. devl_unlock(devlink);
  995. }
  996. EXPORT_SYMBOL_GPL(devlink_region_destroy);
  997. /**
  998. * devlink_region_snapshot_id_get - get snapshot ID
  999. *
  1000. * This callback should be called when adding a new snapshot,
  1001. * Driver should use the same id for multiple snapshots taken
  1002. * on multiple regions at the same time/by the same trigger.
  1003. *
  1004. * The caller of this function must use devlink_region_snapshot_id_put
  1005. * when finished creating regions using this id.
  1006. *
  1007. * Returns zero on success, or a negative error code on failure.
  1008. *
  1009. * @devlink: devlink
  1010. * @id: storage to return id
  1011. */
  1012. int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
  1013. {
  1014. return __devlink_region_snapshot_id_get(devlink, id);
  1015. }
  1016. EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
  1017. /**
  1018. * devlink_region_snapshot_id_put - put snapshot ID reference
  1019. *
  1020. * This should be called by a driver after finishing creating snapshots
  1021. * with an id. Doing so ensures that the ID can later be released in the
  1022. * event that all snapshots using it have been destroyed.
  1023. *
  1024. * @devlink: devlink
  1025. * @id: id to release reference on
  1026. */
  1027. void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
  1028. {
  1029. __devlink_snapshot_id_decrement(devlink, id);
  1030. }
  1031. EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
  1032. /**
  1033. * devlink_region_snapshot_create - create a new snapshot
  1034. * This will add a new snapshot of a region. The snapshot
  1035. * will be stored on the region struct and can be accessed
  1036. * from devlink. This is useful for future analyses of snapshots.
  1037. * Multiple snapshots can be created on a region.
  1038. * The @snapshot_id should be obtained using the getter function.
  1039. *
  1040. * @region: devlink region of the snapshot
  1041. * @data: snapshot data
  1042. * @snapshot_id: snapshot id to be created
  1043. */
  1044. int devlink_region_snapshot_create(struct devlink_region *region,
  1045. u8 *data, u32 snapshot_id)
  1046. {
  1047. int err;
  1048. mutex_lock(&region->snapshot_lock);
  1049. err = __devlink_region_snapshot_create(region, data, snapshot_id);
  1050. mutex_unlock(&region->snapshot_lock);
  1051. return err;
  1052. }
  1053. EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);