AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/09/05 20:47 flow patch-triage
1m Args:
null

Results:
{
  "EnableConfigs": [
    "CMA",
    "DMA_CMA",
    "DMABUF_HEAPS",
    "DMABUF_HEAPS_CMA",
    "CMA_DEBUGFS",
    "DEBUG_FS"
  ],
  "FocusSymbols": [
    "cma_range_alloc",
    "__cma_alloc_frozen",
    "cma_alloc_at",
    "cma_alloc_at_frozen",
    "dma_heap_debug_show"
  ],
  "KMSANReasoning": "The patch series introduces support for NVIDIA Tegra Video Protection Region (VPR) as a DMA-BUF heap, adds debugfs support to dma-heap, and adds fixed-offset CMA allocation support (`cma_alloc_at`).\n\nAll newly introduced kernel heap data structures (`tegra_vpr`, `tegra_vpr_buffer`, `tegra_vpr_attachment`, `tegra_vpr_device`, `tegra_vpr_chunk`, bitmaps) are zero-initialized via `kzalloc_obj`, `kzalloc_objs`, and `bitmap_zalloc`. All local variables used across CMA and VPR functions are explicitly initialized before use. Furthermore, CPU access and user mappings for VPR buffers are explicitly rejected with `-EPERM` (`tegra_vpr_mmap`, `tegra_vpr_begin_cpu_access`, `tegra_vpr_end_cpu_access`), preventing kernel or user reads of VPR memory. There are no kernel-to-user info leaks or branching on uninitialized data. Any potential concurrency, allocation bounds, or reference management issues are covered by standard sanitizers (KASAN, LOCKDEP). Therefore, dedicated KMSAN fuzzing is not required.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch series modifies core memory management CMA (Contiguous Memory Allocator) logic in mm/cma.c (refactoring cma_range_alloc, updating __cma_alloc_frozen with a new WARN_ON_ONCE assertion, and introducing cma_alloc_at / cma_alloc_at_frozen), adds debugfs infrastructure to the dma-buf heap subsystem in drivers/dma-buf/dma-heap.c, and updates bitmap helper routines in include/linux/bitmap.h. These core MM and DMA-BUF subsystems are reachable in standard virtualized environments on amd64.",
  "WorthFuzzing": true
}

1/1 2026/09/05 20:47 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 1fa4c99c058d2ba8ddcbfcc5fff6c778e8fb6097\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Sat Sep 5 20:47:42 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml\nindex bdf981781bd5f..fdd3fe9e25470 100644\n--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml\n+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml\n@@ -68,6 +68,14 @@ properties:\n       - const: dma-mem # read\n       - const: write\n \n+  memory-region:\n+    items:\n+      - description: reference to the video protection memory region\n+\n+  memory-region-names:\n+    items:\n+      - const: protected\n+\n   dma-coherent: true\n \n additionalProperties: false\ndiff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml\nindex ce4589466a186..881bfbf4764df 100644\n--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml\n+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml\n@@ -57,6 +57,16 @@ properties:\n       - const: dma-mem # read-0\n       - const: read-1\n \n+  memory-region:\n+    minItems: 1\n+    maxItems: 2\n+\n+  memory-region-names:\n+    items:\n+      enum: [ framebuffer, protected ]\n+    minItems: 1\n+    maxItems: 2\n+\n   nvidia,outputs:\n     description: A list of phandles of outputs that this display\n       controller can drive.\ndiff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml\nindex 69be95afd5627..3bf06dcd61986 100644\n--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml\n+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml\n@@ -65,7 +65,15 @@ properties:\n     items:\n       - description: phandle to the core power domain\n \n-  memory-region: true\n+  memory-region:\n+    minItems: 1\n+    maxItems: 2\n+\n+  memory-region-names:\n+    items:\n+      enum: [ framebuffer, protected ]\n+    minItems: 1\n+    maxItems: 2\n \n   nvidia,head:\n     $ref: /schemas/types.yaml#/definitions/uint32\ndiff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml\nindex 8312b7699cbed..420a1fe5ee805 100644\n--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml\n+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml\n@@ -98,6 +98,13 @@ properties:\n     items:\n       - description: phandle to the HEG or core power domain\n \n+  memory-region:\n+    maxItems: 1\n+\n+  memory-region-names:\n+    items:\n+      - const: protected\n+\n required:\n   - compatible\n   - interrupts\ndiff --git a/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml b/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml\nindex 4eb325cfd2961..bcaaabca945dc 100644\n--- a/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml\n+++ b/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml\n@@ -60,6 +60,14 @@ properties:\n       - const: dma-mem\n       - const: write\n \n+  memory-region:\n+    items:\n+      - description: reference to the video protection memory region\n+\n+  memory-region-names:\n+    items:\n+      - const: protected\n+\n   nvidia,memory-controller:\n     $ref: /schemas/types.yaml#/definitions/phandle\n     description:\ndiff --git a/Documentation/devicetree/bindings/reserved-memory/nvidia,tegra-video-protection-region.yaml b/Documentation/devicetree/bindings/reserved-memory/nvidia,tegra-video-protection-region.yaml\nnew file mode 100644\nindex 0000000000000..862bfd3913786\n--- /dev/null\n+++ b/Documentation/devicetree/bindings/reserved-memory/nvidia,tegra-video-protection-region.yaml\n@@ -0,0 +1,75 @@\n+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)\n+%YAML 1.2\n+---\n+$id: http://devicetree.org/schemas/reserved-memory/nvidia,tegra-video-protection-region.yaml#\n+$schema: http://devicetree.org/meta-schemas/core.yaml#\n+\n+title: NVIDIA Tegra Video Protection Region (VPR)\n+\n+maintainers:\n+  - Thierry Reding \u003cthierry.reding@gmail.com\u003e\n+  - Jon Hunter \u003cjonathanh@nvidia.com\u003e\n+\n+description: |\n+  NVIDIA Tegra chips have long supported a mechanism to protect a single,\n+  contiguous memory region from non-secure memory accesses. Typically this\n+  region is used for decoding and playback of DRM protected content. Various\n+  devices, such as the display controller and multimedia engines (video\n+  decoder) can access this region in a secure way. Access from the CPU is\n+  generally forbidden.\n+\n+  Two variants exist for VPR: one is fixed in both the base address and size,\n+  while the other is resizable. Fixed VPR can be described by just a \"reg\"\n+  property specifying the base address and size, whereas the resizable VPR\n+  is defined by a size/alignment pair of properties. For resizable VPR the\n+  memory is reusable by the rest of the system when it's unused for VPR and\n+  therefore the \"reusable\" property must be specified along with it. For a\n+  fixed VPR, the memory is permanently protected, and therefore it's not\n+  reusable and must also be marked as \"no-map\" to prevent any (including\n+  speculative) accesses to it.\n+\n+allOf:\n+  - $ref: reserved-memory.yaml\n+  - oneOf:\n+      - required:\n+          - compatible\n+          - reg\n+\n+      - required:\n+          - compatible\n+          - size\n+\n+properties:\n+  compatible:\n+    const: nvidia,tegra-video-protection-region\n+\n+dependencies:\n+  size: [alignment, reusable]\n+  alignment: [size, reusable]\n+  reusable: [alignment, size]\n+\n+  reg: [no-map]\n+  no-map: [reg]\n+\n+unevaluatedProperties: false\n+\n+examples:\n+  - |\n+    /* resizable VPR */\n+    protected {\n+      compatible = \"nvidia,tegra-video-protection-region\";\n+\n+      size = \u003c0x0 0x70000000\u003e;\n+      alignment = \u003c0x0 0x100000\u003e;\n+      reusable;\n+    };\n+\n+  - |\n+    /* fixed VPR */\n+    protected@a8000000 {\n+      compatible = \"nvidia,tegra-video-protection-region\";\n+\n+      /* fixed VPR */\n+      reg = \u003c0xa8000000 0x70000000\u003e;\n+      no-map;\n+    };\ndiff --git a/arch/arm64/boot/dts/nvidia/tegra234.dtsi b/arch/arm64/boot/dts/nvidia/tegra234.dtsi\nindex 5e29316a4d759..c6a5ced069e79 100644\n--- a/arch/arm64/boot/dts/nvidia/tegra234.dtsi\n+++ b/arch/arm64/boot/dts/nvidia/tegra234.dtsi\n@@ -29,6 +29,45 @@ aliases {\n \t\ti2c8 = \u0026dp_aux_ch3_i2c;\n \t};\n \n+\treserved-memory {\n+\t\t#address-cells = \u003c2\u003e;\n+\t\t#size-cells = \u003c2\u003e;\n+\t\tranges;\n+\n+\t\tvpr: protected {\n+\t\t\tcompatible = \"nvidia,tegra-video-protection-region\";\n+\t\t\tstatus = \"disabled\";\n+\n+\t\t\t/*\n+\t\t\t * Two variants exist for this. For fixed VPR, the\n+\t\t\t * firmware is supposed to update the \"reg\" property\n+\t\t\t * with the fixed memory region configured as VPR.\n+\t\t\t *\n+\t\t\t * For resizable VPR we don't care about the exact\n+\t\t\t * address and instead want a reserved region to be\n+\t\t\t * allocated with a certain size and alignment at\n+\t\t\t * boot time.\n+\t\t\t *\n+\t\t\t * The below assumes resizable VPR by default. If the\n+\t\t\t * firmwares sets up fixed VPR, it is responsible for\n+\t\t\t * adding the missing \"reg\" property, removing any of\n+\t\t\t * the unused properties, as well as adding a unit-\n+\t\t\t * address matching the \"reg\" property.\n+\t\t\t */\n+\n+\t\t\t/* fixed VPR */\n+\t\t\t/*\n+\t\t\treg = \u003c0x0 0x0 0x0 0x0\u003e;\n+\t\t\tno-map;\n+\t\t\t*/\n+\n+\t\t\t/* resizable VPR */\n+\t\t\tsize = \u003c0x0 0x70000000\u003e;\n+\t\t\talignment = \u003c0x0 0x100000\u003e;\n+\t\t\treusable;\n+\t\t};\n+\t};\n+\n \tbus@0 {\n \t\tcompatible = \"simple-bus\";\n \n@@ -4440,6 +4479,9 @@ vic@15340000 {\n \t\t\t\tinterconnect-names = \"dma-mem\", \"write\";\n \t\t\t\tiommus = \u003c\u0026smmu_niso1 TEGRA234_SID_VIC\u003e;\n \t\t\t\tdma-coherent;\n+\n+\t\t\t\tmemory-region = \u003c\u0026vpr\u003e;\n+\t\t\t\tmemory-region-names = \"protected\";\n \t\t\t};\n \n \t\t\tnvdec@15480000 {\n@@ -4458,6 +4500,9 @@ nvdec@15480000 {\n \t\t\t\tiommus = \u003c\u0026smmu_niso1 TEGRA234_SID_NVDEC\u003e;\n \t\t\t\tdma-coherent;\n \n+\t\t\t\tmemory-region = \u003c\u0026vpr\u003e;\n+\t\t\t\tmemory-region-names = \"protected\";\n+\n \t\t\t\tnvidia,memory-controller = \u003c\u0026mc\u003e;\n \n \t\t\t\t/*\ndiff --git a/arch/arm64/boot/dts/nvidia/tegra264.dtsi b/arch/arm64/boot/dts/nvidia/tegra264.dtsi\nindex 013228a817237..0123fdc0e1250 100644\n--- a/arch/arm64/boot/dts/nvidia/tegra264.dtsi\n+++ b/arch/arm64/boot/dts/nvidia/tegra264.dtsi\n@@ -23,6 +23,39 @@ shmem_bpmp: shmem@86070000 {\n \t\t\treg = \u003c0x0 0x86070000 0x0 0x2000\u003e;\n \t\t\tno-map;\n \t\t};\n+\n+\t\tvpr: protected {\n+\t\t\tcompatible = \"nvidia,tegra-video-protection-region\";\n+\t\t\tstatus = \"disabled\";\n+\n+\t\t\t/*\n+\t\t\t * Two variants exist for this. For fixed VPR, the\n+\t\t\t * firmware is supposed to update the \"reg\" property\n+\t\t\t * with the fixed memory region configured as VPR.\n+\t\t\t *\n+\t\t\t * For resizable VPR we don't care about the exact\n+\t\t\t * address and instead want a reserved region to be\n+\t\t\t * allocated with a certain size and alignment at\n+\t\t\t * boot time.\n+\t\t\t *\n+\t\t\t * The below assumes resizable VPR by default. If the\n+\t\t\t * firmwares sets up fixed VPR, it is responsible for\n+\t\t\t * adding the missing \"reg\" property, removing any of\n+\t\t\t * the unused properties, as well as adding a unit-\n+\t\t\t * address matching the \"reg\" property.\n+\t\t\t */\n+\n+\t\t\t/* fixed VPR */\n+\t\t\t/*\n+\t\t\treg = \u003c0x0 0x0 0x0 0x0\u003e;\n+\t\t\tno-map;\n+\t\t\t*/\n+\n+\t\t\t/* resizable VPR */\n+\t\t\tsize = \u003c0x0 0x70000000\u003e;\n+\t\t\talignment = \u003c0x0 0x100000\u003e;\n+\t\t\treusable;\n+\t\t};\n \t};\n \n \t/* SYSTEM MMIO */\ndiff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c\nindex 132938b32eb16..9253c529f8aa5 100644\n--- a/arch/arm64/mm/pageattr.c\n+++ b/arch/arm64/mm/pageattr.c\n@@ -262,6 +262,7 @@ int set_direct_map_invalid_noflush(struct page *page, unsigned int numpages)\n \treturn update_range_prot((unsigned long)page_address(page),\n \t\t\t\t PAGE_SIZE * numpages, set_mask, clear_mask);\n }\n+EXPORT_SYMBOL(set_direct_map_invalid_noflush);\n \n int set_direct_map_default_noflush(struct page *page, unsigned int numpages)\n {\n@@ -274,6 +275,7 @@ int set_direct_map_default_noflush(struct page *page, unsigned int numpages)\n \treturn update_range_prot((unsigned long)page_address(page),\n \t\t\t\t PAGE_SIZE * numpages, set_mask, clear_mask);\n }\n+EXPORT_SYMBOL(set_direct_map_default_noflush);\n \n static int __set_memory_enc_dec(unsigned long addr,\n \t\t\t\tint numpages,\ndiff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c\nindex 3937dd41bb0f6..f279b8fdd3ce0 100644\n--- a/drivers/dma-buf/dma-heap.c\n+++ b/drivers/dma-buf/dma-heap.c\n@@ -7,6 +7,7 @@\n  */\n \n #include \u003clinux/cdev.h\u003e\n+#include \u003clinux/debugfs.h\u003e\n #include \u003clinux/device.h\u003e\n #include \u003clinux/dma-buf.h\u003e\n #include \u003clinux/dma-heap.h\u003e\n@@ -225,6 +226,40 @@ const char *dma_heap_get_name(struct dma_heap *heap)\n }\n EXPORT_SYMBOL_NS_GPL(dma_heap_get_name, \"DMA_BUF_HEAP\");\n \n+#ifdef CONFIG_DEBUG_FS\n+static int dma_heap_debug_show(struct seq_file *s, void *unused)\n+{\n+\tstruct dma_heap *heap = s-\u003eprivate;\n+\tint err = 0;\n+\n+\tif (heap-\u003eops \u0026\u0026 heap-\u003eops-\u003eshow)\n+\t\terr = heap-\u003eops-\u003eshow(s, heap);\n+\n+\treturn err;\n+}\n+DEFINE_SHOW_ATTRIBUTE(dma_heap_debug);\n+\n+static struct dentry *dma_heap_debugfs_dir;\n+\n+static void dma_heap_init_debugfs(void)\n+{\n+\tdma_heap_debugfs_dir = debugfs_create_dir(\"dma_heap\", NULL);\n+}\n+\n+static void dma_heap_exit_debugfs(void)\n+{\n+\tdebugfs_remove_recursive(dma_heap_debugfs_dir);\n+}\n+#else\n+static void dma_heap_init_debugfs(void)\n+{\n+}\n+\n+static void dma_heap_exit_debugfs(void)\n+{\n+}\n+#endif\n+\n /**\n  * dma_heap_add - adds a heap to dmabuf heaps\n  * @exp_info: information needed to register this heap\n@@ -299,6 +334,13 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)\n \n \t/* Add heap to the list */\n \tlist_add(\u0026heap-\u003elist, \u0026heap_list);\n+\n+#ifdef CONFIG_DEBUG_FS\n+\tif (heap-\u003eops \u0026\u0026 heap-\u003eops-\u003eshow)\n+\t\tdebugfs_create_file(heap-\u003ename, 0444, dma_heap_debugfs_dir,\n+\t\t\t\t    heap, \u0026dma_heap_debug_fops);\n+#endif\n+\n \tmutex_unlock(\u0026heap_list_lock);\n \n \treturn heap;\n@@ -335,6 +377,16 @@ static int dma_heap_init(void)\n \t}\n \tdma_heap_class-\u003edevnode = dma_heap_devnode;\n \n+\tdma_heap_init_debugfs();\n+\n \treturn 0;\n }\n subsys_initcall(dma_heap_init);\n+\n+static void __exit dma_heap_exit(void)\n+{\n+\tdma_heap_exit_debugfs();\n+\tclass_destroy(dma_heap_class);\n+\tunregister_chrdev_region(dma_heap_devt, NUM_HEAP_MINORS);\n+}\n+__exitcall(dma_heap_exit);\ndiff --git a/drivers/dma-buf/heaps/Kconfig b/drivers/dma-buf/heaps/Kconfig\nindex bb729e91545c4..28d2c0800fb57 100644\n--- a/drivers/dma-buf/heaps/Kconfig\n+++ b/drivers/dma-buf/heaps/Kconfig\n@@ -20,3 +20,15 @@ config DMABUF_HEAPS_CMA\n \t  Choose this option to enable dma-buf CMA heap. This heap is backed\n \t  by the Contiguous Memory Allocator (CMA). If your system has these\n \t  regions, you should say Y here.\n+\n+config DMABUF_HEAPS_TEGRA_VPR\n+\ttristate \"NVIDIA Tegra Video-Protected-Region DMA-BUF Heap\"\n+\tdepends on DMABUF_HEAPS \u0026\u0026 DMA_CMA\n+\thelp\n+\t  Choose this option to enable Video-Protected-Region (VPR) support on\n+\t  a range of NVIDIA Tegra devices. Access to VPR memory is limited to\n+\t  a subset of hardware engines and specifically disallowed from the\n+\t  CPU. The region can be fixed, in which case no linear mapping exists\n+\t  for the memory, or it can be resizable on systems that want to reuse\n+\t  the memory for other uses when content-protected video is not played\n+\t  back.\ndiff --git a/drivers/dma-buf/heaps/Makefile b/drivers/dma-buf/heaps/Makefile\nindex 974467791032f..481fcb78f7572 100644\n--- a/drivers/dma-buf/heaps/Makefile\n+++ b/drivers/dma-buf/heaps/Makefile\n@@ -1,3 +1,13 @@\n # SPDX-License-Identifier: GPL-2.0\n obj-$(CONFIG_DMABUF_HEAPS_SYSTEM)\t+= system_heap.o\n obj-$(CONFIG_DMABUF_HEAPS_CMA)\t\t+= cma_heap.o\n+\n+#\n+# The reserved-memory bits always need to be built-in so that the CMA\n+# initialization runs during early boot. The VPR driver itself can be\n+# built as a module.\n+#\n+ifneq ($(CONFIG_DMABUF_HEAPS_TEGRA_VPR),)\n+obj-y\t\t\t\t\t+= tegra-vpr-init.o\n+obj-$(CONFIG_DMABUF_HEAPS_TEGRA_VPR)\t+= tegra-vpr.o\n+endif\ndiff --git a/drivers/dma-buf/heaps/tegra-vpr-init.c b/drivers/dma-buf/heaps/tegra-vpr-init.c\nnew file mode 100644\nindex 0000000000000..65e917713c6d4\n--- /dev/null\n+++ b/drivers/dma-buf/heaps/tegra-vpr-init.c\n@@ -0,0 +1,133 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/*\n+ * DMA-BUF restricted heap exporter for NVIDIA Video-Protection-Region (VPR)\n+ *\n+ * Copyright (C) 2024-2026 NVIDIA Corporation\n+ */\n+\n+#define pr_fmt(fmt) \"tegra-vpr: \" fmt\n+\n+#include \"tegra-vpr.h\"\n+\n+#include \u003clinux/cma.h\u003e\n+#include \u003clinux/of_reserved_mem.h\u003e\n+\n+static DEFINE_MUTEX(vpr_lock);\n+static LIST_HEAD_GUARDED(vpr_list, vpr_lock);\n+\n+static int __init tegra_vpr_node_init(unsigned long offset,\n+\t\t\t\t      struct reserved_mem *rmem)\n+{\n+\tstruct cma *cma;\n+\tint err;\n+\n+\tif (!IS_ALIGNED(rmem-\u003ebase, SZ_1M)) {\n+\t\tpr_err(\"%s: base is not aligned to 1 MiB\\n\", rmem-\u003ename);\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (!IS_ALIGNED(rmem-\u003esize, SZ_1M)) {\n+\t\tpr_err(\"%s: size is not aligned to 1 MiB\\n\", rmem-\u003ename);\n+\t\treturn -EINVAL;\n+\t}\n+\n+\terr = cma_init_reserved_mem(rmem-\u003ebase, rmem-\u003esize, 0, rmem-\u003ename,\n+\t\t\t\t    \u0026cma);\n+\tif (err \u003c 0) {\n+\t\tpr_err(\"%s: failed to initialize CMA: %d\\n\", __func__, err);\n+\t\treturn err;\n+\t}\n+\n+\trmem-\u003epriv = cma;\n+\n+\treturn 0;\n+}\n+\n+static struct tegra_vpr *tegra_vpr_lookup(struct cma *cma)\n+{\n+\tstruct tegra_vpr *vpr;\n+\n+\tmutex_lock(\u0026vpr_lock);\n+\n+\tlist_for_each_entry(vpr, \u0026vpr_list, list) {\n+\t\tif (vpr-\u003ecma == cma) {\n+\t\t\tmutex_unlock(\u0026vpr_lock);\n+\t\t\treturn vpr;\n+\t\t}\n+\t}\n+\n+\tmutex_unlock(\u0026vpr_lock);\n+\n+\treturn ERR_PTR(-EPROBE_DEFER);\n+}\n+\n+static int tegra_vpr_device_init(struct reserved_mem *rmem, struct device *dev)\n+{\n+\tconst struct dev_pm_ops *pm = dev-\u003edriver-\u003epm;\n+\tstruct tegra_vpr_device *node;\n+\tstruct cma *cma = rmem-\u003epriv;\n+\tstruct tegra_vpr *vpr;\n+\n+\tvpr = tegra_vpr_lookup(cma);\n+\tif (IS_ERR(vpr))\n+\t\treturn PTR_ERR(vpr);\n+\n+\tif (!pm || !pm-\u003efreeze || !pm-\u003ethaw)\n+\t\treturn -EINVAL;\n+\n+\tnode = kzalloc_obj(*node, GFP_KERNEL);\n+\tif (!node)\n+\t\treturn -ENOMEM;\n+\n+\tINIT_LIST_HEAD(\u0026node-\u003enode);\n+\tnode-\u003edev = dev;\n+\n+\tmutex_lock(\u0026vpr-\u003elock);\n+\tlist_add_tail(\u0026node-\u003enode, \u0026vpr-\u003edevices);\n+\tmutex_unlock(\u0026vpr-\u003elock);\n+\n+\treturn 0;\n+}\n+\n+static void tegra_vpr_device_release(struct reserved_mem *rmem,\n+\t\t\t\t     struct device *dev)\n+{\n+\tstruct tegra_vpr_device *node, *tmp;\n+\tstruct cma *cma = rmem-\u003epriv;\n+\tstruct tegra_vpr *vpr;\n+\n+\tvpr = tegra_vpr_lookup(cma);\n+\tif (IS_ERR(vpr)) {\n+\t\tdev_WARN(dev, \"failed to find VPR for CMA '%s'\\n\",\n+\t\t\t cma_get_name(cma));\n+\t\treturn;\n+\t}\n+\n+\tmutex_lock(\u0026vpr-\u003elock);\n+\n+\tlist_for_each_entry_safe(node, tmp, \u0026vpr-\u003edevices, node) {\n+\t\tif (node-\u003edev == dev) {\n+\t\t\tlist_del(\u0026node-\u003enode);\n+\t\t\tkfree(node);\n+\t\t}\n+\t}\n+\n+\tmutex_unlock(\u0026vpr-\u003elock);\n+}\n+\n+static const struct reserved_mem_ops tegra_vpr_rmem_ops = {\n+\t.node_init = tegra_vpr_node_init,\n+\t.device_init = tegra_vpr_device_init,\n+\t.device_release = tegra_vpr_device_release,\n+};\n+\n+RESERVEDMEM_OF_DECLARE(tegra_vpr, \"nvidia,tegra-video-protection-region\",\n+\t\t       \u0026tegra_vpr_rmem_ops);\n+\n+void tegra_vpr_add(struct tegra_vpr *vpr)\n+{\n+\tmutex_lock(\u0026vpr_lock);\n+\tlist_add_tail(\u0026vpr-\u003elist, \u0026vpr_list);\n+\tmutex_unlock(\u0026vpr_lock);\n+}\n+EXPORT_SYMBOL(tegra_vpr_add);\ndiff --git a/drivers/dma-buf/heaps/tegra-vpr.c b/drivers/dma-buf/heaps/tegra-vpr.c\nnew file mode 100644\nindex 0000000000000..d8cff7e07ea0b\n--- /dev/null\n+++ b/drivers/dma-buf/heaps/tegra-vpr.c\n@@ -0,0 +1,1210 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/*\n+ * DMA-BUF restricted heap exporter for NVIDIA Video-Protection-Region (VPR)\n+ *\n+ * Copyright (C) 2024-2026 NVIDIA Corporation\n+ */\n+\n+#define pr_fmt(fmt) \"tegra-vpr: \" fmt\n+\n+#include \u003clinux/arm-smccc.h\u003e\n+#include \u003clinux/cma.h\u003e\n+#include \u003clinux/debugfs.h\u003e\n+#include \u003clinux/dma-buf.h\u003e\n+#include \u003clinux/dma-heap.h\u003e\n+#include \u003clinux/find.h\u003e\n+#include \u003clinux/memory.h\u003e\n+#include \u003clinux/of_reserved_mem.h\u003e\n+#include \u003clinux/platform_device.h\u003e\n+#include \u003clinux/pm_runtime.h\u003e\n+#include \u003clinux/reset.h\u003e\n+#include \u003clinux/set_memory.h\u003e\n+\n+#include \"tegra-vpr.h\"\n+\n+MODULE_IMPORT_NS(\"DMA_BUF_HEAP\");\n+MODULE_IMPORT_NS(\"DMA_BUF\");\n+\n+#define CREATE_TRACE_POINTS\n+#include \u003ctrace/events/tegra_vpr.h\u003e\n+\n+#define TEGRA_VPR_MAX_CHUNKS 64\n+\n+struct tegra_vpr_buffer {\n+\tstruct list_head attachments;\n+\tstruct tegra_vpr *vpr;\n+\tstruct list_head list;\n+\n+\t/**\n+\t * @lock: Protects concurrent access to the list of attachments.\n+\t */\n+\tstruct mutex lock;\n+\n+\tstruct page **pages;\n+\tpgoff_t num_pages;\n+\tphys_addr_t start;\n+\tphys_addr_t limit;\n+\tsize_t size;\n+\tint pageno;\n+\tint order;\n+\n+\tDECLARE_BITMAP(chunks, TEGRA_VPR_MAX_CHUNKS);\n+};\n+\n+struct tegra_vpr_attachment {\n+\tstruct device *dev;\n+\tstruct sg_table sgt;\n+\tstruct list_head list;\n+};\n+\n+#define ARM_SMCCC_TE_FUNC_PROGRAM_VPR 0x3\n+\n+#define ARM_SMCCC_VENDOR_SIP_TE_PROGRAM_VPR_FUNC_ID\t\t\\\n+\tARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,\t\t\t\\\n+\t\t\t   ARM_SMCCC_SMC_32,\t\t\t\\\n+\t\t\t   ARM_SMCCC_OWNER_SIP,\t\t\t\\\n+\t\t\t   ARM_SMCCC_TE_FUNC_PROGRAM_VPR)\n+\n+static int tegra_vpr_set(phys_addr_t base, phys_addr_t size)\n+{\n+\tstruct arm_smccc_res res;\n+\n+\tarm_smccc_smc(ARM_SMCCC_VENDOR_SIP_TE_PROGRAM_VPR_FUNC_ID, base, size,\n+\t\t      0, 0, 0, 0, 0, \u0026res);\n+\n+\treturn res.a0;\n+}\n+\n+static int tegra_vpr_get_extents(struct tegra_vpr *vpr, phys_addr_t *base,\n+\t\t\t\t phys_addr_t *size)\n+{\n+\tphys_addr_t start = ~0, limit = 0;\n+\tunsigned int i;\n+\n+\tfor (i = 0; i \u003c vpr-\u003enum_chunks; i++) {\n+\t\tstruct tegra_vpr_chunk *chunk = \u0026vpr-\u003echunks[i];\n+\n+\t\tif (chunk-\u003eactive) {\n+\t\t\tif (chunk-\u003estart \u003c start)\n+\t\t\t\tstart = chunk-\u003estart;\n+\n+\t\t\tif (chunk-\u003elimit \u003e limit)\n+\t\t\t\tlimit = chunk-\u003elimit;\n+\t\t}\n+\t}\n+\n+\tif (limit \u003e start) {\n+\t\t*size = limit - start;\n+\t\t*base = start;\n+\t} else {\n+\t\t*base = *size = 0;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int tegra_vpr_resize(struct tegra_vpr *vpr)\n+{\n+\tstruct tegra_vpr_device *node;\n+\tphys_addr_t base, size;\n+\tint err, status = 0;\n+\n+\terr = tegra_vpr_get_extents(vpr, \u0026base, \u0026size);\n+\tif (err \u003c 0) {\n+\t\tpr_err(\"%s(): failed to get VPR extents: %d\\n\", __func__, err);\n+\t\treturn err;\n+\t}\n+\n+\tlist_for_each_entry(node, \u0026vpr-\u003edevices, node) {\n+\t\terr = pm_generic_freeze(node-\u003edev);\n+\t\tif (err \u003c 0) {\n+\t\t\tpr_err(\"failed to freeze %s: %d\\n\",\n+\t\t\t       dev_name(node-\u003edev), err);\n+\t\t\tstatus = err;\n+\t\t\tgoto thaw;\n+\t\t}\n+\t}\n+\n+\ttrace_tegra_vpr_set(base, size);\n+\n+\terr = tegra_vpr_set(base, size);\n+\tif (err \u003c 0) {\n+\t\tpr_err(\"failed to secure VPR: %d\\n\", err);\n+\t\tstatus = err;\n+\t}\n+\n+thaw:\n+\tlist_for_each_entry_continue_reverse(node, \u0026vpr-\u003edevices, node) {\n+\t\terr = pm_generic_thaw(node-\u003edev);\n+\t\tif (err \u003c 0) {\n+\t\t\tpr_err(\"failed to thaw %s: %d\\n\",\n+\t\t\t       dev_name(node-\u003edev), err);\n+\t\t\tcontinue;\n+\t\t}\n+\t}\n+\n+\treturn status;\n+}\n+\n+static int tegra_vpr_chunk_init(struct tegra_vpr *vpr,\n+\t\t\t\tstruct tegra_vpr_chunk *chunk,\n+\t\t\t\tphys_addr_t start, size_t size,\n+\t\t\t\tunsigned int order, const char *name)\n+{\n+\tchunk-\u003estart = start;\n+\tchunk-\u003elimit = start + size;\n+\tchunk-\u003esize = size;\n+\tchunk-\u003evpr = vpr;\n+\n+\tchunk-\u003eoffset = (start - vpr-\u003ebase) \u003e\u003e PAGE_SHIFT;\n+\tchunk-\u003enum_pages = size \u003e\u003e PAGE_SHIFT;\n+\tchunk-\u003enum_buffers = 0;\n+\n+\t/* CMA area is not reserved yet */\n+\tchunk-\u003estart_page = NULL;\n+\tchunk-\u003evirt = 0;\n+\n+\treturn 0;\n+}\n+\n+static void tegra_vpr_chunk_free(struct tegra_vpr_chunk *chunk)\n+{\n+}\n+\n+static inline bool tegra_vpr_chunk_is_last(const struct tegra_vpr_chunk *chunk)\n+{\n+\tphys_addr_t limit = chunk-\u003evpr-\u003ebase + chunk-\u003evpr-\u003esize;\n+\n+\treturn chunk-\u003elimit == limit;\n+}\n+\n+static inline bool tegra_vpr_chunk_is_leaf(const struct tegra_vpr_chunk *chunk)\n+{\n+\tconst struct tegra_vpr_chunk *next = chunk + 1;\n+\n+\tif (tegra_vpr_chunk_is_last(chunk))\n+\t\treturn true;\n+\n+\treturn !next-\u003eactive;\n+}\n+\n+static int tegra_vpr_chunk_alloc(struct tegra_vpr_chunk *chunk)\n+{\n+\tchunk-\u003estart_page = cma_alloc_at(chunk-\u003evpr-\u003ecma, chunk-\u003eoffset,\n+\t\t\t\t\t chunk-\u003enum_pages, false);\n+\tif (!chunk-\u003estart_page)\n+\t\treturn -ENOMEM;\n+\n+\tchunk-\u003evirt = (unsigned long)page_to_virt(chunk-\u003estart_page);\n+\n+\treturn 0;\n+}\n+\n+static int tegra_vpr_chunk_activate(struct tegra_vpr_chunk *chunk)\n+{\n+\tint err;\n+\n+\ttrace_tegra_vpr_chunk_activate(chunk-\u003estart, chunk-\u003elimit);\n+\n+\terr = set_direct_map_invalid_noflush(chunk-\u003estart_page,\n+\t\t\t\t\t     chunk-\u003enum_pages);\n+\tif (err)\n+\t\treturn err;\n+\n+\tflush_tlb_kernel_range(chunk-\u003evirt, chunk-\u003evirt + chunk-\u003esize);\n+\tchunk-\u003einvalid = false;\n+\tchunk-\u003eactive = true;\n+\n+\treturn 0;\n+}\n+\n+static int tegra_vpr_chunk_deactivate(struct tegra_vpr_chunk *chunk)\n+{\n+\tint err;\n+\n+\tif (!chunk-\u003eactive)\n+\t\treturn 0;\n+\n+\t/* do not deactivate if there are buffers left in this chunk */\n+\tif (WARN_ON(chunk-\u003enum_buffers \u003e 0))\n+\t\treturn -EBUSY;\n+\n+\ttrace_tegra_vpr_chunk_deactivate(chunk-\u003estart, chunk-\u003elimit);\n+\n+\terr = set_direct_map_default_noflush(chunk-\u003estart_page,\n+\t\t\t\t\t     chunk-\u003enum_pages);\n+\tif (err)\n+\t\treturn err;\n+\n+\tflush_tlb_kernel_range(chunk-\u003evirt, chunk-\u003evirt + chunk-\u003esize);\n+\tchunk-\u003einvalid = false;\n+\tchunk-\u003eactive = false;\n+\n+\treturn 0;\n+}\n+\n+static void tegra_vpr_chunk_release(struct tegra_vpr_chunk *chunk)\n+{\n+\tif (!WARN_ON(chunk-\u003eactive || chunk-\u003einvalid)) {\n+\t\tcma_release(chunk-\u003evpr-\u003ecma, chunk-\u003estart_page,\n+\t\t\t    chunk-\u003enum_pages);\n+\t\tchunk-\u003estart_page = NULL;\n+\t\tchunk-\u003evirt = 0;\n+\t}\n+}\n+\n+static bool tegra_vpr_chunk_overlaps(struct tegra_vpr_chunk *chunk,\n+\t\t\t\t     unsigned int start, unsigned int limit)\n+{\n+\tunsigned int first = chunk-\u003eoffset;\n+\tunsigned int last = chunk-\u003eoffset + chunk-\u003enum_pages - 1;\n+\n+\tif (last \u003c start || first \u003e= limit)\n+\t\treturn false;\n+\n+\treturn true;\n+}\n+\n+static int tegra_vpr_activate_chunks(struct tegra_vpr *vpr,\n+\t\t\t\t     struct tegra_vpr_buffer *buffer)\n+{\n+\tDECLARE_BITMAP(dirty, vpr-\u003enum_chunks);\n+\tunsigned int i, bottom, top;\n+\tint err = 0, ret;\n+\n+\tbitmap_zero(dirty, vpr-\u003enum_chunks);\n+\n+\t/* activate any inactive chunks that overlap this buffer */\n+\tfor_each_set_bit(i, buffer-\u003echunks, vpr-\u003enum_chunks) {\n+\t\tstruct tegra_vpr_chunk *chunk = \u0026vpr-\u003echunks[i];\n+\n+\t\tif (chunk-\u003eactive)\n+\t\t\tcontinue;\n+\n+\t\terr = tegra_vpr_chunk_alloc(chunk);\n+\t\tif (err \u003c 0)\n+\t\t\tgoto deactivate;\n+\n+\t\terr = tegra_vpr_chunk_activate(chunk);\n+\t\tif (err \u003c 0) {\n+\t\t\ttegra_vpr_chunk_release(chunk);\n+\t\t\tgoto deactivate;\n+\t\t}\n+\n+\t\tset_bit(i, vpr-\u003eactive);\n+\t\tset_bit(i, dirty);\n+\t}\n+\n+\t/*\n+\t * Activating chunks above may have created holes, but since the VPR\n+\t * can only ever be a single contiguous region, make sure to activate\n+\t * any missing chunks.\n+\t */\n+\tfor_each_clear_bitrange(bottom, top, vpr-\u003eactive, vpr-\u003enum_chunks) {\n+\t\t/* inactive chunks at the bottom or the top are harmless */\n+\t\tif (bottom == 0 || top == vpr-\u003enum_chunks)\n+\t\t\tcontinue;\n+\n+\t\tfor (i = bottom; i \u003c top; i++) {\n+\t\t\tstruct tegra_vpr_chunk *chunk = \u0026vpr-\u003echunks[i];\n+\n+\t\t\terr = tegra_vpr_chunk_alloc(chunk);\n+\t\t\tif (err \u003c 0)\n+\t\t\t\tgoto deactivate;\n+\n+\t\t\terr = tegra_vpr_chunk_activate(chunk);\n+\t\t\tif (err \u003c 0) {\n+\t\t\t\ttegra_vpr_chunk_release(chunk);\n+\t\t\t\tgoto deactivate;\n+\t\t\t}\n+\n+\t\t\tset_bit(i, vpr-\u003eactive);\n+\t\t\tset_bit(i, dirty);\n+\t\t}\n+\t}\n+\n+\t/* if any chunks have been activated, VPR needs to be resized */\n+\tif (!bitmap_empty(dirty, vpr-\u003enum_chunks)) {\n+\t\terr = tegra_vpr_resize(vpr);\n+\t\tif (err \u003c 0) {\n+\t\t\tpr_err(\"failed to grow VPR: %d\\n\", err);\n+\t\t\tgoto deactivate;\n+\t\t}\n+\t}\n+\n+\t/* increment buffer count for each chunk */\n+\tfor_each_set_bit(i, buffer-\u003echunks, vpr-\u003enum_chunks)\n+\t\tvpr-\u003echunks[i].num_buffers++;\n+\n+\treturn 0;\n+\n+deactivate:\n+\t/* deactivate any of the previously inactive chunks on failure */\n+\tfor_each_set_bit(i, dirty, vpr-\u003enum_chunks) {\n+\t\tstruct tegra_vpr_chunk *chunk = \u0026vpr-\u003echunks[i];\n+\n+\t\tret = tegra_vpr_chunk_deactivate(chunk);\n+\t\tif (WARN_ON(ret \u003c 0)) {\n+\t\t\tpr_err(\"failed to deactivate chunk #%u: %d\\n\", i, ret);\n+\t\t} else {\n+\t\t\ttegra_vpr_chunk_release(chunk);\n+\t\t\tclear_bit(i, vpr-\u003eactive);\n+\t\t}\n+\t}\n+\n+\treturn err;\n+}\n+\n+/*\n+ * Retrieve the range of pages within the activate region of the VPR.\n+ */\n+static bool tegra_vpr_get_active_range(struct tegra_vpr *vpr,\n+\t\t\t\t       unsigned int *first,\n+\t\t\t\t       unsigned int *last)\n+{\n+\tunsigned long i, j;\n+\n+\ti = find_first_bit(vpr-\u003eactive, vpr-\u003enum_chunks);\n+\tif (i \u003e= vpr-\u003enum_chunks)\n+\t\treturn false;\n+\n+\tj = find_last_bit(vpr-\u003eactive, vpr-\u003enum_chunks);\n+\tif (j \u003e= vpr-\u003enum_chunks)\n+\t\treturn false;\n+\n+\t*first = vpr-\u003echunks[i].offset;\n+\t*last = vpr-\u003echunks[j].offset + vpr-\u003echunks[j].num_pages;\n+\n+\treturn true;\n+}\n+\n+/*\n+ * Try to find and allocate a free region within a specific page range.\n+ * Returns the page number if successful, -ENOSPC otherwise.\n+ *\n+ * This function mimics bitmap_find_free_region() but restricts the search\n+ * to a specific range to enable allocation within individual chunks.\n+ */\n+static int tegra_vpr_find_free_region_in_range(struct tegra_vpr *vpr,\n+\t\t\t\t\t       unsigned int start_page,\n+\t\t\t\t\t       unsigned int end_page,\n+\t\t\t\t\t       unsigned int num_pages,\n+\t\t\t\t\t       unsigned int align)\n+{\n+\tunsigned int pos, next = ALIGN(start_page, align);\n+\n+\t/* Scan through aligned positions, trying to allocate at each one */\n+\tfor (pos = next; pos + num_pages \u003c= end_page; pos = next) {\n+\t\tnext = find_next_bit(vpr-\u003ebitmap, pos + num_pages, pos);\n+\n+\t\tif (next \u003e= pos + num_pages) {\n+\t\t\tbitmap_set(vpr-\u003ebitmap, pos, num_pages);\n+\t\t\treturn pos;\n+\t\t}\n+\n+\t\tnext = find_next_zero_bit(vpr-\u003ebitmap, vpr-\u003enum_pages, next);\n+\t\tnext = ALIGN(next, align);\n+\t}\n+\n+\treturn -ENOSPC;\n+}\n+\n+static int tegra_vpr_find_free_region(struct tegra_vpr *vpr,\n+\t\t\t\t      unsigned int num_pages,\n+\t\t\t\t      unsigned long align)\n+{\n+\treturn tegra_vpr_find_free_region_in_range(vpr, 0, vpr-\u003enum_pages - 1,\n+\t\t\t\t\t\t   num_pages, align);\n+}\n+\n+static int tegra_vpr_find_free_region_clustered(struct tegra_vpr *vpr,\n+\t\t\t\t\t\tunsigned int num_pages,\n+\t\t\t\t\t\tunsigned int align)\n+{\n+\tunsigned int target, first, last;\n+\tint pageno;\n+\n+\t/*\n+\t * If there are no allocations, abort the clustered allocation scheme\n+\t * and use the generic allocation scheme instead.\n+\t */\n+\tif (vpr-\u003efirst \u003e vpr-\u003elast)\n+\t\treturn -ENOSPC;\n+\n+\t/*\n+\t * First, try to allocate within the currently allocated region. This\n+\t * keeps allocations tightly packed and minimizes the VPR size needed.\n+\t */\n+\tpageno = tegra_vpr_find_free_region_in_range(vpr, vpr-\u003efirst,\n+\t\t\t\t\t\t     vpr-\u003elast + 1, num_pages,\n+\t\t\t\t\t\t     align);\n+\tif (pageno \u003e= 0)\n+\t\treturn pageno;\n+\n+\t/*\n+\t * If not enough free space exists within the currently allocated\n+\t * region, check to see if the allocation fits anywhere within the\n+\t * active region, avoiding the need to resize the VPR.\n+\t */\n+\tif (tegra_vpr_get_active_range(vpr, \u0026first, \u0026last)) {\n+\t\tpageno = tegra_vpr_find_free_region_in_range(vpr, first, last,\n+\t\t\t\t\t\t\t     num_pages, align);\n+\t\tif (pageno \u003e= 0)\n+\t\t\treturn pageno;\n+\t}\n+\n+\t/*\n+\t * If not enough free space exists within the currently active region,\n+\t * try to allocate adjacent to it to grow it contiguously and ensure\n+\t * optimal packing.\n+\t */\n+\n+\t/*\n+\t * Calculate where the allocation should start to end right at the\n+\t * first allocated page, with proper alignment.\n+\t */\n+\tif (vpr-\u003efirst \u003e= num_pages) {\n+\t\ttarget = ALIGN_DOWN(vpr-\u003efirst - num_pages, align);\n+\n+\t\tif (!bitmap_allocate(vpr-\u003ebitmap, target, num_pages))\n+\t\t\treturn target;\n+\t}\n+\n+\t/* Try after the last allocation */\n+\ttarget = ALIGN(vpr-\u003elast + 1, align);\n+\n+\tif (target + num_pages \u003c= vpr-\u003enum_pages \u0026\u0026\n+\t    !bitmap_allocate(vpr-\u003ebitmap, target, num_pages))\n+\t\treturn target;\n+\n+\t/*\n+\t * Couldn't allocate at the ideal adjacent position, search for any\n+\t * available space before the first allocated page.\n+\t */\n+\tpageno = tegra_vpr_find_free_region_in_range(vpr, 0, vpr-\u003efirst,\n+\t\t\t\t\t\t     num_pages, align);\n+\tif (pageno \u003e= 0)\n+\t\treturn pageno;\n+\n+\t/*\n+\t * Couldn't allocate at the ideal adjacent position, search\n+\t * for any available space after the last allocated page.\n+\t */\n+\tpageno = tegra_vpr_find_free_region_in_range(vpr, vpr-\u003elast + 1,\n+\t\t\t\t\t\t     vpr-\u003enum_pages, num_pages,\n+\t\t\t\t\t\t     align);\n+\tif (pageno \u003e= 0)\n+\t\treturn pageno;\n+\n+\treturn -ENOSPC;\n+}\n+\n+/*\n+ * Find a free region, preferring locations near existing allocations to\n+ * minimize VPR fragmentation. The allocation strategy is to first allocate\n+ * within or adjacent to the existing region to keep allocations clustered.\n+ * Otherwise fall back to a generic allocation using the first available\n+ * space.\n+ *\n+ * This approach focuses on page-level allocation first, then the chunk\n+ * system determines which chunks need to be activated based on where the\n+ * pages ended up.\n+ */\n+static int tegra_vpr_allocate_region(struct tegra_vpr *vpr,\n+\t\t\t\t     unsigned int num_pages,\n+\t\t\t\t     unsigned int align)\n+{\n+\tint pageno;\n+\n+\t/*\n+\t * For non-resizable VPR (no chunks), use simple first-fit allocation.\n+\t * Clustering optimization is only beneficial for resizable VPR where\n+\t * keeping allocations together minimizes the active VPR size.\n+\t */\n+\tif (!vpr-\u003eresizable)\n+\t\treturn tegra_vpr_find_free_region(vpr, num_pages, align);\n+\n+\t/*\n+\t * Check if there are any existing allocations in the bitmap. If so,\n+\t * try to allocate near them to minimize fragmentation.\n+\t */\n+\tpageno = tegra_vpr_find_free_region_clustered(vpr, num_pages, align);\n+\tif (pageno \u003e= 0)\n+\t\treturn pageno;\n+\n+\t/*\n+\t * If there are no existing allocations, or no space adjacent to them,\n+\t * fall back to the first available space anywhere in the VPR.\n+\t */\n+\tpageno = tegra_vpr_find_free_region(vpr, num_pages, align);\n+\tif (pageno \u003e= 0)\n+\t\treturn pageno;\n+\n+\treturn -ENOSPC;\n+}\n+\n+static struct tegra_vpr_buffer *\n+tegra_vpr_buffer_allocate(struct tegra_vpr *vpr, size_t size)\n+{\n+\tunsigned int num_pages = size \u003e\u003e PAGE_SHIFT;\n+\tunsigned int order = get_order(size);\n+\tstruct tegra_vpr_buffer *buffer;\n+\tunsigned long first, last;\n+\tint pageno, err;\n+\n+\t/*\n+\t * Quick sanity check that we're not trying to allocate a buffer that\n+\t * has no chance of fitting into the VPR.\n+\t */\n+\tif (size \u003e vpr-\u003esize)\n+\t\treturn ERR_PTR(-EINVAL);\n+\n+\t/*\n+\t * \"order\" defines the alignment and size, so this may result in\n+\t * fragmented memory depending on the allocation patterns. However,\n+\t * since this is used primarily for video frames, it is expected that\n+\t * a number of buffers of the same size will be allocated, so\n+\t * fragmentation should be negligible.\n+\t */\n+\tpageno = tegra_vpr_allocate_region(vpr, num_pages, 1);\n+\tif (pageno \u003c 0)\n+\t\treturn ERR_PTR(pageno);\n+\n+\tfirst = find_first_bit(vpr-\u003ebitmap, vpr-\u003enum_pages);\n+\tlast = find_last_bit(vpr-\u003ebitmap, vpr-\u003enum_pages);\n+\n+\tbuffer = kzalloc_obj(*buffer, GFP_KERNEL);\n+\tif (!buffer) {\n+\t\terr = -ENOMEM;\n+\t\tgoto release;\n+\t}\n+\n+\tINIT_LIST_HEAD(\u0026buffer-\u003eattachments);\n+\tINIT_LIST_HEAD(\u0026buffer-\u003elist);\n+\tmutex_init(\u0026buffer-\u003elock);\n+\tbuffer-\u003estart = vpr-\u003ebase + (pageno \u003c\u003c PAGE_SHIFT);\n+\tbuffer-\u003elimit = buffer-\u003estart + size;\n+\tbuffer-\u003esize = size;\n+\tbuffer-\u003enum_pages = num_pages;\n+\tbuffer-\u003epageno = pageno;\n+\tbuffer-\u003eorder = order;\n+\n+\t/* track which chunks this buffer overlaps */\n+\tif (vpr-\u003eresizable) {\n+\t\tunsigned int limit = buffer-\u003epageno + buffer-\u003enum_pages;\n+\t\tpgoff_t i;\n+\n+\t\t/*\n+\t\t * Memory is backed by struct page, so track which ones we\n+\t\t * use.\n+\t\t */\n+\t\tbuffer-\u003epages = kvmalloc_array(buffer-\u003enum_pages,\n+\t\t\t\t\t       sizeof(*buffer-\u003epages),\n+\t\t\t\t\t       GFP_KERNEL);\n+\t\tif (!buffer-\u003epages) {\n+\t\t\terr = -ENOMEM;\n+\t\t\tgoto free;\n+\t\t}\n+\n+\t\tfor (i = 0; i \u003c buffer-\u003enum_pages; i++)\n+\t\t\tbuffer-\u003epages[i] = \u0026vpr-\u003estart_page[pageno + i];\n+\n+\t\tfor (i = 0; i \u003c vpr-\u003enum_chunks; i++) {\n+\t\t\tstruct tegra_vpr_chunk *chunk = \u0026vpr-\u003echunks[i];\n+\n+\t\t\tif (tegra_vpr_chunk_overlaps(chunk, pageno, limit))\n+\t\t\t\tset_bit(i, buffer-\u003echunks);\n+\t\t}\n+\n+\t\t/* activate chunks if necessary */\n+\t\terr = tegra_vpr_activate_chunks(vpr, buffer);\n+\t\tif (err \u003c 0) {\n+\t\t\tkfree(buffer-\u003epages);\n+\t\t\tgoto free;\n+\t\t}\n+\n+\t\t/* track first and last allocated pages */\n+\t\tif (buffer-\u003epageno \u003c vpr-\u003efirst)\n+\t\t\tvpr-\u003efirst = buffer-\u003epageno;\n+\n+\t\tif (limit - 1 \u003e vpr-\u003elast)\n+\t\t\tvpr-\u003elast = limit - 1;\n+\t}\n+\n+\treturn buffer;\n+\n+free:\n+\tkfree(buffer);\n+release:\n+\tbitmap_clear(vpr-\u003ebitmap, pageno, num_pages);\n+\treturn ERR_PTR(err);\n+}\n+\n+static void tegra_vpr_buffer_release(struct tegra_vpr_buffer *buffer)\n+{\n+\tstruct tegra_vpr *vpr = buffer-\u003evpr;\n+\tstruct tegra_vpr_buffer *entry;\n+\tunsigned int i;\n+\n+\t/*\n+\t * Decrement buffer count for each overlapping chunk. Note that chunks\n+\t * are not deactivated here yet, that's done in tegra_vpr_recycle()\n+\t * instead.\n+\t */\n+\tfor_each_set_bit(i, buffer-\u003echunks, vpr-\u003enum_chunks) {\n+\t\tif (!WARN_ON(vpr-\u003echunks[i].num_buffers == 0))\n+\t\t\tvpr-\u003echunks[i].num_buffers--;\n+\t}\n+\n+\t/* track first and last allocated pages */\n+\tif (list_is_first(\u0026buffer-\u003elist, \u0026vpr-\u003ebuffers) \u0026\u0026\n+\t    list_is_last(\u0026buffer-\u003elist, \u0026vpr-\u003ebuffers)) {\n+\t\t/* if there are no remaining buffers after this, reset */\n+\t\tvpr-\u003efirst = ~0U;\n+\t\tvpr-\u003elast = 0U;\n+\t} else if (list_is_first(\u0026buffer-\u003elist, \u0026vpr-\u003ebuffers)) {\n+\t\tentry = list_next_entry(buffer, list);\n+\t\tvpr-\u003efirst = entry-\u003epageno;\n+\t} else if (list_is_last(\u0026buffer-\u003elist, \u0026vpr-\u003ebuffers)) {\n+\t\tentry = list_prev_entry(buffer, list);\n+\t\tvpr-\u003elast = entry-\u003epageno + entry-\u003enum_pages - 1;\n+\t}\n+\n+\tbitmap_clear(vpr-\u003ebitmap, buffer-\u003epageno, buffer-\u003enum_pages);\n+\tlist_del(\u0026buffer-\u003elist);\n+\tkfree(buffer-\u003epages);\n+\tkfree(buffer);\n+}\n+\n+static int tegra_vpr_attach(struct dma_buf *buf,\n+\t\t\t    struct dma_buf_attachment *attachment)\n+{\n+\tstruct tegra_vpr_buffer *buffer = buf-\u003epriv;\n+\tstruct tegra_vpr_attachment *attach;\n+\tint err;\n+\n+\tattach = kzalloc_obj(*attach, GFP_KERNEL);\n+\tif (!attach)\n+\t\treturn -ENOMEM;\n+\n+\t/*\n+\t * For resizable VPR, the memory is backed by struct page, so we can\n+\t * use the convenient helper to create the SG table.\n+\t */\n+\tif (buffer-\u003epages) {\n+\t\terr = sg_alloc_table_from_pages(\u0026attach-\u003esgt, buffer-\u003epages,\n+\t\t\t\t\t\tbuffer-\u003enum_pages, 0,\n+\t\t\t\t\t\tbuffer-\u003esize, GFP_KERNEL);\n+\t\tif (err \u003c 0)\n+\t\t\tgoto free;\n+\t} else {\n+\t\tif (sg_alloc_table(\u0026attach-\u003esgt, 1, GFP_KERNEL)) {\n+\t\t\terr = -ENOMEM;\n+\t\t\tgoto free;\n+\t\t}\n+\n+\t\tsg_set_page(attach-\u003esgt.sgl, NULL, buffer-\u003esize, 0);\n+\t\tsg_dma_address(attach-\u003esgt.sgl) = buffer-\u003estart;\n+\t\tsg_dma_len(attach-\u003esgt.sgl) = buffer-\u003esize;\n+\t}\n+\n+\tattach-\u003edev = attachment-\u003edev;\n+\tINIT_LIST_HEAD(\u0026attach-\u003elist);\n+\tattachment-\u003epriv = attach;\n+\n+\tmutex_lock(\u0026buffer-\u003elock);\n+\tlist_add(\u0026attach-\u003elist, \u0026buffer-\u003eattachments);\n+\tmutex_unlock(\u0026buffer-\u003elock);\n+\n+\treturn 0;\n+\n+free:\n+\tkfree(attach);\n+\treturn err;\n+}\n+\n+static void tegra_vpr_detach(struct dma_buf *buf,\n+\t\t\t     struct dma_buf_attachment *attachment)\n+{\n+\tstruct tegra_vpr_buffer *buffer = buf-\u003epriv;\n+\tstruct tegra_vpr_attachment *attach = attachment-\u003epriv;\n+\n+\tmutex_lock(\u0026buffer-\u003elock);\n+\tlist_del(\u0026attach-\u003elist);\n+\tmutex_unlock(\u0026buffer-\u003elock);\n+\n+\tsg_free_table(\u0026attach-\u003esgt);\n+\tkfree(attach);\n+}\n+\n+static struct sg_table *\n+tegra_vpr_map_dma_buf(struct dma_buf_attachment *attachment,\n+\t\t      enum dma_data_direction direction)\n+{\n+\tstruct tegra_vpr_attachment *attach = attachment-\u003epriv;\n+\tstruct sg_table *sgt = \u0026attach-\u003esgt;\n+\tint err;\n+\n+\terr = dma_map_sgtable(attachment-\u003edev, sgt, direction,\n+\t\t\t      DMA_ATTR_SKIP_CPU_SYNC);\n+\tif (err \u003c 0)\n+\t\treturn ERR_PTR(err);\n+\n+\treturn sgt;\n+}\n+\n+static void tegra_vpr_unmap_dma_buf(struct dma_buf_attachment *attachment,\n+\t\t\t\t    struct sg_table *sgt,\n+\t\t\t\t    enum dma_data_direction direction)\n+{\n+\tdma_unmap_sgtable(attachment-\u003edev, sgt, direction,\n+\t\t\t  DMA_ATTR_SKIP_CPU_SYNC);\n+}\n+\n+static void tegra_vpr_recycle(struct tegra_vpr *vpr)\n+{\n+\tDECLARE_BITMAP(dirty, vpr-\u003enum_chunks);\n+\tunsigned int i;\n+\tint err;\n+\n+\tif (!vpr-\u003eresizable)\n+\t\treturn;\n+\n+\tbitmap_zero(dirty, vpr-\u003enum_chunks);\n+\n+\t/*\n+\t * Deactivate any unused chunks from the bottom...\n+\t */\n+\tfor (i = 0; i \u003c vpr-\u003enum_chunks; i++) {\n+\t\tstruct tegra_vpr_chunk *chunk = \u0026vpr-\u003echunks[i];\n+\n+\t\tif (!chunk-\u003eactive)\n+\t\t\tcontinue;\n+\n+\t\tif (chunk-\u003enum_buffers \u003e 0)\n+\t\t\tbreak;\n+\n+\t\terr = tegra_vpr_chunk_deactivate(chunk);\n+\t\tif (err \u003c 0) {\n+\t\t\tpr_err(\"failed to deactivate chunk #%u: %d\\n\", i, err);\n+\t\t\tgoto activate;\n+\t\t} else {\n+\t\t\tclear_bit(i, vpr-\u003eactive);\n+\t\t\tset_bit(i, dirty);\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * ... and the top.\n+\t */\n+\tfor (i = 0; i \u003c vpr-\u003enum_chunks; i++) {\n+\t\tunsigned int index = vpr-\u003enum_chunks - i - 1;\n+\t\tstruct tegra_vpr_chunk *chunk = \u0026vpr-\u003echunks[index];\n+\n+\t\tif (!chunk-\u003eactive)\n+\t\t\tcontinue;\n+\n+\t\tif (chunk-\u003enum_buffers \u003e 0)\n+\t\t\tbreak;\n+\n+\t\terr = tegra_vpr_chunk_deactivate(chunk);\n+\t\tif (err \u003c 0) {\n+\t\t\tpr_err(\"failed to deactivate chunk #%u: %d\\n\", index,\n+\t\t\t       err);\n+\t\t\tgoto activate;\n+\t\t} else {\n+\t\t\tclear_bit(index, vpr-\u003eactive);\n+\t\t\tset_bit(index, dirty);\n+\t\t}\n+\t}\n+\n+\tif (!bitmap_empty(dirty, vpr-\u003enum_chunks)) {\n+\t\terr = tegra_vpr_resize(vpr);\n+\t\tif (err \u003c 0) {\n+\t\t\tpr_err(\"failed to shrink VPR: %d\\n\", err);\n+\t\t\tgoto activate;\n+\t\t}\n+\t}\n+\n+\t/* release the CMA memory associated with deactivated chunks */\n+\tfor_each_set_bit(i, dirty, vpr-\u003enum_chunks)\n+\t\ttegra_vpr_chunk_release(\u0026vpr-\u003echunks[i]);\n+\n+\treturn;\n+\n+activate:\n+\tfor_each_set_bit(i, dirty, vpr-\u003enum_chunks) {\n+\t\terr = tegra_vpr_chunk_activate(\u0026vpr-\u003echunks[i]);\n+\t\tif (WARN_ON(err \u003c 0))\n+\t\t\tpr_err(\"failed to activate chunk #%u: %d\\n\", i, err);\n+\n+\t\t/*\n+\t\t * This may not be fully activated at this point, but we need\n+\t\t * to keep track of it anyway to make sure the CMA region can\n+\t\t * eventually be released. The WARN_ON above tells us when it\n+\t\t * happens: here be dragons.\n+\t\t */\n+\t\tset_bit(i, vpr-\u003eactive);\n+\t}\n+}\n+\n+static void tegra_vpr_release(struct dma_buf *buf)\n+{\n+\tstruct tegra_vpr_buffer *buffer = buf-\u003epriv;\n+\tstruct tegra_vpr *vpr = buffer-\u003evpr;\n+\n+\tmutex_lock(\u0026vpr-\u003elock);\n+\n+\ttegra_vpr_buffer_release(buffer);\n+\ttegra_vpr_recycle(vpr);\n+\n+\tmutex_unlock(\u0026vpr-\u003elock);\n+}\n+\n+/*\n+ * Prohibit userspace mapping because the CPU cannot access this memory\n+ * anyway.\n+ */\n+static int tegra_vpr_begin_cpu_access(struct dma_buf *buf,\n+\t\t\t\t      enum dma_data_direction direction)\n+{\n+\treturn -EPERM;\n+}\n+\n+static int tegra_vpr_end_cpu_access(struct dma_buf *buf,\n+\t\t\t\t    enum dma_data_direction direction)\n+{\n+\treturn -EPERM;\n+}\n+\n+static int tegra_vpr_mmap(struct dma_buf *buf, struct vm_area_struct *vma)\n+{\n+\treturn -EPERM;\n+}\n+\n+static const struct dma_buf_ops tegra_vpr_buf_ops = {\n+\t.attach = tegra_vpr_attach,\n+\t.detach = tegra_vpr_detach,\n+\t.map_dma_buf = tegra_vpr_map_dma_buf,\n+\t.unmap_dma_buf = tegra_vpr_unmap_dma_buf,\n+\t.release = tegra_vpr_release,\n+\t.begin_cpu_access = tegra_vpr_begin_cpu_access,\n+\t.end_cpu_access = tegra_vpr_end_cpu_access,\n+\t.mmap = tegra_vpr_mmap,\n+};\n+\n+static struct dma_buf *tegra_vpr_allocate(struct dma_heap *heap,\n+\t\t\t\t\t  unsigned long len, u32 fd_flags,\n+\t\t\t\t\t  u64 heap_flags)\n+{\n+\tstruct tegra_vpr *vpr = dma_heap_get_drvdata(heap);\n+\tstruct tegra_vpr_buffer *buffer, *entry;\n+\tsize_t size = ALIGN(len, vpr-\u003ealign);\n+\tDEFINE_DMA_BUF_EXPORT_INFO(export);\n+\tstruct dma_buf *buf;\n+\n+\tmutex_lock(\u0026vpr-\u003elock);\n+\n+\tbuffer = tegra_vpr_buffer_allocate(vpr, size);\n+\tif (IS_ERR(buffer)) {\n+\t\tmutex_unlock(\u0026vpr-\u003elock);\n+\t\treturn ERR_CAST(buffer);\n+\t}\n+\n+\t/* insert in the correct order */\n+\tif (!list_empty(\u0026vpr-\u003ebuffers)) {\n+\t\tlist_for_each_entry(entry, \u0026vpr-\u003ebuffers, list) {\n+\t\t\tif (buffer-\u003epageno \u003c entry-\u003epageno) {\n+\t\t\t\tlist_add_tail(\u0026buffer-\u003elist, \u0026entry-\u003elist);\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tif (list_empty(\u0026buffer-\u003elist))\n+\t\tlist_add_tail(\u0026buffer-\u003elist, \u0026vpr-\u003ebuffers);\n+\n+\tbuffer-\u003evpr = vpr;\n+\n+\t/*\n+\t * If a valid buffer was allocated, wrap it in a dma_buf\n+\t * and return it.\n+\t */\n+\texport.exp_name = dma_heap_get_name(heap);\n+\texport.ops = \u0026tegra_vpr_buf_ops;\n+\texport.size = buffer-\u003esize;\n+\texport.flags = fd_flags;\n+\texport.priv = buffer;\n+\n+\tbuf = dma_buf_export(\u0026export);\n+\tif (IS_ERR(buf)) {\n+\t\ttegra_vpr_buffer_release(buffer);\n+\t\ttegra_vpr_recycle(vpr);\n+\t}\n+\n+\tmutex_unlock(\u0026vpr-\u003elock);\n+\treturn buf;\n+}\n+\n+static void tegra_vpr_debugfs_show_buffers(struct tegra_vpr *vpr,\n+\t\t\t\t\t   struct seq_file *s)\n+{\n+\tstruct tegra_vpr_buffer *buffer;\n+\tchar buf[16];\n+\n+\tmutex_lock(\u0026vpr-\u003elock);\n+\n+\tlist_for_each_entry(buffer, \u0026vpr-\u003ebuffers, list) {\n+\t\tstring_get_size(buffer-\u003esize, 1, STRING_UNITS_2, buf,\n+\t\t\t\tsizeof(buf));\n+\t\tseq_printf(s, \"  %pap-%pap (%s)\\n\", \u0026buffer-\u003estart,\n+\t\t\t   \u0026buffer-\u003elimit, buf);\n+\t}\n+\n+\tmutex_unlock(\u0026vpr-\u003elock);\n+}\n+\n+static void tegra_vpr_debugfs_show_chunks(struct tegra_vpr *vpr,\n+\t\t\t\t\t  struct seq_file *s)\n+{\n+\tstruct tegra_vpr_buffer *buffer;\n+\tunsigned int i;\n+\tchar buf[16];\n+\n+\tfor (i = 0; i \u003c vpr-\u003enum_chunks; i++) {\n+\t\tconst struct tegra_vpr_chunk *chunk = \u0026vpr-\u003echunks[i];\n+\n+\t\tstring_get_size(chunk-\u003esize, 1, STRING_UNITS_2, buf,\n+\t\t\t\tsizeof(buf));\n+\t\tseq_printf(s, \"  %pap-%pap (%s) (%s, %u buffers)\\n\",\n+\t\t\t   \u0026chunk-\u003estart, \u0026chunk-\u003elimit, buf,\n+\t\t\t   chunk-\u003eactive ? \"active\" : \"inactive\",\n+\t\t\t   chunk-\u003enum_buffers);\n+\t}\n+\n+\tlist_for_each_entry(buffer, \u0026vpr-\u003ebuffers, list) {\n+\t\tstring_get_size(buffer-\u003esize, 1, STRING_UNITS_2, buf,\n+\t\t\t\tsizeof(buf));\n+\t\tseq_printf(s, \"%pap-%pap (%s, chunks: %*pbl)\\n\",\n+\t\t\t   \u0026buffer-\u003estart, \u0026buffer-\u003elimit, buf,\n+\t\t\t   vpr-\u003enum_chunks, buffer-\u003echunks);\n+\t}\n+}\n+\n+static int tegra_vpr_debugfs_show(struct seq_file *s, struct dma_heap *heap)\n+{\n+\tstruct tegra_vpr *vpr = dma_heap_get_drvdata(heap);\n+\tphys_addr_t limit = vpr-\u003ebase + vpr-\u003esize;\n+\tchar buf[16];\n+\n+\tstring_get_size(vpr-\u003esize, 1, STRING_UNITS_2, buf, sizeof(buf));\n+\tseq_printf(s, \"%pap-%pap (%s)\\n\", \u0026vpr-\u003ebase, \u0026limit, buf);\n+\n+\tif (!vpr-\u003eresizable)\n+\t\ttegra_vpr_debugfs_show_buffers(vpr, s);\n+\telse\n+\t\ttegra_vpr_debugfs_show_chunks(vpr, s);\n+\n+\treturn 0;\n+}\n+\n+static const struct dma_heap_ops tegra_vpr_heap_ops = {\n+\t.allocate = tegra_vpr_allocate,\n+\t.show = tegra_vpr_debugfs_show,\n+};\n+\n+static int tegra_vpr_setup_chunks(struct tegra_vpr *vpr, const char *name)\n+{\n+\tphys_addr_t start, limit;\n+\tunsigned int order, i = 0;\n+\tsize_t max_size;\n+\tint err;\n+\n+\t/* Memory is backed by struct page, so track the first one. */\n+\tvpr-\u003estart_page = phys_to_page(vpr-\u003ebase);\n+\n+\t/* This seems a reasonable value, so hard-code it for now. */\n+\tvpr-\u003enum_chunks = 4;\n+\n+\tvpr-\u003echunks = kzalloc_objs(*vpr-\u003echunks, vpr-\u003enum_chunks);\n+\tif (!vpr-\u003echunks)\n+\t\treturn -ENOMEM;\n+\n+\tvpr-\u003eactive = bitmap_zalloc(vpr-\u003enum_chunks, GFP_KERNEL);\n+\tif (!vpr-\u003eactive) {\n+\t\terr = -ENOMEM;\n+\t\tgoto free;\n+\t}\n+\n+\tmax_size = PAGE_SIZE \u003c\u003c (get_order(vpr-\u003esize) - ilog2(vpr-\u003enum_chunks));\n+\torder = get_order(vpr-\u003ealign);\n+\n+\t/*\n+\t * Allocate CMA areas for VPR. All areas will be roughtly the same\n+\t * size, with the last area taking up the rest.\n+\t */\n+\tstart = vpr-\u003ebase;\n+\tlimit = vpr-\u003ebase + vpr-\u003esize;\n+\n+\tpr_debug(\"VPR: %pap-%pap (%lu pages, %u chunks, %lu MiB)\\n\", \u0026start,\n+\t\t \u0026limit, vpr-\u003enum_pages, vpr-\u003enum_chunks,\n+\t\t (unsigned long)vpr-\u003esize / 1024 / 1024);\n+\n+\tfor (i = 0; i \u003c vpr-\u003enum_chunks; i++) {\n+\t\tsize_t size = limit - start;\n+\t\tphys_addr_t end;\n+\n+\t\tsize = min_t(size_t, size, max_size);\n+\t\tend = start + size - 1;\n+\n+\t\terr = tegra_vpr_chunk_init(vpr, \u0026vpr-\u003echunks[i], start, size,\n+\t\t\t\t\t   order, name);\n+\t\tif (err \u003c 0) {\n+\t\t\tpr_err(\"failed to create VPR chunk: %d\\n\", err);\n+\t\t\tgoto free;\n+\t\t}\n+\n+\t\tpr_debug(\"  %2u: %pap-%pap (%lu MiB)\\n\", i, \u0026start, \u0026end,\n+\t\t\t size / 1024 / 1024);\n+\t\tstart += size;\n+\t}\n+\n+\tvpr-\u003efirst = ~0U;\n+\tvpr-\u003elast = 0U;\n+\n+\treturn 0;\n+\n+free:\n+\twhile (i--)\n+\t\ttegra_vpr_chunk_free(\u0026vpr-\u003echunks[i]);\n+\n+\tkfree(vpr-\u003eactive);\n+\tkfree(vpr-\u003echunks);\n+\treturn err;\n+}\n+\n+static void tegra_vpr_free_chunks(struct tegra_vpr *vpr)\n+{\n+\tunsigned int i;\n+\n+\tfor (i = 0; i \u003c vpr-\u003enum_chunks; i++)\n+\t\ttegra_vpr_chunk_free(\u0026vpr-\u003echunks[i]);\n+\n+\tkfree(vpr-\u003echunks);\n+}\n+\n+static int tegra_vpr_setup_static(struct tegra_vpr *vpr)\n+{\n+\tphys_addr_t start, limit;\n+\n+\tstart = vpr-\u003ebase;\n+\tlimit = vpr-\u003ebase + vpr-\u003esize;\n+\n+\tpr_debug(\"VPR: %pap-%pap (%lu pages, %lu MiB)\\n\", \u0026start, \u0026limit,\n+\t\t vpr-\u003enum_pages, (unsigned long)vpr-\u003esize / 1024 / 1024);\n+\n+\treturn 0;\n+}\n+\n+static int tegra_vpr_add_heap(struct reserved_mem *rmem,\n+\t\t\t      struct device_node *np)\n+{\n+\tstruct dma_heap_export_info info = {};\n+\tunsigned long first, last;\n+\tstruct dma_heap *heap;\n+\tstruct tegra_vpr *vpr;\n+\tint err;\n+\n+\tvpr = kzalloc_obj(*vpr, GFP_KERNEL);\n+\tif (!vpr)\n+\t\treturn -ENOMEM;\n+\n+\tINIT_LIST_HEAD(\u0026vpr-\u003elist);\n+\tINIT_LIST_HEAD(\u0026vpr-\u003ebuffers);\n+\tINIT_LIST_HEAD(\u0026vpr-\u003edevices);\n+\tmutex_init(\u0026vpr-\u003elock);\n+\n+\tvpr-\u003eresizable = !of_property_read_bool(np, \"no-map\");\n+\tvpr-\u003edev_node = of_node_get(np);\n+\tvpr-\u003ealign = PAGE_SIZE;\n+\tvpr-\u003ebase = rmem-\u003ebase;\n+\tvpr-\u003esize = rmem-\u003esize;\n+\tvpr-\u003enum_pages = vpr-\u003esize \u003e\u003e PAGE_SHIFT;\n+\tvpr-\u003enid = of_node_to_nid(np);\n+\tvpr-\u003ecma = rmem-\u003epriv;\n+\n+\tvpr-\u003ebitmap = bitmap_zalloc(vpr-\u003enum_pages, GFP_KERNEL);\n+\tif (!vpr-\u003ebitmap) {\n+\t\terr = -ENOMEM;\n+\t\tgoto free;\n+\t}\n+\n+\tfirst = find_first_bit(vpr-\u003ebitmap, vpr-\u003enum_pages);\n+\tlast = find_last_bit(vpr-\u003ebitmap, vpr-\u003enum_pages);\n+\n+\tif (vpr-\u003eresizable)\n+\t\terr = tegra_vpr_setup_chunks(vpr, rmem-\u003ename);\n+\telse\n+\t\terr = tegra_vpr_setup_static(vpr);\n+\n+\tif (err \u003c 0)\n+\t\tgoto free;\n+\n+\tinfo.name = vpr-\u003edev_node-\u003ename;\n+\tinfo.ops = \u0026tegra_vpr_heap_ops;\n+\tinfo.priv = vpr;\n+\n+\theap = dma_heap_add(\u0026info);\n+\tif (IS_ERR(heap)) {\n+\t\terr = PTR_ERR(heap);\n+\t\tgoto cleanup;\n+\t}\n+\n+\ttegra_vpr_add(vpr);\n+\n+\treturn 0;\n+\n+cleanup:\n+\tif (vpr-\u003eresizable)\n+\t\ttegra_vpr_free_chunks(vpr);\n+free:\n+\tbitmap_free(vpr-\u003ebitmap);\n+\tkfree(vpr);\n+\treturn err;\n+}\n+\n+static int tegra_vpr_init(void)\n+{\n+\tconst char *compatible = \"nvidia,tegra-video-protection-region\";\n+\tstruct device_node *parent;\n+\tstruct reserved_mem *rmem;\n+\tint err;\n+\n+\tparent = of_find_node_by_path(\"/reserved-memory\");\n+\tif (!parent)\n+\t\treturn 0;\n+\n+\tfor_each_child_of_node_scoped(parent, child) {\n+\t\tif (!of_device_is_compatible(child, compatible))\n+\t\t\tcontinue;\n+\n+\t\trmem = of_reserved_mem_lookup(child);\n+\t\tif (!rmem)\n+\t\t\tcontinue;\n+\n+\t\terr = tegra_vpr_add_heap(rmem, child);\n+\t\tif (err \u003c 0)\n+\t\t\tpr_err(\"failed to add VPR heap for %pOF: %d\\n\", child,\n+\t\t\t       err);\n+\n+\t\t/* only a single VPR heap is supported */\n+\t\tbreak;\n+\t}\n+\n+\tof_node_put(parent);\n+\treturn 0;\n+}\n+module_init(tegra_vpr_init);\n+\n+MODULE_DESCRIPTION(\"NVIDIA Tegra Video-Protection-Region DMA-BUF heap driver\");\n+MODULE_LICENSE(\"GPL\");\ndiff --git a/drivers/dma-buf/heaps/tegra-vpr.h b/drivers/dma-buf/heaps/tegra-vpr.h\nnew file mode 100644\nindex 0000000000000..ba2afba8cc617\n--- /dev/null\n+++ b/drivers/dma-buf/heaps/tegra-vpr.h\n@@ -0,0 +1,73 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+/*\n+ * DMA-BUF restricted heap exporter for NVIDIA Video-Protection-Region (VPR)\n+ *\n+ * Copyright (C) 2024-2026 NVIDIA Corporation\n+ */\n+\n+#ifndef DMA_BUF_HEAPS_TEGRA_VPR_H\n+#define DMA_BUF_HEAPS_TEGRA_VPR_H\n+\n+#include \u003clinux/list.h\u003e\n+#include \u003clinux/mutex.h\u003e\n+\n+struct tegra_vpr_device {\n+\tstruct list_head node;\n+\tstruct device *dev;\n+};\n+\n+struct tegra_vpr;\n+\n+struct tegra_vpr_chunk {\n+\tphys_addr_t start;\n+\tphys_addr_t limit;\n+\tsize_t size;\n+\n+\tstruct tegra_vpr *vpr;\n+\tbool invalid;\n+\tbool active;\n+\n+\tstruct page *start_page;\n+\tunsigned int offset;\n+\tunsigned long virt;\n+\tpgoff_t num_pages;\n+\n+\tunsigned int num_buffers;\n+};\n+\n+struct tegra_vpr {\n+\tstruct list_head list;\n+\n+\tstruct device_node *dev_node;\n+\tunsigned long align;\n+\tphys_addr_t base;\n+\tphys_addr_t size;\n+\tint nid;\n+\n+\tstruct list_head buffers;\n+\tunsigned long *bitmap;\n+\tpgoff_t num_pages;\n+\n+\t/* resizable VPR */\n+\tstruct cma *cma;\n+\tunsigned long *active;\n+\tstruct tegra_vpr_chunk *chunks;\n+\tunsigned int num_chunks;\n+\tstruct page *start_page;\n+\tbool resizable;\n+\n+\tunsigned int first;\n+\tunsigned int last;\n+\n+\tstruct list_head devices;\n+\n+\t/**\n+\t * @lock: Protects concurrent access to the allocation bitmap, as well\n+\t * as the buffers and devices lists.\n+\t */\n+\tstruct mutex lock;\n+};\n+\n+void tegra_vpr_add(struct tegra_vpr *vpr);\n+\n+#endif\ndiff --git a/drivers/of/of_numa.c b/drivers/of/of_numa.c\nindex 230d5f628c1b4..da2e9bd1717c2 100644\n--- a/drivers/of/of_numa.c\n+++ b/drivers/of/of_numa.c\n@@ -171,6 +171,7 @@ int of_node_to_nid(struct device_node *device)\n \n \treturn NUMA_NO_NODE;\n }\n+EXPORT_SYMBOL(of_node_to_nid);\n \n int __init of_numa_init(void)\n {\ndiff --git a/include/linux/bitmap.h b/include/linux/bitmap.h\nindex 7df1573a409c3..c2ab02985b5fa 100644\n--- a/include/linux/bitmap.h\n+++ b/include/linux/bitmap.h\n@@ -716,10 +716,10 @@ void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)\n }\n \n /**\n- * bitmap_allocate_region - allocate bitmap region\n+ * bitmap_allocate - allocate bitmap region\n  *\t@bitmap: array of unsigned longs corresponding to the bitmap\n  *\t@pos: beginning of bit region to allocate\n- *\t@order: region size (log base 2 of number of bits) to allocate\n+ *\t@len: number of bits to allocate\n  *\n  * Allocate (set bits in) a specified region of a bitmap.\n  *\n@@ -727,16 +727,31 @@ void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)\n  * free (not all bits were zero).\n  */\n static __always_inline\n-int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)\n+int bitmap_allocate(unsigned long *bitmap, unsigned int pos, unsigned int len)\n {\n-\tunsigned int len = BIT(order);\n-\n \tif (find_next_bit(bitmap, pos + len, pos) \u003c pos + len)\n \t\treturn -EBUSY;\n \tbitmap_set(bitmap, pos, len);\n \treturn 0;\n }\n \n+/**\n+ * bitmap_allocate_region - allocate bitmap region\n+ *\t@bitmap: array of unsigned longs corresponding to the bitmap\n+ *\t@pos: beginning of bit region to allocate\n+ *\t@order: region size (log base 2 of number of bits) to allocate\n+ *\n+ * Allocate (set bits in) a specified region of a bitmap.\n+ *\n+ * Returns: 0 on success, or %-EBUSY if specified region wasn't\n+ * free (not all bits were zero).\n+ */\n+static __always_inline\n+int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)\n+{\n+\treturn bitmap_allocate(bitmap, pos, BIT(order));\n+}\n+\n /**\n  * bitmap_find_free_region - find a contiguous aligned mem region\n  *\t@bitmap: array of unsigned longs corresponding to the bitmap\ndiff --git a/include/linux/cma.h b/include/linux/cma.h\nindex 8555d38a97b1a..844404459a424 100644\n--- a/include/linux/cma.h\n+++ b/include/linux/cma.h\n@@ -49,11 +49,15 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,\n \t\t\t\t\tstruct cma **res_cma);\n extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align,\n \t\t\t      bool no_warn);\n+extern struct page *cma_alloc_at(struct cma *cma, unsigned long offset,\n+\t\t\t\t unsigned long count, bool no_warn);\n extern bool cma_release(struct cma *cma, const struct page *pages, unsigned long count);\n \n struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,\n \t\tunsigned int align, bool no_warn);\n struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order);\n+struct page *cma_alloc_at_frozen(struct cma *cma, unsigned long offset,\n+\t\t\t\t unsigned long count, bool no_warn);\n bool cma_release_frozen(struct cma *cma, const struct page *pages,\n \t\tunsigned long count);\n \ndiff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h\nindex 648328a64b27e..1c9bed1f4ddef 100644\n--- a/include/linux/dma-heap.h\n+++ b/include/linux/dma-heap.h\n@@ -12,6 +12,7 @@\n #include \u003clinux/types.h\u003e\n \n struct dma_heap;\n+struct seq_file;\n \n /**\n  * struct dma_heap_ops - ops to operate on a given heap\n@@ -24,6 +25,7 @@ struct dma_heap_ops {\n \t\t\t\t    unsigned long len,\n \t\t\t\t    u32 fd_flags,\n \t\t\t\t    u64 heap_flags);\n+\tint (*show)(struct seq_file *s, struct dma_heap *heap);\n };\n \n /**\ndiff --git a/include/trace/events/cma.h b/include/trace/events/cma.h\nindex 37195edf24986..4e0ab63e303f0 100644\n--- a/include/trace/events/cma.h\n+++ b/include/trace/events/cma.h\n@@ -38,13 +38,17 @@ TRACE_EVENT(cma_release,\n \n TRACE_EVENT(cma_alloc_start,\n \n-\tTP_PROTO(const char *name, unsigned long request_count, unsigned long available_count,\n+\tTP_PROTO(const char *name, unsigned long start, unsigned long end,\n+\t\tunsigned long request_count, unsigned long available_count,\n \t\tunsigned long total_count, unsigned int align),\n \n-\tTP_ARGS(name, request_count, available_count, total_count, align),\n+\tTP_ARGS(name, start, end, request_count, available_count, total_count,\n+\t\talign),\n \n \tTP_STRUCT__entry(\n \t\t__string(name, name)\n+\t\t__field(unsigned long, start)\n+\t\t__field(unsigned long, end)\n \t\t__field(unsigned long, request_count)\n \t\t__field(unsigned long, available_count)\n \t\t__field(unsigned long, total_count)\n@@ -53,14 +57,18 @@ TRACE_EVENT(cma_alloc_start,\n \n \tTP_fast_assign(\n \t\t__assign_str(name);\n+\t\t__entry-\u003estart = start;\n+\t\t__entry-\u003eend = end;\n \t\t__entry-\u003erequest_count = request_count;\n \t\t__entry-\u003eavailable_count = available_count;\n \t\t__entry-\u003etotal_count = total_count;\n \t\t__entry-\u003ealign = align;\n \t),\n \n-\tTP_printk(\"name=%s request_count=%lu available_count=%lu total_count=%lu align=%u\",\n+\tTP_printk(\"name=%s start=%lu end=%lu request_count=%lu available_count=%lu total_count=%lu align=%u\",\n \t\t  __get_str(name),\n+\t\t  __entry-\u003estart,\n+\t\t  __entry-\u003eend,\n \t\t  __entry-\u003erequest_count,\n \t\t  __entry-\u003eavailable_count,\n \t\t  __entry-\u003etotal_count,\n@@ -132,6 +140,69 @@ TRACE_EVENT(cma_alloc_busy_retry,\n \t\t  __entry-\u003ealign)\n );\n \n+TRACE_EVENT(cma_alloc_at_start,\n+\n+\tTP_PROTO(const char *name, unsigned long pfn,\n+\t\t unsigned long request_count, unsigned long available_count,\n+\t\t unsigned long total_count),\n+\n+\tTP_ARGS(name, pfn, request_count, available_count, total_count),\n+\n+\tTP_STRUCT__entry(\n+\t\t__string(name, name)\n+\t\t__field(unsigned long, pfn)\n+\t\t__field(unsigned long, request_count)\n+\t\t__field(unsigned long, available_count)\n+\t\t__field(unsigned long, total_count)\n+\t),\n+\n+\tTP_fast_assign(\n+\t\t__assign_str(name);\n+\t\t__entry-\u003epfn = pfn;\n+\t\t__entry-\u003erequest_count = request_count;\n+\t\t__entry-\u003eavailable_count = available_count;\n+\t\t__entry-\u003etotal_count = total_count;\n+\t),\n+\n+\tTP_printk(\"name=%s pfn=%lx, request_count=%lu available_count=%lu total_count=%lu\",\n+\t\t  __get_str(name),\n+\t\t  __entry-\u003epfn,\n+\t\t  __entry-\u003erequest_count,\n+\t\t  __entry-\u003eavailable_count,\n+\t\t  __entry-\u003etotal_count)\n+);\n+\n+TRACE_EVENT(cma_alloc_at_finish,\n+\n+\tTP_PROTO(const char *name, unsigned long pfn, const struct page *page,\n+\t\t unsigned long count, int errorno),\n+\n+\tTP_ARGS(name, pfn, page, count, errorno),\n+\n+\tTP_STRUCT__entry(\n+\t\t__string(name, name)\n+\t\t__field(unsigned long, pfn)\n+\t\t__field(const struct page *, page)\n+\t\t__field(unsigned long, count)\n+\t\t__field(int, errorno)\n+\t),\n+\n+\tTP_fast_assign(\n+\t\t__assign_str(name);\n+\t\t__entry-\u003epfn = pfn;\n+\t\t__entry-\u003epage = page;\n+\t\t__entry-\u003ecount = count;\n+\t\t__entry-\u003eerrorno = errorno;\n+\t),\n+\n+\tTP_printk(\"name=%s pfn=0x%lx page=%p count=%lu errorno=%d\",\n+\t\t  __get_str(name),\n+\t\t  __entry-\u003epfn,\n+\t\t  __entry-\u003epage,\n+\t\t  __entry-\u003ecount,\n+\t\t  __entry-\u003eerrorno)\n+);\n+\n #endif /* _TRACE_CMA_H */\n \n /* This part must be outside protection */\ndiff --git a/include/trace/events/tegra_vpr.h b/include/trace/events/tegra_vpr.h\nnew file mode 100644\nindex 0000000000000..f8ceb17679fe0\n--- /dev/null\n+++ b/include/trace/events/tegra_vpr.h\n@@ -0,0 +1,57 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+\n+#if !defined(_TRACE_TEGRA_VPR_H) || defined(TRACE_HEADER_MULTI_READ)\n+#define _TRACE_TEGRA_VPR_H\n+\n+#undef TRACE_SYSTEM\n+#define TRACE_SYSTEM tegra_vpr\n+\n+#include \u003clinux/tracepoint.h\u003e\n+\n+TRACE_EVENT(tegra_vpr_chunk_activate,\n+\tTP_PROTO(phys_addr_t start, phys_addr_t limit),\n+\tTP_ARGS(start, limit),\n+\tTP_STRUCT__entry(\n+\t\t__field(phys_addr_t, start)\n+\t\t__field(phys_addr_t, limit)\n+\t),\n+\tTP_fast_assign(\n+\t\t__entry-\u003estart = start;\n+\t\t__entry-\u003elimit = limit;\n+\t),\n+\tTP_printk(\"%pap-%pap\", \u0026__entry-\u003estart,\n+\t\t  \u0026__entry-\u003elimit)\n+);\n+\n+TRACE_EVENT(tegra_vpr_chunk_deactivate,\n+\tTP_PROTO(phys_addr_t start, phys_addr_t limit),\n+\tTP_ARGS(start, limit),\n+\tTP_STRUCT__entry(\n+\t\t__field(phys_addr_t, start)\n+\t\t__field(phys_addr_t, limit)\n+\t),\n+\tTP_fast_assign(\n+\t\t__entry-\u003estart = start;\n+\t\t__entry-\u003elimit = limit;\n+\t),\n+\tTP_printk(\"%pap-%pap\", \u0026__entry-\u003estart,\n+\t\t  \u0026__entry-\u003elimit)\n+);\n+\n+TRACE_EVENT(tegra_vpr_set,\n+\tTP_PROTO(phys_addr_t base, phys_addr_t size),\n+\tTP_ARGS(base, size),\n+\tTP_STRUCT__entry(\n+\t\t__field(phys_addr_t, start)\n+\t\t__field(phys_addr_t, limit)\n+\t),\n+\tTP_fast_assign(\n+\t\t__entry-\u003estart = base;\n+\t\t__entry-\u003elimit = base + size;\n+\t),\n+\tTP_printk(\"%pap-%pap\", \u0026__entry-\u003estart, \u0026__entry-\u003elimit)\n+);\n+\n+#endif /* _TRACE_TEGRA_VPR_H */\n+\n+#include \u003ctrace/define_trace.h\u003e\ndiff --git a/mm/cma.c b/mm/cma.c\nindex a10ea37a261da..f92d3a1882098 100644\n--- a/mm/cma.c\n+++ b/mm/cma.c\n@@ -783,25 +783,32 @@ static void cma_debug_show_areas(struct cma *cma)\n \tspin_unlock_irq(\u0026cma-\u003elock);\n }\n \n+/*\n+ * Searches the CMA memrange, from @start to @end, for a free region of\n+ * @count bits. If @end is less or equal to @start, will search the entire\n+ * memrange.\n+ */\n static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,\n-\t\t\t\tunsigned long count, unsigned int align,\n-\t\t\t\tstruct page **pagep, gfp_t gfp)\n+\t\t\t   unsigned long start, unsigned long end,\n+\t\t\t   unsigned long count, unsigned int align,\n+\t\t\t   struct page **pagep, gfp_t gfp)\n {\n \tunsigned long bitmap_maxno, bitmap_no, bitmap_count;\n-\tunsigned long start, pfn, mask, offset;\n-\tint ret = -EBUSY;\n+\tunsigned long pfn, mask, offset;\n \tstruct page *page = NULL;\n+\tint ret = -EBUSY;\n \n \tmask = cma_bitmap_aligned_mask(cma, align);\n \toffset = cma_bitmap_aligned_offset(cma, cmr, align);\n-\tbitmap_maxno = cma_bitmap_maxno(cma, cmr);\n+\tbitmap_maxno = (end \u003e start) ? end : cma_bitmap_maxno(cma, cmr);\n \tbitmap_count = cma_bitmap_pages_to_bits(cma, count);\n \n \tif (bitmap_count \u003e bitmap_maxno)\n \t\tgoto out;\n \n-\tfor (start = 0; ; start = bitmap_no + mask + 1) {\n+\twhile (true) {\n \t\tspin_lock_irq(\u0026cma-\u003elock);\n+\n \t\t/*\n \t\t * If the request is larger than the available number\n \t\t * of pages, stop right away.\n@@ -810,6 +817,7 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,\n \t\t\tspin_unlock_irq(\u0026cma-\u003elock);\n \t\t\tbreak;\n \t\t}\n+\n \t\tbitmap_no = bitmap_find_next_zero_area_off(cmr-\u003ebitmap,\n \t\t\t\tbitmap_maxno, start, bitmap_count, mask,\n \t\t\t\toffset);\n@@ -835,10 +843,11 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,\n \n \t\tbitmap_set(cmr-\u003ebitmap, bitmap_no, bitmap_count);\n \t\tcma-\u003eavailable_count -= count;\n+\n \t\t/*\n-\t\t * It's safe to drop the lock here. We've marked this region for\n-\t\t * our exclusive use. If the migration fails we will take the\n-\t\t * lock again and unmark it.\n+\t\t * It's safe to drop the lock here. We've marked this region\n+\t\t * for our exclusive use. If the migration fails we will take\n+\t\t * the lock again and unmark it.\n \t\t */\n \t\tspin_unlock_irq(\u0026cma-\u003elock);\n \n@@ -856,37 +865,45 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,\n \t\t\t __func__, pfn, page);\n \n \t\ttrace_cma_alloc_busy_retry(cma-\u003ename, pfn, page, count, align);\n+\t\tstart = bitmap_no + mask + 1;\n \t}\n+\n out:\n \tif (!ret)\n \t\t*pagep = page;\n+\n \treturn ret;\n }\n \n-static struct page *__cma_alloc_frozen(struct cma *cma,\n-\t\tunsigned long count, unsigned int align, gfp_t gfp)\n+static struct page *__cma_alloc_frozen(struct cma *cma, unsigned long start,\n+\t\t\t\t       unsigned long end, unsigned long count,\n+\t\t\t\t       unsigned int align, gfp_t gfp)\n {\n \tstruct page *page = NULL;\n \tint ret = -ENOMEM, r;\n \tunsigned long i;\n-\tconst char *name = cma ? cma-\u003ename : NULL;\n+\n+\t/* cma_alloc_at() and friends will only work with single-range CMA */\n+\tif (WARN_ON_ONCE(start \u003e 0 \u0026\u0026 cma-\u003enranges != 1))\n+\t\treturn page;\n \n \tif (!cma || !cma-\u003ecount)\n \t\treturn page;\n \n-\tpr_debug(\"%s(cma %p, name: %s, count %lu, align %d)\\n\", __func__,\n-\t\t(void *)cma, cma-\u003ename, count, align);\n+\tpr_debug(\"%s(cma %p, name: %s, start %lu, end %lu, count %lu, align %d)\\n\",\n+\t\t __func__, (void *)cma, cma-\u003ename, start, end, count, align);\n \n \tif (!count)\n \t\treturn page;\n \n-\ttrace_cma_alloc_start(name, count, cma-\u003eavailable_count, cma-\u003ecount, align);\n+\ttrace_cma_alloc_start(cma-\u003ename, start, end, count,\n+\t\t\t      cma-\u003eavailable_count, cma-\u003ecount, align);\n \n \tfor (r = 0; r \u003c cma-\u003enranges; r++) {\n \t\tpage = NULL;\n \n-\t\tret = cma_range_alloc(cma, \u0026cma-\u003eranges[r], count, align,\n-\t\t\t\t       \u0026page, gfp);\n+\t\tret = cma_range_alloc(cma, \u0026cma-\u003eranges[r], start, end,\n+\t\t\t\t      count, align, \u0026page, gfp);\n \t\tif (ret != -EBUSY || page)\n \t\t\tbreak;\n \t}\n@@ -908,7 +925,7 @@ static struct page *__cma_alloc_frozen(struct cma *cma,\n \t}\n \n \tpr_debug(\"%s(): returned %p\\n\", __func__, page);\n-\ttrace_cma_alloc_finish(name, page ? page_to_pfn(page) : 0,\n+\ttrace_cma_alloc_finish(cma-\u003ename, page ? page_to_pfn(page) : 0,\n \t\t\t       page, count, align, ret);\n \tif (page) {\n \t\tcount_vm_event(CMA_ALLOC_SUCCESS);\n@@ -926,14 +943,22 @@ struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,\n {\n \tgfp_t gfp = GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0);\n \n-\treturn __cma_alloc_frozen(cma, count, align, gfp);\n+\treturn __cma_alloc_frozen(cma, 0, 0, count, align, gfp);\n }\n \n struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order)\n {\n \tgfp_t gfp = GFP_KERNEL | __GFP_COMP | __GFP_NOWARN;\n \n-\treturn __cma_alloc_frozen(cma, 1 \u003c\u003c order, order, gfp);\n+\treturn __cma_alloc_frozen(cma, 0, 0, 1 \u003c\u003c order, order, gfp);\n+}\n+\n+struct page *cma_alloc_at_frozen(struct cma *cma, unsigned long offset,\n+\t\t\t\t unsigned long count, bool no_warn)\n+{\n+\tgfp_t gfp = GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0);\n+\n+\treturn __cma_alloc_frozen(cma, offset, offset + count, count, 0, gfp);\n }\n \n /**\n@@ -959,6 +984,29 @@ struct page *cma_alloc(struct cma *cma, unsigned long count,\n }\n EXPORT_SYMBOL_GPL(cma_alloc);\n \n+/**\n+ * cma_alloc_at() - allocate pages from contiguous area at fixed offset\n+ * @cma:     Contiguous memory region for which the allocation is performed.\n+ * @offset:  Index of the first page to allocate.\n+ * @count:   Requested number of pages (in PAGE_SIZE order).\n+ * @no_warn: Avoid printing message about failed allocation.\n+ *\n+ * This function allocates a part of the contiguous memory on a specific\n+ * contiguous memory area.\n+ */\n+struct page *cma_alloc_at(struct cma *cma, unsigned long offset,\n+\t\t\t  unsigned long count, bool no_warn)\n+{\n+\tstruct page *page;\n+\n+\tpage = cma_alloc_at_frozen(cma, offset, count, no_warn);\n+\tif (page)\n+\t\tset_pages_refcounted(page, count);\n+\n+\treturn page;\n+}\n+EXPORT_SYMBOL_GPL(cma_alloc_at);\n+\n static struct cma_memrange *find_cma_memrange(struct cma *cma,\n \t\tconst struct page *pages, unsigned long count)\n {\n"
}

2/1 2026/09/05 20:47 agent patch-evaluator
1m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "CMA",
    "DMA_CMA",
    "DMABUF_HEAPS",
    "DMABUF_HEAPS_CMA",
    "CMA_DEBUGFS",
    "DEBUG_FS"
  ],
  "FocusSymbols": [
    "cma_range_alloc",
    "__cma_alloc_frozen",
    "cma_alloc_at",
    "cma_alloc_at_frozen",
    "dma_heap_debug_show"
  ],
  "Reasoning": "The patch series modifies core memory management CMA (Contiguous Memory Allocator) logic in mm/cma.c (refactoring cma_range_alloc, updating __cma_alloc_frozen with a new WARN_ON_ONCE assertion, and introducing cma_alloc_at / cma_alloc_at_frozen), adds debugfs infrastructure to the dma-buf heap subsystem in drivers/dma-buf/dma-heap.c, and updates bitmap helper routines in include/linux/bitmap.h. These core MM and DMA-BUF subsystems are reachable in standard virtualized environments on amd64.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.

================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.

- REACHABILITY IS THE PRIMARY GATE:
  Fuzzing can only discover bugs in code that can actually execute in standard virtualized
  environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
  If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
  regardless of whether it adds assertions or complex logic.

- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
  Patch authors routinely label changes as "cleanups", "refactorings", or state
  "No functional change intended". Do NOT take these claims at face value.
  Code refactorings that rearrange logic, introduce helper functions, or alter state management
  in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
  If reachable executable code is modified or refactored, it MUST be fuzzed.

- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
  When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
  BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
  Even if the author believes the invariant always holds, fuzzing is essential to verify whether
  an unusual sequence of operations can violate it.

================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:

- Non-kernel and non-executable changes:
  * Modifications to Documentation/, comments, or spelling fixes.
  * User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
    that do not affect the compiled kernel image (vmlinux) or kernel modules.
  * Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
    that do not alter control flow or data structures.
  * Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
  * Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
    ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
  * Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
    physical PCI hot-unplug or manual sysfs driver unbinding.
  * Code paths exclusive to architectures other than the target architecture.

================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
  * Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
    BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
  * Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
  * Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
  * Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).

================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:

- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
  Do NOT list generic, ubiquitous functions called by almost every program in the corpus
  (including, but not limited to: general memory allocators and deallocators, page fault
  and trap handlers, or core synchronization primitives; this is not an exhaustive list).
  Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
  which severely dilutes fuzzing effort away from the actual changes.

- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
  List functions that specifically implement the logic being added or altered, or direct API entrypoints
  for the subsystem feature under review.

- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
  Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
  distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
  If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
  functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).

================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 1fa4c99c058d2ba8ddcbfcc5fff6c778e8fb6097
Author: syz-cluster <triage@syzkaller.com>
Date:   Sat Sep 5 20:47:42 2026 +0000

    syz-cluster: applied patch under review

diff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml
index bdf981781bd5f..fdd3fe9e25470 100644
--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml
+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml
@@ -68,6 +68,14 @@ properties:
       - const: dma-mem # read
       - const: write
 
+  memory-region:
+    items:
+      - description: reference to the video protection memory region
+
+  memory-region-names:
+    items:
+      - const: protected
+
   dma-coherent: true
 
 additionalProperties: false
diff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml
index ce4589466a186..881bfbf4764df 100644
--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml
+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml
@@ -57,6 +57,16 @@ properties:
       - const: dma-mem # read-0
       - const: read-1
 
+  memory-region:
+    minItems: 1
+    maxItems: 2
+
+  memory-region-names:
+    items:
+      enum: [ framebuffer, protected ]
+    minItems: 1
+    maxItems: 2
+
   nvidia,outputs:
     description: A list of phandles of outputs that this display
       controller can drive.
diff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml
index 69be95afd5627..3bf06dcd61986 100644
--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml
+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml
@@ -65,7 +65,15 @@ properties:
     items:
       - description: phandle to the core power domain
 
-  memory-region: true
+  memory-region:
+    minItems: 1
+    maxItems: 2
+
+  memory-region-names:
+    items:
+      enum: [ framebuffer, protected ]
+    minItems: 1
+    maxItems: 2
 
   nvidia,head:
     $ref: /schemas/types.yaml#/definitions/uint32
diff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml
index 8312b7699cbed..420a1fe5ee805 100644
--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml
+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml
@@ -98,6 +98,13 @@ properties:
     items:
       - description: phandle to the HEG or core power domain
 
+  memory-region:
+    maxItems: 1
+
+  memory-region-names:
+    items:
+      - const: protected
+
 required:
   - compatible
   - interrupts
diff --git a/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml b/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml
index 4eb325cfd2961..bcaaabca945dc 100644
--- a/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml
+++ b/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml
@@ -60,6 +60,14 @@ properties:
       - const: dma-mem
       - const: write
 
+  memory-region:
+    items:
+      - description: reference to the video protection memory region
+
+  memory-region-names:
+    items:
+      - const: protected
+
   nvidia,memory-controller:
     $ref: /schemas/types.yaml#/definitions/phandle
     description:
diff --git a/Documentation/devicetree/bindings/reserved-memory/nvidia,tegra-video-protection-region.yaml b/Documentation/devicetree/bindings/reserved-memory/nvidia,tegra-video-protection-region.yaml
new file mode 100644
index 0000000000000..862bfd3913786
--- /dev/null
+++ b/Documentation/devicetree/bindings/reserved-memory/nvidia,tegra-video-protection-region.yaml
@@ -0,0 +1,75 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/reserved-memory/nvidia,tegra-video-protection-region.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: NVIDIA Tegra Video Protection Region (VPR)
+
+maintainers:
+  - Thierry Reding <thierry.reding@gmail.com>
+  - Jon Hunter <jonathanh@nvidia.com>
+
+description: |
+  NVIDIA Tegra chips have long supported a mechanism to protect a single,
+  contiguous memory region from non-secure memory accesses. Typically this
+  region is used for decoding and playback of DRM protected content. Various
+  devices, such as the display controller and multimedia engines (video
+  decoder) can access this region in a secure way. Access from the CPU is
+  generally forbidden.
+
+  Two variants exist for VPR: one is fixed in both the base address and size,
+  while the other is resizable. Fixed VPR can be described by just a "reg"
+  property specifying the base address and size, whereas the resizable VPR
+  is defined by a size/alignment pair of properties. For resizable VPR the
+  memory is reusable by the rest of the system when it's unused for VPR and
+  therefore the "reusable" property must be specified along with it. For a
+  fixed VPR, the memory is permanently protected, and therefore it's not
+  reusable and must also be marked as "no-map" to prevent any (including
+  speculative) accesses to it.
+
+allOf:
+  - $ref: reserved-memory.yaml
+  - oneOf:
+      - required:
+          - compatible
+          - reg
+
+      - required:
+          - compatible
+          - size
+
+properties:
+  compatible:
+    const: nvidia,tegra-video-protection-region
+
+dependencies:
+  size: [alignment, reusable]
+  alignment: [size, reusable]
+  reusable: [alignment, size]
+
+  reg: [no-map]
+  no-map: [reg]
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    /* resizable VPR */
+    protected {
+      compatible = "nvidia,tegra-video-protection-region";
+
+      size = <0x0 0x70000000>;
+      alignment = <0x0 0x100000>;
+      reusable;
+    };
+
+  - |
+    /* fixed VPR */
+    protected@a8000000 {
+      compatible = "nvidia,tegra-video-protection-region";
+
+      /* fixed VPR */
+      reg = <0xa8000000 0x70000000>;
+      no-map;
+    };
diff --git a/arch/arm64/boot/dts/nvidia/tegra234.dtsi b/arch/arm64/boot/dts/nvidia/tegra234.dtsi
index 5e29316a4d759..c6a5ced069e79 100644
--- a/arch/arm64/boot/dts/nvidia/tegra234.dtsi
+++ b/arch/arm64/boot/dts/nvidia/tegra234.dtsi
@@ -29,6 +29,45 @@ aliases {
 		i2c8 = &dp_aux_ch3_i2c;
 	};
 
+	reserved-memory {
+		#address-cells = <2>;
+		#size-cells = <2>;
+		ranges;
+
+		vpr: protected {
+			compatible = "nvidia,tegra-video-protection-region";
+			status = "disabled";
+
+			/*
+			 * Two variants exist for this. For fixed VPR, the
+			 * firmware is supposed to update the "reg" property
+			 * with the fixed memory region configured as VPR.
+			 *
+			 * For resizable VPR we don't care about the exact
+			 * address and instead want a reserved region to be
+			 * allocated with a certain size and alignment at
+			 * boot time.
+			 *
+			 * The below assumes resizable VPR by default. If the
+			 * firmwares sets up fixed VPR, it is responsible for
+			 * adding the missing "reg" property, removing any of
+			 * the unused properties, as well as adding a unit-
+			 * address matching the "reg" property.
+			 */
+
+			/* fixed VPR */
+			/*
+			reg = <0x0 0x0 0x0 0x0>;
+			no-map;
+			*/
+
+			/* resizable VPR */
+			size = <0x0 0x70000000>;
+			alignment = <0x0 0x100000>;
+			reusable;
+		};
+	};
+
 	bus@0 {
 		compatible = "simple-bus";
 
@@ -4440,6 +4479,9 @@ vic@15340000 {
 				interconnect-names = "dma-mem", "write";
 				iommus = <&smmu_niso1 TEGRA234_SID_VIC>;
 				dma-coherent;
+
+				memory-region = <&vpr>;
+				memory-region-names = "protected";
 			};
 
 			nvdec@15480000 {
@@ -4458,6 +4500,9 @@ nvdec@15480000 {
 				iommus = <&smmu_niso1 TEGRA234_SID_NVDEC>;
 				dma-coherent;
 
+				memory-region = <&vpr>;
+				memory-region-names = "protected";
+
 				nvidia,memory-controller = <&mc>;
 
 				/*
diff --git a/arch/arm64/boot/dts/nvidia/tegra264.dtsi b/arch/arm64/boot/dts/nvidia/tegra264.dtsi
index 013228a817237..0123fdc0e1250 100644
--- a/arch/arm64/boot/dts/nvidia/tegra264.dtsi
+++ b/arch/arm64/boot/dts/nvidia/tegra264.dtsi
@@ -23,6 +23,39 @@ shmem_bpmp: shmem@86070000 {
 			reg = <0x0 0x86070000 0x0 0x2000>;
 			no-map;
 		};
+
+		vpr: protected {
+			compatible = "nvidia,tegra-video-protection-region";
+			status = "disabled";
+
+			/*
+			 * Two variants exist for this. For fixed VPR, the
+			 * firmware is supposed to update the "reg" property
+			 * with the fixed memory region configured as VPR.
+			 *
+			 * For resizable VPR we don't care about the exact
+			 * address and instead want a reserved region to be
+			 * allocated with a certain size and alignment at
+			 * boot time.
+			 *
+			 * The below assumes resizable VPR by default. If the
+			 * firmwares sets up fixed VPR, it is responsible for
+			 * adding the missing "reg" property, removing any of
+			 * the unused properties, as well as adding a unit-
+			 * address matching the "reg" property.
+			 */
+
+			/* fixed VPR */
+			/*
+			reg = <0x0 0x0 0x0 0x0>;
+			no-map;
+			*/
+
+			/* resizable VPR */
+			size = <0x0 0x70000000>;
+			alignment = <0x0 0x100000>;
+			reusable;
+		};
 	};
 
 	/* SYSTEM MMIO */
diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c
index 132938b32eb16..9253c529f8aa5 100644
--- a/arch/arm64/mm/pageattr.c
+++ b/arch/arm64/mm/pageattr.c
@@ -262,6 +262,7 @@ int set_direct_map_invalid_noflush(struct page *page, unsigned int numpages)
 	return update_range_prot((unsigned long)page_address(page),
 				 PAGE_SIZE * numpages, set_mask, clear_mask);
 }
+EXPORT_SYMBOL(set_direct_map_invalid_noflush);
 
 int set_direct_map_default_noflush(struct page *page, unsigned int numpages)
 {
@@ -274,6 +275,7 @@ int set_direct_map_default_noflush(struct page *page, unsigned int numpages)
 	return update_range_prot((unsigned long)page_address(page),
 				 PAGE_SIZE * numpages, set_mask, clear_mask);
 }
+EXPORT_SYMBOL(set_direct_map_default_noflush);
 
 static int __set_memory_enc_dec(unsigned long addr,
 				int numpages,
diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c
index 3937dd41bb0f6..f279b8fdd3ce0 100644
--- a/drivers/dma-buf/dma-heap.c
+++ b/drivers/dma-buf/dma-heap.c
@@ -7,6 +7,7 @@
  */
 
 #include <linux/cdev.h>
+#include <linux/debugfs.h>
 #include <linux/device.h>
 #include <linux/dma-buf.h>
 #include <linux/dma-heap.h>
@@ -225,6 +226,40 @@ const char *dma_heap_get_name(struct dma_heap *heap)
 }
 EXPORT_SYMBOL_NS_GPL(dma_heap_get_name, "DMA_BUF_HEAP");
 
+#ifdef CONFIG_DEBUG_FS
+static int dma_heap_debug_show(struct seq_file *s, void *unused)
+{
+	struct dma_heap *heap = s->private;
+	int err = 0;
+
+	if (heap->ops && heap->ops->show)
+		err = heap->ops->show(s, heap);
+
+	return err;
+}
+DEFINE_SHOW_ATTRIBUTE(dma_heap_debug);
+
+static struct dentry *dma_heap_debugfs_dir;
+
+static void dma_heap_init_debugfs(void)
+{
+	dma_heap_debugfs_dir = debugfs_create_dir("dma_heap", NULL);
+}
+
+static void dma_heap_exit_debugfs(void)
+{
+	debugfs_remove_recursive(dma_heap_debugfs_dir);
+}
+#else
+static void dma_heap_init_debugfs(void)
+{
+}
+
+static void dma_heap_exit_debugfs(void)
+{
+}
+#endif
+
 /**
  * dma_heap_add - adds a heap to dmabuf heaps
  * @exp_info: information needed to register this heap
@@ -299,6 +334,13 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
 
 	/* Add heap to the list */
 	list_add(&heap->list, &heap_list);
+
+#ifdef CONFIG_DEBUG_FS
+	if (heap->ops && heap->ops->show)
+		debugfs_create_file(heap->name, 0444, dma_heap_debugfs_dir,
+				    heap, &dma_heap_debug_fops);
+#endif
+
 	mutex_unlock(&heap_list_lock);
 
 	return heap;
@@ -335,6 +377,16 @@ static int dma_heap_init(void)
 	}
 	dma_heap_class->devnode = dma_heap_devnode;
 
+	dma_heap_init_debugfs();
+
 	return 0;
 }
 subsys_initcall(dma_heap_init);
+
+static void __exit dma_heap_exit(void)
+{
+	dma_heap_exit_debugfs();
+	class_destroy(dma_heap_class);
+	unregister_chrdev_region(dma_heap_devt, NUM_HEAP_MINORS);
+}
+__exitcall(dma_heap_exit);
diff --git a/drivers/dma-buf/heaps/Kconfig b/drivers/dma-buf/heaps/Kconfig
index bb729e91545c4..28d2c0800fb57 100644
--- a/drivers/dma-buf/heaps/Kconfig
+++ b/drivers/dma-buf/heaps/Kconfig
@@ -20,3 +20,15 @@ config DMABUF_HEAPS_CMA
 	  Choose this option to enable dma-buf CMA heap. This heap is backed
 	  by the Contiguous Memory Allocator (CMA). If your system has these
 	  regions, you should say Y here.
+
+config DMABUF_HEAPS_TEGRA_VPR
+	tristate "NVIDIA Tegra Video-Protected-Region DMA-BUF Heap"
+	depends on DMABUF_HEAPS && DMA_CMA
+	help
+	  Choose this option to enable Video-Protected-Region (VPR) support on
+	  a range of NVIDIA Tegra devices. Access to VPR memory is limited to
+	  a subset of hardware engines and specifically disallowed from the
+	  CPU. The region can be fixed, in which case no linear mapping exists
+	  for the memory, or it can be resizable on systems that want to reuse
+	  the memory for other uses when content-protected video is not played
+	  back.
diff --git a/drivers/dma-buf/heaps/Makefile b/drivers/dma-buf/heaps/Makefile
index 974467791032f..481fcb78f7572 100644
--- a/drivers/dma-buf/heaps/Makefile
+++ b/drivers/dma-buf/heaps/Makefile
@@ -1,3 +1,13 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_DMABUF_HEAPS_SYSTEM)	+= system_heap.o
 obj-$(CONFIG_DMABUF_HEAPS_CMA)		+= cma_heap.o
+
+#
+# The reserved-memory bits always need to be built-in so that the CMA
+# initialization runs during early boot. The VPR driver itself can be
+# built as a module.
+#
+ifneq ($(CONFIG_DMABUF_HEAPS_TEGRA_VPR),)
+obj-y					+= tegra-vpr-init.o
+obj-$(CONFIG_DMABUF_HEAPS_TEGRA_VPR)	+= tegra-vpr.o
+endif
diff --git a/drivers/dma-buf/heaps/tegra-vpr-init.c b/drivers/dma-buf/heaps/tegra-vpr-init.c
new file mode 100644
index 0000000000000..65e917713c6d4
--- /dev/null
+++ b/drivers/dma-buf/heaps/tegra-vpr-init.c
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DMA-BUF restricted heap exporter for NVIDIA Video-Protection-Region (VPR)
+ *
+ * Copyright (C) 2024-2026 NVIDIA Corporation
+ */
+
+#define pr_fmt(fmt) "tegra-vpr: " fmt
+
+#include "tegra-vpr.h"
+
+#include <linux/cma.h>
+#include <linux/of_reserved_mem.h>
+
+static DEFINE_MUTEX(vpr_lock);
+static LIST_HEAD_GUARDED(vpr_list, vpr_lock);
+
+static int __init tegra_vpr_node_init(unsigned long offset,
+				      struct reserved_mem *rmem)
+{
+	struct cma *cma;
+	int err;
+
+	if (!IS_ALIGNED(rmem->base, SZ_1M)) {
+		pr_err("%s: base is not aligned to 1 MiB\n", rmem->name);
+		return -EINVAL;
+	}
+
+	if (!IS_ALIGNED(rmem->size, SZ_1M)) {
+		pr_err("%s: size is not aligned to 1 MiB\n", rmem->name);
+		return -EINVAL;
+	}
+
+	err = cma_init_reserved_mem(rmem->base, rmem->size, 0, rmem->name,
+				    &cma);
+	if (err < 0) {
+		pr_err("%s: failed to initialize CMA: %d\n", __func__, err);
+		return err;
+	}
+
+	rmem->priv = cma;
+
+	return 0;
+}
+
+static struct tegra_vpr *tegra_vpr_lookup(struct cma *cma)
+{
+	struct tegra_vpr *vpr;
+
+	mutex_lock(&vpr_lock);
+
+	list_for_each_entry(vpr, &vpr_list, list) {
+		if (vpr->cma == cma) {
+			mutex_unlock(&vpr_lock);
+			return vpr;
+		}
+	}
+
+	mutex_unlock(&vpr_lock);
+
+	return ERR_PTR(-EPROBE_DEFER);
+}
+
+static int tegra_vpr_device_init(struct reserved_mem *rmem, struct device *dev)
+{
+	const struct dev_pm_ops *pm = dev->driver->pm;
+	struct tegra_vpr_device *node;
+	struct cma *cma = rmem->priv;
+	struct tegra_vpr *vpr;
+
+	vpr = tegra_vpr_lookup(cma);
+	if (IS_ERR(vpr))
+		return PTR_ERR(vpr);
+
+	if (!pm || !pm->freeze || !pm->thaw)
+		return -EINVAL;
+
+	node = kzalloc_obj(*node, GFP_KERNEL);
+	if (!node)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&node->node);
+	node->dev = dev;
+
+	mutex_lock(&vpr->lock);
+	list_add_tail(&node->node, &vpr->devices);
+	mutex_unlock(&vpr->lock);
+
+	return 0;
+}
+
+static void tegra_vpr_device_release(struct reserved_mem *rmem,
+				     struct device *dev)
+{
+	struct tegra_vpr_device *node, *tmp;
+	struct cma *cma = rmem->priv;
+	struct tegra_vpr *vpr;
+
+	vpr = tegra_vpr_lookup(cma);
+	if (IS_ERR(vpr)) {
+		dev_WARN(dev, "failed to find VPR for CMA '%s'\n",
+			 cma_get_name(cma));
+		return;
+	}
+
+	mutex_lock(&vpr->lock);
+
+	list_for_each_entry_safe(node, tmp, &vpr->devices, node) {
+		if (node->dev == dev) {
+			list_del(&node->node);
+			kfree(node);
+		}
+	}
+
+	mutex_unlock(&vpr->lock);
+}
+
+static const struct reserved_mem_ops tegra_vpr_rmem_ops = {
+	.node_init = tegra_vpr_node_init,
+	.device_init = tegra_vpr_device_init,
+	.device_release = tegra_vpr_device_release,
+};
+
+RESERVEDMEM_OF_DECLARE(tegra_vpr, "nvidia,tegra-video-protection-region",
+		       &tegra_vpr_rmem_ops);
+
+void tegra_vpr_add(struct tegra_vpr *vpr)
+{
+	mutex_lock(&vpr_lock);
+	list_add_tail(&vpr->list, &vpr_list);
+	mutex_unlock(&vpr_lock);
+}
+EXPORT_SYMBOL(tegra_vpr_add);
diff --git a/drivers/dma-buf/heaps/tegra-vpr.c b/drivers/dma-buf/heaps/tegra-vpr.c
new file mode 100644
index 0000000000000..d8cff7e07ea0b
--- /dev/null
+++ b/drivers/dma-buf/heaps/tegra-vpr.c
@@ -0,0 +1,1210 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DMA-BUF restricted heap exporter for NVIDIA Video-Protection-Region (VPR)
+ *
+ * Copyright (C) 2024-2026 NVIDIA Corporation
+ */
+
+#define pr_fmt(fmt) "tegra-vpr: " fmt
+
+#include <linux/arm-smccc.h>
+#include <linux/cma.h>
+#include <linux/debugfs.h>
+#include <linux/dma-buf.h>
+#include <linux/dma-heap.h>
+#include <linux/find.h>
+#include <linux/memory.h>
+#include <linux/of_reserved_mem.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/reset.h>
+#include <linux/set_memory.h>
+
+#include "tegra-vpr.h"
+
+MODULE_IMPORT_NS("DMA_BUF_HEAP");
+MODULE_IMPORT_NS("DMA_BUF");
+
+#define CREATE_TRACE_POINTS
+#include <trace/events/tegra_vpr.h>
+
+#define TEGRA_VPR_MAX_CHUNKS 64
+
+struct tegra_vpr_buffer {
+	struct list_head attachments;
+	struct tegra_vpr *vpr;
+	struct list_head list;
+
+	/**
+	 * @lock: Protects concurrent access to the list of attachments.
+	 */
+	struct mutex lock;
+
+	struct page **pages;
+	pgoff_t num_pages;
+	phys_addr_t start;
+	phys_addr_t limit;
+	size_t size;
+	int pageno;
+	int order;
+
+	DECLARE_BITMAP(chunks, TEGRA_VPR_MAX_CHUNKS);
+};
+
+struct tegra_vpr_attachment {
+	struct device *dev;
+	struct sg_table sgt;
+	struct list_head list;
+};
+
+#define ARM_SMCCC_TE_FUNC_PROGRAM_VPR 0x3
+
+#define ARM_SMCCC_VENDOR_SIP_TE_PROGRAM_VPR_FUNC_ID		\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,			\
+			   ARM_SMCCC_SMC_32,			\
+			   ARM_SMCCC_OWNER_SIP,			\
+			   ARM_SMCCC_TE_FUNC_PROGRAM_VPR)
+
+static int tegra_vpr_set(phys_addr_t base, phys_addr_t size)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_smc(ARM_SMCCC_VENDOR_SIP_TE_PROGRAM_VPR_FUNC_ID, base, size,
+		      0, 0, 0, 0, 0, &res);
+
+	return res.a0;
+}
+
+static int tegra_vpr_get_extents(struct tegra_vpr *vpr, phys_addr_t *base,
+				 phys_addr_t *size)
+{
+	phys_addr_t start = ~0, limit = 0;
+	unsigned int i;
+
+	for (i = 0; i < vpr->num_chunks; i++) {
+		struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+		if (chunk->active) {
+			if (chunk->start < start)
+				start = chunk->start;
+
+			if (chunk->limit > limit)
+				limit = chunk->limit;
+		}
+	}
+
+	if (limit > start) {
+		*size = limit - start;
+		*base = start;
+	} else {
+		*base = *size = 0;
+	}
+
+	return 0;
+}
+
+static int tegra_vpr_resize(struct tegra_vpr *vpr)
+{
+	struct tegra_vpr_device *node;
+	phys_addr_t base, size;
+	int err, status = 0;
+
+	err = tegra_vpr_get_extents(vpr, &base, &size);
+	if (err < 0) {
+		pr_err("%s(): failed to get VPR extents: %d\n", __func__, err);
+		return err;
+	}
+
+	list_for_each_entry(node, &vpr->devices, node) {
+		err = pm_generic_freeze(node->dev);
+		if (err < 0) {
+			pr_err("failed to freeze %s: %d\n",
+			       dev_name(node->dev), err);
+			status = err;
+			goto thaw;
+		}
+	}
+
+	trace_tegra_vpr_set(base, size);
+
+	err = tegra_vpr_set(base, size);
+	if (err < 0) {
+		pr_err("failed to secure VPR: %d\n", err);
+		status = err;
+	}
+
+thaw:
+	list_for_each_entry_continue_reverse(node, &vpr->devices, node) {
+		err = pm_generic_thaw(node->dev);
+		if (err < 0) {
+			pr_err("failed to thaw %s: %d\n",
+			       dev_name(node->dev), err);
+			continue;
+		}
+	}
+
+	return status;
+}
+
+static int tegra_vpr_chunk_init(struct tegra_vpr *vpr,
+				struct tegra_vpr_chunk *chunk,
+				phys_addr_t start, size_t size,
+				unsigned int order, const char *name)
+{
+	chunk->start = start;
+	chunk->limit = start + size;
+	chunk->size = size;
+	chunk->vpr = vpr;
+
+	chunk->offset = (start - vpr->base) >> PAGE_SHIFT;
+	chunk->num_pages = size >> PAGE_SHIFT;
+	chunk->num_buffers = 0;
+
+	/* CMA area is not reserved yet */
+	chunk->start_page = NULL;
+	chunk->virt = 0;
+
+	return 0;
+}
+
+static void tegra_vpr_chunk_free(struct tegra_vpr_chunk *chunk)
+{
+}
+
+static inline bool tegra_vpr_chunk_is_last(const struct tegra_vpr_chunk *chunk)
+{
+	phys_addr_t limit = chunk->vpr->base + chunk->vpr->size;
+
+	return chunk->limit == limit;
+}
+
+static inline bool tegra_vpr_chunk_is_leaf(const struct tegra_vpr_chunk *chunk)
+{
+	const struct tegra_vpr_chunk *next = chunk + 1;
+
+	if (tegra_vpr_chunk_is_last(chunk))
+		return true;
+
+	return !next->active;
+}
+
+static int tegra_vpr_chunk_alloc(struct tegra_vpr_chunk *chunk)
+{
+	chunk->start_page = cma_alloc_at(chunk->vpr->cma, chunk->offset,
+					 chunk->num_pages, false);
+	if (!chunk->start_page)
+		return -ENOMEM;
+
+	chunk->virt = (unsigned long)page_to_virt(chunk->start_page);
+
+	return 0;
+}
+
+static int tegra_vpr_chunk_activate(struct tegra_vpr_chunk *chunk)
+{
+	int err;
+
+	trace_tegra_vpr_chunk_activate(chunk->start, chunk->limit);
+
+	err = set_direct_map_invalid_noflush(chunk->start_page,
+					     chunk->num_pages);
+	if (err)
+		return err;
+
+	flush_tlb_kernel_range(chunk->virt, chunk->virt + chunk->size);
+	chunk->invalid = false;
+	chunk->active = true;
+
+	return 0;
+}
+
+static int tegra_vpr_chunk_deactivate(struct tegra_vpr_chunk *chunk)
+{
+	int err;
+
+	if (!chunk->active)
+		return 0;
+
+	/* do not deactivate if there are buffers left in this chunk */
+	if (WARN_ON(chunk->num_buffers > 0))
+		return -EBUSY;
+
+	trace_tegra_vpr_chunk_deactivate(chunk->start, chunk->limit);
+
+	err = set_direct_map_default_noflush(chunk->start_page,
+					     chunk->num_pages);
+	if (err)
+		return err;
+
+	flush_tlb_kernel_range(chunk->virt, chunk->virt + chunk->size);
+	chunk->invalid = false;
+	chunk->active = false;
+
+	return 0;
+}
+
+static void tegra_vpr_chunk_release(struct tegra_vpr_chunk *chunk)
+{
+	if (!WARN_ON(chunk->active || chunk->invalid)) {
+		cma_release(chunk->vpr->cma, chunk->start_page,
+			    chunk->num_pages);
+		chunk->start_page = NULL;
+		chunk->virt = 0;
+	}
+}
+
+static bool tegra_vpr_chunk_overlaps(struct tegra_vpr_chunk *chunk,
+				     unsigned int start, unsigned int limit)
+{
+	unsigned int first = chunk->offset;
+	unsigned int last = chunk->offset + chunk->num_pages - 1;
+
+	if (last < start || first >= limit)
+		return false;
+
+	return true;
+}
+
+static int tegra_vpr_activate_chunks(struct tegra_vpr *vpr,
+				     struct tegra_vpr_buffer *buffer)
+{
+	DECLARE_BITMAP(dirty, vpr->num_chunks);
+	unsigned int i, bottom, top;
+	int err = 0, ret;
+
+	bitmap_zero(dirty, vpr->num_chunks);
+
+	/* activate any inactive chunks that overlap this buffer */
+	for_each_set_bit(i, buffer->chunks, vpr->num_chunks) {
+		struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+		if (chunk->active)
+			continue;
+
+		err = tegra_vpr_chunk_alloc(chunk);
+		if (err < 0)
+			goto deactivate;
+
+		err = tegra_vpr_chunk_activate(chunk);
+		if (err < 0) {
+			tegra_vpr_chunk_release(chunk);
+			goto deactivate;
+		}
+
+		set_bit(i, vpr->active);
+		set_bit(i, dirty);
+	}
+
+	/*
+	 * Activating chunks above may have created holes, but since the VPR
+	 * can only ever be a single contiguous region, make sure to activate
+	 * any missing chunks.
+	 */
+	for_each_clear_bitrange(bottom, top, vpr->active, vpr->num_chunks) {
+		/* inactive chunks at the bottom or the top are harmless */
+		if (bottom == 0 || top == vpr->num_chunks)
+			continue;
+
+		for (i = bottom; i < top; i++) {
+			struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+			err = tegra_vpr_chunk_alloc(chunk);
+			if (err < 0)
+				goto deactivate;
+
+			err = tegra_vpr_chunk_activate(chunk);
+			if (err < 0) {
+				tegra_vpr_chunk_release(chunk);
+				goto deactivate;
+			}
+
+			set_bit(i, vpr->active);
+			set_bit(i, dirty);
+		}
+	}
+
+	/* if any chunks have been activated, VPR needs to be resized */
+	if (!bitmap_empty(dirty, vpr->num_chunks)) {
+		err = tegra_vpr_resize(vpr);
+		if (err < 0) {
+			pr_err("failed to grow VPR: %d\n", err);
+			goto deactivate;
+		}
+	}
+
+	/* increment buffer count for each chunk */
+	for_each_set_bit(i, buffer->chunks, vpr->num_chunks)
+		vpr->chunks[i].num_buffers++;
+
+	return 0;
+
+deactivate:
+	/* deactivate any of the previously inactive chunks on failure */
+	for_each_set_bit(i, dirty, vpr->num_chunks) {
+		struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+		ret = tegra_vpr_chunk_deactivate(chunk);
+		if (WARN_ON(ret < 0)) {
+			pr_err("failed to deactivate chunk #%u: %d\n", i, ret);
+		} else {
+			tegra_vpr_chunk_release(chunk);
+			clear_bit(i, vpr->active);
+		}
+	}
+
+	return err;
+}
+
+/*
+ * Retrieve the range of pages within the activate region of the VPR.
+ */
+static bool tegra_vpr_get_active_range(struct tegra_vpr *vpr,
+				       unsigned int *first,
+				       unsigned int *last)
+{
+	unsigned long i, j;
+
+	i = find_first_bit(vpr->active, vpr->num_chunks);
+	if (i >= vpr->num_chunks)
+		return false;
+
+	j = find_last_bit(vpr->active, vpr->num_chunks);
+	if (j >= vpr->num_chunks)
+		return false;
+
+	*first = vpr->chunks[i].offset;
+	*last = vpr->chunks[j].offset + vpr->chunks[j].num_pages;
+
+	return true;
+}
+
+/*
+ * Try to find and allocate a free region within a specific page range.
+ * Returns the page number if successful, -ENOSPC otherwise.
+ *
+ * This function mimics bitmap_find_free_region() but restricts the search
+ * to a specific range to enable allocation within individual chunks.
+ */
+static int tegra_vpr_find_free_region_in_range(struct tegra_vpr *vpr,
+					       unsigned int start_page,
+					       unsigned int end_page,
+					       unsigned int num_pages,
+					       unsigned int align)
+{
+	unsigned int pos, next = ALIGN(start_page, align);
+
+	/* Scan through aligned positions, trying to allocate at each one */
+	for (pos = next; pos + num_pages <= end_page; pos = next) {
+		next = find_next_bit(vpr->bitmap, pos + num_pages, pos);
+
+		if (next >= pos + num_pages) {
+			bitmap_set(vpr->bitmap, pos, num_pages);
+			return pos;
+		}
+
+		next = find_next_zero_bit(vpr->bitmap, vpr->num_pages, next);
+		next = ALIGN(next, align);
+	}
+
+	return -ENOSPC;
+}
+
+static int tegra_vpr_find_free_region(struct tegra_vpr *vpr,
+				      unsigned int num_pages,
+				      unsigned long align)
+{
+	return tegra_vpr_find_free_region_in_range(vpr, 0, vpr->num_pages - 1,
+						   num_pages, align);
+}
+
+static int tegra_vpr_find_free_region_clustered(struct tegra_vpr *vpr,
+						unsigned int num_pages,
+						unsigned int align)
+{
+	unsigned int target, first, last;
+	int pageno;
+
+	/*
+	 * If there are no allocations, abort the clustered allocation scheme
+	 * and use the generic allocation scheme instead.
+	 */
+	if (vpr->first > vpr->last)
+		return -ENOSPC;
+
+	/*
+	 * First, try to allocate within the currently allocated region. This
+	 * keeps allocations tightly packed and minimizes the VPR size needed.
+	 */
+	pageno = tegra_vpr_find_free_region_in_range(vpr, vpr->first,
+						     vpr->last + 1, num_pages,
+						     align);
+	if (pageno >= 0)
+		return pageno;
+
+	/*
+	 * If not enough free space exists within the currently allocated
+	 * region, check to see if the allocation fits anywhere within the
+	 * active region, avoiding the need to resize the VPR.
+	 */
+	if (tegra_vpr_get_active_range(vpr, &first, &last)) {
+		pageno = tegra_vpr_find_free_region_in_range(vpr, first, last,
+							     num_pages, align);
+		if (pageno >= 0)
+			return pageno;
+	}
+
+	/*
+	 * If not enough free space exists within the currently active region,
+	 * try to allocate adjacent to it to grow it contiguously and ensure
+	 * optimal packing.
+	 */
+
+	/*
+	 * Calculate where the allocation should start to end right at the
+	 * first allocated page, with proper alignment.
+	 */
+	if (vpr->first >= num_pages) {
+		target = ALIGN_DOWN(vpr->first - num_pages, align);
+
+		if (!bitmap_allocate(vpr->bitmap, target, num_pages))
+			return target;
+	}
+
+	/* Try after the last allocation */
+	target = ALIGN(vpr->last + 1, align);
+
+	if (target + num_pages <= vpr->num_pages &&
+	    !bitmap_allocate(vpr->bitmap, target, num_pages))
+		return target;
+
+	/*
+	 * Couldn't allocate at the ideal adjacent position, search for any
+	 * available space before the first allocated page.
+	 */
+	pageno = tegra_vpr_find_free_region_in_range(vpr, 0, vpr->first,
+						     num_pages, align);
+	if (pageno >= 0)
+		return pageno;
+
+	/*
+	 * Couldn't allocate at the ideal adjacent position, search
+	 * for any available space after the last allocated page.
+	 */
+	pageno = tegra_vpr_find_free_region_in_range(vpr, vpr->last + 1,
+						     vpr->num_pages, num_pages,
+						     align);
+	if (pageno >= 0)
+		return pageno;
+
+	return -ENOSPC;
+}
+
+/*
+ * Find a free region, preferring locations near existing allocations to
+ * minimize VPR fragmentation. The allocation strategy is to first allocate
+ * within or adjacent to the existing region to keep allocations clustered.
+ * Otherwise fall back to a generic allocation using the first available
+ * space.
+ *
+ * This approach focuses on page-level allocation first, then the chunk
+ * system determines which chunks need to be activated based on where the
+ * pages ended up.
+ */
+static int tegra_vpr_allocate_region(struct tegra_vpr *vpr,
+				     unsigned int num_pages,
+				     unsigned int align)
+{
+	int pageno;
+
+	/*
+	 * For non-resizable VPR (no chunks), use simple first-fit allocation.
+	 * Clustering optimization is only beneficial for resizable VPR where
+	 * keeping allocations together minimizes the active VPR size.
+	 */
+	if (!vpr->resizable)
+		return tegra_vpr_find_free_region(vpr, num_pages, align);
+
+	/*
+	 * Check if there are any existing allocations in the bitmap. If so,
+	 * try to allocate near them to minimize fragmentation.
+	 */
+	pageno = tegra_vpr_find_free_region_clustered(vpr, num_pages, align);
+	if (pageno >= 0)
+		return pageno;
+
+	/*
+	 * If there are no existing allocations, or no space adjacent to them,
+	 * fall back to the first available space anywhere in the VPR.
+	 */
+	pageno = tegra_vpr_find_free_region(vpr, num_pages, align);
+	if (pageno >= 0)
+		return pageno;
+
+	return -ENOSPC;
+}
+
+static struct tegra_vpr_buffer *
+tegra_vpr_buffer_allocate(struct tegra_vpr *vpr, size_t size)
+{
+	unsigned int num_pages = size >> PAGE_SHIFT;
+	unsigned int order = get_order(size);
+	struct tegra_vpr_buffer *buffer;
+	unsigned long first, last;
+	int pageno, err;
+
+	/*
+	 * Quick sanity check that we're not trying to allocate a buffer that
+	 * has no chance of fitting into the VPR.
+	 */
+	if (size > vpr->size)
+		return ERR_PTR(-EINVAL);
+
+	/*
+	 * "order" defines the alignment and size, so this may result in
+	 * fragmented memory depending on the allocation patterns. However,
+	 * since this is used primarily for video frames, it is expected that
+	 * a number of buffers of the same size will be allocated, so
+	 * fragmentation should be negligible.
+	 */
+	pageno = tegra_vpr_allocate_region(vpr, num_pages, 1);
+	if (pageno < 0)
+		return ERR_PTR(pageno);
+
+	first = find_first_bit(vpr->bitmap, vpr->num_pages);
+	last = find_last_bit(vpr->bitmap, vpr->num_pages);
+
+	buffer = kzalloc_obj(*buffer, GFP_KERNEL);
+	if (!buffer) {
+		err = -ENOMEM;
+		goto release;
+	}
+
+	INIT_LIST_HEAD(&buffer->attachments);
+	INIT_LIST_HEAD(&buffer->list);
+	mutex_init(&buffer->lock);
+	buffer->start = vpr->base + (pageno << PAGE_SHIFT);
+	buffer->limit = buffer->start + size;
+	buffer->size = size;
+	buffer->num_pages = num_pages;
+	buffer->pageno = pageno;
+	buffer->order = order;
+
+	/* track which chunks this buffer overlaps */
+	if (vpr->resizable) {
+		unsigned int limit = buffer->pageno + buffer->num_pages;
+		pgoff_t i;
+
+		/*
+		 * Memory is backed by struct page, so track which ones we
+		 * use.
+		 */
+		buffer->pages = kvmalloc_array(buffer->num_pages,
+					       sizeof(*buffer->pages),
+					       GFP_KERNEL);
+		if (!buffer->pages) {
+			err = -ENOMEM;
+			goto free;
+		}
+
+		for (i = 0; i < buffer->num_pages; i++)
+			buffer->pages[i] = &vpr->start_page[pageno + i];
+
+		for (i = 0; i < vpr->num_chunks; i++) {
+			struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+			if (tegra_vpr_chunk_overlaps(chunk, pageno, limit))
+				set_bit(i, buffer->chunks);
+		}
+
+		/* activate chunks if necessary */
+		err = tegra_vpr_activate_chunks(vpr, buffer);
+		if (err < 0) {
+			kfree(buffer->pages);
+			goto free;
+		}
+
+		/* track first and last allocated pages */
+		if (buffer->pageno < vpr->first)
+			vpr->first = buffer->pageno;
+
+		if (limit - 1 > vpr->last)
+			vpr->last = limit - 1;
+	}
+
+	return buffer;
+
+free:
+	kfree(buffer);
+release:
+	bitmap_clear(vpr->bitmap, pageno, num_pages);
+	return ERR_PTR(err);
+}
+
+static void tegra_vpr_buffer_release(struct tegra_vpr_buffer *buffer)
+{
+	struct tegra_vpr *vpr = buffer->vpr;
+	struct tegra_vpr_buffer *entry;
+	unsigned int i;
+
+	/*
+	 * Decrement buffer count for each overlapping chunk. Note that chunks
+	 * are not deactivated here yet, that's done in tegra_vpr_recycle()
+	 * instead.
+	 */
+	for_each_set_bit(i, buffer->chunks, vpr->num_chunks) {
+		if (!WARN_ON(vpr->chunks[i].num_buffers == 0))
+			vpr->chunks[i].num_buffers--;
+	}
+
+	/* track first and last allocated pages */
+	if (list_is_first(&buffer->list, &vpr->buffers) &&
+	    list_is_last(&buffer->list, &vpr->buffers)) {
+		/* if there are no remaining buffers after this, reset */
+		vpr->first = ~0U;
+		vpr->last = 0U;
+	} else if (list_is_first(&buffer->list, &vpr->buffers)) {
+		entry = list_next_entry(buffer, list);
+		vpr->first = entry->pageno;
+	} else if (list_is_last(&buffer->list, &vpr->buffers)) {
+		entry = list_prev_entry(buffer, list);
+		vpr->last = entry->pageno + entry->num_pages - 1;
+	}
+
+	bitmap_clear(vpr->bitmap, buffer->pageno, buffer->num_pages);
+	list_del(&buffer->list);
+	kfree(buffer->pages);
+	kfree(buffer);
+}
+
+static int tegra_vpr_attach(struct dma_buf *buf,
+			    struct dma_buf_attachment *attachment)
+{
+	struct tegra_vpr_buffer *buffer = buf->priv;
+	struct tegra_vpr_attachment *attach;
+	int err;
+
+	attach = kzalloc_obj(*attach, GFP_KERNEL);
+	if (!attach)
+		return -ENOMEM;
+
+	/*
+	 * For resizable VPR, the memory is backed by struct page, so we can
+	 * use the convenient helper to create the SG table.
+	 */
+	if (buffer->pages) {
+		err = sg_alloc_table_from_pages(&attach->sgt, buffer->pages,
+						buffer->num_pages, 0,
+						buffer->size, GFP_KERNEL);
+		if (err < 0)
+			goto free;
+	} else {
+		if (sg_alloc_table(&attach->sgt, 1, GFP_KERNEL)) {
+			err = -ENOMEM;
+			goto free;
+		}
+
+		sg_set_page(attach->sgt.sgl, NULL, buffer->size, 0);
+		sg_dma_address(attach->sgt.sgl) = buffer->start;
+		sg_dma_len(attach->sgt.sgl) = buffer->size;
+	}
+
+	attach->dev = attachment->dev;
+	INIT_LIST_HEAD(&attach->list);
+	attachment->priv = attach;
+
+	mutex_lock(&buffer->lock);
+	list_add(&attach->list, &buffer->attachments);
+	mutex_unlock(&buffer->lock);
+
+	return 0;
+
+free:
+	kfree(attach);
+	return err;
+}
+
+static void tegra_vpr_detach(struct dma_buf *buf,
+			     struct dma_buf_attachment *attachment)
+{
+	struct tegra_vpr_buffer *buffer = buf->priv;
+	struct tegra_vpr_attachment *attach = attachment->priv;
+
+	mutex_lock(&buffer->lock);
+	list_del(&attach->list);
+	mutex_unlock(&buffer->lock);
+
+	sg_free_table(&attach->sgt);
+	kfree(attach);
+}
+
+static struct sg_table *
+tegra_vpr_map_dma_buf(struct dma_buf_attachment *attachment,
+		      enum dma_data_direction direction)
+{
+	struct tegra_vpr_attachment *attach = attachment->priv;
+	struct sg_table *sgt = &attach->sgt;
+	int err;
+
+	err = dma_map_sgtable(attachment->dev, sgt, direction,
+			      DMA_ATTR_SKIP_CPU_SYNC);
+	if (err < 0)
+		return ERR_PTR(err);
+
+	return sgt;
+}
+
+static void tegra_vpr_unmap_dma_buf(struct dma_buf_attachment *attachment,
+				    struct sg_table *sgt,
+				    enum dma_data_direction direction)
+{
+	dma_unmap_sgtable(attachment->dev, sgt, direction,
+			  DMA_ATTR_SKIP_CPU_SYNC);
+}
+
+static void tegra_vpr_recycle(struct tegra_vpr *vpr)
+{
+	DECLARE_BITMAP(dirty, vpr->num_chunks);
+	unsigned int i;
+	int err;
+
+	if (!vpr->resizable)
+		return;
+
+	bitmap_zero(dirty, vpr->num_chunks);
+
+	/*
+	 * Deactivate any unused chunks from the bottom...
+	 */
+	for (i = 0; i < vpr->num_chunks; i++) {
+		struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+		if (!chunk->active)
+			continue;
+
+		if (chunk->num_buffers > 0)
+			break;
+
+		err = tegra_vpr_chunk_deactivate(chunk);
+		if (err < 0) {
+			pr_err("failed to deactivate chunk #%u: %d\n", i, err);
+			goto activate;
+		} else {
+			clear_bit(i, vpr->active);
+			set_bit(i, dirty);
+		}
+	}
+
+	/*
+	 * ... and the top.
+	 */
+	for (i = 0; i < vpr->num_chunks; i++) {
+		unsigned int index = vpr->num_chunks - i - 1;
+		struct tegra_vpr_chunk *chunk = &vpr->chunks[index];
+
+		if (!chunk->active)
+			continue;
+
+		if (chunk->num_buffers > 0)
+			break;
+
+		err = tegra_vpr_chunk_deactivate(chunk);
+		if (err < 0) {
+			pr_err("failed to deactivate chunk #%u: %d\n", index,
+			       err);
+			goto activate;
+		} else {
+			clear_bit(index, vpr->active);
+			set_bit(index, dirty);
+		}
+	}
+
+	if (!bitmap_empty(dirty, vpr->num_chunks)) {
+		err = tegra_vpr_resize(vpr);
+		if (err < 0) {
+			pr_err("failed to shrink VPR: %d\n", err);
+			goto activate;
+		}
+	}
+
+	/* release the CMA memory associated with deactivated chunks */
+	for_each_set_bit(i, dirty, vpr->num_chunks)
+		tegra_vpr_chunk_release(&vpr->chunks[i]);
+
+	return;
+
+activate:
+	for_each_set_bit(i, dirty, vpr->num_chunks) {
+		err = tegra_vpr_chunk_activate(&vpr->chunks[i]);
+		if (WARN_ON(err < 0))
+			pr_err("failed to activate chunk #%u: %d\n", i, err);
+
+		/*
+		 * This may not be fully activated at this point, but we need
+		 * to keep track of it anyway to make sure the CMA region can
+		 * eventually be released. The WARN_ON above tells us when it
+		 * happens: here be dragons.
+		 */
+		set_bit(i, vpr->active);
+	}
+}
+
+static void tegra_vpr_release(struct dma_buf *buf)
+{
+	struct tegra_vpr_buffer *buffer = buf->priv;
+	struct tegra_vpr *vpr = buffer->vpr;
+
+	mutex_lock(&vpr->lock);
+
+	tegra_vpr_buffer_release(buffer);
+	tegra_vpr_recycle(vpr);
+
+	mutex_unlock(&vpr->lock);
+}
+
+/*
+ * Prohibit userspace mapping because the CPU cannot access this memory
+ * anyway.
+ */
+static int tegra_vpr_begin_cpu_access(struct dma_buf *buf,
+				      enum dma_data_direction direction)
+{
+	return -EPERM;
+}
+
+static int tegra_vpr_end_cpu_access(struct dma_buf *buf,
+				    enum dma_data_direction direction)
+{
+	return -EPERM;
+}
+
+static int tegra_vpr_mmap(struct dma_buf *buf, struct vm_area_struct *vma)
+{
+	return -EPERM;
+}
+
+static const struct dma_buf_ops tegra_vpr_buf_ops = {
+	.attach = tegra_vpr_attach,
+	.detach = tegra_vpr_detach,
+	.map_dma_buf = tegra_vpr_map_dma_buf,
+	.unmap_dma_buf = tegra_vpr_unmap_dma_buf,
+	.release = tegra_vpr_release,
+	.begin_cpu_access = tegra_vpr_begin_cpu_access,
+	.end_cpu_access = tegra_vpr_end_cpu_access,
+	.mmap = tegra_vpr_mmap,
+};
+
+static struct dma_buf *tegra_vpr_allocate(struct dma_heap *heap,
+					  unsigned long len, u32 fd_flags,
+					  u64 heap_flags)
+{
+	struct tegra_vpr *vpr = dma_heap_get_drvdata(heap);
+	struct tegra_vpr_buffer *buffer, *entry;
+	size_t size = ALIGN(len, vpr->align);
+	DEFINE_DMA_BUF_EXPORT_INFO(export);
+	struct dma_buf *buf;
+
+	mutex_lock(&vpr->lock);
+
+	buffer = tegra_vpr_buffer_allocate(vpr, size);
+	if (IS_ERR(buffer)) {
+		mutex_unlock(&vpr->lock);
+		return ERR_CAST(buffer);
+	}
+
+	/* insert in the correct order */
+	if (!list_empty(&vpr->buffers)) {
+		list_for_each_entry(entry, &vpr->buffers, list) {
+			if (buffer->pageno < entry->pageno) {
+				list_add_tail(&buffer->list, &entry->list);
+				break;
+			}
+		}
+	}
+
+	if (list_empty(&buffer->list))
+		list_add_tail(&buffer->list, &vpr->buffers);
+
+	buffer->vpr = vpr;
+
+	/*
+	 * If a valid buffer was allocated, wrap it in a dma_buf
+	 * and return it.
+	 */
+	export.exp_name = dma_heap_get_name(heap);
+	export.ops = &tegra_vpr_buf_ops;
+	export.size = buffer->size;
+	export.flags = fd_flags;
+	export.priv = buffer;
+
+	buf = dma_buf_export(&export);
+	if (IS_ERR(buf)) {
+		tegra_vpr_buffer_release(buffer);
+		tegra_vpr_recycle(vpr);
+	}
+
+	mutex_unlock(&vpr->lock);
+	return buf;
+}
+
+static void tegra_vpr_debugfs_show_buffers(struct tegra_vpr *vpr,
+					   struct seq_file *s)
+{
+	struct tegra_vpr_buffer *buffer;
+	char buf[16];
+
+	mutex_lock(&vpr->lock);
+
+	list_for_each_entry(buffer, &vpr->buffers, list) {
+		string_get_size(buffer->size, 1, STRING_UNITS_2, buf,
+				sizeof(buf));
+		seq_printf(s, "  %pap-%pap (%s)\n", &buffer->start,
+			   &buffer->limit, buf);
+	}
+
+	mutex_unlock(&vpr->lock);
+}
+
+static void tegra_vpr_debugfs_show_chunks(struct tegra_vpr *vpr,
+					  struct seq_file *s)
+{
+	struct tegra_vpr_buffer *buffer;
+	unsigned int i;
+	char buf[16];
+
+	for (i = 0; i < vpr->num_chunks; i++) {
+		const struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+		string_get_size(chunk->size, 1, STRING_UNITS_2, buf,
+				sizeof(buf));
+		seq_printf(s, "  %pap-%pap (%s) (%s, %u buffers)\n",
+			   &chunk->start, &chunk->limit, buf,
+			   chunk->active ? "active" : "inactive",
+			   chunk->num_buffers);
+	}
+
+	list_for_each_entry(buffer, &vpr->buffers, list) {
+		string_get_size(buffer->size, 1, STRING_UNITS_2, buf,
+				sizeof(buf));
+		seq_printf(s, "%pap-%pap (%s, chunks: %*pbl)\n",
+			   &buffer->start, &buffer->limit, buf,
+			   vpr->num_chunks, buffer->chunks);
+	}
+}
+
+static int tegra_vpr_debugfs_show(struct seq_file *s, struct dma_heap *heap)
+{
+	struct tegra_vpr *vpr = dma_heap_get_drvdata(heap);
+	phys_addr_t limit = vpr->base + vpr->size;
+	char buf[16];
+
+	string_get_size(vpr->size, 1, STRING_UNITS_2, buf, sizeof(buf));
+	seq_printf(s, "%pap-%pap (%s)\n", &vpr->base, &limit, buf);
+
+	if (!vpr->resizable)
+		tegra_vpr_debugfs_show_buffers(vpr, s);
+	else
+		tegra_vpr_debugfs_show_chunks(vpr, s);
+
+	return 0;
+}
+
+static const struct dma_heap_ops tegra_vpr_heap_ops = {
+	.allocate = tegra_vpr_allocate,
+	.show = tegra_vpr_debugfs_show,
+};
+
+static int tegra_vpr_setup_chunks(struct tegra_vpr *vpr, const char *name)
+{
+	phys_addr_t start, limit;
+	unsigned int order, i = 0;
+	size_t max_size;
+	int err;
+
+	/* Memory is backed by struct page, so track the first one. */
+	vpr->start_page = phys_to_page(vpr->base);
+
+	/* This seems a reasonable value, so hard-code it for now. */
+	vpr->num_chunks = 4;
+
+	vpr->chunks = kzalloc_objs(*vpr->chunks, vpr->num_chunks);
+	if (!vpr->chunks)
+		return -ENOMEM;
+
+	vpr->active = bitmap_zalloc(vpr->num_chunks, GFP_KERNEL);
+	if (!vpr->active) {
+		err = -ENOMEM;
+		goto free;
+	}
+
+	max_size = PAGE_SIZE << (get_order(vpr->size) - ilog2(vpr->num_chunks));
+	order = get_order(vpr->align);
+
+	/*
+	 * Allocate CMA areas for VPR. All areas will be roughtly the same
+	 * size, with the last area taking up the rest.
+	 */
+	start = vpr->base;
+	limit = vpr->base + vpr->size;
+
+	pr_debug("VPR: %pap-%pap (%lu pages, %u chunks, %lu MiB)\n", &start,
+		 &limit, vpr->num_pages, vpr->num_chunks,
+		 (unsigned long)vpr->size / 1024 / 1024);
+
+	for (i = 0; i < vpr->num_chunks; i++) {
+		size_t size = limit - start;
+		phys_addr_t end;
+
+		size = min_t(size_t, size, max_size);
+		end = start + size - 1;
+
+		err = tegra_vpr_chunk_init(vpr, &vpr->chunks[i], start, size,
+					   order, name);
+		if (err < 0) {
+			pr_err("failed to create VPR chunk: %d\n", err);
+			goto free;
+		}
+
+		pr_debug("  %2u: %pap-%pap (%lu MiB)\n", i, &start, &end,
+			 size / 1024 / 1024);
+		start += size;
+	}
+
+	vpr->first = ~0U;
+	vpr->last = 0U;
+
+	return 0;
+
+free:
+	while (i--)
+		tegra_vpr_chunk_free(&vpr->chunks[i]);
+
+	kfree(vpr->active);
+	kfree(vpr->chunks);
+	return err;
+}
+
+static void tegra_vpr_free_chunks(struct tegra_vpr *vpr)
+{
+	unsigned int i;
+
+	for (i = 0; i < vpr->num_chunks; i++)
+		tegra_vpr_chunk_free(&vpr->chunks[i]);
+
+	kfree(vpr->chunks);
+}
+
+static int tegra_vpr_setup_static(struct tegra_vpr *vpr)
+{
+	phys_addr_t start, limit;
+
+	start = vpr->base;
+	limit = vpr->base + vpr->size;
+
+	pr_debug("VPR: %pap-%pap (%lu pages, %lu MiB)\n", &start, &limit,
+		 vpr->num_pages, (unsigned long)vpr->size / 1024 / 1024);
+
+	return 0;
+}
+
+static int tegra_vpr_add_heap(struct reserved_mem *rmem,
+			      struct device_node *np)
+{
+	struct dma_heap_export_info info = {};
+	unsigned long first, last;
+	struct dma_heap *heap;
+	struct tegra_vpr *vpr;
+	int err;
+
+	vpr = kzalloc_obj(*vpr, GFP_KERNEL);
+	if (!vpr)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&vpr->list);
+	INIT_LIST_HEAD(&vpr->buffers);
+	INIT_LIST_HEAD(&vpr->devices);
+	mutex_init(&vpr->lock);
+
+	vpr->resizable = !of_property_read_bool(np, "no-map");
+	vpr->dev_node = of_node_get(np);
+	vpr->align = PAGE_SIZE;
+	vpr->base = rmem->base;
+	vpr->size = rmem->size;
+	vpr->num_pages = vpr->size >> PAGE_SHIFT;
+	vpr->nid = of_node_to_nid(np);
+	vpr->cma = rmem->priv;
+
+	vpr->bitmap = bitmap_zalloc(vpr->num_pages, GFP_KERNEL);
+	if (!vpr->bitmap) {
+		err = -ENOMEM;
+		goto free;
+	}
+
+	first = find_first_bit(vpr->bitmap, vpr->num_pages);
+	last = find_last_bit(vpr->bitmap, vpr->num_pages);
+
+	if (vpr->resizable)
+		err = tegra_vpr_setup_chunks(vpr, rmem->name);
+	else
+		err = tegra_vpr_setup_static(vpr);
+
+	if (err < 0)
+		goto free;
+
+	info.name = vpr->dev_node->name;
+	info.ops = &tegra_vpr_heap_ops;
+	info.priv = vpr;
+
+	heap = dma_heap_add(&info);
+	if (IS_ERR(heap)) {
+		err = PTR_ERR(heap);
+		goto cleanup;
+	}
+
+	tegra_vpr_add(vpr);
+
+	return 0;
+
+cleanup:
+	if (vpr->resizable)
+		tegra_vpr_free_chunks(vpr);
+free:
+	bitmap_free(vpr->bitmap);
+	kfree(vpr);
+	return err;
+}
+
+static int tegra_vpr_init(void)
+{
+	const char *compatible = "nvidia,tegra-video-protection-region";
+	struct device_node *parent;
+	struct reserved_mem *rmem;
+	int err;
+
+	parent = of_find_node_by_path("/reserved-memory");
+	if (!parent)
+		return 0;
+
+	for_each_child_of_node_scoped(parent, child) {
+		if (!of_device_is_compatible(child, compatible))
+			continue;
+
+		rmem = of_reserved_mem_lookup(child);
+		if (!rmem)
+			continue;
+
+		err = tegra_vpr_add_heap(rmem, child);
+		if (err < 0)
+			pr_err("failed to add VPR heap for %pOF: %d\n", child,
+			       err);
+
+		/* only a single VPR heap is supported */
+		break;
+	}
+
+	of_node_put(parent);
+	return 0;
+}
+module_init(tegra_vpr_init);
+
+MODULE_DESCRIPTION("NVIDIA Tegra Video-Protection-Region DMA-BUF heap driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/dma-buf/heaps/tegra-vpr.h b/drivers/dma-buf/heaps/tegra-vpr.h
new file mode 100644
index 0000000000000..ba2afba8cc617
--- /dev/null
+++ b/drivers/dma-buf/heaps/tegra-vpr.h
@@ -0,0 +1,73 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * DMA-BUF restricted heap exporter for NVIDIA Video-Protection-Region (VPR)
+ *
+ * Copyright (C) 2024-2026 NVIDIA Corporation
+ */
+
+#ifndef DMA_BUF_HEAPS_TEGRA_VPR_H
+#define DMA_BUF_HEAPS_TEGRA_VPR_H
+
+#include <linux/list.h>
+#include <linux/mutex.h>
+
+struct tegra_vpr_device {
+	struct list_head node;
+	struct device *dev;
+};
+
+struct tegra_vpr;
+
+struct tegra_vpr_chunk {
+	phys_addr_t start;
+	phys_addr_t limit;
+	size_t size;
+
+	struct tegra_vpr *vpr;
+	bool invalid;
+	bool active;
+
+	struct page *start_page;
+	unsigned int offset;
+	unsigned long virt;
+	pgoff_t num_pages;
+
+	unsigned int num_buffers;
+};
+
+struct tegra_vpr {
+	struct list_head list;
+
+	struct device_node *dev_node;
+	unsigned long align;
+	phys_addr_t base;
+	phys_addr_t size;
+	int nid;
+
+	struct list_head buffers;
+	unsigned long *bitmap;
+	pgoff_t num_pages;
+
+	/* resizable VPR */
+	struct cma *cma;
+	unsigned long *active;
+	struct tegra_vpr_chunk *chunks;
+	unsigned int num_chunks;
+	struct page *start_page;
+	bool resizable;
+
+	unsigned int first;
+	unsigned int last;
+
+	struct list_head devices;
+
+	/**
+	 * @lock: Protects concurrent access to the allocation bitmap, as well
+	 * as the buffers and devices lists.
+	 */
+	struct mutex lock;
+};
+
+void tegra_vpr_add(struct tegra_vpr *vpr);
+
+#endif
diff --git a/drivers/of/of_numa.c b/drivers/of/of_numa.c
index 230d5f628c1b4..da2e9bd1717c2 100644
--- a/drivers/of/of_numa.c
+++ b/drivers/of/of_numa.c
@@ -171,6 +171,7 @@ int of_node_to_nid(struct device_node *device)
 
 	return NUMA_NO_NODE;
 }
+EXPORT_SYMBOL(of_node_to_nid);
 
 int __init of_numa_init(void)
 {
diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 7df1573a409c3..c2ab02985b5fa 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -716,10 +716,10 @@ void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
 }
 
 /**
- * bitmap_allocate_region - allocate bitmap region
+ * bitmap_allocate - allocate bitmap region
  *	@bitmap: array of unsigned longs corresponding to the bitmap
  *	@pos: beginning of bit region to allocate
- *	@order: region size (log base 2 of number of bits) to allocate
+ *	@len: number of bits to allocate
  *
  * Allocate (set bits in) a specified region of a bitmap.
  *
@@ -727,16 +727,31 @@ void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
  * free (not all bits were zero).
  */
 static __always_inline
-int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
+int bitmap_allocate(unsigned long *bitmap, unsigned int pos, unsigned int len)
 {
-	unsigned int len = BIT(order);
-
 	if (find_next_bit(bitmap, pos + len, pos) < pos + len)
 		return -EBUSY;
 	bitmap_set(bitmap, pos, len);
 	return 0;
 }
 
+/**
+ * bitmap_allocate_region - allocate bitmap region
+ *	@bitmap: array of unsigned longs corresponding to the bitmap
+ *	@pos: beginning of bit region to allocate
+ *	@order: region size (log base 2 of number of bits) to allocate
+ *
+ * Allocate (set bits in) a specified region of a bitmap.
+ *
+ * Returns: 0 on success, or %-EBUSY if specified region wasn't
+ * free (not all bits were zero).
+ */
+static __always_inline
+int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
+{
+	return bitmap_allocate(bitmap, pos, BIT(order));
+}
+
 /**
  * bitmap_find_free_region - find a contiguous aligned mem region
  *	@bitmap: array of unsigned longs corresponding to the bitmap
diff --git a/include/linux/cma.h b/include/linux/cma.h
index 8555d38a97b1a..844404459a424 100644
--- a/include/linux/cma.h
+++ b/include/linux/cma.h
@@ -49,11 +49,15 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
 					struct cma **res_cma);
 extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align,
 			      bool no_warn);
+extern struct page *cma_alloc_at(struct cma *cma, unsigned long offset,
+				 unsigned long count, bool no_warn);
 extern bool cma_release(struct cma *cma, const struct page *pages, unsigned long count);
 
 struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,
 		unsigned int align, bool no_warn);
 struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order);
+struct page *cma_alloc_at_frozen(struct cma *cma, unsigned long offset,
+				 unsigned long count, bool no_warn);
 bool cma_release_frozen(struct cma *cma, const struct page *pages,
 		unsigned long count);
 
diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h
index 648328a64b27e..1c9bed1f4ddef 100644
--- a/include/linux/dma-heap.h
+++ b/include/linux/dma-heap.h
@@ -12,6 +12,7 @@
 #include <linux/types.h>
 
 struct dma_heap;
+struct seq_file;
 
 /**
  * struct dma_heap_ops - ops to operate on a given heap
@@ -24,6 +25,7 @@ struct dma_heap_ops {
 				    unsigned long len,
 				    u32 fd_flags,
 				    u64 heap_flags);
+	int (*show)(struct seq_file *s, struct dma_heap *heap);
 };
 
 /**
diff --git a/include/trace/events/cma.h b/include/trace/events/cma.h
index 37195edf24986..4e0ab63e303f0 100644
--- a/include/trace/events/cma.h
+++ b/include/trace/events/cma.h
@@ -38,13 +38,17 @@ TRACE_EVENT(cma_release,
 
 TRACE_EVENT(cma_alloc_start,
 
-	TP_PROTO(const char *name, unsigned long request_count, unsigned long available_count,
+	TP_PROTO(const char *name, unsigned long start, unsigned long end,
+		unsigned long request_count, unsigned long available_count,
 		unsigned long total_count, unsigned int align),
 
-	TP_ARGS(name, request_count, available_count, total_count, align),
+	TP_ARGS(name, start, end, request_count, available_count, total_count,
+		align),
 
 	TP_STRUCT__entry(
 		__string(name, name)
+		__field(unsigned long, start)
+		__field(unsigned long, end)
 		__field(unsigned long, request_count)
 		__field(unsigned long, available_count)
 		__field(unsigned long, total_count)
@@ -53,14 +57,18 @@ TRACE_EVENT(cma_alloc_start,
 
 	TP_fast_assign(
 		__assign_str(name);
+		__entry->start = start;
+		__entry->end = end;
 		__entry->request_count = request_count;
 		__entry->available_count = available_count;
 		__entry->total_count = total_count;
 		__entry->align = align;
 	),
 
-	TP_printk("name=%s request_count=%lu available_count=%lu total_count=%lu align=%u",
+	TP_printk("name=%s start=%lu end=%lu request_count=%lu available_count=%lu total_count=%lu align=%u",
 		  __get_str(name),
+		  __entry->start,
+		  __entry->end,
 		  __entry->request_count,
 		  __entry->available_count,
 		  __entry->total_count,
@@ -132,6 +140,69 @@ TRACE_EVENT(cma_alloc_busy_retry,
 		  __entry->align)
 );
 
+TRACE_EVENT(cma_alloc_at_start,
+
+	TP_PROTO(const char *name, unsigned long pfn,
+		 unsigned long request_count, unsigned long available_count,
+		 unsigned long total_count),
+
+	TP_ARGS(name, pfn, request_count, available_count, total_count),
+
+	TP_STRUCT__entry(
+		__string(name, name)
+		__field(unsigned long, pfn)
+		__field(unsigned long, request_count)
+		__field(unsigned long, available_count)
+		__field(unsigned long, total_count)
+	),
+
+	TP_fast_assign(
+		__assign_str(name);
+		__entry->pfn = pfn;
+		__entry->request_count = request_count;
+		__entry->available_count = available_count;
+		__entry->total_count = total_count;
+	),
+
+	TP_printk("name=%s pfn=%lx, request_count=%lu available_count=%lu total_count=%lu",
+		  __get_str(name),
+		  __entry->pfn,
+		  __entry->request_count,
+		  __entry->available_count,
+		  __entry->total_count)
+);
+
+TRACE_EVENT(cma_alloc_at_finish,
+
+	TP_PROTO(const char *name, unsigned long pfn, const struct page *page,
+		 unsigned long count, int errorno),
+
+	TP_ARGS(name, pfn, page, count, errorno),
+
+	TP_STRUCT__entry(
+		__string(name, name)
+		__field(unsigned long, pfn)
+		__field(const struct page *, page)
+		__field(unsigned long, count)
+		__field(int, errorno)
+	),
+
+	TP_fast_assign(
+		__assign_str(name);
+		__entry->pfn = pfn;
+		__entry->page = page;
+		__entry->count = count;
+		__entry->errorno = errorno;
+	),
+
+	TP_printk("name=%s pfn=0x%lx page=%p count=%lu errorno=%d",
+		  __get_str(name),
+		  __entry->pfn,
+		  __entry->page,
+		  __entry->count,
+		  __entry->errorno)
+);
+
 #endif /* _TRACE_CMA_H */
 
 /* This part must be outside protection */
diff --git a/include/trace/events/tegra_vpr.h b/include/trace/events/tegra_vpr.h
new file mode 100644
index 0000000000000..f8ceb17679fe0
--- /dev/null
+++ b/include/trace/events/tegra_vpr.h
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#if !defined(_TRACE_TEGRA_VPR_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_TEGRA_VPR_H
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM tegra_vpr
+
+#include <linux/tracepoint.h>
+
+TRACE_EVENT(tegra_vpr_chunk_activate,
+	TP_PROTO(phys_addr_t start, phys_addr_t limit),
+	TP_ARGS(start, limit),
+	TP_STRUCT__entry(
+		__field(phys_addr_t, start)
+		__field(phys_addr_t, limit)
+	),
+	TP_fast_assign(
+		__entry->start = start;
+		__entry->limit = limit;
+	),
+	TP_printk("%pap-%pap", &__entry->start,
+		  &__entry->limit)
+);
+
+TRACE_EVENT(tegra_vpr_chunk_deactivate,
+	TP_PROTO(phys_addr_t start, phys_addr_t limit),
+	TP_ARGS(start, limit),
+	TP_STRUCT__entry(
+		__field(phys_addr_t, start)
+		__field(phys_addr_t, limit)
+	),
+	TP_fast_assign(
+		__entry->start = start;
+		__entry->limit = limit;
+	),
+	TP_printk("%pap-%pap", &__entry->start,
+		  &__entry->limit)
+);
+
+TRACE_EVENT(tegra_vpr_set,
+	TP_PROTO(phys_addr_t base, phys_addr_t size),
+	TP_ARGS(base, size),
+	TP_STRUCT__entry(
+		__field(phys_addr_t, start)
+		__field(phys_addr_t, limit)
+	),
+	TP_fast_assign(
+		__entry->start = base;
+		__entry->limit = base + size;
+	),
+	TP_printk("%pap-%pap", &__entry->start, &__entry->limit)
+);
+
+#endif /* _TRACE_TEGRA_VPR_H */
+
+#include <trace/define_trace.h>
diff --git a/mm/cma.c b/mm/cma.c
index a10ea37a261da..f92d3a1882098 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -783,25 +783,32 @@ static void cma_debug_show_areas(struct cma *cma)
 	spin_unlock_irq(&cma->lock);
 }
 
+/*
+ * Searches the CMA memrange, from @start to @end, for a free region of
+ * @count bits. If @end is less or equal to @start, will search the entire
+ * memrange.
+ */
 static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
-				unsigned long count, unsigned int align,
-				struct page **pagep, gfp_t gfp)
+			   unsigned long start, unsigned long end,
+			   unsigned long count, unsigned int align,
+			   struct page **pagep, gfp_t gfp)
 {
 	unsigned long bitmap_maxno, bitmap_no, bitmap_count;
-	unsigned long start, pfn, mask, offset;
-	int ret = -EBUSY;
+	unsigned long pfn, mask, offset;
 	struct page *page = NULL;
+	int ret = -EBUSY;
 
 	mask = cma_bitmap_aligned_mask(cma, align);
 	offset = cma_bitmap_aligned_offset(cma, cmr, align);
-	bitmap_maxno = cma_bitmap_maxno(cma, cmr);
+	bitmap_maxno = (end > start) ? end : cma_bitmap_maxno(cma, cmr);
 	bitmap_count = cma_bitmap_pages_to_bits(cma, count);
 
 	if (bitmap_count > bitmap_maxno)
 		goto out;
 
-	for (start = 0; ; start = bitmap_no + mask + 1) {
+	while (true) {
 		spin_lock_irq(&cma->lock);
+
 		/*
 		 * If the request is larger than the available number
 		 * of pages, stop right away.
@@ -810,6 +817,7 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
 			spin_unlock_irq(&cma->lock);
 			break;
 		}
+
 		bitmap_no = bitmap_find_next_zero_area_off(cmr->bitmap,
 				bitmap_maxno, start, bitmap_count, mask,
 				offset);
@@ -835,10 +843,11 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
 
 		bitmap_set(cmr->bitmap, bitmap_no, bitmap_count);
 		cma->available_count -= count;
+
 		/*
-		 * It's safe to drop the lock here. We've marked this region for
-		 * our exclusive use. If the migration fails we will take the
-		 * lock again and unmark it.
+		 * It's safe to drop the lock here. We've marked this region
+		 * for our exclusive use. If the migration fails we will take
+		 * the lock again and unmark it.
 		 */
 		spin_unlock_irq(&cma->lock);
 
@@ -856,37 +865,45 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
 			 __func__, pfn, page);
 
 		trace_cma_alloc_busy_retry(cma->name, pfn, page, count, align);
+		start = bitmap_no + mask + 1;
 	}
+
 out:
 	if (!ret)
 		*pagep = page;
+
 	return ret;
 }
 
-static struct page *__cma_alloc_frozen(struct cma *cma,
-		unsigned long count, unsigned int align, gfp_t gfp)
+static struct page *__cma_alloc_frozen(struct cma *cma, unsigned long start,
+				       unsigned long end, unsigned long count,
+				       unsigned int align, gfp_t gfp)
 {
 	struct page *page = NULL;
 	int ret = -ENOMEM, r;
 	unsigned long i;
-	const char *name = cma ? cma->name : NULL;
+
+	/* cma_alloc_at() and friends will only work with single-range CMA */
+	if (WARN_ON_ONCE(start > 0 && cma->nranges != 1))
+		return page;
 
 	if (!cma || !cma->count)
 		return page;
 
-	pr_debug("%s(cma %p, name: %s, count %lu, align %d)\n", __func__,
-		(void *)cma, cma->name, count, align);
+	pr_debug("%s(cma %p, name: %s, start %lu, end %lu, count %lu, align %d)\n",
+		 __func__, (void *)cma, cma->name, start, end, count, align);
 
 	if (!count)
 		return page;
 
-	trace_cma_alloc_start(name, count, cma->available_count, cma->count, align);
+	trace_cma_alloc_start(cma->name, start, end, count,
+			      cma->available_count, cma->count, align);
 
 	for (r = 0; r < cma->nranges; r++) {
 		page = NULL;
 
-		ret = cma_range_alloc(cma, &cma->ranges[r], count, align,
-				       &page, gfp);
+		ret = cma_range_alloc(cma, &cma->ranges[r], start, end,
+				      count, align, &page, gfp);
 		if (ret != -EBUSY || page)
 			break;
 	}
@@ -908,7 +925,7 @@ static struct page *__cma_alloc_frozen(struct cma *cma,
 	}
 
 	pr_debug("%s(): returned %p\n", __func__, page);
-	trace_cma_alloc_finish(name, page ? page_to_pfn(page) : 0,
+	trace_cma_alloc_finish(cma->name, page ? page_to_pfn(page) : 0,
 			       page, count, align, ret);
 	if (page) {
 		count_vm_event(CMA_ALLOC_SUCCESS);
@@ -926,14 +943,22 @@ struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,
 {
 	gfp_t gfp = GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0);
 
-	return __cma_alloc_frozen(cma, count, align, gfp);
+	return __cma_alloc_frozen(cma, 0, 0, count, align, gfp);
 }
 
 struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order)
 {
 	gfp_t gfp = GFP_KERNEL | __GFP_COMP | __GFP_NOWARN;
 
-	return __cma_alloc_frozen(cma, 1 << order, order, gfp);
+	return __cma_alloc_frozen(cma, 0, 0, 1 << order, order, gfp);
+}
+
+struct page *cma_alloc_at_frozen(struct cma *cma, unsigned long offset,
+				 unsigned long count, bool no_warn)
+{
+	gfp_t gfp = GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0);
+
+	return __cma_alloc_frozen(cma, offset, offset + count, count, 0, gfp);
 }
 
 /**
@@ -959,6 +984,29 @@ struct page *cma_alloc(struct cma *cma, unsigned long count,
 }
 EXPORT_SYMBOL_GPL(cma_alloc);
 
+/**
+ * cma_alloc_at() - allocate pages from contiguous area at fixed offset
+ * @cma:     Contiguous memory region for which the allocation is performed.
+ * @offset:  Index of the first page to allocate.
+ * @count:   Requested number of pages (in PAGE_SIZE order).
+ * @no_warn: Avoid printing message about failed allocation.
+ *
+ * This function allocates a part of the contiguous memory on a specific
+ * contiguous memory area.
+ */
+struct page *cma_alloc_at(struct cma *cma, unsigned long offset,
+			  unsigned long count, bool no_warn)
+{
+	struct page *page;
+
+	page = cma_alloc_at_frozen(cma, offset, count, no_warn);
+	if (page)
+		set_pages_refcounted(page, count);
+
+	return page;
+}
+EXPORT_SYMBOL_GPL(cma_alloc_at);
+
 static struct cma_memrange *find_cma_memrange(struct cma *cma,
 		const struct page *pages, unsigned long count)
 {

3/2 2026/09/05 20:47 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 28474
							output: 18
							thoughts: 709
						

Thoughts:
**Analyzing this Patch Series**

Okay, let's dive into this patch series. My initial thought is to go through each piece methodically and understand the implications, especially given my experience. First, let's carefully dissect the changes.

I see a bunch of Device Tree bindings and DTS updates for the Tegra VPR, targeting several Tegra SoCs. The `arch/arm64/mm/pageattr.c` changes look straightforward: two symbols are being exported, `set_direct_map_invalid_noflush` and `set_direct_map_default_noflush`.

Then, we have additions and changes to the `dma-buf` framework in `drivers/dma-buf/dma-heap.c`. Debugfs support is being added, which is always useful for debugging. Specifically, it seems like we'll have `dma_heap_debug_show`, `dma_heap_init_debugfs`, `dma_heap_exit_debugfs`, and the `dma_heap_exit()` function. There's also the addition of `DMABUF_HEAPS_TEGRA_VPR` in Kconfig and Makefile. We also have a dedicated Tegra VPR dma-buf heap driver, involving `arm_smccc_smc(...)` which makes me think this depends heavily on the Tegra hardware, Device Tree, and ARM SMCCC SIP calls.

We also have an `EXPORT_SYMBOL(of_node_to_nid)` in `drivers/of/of_numa.c`. Okay, bitmap refactoring in `include/linux/bitmap.h`: `bitmap_allocate` helper added, `bitmap_allocate_region` implemented in terms of `bitmap_allocate`. Standard stuff.

But here's where it gets interesting: the core mm/CMA is getting some love! `cma_range_alloc` and `__cma_alloc_frozen` now take `start` and `end` parameters. There are new functions being added: `cma_alloc_at_frozen`, `cma_alloc_at`. We have new trace events, too, and an update to `cma_alloc_start`.

Wait, the target architecture is amd64! My immediate concern is, can this code even be reached on amd64? This is important. I need to figure out if these CMA and DMA heap changes apply to amd64, and if the Tegra VPR changes are conditionally compiled.

So, I need to check `mm/cma.c`, `include/linux/bitmap.h`, and `drivers/dma-buf/dma-heap.c`. The critical thing to determine is whether CMA is even enabled and usable on x86_64/amd64. How are `cma_range_alloc` and the other CMA functions used in the context of amd64? Is `dma-heap.c` functionality reachable on amd64?

I need to use grepper to search for CMA and DMA heap usages or drivers on x86/amd64 to give me the information I need.



4/2 2026/09/05 20:47 tool grepper
0m Args:
{
  "Expression": "CONFIG_CMA"
}

Results:
{
  "Output": "\nFull output is too long, showing 500 out of 565 lines.\nUse more precise expression if possible.\n\nDocumentation/arch/arm/omap/dss.rst=296=OMAP FB allocates the framebuffer memory using the standard dma allocator. You\nDocumentation/arch/arm/omap/dss.rst:297:can enable Contiguous Memory Allocator (CONFIG_CMA) to improve the dma\nDocumentation/arch/arm/omap/dss.rst-298-allocator, and if CMA is enabled, you use \"cma=\" kernel parameter to increase\n--\nDocumentation/mm/physical_memory.rst=374=General\n--\nDocumentation/mm/physical_memory.rst-490-  The pages reserved for CMA use. These pages behave like ``ZONE_MOVABLE`` when\nDocumentation/mm/physical_memory.rst:491:  they are not used for CMA. Defined only when ``CONFIG_CMA`` is enabled.\nDocumentation/mm/physical_memory.rst-492-\n--\narch/arm/configs/bcm2835_defconfig=46=CONFIG_KSM=y\narch/arm/configs/bcm2835_defconfig:47:CONFIG_CMA=y\narch/arm/configs/bcm2835_defconfig-48-CONFIG_NET=y\n--\narch/arm/configs/bcm2835_defconfig=173=CONFIG_DMA_CMA=y\narch/arm/configs/bcm2835_defconfig:174:CONFIG_CMA_SIZE_MBYTES=32\narch/arm/configs/bcm2835_defconfig-175-CONFIG_PRINTK_TIME=y\n--\narch/arm/configs/davinci_all_defconfig=35=CONFIG_PARTITION_ADVANCED=y\narch/arm/configs/davinci_all_defconfig-36-# CONFIG_SWAP is not set\narch/arm/configs/davinci_all_defconfig:37:CONFIG_CMA=y\narch/arm/configs/davinci_all_defconfig-38-CONFIG_NET=y\n--\narch/arm/configs/exynos_defconfig=38=CONFIG_PARTITION_ADVANCED=y\narch/arm/configs/exynos_defconfig:39:CONFIG_CMA=y\narch/arm/configs/exynos_defconfig-40-CONFIG_NET=y\n--\narch/arm/configs/exynos_defconfig=368=CONFIG_DMA_CMA=y\narch/arm/configs/exynos_defconfig:369:CONFIG_CMA_SIZE_MBYTES=96\narch/arm/configs/exynos_defconfig-370-CONFIG_FONTS=y\n--\narch/arm/configs/gemini_defconfig=18=CONFIG_BINFMT_MISC=y\narch/arm/configs/gemini_defconfig:19:CONFIG_CMA=y\narch/arm/configs/gemini_defconfig-20-CONFIG_NET=y\n--\narch/arm/configs/imx_v6_v7_defconfig=471=CONFIG_CRYPTO_DEV_MXS_DCP=y\narch/arm/configs/imx_v6_v7_defconfig:472:CONFIG_CMA_SIZE_MBYTES=64\narch/arm/configs/imx_v6_v7_defconfig-473-CONFIG_FONTS=y\n--\narch/arm/configs/keystone_defconfig=35=CONFIG_MODVERSIONS=y\narch/arm/configs/keystone_defconfig-36-# CONFIG_SWAP is not set\narch/arm/configs/keystone_defconfig:37:CONFIG_CMA=y\narch/arm/configs/keystone_defconfig-38-CONFIG_NET=y\n--\narch/arm/configs/milbeaut_m10v_defconfig=50=CONFIG_CMDLINE_PARTITION=y\narch/arm/configs/milbeaut_m10v_defconfig:51:CONFIG_CMA=y\narch/arm/configs/milbeaut_m10v_defconfig-52-CONFIG_DEVTMPFS=y\n--\narch/arm/configs/milbeaut_m10v_defconfig=104=CONFIG_DMA_CMA=y\narch/arm/configs/milbeaut_m10v_defconfig:105:CONFIG_CMA_SIZE_MBYTES=64\narch/arm/configs/milbeaut_m10v_defconfig-106-CONFIG_PRINTK_TIME=y\n--\narch/arm/configs/multi_v7_defconfig=1328=CONFIG_CRYPTO_DEV_STM32_CRYP=m\narch/arm/configs/multi_v7_defconfig:1329:CONFIG_CMA_SIZE_MBYTES=64\narch/arm/configs/multi_v7_defconfig-1330-CONFIG_PRINTK_TIME=y\n--\narch/arm/configs/nhk8815_defconfig=16=CONFIG_MODULE_UNLOAD=y\narch/arm/configs/nhk8815_defconfig-17-# CONFIG_SWAP is not set\narch/arm/configs/nhk8815_defconfig:18:CONFIG_CMA=y\narch/arm/configs/nhk8815_defconfig-19-CONFIG_NET=y\n--\narch/arm/configs/omap2plus_defconfig=63=CONFIG_BINFMT_MISC=y\narch/arm/configs/omap2plus_defconfig:64:CONFIG_CMA=y\narch/arm/configs/omap2plus_defconfig-65-CONFIG_ZSMALLOC=m\n--\narch/arm/configs/qcom_defconfig=30=CONFIG_PARTITION_ADVANCED=y\n--\narch/arm/configs/qcom_defconfig-32-# CONFIG_COMPAT_BRK is not set\narch/arm/configs/qcom_defconfig:33:CONFIG_CMA=y\narch/arm/configs/qcom_defconfig-34-CONFIG_NET=y\n--\narch/arm/configs/qcom_defconfig=300=CONFIG_DMA_CMA=y\narch/arm/configs/qcom_defconfig:301:CONFIG_CMA_SIZE_MBYTES=64\narch/arm/configs/qcom_defconfig-302-CONFIG_PRINTK_TIME=y\n--\narch/arm/configs/realview_defconfig=19=CONFIG_MODULE_UNLOAD=y\narch/arm/configs/realview_defconfig-20-# CONFIG_SWAP is not set\narch/arm/configs/realview_defconfig:21:CONFIG_CMA=y\narch/arm/configs/realview_defconfig-22-CONFIG_NET=y\n--\narch/arm/configs/sama7_defconfig=38=CONFIG_PARTITION_ADVANCED=y\n--\narch/arm/configs/sama7_defconfig-42-# CONFIG_COMPACTION is not set\narch/arm/configs/sama7_defconfig:43:CONFIG_CMA=y\narch/arm/configs/sama7_defconfig-44-# CONFIG_VM_EVENT_COUNTERS is not set\n--\narch/arm/configs/sama7_defconfig=240=CONFIG_DMA_CMA=y\narch/arm/configs/sama7_defconfig:241:CONFIG_CMA_SIZE_MBYTES=32\narch/arm/configs/sama7_defconfig:242:CONFIG_CMA_ALIGNMENT=9\narch/arm/configs/sama7_defconfig-243-# CONFIG_DEBUG_MISC is not set\n--\narch/arm/configs/shmobile_defconfig=28=CONFIG_SLAB_FREELIST_HARDENED=y\narch/arm/configs/shmobile_defconfig:29:CONFIG_CMA=y\narch/arm/configs/shmobile_defconfig-30-CONFIG_NET=y\n--\narch/arm/configs/shmobile_defconfig=223=CONFIG_DMA_CMA=y\narch/arm/configs/shmobile_defconfig:224:CONFIG_CMA_SIZE_MBYTES=64\narch/arm/configs/shmobile_defconfig-225-CONFIG_PRINTK_TIME=y\n--\narch/arm/configs/sunxi_defconfig=17=CONFIG_MODULE_UNLOAD=y\narch/arm/configs/sunxi_defconfig:18:CONFIG_CMA=y\narch/arm/configs/sunxi_defconfig-19-CONFIG_NET=y\n--\narch/arm/configs/tegra_defconfig=33=CONFIG_PARTITION_ADVANCED=y\narch/arm/configs/tegra_defconfig:34:CONFIG_CMA=y\narch/arm/configs/tegra_defconfig-35-CONFIG_NET=y\n--\narch/arm/configs/tegra_defconfig=336=CONFIG_DMA_CMA=y\narch/arm/configs/tegra_defconfig:337:CONFIG_CMA_SIZE_MBYTES=64\narch/arm/configs/tegra_defconfig-338-CONFIG_PRINTK_TIME=y\n--\narch/arm/configs/u8500_defconfig=22=CONFIG_PARTITION_ADVANCED=y\narch/arm/configs/u8500_defconfig-23-# CONFIG_SWAP is not set\narch/arm/configs/u8500_defconfig:24:CONFIG_CMA=y\narch/arm/configs/u8500_defconfig-25-CONFIG_NET=y\n--\narch/arm/configs/versatile_defconfig=14=CONFIG_PARTITION_ADVANCED=y\narch/arm/configs/versatile_defconfig:15:CONFIG_CMA=y\narch/arm/configs/versatile_defconfig-16-CONFIG_NET=y\n--\narch/arm/configs/vexpress_defconfig=29=CONFIG_MODULE_UNLOAD=y\narch/arm/configs/vexpress_defconfig-30-# CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS is not set\narch/arm/configs/vexpress_defconfig:31:CONFIG_CMA=y\narch/arm/configs/vexpress_defconfig-32-CONFIG_NET=y\n--\narch/arm/mm/dma-mapping.c=249=postcore_initcall(atomic_pool_init);\narch/arm/mm/dma-mapping.c-250-\narch/arm/mm/dma-mapping.c:251:#ifdef CONFIG_CMA_AREAS\narch/arm/mm/dma-mapping.c-252-struct dma_contig_early_reserve {\n--\narch/arm/mm/mm.h=90=void arm_mm_memblock_reserve(void);\narch/arm/mm/mm.h:91:#ifdef CONFIG_CMA_AREAS\narch/arm/mm/mm.h-92-void dma_contiguous_remap(void);\n--\narch/arm64/configs/defconfig=1960=CONFIG_DMA_RESTRICTED_POOL=y\narch/arm64/configs/defconfig:1961:CONFIG_CMA_SIZE_MBYTES=32\narch/arm64/configs/defconfig-1962-CONFIG_PRINTK_TIME=y\n--\narch/arm64/mm/hugetlbpage.c-37- */\narch/arm64/mm/hugetlbpage.c:38:#ifdef CONFIG_CMA\narch/arm64/mm/hugetlbpage.c-39-unsigned int arch_hugetlb_cma_order(void)\n--\narch/arm64/mm/hugetlbpage.c-45-}\narch/arm64/mm/hugetlbpage.c:46:#endif /* CONFIG_CMA */\narch/arm64/mm/hugetlbpage.c-47-\n--\narch/loongarch/configs/loongson32_defconfig=105=CONFIG_KSM=y\narch/loongarch/configs/loongson32_defconfig:106:CONFIG_CMA=y\narch/loongarch/configs/loongson32_defconfig:107:CONFIG_CMA_SYSFS=y\narch/loongarch/configs/loongson32_defconfig-108-CONFIG_USERFAULTFD=y\n--\narch/loongarch/configs/loongson32_defconfig=1089=CONFIG_DMA_CMA=y\narch/loongarch/configs/loongson32_defconfig:1090:CONFIG_CMA_SIZE_MBYTES=0\narch/loongarch/configs/loongson32_defconfig-1091-CONFIG_PRINTK_TIME=y\n--\narch/loongarch/configs/loongson64_defconfig=121=CONFIG_TRANSPARENT_HUGEPAGE=y\narch/loongarch/configs/loongson64_defconfig:122:CONFIG_CMA=y\narch/loongarch/configs/loongson64_defconfig:123:CONFIG_CMA_SYSFS=y\narch/loongarch/configs/loongson64_defconfig-124-CONFIG_USERFAULTFD=y\n--\narch/loongarch/configs/loongson64_defconfig=1123=CONFIG_DMA_NUMA_CMA=y\narch/loongarch/configs/loongson64_defconfig:1124:CONFIG_CMA_SIZE_MBYTES=0\narch/loongarch/configs/loongson64_defconfig-1125-CONFIG_PRINTK_TIME=y\n--\narch/microblaze/configs/mmu_defconfig=21=CONFIG_PARTITION_ADVANCED=y\narch/microblaze/configs/mmu_defconfig-22-# CONFIG_EFI_PARTITION is not set\narch/microblaze/configs/mmu_defconfig:23:CONFIG_CMA=y\narch/microblaze/configs/mmu_defconfig-24-CONFIG_NET=y\n--\narch/mips/configs/bmips_stb_defconfig=81=CONFIG_DMA_CMA=y\narch/mips/configs/bmips_stb_defconfig:82:CONFIG_CMA_ALIGNMENT=12\narch/mips/configs/bmips_stb_defconfig-83-CONFIG_SPI=y\n--\narch/mips/configs/ci20_defconfig=28=CONFIG_MODULES=y\n--\narch/mips/configs/ci20_defconfig-31-# CONFIG_COMPACTION is not set\narch/mips/configs/ci20_defconfig:32:CONFIG_CMA=y\narch/mips/configs/ci20_defconfig-33-# CONFIG_VM_EVENT_COUNTERS is not set\n--\narch/mips/configs/ci20_defconfig=218=CONFIG_DMA_CMA=y\narch/mips/configs/ci20_defconfig:219:CONFIG_CMA_SIZE_MBYTES=32\narch/mips/configs/ci20_defconfig-220-CONFIG_PRINTK_TIME=y\n--\narch/mips/configs/cu1000-neo_defconfig=27=CONFIG_MODULES=y\n--\narch/mips/configs/cu1000-neo_defconfig-29-# CONFIG_COMPACTION is not set\narch/mips/configs/cu1000-neo_defconfig:30:CONFIG_CMA=y\narch/mips/configs/cu1000-neo_defconfig-31-CONFIG_NET=y\n--\narch/mips/configs/cu1830-neo_defconfig=27=CONFIG_MODULES=y\n--\narch/mips/configs/cu1830-neo_defconfig-29-# CONFIG_COMPACTION is not set\narch/mips/configs/cu1830-neo_defconfig:30:CONFIG_CMA=y\narch/mips/configs/cu1830-neo_defconfig-31-CONFIG_NET=y\n--\narch/mips/configs/db1xxx_defconfig=27=CONFIG_PARTITION_ADVANCED=y\narch/mips/configs/db1xxx_defconfig:28:CONFIG_CMA=y\narch/mips/configs/db1xxx_defconfig:29:CONFIG_CMA_DEBUG=y\narch/mips/configs/db1xxx_defconfig-30-CONFIG_NET=y\n--\narch/mips/configs/qi_lb60_defconfig=16=CONFIG_PARTITION_ADVANCED=y\n--\narch/mips/configs/qi_lb60_defconfig-18-# CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS is not set\narch/mips/configs/qi_lb60_defconfig:19:CONFIG_CMA=y\narch/mips/configs/qi_lb60_defconfig-20-CONFIG_NET=y\n--\narch/mips/configs/rs90_defconfig=39=CONFIG_FRONTSWAP=y\narch/mips/configs/rs90_defconfig:40:CONFIG_CMA=y\narch/mips/configs/rs90_defconfig-41-CONFIG_ZSMALLOC=y\n--\narch/powerpc/include/asm/fadump.h=37=extern int fadump_reserve_mem(void);\n--\narch/powerpc/include/asm/fadump.h-39-\narch/powerpc/include/asm/fadump.h:40:#if defined(CONFIG_FA_DUMP) \u0026\u0026 defined(CONFIG_CMA)\narch/powerpc/include/asm/fadump.h-41-void fadump_cma_init(void);\n--\narch/powerpc/kernel/fadump.c=64=static void __init early_init_dt_scan_reserved_ranges(unsigned long node);\narch/powerpc/kernel/fadump.c-65-\narch/powerpc/kernel/fadump.c:66:#ifdef CONFIG_CMA\narch/powerpc/kernel/fadump.c-67-static struct cma *fadump_cma;\n--\narch/powerpc/kernel/fadump.c=82=void __init fadump_cma_init(void)\n--\narch/powerpc/kernel/fadump.c-142-}\narch/powerpc/kernel/fadump.c:143:#endif /* CONFIG_CMA */\narch/powerpc/kernel/fadump.c-144-\n--\narch/s390/configs/debug_defconfig=111=CONFIG_TRANSPARENT_HUGEPAGE=y\narch/s390/configs/debug_defconfig:112:CONFIG_CMA_DEBUGFS=y\narch/s390/configs/debug_defconfig:113:CONFIG_CMA_SYSFS=y\narch/s390/configs/debug_defconfig:114:CONFIG_CMA_AREAS=7\narch/s390/configs/debug_defconfig-115-CONFIG_MEM_SOFT_DIRTY=y\n--\narch/s390/configs/debug_defconfig=831=CONFIG_DMA_CMA=y\narch/s390/configs/debug_defconfig:832:CONFIG_CMA_SIZE_MBYTES=0\narch/s390/configs/debug_defconfig-833-CONFIG_PRINTK_TIME=y\n--\narch/s390/configs/defconfig=104=CONFIG_TRANSPARENT_HUGEPAGE=y\narch/s390/configs/defconfig:105:CONFIG_CMA_SYSFS=y\narch/s390/configs/defconfig:106:CONFIG_CMA_AREAS=7\narch/s390/configs/defconfig-107-CONFIG_MEM_SOFT_DIRTY=y\n--\narch/s390/configs/defconfig=815=CONFIG_DMA_CMA=y\narch/s390/configs/defconfig:816:CONFIG_CMA_SIZE_MBYTES=0\narch/s390/configs/defconfig-817-CONFIG_PRINTK_TIME=y\n--\narch/s390/mm/init.c=203=void __init setup_per_cpu_areas(void)\n--\narch/s390/mm/init.c-226-\narch/s390/mm/init.c:227:#ifdef CONFIG_CMA\narch/s390/mm/init.c-228-\n--\narch/s390/mm/init.c=271=device_initcall(s390_cma_mem_init);\narch/s390/mm/init.c-272-\narch/s390/mm/init.c:273:#endif /* CONFIG_CMA */\narch/s390/mm/init.c-274-\n--\ndrivers/dma-buf/heaps/cma_heap.c=281=static struct dma_buf *cma_heap_allocate(struct dma_heap *heap,\n--\ndrivers/dma-buf/heaps/cma_heap.c-304-\ndrivers/dma-buf/heaps/cma_heap.c:305:\tif (align \u003e CONFIG_CMA_ALIGNMENT)\ndrivers/dma-buf/heaps/cma_heap.c:306:\t\talign = CONFIG_CMA_ALIGNMENT;\ndrivers/dma-buf/heaps/cma_heap.c-307-\n--\ndrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h=19=struct arm_vsmmu;\n--\ndrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h-200-/* Ensure DMA allocations are naturally aligned */\ndrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h:201:#ifdef CONFIG_CMA_ALIGNMENT\ndrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h:202:#define Q_MAX_SZ_SHIFT\t\t\t(PAGE_SHIFT + CONFIG_CMA_ALIGNMENT)\ndrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h-203-#else\n--\ndrivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c-13- * Minimum resolution is 64 x 64, maximum 8192 x 8192.\ndrivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c:14: * To achieve 8192 x 8192, modify in defconfig: CONFIG_CMA_SIZE_MBYTES=320\ndrivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c-15- * The alignment requirements for the resolution depend on the format,\n--\nfs/proc/meminfo.c-16-#include \u003clinux/vmalloc.h\u003e\nfs/proc/meminfo.c:17:#ifdef CONFIG_CMA\nfs/proc/meminfo.c-18-#include \u003clinux/cma.h\u003e\n--\nfs/proc/meminfo.c=34=static int meminfo_proc_show(struct seq_file *m, void *v)\n--\nfs/proc/meminfo.c-153-\nfs/proc/meminfo.c:154:#ifdef CONFIG_CMA\nfs/proc/meminfo.c-155-\tshow_val_kb(m, \"CmaTotal:       \", totalcma_pages);\n--\ninclude/linux/cma.h-8-\ninclude/linux/cma.h:9:#ifdef CONFIG_CMA_AREAS\ninclude/linux/cma.h:10:#define MAX_CMA_AREAS\tCONFIG_CMA_AREAS\ninclude/linux/cma.h-11-#endif\n--\ninclude/linux/crash_reserve.h=16=extern struct range crashk_cma_ranges[];\n--\ninclude/linux/crash_reserve.h-18-#define CRASHK_CMA_RANGES_MAX 4\ninclude/linux/crash_reserve.h:19:#if defined(CONFIG_CMA) \u0026\u0026 defined(CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION)\ninclude/linux/crash_reserve.h-20-#define CRASHKERNEL_CMA\n--\ninclude/linux/hugetlb.h=1312=static inline spinlock_t *huge_pte_lock(struct hstate *h,\n--\ninclude/linux/hugetlb.h-1321-\ninclude/linux/hugetlb.h:1322:#if defined(CONFIG_HUGETLB_PAGE) \u0026\u0026 defined(CONFIG_CMA)\ninclude/linux/hugetlb.h-1323-extern void __init hugetlb_cma_reserve(void);\n--\ninclude/linux/mm.h=2808=static inline bool folio_is_longterm_pinnable(struct folio *folio)\ninclude/linux/mm.h-2809-{\ninclude/linux/mm.h:2810:#ifdef CONFIG_CMA\ninclude/linux/mm.h-2811-\tint mt = folio_migratetype(folio);\n--\ninclude/linux/mmzone.h=121=enum migratetype {\n--\ninclude/linux/mmzone.h-126-\tMIGRATE_HIGHATOMIC = MIGRATE_PCPTYPES,\ninclude/linux/mmzone.h:127:#ifdef CONFIG_CMA\ninclude/linux/mmzone.h-128-\t/*\n--\ninclude/linux/mmzone.h=150=extern const char * const migratetype_names[MIGRATE_TYPES];\ninclude/linux/mmzone.h-151-\ninclude/linux/mmzone.h:152:#ifdef CONFIG_CMA\ninclude/linux/mmzone.h-153-#  define is_migrate_cma(migratetype) unlikely((migratetype) == MIGRATE_CMA)\n--\ninclude/linux/mmzone.h=974=struct zone {\n--\ninclude/linux/mmzone.h-1067-#endif\ninclude/linux/mmzone.h:1068:#ifdef CONFIG_CMA\ninclude/linux/mmzone.h-1069-\tunsigned long\t\tcma_pages;\n--\ninclude/linux/mmzone.h-1122-\ninclude/linux/mmzone.h:1123:#if defined CONFIG_COMPACTION || defined CONFIG_CMA\ninclude/linux/mmzone.h-1124-\t/* pfn where compaction free scanner should start */\n--\ninclude/linux/mmzone.h-1143-\ninclude/linux/mmzone.h:1144:#if defined CONFIG_COMPACTION || defined CONFIG_CMA\ninclude/linux/mmzone.h-1145-\t/* Set to true when the PG_migrate_skip bits should be cleared */\n--\ninclude/linux/mmzone.h=1206=static inline unsigned long zone_cma_pages(struct zone *zone)\ninclude/linux/mmzone.h-1207-{\ninclude/linux/mmzone.h:1208:#ifdef CONFIG_CMA\ninclude/linux/mmzone.h-1209-\treturn zone-\u003ecma_pages;\n--\ninclude/linux/vm_event_item.h=34=enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,\n--\ninclude/linux/vm_event_item.h-74-#endif\ninclude/linux/vm_event_item.h:75:#ifdef CONFIG_CMA\ninclude/linux/vm_event_item.h-76-\t\tCMA_ALLOC_SUCCESS,\n--\nkernel/dma/contiguous.c-48-\nkernel/dma/contiguous.c:49:#ifdef CONFIG_CMA_SIZE_MBYTES\nkernel/dma/contiguous.c:50:#define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES\nkernel/dma/contiguous.c-51-#else\n--\nkernel/dma/contiguous.c=179=early_param(\"cma_pernuma\", early_cma_pernuma);\n--\nkernel/dma/contiguous.c-181-\nkernel/dma/contiguous.c:182:#ifdef CONFIG_CMA_SIZE_PERCENTAGE\nkernel/dma/contiguous.c-183-\nkernel/dma/contiguous.c=184=static phys_addr_t __init __maybe_unused cma_early_percent_memory(void)\n--\nkernel/dma/contiguous.c-187-\nkernel/dma/contiguous.c:188:\treturn (total_pages * CONFIG_CMA_SIZE_PERCENTAGE / 100) \u003c\u003c PAGE_SHIFT;\nkernel/dma/contiguous.c-189-}\n--\nkernel/dma/contiguous.c=201=static void __init dma_numa_cma_reserve(void)\n--\nkernel/dma/contiguous.c-204-\nkernel/dma/contiguous.c:205:\tif (IS_ENABLED(CONFIG_CMA_SIZE_PERNUMA) \u0026\u0026\nkernel/dma/contiguous.c-206-\t    !numa_cma_configured \u0026\u0026 dma_contiguous_default_area \u0026\u0026\n--\nkernel/dma/contiguous.c=250=void __init dma_contiguous_reserve(phys_addr_t limit)\n--\nkernel/dma/contiguous.c-268-\t} else {\nkernel/dma/contiguous.c:269:#ifdef CONFIG_CMA_SIZE_SEL_MBYTES\nkernel/dma/contiguous.c-270-\t\tselected_size = size_bytes;\nkernel/dma/contiguous.c:271:#elif defined(CONFIG_CMA_SIZE_SEL_PERCENTAGE)\nkernel/dma/contiguous.c-272-\t\tselected_size = cma_early_percent_memory();\nkernel/dma/contiguous.c:273:#elif defined(CONFIG_CMA_SIZE_SEL_MIN)\nkernel/dma/contiguous.c-274-\t\tselected_size = min(size_bytes, cma_early_percent_memory());\nkernel/dma/contiguous.c:275:#elif defined(CONFIG_CMA_SIZE_SEL_MAX)\nkernel/dma/contiguous.c-276-\t\tselected_size = max(size_bytes, cma_early_percent_memory());\n--\nkernel/dma/contiguous.c=368=struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,\n--\nkernel/dma/contiguous.c-370-{\nkernel/dma/contiguous.c:371:\tif (align \u003e CONFIG_CMA_ALIGNMENT)\nkernel/dma/contiguous.c:372:\t\talign = CONFIG_CMA_ALIGNMENT;\nkernel/dma/contiguous.c-373-\n--\nkernel/dma/contiguous.c=393=static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp)\nkernel/dma/contiguous.c-394-{\nkernel/dma/contiguous.c:395:\tunsigned int align = min(get_order(size), CONFIG_CMA_ALIGNMENT);\nkernel/dma/contiguous.c-396-\n--\nmm/Makefile=82=obj-$(CONFIG_HUGETLBFS)\t+= hugetlb.o hugetlb_sysfs.o hugetlb_sysctl.o\nmm/Makefile:83:ifdef CONFIG_CMA\nmm/Makefile-84-obj-$(CONFIG_HUGETLBFS)\t+= hugetlb_cma.o\n--\nmm/Makefile=121=obj-$(CONFIG_GENERIC_EARLY_IOREMAP) += early_ioremap.o\nmm/Makefile:122:obj-$(CONFIG_CMA)\t+= cma.o\nmm/Makefile-123-obj-$(CONFIG_NUMA) += numa.o\n--\nmm/Makefile=129=obj-$(CONFIG_PAGE_TABLE_CHECK) += page_table_check.o\nmm/Makefile:130:obj-$(CONFIG_CMA_DEBUGFS) += cma_debug.o\nmm/Makefile-131-obj-$(CONFIG_SECRETMEM) += secretmem.o\nmm/Makefile:132:obj-$(CONFIG_CMA_SYSFS) += cma_sysfs.o\nmm/Makefile-133-obj-$(CONFIG_USERFAULTFD) += userfaultfd.o\n--\nmm/cma.c=141=static void __init cma_activate_area(struct cma *cma)\n--\nmm/cma.c-178-\nmm/cma.c:179:#ifdef CONFIG_CMA_DEBUGFS\nmm/cma.c-180-\tINIT_HLIST_HEAD(\u0026cma-\u003emem_head);\n--\nmm/cma.h-10-\nmm/cma.h:11:#ifdef CONFIG_CMA_DEBUGFS\nmm/cma.h-12-#include \u003clinux/debugfs.h\u003e\n--\nmm/cma.h-14-\nmm/cma.h:15:#ifdef CONFIG_CMA_SYSFS\nmm/cma.h-16-#include \u003clinux/kobject.h\u003e\n--\nmm/cma.h=37=struct cma_memrange {\n--\nmm/cma.h-43-\t};\nmm/cma.h:44:#ifdef CONFIG_CMA_DEBUGFS\nmm/cma.h-45-\tstruct debugfs_u32_array dfs_bitmap;\n--\nmm/cma.h=50=struct cma {\n--\nmm/cma.h-55-\tstruct mutex alloc_mutex;\nmm/cma.h:56:#ifdef CONFIG_CMA_DEBUGFS\nmm/cma.h-57-\tstruct hlist_head mem_head;\n--\nmm/cma.h-62-\tstruct cma_memrange ranges[CMA_MAX_RANGES];\nmm/cma.h:63:#ifdef CONFIG_CMA_SYSFS\nmm/cma.h-64-\t/* the number of CMA page successful allocations */\n--\nmm/cma.h=88=static inline unsigned long cma_bitmap_maxno(struct cma *cma,\n--\nmm/cma.h-93-\nmm/cma.h:94:#ifdef CONFIG_CMA_SYSFS\nmm/cma.h-95-void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages);\n--\nmm/compaction.c=60=static inline bool is_via_compact_memory(int order) { return false; }\n--\nmm/compaction.c-62-\nmm/compaction.c:63:#if defined CONFIG_COMPACTION || defined CONFIG_CMA\nmm/compaction.c-64-\n--\nmm/compaction.c=1337=isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,\n--\nmm/compaction.c-1372-\nmm/compaction.c:1373:#endif /* CONFIG_COMPACTION || CONFIG_CMA */\nmm/compaction.c-1374-#ifdef CONFIG_COMPACTION\n--\nmm/compaction.c=2280=static enum compact_result __compact_finished(struct compact_control *cc)\n--\nmm/compaction.c-2361-\nmm/compaction.c:2362:#ifdef CONFIG_CMA\nmm/compaction.c-2363-\t\t/* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */\n--\nmm/hugetlb_cma.h-6-\nmm/hugetlb_cma.h:7:#ifdef CONFIG_CMA\nmm/hugetlb_cma.h-8-void hugetlb_cma_free_frozen_folio(struct folio *folio);\n--\nmm/internal.h=789=static inline void init_compound_tail(struct page *tail,\n--\nmm/internal.h-797-\nmm/internal.h:798:#if defined CONFIG_COMPACTION || defined CONFIG_CMA\nmm/internal.h-799-\n--\nmm/internal.h=871=isolate_migratepages_range(struct compact_control *cc,\n--\nmm/internal.h-873-\nmm/internal.h:874:#endif /* CONFIG_COMPACTION || CONFIG_CMA */\nmm/internal.h-875-\nmm/internal.h=876=struct cma;\nmm/internal.h-877-\nmm/internal.h:878:#ifdef CONFIG_CMA\nmm/internal.h-879-bool cma_validate_zones(struct cma *cma);\n--\nmm/memory_hotplug.c=790=static void auto_movable_stats_account_zone(struct auto_movable_stats *stats,\n--\nmm/memory_hotplug.c-796-\t\tstats-\u003ekernel_early_pages += zone-\u003epresent_early_pages;\nmm/memory_hotplug.c:797:#ifdef CONFIG_CMA\nmm/memory_hotplug.c-798-\t\t/*\n--\nmm/memory_hotplug.c-803-\t\tstats-\u003ekernel_early_pages -= zone-\u003ecma_pages;\nmm/memory_hotplug.c:804:#endif /* CONFIG_CMA */\nmm/memory_hotplug.c-805-\t}\n--\nmm/mm_init.c=2160=bool __init deferred_grow_zone(struct zone *zone, unsigned int order)\n--\nmm/mm_init.c-2212-\nmm/mm_init.c:2213:#ifdef CONFIG_CMA\nmm/mm_init.c-2214-void __init init_cma_reserved_pageblock(struct page *page)\n--\nmm/mm_init.h=42=struct zone *pfn_to_zone(unsigned long pfn, int nid);\nmm/mm_init.h-43-\nmm/mm_init.h:44:#if defined CONFIG_COMPACTION || defined CONFIG_CMA\nmm/mm_init.h-45-/* Free whole pageblock and set its migration type to MIGRATE_CMA. */\nmm/mm_init.h=46=void init_cma_reserved_pageblock(struct page *page);\n--\nmm/mm_init.h-48-\n\n"
}

5/2 2026/09/05 20:47 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 39521
							output: 18
							thoughts: 66
						

Thoughts:
Here's my take on the thought process, as requested:

**Digging into the CMA Configuration Landscape**

Okay, so I'm looking into how the Configuration options related to CMA (Contiguous Memory Allocator), DMABUF Heaps, and DMA are all set up within the kernel. I need to understand the configuration hierarchy. My initial thought is, where would I find the most relevant `Kconfig` files that orchestrate these options? Let's start by checking `mm/Kconfig`, that's the obvious place to begin with memory management options. If that doesn't hold the key, then the DMA-BUF heap specifics will likely be in `drivers/dma-buf/heaps/Kconfig`. And, since we are talking DMA, it might be in `kernel/dma/Kconfig` too, just to be thorough. I'm thinking I'll find the definitions for `CONFIG_CMA`, `CONFIG_DMA_CMA`, `CONFIG_DMABUF_HEAPS`, and any related variables in one or more of these locations. From there, I can trace how they depend on each other and how they are used within the broader context of the kernel. This will give me a clear picture of how they are activated and what impacts they have on the system.



6/2 2026/09/05 20:47 tool grepper
0m Args:
{
  "Expression": "config CMA\\b"
}

Results:
{
  "Output": "mm/Kconfig=1048=config HAVE_SETUP_PER_CPU_AREA\n--\nmm/Kconfig-1050-\nmm/Kconfig:1051:config CMA\nmm/Kconfig-1052-\tbool \"Contiguous Memory Allocator\"\n"
}

7/2 2026/09/05 20:48 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 39958
							output: 33
							thoughts: 18
						

Thoughts:
Here's my attempt at summarizing the thought process, tailored for an expert audience:

**Diving into the Kernel Configuration Landscape**

Okay, so I need to understand this aspect of the kernel build configuration. Let's start with `mm/Kconfig`, specifically around line 1051. I'm guessing the specific detail I'm looking for relates to memory management, which is what `mm` refers to. The `Kconfig` files are always a good starting point for exploring build-time options and their dependencies. I need to refresh my memory on the likely options defined in this area of the file. Given the line number, I'm expecting to find a particular feature or configuration option related to memory management. Let's see what's defined. I'm going to start by skimming for keywords related to the relevant areas. Let's dig in and figure this out.



8/2 2026/09/05 20:48 tool read-file
0m Args:
{
  "File": "mm/Kconfig",
  "FirstLine": 1048,
  "LineCount": 40
}

Results:
{
  "Contents": "1048:\tconfig HAVE_SETUP_PER_CPU_AREA\n1049:\t\tbool\n1050:\t\n1051:\tconfig CMA\n1052:\t\tbool \"Contiguous Memory Allocator\"\n1053:\t\tdepends on MMU\n1054:\t\tselect MIGRATION\n1055:\t\tselect MEMORY_ISOLATION\n1056:\t\thelp\n1057:\t\t  This enables the Contiguous Memory Allocator which allows other\n1058:\t\t  subsystems to allocate big physically-contiguous blocks of memory.\n1059:\t\t  CMA reserves a region of memory and allows only movable pages to\n1060:\t\t  be allocated from it. This way, the kernel can use the memory for\n1061:\t\t  pagecache and when a subsystem requests for contiguous area, the\n1062:\t\t  allocated pages are migrated away to serve the contiguous request.\n1063:\t\n1064:\t\t  If unsure, say \"n\".\n1065:\t\n1066:\tconfig CMA_DEBUGFS\n1067:\t\tbool \"CMA debugfs interface\"\n1068:\t\tdepends on CMA \u0026\u0026 DEBUG_FS\n1069:\t\thelp\n1070:\t\t  Turns on the DebugFS interface for CMA.\n1071:\t\n1072:\tconfig CMA_SYSFS\n1073:\t\tbool \"CMA information through sysfs interface\"\n1074:\t\tdepends on CMA \u0026\u0026 SYSFS\n1075:\t\thelp\n1076:\t\t  This option exposes some sysfs attributes to get information\n1077:\t\t  from CMA.\n1078:\t\n1079:\tconfig CMA_AREAS\n1080:\t\tint \"Maximum count of the CMA areas\"\n1081:\t\tdepends on CMA\n1082:\t\tdefault 20 if NUMA\n1083:\t\tdefault 8\n1084:\t\thelp\n1085:\t\t  CMA allows to create CMA areas for particular purpose, mainly,\n1086:\t\t  used as device private area. This parameter sets the maximum\n1087:\t\t  number of CMA area in the system.\n"
}

9/2 2026/09/05 20:48 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 40744
							output: 38
							thoughts: 54
						

Thoughts:
**Checking DMA-BUF Heap Configuration**

Okay, let's dive right in. Given what we're working on, I need to verify which DMA-BUF heaps are enabled in the kernel configuration. I'm specifically looking at the `drivers/dma-buf/heaps/Kconfig` file. I'm going to quickly check for the presence of `DMABUF_HEAPS`, and then I need to confirm the specifics of the supported heaps. I'm particularly interested in `DMABUF_HEAPS_CMA`, which is pretty standard, and then I also want to make sure whether we might have something more specialized like `DMABUF_HEAPS_TEGRA_VPR` active, depending on the target platform. This will help clarify which buffer allocation strategies are available to us.



10/2 2026/09/05 20:48 tool read-file
0m Args:
{
  "File": "drivers/dma-buf/heaps/Kconfig",
  "FirstLine": 1,
  "LineCount": 40
}

Results:
{
  "Contents": "   1:\tconfig DMABUF_HEAPS_SYSTEM\n   2:\t\ttristate \"DMA-BUF System Heap\"\n   3:\t\tdepends on DMABUF_HEAPS\n   4:\t\thelp\n   5:\t\t  Choose this option to enable the system dmabuf heap. The system heap\n   6:\t\t  is backed by pages from the buddy allocator. If in doubt, say Y.\n   7:\t\n   8:\tconfig DMABUF_HEAPS_SYSTEM_CC_SHARED\n   9:\t\tbool \"DMA-BUF System Heap for decrypted CoCo VMs\"\n  10:\t\tdepends on DMABUF_HEAPS \u0026\u0026 ARCH_HAS_MEM_ENCRYPT \u0026\u0026 DMABUF_HEAPS_SYSTEM=y\n  11:\t\thelp\n  12:\t\t  Choose this option to enable the system_cc_shared dmabuf heap. This\n  13:\t\t  allows allocating shared (decrypted) memory for confidential computing\n  14:\t\t  (CoCo) VMs.\n  15:\t\n  16:\tconfig DMABUF_HEAPS_CMA\n  17:\t\ttristate \"DMA-BUF CMA Heap\"\n  18:\t\tdepends on DMABUF_HEAPS \u0026\u0026 DMA_CMA\n  19:\t\thelp\n  20:\t\t  Choose this option to enable dma-buf CMA heap. This heap is backed\n  21:\t\t  by the Contiguous Memory Allocator (CMA). If your system has these\n  22:\t\t  regions, you should say Y here.\n  23:\t\n  24:\tconfig DMABUF_HEAPS_TEGRA_VPR\n  25:\t\ttristate \"NVIDIA Tegra Video-Protected-Region DMA-BUF Heap\"\n  26:\t\tdepends on DMABUF_HEAPS \u0026\u0026 DMA_CMA\n  27:\t\thelp\n  28:\t\t  Choose this option to enable Video-Protected-Region (VPR) support on\n  29:\t\t  a range of NVIDIA Tegra devices. Access to VPR memory is limited to\n  30:\t\t  a subset of hardware engines and specifically disallowed from the\n  31:\t\t  CPU. The region can be fixed, in which case no linear mapping exists\n  32:\t\t  for the memory, or it can be resizable on systems that want to reuse\n  33:\t\t  the memory for other uses when content-protected video is not played\n  34:\t\t  back.\n"
}

11/2 2026/09/05 20:48 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 41585
							output: 30
							thoughts: 282
						

Thoughts:
**Analyzing the Patch: Diving into the Details**

Okay, let's break this down. First, I need to understand exactly what this patch is doing. I'll start by looking at the line-by-line changes. I see we have some updates in `Documentation/devicetree/bindings/...` and `arch/arm64/boot/dts/...` -- I'll have to investigate those bindings and DT changes later, but they're likely related to hardware configuration. Then there's `arch/arm64/mm/pageattr.c`, where I see the addition of `EXPORT_SYMBOL` for `set_direct_map_invalid_noflush` and `set_direct_map_default_noflush`. These are important symbols; the changes likely affect how memory is mapped, potentially related to cache coherency or specific memory regions on the Aarch64 architecture. I'll need to remember to look into the implications for the memory management subsystem.

Now, the main chunk of the patch seems to be in `drivers/dma-buf/dma-heap.c`. I notice several additions: `dma_heap_debug_show`, `dma_heap_init_debugfs`, `dma_heap_exit_debugfs`, and `dma_heap_exit`. These clearly point to adding debug functionality to the DMA heap subsystem, likely leveraging debugfs. The code snippet within `dma_heap_add` using `#ifdef CONFIG_DEBUG_FS` is particularly interesting. It's creating a debugfs file if `CONFIG_DEBUG_FS` is enabled, and the file name is going to be `heap->name`. However, it's conditionally including a function pointer `heap->ops->show`. *Wait a minute!* This implies that there is a way to display information through debugfs. That means there's some kind of structure holding function pointers, and those functions are then called through debugfs.

But here's my question: Does *any* `dma_heap` actually *set* `ops->show`? I'll need to dig deeper into the code to see if any specific `dma_heap` implementation is actually using this functionality. Otherwise, it's just a dead branch. I'll check other heap implementations to find out.



12/2 2026/09/05 20:48 tool grepper
0m Args:
{
  "Expression": "\\.show\\b",
  "PathPrefix": "drivers/dma-buf"
}

Results:
{
  "Output": "drivers/dma-buf/heaps/tegra-vpr.c=1011=static const struct dma_heap_ops tegra_vpr_heap_ops = {\ndrivers/dma-buf/heaps/tegra-vpr.c-1012-\t.allocate = tegra_vpr_allocate,\ndrivers/dma-buf/heaps/tegra-vpr.c:1013:\t.show = tegra_vpr_debugfs_show,\ndrivers/dma-buf/heaps/tegra-vpr.c-1014-};\n"
}

13/2 2026/09/05 20:48 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 42548
							output: 17
							thoughts: 189
						

Thoughts:
**Kernel Memory Allocation Investigation**

Okay, so I'm digging into how this `heap_ops->show` function gets set, and it seems like *only* the `tegra-vpr` driver is doing it. That feels... too specific. Something's nagging at me. Wait! Hold on a second, what about the bitmap allocation functions? I need to check `include/linux/bitmap.h`. Aha, I remember this:

```c
static __always_inline
int bitmap_allocate(unsigned long *bitmap, unsigned int pos, unsigned int len)
{
	if (find_next_bit(bitmap, pos + len, pos) < pos + len)
		return -EBUSY;
	bitmap_set(bitmap, pos, len);
	return 0;
}

static __always_inline
int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
{
	return bitmap_allocate(bitmap, pos, BIT(order));
}
```

Now, the question is, where are these actually *used* within the kernel? I need to track down the calls to `bitmap_allocate_region` and `bitmap_allocate` to see if they shed any light on how memory is being managed more broadly. Let's start searching for those function calls throughout the codebase. Maybe this will lead to something more general.



14/2 2026/09/05 20:48 tool grepper
0m Args:
{
  "Expression": "bitmap_allocate"
}

Results:
{
  "Output": "arch/powerpc/sysdev/msi_bitmap.c=53=void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)\n--\narch/powerpc/sysdev/msi_bitmap.c-59-\tspin_lock_irqsave(\u0026bmp-\u003elock, flags);\narch/powerpc/sysdev/msi_bitmap.c:60:\tbitmap_allocate_region(bmp-\u003ebitmap, hwirq, 0);\narch/powerpc/sysdev/msi_bitmap.c-61-\tspin_unlock_irqrestore(\u0026bmp-\u003elock, flags);\n--\narch/powerpc/sysdev/msi_bitmap.c=75=int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)\n--\narch/powerpc/sysdev/msi_bitmap.c-95-\narch/powerpc/sysdev/msi_bitmap.c:96:\tbitmap_allocate_region(bmp-\u003ebitmap, 0, get_count_order(bmp-\u003eirq_count));\narch/powerpc/sysdev/msi_bitmap.c-97-\n--\ndrivers/dma-buf/heaps/tegra-vpr.c=420=static int tegra_vpr_find_free_region_clustered(struct tegra_vpr *vpr,\n--\ndrivers/dma-buf/heaps/tegra-vpr.c-468-\ndrivers/dma-buf/heaps/tegra-vpr.c:469:\t\tif (!bitmap_allocate(vpr-\u003ebitmap, target, num_pages))\ndrivers/dma-buf/heaps/tegra-vpr.c-470-\t\t\treturn target;\n--\ndrivers/dma-buf/heaps/tegra-vpr.c-476-\tif (target + num_pages \u003c= vpr-\u003enum_pages \u0026\u0026\ndrivers/dma-buf/heaps/tegra-vpr.c:477:\t    !bitmap_allocate(vpr-\u003ebitmap, target, num_pages))\ndrivers/dma-buf/heaps/tegra-vpr.c-478-\t\treturn target;\n--\ndrivers/hwtracing/stm/core.c=186=static void stm_output_claim(struct stm_device *stm, struct stm_output *output)\n--\ndrivers/hwtracing/stm/core.c-195-\ndrivers/hwtracing/stm/core.c:196:\tbitmap_allocate_region(\u0026master-\u003echan_map[0], output-\u003echannel,\ndrivers/hwtracing/stm/core.c-197-\t\t\t       ilog2(output-\u003enr_chans));\n--\ndrivers/net/ethernet/chelsio/cxgb4/cxgb4_filter.c=689=static int cxgb4_set_ftid(struct tid_info *t, int fidx, int family,\n--\ndrivers/net/ethernet/chelsio/cxgb4/cxgb4_filter.c-702-\t\tif (chip_ver \u003c CHELSIO_T6)\ndrivers/net/ethernet/chelsio/cxgb4/cxgb4_filter.c:703:\t\t\tbitmap_allocate_region(t-\u003eftid_bmap, fidx, 2);\ndrivers/net/ethernet/chelsio/cxgb4/cxgb4_filter.c-704-\t\telse\ndrivers/net/ethernet/chelsio/cxgb4/cxgb4_filter.c:705:\t\t\tbitmap_allocate_region(t-\u003eftid_bmap, fidx, 1);\ndrivers/net/ethernet/chelsio/cxgb4/cxgb4_filter.c-706-\t}\n--\ndrivers/net/ethernet/chelsio/cxgb4/cxgb4_filter.c=712=static int cxgb4_set_hpftid(struct tid_info *t, int fidx, int family)\n--\ndrivers/net/ethernet/chelsio/cxgb4/cxgb4_filter.c-723-\telse\ndrivers/net/ethernet/chelsio/cxgb4/cxgb4_filter.c:724:\t\tbitmap_allocate_region(t-\u003ehpftid_bmap, fidx, 1);\ndrivers/net/ethernet/chelsio/cxgb4/cxgb4_filter.c-725-\n--\ninclude/linux/bitmap.h=17=struct device;\n--\ninclude/linux/bitmap.h-81- *  bitmap_release_region(bitmap, pos, order)   Free specified bit region\ninclude/linux/bitmap.h:82: *  bitmap_allocate_region(bitmap, pos, order)  Allocate specified bit region\ninclude/linux/bitmap.h-83- *  bitmap_from_arr32(dst, buf, nbits)          Copy nbits from u32[] buf to dst\n--\ninclude/linux/bitmap.h=713=void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)\n--\ninclude/linux/bitmap.h-718-/**\ninclude/linux/bitmap.h:719: * bitmap_allocate - allocate bitmap region\ninclude/linux/bitmap.h-720- *\t@bitmap: array of unsigned longs corresponding to the bitmap\n--\ninclude/linux/bitmap.h=729=static __always_inline\ninclude/linux/bitmap.h:730:int bitmap_allocate(unsigned long *bitmap, unsigned int pos, unsigned int len)\ninclude/linux/bitmap.h-731-{\n--\ninclude/linux/bitmap.h-738-/**\ninclude/linux/bitmap.h:739: * bitmap_allocate_region - allocate bitmap region\ninclude/linux/bitmap.h-740- *\t@bitmap: array of unsigned longs corresponding to the bitmap\n--\ninclude/linux/bitmap.h=749=static __always_inline\ninclude/linux/bitmap.h:750:int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)\ninclude/linux/bitmap.h-751-{\ninclude/linux/bitmap.h:752:\treturn bitmap_allocate(bitmap, pos, BIT(order));\ninclude/linux/bitmap.h-753-}\n--\ninclude/linux/bitmap.h=770=int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)\n--\ninclude/linux/bitmap.h-774-\tfor (pos = 0; (end = pos + BIT(order)) \u003c= bits; pos = end) {\ninclude/linux/bitmap.h:775:\t\tif (!bitmap_allocate_region(bitmap, pos, order))\ninclude/linux/bitmap.h-776-\t\t\treturn pos;\n"
}

15/2 2026/09/05 20:48 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 44723
							output: 20
							thoughts: 769
						

Thoughts:
**Analyzing CMA Allocation Changes**

Okay, let's dive into the CMA (Contiguous Memory Allocator) changes, specifically within `mm/cma.c` and `include/linux/cma.h`. First, let's zoom in on `cma_range_alloc`. I need to understand this function.

Ah, here it is: `cma_range_alloc`. It seems to handle the actual allocation within a specified memory range. I see the bitmap manipulation, aligning requests based on the provided `align` parameter, and the essential loop that checks for available contiguous memory. It iterates through the bitmap, looking for a suitable region defined by `start`, `end`, `count`, and `align`. The spinlock usage is expected for concurrency protection.

Now, I'm going to follow the call chain and understand how `cma_range_alloc` is called, specifically by  `__cma_alloc_frozen` and others. I see the flow:
*   `cma_alloc_frozen` calls `__cma_alloc_frozen`.
*   `cma_alloc_frozen_compound` also calls `__cma_alloc_frozen` (interesting: it's handling compound pages, so it's probably using orders).
*   `cma_alloc` calls `cma_alloc_frozen`. This makes sense; it seems like `cma_alloc` is the main entry point to grab the memory and the function chains into a frozen allocation.
*   `cma_alloc_at_frozen` also calls `__cma_alloc_frozen`.
*   `cma_alloc_at` calls `cma_alloc_at_frozen`.

Okay, so there's a clear distinction between the "frozen" variants and the standard allocation functions. `cma_alloc_at` and `cma_alloc_at_frozen` are interesting: they allow allocating memory at a specific offset. So now, the key question is, where are these `cma_alloc_at` and `cma_alloc_at_frozen` functions *actually* used within the kernel? I need to track down the callers.



16/2 2026/09/05 20:48 tool grepper
0m Args:
{
  "Expression": "cma_alloc_at"
}

Results:
{
  "Output": "drivers/dma-buf/heaps/tegra-vpr.c=191=static int tegra_vpr_chunk_alloc(struct tegra_vpr_chunk *chunk)\ndrivers/dma-buf/heaps/tegra-vpr.c-192-{\ndrivers/dma-buf/heaps/tegra-vpr.c:193:\tchunk-\u003estart_page = cma_alloc_at(chunk-\u003evpr-\u003ecma, chunk-\u003eoffset,\ndrivers/dma-buf/heaps/tegra-vpr.c-194-\t\t\t\t\t chunk-\u003enum_pages, false);\n--\ninclude/linux/cma.h=50=extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align,\ninclude/linux/cma.h-51-\t\t\t      bool no_warn);\ninclude/linux/cma.h:52:extern struct page *cma_alloc_at(struct cma *cma, unsigned long offset,\ninclude/linux/cma.h-53-\t\t\t\t unsigned long count, bool no_warn);\n--\ninclude/linux/cma.h=58=struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order);\ninclude/linux/cma.h:59:struct page *cma_alloc_at_frozen(struct cma *cma, unsigned long offset,\ninclude/linux/cma.h-60-\t\t\t\t unsigned long count, bool no_warn);\n--\ninclude/trace/events/cma.h=112=TRACE_EVENT(cma_alloc_busy_retry,\n--\ninclude/trace/events/cma.h-142-\ninclude/trace/events/cma.h:143:TRACE_EVENT(cma_alloc_at_start,\ninclude/trace/events/cma.h-144-\n--\ninclude/trace/events/cma.h-174-\ninclude/trace/events/cma.h:175:TRACE_EVENT(cma_alloc_at_finish,\ninclude/trace/events/cma.h-176-\n--\nmm/cma.c=878=static struct page *__cma_alloc_frozen(struct cma *cma, unsigned long start,\n--\nmm/cma.c-885-\nmm/cma.c:886:\t/* cma_alloc_at() and friends will only work with single-range CMA */\nmm/cma.c-887-\tif (WARN_ON_ONCE(start \u003e 0 \u0026\u0026 cma-\u003enranges != 1))\n--\nmm/cma.c=949=struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order)\n--\nmm/cma.c-955-\nmm/cma.c:956:struct page *cma_alloc_at_frozen(struct cma *cma, unsigned long offset,\nmm/cma.c-957-\t\t\t\t unsigned long count, bool no_warn)\n--\nmm/cma.c=985=EXPORT_SYMBOL_GPL(cma_alloc);\n--\nmm/cma.c-987-/**\nmm/cma.c:988: * cma_alloc_at() - allocate pages from contiguous area at fixed offset\nmm/cma.c-989- * @cma:     Contiguous memory region for which the allocation is performed.\n--\nmm/cma.c-996- */\nmm/cma.c:997:struct page *cma_alloc_at(struct cma *cma, unsigned long offset,\nmm/cma.c-998-\t\t\t  unsigned long count, bool no_warn)\n--\nmm/cma.c-1001-\nmm/cma.c:1002:\tpage = cma_alloc_at_frozen(cma, offset, count, no_warn);\nmm/cma.c-1003-\tif (page)\n--\nmm/cma.c-1007-}\nmm/cma.c:1008:EXPORT_SYMBOL_GPL(cma_alloc_at);\nmm/cma.c-1009-\n"
}

17/2 2026/09/05 20:48 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 47032
							output: 19
							thoughts: 1801
						

Thoughts:
**Analysis of the CMA Patch Series for Fuzzing Suitability**

Okay, let's break this down. First, the context of the whole patch series is crucial. I see changes to `cma_range_alloc()` in `mm/cma.c`. Hold on, a deeper dive is necessary – `cma_range_alloc()` gets called by `__cma_alloc_frozen()`. And where does that get called? Ah, right: `cma_alloc_frozen()` which feeds into *everywhere* CMA is used! (like DMA allocations and huge pages), as well as `cma_alloc_frozen_compound()` for hugetlb CMA and `cma_alloc_at_frozen()`, which then leads to `cma_alloc_at()`. So any change here *has* the potential to have wide impact.

Looking at the code changes, `cma_range_alloc()` has been reworked, with a new loop structure and different parameters, plus an added `WARN_ON_ONCE` assertion in `__cma_alloc_frozen()`. The `bitmap.h` changes with `bitmap_allocate()` and `bitmap_allocate_region()` could be relevant, especially in how `bitmap_find_free_region()` now uses `bitmap_allocate_region()`.

And then there's `dma-buf/dma-heap.c`. I see `dma_heap_add()` has been modified. Debugfs initialization with the new `dma_heap_init_debugfs()` and the addition of `dma_heap_exit()` have also been introduced.

Now, the critical question: is this worth fuzzing? Let's check against the fuzzing guidelines. Reachability is key. Can the modified code *actually* execute in a standard virtualized environment (QEMU or GCE) with emulated devices? The guide is clear: if it's reachable, fuzz it, regardless of "no functional change" or "refactoring" claims. Refactorings can introduce subtle shifts or uncover latent bugs, and these changes are definitely refactorings. Moreover, new or modified assertions *must* be fuzzed. And there *is* a new assertion!

I'm seeing that `mm/cma.c` is in a core subsystem (memory management), and I need to verify its reachability on x86_64, which is the standard architecture I'm concerned with. How is CMA used/reached in standard kernel configs when `CONFIG_CMA`, `DMA_CMA` or `DMABUF_HEAPS_CMA` is enabled? Let's look at syzkaller. Aha! Syzkaller's x86_64 config *enables* `CONFIG_CMA`, `CONFIG_DMA_CMA`, `CONFIG_CMA_DEBUGFS`, `CONFIG_CMA_SYSFS`, `CONFIG_DMABUF_HEAPS`, `CONFIG_DMABUF_HEAPS_CMA` and `CONFIG_DMABUF_HEAPS_SYSTEM`! This confirms reachability.  Now I just need to verify how `cma_alloc`, `__cma_alloc_frozen`, or `cma_range_alloc` are invoked.

Based on these guidelines, the changes definitely meet the criteria for fuzzing.



18/2 2026/09/05 20:48 tool grepper
0m Args:
{
  "Expression": "cma_alloc\\("
}

Results:
{
  "Output": "arch/powerpc/kvm/book3s_hv_builtin.c=58=struct page *kvm_alloc_hpt_cma(unsigned long nr_pages)\n--\narch/powerpc/kvm/book3s_hv_builtin.c-61-\narch/powerpc/kvm/book3s_hv_builtin.c:62:\treturn cma_alloc(kvm_cma, nr_pages, order_base_2(HPT_ALIGN_PAGES),\narch/powerpc/kvm/book3s_hv_builtin.c-63-\t\t\t false);\n--\ndrivers/dma-buf/heaps/cma_heap.c=281=static struct dma_buf *cma_heap_allocate(struct dma_heap *heap,\n--\ndrivers/dma-buf/heaps/cma_heap.c-307-\ndrivers/dma-buf/heaps/cma_heap.c:308:\tcma_pages = cma_alloc(cma_heap-\u003ecma, pagecount, align, false);\ndrivers/dma-buf/heaps/cma_heap.c-309-\tif (!cma_pages)\n--\ndrivers/s390/char/vmcp.c=59=static void vmcp_response_alloc(struct vmcp_session *session)\n--\ndrivers/s390/char/vmcp.c-71-\tif (order \u003e 2)\ndrivers/s390/char/vmcp.c:72:\t\tpage = cma_alloc(vmcp_cma, nr_pages, 0, false);\ndrivers/s390/char/vmcp.c-73-\tif (page) {\n--\ninclude/linux/cma.h=46=extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,\n--\ninclude/linux/cma.h-49-\t\t\t\t\tstruct cma **res_cma);\ninclude/linux/cma.h:50:extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align,\ninclude/linux/cma.h-51-\t\t\t      bool no_warn);\n--\nkernel/dma/contiguous.c=368=struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,\n--\nkernel/dma/contiguous.c-373-\nkernel/dma/contiguous.c:374:\treturn cma_alloc(dev_get_cma_area(dev), count, align, no_warn);\nkernel/dma/contiguous.c-375-}\n--\nkernel/dma/contiguous.c=393=static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp)\n--\nkernel/dma/contiguous.c-396-\nkernel/dma/contiguous.c:397:\treturn cma_alloc(cma, size \u003e\u003e PAGE_SHIFT, align, gfp \u0026 __GFP_NOWARN);\nkernel/dma/contiguous.c-398-}\n--\nmm/cma.c=956=struct page *cma_alloc_at_frozen(struct cma *cma, unsigned long offset,\n--\nmm/cma.c-964-/**\nmm/cma.c:965: * cma_alloc() - allocate pages from contiguous area\nmm/cma.c-966- * @cma:   Contiguous memory region for which the allocation is performed.\n--\nmm/cma.c-973- */\nmm/cma.c:974:struct page *cma_alloc(struct cma *cma, unsigned long count,\nmm/cma.c-975-\t\t       unsigned int align, bool no_warn)\n--\nmm/cma.c=1044=static void __cma_release_frozen(struct cma *cma, struct cma_memrange *cmr,\n--\nmm/cma.c-1062- *\nmm/cma.c:1063: * This function releases memory allocated by cma_alloc().\nmm/cma.c-1064- * It returns false when provided pages do not belong to contiguous area and\n--\nmm/cma_debug.c=129=static int cma_alloc_mem(struct cma *cma, int count)\n--\nmm/cma_debug.c-137-\nmm/cma_debug.c:138:\tp = cma_alloc(cma, count, 0, false);\nmm/cma_debug.c-139-\tif (!p) {\n"
}

19/2 2026/09/05 20:48 llm patch-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 50734
							output: 257
							thoughts: 1233
						

Thoughts:
**Analysis of Memory Allocation Changes**

Okay, so I'm looking at this code and immediately I see `cma_alloc()` is the entry point, called through `cma_heap_allocate()` and accessible through `/dev/dma_heap/cma`, `dma_alloc_from_contiguous`, or debugfs. That means we have userspace interaction here, which is key for fuzzing. `cma_alloc` then calls `__cma_alloc_frozen`, which calls `cma_range_alloc`. The changes are concentrated right in `mm/cma.c`, which is the heart of CMA allocation. Specifically, `cma_range_alloc` and `__cma_alloc_frozen` were modified. This is core functionality, any changes here warrant careful attention. New functions, `cma_alloc_at_frozen` and `cma_alloc_at`, were added, and there's a new `WARN_ON_ONCE` in `__cma_alloc_frozen`. That screams potential for new states or conditions, and thus fuzzing opportunities.

I also checked `bitmap.h` because that's often a source of off-by-one errors or resource leaks in memory management. I found `bitmap_allocate` is called by `bitmap_allocate_region`, which is used in the CMA implementation.

Next, I looked at `drivers/dma-buf/dma-heap.c`. This added debugfs support, which is interesting because that means more interfaces and potential entry points. Specifically, it added `/sys/kernel/debug/dma_heap`, which again is another vector for testing.

So, the symbols I need to focus on are: `cma_range_alloc`, `__cma_alloc_frozen`, `cma_alloc_at_frozen`, `cma_alloc_at`, `dma_heap_debug_show`, `dma_heap_add`, and `bitmap_allocate`.

Hold on, I see something about Tegra VPR in `drivers/dma-buf/heaps/tegra-vpr.c`. I know that's specific to NVIDIA Tegra SoCs, but those changes aren't directly relevant because they're not part of the core CMA or DMA heap code. The changes to mm/cma.c are, and they are compiled across all architectures.

I'm making sure these are the key symbols to fuzz.

I need to confirm that `cma_alloc`, `cma_range_alloc`, and `__cma_alloc_frozen` are specific feature functions, not ubiquitous hot paths. Yes, CMA allocation only happens when CMA is explicitly requested, not on every memory allocation. So, these are specific to CMA functionality and not the generic memory management routines. This makes them good targets.

Now for the configurations. We'll need `CMA`, `DMA_CMA`, `DMABUF_HEAPS`, `DMABUF_HEAPS_CMA`, `CMA_DEBUGFS`, and `DEBUG_FS`. Let's also check dependencies. `CMA` requires `MMU`, `DMA_CMA` needs `HAVE_DMA_CONTIGUOUS` and `CMA`, `DMABUF_HEAPS` is independent, `DMABUF_HEAPS_CMA` needs `DMABUF_HEAPS` and `DMA_CMA`, `CMA_DEBUGFS` needs `CMA` and `DEBUG_FS`, and finally `CMA_SYSFS` needs `CMA` and `SYSFS`.

Okay, I'm confident in the analysis. Here's what I've got:

`WorthFuzzing: true`
`EnableConfigs: ["CMA", "DMA_CMA", "DMABUF_HEAPS", "DMABUF_HEAPS_CMA", "CMA_DEBUGFS", "DEBUG_FS"]`
`FocusSymbols: ["cma_range_alloc", "__cma_alloc_frozen", "cma_alloc_at", "cma_alloc_at_frozen", "dma_heap_debug_show"]`

Now, let's call `set-results`.



20/2 2026/09/05 20:48 tool set-results
0m Args:
{
  "EnableConfigs": [
    "CMA",
    "DMA_CMA",
    "DMABUF_HEAPS",
    "DMABUF_HEAPS_CMA",
    "CMA_DEBUGFS",
    "DEBUG_FS"
  ],
  "FocusSymbols": [
    "cma_range_alloc",
    "__cma_alloc_frozen",
    "cma_alloc_at",
    "cma_alloc_at_frozen",
    "dma_heap_debug_show"
  ],
  "Reasoning": "The patch series modifies core memory management CMA (Contiguous Memory Allocator) logic in mm/cma.c (refactoring cma_range_alloc, updating __cma_alloc_frozen with a new WARN_ON_ONCE assertion, and introducing cma_alloc_at / cma_alloc_at_frozen), adds debugfs infrastructure to the dma-buf heap subsystem in drivers/dma-buf/dma-heap.c, and updates bitmap helper routines in include/linux/bitmap.h. These core MM and DMA-BUF subsystems are reachable in standard virtualized environments on amd64.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "CMA",
    "DMA_CMA",
    "DMABUF_HEAPS",
    "DMABUF_HEAPS_CMA",
    "CMA_DEBUGFS",
    "DEBUG_FS"
  ],
  "FocusSymbols": [
    "cma_range_alloc",
    "__cma_alloc_frozen",
    "cma_alloc_at",
    "cma_alloc_at_frozen",
    "dma_heap_debug_show"
  ],
  "Reasoning": "The patch series modifies core memory management CMA (Contiguous Memory Allocator) logic in mm/cma.c (refactoring cma_range_alloc, updating __cma_alloc_frozen with a new WARN_ON_ONCE assertion, and introducing cma_alloc_at / cma_alloc_at_frozen), adds debugfs infrastructure to the dma-buf heap subsystem in drivers/dma-buf/dma-heap.c, and updates bitmap helper routines in include/linux/bitmap.h. These core MM and DMA-BUF subsystems are reachable in standard virtualized environments on amd64.",
  "WorthFuzzing": true
}

21/1 2026/09/05 20:48 action If
0m Args:
{
  "WorthFuzzing": true
}

Results:
null

22/2 2026/09/05 20:48 agent kmsan-evaluator
0m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series introduces support for NVIDIA Tegra Video Protection Region (VPR) as a DMA-BUF heap, adds debugfs support to dma-heap, and adds fixed-offset CMA allocation support (`cma_alloc_at`).\n\nAll newly introduced kernel heap data structures (`tegra_vpr`, `tegra_vpr_buffer`, `tegra_vpr_attachment`, `tegra_vpr_device`, `tegra_vpr_chunk`, bitmaps) are zero-initialized via `kzalloc_obj`, `kzalloc_objs`, and `bitmap_zalloc`. All local variables used across CMA and VPR functions are explicitly initialized before use. Furthermore, CPU access and user mappings for VPR buffers are explicitly rejected with `-EPERM` (`tegra_vpr_mmap`, `tegra_vpr_begin_cpu_access`, `tegra_vpr_end_cpu_access`), preventing kernel or user reads of VPR memory. There are no kernel-to-user info leaks or branching on uninitialized data. Any potential concurrency, allocation bounds, or reference management issues are covered by standard sanitizers (KASAN, LOCKDEP). Therefore, dedicated KMSAN fuzzing is not required.",
  "NeedsKMSAN": false
}

Instruction:
You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.

CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
  a comprehensive suite of debugging tools and sanitizers, including KASAN
  (out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
  (locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
  or page allocations) and kernel-to-user memory info-leaks.

Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.

Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.

Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
   attributes, ioctl output arguments, socket options, or BPF buffers) where fields
   or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
   or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
   could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
   uninitialized bytes of existing buffers.

Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
  dereferences, locking deadlocks, or use-after-free bugs (these are already caught
  by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
  or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.

Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 1fa4c99c058d2ba8ddcbfcc5fff6c778e8fb6097
Author: syz-cluster <triage@syzkaller.com>
Date:   Sat Sep 5 20:47:42 2026 +0000

    syz-cluster: applied patch under review

diff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml
index bdf981781bd5f..fdd3fe9e25470 100644
--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml
+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra124-vic.yaml
@@ -68,6 +68,14 @@ properties:
       - const: dma-mem # read
       - const: write
 
+  memory-region:
+    items:
+      - description: reference to the video protection memory region
+
+  memory-region-names:
+    items:
+      - const: protected
+
   dma-coherent: true
 
 additionalProperties: false
diff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml
index ce4589466a186..881bfbf4764df 100644
--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml
+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra186-dc.yaml
@@ -57,6 +57,16 @@ properties:
       - const: dma-mem # read-0
       - const: read-1
 
+  memory-region:
+    minItems: 1
+    maxItems: 2
+
+  memory-region-names:
+    items:
+      enum: [ framebuffer, protected ]
+    minItems: 1
+    maxItems: 2
+
   nvidia,outputs:
     description: A list of phandles of outputs that this display
       controller can drive.
diff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml
index 69be95afd5627..3bf06dcd61986 100644
--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml
+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-dc.yaml
@@ -65,7 +65,15 @@ properties:
     items:
       - description: phandle to the core power domain
 
-  memory-region: true
+  memory-region:
+    minItems: 1
+    maxItems: 2
+
+  memory-region-names:
+    items:
+      enum: [ framebuffer, protected ]
+    minItems: 1
+    maxItems: 2
 
   nvidia,head:
     $ref: /schemas/types.yaml#/definitions/uint32
diff --git a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml
index 8312b7699cbed..420a1fe5ee805 100644
--- a/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml
+++ b/Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.yaml
@@ -98,6 +98,13 @@ properties:
     items:
       - description: phandle to the HEG or core power domain
 
+  memory-region:
+    maxItems: 1
+
+  memory-region-names:
+    items:
+      - const: protected
+
 required:
   - compatible
   - interrupts
diff --git a/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml b/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml
index 4eb325cfd2961..bcaaabca945dc 100644
--- a/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml
+++ b/Documentation/devicetree/bindings/gpu/host1x/nvidia,tegra234-nvdec.yaml
@@ -60,6 +60,14 @@ properties:
       - const: dma-mem
       - const: write
 
+  memory-region:
+    items:
+      - description: reference to the video protection memory region
+
+  memory-region-names:
+    items:
+      - const: protected
+
   nvidia,memory-controller:
     $ref: /schemas/types.yaml#/definitions/phandle
     description:
diff --git a/Documentation/devicetree/bindings/reserved-memory/nvidia,tegra-video-protection-region.yaml b/Documentation/devicetree/bindings/reserved-memory/nvidia,tegra-video-protection-region.yaml
new file mode 100644
index 0000000000000..862bfd3913786
--- /dev/null
+++ b/Documentation/devicetree/bindings/reserved-memory/nvidia,tegra-video-protection-region.yaml
@@ -0,0 +1,75 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/reserved-memory/nvidia,tegra-video-protection-region.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: NVIDIA Tegra Video Protection Region (VPR)
+
+maintainers:
+  - Thierry Reding <thierry.reding@gmail.com>
+  - Jon Hunter <jonathanh@nvidia.com>
+
+description: |
+  NVIDIA Tegra chips have long supported a mechanism to protect a single,
+  contiguous memory region from non-secure memory accesses. Typically this
+  region is used for decoding and playback of DRM protected content. Various
+  devices, such as the display controller and multimedia engines (video
+  decoder) can access this region in a secure way. Access from the CPU is
+  generally forbidden.
+
+  Two variants exist for VPR: one is fixed in both the base address and size,
+  while the other is resizable. Fixed VPR can be described by just a "reg"
+  property specifying the base address and size, whereas the resizable VPR
+  is defined by a size/alignment pair of properties. For resizable VPR the
+  memory is reusable by the rest of the system when it's unused for VPR and
+  therefore the "reusable" property must be specified along with it. For a
+  fixed VPR, the memory is permanently protected, and therefore it's not
+  reusable and must also be marked as "no-map" to prevent any (including
+  speculative) accesses to it.
+
+allOf:
+  - $ref: reserved-memory.yaml
+  - oneOf:
+      - required:
+          - compatible
+          - reg
+
+      - required:
+          - compatible
+          - size
+
+properties:
+  compatible:
+    const: nvidia,tegra-video-protection-region
+
+dependencies:
+  size: [alignment, reusable]
+  alignment: [size, reusable]
+  reusable: [alignment, size]
+
+  reg: [no-map]
+  no-map: [reg]
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    /* resizable VPR */
+    protected {
+      compatible = "nvidia,tegra-video-protection-region";
+
+      size = <0x0 0x70000000>;
+      alignment = <0x0 0x100000>;
+      reusable;
+    };
+
+  - |
+    /* fixed VPR */
+    protected@a8000000 {
+      compatible = "nvidia,tegra-video-protection-region";
+
+      /* fixed VPR */
+      reg = <0xa8000000 0x70000000>;
+      no-map;
+    };
diff --git a/arch/arm64/boot/dts/nvidia/tegra234.dtsi b/arch/arm64/boot/dts/nvidia/tegra234.dtsi
index 5e29316a4d759..c6a5ced069e79 100644
--- a/arch/arm64/boot/dts/nvidia/tegra234.dtsi
+++ b/arch/arm64/boot/dts/nvidia/tegra234.dtsi
@@ -29,6 +29,45 @@ aliases {
 		i2c8 = &dp_aux_ch3_i2c;
 	};
 
+	reserved-memory {
+		#address-cells = <2>;
+		#size-cells = <2>;
+		ranges;
+
+		vpr: protected {
+			compatible = "nvidia,tegra-video-protection-region";
+			status = "disabled";
+
+			/*
+			 * Two variants exist for this. For fixed VPR, the
+			 * firmware is supposed to update the "reg" property
+			 * with the fixed memory region configured as VPR.
+			 *
+			 * For resizable VPR we don't care about the exact
+			 * address and instead want a reserved region to be
+			 * allocated with a certain size and alignment at
+			 * boot time.
+			 *
+			 * The below assumes resizable VPR by default. If the
+			 * firmwares sets up fixed VPR, it is responsible for
+			 * adding the missing "reg" property, removing any of
+			 * the unused properties, as well as adding a unit-
+			 * address matching the "reg" property.
+			 */
+
+			/* fixed VPR */
+			/*
+			reg = <0x0 0x0 0x0 0x0>;
+			no-map;
+			*/
+
+			/* resizable VPR */
+			size = <0x0 0x70000000>;
+			alignment = <0x0 0x100000>;
+			reusable;
+		};
+	};
+
 	bus@0 {
 		compatible = "simple-bus";
 
@@ -4440,6 +4479,9 @@ vic@15340000 {
 				interconnect-names = "dma-mem", "write";
 				iommus = <&smmu_niso1 TEGRA234_SID_VIC>;
 				dma-coherent;
+
+				memory-region = <&vpr>;
+				memory-region-names = "protected";
 			};
 
 			nvdec@15480000 {
@@ -4458,6 +4500,9 @@ nvdec@15480000 {
 				iommus = <&smmu_niso1 TEGRA234_SID_NVDEC>;
 				dma-coherent;
 
+				memory-region = <&vpr>;
+				memory-region-names = "protected";
+
 				nvidia,memory-controller = <&mc>;
 
 				/*
diff --git a/arch/arm64/boot/dts/nvidia/tegra264.dtsi b/arch/arm64/boot/dts/nvidia/tegra264.dtsi
index 013228a817237..0123fdc0e1250 100644
--- a/arch/arm64/boot/dts/nvidia/tegra264.dtsi
+++ b/arch/arm64/boot/dts/nvidia/tegra264.dtsi
@@ -23,6 +23,39 @@ shmem_bpmp: shmem@86070000 {
 			reg = <0x0 0x86070000 0x0 0x2000>;
 			no-map;
 		};
+
+		vpr: protected {
+			compatible = "nvidia,tegra-video-protection-region";
+			status = "disabled";
+
+			/*
+			 * Two variants exist for this. For fixed VPR, the
+			 * firmware is supposed to update the "reg" property
+			 * with the fixed memory region configured as VPR.
+			 *
+			 * For resizable VPR we don't care about the exact
+			 * address and instead want a reserved region to be
+			 * allocated with a certain size and alignment at
+			 * boot time.
+			 *
+			 * The below assumes resizable VPR by default. If the
+			 * firmwares sets up fixed VPR, it is responsible for
+			 * adding the missing "reg" property, removing any of
+			 * the unused properties, as well as adding a unit-
+			 * address matching the "reg" property.
+			 */
+
+			/* fixed VPR */
+			/*
+			reg = <0x0 0x0 0x0 0x0>;
+			no-map;
+			*/
+
+			/* resizable VPR */
+			size = <0x0 0x70000000>;
+			alignment = <0x0 0x100000>;
+			reusable;
+		};
 	};
 
 	/* SYSTEM MMIO */
diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c
index 132938b32eb16..9253c529f8aa5 100644
--- a/arch/arm64/mm/pageattr.c
+++ b/arch/arm64/mm/pageattr.c
@@ -262,6 +262,7 @@ int set_direct_map_invalid_noflush(struct page *page, unsigned int numpages)
 	return update_range_prot((unsigned long)page_address(page),
 				 PAGE_SIZE * numpages, set_mask, clear_mask);
 }
+EXPORT_SYMBOL(set_direct_map_invalid_noflush);
 
 int set_direct_map_default_noflush(struct page *page, unsigned int numpages)
 {
@@ -274,6 +275,7 @@ int set_direct_map_default_noflush(struct page *page, unsigned int numpages)
 	return update_range_prot((unsigned long)page_address(page),
 				 PAGE_SIZE * numpages, set_mask, clear_mask);
 }
+EXPORT_SYMBOL(set_direct_map_default_noflush);
 
 static int __set_memory_enc_dec(unsigned long addr,
 				int numpages,
diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c
index 3937dd41bb0f6..f279b8fdd3ce0 100644
--- a/drivers/dma-buf/dma-heap.c
+++ b/drivers/dma-buf/dma-heap.c
@@ -7,6 +7,7 @@
  */
 
 #include <linux/cdev.h>
+#include <linux/debugfs.h>
 #include <linux/device.h>
 #include <linux/dma-buf.h>
 #include <linux/dma-heap.h>
@@ -225,6 +226,40 @@ const char *dma_heap_get_name(struct dma_heap *heap)
 }
 EXPORT_SYMBOL_NS_GPL(dma_heap_get_name, "DMA_BUF_HEAP");
 
+#ifdef CONFIG_DEBUG_FS
+static int dma_heap_debug_show(struct seq_file *s, void *unused)
+{
+	struct dma_heap *heap = s->private;
+	int err = 0;
+
+	if (heap->ops && heap->ops->show)
+		err = heap->ops->show(s, heap);
+
+	return err;
+}
+DEFINE_SHOW_ATTRIBUTE(dma_heap_debug);
+
+static struct dentry *dma_heap_debugfs_dir;
+
+static void dma_heap_init_debugfs(void)
+{
+	dma_heap_debugfs_dir = debugfs_create_dir("dma_heap", NULL);
+}
+
+static void dma_heap_exit_debugfs(void)
+{
+	debugfs_remove_recursive(dma_heap_debugfs_dir);
+}
+#else
+static void dma_heap_init_debugfs(void)
+{
+}
+
+static void dma_heap_exit_debugfs(void)
+{
+}
+#endif
+
 /**
  * dma_heap_add - adds a heap to dmabuf heaps
  * @exp_info: information needed to register this heap
@@ -299,6 +334,13 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
 
 	/* Add heap to the list */
 	list_add(&heap->list, &heap_list);
+
+#ifdef CONFIG_DEBUG_FS
+	if (heap->ops && heap->ops->show)
+		debugfs_create_file(heap->name, 0444, dma_heap_debugfs_dir,
+				    heap, &dma_heap_debug_fops);
+#endif
+
 	mutex_unlock(&heap_list_lock);
 
 	return heap;
@@ -335,6 +377,16 @@ static int dma_heap_init(void)
 	}
 	dma_heap_class->devnode = dma_heap_devnode;
 
+	dma_heap_init_debugfs();
+
 	return 0;
 }
 subsys_initcall(dma_heap_init);
+
+static void __exit dma_heap_exit(void)
+{
+	dma_heap_exit_debugfs();
+	class_destroy(dma_heap_class);
+	unregister_chrdev_region(dma_heap_devt, NUM_HEAP_MINORS);
+}
+__exitcall(dma_heap_exit);
diff --git a/drivers/dma-buf/heaps/Kconfig b/drivers/dma-buf/heaps/Kconfig
index bb729e91545c4..28d2c0800fb57 100644
--- a/drivers/dma-buf/heaps/Kconfig
+++ b/drivers/dma-buf/heaps/Kconfig
@@ -20,3 +20,15 @@ config DMABUF_HEAPS_CMA
 	  Choose this option to enable dma-buf CMA heap. This heap is backed
 	  by the Contiguous Memory Allocator (CMA). If your system has these
 	  regions, you should say Y here.
+
+config DMABUF_HEAPS_TEGRA_VPR
+	tristate "NVIDIA Tegra Video-Protected-Region DMA-BUF Heap"
+	depends on DMABUF_HEAPS && DMA_CMA
+	help
+	  Choose this option to enable Video-Protected-Region (VPR) support on
+	  a range of NVIDIA Tegra devices. Access to VPR memory is limited to
+	  a subset of hardware engines and specifically disallowed from the
+	  CPU. The region can be fixed, in which case no linear mapping exists
+	  for the memory, or it can be resizable on systems that want to reuse
+	  the memory for other uses when content-protected video is not played
+	  back.
diff --git a/drivers/dma-buf/heaps/Makefile b/drivers/dma-buf/heaps/Makefile
index 974467791032f..481fcb78f7572 100644
--- a/drivers/dma-buf/heaps/Makefile
+++ b/drivers/dma-buf/heaps/Makefile
@@ -1,3 +1,13 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_DMABUF_HEAPS_SYSTEM)	+= system_heap.o
 obj-$(CONFIG_DMABUF_HEAPS_CMA)		+= cma_heap.o
+
+#
+# The reserved-memory bits always need to be built-in so that the CMA
+# initialization runs during early boot. The VPR driver itself can be
+# built as a module.
+#
+ifneq ($(CONFIG_DMABUF_HEAPS_TEGRA_VPR),)
+obj-y					+= tegra-vpr-init.o
+obj-$(CONFIG_DMABUF_HEAPS_TEGRA_VPR)	+= tegra-vpr.o
+endif
diff --git a/drivers/dma-buf/heaps/tegra-vpr-init.c b/drivers/dma-buf/heaps/tegra-vpr-init.c
new file mode 100644
index 0000000000000..65e917713c6d4
--- /dev/null
+++ b/drivers/dma-buf/heaps/tegra-vpr-init.c
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DMA-BUF restricted heap exporter for NVIDIA Video-Protection-Region (VPR)
+ *
+ * Copyright (C) 2024-2026 NVIDIA Corporation
+ */
+
+#define pr_fmt(fmt) "tegra-vpr: " fmt
+
+#include "tegra-vpr.h"
+
+#include <linux/cma.h>
+#include <linux/of_reserved_mem.h>
+
+static DEFINE_MUTEX(vpr_lock);
+static LIST_HEAD_GUARDED(vpr_list, vpr_lock);
+
+static int __init tegra_vpr_node_init(unsigned long offset,
+				      struct reserved_mem *rmem)
+{
+	struct cma *cma;
+	int err;
+
+	if (!IS_ALIGNED(rmem->base, SZ_1M)) {
+		pr_err("%s: base is not aligned to 1 MiB\n", rmem->name);
+		return -EINVAL;
+	}
+
+	if (!IS_ALIGNED(rmem->size, SZ_1M)) {
+		pr_err("%s: size is not aligned to 1 MiB\n", rmem->name);
+		return -EINVAL;
+	}
+
+	err = cma_init_reserved_mem(rmem->base, rmem->size, 0, rmem->name,
+				    &cma);
+	if (err < 0) {
+		pr_err("%s: failed to initialize CMA: %d\n", __func__, err);
+		return err;
+	}
+
+	rmem->priv = cma;
+
+	return 0;
+}
+
+static struct tegra_vpr *tegra_vpr_lookup(struct cma *cma)
+{
+	struct tegra_vpr *vpr;
+
+	mutex_lock(&vpr_lock);
+
+	list_for_each_entry(vpr, &vpr_list, list) {
+		if (vpr->cma == cma) {
+			mutex_unlock(&vpr_lock);
+			return vpr;
+		}
+	}
+
+	mutex_unlock(&vpr_lock);
+
+	return ERR_PTR(-EPROBE_DEFER);
+}
+
+static int tegra_vpr_device_init(struct reserved_mem *rmem, struct device *dev)
+{
+	const struct dev_pm_ops *pm = dev->driver->pm;
+	struct tegra_vpr_device *node;
+	struct cma *cma = rmem->priv;
+	struct tegra_vpr *vpr;
+
+	vpr = tegra_vpr_lookup(cma);
+	if (IS_ERR(vpr))
+		return PTR_ERR(vpr);
+
+	if (!pm || !pm->freeze || !pm->thaw)
+		return -EINVAL;
+
+	node = kzalloc_obj(*node, GFP_KERNEL);
+	if (!node)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&node->node);
+	node->dev = dev;
+
+	mutex_lock(&vpr->lock);
+	list_add_tail(&node->node, &vpr->devices);
+	mutex_unlock(&vpr->lock);
+
+	return 0;
+}
+
+static void tegra_vpr_device_release(struct reserved_mem *rmem,
+				     struct device *dev)
+{
+	struct tegra_vpr_device *node, *tmp;
+	struct cma *cma = rmem->priv;
+	struct tegra_vpr *vpr;
+
+	vpr = tegra_vpr_lookup(cma);
+	if (IS_ERR(vpr)) {
+		dev_WARN(dev, "failed to find VPR for CMA '%s'\n",
+			 cma_get_name(cma));
+		return;
+	}
+
+	mutex_lock(&vpr->lock);
+
+	list_for_each_entry_safe(node, tmp, &vpr->devices, node) {
+		if (node->dev == dev) {
+			list_del(&node->node);
+			kfree(node);
+		}
+	}
+
+	mutex_unlock(&vpr->lock);
+}
+
+static const struct reserved_mem_ops tegra_vpr_rmem_ops = {
+	.node_init = tegra_vpr_node_init,
+	.device_init = tegra_vpr_device_init,
+	.device_release = tegra_vpr_device_release,
+};
+
+RESERVEDMEM_OF_DECLARE(tegra_vpr, "nvidia,tegra-video-protection-region",
+		       &tegra_vpr_rmem_ops);
+
+void tegra_vpr_add(struct tegra_vpr *vpr)
+{
+	mutex_lock(&vpr_lock);
+	list_add_tail(&vpr->list, &vpr_list);
+	mutex_unlock(&vpr_lock);
+}
+EXPORT_SYMBOL(tegra_vpr_add);
diff --git a/drivers/dma-buf/heaps/tegra-vpr.c b/drivers/dma-buf/heaps/tegra-vpr.c
new file mode 100644
index 0000000000000..d8cff7e07ea0b
--- /dev/null
+++ b/drivers/dma-buf/heaps/tegra-vpr.c
@@ -0,0 +1,1210 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DMA-BUF restricted heap exporter for NVIDIA Video-Protection-Region (VPR)
+ *
+ * Copyright (C) 2024-2026 NVIDIA Corporation
+ */
+
+#define pr_fmt(fmt) "tegra-vpr: " fmt
+
+#include <linux/arm-smccc.h>
+#include <linux/cma.h>
+#include <linux/debugfs.h>
+#include <linux/dma-buf.h>
+#include <linux/dma-heap.h>
+#include <linux/find.h>
+#include <linux/memory.h>
+#include <linux/of_reserved_mem.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/reset.h>
+#include <linux/set_memory.h>
+
+#include "tegra-vpr.h"
+
+MODULE_IMPORT_NS("DMA_BUF_HEAP");
+MODULE_IMPORT_NS("DMA_BUF");
+
+#define CREATE_TRACE_POINTS
+#include <trace/events/tegra_vpr.h>
+
+#define TEGRA_VPR_MAX_CHUNKS 64
+
+struct tegra_vpr_buffer {
+	struct list_head attachments;
+	struct tegra_vpr *vpr;
+	struct list_head list;
+
+	/**
+	 * @lock: Protects concurrent access to the list of attachments.
+	 */
+	struct mutex lock;
+
+	struct page **pages;
+	pgoff_t num_pages;
+	phys_addr_t start;
+	phys_addr_t limit;
+	size_t size;
+	int pageno;
+	int order;
+
+	DECLARE_BITMAP(chunks, TEGRA_VPR_MAX_CHUNKS);
+};
+
+struct tegra_vpr_attachment {
+	struct device *dev;
+	struct sg_table sgt;
+	struct list_head list;
+};
+
+#define ARM_SMCCC_TE_FUNC_PROGRAM_VPR 0x3
+
+#define ARM_SMCCC_VENDOR_SIP_TE_PROGRAM_VPR_FUNC_ID		\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,			\
+			   ARM_SMCCC_SMC_32,			\
+			   ARM_SMCCC_OWNER_SIP,			\
+			   ARM_SMCCC_TE_FUNC_PROGRAM_VPR)
+
+static int tegra_vpr_set(phys_addr_t base, phys_addr_t size)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_smc(ARM_SMCCC_VENDOR_SIP_TE_PROGRAM_VPR_FUNC_ID, base, size,
+		      0, 0, 0, 0, 0, &res);
+
+	return res.a0;
+}
+
+static int tegra_vpr_get_extents(struct tegra_vpr *vpr, phys_addr_t *base,
+				 phys_addr_t *size)
+{
+	phys_addr_t start = ~0, limit = 0;
+	unsigned int i;
+
+	for (i = 0; i < vpr->num_chunks; i++) {
+		struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+		if (chunk->active) {
+			if (chunk->start < start)
+				start = chunk->start;
+
+			if (chunk->limit > limit)
+				limit = chunk->limit;
+		}
+	}
+
+	if (limit > start) {
+		*size = limit - start;
+		*base = start;
+	} else {
+		*base = *size = 0;
+	}
+
+	return 0;
+}
+
+static int tegra_vpr_resize(struct tegra_vpr *vpr)
+{
+	struct tegra_vpr_device *node;
+	phys_addr_t base, size;
+	int err, status = 0;
+
+	err = tegra_vpr_get_extents(vpr, &base, &size);
+	if (err < 0) {
+		pr_err("%s(): failed to get VPR extents: %d\n", __func__, err);
+		return err;
+	}
+
+	list_for_each_entry(node, &vpr->devices, node) {
+		err = pm_generic_freeze(node->dev);
+		if (err < 0) {
+			pr_err("failed to freeze %s: %d\n",
+			       dev_name(node->dev), err);
+			status = err;
+			goto thaw;
+		}
+	}
+
+	trace_tegra_vpr_set(base, size);
+
+	err = tegra_vpr_set(base, size);
+	if (err < 0) {
+		pr_err("failed to secure VPR: %d\n", err);
+		status = err;
+	}
+
+thaw:
+	list_for_each_entry_continue_reverse(node, &vpr->devices, node) {
+		err = pm_generic_thaw(node->dev);
+		if (err < 0) {
+			pr_err("failed to thaw %s: %d\n",
+			       dev_name(node->dev), err);
+			continue;
+		}
+	}
+
+	return status;
+}
+
+static int tegra_vpr_chunk_init(struct tegra_vpr *vpr,
+				struct tegra_vpr_chunk *chunk,
+				phys_addr_t start, size_t size,
+				unsigned int order, const char *name)
+{
+	chunk->start = start;
+	chunk->limit = start + size;
+	chunk->size = size;
+	chunk->vpr = vpr;
+
+	chunk->offset = (start - vpr->base) >> PAGE_SHIFT;
+	chunk->num_pages = size >> PAGE_SHIFT;
+	chunk->num_buffers = 0;
+
+	/* CMA area is not reserved yet */
+	chunk->start_page = NULL;
+	chunk->virt = 0;
+
+	return 0;
+}
+
+static void tegra_vpr_chunk_free(struct tegra_vpr_chunk *chunk)
+{
+}
+
+static inline bool tegra_vpr_chunk_is_last(const struct tegra_vpr_chunk *chunk)
+{
+	phys_addr_t limit = chunk->vpr->base + chunk->vpr->size;
+
+	return chunk->limit == limit;
+}
+
+static inline bool tegra_vpr_chunk_is_leaf(const struct tegra_vpr_chunk *chunk)
+{
+	const struct tegra_vpr_chunk *next = chunk + 1;
+
+	if (tegra_vpr_chunk_is_last(chunk))
+		return true;
+
+	return !next->active;
+}
+
+static int tegra_vpr_chunk_alloc(struct tegra_vpr_chunk *chunk)
+{
+	chunk->start_page = cma_alloc_at(chunk->vpr->cma, chunk->offset,
+					 chunk->num_pages, false);
+	if (!chunk->start_page)
+		return -ENOMEM;
+
+	chunk->virt = (unsigned long)page_to_virt(chunk->start_page);
+
+	return 0;
+}
+
+static int tegra_vpr_chunk_activate(struct tegra_vpr_chunk *chunk)
+{
+	int err;
+
+	trace_tegra_vpr_chunk_activate(chunk->start, chunk->limit);
+
+	err = set_direct_map_invalid_noflush(chunk->start_page,
+					     chunk->num_pages);
+	if (err)
+		return err;
+
+	flush_tlb_kernel_range(chunk->virt, chunk->virt + chunk->size);
+	chunk->invalid = false;
+	chunk->active = true;
+
+	return 0;
+}
+
+static int tegra_vpr_chunk_deactivate(struct tegra_vpr_chunk *chunk)
+{
+	int err;
+
+	if (!chunk->active)
+		return 0;
+
+	/* do not deactivate if there are buffers left in this chunk */
+	if (WARN_ON(chunk->num_buffers > 0))
+		return -EBUSY;
+
+	trace_tegra_vpr_chunk_deactivate(chunk->start, chunk->limit);
+
+	err = set_direct_map_default_noflush(chunk->start_page,
+					     chunk->num_pages);
+	if (err)
+		return err;
+
+	flush_tlb_kernel_range(chunk->virt, chunk->virt + chunk->size);
+	chunk->invalid = false;
+	chunk->active = false;
+
+	return 0;
+}
+
+static void tegra_vpr_chunk_release(struct tegra_vpr_chunk *chunk)
+{
+	if (!WARN_ON(chunk->active || chunk->invalid)) {
+		cma_release(chunk->vpr->cma, chunk->start_page,
+			    chunk->num_pages);
+		chunk->start_page = NULL;
+		chunk->virt = 0;
+	}
+}
+
+static bool tegra_vpr_chunk_overlaps(struct tegra_vpr_chunk *chunk,
+				     unsigned int start, unsigned int limit)
+{
+	unsigned int first = chunk->offset;
+	unsigned int last = chunk->offset + chunk->num_pages - 1;
+
+	if (last < start || first >= limit)
+		return false;
+
+	return true;
+}
+
+static int tegra_vpr_activate_chunks(struct tegra_vpr *vpr,
+				     struct tegra_vpr_buffer *buffer)
+{
+	DECLARE_BITMAP(dirty, vpr->num_chunks);
+	unsigned int i, bottom, top;
+	int err = 0, ret;
+
+	bitmap_zero(dirty, vpr->num_chunks);
+
+	/* activate any inactive chunks that overlap this buffer */
+	for_each_set_bit(i, buffer->chunks, vpr->num_chunks) {
+		struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+		if (chunk->active)
+			continue;
+
+		err = tegra_vpr_chunk_alloc(chunk);
+		if (err < 0)
+			goto deactivate;
+
+		err = tegra_vpr_chunk_activate(chunk);
+		if (err < 0) {
+			tegra_vpr_chunk_release(chunk);
+			goto deactivate;
+		}
+
+		set_bit(i, vpr->active);
+		set_bit(i, dirty);
+	}
+
+	/*
+	 * Activating chunks above may have created holes, but since the VPR
+	 * can only ever be a single contiguous region, make sure to activate
+	 * any missing chunks.
+	 */
+	for_each_clear_bitrange(bottom, top, vpr->active, vpr->num_chunks) {
+		/* inactive chunks at the bottom or the top are harmless */
+		if (bottom == 0 || top == vpr->num_chunks)
+			continue;
+
+		for (i = bottom; i < top; i++) {
+			struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+			err = tegra_vpr_chunk_alloc(chunk);
+			if (err < 0)
+				goto deactivate;
+
+			err = tegra_vpr_chunk_activate(chunk);
+			if (err < 0) {
+				tegra_vpr_chunk_release(chunk);
+				goto deactivate;
+			}
+
+			set_bit(i, vpr->active);
+			set_bit(i, dirty);
+		}
+	}
+
+	/* if any chunks have been activated, VPR needs to be resized */
+	if (!bitmap_empty(dirty, vpr->num_chunks)) {
+		err = tegra_vpr_resize(vpr);
+		if (err < 0) {
+			pr_err("failed to grow VPR: %d\n", err);
+			goto deactivate;
+		}
+	}
+
+	/* increment buffer count for each chunk */
+	for_each_set_bit(i, buffer->chunks, vpr->num_chunks)
+		vpr->chunks[i].num_buffers++;
+
+	return 0;
+
+deactivate:
+	/* deactivate any of the previously inactive chunks on failure */
+	for_each_set_bit(i, dirty, vpr->num_chunks) {
+		struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+		ret = tegra_vpr_chunk_deactivate(chunk);
+		if (WARN_ON(ret < 0)) {
+			pr_err("failed to deactivate chunk #%u: %d\n", i, ret);
+		} else {
+			tegra_vpr_chunk_release(chunk);
+			clear_bit(i, vpr->active);
+		}
+	}
+
+	return err;
+}
+
+/*
+ * Retrieve the range of pages within the activate region of the VPR.
+ */
+static bool tegra_vpr_get_active_range(struct tegra_vpr *vpr,
+				       unsigned int *first,
+				       unsigned int *last)
+{
+	unsigned long i, j;
+
+	i = find_first_bit(vpr->active, vpr->num_chunks);
+	if (i >= vpr->num_chunks)
+		return false;
+
+	j = find_last_bit(vpr->active, vpr->num_chunks);
+	if (j >= vpr->num_chunks)
+		return false;
+
+	*first = vpr->chunks[i].offset;
+	*last = vpr->chunks[j].offset + vpr->chunks[j].num_pages;
+
+	return true;
+}
+
+/*
+ * Try to find and allocate a free region within a specific page range.
+ * Returns the page number if successful, -ENOSPC otherwise.
+ *
+ * This function mimics bitmap_find_free_region() but restricts the search
+ * to a specific range to enable allocation within individual chunks.
+ */
+static int tegra_vpr_find_free_region_in_range(struct tegra_vpr *vpr,
+					       unsigned int start_page,
+					       unsigned int end_page,
+					       unsigned int num_pages,
+					       unsigned int align)
+{
+	unsigned int pos, next = ALIGN(start_page, align);
+
+	/* Scan through aligned positions, trying to allocate at each one */
+	for (pos = next; pos + num_pages <= end_page; pos = next) {
+		next = find_next_bit(vpr->bitmap, pos + num_pages, pos);
+
+		if (next >= pos + num_pages) {
+			bitmap_set(vpr->bitmap, pos, num_pages);
+			return pos;
+		}
+
+		next = find_next_zero_bit(vpr->bitmap, vpr->num_pages, next);
+		next = ALIGN(next, align);
+	}
+
+	return -ENOSPC;
+}
+
+static int tegra_vpr_find_free_region(struct tegra_vpr *vpr,
+				      unsigned int num_pages,
+				      unsigned long align)
+{
+	return tegra_vpr_find_free_region_in_range(vpr, 0, vpr->num_pages - 1,
+						   num_pages, align);
+}
+
+static int tegra_vpr_find_free_region_clustered(struct tegra_vpr *vpr,
+						unsigned int num_pages,
+						unsigned int align)
+{
+	unsigned int target, first, last;
+	int pageno;
+
+	/*
+	 * If there are no allocations, abort the clustered allocation scheme
+	 * and use the generic allocation scheme instead.
+	 */
+	if (vpr->first > vpr->last)
+		return -ENOSPC;
+
+	/*
+	 * First, try to allocate within the currently allocated region. This
+	 * keeps allocations tightly packed and minimizes the VPR size needed.
+	 */
+	pageno = tegra_vpr_find_free_region_in_range(vpr, vpr->first,
+						     vpr->last + 1, num_pages,
+						     align);
+	if (pageno >= 0)
+		return pageno;
+
+	/*
+	 * If not enough free space exists within the currently allocated
+	 * region, check to see if the allocation fits anywhere within the
+	 * active region, avoiding the need to resize the VPR.
+	 */
+	if (tegra_vpr_get_active_range(vpr, &first, &last)) {
+		pageno = tegra_vpr_find_free_region_in_range(vpr, first, last,
+							     num_pages, align);
+		if (pageno >= 0)
+			return pageno;
+	}
+
+	/*
+	 * If not enough free space exists within the currently active region,
+	 * try to allocate adjacent to it to grow it contiguously and ensure
+	 * optimal packing.
+	 */
+
+	/*
+	 * Calculate where the allocation should start to end right at the
+	 * first allocated page, with proper alignment.
+	 */
+	if (vpr->first >= num_pages) {
+		target = ALIGN_DOWN(vpr->first - num_pages, align);
+
+		if (!bitmap_allocate(vpr->bitmap, target, num_pages))
+			return target;
+	}
+
+	/* Try after the last allocation */
+	target = ALIGN(vpr->last + 1, align);
+
+	if (target + num_pages <= vpr->num_pages &&
+	    !bitmap_allocate(vpr->bitmap, target, num_pages))
+		return target;
+
+	/*
+	 * Couldn't allocate at the ideal adjacent position, search for any
+	 * available space before the first allocated page.
+	 */
+	pageno = tegra_vpr_find_free_region_in_range(vpr, 0, vpr->first,
+						     num_pages, align);
+	if (pageno >= 0)
+		return pageno;
+
+	/*
+	 * Couldn't allocate at the ideal adjacent position, search
+	 * for any available space after the last allocated page.
+	 */
+	pageno = tegra_vpr_find_free_region_in_range(vpr, vpr->last + 1,
+						     vpr->num_pages, num_pages,
+						     align);
+	if (pageno >= 0)
+		return pageno;
+
+	return -ENOSPC;
+}
+
+/*
+ * Find a free region, preferring locations near existing allocations to
+ * minimize VPR fragmentation. The allocation strategy is to first allocate
+ * within or adjacent to the existing region to keep allocations clustered.
+ * Otherwise fall back to a generic allocation using the first available
+ * space.
+ *
+ * This approach focuses on page-level allocation first, then the chunk
+ * system determines which chunks need to be activated based on where the
+ * pages ended up.
+ */
+static int tegra_vpr_allocate_region(struct tegra_vpr *vpr,
+				     unsigned int num_pages,
+				     unsigned int align)
+{
+	int pageno;
+
+	/*
+	 * For non-resizable VPR (no chunks), use simple first-fit allocation.
+	 * Clustering optimization is only beneficial for resizable VPR where
+	 * keeping allocations together minimizes the active VPR size.
+	 */
+	if (!vpr->resizable)
+		return tegra_vpr_find_free_region(vpr, num_pages, align);
+
+	/*
+	 * Check if there are any existing allocations in the bitmap. If so,
+	 * try to allocate near them to minimize fragmentation.
+	 */
+	pageno = tegra_vpr_find_free_region_clustered(vpr, num_pages, align);
+	if (pageno >= 0)
+		return pageno;
+
+	/*
+	 * If there are no existing allocations, or no space adjacent to them,
+	 * fall back to the first available space anywhere in the VPR.
+	 */
+	pageno = tegra_vpr_find_free_region(vpr, num_pages, align);
+	if (pageno >= 0)
+		return pageno;
+
+	return -ENOSPC;
+}
+
+static struct tegra_vpr_buffer *
+tegra_vpr_buffer_allocate(struct tegra_vpr *vpr, size_t size)
+{
+	unsigned int num_pages = size >> PAGE_SHIFT;
+	unsigned int order = get_order(size);
+	struct tegra_vpr_buffer *buffer;
+	unsigned long first, last;
+	int pageno, err;
+
+	/*
+	 * Quick sanity check that we're not trying to allocate a buffer that
+	 * has no chance of fitting into the VPR.
+	 */
+	if (size > vpr->size)
+		return ERR_PTR(-EINVAL);
+
+	/*
+	 * "order" defines the alignment and size, so this may result in
+	 * fragmented memory depending on the allocation patterns. However,
+	 * since this is used primarily for video frames, it is expected that
+	 * a number of buffers of the same size will be allocated, so
+	 * fragmentation should be negligible.
+	 */
+	pageno = tegra_vpr_allocate_region(vpr, num_pages, 1);
+	if (pageno < 0)
+		return ERR_PTR(pageno);
+
+	first = find_first_bit(vpr->bitmap, vpr->num_pages);
+	last = find_last_bit(vpr->bitmap, vpr->num_pages);
+
+	buffer = kzalloc_obj(*buffer, GFP_KERNEL);
+	if (!buffer) {
+		err = -ENOMEM;
+		goto release;
+	}
+
+	INIT_LIST_HEAD(&buffer->attachments);
+	INIT_LIST_HEAD(&buffer->list);
+	mutex_init(&buffer->lock);
+	buffer->start = vpr->base + (pageno << PAGE_SHIFT);
+	buffer->limit = buffer->start + size;
+	buffer->size = size;
+	buffer->num_pages = num_pages;
+	buffer->pageno = pageno;
+	buffer->order = order;
+
+	/* track which chunks this buffer overlaps */
+	if (vpr->resizable) {
+		unsigned int limit = buffer->pageno + buffer->num_pages;
+		pgoff_t i;
+
+		/*
+		 * Memory is backed by struct page, so track which ones we
+		 * use.
+		 */
+		buffer->pages = kvmalloc_array(buffer->num_pages,
+					       sizeof(*buffer->pages),
+					       GFP_KERNEL);
+		if (!buffer->pages) {
+			err = -ENOMEM;
+			goto free;
+		}
+
+		for (i = 0; i < buffer->num_pages; i++)
+			buffer->pages[i] = &vpr->start_page[pageno + i];
+
+		for (i = 0; i < vpr->num_chunks; i++) {
+			struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+			if (tegra_vpr_chunk_overlaps(chunk, pageno, limit))
+				set_bit(i, buffer->chunks);
+		}
+
+		/* activate chunks if necessary */
+		err = tegra_vpr_activate_chunks(vpr, buffer);
+		if (err < 0) {
+			kfree(buffer->pages);
+			goto free;
+		}
+
+		/* track first and last allocated pages */
+		if (buffer->pageno < vpr->first)
+			vpr->first = buffer->pageno;
+
+		if (limit - 1 > vpr->last)
+			vpr->last = limit - 1;
+	}
+
+	return buffer;
+
+free:
+	kfree(buffer);
+release:
+	bitmap_clear(vpr->bitmap, pageno, num_pages);
+	return ERR_PTR(err);
+}
+
+static void tegra_vpr_buffer_release(struct tegra_vpr_buffer *buffer)
+{
+	struct tegra_vpr *vpr = buffer->vpr;
+	struct tegra_vpr_buffer *entry;
+	unsigned int i;
+
+	/*
+	 * Decrement buffer count for each overlapping chunk. Note that chunks
+	 * are not deactivated here yet, that's done in tegra_vpr_recycle()
+	 * instead.
+	 */
+	for_each_set_bit(i, buffer->chunks, vpr->num_chunks) {
+		if (!WARN_ON(vpr->chunks[i].num_buffers == 0))
+			vpr->chunks[i].num_buffers--;
+	}
+
+	/* track first and last allocated pages */
+	if (list_is_first(&buffer->list, &vpr->buffers) &&
+	    list_is_last(&buffer->list, &vpr->buffers)) {
+		/* if there are no remaining buffers after this, reset */
+		vpr->first = ~0U;
+		vpr->last = 0U;
+	} else if (list_is_first(&buffer->list, &vpr->buffers)) {
+		entry = list_next_entry(buffer, list);
+		vpr->first = entry->pageno;
+	} else if (list_is_last(&buffer->list, &vpr->buffers)) {
+		entry = list_prev_entry(buffer, list);
+		vpr->last = entry->pageno + entry->num_pages - 1;
+	}
+
+	bitmap_clear(vpr->bitmap, buffer->pageno, buffer->num_pages);
+	list_del(&buffer->list);
+	kfree(buffer->pages);
+	kfree(buffer);
+}
+
+static int tegra_vpr_attach(struct dma_buf *buf,
+			    struct dma_buf_attachment *attachment)
+{
+	struct tegra_vpr_buffer *buffer = buf->priv;
+	struct tegra_vpr_attachment *attach;
+	int err;
+
+	attach = kzalloc_obj(*attach, GFP_KERNEL);
+	if (!attach)
+		return -ENOMEM;
+
+	/*
+	 * For resizable VPR, the memory is backed by struct page, so we can
+	 * use the convenient helper to create the SG table.
+	 */
+	if (buffer->pages) {
+		err = sg_alloc_table_from_pages(&attach->sgt, buffer->pages,
+						buffer->num_pages, 0,
+						buffer->size, GFP_KERNEL);
+		if (err < 0)
+			goto free;
+	} else {
+		if (sg_alloc_table(&attach->sgt, 1, GFP_KERNEL)) {
+			err = -ENOMEM;
+			goto free;
+		}
+
+		sg_set_page(attach->sgt.sgl, NULL, buffer->size, 0);
+		sg_dma_address(attach->sgt.sgl) = buffer->start;
+		sg_dma_len(attach->sgt.sgl) = buffer->size;
+	}
+
+	attach->dev = attachment->dev;
+	INIT_LIST_HEAD(&attach->list);
+	attachment->priv = attach;
+
+	mutex_lock(&buffer->lock);
+	list_add(&attach->list, &buffer->attachments);
+	mutex_unlock(&buffer->lock);
+
+	return 0;
+
+free:
+	kfree(attach);
+	return err;
+}
+
+static void tegra_vpr_detach(struct dma_buf *buf,
+			     struct dma_buf_attachment *attachment)
+{
+	struct tegra_vpr_buffer *buffer = buf->priv;
+	struct tegra_vpr_attachment *attach = attachment->priv;
+
+	mutex_lock(&buffer->lock);
+	list_del(&attach->list);
+	mutex_unlock(&buffer->lock);
+
+	sg_free_table(&attach->sgt);
+	kfree(attach);
+}
+
+static struct sg_table *
+tegra_vpr_map_dma_buf(struct dma_buf_attachment *attachment,
+		      enum dma_data_direction direction)
+{
+	struct tegra_vpr_attachment *attach = attachment->priv;
+	struct sg_table *sgt = &attach->sgt;
+	int err;
+
+	err = dma_map_sgtable(attachment->dev, sgt, direction,
+			      DMA_ATTR_SKIP_CPU_SYNC);
+	if (err < 0)
+		return ERR_PTR(err);
+
+	return sgt;
+}
+
+static void tegra_vpr_unmap_dma_buf(struct dma_buf_attachment *attachment,
+				    struct sg_table *sgt,
+				    enum dma_data_direction direction)
+{
+	dma_unmap_sgtable(attachment->dev, sgt, direction,
+			  DMA_ATTR_SKIP_CPU_SYNC);
+}
+
+static void tegra_vpr_recycle(struct tegra_vpr *vpr)
+{
+	DECLARE_BITMAP(dirty, vpr->num_chunks);
+	unsigned int i;
+	int err;
+
+	if (!vpr->resizable)
+		return;
+
+	bitmap_zero(dirty, vpr->num_chunks);
+
+	/*
+	 * Deactivate any unused chunks from the bottom...
+	 */
+	for (i = 0; i < vpr->num_chunks; i++) {
+		struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+		if (!chunk->active)
+			continue;
+
+		if (chunk->num_buffers > 0)
+			break;
+
+		err = tegra_vpr_chunk_deactivate(chunk);
+		if (err < 0) {
+			pr_err("failed to deactivate chunk #%u: %d\n", i, err);
+			goto activate;
+		} else {
+			clear_bit(i, vpr->active);
+			set_bit(i, dirty);
+		}
+	}
+
+	/*
+	 * ... and the top.
+	 */
+	for (i = 0; i < vpr->num_chunks; i++) {
+		unsigned int index = vpr->num_chunks - i - 1;
+		struct tegra_vpr_chunk *chunk = &vpr->chunks[index];
+
+		if (!chunk->active)
+			continue;
+
+		if (chunk->num_buffers > 0)
+			break;
+
+		err = tegra_vpr_chunk_deactivate(chunk);
+		if (err < 0) {
+			pr_err("failed to deactivate chunk #%u: %d\n", index,
+			       err);
+			goto activate;
+		} else {
+			clear_bit(index, vpr->active);
+			set_bit(index, dirty);
+		}
+	}
+
+	if (!bitmap_empty(dirty, vpr->num_chunks)) {
+		err = tegra_vpr_resize(vpr);
+		if (err < 0) {
+			pr_err("failed to shrink VPR: %d\n", err);
+			goto activate;
+		}
+	}
+
+	/* release the CMA memory associated with deactivated chunks */
+	for_each_set_bit(i, dirty, vpr->num_chunks)
+		tegra_vpr_chunk_release(&vpr->chunks[i]);
+
+	return;
+
+activate:
+	for_each_set_bit(i, dirty, vpr->num_chunks) {
+		err = tegra_vpr_chunk_activate(&vpr->chunks[i]);
+		if (WARN_ON(err < 0))
+			pr_err("failed to activate chunk #%u: %d\n", i, err);
+
+		/*
+		 * This may not be fully activated at this point, but we need
+		 * to keep track of it anyway to make sure the CMA region can
+		 * eventually be released. The WARN_ON above tells us when it
+		 * happens: here be dragons.
+		 */
+		set_bit(i, vpr->active);
+	}
+}
+
+static void tegra_vpr_release(struct dma_buf *buf)
+{
+	struct tegra_vpr_buffer *buffer = buf->priv;
+	struct tegra_vpr *vpr = buffer->vpr;
+
+	mutex_lock(&vpr->lock);
+
+	tegra_vpr_buffer_release(buffer);
+	tegra_vpr_recycle(vpr);
+
+	mutex_unlock(&vpr->lock);
+}
+
+/*
+ * Prohibit userspace mapping because the CPU cannot access this memory
+ * anyway.
+ */
+static int tegra_vpr_begin_cpu_access(struct dma_buf *buf,
+				      enum dma_data_direction direction)
+{
+	return -EPERM;
+}
+
+static int tegra_vpr_end_cpu_access(struct dma_buf *buf,
+				    enum dma_data_direction direction)
+{
+	return -EPERM;
+}
+
+static int tegra_vpr_mmap(struct dma_buf *buf, struct vm_area_struct *vma)
+{
+	return -EPERM;
+}
+
+static const struct dma_buf_ops tegra_vpr_buf_ops = {
+	.attach = tegra_vpr_attach,
+	.detach = tegra_vpr_detach,
+	.map_dma_buf = tegra_vpr_map_dma_buf,
+	.unmap_dma_buf = tegra_vpr_unmap_dma_buf,
+	.release = tegra_vpr_release,
+	.begin_cpu_access = tegra_vpr_begin_cpu_access,
+	.end_cpu_access = tegra_vpr_end_cpu_access,
+	.mmap = tegra_vpr_mmap,
+};
+
+static struct dma_buf *tegra_vpr_allocate(struct dma_heap *heap,
+					  unsigned long len, u32 fd_flags,
+					  u64 heap_flags)
+{
+	struct tegra_vpr *vpr = dma_heap_get_drvdata(heap);
+	struct tegra_vpr_buffer *buffer, *entry;
+	size_t size = ALIGN(len, vpr->align);
+	DEFINE_DMA_BUF_EXPORT_INFO(export);
+	struct dma_buf *buf;
+
+	mutex_lock(&vpr->lock);
+
+	buffer = tegra_vpr_buffer_allocate(vpr, size);
+	if (IS_ERR(buffer)) {
+		mutex_unlock(&vpr->lock);
+		return ERR_CAST(buffer);
+	}
+
+	/* insert in the correct order */
+	if (!list_empty(&vpr->buffers)) {
+		list_for_each_entry(entry, &vpr->buffers, list) {
+			if (buffer->pageno < entry->pageno) {
+				list_add_tail(&buffer->list, &entry->list);
+				break;
+			}
+		}
+	}
+
+	if (list_empty(&buffer->list))
+		list_add_tail(&buffer->list, &vpr->buffers);
+
+	buffer->vpr = vpr;
+
+	/*
+	 * If a valid buffer was allocated, wrap it in a dma_buf
+	 * and return it.
+	 */
+	export.exp_name = dma_heap_get_name(heap);
+	export.ops = &tegra_vpr_buf_ops;
+	export.size = buffer->size;
+	export.flags = fd_flags;
+	export.priv = buffer;
+
+	buf = dma_buf_export(&export);
+	if (IS_ERR(buf)) {
+		tegra_vpr_buffer_release(buffer);
+		tegra_vpr_recycle(vpr);
+	}
+
+	mutex_unlock(&vpr->lock);
+	return buf;
+}
+
+static void tegra_vpr_debugfs_show_buffers(struct tegra_vpr *vpr,
+					   struct seq_file *s)
+{
+	struct tegra_vpr_buffer *buffer;
+	char buf[16];
+
+	mutex_lock(&vpr->lock);
+
+	list_for_each_entry(buffer, &vpr->buffers, list) {
+		string_get_size(buffer->size, 1, STRING_UNITS_2, buf,
+				sizeof(buf));
+		seq_printf(s, "  %pap-%pap (%s)\n", &buffer->start,
+			   &buffer->limit, buf);
+	}
+
+	mutex_unlock(&vpr->lock);
+}
+
+static void tegra_vpr_debugfs_show_chunks(struct tegra_vpr *vpr,
+					  struct seq_file *s)
+{
+	struct tegra_vpr_buffer *buffer;
+	unsigned int i;
+	char buf[16];
+
+	for (i = 0; i < vpr->num_chunks; i++) {
+		const struct tegra_vpr_chunk *chunk = &vpr->chunks[i];
+
+		string_get_size(chunk->size, 1, STRING_UNITS_2, buf,
+				sizeof(buf));
+		seq_printf(s, "  %pap-%pap (%s) (%s, %u buffers)\n",
+			   &chunk->start, &chunk->limit, buf,
+			   chunk->active ? "active" : "inactive",
+			   chunk->num_buffers);
+	}
+
+	list_for_each_entry(buffer, &vpr->buffers, list) {
+		string_get_size(buffer->size, 1, STRING_UNITS_2, buf,
+				sizeof(buf));
+		seq_printf(s, "%pap-%pap (%s, chunks: %*pbl)\n",
+			   &buffer->start, &buffer->limit, buf,
+			   vpr->num_chunks, buffer->chunks);
+	}
+}
+
+static int tegra_vpr_debugfs_show(struct seq_file *s, struct dma_heap *heap)
+{
+	struct tegra_vpr *vpr = dma_heap_get_drvdata(heap);
+	phys_addr_t limit = vpr->base + vpr->size;
+	char buf[16];
+
+	string_get_size(vpr->size, 1, STRING_UNITS_2, buf, sizeof(buf));
+	seq_printf(s, "%pap-%pap (%s)\n", &vpr->base, &limit, buf);
+
+	if (!vpr->resizable)
+		tegra_vpr_debugfs_show_buffers(vpr, s);
+	else
+		tegra_vpr_debugfs_show_chunks(vpr, s);
+
+	return 0;
+}
+
+static const struct dma_heap_ops tegra_vpr_heap_ops = {
+	.allocate = tegra_vpr_allocate,
+	.show = tegra_vpr_debugfs_show,
+};
+
+static int tegra_vpr_setup_chunks(struct tegra_vpr *vpr, const char *name)
+{
+	phys_addr_t start, limit;
+	unsigned int order, i = 0;
+	size_t max_size;
+	int err;
+
+	/* Memory is backed by struct page, so track the first one. */
+	vpr->start_page = phys_to_page(vpr->base);
+
+	/* This seems a reasonable value, so hard-code it for now. */
+	vpr->num_chunks = 4;
+
+	vpr->chunks = kzalloc_objs(*vpr->chunks, vpr->num_chunks);
+	if (!vpr->chunks)
+		return -ENOMEM;
+
+	vpr->active = bitmap_zalloc(vpr->num_chunks, GFP_KERNEL);
+	if (!vpr->active) {
+		err = -ENOMEM;
+		goto free;
+	}
+
+	max_size = PAGE_SIZE << (get_order(vpr->size) - ilog2(vpr->num_chunks));
+	order = get_order(vpr->align);
+
+	/*
+	 * Allocate CMA areas for VPR. All areas will be roughtly the same
+	 * size, with the last area taking up the rest.
+	 */
+	start = vpr->base;
+	limit = vpr->base + vpr->size;
+
+	pr_debug("VPR: %pap-%pap (%lu pages, %u chunks, %lu MiB)\n", &start,
+		 &limit, vpr->num_pages, vpr->num_chunks,
+		 (unsigned long)vpr->size / 1024 / 1024);
+
+	for (i = 0; i < vpr->num_chunks; i++) {
+		size_t size = limit - start;
+		phys_addr_t end;
+
+		size = min_t(size_t, size, max_size);
+		end = start + size - 1;
+
+		err = tegra_vpr_chunk_init(vpr, &vpr->chunks[i], start, size,
+					   order, name);
+		if (err < 0) {
+			pr_err("failed to create VPR chunk: %d\n", err);
+			goto free;
+		}
+
+		pr_debug("  %2u: %pap-%pap (%lu MiB)\n", i, &start, &end,
+			 size / 1024 / 1024);
+		start += size;
+	}
+
+	vpr->first = ~0U;
+	vpr->last = 0U;
+
+	return 0;
+
+free:
+	while (i--)
+		tegra_vpr_chunk_free(&vpr->chunks[i]);
+
+	kfree(vpr->active);
+	kfree(vpr->chunks);
+	return err;
+}
+
+static void tegra_vpr_free_chunks(struct tegra_vpr *vpr)
+{
+	unsigned int i;
+
+	for (i = 0; i < vpr->num_chunks; i++)
+		tegra_vpr_chunk_free(&vpr->chunks[i]);
+
+	kfree(vpr->chunks);
+}
+
+static int tegra_vpr_setup_static(struct tegra_vpr *vpr)
+{
+	phys_addr_t start, limit;
+
+	start = vpr->base;
+	limit = vpr->base + vpr->size;
+
+	pr_debug("VPR: %pap-%pap (%lu pages, %lu MiB)\n", &start, &limit,
+		 vpr->num_pages, (unsigned long)vpr->size / 1024 / 1024);
+
+	return 0;
+}
+
+static int tegra_vpr_add_heap(struct reserved_mem *rmem,
+			      struct device_node *np)
+{
+	struct dma_heap_export_info info = {};
+	unsigned long first, last;
+	struct dma_heap *heap;
+	struct tegra_vpr *vpr;
+	int err;
+
+	vpr = kzalloc_obj(*vpr, GFP_KERNEL);
+	if (!vpr)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&vpr->list);
+	INIT_LIST_HEAD(&vpr->buffers);
+	INIT_LIST_HEAD(&vpr->devices);
+	mutex_init(&vpr->lock);
+
+	vpr->resizable = !of_property_read_bool(np, "no-map");
+	vpr->dev_node = of_node_get(np);
+	vpr->align = PAGE_SIZE;
+	vpr->base = rmem->base;
+	vpr->size = rmem->size;
+	vpr->num_pages = vpr->size >> PAGE_SHIFT;
+	vpr->nid = of_node_to_nid(np);
+	vpr->cma = rmem->priv;
+
+	vpr->bitmap = bitmap_zalloc(vpr->num_pages, GFP_KERNEL);
+	if (!vpr->bitmap) {
+		err = -ENOMEM;
+		goto free;
+	}
+
+	first = find_first_bit(vpr->bitmap, vpr->num_pages);
+	last = find_last_bit(vpr->bitmap, vpr->num_pages);
+
+	if (vpr->resizable)
+		err = tegra_vpr_setup_chunks(vpr, rmem->name);
+	else
+		err = tegra_vpr_setup_static(vpr);
+
+	if (err < 0)
+		goto free;
+
+	info.name = vpr->dev_node->name;
+	info.ops = &tegra_vpr_heap_ops;
+	info.priv = vpr;
+
+	heap = dma_heap_add(&info);
+	if (IS_ERR(heap)) {
+		err = PTR_ERR(heap);
+		goto cleanup;
+	}
+
+	tegra_vpr_add(vpr);
+
+	return 0;
+
+cleanup:
+	if (vpr->resizable)
+		tegra_vpr_free_chunks(vpr);
+free:
+	bitmap_free(vpr->bitmap);
+	kfree(vpr);
+	return err;
+}
+
+static int tegra_vpr_init(void)
+{
+	const char *compatible = "nvidia,tegra-video-protection-region";
+	struct device_node *parent;
+	struct reserved_mem *rmem;
+	int err;
+
+	parent = of_find_node_by_path("/reserved-memory");
+	if (!parent)
+		return 0;
+
+	for_each_child_of_node_scoped(parent, child) {
+		if (!of_device_is_compatible(child, compatible))
+			continue;
+
+		rmem = of_reserved_mem_lookup(child);
+		if (!rmem)
+			continue;
+
+		err = tegra_vpr_add_heap(rmem, child);
+		if (err < 0)
+			pr_err("failed to add VPR heap for %pOF: %d\n", child,
+			       err);
+
+		/* only a single VPR heap is supported */
+		break;
+	}
+
+	of_node_put(parent);
+	return 0;
+}
+module_init(tegra_vpr_init);
+
+MODULE_DESCRIPTION("NVIDIA Tegra Video-Protection-Region DMA-BUF heap driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/dma-buf/heaps/tegra-vpr.h b/drivers/dma-buf/heaps/tegra-vpr.h
new file mode 100644
index 0000000000000..ba2afba8cc617
--- /dev/null
+++ b/drivers/dma-buf/heaps/tegra-vpr.h
@@ -0,0 +1,73 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * DMA-BUF restricted heap exporter for NVIDIA Video-Protection-Region (VPR)
+ *
+ * Copyright (C) 2024-2026 NVIDIA Corporation
+ */
+
+#ifndef DMA_BUF_HEAPS_TEGRA_VPR_H
+#define DMA_BUF_HEAPS_TEGRA_VPR_H
+
+#include <linux/list.h>
+#include <linux/mutex.h>
+
+struct tegra_vpr_device {
+	struct list_head node;
+	struct device *dev;
+};
+
+struct tegra_vpr;
+
+struct tegra_vpr_chunk {
+	phys_addr_t start;
+	phys_addr_t limit;
+	size_t size;
+
+	struct tegra_vpr *vpr;
+	bool invalid;
+	bool active;
+
+	struct page *start_page;
+	unsigned int offset;
+	unsigned long virt;
+	pgoff_t num_pages;
+
+	unsigned int num_buffers;
+};
+
+struct tegra_vpr {
+	struct list_head list;
+
+	struct device_node *dev_node;
+	unsigned long align;
+	phys_addr_t base;
+	phys_addr_t size;
+	int nid;
+
+	struct list_head buffers;
+	unsigned long *bitmap;
+	pgoff_t num_pages;
+
+	/* resizable VPR */
+	struct cma *cma;
+	unsigned long *active;
+	struct tegra_vpr_chunk *chunks;
+	unsigned int num_chunks;
+	struct page *start_page;
+	bool resizable;
+
+	unsigned int first;
+	unsigned int last;
+
+	struct list_head devices;
+
+	/**
+	 * @lock: Protects concurrent access to the allocation bitmap, as well
+	 * as the buffers and devices lists.
+	 */
+	struct mutex lock;
+};
+
+void tegra_vpr_add(struct tegra_vpr *vpr);
+
+#endif
diff --git a/drivers/of/of_numa.c b/drivers/of/of_numa.c
index 230d5f628c1b4..da2e9bd1717c2 100644
--- a/drivers/of/of_numa.c
+++ b/drivers/of/of_numa.c
@@ -171,6 +171,7 @@ int of_node_to_nid(struct device_node *device)
 
 	return NUMA_NO_NODE;
 }
+EXPORT_SYMBOL(of_node_to_nid);
 
 int __init of_numa_init(void)
 {
diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 7df1573a409c3..c2ab02985b5fa 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -716,10 +716,10 @@ void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
 }
 
 /**
- * bitmap_allocate_region - allocate bitmap region
+ * bitmap_allocate - allocate bitmap region
  *	@bitmap: array of unsigned longs corresponding to the bitmap
  *	@pos: beginning of bit region to allocate
- *	@order: region size (log base 2 of number of bits) to allocate
+ *	@len: number of bits to allocate
  *
  * Allocate (set bits in) a specified region of a bitmap.
  *
@@ -727,16 +727,31 @@ void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
  * free (not all bits were zero).
  */
 static __always_inline
-int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
+int bitmap_allocate(unsigned long *bitmap, unsigned int pos, unsigned int len)
 {
-	unsigned int len = BIT(order);
-
 	if (find_next_bit(bitmap, pos + len, pos) < pos + len)
 		return -EBUSY;
 	bitmap_set(bitmap, pos, len);
 	return 0;
 }
 
+/**
+ * bitmap_allocate_region - allocate bitmap region
+ *	@bitmap: array of unsigned longs corresponding to the bitmap
+ *	@pos: beginning of bit region to allocate
+ *	@order: region size (log base 2 of number of bits) to allocate
+ *
+ * Allocate (set bits in) a specified region of a bitmap.
+ *
+ * Returns: 0 on success, or %-EBUSY if specified region wasn't
+ * free (not all bits were zero).
+ */
+static __always_inline
+int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
+{
+	return bitmap_allocate(bitmap, pos, BIT(order));
+}
+
 /**
  * bitmap_find_free_region - find a contiguous aligned mem region
  *	@bitmap: array of unsigned longs corresponding to the bitmap
diff --git a/include/linux/cma.h b/include/linux/cma.h
index 8555d38a97b1a..844404459a424 100644
--- a/include/linux/cma.h
+++ b/include/linux/cma.h
@@ -49,11 +49,15 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
 					struct cma **res_cma);
 extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align,
 			      bool no_warn);
+extern struct page *cma_alloc_at(struct cma *cma, unsigned long offset,
+				 unsigned long count, bool no_warn);
 extern bool cma_release(struct cma *cma, const struct page *pages, unsigned long count);
 
 struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,
 		unsigned int align, bool no_warn);
 struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order);
+struct page *cma_alloc_at_frozen(struct cma *cma, unsigned long offset,
+				 unsigned long count, bool no_warn);
 bool cma_release_frozen(struct cma *cma, const struct page *pages,
 		unsigned long count);
 
diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h
index 648328a64b27e..1c9bed1f4ddef 100644
--- a/include/linux/dma-heap.h
+++ b/include/linux/dma-heap.h
@@ -12,6 +12,7 @@
 #include <linux/types.h>
 
 struct dma_heap;
+struct seq_file;
 
 /**
  * struct dma_heap_ops - ops to operate on a given heap
@@ -24,6 +25,7 @@ struct dma_heap_ops {
 				    unsigned long len,
 				    u32 fd_flags,
 				    u64 heap_flags);
+	int (*show)(struct seq_file *s, struct dma_heap *heap);
 };
 
 /**
diff --git a/include/trace/events/cma.h b/include/trace/events/cma.h
index 37195edf24986..4e0ab63e303f0 100644
--- a/include/trace/events/cma.h
+++ b/include/trace/events/cma.h
@@ -38,13 +38,17 @@ TRACE_EVENT(cma_release,
 
 TRACE_EVENT(cma_alloc_start,
 
-	TP_PROTO(const char *name, unsigned long request_count, unsigned long available_count,
+	TP_PROTO(const char *name, unsigned long start, unsigned long end,
+		unsigned long request_count, unsigned long available_count,
 		unsigned long total_count, unsigned int align),
 
-	TP_ARGS(name, request_count, available_count, total_count, align),
+	TP_ARGS(name, start, end, request_count, available_count, total_count,
+		align),
 
 	TP_STRUCT__entry(
 		__string(name, name)
+		__field(unsigned long, start)
+		__field(unsigned long, end)
 		__field(unsigned long, request_count)
 		__field(unsigned long, available_count)
 		__field(unsigned long, total_count)
@@ -53,14 +57,18 @@ TRACE_EVENT(cma_alloc_start,
 
 	TP_fast_assign(
 		__assign_str(name);
+		__entry->start = start;
+		__entry->end = end;
 		__entry->request_count = request_count;
 		__entry->available_count = available_count;
 		__entry->total_count = total_count;
 		__entry->align = align;
 	),
 
-	TP_printk("name=%s request_count=%lu available_count=%lu total_count=%lu align=%u",
+	TP_printk("name=%s start=%lu end=%lu request_count=%lu available_count=%lu total_count=%lu align=%u",
 		  __get_str(name),
+		  __entry->start,
+		  __entry->end,
 		  __entry->request_count,
 		  __entry->available_count,
 		  __entry->total_count,
@@ -132,6 +140,69 @@ TRACE_EVENT(cma_alloc_busy_retry,
 		  __entry->align)
 );
 
+TRACE_EVENT(cma_alloc_at_start,
+
+	TP_PROTO(const char *name, unsigned long pfn,
+		 unsigned long request_count, unsigned long available_count,
+		 unsigned long total_count),
+
+	TP_ARGS(name, pfn, request_count, available_count, total_count),
+
+	TP_STRUCT__entry(
+		__string(name, name)
+		__field(unsigned long, pfn)
+		__field(unsigned long, request_count)
+		__field(unsigned long, available_count)
+		__field(unsigned long, total_count)
+	),
+
+	TP_fast_assign(
+		__assign_str(name);
+		__entry->pfn = pfn;
+		__entry->request_count = request_count;
+		__entry->available_count = available_count;
+		__entry->total_count = total_count;
+	),
+
+	TP_printk("name=%s pfn=%lx, request_count=%lu available_count=%lu total_count=%lu",
+		  __get_str(name),
+		  __entry->pfn,
+		  __entry->request_count,
+		  __entry->available_count,
+		  __entry->total_count)
+);
+
+TRACE_EVENT(cma_alloc_at_finish,
+
+	TP_PROTO(const char *name, unsigned long pfn, const struct page *page,
+		 unsigned long count, int errorno),
+
+	TP_ARGS(name, pfn, page, count, errorno),
+
+	TP_STRUCT__entry(
+		__string(name, name)
+		__field(unsigned long, pfn)
+		__field(const struct page *, page)
+		__field(unsigned long, count)
+		__field(int, errorno)
+	),
+
+	TP_fast_assign(
+		__assign_str(name);
+		__entry->pfn = pfn;
+		__entry->page = page;
+		__entry->count = count;
+		__entry->errorno = errorno;
+	),
+
+	TP_printk("name=%s pfn=0x%lx page=%p count=%lu errorno=%d",
+		  __get_str(name),
+		  __entry->pfn,
+		  __entry->page,
+		  __entry->count,
+		  __entry->errorno)
+);
+
 #endif /* _TRACE_CMA_H */
 
 /* This part must be outside protection */
diff --git a/include/trace/events/tegra_vpr.h b/include/trace/events/tegra_vpr.h
new file mode 100644
index 0000000000000..f8ceb17679fe0
--- /dev/null
+++ b/include/trace/events/tegra_vpr.h
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#if !defined(_TRACE_TEGRA_VPR_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_TEGRA_VPR_H
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM tegra_vpr
+
+#include <linux/tracepoint.h>
+
+TRACE_EVENT(tegra_vpr_chunk_activate,
+	TP_PROTO(phys_addr_t start, phys_addr_t limit),
+	TP_ARGS(start, limit),
+	TP_STRUCT__entry(
+		__field(phys_addr_t, start)
+		__field(phys_addr_t, limit)
+	),
+	TP_fast_assign(
+		__entry->start = start;
+		__entry->limit = limit;
+	),
+	TP_printk("%pap-%pap", &__entry->start,
+		  &__entry->limit)
+);
+
+TRACE_EVENT(tegra_vpr_chunk_deactivate,
+	TP_PROTO(phys_addr_t start, phys_addr_t limit),
+	TP_ARGS(start, limit),
+	TP_STRUCT__entry(
+		__field(phys_addr_t, start)
+		__field(phys_addr_t, limit)
+	),
+	TP_fast_assign(
+		__entry->start = start;
+		__entry->limit = limit;
+	),
+	TP_printk("%pap-%pap", &__entry->start,
+		  &__entry->limit)
+);
+
+TRACE_EVENT(tegra_vpr_set,
+	TP_PROTO(phys_addr_t base, phys_addr_t size),
+	TP_ARGS(base, size),
+	TP_STRUCT__entry(
+		__field(phys_addr_t, start)
+		__field(phys_addr_t, limit)
+	),
+	TP_fast_assign(
+		__entry->start = base;
+		__entry->limit = base + size;
+	),
+	TP_printk("%pap-%pap", &__entry->start, &__entry->limit)
+);
+
+#endif /* _TRACE_TEGRA_VPR_H */
+
+#include <trace/define_trace.h>
diff --git a/mm/cma.c b/mm/cma.c
index a10ea37a261da..f92d3a1882098 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -783,25 +783,32 @@ static void cma_debug_show_areas(struct cma *cma)
 	spin_unlock_irq(&cma->lock);
 }
 
+/*
+ * Searches the CMA memrange, from @start to @end, for a free region of
+ * @count bits. If @end is less or equal to @start, will search the entire
+ * memrange.
+ */
 static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
-				unsigned long count, unsigned int align,
-				struct page **pagep, gfp_t gfp)
+			   unsigned long start, unsigned long end,
+			   unsigned long count, unsigned int align,
+			   struct page **pagep, gfp_t gfp)
 {
 	unsigned long bitmap_maxno, bitmap_no, bitmap_count;
-	unsigned long start, pfn, mask, offset;
-	int ret = -EBUSY;
+	unsigned long pfn, mask, offset;
 	struct page *page = NULL;
+	int ret = -EBUSY;
 
 	mask = cma_bitmap_aligned_mask(cma, align);
 	offset = cma_bitmap_aligned_offset(cma, cmr, align);
-	bitmap_maxno = cma_bitmap_maxno(cma, cmr);
+	bitmap_maxno = (end > start) ? end : cma_bitmap_maxno(cma, cmr);
 	bitmap_count = cma_bitmap_pages_to_bits(cma, count);
 
 	if (bitmap_count > bitmap_maxno)
 		goto out;
 
-	for (start = 0; ; start = bitmap_no + mask + 1) {
+	while (true) {
 		spin_lock_irq(&cma->lock);
+
 		/*
 		 * If the request is larger than the available number
 		 * of pages, stop right away.
@@ -810,6 +817,7 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
 			spin_unlock_irq(&cma->lock);
 			break;
 		}
+
 		bitmap_no = bitmap_find_next_zero_area_off(cmr->bitmap,
 				bitmap_maxno, start, bitmap_count, mask,
 				offset);
@@ -835,10 +843,11 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
 
 		bitmap_set(cmr->bitmap, bitmap_no, bitmap_count);
 		cma->available_count -= count;
+
 		/*
-		 * It's safe to drop the lock here. We've marked this region for
-		 * our exclusive use. If the migration fails we will take the
-		 * lock again and unmark it.
+		 * It's safe to drop the lock here. We've marked this region
+		 * for our exclusive use. If the migration fails we will take
+		 * the lock again and unmark it.
 		 */
 		spin_unlock_irq(&cma->lock);
 
@@ -856,37 +865,45 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
 			 __func__, pfn, page);
 
 		trace_cma_alloc_busy_retry(cma->name, pfn, page, count, align);
+		start = bitmap_no + mask + 1;
 	}
+
 out:
 	if (!ret)
 		*pagep = page;
+
 	return ret;
 }
 
-static struct page *__cma_alloc_frozen(struct cma *cma,
-		unsigned long count, unsigned int align, gfp_t gfp)
+static struct page *__cma_alloc_frozen(struct cma *cma, unsigned long start,
+				       unsigned long end, unsigned long count,
+				       unsigned int align, gfp_t gfp)
 {
 	struct page *page = NULL;
 	int ret = -ENOMEM, r;
 	unsigned long i;
-	const char *name = cma ? cma->name : NULL;
+
+	/* cma_alloc_at() and friends will only work with single-range CMA */
+	if (WARN_ON_ONCE(start > 0 && cma->nranges != 1))
+		return page;
 
 	if (!cma || !cma->count)
 		return page;
 
-	pr_debug("%s(cma %p, name: %s, count %lu, align %d)\n", __func__,
-		(void *)cma, cma->name, count, align);
+	pr_debug("%s(cma %p, name: %s, start %lu, end %lu, count %lu, align %d)\n",
+		 __func__, (void *)cma, cma->name, start, end, count, align);
 
 	if (!count)
 		return page;
 
-	trace_cma_alloc_start(name, count, cma->available_count, cma->count, align);
+	trace_cma_alloc_start(cma->name, start, end, count,
+			      cma->available_count, cma->count, align);
 
 	for (r = 0; r < cma->nranges; r++) {
 		page = NULL;
 
-		ret = cma_range_alloc(cma, &cma->ranges[r], count, align,
-				       &page, gfp);
+		ret = cma_range_alloc(cma, &cma->ranges[r], start, end,
+				      count, align, &page, gfp);
 		if (ret != -EBUSY || page)
 			break;
 	}
@@ -908,7 +925,7 @@ static struct page *__cma_alloc_frozen(struct cma *cma,
 	}
 
 	pr_debug("%s(): returned %p\n", __func__, page);
-	trace_cma_alloc_finish(name, page ? page_to_pfn(page) : 0,
+	trace_cma_alloc_finish(cma->name, page ? page_to_pfn(page) : 0,
 			       page, count, align, ret);
 	if (page) {
 		count_vm_event(CMA_ALLOC_SUCCESS);
@@ -926,14 +943,22 @@ struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,
 {
 	gfp_t gfp = GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0);
 
-	return __cma_alloc_frozen(cma, count, align, gfp);
+	return __cma_alloc_frozen(cma, 0, 0, count, align, gfp);
 }
 
 struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order)
 {
 	gfp_t gfp = GFP_KERNEL | __GFP_COMP | __GFP_NOWARN;
 
-	return __cma_alloc_frozen(cma, 1 << order, order, gfp);
+	return __cma_alloc_frozen(cma, 0, 0, 1 << order, order, gfp);
+}
+
+struct page *cma_alloc_at_frozen(struct cma *cma, unsigned long offset,
+				 unsigned long count, bool no_warn)
+{
+	gfp_t gfp = GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0);
+
+	return __cma_alloc_frozen(cma, offset, offset + count, count, 0, gfp);
 }
 
 /**
@@ -959,6 +984,29 @@ struct page *cma_alloc(struct cma *cma, unsigned long count,
 }
 EXPORT_SYMBOL_GPL(cma_alloc);
 
+/**
+ * cma_alloc_at() - allocate pages from contiguous area at fixed offset
+ * @cma:     Contiguous memory region for which the allocation is performed.
+ * @offset:  Index of the first page to allocate.
+ * @count:   Requested number of pages (in PAGE_SIZE order).
+ * @no_warn: Avoid printing message about failed allocation.
+ *
+ * This function allocates a part of the contiguous memory on a specific
+ * contiguous memory area.
+ */
+struct page *cma_alloc_at(struct cma *cma, unsigned long offset,
+			  unsigned long count, bool no_warn)
+{
+	struct page *page;
+
+	page = cma_alloc_at_frozen(cma, offset, count, no_warn);
+	if (page)
+		set_pages_refcounted(page, count);
+
+	return page;
+}
+EXPORT_SYMBOL_GPL(cma_alloc_at);
+
 static struct cma_memrange *find_cma_memrange(struct cma *cma,
 		const struct page *pages, unsigned long count)
 {

23/3 2026/09/05 20:48 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 27686
							output: 39
							thoughts: 911
						

Thoughts:
**Analyzing the Patch Series for Potential Uninitialized Memory Risks**

Okay, let's dive into this patch series. My initial concern is always uninitialized memory, so KMSAN fuzzing is potentially on the table if I find something that warrants it. I need to understand the scope of the changes first.

Looking at the changes, we've got a mixed bag: DT binding updates across several Tegra platforms, DTS file modifications for tegra234 and tegra264, the exporting of some functions in `pageattr.c`, debugfs support and a new Tegra VPR dma-buf heap driver. We've also got changes in the core DMA-BUF and CMA infrastructure, bitmap and CMA allocation functions being updated and implemented. Plus, some trace events added for CMA and tegra_vpr.

Specifically, I need to focus on potential user-space interaction. The introduction of the DMA-BUF heap is a prime suspect. User space can interact with this through `/dev/dma_heap/<heap_name>` using `DMA_HEAP_IOCTL_ALLOC`.  That's my entry point for investigation.  

This ioctl calls `dma_heap_ioctl_allocate`, which in turn, calls the `heap->ops->allocate(heap, ...)` function. In this case, that resolves to the `tegra_vpr_allocate` function. I will check the tegra-vpr code, to ensure no memory is uninitialized.

To truly understand the lifecycle, I'll review `drivers/dma-buf/dma-heap.c` to see how dma-heap manages the allocation of dma_bufs and how the export process works. I think I will use `grepper` or `read-file` to check the code.

Alright, time to roll up my sleeves and get into the code.



24/3 2026/09/05 20:48 tool grepper
0m Args:
{
  "Expression": "dma_heap_ioctl",
  "PathPrefix": "drivers/dma-buf/dma-heap.c"
}

Results:
{
  "Output": "drivers/dma-buf/dma-heap.c=87=static int dma_heap_open(struct inode *inode, struct file *file)\n--\ndrivers/dma-buf/dma-heap.c-103-\ndrivers/dma-buf/dma-heap.c:104:static long dma_heap_ioctl_allocate(struct file *file, void *data)\ndrivers/dma-buf/dma-heap.c-105-{\n--\ndrivers/dma-buf/dma-heap.c-129-\ndrivers/dma-buf/dma-heap.c:130:static unsigned int dma_heap_ioctl_cmds[] = {\ndrivers/dma-buf/dma-heap.c-131-\tDMA_HEAP_IOCTL_ALLOC,\n--\ndrivers/dma-buf/dma-heap.c-133-\ndrivers/dma-buf/dma-heap.c:134:static long dma_heap_ioctl(struct file *file, unsigned int ucmd,\ndrivers/dma-buf/dma-heap.c-135-\t\t\t   unsigned long arg)\n--\ndrivers/dma-buf/dma-heap.c-143-\ndrivers/dma-buf/dma-heap.c:144:\tif (nr \u003e= ARRAY_SIZE(dma_heap_ioctl_cmds))\ndrivers/dma-buf/dma-heap.c-145-\t\treturn -EINVAL;\ndrivers/dma-buf/dma-heap.c-146-\ndrivers/dma-buf/dma-heap.c:147:\tnr = array_index_nospec(nr, ARRAY_SIZE(dma_heap_ioctl_cmds));\ndrivers/dma-buf/dma-heap.c-148-\t/* Get the kernel ioctl cmd that matches */\ndrivers/dma-buf/dma-heap.c:149:\tkcmd = dma_heap_ioctl_cmds[nr];\ndrivers/dma-buf/dma-heap.c-150-\n--\ndrivers/dma-buf/dma-heap.c-178-\tcase DMA_HEAP_IOCTL_ALLOC:\ndrivers/dma-buf/dma-heap.c:179:\t\tret = dma_heap_ioctl_allocate(file, kdata);\ndrivers/dma-buf/dma-heap.c-180-\t\tbreak;\n--\ndrivers/dma-buf/dma-heap.c=194=static const struct file_operations dma_heap_fops = {\n--\ndrivers/dma-buf/dma-heap.c-196-\t.open\t\t= dma_heap_open,\ndrivers/dma-buf/dma-heap.c:197:\t.unlocked_ioctl = dma_heap_ioctl,\ndrivers/dma-buf/dma-heap.c-198-#ifdef CONFIG_COMPAT\ndrivers/dma-buf/dma-heap.c:199:\t.compat_ioctl\t= dma_heap_ioctl,\ndrivers/dma-buf/dma-heap.c-200-#endif\n"
}

25/3 2026/09/05 20:48 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 29824
							output: 41
							thoughts: 24
						

Thoughts:
Here's a summary of my immediate thought process:

**Diving into DMA Heap Allocation on Tegra**

Okay, so I need to understand how DMA heaps are being allocated within this Tegra-specific context. My initial instinct is to look at two specific pieces of the puzzle: the general `dma_heap_ioctl_allocate` function and, crucially, the `tegra-vpr.c` driver code. I'm assuming that the `ioctl` call is the user-facing entry point for requesting memory, and the driver is likely where the Tegra-specific hardware interactions and memory management happen. Examining how these two interact will give me a solid grasp of the flow: the request, the driver's response (or rejection), and the allocation process itself, all within the constraints of the Tegra architecture. I expect `tegra-vpr.c` to contain the nitty-gritty details on how physical memory is mapped, configured, and made available as a DMA buffer. Knowing this is key to debugging/optimizing memory management in this system.



26/3 2026/09/05 20:49 tool read-file
0m Args:
{
  "File": "drivers/dma-buf/dma-heap.c",
  "FirstLine": 100,
  "LineCount": 40
}

Results:
{
  "Contents": " 100:\t\n 101:\t\treturn 0;\n 102:\t}\n 103:\t\n 104:\tstatic long dma_heap_ioctl_allocate(struct file *file, void *data)\n 105:\t{\n 106:\t\tstruct dma_heap_allocation_data *heap_allocation = data;\n 107:\t\tstruct dma_heap *heap = file-\u003eprivate_data;\n 108:\t\tint fd;\n 109:\t\n 110:\t\tif (heap_allocation-\u003efd)\n 111:\t\t\treturn -EINVAL;\n 112:\t\n 113:\t\tif (heap_allocation-\u003efd_flags \u0026 ~DMA_HEAP_VALID_FD_FLAGS)\n 114:\t\t\treturn -EINVAL;\n 115:\t\n 116:\t\tif (heap_allocation-\u003eheap_flags \u0026 ~DMA_HEAP_VALID_HEAP_FLAGS)\n 117:\t\t\treturn -EINVAL;\n 118:\t\n 119:\t\tfd = dma_heap_buffer_alloc(heap, heap_allocation-\u003elen,\n 120:\t\t\t\t\t   heap_allocation-\u003efd_flags,\n 121:\t\t\t\t\t   heap_allocation-\u003eheap_flags);\n 122:\t\tif (fd \u003c 0)\n 123:\t\t\treturn fd;\n 124:\t\n 125:\t\theap_allocation-\u003efd = fd;\n 126:\t\n 127:\t\treturn 0;\n 128:\t}\n 129:\t\n 130:\tstatic unsigned int dma_heap_ioctl_cmds[] = {\n 131:\t\tDMA_HEAP_IOCTL_ALLOC,\n 132:\t};\n 133:\t\n 134:\tstatic long dma_heap_ioctl(struct file *file, unsigned int ucmd,\n 135:\t\t\t\t   unsigned long arg)\n 136:\t{\n 137:\t\tchar stack_kdata[128];\n 138:\t\tchar *kdata = stack_kdata;\n 139:\t\tunsigned int kcmd;\n"
}

27/3 2026/09/05 20:49 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 30664
							output: 34
							thoughts: 1073
						

Thoughts:
**Analyzing the Tegra VPR DMA Heap Implementation**

Okay, let's dive into this Tegra VPR code. My immediate focus is on memory allocation and potential vulnerabilities, so I'm starting with `tegra_vpr_allocate` and `tegra_vpr_buf_ops`. I'm seeing that the buffer operations (`tegra_vpr_buf_ops`) define several functions, but `begin_cpu_access`, `end_cpu_access`, and `mmap` all return `-EPERM`. That's interesting, but not necessarily a red flag yet; it could just be the intended behavior for this specific DMA heap.

Looking at `tegra_vpr_buffer_allocate`, the buffer itself is being allocated with `kzalloc_obj`, which is good since it zero-initializes the memory. The `buffer->pages` array, holding the physical pages, is also allocated using `kvmalloc_array`. I like that `buffer->pages` is initialized in a loop, assigning addresses to the pages using `&vpr->start_page[pageno + i]`.  This suggests a direct mapping from the VPR region's start page. The chunk allocations (`vpr->chunks`, `vpr->active`, `vpr->bitmap`, etc.) are also using `kzalloc_obj`, and that’s good to see. The bitmap allocation, `bitmap_zalloc`, is equally well done. All of this initialisation is important to prevent out-of-the-box security issues.

Next, I'll switch gears and examine the debugfs interface. The `dma_heap_debug_show` function, and more specifically `tegra_vpr_debugfs_show`, gives me a way to inspect the heap's state. I'm noting the use of `string_get_size` to format the size in a human-readable format.  It's calling either `tegra_vpr_debugfs_show_buffers` or `tegra_vpr_debugfs_show_chunks` depending on the `resizable` flag.

I'm glancing at `bitmap.h` next. `bitmap_allocate` and `bitmap_allocate_region` look like standard, efficient bitmap manipulation functions. Nothing particularly unusual there, but I'll make sure to verify the usage later.

Now, let's see what’s going on in the CMA code. Since I'm still trying to find something to sink my teeth into, I'll go through the `cma.c` file around lines 780-990 to see if anything uninitialized could be used or returned. I'll be looking for something that might be missed or have a race condition.



28/3 2026/09/05 20:49 tool read-file
0m Args:
{
  "File": "mm/cma.c",
  "FirstLine": 780,
  "LineCount": 80
}

Results:
{
  "Contents": " 780:\t\t}\n 781:\t\tpr_cont(\"=\u003e %lu free of %lu total pages\\n\", cma-\u003eavailable_count,\n 782:\t\t\t\tcma-\u003ecount);\n 783:\t\tspin_unlock_irq(\u0026cma-\u003elock);\n 784:\t}\n 785:\t\n 786:\t/*\n 787:\t * Searches the CMA memrange, from @start to @end, for a free region of\n 788:\t * @count bits. If @end is less or equal to @start, will search the entire\n 789:\t * memrange.\n 790:\t */\n 791:\tstatic int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,\n 792:\t\t\t\t   unsigned long start, unsigned long end,\n 793:\t\t\t\t   unsigned long count, unsigned int align,\n 794:\t\t\t\t   struct page **pagep, gfp_t gfp)\n 795:\t{\n 796:\t\tunsigned long bitmap_maxno, bitmap_no, bitmap_count;\n 797:\t\tunsigned long pfn, mask, offset;\n 798:\t\tstruct page *page = NULL;\n 799:\t\tint ret = -EBUSY;\n 800:\t\n 801:\t\tmask = cma_bitmap_aligned_mask(cma, align);\n 802:\t\toffset = cma_bitmap_aligned_offset(cma, cmr, align);\n 803:\t\tbitmap_maxno = (end \u003e start) ? end : cma_bitmap_maxno(cma, cmr);\n 804:\t\tbitmap_count = cma_bitmap_pages_to_bits(cma, count);\n 805:\t\n 806:\t\tif (bitmap_count \u003e bitmap_maxno)\n 807:\t\t\tgoto out;\n 808:\t\n 809:\t\twhile (true) {\n 810:\t\t\tspin_lock_irq(\u0026cma-\u003elock);\n 811:\t\n 812:\t\t\t/*\n 813:\t\t\t * If the request is larger than the available number\n 814:\t\t\t * of pages, stop right away.\n 815:\t\t\t */\n 816:\t\t\tif (count \u003e cma-\u003eavailable_count) {\n 817:\t\t\t\tspin_unlock_irq(\u0026cma-\u003elock);\n 818:\t\t\t\tbreak;\n 819:\t\t\t}\n 820:\t\n 821:\t\t\tbitmap_no = bitmap_find_next_zero_area_off(cmr-\u003ebitmap,\n 822:\t\t\t\t\tbitmap_maxno, start, bitmap_count, mask,\n 823:\t\t\t\t\toffset);\n 824:\t\t\tif (bitmap_no \u003e= bitmap_maxno) {\n 825:\t\t\t\tspin_unlock_irq(\u0026cma-\u003elock);\n 826:\t\t\t\tbreak;\n 827:\t\t\t}\n 828:\t\n 829:\t\t\tpfn = cmr-\u003ebase_pfn + (bitmap_no \u003c\u003c cma-\u003eorder_per_bit);\n 830:\t\t\tpage = pfn_to_page(pfn);\n 831:\t\n 832:\t\t\t/*\n 833:\t\t\t * Do not hand out page ranges that are not contiguous, so\n 834:\t\t\t * callers can just iterate the pages without having to worry\n 835:\t\t\t * about these corner cases.\n 836:\t\t\t */\n 837:\t\t\tif (!page_range_contiguous(page, count)) {\n 838:\t\t\t\tspin_unlock_irq(\u0026cma-\u003elock);\n 839:\t\t\t\tpr_warn_ratelimited(\"%s: %s: skipping incompatible area [0x%lx-0x%lx]\",\n 840:\t\t\t\t\t\t    __func__, cma-\u003ename, pfn, pfn + count - 1);\n 841:\t\t\t\tcontinue;\n 842:\t\t\t}\n 843:\t\n 844:\t\t\tbitmap_set(cmr-\u003ebitmap, bitmap_no, bitmap_count);\n 845:\t\t\tcma-\u003eavailable_count -= count;\n 846:\t\n 847:\t\t\t/*\n 848:\t\t\t * It's safe to drop the lock here. We've marked this region\n 849:\t\t\t * for our exclusive use. If the migration fails we will take\n 850:\t\t\t * the lock again and unmark it.\n 851:\t\t\t */\n 852:\t\t\tspin_unlock_irq(\u0026cma-\u003elock);\n 853:\t\n 854:\t\t\tmutex_lock(\u0026cma-\u003ealloc_mutex);\n 855:\t\t\tret = alloc_contig_frozen_range(pfn, pfn + count, ACR_FLAGS_CMA, gfp);\n 856:\t\t\tmutex_unlock(\u0026cma-\u003ealloc_mutex);\n 857:\t\t\tif (!ret)\n 858:\t\t\t\tbreak;\n 859:\t\n"
}

29/3 2026/09/05 20:49 llm kmsan-evaluator
0m Model:
gemini-3.7-flash

Args:
null

Results:
null

Tokens:
							input: 33618
							output: 287
							thoughts: 1116
						

Thoughts:
**KMSAN Assessment: NeedsKMSAN = false**

Okay, let's break this down. First, I checked the relevant files, `tegra-vpr-init.c` and `tegra-vpr.c`, to understand the initialization and usage of the Virtual Physical Region (VPR) memory. I observed how the `struct tegra_vpr` is initialized, specifically looking at the use of `cma`, `kzalloc_obj`, and the `INIT_LIST_HEAD` macro.  I examined `tegra_vpr_add_heap`, focusing on how the heap is defined and its properties are set, paying attention to the `info.priv` assignment, which is likely a key aspect. I also went through how the start/limit and size/base is calculated. I followed the logic flow within `tegra_vpr_get_active_range`, understanding the significance of `find_first_bit` and `find_last_bit` and verifying that the code correctly handles the case where no active range exists, thereby preventing any potential reads of uninitialized memory.

Next, I looked specifically for potential information leaks or uninitialized variable reads. I carefully examined the code for any direct use of functions like `copy_to_user` or `put_user` as these are direct signals to examine. I also scanned for network structures (sockets, netlink, BPF) since these are a common avenue for leaking data. I confirmed that these were not used. I noted that allocation in the patch series is done using `kzalloc_obj`, `kzalloc_objs`, and `bitmap_zalloc`, meaning the memory is zeroed out by default.  Furthermore, I established that local variables are initialized before use. The architecture explicitly prevents CPU access to the VPR memory via `tegra_vpr_mmap`, `tegra_vpr_begin_cpu_access`, and `tegra_vpr_end_cpu_access` which returns `-EPERM`.

Given the above checks, I reviewed the KMSAN criteria. Based on my analysis, this patch series *does not* introduce or modify any kernel structures that are sent to user space where fields might be uninitialized. There's no conditional logic that depends on uninitialized variables.  The allocation of complex data structures uses zeroing functions, and no uninitialized fields are read in any code path.  There are no bounds checks or logic that allow access to uninitialized bytes.  Therefore, it clearly avoids the potential pitfalls outlined in the KMSAN criteria. Since the code changes are primarily geared toward memory management and initialization, the risk is limited to out-of-bounds access, null pointer dereferences, locking issues, or use-after-free bugs which are typically covered by KASAN/LOCKDEP/standard bug detectors.  All newly introduced structures are either zeroed or fully initialized before use. Consequently, I'm confident that this patch series does not introduce any risks of uninitialized memory usage or information leaks.

Therefore, `NeedsKMSAN` should be set to `false`, and fuzzing with KASAN and LOCKDEP should be sufficient. I'm going to call `set-results`.



30/3 2026/09/05 20:49 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series introduces support for NVIDIA Tegra Video Protection Region (VPR) as a DMA-BUF heap, adds debugfs support to dma-heap, and adds fixed-offset CMA allocation support (`cma_alloc_at`).\n\nAll newly introduced kernel heap data structures (`tegra_vpr`, `tegra_vpr_buffer`, `tegra_vpr_attachment`, `tegra_vpr_device`, `tegra_vpr_chunk`, bitmaps) are zero-initialized via `kzalloc_obj`, `kzalloc_objs`, and `bitmap_zalloc`. All local variables used across CMA and VPR functions are explicitly initialized before use. Furthermore, CPU access and user mappings for VPR buffers are explicitly rejected with `-EPERM` (`tegra_vpr_mmap`, `tegra_vpr_begin_cpu_access`, `tegra_vpr_end_cpu_access`), preventing kernel or user reads of VPR memory. There are no kernel-to-user info leaks or branching on uninitialized data. Any potential concurrency, allocation bounds, or reference management issues are covered by standard sanitizers (KASAN, LOCKDEP). Therefore, dedicated KMSAN fuzzing is not required.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series introduces support for NVIDIA Tegra Video Protection Region (VPR) as a DMA-BUF heap, adds debugfs support to dma-heap, and adds fixed-offset CMA allocation support (`cma_alloc_at`).\n\nAll newly introduced kernel heap data structures (`tegra_vpr`, `tegra_vpr_buffer`, `tegra_vpr_attachment`, `tegra_vpr_device`, `tegra_vpr_chunk`, bitmaps) are zero-initialized via `kzalloc_obj`, `kzalloc_objs`, and `bitmap_zalloc`. All local variables used across CMA and VPR functions are explicitly initialized before use. Furthermore, CPU access and user mappings for VPR buffers are explicitly rejected with `-EPERM` (`tegra_vpr_mmap`, `tegra_vpr_begin_cpu_access`, `tegra_vpr_end_cpu_access`), preventing kernel or user reads of VPR memory. There are no kernel-to-user info leaks or branching on uninitialized data. Any potential concurrency, allocation bounds, or reference management issues are covered by standard sanitizers (KASAN, LOCKDEP). Therefore, dedicated KMSAN fuzzing is not required.",
  "NeedsKMSAN": false
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)