Skip to contentSkip to conversation

swiotlb: Do not set total_used to 0 in swiotlb_create_debugfs_files()

PATCH Applied Dexuan Cui 6 participants 9 messages Mar 29, 2024 – Apr 2, 2024

Subsystems (2)
lore
Share & tools
lore

Dexuan Cui

author
#0 lore raw

PATCH 1/1 swiotlb: Do not set total_used to 0 in swiotlb_create_debugfs_files()

Sometimes the readout of /sys/kernel/debug/swiotlb/io_tlb_used and io_tlb_used_hiwater can be a huge number (e.g. 18446744073709551615), which is actually a negative number if we use "%ld" to print the number.

When swiotlb_create_default_debugfs() is running from late_initcall, mem->total_used may already be non-zero, because the storage driver may have already started to perform I/O operations: if the storage driver is built-in, its probe() callback is called before late_initcall.

swiotlb_create_debugfs_files() should not blindly set mem->total_used and mem->used_hiwater to 0; actually it doesn't have to initialize the fields at all, because the fields, as part of the global struct io_tlb_default_mem, have been implicitly initialized to zero.

Also don't explicitly set mem->transient_nslabs to 0.

Fixes 8b0977ecc8b3 ("swiotlb: track and report io_tlb_used high water marks in debugfs")
Fixes 02e765697038 ("swiotlb: add debugfs to track swiotlb transient pool usage")
---
 kernel/dma/swiotlb.c | 4 ----
 1 file changed, 4 deletions(-)
kernel/dma/swiotlb.c +0 -4
index 86fe172b5958..4a9f02c13da6 100644
@@ -1647,9 +1647,6 @@ DEFINE_DEBUGFS_ATTRIBUTE(fops_io_tlb_hiwater, io_tlb_hiwater_get,
 static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem,
 					 const char *dirname)
 {
-	atomic_long_set(&mem->total_used, 0);
-	atomic_long_set(&mem->used_hiwater, 0);
-
 	mem->debugfs = debugfs_create_dir(dirname, io_tlb_default_mem.debugfs);
 	if (!mem->nslabs)
 		return;
@@ -1660,7 +1657,6 @@ static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem,
 	debugfs_create_file("io_tlb_used_hiwater", 0600, mem->debugfs, mem,
 			&fops_io_tlb_hiwater);
 #ifdef CONFIG_SWIOTLB_DYNAMIC
-	atomic_long_set(&mem->transient_nslabs, 0);
 	debugfs_create_file("io_tlb_transient_nslabs", 0400, mem->debugfs,
 			    mem, &fops_io_tlb_transient_used);
 #endif
Signature · 1 line
2.34.1
#1 lore raw
Quoted 16 lines · Dexuan Cui

Sometimes the readout of /sys/kernel/debug/swiotlb/io_tlb_used and io_tlb_used_hiwater can be a huge number (e.g. 18446744073709551615), which is actually a negative number if we use "%ld" to print the number.

When swiotlb_create_default_debugfs() is running from late_initcall, mem->total_used may already be non-zero, because the storage driver may have already started to perform I/O operations: if the storage driver is built-in, its probe() callback is called before late_initcall.

swiotlb_create_debugfs_files() should not blindly set mem->total_used and mem->used_hiwater to 0; actually it doesn't have to initialize the fields at all, because the fields, as part of the global struct io_tlb_default_mem, have been implicitly initialized to zero.

Also don't explicitly set mem->transient_nslabs to 0.

Quoted 8 lines
Fixes 8b0977ecc8b3 ("swiotlb: track and report io_tlb_used high water marks in debugfs")
Fixes 02e765697038 ("swiotlb: add debugfs to track swiotlb transient pool usage")
---
 kernel/dma/swiotlb.c | 4 ----
 1 file changed, 4 deletions(-)

Sorry, I'm missing a why in this commit message. Can you say what happens if the total_used and used_hiwater IS blindly set to 0? Is the only effect the change in the readout of the swiotlb debugfs files?

Thanks,
Easwar

author
#2 lore raw
Quoted 9 lines

From: Easwar Hariharan <[email protected]>
Sent: Friday, March 29, 2024 12:47 PM
[...]
Sorry, I'm missing a why in this commit message. Can you say what happens if the total_used and used_hiwater IS blindly set to 0? Is the only effect the change in the readout of the swiotlb debugfs files?

Thanks,
Easwar

Right, when the system is not doing any I/O, the readout may return a huge number while it should return 0. This is the only effect.

Thanks,
Dexuan

author
#3 lore raw
Quoted 18 lines

From: Dexuan Cui
Sent: Friday, March 29, 2024 1:23 PM
To: Easwar Hariharan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]
Subject: RE: [PATCH] swiotlb: Do not set total_used to 0 in swiotlb_create_debugfs_files()

