On Mon, 23 Nov 2026 18:11:00 +0100
Olivier MOYSAN <olivier.moysan(a)foss.st.com> wrote:
> Hi Conor,
>
Leaving a lot more than I normally would given I don't provide
much insight but want to make sure it is visible for others.
> On 11/19/26 19:37, Conor Dooley wrote:
> > On Wed, Nov 18, 2026 at 04:17:45PM +0100, Olivier Moysan wrote:
> >> This series adds support for the STM32 Multi-function Digital Filter
> >> (MDF) found on STM32MP2 SoCs. MDF converts bitstreams from external
> >> sigma-delta modulators and PDM microphones into samples for IIO and
> >> ASoC consumers.
> >>
> >> The drivers support direct IIO conversions, cyclic DMA acquisition,
> >> CIC configuration and scaling, synchronized and interleaved filters,
> >> and external timer triggers. An ASoC DAI provides PDM microphone capture.
> >>
> >> This revision addresses the v1 review comments listed below and splits
> >> the driver patch into smaller functional patches for readability.
> >> It retains the existing IIO backend interface for external sigma-delta
> >> modulators, pending agreement on the preferred abstraction.
> >>
> >> These changes do not resolve all v1 review comments. In particular, the
> >> frontend/backend architecture, DT child-node model and complete clock
> >> topology remain open discussion points.
> >>
> >> * Open questions: MDF topology (backend/frontend)
> >>
> >> In Analog Devices use of the IIO backend framework, the AXI ADC is a
> >> hardware backend, while the AD9467 is a hardware frontend. The external
> >> sigma-delta modulator is actually a hardware frontend in the MDF signal
> >> chain. The difference from the AD9467 use case is which driver owns the
> >> aggregate IIO device.
> >
> > My red line here, that I probably made pretty clear on v1, is that what
> > is done needs to make sense when the ADC is feeding the MDF, it cannot
> > only work for the data coming in from sd modulators or w/e you connect
> > to the SITFx inputs.
> >
>
> I understand your concern. Let me first give some context on the use
> cases. This series only supports external devices connected to the
> serial interfaces (SITF): PDM microphones, which are the main use case,
> and sigma-delta modulators. Still, I think the MDF centered model
> (option 1 & 2) makes sense for the internal ADC to MDF path as well.
> Here is how it could look:
>
> - The ADC becomes a provider for the MDF filter, like an external
> modulator: it provides its resolution and vref and runs its
> conversions in MDF mode, while the filter handles decimation,
> trigger and DMA.
>
> - The routing is static. On the STM32 ADC, data routing to the MDF is a
> global setting for the ADC instance, not a per-channel configuration.
> An ADC instance feeding the MDF is dedicated to this path. It would
> not be a frontend for some channels and a backend for others at the
> same time.
>
> - The binding describes the data source of each filter: could be either
> a stream on a serial interface (the current st,sitf reference) or the
> ADC interface (reference to the ADC node). The binding can be
> completed this way without changing the rest of the model.
>
> - The same model also covers interleaved groups that mix filters fed by
> the ADC and filters fed by serial interfaces, as well as the audio
> use case described below.
>
> The STM32 ADC driver does not support a backend mode today, but it could
> be added as an exclusive per-instance mode, selected in DT, in which the
> ADC registers an IIO backend instead of an IIO device.
>
> I agree that the IIO device exposing a given analog signal is not the
> same when the signal is read directly from the ADC and when it goes
> through the MDF.
> I think this is consistent with the hardware: these are two distinct
> acquisition chains, with different configurations. As the routing is
> fixed in DT, userspace sees only one device for a given channel.
>
> >> Option 1:
> >> One option would be to generalize the existing backend abstraction to
> >> represent components contributing to an aggregate IIO device,
> >> independently of their position in the signal chain.
> >>
> >> The existing framework appears to provide the required mechanisms. None
> >> of these mechanisms inherently requires the provider to be a hardware backend.
> >> A generic component/provider model would support both topologies without
> >> duplicating interfaces or requiring reciprocal dependencies. The key
> >> point here is to preserve the compatibility of APIs and DT bindings.
> >>
> >> Option 2:
> >> Alternatively, if the backend abstraction should retain its hardware
> >> meaning, we could introduce an explicit frontend-provider abstraction
> >> sharing the existing infrastructure. Frontend operations would cover
> >> analog-side control and conversion parameters.
> >>
> >> Would generalizing the existing abstraction be acceptable, or would a
> >> separate frontend interface built on shared infrastructure be preferred?
> >
> > For either of these options, do you envisage the data being retrieved
> > from the mdf sysfs files rather than from the modulator?
> >
>
> Yes, in options 1 & 2, the MDF filter registers the IIO device that
> exposes the data. The modulator, or the ADC in the case above, only
> provides the analog-side information, such as the reference voltage used
> to compute the scale.
> This would generalize the IIO backend framework, regardless of where the
> companion component is placed in the chain.
>
> +---------------------+ +----------------+
> | frontend w/o iio dev| ----> | MDF |---> IIO dev
> | | | |
> +---------------------+ +----------------+
>
> This also resembles the way the acquisition chain is described in the
> ASoC framework, where the device is not attached to the codec. (see below)
>
> >>
> >> Option 3:
> >> An aggregate IIO device based on the component framework, as discussed
> >> in previous threads, could also be considered. I currently regard this
> >> as a fallback solution:
> >> https://lore.kernel.org/all/ec411432-4b77-4410-b0aa-b29f73b82c84@foss.st.co…
> >
> > You option 2 in the earlier thread had:
> > | 2 - DFSDM managed as a "hardware accelerator"
> > |
> > |
> > | +-------------+
> > | | Sysfs |
> > | | - Scale | +---------------+
> > | | - Raw data | | IIO char dev |
> > | +-------------+ +---------------+
> > | ^ /\
> > | | ||
> > | | || Userland
> > | -----------------------------------------------------------
> > | | || IIO fw
> > | | ||
> > | | ||
> > | -----------------------------------------------------------
> > | | ||
> > | +--|-------------||--+ +----------------+
> > | | | || | | | Drivers
> > | | | ======================= |
> > | | | | | || |
> > | | +-----------------+-----+--+ || |
> > | | +---------+-----+--+ || |
> > | +----------|---------+ +-----------||---+
> > | | ||
> > | -----------------------------------------------------------
> > | | ||
> > | +----------|---------+ +-----------||---+
> > | | | | || | Hardware
> > | | sd modulator | | DFSDM || |
> > | ============================================= |
> > | | | | |
> > | +----------+---------+ +----------------+
> > | |
> > | |
> > | Vref
> > |
> > | The DFSDM is a consumer of the SD modulator.
> > |
> > | In addition to its complexity, the main drawback of this approach is
> > | that it is unsuitable for interleaved mode.
> >
> > Two mails prior I had theorised a way to deal with the interleaved mode
> > https://lore.kernel.org/all/20261015-attentive-conflict-1a1f538de77d@spud/
> > but I never saw a response from you as to why something like that cannot
> > work. That's twice in that thread you said it was unsuitable without
> > providing the actual explanation for why?
> >
>
> Sorry, I did not respond to your proposal directly. Here is the explanation.
>
> In interleaved mode, filters 0 to N-1 are grouped, and synchronized by
> filter 0. The samples of all the filters are transferred through the
> filter 0 DMA request as a single interleaved stream. Filters 1 to N-1
> have no DMA transfer and no configuration of their own in this mode.
> Interleaved is used in audio (ex stereo) and analog (scan mode) use cases.
> Interleaved mode is needed in following use cases: multichannel audio
> capture (e.g. stereo microphones), and synchronous multichannel
> acquisition of analog sources (scan mode).
>
> With the MDF as a backend, each modulator would be a frontend
> registering its own IIO device. I see the following limitations:
>
> - One hardware buffer, several IIO devices.
> The interleaved stream is a single DMA buffer carrying samples from
> N modulators. A backend buffer is attached to a single IIO device.
> There is currently no way to share one hardware buffer between several
> frontends, or to demux it into several IIO devices.
> Jonathan described this as the swdemux/swmux concepts, which do not
> exist yet:
> https://lore.kernel.org/all/20261019022510.0d554b39@jic23-hlaptop/
>
> - Ownership of the interleaved data.
> With a dedicated cell value (e.g. -1) for the interleaved data, some
> node has to reference it. If it is one of the modulators, this
> modulator becomes in practice the aggregate device (or interleaver)
> It exposes the N channels and needs the scale and supplies of the
> other modulators.
> The choice of this modulator is arbitrary and nothing in the hardware
> reflects it. If it is a dedicated node, it describes a software
> construct rather than hardware.
>
> - Shared configuration.
> Because the configuration is common to the group, writing config
> (sampling_frequency or oversampling) on one frontend would silently
> change the other frontends.
>
> - Frontends unusable on their own.
> Filters 1 to N-1 have no DMA and no config of their own in this mode,
> so the frontend IIO devices linked to them could not acquire alone.
> Acquisition can be started through the filter 0 only.
> The current driver avoids this by only exposing the group, through
> filter 0.
>
> None of these points is impossible to solve in principle, but they
> require new IIO infrastructure (cross-device buffer demux, coordinated
> buffer enable, arbitration of shared attributes).
>
>
> * Audio use case
>
> Audio is currently the main use case for the MDF.
> The audio chain is modeled as follows in ASoC framework:
>
> Digital Audio Interface link
> +------------------------------------------------------+
> | +--------------------+ +----------------+ |
> | | DMIC | ----> | MDF | -----|--> audio device
> | | (Audio codec) | | (CPU DAI) | |
> | +--------------------+ +----------------+ |
> +------------------------------------------------------+
>
> - The sources are PDM microphones. They are handled on the ASoC side
> (e.g. the generic dmic codec). They have no IIO representation.
>
> - ALSA expects interleaved PCM frames for multichannel capture. An
> N-channel capture is exposed as a single audio device fed by the
> interleaved filter group, whereas N independent mono captures are
> exposed as N separate audio devices.
>
> - The data goes to the ASoC DAI through an ASoC callback, not to IIO
> userspace.
>
> So for audio, the interleaved stream is the data itself, not an
> optimisation of DMA resources. It is consumed as one multichannel
> stream whose sources have no IIO frontend. In a model where the MDF is
> only a backend of IIO frontends, there is no frontend to own this
> stream. The MDF would remain the data producer for audio while being a
> backend for the analog sources, i.e. two models in the same driver.
>
> This let me say that MDF as backend is "unsuitable" (complexity &
> missing IIO infrastructure)
>
> This is why I think the model should be centered on the MDF filter
> as the device producing the data, with the data sources
> (external modulators, or the internal ADC) providing their analog-side
> information. This model covers the serial interface and ADC sources,
> interleaved groups, and audio, with a single approach. Option 1 or 2
> from the cover letter, or the aggregate device of option 3, would fit it.
>
> Jonathan and Nuno, please let me know if you have an opinion on this.
> In particular on whether a frontend-provider abstraction (option 2)
> would be acceptable.
This whole thing is an interesting challenge. However, I don't have enough
visibility to really understand if this is more or less a one off thing
or more general. We have had a few ADI parts recently where we have seen
not dissimilar requirements for a composite device with shared back end
so it may well be that it is time to work out how to flesh out a more
general solution for IIO wrt to these devices.
Nuno and other ADI folk can you have a look at what Oliver has here
and conclude whether there is sufficient overlap to look for that general
solution or not?
>
> If not, it seems to me the that option 3 (aggregate IIO device based on
> component framework) has less drawbacks than the swmux/demux approach.
We are going to end up with something that at least a little bit smells
like a composite device even if we do go for a general IIO solution.
If this were all fairly low speed it would actually smell quite like
a sensor hub, but there we don't have the complexity of a DMA buffer
so we just deinterleave the streams in software.
Also remember is a separation between what we put in DT and what we do
with that description. So we may be able to describe things
more cleanly than the software model we build on top. That may give
us a path forwards before we get to a more universal solution.
IIRC you 'can' do a search for phandles that point to a device. I think
we did this somewhere to paper over reversing the direction when
the backend stuff was first introduced. It is horrible though!
I'm going to skip a detailed review of the driver for now
given my backlog is too large (and I have an excuse as we are
talking about the fundamental design)
Jonathan
>
> Best regards,
> Olivier
>
>
> > Cheers,
> > Conor.
> >
> >>
> >> * Driver implementation
> >>
> >> Serial interfaces (SITF) are shared resources managed directly by the
> >> MDF core and no longer have separate platform devices. This revision
> >> retains their DT nodes to describe configuration and clock connections,
> >> and to allow filters to reference their input bitstreams without
> >> duplicating shared settings.
> >>
> >> This revision also retains a platform device for each MDF filter, as
> >> each filter manages its own registers, interrupt, DMA channel and IIO
> >> acquisition state, with a separate suspend/resume lifecycle.
> >>
> >> * Clock management
> >>
> >> Several clock-management review comments are addressed in this revision:
> >> - The provider-side clock-frequency property has been removed. Consumers
> >> request CCK rates through assigned-clocks and assigned-clock-rates.
> >> - The clock-names property has been removed. The core acquires its single
> >> input clock by index, and CCK gating uses regmap-based clock operations.
> >> - Remove the unused clock-ranges property, as the core now explicitly
> >> shares the kernel clock with the serial interfaces and filters instead
> >> of relying on parent clock lookup.
> >>
> >> Clock topology point remains open, awaiting clarification of the MDF topology.
> >>
> >> Changes since v1:
> >> - Split the large IIO driver patch into smaller functional patches.
> >> - Replace provider-side clock-frequency with consumer-assigned CCK rates
> >> through the common clock framework.
> >> - Remove the platform devices for serial interfaces.
> >> - Replace the interleave phandle list with a filter count, covering
> >> consecutive filters starting at filter 0.
> >> - Simplify clock-output-names, remove clock-names, reset-names and
> >> filter-level clock properties, and move mdf-dai into an explicit
> >> property outside the conditional schema.
> >> - Rename the binding to match the STM32MP25 compatible, sort compatible
> >> entries, and simplify the binding patch subject.
> >> - Use unsigned long for match data, improve probe error reporting and
> >> wrapping, remove a probe-success debug message, and add kernel-doc for
> >> exported core functions.
> >> - Use scoped spinlock guards.
> >> - Remove empty serial-interface runtime-PM callbacks.
> >> - Remove the redundant ASoC platform module alias.
> >>
> >> Additional fixes since v1 (following Sashiko review):
> >> - Compute the settling-time discard count with 64-bit arithmetic.
> >> - Size cyclic DMA periods in complete scans and validate buffer and
> >> period alignment before starting DMA.
> >> - Fix backend-enable rollback so backend zero is disabled as well.
> >> - Use managed allocation for the conversion buffer and managed IIO
> >> triggered-buffer setup.
> >> - Release temporary synchronization and serial-interface firmware-node
> >> references. Replacing interleave phandles also removes the associated
> >> stored node references.
> >> - Avoid unregistering secondary interleaved filters that were never
> >> registered as IIO devices.
> >> - Use managed acquisition of the optional serial clock.
> >> - Replace the GCD-based CCK divider selection with a search for the
> >> largest valid divisor, allowing non-power-of-two divider values.
> >> - Remove the selection of the undefined COMMON_CLK_REGMAP symbol.
> >>
> >> Previous discussion:
> >> https://lore.kernel.org/all/20261001145702.2628429-1-olivier.moysan@foss.st…
> >>
> >> Signed-off-by: Olivier Moysan <olivier.moysan(a)foss.st.com>
> >> ---
> >> Olivier Moysan (11):
> >> dt-bindings: iio: adc: add stm32mp2 mdf filter
> >> iio: adc: stm32-mdf: add core registration
> >> iio: adc: stm32-mdf: add cck clocks provider
> >> iio: adc: stm32-mdf: add serial interfaces support
> >> iio: adc: stm32-mdf: add digital filter device support
> >> iio: adc: stm32-mdf: configure cic filters and sample rates
> >> iio: adc: stm32-mdf: add direct iio conversion support
> >> iio: adc: stm32-mdf: add dma buffered acquisition support
> >> iio: adc: stm32-mdf: add external timer trigger support
> >> iio: adc: stm32-mdf: add dmic audio filter support
> >> ASoC: stm32: add mdf dai support
> >>
> >> .../bindings/iio/adc/st,stm32mp25-mdf.yaml | 341 ++++
> >> drivers/iio/adc/Kconfig | 29 +
> >> drivers/iio/adc/Makefile | 3 +
> >> drivers/iio/adc/stm32-mdf-adc.c | 2080 ++++++++++++++++++++
> >> drivers/iio/adc/stm32-mdf-core.c | 899 +++++++++
> >> drivers/iio/adc/stm32-mdf-serial.c | 223 +++
> >> drivers/iio/adc/stm32-mdf.h | 275 +++
> >> include/linux/iio/adc/stm32-mdf-adc.h | 19 +
> >> sound/soc/stm/Kconfig | 15 +
> >> sound/soc/stm/Makefile | 3 +
> >> sound/soc/stm/stm32_amdf.c | 375 ++++
> >> 11 files changed, 4262 insertions(+)
> >> ---
> >> base-commit: d922ed738439e1d5b4701df7f4cdd49a09925624
> >> change-id: 20261112-upstream_mdf_v2-76040d99138e
> >>
> >> Best regards,
> >> --
> >> Olivier Moysan <olivier.moysan(a)foss.st.com>
> >>
>
>
This series tightens the alignment requirements for buffers that are shared
between confidential-computing guests and the host, and adds a common
allocator for host-shared memory.
When a guest runs with private memory, buffers shared with the hypervisor
are not only accessed by the guest. They are also accessed by the host
kernel, and the host may manage the corresponding shared/private state at a
granularity larger than the guest page size.
Architectures such as Arm can detect incorrect accesses to Realm physical
address space PFNs through GPC faults. However, relying on that as the only
line of defence is fragile and can still lead to kernel crashes. The risk
is especially visible for shared buffers that are later mmapped into
userspace, such as guest_memfd or dma-buf backed allocations. Once
userspace can access the mapping, the kernel cannot guarantee that
applications will only touch the intended 4K region rather than the whole
host page mapped into their address space. Those userspace addresses may
also be passed back into the kernel and accessed through the linear map,
resulting in a GPC fault.
To avoid this, host-shared buffers must satisfy two constraints:
- the address must be aligned to the CoCo shared-granule size
- the size must be a multiple of that granule size
The series adds a common CoCo shared-memory layer for enforcing these
constraints. It provides shared-granule geometry and range-validation
helpers, byte-oriented private/shared transition helpers, and
alloc_cc_shared_pages(). The allocator rounds a request to the architecture
shared granule, allocates suitably aligned contiguous pages, transitions
the complete allocation to shared state, and returns the transitioned size
alongside the page.
The corresponding free helper restores the complete allocation to private
state before returning it to the buddy allocator. If private state cannot
be restored safely, the allocation is deliberately leaked rather than
returning potentially shared memory for unrelated use.
The series also makes zeroing part of private-to-shared transitions, with
each architecture clearing memory at the appropriate point. Callers must
therefore initialize shared buffers after the transition; existing contents
are not preserved.
The shared-granule size defaults to PAGE_SIZE and can be overridden by an
architecture. The arm64 CCA changes to discover the host's IPA state change
alignment are not included in this series.
GIC ITS command queues and tables and the dma-buf system_cc_shared heap
use the common allocator. DMA direct, atomic DMA pools and SWIOTLB retain
their allocation paths and use the common layout or transition helpers.
Restricted DMA pools are checked for shared-granule alignment.
Hyper-V users of set_memory_encrypted() and set_memory_decrypted() are not
changed to use cc_make_shared() by this series. Those paths are not
currently used by the arm64 CCA code path, and therefore are not part of
the arm64 CCA IPA state change alignment problem addressed here.
Notes:
* Codex assisted with implementation, review and series organization.
Changes from v9:
https://lore.kernel.org/all/20261104164555.3555725-1-aneesh.kumar@kernel.org
* Leak allocations after failed sharing in the common allocator and SWIOTLB.
* Move x86 zeroing from the AMD, TDX and Hyper-V vTOM callbacks to
set_memory_decrypted(), after a successful transition
* Add set_memory_decrypted_nozero() for VGA mappings, which must change
memory attributes without clearing device memory.
* Zero memory even when encryption is inactive in the generic, arm64 and
pSeries implementations of set_memory_decrypted().
* Move pSeries zeroing after sharing. Document why pKVM and s390 must clear
private contents before sharing
* Drop the GIC ITS VPE L1 preallocation patch from this series.
This issue predates the series and will be addressed separately.
* Add mm/cc_shared.c and include/linux/cc_shared.h to the TSM MAINTAINERS
entry.
Changes from v8:
https://lore.kernel.org/all/20260924100529.1398790-1-aneesh.kumar@kernel.org
* Rebase onto the 7.4-rc1
* Move TDX and AMD (including SEV-SNP) zeroing to their finish callbacks
so private-to-shared transitions clear the final shared mapping.
* Clear __GFP_MOVABLE for shared allocations, which cannot be migrated.
* Resolve NUMA_NO_NODE in the common ITS allocation helper to preserve
placement for all ITS callers.
* Stop SWIOTLB allocation retries when the reduced pool would fall below
the shared granule size, avoiding an allocation retry loop.
Changes from v7:
https://lore.kernel.org/all/20260921144847.501151-1-aneesh.kumar@kernel.org
* Add the following new patches:
* "irqchip/gic-v3-its: Preallocate VPE L1 tables"
* "mm: Assert CoCo shared allocations may sleep"
* "mm: Zero memory during shared memory transitions"
* Drop the arm64 RHI and shared granule size patches so that the series can
be rebased on top of upstream to enable Shashiko review.
Changes from v6:
https://lore.kernel.org/all/20260904103452.1197239-1-aneesh.kumar@kernel.org
* Add a common allocator and geometry/transition helpers for CoCo host-shared
memory.
* Convert GIC ITS, dma-direct, atomic DMA pools, and the dma-buf
system_cc_shared heap to the common allocator.
* Limit dma-buf scatterlist entries to the requested buffer size so rounded
backing is not exposed to importers.
Changes from v5:
https://lore.kernel.org/all/20260706060432.1375570-1-aneesh.kumar@kernel.org
* Rebased to latest kernel
* Drop patch arm64: realm: Move Realm memory encryption ops to RSI code
Changes from v4:
https://lore.kernel.org/all/20260427063108.909019-1-aneesh.kumar@kernel.org
* Rename the helpers to use CoCo terminology
(mem_cc_shared_granule_size() / mem_cc_align_to_shared_granule() instead of
mem_decrypt_granule_size() / mem_decrypt_align()).
* Use __DMA_ATTR_ALLOC_CC_SHARED to pass CoCo shared allocation requirements
down to CMA-based allocation helpers.
* Add validation for restricted DMA pools to reject pools that are not aligned
to the shared granule size.
* Add dma-buf system heap handling for cc-shared buffers.
* Split the previous combined DMA/SWIOTLB/ITS change into smaller subsystem
patches covering ITS, DMA direct, SWIOTLB, restricted DMA pools, dma-buf
system heap, and arm64 Realm support.
* Rework arm64 Realm support by moving Realm memory encryption ops into RSI
code and exposing the CCA shared granule size through arm64_mem_crypt_ops.
Changes from v3:
https://lore.kernel.org/all/20260309102625.2315725-1-aneesh.kumar@kernel.org
* Fix build error reported by kernel test robot <lkp(a)intel.com>
Changes from v2:
https://lore.kernel.org/all/20251221160920.297689-1-aneesh.kumar@kernel.org
* Rebase to latest kernel
* Consider swiotlb always decrypted and don't align when allocating from swiotlb.
Changes from v1:
* Rename the helper to mem_encrypt_align
* Improve the commit message
* Handle DMA allocations from contiguous memory
* Handle DMA allocations from the pool
* swiotlb is still considered unencrypted. Support for an encrypted swiotlb pool
is left as TODO and is independent of this series.
Cc: Andrew Morton <akpm(a)linux-foundation.org>
Cc: Baoquan He <baoquan.he(a)linux.dev>
Cc: Mike Rapoport <rppt(a)kernel.org>
Cc: Pasha Tatashin <pasha.tatashin(a)soleen.com>
Cc: Pratyush Yadav <pratyush(a)kernel.org>
Cc: Catalin Marinas <catalin.marinas(a)arm.com>
Cc: "Christian König" <christian.koenig(a)amd.com>
Cc: Jason Gunthorpe <jgg(a)ziepe.ca>
Cc: Joerg Roedel (AMD) <joro(a)8bytes.org>
Cc: Marc Zyngier <maz(a)kernel.org>
Cc: Marek Szyprowski <m.szyprowski(a)samsung.com>
Cc: Robin Murphy <robin.murphy(a)arm.com>
Cc: Steven Price <steven.price(a)arm.com>
Cc: Sumit Semwal <sumit.semwal(a)linaro.org>
Cc: Suzuki K Poulose <suzuki.poulose(a)arm.com>
Cc: Thomas Gleixner <tglx(a)kernel.org>
Cc: Will Deacon <will(a)kernel.org>
Cc: Russell King <linux(a)armlinux.org.uk>
Cc: Benjamin Gaignard <benjamin.gaignard(a)collabora.com>
Cc: Brian Starkey <Brian.Starkey(a)arm.com>
Cc: John Stultz <jstultz(a)google.com>
Cc: Mark Rutland <mark.rutland(a)arm.com>
Cc: Radu Rendec <radu(a)rendec.net>
Cc: "T.J. Mercier" <tjmercier(a)google.com>
Cc: Madhavan Srinivasan <maddy(a)linux.ibm.com>
Cc: Michael Ellerman <mpe(a)ellerman.id.au>
Cc: Nicholas Piggin <npiggin(a)gmail.com>
Cc: Christophe Leroy (CS GROUP) <chleroy(a)kernel.org>
Cc: Ritesh Harjani (IBM) <ritesh.list(a)gmail.com>
Cc: Shrikanth Hegde <sshegde(a)linux.ibm.com>
Cc: Alexander Gordeev <agordeev(a)linux.ibm.com>
Cc: Gerald Schaefer <gerald.schaefer(a)linux.ibm.com>
Cc: Heiko Carstens <hca(a)linux.ibm.com>
Cc: Vasily Gorbik <gor(a)linux.ibm.com>
Cc: Christian Borntraeger <borntraeger(a)linux.ibm.com>
Cc: Sven Schnelle <svens(a)linux.ibm.com>
Cc: Ingo Molnar <mingo(a)redhat.com>
Cc: Borislav Petkov <bp(a)alien8.de>
Cc: Dave Hansen <dave.hansen(a)linux.intel.com>
Cc: x86(a)kernel.org
Cc: H. Peter Anvin <hpa(a)zytor.com>
Cc: Kiryl Shutsemau <kas(a)kernel.org>
Cc: Rick Edgecombe <rick.p.edgecombe(a)intel.com>
Cc: K. Y. Srinivasan <kys(a)microsoft.com>
Cc: Haiyang Zhang <haiyangz(a)microsoft.com>
Cc: Wei Liu <wei.liu(a)kernel.org>
Cc: Dexuan Cui <decui(a)microsoft.com>
Cc: Long Li <longli(a)microsoft.com>
Cc: Paolo Bonzini <pbonzini(a)redhat.com>
Cc: Vitaly Kuznetsov <vkuznets(a)redhat.com>
Cc: Andy Lutomirski <luto(a)kernel.org>
Cc: Peter Zijlstra <peterz(a)infradead.org>
Cc: dri-devel(a)lists.freedesktop.org
Cc: iommu(a)lists.linux.dev
Cc: linaro-mm-sig(a)lists.linaro.org
Cc: linux-arm-kernel(a)lists.infradead.org
Cc: linux-media(a)vger.kernel.org
Cc: linux-mm(a)kvack.org
Cc: kvmarm(a)lists.linux.dev
Cc: patches(a)lists.linux.dev
Aneesh Kumar K.V (Arm) (13):
mm: Add an allocator for CoCo shared memory
mm: Zero memory during shared memory transitions
irqchip/gic-v3-its: Resolve the default NUMA node explicitly
irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory
allocator
dma-contiguous: Derive shared alignment from DMA attributes
dma-pool: Allocate CoCo atomic pools using CoCo shared memory
allocator
dma-direct: Align CoCo shared DMA allocations to the shared granule
size
swiotlb: Align shared IO TLB pools to the shared granule size
swiotlb: Reject misaligned restricted DMA pools for CoCo guests
dma-buf: system_heap: Limit scatterlist entries to the buffer size
dma-buf: system_heap: Allocate shared buffers using CoCo shared memory
allocator
swiotlb: Make rounded shared pool capacity allocatable
mm: Assert CoCo shared allocations may sleep
MAINTAINERS | 2 +
arch/arm/mm/dma-mapping.c | 5 +-
arch/arm64/mm/mem_encrypt.c | 8 +-
arch/powerpc/platforms/pseries/svm.c | 13 +-
arch/s390/mm/init.c | 7 +
arch/x86/hyperv/hv_init.c | 6 +-
arch/x86/include/asm/set_memory.h | 1 +
arch/x86/include/asm/vga.h | 4 +-
arch/x86/kernel/kvmclock.c | 6 +-
arch/x86/mm/mem_encrypt_amd.c | 1 +
arch/x86/mm/pat/set_memory.c | 19 +-
drivers/dma-buf/heaps/system_heap.c | 128 +++++-----
drivers/firmware/arm_rmm/rsi.c | 9 +
drivers/hv/connection.c | 41 ++-
drivers/hv/hv.c | 11 +-
drivers/hv/hv_common.c | 2 -
drivers/iommu/dma-iommu.c | 2 +-
drivers/irqchip/irq-gic-v3-its.c | 42 ++--
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 4 +
include/linux/cc_shared.h | 39 +++
include/linux/dma-map-ops.h | 9 +-
include/linux/set_memory.h | 8 +-
kernel/dma/contiguous.c | 41 ++-
kernel/dma/direct.c | 73 ++++--
kernel/dma/ops_helpers.c | 2 +-
kernel/dma/pool.c | 27 +-
kernel/dma/swiotlb.c | 94 ++++---
kernel/kexec_file.c | 3 +-
mm/Makefile | 1 +
mm/cc_shared.c | 238 ++++++++++++++++++
30 files changed, 635 insertions(+), 211 deletions(-)
create mode 100644 include/linux/cc_shared.h
create mode 100644 mm/cc_shared.c
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
prerequisite-patch-id: 121492d71cc22b2adbfdedd668868c5a449ad764
prerequisite-patch-id: ed31fd728f0adba3d60dcd6f3bae8da1a2193bcc
--
2.43.0
On Fri Nov 27, 2026 at 6:30 PM GMT, Daniel Almeida wrote:
> dma_fence_begin_signalling() and dma_fence_end_signalling() mark the
> code that the completion of a dma-fence depends on, so that lockdep can
> check it against the dma-fence rules. However, up until now, it was not
> possible to use the signalling tokens from Rust. Thus, add support for
> it.
>
> Patch 1 adds SignallingSection, a guard that begins a section when it
> is created and ends it when it is dropped. Patch 2 makes
> DriverFence::signal() take a &SignallingSection, so that its callers
> have to be in one.
Do you need it being a guard? You can do things like
let a = SignallingSection::begin();
let b = SignallingSection::begin();
drop(a);
drop(b);
to break nesting. A closure-based approach wouldn't have this issue.
Best,
Gary
>
> Patch 5 of the JobQueue series [1] changes the same signature to
> signal(&self, res). I will rebase on top of it once it lands and add
> the section to its callers then.
>
> A follow-up series adds a workqueue whose work items run in a
> signalling section. Tyr will use both.
>
> This is based on drm-rust-next.
>
> [1] https://lore.kernel.org/r/20261124122951.878199-2-phasta@kernel.org
>
> ---
> Daniel Almeida (2):
> rust: dma_buf: dma_fence: add SignallingSection
> rust: dma_buf: dma_fence: require a SignallingSection in signal()
>
> rust/kernel/dma_buf/dma_fence.rs | 77 ++++++++++++++++++++++++++++++++++++++--
> rust/kernel/dma_buf/mod.rs | 3 +-
> 2 files changed, 76 insertions(+), 4 deletions(-)
> ---
> base-commit: abfb640b2374e26de8ff81d488c39d8254db538e
> change-id: 20261127-dma-fence-signalling-cae3def9aa97
>
> Best regards,
> --
> Daniel Almeida <daniel.almeida(a)collabora.com>
On 11/26/26 23:29, Dave Airlie wrote:
> On Thu, 26 Nov 2026 at 03:26, Christian König <christian.koenig(a)amd.com> wrote:
...
>>> The core then negotiates compatible types and adds converters between them,
>>
>> I have strong objections to that plan and would potentially NAK it.
>>
>> DMA-buf should *NEVER* accept CPU physical addresses nor provide conversion between different address types.
>>
>> Keep in mind that DMA-buf is solely a container for DMA addresses, nothing else and it should not provide any mapping functionality whatsoever.
>
> Is that because it's called dma-buf, if we just called it's shared-buf
> would that alleviate things? I just feel like there are use-cases
> where it containing CPU physical addresses might be what is optimal
> for a certain scenario, and re-creating a whole parallel
> infrastructure that ends up being 'I-can't-believe-it's-not-dma-buf'
> would end up being bad.
Well the problem is not so much transmitting CPU physical addresses between exporter and importer, but rather what the importer and DMA-buf layer should or should not be doing with them.
One of the few things which the DMA-buf design got right from the beginning is to avoid to much middle layering.
In other words the exporter is in control what is done with it's buffers and what isn't, so for example errors which come up while creating the mapping can be handled gracefully by the exporter. The map_attachment() callback either returns a fully populated mapping or an error code, nothing in between.
If we somehow prevent the CPU physical address from being casted to a struct page and/or used with vmf_insert_pfn/page/dma_map_page etc.. then there is not much of a problem with passing around physical addresses.
And if two drivers really need the system memory semantics with fine grained page faults, struct page refcounting etc... we still have memfd for that.
> If we need to shared object backings and have clearly typed and
> negotiated on what the backing contains then we should allow it.
> However I mostly agree on the mapping thing, I'd want drivers to
> negotiate and natively support whatever backings they can rather than
> having an intermediate trying to handle the conversions Not saying the
> code doing the conversions can't be a helper, bit it shouldn't ever be
> autoamtic or outside driver control.
Yes, exactly that's my concern as well. We need to avoid this middle layer trap.
Christian.
>
> Dave.
On 11/26/26 07:24, Alistair Popple wrote:
> On 2026-11-26 at 06:07 +1100, Jason Gunthorpe <jgg(a)ziepe.ca> wrote...
>> On Wed, Nov 25, 2026 at 07:17:51PM +0100, Christian König wrote:
>>> On 11/25/26 16:55, Jason Gunthorpe wrote:
>>>> On Wed, Nov 25, 2026 at 12:19:08PM +0100, Christian König wrote:
>
> [...]
>
> > > We cannot disconnect this from the DMA API, ie to use an iommu domain
>>>> or ualink, if exporting drivers are hardwiring calls into it.
>>>
>>> Well the plan is to not disconnect it at all. The exporting drivers
>>> just says to the underlying API here is my buffer please map that
>>> for this importer and that result is then given back to the importer
>>> through the DMA API again.
>>
>> DMA API will only do basic PCI DMA mappings into dma_addr_t.
>>
>> It will never handle iommu_domain, it will never handle ualink/etc.
>>
>> Give us a plan for those if you don't like any of these ideas.
>>
>>>> Fundamentally the need is for the driver to get the phys_addr list,
>>>> exactly like today, and then pass it to an op which formats that into
>>>> the right data structure for the importer. Be it dma_addr[],
>>>> iommu_domain, or some ualink_addr[] - or whatever.
>>>
>>> No, the phys_addr type should only ever been seen by the DMA API and
>>> not by DMA-buf.
>>
>> Again, I don't care how you label things or what "library" they live
>> in, I can't tell if we are nitpicking names or what.
>>
>> But there needs to be some kind of abstraction that takes in
>> phys_addr_t and makes sgt, dma_addr_t, or iommu_domain depending on
>> what the importer needs.
>>
>> Exporters must not open code that.
>>
>> Obviously DMABUF is involved here *somehow* since it is the only thing
>> that knows an importer even exists.
>>
>> Would you be happy if we made a 'dma mapping for dmabuf' library that
>> did all of this and DMABUF just passes its type opaquely and drivers
>> just call its tightly abstracted functions?
>>
>> That is the component Alistair is proposing, he just prefixed it with
>> dmabuf_* as the namespace because, lets be honest, only dmabuf needs
>> this weird looking thing.
>
> Right, I don't really mind where and what things are named. I'm fine with this
> being a separate library somewhere or building it all on top of opaque/private
> types. But I'm having trouble understanding where exactly the objections lie and
> what the path forward is.
Would it help if have a call on Teams or similar services to discuss the whole thing?
That might be easier and especially faster than by mail.
Regards,
Christian.
> So given no one outright rejected the plan I wrote earlier I'm going to go ahead
> and post the series implementing it (I was hoping to do that today, but it's not
> quite ready).
>
> Hopefully that will help by giving us actual code to ground the discussion on.
>
> - Alistair
>
>> Jason
On 11/25/26 08:18, Alistair Popple wrote:
> On 2026-11-25 at 06:46 +1100, Jason Gunthorpe <jgg(a)ziepe.ca> wrote...
>> On Tue, Nov 24, 2026 at 05:02:31PM +0100, Christian König wrote:
>>> The whole conversion from phys_vec to DMA addresses is something
>>> some API should provide based on the data the exporter/importer have
>>> negotiated (e.g. p2pdma_provider for example).
>>
>> Yeah, probably, and this is exactly why I think it will take some
>> time. Building a new something in lib/ like this is not a
>> straightforward task.
>>
>> But also, this:
>>
>>>> You need to negotiate the actual C data types too, especially if you
>>>> want to do that nasty idea where dmabuf populates an iommu domain.
>>>
>>> Yeah agree as well, but to be honest that task is negligible. We
>>> first need to figure out where to put the code.
>>
>> Means you need to get the phys_addr_t into the dma-buf code so it can
>> do this conversion.
>
> We all agreed that open-coding DMA mapping of buffers in every exporter is
> something we don't want to do. The disagreement now seems to be where the code
> converting between physical locations provided by an exporter to DMA addresses
> usable by an importer lives.
Yes, exactly that.
>> I'm still absolutely NAKity against allowing any DRM driver visibility
>> to an importer's iommu_domain if we do this hack.
>>
>> I don't see how to meet all these concerns without making dma-buf into
>> a "converter" framework...
>
> Right, something has to do the conversion and we've agreed that shouldn't be
> in either the exporters or the importers. The main objection seems to be having
> dma-buf do that, and instead having some DMA/library code to do it.
Right.
DMA-buf is the high level API/uAPI importers and exporters talk to interchange information how to access the buffer, handle locking, handle lifetime, handle a generic driver independent uAPI....
The low level API which talks for example to the IOMMU driver for creating the information how to access the buffer is simply not the task at hand for that framework. The stuff we already handle is more than complicated enough.
> If I drop the type/placement negotiation for now and focus on SGT removal I can
> simplify things a lot. Plan would be something like:
>
> 1) Introduce a new C data type (dma_phys_list?) for exporters to pass physical
> location info and context for mapping.
Something like this maybe?
struct {
u32 mapping_type;
u32 num_entries;
union {
dma_addr_t *dma_addr;
u64 *generic_64bit_addr;
...
};
}
....
> 2) Introduce two new dma_buf_ops - map_dma_phys_list() and unmap_dma_phys_list()
> - which are replacements for {un}map_dma_buf that converted exporters use to
> return a dma_phys_list instead of returning a sg_table.
>
> 3) Move dma_buf_phys_vec_to_sgt() somewhere other than drivers/dma-buf and
> rename it dma_phys_list_to_sgt().
>
> 4) Update dma_buf_{un}map_sgt() to call the new map_dma_phys_list() ops and
> dma_phys_list_to_sgt() to create an sg_table for unconverted importers
> importing from a converted exporter.
>
> 5) Add dma_buf_iommu_map() taking a IOMMU domain/iova/offset/size to be called
> from iommufd for mapping an attachment into an IOMMU domain.
I think a whole bunch of other drivers would be really happy to use that interface as well, yes.
>
> 6) Add a new C data type (dma_map_list?) containing DMA address regions
> an importer should program HW with and new dma-buf functions to replace
> dma_buf_{un}map_sgt().
>
> Names are really just placeholders, so easily changed if you don't like them.
> Let me know what you think, thanks.
That totally sounds like a plan to me.
Regards,
Christian.
>
> Regards,
>
> Alistair
On Tue, Nov 24, 2026 at 2:50 PM Christian Göttsche
<cgzones(a)googlemail.com> wrote:
>
> On Tue, 24 Nov 2026 at 14:05, Albert Esteve <aesteve(a)redhat.com> wrote:
> >
> > The security_current_memcg_charge_task() and
> > security_current_memcg_charge_cgroup() hooks allow security modules to
> > control which processes may charge or transfer memcg memory to another
> > process's cgroup, named by a pidfd, or to an arbitrary cgroup named by
> > a cgroupfd, both passed in charge_fd. Without a policy implementation,
> > either hook is a no-op and the restriction is not enforced.
> >
> > On SELinux-managed systems the only limit is CAP_SYS_RESOURCE. A
> > domain that holds both dma-heap access (today's only exporter that
> > opts-in) and that capability, can charge memory to any cgroup outside
> > its own subtree. Since the capability is all-or-nothing, SELinux
> > policy cannot express which targets such a domain may charge to.
> >
> > Implement both selinux_memcg_charge_task() and
> > selinux_memcg_charge_cgroup() using avc_has_perm() with a new cgroup
> > object class and a new charge_to permission. Both charge_fd flavours
> > ultimately charge a cgroup, so one permission covers them and the
> > target type keeps the distinction: for the pidfd path the target is
> > the referred task's SID, as there is no cgroup object to take a label
> > from; for the cgroupfd path it is the cgroup directory's inode SID,
> > which comes from the genfscon entry for cgroupfs unless the directory
> > is relabeled.
> >
> > The checks run when the charge target is resolved, before the
> > CAP_SYS_RESOURCE baseline is evaluated, and apply to every explicit
> > charge_fd target, including the caller's own subtree, so policy needs
> > a rule even for self-charging. Policy authors can then grant
> > cross-cgroup charging selectively, for example:
> >
> > allow allocator_app_t client_app_t:cgroup charge_to;
> > allow allocator_app_t cgroup_t:cgroup charge_to;
> >
> > Signed-off-by: Albert Esteve <aesteve(a)redhat.com>
> > ---
> > security/selinux/hooks.c | 19 +++++++++++++++++++
> > security/selinux/include/classmap.h | 1 +
> > 2 files changed, 20 insertions(+)
> >
> > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > index 6c4c64d6e904..76a714107ae4 100644
> > --- a/security/selinux/hooks.c
> > +++ b/security/selinux/hooks.c
> > @@ -2204,6 +2204,23 @@ static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
> > return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
> > }
> >
> > +static int selinux_memcg_charge_task(const struct cred *to)
> > +{
> > + const struct cred *cred = current_cred();
> > +
> > + return avc_has_perm(cred_sid(cred), cred_sid(to),
>
> Small nit:
> `cred_sid(current_cred())` could be simplified using `current_sid()`
Nice, thanks! I will apply that for the next version.
>
> > + SECCLASS_CGROUP, CGROUP__CHARGE_TO, NULL);
> > +}
> > +
> > +static int selinux_memcg_charge_cgroup(const struct file *file)
> > +{
> > + const struct cred *cred = current_cred();
> > + struct inode_security_struct *isec = inode_security(file_inode(file));
> > +
> > + return avc_has_perm(cred_sid(cred), isec->sid, SECCLASS_CGROUP,
> > + CGROUP__CHARGE_TO, NULL);
> > +}
> > +
> > static int selinux_quotactl(int cmds, int type, int id, const struct super_block *sb)
> > {
> > const struct cred *cred = current_cred();
> > @@ -7683,6 +7700,8 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {
> > LSM_HOOK_INIT(capget, selinux_capget),
> > LSM_HOOK_INIT(capset, selinux_capset),
> > LSM_HOOK_INIT(capable, selinux_capable),
> > + LSM_HOOK_INIT(memcg_charge_task, selinux_memcg_charge_task),
> > + LSM_HOOK_INIT(memcg_charge_cgroup, selinux_memcg_charge_cgroup),
> > LSM_HOOK_INIT(quotactl, selinux_quotactl),
> > LSM_HOOK_INIT(quota_on, selinux_quota_on),
> > LSM_HOOK_INIT(syslog, selinux_syslog),
> > diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h
> > index 90cb61b16425..1f1f2fc4d35d 100644
> > --- a/security/selinux/include/classmap.h
> > +++ b/security/selinux/include/classmap.h
> > @@ -181,6 +181,7 @@ const struct security_class_mapping secclass_map[] = {
> > { "user_namespace", { "create", NULL } },
> > { "memfd_file",
> > { COMMON_FILE_PERMS, "execute_no_trans", "entrypoint", NULL } },
> > + { "cgroup", { "charge_to", NULL } },
> > /* last one */ { NULL, {} }
> > };
> >
> >
> > --
> > 2.55.0
> >
> >
>
On Mon, Nov 23, 2026 at 09:28:07AM +0800, Mengmeng Zhao wrote:
> > This is stealing yet another FMODE_* bit for special-purpose stuff.
> > We're really really short of them. Did Christoph suggest this?
> Hi Christian, thanks for pointing out this. If I understand correctly,
> Christoph suggests a new file->f_mode to allow dma-buf check in RFC v1[1].
>
> Hi Christoph, do you have any comments or suggestions? I'am happy to adjust
> this in the next version if there is a better approach.
I didn't suggest a particular approach, but the FMODE_ bits seems very
fitting here as it sorts out the support issue very well.
On Mon, Nov 23, 2026 at 6:45 PM Qiliang Yuan <qiliang.yuan(a)linux.dev> wrote:
>
> udmabuf maps the ranges of a buffer that are backed by huge pages with
> PMDs, provided the mapping covers whole PMDs at an aligned address.
>
> The existing tests of huge page backed buffers only map a few pages, so
> they never get a PMD mapping or check what is read through one.
>
> Map a 64 MiB udmabuf backed by 2 MiB huge pages and read back what was
> written to the memfd, with a different pattern in each huge page: through
> a mapping of the whole buffer, which must be PMD aligned for a 64-bit
> task when THP support is built in, through a mapping at an unaligned
> offset, and from a forked child that inherits the mapping of the whole
> buffer.
>
> Signed-off-by: Qiliang Yuan <qiliang.yuan(a)linux.dev>
> ---
> tools/testing/selftests/drivers/dma-buf/udmabuf.c | 97 ++++++++++++++++++++++-
> 1 file changed, 96 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/drivers/dma-buf/udmabuf.c b/tools/testing/selftests/drivers/dma-buf/udmabuf.c
> index d78aec662586b..6eaef1b3aee01 100644
> --- a/tools/testing/selftests/drivers/dma-buf/udmabuf.c
> +++ b/tools/testing/selftests/drivers/dma-buf/udmabuf.c
> @@ -14,6 +14,7 @@
> #include <sys/ioctl.h>
> #include <sys/syscall.h>
> #include <sys/mman.h>
> +#include <sys/wait.h>
> #include <linux/memfd.h>
> #include <linux/udmabuf.h>
> #include "kselftest.h"
> @@ -130,6 +131,98 @@ static int compare_chunks(void *addr1, void *addr2, off64_t memfd_size)
> return ret;
> }
>
> +#define HPAGE_SIZE (2UL << 20)
This isn't true on 16k or 64k PAGE_SIZE arm64. Can we read
hpage_pmd_size for the PMD alignment and Hugepagesize: from
/proc/meminfo for the MFD_HUGETLB backing size?
> +#define HUGE_BUF_SIZE (32 * HPAGE_SIZE)
I think we only really need 8 of these for the sub-range test, and
this gets huge for 16k or 64k base pages.
> +/* Check that @len bytes mapped at @addr match @mem at @off */
> +static bool check_huge_mapping(char *addr, char *mem, off_t off, size_t len)
> +{
> + size_t i;
> +
> + for (i = 0; i < len; i += getpagesize()) {
> + if (addr[i] != mem[off + i])
> + return false;
> + }
> + return true;
> +}
> +
> +/* Check that @len bytes of @buf at @off read back what was written to @mem */
> +static bool check_huge_range(int buf, char *mem, off_t off, size_t len)
> +{
> + char *addr;
> + bool ok;
> +
> + addr = mmap(NULL, len, PROT_READ, MAP_SHARED, buf, off);
> + if (addr == MAP_FAILED)
> + return false;
> +
> + ok = check_huge_mapping(addr, mem, off, len);
> + munmap(addr, len);
> + return ok;
> +}
> +
> +/*
> + * Map a udmabuf backed by 2MB huge pages, which can be mapped with PMDs, and
> + * read it back from a PMD-aligned address, a sub-range at an unaligned offset
> + * and a forked child that inherits the mapping.
> + */
> +static void test_huge_mapping(int devfd)
> +{
> + struct udmabuf_create create = {};
> + int memfd, buf, status;
> + bool thp, aligned, ok;
> + char *mem, *addr;
> + pid_t pid;
> + size_t i;
> +
> + memfd = create_memfd_with_seals(HUGE_BUF_SIZE, true);
> + mem = mmap_fd(memfd, HUGE_BUF_SIZE);
> + /* Give each huge page its own pattern */
> + for (i = 0; i < HUGE_BUF_SIZE; i += getpagesize())
> + mem[i] = (char)(i / getpagesize() + i / HPAGE_SIZE);
> +
> + create.memfd = memfd;
> + create.size = HUGE_BUF_SIZE;
> + buf = ioctl(devfd, UDMABUF_CREATE, &create);
> + if (buf < 0) {
> + ksft_test_result_fail("%s: [FAIL,test-8]\n", TEST_PREFIX);
> + ksft_test_result_skip("%s: [SKIP,test-9]\n", TEST_PREFIX);
> + goto out;
> + }
> +
> + /* The address is only aligned for 64-bit tasks with THP support */
> + thp = sizeof(long) == 8 &&
> + !access("/sys/kernel/mm/transparent_hugepage", F_OK);
> + addr = mmap(NULL, HUGE_BUF_SIZE, PROT_READ, MAP_SHARED, buf, 0);
> + aligned = addr != MAP_FAILED &&
> + (!thp || !((unsigned long)addr % HPAGE_SIZE));
> + ok = aligned && check_huge_mapping(addr, mem, 0, HUGE_BUF_SIZE);
> + if (ok)
> + ksft_test_result_pass("%s: [PASS,test-8]\n", TEST_PREFIX);
> + else
> + ksft_test_result_fail("%s: [FAIL,test-8]\n", TEST_PREFIX);
> +
> + ok = check_huge_range(buf, mem, HPAGE_SIZE + getpagesize(),
> + 5 * HPAGE_SIZE + 3 * getpagesize());
We should probably also have a test with create.offset = getpagesize()
to exercise the misaligned PFN fallback.
Also a test that faults a PMD, then disables THP and then faults
elsewhere to cover the v1 bug.
On Mon, Nov 23, 2026 at 6:45 PM Qiliang Yuan <qiliang.yuan(a)linux.dev> wrote:
>
> QEMU and crosvm hand guest memory to the host renderer and display as
> udmabufs for virtio-gpu blob resources. The guest memory is a memfd that
> is often backed by 2 MiB huge pages, and the host process mmap()s the
> dma-buf to access it with the CPU.
>
> udmabuf maps every page with its own PTE: the first fault inserts a PFN
> for each 4 KiB page of the whole VMA. Mapping and touching 4 GiB this way
> takes over 200 ms and a million PTEs, although the memory behind it is
> 2048 huge pages.
>
> Add a ->huge_fault() handler that maps a PMD-sized range with one PMD
> when it is backed by an aligned run of contiguous PFNs, and falls back to
> PTEs otherwise. Have the pre-mapping on the first fault skip the ranges
> the handler will map, so that their PTEs don't take the place of the
> PMDs. Once THP is disabled for the VMA, the ranges already mapped with a
> PMD keep it, so pre-map only the PMD-sized range of the fault then.
>
> Mapping a udmabuf over a memfd filled with data and reading a byte of
> each 4 KiB page through the mapping, base and patched kernels alternating
> over 3 rounds of 5 runs (7 for shmem 4K), median of the per-round medians
> and range in parentheses:
>
> backing size before (ms) after (ms) change
> hugetlb 256M 13.3 (13.3-13.7) 1.7 (1.7-1.8) -87%
> hugetlb 1G 53.6 (53.0-53.7) 6.5 (4.2-6.6) -88%
> hugetlb 4G 211 (211-212) 23.6 (17.6-25.2) -89%
> hugetlb 8G 423 (422-424) 45.8 (45.8-47.2) -89%
> shmem THP 1G 52.8 (52.5-53.1) 6.3 (5.8-6.8) -88%
> shmem THP 4G 210 (207-214) 20.7 (19.5-21.1) -90%
> shmem 4K 1G 52.0 (51.6-52.7) 51.8 (51.5-52.9) 0%
> shmem 4K 4G 209 (209-211) 208 (208-209) 0%
>
> Signed-off-by: Qiliang Yuan <qiliang.yuan(a)linux.dev>
This commit message should explicitly say this patch depends on
Lorenzo's series in addition to mentioning it in the cover letter (for
merging / backporting). The mm/core hashes may change, so just by the
series title or patch subjects for now.
> ---
> drivers/dma-buf/udmabuf.c | 85 ++++++++++++++++++++++++++++++++++++++++++++---
> 1 file changed, 81 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
> index 018937435356e..9f53d33fbdd9a 100644
> --- a/drivers/dma-buf/udmabuf.c
> +++ b/drivers/dma-buf/udmabuf.c
> @@ -44,12 +44,63 @@ struct udmabuf {
> struct miscdevice *device;
> };
>
> +/*
> + * Return whether the PMD-sized range of @vma at @addr is backed by one aligned
> + * run of contiguous PFNs, which can then be mapped with a single PMD.
> + */
> +static bool udmabuf_pmd_mappable(struct vm_area_struct *vma, unsigned long addr,
> + unsigned long *pfn)
> +{
> + struct udmabuf *ubuf = vma->vm_private_data;
> + const pgoff_t nr = PMD_SIZE >> PAGE_SHIFT;
> + unsigned long first;
> + pgoff_t pgoff, i;
> +
> + if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_PMD_PFNMAP))
> + return false;
> +
> + if (!IS_ALIGNED(addr, PMD_SIZE) || addr < vma->vm_start ||
> + vma->vm_end - addr < PMD_SIZE)
> + return false;
> +
> + pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
> + if (pgoff >= ubuf->pagecount || ubuf->pagecount - pgoff < nr)
> + return false;
> +
> + first = page_to_pfn(ubuf->pages[pgoff]);
> + if (!IS_ALIGNED(first, nr))
> + return false;
> +
> + for (i = 1; i < nr; i++) {
> + if (page_to_pfn(ubuf->pages[pgoff + i]) != first + i)
> + return false;
> + }
> +
> + *pfn = first;
> + return true;
> +}
> +
> +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
> +static vm_fault_t udmabuf_vm_huge_fault(struct vm_fault *vmf,
> + unsigned int order)
> +{
> + unsigned long pfn;
> +
> + if (order != PMD_ORDER ||
> + !udmabuf_pmd_mappable(vmf->vma, vmf->address & PMD_MASK, &pfn))
> + return VM_FAULT_FALLBACK;
> +
> + return vmf_insert_pfn_pmd(vmf, pfn, vmf->flags & FAULT_FLAG_WRITE);
> +}
> +#endif
> +
> static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
> {
> struct vm_area_struct *vma = vmf->vma;
> struct udmabuf *ubuf = vma->vm_private_data;
> pgoff_t pgoff = vmf->pgoff;
> - unsigned long addr, pfn;
> + unsigned long addr, end, pfn;
> + bool pmd_ok;
> vm_fault_t ret;
>
> if (pgoff >= ubuf->pagecount)
> @@ -61,14 +112,37 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
> if (ret & VM_FAULT_ERROR)
> return ret;
>
> - /* pre fault */
> - pgoff = vma->vm_pgoff;
> + /*
> + * Pre-fault the rest of the VMA, except for the ranges that
> + * udmabuf_vm_huge_fault() maps with a PMD once they are accessed.
> + */
> + pmd_ok = thp_vma_allowable_order(vma, vma->vm_flags, TVA_PAGEFAULT,
> + PMD_ORDER);
> addr = vma->vm_start;
> + end = vma->vm_end;
>
> - for (; addr < vma->vm_end; pgoff++, addr += PAGE_SIZE) {
> + /*
> + * Ranges mapped with a PMD before THP was disabled for the VMA keep
> + * their PMD, so pre-fault only the PMD-sized range of the fault.
> + */
> + if (IS_ENABLED(CONFIG_ARCH_SUPPORTS_PMD_PFNMAP) && !pmd_ok) {
> + addr = max(addr, vmf->address & PMD_MASK);
> + end = min(end, (vmf->address & PMD_MASK) + PMD_SIZE);
> + }
This fixes the v1 problem, but it means that a task with THP disabled
takes a fault per PMD size instead of the whole VMA. Including buffers
that are entirely backed by 4k pages. That partially undoes the
f0bbcc258e81 ("udmabuf: pre-fault when first page fault") behavior.
That's probably fine (would be good to see results for shmem 4K under
PR_SET_THP_DISABLE), but I think the commit message should mention
this also applies to 4k buffers.
Also probably worth mentioning / commenting that
/sys/kernel/mm/transparent_hugepage/enabled = never (we use that)
doesn't get you here because the THP tuneables are bypassed for
VM_PFNMAP VMAs with huge_fault().
If we stick with this behavior when THP is disabled, can we just do it
unconditionally (using pmd_addr_end(vmf->address, vma->vm_end))? That
would eliminate pmd_ok, the thp_vma_allowable_order() call, the skip
branch below, and keep udmabuf_vm_fault() within the faulting PMD's
page table and avoid repeated full-VMA scans (e.g. post
MADV_DONTNEED), which seems like it might be worth the extra faults.
Worth checking the shmem 4k numbers to see if the extra per-PMD faults
are negligible though.
> + pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
> +
> + for (; addr < end; pgoff++, addr += PAGE_SIZE) {
> if (addr == vmf->address)
> continue;
>
> + if (pmd_ok && IS_ALIGNED(addr, PMD_SIZE) &&
> + (vmf->address & PMD_MASK) != addr &&
> + udmabuf_pmd_mappable(vma, addr, &pfn)) {
> + pgoff += (PMD_SIZE >> PAGE_SHIFT) - 1;
> + addr += PMD_SIZE - PAGE_SIZE;
> + continue;
> + }
> +
> if (WARN_ON(pgoff >= ubuf->pagecount))
> break;
>
> @@ -89,6 +163,9 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
>
> static const struct vm_operations_struct udmabuf_vm_ops = {
> .fault = udmabuf_vm_fault,
> +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
> + .huge_fault = udmabuf_vm_huge_fault,
> +#endif
> };
>
> static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
>
> --
> 2.43.0
>