Skip to contentSkip to conversation

ALSA: us122l: mark hwdep mappings VM_MIXEDMAP

PATCH Applied Lorenzo Stoakes (ARM) 3 participants 4 messages Oct 23 – Oct 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: us122l: mark hwdep mappings VM_MIXEDMAP

mm has recently moved to a strict model which requires any driver which clears VMA_MAYWRITE_BIT to be mapping non-mm-managed memory (VMA_PFNMAP_BIT or VMA_MIXEDMAP_BIT), failing any such mmap() with an error and a kernel warning - see "mm/vma: only allow mmap to clear VMA_MAYWRITE_BIT if not mm-managed".

However, simultaneous to this, commit 71c610aeb177 ("ALSA: us122l: Prevent write upgrades for read mappings") updated us122l to clear VMA_MAYWRITE_BIT without marking the region with either VMA_PFNMAP_BIT or VMA_MIXEDMAP_BIT.

vm_insert_pages() and friends set this automatically, however this driver maps its pre-allocated pages from a fault handler, so never calls these.

Simply set the flag in usb_stream_hwdep_mmap() to resolve the issue. This correctly marks the mapping as one whose pages core mm does not manage, which has no meaningful impact for this driver beyond preventing the mmap() from failing.

Fixes 71c610aeb177 ("ALSA: us122l: Prevent write upgrades for read mappings")
Cc Kazuki Hanai <[email protected]>
Cc Takashi Iwai <[email protected]>
Cc Jaroslav Kysela <[email protected]>

---
Andrew - I think easiest for you to take this.

It should go in before my series ideally to avoid bisection hazard, I think while it's a fix for the driver, that'll be the safest way.

It should be prioritised to be part of the 2nd batch of patches for this merge window.

The commit it fixes is cc: stable but this does NOT need to be backported, it's for 7.4 only.

Thanks!

---
 sound/usb/usx2y/us122l.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/usb/usx2y/us122l.c +1 -1
index a5db0d044ef9..2986a90ea335 100644
@@ -193,7 +193,7 @@ static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
 	}
 
 	area->vm_ops = &usb_stream_hwdep_vm_ops;
-	vm_flags_set(area, VM_DONTDUMP);
+	vm_flags_set(area, VM_MIXEDMAP | VM_DONTDUMP);
 	if (!read)
 		vm_flags_set(area, VM_DONTEXPAND);
 	area->vm_private_data = us122l;

---
base-commit: 2bba28e98578de622ef307d00875aa8f8bca3d10
change-id: 20261023-fix-us122l-mixedmap-2d42f05ab421

Cheers,

Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Acked-by on v1 1/1
#1 lore raw
Quoted 27 lines · Lorenzo Stoakes (ARM)

mm has recently moved to a strict model which requires any driver which clears VMA_MAYWRITE_BIT to be mapping non-mm-managed memory (VMA_PFNMAP_BIT or VMA_MIXEDMAP_BIT), failing any such mmap() with an error and a kernel warning - see "mm/vma: only allow mmap to clear VMA_MAYWRITE_BIT if not mm-managed".

However, simultaneous to this, commit 71c610aeb177 ("ALSA: us122l: Prevent write upgrades for read mappings") updated us122l to clear VMA_MAYWRITE_BIT without marking the region with either VMA_PFNMAP_BIT or VMA_MIXEDMAP_BIT.

vm_insert_pages() and friends set this automatically, however this driver maps its pre-allocated pages from a fault handler, so never calls these.

Simply set the flag in usb_stream_hwdep_mmap() to resolve the issue. This correctly marks the mapping as one whose pages core mm does not manage, which has no meaningful impact for this driver beyond preventing the mmap() from failing.

Fixes 71c610aeb177 ("ALSA: us122l: Prevent write upgrades for read mappings")
Cc Kazuki Hanai <[email protected]>
Cc Takashi Iwai <[email protected]>
Cc Jaroslav Kysela <[email protected]>
Quoted 5 lines

---
Andrew - I think easiest for you to take this.

It should go in before my series ideally to avoid bisection hazard, I think while it's a fix for the driver, that'll be the safest way.

Yes, feel free to take:

thanks,

Takashi

#2 lore raw
In reply to Lorenzo Stoakes (ARM)
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] ALSA: us122l: VM_MIXEDMAP in usb_stream_hwdep_mmap() breaks mremap() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] ALSA: us122l: VM_MIXEDMAP in usb_stream_hwdep_mmap() breaks mremap() --

commit c40f3445d293957b8d56aca33b8d5ed4071fcf4b
Author: Lorenzo Stoakes (ARM) <[email protected]>

ALSA: us122l: mark hwdep mappings VM_MIXEDMAP

This commit sets VM_MIXEDMAP in usb_stream_hwdep_mmap() so that clearing VM_MAYWRITE on read mappings satisfies the core mm requirement that mappings clearing VMA_MAYWRITE_BIT must not be mm-managed.

sound/usb/usx2y/us122l.c static int usb_stream_hwdep_mmap(struct snd_hwdep *hw, · line 198

Quoted 12 lines
sound/usb/usx2y/us122l.c +1 -1
index a5db0d044ef96..2986a90ea3351 100644
@@ -193,7 +193,7 @@ static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
 	}
 
 	area->vm_ops = &usb_stream_hwdep_vm_ops;
