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