Commit graph

19855 commits

Author SHA1 Message Date
John Baldwin
92991e8f8d new-bus: Disable assertions for rman mismatches for activate/deactivate
Bus drivers which use an rman to sub-divide a resource allocated from
a parent bus should handle mapping requests (and activate/deactivate
requests) for those sub-allocated resources by doing a subset mapping
of the resource allocated from the parent (and then using this to
handle activate/deactivate requests).

However, not all bus drivers which use internal rmans (such as acpi(4)
and pci_pci(4)) do that since not all nexus drivers support
bus_map/unmap.  Eventually bus drivers should be updated to do this
properly at which point these assertions can be reenabled.

Reported by:	delphij, kib

(cherry picked from commit ed88eef140a1c3d57d546f409c216806dd3da809)
2024-01-03 12:47:24 -08:00
John Baldwin
0c7aa9afb1 new-bus: Add comments for resource_*_map_request*
Requested by:	mhorne

(cherry picked from commit 46971d38de334a9418e2b66b37cea7d051b6731a)
2024-01-03 12:47:18 -08:00
John Baldwin
0962b9d086 newbus: Add a set of bus resource helpers for nexus-like devices
These routines can be used to implement
bus_alloc/adjust/activate/deactive/release_resource on bus drivers
which suballocate resources from rman(9) resource managers.

These methods require a new bus_get_rman method in the bus driver to
return the suitable rman for a given resource type.  The
activate/deactivate helpers also require the bus to implement the
bus_map/ummap_resource methods.

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D42739

(cherry picked from commit 751615c538446ea0384f8faa9cb2508670c3799a)
2024-01-03 12:47:08 -08:00
John Baldwin
a6d562d415 new-bus: Add a comment for bus_generic_get_domain
(cherry picked from commit 00b3cde596dd3945d56aa12f2a4957b2910e1ee3)
2024-01-03 12:47:02 -08:00
John Baldwin
d9c1d04ad5 new-bus: Add resource_validate_map_request function
This helper function for BUS_MAP_RESOURCE performs common argument
validation.

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D42723

(cherry picked from commit 19f073c612afa0111d216e5ccab9525bfc97ec32)
2024-01-03 12:34:11 -08:00
John Baldwin
b636e35bb7 vfs mount: Consistently use ENODEV internally for an invalid fstype
Change vfs_byname_kld to always return an error value of ENODEV to
indicate an unsupported fstype leaving ENOENT to indicate errors such
as a missing mount point or invalid path.  This allows nmount(2) to
better distinguish these cases and avoid treating a missing device
node as an invalid fstype after commit 6e8272f317.

While here, change mount(2) to return EINVAL instead of ENODEV for an
invalid fstype to match nmount(2).

PR:		274600
Reviewed by:	pstef, markj
Differential Revision:	https://reviews.freebsd.org/D42327

(cherry picked from commit 3eed4803f943e2937325e81140b88e2e8eea8deb)
2024-01-03 12:18:44 -08:00
Rick Macklem
2f3ff6fe1a vfs_vnops.c: Fix vn_generic_copy_file_range() for truncation
When copy_file_range(2) was first being developed,
*inoffp + len had to be <= infile_size or an error was
returned. This semantic (as defined by Linux) changed
to allow *inoffp + len to be greater than infile_size and
the copy would end at *inoffp + infile_size.

Unfortunately, the code that decided if the outfd should
be truncated in length did not get updated for this
semantics change.
As such, if a copy_file_range(2) is done, where infile_size - *inoffp
is less that outfile_size but len is large, the outfd file is truncated
when it should not be. (The semantics for this for Linux is to not
truncate outfd in this case.)

This patch fixes the problem. I believe the calculation is safe
for all non-negative values of outsize, *outoffp, *inoffp and insize,
which should be ok, since they are all guaranteed to be non-negative.

Note that this bug is not observed over NFSv4.2, since it truncates
len to infile_size - *inoffp.

PR:	276045

