Skip to contentSkip to conversation

Questions about transfer lengths

DISCUSSION Alan Stern 2 participants 7 messages Sep 13, 2002 – Sep 15, 2002 1 message not in the archive SCSI

lore
Share & tools
lore

Alan Stern

author
#1 lore raw
In reply to a message not in the archive

Matt:

I don't really understand the logic behind the calculation used in usb_stor_transfer_length(). Why does the code distinguish between reading and writing?

Clearly this question opens up a can of worms. I realize that the issue of how to handle transfer lengths for emulated scsi devices is a difficult one. So I invite discussion on this topic... For those who don't know, this is all from the perspective of a device-driver author for usb-storage.

As I see it, there's about six different quantities that concern us:

A = the size embedded in the scsi CDB (command data block). We
are given this, although if needed we could alter it.

B = the length that the device really wants to transfer. AFAIK,
some devices will choke if A != B, some will choke if A > B, and some will choke if A < B.

C = the size that we tell the interface hardware about. (For us,
the interface is a USB host controller.) This is really the only value under our direct control. There may be additional interfaces involved, such as a USB->something-else bridge; in that case we also have C' = the size passed to the extra interface. Some hardware will choke if C' has the wrong value.

D = the size of the request given to us by the caller. That would
be the request_bufflen field of the SRB (scsi request block).

E = the size of the memory buffer allocated for the transfer.
For scatter-gather transfers, this is the sum of the length fields of the s-g descriptors in the SRB. For non-scatter-gather, I don't think we know this value -- we seem to assume that it is the same as D.

F = the amount of data that ultimately gets transferred. We ought
to find this out and report it back to the user.

Clearly it is an error if D > E, and it's disastrous if F > E. It's probably also an error if F > D. Other than that, I don't know much.

The logic used to usb_stor_transfer_length() can be described as follows:

For writes (and a few assorted types of reads), calculate C = min(D,E). For most types of reads, calculate C = A. Report an error in the kernel log if C != D, and then return C.

Also, we get F from the lower-level drivers, but we don't seem to use it (beyond checking for a truncated transfer). Certainly we don't report the value back to the caller. I presume the higher scsi routines have their own ways of figuring out how much data got transferred; is that correct?

Comments of any sort are welcome.

Alan Stern

#2 lore raw

Okay... this is a long and confusing and painful subject, but it eventually boils down to some easier rules... it's also been discussed on linux-scsi before, so this should just be a refresher to the folks there...

First, we need to guarantee that A == C. That can be different from F without any problems, but A must == C.

Since C is under our control, we use C := A. But we don't always know A, since the CDB can specify size in 'blocks', the size of which varies from device to device. Thus, if we see a command that uses 'blocks' (i.e. READ_10, etc), we choose to interpret A := D/E -- we try to enforce that D == E. For those commands where we actually can determine A (i.e. INQUIRY, where the length is encoded in a bytecount in the CDB), we use that value.

F should be reported back to the user. There is a residue field in the request structure from the mid-layer, and we simply need to copy this data back to that field.

Now, the good news: After many long discussions with Linus and other on the linux-scsi list, we've decided that D == E should be an invariant established by the thing that actually originates the request. Also, A == D == E is also a requirement. The recent discussion on my use of BUG_ON() is actually in relation to this -- I was trying to flag any command that came down the pipe with this rule violated.

Any place you see that A != D or D != E is a bug, and should be fixed at the higher layer that sends the command. Eventually, all that logic to determine the transfer length will be removed (it only works _most_, but not _all_ of the time) and replaced with references to D.

So that just leaves B and F... F I've discussed above. As for B... well, here's where we have to hope the device vendors have done their job. We try to avoid C != B as much as possible, mostly by trying to craft the commands being sent to look like what Windows or MacOS is likely to send -- we do this at the source of the command. This is why the INQUIRY-sending logic in the scsi scanning code is so involved, but it works more universally.

So the good news is that we really are trying to reduce the number of different values that need to be considered.

Matt

Quoted 58 lines · Alan Stern

Matt:

I don't really understand the logic behind the calculation used in usb_stor_transfer_length(). Why does the code distinguish between reading and writing?

Clearly this question opens up a can of worms. I realize that the issue of how to handle transfer lengths for emulated scsi devices is a difficult one. So I invite discussion on this topic... For those who don't know, this is all from the perspective of a device-driver author for usb-storage.

As I see it, there's about six different quantities that concern us:

A = the size embedded in the scsi CDB (command data block). We
are given this, although if needed we could alter it.

B = the length that the device really wants to transfer. AFAIK,
some devices will choke if A != B, some will choke if A > B, and some will choke if A < B.