-	vm_flags_set(area, VM_DONTDUMP);
+	vm_flags_set(area, VM_MIXEDMAP | VM_DONTDUMP);
 	if (!read)
 		vm_flags_set(area, VM_DONTEXPAND);

[Severity: High]
Does setting VM_MIXEDMAP here cause a regression when userspace expands the read mapping with mremap()?

In sound/usb/usx2y/us122l.c:usb_stream_hwdep_mmap(), VM_DONTEXPAND is intentionally omitted when read is true so that userspace (such as the alsa-plugins pcm_usb_stream plugin) can initially mmap() the first page to read s->read_size and then expand the read VMA to s->read_size via mremap().

When userspace calls mremap() to expand this VMA and the virtual address range following the VMA is unmapped, do_mremap() -> mremap_at() -> expand_vma() takes the in-place expansion path in mm/mremap.c:

mm/mremap.c:expand_vma() {
	if (vrm_can_expand_in_place(vrm)) {
		err = expand_vma_in_place(vrm);
		if (err)
			return err;

		/* OK we're done! */
		return vrm->addr;
	}
}

expand_vma_in_place() then calls vma_merge_extend():

mm/mremap.c:expand_vma_in_place() {
	vma = vma_merge_extend(&vmi, vma, vrm->delta);
	if (!vma) {
		vrm_uncharge(vrm);
		return -ENOMEM;
	}
}

However, vma_merge_extend() -> vma_merge_new_range() in mm/vma.c checks whether the VMA flags allow merging:

mm/vma.c:vma_merge_new_range() {
	if (!vma_flags_can_merge(&vmg->vma_flags))
		return NULL;
}

And vma_flags_can_merge() -> vma_flags_is_mm_backed() -> vma_flags_is_mm_managed() in include/linux/mm.h rejects VMAs with VMA_MIXEDMAP_BIT set:

include/linux/mm.h:vma_flags_is_mm_managed() {
	return !vma_flags_test_any(flags, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT);
}

With VM_MIXEDMAP set on the read VMA, won't vma_merge_extend() return NULL and cause expand_vma_in_place() to fail the mremap() call with -ENOMEM?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw
Quoted 73 lines · On Sat, Oct 24, 2026 at 10:21:59AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] ALSA: us122l: VM_MIXEDMAP in usb_stream_hwdep_mmap() breaks mremap() --

commit c40f3445d293957b8d56aca33b8d5ed4071fcf4b
Author: Lorenzo Stoakes (ARM) <[email protected]>

ALSA: us122l: mark hwdep mappings VM_MIXEDMAP

This commit sets VM_MIXEDMAP in usb_stream_hwdep_mmap() so that clearing VM_MAYWRITE on read mappings satisfies the core mm requirement that mappings clearing VMA_MAYWRITE_BIT must not be mm-managed.

sound/usb/usx2y/us122l.c static int usb_stream_hwdep_mmap(struct snd_hwdep *hw, · line 198

Quoted 12 lines
sound/usb/usx2y/us122l.c +1 -1
index a5db0d044ef96..2986a90ea3351 100644
@@ -193,7 +193,7 @@ static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
 	}
 
 	area->vm_ops = &usb_stream_hwdep_vm_ops;
-	vm_flags_set(area, VM_DONTDUMP);
+	vm_flags_set(area, VM_MIXEDMAP | VM_DONTDUMP);
 	if (!read)
 		vm_flags_set(area, VM_DONTEXPAND);

[Severity: High]
Does setting VM_MIXEDMAP here cause a regression when userspace expands the read mapping with mremap()?

In sound/usb/usx2y/us122l.c:usb_stream_hwdep_mmap(), VM_DONTEXPAND is intentionally omitted when read is true so that userspace (such as the alsa-plugins pcm_usb_stream plugin) can initially mmap() the first page to read s->read_size and then expand the read VMA to s->read_size via mremap().

When userspace calls mremap() to expand this VMA and the virtual address range following the VMA is unmapped, do_mremap() -> mremap_at() -> expand_vma() takes the in-place expansion path in mm/mremap.c:

mm/mremap.c:expand_vma() {
	if (vrm_can_expand_in_place(vrm)) {
		err = expand_vma_in_place(vrm);
		if (err)
			return err;

		/* OK we're done! */
		return vrm->addr;
	}
}

expand_vma_in_place() then calls vma_merge_extend():

mm/mremap.c:expand_vma_in_place() {
	vma = vma_merge_extend(&vmi, vma, vrm->delta);
	if (!vma) {
		vrm_uncharge(vrm);
		return -ENOMEM;
	}
}

However, vma_merge_extend() -> vma_merge_new_range() in mm/vma.c checks whether the VMA flags allow merging:

mm/vma.c:vma_merge_new_range() {
	if (!vma_flags_can_merge(&vmg->vma_flags))
		return NULL;
}

And vma_flags_can_merge() -> vma_flags_is_mm_backed() -> vma_flags_is_mm_managed() in include/linux/mm.h rejects VMAs with VMA_MIXEDMAP_BIT set:

Quoted 6 lines
include/linux/mm.h:vma_flags_is_mm_managed() {
	return !vma_flags_test_any(flags, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT);
}

With VM_MIXEDMAP set on the read VMA, won't vma_merge_extend() return NULL and cause expand_vma_in_place() to fail the mremap() call with -ENOMEM?

This describes a bug that I recently sent a fix for.

--
Cheers, Lorenzo