(cherry picked from commit 2319ca6a01816f7fc85d623097c639f239e18c6a)
2024-01-02 17:22:26 -08:00
John Baldwin
e74b03451f abort2: Generate a core dump
Call sigexit rather than exit1 so that a core is generated.

If running the SIGABRT handler is desired, this would need to use
kern_psignal() instead.  In that case a userspace wrapper in libc
would be needed to force an exit if the handler doesn't exit.  Given
that abort2(2)'s intended use case is when userland is in a
sufficiently bad state such that it can't safely call syslog(3) before
abort(3), a userspace abort2(3) wrapper in libc might be dubious.

Reviewed by:	Olivier Certner <olce.freebsd@certner.fr>, emaste
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D42163

(cherry picked from commit 9b57e30cf5b6036263a1a2551df8574571c6f5a4)
2024-01-02 11:20:37 -08:00
Mark Johnston
fecd8e111a compat_freebsd4: Fix handling of errors from subyte()
Upon failure, subyte() returns -1, not an errno value.

MFC after:	1 week

(cherry picked from commit c38df501ce2ed7da128448f815ec627c39fd3bad)
2024-01-01 19:30:00 -05:00
Mark Johnston
7bc713a5a5 nmount: Ignore errors when copying out an error string
In general we copy error strings as part of reporting an error from
lower layers, so if the copyout() fails there's nothing to do since we'd
prefer to preserve the original error.

This is in preparation for annotating copyin() and related functions
with __result_use_check.

Reviewed by:	olce, kib
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D43147

(cherry picked from commit 099d25c354d93d9cd9c9cd261428f5ab0547a194)
2024-01-01 19:29:58 -05:00
Mark Johnston
8ff1566cb2 umtx: Check for errors from suword32()
This is in preparation for annotating copyin() and related functions
with __result_use_check.

Reviewed by:	kib
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D43144

(cherry picked from commit 3379d9b5de4c4876a317d25ca008e66b1111b701)
2024-01-01 19:29:54 -05:00
Mark Johnston
14943fbc48 thread: Ignore errors when copying out during thr_exit()
It does not seem reasonable to return to userspace after calling
umtx_thread_exit().

This is in preparation for annotating copyin() and related functions
with __result_use_check.

Reviewed by:	olce, kib
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D43143

(cherry picked from commit f450277f7a608f26624384e046c1987490c51296)
2024-01-01 19:29:53 -05:00
Mark Johnston
4b4f8106bd jail: Ignore errors from copyout() while copying the error string
Reviewed by:	zlei, jamie
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D43142

(cherry picked from commit abbc260f74b203646dd108ad4038627fcfb0ded7)
2024-01-01 19:29:52 -05:00
Mark Johnston
c7975edd99 sendfile: Explicitly ignore errors from copyout()
There is a documented bug in sendfile.2 which notes that sendfile(2)
does not raise an error if it fails to copy out the number of bytes
written.  Explicitly ignore the error from copyout() calls in
preparation for annotating copyout() with __result_use_check.

Reviewed by:	glebius, kib
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D43129

(cherry picked from commit d0adc2f283ad5db6b568ca533a056c9f635551cd)
2024-01-01 19:29:51 -05:00
Mark Johnston
f9c3b1be36 thread: Add a return value to cpu_set_upcall()
Some implementations copy data to userspace, an operation which can in
principle fail.  In preparation for adding a __result_use_check
annotation to copyin() and related functions, let implementations of
cpu_set_upcall() return an error, and check for errors when copying data
to user memory.

Reviewed by:	kib, jhb
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D43100

(cherry picked from commit 7b68fb5ab2a276ccd081cc1a43cebf0fb315e952)
2024-01-01 19:29:48 -05:00
Mark Johnston
81ee40d0a2 compat_freebsd4: Check for errors from subyte() in freebsd4_uname()
This is in preparation for adding a __result_use_check annotation to
copyin() and related functions.

Reviewed by:	imp, kib, jhb
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D43099

