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

mailbox: Use guard/scoped_guard for con_mutex

Use guard and scoped_guard for con_mutex to simplify code.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Signed-off-by: Jassi Brar <jassisinghbrar@gmail.com>

authored by

Peng Fan and committed by
Jassi Brar
16da9a65 9be02247

+26 -35
+26 -35
drivers/mailbox/mailbox.c
··· 6 6 * Author: Jassi Brar <jassisinghbrar@gmail.com> 7 7 */ 8 8 9 + #include <linux/cleanup.h> 9 10 #include <linux/delay.h> 10 11 #include <linux/device.h> 11 12 #include <linux/err.h> ··· 371 370 */ 372 371 int mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl) 373 372 { 374 - int ret; 373 + guard(mutex)(&con_mutex); 375 374 376 - mutex_lock(&con_mutex); 377 - ret = __mbox_bind_client(chan, cl); 378 - mutex_unlock(&con_mutex); 379 - 380 - return ret; 375 + return __mbox_bind_client(chan, cl); 381 376 } 382 377 EXPORT_SYMBOL_GPL(mbox_bind_client); 383 378 ··· 414 417 return ERR_PTR(ret); 415 418 } 416 419 417 - mutex_lock(&con_mutex); 420 + scoped_guard(mutex, &con_mutex) { 421 + chan = ERR_PTR(-EPROBE_DEFER); 422 + list_for_each_entry(mbox, &mbox_cons, node) 423 + if (mbox->dev->of_node == spec.np) { 424 + chan = mbox->of_xlate(mbox, &spec); 425 + if (!IS_ERR(chan)) 426 + break; 427 + } 418 428 419 - chan = ERR_PTR(-EPROBE_DEFER); 420 - list_for_each_entry(mbox, &mbox_cons, node) 421 - if (mbox->dev->of_node == spec.np) { 422 - chan = mbox->of_xlate(mbox, &spec); 423 - if (!IS_ERR(chan)) 424 - break; 425 - } 429 + of_node_put(spec.np); 426 430 427 - of_node_put(spec.np); 431 + if (IS_ERR(chan)) 432 + return chan; 428 433 429 - if (IS_ERR(chan)) { 430 - mutex_unlock(&con_mutex); 431 - return chan; 434 + ret = __mbox_bind_client(chan, cl); 435 + if (ret) 436 + chan = ERR_PTR(ret); 432 437 } 433 438 434 - ret = __mbox_bind_client(chan, cl); 435 - if (ret) 436 - chan = ERR_PTR(ret); 437 - 438 - mutex_unlock(&con_mutex); 439 439 return chan; 440 440 } 441 441 EXPORT_SYMBOL_GPL(mbox_request_channel); ··· 541 547 if (!mbox->of_xlate) 542 548 mbox->of_xlate = of_mbox_index_xlate; 543 549 544 - mutex_lock(&con_mutex); 545 - list_add_tail(&mbox->node, &mbox_cons); 546 - mutex_unlock(&con_mutex); 550 + scoped_guard(mutex, &con_mutex) 551 + list_add_tail(&mbox->node, &mbox_cons); 547 552 548 553 return 0; 549 554 } ··· 559 566 if (!mbox) 560 567 return; 561 568 562 - mutex_lock(&con_mutex); 569 + scoped_guard(mutex, &con_mutex) { 570 + list_del(&mbox->node); 563 571 564 - list_del(&mbox->node); 572 + for (i = 0; i < mbox->num_chans; i++) 573 + mbox_free_channel(&mbox->chans[i]); 565 574 566 - for (i = 0; i < mbox->num_chans; i++) 567 - mbox_free_channel(&mbox->chans[i]); 568 - 569 - if (mbox->txdone_poll) 570 - hrtimer_cancel(&mbox->poll_hrt); 571 - 572 - mutex_unlock(&con_mutex); 575 + if (mbox->txdone_poll) 576 + hrtimer_cancel(&mbox->poll_hrt); 577 + } 573 578 } 574 579 EXPORT_SYMBOL_GPL(mbox_controller_unregister); 575 580