Skip to contentSkip to conversation

lib: scatterlist: Fix sg_split_phys to preserve original scatterlist offsets

PATCH Applied T Pratham 2 participants 5 messages Mar 19, 2025 – Mar 27, 2025 Library code

lore
Share & tools
lore

T Pratham

author
#0 lore raw

PATCH 1/1 lib: scatterlist: Fix sg_split_phys to preserve original scatterlist offsets

The split_sg_phys function was incorrectly setting the offsets of all scatterlist entries (except the first) to 0. Only the first scatterlist entry's offset and length needs to be modified to account for the skip. Setting the rest entries' offsets to 0 could lead to incorrect data access.

This patch removes the offending code, ensuring that the page offsets in the input scatterlist are preserved in the output scatterlist.

Fixes f8bcbe62acd0 ("lib: scatterlist: add sg splitting function")
---
 lib/sg_split.c | 2 --
 1 file changed, 2 deletions(-)
lib/sg_split.c +0 -2
index 60a0babebf2e..0f89aab5c671 100644
@@ -88,8 +88,6 @@ static void sg_split_phys(struct sg_splitter *splitters, const int nb_splits)
 			if (!j) {
 				out_sg->offset += split->skip_sg0;
 				out_sg->length -= split->skip_sg0;
-			} else {
-				out_sg->offset = 0;
 			}
 			sg_dma_address(out_sg) = 0;
 			sg_dma_len(out_sg) = 0;
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 8 lines · On Wed, 19 Mar 2025 16:44:38 +0530 T Pratham <[email protected]> wrote:

The split_sg_phys function was incorrectly setting the offsets of all scatterlist entries (except the first) to 0. Only the first scatterlist entry's offset and length needs to be modified to account for the skip. Setting the rest entries' offsets to 0 could lead to incorrect data access.

This patch removes the offending code, ensuring that the page offsets in the input scatterlist are preserved in the output scatterlist.

Is this merely from code inspection, or is this issues known to have observable runtime effects?

If the latter, please provide a complete description.

Quoted 16 lines

...

lib/sg_split.c +0 -2
@@ -88,8 +88,6 @@ static void sg_split_phys(struct sg_splitter *splitters, const int nb_splits)
 			if (!j) {
 				out_sg->offset += split->skip_sg0;
 				out_sg->length -= split->skip_sg0;
-			} else {
-				out_sg->offset = 0;
 			}
 			sg_dma_address(out_sg) = 0;
 			sg_dma_len(out_sg) = 0;
Signature · 1 line
2.34.1
author
#2 lore raw
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Wed, 19 Mar 2025 16:44:38 +0530 T Pratham <[email protected]> wrote:

The split_sg_phys function was incorrectly setting the offsets of all scatterlist entries (except the first) to 0. Only the first scatterlist entry's offset and length needs to be modified to account for the skip. Setting the rest entries' offsets to 0 could lead to incorrect data access.

Quoted 6 lines
Quoted 2 lines

This patch removes the offending code, ensuring that the page offsets in the input scatterlist are preserved in the output scatterlist.

Is this merely from code inspection, or is this issues known to have observable runtime effects?

If the latter, please provide a complete description.

Hi,

I am using this function in a crypto driver that I'm currently developing (not yet sent to mailing list). During testing, it was observed that the output scatterlists (except the first one) contained incorrect garbage data.

I narrowed this issue down to the call of sg_split(). Upon debugging inside this function, I found that this resetting of offset is the cause of the problem, causing the subsequent scatterlists to point to incorrect memory locations in a page. By removing this code, I am obtaining expected data in all the split output scatterlists. Thus, this was indeed causing observable runtime effects!

Regards
T Pratham <[email protected]>

Quoted 15 lines
Quoted 15 lines

...

lib/sg_split.c +0 -2
@@ -88,8 +88,6 @@ static void sg_split_phys(struct sg_splitter *splitters, const int nb_splits)
 			if (!j) {
 				out_sg->offset += split->skip_sg0;
 				out_sg->length -= split->skip_sg0;
-			} else {
-				out_sg->offset = 0;
 			}
 			sg_dma_address(out_sg) = 0;
 			sg_dma_len(out_sg) = 0;
Signature · 1 line
2.34.1
author
#3 lore raw
Quoted 29 lines · T Pratham
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Wed, 19 Mar 2025 16:44:38 +0530 T Pratham <[email protected]> wrote:

The split_sg_phys function was incorrectly setting the offsets of all scatterlist entries (except the first) to 0. Only the first scatterlist entry's offset and length needs to be modified to account for the skip. Setting the rest entries' offsets to 0 could lead to incorrect data access.

Quoted 6 lines
Quoted 2 lines

This patch removes the offending code, ensuring that the page offsets in the input scatterlist are preserved in the output scatterlist.

Is this merely from code inspection, or is this issues known to have observable runtime effects?

If the latter, please provide a complete description.

Hi,

I am using this function in a crypto driver that I'm currently developing (not yet sent to mailing list). During testing, it was observed that the output scatterlists (except the first one) contained incorrect garbage data.

I narrowed this issue down to the call of sg_split(). Upon debugging inside this function, I found that this resetting of offset is the cause of the problem, causing the subsequent scatterlists to point to incorrect memory locations in a page. By removing this code, I am obtaining expected data in all the split output scatterlists. Thus, this was indeed causing observable runtime effects!

Quoted 2 lines

Regards
T Pratham <[email protected]>

Hi Andrew,

Do you need the above details to be incorporated into the commit message and be resent? Kindly let me know.

Regards
T Pratham <[email protected]>

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

...

lib/sg_split.c +0 -2
@@ -88,8 +88,6 @@ static void sg_split_phys(struct sg_splitter *splitters, const int nb_splits)
 			if (!j) {
 				out_sg->offset += split->skip_sg0;
 				out_sg->length -= split->skip_sg0;
-			} else {
-				out_sg->offset = 0;
 			}
 			sg_dma_address(out_sg) = 0;
 			sg_dma_len(out_sg) = 0;
Signature · 1 line
2.34.1
maintainer
#4 lore raw
Quoted 18 lines · On Wed, 26 Mar 2025 14:13:02 +0530 T Pratham <[email protected]> wrote:
Quoted 18 lines
Quoted 4 lines

Is this merely from code inspection, or is this issues known to have observable runtime effects?

If the latter, please provide a complete description.

Hi,

I am using this function in a crypto driver that I'm currently developing (not yet sent to mailing list). During testing, it was observed that the output scatterlists (except the first one) contained incorrect garbage data.

I narrowed this issue down to the call of sg_split(). Upon debugging inside this function, I found that this resetting of offset is the cause of the problem, causing the subsequent scatterlists to point to incorrect memory locations in a page. By removing this code, I am obtaining expected data in all the split output scatterlists. Thus, this was indeed causing observable runtime effects!

Quoted 7 lines
Quoted 2 lines

Regards
T Pratham <[email protected]>

Hi Andrew,

Do you need the above details to be incorporated into the commit message and be resent? Kindly let me know.

I pasted it into the changelog.

I also added cc:stable, as this might be affecting other drivers, whether in-tree or out-of-tree.