(cherry picked from commit 4f35450ce52a7b141e7ae8d37fa257b5f8971dda)
2024-01-01 19:29:47 -05:00
Andrew Gierth
2001d7f6a2 vfs_domount_update(): correct fsidcmp() usage
(cherry picked from commit 2a1d50fc12f6e604da834fbaea961d412aae6e85)
2023-12-29 02:34:07 +02:00
Mark Johnston
a9184e99af kthread: Set *newtdp earlier in kthread_add1()
syzbot reported a single boot-time crash in g_event_procbody(), a page
fault when dereferencing g_event_td.  g_event_td is initialized by the
kproc_kthread_add() call which creates the GEOM event thread:

  kproc_kthread_add(g_event_procbody, NULL, &g_proc, &g_event_td,
      RFHIGHPID, 0, "geom", "g_event");

I believe that the caller of kproc_kthread_add() was preempted after
adding the new thread to the scheduler, and before setting *newtdp,
which is equal to g_event_td.  Thus, since the first action of the GEOM
event thread is to lock itself, it ended up dereferencing a NULL
pointer.

Fix the problem simply by initializing *newtdp earlier.  I see no harm
in that, and it matches kproc_create1().  The scheduler provides
sufficient synchronization to ensure that the store is visible to the
new thread, wherever it happens to run.

Reported by:	syzbot+5397f4d39219b85a9409@syzkaller.appspotmail.com
Reviewed by:	kib
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D42986

(cherry picked from commit ae77041e0714627f9ec8045ca9ee2b6ea563138e)
2023-12-17 21:20:13 -05:00
Mark Johnston
c07ebf5bec kmsan: Add kmsan_check_uio()
This was handy for some ad-hoc debugging and fits in with other
kmsan_check_*() routines which operate on some kind of data container.

MFC after:	1 week
Sponsored by:	The FreeBSD Foundation

(cherry picked from commit be5464ae233ada46a778cc82f7107a10a7d5343b)
2023-12-14 09:44:38 -05:00
Mark Johnston
df81b15118 tty: Avoid a kernel memory discloure via kern.ttys
Four pad bytes at the end of each xtty structure were not being cleared
before being copied out.  Fix this by clearing the whole structure
before populating fields.

MFC after:	3 days
Reported by:	KMSAN

(cherry picked from commit 3c0fb026b2fc998fa9bea8aed76e96c58671aee3)
2023-12-14 09:29:15 -05:00
Brooks Davis
3ad2fd6fb1 makesyscalls: don't make syscall.mk by default
We only want to produce syscall.mk for the main syscall table so default
to not producing it (send it to /dev/null) and add a syscalls.conf to
sys/kern to trigger the creation of sys/sys/syscall.mk.  This eliminates
the need for entries in other syscalls.conf files and is a cleaner
pattern going forward.

Reviewed by:	kevans, imp
Differential Revision:	https://reviews.freebsd.org/D42663

(cherry picked from commit 54d487c4d01d68ef0ac03eae1fc574f7533d46f6)
2023-12-13 23:07:06 +00:00
Mitchell Horne
c27113379e kern_reboot(): don't clear kdb_active
It is possible to reach this function from ddb via the "reset" command.
When this happens, we don't actually exit kdb, meaning we never execute
the latter steps of kdb_break() to restore the system state (e.g.
re-enable scheduler).

Therefore, we should not clear the kdb_active flag in this function, as
the debugger is still active. Put differently, kern_reboot() is not an
authority on kdb state, and should not touch it. The original motivation
for this assignment is not clear; I have checked thoroughly and I am
convinced it is not required by any reset code.

This fixes an edge case where a panic can be triggered during reset from
ddb:
 1. Enter ddb via keyboard break sequence (KERNEL_PANICKED() == false &&
    td->td_critnest > 0)
 2. Execute the "reset" command
 3. kern_reboot() sets kdb_active = false
 4. A witness_checkorder() call via shutdown handler sees !kdb_active
    and panics

