Skip to contentSkip to conversation

s390/vfio_ccw: Fix target addresses of TIC CCWs

PATCH Superseded Eric Farman 2 participants 5 messages Jun 27, 2024 – Jun 28, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Eric Farman

author maintainer
#0 lore raw

PATCH 1/1 s390/vfio_ccw: Fix target addresses of TIC CCWs

The processing of a Transfer-In-Channel (TIC) CCW requires locating the target of the CCW in the channel program, and updating the address to reflect what will actually be sent to hardware.

An error exists where the 64-bit virtual address is truncated to 32-bits (variable "cda") when performing this math. Since s390 addresses of that size are 31-bits, this leaves that additional bit enabled such that the resulting I/O triggers a channel program check. This shows up occasionally when booting a KVM guest from a passthrough DASD device:

..snip...
Interrupt Response Block Data:
: 0x0000000000003990
Function Ctrl : [Start]
Activity Ctrl :
Status Ctrl : [Alert] [Primary] [Secondary] [Status-Pending] Device Status :
Channel Status : [Program-Check]
cpa=: 0x00000000008d0018
prev_ccw=: 0x0000000000000000
this_ccw=: 0x0000000000000000
...snip...
dasd-ipl: Failed to run IPL1 channel program

The channel program address of "0x008d0018" in the IRB doesn't look wrong, but tracing the CCWs shows the offending bit enabled:

ccw=0x0000012e808d0000 cda=00a0b030
ccw=0x0000012e808d0008 cda=00a0b038
ccw=0x0000012e808d0010 cda=808d0008
ccw=0x0000012e808d0018 cda=00a0b040

Fix the calculation of the TIC CCW's data address such that it points to a valid 31-bit address regardless of the input address.

Fixes bd36cfbbb9e1 ("s390/vfio_ccw_cp: use new address translation helpers")
---
 drivers/s390/cio/vfio_ccw_cp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/s390/cio/vfio_ccw_cp.c +3 -2
index 6e5c508b1e07..fd8cb052f096 100644
@@ -495,8 +495,9 @@ static int ccwchain_fetch_tic(struct ccw1 *ccw,
 	list_for_each_entry(iter, &cp->ccwchain_list, next) {
 		ccw_head = iter->ch_iova;
 		if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) {
-			cda = (u64)iter->ch_ccw + dma32_to_u32(ccw->cda) - ccw_head;
-			ccw->cda = u32_to_dma32(cda);
+			/* Calculate offset of TIC target */
+			cda = dma32_to_u32(ccw->cda) - ccw_head;
+			ccw->cda = virt_to_dma32(iter->ch_ccw) + cda;
 			return 0;
 		}
 	}
Signature · 1 line
2.40.1
maintainer
#1 lore raw
Quoted 6 lines · Eric Farman

The processing of a Transfer-In-Channel (TIC) CCW requires locating the target of the CCW in the channel program, and updating the address to reflect what will actually be sent to hardware.

An error exists where the 64-bit virtual address is truncated to 32-bits (variable "cda") when performing this math. Since s390

...

Quoted 14 lines

Fix the calculation of the TIC CCW's data address such that it points to a valid 31-bit address regardless of the input address.

Fixes bd36cfbbb9e1 ("s390/vfio_ccw_cp: use new address translation helpers")
---
 drivers/s390/cio/vfio_ccw_cp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/s390/cio/vfio_ccw_cp.c +0 -0
index 6e5c508b1e07..fd8cb052f096 100644

