On Fri, Nov 13, 2026 at 03:06:19PM +0800, Mengmeng Zhao wrote:
> From: Mengmeng Zhao <zhaomengmeng(a)kylinos.cn>
>
> Add a file mode bit for files that can be the target of dma-buf I/O and
> require it when a buffer is registered. Block device files set it when
> the backing disk supports dma-buf I/O, and filesystems can set it for
> inodes whose data can be reached without the page cache. The bit is
> coarse: a driver that installs ->init_dma_buf_io_ctx for every
> namespace, like nvme, still decides per device at registration.
>
> dma_buf_io_ctx_create() is the only caller of ->init_dma_buf_io_ctx, so
> the capability and the O_DIRECT requirement are checked there for every
> file type rather than in each helper; O_DIRECT belongs at registration
> because F_SETFL can change it afterwards. Add bdev_can_dmabuf_io() so
> the open-time and registration checks share one predicate.
>
> Signed-off-by: Mengmeng Zhao <zhaomengmeng(a)kylinos.cn>
> Signed-off-by: Li Wang <liwang(a)kylinos.cn>
> ---
> block/bdev.c | 8 ++++----
> block/fops.c | 2 +-
> drivers/dma-buf/dma-buf-io.c | 13 +++++++++----
> include/linux/blkdev.h | 7 ++++++-
> include/linux/fs.h | 8 +++++++-
> 5 files changed, 27 insertions(+), 11 deletions(-)
>
> diff --git a/block/bdev.c b/block/bdev.c
> index 88e3f552f5d4..4e7a7f7e1e8b 100644
> --- a/block/bdev.c
> +++ b/block/bdev.c
> @@ -61,14 +61,12 @@ struct block_device *file_bdev(struct file *bdev_file)
> }
> EXPORT_SYMBOL(file_bdev);
>
> -int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev,
> +int bdev_init_dma_buf_io_ctx(struct block_device *bdev,
> struct dma_buf_io_ctx *ctx)
> {
> - if (!(file->f_flags & O_DIRECT))
> - return -EINVAL;
> if (ctx->dmabuf->size > UINT_MAX)
> return -EINVAL;
> - if (!bdev->bd_disk->fops->init_dma_buf_io_ctx)
> + if (!bdev_can_dmabuf_io(bdev))
> return -EOPNOTSUPP;
> return bdev->bd_disk->fops->init_dma_buf_io_ctx(bdev, ctx);
> }
> @@ -1041,6 +1039,8 @@ int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder,
>
> bdev_file->f_flags |= O_LARGEFILE;
> bdev_file->f_mode |= FMODE_CAN_ODIRECT;
> + if (bdev_can_dmabuf_io(bdev))
> + bdev_file->f_mode |= FMODE_CAN_DMABUF_IO;
> if (bdev_nowait(bdev))
> bdev_file->f_mode |= FMODE_NOWAIT;
> if (mode & BLK_OPEN_RESTRICT_WRITES)
> diff --git a/block/fops.c b/block/fops.c
> index 86bf1f298b79..7e5702e047a2 100644
> --- a/block/fops.c
> +++ b/block/fops.c
> @@ -1008,7 +1008,7 @@ static int blkdev_mmap_prepare(struct vm_area_desc *desc)
> static int blkdev_init_dma_buf_io_ctx(struct file *file,
> struct dma_buf_io_ctx *ctx)
> {
> - return bdev_init_dma_buf_io_ctx(file, file_bdev(file), ctx);
> + return bdev_init_dma_buf_io_ctx(file_bdev(file), ctx);
> }
>
> const struct file_operations def_blk_fops = {
> diff --git a/drivers/dma-buf/dma-buf-io.c b/drivers/dma-buf/dma-buf-io.c
> index 8440151de860..9575b7d65ec5 100644
> --- a/drivers/dma-buf/dma-buf-io.c
> +++ b/drivers/dma-buf/dma-buf-io.c
> @@ -152,9 +152,11 @@ static void dma_buf_io_ctx_shutdown(struct dma_buf_io_ctx *ctx)
> * @out_ctx: [out] resulting ctx
> *
> * Create a new I/O context from the given DMA buffer for the given file.
> - * It only works with files implementing ->init_dma_buf_io_ctx and fails
> - * with -EOPNOTSUPP otherwise. Once created, the user can create a map
> - * and use it to issue I/O, such as read/write requests, to @file.
> + * It only works with files implementing ->init_dma_buf_io_ctx,
> + * advertising FMODE_CAN_DMABUF_IO and currently opened for direct I/O,
> + * and fails with -EOPNOTSUPP or -EINVAL otherwise. Once created, the
> + * user can create a map and use it to issue I/O, such as read/write
> + * requests, to @file.
> */
> int dma_buf_io_ctx_create(struct file *file,
> struct dma_buf *dmabuf,
> @@ -164,8 +166,11 @@ int dma_buf_io_ctx_create(struct file *file,
> struct dma_buf_io_ctx *ctx;
> int ret;
>
> - if (!file->f_op->init_dma_buf_io_ctx)
> + if (!file->f_op->init_dma_buf_io_ctx ||
> + !(file->f_mode & FMODE_CAN_DMABUF_IO))
> return -EOPNOTSUPP;
> + if (!(file->f_flags & O_DIRECT))
> + return -EINVAL;
>
> ctx = kzalloc_obj(*ctx);
> if (!ctx)
> diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
> index b98f43478cc0..0cfb6fb6b4e2 100644
> --- a/include/linux/blkdev.h
> +++ b/include/linux/blkdev.h
> @@ -1610,9 +1610,14 @@ struct block_device_operations {
> int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
> };
>
> -int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev,
> +int bdev_init_dma_buf_io_ctx(struct block_device *bdev,
> struct dma_buf_io_ctx *ctx);
>
> +static inline bool bdev_can_dmabuf_io(struct block_device *bdev)
> +{
> + return !!bdev->bd_disk->fops->init_dma_buf_io_ctx;
> +}
> +
> #ifdef CONFIG_COMPAT
> extern int blkdev_compat_ptr_ioctl(struct block_device *, blk_mode_t,
> unsigned int, unsigned long);
> diff --git a/include/linux/fs.h b/include/linux/fs.h
> index 99caef43ff3c..f5524fc770d4 100644
> --- a/include/linux/fs.h
> +++ b/include/linux/fs.h
> @@ -121,7 +121,13 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
> /* File supports atomic writes */
> #define FMODE_CAN_ATOMIC_WRITE ((__force fmode_t)(1 << 7))
>
> -/* FMODE_* bit 8 */
> +/*
> + * File can be the target of dma-buf I/O. Set it for files whose data can be
> + * reached with direct I/O without the page cache: a dma-buf iterator has no
> + * pages to copy, so a buffered path cannot serve it. The bit is coarse; both
> + * the device behind it and O_DIRECT are checked when a buffer is registered.
> + */
> +#define FMODE_CAN_DMABUF_IO ((__force fmode_t)(1 << 8))
This is stealing yet another FMODE_* bit for special-purpose stuff.
We're really really short of them. Did Christoph suggest this?
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.
> 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?
>
> 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?
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>
>
On Tue, Nov 17, 2026 at 11:54 PM Paul Moore <paul(a)paul-moore.com> wrote:
>
> On Nov 17, 2026 Albert Esteve <aesteve(a)redhat.com> wrote:
> >
> > Both DMA_HEAP_IOCTL_ALLOC and the new DMA_BUF_IOCTL_XFER_CHARGE ioctl
> > accept a charge_fd field that, when set, causes the allocation to be
> > charged to an arbitrary process's cgroup rather than the caller's.
> >
> > Charging is currently limited to the caller's own memcg subtree unless
> > the caller holds CAP_SYS_RESOURCE. But the capability is all-or-nothing.
> > A process holding it, along with a dma-heap device node or a dma-buf
> > fd can charge unlimited memory to any other process's cgroup, potentially
> > exhausting that cgroup's limit and triggering OOM kills independent of
> > the victim's own activity or privileges.
> >
> > Add security_memcg_charge_task(), called in get_obj_cgroup_from_fd()
> > when charge_fd refers to another process. The hook receives
> > the credentials of the allocating process (from) and the credentials
> > of the process whose cgroup will be charged (to), giving security
> > modules a controlled enforcement point for cross-cgroup objcg
> > attribution policy.
> >
> > When CONFIG_SECURITY is not set the hook compiles to an inline
> > returning 0, adding no overhead to the fast path.
> >
> > Signed-off-by: Albert Esteve <aesteve(a)redhat.com>
> > ---
> > include/linux/lsm_hook_defs.h | 1 +
> > include/linux/security.h | 7 +++++++
> > mm/memcontrol.c | 11 +++++++++++
> > security/security.c | 15 +++++++++++++++
> > 4 files changed, 34 insertions(+)
>
> ...
>
> > diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> > index 03cd90d5e623..2e6fcbe14d43 100644
> > --- a/mm/memcontrol.c
> > +++ b/mm/memcontrol.c
> > @@ -60,6 +60,7 @@
> > #include <linux/lockdep.h>
> > #include <linux/resume_user_mode.h>
> > #include <linux/psi.h>
> > +#include <linux/security.h>
> > #include <linux/seq_buf.h>
> > #include <linux/sched/isolation.h>
> > #include <linux/kmemleak.h>
> > @@ -3198,10 +3199,12 @@ static bool mem_cgroup_may_charge(struct mem_cgroup *memcg)
> > struct obj_cgroup *get_obj_cgroup_from_fd(int fd)
> > {
> > struct cgroup_subsys_state *css;
> > + const struct cred *tcred;
> > struct obj_cgroup *objcg;
> > struct mem_cgroup *memcg;
> > struct task_struct *task;
> > unsigned int pidfd_flags;
> > + int ret;
> >
> > if (mem_cgroup_disabled())
> > return ERR_PTR(-EOPNOTSUPP);
> > @@ -3226,6 +3229,14 @@ struct obj_cgroup *get_obj_cgroup_from_fd(int fd)
> > if (IS_ERR(task))
> > return ERR_CAST(task);
> >
> > + tcred = get_task_cred(task);
> > + ret = security_memcg_charge_task(current_cred(), tcred);
>
> Do you ever envision this "from" cred being something other than
> current_cred()?
Good point. No, I do not.
> If the answer is "no", we might want to consider changing
> the LSM hook slightly to help prevent misuse in the future:
>
> int security_current_memcg_charge_task(const struct cred *tcred);
Agreed. I'll apply the same to the cgroupfd variant for symmetry (its
"from" is also always current_cred()).
BR,
Albert.
>
> > + put_cred(tcred);
> > + if (ret) {
> > + put_task_struct(task);
> > + return ERR_PTR(ret);
> > + }
> > +
> > rcu_read_lock();
> > css = task_css(task, memory_cgrp_id);
> > put_task_struct(task);
>
> --
> paul-moore.com
>
On Tue, Nov 17, 2026 at 03:54:31PM +0000, Ying Xiao wrote:
> system_heap_allocate() allocates pages via order_flags[] with __GFP_ZERO
> while the direct map still has the memory encryption bit set. For the
> system_cc_shared heap, set_memory_decrypted() subsequently clears the
> encryption bit without re-zeroing, leaving ciphertext in DRAM on
> confidential computing (CoCo) guests instead of plaintext zeros.
>
> Skip __GFP_ZERO during alloc_pages() for cc_shared allocations, and zero
> the pages once in system_heap_set_page_decrypted() after
> set_memory_decrypted() succeeds.
>
> Fixes: 78b30c50a7ac ("dma-buf: heaps: system: add system_cc_shared heap for explicitly shared memory")
> Reviewed-by: Jacob Moroni <jmoroni(a)google.com>
> Signed-off-by: Ying Xiao <eagleshaw(a)google.com>
> ---
> drivers/dma-buf/heaps/system_heap.c | 24 ++++++++++++++++++++----
> 1 file changed, 20 insertions(+), 4 deletions(-)
This seems fine, but Aneesh is fixing this in a more complete way with
this other series.
Reviewed-by: Jason Gunthorpe <jgg(a)nvidia.com>
Jason
On Tue, Nov 17, 2026 at 11:54 PM Paul Moore <paul(a)paul-moore.com> wrote:
>
> On Nov 17, 2026 Albert Esteve <aesteve(a)redhat.com> wrote:
> >
> > Shared-memory allocator services (e.g., Android's gralloc)
> > allocate dma-bufs on behalf of client processes in different
> > cgroups. Today those allocations are charged to the allocator's
> > own cgroup, leaving client cgroups with no visibility into their
> > actual memory consumption and defeating resource limits.
> >
> > This series adds memory cgroup accounting for dma-buf allocations,
> > building on T.J. Mercier's earlier series [1] and incorporating
> > feedback from previous versions and LPC discussions [2].
> >
> > Patch 1 introduces the MEMCG_DMABUF stat and the
> > obj_cgroup_reserve_dmabuf()/obj_cgroup_cancel_dmabuf() and
> > mem_cgroup_commit_dmabuf()/mem_cgroup_uncharge_dmabuf() helpers
> > in mm/memcontrol.c
> >
> > Patch 2 wires up the charging policy: dma_buf_export() stores
> > exp_info.objcg and updates the stat, a new charge_fd field in
> > DMA_HEAP_IOCTL_ALLOC accepts a pidfd or cgroupfd to identify
> > the target cgroup, and __GFP_ACCOUNT is removed from system-heap
> > allocations to avoid double counting.
> >
> > Patch 3 adds DMA_BUF_IOCTL_XFER_CHARGE, a general ioctl on any
> > dma-buf fd to transfer charges post-allocation. This covers the
> > double-hop allocator case (SurfaceFlinger -> gralloc -> kernel)
> > and works for buffers from any exporter that opts in. Charging
> > is gated by dmabuf->objcg being set at export time, ensuring that
> > transfer is only applied for buffers backed by system memory where
> > size reflects actual bytes. If the receiving cgroup lacks capacity
> > for the charge, the ioctl returns -ENOMEM and the original charge
> > is preserved.
> >
> > Patches 4-7 add access control for cross-cgroup charging. Letting
> > userspace decide who to charge needs to be a privileged operation
> > because it has material impact on OOM decision and resource limits.
> > Charging outside the caller's own memcg subtree requires
> > CAP_SYS_RESOURCE. On top of that, LSM hooks gate the two charge_fd
> > flavours: security_memcg_charge_task() gates the pidfd path (target
> > identified by task credentials) and security_memcg_charge_cgroup()
> > gates the cgroupfd path (target identified by cgroup directory label),
> > with SELinux implementations using process2:charge_to and
> > cgroup:charge_to permissions.
>
> I'm probably misunderstanding the description here, but in both cases
> (the pidfd or cgroupfd case), ultimately the cgroup is charged, yes? In
> the cgroupfd case it is more direct, but in the pidfd case it is still
> the cgroup associated with the process identified by pidfd, yes?
Your understanding is correct here.
>
> If that's the case - and it very well may not be (!) - it probably makes
> more sense to combine the two SELinux access controls into one. Yes, the
> LSM hooks and callbacks would likely need to be different due to the
> mechanics of it, but the permission check "cgroup/charge" could be the
> same.
Combining SELinux access controls makes sense to me. I kept them split
because I wanted to preserve granularity between the two flavours, and
process2 seemed like a natural fit for the pidfd path, but I guess it
can still be achieved with a single permission through the target
types.
I'll rework for v6. So, in the pidfd case the check target would
remain the process's domain:
allow allocator_t client_t:cgroup charge_to;
where allocator_t is the domain requesting the charge (i.e., the
central allocator), and client_t is the domain of the process the
pidfd refers to. The groupfd path would check the cgroup directory
label like so:
allow allocator_t cgroup_t:cgroup charge_to;
Would that be ok?
>
> > Patch 8 adds selftests. The tests and vmtest script were mostly
> > written by an LLM, closely following the style of the surrounding
> > tests and the virtme-ng setup. All code has been reviewed and tested.
>
> I think we need to have selinux-testsuite tests to go along with
> this, especially as this would be the first patchset to add cgroup
> controls. Historically I've pushed back on piecemeal cgroup additions,
> pushing for some basic design of how to manage access controls for
> resource management first, but since evidently no one wants to do that,
> we really do need tests.
>
> https://github.com/SELinuxProject/selinux-testsuite
Agreed! I was already playing with it between the last version and
this one. I have some tests drafted, more or less. I was unfamiliar
with the testsuite, so instead of offloading the work to an LLM, I
tried to follow along and proceeded too slowly.
I will link a PR when sending the next version.
>
> > Known limitations:
> >
> > - Single-owner model: a dma-buf carries one memcg charge regardless
> > of how many processes share it.
>
> Once again, I'm sure I'm misunderstanding things, but are you saying
> that multiple cgroups can end up sharing the same dma-buf despite
> only one of the cgroups taking the accounting hit?
Yes, you are correct. A dma-buf (its fd) can be shared by multiple
processes from multiple cgroups, but only one holds the charge. With
this series it can be transferred, but never shared.
Sharing could be a follow-up series. Perhaps splitting the charge
equally? But sharing is a big problem on its own, so I preferred to
keep it out of this series.
>
> Hopefully the charged cgroup is deterministic?
Yes, the charged cgroup is deterministic: it is set at allocation, and
only moves via an explicit ioctl.
>
> > - Only memcg accounting. CMA heaps would additionally need dmem
> > controller integration.
>
> Do you have a rough idea of how that would work? I ask only because I'm
> interested in seeing if that would require an additional LSM hook or if
> there is some way we could leverage the same hook(s) used here.
Rough idea, yes. Today, no dma-buf allocation charges dmem at all, and
of course no target charging either. The first goal for CMA is simply
getting those heaps accounted. In that regard, Eric Chanudet has a
series making CMA allocations memcg-accountable with per-area counter
[1], and is working on extending that approach to dmem. It's a bit on
hold waiting for the feedback on this series.
But afaik the cross-cgroup charging and central allocator problem is
mainly a system-memory one. Whether dmem ever needs the same
cross-cgroup attribution (e.g., DRM central allocator wanting to
charge clients' dmem budgets) is not clear to me.
Nonetheless, if it does, the internals should be mostly the same;
everything, including the permission checks is controller-agnostic.
Only the controller performing the charge changes. I expect no
additional hook is required.
[1] https://lore.kernel.org/all/20260821-cma-memcg-regions-v1-0-d21b165b8440@re…
>
> > - If a transfer of an uncharged buffer fails, the buffer remains
> > charged to the original cgroup. The allocator (userspace) is
> > expected to handle this (retry, free, ...).
> > - The LSM hooks are intentionally narrow. Broader cgroup access controls
> > (e.g. for the memfd/kexec handover case [2]) could be a separate effort
> > reusing the same pattern.
> > - Only system heap sets exp_info.objcg today. Other exporters
> > (DRM, V4L2, etc.) can opt in by setting it, but are not
> > covered by this series.
> > - Related to the above: lazy backing-store allocation is not supported.
> > This can be extended later, e.g., by adding a flag on the dma-buf
> > struct for explicitly marking when memory is committed.
>
> Similar to the CMA case, any rough ideas on what this would look like?
Yes, again I have a rough idea. We already separate reserve and commit
operations, so the idea is to delay when a charge is committed. For
that we can use a flag in the dma-buf struct to mark when the memory
is committed. The limit is still enforced up front (so a lazy exporter
cannot overcommit), and the stat reflects the actual allocation.
The permission model is also unchanged for this case. The flag only
affects when the stat will reflect the charge.
Thanks!
Albert.
>
> > Note on TTM/GPU accounting [3]: that effort charges system memory
> > allocated for GPU buffers per page, with its own stats (gpu_active
> > and gpu_reclaim). This series charges at the dma-buf level, for
> > memory with no memory.stat category of its own; exporters whose
> > memory is already charged by their allocator must not opt in, so
> > the two do not overlap.
> >
> > [1] https://lore.kernel.org/cgroups/20230109213809.418135-1-tjmercier@google.co…
> > [2] https://lpc.events/event/20/contributions/2416/
> > [3] https://lore.kernel.org/all/20260706052330.1110909-1-airlied@gmail.com/
> >
> > Signed-off-by: Albert Esteve <aesteve(a)redhat.com>
>
> --
> paul-moore.com
>
Hi all,
The goal of this series is to enable userspace driver designs that use
VFIO to export DMABUFs representing subsets of PCI device BARs, and
"vend" those buffers from a primary process to other subordinate
processes by fd. This is achieved by allowing the processes to mmap()
the DMABUFs; their access to the device is isolated to the exported
ranges. The primary userspace driver process can forcibly revoke
access to previously-shared buffers upon cleanup (without requiring
cooperation from the subordinate processes). This is an improvement
on sharing the VFIO device fd to subordinate processes, which would
allow unfettered access. See the RFCs for background.
The existing VFIO PCI BAR mmap() becomes backed by a DMABUF too,
keeping common vm_ops and fault handler for VMAs from the VFIO device
and explicitly-exported DMABUFs. This will help future iommufd
emulation of VFIO Type1 peer-to-peer, making it easier to get a DMABUF
for a VFIO BAR as a DMA target.
Below are per-patch notes & background info, and at the bottom are
several related questions that reviewers may like to consider (worth
at least skipping to #1, a possible bug).
Notes on patches
================
mmap() conversion to use DMABUF underneath has been done for vfio-pci,
but not sub-drivers:
nvgrace-gpu's mmap() override path is unchanged; I kept this out of
scope for now not least because I don't have a thorough test setup for
this system. I would prefer to help the nvgrace-gpu maintainers
enable BAR mmap() DMABUFs themselves.
It's also worth noting that one reason for nvgrace-gpu's special BAR
handling is to map RESMEM with a WC memory type. This is unchanged
for existing mmap() but WC is not currently supported for mapping
DMABUFs exported for that BAR; all DMABUF VMAs currently have the same
memory type and control of UC/WC for a mapping is future work.
vfio/pci: Remove DMABUF export dependency on vdev->memory_lock
In v5 of this series we discover that doing an export from the VFIO
mmap() path (fundamental!) adds a dependency between mmap_lock and
vdev->memory_lock(W) because export was using memory_lock(W) to
protect the vdev->dmabufs list and state within, and a deadlock
scenario leapt out to bite:
https://lore.kernel.org/kvm/9a615f22-c0d4-46ae-9654-db11e94e5fec@ozlabs.org/
The suggestion was to add a dedicated mutex/rwsem specifically for
the DMABUFs/list, which is cleaner than overloading memory_lock(W).
But whilst export could now downgrade to holding memory_lock(R) to
test __vfio_pci_memory_enabled(), a very similar deadlock can still
arise due to a memory_lock(W) elsewhere depending on a prior
memory_lock(R) to be released, and attempting to take
memory_lock(R) will queue behind the (W) for fairness (effectively
an R->R dependency). I'd overlooked that rwsem cannot guarantee
multiple readers.
To be able to export while holding mmap_lock, export cannot hold
memory_lock at all. Instead of testing __vfio_pci_memory_enabled()
(which tracks PCI_COMMAND.MSE and PM state), this patch tracks
device-global DMABUF revocation state in a new vdev->bars_revoked
flag updated by vfio_pci_dma_buf_move(). If an mmap() is somehow
performed during a period when DMABUFs are all revoked, then the
DMABUF is created revoked. Move() already bookends reset, PM
transitions etc., so subsequent revocation state changes work
as-is. This flag is also protected by the dmabuf_lock and thus
memory_lock is not required to export.
vfio/pci: Un-revoke DMABUFs in LOW_POWER_ENTRY_WITH_WAKEUP resume
On LOW_POWER entry, DMABUFs have a move(revoke=true), but the
runtime resume path didn't un-revoke. This adds a corresponding
move(revoke=false), which will later turn into
vfio_pci_unrevoke_bars().
NOTE: the unrevoke is reordered _before_ the eventfd_signal() in
vfio_pci_core_runtime_resume() to remove a window in which a waking
waiter could have observed the DMABUF state as still revoked (or
pm_runtime_engaged = true). (The UAPI docs state the event means
the resume's complete, so observing otherwise seemed unintended.)
Also, when DMABUFs are later mmap()ed, a waiter waking and taking a
fault could have seen the unrevoked state and SIGBUS just before
taking the memory_lock. (If the handler gets to acquire the lock,
though, the resume sequence is complete, and the handler observes
pm_runtime_engaged = false.)
This fix is in this series because the issue will impact CPU access
to the VMA as well (once they use DMABUFs), and so it's a strict
dependency of later commits.
dma-buf: Provide dma_buf_set_name()
Makes dma_buf_set_name() available for use (by helper patch),
taking a kernel-allocated string. The pre-existing local helper
becomes a wrapper copying a __user string for the set-name ioctl.
vfio/pci: Add a helper to look up PFNs for DMABUFs
Adds a DMABUF VMA fault handler helper to determine arbitrary-sized
PFNs from ranges in DMABUF.
vfio/pci: Add a helper to create a DMABUF for a BAR-map VMA
Refactors DMABUF export for use by the existing export feature, and
adds a helper that creates a DMABUF corresponding to a VFIO BAR
mmap() request.
There was a request for decent debug naming in /proc/<pid>/maps
etc. comparable to the existing VFIO names: since the VMAs are
DMABUFs, they have a "dmabuf:" prefix and can't be 100% identical
to before. This is a user-visible change, but this patch at least
now gives us extra info on the BDF & BAR being mmap()ed. The name
is installed with the new dma_buf_set_name() above.
vfio/pci: Convert BAR mmap() to use a DMABUF
The vfio-pci core mmap() creates a DMABUF with the helper above,
and the vm_ops fault handler uses the other helper to resolve the
fault. Because this depends on DMABUF structs/code,
CONFIG_VFIO_PCI_CORE needs to depend on CONFIG_DMA_SHARED_BUFFER.
The CONFIG_VFIO_PCI_DMABUF still conditionally enables the export
support code.
NOTE: The user mmap()s a device fd, but the resulting VMA's vm_file
becomes that of the DMABUF. The DMABUF takes ownership of the
device file and put()s it on release, which maintains the existing
behaviour of a VMA keeping the VFIO device open.
BAR zapping then happens via the existing vfio_pci_dma_buf_move()
path, which now needs to unmap PTEs in the DMABUF's address_space.
NOTE: As local LLM reviews did, Sashiko might falsely worry about
the DMABUF fd being obtained through /proc/pid/map_files and
remapped, but the file is an anon inode and doesn't support the
open op (-ENXIO) so AFAICT this is currently impossible.
NOTE: A side-effect of this is that is_mergeable_vma() will be
false between adjacent mappings of VFIO BARs; merging would require
rebuilding a larger DMABUF representing the union, not just
plugging the VMAs together.
The DMABUF backing the BAR is an implicit/internal export, even
when the CONFIG_VFIO_PCI_DMABUF feature is not included because
CONFIG_PCI_P2PDMA is not available. Without P2PDMA, it's
acceptable for the DMABUF to not have a P2PDMA provider. In this
configuration, the VFIO DMABUF .attach prohibits any import, which
avoids getting as far as a WARN in dma_buf_map_attachment(), which
would fail without P2PDMA anyway.
vfio/pci: Clean up BAR zap and revocation
In general (see NOTE!) the vfio_pci_zap_bars() is now obsolete,
since it unmaps PTEs in the VFIO device address_space which is now
unused. This consolidates all calls (e.g. around reset) with the
neighbouring vfio_pci_dma_buf_move()s into new functions, to
revoke/unrevoke (making the steps clearer).
NOTE: Because drivers can use their own vm_ops and override .mmap,
the core must conservatively assume an overridden .mmap might still
add PTEs to the VFIO device address_space and therefore still does
the zap. A new flag, zap_bars_on_revoke, enables the zap when
.mmap is overridden. A driver that does not need the zap can clear
this to opt-out, e.g. if the driver calls down to the common mmap
(and so uses DMABUFs). hisi-acc-vfio-pci does just this, and thus
sets the opt-out flag.
vfio/pci: Support mmap() of a VFIO DMABUF
Adds mmap() for a DMABUF fd exported from vfio-pci.
It was a goal to keep the VFIO device fd lifetime behaviour
unchanged with respect to the DMABUFs. An application can close
all device fds, and this will revoke/clean up all DMABUFs; then, no
mappings or other access can be performed. When enabling mmap() of
the DMABUFs, this means access through the VMA is also revoked.
This complicates the fault handler because whilst the DMABUF
exists, it has no guarantee that the corresponding VFIO device is
still alive. Adds synchronisation ensuring the vdev is available
before the locks in vdev are touched; this holds the device
registration so that even if the buffer has been cleaned up, vdev
hasn't been freed and so the locks can be safely taken.
vfio/pci: Revoke a DMABUF on request from userspace
This is mostly a rename of `revoked` to an enum, `status`, and
adding a third state for a buffer: usable, revoked temporary,
revoked permanent. A new VFIO feature is added,
VFIO_DEVICE_FEATURE_DMA_BUF_REVOKE, which takes a DMABUF (exported
from the same device) and permanently revokes it. Thus a userspace
driver can guarantee any downstream consumers of a shared fd are
prevented from accessing a BAR range, and that range can be reused.
NOTE: This might block userspace, waiting on importers to detach.
The code doing revocation in vfio_pci_dma_buf_move() is moved, to a
common function used by ..._move() and this new feature.
Testing
=======
(The [RFC ONLY] userspace test program, which drives a QEMU
bochs-display function, can be found in the GitHub branch below. It
at least illustrates how the export, map, revoke, and close semantics
interoperate. WIP on a follow-up with a proper vfio-selftests style
test based on this -- this won't be part of this series.)
This code has been tested in mapping DMABUFs of single/multiple ranges
from multiple BARs, aliasing mmap()s, aliasing ranges across DMABUFs,
vm_pgoff > 0, revocation, shutdown/cleanup scenarios, and hugepage
mappings. No regressions observed on the VFIO selftests, or on our
internal vfio-pci applications. VFIO on i386 has been build-tested.
Thanks to Alex Mastro for building a (WIP) testcase for the
prior mmap_lock->memory_lock issue.
END
===
This is based on v7.3-rc6
These commits are on GitHub for easier browsing, along with
"[RFC ONLY] selftests: vfio: Add standalone vfio_dmabuf_mmap_test":
https://github.com/evansm7/linux/compare/v7.3-rc6...dev/mev/vfio-dmabuf-mma…
Also please see...
https://lore.kernel.org/all/f8efaabd-c06e-4f04-8cfa-489c148e37ba@ozlabs.org/
("[PATCH] dma-buf: Annul dmabuf->file on file release")
...which fixes a stale file pointer issue that could affect VFIO's
DMABUF export. Although the issue exists prior to this series, the
addition of another dmabuf->file dereference adds additional
unpleasant failure modes (for example, unmap_mapping_range() the wrong
file, or good old random data dereference, etc.)
Thanks for reading,
Matt
================================================================================
Changelog:
v8:
- Rebased, on v7.3-rc6
- "dma-buf: Provide dma_buf_set_name()": Comment and "DOC: locking
convention" updates. dma_buf_set_name() is is listed in the "do
not hold resv" category
v7: https://lore.kernel.org/all/20260924152159.49702-1-matt@ozlabs.org/
v6: https://lore.kernel.org/all/20260911214200.33793-1-matt@ozlabs.org/
v5: https://lore.kernel.org/all/20260715174737.15287-1-matt@ozlabs.org/
v4: https://lore.kernel.org/all/20260701171245.90111-1-matt@ozlabs.org/
v3: https://lore.kernel.org/all/20260610154327.37758-1-matt@ozlabs.org/
v2: https://lore.kernel.org/all/20260527102319.100128-1-mattev@meta.com/
v1: https://lore.kernel.org/kvm/20260416131815.2729131-1-mattev@meta.com/
RFCv2: https://lore.kernel.org/kvm/20260312184613.3710705-1-mattev@meta.com/
RFCv1: https://lore.kernel.org/all/20260226202211.929005-1-mattev@meta.com/
Tech topic: https://lore.kernel.org/linux-iommu/20250918214425.2677057-1-amastro@fb.com/
Matt Evans (9):
vfio/pci: Remove DMABUF export dependency on vdev->memory_lock
vfio/pci: Un-revoke DMABUFs in LOW_POWER_ENTRY_WITH_WAKEUP resume
dma-buf: Provide dma_buf_set_name()
vfio/pci: Add a helper to look up PFNs for DMABUFs
vfio/pci: Add a helper to create a DMABUF for a BAR-map VMA
vfio/pci: Convert BAR mmap() to use a DMABUF
vfio/pci: Clean up BAR zap and revocation
vfio/pci: Support mmap() of a VFIO DMABUF
vfio/pci: Revoke a DMABUF on request from userspace
drivers/dma-buf/dma-buf.c | 84 ++-
drivers/vfio/pci/Kconfig | 4 +-
drivers/vfio/pci/Makefile | 3 +-
.../vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 14 +-
drivers/vfio/pci/vfio_pci_config.c | 30 +-
drivers/vfio/pci/vfio_pci_core.c | 222 +++++--
drivers/vfio/pci/vfio_pci_dmabuf.c | 609 +++++++++++++++---
drivers/vfio/pci/vfio_pci_priv.h | 54 +-
include/linux/dma-buf.h | 16 +-
include/linux/vfio_pci_core.h | 3 +
include/uapi/linux/vfio.h | 24 +
11 files changed, 870 insertions(+), 193 deletions(-)
--
2.47.3
Fuad Tabba <fuad.tabba(a)linux.dev> writes:
> Hi Jason, Aneesh,
>
> On Mon, 16 Nov 2026 14:02:59 +0000, Jason Gunthorpe <jgg(a)ziepe.ca> wrote:
> [...]
>> Just like you are wrapping set_memory_decrypted() in an allocator API
>> the user that wants to have the no-change sharing needs its own
>> special API too.
>
> That works. A separate API can cover no-change sharing when a user
> needs it, so I'm fine with set_memory_decrypted() zeroing.
>
> Aneesh, the pKVM hunk does need cache maintenance after the memset(),
> which I have in the Cc: stable patch I posted earlier today [1].
>
I will add that, along with the relevant comments.
Thanks,
-aneesh