Reviewed by:	imp, markj
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D42684

(cherry picked from commit 4e78a766f607192698514d970ff4e9fa91d0482d)
2023-12-08 18:02:45 -04:00
Mitchell Horne
08b952c56b shutdown: tweak kproc/kthread shutdown check
This is to handle the case where the system has not panicked but the
debugger is active, where we still can't wait for thread termination.

Reviewed by:	markj
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D42683

(cherry picked from commit 960612a19f009df602a4cb008fa90a45a6e869bb)
2023-12-08 18:02:45 -04:00
Mitchell Horne
bbfd41d430 alq, siftr: add panic/debugger checks to shutdown hooks
Don't try to gracefully terminate the pkt_manager thread if the
scheduler is not running.

We should not attempt to shutdown ald if RB_NOSYNC is set, and must not
if the scheduler is stopped (the function calls wakeup()).

Reviewed by:	markj
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D42340

(cherry picked from commit d79a9edb5ce162c1ba49e12e5c93b894e6a25ad2)
2023-12-08 18:02:44 -04:00
Konstantin Belousov
c99f50351c vn_copy_file_range(): provide ENOSYS fallback to vn_generic_copy_file_range()
(cherry picked from commit c5405d1c850765d04f74067ebb71f57e9a26b8ea)
2023-12-05 02:43:27 +02:00
Konstantin Belousov
7dc643fe15 vn_copy_file_range(): find write vnodes on which to call the VOP
(cherry picked from commit a9bc8637690ce29496650a41d3c25e225ed22e3d)
2023-12-05 02:43:27 +02:00
Konstantin Belousov
6bed0f65fb VFS: add VOP_GETLOWVNODE()
(cherry picked from commit 4cbe4c48a7e574ec1bb919351d7a57d252b00b62)
2023-12-05 02:43:27 +02:00
Konstantin Belousov
71f01edb54 EVFILT_TIMER: intialize stop timer list in type-stable proc init, instead of fork
(cherry picked from commit 171f0832c5b15f2f66d2fe59967348a42814b0c1)
2023-12-05 02:43:27 +02:00
Konstantin Belousov
24346a2f77 EVFILT_SIGNAL: do not use target process pointer on detach
PR:	275286

(cherry picked from commit ed410b78edc53e17b5a3e93ace2adbeb3a734ae9)
2023-12-05 02:43:27 +02:00
Brooks Davis
01df347671 auxv: make AT_BSDFLAGS unsigned
AT_BSDFLAGS shouldn't be sign extended on 64-bit systems so use a
uint32_t instead of an int.

Reviewed by:	imp, kib
Differential Revision:	https://reviews.freebsd.org/D42365

(cherry picked from commit 326bf5089ca788d5ff1951eed7a9067281a2b65e)
2023-12-02 00:03:25 +00:00
Mateusz Guzik
746b0f70a2 vfs: dodge locking for lseek(fd, 0, SEEK_CUR)
It is very common and according to dtrace while running poudriere almost
all calls with SEEK_CUR pass 0.

(cherry picked from commit 305a2676ae93fb50a623024d51039415521cb2da)
2023-11-29 10:58:16 +00:00
Konstantin Belousov
c4a62edfe2 sysctl kern.supported_archs: return correct value
(cherry picked from commit 26b36a64be410cacbec1a5cabec091bf7de2b251)
2023-11-28 13:39:38 +02:00
Rick Macklem
a7c25f0d06 nfsd: Fix NFS access to .zfs/snapshot snapshots
When a process attempts to access a snapshot under
/<dataset>/.zfs/snapshot, the snapshot is automounted.
However, without this patch, the automount does not
set mnt_exjail, which results in the snapshot not being
accessible over NFS.

This patch defines a new function called vfs_exjail_clone()
which sets mnt_exjail from another mount point and
then uses that function to set mnt_exjail in the snapshot
automount.  A separate patch that is currently a pull request
for OpenZFS, calls this function to fix the problem.