Quoted 4 lines

From: Easwar Hariharan <[email protected]>
Sent: Friday, March 29, 2024 12:47 PM
[...]
Sorry, I'm missing a why in this commit message. Can you say what

happens

Quoted 3 lines

if the total_used and used_hiwater IS blindly set to 0? Is the only effect the change in the readout of the swiotlb debugfs files?

Quoted 8 lines
Quoted 2 lines

Thanks,
Easwar

Right, when the system is not doing any I/O, the readout may return a huge number while it should return 0. This is the only effect.

Thanks,
Dexuan

Let me share more details.

kernel/dma/swiotlb.c uses inc_used_and_hiwater() and dec_used() to do the accounting.

The issue happens this way:

1. inc_used_and_hiwater() adds n to total_used.
2. swiotlb_create_debugfs_files() sets total_used to 0.
3. dec_used() decreases total_used by n, i.e. total_used incorrectly becomes a negative number -n, which is a huge number since mem_used() converts the 'long' to 'unsigned long'.

Thanks,
Dexuan

#4 lore raw
Quoted 34 lines · Dexuan Cui
Quoted 18 lines

From: Dexuan Cui
Sent: Friday, March 29, 2024 1:23 PM
To: Easwar Hariharan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]
Subject: RE: [PATCH] swiotlb: Do not set total_used to 0 in swiotlb_create_debugfs_files()

Quoted 4 lines

From: Easwar Hariharan <[email protected]>
Sent: Friday, March 29, 2024 12:47 PM
[...]
Sorry, I'm missing a why in this commit message. Can you say what

happens

Quoted 3 lines

if the total_used and used_hiwater IS blindly set to 0? Is the only effect the change in the readout of the swiotlb debugfs files?

Quoted 8 lines
Quoted 2 lines

Thanks,
Easwar

Right, when the system is not doing any I/O, the readout may return a huge number while it should return 0. This is the only effect.

Thanks,
Dexuan

Let me share more details.

kernel/dma/swiotlb.c uses inc_used_and_hiwater() and dec_used() to do the accounting.

The issue happens this way:

Quoted 9 lines

1. inc_used_and_hiwater() adds n to total_used.
2. swiotlb_create_debugfs_files() sets total_used to 0.
3. dec_used() decreases total_used by n, i.e. total_used incorrectly becomes a negative number -n, which is a huge number since mem_used() converts the 'long' to 'unsigned long'.

Thanks,
Dexuan

Thanks for the detail. I only ask because the patch is marked for stable, and I was wondering if it meets the criteria. But, as you mentioned off list, two Fixes tags probably do meet the bar.

Thanks,
Easwar

Reviewed-by on v1 1/1
#5 lore raw
In reply to Dexuan Cui

From: Dexuan Cui <[email protected]> Sent: Friday, March 29, 2024 12:28 PM

Quoted 49 lines

Sometimes the readout of /sys/kernel/debug/swiotlb/io_tlb_used and io_tlb_used_hiwater can be a huge number (e.g. 18446744073709551615), which is actually a negative number if we use "%ld" to print the number.

When swiotlb_create_default_debugfs() is running from late_initcall, mem->total_used may already be non-zero, because the storage driver may have already started to perform I/O operations: if the storage driver is built-in, its probe() callback is called before late_initcall.

swiotlb_create_debugfs_files() should not blindly set mem->total_used and mem->used_hiwater to 0; actually it doesn't have to initialize the fields at all, because the fields, as part of the global struct io_tlb_default_mem, have been implicitly initialized to zero.

