FAILED: patch "[PATCH] cxl/port: Fix use-after-free, permit out-of-order decoder" failed to apply to 6.1-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x 101c268bd2f37e965a5468353e62d154db38838e
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2024110539-swooned-cold-53f5@gregkh' --subject-prefix 'PATCH 6.1.y' HEAD^..
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 101c268bd2f37e965a5468353e62d154db38838e Mon Sep 17 00:00:00 2001
From: Dan Williams <[email protected]>
Date: Tue, 22 Oct 2024 18:43:49 -0700
Subject: [PATCH] cxl/port: Fix use-after-free, permit out-of-order decoder
shutdown
In support of investigating an initialization failure report [1], cxl_test was updated to register mock memory-devices after the mock root-port/bus device had been registered. That led to cxl_test crashing with a use-after-free bug with the following signature:
cxl_port_attach_region: cxl region3: cxl_host_bridge.0:port3 decoder3.0 add: mem0:decoder7.0 @ 0 next: cxl_switch_uport.0 nr_eps: 1 nr_targets: 1
cxl_port_attach_region: cxl region3: cxl_host_bridge.0:port3 decoder3.0 add: mem4:decoder14.0 @ 1 next: cxl_switch_uport.0 nr_eps: 2 nr_targets: 1
cxl_port_setup_targets: cxl region3: cxl_switch_uport.0:port6 target[0] = cxl_switch_dport.0 for mem0:decoder7.0 @ 0
1) cxl_port_setup_targets: cxl region3: cxl_switch_uport.0:port6 target[1] = cxl_switch_dport.4 for mem4:decoder14.0 @ 1 [..]
cxld_unregister: cxl decoder14.0:
cxl_region_decode_reset: cxl_region region3:
mock_decoder_reset: cxl_port port3: decoder3.0 reset
2) mock_decoder_reset: cxl_port port3: decoder3.0: out of order reset, expected decoder3.1
cxl_endpoint_decoder_release: cxl decoder14.0:
[..]
cxld_unregister: cxl decoder7.0:
3) cxl_region_decode_reset: cxl_region region3:
Oops: general protection fault, probably for non-canonical address 0x6b6b6b6b6b6b6bc3: 0000 [#1] PREEMPT SMP PTI
[..]
RIP: 0010:to_cxl_port+0x8/0x60 [cxl_core]
[..]
Call Trace:
<TASK>
cxl_region_decode_reset+0x69/0x190 [cxl_core]
cxl_region_detach+0xe8/0x210 [cxl_core]
cxl_decoder_kill_region+0x27/0x40 [cxl_core]
cxld_unregister+0x5d/0x60 [cxl_core]At 1) a region has been established with 2 endpoint decoders (7.0 and 14.0). Those endpoints share a common switch-decoder in the topology (3.0). At teardown, 2), decoder14.0 is the first to be removed and hits the "out of order reset case" in the switch decoder. The effect though is that region3 cleanup is aborted leaving it in-tact and referencing decoder14.0. At 3) the second attempt to teardown region3 trips over the stale decoder14.0 object which has long since been deleted.
The fix here is to recognize that the CXL specification places no mandate on in-order shutdown of switch-decoders, the driver enforces in-order allocation, and hardware enforces in-order commit. So, rather than fail and leave objects dangling, always remove them.
In support of making cxl_region_decode_reset() always succeed, cxl_region_invalidate_memregion() failures are turned into warnings. Crashing the kernel is ok there since system integrity is at risk if caches cannot be managed around physical address mutation events like CXL region destruction.
A new device_for_each_child_reverse_from() is added to cleanup port->commit_end after all dependent decoders have been disabled. In other words if decoders are allocated 0->1->2 and disabled 1->2->0 then port->commit_end only decrements from 2 after 2 has been disabled, and it decrements all the way to zero since 1 was disabled previously.
drivers/base/core.c +35 -0
@@ -4037,6 +4037,41 @@ int device_for_each_child_reverse(struct device *parent, void *data, }EXPORT_SYMBOL_GPL (device_for_each_child_reverse); +/** +* device_for_each_child_reverse_from - device child iterator in reversed order. +* @parent: parent struct device. +* @from: optional starting point in child list +* @fn: function to be called for each device. +* @data: data for the callback. +* +* Iterate over @parent's child devices, starting at @from, and call @fn +* for each, passing it @data. This helper is identical to +* device_for_each_child_reverse() when @from is NULL. +* +* @fn is checked each iteration. If it returns anything other than 0, +* iteration stop and that value is returned to the caller of +* device_for_each_child_reverse_from(); +*/ +int device_for_each_child_reverse_from (struct device *parent, +struct device *from,const void *data, +int (*fn)(struct device *,const void *)) +{ +struct klist_iter i; +struct device *child; +int error =0 ; + +if (!parent->p) +return 0 ; + +klist_iter_init_node (&parent->p->klist_children, &i, + (from ? &from->p->knode_parent :NULL )); +while ((child =prev_device (&i)) && !error) + error =fn (child, data); +klist_iter_exit (&i); +return error; +} +EXPORT_SYMBOL_GPL (device_for_each_child_reverse_from); +/** * device_find_child - device iterator for locating a particular device. * @parent: parent struct device
drivers/cxl/core/hdm.c +42 -8
78 lines not on this page · raw message
drivers/cxl/core/region.c +13 -35
92 lines not on this page · raw message
drivers/cxl/cxl.h +2 -1
@@ -359,7 +359,7 @@ struct cxl_decoder {struct cxl_region *region;unsigned long flags;int (*commit)(struct cxl_decoder *cxld); -int (*reset)(struct cxl_decoder *cxld); +void (*reset)(struct cxl_decoder *cxld); };/*
@@ -730,6 +730,7 @@ static inline bool is_cxl_root(struct cxl_port *port)int cxl_num_decoders_committed (struct cxl_port *port);bool is_cxl_port (const struct device *dev);struct cxl_port *to_cxl_port (const struct device *dev); +void cxl_port_commit_reap (struct cxl_decoder *cxld);struct pci_bus ;int devm_cxl_register_pci_bus (struct device *host,struct device *uport_dev,struct pci_bus *bus);
include/linux/device.h +3 -0
@@ -1078,6 +1078,9 @@ int device_for_each_child(struct device *dev, void *data, int (*fn)(struct device *dev, void *data));int device_for_each_child_reverse (struct device *dev,void *data,int (*fn)(struct device *dev,void *data)); +int device_for_each_child_reverse_from (struct device *parent, +struct device *from,const void *data, +int (*fn)(struct device *,const void *));struct device *device_find_child (struct device *dev,void *data,int (*match)(struct device *dev,void *data)); struct device *device_find_child_by_name(struct device *parent,
tools/testing/cxl/test/cxl.c +5 -9
@@ -693,26 +693,22 @@ static int mock_decoder_commit(struct cxl_decoder *cxld)return 0 ; } -static int mock_decoder_reset (struct cxl_decoder *cxld) +static void mock_decoder_reset (struct cxl_decoder *cxld) {struct cxl_port *port =to_cxl_port (cxld->dev.parent);int id = cxld->id;if ((cxld->flags &CXL_DECODER_F_ENABLE ) ==0 ) -return 0 ; +return ;dev_dbg (&port->dev,"%s reset\n" ,dev_name (&cxld->dev)); -if (port->commit_end != id) { +if (port->commit_end == id) +cxl_port_commit_reap (cxld); +else dev_dbg (&port->dev,"%s: out of order reset, expected decoder%d.%d\n" ,dev_name (&cxld->dev), port->id, port->commit_end); -return -EBUSY ; - } - - port->commit_end--; cxld->flags &= ~CXL_DECODER_F_ENABLE ; - -return 0 ; }static void default_mock_decoder (struct cxl_decoder *cxld)