C = the size that we tell the interface hardware about. (For us,
the interface is a USB host controller.) This is really the only value under our direct control. There may be additional interfaces involved, such as a USB->something-else bridge; in that case we also have C' = the size passed to the extra interface. Some hardware will choke if C' has the wrong value.

D = the size of the request given to us by the caller. That would
be the request_bufflen field of the SRB (scsi request block).

E = the size of the memory buffer allocated for the transfer.
For scatter-gather transfers, this is the sum of the length fields of the s-g descriptors in the SRB. For non-scatter-gather, I don't think we know this value -- we seem to assume that it is the same as D.

F = the amount of data that ultimately gets transferred. We ought
to find this out and report it back to the user.

Clearly it is an error if D > E, and it's disastrous if F > E. It's probably also an error if F > D. Other than that, I don't know much.

The logic used to usb_stor_transfer_length() can be described as follows:

For writes (and a few assorted types of reads), calculate C = min(D,E). For most types of reads, calculate C = A. Report an error in the kernel log if C != D, and then return C.

Also, we get F from the lower-level drivers, but we don't seem to use it (beyond checking for a truncated transfer). Certainly we don't report the value back to the caller. I presume the higher scsi routines have their own ways of figuring out how much data got transferred; is that correct?

Comments of any sort are welcome.

Alan Stern

Signature · 6 lines
Matthew Dharm                              Home: [email protected] 
Maintainer, Linux USB Mass Storage Driver

What the hell are you?
					-- Pitr to Dust Puppy 
User Friendly, 12/3/1997
author
#3 lore raw
Quoted 11 lines · Matthew Dharm

Since C is under our control, we use C := A. But we don't always know A, since the CDB can specify size in 'blocks', the size of which varies from device to device. Thus, if we see a command that uses 'blocks' (i.e. READ_10, etc), we choose to interpret A := D/E -- we try to enforce that D == E. For those commands where we actually can determine A (i.e. INQUIRY, where the length is encoded in a bytecount in the CDB), we use that value.

F should be reported back to the user. There is a residue field in the request structure from the mid-layer, and we simply need to copy this data back to that field.