Also don't explicitly set mem->transient_nslabs to 0.

Fixes 8b0977ecc8b3 ("swiotlb: track and report io_tlb_used high water marks in debugfs")
Fixes 02e765697038 ("swiotlb: add debugfs to track swiotlb transient pool usage")
---
 kernel/dma/swiotlb.c | 4 ----
 1 file changed, 4 deletions(-)
kernel/dma/swiotlb.c +0 -3
index 86fe172b5958..4a9f02c13da6 100644
@@ -1647,9 +1647,6 @@
 DEFINE_DEBUGFS_ATTRIBUTE(fops_io_tlb_hiwater, io_tlb_hiwater_get,
 static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem,
 					 const char *dirname)
 {
-	atomic_long_set(&mem->total_used, 0);
-	atomic_long_set(&mem->used_hiwater, 0);
-
 	mem->debugfs = debugfs_create_dir(dirname, io_tlb_default_mem.debugfs);
 	if (!mem->nslabs)
kernel/dma/swiotlb.c +0 -2
 		return;
@@ -1660,7 +1657,6 @@ static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem,
 	debugfs_create_file("io_tlb_used_hiwater", 0600, mem->debugfs, mem,
 			&fops_io_tlb_hiwater);
 #ifdef CONFIG_SWIOTLB_DYNAMIC
-	atomic_long_set(&mem->transient_nslabs, 0);
 	debugfs_create_file("io_tlb_transient_nslabs", 0400, mem->debugfs,
 			    mem, &fops_io_tlb_transient_used);
 #endif
--
Quoted 1 line

2.34.1

Reviewed-by on v1 1/1
#6 lore raw
In reply to Dexuan Cui
Quoted 14 lines · Dexuan Cui

Sometimes the readout of /sys/kernel/debug/swiotlb/io_tlb_used and io_tlb_used_hiwater can be a huge number (e.g. 18446744073709551615), which is actually a negative number if we use "%ld" to print the number.

When swiotlb_create_default_debugfs() is running from late_initcall, mem->total_used may already be non-zero, because the storage driver may have already started to perform I/O operations: if the storage driver is built-in, its probe() callback is called before late_initcall.

swiotlb_create_debugfs_files() should not blindly set mem->total_used and mem->used_hiwater to 0; actually it doesn't have to initialize the fields at all, because the fields, as part of the global struct io_tlb_default_mem, have been implicitly initialized to zero.

Quoted 6 lines

Also don't explicitly set mem->transient_nslabs to 0.

Fixes 8b0977ecc8b3 ("swiotlb: track and report io_tlb_used high water marks in debugfs")
Fixes 02e765697038 ("swiotlb: add debugfs to track swiotlb transient pool usage")
Signature · 2 lines
Best Regards,
Peng
Reviewed-by Petr Tesarik on v1 1/1
#7 lore raw
In reply to Dexuan Cui

V Fri, 29 Mar 2024 12:28:09 -0700
Dexuan Cui <[email protected]> napsáno:

Quoted 14 lines

Sometimes the readout of /sys/kernel/debug/swiotlb/io_tlb_used and io_tlb_used_hiwater can be a huge number (e.g. 18446744073709551615), which is actually a negative number if we use "%ld" to print the number.

When swiotlb_create_default_debugfs() is running from late_initcall, mem->total_used may already be non-zero, because the storage driver may have already started to perform I/O operations: if the storage driver is built-in, its probe() callback is called before late_initcall.

swiotlb_create_debugfs_files() should not blindly set mem->total_used and mem->used_hiwater to 0; actually it doesn't have to initialize the fields at all, because the fields, as part of the global struct io_tlb_default_mem, have been implicitly initialized to zero.

Quoted 6 lines

Also don't explicitly set mem->transient_nslabs to 0.

Fixes 8b0977ecc8b3 ("swiotlb: track and report io_tlb_used high water marks in debugfs")
Fixes 02e765697038 ("swiotlb: add debugfs to track swiotlb transient pool usage")

FWIW I confirm that the transient_nslabs counter does not have to be initialized either.

Petr T