Skip to contentSkip to conversation

mm: zswap: fix kernel BUG in sg_init_one

PATCH Superseded Barry Song 2 participants 4 messages Mar 18, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Barry Song

author
#0 lore raw

PATCH 1/1 mm: zswap: fix kernel BUG in sg_init_one

From: Barry Song <[email protected]>

sg_init_one() relies on linearly mapped low memory for the safe utilization of virt_to_page(). Consequently, we have two choices:
either employ kmap_to_page() alongside sg_set_page(), or resort to copying high memory contents to a temporary buffer residing in low memory. However, considering the introduction of the WARN_ON_ONCE in commit ef6e06b2ef870 ("highmem: fix kmap_to_page() for kmap_local_page() addresses"), which specifically addresses high memory concerns, it appears that memcpy remains the sole viable option.

Reported-and-tested-by [email protected]
Fixes 270700dd06ca ("mm/zswap: remove the memcpy if acomp is not sleepable")
---
 mm/zswap.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/zswap.c +3 -2
index 9dec853647c8..17bf6d87b274 100644
@@ -1080,7 +1080,8 @@ static void zswap_decompress(struct zswap_entry *entry, struct page *page)
 	mutex_lock(&acomp_ctx->mutex);
 
 	src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO);
-	if (acomp_ctx->is_sleepable && !zpool_can_sleep_mapped(zpool)) {
+	if ((acomp_ctx->is_sleepable && !zpool_can_sleep_mapped(zpool)) ||
+	    !virt_addr_valid(src)) {
 		memcpy(acomp_ctx->buffer, src, entry->length);
 		src = acomp_ctx->buffer;
 		zpool_unmap_handle(zpool, entry->handle);
@@ -1094,7 +1095,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct page *page)
 	BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
 	mutex_unlock(&acomp_ctx->mutex);
 
-	if (!acomp_ctx->is_sleepable || zpool_can_sleep_mapped(zpool))
+	if (src != acomp_ctx->buffer)
 		zpool_unmap_handle(zpool, entry->handle);
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 26 lines · Barry Song

From: Barry Song <[email protected]>

sg_init_one() relies on linearly mapped low memory for the safe utilization of virt_to_page(). Consequently, we have two choices:
either employ kmap_to_page() alongside sg_set_page(), or resort to copying high memory contents to a temporary buffer residing in low memory. However, considering the introduction of the WARN_ON_ONCE in commit ef6e06b2ef870 ("highmem: fix kmap_to_page() for kmap_local_page() addresses"), which specifically addresses high memory concerns, it appears that memcpy remains the sole viable option.

Reported-and-tested-by [email protected]
Fixes 270700dd06ca ("mm/zswap: remove the memcpy if acomp is not sleepable")
---
 mm/zswap.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/zswap.c +0 -0
index 9dec853647c8..17bf6d87b274 100644

mm/zswap.c static void zswap_decompress(struct zswap_entry *entry, struct page *page) · line 1084

Quoted 6 lines
mm/zswap.c +2 -1
@@ -1080,7 +1080,8 @@ static void zswap_decompress(struct zswap_entry *entry, struct page *page)
        mutex_lock(&acomp_ctx->mutex);
 
        src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO);
-       if (acomp_ctx->is_sleepable && !zpool_can_sleep_mapped(zpool)) {
+       if ((acomp_ctx->is_sleepable && !zpool_can_sleep_mapped(zpool)) ||
+           !virt_addr_valid(src)) {

Would it be better to explicitly check is_kmap_addr() here? I am particularly worried about hiding a bug where the returned address from zpool_map_handle() is not a kmap address, but also not a valid linear mapping address.

If we use is_kmap_addr() here, then the virt_addr_valid() check in sg_init_one() will catch any non-kmap non-linear mapping addresses. WDYT? Am I being paranoid? :)

Also, I think a comment would be nice to explain the cases where we need to use a temporary buffer since we have two different cases now.

Quoted 16 lines
mm/zswap.c +1 -2
                memcpy(acomp_ctx->buffer, src, entry->length);
                src = acomp_ctx->buffer;
                zpool_unmap_handle(zpool, entry->handle);
@@ -1094,7 +1095,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct page *page)
        BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
        mutex_unlock(&acomp_ctx->mutex);
 
-       if (!acomp_ctx->is_sleepable || zpool_can_sleep_mapped(zpool))
+       if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);
 }
 
--

2.34.1

author
#2 lore raw
Quoted 41 lines · Yosry Ahmed
Quoted 26 lines · Barry Song

From: Barry Song <[email protected]>

sg_init_one() relies on linearly mapped low memory for the safe utilization of virt_to_page(). Consequently, we have two choices:
either employ kmap_to_page() alongside sg_set_page(), or resort to copying high memory contents to a temporary buffer residing in low memory. However, considering the introduction of the WARN_ON_ONCE in commit ef6e06b2ef870 ("highmem: fix kmap_to_page() for kmap_local_page() addresses"), which specifically addresses high memory concerns, it appears that memcpy remains the sole viable option.

