Skip to contentSkip to conversation

mm: zswap: Fix a potential memory leak in zswap_decompress().

PATCH In review Kanchana P Sridhar 5 participants 27 messages Nov 13, 2024 – Nov 20, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: zswap: Fix a potential memory leak in zswap_decompress().

This is a hotfix for a potential zpool memory leak that could result in the existing zswap_decompress():

        mutex_unlock(&acomp_ctx->mutex);

        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Releasing the lock before the conditional does not protect the integrity of "src", which is set earlier under the acomp_ctx mutex lock. This poses a risk for the conditional behaving as intended, and consequently not unmapping the zpool handle, which could cause a zswap zpool memory leak.

This patch moves the mutex_unlock() to occur after the conditional and subsequent zpool_unmap_handle(). This ensures that the value of "src" obtained earlier, with the mutex locked, does not change.

Even though an actual memory leak was not observed, this fix seems like a cleaner implementation.

Fixes 9c500835f279 ("mm: zswap: fix kernel BUG in sg_init_one")
---
 mm/zswap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/zswap.c +2 -1
index f6316b66fb23..58810fa8ff23 100644
@@ -986,10 +986,11 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)
 	acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, PAGE_SIZE);
 	BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait));
 	BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
-	mutex_unlock(&acomp_ctx->mutex);
 
 	if (src != acomp_ctx->buffer)
 		zpool_unmap_handle(zpool, entry->handle);