PR:	275200

(cherry picked from commit f5f277728adec4c5b3e840a1fb16bd16f8cc956d)
2023-11-25 16:53:29 -08:00
Mike Karels
3569e21f07 khelp: suppress useless warning message on shutdown
If a module (e.g. the ertt hhook for TCP) can't clean up at
shutdown, there is nothing to be done about it.  In the ertt case,
cleanup is just shutting down a UMA zone, which doesn't need to be
done.  Suppress EBUSY warnings on shutdown.

PR:		271677
Reviewed by:	tuexen, imp
Differential Revision:	https://reviews.freebsd.org/D42650

(cherry picked from commit 415c1c748d5492e41328fedf96b6bf3c9be94595)
2023-11-24 12:11:16 -06:00
Alexander Motin
a2bb07585b ktls: Remove unneeded vm/uma_dbg.h include
It was used in original implementation, but is no longer.

MFC after:	2 weeks

(cherry picked from commit 1f8a5187ffdd9eb7d78e699f948f7bab3abf60e9)
2023-11-23 10:48:08 -05:00
Konstantin Belousov
3114e149b0 vn_lock_pair(): reasonably handle vp1 == vp2 case
(cherry picked from commit 22bac49b09a9f975330dc0d811436599ce740890)
2023-11-23 06:00:36 +02:00
Konstantin Belousov
9d7412604d kernel: add missed FEATUREs compat_freebsd 8-14
(cherry picked from commit e256f71389fbf0ef6cf8f547342d5f789e69f918)
2023-11-23 06:00:36 +02:00
Bojan Novković
ae8387cc81 tty: properly check character position when handling IUTF8 backspaces
The tty_rubchar() code handling backspaces for UTF-8 characters didn't
properly check whether the beginning of the current line was reached.
This resulted in a kernel panic in ttyinq_unputchar() when prodded with
certain malformed UTF-8 sequences.

PR:		275009
Reviewed by:	christos
Differential Revision:	https://reviews.freebsd.org/D42564

(cherry picked from commit c6d7be214811c315d234d64c6cbaa92d4f55d2c1)
2023-11-20 18:54:54 +02:00
Konstantin Belousov
0784b5768e vn_copy_file_range(): busy both in and out mp around call to VOP_COPY_FILE_RANGE()
(cherry picked from commit 23210f538a008788b2e16b9eddafa4f598a21663)
2023-11-20 03:39:22 +02:00
Konstantin Belousov
1703a823c5 vn_copy_file_range(): use local variables for invp/outvp vnodes v_mounts
(cherry picked from commit 89188bd6ba8d8332c65498f2b71c90e5ed4b9dae)
2023-11-20 03:39:22 +02:00
Olivier Certner
e191c0283e Ensure 'struct thread' is aligned to a cache line
Using the new UMA_ALIGN_CACHE_AND_MASK() facility, which allows to
simultaneously guarantee a minimum of 32 bytes of alignment (the 5 lower
bits are always 0).

For the record, to this day, here's a (possibly non-exhaustive) list of
synchronization primitives using lower bits to store flags in pointers
to thread structures:
- lockmgr, rwlock and sx all use the 5 bits directly.
- rmlock indirectly relies on sx, so can use the 5 bits.
- mtx (non-spin) relies on the 3 lower bits.

Reviewed by:            markj, kib
MFC after:              2 week
Sponsored by:           The FreeBSD Foundation
Differential Revision:  https://reviews.freebsd.org/D42266

(cherry picked from commit 7d1469e555bdce32b3dfc898478ae5564d5072b1)
2023-11-16 10:07:30 -05:00
Jason A. Harmening
66b8f5484c vfs_lookup_cross_mount(): restore previous do...while loop
When the cross-mount walking logic in vfs_lookup() was factored into
a separate function, the main cross-mount traversal loop was changed
from a do...while loop conditional on the current vnode having
VIRF_MOUNTPOINT set to an unconditional for(;;) loop.  For the
unionfs 'crosslock' case in which the vnode may be re-locked, this
meant that continuing the loop upon finding inconsistent
v_mountedhere state would no longer branch to a check that the vnode
is in fact still a mountpoint.  This would in turn lead to over-
iteration and, for INVARIANTS builds, a failed assert on the next
iteration.