drivers/s390/cio/vfio_ccw_cp.c static int ccwchain_fetch_tic(struct ccw1 *ccw, · line 500

Quoted 8 lines
drivers/s390/cio/vfio_ccw_cp.c +3 -2
@@ -495,8 +495,9 @@ static int ccwchain_fetch_tic(struct ccw1 *ccw,
 	list_for_each_entry(iter, &cp->ccwchain_list, next) {
 		ccw_head = iter->ch_iova;
 		if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) {
-			cda = (u64)iter->ch_ccw + dma32_to_u32(ccw->cda) - ccw_head;
-			ccw->cda = u32_to_dma32(cda);
+			/* Calculate offset of TIC target */
+			cda = dma32_to_u32(ccw->cda) - ccw_head;
+			ccw->cda = virt_to_dma32(iter->ch_ccw) + cda;

I would suggest to rename cda to "offset", since that reflects what it is now. Also this code needs to take care of type checking, which will fail now due to dma32_t type (try "make C=1 drivers/s390/cio/vfio_ccw_cp.o).

You could write the above as:

			ccw->cda = virt_to_dma32((void *)iter->ch_ccw + cda);

Note that somebody :) introduced a similar bug in cp_update_scsw(). I guess you could add this hunk to your patch:

@@ -915,7 +915,7 @@ void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
* in the ioctl directly. Path status changes etc.
*/
list_for_each_entry(chain, &cp->ccwchain_list, next) {
- ccw_head = (u32)(u64)chain->ch_ccw;
+ ccw_head = (__force u32)virt_to_dma32(chain->ch_ccw);
/*
* On successful execution, cpa points just beyond the end
* of the chain.

Furthermore it looks to me like the ch_iova member of struct ccwchain should get a dma32_t type instead of u64. The same applies to quite a few variables to the code. I could give this a try, but I think it would be better if somebody who knows what he is doing would address this :)

author maintainer
#2 lore raw
Quoted 31 lines · Heiko Carstens
Quoted 6 lines · Eric Farman

The processing of a Transfer-In-Channel (TIC) CCW requires locating the target of the CCW in the channel program, and updating the address to reflect what will actually be sent to hardware.

An error exists where the 64-bit virtual address is truncated to 32-bits (variable "cda") when performing this math. Since s390

...

Quoted 21 lines

Fix the calculation of the TIC CCW's data address such that it points
to a valid 31-bit address regardless of the input address.

Fixes bd36cfbbb9e1 ("s390/vfio_ccw_cp: use new address translation

helpers")

---
 drivers/s390/cio/vfio_ccw_cp.c | 5 +++--

 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/s390/cio/vfio_ccw_cp.c +0 -0
index 6e5c508b1e07..fd8cb052f096 100644
@@ -495,8 +495,9 @@ static int ccwchain_fetch_tic(struct ccw1 *ccw,
  	list_for_each_entry(iter, &cp->ccwchain_list, next) {
  		ccw_head = iter->ch_iova;

  if (is_cpa_within_range(ccw->cda, ccw_head, iter-

Quoted 1 line

ch_len)) {

Quoted 13 lines
Quoted 7 lines
drivers/s390/cio/vfio_ccw_cp.c +0 -1
-			cda = (u64)iter->ch_ccw +

dma32_to_u32(ccw->cda) - ccw_head;

drivers/s390/cio/vfio_ccw_cp.c +3 -1
-			ccw->cda = u32_to_dma32(cda);
+			/* Calculate offset of TIC target */
+			cda = dma32_to_u32(ccw->cda) - ccw_head;
+			ccw->cda = virt_to_dma32(iter->ch_ccw) +

cda;

I would suggest to rename cda to "offset", since that reflects what it is
now. Also this code needs to take care of type checking, which will fail now
due to dma32_t type (try "make C=1 drivers/s390/cio/vfio_ccw_cp.o).

Argh, I missed that. Sorry.

Quoted 7 lines

You could write the above as:

			ccw->cda = virt_to_dma32((void *)iter-
Quoted 1 line

ch_ccw + cda);

Note that somebody :) introduced a similar bug in cp_update_scsw().

:)

I was poking at that code yesterday because it seemed suspect, but as I wasn't getting an explicit failure (versus the CPC generated by hw), I opted to leave it for now. I agree they should both be fixed up.

Quoted 16 lines

I guess
you could add this hunk to your patch:

(quoted diff) +1 -1
@@ -915,7 +915,7 @@ void cp_update_scsw(struct channel_program *cp,
 union scsw *scsw)
  	 * in the ioctl directly. Path status changes etc.
  	 */
  	list_for_each_entry(chain, &cp->ccwchain_list, next) {
-		ccw_head = (u32)(u64)chain->ch_ccw;
+		ccw_head = (__force u32)virt_to_dma32(chain-
 >ch_ccw);
  		/*

  * On successful execution, cpa points just beyond the end
  * of the chain.

Quoted 5 lines

Furthermore it looks to me like the ch_iova member of struct ccwchain should
get a dma32_t type instead of u64. The same applies to quite a few variables
to the code.

Agreed. I started this some time back after the IDAW code got reworked, but have been sidetracked. The problem with ch_iova is more apparent after the dma32 stuff.

Quoted 2 lines

I could give this a try, but I think it would be better if somebody who knows what he is doing would address this :)

I'll finish them up. But v2 will have to wait until after my holiday. Thanks for reminding me of the typechecking!

maintainer
#3 lore raw
Quoted 12 lines · Eric Farman
Quoted 12 lines
Quoted 6 lines

dma32_to_u32(ccw->cda) - ccw_head;

drivers/s390/cio/vfio_ccw_cp.c +3 -1
-			ccw->cda = u32_to_dma32(cda);
+			/* Calculate offset of TIC target */
+			cda = dma32_to_u32(ccw->cda) - ccw_head;
+			ccw->cda = virt_to_dma32(iter->ch_ccw) +

cda;

I would suggest to rename cda to "offset", since that reflects what it is
now. Also this code needs to take care of type checking, which will fail now
due to dma32_t type (try "make C=1 drivers/s390/cio/vfio_ccw_cp.o).

...

Quoted 3 lines

I was poking at that code yesterday because it seemed suspect, but as I wasn't getting an explicit failure (versus the CPC generated by hw), I opted to leave it for now. I agree they should both be fixed up.

...

Quoted 15 lines
Quoted 15 lines

I guess
you could add this hunk to your patch:

(quoted diff) +1 -1
@@ -915,7 +915,7 @@ void cp_update_scsw(struct channel_program *cp,
 union scsw *scsw)
  	 * in the ioctl directly. Path status changes etc.
  	 */
  	list_for_each_entry(chain, &cp->ccwchain_list, next) {
-		ccw_head = (u32)(u64)chain->ch_ccw;
+		ccw_head = (__force u32)virt_to_dma32(chain-
 >ch_ccw);
  		/*

  * On successful execution, cpa points just beyond the end
  * of the chain.

...

Quoted 10 lines
Quoted 5 lines

Furthermore it looks to me like the ch_iova member of struct ccwchain should
get a dma32_t type instead of u64. The same applies to quite a few variables
to the code.

Agreed. I started this some time back after the IDAW code got reworked, but have been sidetracked. The problem with ch_iova is more apparent after the dma32 stuff.

Quoted 5 lines
Quoted 2 lines

I could give this a try, but I think it would be better if somebody who knows what he is doing would address this :)

I'll finish them up. But v2 will have to wait until after my holiday. Thanks for reminding me of the typechecking!

I hope you didn't get me wrong: from my point of view we want one or two small patches (the above hunks), which fix the bugs, if you agree.

And then address the type checking stuff at a later point in time.

(btw: your mailer adds lot's of extra line wraps)

author maintainer
#4 lore raw
Quoted 53 lines · Heiko Carstens
Quoted 12 lines · Eric Farman
Quoted 12 lines
Quoted 6 lines

dma32_to_u32(ccw->cda) - ccw_head;

drivers/s390/cio/vfio_ccw_cp.c +3 -1
-			ccw->cda = u32_to_dma32(cda);
+			/* Calculate offset of TIC target */
+			cda = dma32_to_u32(ccw->cda) - ccw_head;
+			ccw->cda = virt_to_dma32(iter->ch_ccw) +

cda;

I would suggest to rename cda to "offset", since that reflects what it is
now. Also this code needs to take care of type checking, which will fail now
due to dma32_t type (try "make C=1 drivers/s390/cio/vfio_ccw_cp.o).

...

Quoted 3 lines

I was poking at that code yesterday because it seemed suspect, but as I wasn't getting an explicit failure (versus the CPC generated by hw), I opted to leave it for now. I agree they should both be fixed up.

...

Quoted 15 lines
Quoted 15 lines

I guess
you could add this hunk to your patch:

(quoted diff) +1 -1
@@ -915,7 +915,7 @@ void cp_update_scsw(struct channel_program *cp,
 union scsw *scsw)
  	 * in the ioctl directly. Path status changes etc.
  	 */
  	list_for_each_entry(chain, &cp->ccwchain_list, next) {
-		ccw_head = (u32)(u64)chain->ch_ccw;
+		ccw_head = (__force u32)virt_to_dma32(chain-
 > ch_ccw);
  		/*

  * On successful execution, cpa points just beyond the end
  * of the chain.

...

Quoted 13 lines
Quoted 5 lines

Furthermore it looks to me like the ch_iova member of struct ccwchain should
get a dma32_t type instead of u64. The same applies to quite a few variables
to the code.

Agreed. I started this some time back after the IDAW code got reworked, but have been sidetracked. The problem with ch_iova is more apparent after the dma32 stuff.

Quoted 2 lines

I could give this a try, but I think it would be better if somebody who knows what he is doing would address this :)

Quoted 8 lines
Quoted 2 lines

I'll finish them up. But v2 will have to wait until after my holiday. Thanks for reminding me of the typechecking!

I hope you didn't get me wrong: from my point of view we want one or two small patches (the above hunks), which fix the bugs, if you agree.

And then address the type checking stuff at a later point in time.

-ENOCOFFEE. Sorry, I did misunderstand. I'll send the small patches later this morning.

Quoted 3 lines

(btw: your mailer adds lot's of extra line wraps)

Ugh. Thanks for the heads up.