Skip to contentSkip to conversation

mm: kvfree the swap cluster info if the swap file is unsatisfactory

PATCH Applied Darrick J. Wong 4 participants 5 messages Aug 31, 2017 – Sep 4, 2017

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

PATCH 1/1 mm: kvfree the swap cluster info if the swap file is unsatisfactory

If initializing a small swap file fails because the swap file has a problem (holes, etc.) then we need to free the cluster info as part of cleanup. Unfortunately a previous patch changed the code to use kvzalloc but did not change all the vfree calls to use kvfree.

Found by running generic/357 from xfstests.

---
 mm/swapfile.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/swapfile.c +1 -1
index 6ba4aab..c1deb01 100644
@@ -3052,7 +3052,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	p->flags = 0;
 	spin_unlock(&swap_lock);
 	vfree(swap_map);
-	vfree(cluster_info);
+	kvfree(cluster_info);
 	if (swap_file) {
 		if (inode && S_ISREG(inode->i_mode)) {
 			inode_unlock(inode);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

[email protected] liked your message with Boxer for Android.

On Aug 31, 2017 7:36 PM, "Darrick J. Wong" <[email protected]> wrote:

If initializing a small swap file fails because the swap file has a problem (holes, etc.) then we need to free the cluster info as part of cleanup. Unfortunately a previous patch changed the code to use kvzalloc but did not change all the vfree calls to use kvfree.

Found by running generic/357 from xfstests.

---
mm/swapfile.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
mm/swapfile.c +0 -0
index 6ba4aab..c1deb01 100644
@@ -3052,7 +3052,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)

p->flags = 0;
spin_unlock(&swap_lock);
vfree(swap_map);
-vfree(cluster_info);
+kvfree(cluster_info);
if (swap_file) {
if (inode && S_ISREG(inode->i_mode)) {
inode_unlock(inode);

Reviewed-by on v1 1/1
#2 lore raw
In reply to Darrick J. Wong
Quoted 8 lines · Darrick J. Wong

If initializing a small swap file fails because the swap file has a problem (holes, etc.) then we need to free the cluster info as part of cleanup. Unfortunately a previous patch changed the code to use kvzalloc but did not change all the vfree calls to use kvfree.

Found by running generic/357 from xfstests.

Thanks for fixing!

Best Regards,
Huang, Ying

Quoted 9 lines
---
 mm/swapfile.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/swapfile.c +0 -0
index 6ba4aab..c1deb01 100644

mm/swapfile.c SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) · line 3058

Quoted 8 lines
mm/swapfile.c +1 -1
@@ -3052,7 +3052,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	p->flags = 0;
 	spin_unlock(&swap_lock);
 	vfree(swap_map);
-	vfree(cluster_info);
+	kvfree(cluster_info);
 	if (swap_file) {
 		if (inode && S_ISREG(inode->i_mode)) {
 			inode_unlock(inode);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by on v1 1/1
#3 lore raw
In reply to Darrick J. Wong
Quoted 5 lines · Darrick J. Wong

If initializing a small swap file fails because the swap file has a problem (holes, etc.) then we need to free the cluster info as part of cleanup. Unfortunately a previous patch changed the code to use kvzalloc but did not change all the vfree calls to use kvfree.

Hopefully this can make it into 4.13.

Fixes 54f180d3c181 ("mm, swap: use kvzalloc to allocate some swap data structures")
Quoted 3 lines

Found by running generic/357 from xfstests.

But I think there's also a memory leak and we need this on top of your fix:

mm, swapfile: fix swapon frontswap_map memory leak on error

Free frontswap_map if an error is encountered before enable_swap_info().

---
 mm/swapfile.c | 1 +
 1 file changed, 1 insertion(+)
mm/swapfile.c +1 -0
@@ -3053,6 +3053,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	spin_unlock(&swap_lock);
 	vfree(swap_map);
 	kvfree(cluster_info);
+	kvfree(frontswap_map);
 	if (swap_file) {
 		if (inode && S_ISREG(inode->i_mode)) {
 			inode_unlock(inode);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Reviewed-by
#4 lore raw
Quoted 36 lines · David Rientjes
Quoted 5 lines · Darrick J. Wong

If initializing a small swap file fails because the swap file has a problem (holes, etc.) then we need to free the cluster info as part of cleanup. Unfortunately a previous patch changed the code to use kvzalloc but did not change all the vfree calls to use kvfree.

Hopefully this can make it into 4.13.

Fixes 54f180d3c181 ("mm, swap: use kvzalloc to allocate some swap data structures")
Quoted 3 lines

Found by running generic/357 from xfstests.

But I think there's also a memory leak and we need this on top of your fix:

mm, swapfile: fix swapon frontswap_map memory leak on error

Free frontswap_map if an error is encountered before enable_swap_info().

---
 mm/swapfile.c | 1 +
 1 file changed, 1 insertion(+)

mm/swapfile.c SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) · line 3059

Quoted 7 lines
mm/swapfile.c +1 -0
@@ -3053,6 +3053,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	spin_unlock(&swap_lock);
 	vfree(swap_map);
 	kvfree(cluster_info);
+	kvfree(frontswap_map);
 	if (swap_file) {
 		if (inode && S_ISREG(inode->i_mode)) {
 			inode_unlock(inode);

Yes. There is a memory leak.

Best Regards,
Huang, Ying

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>