From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.11 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796063581; b=NIwVa0/qkk4BuLMHGHm3p9DvznkAXU8S2QrYMZmQbe/hhIH3x2afSUJ79aM4J28YCtsj EDNISGqD1zBW3ZyIiUfrG3Yoghjtn/OCpNWzqufAwDF6dtfkJlBkrKk8xXTbyGWDrVzuj o4u5iGmeI5mj8IO/RhqJ1fH/OfHRLYH42d9fVf2QEfIASDO7Rhju+OYLZmPanJV+rktNx pNH7dQEsC8TT/nZuiAj+/uc224uW2yAkQGJSFXOATHADxLyPKdzEu1PmjHaZtGgFlJrl/ KLMDUBpCXh3+vsLM7LS9ltJkBmkoUyEbMbGyGT+vqGTm0N8H/jK1Sw4noD/H8hQY+mg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796063581; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:Subject:To:References:From:Content-Language: In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=EvK6cl/uaV3A+cHdB8C9CY4oXhcE30qbrhF+tArHO4Q=; b=38nS5NgQQUPjWMkL34/Gq1njl+JKS2HUK7HfpJvIx6dEdZaCXHi+RYfxYfbGecg0QZhH cvIzzVherdNZ3vjbVCIDHBNK9HGnAfYakqfAWg8EQ34ehHN/TzSYS3zcG917x4RubAIc3 44I7p/tGde72nZYrcodEJa28WlTcjxHynXnCGfm6ojS2reQT6H0yDE31zRNEXjYolCV7b Bq3Pjp908STTJsdhhFzkD2+TVU5XT2462OQKDDUHFv9sWgqnQzdkpOhG/M5pJv+DT9LGG lOVAJjIwYIbLZwwOQah4uaQvlUbyTzbLVi0oIDf9y8AcgnZD88To1hX9lVsMBuqL5SQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Tb4RBWUk; arc=none smtp.remote-ip=192.198.163.11 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 06C46606A320 for ; Mon, 30 Nov 2026 18:33:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 06C46606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Tb4RBWUk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796063581; x=1827599581; h=message-id:date:mime-version:subject:to:references:from: in-reply-to:content-transfer-encoding; bh=EvK6cl/uaV3A+cHdB8C9CY4oXhcE30qbrhF+tArHO4Q=; b=Tb4RBWUk3AJUufTewFPVB1Yr3kVBaYg/wLqQZLJOYfZul5HStVQd8H+1 QiYK561l+uDzwjA85OPm5qskscqW8s0Nv5L2+W6/Cdn+TTFFHhmXEuG8D jC8VkcheUrz6s5TqdNfa7iKSzqu2yrKRlSsNWlYvao7IKZZVRpLxuGHUE Y3ObMmDSYTN/Q2y0on+ZoN1ADHBat+Ob29T/MdOno5Iw1Bed09IS6KOBc FJDSG0Rms05ILHOhldMdSi/mTddTyxg6REAqZ+qc/PUVhyeOKa3RHb/zc kUDtxSyGoII3R70EK4u6Jhm+ukbOt1s3IjDzXNkJ9xuDMtyn1qIH5NIrg w==; X-CSE-ConnectionGUID: qp0lIN97T+OKtUWIJvZ0nw== X-CSE-MsgGUID: AYk+O8C0SEyaqy8HUPY83g== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4823859" X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="4823859" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 10:33:00 -0800 X-CSE-ConnectionGUID: E6L2K3TvQ2uCDLvKBAcB9g== X-CSE-MsgGUID: 2n4Nzw8+TJWBFzH6/6KjRA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="12845379" Received: from cooperst-gp92.amr.corp.intel.com (HELO [10.125.110.218]) ([10.125.110.218]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 10:32:59 -0800 Message-ID: Date: Mon, 30 Nov 2026 11:32:58 -0700 X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC 0/3] Type2 unwinding ordering change To: "Lucero Palau, Alejandro" , alucerop@amd.com, linux-cxl@vger.kernel.org, dave@stgolabs.net, jic23@kernel.org, alison.schofield@intel.com, vishal.l.verma@intel.com References: <20261118191431.21781-1-alucerop@amd.com> <58fdf73a-a33a-44fa-bd35-a9610624d3b3@intel.com> <3fabb5c9-68ae-40e7-ba02-c22d91eaf1c4@amd.com> From: Dave Jiang Content-Language: en-US In-Reply-To: <3fabb5c9-68ae-40e7-ba02-c22d91eaf1c4@amd.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 >>>>> >>>>> > > > > >> 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