So far as I can tell this isn't done anywhere, and it hasn't seemed to hurt anything. (In fact, grep'ing for 'resid' in the usb-storage source directory reveals no matches at all.) The proper spot to fill in the residue field is usb_stor_transfer(), but the code doesn't do it. That subroutine doesn't even know how much data got sent! -- the size information is not propagated from usb_stor_transfer_partial().

Is this an error in usb-storage? If it is, why hasn't it caused any problems? Or maybe it has, and nobody realized what was going on?

Quoted 7 lines

Now, the good news: After many long discussions with Linus and other on the linux-scsi list, we've decided that D == E should be an invariant established by the thing that actually originates the request. Also, A == D == E is also a requirement. The recent discussion on my use of BUG_ON() is actually in relation to this -- I was trying to flag any command that came down the pipe with this rule violated.

Quoted 6 lines

Any place you see that A != D or D != E is a bug, and should be fixed at the higher layer that sends the command. Eventually, all that logic to determine the transfer length will be removed (it only works _most_, but not _all_ of the time) and replaced with references to D.

From what you're saying, it sounds like the ideal thing would be to use

C := D, but instead as a defense against buggy callers we actually should try to set C := min(A,D,E) if we can determine A, and C := min(D,E) otherwise. In any case we should always check and report it as a bug if A != D or D != E. Note that this is definitely _not_ what the code does now.

And why does the current code only calculate A for reads and not for writes?

Alan Stern

Quoted 47 lines
Quoted 45 lines

As I see it, there's about six different quantities that concern us:

A = the size embedded in the scsi CDB (command data block). We
are given this, although if needed we could alter it.

B = the length that the device really wants to transfer. AFAIK,
some devices will choke if A != B, some will choke if A > B, and some will choke if A < B.

C = the size that we tell the interface hardware about. (For us,
the interface is a USB host controller.) This is really the only value under our direct control. There may be additional interfaces involved, such as a USB->something-else bridge; in that case we also have C' = the size passed to the extra interface. Some hardware will choke if C' has the wrong value.

D = the size of the request given to us by the caller. That would
be the request_bufflen field of the SRB (scsi request block).

E = the size of the memory buffer allocated for the transfer.
For scatter-gather transfers, this is the sum of the length fields of the s-g descriptors in the SRB. For non-scatter-gather, I don't think we know this value -- we seem to assume that it is the same as D.

F = the amount of data that ultimately gets transferred. We ought
to find this out and report it back to the user.

Clearly it is an error if D > E, and it's disastrous if F > E. It's probably also an error if F > D. Other than that, I don't know much.

The logic used to usb_stor_transfer_length() can be described as follows:

For writes (and a few assorted types of reads), calculate C = min(D,E). For most types of reads, calculate C = A. Report an error in the kernel log if C != D, and then return C.

Also, we get F from the lower-level drivers, but we don't seem to use it (beyond checking for a truncated transfer). Certainly we don't report the value back to the caller. I presume the higher scsi routines have their own ways of figuring out how much data got transferred; is that correct?

Comments of any sort are welcome.

Alan Stern

#4 lore raw
Quoted 21 lines · Alan Stern
Quoted 11 lines · Matthew Dharm

Since C is under our control, we use C := A. But we don't always know A, since the CDB can specify size in 'blocks', the size of which varies from device to device. Thus, if we see a command that uses 'blocks' (i.e. READ_10, etc), we choose to interpret A := D/E -- we try to enforce that D == E. For those commands where we actually can determine A (i.e. INQUIRY, where the length is encoded in a bytecount in the CDB), we use that value.

F should be reported back to the user. There is a residue field in the request structure from the mid-layer, and we simply need to copy this data back to that field.

So far as I can tell this isn't done anywhere, and it hasn't seemed to hurt anything. (In fact, grep'ing for 'resid' in the usb-storage source directory reveals no matches at all.) The proper spot to fill in the residue field is usb_stor_transfer(), but the code doesn't do it. That subroutine doesn't even know how much data got sent! -- the size information is not propagated from usb_stor_transfer_partial().

Quoted 2 lines

Is this an error in usb-storage? If it is, why hasn't it caused any problems? Or maybe it has, and nobody realized what was going on?

It's an error. Very few things check the residue. Actually, nothing in the kernel check it -- the data is passed back to userspace apps that use the scsi-generic interface.

Quoted 12 lines
Quoted 11 lines

Now, the good news: After many long discussions with Linus and other on the linux-scsi list, we've decided that D == E should be an invariant established by the thing that actually originates the request. Also, A == D == E is also a requirement. The recent discussion on my use of BUG_ON() is actually in relation to this -- I was trying to flag any command that came down the pipe with this rule violated.

Any place you see that A != D or D != E is a bug, and should be fixed at the higher layer that sends the command. Eventually, all that logic to determine the transfer length will be removed (it only works _most_, but not _all_ of the time) and replaced with references to D.

Quoted 6 lines

From what you're saying, it sounds like the ideal thing would be to use C := D, but instead as a defense against buggy callers we actually should try to set C := min(A,D,E) if we can determine A, and C := min(D,E) otherwise. In any case we should always check and report it as a bug if A != D or D != E. Note that this is definitely _not_ what the code does now.

Um... we do check that C != D or E -- we used to BUG_ON() if they were different, but Linus didn't like that. Eventually, we will be removing the entire usb_stor_transfer_length() mess, tho.

Quoted 2 lines

And why does the current code only calculate A for reads and not for writes?

The asymmetry of USB. If we're writing and we write too much, the device can tell us. If we're reading and we read too much, all we get is a CSW block loaded into our user buffer.

Matt

Signature · 8 lines
Matthew Dharm                              Home: [email protected] 
Maintainer, Linux USB Mass Storage Driver

G:  Money isn't everything, A.J.
AJ: Who convinced you of that?
G:  The Chief, at my last salary review.
					-- Mike and Greg
User Friendly, 11/3/1998
author
#5 lore raw
Quoted 10 lines · Matthew Dharm
Quoted 6 lines

From what you're saying, it sounds like the ideal thing would be to use C := D, but instead as a defense against buggy callers we actually should try to set C := min(A,D,E) if we can determine A, and C := min(D,E) otherwise. In any case we should always check and report it as a bug if A != D or D != E. Note that this is definitely _not_ what the code does now.

Um... we do check that C != D or E -- we used to BUG_ON() if they were different, but Linus didn't like that. Eventually, we will be removing the entire usb_stor_transfer_length() mess, tho.

That's not quite true. If we look at E at all (which we only do sometimes), we only make sure that C = min(D,E). And we don't log an error message if D != E.

Quoted 6 lines
Quoted 2 lines

And why does the current code only calculate A for reads and not for writes?

The asymmetry of USB. If we're writing and we write too much, the device can tell us. If we're reading and we read too much, all we get is a CSW block loaded into our user buffer.

Okay, that explains that. But consider this: For now, the only reasons we have for going through all these shenanigans are:

1. We don't trust the caller to get it right; and

2. If the caller gets it wrong, we want to make sure that it doesn't look as though things worked okay when in fact they didn't (reading CSW bytes into a data buffer for example).

Given these reasons, I think the best course is always to take the time to compute A and E whenever possible, for writes as well as reads, and always log an error (and maybe a stack trace) if they disagree. After all, if the whole point is to catch our callers' errors, then let's look for them! (Also, it would help simplify part of the isd200 driver, where apparently the length embedded in the CDB needs to be decoded for both reads and writes.)

Alan Stern

#6 lore raw
Quoted 9 lines · Alan Stern
Quoted 7 lines · Matthew Dharm
Quoted 6 lines

From what you're saying, it sounds like the ideal thing would be to use C := D, but instead as a defense against buggy callers we actually should try to set C := min(A,D,E) if we can determine A, and C := min(D,E) otherwise. In any case we should always check and report it as a bug if A != D or D != E. Note that this is definitely _not_ what the code does now.

Quoted 7 lines
Quoted 3 lines

Um... we do check that C != D or E -- we used to BUG_ON() if they were different, but Linus didn't like that. Eventually, we will be removing the entire usb_stor_transfer_length() mess, tho.

That's not quite true. If we look at E at all (which we only do sometimes), we only make sure that C = min(D,E). And we don't log an error message if D != E.

True enough.

Quoted 16 lines
Quoted 6 lines
Quoted 2 lines

And why does the current code only calculate A for reads and not for writes?

The asymmetry of USB. If we're writing and we write too much, the device can tell us. If we're reading and we read too much, all we get is a CSW block loaded into our user buffer.

Okay, that explains that. But consider this: For now, the only reasons we have for going through all these shenanigans are:

1. We don't trust the caller to get it right; and

2. If the caller gets it wrong, we want to make sure that it doesn't look as though things worked okay when in fact they didn't (reading CSW bytes into a data buffer for example).

Quoted 7 lines

Given these reasons, I think the best course is always to take the time to compute A and E whenever possible, for writes as well as reads, and always log an error (and maybe a stack trace) if they disagree. After all, if the whole point is to catch our callers' errors, then let's look for them! (Also, it would help simplify part of the isd200 driver, where apparently the length embedded in the CDB needs to be decoded for both reads and writes.)

The point of where I'm trying to take the code is that we should compute nothing, because we cannot guarantee that we are correct. We _must_ trust the caller to get it right.... there is no perfect algorithm for determining, given the information available to the usb-storage driver, what the 'right' answer is. The best we can do is use a heuristic to get close, and that's not acceptable. The caller is the only party that can get it right, so we must insist that it does so.

Remember, we're not worried about target error here, only host error. The major thing we must do is make certain that the length in the CBW matches the command (because _lots_ of bridges freak out if that's not true). Most of the other logic is to try to catch weirdo callers that didn't get it right in creative ways -- most of the callers are already fixed now.

I'm merging patches right now... the show_trace() is changing into a KERN_ERR printk. I figure that by next week we should be ripping out all of that function... we'll actually likely just move it into debug.c, just in case we ever need it again.

Matt

Signature · 7 lines
Matthew Dharm                              Home: [email protected] 
Maintainer, Linux USB Mass Storage Driver

My mother not mind to die for stoppink Windows NT!  She is rememberink 
Stalin!
					-- Pitr
User Friendly, 9/6/1998
#7 lore raw
Quoted 14 lines · Matthew Dharm
Quoted 13 lines · Alan Stern
Quoted 10 lines · Matthew Dharm
Quoted 6 lines

From what you're saying, it sounds like the ideal thing would be to use C := D, but instead as a defense against buggy callers we actually should try to set C := min(A,D,E) if we can determine A, and C := min(D,E) otherwise. In any case we should always check and report it as a bug if A != D or D != E. Note that this is definitely _not_ what the code does now.

Um... we do check that C != D or E -- we used to BUG_ON() if they were different, but Linus didn't like that. Eventually, we will be removing the entire usb_stor_transfer_length() mess, tho.

Quoted 5 lines
Quoted 3 lines

That's not quite true. If we look at E at all (which we only do sometimes), we only make sure that C = min(D,E). And we don't log an error message if D != E.

True enough.

Just to clarify this... in discussions on linux-scsi and with Linus, we decided that D != E is a bug and a caller problem. No, we don't check for it...

Again, the entire point is to try to get it so that we can trust all the callers. We're almost there -- any effort on this is best spent trying to find any place where the caller is wrong and fixing it.

Matt

Signature · 8 lines
Matthew Dharm                              Home: [email protected] 
Maintainer, Linux USB Mass Storage Driver

A:  The most ironic oxymoron wins ...
DP: "Microsoft Works"
A:  Uh, okay, you win.
					-- A.J. & Dust Puppy
User Friendly, 1/18/1998