drm/i915: map I915_MMAP_WC objects write-combining
An I915_MMAP_WC mmap marked every resident page of the GEM object write
combining with pmap_page_set_memattr(), which also rewrote the direct
map (DMAP) entry containing each page and flushed the cache. The pages
keep the attribute when the object is destroyed, so later allocations
map normal memory write combining, and reading it is about a hundred
times slower.
Set the memory attribute of the GEM object instead, before the mapping
is created, and make pmap_enter() take the PTE cache bits from the
attribute of the object that backs the map entry. Pages reached
through the shadow chain (backing_ba) keep the attribute of the page.
The pages and the DMAP keep the default attribute, and a WC mmap cannot
leave a window in which the new address faults write-back. The cache
flush of the old code is gone.
The attribute belongs to the object, not to the mapping: pages faulted
after the first WC mmap are write combining in every mapping of the
[13 lines not shown]
drm: Drop LK_SLEEPFAIL flag for the interruptible lock wrappers
mutex_lock_interruptible() and down_write_killable() passed
LK_SLEEPFAIL, so any caller that has to sleep never acquires the lock:
lockmgr() returns ENOLCK and the wrappers turned that into -EINTR.
So a non-blocking read(2) on the DRM fd could fail with with EINTR
although no signal was delivered.
Linux returns 0 on success and -EINTR only when a signal interrupted the
sleep. Drop LK_SLEEPFAIL, keep LK_EXCLUSIVE|LK_PCATCH so the sleep stays
interruptible, map lockmgr()'s EINTR and ERESTART to -EINTR, and turn any
other failure into a negative errno with a warning that names the wrapper
and the error, instead of returning a positive value to the callers. DRM
locks are neither created with LK_TIMELOCK nor taken with LK_CANCELABLE,
so ENOLCK and EWOULDBLOCK are unreachable without LK_SLEEPFAIL and the
warning only fires if that assumption breaks.
ww_mutex_lock_interruptible() uses its own ww_mutex path and is not
affected.
[2 lines not shown]
ntfs: Fix ntfs_readdir() to avoid stack overflow
The readdir name conversion in ntfs_readdir() never validates
ie_fnamelen against the destination: it is an attacker-controlled u8
taken straight from the mounted image, and the loop writes into the
256-byte stack buffer convname[] with no bound on the write index. On
the default mount NTFS_U28 resolves to ntfs_u28(), whose conversion
table cells hold sign-extended chars, so every name wchar >= 0x80
makes the loop emit two bytes per character. With ie_fnamelen = 255
the write index j reaches 510 and the loop writes 255 bytes past
convname[], overwriting five saved registers, rbp and the return
address (the kernel has no stack canary on this path). A single
getdents(2) on a mounted crafted NTFS image by any user with read
access to the mount then controls the return address; verified to
give uid 0 on master.
Fix ntfs_readdir() to check the buffer space and thus avoid stack
overflow.
[12 lines not shown]
drm: return a positive errno from drm_mmap_single()
drm_mmap_single() is the d_mmap_single method of drm_cdevsw. On devices
with a TTM buffer object device (radeon, amdgpu) it returned the result
of ttm_bo_mmap_single() unchanged: ttm_bo_mmap() gives the negative
errno of the Linux code (-EINVAL, -EACCES and -EPERM, which is ERESTART
here), while the DragonFly part of ttm_bo_mmap_single() assigns a
positive EINVAL.
Convert the result with drm_cdev_errno() like the other methods;
positive values pass through unchanged.
GitHub-PR: https://github.com/DragonFlyBSD/DragonFlyBSD/pull/71
drm: convert errors at the cdevsw boundary
drm_open(), drm_read() and drm_ioctl() are the d_open, d_read and
d_ioctl methods of drm_cdevsw, but their bodies are Linux code that
returns a negative errno (and the amount read for drm_read()), while the
cdev layer expects zero or a positive errno. They were adapted one
function at a time: drm_ioctl() negated the handler results, but the
Linux syncs left -ENODEV, drm_ioctl_permit(), drm_open() and drm_read()
returning negative values, which e9a3645661, 4d09039497 and d23da7dd7d
fixed one by one.
Keep the drm_*() functions in the Linux convention and convert the
result once, in dev_ops wrappers in drm_drv.c: a negative errno becomes
positive, -ERESTARTSYS becomes EINTR, and a positive amount read from
drm_read() becomes zero. drm_ioctl() no longer negates the handler
results or maps ERESTARTSYS itself, and drm_read() loses its zero
return special case.
GitHub-PR: https://github.com/DragonFlyBSD/DragonFlyBSD/pull/71
drm: fix a panic on mmap() at an unknown offset
mmap(2) of a DRM device node at an offset that is not a GEM mmap
offset panicked the kernel. drm_gem_mmap_single() returns ENODEV for
such an offset, which makes vm_mmap() fall back to the device pager,
and the pager calls drm_mmap() without a file; drm_mmap() dereferenced
the NULL file pointer (page fault at address 0x78 in drm_mmap(), called
from dev_dmmap()). Any user who can open /dev/dri/card0 could trigger
it.
The device pager never has a file for drm_mmap(), so the fallback
cannot work: fail with EINVAL as Linux does, and make drm_mmap() refuse
a call without a file.
Bug: https://bugs.dragonflybsd.org/issues/3484
ntfs: bounce reads of clusters smaller than a page
Same defect as msdosfs: uiomovebp() zero-fills and validates the whole
last VM page of the held buffer when that page is not fully valid, and
with clusters smaller than a page the unread sibling clusters of the
same file are later served the fabricated zeros from the buffer cache.
For such clusters ntfs_read() copies the requested bytes into a kernel
buffer and releases the cluster before copying to user space. The
ntfs_subr.c copy sites are left alone: they transfer to and from device
buffers and do not check the result. Clusters of a page or more keep
uiomovebp(). Page-in reads (UIO_NOCOPY) copy nothing and use plain
uiomove().
Bug: https://bugs.dragonflybsd.org/issues/3461
Discussed-with: dillon
msdosfs: bounce reads of clusters smaller than a page
uiomovebp() zero-fills and validates the whole last VM page of a held
buffer when that page is not fully valid. With clusters smaller than a
page the unread sibling clusters of the same file share that page, and
later reads of those clusters return the fabricated zeros from the
buffer cache without I/O.
For such clusters msdosfs_read() copies the requested bytes into a
kernel buffer and releases the cluster before copying to user space.
Clusters of a page or more keep uiomovebp(). Directory reads keep
uiomovebp(): their buffers are on the device vnode, so the VREG-only
fix-up never fires for them. Page-in reads (UIO_NOCOPY) copy nothing
and use plain uiomove().
Bug: https://bugs.dragonflybsd.org/issues/3461
Discussed-with: dillon
msdosfs: stage write data before raising the file size
A write whose source is a mapping of the same file can fault on the
user side; the copy cannot be retried while the cluster is locked, and
uiomovebp() would also zero-fill and validate the whole last VM page,
destroying sibling clusters of the same file.
For clusters smaller than a page, copy the user data into a kernel
buffer before the file size is raised (de_FileSize and
vnode_pager_setsize; the cluster allocation in extendfile() and
deextend() still happens before the loop), then copy from that buffer
into the cluster. A failure of that copy rewinds the uio by what it
consumed. The bmap and bread failure paths also rewind the uio and
free the buffer, but they run after the size is raised and rely on the
existing IO_UNIT rollback. UIO_NOCOPY writes copy nothing and use
plain uiomove(), which cannot fail for NOCOPY, for every cluster size;
clusters of a page or more otherwise keep uiomovebp().
Bug: https://bugs.dragonflybsd.org/issues/3461
Discussed-with: dillon
syscons: let VT_WAITACTIVE wait for a vty that was never opened
VT_WAITACTIVE slept on the target vty's scr_stat, which exists only
once something has opened the vty. Since 8aefdd25a3 (bug #2481) a
NULL scr_stat makes it return 0 at once, so waiting for an unopened
vty reported it as active right away.
Do as FreeBSD's syscons: compare the vty index with the current one
and sleep on a per-index channel, &sc->dev[i], which exists whether
or not the vty was opened. The switch code wakes that channel.
Bug: https://bugs.dragonflybsd.org/issues/3472
Reviewed-by: @dillon
kernel - Make stop_cpus()/restart_cpus() much more reliable
* Wait for cpus to not be inside an interrupt or to be initiating a
TLB shootdown while stopping them. This is better than trying to
fixup causes where the stop interrupts special interrupts (or any
interrupt, really).
Note that in DragonFly, stopped CPUs are still allowed to take
interrupts. We do not completely crowbar the CPU while it is
stopped, that just creates too many problems.
* Make DDB and crash-dump entry a lot more reliable.
* With analysis and testing by claude. However, we didn't really
use the primary recommendations and instead did it a slightly
different way.
Reported-by: tuxillo
Debugging-by: tuxillo
Bug: https://bugs.dragonflybsd.org/issues/3475
drm: return a positive errno when drm_ioctl_permit() fails
Since 908aca8817 drm_ioctl() hands drm_ioctl_permit()'s Linux-style
negative error code to userland unchanged, so a refused ioctl sets
errno to -13 (-EACCES) instead of 13. The handler results below are
already negated (retcode = -func(...)). The same commit also changed
"return ENODEV" to "return -ENODEV" for an unplugged device; restore the
positive value (nothing reaches it today, drm_dev_unplug() is only
called from code that is not built).
drmAuthMagic() returns -errno, i.e. +13, which never equals -EACCES,
so libdrm's drmIsMaster() reports non-master fds as master. wlroots
then calls drmDropMaster() on a cloned non-master fd, logs "Failed to
drop master", closes the fd, and the DRM lease manager skips the card.
Negate the value, as is done for the handlers.
Tested on 6.5-DEVELOPMENT (i915): AUTH_MAGIC on a non-master fd now
fails with errno 13 instead of -13, sway no longer logs "Failed to drop
master" or skips the card for leasing, and X starts.
drm: return a positive errno or zero from drm_read()
drm_read() is the d_read method of drm_cdevsw, which returns zero or a
positive errno; the amount read is in uio_resid. It returned the
Linux-style negative values instead: -EAGAIN for an O_NONBLOCK read
without events, -EFAULT, -EINTR from mutex_lock_interruptible() and
-ERESTARTSYS from wait_event_interruptible(). It also returned the
number of bytes read when the next event did not fit in the buffer;
read(2) then failed with that number as the errno and the events
already copied were lost.
Negate the errno values, map -ERESTARTSYS to EINTR as drm_ioctl() does,
and return zero when data was read.
Tested on 6.5-DEVELOPMENT (i915): an O_NONBLOCK read without events now
fails with errno 35 (EAGAIN) instead of -35, a read interrupted by a
signal with errno 4 (EINTR) instead of -512, and read(2) of 40 bytes
with three 32-byte vblank events queued returns 32 instead of failing
with errno 32 (EPIPE) and losing the event.
drm: return a positive errno from drm_open()
drm_open() is the d_open method of drm_cdevsw, which returns a
positive errno, but its failure paths return the Linux-style negative
values: -ENODEV from drm_minor_acquire(), -EBUSY for O_EXCL and the
negative errors of drm_open_helper() and drm_setup(). A failed open()
therefore hands a negative errno to userland, as drm_ioctl() did.
Negate the value on both error returns.
Tested on 6.5-DEVELOPMENT (i915): open(2) of /dev/dri/card0 with O_EXCL
now fails with errno 16 (EBUSY) instead of -16.
drm: remove debug kprintf()s from drm_auth.c
drm_setmaster_ioctl() and drm_dropmaster_ioctl() print unconditional
kprintf()s: every DRM_IOCTL_DROP_MASTER, and every SET_MASTER that does
not return early, leaves a line in dmesg (seen at the end of every sway
session started through seatd). They were added as debugging aids in
4ec80808ee. Remove them; the setmaster workaround itself is unchanged.
kernel - Move mfence into selected_apic_ipi() and single_apic_ipi()
* Make sure pending writes are flushed before issuing APIC IPIs.
Probably only needed for MSR-based IPIs, but do it for both
MEM and MSR.
* Remove explicit mfence prior to issuing IPI.
vfs: drop S_ISGID on create when not a member of the group
A new file on tmpfs, HAMMER or HAMMER2 gets the directory's group and
the mode the caller asked for. If the creator was not a member of
that group, S_ISGID was kept, so any user who can write to a
directory owned by another group could create a set-group-ID
executable for that group; with /tmp on tmpfs (root:wheel 1777) that
is wheel. UFS and ext2fs already drop the bit when they create the
inode.
Do the same check as ufs_makeinode() in tmpfs_alloc_file(),
hammer_create_inode() and hammer2_inode_create_normal(), after the
group has been chosen: keep S_ISGID only for a member of the group or
with SYSCAP_NOVFS_SETGID. As in FreeBSD's fix for tmpfs, this covers
every node type.
Same issue as FreeBSD-SA-26:94.tmpfs (CVE-2026-127749).
Tested on tmpfs, UFS, HAMMER and HAMMER2: a file created with mode
[6 lines not shown]
kernel - Fix cpustop/resume (debugger, kernel core)
* Make cpu_halt() just do the basic instruction, remove halt()
on naming semantics.
* Add cpu_halt_forever() that halts forever.
* Add cpu_halt_and_enable_intr() to do "sti; halt" sequence.
* Fix stop race in loop test, and restart signaling issues when
using HLT instead of MONITOR/MWAIT.
* mfence() prior to resume IPI (MSR/LAPIC does not flush pending writes)
Reported-by: tuxillo and Claud
Bug: https://bugs.dragonflybsd.org/issues/3476
Testing-by: tuxillo
corecstat(4): Add Goldmont, Goldmont Plus, and Tremont CPU support.
* This should cover Apollo Lake, Denverton, Gemini Lake,
Elkhart Lake, and Jasper Lake models.
* These last Atom CPU cores use the same MSR registers for the same
states as "Core" CPUs.
corepower(4): Add Goldmont, Goldmont Plus, and Tremont CPU support.
* This should cover Apollo Lake, Denverton, Gemini Lake,
Elkhart Lake, and Jasper Lake models.
nvmm: Merge one #ifdef with NetBSD in ASM code handling
Since commit 9bd9bca17d53c0f7d7bcd6cd3e029bb77ccb13a6 the ASM code has
become more OS-independent, so <machine/asm.h> is no longer needed in
both OSes.
Since commit b94dd9242808203c854242a803f484ff25f76e32 DragonFly migrated
to use the new "assym.h" as NetBSD, so this #ifdef can be merged for
both OSes.
pc64,vkernel64: Prune includes in genassym.cf
In addition, remove the obsolete "opt_nfs.h" header dependency of
genassym.cf in sys/dev/virtual/nvmm/Makefile.
Migrate to the newly imported genassym.sh and generated assym.h
* Remove the old sys/kern/genassym.sh, sys/sys/assym.h and genassym.c
files.
* Migrate the genassym.c code to genassym.cf configs for genassym.sh.
* Update all sources to include the new generated "assym.h" instead of
the old "assym.s".
* Update the kernel makefiles in sys/conf to call genassym.sh to
generate "assym.h".
* Update libc to use genassym.sh to generate "asmcontext.h".
sys/conf: Share compiler flags and rules between kernel and modules
* Move the common CFLAGS to bsd.kern.mk, and include it earlier in
kern.pre.mk and kmod.sh, so the flags ordering in CFLAGS is mostly
preserved.
* Clear the suffix rules and CFLAGS from <sys.mk> and explicitly define
them in bsd.kern.mk, for use in the kernel and modules building.
pc64: Remove redundant symbol exports in genassym.c
The MSR_GSBASE, MSR_KGSBASE and MSR_FSBASE are constants defined in
<machine/specialreg.h> and can be used in assembly code, so no need to
redefine them.
Caught by the new genassym.sh, whose output has different value format
(e.g., `#define MSR_GSBASE 3221225729`) and thus caused redefinition
error in compilation.
While there, remove a few commented symbols as well.