Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'devlink-cleanups-and-move-devlink-health-functionality-to-separate-file'

Moshe Shemesh says:

====================
devlink: cleanups and move devlink health functionality to separate file

This patchset moves devlink health callbacks, helpers and related code
from leftover.c to new file health.c. About 1.3K LoC are moved by this
patchset, covering all devlink health functionality.

In addition this patchset includes a couple of small cleanups in devlink
health code and documentation update.
====================

Link: https://lore.kernel.org/r/1676392686-405892-1-git-send-email-moshe@nvidia.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+1374 -1332
+1 -1
include/trace/events/devlink.h
··· 88 88 __string(bus_name, devlink_to_dev(devlink)->bus->name) 89 89 __string(dev_name, dev_name(devlink_to_dev(devlink))) 90 90 __string(driver_name, devlink_to_dev(devlink)->driver->name) 91 - __string(reporter_name, msg) 91 + __string(reporter_name, reporter_name) 92 92 __string(msg, msg) 93 93 ), 94 94
+1 -1
net/devlink/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 2 3 - obj-y := leftover.o core.o netlink.o dev.o 3 + obj-y := leftover.o core.o netlink.o dev.o health.o
+16
net/devlink/devl_internal.h
··· 176 176 177 177 struct devlink_port * 178 178 devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info); 179 + struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, 180 + struct nlattr **attrs); 179 181 180 182 /* Reload */ 181 183 bool devlink_reload_actions_valid(const struct devlink_ops *ops); ··· 223 221 int devlink_nl_cmd_flash_update(struct sk_buff *skb, struct genl_info *info); 224 222 int devlink_nl_cmd_selftests_get_doit(struct sk_buff *skb, struct genl_info *info); 225 223 int devlink_nl_cmd_selftests_run(struct sk_buff *skb, struct genl_info *info); 224 + int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb, 225 + struct genl_info *info); 226 + int devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb, 227 + struct genl_info *info); 228 + int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb, 229 + struct genl_info *info); 230 + int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb, 231 + struct genl_info *info); 232 + int devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb, 233 + struct netlink_callback *cb); 234 + int devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb, 235 + struct genl_info *info); 236 + int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb, 237 + struct genl_info *info);
+1333
net/devlink/health.c
··· 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 <net/genetlink.h> 8 + #include <net/sock.h> 9 + #include <trace/events/devlink.h> 10 + #include "devl_internal.h" 11 + 12 + struct devlink_fmsg_item { 13 + struct list_head list; 14 + int attrtype; 15 + u8 nla_type; 16 + u16 len; 17 + int value[]; 18 + }; 19 + 20 + struct devlink_fmsg { 21 + struct list_head item_list; 22 + bool putting_binary; /* This flag forces enclosing of binary data 23 + * in an array brackets. It forces using 24 + * of designated API: 25 + * devlink_fmsg_binary_pair_nest_start() 26 + * devlink_fmsg_binary_pair_nest_end() 27 + */ 28 + }; 29 + 30 + static struct devlink_fmsg *devlink_fmsg_alloc(void) 31 + { 32 + struct devlink_fmsg *fmsg; 33 + 34 + fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL); 35 + if (!fmsg) 36 + return NULL; 37 + 38 + INIT_LIST_HEAD(&fmsg->item_list); 39 + 40 + return fmsg; 41 + } 42 + 43 + static void devlink_fmsg_free(struct devlink_fmsg *fmsg) 44 + { 45 + struct devlink_fmsg_item *item, *tmp; 46 + 47 + list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) { 48 + list_del(&item->list); 49 + kfree(item); 50 + } 51 + kfree(fmsg); 52 + } 53 + 54 + struct devlink_health_reporter { 55 + struct list_head list; 56 + void *priv; 57 + const struct devlink_health_reporter_ops *ops; 58 + struct devlink *devlink; 59 + struct devlink_port *devlink_port; 60 + struct devlink_fmsg *dump_fmsg; 61 + struct mutex dump_lock; /* lock parallel read/write from dump buffers */ 62 + u64 graceful_period; 63 + bool auto_recover; 64 + bool auto_dump; 65 + u8 health_state; 66 + u64 dump_ts; 67 + u64 dump_real_ts; 68 + u64 error_count; 69 + u64 recovery_count; 70 + u64 last_recovery_ts; 71 + }; 72 + 73 + void * 74 + devlink_health_reporter_priv(struct devlink_health_reporter *reporter) 75 + { 76 + return reporter->priv; 77 + } 78 + EXPORT_SYMBOL_GPL(devlink_health_reporter_priv); 79 + 80 + static struct devlink_health_reporter * 81 + __devlink_health_reporter_find_by_name(struct list_head *reporter_list, 82 + const char *reporter_name) 83 + { 84 + struct devlink_health_reporter *reporter; 85 + 86 + list_for_each_entry(reporter, reporter_list, list) 87 + if (!strcmp(reporter->ops->name, reporter_name)) 88 + return reporter; 89 + return NULL; 90 + } 91 + 92 + static struct devlink_health_reporter * 93 + devlink_health_reporter_find_by_name(struct devlink *devlink, 94 + const char *reporter_name) 95 + { 96 + return __devlink_health_reporter_find_by_name(&devlink->reporter_list, 97 + reporter_name); 98 + } 99 + 100 + static struct devlink_health_reporter * 101 + devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port, 102 + const char *reporter_name) 103 + { 104 + return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list, 105 + reporter_name); 106 + } 107 + 108 + static struct devlink_health_reporter * 109 + __devlink_health_reporter_create(struct devlink *devlink, 110 + const struct devlink_health_reporter_ops *ops, 111 + u64 graceful_period, void *priv) 112 + { 113 + struct devlink_health_reporter *reporter; 114 + 115 + if (WARN_ON(graceful_period && !ops->recover)) 116 + return ERR_PTR(-EINVAL); 117 + 118 + reporter = kzalloc(sizeof(*reporter), GFP_KERNEL); 119 + if (!reporter) 120 + return ERR_PTR(-ENOMEM); 121 + 122 + reporter->priv = priv; 123 + reporter->ops = ops; 124 + reporter->devlink = devlink; 125 + reporter->graceful_period = graceful_period; 126 + reporter->auto_recover = !!ops->recover; 127 + reporter->auto_dump = !!ops->dump; 128 + mutex_init(&reporter->dump_lock); 129 + return reporter; 130 + } 131 + 132 + /** 133 + * devl_port_health_reporter_create() - create devlink health reporter for 134 + * specified port instance 135 + * 136 + * @port: devlink_port to which health reports will relate 137 + * @ops: devlink health reporter ops 138 + * @graceful_period: min time (in msec) between recovery attempts 139 + * @priv: driver priv pointer 140 + */ 141 + struct devlink_health_reporter * 142 + devl_port_health_reporter_create(struct devlink_port *port, 143 + const struct devlink_health_reporter_ops *ops, 144 + u64 graceful_period, void *priv) 145 + { 146 + struct devlink_health_reporter *reporter; 147 + 148 + devl_assert_locked(port->devlink); 149 + 150 + if (__devlink_health_reporter_find_by_name(&port->reporter_list, 151 + ops->name)) 152 + return ERR_PTR(-EEXIST); 153 + 154 + reporter = __devlink_health_reporter_create(port->devlink, ops, 155 + graceful_period, priv); 156 + if (IS_ERR(reporter)) 157 + return reporter; 158 + 159 + reporter->devlink_port = port; 160 + list_add_tail(&reporter->list, &port->reporter_list); 161 + return reporter; 162 + } 163 + EXPORT_SYMBOL_GPL(devl_port_health_reporter_create); 164 + 165 + struct devlink_health_reporter * 166 + devlink_port_health_reporter_create(struct devlink_port *port, 167 + const struct devlink_health_reporter_ops *ops, 168 + u64 graceful_period, void *priv) 169 + { 170 + struct devlink_health_reporter *reporter; 171 + struct devlink *devlink = port->devlink; 172 + 173 + devl_lock(devlink); 174 + reporter = devl_port_health_reporter_create(port, ops, 175 + graceful_period, priv); 176 + devl_unlock(devlink); 177 + return reporter; 178 + } 179 + EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create); 180 + 181 + /** 182 + * devl_health_reporter_create - create devlink health reporter 183 + * 184 + * @devlink: devlink instance which the health reports will relate 185 + * @ops: devlink health reporter ops 186 + * @graceful_period: min time (in msec) between recovery attempts 187 + * @priv: driver priv pointer 188 + */ 189 + struct devlink_health_reporter * 190 + devl_health_reporter_create(struct devlink *devlink, 191 + const struct devlink_health_reporter_ops *ops, 192 + u64 graceful_period, void *priv) 193 + { 194 + struct devlink_health_reporter *reporter; 195 + 196 + devl_assert_locked(devlink); 197 + 198 + if (devlink_health_reporter_find_by_name(devlink, ops->name)) 199 + return ERR_PTR(-EEXIST); 200 + 201 + reporter = __devlink_health_reporter_create(devlink, ops, 202 + graceful_period, priv); 203 + if (IS_ERR(reporter)) 204 + return reporter; 205 + 206 + list_add_tail(&reporter->list, &devlink->reporter_list); 207 + return reporter; 208 + } 209 + EXPORT_SYMBOL_GPL(devl_health_reporter_create); 210 + 211 + struct devlink_health_reporter * 212 + devlink_health_reporter_create(struct devlink *devlink, 213 + const struct devlink_health_reporter_ops *ops, 214 + u64 graceful_period, void *priv) 215 + { 216 + struct devlink_health_reporter *reporter; 217 + 218 + devl_lock(devlink); 219 + reporter = devl_health_reporter_create(devlink, ops, 220 + graceful_period, priv); 221 + devl_unlock(devlink); 222 + return reporter; 223 + } 224 + EXPORT_SYMBOL_GPL(devlink_health_reporter_create); 225 + 226 + static void 227 + devlink_health_reporter_free(struct devlink_health_reporter *reporter) 228 + { 229 + mutex_destroy(&reporter->dump_lock); 230 + if (reporter->dump_fmsg) 231 + devlink_fmsg_free(reporter->dump_fmsg); 232 + kfree(reporter); 233 + } 234 + 235 + /** 236 + * devl_health_reporter_destroy() - destroy devlink health reporter 237 + * 238 + * @reporter: devlink health reporter to destroy 239 + */ 240 + void 241 + devl_health_reporter_destroy(struct devlink_health_reporter *reporter) 242 + { 243 + devl_assert_locked(reporter->devlink); 244 + 245 + list_del(&reporter->list); 246 + devlink_health_reporter_free(reporter); 247 + } 248 + EXPORT_SYMBOL_GPL(devl_health_reporter_destroy); 249 + 250 + void 251 + devlink_health_reporter_destroy(struct devlink_health_reporter *reporter) 252 + { 253 + struct devlink *devlink = reporter->devlink; 254 + 255 + devl_lock(devlink); 256 + devl_health_reporter_destroy(reporter); 257 + devl_unlock(devlink); 258 + } 259 + EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy); 260 + 261 + static int 262 + devlink_nl_health_reporter_fill(struct sk_buff *msg, 263 + struct devlink_health_reporter *reporter, 264 + enum devlink_command cmd, u32 portid, 265 + u32 seq, int flags) 266 + { 267 + struct devlink *devlink = reporter->devlink; 268 + struct nlattr *reporter_attr; 269 + void *hdr; 270 + 271 + hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); 272 + if (!hdr) 273 + return -EMSGSIZE; 274 + 275 + if (devlink_nl_put_handle(msg, devlink)) 276 + goto genlmsg_cancel; 277 + 278 + if (reporter->devlink_port) { 279 + if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index)) 280 + goto genlmsg_cancel; 281 + } 282 + reporter_attr = nla_nest_start_noflag(msg, 283 + DEVLINK_ATTR_HEALTH_REPORTER); 284 + if (!reporter_attr) 285 + goto genlmsg_cancel; 286 + if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME, 287 + reporter->ops->name)) 288 + goto reporter_nest_cancel; 289 + if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE, 290 + reporter->health_state)) 291 + goto reporter_nest_cancel; 292 + if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT, 293 + reporter->error_count, DEVLINK_ATTR_PAD)) 294 + goto reporter_nest_cancel; 295 + if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT, 296 + reporter->recovery_count, DEVLINK_ATTR_PAD)) 297 + goto reporter_nest_cancel; 298 + if (reporter->ops->recover && 299 + nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD, 300 + reporter->graceful_period, 301 + DEVLINK_ATTR_PAD)) 302 + goto reporter_nest_cancel; 303 + if (reporter->ops->recover && 304 + nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER, 305 + reporter->auto_recover)) 306 + goto reporter_nest_cancel; 307 + if (reporter->dump_fmsg && 308 + nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS, 309 + jiffies_to_msecs(reporter->dump_ts), 310 + DEVLINK_ATTR_PAD)) 311 + goto reporter_nest_cancel; 312 + if (reporter->dump_fmsg && 313 + nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS, 314 + reporter->dump_real_ts, DEVLINK_ATTR_PAD)) 315 + goto reporter_nest_cancel; 316 + if (reporter->ops->dump && 317 + nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP, 318 + reporter->auto_dump)) 319 + goto reporter_nest_cancel; 320 + 321 + nla_nest_end(msg, reporter_attr); 322 + genlmsg_end(msg, hdr); 323 + return 0; 324 + 325 + reporter_nest_cancel: 326 + nla_nest_cancel(msg, reporter_attr); 327 + genlmsg_cancel: 328 + genlmsg_cancel(msg, hdr); 329 + return -EMSGSIZE; 330 + } 331 + 332 + static struct devlink_health_reporter * 333 + devlink_health_reporter_get_from_attrs(struct devlink *devlink, 334 + struct nlattr **attrs) 335 + { 336 + struct devlink_port *devlink_port; 337 + char *reporter_name; 338 + 339 + if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]) 340 + return NULL; 341 + 342 + reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]); 343 + devlink_port = devlink_port_get_from_attrs(devlink, attrs); 344 + if (IS_ERR(devlink_port)) 345 + return devlink_health_reporter_find_by_name(devlink, 346 + reporter_name); 347 + else 348 + return devlink_port_health_reporter_find_by_name(devlink_port, 349 + reporter_name); 350 + } 351 + 352 + static struct devlink_health_reporter * 353 + devlink_health_reporter_get_from_info(struct devlink *devlink, 354 + struct genl_info *info) 355 + { 356 + return devlink_health_reporter_get_from_attrs(devlink, info->attrs); 357 + } 358 + 359 + int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb, 360 + struct genl_info *info) 361 + { 362 + struct devlink *devlink = info->user_ptr[0]; 363 + struct devlink_health_reporter *reporter; 364 + struct sk_buff *msg; 365 + int err; 366 + 367 + reporter = devlink_health_reporter_get_from_info(devlink, info); 368 + if (!reporter) 369 + return -EINVAL; 370 + 371 + msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 372 + if (!msg) 373 + return -ENOMEM; 374 + 375 + err = devlink_nl_health_reporter_fill(msg, reporter, 376 + DEVLINK_CMD_HEALTH_REPORTER_GET, 377 + info->snd_portid, info->snd_seq, 378 + 0); 379 + if (err) { 380 + nlmsg_free(msg); 381 + return err; 382 + } 383 + 384 + return genlmsg_reply(msg, info); 385 + } 386 + 387 + static int 388 + devlink_nl_cmd_health_reporter_get_dump_one(struct sk_buff *msg, 389 + struct devlink *devlink, 390 + struct netlink_callback *cb) 391 + { 392 + struct devlink_nl_dump_state *state = devlink_dump_state(cb); 393 + struct devlink_health_reporter *reporter; 394 + struct devlink_port *port; 395 + unsigned long port_index; 396 + int idx = 0; 397 + int err; 398 + 399 + list_for_each_entry(reporter, &devlink->reporter_list, list) { 400 + if (idx < state->idx) { 401 + idx++; 402 + continue; 403 + } 404 + err = devlink_nl_health_reporter_fill(msg, reporter, 405 + DEVLINK_CMD_HEALTH_REPORTER_GET, 406 + NETLINK_CB(cb->skb).portid, 407 + cb->nlh->nlmsg_seq, 408 + NLM_F_MULTI); 409 + if (err) { 410 + state->idx = idx; 411 + return err; 412 + } 413 + idx++; 414 + } 415 + xa_for_each(&devlink->ports, port_index, port) { 416 + list_for_each_entry(reporter, &port->reporter_list, list) { 417 + if (idx < state->idx) { 418 + idx++; 419 + continue; 420 + } 421 + err = devlink_nl_health_reporter_fill(msg, reporter, 422 + DEVLINK_CMD_HEALTH_REPORTER_GET, 423 + NETLINK_CB(cb->skb).portid, 424 + cb->nlh->nlmsg_seq, 425 + NLM_F_MULTI); 426 + if (err) { 427 + state->idx = idx; 428 + return err; 429 + } 430 + idx++; 431 + } 432 + } 433 + 434 + return 0; 435 + } 436 + 437 + const struct devlink_cmd devl_cmd_health_reporter_get = { 438 + .dump_one = devlink_nl_cmd_health_reporter_get_dump_one, 439 + }; 440 + 441 + int devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb, 442 + struct genl_info *info) 443 + { 444 + struct devlink *devlink = info->user_ptr[0]; 445 + struct devlink_health_reporter *reporter; 446 + 447 + reporter = devlink_health_reporter_get_from_info(devlink, info); 448 + if (!reporter) 449 + return -EINVAL; 450 + 451 + if (!reporter->ops->recover && 452 + (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] || 453 + info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) 454 + return -EOPNOTSUPP; 455 + 456 + if (!reporter->ops->dump && 457 + info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) 458 + return -EOPNOTSUPP; 459 + 460 + if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]) 461 + reporter->graceful_period = 462 + nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]); 463 + 464 + if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]) 465 + reporter->auto_recover = 466 + nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]); 467 + 468 + if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) 469 + reporter->auto_dump = 470 + nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]); 471 + 472 + return 0; 473 + } 474 + 475 + static void devlink_recover_notify(struct devlink_health_reporter *reporter, 476 + enum devlink_command cmd) 477 + { 478 + struct devlink *devlink = reporter->devlink; 479 + struct sk_buff *msg; 480 + int err; 481 + 482 + WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER); 483 + WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)); 484 + 485 + msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 486 + if (!msg) 487 + return; 488 + 489 + err = devlink_nl_health_reporter_fill(msg, reporter, cmd, 0, 0, 0); 490 + if (err) { 491 + nlmsg_free(msg); 492 + return; 493 + } 494 + 495 + genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, 496 + 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); 497 + } 498 + 499 + void 500 + devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter) 501 + { 502 + reporter->recovery_count++; 503 + reporter->last_recovery_ts = jiffies; 504 + } 505 + EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done); 506 + 507 + static int 508 + devlink_health_reporter_recover(struct devlink_health_reporter *reporter, 509 + void *priv_ctx, struct netlink_ext_ack *extack) 510 + { 511 + int err; 512 + 513 + if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY) 514 + return 0; 515 + 516 + if (!reporter->ops->recover) 517 + return -EOPNOTSUPP; 518 + 519 + err = reporter->ops->recover(reporter, priv_ctx, extack); 520 + if (err) 521 + return err; 522 + 523 + devlink_health_reporter_recovery_done(reporter); 524 + reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY; 525 + devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); 526 + 527 + return 0; 528 + } 529 + 530 + static void 531 + devlink_health_dump_clear(struct devlink_health_reporter *reporter) 532 + { 533 + if (!reporter->dump_fmsg) 534 + return; 535 + devlink_fmsg_free(reporter->dump_fmsg); 536 + reporter->dump_fmsg = NULL; 537 + } 538 + 539 + static int devlink_health_do_dump(struct devlink_health_reporter *reporter, 540 + void *priv_ctx, 541 + struct netlink_ext_ack *extack) 542 + { 543 + int err; 544 + 545 + if (!reporter->ops->dump) 546 + return 0; 547 + 548 + if (reporter->dump_fmsg) 549 + return 0; 550 + 551 + reporter->dump_fmsg = devlink_fmsg_alloc(); 552 + if (!reporter->dump_fmsg) { 553 + err = -ENOMEM; 554 + return err; 555 + } 556 + 557 + err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg); 558 + if (err) 559 + goto dump_err; 560 + 561 + err = reporter->ops->dump(reporter, reporter->dump_fmsg, 562 + priv_ctx, extack); 563 + if (err) 564 + goto dump_err; 565 + 566 + err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg); 567 + if (err) 568 + goto dump_err; 569 + 570 + reporter->dump_ts = jiffies; 571 + reporter->dump_real_ts = ktime_get_real_ns(); 572 + 573 + return 0; 574 + 575 + dump_err: 576 + devlink_health_dump_clear(reporter); 577 + return err; 578 + } 579 + 580 + int devlink_health_report(struct devlink_health_reporter *reporter, 581 + const char *msg, void *priv_ctx) 582 + { 583 + enum devlink_health_reporter_state prev_health_state; 584 + struct devlink *devlink = reporter->devlink; 585 + unsigned long recover_ts_threshold; 586 + int ret; 587 + 588 + /* write a log message of the current error */ 589 + WARN_ON(!msg); 590 + trace_devlink_health_report(devlink, reporter->ops->name, msg); 591 + reporter->error_count++; 592 + prev_health_state = reporter->health_state; 593 + reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR; 594 + devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); 595 + 596 + /* abort if the previous error wasn't recovered */ 597 + recover_ts_threshold = reporter->last_recovery_ts + 598 + msecs_to_jiffies(reporter->graceful_period); 599 + if (reporter->auto_recover && 600 + (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY || 601 + (reporter->last_recovery_ts && reporter->recovery_count && 602 + time_is_after_jiffies(recover_ts_threshold)))) { 603 + trace_devlink_health_recover_aborted(devlink, 604 + reporter->ops->name, 605 + reporter->health_state, 606 + jiffies - 607 + reporter->last_recovery_ts); 608 + return -ECANCELED; 609 + } 610 + 611 + if (reporter->auto_dump) { 612 + mutex_lock(&reporter->dump_lock); 613 + /* store current dump of current error, for later analysis */ 614 + devlink_health_do_dump(reporter, priv_ctx, NULL); 615 + mutex_unlock(&reporter->dump_lock); 616 + } 617 + 618 + if (!reporter->auto_recover) 619 + return 0; 620 + 621 + devl_lock(devlink); 622 + ret = devlink_health_reporter_recover(reporter, priv_ctx, NULL); 623 + devl_unlock(devlink); 624 + 625 + return ret; 626 + } 627 + EXPORT_SYMBOL_GPL(devlink_health_report); 628 + 629 + void 630 + devlink_health_reporter_state_update(struct devlink_health_reporter *reporter, 631 + enum devlink_health_reporter_state state) 632 + { 633 + if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY && 634 + state != DEVLINK_HEALTH_REPORTER_STATE_ERROR)) 635 + return; 636 + 637 + if (reporter->health_state == state) 638 + return; 639 + 640 + reporter->health_state = state; 641 + trace_devlink_health_reporter_state_update(reporter->devlink, 642 + reporter->ops->name, state); 643 + devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); 644 + } 645 + EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update); 646 + 647 + int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb, 648 + struct genl_info *info) 649 + { 650 + struct devlink *devlink = info->user_ptr[0]; 651 + struct devlink_health_reporter *reporter; 652 + 653 + reporter = devlink_health_reporter_get_from_info(devlink, info); 654 + if (!reporter) 655 + return -EINVAL; 656 + 657 + return devlink_health_reporter_recover(reporter, NULL, info->extack); 658 + } 659 + 660 + static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg, 661 + int attrtype) 662 + { 663 + struct devlink_fmsg_item *item; 664 + 665 + item = kzalloc(sizeof(*item), GFP_KERNEL); 666 + if (!item) 667 + return -ENOMEM; 668 + 669 + item->attrtype = attrtype; 670 + list_add_tail(&item->list, &fmsg->item_list); 671 + 672 + return 0; 673 + } 674 + 675 + int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg) 676 + { 677 + if (fmsg->putting_binary) 678 + return -EINVAL; 679 + 680 + return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START); 681 + } 682 + EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start); 683 + 684 + static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg) 685 + { 686 + if (fmsg->putting_binary) 687 + return -EINVAL; 688 + 689 + return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END); 690 + } 691 + 692 + int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg) 693 + { 694 + if (fmsg->putting_binary) 695 + return -EINVAL; 696 + 697 + return devlink_fmsg_nest_end(fmsg); 698 + } 699 + EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end); 700 + 701 + #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN) 702 + 703 + static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name) 704 + { 705 + struct devlink_fmsg_item *item; 706 + 707 + if (fmsg->putting_binary) 708 + return -EINVAL; 709 + 710 + if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE) 711 + return -EMSGSIZE; 712 + 713 + item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL); 714 + if (!item) 715 + return -ENOMEM; 716 + 717 + item->nla_type = NLA_NUL_STRING; 718 + item->len = strlen(name) + 1; 719 + item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME; 720 + memcpy(&item->value, name, item->len); 721 + list_add_tail(&item->list, &fmsg->item_list); 722 + 723 + return 0; 724 + } 725 + 726 + int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name) 727 + { 728 + int err; 729 + 730 + if (fmsg->putting_binary) 731 + return -EINVAL; 732 + 733 + err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START); 734 + if (err) 735 + return err; 736 + 737 + err = devlink_fmsg_put_name(fmsg, name); 738 + if (err) 739 + return err; 740 + 741 + return 0; 742 + } 743 + EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start); 744 + 745 + int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg) 746 + { 747 + if (fmsg->putting_binary) 748 + return -EINVAL; 749 + 750 + return devlink_fmsg_nest_end(fmsg); 751 + } 752 + EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end); 753 + 754 + int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg, 755 + const char *name) 756 + { 757 + int err; 758 + 759 + if (fmsg->putting_binary) 760 + return -EINVAL; 761 + 762 + err = devlink_fmsg_pair_nest_start(fmsg, name); 763 + if (err) 764 + return err; 765 + 766 + err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START); 767 + if (err) 768 + return err; 769 + 770 + return 0; 771 + } 772 + EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start); 773 + 774 + int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg) 775 + { 776 + int err; 777 + 778 + if (fmsg->putting_binary) 779 + return -EINVAL; 780 + 781 + err = devlink_fmsg_nest_end(fmsg); 782 + if (err) 783 + return err; 784 + 785 + err = devlink_fmsg_nest_end(fmsg); 786 + if (err) 787 + return err; 788 + 789 + return 0; 790 + } 791 + EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end); 792 + 793 + int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg, 794 + const char *name) 795 + { 796 + int err; 797 + 798 + err = devlink_fmsg_arr_pair_nest_start(fmsg, name); 799 + if (err) 800 + return err; 801 + 802 + fmsg->putting_binary = true; 803 + return err; 804 + } 805 + EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start); 806 + 807 + int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg) 808 + { 809 + if (!fmsg->putting_binary) 810 + return -EINVAL; 811 + 812 + fmsg->putting_binary = false; 813 + return devlink_fmsg_arr_pair_nest_end(fmsg); 814 + } 815 + EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end); 816 + 817 + static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg, 818 + const void *value, u16 value_len, 819 + u8 value_nla_type) 820 + { 821 + struct devlink_fmsg_item *item; 822 + 823 + if (value_len > DEVLINK_FMSG_MAX_SIZE) 824 + return -EMSGSIZE; 825 + 826 + item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL); 827 + if (!item) 828 + return -ENOMEM; 829 + 830 + item->nla_type = value_nla_type; 831 + item->len = value_len; 832 + item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; 833 + memcpy(&item->value, value, item->len); 834 + list_add_tail(&item->list, &fmsg->item_list); 835 + 836 + return 0; 837 + } 838 + 839 + static int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value) 840 + { 841 + if (fmsg->putting_binary) 842 + return -EINVAL; 843 + 844 + return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG); 845 + } 846 + 847 + static int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value) 848 + { 849 + if (fmsg->putting_binary) 850 + return -EINVAL; 851 + 852 + return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8); 853 + } 854 + 855 + int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value) 856 + { 857 + if (fmsg->putting_binary) 858 + return -EINVAL; 859 + 860 + return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32); 861 + } 862 + EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put); 863 + 864 + static int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value) 865 + { 866 + if (fmsg->putting_binary) 867 + return -EINVAL; 868 + 869 + return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64); 870 + } 871 + 872 + int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value) 873 + { 874 + if (fmsg->putting_binary) 875 + return -EINVAL; 876 + 877 + return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1, 878 + NLA_NUL_STRING); 879 + } 880 + EXPORT_SYMBOL_GPL(devlink_fmsg_string_put); 881 + 882 + int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value, 883 + u16 value_len) 884 + { 885 + if (!fmsg->putting_binary) 886 + return -EINVAL; 887 + 888 + return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY); 889 + } 890 + EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put); 891 + 892 + int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name, 893 + bool value) 894 + { 895 + int err; 896 + 897 + err = devlink_fmsg_pair_nest_start(fmsg, name); 898 + if (err) 899 + return err; 900 + 901 + err = devlink_fmsg_bool_put(fmsg, value); 902 + if (err) 903 + return err; 904 + 905 + err = devlink_fmsg_pair_nest_end(fmsg); 906 + if (err) 907 + return err; 908 + 909 + return 0; 910 + } 911 + EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put); 912 + 913 + int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name, 914 + u8 value) 915 + { 916 + int err; 917 + 918 + err = devlink_fmsg_pair_nest_start(fmsg, name); 919 + if (err) 920 + return err; 921 + 922 + err = devlink_fmsg_u8_put(fmsg, value); 923 + if (err) 924 + return err; 925 + 926 + err = devlink_fmsg_pair_nest_end(fmsg); 927 + if (err) 928 + return err; 929 + 930 + return 0; 931 + } 932 + EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put); 933 + 934 + int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name, 935 + u32 value) 936 + { 937 + int err; 938 + 939 + err = devlink_fmsg_pair_nest_start(fmsg, name); 940 + if (err) 941 + return err; 942 + 943 + err = devlink_fmsg_u32_put(fmsg, value); 944 + if (err) 945 + return err; 946 + 947 + err = devlink_fmsg_pair_nest_end(fmsg); 948 + if (err) 949 + return err; 950 + 951 + return 0; 952 + } 953 + EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put); 954 + 955 + int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name, 956 + u64 value) 957 + { 958 + int err; 959 + 960 + err = devlink_fmsg_pair_nest_start(fmsg, name); 961 + if (err) 962 + return err; 963 + 964 + err = devlink_fmsg_u64_put(fmsg, value); 965 + if (err) 966 + return err; 967 + 968 + err = devlink_fmsg_pair_nest_end(fmsg); 969 + if (err) 970 + return err; 971 + 972 + return 0; 973 + } 974 + EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put); 975 + 976 + int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name, 977 + const char *value) 978 + { 979 + int err; 980 + 981 + err = devlink_fmsg_pair_nest_start(fmsg, name); 982 + if (err) 983 + return err; 984 + 985 + err = devlink_fmsg_string_put(fmsg, value); 986 + if (err) 987 + return err; 988 + 989 + err = devlink_fmsg_pair_nest_end(fmsg); 990 + if (err) 991 + return err; 992 + 993 + return 0; 994 + } 995 + EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put); 996 + 997 + int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name, 998 + const void *value, u32 value_len) 999 + { 1000 + u32 data_size; 1001 + int end_err; 1002 + u32 offset; 1003 + int err; 1004 + 1005 + err = devlink_fmsg_binary_pair_nest_start(fmsg, name); 1006 + if (err) 1007 + return err; 1008 + 1009 + for (offset = 0; offset < value_len; offset += data_size) { 1010 + data_size = value_len - offset; 1011 + if (data_size > DEVLINK_FMSG_MAX_SIZE) 1012 + data_size = DEVLINK_FMSG_MAX_SIZE; 1013 + err = devlink_fmsg_binary_put(fmsg, value + offset, data_size); 1014 + if (err) 1015 + break; 1016 + /* Exit from loop with a break (instead of 1017 + * return) to make sure putting_binary is turned off in 1018 + * devlink_fmsg_binary_pair_nest_end 1019 + */ 1020 + } 1021 + 1022 + end_err = devlink_fmsg_binary_pair_nest_end(fmsg); 1023 + if (end_err) 1024 + err = end_err; 1025 + 1026 + return err; 1027 + } 1028 + EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put); 1029 + 1030 + static int 1031 + devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb) 1032 + { 1033 + switch (msg->nla_type) { 1034 + case NLA_FLAG: 1035 + case NLA_U8: 1036 + case NLA_U32: 1037 + case NLA_U64: 1038 + case NLA_NUL_STRING: 1039 + case NLA_BINARY: 1040 + return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE, 1041 + msg->nla_type); 1042 + default: 1043 + return -EINVAL; 1044 + } 1045 + } 1046 + 1047 + static int 1048 + devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb) 1049 + { 1050 + int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; 1051 + u8 tmp; 1052 + 1053 + switch (msg->nla_type) { 1054 + case NLA_FLAG: 1055 + /* Always provide flag data, regardless of its value */ 1056 + tmp = *(bool *)msg->value; 1057 + 1058 + return nla_put_u8(skb, attrtype, tmp); 1059 + case NLA_U8: 1060 + return nla_put_u8(skb, attrtype, *(u8 *)msg->value); 1061 + case NLA_U32: 1062 + return nla_put_u32(skb, attrtype, *(u32 *)msg->value); 1063 + case NLA_U64: 1064 + return nla_put_u64_64bit(skb, attrtype, *(u64 *)msg->value, 1065 + DEVLINK_ATTR_PAD); 1066 + case NLA_NUL_STRING: 1067 + return nla_put_string(skb, attrtype, (char *)&msg->value); 1068 + case NLA_BINARY: 1069 + return nla_put(skb, attrtype, msg->len, (void *)&msg->value); 1070 + default: 1071 + return -EINVAL; 1072 + } 1073 + } 1074 + 1075 + static int 1076 + devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb, 1077 + int *start) 1078 + { 1079 + struct devlink_fmsg_item *item; 1080 + struct nlattr *fmsg_nlattr; 1081 + int err = 0; 1082 + int i = 0; 1083 + 1084 + fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG); 1085 + if (!fmsg_nlattr) 1086 + return -EMSGSIZE; 1087 + 1088 + list_for_each_entry(item, &fmsg->item_list, list) { 1089 + if (i < *start) { 1090 + i++; 1091 + continue; 1092 + } 1093 + 1094 + switch (item->attrtype) { 1095 + case DEVLINK_ATTR_FMSG_OBJ_NEST_START: 1096 + case DEVLINK_ATTR_FMSG_PAIR_NEST_START: 1097 + case DEVLINK_ATTR_FMSG_ARR_NEST_START: 1098 + case DEVLINK_ATTR_FMSG_NEST_END: 1099 + err = nla_put_flag(skb, item->attrtype); 1100 + break; 1101 + case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA: 1102 + err = devlink_fmsg_item_fill_type(item, skb); 1103 + if (err) 1104 + break; 1105 + err = devlink_fmsg_item_fill_data(item, skb); 1106 + break; 1107 + case DEVLINK_ATTR_FMSG_OBJ_NAME: 1108 + err = nla_put_string(skb, item->attrtype, 1109 + (char *)&item->value); 1110 + break; 1111 + default: 1112 + err = -EINVAL; 1113 + break; 1114 + } 1115 + if (!err) 1116 + *start = ++i; 1117 + else 1118 + break; 1119 + } 1120 + 1121 + nla_nest_end(skb, fmsg_nlattr); 1122 + return err; 1123 + } 1124 + 1125 + static int devlink_fmsg_snd(struct devlink_fmsg *fmsg, 1126 + struct genl_info *info, 1127 + enum devlink_command cmd, int flags) 1128 + { 1129 + struct nlmsghdr *nlh; 1130 + struct sk_buff *skb; 1131 + bool last = false; 1132 + int index = 0; 1133 + void *hdr; 1134 + int err; 1135 + 1136 + while (!last) { 1137 + int tmp_index = index; 1138 + 1139 + skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 1140 + if (!skb) 1141 + return -ENOMEM; 1142 + 1143 + hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, 1144 + &devlink_nl_family, flags | NLM_F_MULTI, cmd); 1145 + if (!hdr) { 1146 + err = -EMSGSIZE; 1147 + goto nla_put_failure; 1148 + } 1149 + 1150 + err = devlink_fmsg_prepare_skb(fmsg, skb, &index); 1151 + if (!err) 1152 + last = true; 1153 + else if (err != -EMSGSIZE || tmp_index == index) 1154 + goto nla_put_failure; 1155 + 1156 + genlmsg_end(skb, hdr); 1157 + err = genlmsg_reply(skb, info); 1158 + if (err) 1159 + return err; 1160 + } 1161 + 1162 + skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 1163 + if (!skb) 1164 + return -ENOMEM; 1165 + nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, 1166 + NLMSG_DONE, 0, flags | NLM_F_MULTI); 1167 + if (!nlh) { 1168 + err = -EMSGSIZE; 1169 + goto nla_put_failure; 1170 + } 1171 + 1172 + return genlmsg_reply(skb, info); 1173 + 1174 + nla_put_failure: 1175 + nlmsg_free(skb); 1176 + return err; 1177 + } 1178 + 1179 + static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb, 1180 + struct netlink_callback *cb, 1181 + enum devlink_command cmd) 1182 + { 1183 + struct devlink_nl_dump_state *state = devlink_dump_state(cb); 1184 + int index = state->idx; 1185 + int tmp_index = index; 1186 + void *hdr; 1187 + int err; 1188 + 1189 + hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1190 + &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd); 1191 + if (!hdr) { 1192 + err = -EMSGSIZE; 1193 + goto nla_put_failure; 1194 + } 1195 + 1196 + err = devlink_fmsg_prepare_skb(fmsg, skb, &index); 1197 + if ((err && err != -EMSGSIZE) || tmp_index == index) 1198 + goto nla_put_failure; 1199 + 1200 + state->idx = index; 1201 + genlmsg_end(skb, hdr); 1202 + return skb->len; 1203 + 1204 + nla_put_failure: 1205 + genlmsg_cancel(skb, hdr); 1206 + return err; 1207 + } 1208 + 1209 + int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb, 1210 + struct genl_info *info) 1211 + { 1212 + struct devlink *devlink = info->user_ptr[0]; 1213 + struct devlink_health_reporter *reporter; 1214 + struct devlink_fmsg *fmsg; 1215 + int err; 1216 + 1217 + reporter = devlink_health_reporter_get_from_info(devlink, info); 1218 + if (!reporter) 1219 + return -EINVAL; 1220 + 1221 + if (!reporter->ops->diagnose) 1222 + return -EOPNOTSUPP; 1223 + 1224 + fmsg = devlink_fmsg_alloc(); 1225 + if (!fmsg) 1226 + return -ENOMEM; 1227 + 1228 + err = devlink_fmsg_obj_nest_start(fmsg); 1229 + if (err) 1230 + goto out; 1231 + 1232 + err = reporter->ops->diagnose(reporter, fmsg, info->extack); 1233 + if (err) 1234 + goto out; 1235 + 1236 + err = devlink_fmsg_obj_nest_end(fmsg); 1237 + if (err) 1238 + goto out; 1239 + 1240 + err = devlink_fmsg_snd(fmsg, info, 1241 + DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0); 1242 + 1243 + out: 1244 + devlink_fmsg_free(fmsg); 1245 + return err; 1246 + } 1247 + 1248 + static struct devlink_health_reporter * 1249 + devlink_health_reporter_get_from_cb(struct netlink_callback *cb) 1250 + { 1251 + const struct genl_dumpit_info *info = genl_dumpit_info(cb); 1252 + struct devlink_health_reporter *reporter; 1253 + struct nlattr **attrs = info->attrs; 1254 + struct devlink *devlink; 1255 + 1256 + devlink = devlink_get_from_attrs_lock(sock_net(cb->skb->sk), attrs); 1257 + if (IS_ERR(devlink)) 1258 + return NULL; 1259 + devl_unlock(devlink); 1260 + 1261 + reporter = devlink_health_reporter_get_from_attrs(devlink, attrs); 1262 + devlink_put(devlink); 1263 + return reporter; 1264 + } 1265 + 1266 + int devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb, 1267 + struct netlink_callback *cb) 1268 + { 1269 + struct devlink_nl_dump_state *state = devlink_dump_state(cb); 1270 + struct devlink_health_reporter *reporter; 1271 + int err; 1272 + 1273 + reporter = devlink_health_reporter_get_from_cb(cb); 1274 + if (!reporter) 1275 + return -EINVAL; 1276 + 1277 + if (!reporter->ops->dump) 1278 + return -EOPNOTSUPP; 1279 + 1280 + mutex_lock(&reporter->dump_lock); 1281 + if (!state->idx) { 1282 + err = devlink_health_do_dump(reporter, NULL, cb->extack); 1283 + if (err) 1284 + goto unlock; 1285 + state->dump_ts = reporter->dump_ts; 1286 + } 1287 + if (!reporter->dump_fmsg || state->dump_ts != reporter->dump_ts) { 1288 + NL_SET_ERR_MSG(cb->extack, "Dump trampled, please retry"); 1289 + err = -EAGAIN; 1290 + goto unlock; 1291 + } 1292 + 1293 + err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb, 1294 + DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET); 1295 + unlock: 1296 + mutex_unlock(&reporter->dump_lock); 1297 + return err; 1298 + } 1299 + 1300 + int devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb, 1301 + struct genl_info *info) 1302 + { 1303 + struct devlink *devlink = info->user_ptr[0]; 1304 + struct devlink_health_reporter *reporter; 1305 + 1306 + reporter = devlink_health_reporter_get_from_info(devlink, info); 1307 + if (!reporter) 1308 + return -EINVAL; 1309 + 1310 + if (!reporter->ops->dump) 1311 + return -EOPNOTSUPP; 1312 + 1313 + mutex_lock(&reporter->dump_lock); 1314 + devlink_health_dump_clear(reporter); 1315 + mutex_unlock(&reporter->dump_lock); 1316 + return 0; 1317 + } 1318 + 1319 + int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb, 1320 + struct genl_info *info) 1321 + { 1322 + struct devlink *devlink = info->user_ptr[0]; 1323 + struct devlink_health_reporter *reporter; 1324 + 1325 + reporter = devlink_health_reporter_get_from_info(devlink, info); 1326 + if (!reporter) 1327 + return -EINVAL; 1328 + 1329 + if (!reporter->ops->test) 1330 + return -EOPNOTSUPP; 1331 + 1332 + return reporter->ops->test(reporter, info->extack); 1333 + }
+2 -1328
net/devlink/leftover.c
··· 156 156 return xa_load(&devlink->ports, port_index); 157 157 } 158 158 159 - static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, 160 - struct nlattr **attrs) 159 + struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, 160 + struct nlattr **attrs) 161 161 { 162 162 if (attrs[DEVLINK_ATTR_PORT_INDEX]) { 163 163 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]); ··· 5370 5370 devl_unlock(devlink); 5371 5371 devlink_put(devlink); 5372 5372 return err; 5373 - } 5374 - 5375 - struct devlink_fmsg_item { 5376 - struct list_head list; 5377 - int attrtype; 5378 - u8 nla_type; 5379 - u16 len; 5380 - int value[]; 5381 - }; 5382 - 5383 - struct devlink_fmsg { 5384 - struct list_head item_list; 5385 - bool putting_binary; /* This flag forces enclosing of binary data 5386 - * in an array brackets. It forces using 5387 - * of designated API: 5388 - * devlink_fmsg_binary_pair_nest_start() 5389 - * devlink_fmsg_binary_pair_nest_end() 5390 - */ 5391 - }; 5392 - 5393 - static struct devlink_fmsg *devlink_fmsg_alloc(void) 5394 - { 5395 - struct devlink_fmsg *fmsg; 5396 - 5397 - fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL); 5398 - if (!fmsg) 5399 - return NULL; 5400 - 5401 - INIT_LIST_HEAD(&fmsg->item_list); 5402 - 5403 - return fmsg; 5404 - } 5405 - 5406 - static void devlink_fmsg_free(struct devlink_fmsg *fmsg) 5407 - { 5408 - struct devlink_fmsg_item *item, *tmp; 5409 - 5410 - list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) { 5411 - list_del(&item->list); 5412 - kfree(item); 5413 - } 5414 - kfree(fmsg); 5415 - } 5416 - 5417 - static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg, 5418 - int attrtype) 5419 - { 5420 - struct devlink_fmsg_item *item; 5421 - 5422 - item = kzalloc(sizeof(*item), GFP_KERNEL); 5423 - if (!item) 5424 - return -ENOMEM; 5425 - 5426 - item->attrtype = attrtype; 5427 - list_add_tail(&item->list, &fmsg->item_list); 5428 - 5429 - return 0; 5430 - } 5431 - 5432 - int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg) 5433 - { 5434 - if (fmsg->putting_binary) 5435 - return -EINVAL; 5436 - 5437 - return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START); 5438 - } 5439 - EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start); 5440 - 5441 - static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg) 5442 - { 5443 - if (fmsg->putting_binary) 5444 - return -EINVAL; 5445 - 5446 - return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END); 5447 - } 5448 - 5449 - int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg) 5450 - { 5451 - if (fmsg->putting_binary) 5452 - return -EINVAL; 5453 - 5454 - return devlink_fmsg_nest_end(fmsg); 5455 - } 5456 - EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end); 5457 - 5458 - #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN) 5459 - 5460 - static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name) 5461 - { 5462 - struct devlink_fmsg_item *item; 5463 - 5464 - if (fmsg->putting_binary) 5465 - return -EINVAL; 5466 - 5467 - if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE) 5468 - return -EMSGSIZE; 5469 - 5470 - item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL); 5471 - if (!item) 5472 - return -ENOMEM; 5473 - 5474 - item->nla_type = NLA_NUL_STRING; 5475 - item->len = strlen(name) + 1; 5476 - item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME; 5477 - memcpy(&item->value, name, item->len); 5478 - list_add_tail(&item->list, &fmsg->item_list); 5479 - 5480 - return 0; 5481 - } 5482 - 5483 - int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name) 5484 - { 5485 - int err; 5486 - 5487 - if (fmsg->putting_binary) 5488 - return -EINVAL; 5489 - 5490 - err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START); 5491 - if (err) 5492 - return err; 5493 - 5494 - err = devlink_fmsg_put_name(fmsg, name); 5495 - if (err) 5496 - return err; 5497 - 5498 - return 0; 5499 - } 5500 - EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start); 5501 - 5502 - int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg) 5503 - { 5504 - if (fmsg->putting_binary) 5505 - return -EINVAL; 5506 - 5507 - return devlink_fmsg_nest_end(fmsg); 5508 - } 5509 - EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end); 5510 - 5511 - int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg, 5512 - const char *name) 5513 - { 5514 - int err; 5515 - 5516 - if (fmsg->putting_binary) 5517 - return -EINVAL; 5518 - 5519 - err = devlink_fmsg_pair_nest_start(fmsg, name); 5520 - if (err) 5521 - return err; 5522 - 5523 - err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START); 5524 - if (err) 5525 - return err; 5526 - 5527 - return 0; 5528 - } 5529 - EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start); 5530 - 5531 - int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg) 5532 - { 5533 - int err; 5534 - 5535 - if (fmsg->putting_binary) 5536 - return -EINVAL; 5537 - 5538 - err = devlink_fmsg_nest_end(fmsg); 5539 - if (err) 5540 - return err; 5541 - 5542 - err = devlink_fmsg_nest_end(fmsg); 5543 - if (err) 5544 - return err; 5545 - 5546 - return 0; 5547 - } 5548 - EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end); 5549 - 5550 - int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg, 5551 - const char *name) 5552 - { 5553 - int err; 5554 - 5555 - err = devlink_fmsg_arr_pair_nest_start(fmsg, name); 5556 - if (err) 5557 - return err; 5558 - 5559 - fmsg->putting_binary = true; 5560 - return err; 5561 - } 5562 - EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start); 5563 - 5564 - int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg) 5565 - { 5566 - if (!fmsg->putting_binary) 5567 - return -EINVAL; 5568 - 5569 - fmsg->putting_binary = false; 5570 - return devlink_fmsg_arr_pair_nest_end(fmsg); 5571 - } 5572 - EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end); 5573 - 5574 - static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg, 5575 - const void *value, u16 value_len, 5576 - u8 value_nla_type) 5577 - { 5578 - struct devlink_fmsg_item *item; 5579 - 5580 - if (value_len > DEVLINK_FMSG_MAX_SIZE) 5581 - return -EMSGSIZE; 5582 - 5583 - item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL); 5584 - if (!item) 5585 - return -ENOMEM; 5586 - 5587 - item->nla_type = value_nla_type; 5588 - item->len = value_len; 5589 - item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; 5590 - memcpy(&item->value, value, item->len); 5591 - list_add_tail(&item->list, &fmsg->item_list); 5592 - 5593 - return 0; 5594 - } 5595 - 5596 - static int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value) 5597 - { 5598 - if (fmsg->putting_binary) 5599 - return -EINVAL; 5600 - 5601 - return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG); 5602 - } 5603 - 5604 - static int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value) 5605 - { 5606 - if (fmsg->putting_binary) 5607 - return -EINVAL; 5608 - 5609 - return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8); 5610 - } 5611 - 5612 - int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value) 5613 - { 5614 - if (fmsg->putting_binary) 5615 - return -EINVAL; 5616 - 5617 - return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32); 5618 - } 5619 - EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put); 5620 - 5621 - static int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value) 5622 - { 5623 - if (fmsg->putting_binary) 5624 - return -EINVAL; 5625 - 5626 - return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64); 5627 - } 5628 - 5629 - int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value) 5630 - { 5631 - if (fmsg->putting_binary) 5632 - return -EINVAL; 5633 - 5634 - return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1, 5635 - NLA_NUL_STRING); 5636 - } 5637 - EXPORT_SYMBOL_GPL(devlink_fmsg_string_put); 5638 - 5639 - int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value, 5640 - u16 value_len) 5641 - { 5642 - if (!fmsg->putting_binary) 5643 - return -EINVAL; 5644 - 5645 - return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY); 5646 - } 5647 - EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put); 5648 - 5649 - int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name, 5650 - bool value) 5651 - { 5652 - int err; 5653 - 5654 - err = devlink_fmsg_pair_nest_start(fmsg, name); 5655 - if (err) 5656 - return err; 5657 - 5658 - err = devlink_fmsg_bool_put(fmsg, value); 5659 - if (err) 5660 - return err; 5661 - 5662 - err = devlink_fmsg_pair_nest_end(fmsg); 5663 - if (err) 5664 - return err; 5665 - 5666 - return 0; 5667 - } 5668 - EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put); 5669 - 5670 - int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name, 5671 - u8 value) 5672 - { 5673 - int err; 5674 - 5675 - err = devlink_fmsg_pair_nest_start(fmsg, name); 5676 - if (err) 5677 - return err; 5678 - 5679 - err = devlink_fmsg_u8_put(fmsg, value); 5680 - if (err) 5681 - return err; 5682 - 5683 - err = devlink_fmsg_pair_nest_end(fmsg); 5684 - if (err) 5685 - return err; 5686 - 5687 - return 0; 5688 - } 5689 - EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put); 5690 - 5691 - int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name, 5692 - u32 value) 5693 - { 5694 - int err; 5695 - 5696 - err = devlink_fmsg_pair_nest_start(fmsg, name); 5697 - if (err) 5698 - return err; 5699 - 5700 - err = devlink_fmsg_u32_put(fmsg, value); 5701 - if (err) 5702 - return err; 5703 - 5704 - err = devlink_fmsg_pair_nest_end(fmsg); 5705 - if (err) 5706 - return err; 5707 - 5708 - return 0; 5709 - } 5710 - EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put); 5711 - 5712 - int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name, 5713 - u64 value) 5714 - { 5715 - int err; 5716 - 5717 - err = devlink_fmsg_pair_nest_start(fmsg, name); 5718 - if (err) 5719 - return err; 5720 - 5721 - err = devlink_fmsg_u64_put(fmsg, value); 5722 - if (err) 5723 - return err; 5724 - 5725 - err = devlink_fmsg_pair_nest_end(fmsg); 5726 - if (err) 5727 - return err; 5728 - 5729 - return 0; 5730 - } 5731 - EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put); 5732 - 5733 - int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name, 5734 - const char *value) 5735 - { 5736 - int err; 5737 - 5738 - err = devlink_fmsg_pair_nest_start(fmsg, name); 5739 - if (err) 5740 - return err; 5741 - 5742 - err = devlink_fmsg_string_put(fmsg, value); 5743 - if (err) 5744 - return err; 5745 - 5746 - err = devlink_fmsg_pair_nest_end(fmsg); 5747 - if (err) 5748 - return err; 5749 - 5750 - return 0; 5751 - } 5752 - EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put); 5753 - 5754 - int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name, 5755 - const void *value, u32 value_len) 5756 - { 5757 - u32 data_size; 5758 - int end_err; 5759 - u32 offset; 5760 - int err; 5761 - 5762 - err = devlink_fmsg_binary_pair_nest_start(fmsg, name); 5763 - if (err) 5764 - return err; 5765 - 5766 - for (offset = 0; offset < value_len; offset += data_size) { 5767 - data_size = value_len - offset; 5768 - if (data_size > DEVLINK_FMSG_MAX_SIZE) 5769 - data_size = DEVLINK_FMSG_MAX_SIZE; 5770 - err = devlink_fmsg_binary_put(fmsg, value + offset, data_size); 5771 - if (err) 5772 - break; 5773 - /* Exit from loop with a break (instead of 5774 - * return) to make sure putting_binary is turned off in 5775 - * devlink_fmsg_binary_pair_nest_end 5776 - */ 5777 - } 5778 - 5779 - end_err = devlink_fmsg_binary_pair_nest_end(fmsg); 5780 - if (end_err) 5781 - err = end_err; 5782 - 5783 - return err; 5784 - } 5785 - EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put); 5786 - 5787 - static int 5788 - devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb) 5789 - { 5790 - switch (msg->nla_type) { 5791 - case NLA_FLAG: 5792 - case NLA_U8: 5793 - case NLA_U32: 5794 - case NLA_U64: 5795 - case NLA_NUL_STRING: 5796 - case NLA_BINARY: 5797 - return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE, 5798 - msg->nla_type); 5799 - default: 5800 - return -EINVAL; 5801 - } 5802 - } 5803 - 5804 - static int 5805 - devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb) 5806 - { 5807 - int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; 5808 - u8 tmp; 5809 - 5810 - switch (msg->nla_type) { 5811 - case NLA_FLAG: 5812 - /* Always provide flag data, regardless of its value */ 5813 - tmp = *(bool *) msg->value; 5814 - 5815 - return nla_put_u8(skb, attrtype, tmp); 5816 - case NLA_U8: 5817 - return nla_put_u8(skb, attrtype, *(u8 *) msg->value); 5818 - case NLA_U32: 5819 - return nla_put_u32(skb, attrtype, *(u32 *) msg->value); 5820 - case NLA_U64: 5821 - return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value, 5822 - DEVLINK_ATTR_PAD); 5823 - case NLA_NUL_STRING: 5824 - return nla_put_string(skb, attrtype, (char *) &msg->value); 5825 - case NLA_BINARY: 5826 - return nla_put(skb, attrtype, msg->len, (void *) &msg->value); 5827 - default: 5828 - return -EINVAL; 5829 - } 5830 - } 5831 - 5832 - static int 5833 - devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb, 5834 - int *start) 5835 - { 5836 - struct devlink_fmsg_item *item; 5837 - struct nlattr *fmsg_nlattr; 5838 - int err = 0; 5839 - int i = 0; 5840 - 5841 - fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG); 5842 - if (!fmsg_nlattr) 5843 - return -EMSGSIZE; 5844 - 5845 - list_for_each_entry(item, &fmsg->item_list, list) { 5846 - if (i < *start) { 5847 - i++; 5848 - continue; 5849 - } 5850 - 5851 - switch (item->attrtype) { 5852 - case DEVLINK_ATTR_FMSG_OBJ_NEST_START: 5853 - case DEVLINK_ATTR_FMSG_PAIR_NEST_START: 5854 - case DEVLINK_ATTR_FMSG_ARR_NEST_START: 5855 - case DEVLINK_ATTR_FMSG_NEST_END: 5856 - err = nla_put_flag(skb, item->attrtype); 5857 - break; 5858 - case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA: 5859 - err = devlink_fmsg_item_fill_type(item, skb); 5860 - if (err) 5861 - break; 5862 - err = devlink_fmsg_item_fill_data(item, skb); 5863 - break; 5864 - case DEVLINK_ATTR_FMSG_OBJ_NAME: 5865 - err = nla_put_string(skb, item->attrtype, 5866 - (char *) &item->value); 5867 - break; 5868 - default: 5869 - err = -EINVAL; 5870 - break; 5871 - } 5872 - if (!err) 5873 - *start = ++i; 5874 - else 5875 - break; 5876 - } 5877 - 5878 - nla_nest_end(skb, fmsg_nlattr); 5879 - return err; 5880 - } 5881 - 5882 - static int devlink_fmsg_snd(struct devlink_fmsg *fmsg, 5883 - struct genl_info *info, 5884 - enum devlink_command cmd, int flags) 5885 - { 5886 - struct nlmsghdr *nlh; 5887 - struct sk_buff *skb; 5888 - bool last = false; 5889 - int index = 0; 5890 - void *hdr; 5891 - int err; 5892 - 5893 - while (!last) { 5894 - int tmp_index = index; 5895 - 5896 - skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 5897 - if (!skb) 5898 - return -ENOMEM; 5899 - 5900 - hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, 5901 - &devlink_nl_family, flags | NLM_F_MULTI, cmd); 5902 - if (!hdr) { 5903 - err = -EMSGSIZE; 5904 - goto nla_put_failure; 5905 - } 5906 - 5907 - err = devlink_fmsg_prepare_skb(fmsg, skb, &index); 5908 - if (!err) 5909 - last = true; 5910 - else if (err != -EMSGSIZE || tmp_index == index) 5911 - goto nla_put_failure; 5912 - 5913 - genlmsg_end(skb, hdr); 5914 - err = genlmsg_reply(skb, info); 5915 - if (err) 5916 - return err; 5917 - } 5918 - 5919 - skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 5920 - if (!skb) 5921 - return -ENOMEM; 5922 - nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq, 5923 - NLMSG_DONE, 0, flags | NLM_F_MULTI); 5924 - if (!nlh) { 5925 - err = -EMSGSIZE; 5926 - goto nla_put_failure; 5927 - } 5928 - 5929 - return genlmsg_reply(skb, info); 5930 - 5931 - nla_put_failure: 5932 - nlmsg_free(skb); 5933 - return err; 5934 - } 5935 - 5936 - static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb, 5937 - struct netlink_callback *cb, 5938 - enum devlink_command cmd) 5939 - { 5940 - struct devlink_nl_dump_state *state = devlink_dump_state(cb); 5941 - int index = state->idx; 5942 - int tmp_index = index; 5943 - void *hdr; 5944 - int err; 5945 - 5946 - hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 5947 - &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd); 5948 - if (!hdr) { 5949 - err = -EMSGSIZE; 5950 - goto nla_put_failure; 5951 - } 5952 - 5953 - err = devlink_fmsg_prepare_skb(fmsg, skb, &index); 5954 - if ((err && err != -EMSGSIZE) || tmp_index == index) 5955 - goto nla_put_failure; 5956 - 5957 - state->idx = index; 5958 - genlmsg_end(skb, hdr); 5959 - return skb->len; 5960 - 5961 - nla_put_failure: 5962 - genlmsg_cancel(skb, hdr); 5963 - return err; 5964 - } 5965 - 5966 - struct devlink_health_reporter { 5967 - struct list_head list; 5968 - void *priv; 5969 - const struct devlink_health_reporter_ops *ops; 5970 - struct devlink *devlink; 5971 - struct devlink_port *devlink_port; 5972 - struct devlink_fmsg *dump_fmsg; 5973 - struct mutex dump_lock; /* lock parallel read/write from dump buffers */ 5974 - u64 graceful_period; 5975 - bool auto_recover; 5976 - bool auto_dump; 5977 - u8 health_state; 5978 - u64 dump_ts; 5979 - u64 dump_real_ts; 5980 - u64 error_count; 5981 - u64 recovery_count; 5982 - u64 last_recovery_ts; 5983 - }; 5984 - 5985 - void * 5986 - devlink_health_reporter_priv(struct devlink_health_reporter *reporter) 5987 - { 5988 - return reporter->priv; 5989 - } 5990 - EXPORT_SYMBOL_GPL(devlink_health_reporter_priv); 5991 - 5992 - static struct devlink_health_reporter * 5993 - __devlink_health_reporter_find_by_name(struct list_head *reporter_list, 5994 - const char *reporter_name) 5995 - { 5996 - struct devlink_health_reporter *reporter; 5997 - 5998 - list_for_each_entry(reporter, reporter_list, list) 5999 - if (!strcmp(reporter->ops->name, reporter_name)) 6000 - return reporter; 6001 - return NULL; 6002 - } 6003 - 6004 - static struct devlink_health_reporter * 6005 - devlink_health_reporter_find_by_name(struct devlink *devlink, 6006 - const char *reporter_name) 6007 - { 6008 - return __devlink_health_reporter_find_by_name(&devlink->reporter_list, 6009 - reporter_name); 6010 - } 6011 - 6012 - static struct devlink_health_reporter * 6013 - devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port, 6014 - const char *reporter_name) 6015 - { 6016 - return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list, 6017 - reporter_name); 6018 - } 6019 - 6020 - static struct devlink_health_reporter * 6021 - __devlink_health_reporter_create(struct devlink *devlink, 6022 - const struct devlink_health_reporter_ops *ops, 6023 - u64 graceful_period, void *priv) 6024 - { 6025 - struct devlink_health_reporter *reporter; 6026 - 6027 - if (WARN_ON(graceful_period && !ops->recover)) 6028 - return ERR_PTR(-EINVAL); 6029 - 6030 - reporter = kzalloc(sizeof(*reporter), GFP_KERNEL); 6031 - if (!reporter) 6032 - return ERR_PTR(-ENOMEM); 6033 - 6034 - reporter->priv = priv; 6035 - reporter->ops = ops; 6036 - reporter->devlink = devlink; 6037 - reporter->graceful_period = graceful_period; 6038 - reporter->auto_recover = !!ops->recover; 6039 - reporter->auto_dump = !!ops->dump; 6040 - mutex_init(&reporter->dump_lock); 6041 - return reporter; 6042 - } 6043 - 6044 - /** 6045 - * devl_port_health_reporter_create - create devlink health reporter for 6046 - * specified port instance 6047 - * 6048 - * @port: devlink_port which should contain the new reporter 6049 - * @ops: ops 6050 - * @graceful_period: to avoid recovery loops, in msecs 6051 - * @priv: priv 6052 - */ 6053 - struct devlink_health_reporter * 6054 - devl_port_health_reporter_create(struct devlink_port *port, 6055 - const struct devlink_health_reporter_ops *ops, 6056 - u64 graceful_period, void *priv) 6057 - { 6058 - struct devlink_health_reporter *reporter; 6059 - 6060 - devl_assert_locked(port->devlink); 6061 - 6062 - if (__devlink_health_reporter_find_by_name(&port->reporter_list, 6063 - ops->name)) 6064 - return ERR_PTR(-EEXIST); 6065 - 6066 - reporter = __devlink_health_reporter_create(port->devlink, ops, 6067 - graceful_period, priv); 6068 - if (IS_ERR(reporter)) 6069 - return reporter; 6070 - 6071 - reporter->devlink_port = port; 6072 - list_add_tail(&reporter->list, &port->reporter_list); 6073 - return reporter; 6074 - } 6075 - EXPORT_SYMBOL_GPL(devl_port_health_reporter_create); 6076 - 6077 - struct devlink_health_reporter * 6078 - devlink_port_health_reporter_create(struct devlink_port *port, 6079 - const struct devlink_health_reporter_ops *ops, 6080 - u64 graceful_period, void *priv) 6081 - { 6082 - struct devlink_health_reporter *reporter; 6083 - struct devlink *devlink = port->devlink; 6084 - 6085 - devl_lock(devlink); 6086 - reporter = devl_port_health_reporter_create(port, ops, 6087 - graceful_period, priv); 6088 - devl_unlock(devlink); 6089 - return reporter; 6090 - } 6091 - EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create); 6092 - 6093 - /** 6094 - * devl_health_reporter_create - create devlink health reporter 6095 - * 6096 - * @devlink: devlink 6097 - * @ops: ops 6098 - * @graceful_period: to avoid recovery loops, in msecs 6099 - * @priv: priv 6100 - */ 6101 - struct devlink_health_reporter * 6102 - devl_health_reporter_create(struct devlink *devlink, 6103 - const struct devlink_health_reporter_ops *ops, 6104 - u64 graceful_period, void *priv) 6105 - { 6106 - struct devlink_health_reporter *reporter; 6107 - 6108 - devl_assert_locked(devlink); 6109 - 6110 - if (devlink_health_reporter_find_by_name(devlink, ops->name)) 6111 - return ERR_PTR(-EEXIST); 6112 - 6113 - reporter = __devlink_health_reporter_create(devlink, ops, 6114 - graceful_period, priv); 6115 - if (IS_ERR(reporter)) 6116 - return reporter; 6117 - 6118 - list_add_tail(&reporter->list, &devlink->reporter_list); 6119 - return reporter; 6120 - } 6121 - EXPORT_SYMBOL_GPL(devl_health_reporter_create); 6122 - 6123 - struct devlink_health_reporter * 6124 - devlink_health_reporter_create(struct devlink *devlink, 6125 - const struct devlink_health_reporter_ops *ops, 6126 - u64 graceful_period, void *priv) 6127 - { 6128 - struct devlink_health_reporter *reporter; 6129 - 6130 - devl_lock(devlink); 6131 - reporter = devl_health_reporter_create(devlink, ops, 6132 - graceful_period, priv); 6133 - devl_unlock(devlink); 6134 - return reporter; 6135 - } 6136 - EXPORT_SYMBOL_GPL(devlink_health_reporter_create); 6137 - 6138 - static void 6139 - devlink_health_reporter_free(struct devlink_health_reporter *reporter) 6140 - { 6141 - mutex_destroy(&reporter->dump_lock); 6142 - if (reporter->dump_fmsg) 6143 - devlink_fmsg_free(reporter->dump_fmsg); 6144 - kfree(reporter); 6145 - } 6146 - 6147 - /** 6148 - * devl_health_reporter_destroy - destroy devlink health reporter 6149 - * 6150 - * @reporter: devlink health reporter to destroy 6151 - */ 6152 - void 6153 - devl_health_reporter_destroy(struct devlink_health_reporter *reporter) 6154 - { 6155 - devl_assert_locked(reporter->devlink); 6156 - 6157 - list_del(&reporter->list); 6158 - devlink_health_reporter_free(reporter); 6159 - } 6160 - EXPORT_SYMBOL_GPL(devl_health_reporter_destroy); 6161 - 6162 - void 6163 - devlink_health_reporter_destroy(struct devlink_health_reporter *reporter) 6164 - { 6165 - struct devlink *devlink = reporter->devlink; 6166 - 6167 - devl_lock(devlink); 6168 - devl_health_reporter_destroy(reporter); 6169 - devl_unlock(devlink); 6170 - } 6171 - EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy); 6172 - 6173 - static int 6174 - devlink_nl_health_reporter_fill(struct sk_buff *msg, 6175 - struct devlink_health_reporter *reporter, 6176 - enum devlink_command cmd, u32 portid, 6177 - u32 seq, int flags) 6178 - { 6179 - struct devlink *devlink = reporter->devlink; 6180 - struct nlattr *reporter_attr; 6181 - void *hdr; 6182 - 6183 - hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); 6184 - if (!hdr) 6185 - return -EMSGSIZE; 6186 - 6187 - if (devlink_nl_put_handle(msg, devlink)) 6188 - goto genlmsg_cancel; 6189 - 6190 - if (reporter->devlink_port) { 6191 - if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index)) 6192 - goto genlmsg_cancel; 6193 - } 6194 - reporter_attr = nla_nest_start_noflag(msg, 6195 - DEVLINK_ATTR_HEALTH_REPORTER); 6196 - if (!reporter_attr) 6197 - goto genlmsg_cancel; 6198 - if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME, 6199 - reporter->ops->name)) 6200 - goto reporter_nest_cancel; 6201 - if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE, 6202 - reporter->health_state)) 6203 - goto reporter_nest_cancel; 6204 - if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT, 6205 - reporter->error_count, DEVLINK_ATTR_PAD)) 6206 - goto reporter_nest_cancel; 6207 - if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT, 6208 - reporter->recovery_count, DEVLINK_ATTR_PAD)) 6209 - goto reporter_nest_cancel; 6210 - if (reporter->ops->recover && 6211 - nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD, 6212 - reporter->graceful_period, 6213 - DEVLINK_ATTR_PAD)) 6214 - goto reporter_nest_cancel; 6215 - if (reporter->ops->recover && 6216 - nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER, 6217 - reporter->auto_recover)) 6218 - goto reporter_nest_cancel; 6219 - if (reporter->dump_fmsg && 6220 - nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS, 6221 - jiffies_to_msecs(reporter->dump_ts), 6222 - DEVLINK_ATTR_PAD)) 6223 - goto reporter_nest_cancel; 6224 - if (reporter->dump_fmsg && 6225 - nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS, 6226 - reporter->dump_real_ts, DEVLINK_ATTR_PAD)) 6227 - goto reporter_nest_cancel; 6228 - if (reporter->ops->dump && 6229 - nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP, 6230 - reporter->auto_dump)) 6231 - goto reporter_nest_cancel; 6232 - 6233 - nla_nest_end(msg, reporter_attr); 6234 - genlmsg_end(msg, hdr); 6235 - return 0; 6236 - 6237 - reporter_nest_cancel: 6238 - nla_nest_end(msg, reporter_attr); 6239 - genlmsg_cancel: 6240 - genlmsg_cancel(msg, hdr); 6241 - return -EMSGSIZE; 6242 - } 6243 - 6244 - static void devlink_recover_notify(struct devlink_health_reporter *reporter, 6245 - enum devlink_command cmd) 6246 - { 6247 - struct devlink *devlink = reporter->devlink; 6248 - struct sk_buff *msg; 6249 - int err; 6250 - 6251 - WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER); 6252 - WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)); 6253 - 6254 - msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 6255 - if (!msg) 6256 - return; 6257 - 6258 - err = devlink_nl_health_reporter_fill(msg, reporter, cmd, 0, 0, 0); 6259 - if (err) { 6260 - nlmsg_free(msg); 6261 - return; 6262 - } 6263 - 6264 - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, 6265 - 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); 6266 - } 6267 - 6268 - void 6269 - devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter) 6270 - { 6271 - reporter->recovery_count++; 6272 - reporter->last_recovery_ts = jiffies; 6273 - } 6274 - EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done); 6275 - 6276 - static int 6277 - devlink_health_reporter_recover(struct devlink_health_reporter *reporter, 6278 - void *priv_ctx, struct netlink_ext_ack *extack) 6279 - { 6280 - int err; 6281 - 6282 - if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY) 6283 - return 0; 6284 - 6285 - if (!reporter->ops->recover) 6286 - return -EOPNOTSUPP; 6287 - 6288 - err = reporter->ops->recover(reporter, priv_ctx, extack); 6289 - if (err) 6290 - return err; 6291 - 6292 - devlink_health_reporter_recovery_done(reporter); 6293 - reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY; 6294 - devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); 6295 - 6296 - return 0; 6297 - } 6298 - 6299 - static void 6300 - devlink_health_dump_clear(struct devlink_health_reporter *reporter) 6301 - { 6302 - if (!reporter->dump_fmsg) 6303 - return; 6304 - devlink_fmsg_free(reporter->dump_fmsg); 6305 - reporter->dump_fmsg = NULL; 6306 - } 6307 - 6308 - static int devlink_health_do_dump(struct devlink_health_reporter *reporter, 6309 - void *priv_ctx, 6310 - struct netlink_ext_ack *extack) 6311 - { 6312 - int err; 6313 - 6314 - if (!reporter->ops->dump) 6315 - return 0; 6316 - 6317 - if (reporter->dump_fmsg) 6318 - return 0; 6319 - 6320 - reporter->dump_fmsg = devlink_fmsg_alloc(); 6321 - if (!reporter->dump_fmsg) { 6322 - err = -ENOMEM; 6323 - return err; 6324 - } 6325 - 6326 - err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg); 6327 - if (err) 6328 - goto dump_err; 6329 - 6330 - err = reporter->ops->dump(reporter, reporter->dump_fmsg, 6331 - priv_ctx, extack); 6332 - if (err) 6333 - goto dump_err; 6334 - 6335 - err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg); 6336 - if (err) 6337 - goto dump_err; 6338 - 6339 - reporter->dump_ts = jiffies; 6340 - reporter->dump_real_ts = ktime_get_real_ns(); 6341 - 6342 - return 0; 6343 - 6344 - dump_err: 6345 - devlink_health_dump_clear(reporter); 6346 - return err; 6347 - } 6348 - 6349 - int devlink_health_report(struct devlink_health_reporter *reporter, 6350 - const char *msg, void *priv_ctx) 6351 - { 6352 - enum devlink_health_reporter_state prev_health_state; 6353 - struct devlink *devlink = reporter->devlink; 6354 - unsigned long recover_ts_threshold; 6355 - int ret; 6356 - 6357 - /* write a log message of the current error */ 6358 - WARN_ON(!msg); 6359 - trace_devlink_health_report(devlink, reporter->ops->name, msg); 6360 - reporter->error_count++; 6361 - prev_health_state = reporter->health_state; 6362 - reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR; 6363 - devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); 6364 - 6365 - /* abort if the previous error wasn't recovered */ 6366 - recover_ts_threshold = reporter->last_recovery_ts + 6367 - msecs_to_jiffies(reporter->graceful_period); 6368 - if (reporter->auto_recover && 6369 - (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY || 6370 - (reporter->last_recovery_ts && reporter->recovery_count && 6371 - time_is_after_jiffies(recover_ts_threshold)))) { 6372 - trace_devlink_health_recover_aborted(devlink, 6373 - reporter->ops->name, 6374 - reporter->health_state, 6375 - jiffies - 6376 - reporter->last_recovery_ts); 6377 - return -ECANCELED; 6378 - } 6379 - 6380 - if (reporter->auto_dump) { 6381 - mutex_lock(&reporter->dump_lock); 6382 - /* store current dump of current error, for later analysis */ 6383 - devlink_health_do_dump(reporter, priv_ctx, NULL); 6384 - mutex_unlock(&reporter->dump_lock); 6385 - } 6386 - 6387 - if (!reporter->auto_recover) 6388 - return 0; 6389 - 6390 - devl_lock(devlink); 6391 - ret = devlink_health_reporter_recover(reporter, priv_ctx, NULL); 6392 - devl_unlock(devlink); 6393 - 6394 - return ret; 6395 - } 6396 - EXPORT_SYMBOL_GPL(devlink_health_report); 6397 - 6398 - static struct devlink_health_reporter * 6399 - devlink_health_reporter_get_from_attrs(struct devlink *devlink, 6400 - struct nlattr **attrs) 6401 - { 6402 - struct devlink_port *devlink_port; 6403 - char *reporter_name; 6404 - 6405 - if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]) 6406 - return NULL; 6407 - 6408 - reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]); 6409 - devlink_port = devlink_port_get_from_attrs(devlink, attrs); 6410 - if (IS_ERR(devlink_port)) 6411 - return devlink_health_reporter_find_by_name(devlink, 6412 - reporter_name); 6413 - else 6414 - return devlink_port_health_reporter_find_by_name(devlink_port, 6415 - reporter_name); 6416 - } 6417 - 6418 - static struct devlink_health_reporter * 6419 - devlink_health_reporter_get_from_info(struct devlink *devlink, 6420 - struct genl_info *info) 6421 - { 6422 - return devlink_health_reporter_get_from_attrs(devlink, info->attrs); 6423 - } 6424 - 6425 - static struct devlink_health_reporter * 6426 - devlink_health_reporter_get_from_cb(struct netlink_callback *cb) 6427 - { 6428 - const struct genl_dumpit_info *info = genl_dumpit_info(cb); 6429 - struct devlink_health_reporter *reporter; 6430 - struct nlattr **attrs = info->attrs; 6431 - struct devlink *devlink; 6432 - 6433 - devlink = devlink_get_from_attrs_lock(sock_net(cb->skb->sk), attrs); 6434 - if (IS_ERR(devlink)) 6435 - return NULL; 6436 - devl_unlock(devlink); 6437 - 6438 - reporter = devlink_health_reporter_get_from_attrs(devlink, attrs); 6439 - devlink_put(devlink); 6440 - return reporter; 6441 - } 6442 - 6443 - void 6444 - devlink_health_reporter_state_update(struct devlink_health_reporter *reporter, 6445 - enum devlink_health_reporter_state state) 6446 - { 6447 - if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY && 6448 - state != DEVLINK_HEALTH_REPORTER_STATE_ERROR)) 6449 - return; 6450 - 6451 - if (reporter->health_state == state) 6452 - return; 6453 - 6454 - reporter->health_state = state; 6455 - trace_devlink_health_reporter_state_update(reporter->devlink, 6456 - reporter->ops->name, state); 6457 - devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER); 6458 - } 6459 - EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update); 6460 - 6461 - static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb, 6462 - struct genl_info *info) 6463 - { 6464 - struct devlink *devlink = info->user_ptr[0]; 6465 - struct devlink_health_reporter *reporter; 6466 - struct sk_buff *msg; 6467 - int err; 6468 - 6469 - reporter = devlink_health_reporter_get_from_info(devlink, info); 6470 - if (!reporter) 6471 - return -EINVAL; 6472 - 6473 - msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 6474 - if (!msg) 6475 - return -ENOMEM; 6476 - 6477 - err = devlink_nl_health_reporter_fill(msg, reporter, 6478 - DEVLINK_CMD_HEALTH_REPORTER_GET, 6479 - info->snd_portid, info->snd_seq, 6480 - 0); 6481 - if (err) { 6482 - nlmsg_free(msg); 6483 - return err; 6484 - } 6485 - 6486 - return genlmsg_reply(msg, info); 6487 - } 6488 - 6489 - static int 6490 - devlink_nl_cmd_health_reporter_get_dump_one(struct sk_buff *msg, 6491 - struct devlink *devlink, 6492 - struct netlink_callback *cb) 6493 - { 6494 - struct devlink_nl_dump_state *state = devlink_dump_state(cb); 6495 - struct devlink_health_reporter *reporter; 6496 - struct devlink_port *port; 6497 - unsigned long port_index; 6498 - int idx = 0; 6499 - int err; 6500 - 6501 - list_for_each_entry(reporter, &devlink->reporter_list, list) { 6502 - if (idx < state->idx) { 6503 - idx++; 6504 - continue; 6505 - } 6506 - err = devlink_nl_health_reporter_fill(msg, reporter, 6507 - DEVLINK_CMD_HEALTH_REPORTER_GET, 6508 - NETLINK_CB(cb->skb).portid, 6509 - cb->nlh->nlmsg_seq, 6510 - NLM_F_MULTI); 6511 - if (err) { 6512 - state->idx = idx; 6513 - return err; 6514 - } 6515 - idx++; 6516 - } 6517 - xa_for_each(&devlink->ports, port_index, port) { 6518 - list_for_each_entry(reporter, &port->reporter_list, list) { 6519 - if (idx < state->idx) { 6520 - idx++; 6521 - continue; 6522 - } 6523 - err = devlink_nl_health_reporter_fill(msg, reporter, 6524 - DEVLINK_CMD_HEALTH_REPORTER_GET, 6525 - NETLINK_CB(cb->skb).portid, 6526 - cb->nlh->nlmsg_seq, 6527 - NLM_F_MULTI); 6528 - if (err) { 6529 - state->idx = idx; 6530 - return err; 6531 - } 6532 - idx++; 6533 - } 6534 - } 6535 - 6536 - return 0; 6537 - } 6538 - 6539 - const struct devlink_cmd devl_cmd_health_reporter_get = { 6540 - .dump_one = devlink_nl_cmd_health_reporter_get_dump_one, 6541 - }; 6542 - 6543 - static int 6544 - devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb, 6545 - struct genl_info *info) 6546 - { 6547 - struct devlink *devlink = info->user_ptr[0]; 6548 - struct devlink_health_reporter *reporter; 6549 - 6550 - reporter = devlink_health_reporter_get_from_info(devlink, info); 6551 - if (!reporter) 6552 - return -EINVAL; 6553 - 6554 - if (!reporter->ops->recover && 6555 - (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] || 6556 - info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) 6557 - return -EOPNOTSUPP; 6558 - 6559 - if (!reporter->ops->dump && 6560 - info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) 6561 - return -EOPNOTSUPP; 6562 - 6563 - if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]) 6564 - reporter->graceful_period = 6565 - nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]); 6566 - 6567 - if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]) 6568 - reporter->auto_recover = 6569 - nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]); 6570 - 6571 - if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) 6572 - reporter->auto_dump = 6573 - nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]); 6574 - 6575 - return 0; 6576 - } 6577 - 6578 - static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb, 6579 - struct genl_info *info) 6580 - { 6581 - struct devlink *devlink = info->user_ptr[0]; 6582 - struct devlink_health_reporter *reporter; 6583 - 6584 - reporter = devlink_health_reporter_get_from_info(devlink, info); 6585 - if (!reporter) 6586 - return -EINVAL; 6587 - 6588 - return devlink_health_reporter_recover(reporter, NULL, info->extack); 6589 - } 6590 - 6591 - static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb, 6592 - struct genl_info *info) 6593 - { 6594 - struct devlink *devlink = info->user_ptr[0]; 6595 - struct devlink_health_reporter *reporter; 6596 - struct devlink_fmsg *fmsg; 6597 - int err; 6598 - 6599 - reporter = devlink_health_reporter_get_from_info(devlink, info); 6600 - if (!reporter) 6601 - return -EINVAL; 6602 - 6603 - if (!reporter->ops->diagnose) 6604 - return -EOPNOTSUPP; 6605 - 6606 - fmsg = devlink_fmsg_alloc(); 6607 - if (!fmsg) 6608 - return -ENOMEM; 6609 - 6610 - err = devlink_fmsg_obj_nest_start(fmsg); 6611 - if (err) 6612 - goto out; 6613 - 6614 - err = reporter->ops->diagnose(reporter, fmsg, info->extack); 6615 - if (err) 6616 - goto out; 6617 - 6618 - err = devlink_fmsg_obj_nest_end(fmsg); 6619 - if (err) 6620 - goto out; 6621 - 6622 - err = devlink_fmsg_snd(fmsg, info, 6623 - DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0); 6624 - 6625 - out: 6626 - devlink_fmsg_free(fmsg); 6627 - return err; 6628 - } 6629 - 6630 - static int 6631 - devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb, 6632 - struct netlink_callback *cb) 6633 - { 6634 - struct devlink_nl_dump_state *state = devlink_dump_state(cb); 6635 - struct devlink_health_reporter *reporter; 6636 - int err; 6637 - 6638 - reporter = devlink_health_reporter_get_from_cb(cb); 6639 - if (!reporter) 6640 - return -EINVAL; 6641 - 6642 - if (!reporter->ops->dump) 6643 - return -EOPNOTSUPP; 6644 - 6645 - mutex_lock(&reporter->dump_lock); 6646 - if (!state->idx) { 6647 - err = devlink_health_do_dump(reporter, NULL, cb->extack); 6648 - if (err) 6649 - goto unlock; 6650 - state->dump_ts = reporter->dump_ts; 6651 - } 6652 - if (!reporter->dump_fmsg || state->dump_ts != reporter->dump_ts) { 6653 - NL_SET_ERR_MSG(cb->extack, "Dump trampled, please retry"); 6654 - err = -EAGAIN; 6655 - goto unlock; 6656 - } 6657 - 6658 - err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb, 6659 - DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET); 6660 - unlock: 6661 - mutex_unlock(&reporter->dump_lock); 6662 - return err; 6663 - } 6664 - 6665 - static int 6666 - devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb, 6667 - struct genl_info *info) 6668 - { 6669 - struct devlink *devlink = info->user_ptr[0]; 6670 - struct devlink_health_reporter *reporter; 6671 - 6672 - reporter = devlink_health_reporter_get_from_info(devlink, info); 6673 - if (!reporter) 6674 - return -EINVAL; 6675 - 6676 - if (!reporter->ops->dump) 6677 - return -EOPNOTSUPP; 6678 - 6679 - mutex_lock(&reporter->dump_lock); 6680 - devlink_health_dump_clear(reporter); 6681 - mutex_unlock(&reporter->dump_lock); 6682 - return 0; 6683 - } 6684 - 6685 - static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb, 6686 - struct genl_info *info) 6687 - { 6688 - struct devlink *devlink = info->user_ptr[0]; 6689 - struct devlink_health_reporter *reporter; 6690 - 6691 - reporter = devlink_health_reporter_get_from_info(devlink, info); 6692 - if (!reporter) 6693 - return -EINVAL; 6694 - 6695 - if (!reporter->ops->test) 6696 - return -EOPNOTSUPP; 6697 - 6698 - return reporter->ops->test(reporter, info->extack); 6699 5373 } 6700 5374 6701 5375 struct devlink_stats {