Reported-and-tested-by [email protected]
Fixes 270700dd06ca ("mm/zswap: remove the memcpy if acomp is not sleepable")
---
 mm/zswap.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/zswap.c +0 -0
index 9dec853647c8..17bf6d87b274 100644

mm/zswap.c static void zswap_decompress(struct zswap_entry *entry, struct page *page) · line 1084

Quoted 6 lines
mm/zswap.c +2 -1
@@ -1080,7 +1080,8 @@ static void zswap_decompress(struct zswap_entry *entry, struct page *page)
        mutex_lock(&acomp_ctx->mutex);
 
        src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO);
-       if (acomp_ctx->is_sleepable && !zpool_can_sleep_mapped(zpool)) {
+       if ((acomp_ctx->is_sleepable && !zpool_can_sleep_mapped(zpool)) ||
+           !virt_addr_valid(src)) {

Would it be better to explicitly check is_kmap_addr() here? I am particularly worried about hiding a bug where the returned address from zpool_map_handle() is not a kmap address, but also not a valid linear mapping address.

Quoted 3 lines

If we use is_kmap_addr() here, then the virt_addr_valid() check in sg_init_one() will catch any non-kmap non-linear mapping addresses. WDYT? Am I being paranoid? :)

we have a possibility that a userspace buffer or vmalloc address is given to sg_init_one, then it is non-kmap non-linear. but is it possible someday some people return a vmalloc/vmap address from zpool_map_handle() in the future? then we still need !virt_addr_valid().

Quoted 3 lines

Also, I think a comment would be nice to explain the cases where we need to use a temporary buffer since we have two different cases now.

that makes sense.

Quoted 17 lines
Quoted 17 lines
mm/zswap.c +1 -2
                memcpy(acomp_ctx->buffer, src, entry->length);
                src = acomp_ctx->buffer;
                zpool_unmap_handle(zpool, entry->handle);
@@ -1094,7 +1095,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct page *page)
        BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
        mutex_unlock(&acomp_ctx->mutex);
 
-       if (!acomp_ctx->is_sleepable || zpool_can_sleep_mapped(zpool))
+       if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);
 }
 
--

2.34.1

#3 lore raw
Quoted 43 lines · Barry Song
Quoted 41 lines · Yosry Ahmed
Quoted 26 lines · Barry Song

From: Barry Song <[email protected]>

sg_init_one() relies on linearly mapped low memory for the safe utilization of virt_to_page(). Consequently, we have two choices:
either employ kmap_to_page() alongside sg_set_page(), or resort to copying high memory contents to a temporary buffer residing in low memory. However, considering the introduction of the WARN_ON_ONCE in commit ef6e06b2ef870 ("highmem: fix kmap_to_page() for kmap_local_page() addresses"), which specifically addresses high memory concerns, it appears that memcpy remains the sole viable option.

Reported-and-tested-by [email protected]
Fixes 270700dd06ca ("mm/zswap: remove the memcpy if acomp is not sleepable")
---
 mm/zswap.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/zswap.c +0 -0
index 9dec853647c8..17bf6d87b274 100644

mm/zswap.c static void zswap_decompress(struct zswap_entry *entry, struct page *page) · line 1084

Quoted 6 lines
mm/zswap.c +2 -1
@@ -1080,7 +1080,8 @@ static void zswap_decompress(struct zswap_entry *entry, struct page *page)
        mutex_lock(&acomp_ctx->mutex);
 
        src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO);
-       if (acomp_ctx->is_sleepable && !zpool_can_sleep_mapped(zpool)) {
+       if ((acomp_ctx->is_sleepable && !zpool_can_sleep_mapped(zpool)) ||
+           !virt_addr_valid(src)) {

Would it be better to explicitly check is_kmap_addr() here? I am particularly worried about hiding a bug where the returned address from zpool_map_handle() is not a kmap address, but also not a valid linear mapping address.

Quoted 8 lines
Quoted 3 lines

If we use is_kmap_addr() here, then the virt_addr_valid() check in sg_init_one() will catch any non-kmap non-linear mapping addresses. WDYT? Am I being paranoid? :)

we have a possibility that a userspace buffer or vmalloc address is given to sg_init_one, then it is non-kmap non-linear. but is it possible someday some people return a vmalloc/vmap address from zpool_map_handle() in the future? then we still need !virt_addr_valid().

Hmm I guess if zpool_map_handle() ever returns a vmalloc/vmap address we would need to copy the data to the buffer in zswap code, at least until we enlighten the code to deal with them directly. So I guess we can keep virt_addr_valid(). Ideally the comment will clarify this.

Thanks!