+
+	mutex_unlock(&acomp_ctx->mutex);
 }
 
 /*********************************

base-commit: 0e5bdedb39ded767bff4c6184225578595cee98c

Signature · 1 line
2.27.0
#1 lore raw
Quoted 9 lines · Kanchana P Sridhar

This is a hotfix for a potential zpool memory leak that could result in the existing zswap_decompress():

        mutex_unlock(&acomp_ctx->mutex);

        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);
Quoted 8 lines

Releasing the lock before the conditional does not protect the integrity of "src", which is set earlier under the acomp_ctx mutex lock. This poses a risk for the conditional behaving as intended, and consequently not unmapping the zpool handle, which could cause a zswap zpool memory leak.

This patch moves the mutex_unlock() to occur after the conditional and subsequent zpool_unmap_handle(). This ensures that the value of "src" obtained earlier, with the mutex locked, does not change.

The commit log is too complicated and incorrect. It is talking about the stability of 'src', but that's a local variable on the stack anyway. It doesn't need protection.

The problem is 'acomp_ctx->buffer' being reused and changed after the mutex is released. Leading to the check not being reliable. Please simplify this.

Quoted 15 lines

Even though an actual memory leak was not observed, this fix seems like a cleaner implementation.

Fixes 9c500835f279 ("mm: zswap: fix kernel BUG in sg_init_one")
---
 mm/zswap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/zswap.c +0 -0
index f6316b66fb23..58810fa8ff23 100644

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

Quoted 7 lines
mm/zswap.c +0 -1
@@ -986,10 +986,11 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)
        acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, PAGE_SIZE);
        BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait));
        BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
-       mutex_unlock(&acomp_ctx->mutex);
 
        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Actually now that I think more about it, I think this check isn't entirely safe, even under the lock. Is it possible that 'acomp_ctx->buffer' just happens to be equal to 'src' from a previous decompression at the same handle? In this case, we will also mistakenly skip the unmap.

It would be more reliable to set a boolean variable if we copy to acomp_ctx->buffer and do the unmap, and check that flag here to check if the unmap was done or not.

Quoted 10 lines
mm/zswap.c +2 -0
+
+       mutex_unlock(&acomp_ctx->mutex);
 }
 
 /*********************************

base-commit: 0e5bdedb39ded767bff4c6184225578595cee98c --
2.27.0

#2 lore raw
Quoted 35 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 12, 2024 9:35 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 9 lines · Kanchana P Sridhar

This is a hotfix for a potential zpool memory leak that could result in the existing zswap_decompress():

        mutex_unlock(&acomp_ctx->mutex);

        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);
Quoted 4 lines

Releasing the lock before the conditional does not protect the integrity of "src", which is set earlier under the acomp_ctx mutex lock. This poses a risk for the conditional behaving as intended, and consequently not unmapping the zpool handle, which could cause a zswap zpool memory

leak.

Quoted 4 lines

This patch moves the mutex_unlock() to occur after the conditional and subsequent zpool_unmap_handle(). This ensures that the value of "src" obtained earlier, with the mutex locked, does not change.

Quoted 7 lines

The commit log is too complicated and incorrect. It is talking about the stability of 'src', but that's a local variable on the stack anyway. It doesn't need protection.

The problem is 'acomp_ctx->buffer' being reused and changed after the mutex is released. Leading to the check not being reliable. Please simplify this.

Thanks Yosry. That's exactly what I meant, but I think the wording got confusing. The problem I was trying to fix is the acomp_ctx->buffer value changing after the lock is released. This could happen as a result of any other compress or decompress that acquires the lock. I will simplify and clarify accordingly.

Quoted 24 lines

mm/zswap.c static void zswap_decompress(struct

Quoted 16 lines

Even though an actual memory leak was not observed, this fix seems like a cleaner implementation.

Fixes 9c500835f279 ("mm: zswap: fix kernel BUG in sg_init_one")
---
 mm/zswap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/zswap.c +0 -0
index f6316b66fb23..58810fa8ff23 100644
@@ -986,10 +986,11 @@ static void zswap_decompress(struct

zswap_entry *entry, struct folio *folio)

Quoted 7 lines
mm/zswap.c +0 -1
        acomp_request_set_params(acomp_ctx->req, &input, &output, entry-
 length, PAGE_SIZE);
        BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx-
 req), &acomp_ctx->wait));
        BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
-       mutex_unlock(&acomp_ctx->mutex);
 
Quoted 8 lines

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

Quoted 2 lines
mm/zswap.c +0 -0
        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Actually now that I think more about it, I think this check isn't entirely safe, even under the lock. Is it possible that 'acomp_ctx->buffer' just happens to be equal to 'src' from a previous decompression at the same handle? In this case, we will also mistakenly skip the unmap.

If we move the mutex_unlock to happen after the conditional and unmap, shouldn't that be sufficient under all conditions? With the fix, "src" can take on only 2 values in this procedure: the mapped handle or acomp_ctx->buffer. The only ambiguity would be in the (unlikely?) case if the mapped zpool handle happens to be equal to acomp_ctx->buffer.

Please let me know if I am missing anything.

Quoted 4 lines

It would be more reliable to set a boolean variable if we copy to acomp_ctx->buffer and do the unmap, and check that flag here to check if the unmap was done or not.

Sure, this could be done, but not sure if it is required. Please let me know if we still need the boolean variable in addition to moving the mutex_unlock().

Thanks,
Kanchana

Quoted 11 lines
Quoted 11 lines
mm/zswap.c +2 -0
+
+       mutex_unlock(&acomp_ctx->mutex);
 }

/*********************************

base-commit: 0e5bdedb39ded767bff4c6184225578595cee98c --
2.27.0

#3 lore raw
Quoted 83 lines · Kanchana P
Quoted 36 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 12, 2024 9:35 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 9 lines · Kanchana P Sridhar

This is a hotfix for a potential zpool memory leak that could result in the existing zswap_decompress():

        mutex_unlock(&acomp_ctx->mutex);

        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);
Quoted 4 lines

Releasing the lock before the conditional does not protect the integrity of "src", which is set earlier under the acomp_ctx mutex lock. This poses a risk for the conditional behaving as intended, and consequently not unmapping the zpool handle, which could cause a zswap zpool memory

leak.

Quoted 4 lines

This patch moves the mutex_unlock() to occur after the conditional and subsequent zpool_unmap_handle(). This ensures that the value of "src" obtained earlier, with the mutex locked, does not change.

Quoted 7 lines

The commit log is too complicated and incorrect. It is talking about the stability of 'src', but that's a local variable on the stack anyway. It doesn't need protection.

The problem is 'acomp_ctx->buffer' being reused and changed after the mutex is released. Leading to the check not being reliable. Please simplify this.

Thanks Yosry. That's exactly what I meant, but I think the wording got confusing. The problem I was trying to fix is the acomp_ctx->buffer value changing after the lock is released. This could happen as a result of any other compress or decompress that acquires the lock. I will simplify and clarify accordingly.

Quoted 24 lines

mm/zswap.c static void zswap_decompress(struct

Quoted 16 lines

Even though an actual memory leak was not observed, this fix seems like a cleaner implementation.

Fixes 9c500835f279 ("mm: zswap: fix kernel BUG in sg_init_one")
---
 mm/zswap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/zswap.c +0 -0
index f6316b66fb23..58810fa8ff23 100644
@@ -986,10 +986,11 @@ static void zswap_decompress(struct

zswap_entry *entry, struct folio *folio)

Quoted 7 lines
mm/zswap.c +0 -1
        acomp_request_set_params(acomp_ctx->req, &input, &output, entry-
 length, PAGE_SIZE);
        BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx-
 req), &acomp_ctx->wait));
        BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
-       mutex_unlock(&acomp_ctx->mutex);
 
Quoted 8 lines

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

Quoted 2 lines
mm/zswap.c +0 -0
        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Actually now that I think more about it, I think this check isn't entirely safe, even under the lock. Is it possible that 'acomp_ctx->buffer' just happens to be equal to 'src' from a previous decompression at the same handle? In this case, we will also mistakenly skip the unmap.

Quoted 5 lines

If we move the mutex_unlock to happen after the conditional and unmap, shouldn't that be sufficient under all conditions? With the fix, "src" can take on only 2 values in this procedure: the mapped handle or acomp_ctx->buffer. The only ambiguity would be in the (unlikely?) case if the mapped zpool handle happens to be equal to acomp_ctx->buffer.

Yes, that's the scenario I mean.

Specifically, in zswap_decompress(), we do not use 'acomp_ctx->buffer' so 'src' is equal to the mapped handle. But, 'acomp_ctx->buffer' happens to be equal to the same handle from a previous operation as we don't clear it.

In this case, the 'src != acomp_ctx->buffer' check will be false, even though it should be true. This will result in an extra zpool_unmap_handle() call. I didn't look closely, but this seems like it can have a worse effect than leaking memory (e.g. there will be an extra __kunmap_atomic() call in zsmalloc, and we may end up corrupting a random handle).

Quoted 10 lines

Please let me know if I am missing anything.

Quoted 4 lines

It would be more reliable to set a boolean variable if we copy to acomp_ctx->buffer and do the unmap, and check that flag here to check if the unmap was done or not.

Sure, this could be done, but not sure if it is required. Please let me know if we still need the boolean variable in addition to moving the mutex_unlock().

If we use a boolean, there is no need to move mutex_unlock(). The boolean will be a local variable on the stack that doesn't need protection.

Quoted 15 lines

Thanks,
Kanchana

Quoted 11 lines
Quoted 11 lines
mm/zswap.c +2 -0
+
+       mutex_unlock(&acomp_ctx->mutex);
 }

/*********************************

base-commit: 0e5bdedb39ded767bff4c6184225578595cee98c --
2.27.0

#4 lore raw

Hi Yosry,

Quoted 110 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 12, 2024 10:22 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 8 lines · Kanchana P
Quoted 8 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 12, 2024 9:35 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected];

[email protected];

Quoted 2 lines
Quoted 2 lines

[email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; akpm@linux-

foundation.org;

Quoted 11 lines
Quoted 11 lines

Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 4 lines · Kanchana P Sridhar

This is a hotfix for a potential zpool memory leak that could result in the existing zswap_decompress():

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
        mutex_unlock(&acomp_ctx->mutex);

        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Releasing the lock before the conditional does not protect the integrity

of

Quoted 13 lines
Quoted 13 lines
Quoted 3 lines

"src", which is set earlier under the acomp_ctx mutex lock. This poses a risk for the conditional behaving as intended, and consequently not unmapping the zpool handle, which could cause a zswap zpool memory

leak.

Quoted 4 lines

This patch moves the mutex_unlock() to occur after the conditional and subsequent zpool_unmap_handle(). This ensures that the value of "src" obtained earlier, with the mutex locked, does not change.

The commit log is too complicated and incorrect. It is talking about the stability of 'src', but that's a local variable on the stack anyway. It doesn't need protection.

Quoted 7 lines
Quoted 3 lines

The problem is 'acomp_ctx->buffer' being reused and changed after the mutex is released. Leading to the check not being reliable. Please simplify this.

Thanks Yosry. That's exactly what I meant, but I think the wording got confusing. The problem I was trying to fix is the acomp_ctx->buffer value changing after the lock is released. This could happen as a result of

any

Quoted 6 lines

other compress or decompress that acquires the lock. I will simplify and clarify accordingly.

Quoted 3 lines
Quoted 3 lines

Even though an actual memory leak was not observed, this fix seems

like a

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

cleaner implementation.

Fixes 9c500835f279 ("mm: zswap: fix kernel BUG in sg_init_one")
---
 mm/zswap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 7 lines
Quoted 7 lines

mm/zswap.c static void zswap_decompress(struct

Quoted 5 lines
mm/zswap.c +0 -0
index f6316b66fb23..58810fa8ff23 100644
@@ -986,10 +986,11 @@ static void zswap_decompress(struct

zswap_entry *entry, struct folio *folio)

Quoted 1 line
mm/zswap.c +0 -0
        acomp_request_set_params(acomp_ctx->req, &input, &output,

entry-

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
mm/zswap.c +0 -1
 length, PAGE_SIZE);
        BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx-
 req), &acomp_ctx->wait));
        BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
-       mutex_unlock(&acomp_ctx->mutex);
 
Quoted 8 lines

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

Quoted 2 lines
mm/zswap.c +0 -0
        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Actually now that I think more about it, I think this check isn't entirely safe, even under the lock. Is it possible that 'acomp_ctx->buffer' just happens to be equal to 'src' from a previous decompression at the same handle? In this case, we will also mistakenly skip the unmap.

Quoted 5 lines

If we move the mutex_unlock to happen after the conditional and unmap, shouldn't that be sufficient under all conditions? With the fix, "src" can take on only 2 values in this procedure: the mapped handle or acomp_ctx->buffer. The only ambiguity would be in the (unlikely?) case if the mapped zpool handle happens to be equal to acomp_ctx->buffer.

Quoted 6 lines

Yes, that's the scenario I mean.

Specifically, in zswap_decompress(), we do not use 'acomp_ctx->buffer' so 'src' is equal to the mapped handle. But, 'acomp_ctx->buffer' happens to be equal to the same handle from a previous operation as we don't clear it.

Although, we never modify 'acomp_ctx->buffer' in zswap_decompress(), we only copy to it.

Quoted 16 lines

In this case, the 'src != acomp_ctx->buffer' check will be false, even though it should be true. This will result in an extra zpool_unmap_handle() call. I didn't look closely, but this seems like it can have a worse effect than leaking memory (e.g. there will be an extra __kunmap_atomic() call in zsmalloc, and we may end up corrupting a random handle).

Quoted 8 lines

Please let me know if I am missing anything.

Quoted 4 lines

It would be more reliable to set a boolean variable if we copy to acomp_ctx->buffer and do the unmap, and check that flag here to check if the unmap was done or not.

Quoted 7 lines
Quoted 2 lines

Sure, this could be done, but not sure if it is required. Please let me know if we still need the boolean variable in addition to moving the

mutex_unlock().

If we use a boolean, there is no need to move mutex_unlock(). The boolean will be a local variable on the stack that doesn't need protection.

I agree, using the boolean variable to do the unmap rather than the check for (src != acomp_ctx->buffer) is more fail-safe.

I am still thinking moving the mutex_unlock() could help, or at least have no downside. The acomp_ctx is per-cpu and it's mutex_lock/unlock safeguards the interaction between the decompress operation, the sg_*() API calls inside zswap_decompress() and the shared zpool.

If we release the per-cpu acomp_ctx's mutex lock before the zpool_unmap_handle(), is it possible that another cpu could acquire it's acomp_ctx's lock and map the same zpool handle (that the earlier cpu has yet to unmap or is concurrently unmapping) for a write? If this could happen, would it result in undefined state for both these zpool ops on different cpu's?

Would keeping the per-cpu mutex locked through the zpool_unmap_handle() create a non-preemptible state that would avoid this? IOW, if the above scenario is possible, does the per-cpu acomp_ctx's mutex help/is a no-op? Or, is the above scenario somehow prevented by the implementation of the zpools?

Just thought I would bring up these open questions. Please do share your thoughts and advise.

Thanks,
Kanchana

Quoted 16 lines
Quoted 16 lines

Thanks,
Kanchana

Quoted 11 lines
Quoted 11 lines
mm/zswap.c +2 -0
+
+       mutex_unlock(&acomp_ctx->mutex);
 }

/*********************************

base-commit: 0e5bdedb39ded767bff4c6184225578595cee98c --
2.27.0

#5 lore raw
Quoted 115 lines · Kanchana P

Hi Yosry,

Quoted 112 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 12, 2024 10:22 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 8 lines · Kanchana P
Quoted 8 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 12, 2024 9:35 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected];

[email protected];

Quoted 2 lines
Quoted 2 lines

[email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; akpm@linux-

foundation.org;

Quoted 11 lines
Quoted 11 lines

Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 4 lines · Kanchana P Sridhar

This is a hotfix for a potential zpool memory leak that could result in the existing zswap_decompress():

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
        mutex_unlock(&acomp_ctx->mutex);

        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Releasing the lock before the conditional does not protect the integrity

of

Quoted 13 lines
Quoted 13 lines
Quoted 3 lines

"src", which is set earlier under the acomp_ctx mutex lock. This poses a risk for the conditional behaving as intended, and consequently not unmapping the zpool handle, which could cause a zswap zpool memory

leak.

Quoted 4 lines

This patch moves the mutex_unlock() to occur after the conditional and subsequent zpool_unmap_handle(). This ensures that the value of "src" obtained earlier, with the mutex locked, does not change.

The commit log is too complicated and incorrect. It is talking about the stability of 'src', but that's a local variable on the stack anyway. It doesn't need protection.

Quoted 7 lines
Quoted 3 lines

The problem is 'acomp_ctx->buffer' being reused and changed after the mutex is released. Leading to the check not being reliable. Please simplify this.

Thanks Yosry. That's exactly what I meant, but I think the wording got confusing. The problem I was trying to fix is the acomp_ctx->buffer value changing after the lock is released. This could happen as a result of

any

Quoted 6 lines

other compress or decompress that acquires the lock. I will simplify and clarify accordingly.

Quoted 3 lines
Quoted 3 lines

Even though an actual memory leak was not observed, this fix seems

like a

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

cleaner implementation.

Fixes 9c500835f279 ("mm: zswap: fix kernel BUG in sg_init_one")
---
 mm/zswap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 7 lines
Quoted 7 lines

mm/zswap.c static void zswap_decompress(struct

Quoted 5 lines
mm/zswap.c +0 -0
index f6316b66fb23..58810fa8ff23 100644
@@ -986,10 +986,11 @@ static void zswap_decompress(struct

zswap_entry *entry, struct folio *folio)

Quoted 1 line
mm/zswap.c +0 -0
        acomp_request_set_params(acomp_ctx->req, &input, &output,

entry-

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
mm/zswap.c +0 -1
 length, PAGE_SIZE);
        BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx-
 req), &acomp_ctx->wait));
        BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
-       mutex_unlock(&acomp_ctx->mutex);
 
Quoted 8 lines

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

Quoted 2 lines
mm/zswap.c +0 -0
        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Actually now that I think more about it, I think this check isn't entirely safe, even under the lock. Is it possible that 'acomp_ctx->buffer' just happens to be equal to 'src' from a previous decompression at the same handle? In this case, we will also mistakenly skip the unmap.

Quoted 5 lines

If we move the mutex_unlock to happen after the conditional and unmap, shouldn't that be sufficient under all conditions? With the fix, "src" can take on only 2 values in this procedure: the mapped handle or acomp_ctx->buffer. The only ambiguity would be in the (unlikely?) case if the mapped zpool handle happens to be equal to acomp_ctx->buffer.

Yes, that's the scenario I mean.

Quoted 7 lines
Quoted 4 lines

Specifically, in zswap_decompress(), we do not use 'acomp_ctx->buffer' so 'src' is equal to the mapped handle. But, 'acomp_ctx->buffer' happens to be equal to the same handle from a previous operation as we don't clear it.

Although, we never modify 'acomp_ctx->buffer' in zswap_decompress(), we only copy to it.

Duh, yes. I confused myself, sorry for the noise.

Quoted 33 lines
Quoted 17 lines

In this case, the 'src != acomp_ctx->buffer' check will be false, even though it should be true. This will result in an extra zpool_unmap_handle() call. I didn't look closely, but this seems like it can have a worse effect than leaking memory (e.g. there will be an extra __kunmap_atomic() call in zsmalloc, and we may end up corrupting a random handle).

Quoted 8 lines

Please let me know if I am missing anything.

Quoted 4 lines

It would be more reliable to set a boolean variable if we copy to acomp_ctx->buffer and do the unmap, and check that flag here to check if the unmap was done or not.

Quoted 7 lines
Quoted 2 lines

Sure, this could be done, but not sure if it is required. Please let me know if we still need the boolean variable in addition to moving the

mutex_unlock().

If we use a boolean, there is no need to move mutex_unlock(). The boolean will be a local variable on the stack that doesn't need protection.

I agree, using the boolean variable to do the unmap rather than the check for (src != acomp_ctx->buffer) is more fail-safe.

I am still thinking moving the mutex_unlock() could help, or at least have no downside. The acomp_ctx is per-cpu and it's mutex_lock/unlock safeguards the interaction between the decompress operation, the sg_*() API calls inside zswap_decompress() and the shared zpool.

Quoted 6 lines

If we release the per-cpu acomp_ctx's mutex lock before the zpool_unmap_handle(), is it possible that another cpu could acquire it's acomp_ctx's lock and map the same zpool handle (that the earlier cpu has yet to unmap or is concurrently unmapping) for a write? If this could happen, would it result in undefined state for both these zpool ops on different cpu's?

Why would this result in an undefined state? For zsmalloc, mapping uses a per-CPU buffer and preemption is disabled between mapping and unmapping anyway. If another CPU maps the object it should be fine.

Quoted 6 lines

Would keeping the per-cpu mutex locked through the zpool_unmap_handle() create a non-preemptible state that would avoid this? IOW, if the above scenario is possible, does the per-cpu acomp_ctx's mutex help/is a no-op? Or, is the above scenario somehow prevented by the implementation of the zpools?

At least for zsmalloc, I think it is.

Quoted 3 lines

Just thought I would bring up these open questions. Please do share your thoughts and advise.

I think moving the mutex unlock after the unmap won't make much of a difference from a performance side, at least for zsmalloc. Preemption will be disabled until the unmap is done anyway, so even after we release the per-CPU mutex it cannot be acquired by anyone else until the unmap is done.

Anyway, I think the fix you have right now is fine, if you prefer not adding a boolean. If you do add a boolean, whether you move the mutex unlock or not should not make a difference.

Please just rewrite the commit log and CC stable (in the commit log, not in the email CC list).

Thanks and sorry for the confusion!

Quoted 20 lines

Thanks,
Kanchana

Quoted 16 lines
Quoted 16 lines

Thanks,
Kanchana

Quoted 11 lines
Quoted 11 lines
mm/zswap.c +2 -0
+
+       mutex_unlock(&acomp_ctx->mutex);
 }

/*********************************

base-commit: 0e5bdedb39ded767bff4c6184225578595cee98c --
2.27.0

#6 lore raw
Quoted 216 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Wednesday, November 13, 2024 12:11 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 9 lines · Kanchana P

Hi Yosry,

Quoted 6 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 12, 2024 10:22 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected];

[email protected];

Quoted 2 lines
Quoted 2 lines

[email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; akpm@linux-

foundation.org;

Quoted 30 lines
Quoted 30 lines

Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 8 lines · Kanchana P
Quoted 8 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 12, 2024 9:35 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected];

[email protected];

Quoted 2 lines
Quoted 2 lines

[email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; akpm@linux-

foundation.org;

Quoted 11 lines
Quoted 11 lines

Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 4 lines · Kanchana P Sridhar

This is a hotfix for a potential zpool memory leak that could result in the existing zswap_decompress():

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
        mutex_unlock(&acomp_ctx->mutex);

        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Releasing the lock before the conditional does not protect the

integrity

Quoted 2 lines
Quoted 2 lines

of

Quoted 1 line
Quoted 1 line
Quoted 1 line

"src", which is set earlier under the acomp_ctx mutex lock. This

poses a

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

risk for the conditional behaving as intended, and consequently not unmapping the zpool handle, which could cause a zswap zpool

memory

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

leak.

Quoted 2 lines

This patch moves the mutex_unlock() to occur after the conditional

and

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

subsequent zpool_unmap_handle(). This ensures that the value of

"src"

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

obtained earlier, with the mutex locked, does not change.

The commit log is too complicated and incorrect. It is talking about the stability of 'src', but that's a local variable on the stack anyway. It doesn't need protection.

The problem is 'acomp_ctx->buffer' being reused and changed after

the

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

mutex is released. Leading to the check not being reliable. Please simplify this.

Thanks Yosry. That's exactly what I meant, but I think the wording got confusing. The problem I was trying to fix is the acomp_ctx->buffer value changing after the lock is released. This could happen as a result

of

Quoted 22 lines
Quoted 22 lines

any

Quoted 6 lines

other compress or decompress that acquires the lock. I will simplify and clarify accordingly.

Quoted 3 lines
Quoted 3 lines

Even though an actual memory leak was not observed, this fix seems

like a

Quoted 14 lines
Quoted 14 lines

mm/zswap.c static void zswap_decompress(struct

Quoted 13 lines

cleaner implementation.

Fixes 9c500835f279 ("mm: zswap: fix kernel BUG in sg_init_one")
---
 mm/zswap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/zswap.c +0 -0
index f6316b66fb23..58810fa8ff23 100644
@@ -986,10 +986,11 @@ static void zswap_decompress(struct

zswap_entry *entry, struct folio *folio)

Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

mm/zswap.c static void zswap_decompress(struct · line 986

Quoted 1 line
mm/zswap.c +0 -0
        acomp_request_set_params(acomp_ctx->req, &input, &output,

entry-

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
mm/zswap.c +0 -0
 length, PAGE_SIZE);
 

BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx-

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
mm/zswap.c +0 -1
 req), &acomp_ctx->wait));
        BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
-       mutex_unlock(&acomp_ctx->mutex);
 
        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

Actually now that I think more about it, I think this check isn't entirely safe, even under the lock. Is it possible that 'acomp_ctx->buffer' just happens to be equal to 'src' from a previous decompression at the same handle? In this case, we will also mistakenly skip the unmap.

If we move the mutex_unlock to happen after the conditional and

unmap,

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

shouldn't that be sufficient under all conditions? With the fix, "src" can take on only 2 values in this procedure: the mapped handle or acomp_ctx->buffer. The only ambiguity would be in the (unlikely?) case if the mapped zpool handle happens to be equal to acomp_ctx->buffer.

Yes, that's the scenario I mean.

Quoted 7 lines
Quoted 4 lines

Specifically, in zswap_decompress(), we do not use 'acomp_ctx->buffer' so 'src' is equal to the mapped handle. But, 'acomp_ctx->buffer' happens to be equal to the same handle from a previous operation as we don't clear it.

Although, we never modify 'acomp_ctx->buffer' in zswap_decompress(), we only copy to it.

Duh, yes. I confused myself, sorry for the noise.

Quoted 10 lines
Quoted 10 lines

In this case, the 'src != acomp_ctx->buffer' check will be false, even though it should be true. This will result in an extra zpool_unmap_handle() call. I didn't look closely, but this seems like it can have a worse effect than leaking memory (e.g. there will be an extra __kunmap_atomic() call in zsmalloc, and we may end up corrupting a random handle).

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Please let me know if I am missing anything.

Quoted 3 lines

It would be more reliable to set a boolean variable if we copy to acomp_ctx->buffer and do the unmap, and check that flag here to

check

Quoted 18 lines
Quoted 9 lines
Quoted 4 lines
Quoted 1 line

if the unmap was done or not.

Sure, this could be done, but not sure if it is required. Please let me know if we still need the boolean variable in addition to moving the

mutex_unlock().

If we use a boolean, there is no need to move mutex_unlock(). The boolean will be a local variable on the stack that doesn't need protection.

I agree, using the boolean variable to do the unmap rather than the check for (src != acomp_ctx->buffer) is more fail-safe.

I am still thinking moving the mutex_unlock() could help, or at least have no downside. The acomp_ctx is per-cpu and it's mutex_lock/unlock safeguards the interaction between the decompress operation, the sg_*() API calls inside zswap_decompress() and the shared zpool.

Quoted 6 lines

If we release the per-cpu acomp_ctx's mutex lock before the zpool_unmap_handle(), is it possible that another cpu could acquire it's acomp_ctx's lock and map the same zpool handle (that the earlier cpu has yet to unmap or is concurrently unmapping) for a write? If this could happen, would it result in undefined state for both these zpool ops on different cpu's?

Why would this result in an undefined state? For zsmalloc, mapping uses a per-CPU buffer and preemption is disabled between mapping and unmapping anyway. If another CPU maps the object it should be fine.

Quoted 6 lines

Would keeping the per-cpu mutex locked through the zpool_unmap_handle() create a non-preemptible state that would avoid this? IOW, if the above scenario is possible, does the per-cpu acomp_ctx's mutex help/is a no-op? Or, is the above scenario somehow prevented by the implementation of the zpools?

At least for zsmalloc, I think it is.

Quoted 3 lines

Just thought I would bring up these open questions. Please do share your thoughts and advise.

I think moving the mutex unlock after the unmap won't make much of a difference from a performance side, at least for zsmalloc. Preemption will be disabled until the unmap is done anyway, so even after we release the per-CPU mutex it cannot be acquired by anyone else until the unmap is done.

Quoted 3 lines

Anyway, I think the fix you have right now is fine, if you prefer not adding a boolean. If you do add a boolean, whether you move the mutex unlock or not should not make a difference.

Thanks for the guidance on next steps! I will add the boolean and move the mutex.

Quoted 3 lines

Please just rewrite the commit log and CC stable (in the commit log, not in the email CC list).

I guess this refers to adding "Cc: [email protected]" in the commit log (as found from the latest mainline git log) ?

Thanks again,
Kanchana

#7 lore raw

[..]

Quoted 6 lines
Quoted 3 lines

Please just rewrite the commit log and CC stable (in the commit log, not in the email CC list).

I guess this refers to adding "Cc: [email protected]" in the commit log (as found from the latest mainline git log) ?

Yes, thanks!

Quoted 4 lines

Thanks again,
Kanchana

#8 lore raw
Quoted 16 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Wednesday, November 13, 2024 1:00 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

[..]

Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Please just rewrite the commit log and CC stable (in the commit log, not in the email CC list).

I guess this refers to adding "Cc: [email protected]" in the commit log (as found from the latest mainline git log) ?

Yes, thanks!

Sounds good, thanks Yosry!

Quoted 5 lines
Quoted 5 lines

Thanks again,
Kanchana

maintainer
#9 lore raw
In reply to Sridhar, Kanchana P
Quoted 11 lines · Kanchana P

I am still thinking moving the mutex_unlock() could help, or at least have no downside. The acomp_ctx is per-cpu and it's mutex_lock/unlock safeguards the interaction between the decompress operation, the sg_*() API calls inside zswap_decompress() and the shared zpool.

If we release the per-cpu acomp_ctx's mutex lock before the zpool_unmap_handle(), is it possible that another cpu could acquire it's acomp_ctx's lock and map the same zpool handle (that the earlier cpu has yet to unmap or is concurrently unmapping) for a write? If this could happen, would it result in undefined state for both these zpool ops on different cpu's?

The code is fine as is.

Like you said, acomp_ctx->buffer (the pointer) doesn't change. It points to whatever was kmalloced in zswap_cpu_comp_prepare(). The handle points to backend memory. Neither of those addresses can change under us. There is no confusing them, and they cannot coincide.

The mutex guards the *memory* behind the buffer, so that we don't have multiple (de)compressors stepping on each others' toes. But it's fine to drop the mutex once we're done working with the memory. We don't need the mutex to check whether src holds the acomp buffer address.

That being said, I do think there is a UAF bug in CPU hotplugging.

There is an acomp_ctx for each cpu, but note that this is best effort parallelism, not a guarantee that we always have the context of the local CPU. Look closely: we pick the "local" CPU with preemption enabled, then contend for the mutex. This may well put us to sleep and get us migrated, so we could be using the context of a CPU we are no longer running on. This is fine because we hold the mutex - if that other CPU tries to use the acomp_ctx, it'll wait for us.

However, if we get migrated and vacate the CPU whose context we have locked, the CPU might get offlined and zswap_cpu_comp_dead() can free the context underneath us. I think we need to refcount the acomp_ctx.

#10 lore raw
Quoted 21 lines · Johannes Weiner
Quoted 11 lines · Kanchana P

I am still thinking moving the mutex_unlock() could help, or at least have no downside. The acomp_ctx is per-cpu and it's mutex_lock/unlock safeguards the interaction between the decompress operation, the sg_*() API calls inside zswap_decompress() and the shared zpool.

If we release the per-cpu acomp_ctx's mutex lock before the zpool_unmap_handle(), is it possible that another cpu could acquire it's acomp_ctx's lock and map the same zpool handle (that the earlier cpu has yet to unmap or is concurrently unmapping) for a write? If this could happen, would it result in undefined state for both these zpool ops on different cpu's?

The code is fine as is.

Like you said, acomp_ctx->buffer (the pointer) doesn't change. It points to whatever was kmalloced in zswap_cpu_comp_prepare(). The handle points to backend memory. Neither of those addresses can change under us. There is no confusing them, and they cannot coincide.

Quoted 4 lines

The mutex guards the *memory* behind the buffer, so that we don't have multiple (de)compressors stepping on each others' toes. But it's fine to drop the mutex once we're done working with the memory. We don't need the mutex to check whether src holds the acomp buffer address.

I have to admit that I confused myself with this alleged bug more than I like to admit :)

I initially thought acomp_ctx->buffer can be changed, then when I realized it cannot be changed I did not tie that back to the 'fix' not being needed at all. I need more coffee.

Quoted 14 lines

That being said, I do think there is a UAF bug in CPU hotplugging.

There is an acomp_ctx for each cpu, but note that this is best effort parallelism, not a guarantee that we always have the context of the local CPU. Look closely: we pick the "local" CPU with preemption enabled, then contend for the mutex. This may well put us to sleep and get us migrated, so we could be using the context of a CPU we are no longer running on. This is fine because we hold the mutex - if that other CPU tries to use the acomp_ctx, it'll wait for us.

However, if we get migrated and vacate the CPU whose context we have locked, the CPU might get offlined and zswap_cpu_comp_dead() can free the context underneath us. I think we need to refcount the acomp_ctx.

#11 lore raw
In reply to Johannes Weiner
Quoted 34 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 1:30 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Yosry Ahmed <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 11 lines · Kanchana P

I am still thinking moving the mutex_unlock() could help, or at least have no downside. The acomp_ctx is per-cpu and it's mutex_lock/unlock safeguards the interaction between the decompress operation, the sg_*() API calls inside zswap_decompress() and the shared zpool.

If we release the per-cpu acomp_ctx's mutex lock before the zpool_unmap_handle(), is it possible that another cpu could acquire it's acomp_ctx's lock and map the same zpool handle (that the earlier cpu has yet to unmap or is concurrently unmapping) for a write? If this could happen, would it result in undefined state for both these zpool ops on different cpu's?

The code is fine as is.

Like you said, acomp_ctx->buffer (the pointer) doesn't change. It points to whatever was kmalloced in zswap_cpu_comp_prepare(). The handle points to backend memory. Neither of those addresses can change under us. There is no confusing them, and they cannot coincide.

Quoted 4 lines

The mutex guards the *memory* behind the buffer, so that we don't have multiple (de)compressors stepping on each others' toes. But it's fine to drop the mutex once we're done working with the memory. We don't need the mutex to check whether src holds the acomp buffer address.

Thanks Johannes, for these insights. I was thinking of the following in zswap_decompress() as creating a non-preemptible context because of the call to raw_cpu_ptr() at the start; with this context extending until the mutex_unlock():

	acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
	mutex_lock(&acomp_ctx->mutex);

	[...]

	mutex_unlock(&acomp_ctx->mutex);

	if (src != acomp_ctx->buffer)
		zpool_unmap_handle(zpool, entry->handle);

Based on this understanding, I was a bit worried about the "acomp_ctx->buffer" in the conditional that gates the zpool_unmap_handle() not being the same acomp_ctx as the one at the beginning. I may have been confusing myself, since the acomp_ctx is not re-evaluated before the conditional, just reused from the start. My apologies to you and Yosry!

Quoted 11 lines

That being said, I do think there is a UAF bug in CPU hotplugging.

There is an acomp_ctx for each cpu, but note that this is best effort parallelism, not a guarantee that we always have the context of the local CPU. Look closely: we pick the "local" CPU with preemption enabled, then contend for the mutex. This may well put us to sleep and get us migrated, so we could be using the context of a CPU we are no longer running on. This is fine because we hold the mutex - if that other CPU tries to use the acomp_ctx, it'll wait for us.

Quoted 3 lines

However, if we get migrated and vacate the CPU whose context we have locked, the CPU might get offlined and zswap_cpu_comp_dead() can free the context underneath us. I think we need to refcount the acomp_ctx.

I see. Wouldn't it then seem to make the code more fail-safe to not allow the migration to happen until after the check for (src != acomp_ctx->buffer), by moving the mutex_unlock() after this check? Or, use a boolean to determine if the unmap_handle needs to be done as Yosry suggested?

Thanks,
Kanchana

maintainer
#12 lore raw
Quoted 36 lines · Kanchana P

Thanks Johannes, for these insights. I was thinking of the following in zswap_decompress() as creating a non-preemptible context because of the call to raw_cpu_ptr() at the start; with this context extending until the mutex_unlock():

        acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
        mutex_lock(&acomp_ctx->mutex);

        [...]

        mutex_unlock(&acomp_ctx->mutex);

        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Based on this understanding, I was a bit worried about the "acomp_ctx->buffer" in the conditional that gates the zpool_unmap_handle() not being the same acomp_ctx as the one at the beginning. I may have been confusing myself, since the acomp_ctx is not re-evaluated before the conditional, just reused from the start. My apologies to you and Yosry!

Quoted 11 lines

That being said, I do think there is a UAF bug in CPU hotplugging.

There is an acomp_ctx for each cpu, but note that this is best effort parallelism, not a guarantee that we always have the context of the local CPU. Look closely: we pick the "local" CPU with preemption enabled, then contend for the mutex. This may well put us to sleep and get us migrated, so we could be using the context of a CPU we are no longer running on. This is fine because we hold the mutex - if that other CPU tries to use the acomp_ctx, it'll wait for us.

Quoted 8 lines
Quoted 3 lines

However, if we get migrated and vacate the CPU whose context we have locked, the CPU might get offlined and zswap_cpu_comp_dead() can free the context underneath us. I think we need to refcount the acomp_ctx.

I see. Wouldn't it then seem to make the code more fail-safe to not allow the migration to happen until after the check for (src != acomp_ctx->buffer), by moving the mutex_unlock() after this check? Or, use a boolean to determine if the unmap_handle needs to be done as Yosry suggested?

Hmm does it make it safe? It is mutex_lock() that puts the task to sleep, after which it can get migrated to a different CPU. Moving mutex_unlock() to below or not doesn't really matter, no? Or am I missing something here...

I think Johannes' proposal is the safest :)

#13 lore raw
Quoted 58 lines

-----Original Message-----
From: Nhat Pham <[email protected]>
Sent: Wednesday, November 13, 2024 4:29 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Johannes Weiner <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 36 lines · Kanchana P

Thanks Johannes, for these insights. I was thinking of the following in zswap_decompress() as creating a non-preemptible context because of the call to raw_cpu_ptr() at the start; with this context extending until the mutex_unlock():

        acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
        mutex_lock(&acomp_ctx->mutex);

        [...]

        mutex_unlock(&acomp_ctx->mutex);

        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Based on this understanding, I was a bit worried about the "acomp_ctx->buffer" in the conditional that gates the zpool_unmap_handle() not being the same acomp_ctx as the one at the beginning. I may have been confusing myself, since the acomp_ctx is not re-evaluated before the conditional, just reused from the start. My apologies to you and Yosry!

Quoted 11 lines

That being said, I do think there is a UAF bug in CPU hotplugging.

There is an acomp_ctx for each cpu, but note that this is best effort parallelism, not a guarantee that we always have the context of the local CPU. Look closely: we pick the "local" CPU with preemption enabled, then contend for the mutex. This may well put us to sleep and get us migrated, so we could be using the context of a CPU we are no longer running on. This is fine because we hold the mutex - if that other CPU tries to use the acomp_ctx, it'll wait for us.

Quoted 6 lines
Quoted 3 lines

However, if we get migrated and vacate the CPU whose context we have locked, the CPU might get offlined and zswap_cpu_comp_dead() can free the context underneath us. I think we need to refcount the acomp_ctx.

I see. Wouldn't it then seem to make the code more fail-safe to not allow the migration to happen until after the check for (src != acomp_ctx->buffer),

Quoted 8 lines

by

Quoted 2 lines

moving the mutex_unlock() after this check? Or, use a boolean to determine if the unmap_handle needs to be done as Yosry suggested?

Hmm does it make it safe? It is mutex_lock() that puts the task to sleep, after which it can get migrated to a different CPU. Moving mutex_unlock() to below or not doesn't really matter, no? Or am I missing something here...

Thanks for the comments, Nhat. I guess my last comment in response to what Johannes mentioned about the UAF situation, is the one that's still an open from my perspective. If the process can get migrated after the mutex unlock, and if the acomp_ctx obtained earlier gets deleted (as Johannes was mentioning), then we could be accessing
an invalid pointer in the "if (src != acomp_ctx->buffer)". So my question was, can we prevent the migration to a different cpu by relinquishing the mutex lock after this conditional; or, make the conditional be determined by a boolean that gets set earlier to decide whether or not to unmap the zpool handle (as against using the acomp_ctx to decide this).

Thanks,
Kanchana

Quoted 2 lines

I think Johannes' proposal is the safest :)

maintainer
#14 lore raw
Quoted 2 lines · Kanchana P

So my question was, can we prevent the migration to a different cpu by relinquishing the mutex lock after this conditional

Holding the mutex doesn't prevent preemption/migration.

#15 lore raw
Quoted 14 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 5 lines
Quoted 2 lines · Kanchana P

So my question was, can we prevent the migration to a different cpu by relinquishing the mutex lock after this conditional

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of a per-cpu structure obtained via raw_cpu_ptr()?

Would holding the mutex prevent the acomp_ctx of the cpu prior to the migration (in the UAF scenario you described) from being deleted?

If holding the per-cpu acomp_ctx's mutex isn't sufficient to prevent the UAF, I agree, we might need a way to prevent the acomp_ctx from being deleted, e.g. with refcounts as you've suggested, or to not use the acomp_ctx at all for the check, instead use a boolean.

Thanks,
Kanchana

#16 lore raw

Hello,

Quoted 14 lines · Kanchana P
Quoted 14 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 8 lines
Quoted 5 lines · Kanchana P
Quoted 2 lines

So my question was, can we prevent the migration to a different cpu by relinquishing the mutex lock after this conditional

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of a per-cpu structure obtained via raw_cpu_ptr()?

Yes, unless you use migration_disable() or cpus_read_lock() to protect this section.

Quoted 3 lines

Would holding the mutex prevent the acomp_ctx of the cpu prior to the migration (in the UAF scenario you described) from being deleted?

No, cpu offline can kick in anytime to free the acomp_ctx->buffer.

Quoted 4 lines

If holding the per-cpu acomp_ctx's mutex isn't sufficient to prevent the UAF, I agree, we might need a way to prevent the acomp_ctx from being deleted, e.g. with refcounts as you've suggested, or to not use the

Right, refcount solution from Johannes is very good IMHO.

Quoted 1 line

acomp_ctx at all for the check, instead use a boolean.

But this is not enough to just avoid using acomp_ctx for the check, the usage of acomp_ctx inside the mutex is also UAF, since cpu offline can kick in anytime to free the acomp_ctx->buffer.

Thanks.

#17 lore raw

Hi Chengming,

Quoted 35 lines

-----Original Message-----
From: Chengming Zhou <[email protected]>
Sent: Wednesday, November 13, 2024 11:24 PM
To: Sridhar, Kanchana P <[email protected]>; Johannes Weiner <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; akpm@linux- foundation.org; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Hello,

Quoted 18 lines · Kanchana P
Quoted 18 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 2 lines · Kanchana P

So my question was, can we prevent the migration to a different cpu by relinquishing the mutex lock after this conditional

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of a per-cpu structure obtained via raw_cpu_ptr()?

Yes, unless you use migration_disable() or cpus_read_lock() to protect this section.

Ok.

Quoted 12 lines
Quoted 4 lines

Would holding the mutex prevent the acomp_ctx of the cpu prior to the migration (in the UAF scenario you described) from being deleted?

No, cpu offline can kick in anytime to free the acomp_ctx->buffer.

Quoted 4 lines

If holding the per-cpu acomp_ctx's mutex isn't sufficient to prevent the UAF, I agree, we might need a way to prevent the acomp_ctx from being deleted, e.g. with refcounts as you've suggested, or to not use the

Quoted 7 lines

Right, refcount solution from Johannes is very good IMHO.

Quoted 1 line

acomp_ctx at all for the check, instead use a boolean.

But this is not enough to just avoid using acomp_ctx for the check, the usage of acomp_ctx inside the mutex is also UAF, since cpu offline can kick in anytime to free the acomp_ctx->buffer.

I see. How would the refcounts work? Would this add latency to zswap ops? In low memory situations, could the cpu offlining code over-ride the refcounts?

Based on Johannes' earlier comments, I don't think it makes sense for me to submit a v2.

Thanks,
Kanchana

Quoted 2 lines

Thanks.

#18 lore raw
Quoted 64 lines · Kanchana P

Hi Chengming,

Quoted 35 lines

-----Original Message-----
From: Chengming Zhou <[email protected]>
Sent: Wednesday, November 13, 2024 11:24 PM
To: Sridhar, Kanchana P <[email protected]>; Johannes Weiner <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; akpm@linux- foundation.org; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Hello,

Quoted 18 lines · Kanchana P
Quoted 18 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 2 lines · Kanchana P

So my question was, can we prevent the migration to a different cpu by relinquishing the mutex lock after this conditional

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of a per-cpu structure obtained via raw_cpu_ptr()?

Yes, unless you use migration_disable() or cpus_read_lock() to protect this section.

Ok.

Quoted 16 lines
Quoted 4 lines

Would holding the mutex prevent the acomp_ctx of the cpu prior to the migration (in the UAF scenario you described) from being deleted?

No, cpu offline can kick in anytime to free the acomp_ctx->buffer.

Quoted 4 lines

If holding the per-cpu acomp_ctx's mutex isn't sufficient to prevent the UAF, I agree, we might need a way to prevent the acomp_ctx from being deleted, e.g. with refcounts as you've suggested, or to not use the

Right, refcount solution from Johannes is very good IMHO.

Quoted 1 line

acomp_ctx at all for the check, instead use a boolean.

Quoted 7 lines
Quoted 3 lines

But this is not enough to just avoid using acomp_ctx for the check, the usage of acomp_ctx inside the mutex is also UAF, since cpu offline can kick in anytime to free the acomp_ctx->buffer.

I see. How would the refcounts work? Would this add latency to zswap ops? In low memory situations, could the cpu offlining code over-ride the refcounts?

I think what Johannes meant is that the zswap compress/decompress paths grab a ref on the acomp_ctx before using it, and the CPU offlining code only drops the initial ref, and does not free the buffer directly. The buffer is only freed when the ref drops to zero.

I am not familiar with CPU hotplug, would it be simpler if we have a wrapper like get_acomp_ctx() that disables migration or calls cpus_read_lock() before grabbing the per-CPU acomp_ctx? A similar wrapper, put_acompt_ctx() will be used after we are done using the acomp_ctx.

Quoted 9 lines

Based on Johannes' earlier comments, I don't think it makes sense for me to submit a v2.

Thanks,
Kanchana

Quoted 2 lines

Thanks.

#19 lore raw
Quoted 98 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Friday, November 15, 2024 1:49 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 7 lines · Kanchana P

Hi Chengming,

Quoted 4 lines

-----Original Message-----
From: Chengming Zhou <[email protected]>
Sent: Wednesday, November 13, 2024 11:24 PM
To: Sridhar, Kanchana P <[email protected]>; Johannes

Weiner

Quoted 4 lines
Quoted 4 lines

<[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected];

Huang,

Quoted 2 lines
Quoted 2 lines

Ying <[email protected]>; [email protected]; akpm@linux- foundation.org; Feghali, Wajdi K <[email protected]>; Gopal,

Vinodh

Quoted 8 lines
Quoted 8 lines

<[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Hello,

Quoted 1 line · Kanchana P
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected];

[email protected];

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

On Thu, Nov 14, 2024 at 01:56:16AM +0000, Sridhar, Kanchana P

Quoted 29 lines · wrote:
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

So my question was, can we prevent the migration to a different cpu by relinquishing the mutex lock after this conditional

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of a per-cpu structure obtained via raw_cpu_ptr()?

Yes, unless you use migration_disable() or cpus_read_lock() to protect this section.

Ok.

Quoted 16 lines
Quoted 4 lines

Would holding the mutex prevent the acomp_ctx of the cpu prior to the migration (in the UAF scenario you described) from being deleted?

No, cpu offline can kick in anytime to free the acomp_ctx->buffer.

Quoted 4 lines

If holding the per-cpu acomp_ctx's mutex isn't sufficient to prevent the UAF, I agree, we might need a way to prevent the acomp_ctx from being deleted, e.g. with refcounts as you've suggested, or to not use the

Right, refcount solution from Johannes is very good IMHO.

Quoted 1 line

acomp_ctx at all for the check, instead use a boolean.

Quoted 7 lines
Quoted 3 lines

But this is not enough to just avoid using acomp_ctx for the check, the usage of acomp_ctx inside the mutex is also UAF, since cpu offline can kick in anytime to free the acomp_ctx->buffer.

I see. How would the refcounts work? Would this add latency to zswap ops? In low memory situations, could the cpu offlining code over-ride the refcounts?

I think what Johannes meant is that the zswap compress/decompress paths grab a ref on the acomp_ctx before using it, and the CPU offlining code only drops the initial ref, and does not free the buffer directly. The buffer is only freed when the ref drops to zero.

Quoted 5 lines

I am not familiar with CPU hotplug, would it be simpler if we have a wrapper like get_acomp_ctx() that disables migration or calls cpus_read_lock() before grabbing the per-CPU acomp_ctx? A similar wrapper, put_acompt_ctx() will be used after we are done using the acomp_ctx.

Would it be sufficient to add a check for mutex_is_locked() in zswap_cpu_comp_dead() and if this returns true, to exit without deleting the acomp? If this is an acceptable solution, it would also require us to move the mutex_unlock() to occur after the "if (src != acomp_ctx->buffer)" in zswap_decompress(). This would ensure all existing zswap code that's within the mutex_lock()-mutex_unlock() will work correctly without worrying about the acomp_ctx being deleted by cpu offlining.

Not sure if this would be a comprehensive solution, or if it would have unintended consequences to the cpu offlining code. Would appreciate comments.

Thanks,
Kanchana

Quoted 10 lines
Quoted 10 lines

Based on Johannes' earlier comments, I don't think it makes sense for me to submit a v2.

Thanks,
Kanchana

Quoted 2 lines

Thanks.

#20 lore raw
Quoted 105 lines · Kanchana P
Quoted 99 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Friday, November 15, 2024 1:49 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 7 lines · Kanchana P

Hi Chengming,

Quoted 4 lines

-----Original Message-----
From: Chengming Zhou <[email protected]>
Sent: Wednesday, November 13, 2024 11:24 PM
To: Sridhar, Kanchana P <[email protected]>; Johannes

Weiner

Quoted 4 lines
Quoted 4 lines

<[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected];

Huang,

Quoted 2 lines
Quoted 2 lines

Ying <[email protected]>; [email protected]; akpm@linux- foundation.org; Feghali, Wajdi K <[email protected]>; Gopal,

Vinodh

Quoted 8 lines
Quoted 8 lines

<[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Hello,

Quoted 1 line · Kanchana P
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected];

[email protected];

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

On Thu, Nov 14, 2024 at 01:56:16AM +0000, Sridhar, Kanchana P

Quoted 29 lines · wrote:
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

So my question was, can we prevent the migration to a different cpu by relinquishing the mutex lock after this conditional

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of a per-cpu structure obtained via raw_cpu_ptr()?

Yes, unless you use migration_disable() or cpus_read_lock() to protect this section.

Ok.

Quoted 16 lines
Quoted 4 lines

Would holding the mutex prevent the acomp_ctx of the cpu prior to the migration (in the UAF scenario you described) from being deleted?

No, cpu offline can kick in anytime to free the acomp_ctx->buffer.

Quoted 4 lines

If holding the per-cpu acomp_ctx's mutex isn't sufficient to prevent the UAF, I agree, we might need a way to prevent the acomp_ctx from being deleted, e.g. with refcounts as you've suggested, or to not use the

Right, refcount solution from Johannes is very good IMHO.

Quoted 1 line

acomp_ctx at all for the check, instead use a boolean.

Quoted 7 lines
Quoted 3 lines

But this is not enough to just avoid using acomp_ctx for the check, the usage of acomp_ctx inside the mutex is also UAF, since cpu offline can kick in anytime to free the acomp_ctx->buffer.

I see. How would the refcounts work? Would this add latency to zswap ops? In low memory situations, could the cpu offlining code over-ride the refcounts?

I think what Johannes meant is that the zswap compress/decompress paths grab a ref on the acomp_ctx before using it, and the CPU offlining code only drops the initial ref, and does not free the buffer directly. The buffer is only freed when the ref drops to zero.

Quoted 5 lines

I am not familiar with CPU hotplug, would it be simpler if we have a wrapper like get_acomp_ctx() that disables migration or calls cpus_read_lock() before grabbing the per-CPU acomp_ctx? A similar wrapper, put_acompt_ctx() will be used after we are done using the acomp_ctx.

Quoted 3 lines

Would it be sufficient to add a check for mutex_is_locked() in zswap_cpu_comp_dead() and if this returns true, to exit without deleting the acomp?

I don't think this works. First of all, it's racy. It's possible the mutex gets locked after we check mutex_is_locked() but before we delete the acomp_ctx. Also, if we find that the mutex is locked, then we do nothing and essentially leak the memory.

Second, and probably more important, this only checks if anyone is currently holding the mutex. What about tasks that may be sleeping waiting for the mutex to be unlocked? The mutex will be deleted from under them as well.

Quoted 12 lines

If this is an acceptable solution, it would also require us
to move the mutex_unlock() to occur after the "if (src != acomp_ctx->buffer)" in zswap_decompress(). This would ensure all existing zswap code that's within the mutex_lock()-mutex_unlock() will work correctly without worrying about the acomp_ctx being deleted by cpu offlining.

Not sure if this would be a comprehensive solution, or if it would have unintended consequences to the cpu offlining code. Would appreciate comments.

Thanks,
Kanchana

#21 lore raw
Quoted 127 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 19, 2024 11:27 AM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 48 lines · Kanchana P
Quoted 48 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Friday, November 15, 2024 1:49 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 7 lines · Kanchana P

Hi Chengming,

Quoted 4 lines

-----Original Message-----
From: Chengming Zhou <[email protected]>
Sent: Wednesday, November 13, 2024 11:24 PM
To: Sridhar, Kanchana P <[email protected]>; Johannes

Weiner

Quoted 4 lines
Quoted 4 lines

<[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected];

Huang,

Quoted 2 lines
Quoted 2 lines

Ying <[email protected]>; [email protected]; akpm@linux- foundation.org; Feghali, Wajdi K <[email protected]>; Gopal,

Vinodh

Quoted 15 lines
Quoted 15 lines

<[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Hello,

Quoted 8 lines · Kanchana P
Quoted 8 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected];

Quoted 4 lines
Quoted 4 lines

[email protected];

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh

<[email protected]>

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

On Thu, Nov 14, 2024 at 01:56:16AM +0000, Sridhar, Kanchana P

Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

So my question was, can we prevent the migration to a different

cpu

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

by relinquishing the mutex lock after this conditional

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of a per-

cpu

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 4 lines
Quoted 1 line

structure obtained via raw_cpu_ptr()?

Yes, unless you use migration_disable() or cpus_read_lock() to protect this section.

Ok.

Quoted 4 lines
Quoted 4 lines

Would holding the mutex prevent the acomp_ctx of the cpu prior to the migration (in the UAF scenario you described) from being

deleted?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

No, cpu offline can kick in anytime to free the acomp_ctx->buffer.

Quoted 2 lines

If holding the per-cpu acomp_ctx's mutex isn't sufficient to prevent

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

UAF, I agree, we might need a way to prevent the acomp_ctx from

being

Quoted 25 lines
Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line

deleted, e.g. with refcounts as you've suggested, or to not use the

Right, refcount solution from Johannes is very good IMHO.

Quoted 1 line

acomp_ctx at all for the check, instead use a boolean.

Quoted 7 lines
Quoted 3 lines

But this is not enough to just avoid using acomp_ctx for the check, the usage of acomp_ctx inside the mutex is also UAF, since cpu offline can kick in anytime to free the acomp_ctx->buffer.

I see. How would the refcounts work? Would this add latency to zswap ops? In low memory situations, could the cpu offlining code over-ride the refcounts?

I think what Johannes meant is that the zswap compress/decompress paths grab a ref on the acomp_ctx before using it, and the CPU offlining code only drops the initial ref, and does not free the buffer directly. The buffer is only freed when the ref drops to zero.

I am not familiar with CPU hotplug, would it be simpler if we have a wrapper like get_acomp_ctx() that disables migration or calls cpus_read_lock() before grabbing the per-CPU acomp_ctx? A similar wrapper, put_acompt_ctx() will be used after we are done using the acomp_ctx.

Quoted 8 lines
Quoted 3 lines

Would it be sufficient to add a check for mutex_is_locked() in zswap_cpu_comp_dead() and if this returns true, to exit without deleting the acomp?

I don't think this works. First of all, it's racy. It's possible the mutex gets locked after we check mutex_is_locked() but before we delete the acomp_ctx. Also, if we find that the mutex is locked, then we do nothing and essentially leak the memory.

Yes, this would assume the cpu offlining code retries at some interval, which could prevent the memory leak.

Quoted 5 lines

Second, and probably more important, this only checks if anyone is currently holding the mutex. What about tasks that may be sleeping waiting for the mutex to be unlocked? The mutex will be deleted from under them as well.

Wouldn't this and the race described above, also be issues for the refcount based approach?

Also, I am wondering if the mutex design already handles cases where tasks are sleeping, waiting for a mutex that disappears?

Thanks,
Kanchana

Quoted 14 lines
Quoted 14 lines

If this is an acceptable solution, it would also require us to move the mutex_unlock() to occur after the "if (src != acomp_ctx- buffer)"
in zswap_decompress(). This would ensure all existing zswap code that's within the mutex_lock()-mutex_unlock() will work correctly without worrying about the acomp_ctx being deleted by cpu offlining.

Not sure if this would be a comprehensive solution, or if it would have unintended consequences to the cpu offlining code. Would appreciate comments.

Thanks,
Kanchana

#22 lore raw
Quoted 132 lines · Kanchana P
Quoted 132 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 19, 2024 11:27 AM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 48 lines · Kanchana P
Quoted 48 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Friday, November 15, 2024 1:49 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 7 lines · Kanchana P

Hi Chengming,

Quoted 4 lines

-----Original Message-----
From: Chengming Zhou <[email protected]>
Sent: Wednesday, November 13, 2024 11:24 PM
To: Sridhar, Kanchana P <[email protected]>; Johannes

Weiner

Quoted 4 lines
Quoted 4 lines

<[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected];

Huang,

Quoted 2 lines
Quoted 2 lines

Ying <[email protected]>; [email protected]; akpm@linux- foundation.org; Feghali, Wajdi K <[email protected]>; Gopal,

Vinodh

Quoted 15 lines
Quoted 15 lines

<[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Hello,

Quoted 8 lines · Kanchana P
Quoted 8 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected];

Quoted 4 lines
Quoted 4 lines

[email protected];

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh

<[email protected]>

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

On Thu, Nov 14, 2024 at 01:56:16AM +0000, Sridhar, Kanchana P

Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

So my question was, can we prevent the migration to a different

cpu

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

by relinquishing the mutex lock after this conditional

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of a per-

cpu

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 4 lines
Quoted 1 line

structure obtained via raw_cpu_ptr()?

Yes, unless you use migration_disable() or cpus_read_lock() to protect this section.

Ok.

Quoted 4 lines
Quoted 4 lines

Would holding the mutex prevent the acomp_ctx of the cpu prior to the migration (in the UAF scenario you described) from being

deleted?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

No, cpu offline can kick in anytime to free the acomp_ctx->buffer.

Quoted 2 lines

If holding the per-cpu acomp_ctx's mutex isn't sufficient to prevent

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

UAF, I agree, we might need a way to prevent the acomp_ctx from

being

Quoted 28 lines
Quoted 19 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line

deleted, e.g. with refcounts as you've suggested, or to not use the

Right, refcount solution from Johannes is very good IMHO.

Quoted 1 line

acomp_ctx at all for the check, instead use a boolean.

Quoted 7 lines
Quoted 3 lines

But this is not enough to just avoid using acomp_ctx for the check, the usage of acomp_ctx inside the mutex is also UAF, since cpu offline can kick in anytime to free the acomp_ctx->buffer.

I see. How would the refcounts work? Would this add latency to zswap ops? In low memory situations, could the cpu offlining code over-ride the refcounts?

I think what Johannes meant is that the zswap compress/decompress paths grab a ref on the acomp_ctx before using it, and the CPU offlining code only drops the initial ref, and does not free the buffer directly. The buffer is only freed when the ref drops to zero.

Quoted 5 lines

I am not familiar with CPU hotplug, would it be simpler if we have a wrapper like get_acomp_ctx() that disables migration or calls cpus_read_lock() before grabbing the per-CPU acomp_ctx? A similar wrapper, put_acompt_ctx() will be used after we are done using the acomp_ctx.

Would it be sufficient to add a check for mutex_is_locked() in zswap_cpu_comp_dead() and if this returns true, to exit without deleting the acomp?

Quoted 7 lines
Quoted 4 lines

I don't think this works. First of all, it's racy. It's possible the mutex gets locked after we check mutex_is_locked() but before we delete the acomp_ctx. Also, if we find that the mutex is locked, then we do nothing and essentially leak the memory.

Yes, this would assume the cpu offlining code retries at some interval, which could prevent the memory leak.

I am not sure about that, but even so, it wouldn't handle the first scenario where the mutex gets locked after we check mutex_is_locked().

Quoted 9 lines
Quoted 6 lines

Second, and probably more important, this only checks if anyone is currently holding the mutex. What about tasks that may be sleeping waiting for the mutex to be unlocked? The mutex will be deleted from under them as well.

Wouldn't this and the race described above, also be issues for the refcount based approach?

I don't think so, at least if implemented correctly. There are a lot of examples around the kernel that use RCU + refcounts for such use cases. I think there are also some examples in kernel docs.

That being said, I am wondering if we can get away with something simpler like holding the cpus read lock or disabling migration as I suggested earlier, but I am not quite sure.

Quoted 3 lines

Also, I am wondering if the mutex design already handles cases where tasks are sleeping, waiting for a mutex that disappears?

I don't believe so. It doesn't make sense for someone to free a mutex while someone is waiting for it. How would the waiter know if the memory backing the mutex was freed?

#23 lore raw
Quoted 188 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 19, 2024 11:51 AM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 19 lines · Kanchana P
Quoted 19 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 19, 2024 11:27 AM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 2 lines · Kanchana P
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Friday, November 15, 2024 1:49 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected];

[email protected];

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh

<[email protected]>

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 1 line · Kanchana P
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Hi Chengming,

Quoted 4 lines

-----Original Message-----
From: Chengming Zhou <[email protected]>
Sent: Wednesday, November 13, 2024 11:24 PM
To: Sridhar, Kanchana P <[email protected]>;

Johannes

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Weiner

Quoted 4 lines
Quoted 4 lines

<[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected];

[email protected];

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Huang,

Quoted 2 lines
Quoted 2 lines

Ying <[email protected]>; [email protected]; akpm@linux- foundation.org; Feghali, Wajdi K <[email protected]>;

Gopal,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Vinodh

Quoted 6 lines
Quoted 6 lines

<[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Hello,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines · Kanchana P
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected];

linux-

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]; [email protected];

[email protected];

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali,

Wajdi K

Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; Gopal, Vinodh

<[email protected]>

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory

leak in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

zswap_decompress().

On Thu, Nov 14, 2024 at 01:56:16AM +0000, Sridhar, Kanchana

P

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

So my question was, can we prevent the migration to a

different

Quoted 6 lines
Quoted 6 lines

cpu

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

by relinquishing the mutex lock after this conditional

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of a

per-

Quoted 4 lines
Quoted 4 lines

cpu

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

structure obtained via raw_cpu_ptr()?

Yes, unless you use migration_disable() or cpus_read_lock() to

protect

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

this section.

Ok.

Quoted 3 lines
Quoted 3 lines

Would holding the mutex prevent the acomp_ctx of the cpu prior

to

Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the migration (in the UAF scenario you described) from being

deleted?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

No, cpu offline can kick in anytime to free the acomp_ctx->buffer.

Quoted 2 lines

If holding the per-cpu acomp_ctx's mutex isn't sufficient to

prevent

Quoted 2 lines
Quoted 2 lines

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

UAF, I agree, we might need a way to prevent the acomp_ctx

from

Quoted 2 lines
Quoted 2 lines

being

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

deleted, e.g. with refcounts as you've suggested, or to not use

the

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Right, refcount solution from Johannes is very good IMHO.

Quoted 1 line

acomp_ctx at all for the check, instead use a boolean.

But this is not enough to just avoid using acomp_ctx for the check, the usage of acomp_ctx inside the mutex is also UAF, since cpu

offline

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

can kick in anytime to free the acomp_ctx->buffer.

I see. How would the refcounts work? Would this add latency to

zswap

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

ops? In low memory situations, could the cpu offlining code over-ride the refcounts?

I think what Johannes meant is that the zswap compress/decompress paths grab a ref on the acomp_ctx before using it, and the CPU offlining code only drops the initial ref, and does not free the buffer directly. The buffer is only freed when the ref drops to zero.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

I am not familiar with CPU hotplug, would it be simpler if we have a wrapper like get_acomp_ctx() that disables migration or calls cpus_read_lock() before grabbing the per-CPU acomp_ctx? A similar wrapper, put_acompt_ctx() will be used after we are done using the acomp_ctx.

Would it be sufficient to add a check for mutex_is_locked() in zswap_cpu_comp_dead() and if this returns true, to exit without

deleting

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

the acomp?

I don't think this works. First of all, it's racy. It's possible the mutex gets locked after we check mutex_is_locked() but before we delete the acomp_ctx. Also, if we find that the mutex is locked, then we do nothing and essentially leak the memory.

Yes, this would assume the cpu offlining code retries at some interval, which could prevent the memory leak.

I am not sure about that, but even so, it wouldn't handle the first scenario where the mutex gets locked after we check mutex_is_locked().

Quoted 9 lines
Quoted 6 lines

Second, and probably more important, this only checks if anyone is currently holding the mutex. What about tasks that may be sleeping waiting for the mutex to be unlocked? The mutex will be deleted from under them as well.

Wouldn't this and the race described above, also be issues for the refcount based approach?

Quoted 7 lines

I don't think so, at least if implemented correctly. There are a lot of examples around the kernel that use RCU + refcounts for such use cases. I think there are also some examples in kernel docs.

That being said, I am wondering if we can get away with something simpler like holding the cpus read lock or disabling migration as I suggested earlier, but I am not quite sure.

Another idea to consider is how zsmalloc avoids this issue through its use of the local_lock() on the per-cpu mapping area. This disables preemption from zs_map_object() through zs_unmap_object(). Would changing the acomp_ctx's mutex to a local_lock solve the problem?

Quoted 8 lines
Quoted 4 lines

Also, I am wondering if the mutex design already handles cases where tasks are sleeping, waiting for a mutex that disappears?

I don't believe so. It doesn't make sense for someone to free a mutex while someone is waiting for it. How would the waiter know if the memory backing the mutex was freed?

Thanks Yosry, all good points. There would need to be some sort of arbiter (for e.g., the cpu offlining code) that would reschedule tasks running on a cpu before shutting it down, which could address this specific issue. I was thinking these are not problems unique to zswap's per-cpu acomp_ctx->mutex wrt the offlining?

Thanks,
Kanchana

#24 lore raw
Quoted 197 lines · Kanchana P
Quoted 189 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 19, 2024 11:51 AM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 19 lines · Kanchana P
Quoted 19 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 19, 2024 11:27 AM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 2 lines · Kanchana P
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Friday, November 15, 2024 1:49 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected];

[email protected];

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh

<[email protected]>

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 1 line · Kanchana P
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Hi Chengming,

Quoted 4 lines

-----Original Message-----
From: Chengming Zhou <[email protected]>
Sent: Wednesday, November 13, 2024 11:24 PM
To: Sridhar, Kanchana P <[email protected]>;

Johannes

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Weiner

Quoted 4 lines
Quoted 4 lines

<[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected];

[email protected];

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Huang,

Quoted 2 lines
Quoted 2 lines

Ying <[email protected]>; [email protected]; akpm@linux- foundation.org; Feghali, Wajdi K <[email protected]>;

Gopal,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Vinodh

Quoted 6 lines
Quoted 6 lines

<[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Hello,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines · Kanchana P
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected];

linux-

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]; [email protected];

[email protected];

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali,

Wajdi K

Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; Gopal, Vinodh

<[email protected]>

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory

leak in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

zswap_decompress().

On Thu, Nov 14, 2024 at 01:56:16AM +0000, Sridhar, Kanchana

P

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

So my question was, can we prevent the migration to a

different

Quoted 6 lines
Quoted 6 lines

cpu

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

by relinquishing the mutex lock after this conditional

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of a

per-

Quoted 4 lines
Quoted 4 lines

cpu

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

structure obtained via raw_cpu_ptr()?

Yes, unless you use migration_disable() or cpus_read_lock() to

protect

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

this section.

Ok.

Quoted 3 lines
Quoted 3 lines

Would holding the mutex prevent the acomp_ctx of the cpu prior

to

Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the migration (in the UAF scenario you described) from being

deleted?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

No, cpu offline can kick in anytime to free the acomp_ctx->buffer.

Quoted 2 lines

If holding the per-cpu acomp_ctx's mutex isn't sufficient to

prevent

Quoted 2 lines
Quoted 2 lines

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

UAF, I agree, we might need a way to prevent the acomp_ctx

from

Quoted 2 lines
Quoted 2 lines

being

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

deleted, e.g. with refcounts as you've suggested, or to not use

the

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Right, refcount solution from Johannes is very good IMHO.

Quoted 1 line

acomp_ctx at all for the check, instead use a boolean.

But this is not enough to just avoid using acomp_ctx for the check, the usage of acomp_ctx inside the mutex is also UAF, since cpu

offline

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

can kick in anytime to free the acomp_ctx->buffer.

I see. How would the refcounts work? Would this add latency to

zswap

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

ops? In low memory situations, could the cpu offlining code over-ride the refcounts?

I think what Johannes meant is that the zswap compress/decompress paths grab a ref on the acomp_ctx before using it, and the CPU offlining code only drops the initial ref, and does not free the buffer directly. The buffer is only freed when the ref drops to zero.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

I am not familiar with CPU hotplug, would it be simpler if we have a wrapper like get_acomp_ctx() that disables migration or calls cpus_read_lock() before grabbing the per-CPU acomp_ctx? A similar wrapper, put_acompt_ctx() will be used after we are done using the acomp_ctx.

Would it be sufficient to add a check for mutex_is_locked() in zswap_cpu_comp_dead() and if this returns true, to exit without

deleting

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

the acomp?

I don't think this works. First of all, it's racy. It's possible the mutex gets locked after we check mutex_is_locked() but before we delete the acomp_ctx. Also, if we find that the mutex is locked, then we do nothing and essentially leak the memory.

Yes, this would assume the cpu offlining code retries at some interval, which could prevent the memory leak.

I am not sure about that, but even so, it wouldn't handle the first scenario where the mutex gets locked after we check mutex_is_locked().

Quoted 9 lines
Quoted 6 lines

Second, and probably more important, this only checks if anyone is currently holding the mutex. What about tasks that may be sleeping waiting for the mutex to be unlocked? The mutex will be deleted from under them as well.

Wouldn't this and the race described above, also be issues for the refcount based approach?

Quoted 7 lines

I don't think so, at least if implemented correctly. There are a lot of examples around the kernel that use RCU + refcounts for such use cases. I think there are also some examples in kernel docs.

That being said, I am wondering if we can get away with something simpler like holding the cpus read lock or disabling migration as I suggested earlier, but I am not quite sure.

Quoted 5 lines

Another idea to consider is how zsmalloc avoids this issue through its use of the local_lock() on the per-cpu mapping area. This disables preemption from zs_map_object() through zs_unmap_object(). Would changing the acomp_ctx's mutex to a local_lock solve the problem?

This is similar to disabling migration as I suggested, but disabling preemption means that we cannot sleep, we spin on a lock instead.

In zswap_compress(), we send the compression request and may sleep waiting for it. We also make a non-atomic allocation later that may also sleep but that's less of a problem.

In zswap_decompress() we may also sleep, which is why we sometimes copy the data into acomp_ctx->buffer and unmap the handle to begin with.

So I don't think we can just replace the mutex with a lock.

Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

Also, I am wondering if the mutex design already handles cases where tasks are sleeping, waiting for a mutex that disappears?

I don't believe so. It doesn't make sense for someone to free a mutex while someone is waiting for it. How would the waiter know if the memory backing the mutex was freed?

Quoted 5 lines

Thanks Yosry, all good points. There would need to be some sort of arbiter (for e.g., the cpu offlining code) that would reschedule tasks running on a cpu before shutting it down, which could address this specific issue. I was thinking these are not problems unique to zswap's per-cpu acomp_ctx->mutex wrt the offlining?

There are a few reasons why zswap has this problem and other code may not have it. For example the data structure is dynamically allocated and is freed during offlining, it wouldn't be a problem if it was static. Also the fact that we don't disable preemption when accessing the per-CPU data, as I mentioned earlier, which would prevent the CPU we are running on from going offline while we access the per-CPU data.

I think we should either:
(a) Use refcounts.
(b) Disable migration.
(c) Hold the CPUs read lock.

I was hoping someone with more knowledge about CPU offlining would confirm (b) and (c) would work, but I am pretty confident they would.

#25 lore raw
Quoted 247 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 19, 2024 3:45 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 10 lines · Kanchana P
Quoted 10 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 19, 2024 11:51 AM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; akpm@linux-

foundation.org;

Quoted 9 lines
Quoted 9 lines

Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 2 lines · Kanchana P
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 19, 2024 11:27 AM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected];

[email protected];

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh

<[email protected]>

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 2 lines · Kanchana P
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Friday, November 15, 2024 1:49 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes

Weiner

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; Nhat Pham <[email protected]>;

linux-

Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]; [email protected];

[email protected];

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi

K

Quoted 14 lines
Quoted 14 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; Gopal, Vinodh

<[email protected]>

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 7 lines · Kanchana P

Hi Chengming,

Quoted 4 lines

-----Original Message-----
From: Chengming Zhou <[email protected]>
Sent: Wednesday, November 13, 2024 11:24 PM
To: Sridhar, Kanchana P <[email protected]>;

Quoted 5 lines
Quoted 5 lines

Johannes

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Weiner

Quoted 3 lines
Quoted 3 lines

<[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected];

linux-

Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]; [email protected];

[email protected];

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Huang,

Quoted 1 line
Quoted 1 line

Ying <[email protected]>; [email protected];

akpm@linux-

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

foundation.org; Feghali, Wajdi K <[email protected]>;

Gopal,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Vinodh

Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory

leak in

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

zswap_decompress().

Hello,

Quoted 7 lines · Kanchana P
Quoted 7 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected];

Quoted 4 lines
Quoted 4 lines

linux-

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]; [email protected];

[email protected];

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]; Huang, Ying

<[email protected]>;

Quoted 9 lines
Quoted 9 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]; [email protected]; Feghali,

Wajdi K

Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; Gopal, Vinodh

<[email protected]>

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory

leak in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

zswap_decompress().

On Thu, Nov 14, 2024 at 01:56:16AM +0000, Sridhar,

Kanchana

Quoted 10 lines
Quoted 10 lines

P

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

So my question was, can we prevent the migration to a

different

Quoted 6 lines
Quoted 6 lines

cpu

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

by relinquishing the mutex lock after this conditional

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of

a

Quoted 5 lines
Quoted 5 lines

per-

Quoted 4 lines
Quoted 4 lines

cpu

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

structure obtained via raw_cpu_ptr()?

Yes, unless you use migration_disable() or cpus_read_lock() to

Quoted 8 lines
Quoted 8 lines

protect

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

this section.

Ok.

Quoted 3 lines
Quoted 3 lines

Would holding the mutex prevent the acomp_ctx of the cpu

prior

Quoted 15 lines
Quoted 5 lines

to

Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the migration (in the UAF scenario you described) from being

deleted?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

No, cpu offline can kick in anytime to free the acomp_ctx-

buffer.

Quoted 9 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

If holding the per-cpu acomp_ctx's mutex isn't sufficient to

prevent

Quoted 2 lines
Quoted 2 lines

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

UAF, I agree, we might need a way to prevent the acomp_ctx

from

Quoted 2 lines
Quoted 2 lines

being

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

deleted, e.g. with refcounts as you've suggested, or to not

use

Quoted 7 lines
Quoted 7 lines

the

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Right, refcount solution from Johannes is very good IMHO.

Quoted 1 line

acomp_ctx at all for the check, instead use a boolean.

But this is not enough to just avoid using acomp_ctx for the

check,

Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the usage of acomp_ctx inside the mutex is also UAF, since cpu

offline

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

can kick in anytime to free the acomp_ctx->buffer.

I see. How would the refcounts work? Would this add latency to

zswap

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

ops? In low memory situations, could the cpu offlining code over-

ride

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

the refcounts?

I think what Johannes meant is that the zswap

compress/decompress

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

paths grab a ref on the acomp_ctx before using it, and the CPU offlining code only drops the initial ref, and does not free the buffer directly. The buffer is only freed when the ref drops to zero.

I am not familiar with CPU hotplug, would it be simpler if we have

a

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

wrapper like get_acomp_ctx() that disables migration or calls cpus_read_lock() before grabbing the per-CPU acomp_ctx? A

similar

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

wrapper, put_acompt_ctx() will be used after we are done using

the

Quoted 36 lines
Quoted 28 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

acomp_ctx.

Would it be sufficient to add a check for mutex_is_locked() in zswap_cpu_comp_dead() and if this returns true, to exit without

deleting

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

the acomp?

I don't think this works. First of all, it's racy. It's possible the mutex gets locked after we check mutex_is_locked() but before we delete the acomp_ctx. Also, if we find that the mutex is locked, then we do nothing and essentially leak the memory.

Yes, this would assume the cpu offlining code retries at some interval, which could prevent the memory leak.

I am not sure about that, but even so, it wouldn't handle the first scenario where the mutex gets locked after we check mutex_is_locked().

Quoted 9 lines
Quoted 6 lines

Second, and probably more important, this only checks if anyone is currently holding the mutex. What about tasks that may be sleeping waiting for the mutex to be unlocked? The mutex will be deleted from under them as well.

Wouldn't this and the race described above, also be issues for the refcount based approach?

Quoted 7 lines

I don't think so, at least if implemented correctly. There are a lot of examples around the kernel that use RCU + refcounts for such use cases. I think there are also some examples in kernel docs.

That being said, I am wondering if we can get away with something simpler like holding the cpus read lock or disabling migration as I suggested earlier, but I am not quite sure.

Quoted 5 lines

Another idea to consider is how zsmalloc avoids this issue through its use of the local_lock() on the per-cpu mapping area. This disables preemption from zs_map_object() through zs_unmap_object(). Would changing the acomp_ctx's mutex to a local_lock solve the problem?

This is similar to disabling migration as I suggested, but disabling preemption means that we cannot sleep, we spin on a lock instead.

In zswap_compress(), we send the compression request and may sleep waiting for it. We also make a non-atomic allocation later that may also sleep but that's less of a problem.

Quoted 5 lines

In zswap_decompress() we may also sleep, which is why we sometimes copy the data into acomp_ctx->buffer and unmap the handle to begin with.

So I don't think we can just replace the mutex with a lock.

Thanks Yosry, for the explanations. Yes, I understand and agree.

Quoted 24 lines
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

Also, I am wondering if the mutex design already handles cases where tasks are sleeping, waiting for a mutex that disappears?

I don't believe so. It doesn't make sense for someone to free a mutex while someone is waiting for it. How would the waiter know if the memory backing the mutex was freed?

Quoted 5 lines

Thanks Yosry, all good points. There would need to be some sort of arbiter (for e.g., the cpu offlining code) that would reschedule tasks running on a cpu before shutting it down, which could address this specific issue. I was thinking these are not problems unique to zswap's per-cpu acomp_ctx->mutex wrt the offlining?

There are a few reasons why zswap has this problem and other code may not have it. For example the data structure is dynamically allocated and is freed during offlining, it wouldn't be a problem if it was static. Also the fact that we don't disable preemption when accessing the per-CPU data, as I mentioned earlier, which would prevent the CPU we are running on from going offline while we access the per-CPU data.

Quoted 7 lines

I think we should either:
(a) Use refcounts.
(b) Disable migration.
(c) Hold the CPUs read lock.

I was hoping someone with more knowledge about CPU offlining would confirm (b) and (c) would work, but I am pretty confident they would.

Ok. Thanks again for the explanations.

Thanks,
Kanchana

#26 lore raw
In reply to Yosry Ahmed
Quoted 241 lines · Yosry Ahmed
Quoted 197 lines · Kanchana P
Quoted 189 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 19, 2024 11:51 AM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 19 lines · Kanchana P
Quoted 19 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, November 19, 2024 11:27 AM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh <[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 2 lines · Kanchana P
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Friday, November 15, 2024 1:49 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Chengming Zhou <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; linux- [email protected]; [email protected];

[email protected];

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>; Gopal, Vinodh

<[email protected]>

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Quoted 1 line · Kanchana P
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Hi Chengming,

Quoted 4 lines

-----Original Message-----
From: Chengming Zhou <[email protected]>
Sent: Wednesday, November 13, 2024 11:24 PM
To: Sridhar, Kanchana P <[email protected]>;

Johannes

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Weiner

Quoted 4 lines
Quoted 4 lines

<[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected]; linux- [email protected]; [email protected];

[email protected];

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Huang,

Quoted 2 lines
Quoted 2 lines

Ying <[email protected]>; [email protected]; akpm@linux- foundation.org; Feghali, Wajdi K <[email protected]>;

Gopal,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Vinodh

Quoted 6 lines
Quoted 6 lines

<[email protected]>
Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().

Hello,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines · Kanchana P
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Johannes Weiner <[email protected]>
Sent: Wednesday, November 13, 2024 9:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Nhat Pham <[email protected]>; Yosry Ahmed <[email protected]>; [email protected];

linux-

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]; [email protected];

[email protected];

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[email protected]; Huang, Ying <[email protected]>; [email protected]; [email protected]; Feghali,

Wajdi K

Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; Gopal, Vinodh

<[email protected]>

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Subject: Re: [PATCH v1] mm: zswap: Fix a potential memory

leak in

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

zswap_decompress().

On Thu, Nov 14, 2024 at 01:56:16AM +0000, Sridhar, Kanchana

P

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

So my question was, can we prevent the migration to a

different

Quoted 6 lines
Quoted 6 lines

cpu

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

by relinquishing the mutex lock after this conditional

Holding the mutex doesn't prevent preemption/migration.

Sure, however, is this also applicable to holding the mutex of a

per-

Quoted 4 lines
Quoted 4 lines

cpu

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

structure obtained via raw_cpu_ptr()?

Yes, unless you use migration_disable() or cpus_read_lock() to

protect

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

this section.

Ok.

Quoted 3 lines
Quoted 3 lines

Would holding the mutex prevent the acomp_ctx of the cpu prior

to

Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the migration (in the UAF scenario you described) from being

deleted?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

No, cpu offline can kick in anytime to free the acomp_ctx->buffer.

Quoted 2 lines

If holding the per-cpu acomp_ctx's mutex isn't sufficient to

prevent

Quoted 2 lines
Quoted 2 lines

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

UAF, I agree, we might need a way to prevent the acomp_ctx

from

Quoted 2 lines
Quoted 2 lines

being

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

deleted, e.g. with refcounts as you've suggested, or to not use

the

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Right, refcount solution from Johannes is very good IMHO.

Quoted 1 line

acomp_ctx at all for the check, instead use a boolean.

But this is not enough to just avoid using acomp_ctx for the check, the usage of acomp_ctx inside the mutex is also UAF, since cpu

offline

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

can kick in anytime to free the acomp_ctx->buffer.

I see. How would the refcounts work? Would this add latency to

zswap

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

ops? In low memory situations, could the cpu offlining code over-ride the refcounts?

I think what Johannes meant is that the zswap compress/decompress paths grab a ref on the acomp_ctx before using it, and the CPU offlining code only drops the initial ref, and does not free the buffer directly. The buffer is only freed when the ref drops to zero.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

I am not familiar with CPU hotplug, would it be simpler if we have a wrapper like get_acomp_ctx() that disables migration or calls cpus_read_lock() before grabbing the per-CPU acomp_ctx? A similar wrapper, put_acompt_ctx() will be used after we are done using the acomp_ctx.

Would it be sufficient to add a check for mutex_is_locked() in zswap_cpu_comp_dead() and if this returns true, to exit without

deleting

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

the acomp?

I don't think this works. First of all, it's racy. It's possible the mutex gets locked after we check mutex_is_locked() but before we delete the acomp_ctx. Also, if we find that the mutex is locked, then we do nothing and essentially leak the memory.

Yes, this would assume the cpu offlining code retries at some interval, which could prevent the memory leak.

I am not sure about that, but even so, it wouldn't handle the first scenario where the mutex gets locked after we check mutex_is_locked().

Quoted 9 lines
Quoted 6 lines

Second, and probably more important, this only checks if anyone is currently holding the mutex. What about tasks that may be sleeping waiting for the mutex to be unlocked? The mutex will be deleted from under them as well.

Wouldn't this and the race described above, also be issues for the refcount based approach?

Quoted 7 lines

I don't think so, at least if implemented correctly. There are a lot of examples around the kernel that use RCU + refcounts for such use cases. I think there are also some examples in kernel docs.

That being said, I am wondering if we can get away with something simpler like holding the cpus read lock or disabling migration as I suggested earlier, but I am not quite sure.

Quoted 5 lines

Another idea to consider is how zsmalloc avoids this issue through its use of the local_lock() on the per-cpu mapping area. This disables preemption from zs_map_object() through zs_unmap_object(). Would changing the acomp_ctx's mutex to a local_lock solve the problem?

This is similar to disabling migration as I suggested, but disabling preemption means that we cannot sleep, we spin on a lock instead.

In zswap_compress(), we send the compression request and may sleep waiting for it. We also make a non-atomic allocation later that may also sleep but that's less of a problem.

In zswap_decompress() we may also sleep, which is why we sometimes copy the data into acomp_ctx->buffer and unmap the handle to begin with.

So I don't think we can just replace the mutex with a lock.

Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

Also, I am wondering if the mutex design already handles cases where tasks are sleeping, waiting for a mutex that disappears?

I don't believe so. It doesn't make sense for someone to free a mutex while someone is waiting for it. How would the waiter know if the memory backing the mutex was freed?

Quoted 5 lines

Thanks Yosry, all good points. There would need to be some sort of arbiter (for e.g., the cpu offlining code) that would reschedule tasks running on a cpu before shutting it down, which could address this specific issue. I was thinking these are not problems unique to zswap's per-cpu acomp_ctx->mutex wrt the offlining?

There are a few reasons why zswap has this problem and other code may not have it. For example the data structure is dynamically allocated and is freed during offlining, it wouldn't be a problem if it was static. Also the fact that we don't disable preemption when accessing the per-CPU data, as I mentioned earlier, which would prevent the CPU we are running on from going offline while we access the per-CPU data.

Quoted 7 lines

I think we should either:
(a) Use refcounts.
(b) Disable migration.
(c) Hold the CPUs read lock.

I was hoping someone with more knowledge about CPU offlining would confirm (b) and (c) would work, but I am pretty confident they would.

+1, I also think (b) or (c) should work with my limited knowledge about CPU offlining :-) And they seem simpler than refcounts implementation.

Thanks.