Skip to content

Commit 325d5c5

Browse files
bostroessermartinkpetersen
authored andcommitted
scsi: target: tcmu: Avoid holding XArray lock when calling lock_page
In tcmu_blocks_release(), lock_page() is called to prevent a race causing possible data corruption. Since lock_page() might sleep, calling it while holding XArray lock is a bug. To fix this, replace the xas_for_each() call with xa_for_each_range(). Since the latter does its own handling of XArray locking, the xas_lock() and xas_unlock() calls around the original loop are no longer necessary. The switch to xa_for_each_range() slows down the loop slightly. This is acceptable since tcmu_blocks_release() is not relevant for performance. Link: https://lore.kernel.org/r/20220517192913.21405-1-bostroesser@gmail.com Fixes: bb9b9eb ("scsi: target: tcmu: Fix possible data corruption") Reported-by: Dan Carpenter <dan.carpenter@oracle.com> Signed-off-by: Bodo Stroesser <bostroesser@gmail.com> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent d627660 commit 325d5c5

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

drivers/target/target_core_user.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,13 +1661,14 @@ static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
16611661
static u32 tcmu_blocks_release(struct tcmu_dev *udev, unsigned long first,
16621662
unsigned long last)
16631663
{
1664-
XA_STATE(xas, &udev->data_pages, first * udev->data_pages_per_blk);
16651664
struct page *page;
1665+
unsigned long dpi;
16661666
u32 pages_freed = 0;
16671667

1668-
xas_lock(&xas);
1669-
xas_for_each(&xas, page, (last + 1) * udev->data_pages_per_blk - 1) {
1670-
xas_store(&xas, NULL);
1668+
first = first * udev->data_pages_per_blk;
1669+
last = (last + 1) * udev->data_pages_per_blk - 1;
1670+
xa_for_each_range(&udev->data_pages, dpi, page, first, last) {
1671+
xa_erase(&udev->data_pages, dpi);
16711672
/*
16721673
* While reaching here there may be page faults occurring on
16731674
* the to-be-released pages. A race condition may occur if
@@ -1691,7 +1692,6 @@ static u32 tcmu_blocks_release(struct tcmu_dev *udev, unsigned long first,
16911692
__free_page(page);
16921693
pages_freed++;
16931694
}
1694-
xas_unlock(&xas);
16951695

16961696
atomic_sub(pages_freed, &global_page_count);
16971697

0 commit comments

Comments
 (0)