Skip to contentSkip to conversation

x86_64 pci_map_sg modifies sg list - fails multiple map/unmaps

BUG Justin T. Gibbs 5 participants 7 messages Jan 5, 2004 SCSI

lore
Share & tools
lore
#0 lore raw

Berkley Shands recently tripped over this problem. The 2.6.X pci_map_sg code for x86_64 modifies the passed in S/G list to compact it for mapping by the GART. This modification is not reversed when pci_unmap_sg is called. In the case of a retried SCSI command, this causes any attempt to map the command a second time to fail with a BUG assertion since the nseg parameter passed into the second map call is state. nseg comes from the "use_sg" field in the SCSI command structure which is never touched by the HBA drivers invoking pci_map_sg.

DMA-API.txt doesn't seem to cover this issue. Should the low-level DMA code restore the S/G list to its original state on unmap or should the SCSI HBA drivers be changed to update "use_sg" with the segment count reported by the pci_map_sg() API? If the latter, this seems to contradict the mandate in DMA-API that the nseg parameter passed into the unmap call be the same as that passed into the map call. Most of the kernel assumes that an S/G list can be mapped an unmapped multiple times using the same arguments. This doesn't seem to me to be an unreasonable expectation.

--
Justin

#1 lore raw
Quoted 8 lines · On Mon, 05 Jan 2004 10:57:35 -0700 "Justin T. Gibbs" <[email protected]> wrote:

DMA-API.txt doesn't seem to cover this issue. Should the low-level DMA code restore the S/G list to its original state on unmap or should the SCSI HBA drivers be changed to update "use_sg" with the segment count reported by the pci_map_sg() API? If the latter, this seems to contradict the mandate in DMA-API that the nseg parameter passed into the unmap call be the same as that passed into the map call. Most of the kernel assumes that an S/G list can be mapped an unmapped multiple times using the same arguments. This doesn't seem to me to be an unreasonable expectation.

No, the PCI layer is not required at all to restore the SG to it's original state if it does DMA page coalescing as sparc64 and x86_64 do.

But I don't see what the real problem is, all the PCI layer is doing is setting up the DMA address/length pairs in the entries that are needed, then returning the number of such slots that exist. You, in your driver, can remember the original total number of physical entries and use that value to pass things back in.

#2 lore raw
In reply to Justin T. Gibbs
Quoted 4 lines · Justin T. Gibbs

Berkley Shands recently tripped over this problem. The 2.6.X pci_map_sg code for x86_64 modifies the passed in S/G list to compact it for mapping by the GART. This modification is not reversed when pci_unmap_sg is called. In the case of a retried SCSI command, this causes any attempt

Qlogic has the same bug.

Actually I disabled merging by default in the latest x86-64 code, but it can be still enabled by the user using options (it makes some adapters run several percent faster). I would appreciate if you could fix the problem anyways.

I was actually planning to add a BUG() for this. Should do that. There is already one that triggers often when the problem occurs.

Quoted 6 lines

to map the command a second time to fail with a BUG assertion since the nseg parameter passed into the second map call is state. nseg comes from the "use_sg" field in the SCSI command structure which is never touched by the HBA drivers invoking pci_map_sg.

DMA-API.txt doesn't seem to cover this issue. Should the low-level DMA

It does actually, but not very clearly. I've suggested changes of wording recently to make this more clear, but it hasn't gotten in.

Quoted 3 lines

code restore the S/G list to its original state on unmap or should the SCSI HBA drivers be changed to update "use_sg" with the segment count reported by the pci_map_sg() API? If the latter, this seems to contradict

You should never remap an already mapped sg list. Either reuse the already mapped list or keep a copy of the original list around. First is better because the later may have problems with the page reference counts.

-Andi

#3 lore raw
Quoted 8 lines
Quoted 4 lines · Justin T. Gibbs

Berkley Shands recently tripped over this problem. The 2.6.X pci_map_sg code for x86_64 modifies the passed in S/G list to compact it for mapping by the GART. This modification is not reversed when pci_unmap_sg is called. In the case of a retried SCSI command, this causes any attempt

Qlogic has the same bug.

Which bug is this? Not updating use_sg, or mapping the command the second time when it is retried? If the latter, I don't think this is an HBA bug. The HBA driver doesn't know that the command has been mapped in the past, so it will blindly map/unmap it again.

...

Quoted 4 lines
Quoted 1 line

DMA-API.txt doesn't seem to cover this issue.

It does actually, but not very clearly. I've suggested changes of wording recently to make this more clear, but it hasn't gotten in.

