From nobody Mon Feb 9 05:55:04 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1768662501; cv=none; d=zohomail.com; s=zohoarc; b=cSjASZo6l43vQnuUay/eA90XmMmww0cHSR65TILxss+bExUYCti7qYldmwyjiWFA/DhYYEj25zTovkuy7ZRKiy72sPO/1RPbBKw2eJ7yNvAxQFskj0TgPH9IPoYiNAvPkykdDmKmQk68EvUOE6Tlc/NtMN3b8cQlm7gYzGEVsKE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1768662501; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=D4B6UL9XHt4lGntL3nn7vKSx7UvKttOAJiq3eJbr1dk=; b=DblamP9zG8C81VtWRXD5XoTbOS+XqF0+0mYq0d2lYAytHTva0cFViC/4QmvQJaUppoC9Ra+Lb21Z6RH95p7GA8Qvfw97ZGUIXzGqg0ZSJ079nUvI9SkTFm1GuNBTCAOkWBER890lr3hnw52WUTx2QDqFgZ7MGV4BmKprrP2hJ3o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1768662501328888.2606291157663; Sat, 17 Jan 2026 07:08:21 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vh7tu-0007nF-83; Sat, 17 Jan 2026 10:07:42 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vgyRg-0003bx-DY for qemu-devel@nongnu.org; Sat, 17 Jan 2026 00:01:56 -0500 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vgyRe-0007Pq-5k for qemu-devel@nongnu.org; Sat, 17 Jan 2026 00:01:56 -0500 Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-47ee4539adfso22778755e9.3 for ; Fri, 16 Jan 2026 21:01:53 -0800 (PST) Received: from localhost.localdomain ([128.127.123.191]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4801e8c9cbcsm74669665e9.12.2026.01.16.21.01.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Jan 2026 21:01:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768626111; x=1769230911; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=D4B6UL9XHt4lGntL3nn7vKSx7UvKttOAJiq3eJbr1dk=; b=aAV+t9i5I0UpQNCTdVlsBv3JnsbjaU/DkRDGQe35yrTQNfDWaQwHEsFOK5EBGEQP5i ijArvTXOcshKjCUUp1NTAHNa5WQMMlLtigxEfl7XCMUBufa/tUhFD/mPVOdh0A+Hypou w6AERvXyoJrs0+eWyOTF90RccDfD+v5/qz2Xh/qUV6OXgTTvG4ZAV6FzqBk0KCU2D/wL rnTyeao7xWgwYDIyKFz51GOP6cqq8WaTYZlEXtALLdEiw9idkK2KGQwpT3leNwGLVnih 8FenvS1tTOFhhZ/fLCJYFd0BUtAOF9cSFKTf8n01RHMmtG7HhKIrHZzFK5dTl+wtcXXO mmRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768626111; x=1769230911; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=D4B6UL9XHt4lGntL3nn7vKSx7UvKttOAJiq3eJbr1dk=; b=vx1DDXuKKG0YoxTD72k3W/5dH3wwciTcaR336xOk1NvAKIADpG/Gr1Bce99El7pi1a TIWhDIxEhZHLgx2r8K4fJPgZNmZEqNEGSwsqjyQ00QxH0NK3iq7Q4fz8DcjD5bRjb4H9 bzplGB8Usw0Lu9P8/kxC1uS293I3JpwDXI6JYexb4BP6X49+6w4zgnwV+Qdiun++cQWc AZFuZWi1zwwPLSmTivCNSQGZp45DG1vuedglPeYYE9XlQRu/QSMGbc+K7X6DAjph1qp6 jnlVuaV16buF44d4Q6FQ/4Ano1qeDowg0jy0/YqcBNiF7J90yNFJlGOJw1Qt1W19eieM zNEw== X-Gm-Message-State: AOJu0YwLH1lMm1AqjHjK9JVCSijhpIuYSQh3uFG/+L3eIcsk5YE2wnQ0 zcJi8GJEuuvjUY5cfH17wCU6RkBzO8frr5qAemAmXz8HFIx/E4LoK8uIF6luzw== X-Gm-Gg: AY/fxX4lfnEhkA/eWK/WrnpCymAuYka6HYZUC8pOdNRI/EZ/n2oG1rvsKveSvqWTJjw OU3bgEesTwscMo8qJ5dXDq8wj6r11Cb6A/2buLu9i+MyNDwY/H6rFpYR6BYDeXBffvPOsW9MgXc KYXU5Q3Xj/RzT0BcB4N1TmOjR6hsCs+1xhPQ6k1+17/9ei0pqVV03LOcqDk+JRCJfVPyZwtNExG 6AWnV4/SYK3yAOvb67byTh2FERAFt9E9nc/0fEF5uvd94vJnF1Dw4TYLoOuv1r/TXr5NlNGqSGM XhS9sc5k37i3SSfzCeq3/CXJ18jdUO4T5CbqFmj2LVZgkJnMYzGTGinf26sZqCU67jbG5H4xd/j zyu1x8VXNiOZUFtTiqVAS2CkD8KOX2fyS9lLa6nbL7IsSCn7eveHJcfgMMKkkvS7rZXXhcybmJo iVrg0RhsG2oHpAmFIHVQQoNwqJFiotvK5QFA== X-Received: by 2002:a05:600c:628c:b0:47d:403e:9cd5 with SMTP id 5b1f17b1804b1-4801e30b6ffmr51141055e9.11.1768626110426; Fri, 16 Jan 2026 21:01:50 -0800 (PST) From: Razvan Ghiorghe To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, Razvan Ghiorghe Subject: [PATCH] linux-user: Fix mremap() with old_size == 0 Date: Sat, 17 Jan 2026 07:00:49 +0200 Message-ID: <20260117050049.24057-1-razvanghiorghe16@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::32a; envelope-from=razvanghiorghe16@gmail.com; helo=mail-wm1-x32a.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sat, 17 Jan 2026 10:07:39 -0500 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1768662502628158500 Content-Type: text/plain; charset="utf-8" When old_size is zero and old_address refers to a shareable mapping, mremap() should create a new mapping of the same pages according to the mremap(2) man page. The MREMAP_MAYMOVE flag must be specified in this case. Previously, QEMU's target_mremap() rejected this valid case with EFAULT dur= ing the initial validation, before checking for the special old_size =3D=3D 0 b= ehaviour. This patch adds proper handling for old_size =3D=3D 0: - Validates that MREMAP_MAYMOVE flag is set (required by man spec) - Passes the call through to the host mremap() - Creates a new mapping without invalidating the original, with both beeing valid and sharing the same physical memory frames. Tested with the reproducer from the issue on qemu-riscv64. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3105 Signed-off-by: Razvan Ghiorghe --- linux-user/mmap.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 4bcfaf7894..08de949f06 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -1117,6 +1117,38 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong= old_size, errno =3D EINVAL; return -1; } + + if (!old_size) { + if (!(flags & MREMAP_MAYMOVE)) { + errno =3D EINVAL; + return -1; + } + mmap_lock(); + if (flags & MREMAP_FIXED) { + host_addr =3D mremap(g2h_untagged(old_addr), old_size, new_siz= e, + flags, g2h_untagged(new_addr)); + } else { + host_addr =3D mremap(g2h_untagged(old_addr), old_size, new_siz= e, + flags); + } + =20 + if (host_addr =3D=3D MAP_FAILED) { + mmap_unlock(); + return -1; + } + new_addr =3D h2g(host_addr); + prot =3D page_get_flags(old_addr); + /* + * For old_size zero, there is nothing to clear at old_addr. + * Only set the flags for the new mapping. They both are valid. + */ + page_set_flags(new_addr, new_addr + new_size - 1, + prot | PAGE_VALID, PAGE_VALID); + shm_region_rm_complete(new_addr, new_addr + new_size - 1); + mmap_unlock(); + return new_addr; + } + if (!guest_range_valid_untagged(old_addr, old_size)) { errno =3D EFAULT; return -1; --=20 2.43.0