Fix this by restoring the previous loop behavior.

Reported by:	pho
Tested by:	pho
Fixes:		80bd5ef070

(cherry picked from commit 586fed0b03561558644eccc37f824c7110500182)
2023-11-11 20:38:26 -06:00
Konstantin Belousov
d0b4efe6d2 vfs_domount_update(): ensure that 'goto end' works
PR:	274992

(cherry picked from commit ede4c412b3ea9289ef42c664b01b6b5ff7eac434)
2023-11-12 02:31:13 +02:00
Mark Johnston
5d15f0d540 kmsan: Correct the origin address in kmsan_shadow_check()
Otherwise a KMSAN report (which panics the system by default) could
trigger a recursive panic.

MFC after:	1 week
Fixes:	ca6cd604c8 ("kmsan: Use the correct origin bytes in kmsan_check_arg()")

(cherry picked from commit 346134f19aa9ba38a0384244609e2bcd4f7838f4)
2023-11-11 12:30:37 -05:00
Mark Johnston
8da1c37151 reboot: Avoid unlocking Giant if the scheduler is stopped
When the scheduler is stopped, mtx_unlock() turns into a no-op, so the
loop

    while (mtx_owned(&Giant))
	    mtx_unlock(&Giant);

runs forever if the calling thread has Giant locked.

Reviewed by:	mhorne
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D42460

(cherry picked from commit deacab756026f86515781944a9e0271e8db9f86b)
2023-11-06 19:23:38 -05:00
Zhenlei Huang
5019a5acc5 Giant: Postpone removal of Giant-locked drivers until 15
Reviewed by:	imp
MFC after:	1 day
Differential Revision:	https://reviews.freebsd.org/D42401

(cherry picked from commit a8bd34bfc0db38c1c11e9c36b7dd849d607c17a8)
2023-11-02 00:22:22 +08:00
Konstantin Belousov
4b2694fbcf user stack randomization: only enable by default for 64bit processes
(cherry picked from commit 1798b44fda382c473c9fc7762e162613a39dc23c)
2023-10-31 02:46:10 +02:00
John Baldwin
83a00a92da Move kern_extattr_* prototypes to <sys/syscallsubr.h>
All of the kern_* prototypes belong in this header.  While here, sort
the prototypes by function name.

Reviewed by:	dchagin
Fixes:		6453d4240f vfs: Export exattr methods to reuse by Linuxulator
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D41766

(cherry picked from commit 3555be0124a4f105c72d932f00071f332691e8cf)
2023-10-24 10:00:57 -07:00
Mark Johnston
53f62d1c9a witness: Unconditionally initialize out-params for witness_save()
As of LLVM 16, the -fsanitize-memory-param-retval option is set to true
by default, meaning that MSan will eagerly report uninitialized function
parameters and return values, even if they are not used.  A
witness_save()/witness_restore() call pair fails this test since
witness_save() may return before saving file and line number
information.

Modify witness_save() to initialize the out-params unconditionally; this
appears to be the only instance of the problem triggered when booting to
a login prompt, so let's just address it directly.

Sponsored by:	Klara, Inc.
Sponsored by:	Juniper Networks, Inc.
MFC after:	1 week

(cherry picked from commit 7123222220aa563dc16bf1989d335722e4ff57a6)
2023-10-24 09:20:55 -04:00
Mark Johnston
2d49b111a3 uiomove: Add some assertions
Make sure that we don't try to copy with a negative resid.

Make sure that we don't walk off the end of the iovec array.

Reviewed by:	kib
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D42098

(cherry picked from commit 8fd0ec53deaad34383d4b344714b74d67105b258)
2023-10-24 09:19:01 -04:00