Can you point to the section of the document you believe applies?

Quoted 1 line

You should never remap an already mapped sg list.

But the sg list is no longer mapped. The HBA driver did call pci_unmap_sg on it. Did you mean to say, "Never map an sg list again that has been mapped in the past"?

Quoted 3 lines

Either reuse the already mapped list or keep a copy of the original list around. First is better because the later may have problems with the page reference counts.

The mid-layer doesn't map the list. The HBA drivers do. So you're saying that either the mid-layer or the HBA drivers need to copy the list so it can be restored just in case the command will be retried?

--
Justin

#6 lore raw
In reply to Justin T. Gibbs
Quoted 9 lines · Justin T. Gibbs

Berkley Shands recently tripped over this problem. The 2.6.X pci_map_sg code for x86_64 modifies the passed in S/G list to compact it for mapping by the GART. This modification is not reversed when pci_unmap_sg is called. In the case of a retried SCSI command, this causes any attempt to map the command a second time to fail with a BUG assertion since the nseg parameter passed into the second map call is state. nseg comes from the "use_sg" field in the SCSI command structure which is never touched by the HBA drivers invoking pci_map_sg.

Quoted 8 lines

DMA-API.txt doesn't seem to cover this issue. Should the low-level DMA code restore the S/G list to its original state on unmap or should the SCSI HBA drivers be changed to update "use_sg" with the segment count reported by the pci_map_sg() API? If the latter, this seems to contradict the mandate in DMA-API that the nseg parameter passed into the unmap call be the same as that passed into the map call. Most of the kernel assumes that an S/G list can be mapped an unmapped multiple times using the same arguments. This doesn't seem to me to be an unreasonable expectation.

Hi,

I ran into the same thing a month ago. I talked to Andi Kleen about this. He seems to think that, it is okay to modify the sg-list in pci_map_sg(). pci_unmap_sg() can't restore it back, since we lost lot of information. One option is to recreate entire sg-list in case of a retry. I used a patch similar to following to do this. Is this acceptable ?

Thanks,
Badari

scsi_lib.c.new 2004-01-04 19:57:35.000000000 -0800 renamed from scsi_lib.c 2004-01-04 19:53:56.000000000 -0800 +7 -0
@@ -308,6 +308,13 @@ void scsi_setup_cmd_retry(struct scsi_cm
        cmd->cmd_len = cmd->old_cmd_len;
        cmd->sc_data_direction = cmd->sc_old_data_direction;
        cmd->underflow = cmd->old_underflow;
+       if (cmd->use_sg) {
+               struct request     *req = cmd->request;
+               int count;
+
+               count = blk_rq_map_sg(req->q, req, cmd->request_buffer);
+               BUG_ON(count != cmd->use_sg);
+       }
 }
 
 
#4 lore raw
In reply to Andi Kleen
Quoted 7 lines · On Mon, 05 Jan 2004 20:47:19 +0100 Andi Kleen <[email protected]> wrote:

Actually I disabled merging by default in the latest x86-64 code, but it can be still enabled by the user using options (it makes some adapters run several percent faster). I would appreciate if you could fix the problem anyways.

I was actually planning to add a BUG() for this. Should do that. There is already one that triggers often when the problem occurs.

Andi, you must not modify sg->length in any way shape or form.

The following is legal:

	pci_map_sg(..&sg);
	pci_unmap_sg(...&sg);
	pci_map_sg(..&sg);

If you must modify the length field for DMA, you must have a seperate dma_length member of the scatterlist structure on your platform, see what sparc64 does here.

If the documentation states this wrongly, it's a doc bug.

#5 lore raw
Quoted 11 lines · David S. Miller
Quoted 7 lines · On Mon, 05 Jan 2004 20:47:19 +0100 Andi Kleen <[email protected]> wrote:

Actually I disabled merging by default in the latest x86-64 code, but it can be still enabled by the user using options (it makes some adapters run several percent faster). I would appreciate if you could fix the problem anyways.

I was actually planning to add a BUG() for this. Should do that. There is already one that triggers often when the problem occurs.

Quoted 7 lines

Andi, you must not modify sg->length in any way shape or form.

The following is legal:

	pci_map_sg(..&sg);
	pci_unmap_sg(...&sg);
	pci_map_sg(..&sg);

Well, on x86-64 it is not legal.

Quoted 5 lines

If you must modify the length field for DMA, you must have a seperate dma_length member of the scatterlist structure on your platform, see what sparc64 does here.

If the documentation states this wrongly, it's a doc bug.

The documentation doesn't allow it so I didn't implement it.

-Andi