From: Dave Jiang <dave.jiang@intel.com>
To: "Lucero Palau, Alejandro" <alejandro.lucero-palau@amd.com>,
alucerop@amd.com, linux-cxl@vger.kernel.org, dave@stgolabs.net,
jic23@kernel.org, alison.schofield@intel.com,
vishal.l.verma@intel.com
Subject: Re: [RFC 0/3] Type2 unwinding ordering change
Date: Mon, 30 Nov 2026 11:32:58 -0700 [thread overview]
Message-ID: <b89af375-e955-48c8-98e9-4e444aa47b91@intel.com> (raw)
In-Reply-To: <3fabb5c9-68ae-40e7-ba02-c22d91eaf1c4@amd.com>
On 11/23/26 5:53 AM, Lucero Palau, Alejandro wrote:
>
> On 20/11/2026 23:54, Dave Jiang wrote:
>>
>> On 11/19/26 1:13 AM, Lucero Palau, Alejandro wrote:
>>> On 18/11/2026 23:39, Dave Jiang wrote:
>>>> On 11/18/26 12:14 PM, alucerop@amd.com wrote:
>>>>> From: Alejandro Lucero <alucerop@amd.com>
>>>>>
>>>>>
>
> <snip>
>
>
>> I don't think removing the driver first can be made to work for every trigger. I ran your RFC with both fixes on QEMU, with the cxl-type2
>> device from Manish's RFC and a small test driver. With accel, memdev, endpoint and port unbind, region unbind and PCI removal, the driver's
>> remove() still finds its HPA under the region. With region deletion and with cxl_acpi unbind or rmmod, it finds the HPA no longer reserved by a region.
>
>
> Not sure I understand this, but if so, your results are like mine except the region deletion case, but not sure what you mean with it as you say region unbind works with the type2 driver exiting first. I know there is an issue with cxl_acpi removal what I'll try to address before a potential new version of my approach.
Region unbind and region deletion are different paths. Unbind is "echo regionN > /sys/bus/cxl/drivers/cxl_region/unbind". The region device and its HPA stay, and your driver unbinds before anything is released. With a single function, that one works.
Deletion is "echo regionN > /sys/bus/cxl/devices/decoderX.Y/delete_region". That calls unregister_region() under regions_lock and releases the HPA. Your driver's remove() runs later and finds the HPA no longer under the
region.
You may not see delete_region on your platform. It only exists when the CFMWS also allows Host-only memory. QEMU sets both Device Coherent and Host-only by default.
The crash from my first mail is still there with both fixes. If the accel binds while cxl_acpi isn't bound, cxl_mem probe fails before an endpoint exists. cxl_memdev_unregister() still calls delete_endpoint(), and KASAN reports a null-ptr-deref in delete_endpoint().
>
>
>>
>> Region deletion holds regions_lock and calls unregister_region() with the driver still bound. The driver's teardown needs regions_lock too, so it can't run first there. Dan made the same point in the v17 thread [1]: "The difficult part about that is that the remove path for regions and CXL ports hold locks that prevent the accelerator remove path from running."
>
>
> Right. And this points to the complexity I refer to ...
>
>
>> On cxl_acpi unbind, the CXL window is already gone when remove() runs. endpoint_unregister_region() then drops the last root decoder reference while holding its regions_lock, and the mutex debug code warns in mutex_destroy().
>>
>> Skipping autoremove for every DEVMEM decoder also leaks the ones the driver doesn't own. The committed host bridge decoder was never
>> unregistered on any trigger I ran, and its port was never released.
>>
>> I'm trying a different approach based on Dan's guidance. It doesn't try to order the teardown. It works whichever runs first, the region
>> teardown or the driver's remove().
>
>
> ... and your approach, IMO, does not help for reducing it adding this new defunct state. I say in the cover letter that there could be other solutions and you probed it, but the changes this approach needs are not trivial. Compare it to the changes I introduced in patch 2, as the other patches are traces and doc. And if mine works (addressing current issues) multipf support comes easily.
Yeah it's more code. But I don't think patch 2 alone is the right comparison yet. It still needs fixes for region deletion, cxl_acpi unbind and the crash above. On QEMU the committed host bridge decoder is also never unregistered, on any trigger I ran. The defunct state matches
what Dan describes in [1].
On multi-PF, I ran your v2 1/4 and 3/4 on top of your RFC with both fixes on QEMU. 2/4 isn't needed since your RFC already sets attach->cxlr.
PF0 unbind and endpoint unbind deadlock when one function is still probing and another one starts probing. The region teardown waits in
wait_for_device_probe() from device_links_unbind_consumers(). The second function's probe waits in cxl_get_range_and_link() for PF0's device lock, which PF0's unbind holds.
With multi-PF, region unbind also hits the WARN in device_links_driver_bound() when a function probes during the unbind. The wait in the deadlock and the WARN both come from the device link. My branch has no link and passes the same cases.
>
>
> Let me just study yours with more detail and try with the mocking you rely on. It is something I wanted to master and this is the right time for understanding your results and hopefully help in the future with this testing code.
>
Sounds good. The cxl_test patches are on [2]. If you want to run the QEMU cases too, Manish's type2 RFC is at [3], and the test driver and
scripts are at [4]. There's a driver for my branch, one for your RFC, and one for your RFC with v2 3/4.
The other functions are dummy PCI devices at functions 1 and 2 of the Type2's slot. The driver sleeps in probe and remove to hit the windows. Let me know if anything doesn't work for you.
[1]: https://lore.kernel.org/linux-cxl/6892325deccdb_55f09100fb@dwillia2-xfh.jf.intel.com.notmuch/
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/log/?h=type2-revoke2
[3]: https://github.com/davejiang/qemu/tree/cxl-type2-rfc
[4]: https://github.com/davejiang/cxl-testing/tree/main/qemu-type2
DJ
>
> And thank you for taking the time looking at this issue.
>
>
>>
>> When a region goes away while its Type2 driver is still bound, the region goes defunct. The region device and its targets go away as
>> before, but the HPA stays reserved and the driver detach is scheduled. The HPA is released when the last user detaches. Dan's reply [1] expects this: "a region can go into a defunct state if someone triggers removal actions in the wrong order".
>>
>> The decoders stay programmed, since attach regions are CXL_REGION_F_LOCK. So the HPA keeps decoding to the device at least until the driver's remove() returns. It needs no driver callback.
>>
>> On "all-right-state or no state", the driver uses the HPA after probe, and it stays valid until remove() returns. While the region is defunct, a new user gets -EPROBE_DEFER. While a Type2 driver owns the region, writes to targetN and size return -EBUSY.
>>
>> For multi-PF I dropped the device link. With the other functions as consumers of the region, deleting the region unbinds them from
>> device_del(). If a function is still probing, device_links_unbind_consumers() calls wait_for_device_probe(), which waits for every probe in flight. Region deletion runs with regions_lock and port locks held, or from PF0's own failing probe.
>>
>> I hit three hangs under cxl_test. With your v2, cxl_get_range_and_link() takes PF0's device lock, and region deletion during PF0's unbind waits for that function's probe.
>>
>> With the lookup fixed, PF0's probe can fail after devm_cxl_probe_mem() while another function is still probing, and the teardown ends up
>> waiting on PF0's own probe. A memdev unbind can also wait while holding the host bridge port lock, while a second accelerator probing on the same host bridge needs that lock.
>>
>> Working around them inside the link scheme got worse. A trylock on PF0 avoided the first hang, but left functions stuck in deferred probe in 2 of 6 loads. Linking while the region was unbinding hit WARNs in device_links_driver_bound() and device_links_driver_cleanup(). Avoiding those meant checking the region's dev->links.status from CXL code.
>>
>> Instead, devm_cxl_add_region_user() registers the function as a user of PF0's region and returns the HPA. I renamed it from
>> cxl_get_range_and_link() since there's no link anymore. On success, the only CXL lock it takes is the memdev's attach_lock. When the region goes away, each function gets its own async detach.
>>
>> It returns -EPROBE_DEFER while PF0 is still probing or its region is going away, and -ENODEV if PF0 is bound without CXL. PF0 still reads as bound while it unbinds, so a function probing then gets -ENODEV too.
>>
>> I think this is better than the link. Nothing waits for a probe while CXL locks are held, and all three hang tests pass. PF0 and the other
>> functions follow the same rule. The HPA stays reserved until the last of them detaches, in any order. It also needs nothing from the driver core.
>>
>> I tested everything with cxl_test and on QEMU with Manish's type2 RFC and a small type2 test driver. On QEMU, dummy PCI devices at functions 1 and 2 of the Type2's slot act as the other functions, and a Type3 bound to the test driver stands in for a second accelerator on the same host bridge. The sfc patches are build tested only.
>>
>> The branch is at [2]. Please take a look and let me know what you think.
>>
>> [1]: https://lore.kernel.org/linux-cxl/6892325deccdb_55f09100fb@dwillia2-xfh.jf.intel.com.notmuch/
>> [2]: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/log/?h=type2-revoke2
>>
>> DJ
next prev parent reply other threads:[~2026-11-30 18:33 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 19:14 [RFC 0/3] Type2 unwinding ordering change alucerop
2026-11-18 19:14 ` [RFC 1/3] cxl: add tracepoints to the unwinding code path alucerop
2026-11-18 18:00 ` sashiko-bot
2026-11-18 19:14 ` [RFC 2/3] cxl: make explicit removal of endpoint and decoder for Type2 alucerop
2026-11-18 18:09 ` sashiko-bot
2026-11-18 19:14 ` [RFC 3/3] Documentation/cxl: add document about Type2 binding and unbinding alucerop
2026-11-18 18:04 ` sashiko-bot
2026-11-18 23:39 ` [RFC 0/3] Type2 unwinding ordering change Dave Jiang
2026-11-19 8:13 ` Lucero Palau, Alejandro
2026-11-20 23:54 ` Dave Jiang
2026-11-23 12:53 ` Lucero Palau, Alejandro
2026-11-30 18:32 ` Dave Jiang [this message]
2026-11-19 10:00 ` Lucero Palau, Alejandro
2026-11-20 15:17 ` Lucero Palau, Alejandro
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b89af375-e955-48c8-98e9-4e444aa47b91@intel.com \
--to=dave.jiang@intel.com \
--cc=alejandro.lucero-palau@amd.com \
--cc=alison.schofield@intel.com \
--cc=alucerop@amd.com \
--cc=dave@stgolabs.net \
--cc=jic23@kernel.org \
--cc=linux-cxl@vger.kernel.org \
--cc=vishal.l.verma@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox