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

powerpc: book3s: vas: use lock guard for mutex

use lock guards for scope based resource management of mutex.
This would make the code simpler and easier to maintain.

More details on lock guards can be found at
https://lore.kernel.org/all/20230612093537.614161713@infradead.org/T/#u

This shows the use of both guard and scoped_guard

Reviewed-by: Srikar Dronamraju <srikar@linux.ibm.com>
Signed-off-by: Shrikanth Hegde <sshegde@linux.ibm.com>
Tested-by: Venkat Rao Bagalkote <venkat88@linux.ibm.com>
Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com>
Link: https://patch.msgid.link/20250505075333.184463-5-sshegde@linux.ibm.com

authored by

Shrikanth Hegde and committed by
Madhavan Srinivasan
37989b53 56534636

+13 -19
+13 -19
arch/powerpc/platforms/book3s/vas-api.c
··· 425 425 return VM_FAULT_SIGBUS; 426 426 } 427 427 428 - mutex_lock(&txwin->task_ref.mmap_mutex); 429 428 /* 430 429 * The window may be inactive due to lost credit (Ex: core 431 430 * removal with DLPAR). If the window is active again when 432 431 * the credit is available, map the new paste address at the 433 432 * window virtual address. 434 433 */ 435 - if (txwin->status == VAS_WIN_ACTIVE) { 436 - paste_addr = cp_inst->coproc->vops->paste_addr(txwin); 437 - if (paste_addr) { 438 - fault = vmf_insert_pfn(vma, vma->vm_start, 439 - (paste_addr >> PAGE_SHIFT)); 440 - mutex_unlock(&txwin->task_ref.mmap_mutex); 441 - return fault; 434 + scoped_guard(mutex, &txwin->task_ref.mmap_mutex) { 435 + if (txwin->status == VAS_WIN_ACTIVE) { 436 + paste_addr = cp_inst->coproc->vops->paste_addr(txwin); 437 + if (paste_addr) { 438 + fault = vmf_insert_pfn(vma, vma->vm_start, 439 + (paste_addr >> PAGE_SHIFT)); 440 + return fault; 441 + } 442 442 } 443 443 } 444 - mutex_unlock(&txwin->task_ref.mmap_mutex); 445 444 446 445 /* 447 446 * Received this fault due to closing the actual window. ··· 493 494 return; 494 495 } 495 496 496 - mutex_lock(&txwin->task_ref.mmap_mutex); 497 - txwin->task_ref.vma = NULL; 498 - mutex_unlock(&txwin->task_ref.mmap_mutex); 497 + scoped_guard(mutex, &txwin->task_ref.mmap_mutex) 498 + txwin->task_ref.vma = NULL; 499 499 } 500 500 501 501 static const struct vm_operations_struct vas_vm_ops = { ··· 550 552 * close/open event and allows mmap() only when the window is 551 553 * active. 552 554 */ 553 - mutex_lock(&txwin->task_ref.mmap_mutex); 555 + guard(mutex)(&txwin->task_ref.mmap_mutex); 554 556 if (txwin->status != VAS_WIN_ACTIVE) { 555 557 pr_err("Window is not active\n"); 556 - rc = -EACCES; 557 - goto out; 558 + return -EACCES; 558 559 } 559 560 560 561 paste_addr = cp_inst->coproc->vops->paste_addr(txwin); 561 562 if (!paste_addr) { 562 563 pr_err("Window paste address failed\n"); 563 - rc = -EINVAL; 564 - goto out; 564 + return -EINVAL; 565 565 } 566 566 567 567 pfn = paste_addr >> PAGE_SHIFT; ··· 579 583 txwin->task_ref.vma = vma; 580 584 vma->vm_ops = &vas_vm_ops; 581 585 582 - out: 583 - mutex_unlock(&txwin->task_ref.mmap_mutex); 584 586 return rc; 585 587 } 586 588