cxl/core/region: check interleave capability
PATCH v3 1/1 cxl/core/region: check interleave capability
Since interleave capability is not checked, a target can be attached to region successfully even it does not support the interleave way or interleave granularity.
When accessing the memory, unexpected behavior occurs due to converting HPA to an error DPA:
$ numactl -m 2 ls Segmentation fault (core dumped)
--- drivers/cxl/core/hdm.c | 5 ++++ drivers/cxl/core/region.c | 51 +++++++++++++++++++++++++++++++++++++++ drivers/cxl/cxl.h | 2 ++ drivers/cxl/cxlmem.h | 1 + 4 files changed, 59 insertions(+)
drivers/cxl/core/hdm.c +5 -0
@@ -79,6 +79,11 @@ static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm) cxlhdm->interleave_mask |=GENMASK (11 ,8 );if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_14_12 , hdm_cap)) cxlhdm->interleave_mask |=GENMASK (14 ,12 ); + cxlhdm->iw_cap_mask =BIT (1 ) |BIT (2 ) |BIT (4 ) |BIT (8 ); +if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY , hdm_cap)) + cxlhdm->iw_cap_mask |=BIT (3 ) |BIT (6 ) |BIT (12 ); +if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_16_WAY , hdm_cap)) + cxlhdm->iw_cap_mask |=BIT (16 ); }static bool should_emulate_decoders (struct cxl_endpoint_dvsec_info *info)
drivers/cxl/core/region.c +51 -0
69 lines not on this page · raw message
drivers/cxl/cxl.h +2 -0
@@ -45,6 +45,8 @@#define CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)#define CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)#define CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9) +#define CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11) +#define CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12)#define CXL_HDM_DECODER_CTRL_OFFSET 0x4#define CXL_HDM_DECODER_ENABLE BIT(1)#define CXL_HDM_DECODER0_BASE_LOW_OFFSET (i) (0x20 * (i) + 0x10)
drivers/cxl/cxlmem.h +1 -0
@@ -853,6 +853,7 @@ struct cxl_hdm {unsigned int decoder_count;unsigned int target_count;unsigned int interleave_mask; +unsigned int iw_cap_mask;struct cxl_port *port; };
Signature · 1 line
2.37.3
ping
Quoted 96 lines
-----Original Message-----
From: Yao Xingtao <[email protected]>
Sent: Tuesday, April 9, 2024 10:26 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; Yao, Xingtao/姚 幸涛 <[email protected]>
Subject: [PATCH v3] cxl/core/region: check interleave capabilitySince interleave capability is not checked, a target can be attached to region successfully even it does not support the interleave way or interleave granularity.
When accessing the memory, unexpected behavior occurs due to converting HPA to an error DPA:
$ numactl -m 2 ls Segmentation fault (core dumped)Link:
https://lore.kernel.org/qemu-devel/[email protected]
omSigned-off-by Yao Xingtao <[email protected]>--- drivers/cxl/core/hdm.c | 5 ++++ drivers/cxl/core/region.c | 51 +++++++++++++++++++++++++++++++++++++++ drivers/cxl/cxl.h | 2 ++ drivers/cxl/cxlmem.h | 1 + 4 files changed, 59 insertions(+)drivers/cxl/core/hdm.c +5
-0@@ -79,6 +79,11 @@ static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm) cxlhdm->interleave_mask |=GENMASK (11 ,8 );if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_14_12 , hdm_cap)) cxlhdm->interleave_mask |=GENMASK (14 ,12 ); + cxlhdm->iw_cap_mask =BIT (1 ) |BIT (2 ) |BIT (4 ) |BIT (8 ); +if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY , hdm_cap)) + cxlhdm->iw_cap_mask |=BIT (3 ) |BIT (6 ) |BIT (12 ); +if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_16_WAY , hdm_cap)) + cxlhdm->iw_cap_mask |=BIT (16 ); }static bool
should_emulate_decoders(struct cxl_endpoint_dvsec_info *info)drivers/cxl/core/region.c +35
-0@@ -1210,6 +1210,41 @@ static int check_last_peer(struct cxl_endpoint_decoder *cxled, return 0; } +static int check_interleave_cap (struct cxl_decoder *cxld,int iw,int ig) +{ +struct cxl_port *port =to_cxl_port (cxld->dev.parent); +struct cxl_hdm *cxlhdm =dev_get_drvdata (&port->dev); +unsigned int interleave_mask; +u8 eiw; +u16 eig; +int rc, start_pos, stop_pos; + + rc =ways_to_eiw (iw, &eiw); +if (rc) +return rc; + +if (!(cxlhdm->iw_cap_mask &BIT (iw))) +return -EOPNOTSUPP ; + + rc =granularity_to_eig (ig, &eig); +if (rc) +return rc; + +if (eiw >=8 ) + start_pos = eiw + eig -1 ; +else + start_pos = eiw + eig +7 ; + stop_pos = eig +8 ; +if (stop_pos > start_pos) +return 0 ; + + interleave_mask =GENMASK (start_pos, stop_pos); +if ((interleave_mask & cxlhdm->interleave_mask) != interleave_mask) +return -EOPNOTSUPP ; + +return 0 ; +} + static int cxl_port_setup_targets(struct cxl_port *port, struct cxl_region *cxlr,drivers/cxl/core/region.c +0
-0struct cxl_endpoint_decoder *cxled)
drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1398
Quoted 4 lines
drivers/cxl/core/region.c +1
-0@@ -1360,6 +1395,14 @@ static int cxl_port_setup_targets(struct cxl_port *port,return -ENXIO ; } }else { + rc =check_interleave_cap (cxld, iw, ig);
I found that when the switch decoder is passthrough, this check will fail because single ported host-bridges need not publish a decoder capability.
But I can not find a way to check the decoder is passthrough, so I decide to append a flag called 'passthrough' to struct cxl_switch_decoder, and when a passthrough decoder is added, set passthrough of cxl_switch_decoder as true.
In check_interleave_cap(), when the decoder is switch decoder and the passthrough flag is true, skip the following check.
Any ideas?
Quoted 53 lines
drivers/cxl/core/region.c +15
-0+if (rc) { +dev_dbg (&cxlr->dev, +"%s:%s iw: %d ig: %d is not supported\n" , +dev_name (port->uport_dev), +dev_name (&port->dev), iw, ig); +return rc; + } cxld->interleave_ways = iw; cxld->interleave_granularity = ig; cxld->hpa_range = (struct range ) {@@ -1796,6 +1839,14 @@ static int cxl_region_attach(struct cxl_region *cxlr,struct cxl_dport *dport;int rc = -ENXIO ; + rc =check_interleave_cap (&cxled->cxld, p->interleave_ways, + p->interleave_granularity); +if (rc) { +dev_dbg (&cxlr->dev,"%s iw: %d ig: %d is not supported\n" , +dev_name (&cxled->cxld.dev), p->interleave_ways, + p->interleave_granularity); +return rc; + }if (cxled->mode != cxlr->mode) { dev_dbg(&cxlr->dev, "%s region mode: %d mismatch: %d\n", dev_name(&cxled->cxld.dev), cxlr->mode,
cxled->mode);drivers/cxl/cxl.h +2
-0@@ -45,6 +45,8 @@#define CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)#define CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)#define CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9) +#define CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11) +#define CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12)#define CXL_HDM_DECODER_CTRL_OFFSET 0x4#define CXL_HDM_DECODER_ENABLE BIT(1)#define CXL_HDM_DECODER0_BASE_LOW_OFFSET (i) (0x20 * (i) + 0x10)drivers/cxl/cxlmem.h +1
-1@@ -853,6 +853,7 @@ struct cxl_hdm {unsigned int decoder_count;unsigned int target_count;unsigned int interleave_mask; +unsigned int iw_cap_mask;struct cxl_port *port; }; --2.37.3
Quoted 8 lines · On Mon, 8 Apr 2024 22:26:21 -0400 Yao Xingtao <[email protected]> wrote:
Since interleave capability is not checked, a target can be attached to region successfully even it does not support the interleave way or interleave granularity.
When accessing the memory, unexpected behavior occurs due to converting HPA to an error DPA:
$ numactl -m 2 ls Segmentation fault (core dumped)
I'd drop this comment - a real device should have rejected the capacity. We don't need the kernel change log to point out the qemu emulation is buggy :)
Quoted 3 lines
A few minor things mostly around naming.
Jonathan
Quoted 46 lines
--- drivers/cxl/core/hdm.c | 5 ++++ drivers/cxl/core/region.c | 51 +++++++++++++++++++++++++++++++++++++++ drivers/cxl/cxl.h | 2 ++ drivers/cxl/cxlmem.h | 1 + 4 files changed, 59 insertions(+)drivers/cxl/core/hdm.c +5
-0@@ -79,6 +79,11 @@ static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm) cxlhdm->interleave_mask |=GENMASK (11 ,8 );if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_14_12 , hdm_cap)) cxlhdm->interleave_mask |=GENMASK (14 ,12 ); + cxlhdm->iw_cap_mask =BIT (1 ) |BIT (2 ) |BIT (4 ) |BIT (8 ); +if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY , hdm_cap)) + cxlhdm->iw_cap_mask |=BIT (3 ) |BIT (6 ) |BIT (12 ); +if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_16_WAY , hdm_cap)) + cxlhdm->iw_cap_mask |=BIT (16 ); }static bool should_emulate_decoders (struct cxl_endpoint_dvsec_info *info)drivers/cxl/core/region.c +15
-0@@ -1210,6 +1210,41 @@ static int check_last_peer(struct cxl_endpoint_decoder *cxled,return 0 ; } +static int check_interleave_cap (struct cxl_decoder *cxld,int iw,int ig) +{ +struct cxl_port *port =to_cxl_port (cxld->dev.parent); +struct cxl_hdm *cxlhdm =dev_get_drvdata (&port->dev); +unsigned int interleave_mask; +u8 eiw; +u16 eig; +int rc, start_pos, stop_pos; + + rc =ways_to_eiw (iw, &eiw); +if (rc) +return rc; + +if (!(cxlhdm->iw_cap_mask &BIT (iw))) +return -EOPNOTSUPP ;
drivers/cxl/core/region.c static int check_last_peer(struct cxl_endpoint_decoder *cxled, · line 1237
Quoted 10 lines
drivers/cxl/core/region.c +10
-0+ + rc =granularity_to_eig (ig, &eig); +if (rc) +return rc; + +if (eiw >=8 ) + start_pos = eiw + eig -1 ; +else + start_pos = eiw + eig +7 ; + stop_pos = eig +8 ;
I'd like to see a comment with a specification reference for this maths. Probably the implementation note which oddly ended up in 8.2.20.13 Decoder Protection. in r3.1
drivers/cxl/core/region.c static int check_last_peer(struct cxl_endpoint_decoder *cxled, · line 1241
Quoted 4 lines
drivers/cxl/core/region.c +4
-0+if (stop_pos > start_pos) +return 0 ; + + interleave_mask =GENMASK (start_pos, stop_pos);
I'd use high_pos and low_pos to avoid ambiguity around start and stop and which is which (I thought this was backwards at first glance)
drivers/cxl/core/region.c static int check_last_peer(struct cxl_endpoint_decoder *cxled, · line 1242
Quoted 1 line
drivers/cxl/core/region.c +1
-0+if ((interleave_mask & cxlhdm->interleave_mask) != interleave_mask)
This is effectively checking for one not contained in the other. Can use the perhaps simpler check of is the interleave_mask no overlapping with everything outside of cxlhdm->interleave_mask
if (interleave_mask & ~cxlhd->interleave_mask)
I think...
Quoted 63 lines
drivers/cxl/core/region.c +21
-0+return -EOPNOTSUPP ; + +return 0 ; +} +static int cxl_port_setup_targets (struct cxl_port *port,struct cxl_region *cxlr,struct cxl_endpoint_decoder *cxled)@@ -1360,6 +1395,14 @@ static int cxl_port_setup_targets(struct cxl_port *port,return -ENXIO ; } }else { + rc =check_interleave_cap (cxld, iw, ig); +if (rc) { +dev_dbg (&cxlr->dev, +"%s:%s iw: %d ig: %d is not supported\n" , +dev_name (port->uport_dev), +dev_name (&port->dev), iw, ig); +return rc; + } cxld->interleave_ways = iw; cxld->interleave_granularity = ig; cxld->hpa_range = (struct range) {@@ -1796,6 +1839,14 @@ static int cxl_region_attach(struct cxl_region *cxlr,struct cxl_dport *dport;int rc = -ENXIO ; + rc =check_interleave_cap (&cxled->cxld, p->interleave_ways, + p->interleave_granularity); +if (rc) { +dev_dbg (&cxlr->dev,"%s iw: %d ig: %d is not supported\n" , +dev_name (&cxled->cxld.dev), p->interleave_ways, + p->interleave_granularity); +return rc; + }if (cxled->mode != cxlr->mode) {dev_dbg (&cxlr->dev,"%s region mode: %d mismatch: %d\n" ,dev_name (&cxled->cxld.dev), cxlr->mode, cxled->mode);drivers/cxl/cxl.h +2
-0@@ -45,6 +45,8 @@#define CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)#define CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)#define CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9) +#define CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11) +#define CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12)#define CXL_HDM_DECODER_CTRL_OFFSET 0x4#define CXL_HDM_DECODER_ENABLE BIT(1)#define CXL_HDM_DECODER0_BASE_LOW_OFFSET (i) (0x20 * (i) + 0x10)drivers/cxl/cxlmem.h +1
-0@@ -853,6 +853,7 @@ struct cxl_hdm {unsigned int decoder_count;unsigned int target_count;unsigned int interleave_mask; +unsigned int iw_cap_mask;struct cxl_port *port; };
Quoted 8 lines · Yao Xingtao
Since interleave capability is not checked, a target can be attached to region successfully even it does not support the interleave way or interleave granularity.
When accessing the memory, unexpected behavior occurs due to converting HPA to an error DPA:
$ numactl -m 2 ls Segmentation fault (core dumped)
The commit comments only explain what the issue is before the change, maybe add some sentence to describe the change?
Fan
Quoted 130 lines
Signed-off-by Yao Xingtao <[email protected]>--- drivers/cxl/core/hdm.c | 5 ++++ drivers/cxl/core/region.c | 51 +++++++++++++++++++++++++++++++++++++++ drivers/cxl/cxl.h | 2 ++ drivers/cxl/cxlmem.h | 1 + 4 files changed, 59 insertions(+)drivers/cxl/core/hdm.c +5
-0@@ -79,6 +79,11 @@ static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm) cxlhdm->interleave_mask |=GENMASK (11 ,8 );if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_14_12 , hdm_cap)) cxlhdm->interleave_mask |=GENMASK (14 ,12 ); + cxlhdm->iw_cap_mask =BIT (1 ) |BIT (2 ) |BIT (4 ) |BIT (8 ); +if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY , hdm_cap)) + cxlhdm->iw_cap_mask |=BIT (3 ) |BIT (6 ) |BIT (12 ); +if (FIELD_GET (CXL_HDM_DECODER_INTERLEAVE_16_WAY , hdm_cap)) + cxlhdm->iw_cap_mask |=BIT (16 ); }static bool should_emulate_decoders (struct cxl_endpoint_dvsec_info *info)drivers/cxl/core/region.c +51
-0@@ -1210,6 +1210,41 @@ static int check_last_peer(struct cxl_endpoint_decoder *cxled,return 0 ; } +static int check_interleave_cap (struct cxl_decoder *cxld,int iw,int ig) +{ +struct cxl_port *port =to_cxl_port (cxld->dev.parent); +struct cxl_hdm *cxlhdm =dev_get_drvdata (&port->dev); +unsigned int interleave_mask; +u8 eiw; +u16 eig; +int rc, start_pos, stop_pos; + + rc =ways_to_eiw (iw, &eiw); +if (rc) +return rc; + +if (!(cxlhdm->iw_cap_mask &BIT (iw))) +return -EOPNOTSUPP ; + + rc =granularity_to_eig (ig, &eig); +if (rc) +return rc; + +if (eiw >=8 ) + start_pos = eiw + eig -1 ; +else + start_pos = eiw + eig +7 ; + stop_pos = eig +8 ; +if (stop_pos > start_pos) +return 0 ; + + interleave_mask =GENMASK (start_pos, stop_pos); +if ((interleave_mask & cxlhdm->interleave_mask) != interleave_mask) +return -EOPNOTSUPP ; + +return 0 ; +} +static int cxl_port_setup_targets (struct cxl_port *port,struct cxl_region *cxlr,struct cxl_endpoint_decoder *cxled)@@ -1360,6 +1395,14 @@ static int cxl_port_setup_targets(struct cxl_port *port,return -ENXIO ; } }else { + rc =check_interleave_cap (cxld, iw, ig); +if (rc) { +dev_dbg (&cxlr->dev, +"%s:%s iw: %d ig: %d is not supported\n" , +dev_name (port->uport_dev), +dev_name (&port->dev), iw, ig); +return rc; + } cxld->interleave_ways = iw; cxld->interleave_granularity = ig; cxld->hpa_range = (struct range) {@@ -1796,6 +1839,14 @@ static int cxl_region_attach(struct cxl_region *cxlr,struct cxl_dport *dport;int rc = -ENXIO ; + rc =check_interleave_cap (&cxled->cxld, p->interleave_ways, + p->interleave_granularity); +if (rc) { +dev_dbg (&cxlr->dev,"%s iw: %d ig: %d is not supported\n" , +dev_name (&cxled->cxld.dev), p->interleave_ways, + p->interleave_granularity); +return rc; + }if (cxled->mode != cxlr->mode) {dev_dbg (&cxlr->dev,"%s region mode: %d mismatch: %d\n" ,dev_name (&cxled->cxld.dev), cxlr->mode, cxled->mode);drivers/cxl/cxl.h +2
-0@@ -45,6 +45,8 @@#define CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)#define CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)#define CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9) +#define CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11) +#define CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12)#define CXL_HDM_DECODER_CTRL_OFFSET 0x4#define CXL_HDM_DECODER_ENABLE BIT(1)#define CXL_HDM_DECODER0_BASE_LOW_OFFSET (i) (0x20 * (i) + 0x10)drivers/cxl/cxlmem.h +1
-0@@ -853,6 +853,7 @@ struct cxl_hdm {unsigned int decoder_count;unsigned int target_count;unsigned int interleave_mask; +unsigned int iw_cap_mask;struct cxl_port *port; };Signature · 1 line
2.37.3