git: 9a8d5af1ba01 - main - linux: correct errors from mremap(2)

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sat, 24 Oct 2026 02:31:05 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=9a8d5af1ba0174ec5fcc3eedba545aec9a627987

commit 9a8d5af1ba0174ec5fcc3eedba545aec9a627987
Author:     Joy <amutu@amutu.com>
AuthorDate: 2026-10-24 02:22:29 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-10-24 02:24:19 +0000

    linux: correct errors from mremap(2)
    
    Linux returns EFAULT when the range to be remapped is not mapped at all,
    and ENOMEM only when it is mapped but cannot be grown in place. Callers
    depend on that distinction: musl's pthread_getattr_np() measures the
    main thread stack by growing a one page mapping and retries while errno
    is ENOMEM. Returning ENOMEM for every growth request makes that loop
    spin forever.
    
    PR:     299835
    MFC after:      1 week
---
 sys/compat/linux/linux_misc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/sys/compat/linux/linux_misc.c b/sys/compat/linux/linux_misc.c
index e9441a31e8ac..09b41bf37a92 100644
--- a/sys/compat/linux/linux_misc.c
+++ b/sys/compat/linux/linux_misc.c
@@ -29,7 +29,6 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <sys/cdefs.h>
 #include <sys/param.h>
 #include <sys/fcntl.h>
 #include <sys/jail.h>
@@ -64,6 +63,7 @@
 #include <security/mac/mac_framework.h>
 
 #include <vm/pmap.h>
+#include <vm/vm_extern.h>
 #include <vm/vm_map.h>
 #include <vm/swap_pager.h>
 
@@ -362,7 +362,10 @@ linux_mremap(struct thread *td, struct linux_mremap_args *args)
 
 	if (args->new_len > args->old_len) {
 		td->td_retval[0] = 0;
-		return (ENOMEM);
+		if (useracc((void *)(uintptr_t)args->addr, args->old_len,
+		    VM_PROT_READ))
+			return (ENOMEM);
+		return (EFAULT);
 	}
 
 	if (args->new_len < args->old_len) {