From patchwork Wed Aug 21 02:25:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 820950 Delivered-To: patch@linaro.org Received: by 2002:adf:e98a:0:b0:367:895a:4699 with SMTP id h10csp504910wrm; Tue, 20 Aug 2024 19:27:26 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWT0AKPEwUBNMHMa/wYfbn/bl/PiU8Sn6C8+T8+U60M86ubREDFFpOHKVtuISO1TLfRlp/bwA==@linaro.org X-Google-Smtp-Source: AGHT+IGSC7xXKavmO9euJSJ7Z409P3cja1t6jjy6JMCM/PhwzzNAo6UErzc+MkRBXuC1cb7tAIa9 X-Received: by 2002:a05:6902:11c9:b0:e03:6085:33e9 with SMTP id 3f1490d57ef6-e1665411570mr1681432276.2.1724207246489; Tue, 20 Aug 2024 19:27:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1724207246; cv=none; d=google.com; s=arc-20160816; b=ExbTim3Nlqp57wTgwAmjveNqXj3qyI6WFdkZnfasYgtla8O1uV2nWdq6P0WEmdwAXM zPK6ynJF8WxrBkQ7t3eOZBWh1FMnpWfQxsutaUKNEbI92RT8szhIIAtQag8WY1PorwmS V9YgD4v86UMhyYrzK2R7P/+to1b3USXoVJtz8S55Z5bBX9mTQV6Iem2lPXuE1uHqyS6k Sb7f4uVPWI2cBoNzWydVSktooU+fXgp1plFAht1TLUweRf2W67SrIvk/U8qinr/XUoME yUTMTZ2pIFyUeXENXBrZEX0k++v6Qy1eH68ZpupPTBhjkN4cueuts3+iBtRzvtNfPBTV NbrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=S39pbsi04lxxgffqB3S31UEXKo2fS7pxTQUq/De0mMM=; fh=aYwv1qvAWDHKcZLZyhvLuIoAkBRd6ysYy/Itgr1WhFg=; b=ps+eUcf0kYnBxg1QeFv5HRI+mifwuDerFnOiUg4KpuVdc2ohstEwAn/mDX98hOQJA9 gN9N+NE1rXYjjgUXUH6gsbC0Xla/2ZA8TlYoKcC9ahgCP1e9XUY7Jp2UOAWKw0aZVRmA JtZ42lf/GHXAZXaq9bfJ46+C3U3qJzHQ9n7c/I2yk026oLyWYqj+PgQNNcX4e9/GZVo5 RHvens3Nf1mffaKA5iXFRM4cTSnbkI6n0hqvrd80xAPyXFMT+yMI5adGzOlXv6ezMngM w7uaDbco3fN4m1REGt7j/HvNTL9R6wpAJyFqz0eD9CPdaqjEtEqSNBlkLIQwmsaC73G9 G6xQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ff83HT2+; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d75a77b69052e-4536a00291esi137667071cf.178.2024.08.20.19.27.26 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 20 Aug 2024 19:27:26 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ff83HT2+; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sgb30-0000eO-A9; Tue, 20 Aug 2024 22:26:06 -0400 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 1sgb2y-0000VH-Ac for qemu-devel@nongnu.org; Tue, 20 Aug 2024 22:26:04 -0400 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sgb2v-0006DM-Uc for qemu-devel@nongnu.org; Tue, 20 Aug 2024 22:26:03 -0400 Received: by mail-ot1-x336.google.com with SMTP id 46e09a7af769-7093c94435bso2381032a34.0 for ; Tue, 20 Aug 2024 19:25:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1724207157; x=1724811957; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=S39pbsi04lxxgffqB3S31UEXKo2fS7pxTQUq/De0mMM=; b=Ff83HT2+bhzCkN8PtwxeEw6vJ26ve01tJoWokh4hhWdjQ1UwFvds7mJSdpB+AVW1bh Z+APVO99bbSGnYnlcVxG9rv2MT/1p2k8xbuaOyWP8F/EVybAhlMa6shEvuBUbGLs3WUf zWvU3b5gsXwPNbIKLPObzQP5pUsmM1NGpUATORAqFWxP+M7JF6dhVFIaidYSAcgWz3YL Ooe6YBvL7j9ignw/TEzBDRT9n44xO1PYX+jN2AZmC7nq73SiE9ZRPMWPhFlpa7yS3pPQ 1tyx5REKwVvqQneq7elKdJhFjaeAj0Fuebat/lbEzf7NFt8zE9DlagV52fkyGIzMjjOj GTvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724207157; x=1724811957; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=S39pbsi04lxxgffqB3S31UEXKo2fS7pxTQUq/De0mMM=; b=ehxB/ypFojd7qNpBmlyQq+J+xvfoEcsDX8YvQEYc8xYQPcJw2/7NMCFHf63SrAEN08 Zyhhu37w1b1NGzWh3yW0Mw1aypkzDIN8olG/bHF773f3EXsT/YjoHEZT8Z9BLtF5sSzm Dtoiwy5wKBAIiYaV+8i3+8X9hpSAP7Sd/4uJyq/k3y1CPxduRl2k3Kj006vxHDNcRGhU 9KhyCb82uFipLMcjR3fFi4b3TZ8Fp9mSBTZozNmp8MddJH+Al+64aAy+flR1PlBIQoGt 3J7kWB2xB3q5iNuzktq1xAoIJdHHYR74k7t8qdvUbxUdu7InLHua3lMPFejTvMSbPt2X WXfQ== X-Gm-Message-State: AOJu0YyAfSFZOwCr+XskAz+g66w/Xku7AeEQYJBSZAGtnZJmu71rOWQx hZ9EVVkGkOXlGrkcq+Xixb1st13qf1wbdb2AzMhe8nvK0yfrWCrPuBBd30W6v/kn6uM/RaBIlB+ 0HiM= X-Received: by 2002:a05:6870:82a2:b0:258:5143:a21a with SMTP id 586e51a60fabf-2737ef0023bmr845403fac.13.1724207156792; Tue, 20 Aug 2024 19:25:56 -0700 (PDT) Received: from stoup.. ([203.56.140.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7127af1888esm9210774b3a.149.2024.08.20.19.25.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Aug 2024 19:25:56 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PULL 1/5] linux-user: Handle short reads in mmap_h_gt_g Date: Wed, 21 Aug 2024 12:25:44 +1000 Message-ID: <20240821022548.421745-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240821022548.421745-1-richard.henderson@linaro.org> References: <20240821022548.421745-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::336; envelope-from=richard.henderson@linaro.org; helo=mail-ot1-x336.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org In particular, if an image has a large bss, we can hit EOF before reading all host_len bytes of the mapping. Create a helper, mmap_pread to handle the job for both the larger block in mmap_h_gt_g itself, as well as the smaller block in mmap_frag. Cc: qemu-stable@nongnu.org Fixes: eb5027ac618 ("linux-user: Split out mmap_h_gt_g") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2504 Signed-off-by: Richard Henderson Message-Id: <20240820050848.165253-2-richard.henderson@linaro.org> Reviewed-by: Philippe Mathieu-Daudé --- linux-user/mmap.c | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 6418e811f6..e4bf5d5f39 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -283,6 +283,40 @@ static int do_munmap(void *addr, size_t len) return munmap(addr, len); } +/* + * Perform a pread on behalf of target_mmap. We can reach EOF, we can be + * interrupted by signals, and in general there's no good error return path. + * If @zero, zero the rest of the block at EOF. + * Return true on success. + */ +static bool mmap_pread(int fd, void *p, size_t len, off_t offset, bool zero) +{ + while (1) { + ssize_t r = pread(fd, p, len, offset); + + if (likely(r == len)) { + /* Complete */ + return true; + } + if (r == 0) { + /* EOF */ + if (zero) { + memset(p, 0, len); + } + return true; + } + if (r > 0) { + /* Short read */ + p += r; + len -= r; + offset += r; + } else if (errno != EINTR) { + /* Error */ + return false; + } + } +} + /* * Map an incomplete host page. * @@ -357,10 +391,9 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, /* Read or zero the new guest pages. */ if (flags & MAP_ANONYMOUS) { memset(g2h_untagged(start), 0, last - start + 1); - } else { - if (pread(fd, g2h_untagged(start), last - start + 1, offset) == -1) { - return false; - } + } else if (!mmap_pread(fd, g2h_untagged(start), last - start + 1, + offset, true)) { + return false; } /* Put final protection */ @@ -853,8 +886,7 @@ static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len, } if (misaligned_offset) { - /* TODO: The read could be short. */ - if (pread(fd, p, host_len, offset + real_start - start) != host_len) { + if (!mmap_pread(fd, p, host_len, offset + real_start - start, false)) { do_munmap(p, host_len); return -1; }