From patchwork Tue Feb 11 22:50:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970806 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 62E5BC0219B for ; Tue, 11 Feb 2025 22:52:22 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz62-0005bh-OD; Tue, 11 Feb 2025 17:51:14 -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 1thz61-0005bT-AF for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:13 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz5y-00052V-8G for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:12 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314267; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0ArIgTFYZEsCzE0iuv6JP2PM8E5g69hywmt07dGImK4=; b=hFwibjyzausMc6kaQCn1xRoJvHE1bRn96TVwHy18HunmnTSOtEv4RYbHjm0UfzqAPrSZS/ IuK49xM5QgMFLwVr/cBr97u2x0194TgSnFD/ZkOTw4OxHFGoUDBWxWxV/q0oECN33TNh6P LVQ0r+W9768HS54WNsq218VcS/51e0k= Received: from mail-oo1-f72.google.com (mail-oo1-f72.google.com [209.85.161.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-221-n1ClbUsFP4eJFnMMRT_mCQ-1; Tue, 11 Feb 2025 17:51:06 -0500 X-MC-Unique: n1ClbUsFP4eJFnMMRT_mCQ-1 X-Mimecast-MFC-AGG-ID: n1ClbUsFP4eJFnMMRT_mCQ Received: by mail-oo1-f72.google.com with SMTP id 006d021491bc7-5fc5eb91ab2so4392803eaf.2 for ; Tue, 11 Feb 2025 14:51:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314265; x=1739919065; 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=0ArIgTFYZEsCzE0iuv6JP2PM8E5g69hywmt07dGImK4=; b=BoF0V6jBWkUWu2uVHIX6917QimpQIs8XbZhZOXuS123CJq0h1xMFfXFzi9iggMyQgP LHduzKHs9lZgPrdohBzNRQbyvkcugFrtr1twg6eC/SVZgROqdVlAyZamiPObzxTsL/X+ PGItgcmLLpo505rlIgjcYiIRRCFIrh8gfCXTTGg0Jerbi1TM4v4lR9CxSN0IqdSrccjp 8wr4ZhS2EqArVs8OU93JIuD57N16ReBbqnzRNnkFa1pIeHPs46IXwFzkDK9AvPIySkTP ToRWPDsJDuRxbfDxk7oo8bDaQFgTGiEfsbxarAWjRvVn5gnRNBHDuZjPC7YNN2E4984q iS5w== X-Gm-Message-State: AOJu0YzzKVlwBeEhIY/wryLWcraxt63Jj3DKgdqs3t8IW2SDC8iTcs1S RumZikHGTAC+T0fvVIpejXLCgLtSlG7afHindlux1qIDb4D6WWEvluWuacxm4zKlWG6ig0Nwtkh Aw2mxvO55ii4StAGTIS8fwXsNj13kUDC2Q10LcTPvFdcXzzyi2zVIQE0645aPJ5JSFSHCQdU0bt ikOKgeQtHD7OyrJUaABanX5Wsx4SvSlMrymw== X-Gm-Gg: ASbGncteEad/29q6orP8joFb8CcTh2Ez3qvq7z6WovLNDrMC/2woPfTmOGRqYRvxdpM 3aXSRc9W0fyz8hthFryUqKIUKvisEJGQIgogggrhtFgVE96t14QxIFaf20l4qQ8ssmTj3MrzdPS SJcg0I4aqYRILsoZzSRuJ+dSeaZCThldnjqOXCXcViknZ5NcsrBq0LLPqPofjZaxHB/Yk4CXrbU ecjJJaFUDN+9MVHtV30WxniQNBTN87kcn6OcBdX5MV26BAvTpTdpKvb X-Received: by 2002:a05:6820:161e:b0:5fc:947b:c337 with SMTP id 006d021491bc7-5fca141b8bcmr768850eaf.0.1739314265042; Tue, 11 Feb 2025 14:51:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IG0QtoAj7YScHU6hKZ6TE7PCH+WlOG5xicGY808DUUNerDrm7xAi0Lh8DfPAQE1748nNQUgfg== X-Received: by 2002:a05:6820:161e:b0:5fc:947b:c337 with SMTP id 006d021491bc7-5fca141b8bcmr768840eaf.0.1739314264693; Tue, 11 Feb 2025 14:51:04 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:03 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand , William Roche Subject: [PULL 01/14] system/physmem: take into account fd_offset for file fallocate Date: Tue, 11 Feb 2025 17:50:45 -0500 Message-ID: <20250211225059.182533-2-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: William Roche Punching a hole in a file with fallocate needs to take into account the fd_offset value for a correct file location. But guest_memfd internal use doesn't currently consider fd_offset. Fixes: 4b870dc4d0c0 ("hostmem-file: add offset option") Signed-off-by: William Roche Reviewed-by: Peter Xu Reviewed-by: David Hildenbrand Link: https://lore.kernel.org/r/20250122194053.3103617-2-william.roche@oracle.com Signed-off-by: Peter Xu --- system/physmem.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/system/physmem.c b/system/physmem.c index 67c9db9daa..235015f3ea 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -3797,18 +3797,19 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length) } ret = fallocate(rb->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, - start, length); + start + rb->fd_offset, length); if (ret) { ret = -errno; - error_report("%s: Failed to fallocate %s:%" PRIx64 " +%zx (%d)", - __func__, rb->idstr, start, length, ret); + error_report("%s: Failed to fallocate %s:%" PRIx64 "+%" PRIx64 + " +%zx (%d)", __func__, rb->idstr, start, + rb->fd_offset, length, ret); goto err; } #else ret = -ENOSYS; error_report("%s: fallocate not available/file" - "%s:%" PRIx64 " +%zx (%d)", - __func__, rb->idstr, start, length, ret); + "%s:%" PRIx64 "+%" PRIx64 " +%zx (%d)", __func__, + rb->idstr, start, rb->fd_offset, length, ret); goto err; #endif } @@ -3855,6 +3856,7 @@ int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start, int ret = -1; #ifdef CONFIG_FALLOCATE_PUNCH_HOLE + /* ignore fd_offset with guest_memfd */ ret = fallocate(rb->guest_memfd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, start, length); From patchwork Tue Feb 11 22:50:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970802 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5AC01C0219B for ; Tue, 11 Feb 2025 22:51:52 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz66-0005dN-V2; Tue, 11 Feb 2025 17:51:20 -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 1thz62-0005bf-4C for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:14 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz5y-00052l-TP for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314269; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SE2z04Oi2z9AX9NwFPYt/K+wzNu5iFAB6c4IKl++UyY=; b=QXIqfUemfTIokNZR/S5KOhzWBNdpinrBw2cLzkbYwgM45qFZVgmJVlcoyRak4aQJ5/DzdL NyqltNpVT73ihYr7hNS+uDhLScoaVuJwMB5VIJrrj4WjYMH8tRqb5DBL6lqsudiZmv1sfc 1yPeWMSfpnvruUzrhoMcR2N7YgJFpNk= Received: from mail-oo1-f72.google.com (mail-oo1-f72.google.com [209.85.161.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-602-8b5FgZqWOt6EggRAjm6unA-1; Tue, 11 Feb 2025 17:51:08 -0500 X-MC-Unique: 8b5FgZqWOt6EggRAjm6unA-1 X-Mimecast-MFC-AGG-ID: 8b5FgZqWOt6EggRAjm6unA Received: by mail-oo1-f72.google.com with SMTP id 006d021491bc7-5f6e3f02e27so1604475eaf.3 for ; Tue, 11 Feb 2025 14:51:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314267; x=1739919067; 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=SE2z04Oi2z9AX9NwFPYt/K+wzNu5iFAB6c4IKl++UyY=; b=uAWaIzLDIW3HdjDiLRj87vLBaFcSAf86HvIX0mdGNU+iEDOJH6ozBW6MKwCuAZq9DU g81vguQJzAi6zdU6muxhFnvJrVg+Wmjej8VZV++sKbanfKCJ9Ow2ttlOxi9ol5qBpLwL SCtQ/Mnvqrxzg8eRBsFoW9VSFHZp2tTkzDWUwA7OA83yk4XVpfvoeD5woZpofS/r3TLC eKgEwve6roDgSZvwnGBDIAeRoU4N06remws6jM5OvaIzdjpHNojf6IWpB9N+j7I6oNcz 9ubOFOPnptVzKhYsf44HW2UV1swDgdF4R/I0kc4dNur81mAl79m1L9rGCD9nJjVFyFBQ UhNw== X-Gm-Message-State: AOJu0YwhpZNicUmku+XEiMX+dtwNtmyWxpOjNdem+yKe0kD/ky/+Tewx zdpnSkD+Bo5m7KQbFK8Mo+eTMK05ewfSiQ9pfb7+9IXlW/2tZDhJ2nv9FCG+BzADD06pq8rbQXh lyYG2t9ZxNObZfOvnijI0DK3E3jwzJfe2h1h+BG75aJMBF4ucPvZCGu6Pom39ukkyzMM6iUeptH p9KKsNQ5VbmQFsUAoow+ESBdATsu08NNKEkw== X-Gm-Gg: ASbGncv69tS6mwi7cy1IByZusnyYXtEt4a1mi0rxWDKGCjmjGWUeNC6TiazmYcl/otg 4/hS5/PekZJwGvYoMak7wGHcc1uls0HQK4k3ffM51K8Uy2/XOVdIp34Hhmvl+6/xGJba3PNYezn FmItzy+xD74dzj2Nco7J9VHpp22dujhO8LSOllw9mdnwr+gMLRmYDcC69cvuveBf0RPZ1fYhzA3 NXS0FWm/UdOGf/iROR3myndpmfBTAU6m7/kqK/FfK8AofZJIRpBGW0m X-Received: by 2002:a05:6820:c88:b0:5fc:9f93:4af0 with SMTP id 006d021491bc7-5fca1452b9emr867449eaf.0.1739314267241; Tue, 11 Feb 2025 14:51:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IEb+1Dwwj/tSHWL3b54CbYtgo/vlaibQG224JBS+BkBQ4IRg8Q5VGFoVXeob98looMgi3hbPg== X-Received: by 2002:a05:6820:c88:b0:5fc:9f93:4af0 with SMTP id 006d021491bc7-5fca1452b9emr867434eaf.0.1739314266844; Tue, 11 Feb 2025 14:51:06 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:05 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand , Daniil Tatianin , Vladimir Sementsov-Ogievskiy Subject: [PULL 02/14] os: add an ability to lock memory on_fault Date: Tue, 11 Feb 2025 17:50:46 -0500 Message-ID: <20250211225059.182533-3-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Daniil Tatianin This will be used in the following commits to make it possible to only lock memory on fault instead of right away. Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Peter Xu Signed-off-by: Daniil Tatianin Link: https://lore.kernel.org/r/20250123131944.391886-2-d-tatianin@yandex-team.ru Signed-off-by: Peter Xu --- include/system/os-posix.h | 2 +- include/system/os-win32.h | 3 ++- migration/postcopy-ram.c | 2 +- os-posix.c | 10 ++++++++-- system/vl.c | 2 +- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/system/os-posix.h b/include/system/os-posix.h index b881ac6c6f..ce5b3bccf8 100644 --- a/include/system/os-posix.h +++ b/include/system/os-posix.h @@ -53,7 +53,7 @@ bool os_set_runas(const char *user_id); void os_set_chroot(const char *path); void os_setup_limits(void); void os_setup_post(void); -int os_mlock(void); +int os_mlock(bool on_fault); /** * qemu_alloc_stack: diff --git a/include/system/os-win32.h b/include/system/os-win32.h index b82a5d3ad9..cd61d69e10 100644 --- a/include/system/os-win32.h +++ b/include/system/os-win32.h @@ -123,8 +123,9 @@ static inline bool is_daemonized(void) return false; } -static inline int os_mlock(void) +static inline int os_mlock(bool on_fault) { + (void)on_fault; return -ENOSYS; } diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c index 6a6da6ba7f..fc4d8a10df 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -652,7 +652,7 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis) } if (enable_mlock) { - if (os_mlock() < 0) { + if (os_mlock(false) < 0) { error_report("mlock: %s", strerror(errno)); /* * It doesn't feel right to fail at this point, we have a valid diff --git a/os-posix.c b/os-posix.c index 9cce55ff2f..48afb2990d 100644 --- a/os-posix.c +++ b/os-posix.c @@ -327,18 +327,24 @@ void os_set_line_buffering(void) setvbuf(stdout, NULL, _IOLBF, 0); } -int os_mlock(void) +int os_mlock(bool on_fault) { #ifdef HAVE_MLOCKALL int ret = 0; + int flags = MCL_CURRENT | MCL_FUTURE; - ret = mlockall(MCL_CURRENT | MCL_FUTURE); + if (on_fault) { + flags |= MCL_ONFAULT; + } + + ret = mlockall(flags); if (ret < 0) { error_report("mlockall: %s", strerror(errno)); } return ret; #else + (void)on_fault; return -ENOSYS; #endif } diff --git a/system/vl.c b/system/vl.c index 9c6942c6cf..e94fc7ea35 100644 --- a/system/vl.c +++ b/system/vl.c @@ -797,7 +797,7 @@ static QemuOptsList qemu_run_with_opts = { static void realtime_init(void) { if (enable_mlock) { - if (os_mlock() < 0) { + if (os_mlock(false) < 0) { error_report("locking memory failed"); exit(1); } From patchwork Tue Feb 11 22:50:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970814 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E7EE5C021A0 for ; Tue, 11 Feb 2025 22:53:30 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz65-0005ck-0m; Tue, 11 Feb 2025 17:51:17 -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 1thz62-0005bg-7j for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:14 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz5z-000536-Tu for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314271; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VtKB83nCEIcfUbQsfBWETjnjuPR1FjREZuxsSZmMbL0=; b=IPpmzgE9imYLua/xnW7SGJ81UL0U+ba5HMjOkB0Mhwq61+Gn93ecdjc4JZaasAFZ0fUjn4 aOLV/UPyFqImh+mM/nbP7z3ATn9eFWvf3l4ymrRBjPAr2NzpbQ/hwhOyg037YKMXUxIeT4 fPk9EXhANfqbH0n3ZSzxBx4Gpa8rFy4= Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-624-zTBhmZclMY2McRWCTTQqwg-1; Tue, 11 Feb 2025 17:51:10 -0500 X-MC-Unique: zTBhmZclMY2McRWCTTQqwg-1 X-Mimecast-MFC-AGG-ID: zTBhmZclMY2McRWCTTQqwg Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-724d17262dfso712909a34.1 for ; Tue, 11 Feb 2025 14:51:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314269; x=1739919069; 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=VtKB83nCEIcfUbQsfBWETjnjuPR1FjREZuxsSZmMbL0=; b=pskeYWcu8Lqe4j1ogj7SMDbWcFGyxjfP+wNjkuh0DFCpgSXZgFHYCL3rcXqYEOKfvo d1q5I9XIIbS/XOctZLGrQao6eh7vz6cJbq6APMb4EtE21UPVxh/eRCjPkUIhbVhNKg99 +U+9zy5FvEzhhXCcvnJq1V6smY4GdS9iLXsHZBCfs8fzF3PJxB+ws3qVUMzj1sT5hxuy YRl4Z+gg76KQITylAB9p1dcKeNSVDryK2UmXtcwOFVlhvNraZnDJ+pk8iYU0mw/2sMJe fNXp63fqIsIwwNAhWoNl67RZWwfyjjexa2fzMa/B1z3dYc7UmxYXGz7ZaFr7lDNn+H0S NQ7A== X-Gm-Message-State: AOJu0YwP4YE0Q7cIi1imahvpKGHF0HrsWtBE7PU/uGAB/coSAeBxf10Q DW9eaRHzk7wv+79/1TlGPFZUevoagbWcbtJOP+d04Gd18ofvNjWGK4okNPLxFLOgyvte1BboXg/ PKTUbtYFq7+Y3OIBnmTfl7/WhdIfauIkZkTZ9y9++5GA8VEcnQOKOyb2C4mE2id93KROF6K3sJy UcXVG/bvw3E3frgRVJxu+9GbRmxcGaEwsAgA== X-Gm-Gg: ASbGncsokzFki2tZ4QiXnxxBALdLxmNGfyOR8g6ArUTrTZbEML/aVoqdxPpQBcyQJRn xnE7Q6FK1fcNPWCef9PQr+By9OZA93EN3t6AKXpk4yzJeXn3LqVdFrEcik0d1ZAQEKBNpwHZLpF fog35xUSaIhBXiV0Z5sncuo6NLOndtH/gKgvKKvVUjYbv9avCMvS2Bxb8sIVcEjqD6v6hIBNofd Og5Tlb0aQTW0Mmp9hv1oboxz0xE93Id3RXPsco6tQzOpDc+rgJaiVLl X-Received: by 2002:a05:6830:3881:b0:710:fef4:3c92 with SMTP id 46e09a7af769-726f1d17825mr996842a34.21.1739314268937; Tue, 11 Feb 2025 14:51:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IF7BhxmEAcP5cvyNQrF3iK+rfkuaRAMaEMgG64EKe4BizwGguLyDBbiQgoUU6MlNQoQa/kstg== X-Received: by 2002:a05:6830:3881:b0:710:fef4:3c92 with SMTP id 46e09a7af769-726f1d17825mr996819a34.21.1739314268502; Tue, 11 Feb 2025 14:51:08 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:07 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand , Daniil Tatianin , Vladimir Sementsov-Ogievskiy Subject: [PULL 03/14] system/vl: extract overcommit option parsing into a helper Date: Tue, 11 Feb 2025 17:50:47 -0500 Message-ID: <20250211225059.182533-4-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Daniil Tatianin This will be extended in the future commits, let's move it out of line right away so that it's easier to read. Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Peter Xu Signed-off-by: Daniil Tatianin Link: https://lore.kernel.org/r/20250123131944.391886-3-d-tatianin@yandex-team.ru Signed-off-by: Peter Xu --- system/vl.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/system/vl.c b/system/vl.c index e94fc7ea35..72a40985f5 100644 --- a/system/vl.c +++ b/system/vl.c @@ -1875,6 +1875,19 @@ static void object_option_parse(const char *str) visit_free(v); } +static void overcommit_parse(const char *str) +{ + QemuOpts *opts; + + opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"), + str, false); + if (!opts) { + exit(1); + } + enable_mlock = qemu_opt_get_bool(opts, "mem-lock", enable_mlock); + enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", enable_cpu_pm); +} + /* * Very early object creation, before the sandbox options have been activated. */ @@ -3575,13 +3588,7 @@ void qemu_init(int argc, char **argv) object_option_parse(optarg); break; case QEMU_OPTION_overcommit: - opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"), - optarg, false); - if (!opts) { - exit(1); - } - enable_mlock = qemu_opt_get_bool(opts, "mem-lock", enable_mlock); - enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", enable_cpu_pm); + overcommit_parse(optarg); break; case QEMU_OPTION_compat: { From patchwork Tue Feb 11 22:50:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970800 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4C138C0219B for ; Tue, 11 Feb 2025 22:51:49 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz69-0005eB-OQ; Tue, 11 Feb 2025 17:51:22 -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 1thz63-0005cW-Is for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:16 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz61-00053c-RO for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314273; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FNdOQtXSD/YnuXud8aU8SilNqnqdx7h5sqLzM9zGUhg=; b=XIz4NttMNWifwKLwv1OGKOf+2P7qPIMwB0A9ABLHk7167oEWs82bgXxLQQC+oYMzP8/Exk E0xHfkl+Ku0s/AenFf8hdxLcB9R+ZNglQZ9UAO9inT8oIiJ4esmI/DHs6xvMvP/ytPG8/a oUl3jeYJkV10vWXbhehxCCoukktsROk= Received: from mail-ot1-f71.google.com (mail-ot1-f71.google.com [209.85.210.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-356-hmyZV6MlPWq03kcOmR3iJg-1; Tue, 11 Feb 2025 17:51:12 -0500 X-MC-Unique: hmyZV6MlPWq03kcOmR3iJg-1 X-Mimecast-MFC-AGG-ID: hmyZV6MlPWq03kcOmR3iJg Received: by mail-ot1-f71.google.com with SMTP id 46e09a7af769-726d5bafe5cso954111a34.2 for ; Tue, 11 Feb 2025 14:51:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314271; x=1739919071; 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=FNdOQtXSD/YnuXud8aU8SilNqnqdx7h5sqLzM9zGUhg=; b=CBACywkvaI/dZf4al1vFLhzACFSxH+7/Qdzb6Q2lqvO9qVvfeUOApHahWNQS/EnweY 88tV7HFNtd9pEHXhtvtAqqhk+J8b+mdkvZuaa235b3Pr6EHtZFPqxrO7O/fPAvSXCk6U mFGr7Cj4yg3HGZjuP/kMrulGGC3f6RNs+Owd2MBLFrPr9hX6gppzFoqvPIqg2MJix3Ah /6QQsBogWf4g8n3PtRfZPuWV/pWm3RbEl0SnI2+Gg5g/U2DQISa8ExsQqPoshGLsu2u9 l0GPe4w0M1ihBHl19lwkn0OAEQ3pJ2BXuEqZTWDkTDhb9AObGHccJaI47nE9yF+M2mWK eRng== X-Gm-Message-State: AOJu0YwRSrYZUxuekoDNFgbM2rUjrbz1OVZG2Jyl/WssulBcskGYfJlK lqqqhMslTQWLWAvgOADNyP37lsrt1TSKvCEBkLCXkNiD/1/K6h1qwJd3l4hzlQUrfH0+9eGcSdT hhEOqIt2rsyBJuvcdXGRBbxF/cS3yDOva0F+lAW9duqMTaEageJUs8x80zjiS7HWxLE1kFmDmTT Pz2qkoKVt5gKW8wPG1XhVgewmY9FUcJzduTw== X-Gm-Gg: ASbGnct8zRN1f3c+jkeHONgsx/HbFJ7dJ3haWvRR5a8xMtyssidtQmTNxYBgCFlqxW4 zmVDu33tObMUFCn+YB/JKNzI94xTaCp2rhgOYEy+mjrpDLdnF0TPsOxLAJry+YyYhgwWh6slu3p fzR/x4JIeaiVfKMGFedLfBxvNP3/F5BMdRj9XcqbgAnt3QbEtd9xiv9W+Bq7NWwy0Jnqfm1vJO0 nFABEdFNggpgXmvYL79tpHOSW1lt5IG4gUZqbHPh2mmpdFCjVRlQKUM X-Received: by 2002:a05:6830:6d85:b0:71e:1c5:4f7c with SMTP id 46e09a7af769-726f1c47a12mr846461a34.14.1739314270817; Tue, 11 Feb 2025 14:51:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFXgyCV7ZKA+fjyxoQXVJDw2kFDFr8i8zE2S2cyXg140jCZbOwWKhQFZVhBhjrEaDi8yjBa5Q== X-Received: by 2002:a05:6830:6d85:b0:71e:1c5:4f7c with SMTP id 46e09a7af769-726f1c47a12mr846443a34.14.1739314270367; Tue, 11 Feb 2025 14:51:10 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:09 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand , Daniil Tatianin , Vladimir Sementsov-Ogievskiy Subject: [PULL 04/14] system: introduce a new MlockState enum Date: Tue, 11 Feb 2025 17:50:48 -0500 Message-ID: <20250211225059.182533-5-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Daniil Tatianin Replace the boolean value enable_mlock with an enum and add a helper to decide whether we should be calling os_mlock. This is a stepping stone towards introducing a new mlock mode, which will be the third possible state of this enum. Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Peter Xu Signed-off-by: Daniil Tatianin Link: https://lore.kernel.org/r/20250123131944.391886-4-d-tatianin@yandex-team.ru Signed-off-by: Peter Xu --- include/system/system.h | 10 +++++++++- hw/virtio/virtio-mem.c | 2 +- migration/postcopy-ram.c | 2 +- system/globals.c | 7 ++++++- system/vl.c | 9 +++++++-- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/include/system/system.h b/include/system/system.h index 0cbb43ec30..dc7628357a 100644 --- a/include/system/system.h +++ b/include/system/system.h @@ -44,10 +44,18 @@ extern int display_opengl; extern const char *keyboard_layout; extern int old_param; extern uint8_t *boot_splash_filedata; -extern bool enable_mlock; extern bool enable_cpu_pm; extern QEMUClockType rtc_clock; +typedef enum { + MLOCK_OFF = 0, + MLOCK_ON, +} MlockState; + +bool should_mlock(MlockState); + +extern MlockState mlock_state; + #define MAX_OPTION_ROMS 16 typedef struct QEMUOptionRom { const char *name; diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index b1a003736b..7b140add76 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -991,7 +991,7 @@ static void virtio_mem_device_realize(DeviceState *dev, Error **errp) return; } - if (enable_mlock) { + if (should_mlock(mlock_state)) { error_setg(errp, "Incompatible with mlock"); return; } diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c index fc4d8a10df..04068ee039 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -651,7 +651,7 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis) mis->have_fault_thread = false; } - if (enable_mlock) { + if (should_mlock(mlock_state)) { if (os_mlock(false) < 0) { error_report("mlock: %s", strerror(errno)); /* diff --git a/system/globals.c b/system/globals.c index 4867c93ca6..adeff38348 100644 --- a/system/globals.c +++ b/system/globals.c @@ -31,10 +31,15 @@ #include "system/cpus.h" #include "system/system.h" +bool should_mlock(MlockState state) +{ + return state == MLOCK_ON; +} + enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB; int display_opengl; const char* keyboard_layout; -bool enable_mlock; +MlockState mlock_state; bool enable_cpu_pm; int autostart = 1; int vga_interface_type = VGA_NONE; diff --git a/system/vl.c b/system/vl.c index 72a40985f5..2895824c1a 100644 --- a/system/vl.c +++ b/system/vl.c @@ -796,7 +796,7 @@ static QemuOptsList qemu_run_with_opts = { static void realtime_init(void) { - if (enable_mlock) { + if (should_mlock(mlock_state)) { if (os_mlock(false) < 0) { error_report("locking memory failed"); exit(1); @@ -1878,13 +1878,18 @@ static void object_option_parse(const char *str) static void overcommit_parse(const char *str) { QemuOpts *opts; + bool enable_mlock; opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"), str, false); if (!opts) { exit(1); } - enable_mlock = qemu_opt_get_bool(opts, "mem-lock", enable_mlock); + + enable_mlock = qemu_opt_get_bool(opts, "mem-lock", + should_mlock(mlock_state)); + mlock_state = enable_mlock ? MLOCK_ON : MLOCK_OFF; + enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", enable_cpu_pm); } From patchwork Tue Feb 11 22:50:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970807 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CC7EDC0219B for ; Tue, 11 Feb 2025 22:52:24 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz6G-0005gL-HQ; Tue, 11 Feb 2025 17:51:28 -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 1thz6C-0005f4-IR for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz64-000548-6g for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314275; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=q7Z1xQZdmuz+frZCLJTmyASbiF2Y//N1ej3oUF9uqFc=; b=XwJviLEf/WCplq3U1VFtILrZsApnziW1vHm65WFtlK1F2f7X6VX1fLqzGQnOv/7No7+7cB r1XH+UKsxEL5iviGs9v2lkLeB0PadvFAZBymXl8acJZRhBDC4ISILAY/NP1L6XcniHbcBu NdOHffl/7eH4PRHXHKYQypL+gBiPGSw= Received: from mail-oo1-f70.google.com (mail-oo1-f70.google.com [209.85.161.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-452-Wo5kRHj5NeWbrtWNlMURDA-1; Tue, 11 Feb 2025 17:51:14 -0500 X-MC-Unique: Wo5kRHj5NeWbrtWNlMURDA-1 X-Mimecast-MFC-AGG-ID: Wo5kRHj5NeWbrtWNlMURDA Received: by mail-oo1-f70.google.com with SMTP id 006d021491bc7-5fc0058d68eso1672270eaf.0 for ; Tue, 11 Feb 2025 14:51:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314273; x=1739919073; 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=q7Z1xQZdmuz+frZCLJTmyASbiF2Y//N1ej3oUF9uqFc=; b=o8vaqjOCp5I53tPEK7N5jICrh5eiyoCsFYxF6erpFRlqZ7SjG8X09pzknrd5/ctLMU 7XzRMepMj0zZLBnOBA5YVPb6lo3RW3iFVBOGTrcFMD5Xz3BoYsCZwextGDdrdGhhlH1s k5pcJd/fMHKctNmzFb7vGZBWXK9HBH63jJvyueb0E56m8MDtyAEH91naDNGKx6e6EMzl +iFKL47D6+SaLD4p6p4qieAFOplXAyg5ZtwUp6WPYADqlYBE63txwGDVQ506KplIErgw F8w4mqFpUB84kds26aQsArCgXHpqOG8b/34XWKWmBG7AVQ1g8RvJSE6qg3m9nINu7NK6 BDDQ== X-Gm-Message-State: AOJu0YzLCPTmGp3hkoRgDQJhA43+9g52YWzKHPEZmg3uIkHZvmDDqJqU 6vYMUKKKStOOYxtuscLwQ62jDwZfFngWfsyctBEn9zY555B02P/4r0CLRVQCR6Al/fzKCe6qDdz qnJ9b26UGQoRnReb598ZEWaagnZtx/PwbWZ/CuU7wPKdjhRrjue843Yc50HhJCAzOCQY4I2+Ts9 A4mrQkQ5Ap7AQuhq9MA9dJzcI71H0YRS0lkA== X-Gm-Gg: ASbGnctxIrSch4KMysKSR6fQvLbxROupe3OXCtWra7VMoat2IsfF0gT0W5y12ltF6yh E/TIMyc3k8pSi6nDkx4Aqm/P1tZgPKY/93VzuPDBZQ2U4stVojXeKIMJeCr7UmCNMNLaSz9dbLr ju+q3y9A/rAmLvY7J5P/b4NS6MMNNnviFr+1vm5CTtV/4bkJ2S4a7IloqTU7TL7EOYD80zLtzOx sYoHKxHrHz6fsbr1Ftwl8ngEvglA7jYXekD8E5UYoP0gAiPfwMBYboB X-Received: by 2002:a05:6820:1b82:b0:5fa:67c5:5bdf with SMTP id 006d021491bc7-5fca167f789mr832729eaf.2.1739314272714; Tue, 11 Feb 2025 14:51:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IG4flg+A6h/N/Zm6BoFHKJZ7R6sQwmotZHIv+Q0KjwtkO/c+ESvfnYotD4b5Oz5kTEVgq7ZAQ== X-Received: by 2002:a05:6820:1b82:b0:5fa:67c5:5bdf with SMTP id 006d021491bc7-5fca167f789mr832710eaf.2.1739314272239; Tue, 11 Feb 2025 14:51:12 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:11 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand , Daniil Tatianin , Vladimir Sementsov-Ogievskiy Subject: [PULL 05/14] overcommit: introduce mem-lock=on-fault Date: Tue, 11 Feb 2025 17:50:49 -0500 Message-ID: <20250211225059.182533-6-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Daniil Tatianin Locking the memory without MCL_ONFAULT instantly prefaults any mmaped anonymous memory with a write-fault, which introduces a lot of extra overhead in terms of memory usage when all you want to do is to prevent kcompactd from migrating and compacting QEMU pages. Add an option to only lock pages lazily as they're faulted by the process by using MCL_ONFAULT if asked. Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Peter Xu Signed-off-by: Daniil Tatianin Link: https://lore.kernel.org/r/20250123131944.391886-5-d-tatianin@yandex-team.ru Signed-off-by: Peter Xu --- include/system/system.h | 2 ++ migration/postcopy-ram.c | 2 +- system/globals.c | 7 ++++++- system/vl.c | 34 +++++++++++++++++++++++++++------- qemu-options.hx | 14 +++++++++----- 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/include/system/system.h b/include/system/system.h index dc7628357a..a7effe7dfd 100644 --- a/include/system/system.h +++ b/include/system/system.h @@ -50,9 +50,11 @@ extern QEMUClockType rtc_clock; typedef enum { MLOCK_OFF = 0, MLOCK_ON, + MLOCK_ON_FAULT, } MlockState; bool should_mlock(MlockState); +bool is_mlock_on_fault(MlockState); extern MlockState mlock_state; diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c index 04068ee039..5d3edfcfec 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -652,7 +652,7 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis) } if (should_mlock(mlock_state)) { - if (os_mlock(false) < 0) { + if (os_mlock(is_mlock_on_fault(mlock_state)) < 0) { error_report("mlock: %s", strerror(errno)); /* * It doesn't feel right to fail at this point, we have a valid diff --git a/system/globals.c b/system/globals.c index adeff38348..316623bd20 100644 --- a/system/globals.c +++ b/system/globals.c @@ -33,7 +33,12 @@ bool should_mlock(MlockState state) { - return state == MLOCK_ON; + return state == MLOCK_ON || state == MLOCK_ON_FAULT; +} + +bool is_mlock_on_fault(MlockState state) +{ + return state == MLOCK_ON_FAULT; } enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB; diff --git a/system/vl.c b/system/vl.c index 2895824c1a..3c0fa2ff64 100644 --- a/system/vl.c +++ b/system/vl.c @@ -351,7 +351,7 @@ static QemuOptsList qemu_overcommit_opts = { .desc = { { .name = "mem-lock", - .type = QEMU_OPT_BOOL, + .type = QEMU_OPT_STRING, }, { .name = "cpu-pm", @@ -797,7 +797,7 @@ static QemuOptsList qemu_run_with_opts = { static void realtime_init(void) { if (should_mlock(mlock_state)) { - if (os_mlock(false) < 0) { + if (os_mlock(is_mlock_on_fault(mlock_state)) < 0) { error_report("locking memory failed"); exit(1); } @@ -1878,7 +1878,7 @@ static void object_option_parse(const char *str) static void overcommit_parse(const char *str) { QemuOpts *opts; - bool enable_mlock; + const char *mem_lock_opt; opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"), str, false); @@ -1886,11 +1886,31 @@ static void overcommit_parse(const char *str) exit(1); } - enable_mlock = qemu_opt_get_bool(opts, "mem-lock", - should_mlock(mlock_state)); - mlock_state = enable_mlock ? MLOCK_ON : MLOCK_OFF; - enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", enable_cpu_pm); + + mem_lock_opt = qemu_opt_get(opts, "mem-lock"); + if (!mem_lock_opt) { + return; + } + + if (strcmp(mem_lock_opt, "on") == 0) { + mlock_state = MLOCK_ON; + return; + } + + if (strcmp(mem_lock_opt, "off") == 0) { + mlock_state = MLOCK_OFF; + return; + } + + if (strcmp(mem_lock_opt, "on-fault") == 0) { + mlock_state = MLOCK_ON_FAULT; + return; + } + + error_report("parameter 'mem-lock' expects one of " + "'on', 'off', 'on-fault'"); + exit(1); } /* diff --git a/qemu-options.hx b/qemu-options.hx index 1b26ad53bd..61270e3206 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -4632,21 +4632,25 @@ SRST ERST DEF("overcommit", HAS_ARG, QEMU_OPTION_overcommit, - "-overcommit [mem-lock=on|off][cpu-pm=on|off]\n" + "-overcommit [mem-lock=on|off|on-fault][cpu-pm=on|off]\n" " run qemu with overcommit hints\n" - " mem-lock=on|off controls memory lock support (default: off)\n" + " mem-lock=on|off|on-fault controls memory lock support (default: off)\n" " cpu-pm=on|off controls cpu power management (default: off)\n", QEMU_ARCH_ALL) SRST -``-overcommit mem-lock=on|off`` +``-overcommit mem-lock=on|off|on-fault`` \ ``-overcommit cpu-pm=on|off`` Run qemu with hints about host resource overcommit. The default is to assume that host overcommits all resources. Locking qemu and guest memory can be enabled via ``mem-lock=on`` - (disabled by default). This works when host memory is not - overcommitted and reduces the worst-case latency for guest. + or ``mem-lock=on-fault`` (disabled by default). This works when + host memory is not overcommitted and reduces the worst-case latency for + guest. The on-fault option is better for reducing the memory footprint + since it makes allocations lazy, but the pages still get locked in place + once faulted by the guest or QEMU. Note that the two options are mutually + exclusive. Guest ability to manage power state of host cpus (increasing latency for other processes on the same host cpu, but decreasing latency for From patchwork Tue Feb 11 22:50:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970809 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F0533C0219B for ; Tue, 11 Feb 2025 22:53:03 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz6E-0005fn-C8; Tue, 11 Feb 2025 17:51:26 -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 1thz68-0005de-Gb for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:20 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz65-00054H-Jw for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314276; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JD3Ak85mvd0a6yhFU9zB0ucBwPuFg4Ck7y87CxK+Lj0=; b=FVbKoxOtGvbrqBX7Zz2w6vpwhwzppmosrdzjaKgKTQsz5j659mPEgERc+oLpozzGWycH6O rQR5W85aWhzBA3fnVYsg/WMZtvPBXHhJLTbzLVxpW+pyLfL/E2o7yEru0mU65y7WJqkedA 5jUQHNIJJ9I/ZwpisJALDHG62Yv6UrM= Received: from mail-oo1-f72.google.com (mail-oo1-f72.google.com [209.85.161.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-646-qUBDmdDaMBejNh-Hhn5JBQ-1; Tue, 11 Feb 2025 17:51:15 -0500 X-MC-Unique: qUBDmdDaMBejNh-Hhn5JBQ-1 X-Mimecast-MFC-AGG-ID: qUBDmdDaMBejNh-Hhn5JBQ Received: by mail-oo1-f72.google.com with SMTP id 006d021491bc7-5fc476501cdso4561985eaf.3 for ; Tue, 11 Feb 2025 14:51:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314274; x=1739919074; 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=JD3Ak85mvd0a6yhFU9zB0ucBwPuFg4Ck7y87CxK+Lj0=; b=q5atD+SVtQAMY87YqdyZl3PDCMUF4SEivOQXxy078YVdEOxtrMZFjuVuOcMwxBScWt YmbGrhYBavYIrhq4KRlT5bl6q50cnYJMTt4p90X4CfM2xPKp+Y5uxNq/0eWEzPfkSJKJ IeKmH91aNb/BAnrHdzraTVcnnP8Rf2mrcuA8Ww6uEvoACTaVZya7YJCZ7NrR//3AuZ0c IYAwPEkJFOdXEyMlEzZWEv5O8fzjv/7o0r+AktGSGS6aGMfpuVDUFUgTdHnTOJTeB2MR thWwQq/iw8jtXTuUnF/PvkGmMD2i6BmWh9cFmORD09seLkmpC/hRe+DqbKMQdJ6Apmzn XBzA== X-Gm-Message-State: AOJu0YxMGODK/Of5+sMqr9E5v0r49Q15qWwrXcHQt+V25FlOPPMObPeJ eRcf0kLfWoBj+NH7IYGbhXRoYG+ZtJYZZK5ODjChAwfJNv/dCuTlzab8WIA0mi+6DOegcajz/yc mWBhHpbtwyu/WQ8P2YWiRS4tCCXwk07SCSFI/KYARqVZVigQg/MbdQa1/Y22vAC/wJa40ZUm9+W R6s9Wc1BDlQZjp0WkJGGY4MyoEQJxKnNGUhw== X-Gm-Gg: ASbGncvoW2cRbpC8zt93H0g0bLDVkHgHldTSiM7xr8/ofylJ7fKMRvkz3vHWO0m/ZDJ P0dLT7MHygLpPly6ZCf+CG/YycHvWWjUO7tDUxYqSjduhQQ2k1py+dzs+9wHwlo0D8cZOSV/mVI xGtRbOHBczLoOHagljVC8OxbqYAoAbI3RAk0d1RJqtGGZ5UJGUGpD5m+9EGlqUhdhd/10SvNyam HQFCln7CGMQd5Y2XEUjY+KQYOiRpk0RbFlJ4buuOfME9bgMVN1T90qg X-Received: by 2002:a05:6820:8cc:b0:5fc:9ebf:25bd with SMTP id 006d021491bc7-5fca18aa32fmr886072eaf.6.1739314274240; Tue, 11 Feb 2025 14:51:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IEIsQSmH/yPL+mdo9tBvbsCo9e9Z61CRjKpePdLmJi3t/txBdBQJvieabXSs4HYzm2LwBZpTQ== X-Received: by 2002:a05:6820:8cc:b0:5fc:9ebf:25bd with SMTP id 006d021491bc7-5fca18aa32fmr886056eaf.6.1739314273793; Tue, 11 Feb 2025 14:51:13 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:12 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand Subject: [PULL 06/14] physmem: factor out memory_region_is_ram_device() check in memory_access_is_direct() Date: Tue, 11 Feb 2025 17:50:50 -0500 Message-ID: <20250211225059.182533-7-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: David Hildenbrand As documented in commit 4a2e242bbb306 ("memory: Don't use memcpy for ram_device regions"), we disallow direct access to RAM DEVICE regions. Let's make this clearer to prepare for further changes. Note that romd regions will never be RAM DEVICE at the same time. Reviewed-by: Peter Xu Signed-off-by: David Hildenbrand Link: https://lore.kernel.org/r/20250210084648.33798-2-david@redhat.com Signed-off-by: Peter Xu --- include/exec/memory.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 9f73b59867..5cd7574c60 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -2997,12 +2997,19 @@ bool prepare_mmio_access(MemoryRegion *mr); static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) { + /* + * RAM DEVICE regions can be accessed directly using memcpy, but it might + * be MMIO and access using mempy can be wrong (e.g., using instructions not + * intended for MMIO access). So we treat this as IO. + */ + if (memory_region_is_ram_device(mr)) { + return false; + } if (is_write) { return memory_region_is_ram(mr) && !mr->readonly && - !mr->rom_device && !memory_region_is_ram_device(mr); + !mr->rom_device; } else { - return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) || - memory_region_is_romd(mr); + return memory_region_is_ram(mr) || memory_region_is_romd(mr); } } From patchwork Tue Feb 11 22:50:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970812 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1724FC0219B for ; Tue, 11 Feb 2025 22:53:17 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz6E-0005fg-A2; Tue, 11 Feb 2025 17:51:26 -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 1thz6A-0005eX-JF for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:22 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz68-00054b-A9 for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314278; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DJeNFp1k0A/JbpmAa90NjLlwhynPzaZBj2J+1fW9xPg=; b=SYsS5IvgQ+4CuUFIhv3thJkMK4kxx75XsIZElXo8ztyFUXuRDe7pEd/MG1cT7pcFixp1IN TGC4sIQoAyaAwY2BZe8ZdEh/1P8C1nE6hF9yV9kxtCv9j1LyC7sz7pE2WywHykMuDjXadq do9+x8hrf8AxWAQHX0sH9Vmxe489ZV0= Received: from mail-ot1-f69.google.com (mail-ot1-f69.google.com [209.85.210.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-247-1wRnVfYHM0CY0ht3xs6hjQ-1; Tue, 11 Feb 2025 17:51:17 -0500 X-MC-Unique: 1wRnVfYHM0CY0ht3xs6hjQ-1 X-Mimecast-MFC-AGG-ID: 1wRnVfYHM0CY0ht3xs6hjQ Received: by mail-ot1-f69.google.com with SMTP id 46e09a7af769-726e878ddefso1217689a34.3 for ; Tue, 11 Feb 2025 14:51:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314276; x=1739919076; 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=DJeNFp1k0A/JbpmAa90NjLlwhynPzaZBj2J+1fW9xPg=; b=iw2zGZjy5gIhSPQ5TJd8BMoN5omjSPthJzfZ2vMAXkMB4wOuJkgYgihsUtpZyvPA0u bhHLngqKowYhoHsMyme9+aWz8ZRRlE5+//0b4oPc3O0J5e9nDbV5/tR8miKSwbIg/Mk+ ner2J2MRivfShHOi8xhB6WDUNShef9bx/0TXbW8nqWoXbQM8x1yHTBFOQL0QWfEGRYbi BLuFVd7MBs6DZ3eNSyF1URD8yPzItBuQB1APOW2X8gn9wn1fc7CbhE25xvxohLVI+e8w 6v/BKVTpj1yD6Gq/1FwEQnIaUc4Cpal6rIpoBv3a4dSclZvNa15I02xLEJejje+sFSAN EODA== X-Gm-Message-State: AOJu0Yz1cIg0XyQs2s6XwbapK4edkpfmETNhadDx58OHFCaEoa36EOHm V4sl9lVpM45p/zuZFXPyyHpNroVtbw+1LfL4keNCbBbBP1l8XOScYnvwnUDNHhctjW76PXL9LL6 HUPpqnC+kOK8GIHTbGSE1x1mK7w3bdx1Cauhz8JRZNmxTSwAlcWC2pUBGCHsBo/L0Hb1YmKvS4d vEvwnHDfPvHm7cgKjah8HCQE5FCmRvebAQ0g== X-Gm-Gg: ASbGnctJ6ZCpnc0Thn9vFi1JXeL6ZX/jIS4r3bp5fVdhliHzD7QPoAekBJmOoqPyhsy Kgcz8lwWkdTFqzutZOUxWmTBc5z2MhbMz8L1NCQIUasGuNvZ7GCZ1IekZwqERSEfmm+TibYGQvH owvpu3JhtUtzw4ELk3igeVn7i73eiFA1MfWeHV6gMSsY7VYzf9oh154uWR5tKLIPCAu7jyDlqNt Hdoxwh7/5/MsUrigQ9N7RTtD5rsIOVuh1++nybO7pSEuLyQ3OESGo9q X-Received: by 2002:a05:6830:3786:b0:726:ec20:7387 with SMTP id 46e09a7af769-726f1d327b5mr808704a34.27.1739314275801; Tue, 11 Feb 2025 14:51:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGpQgzeu/m/SbCybHDgXYSf9pcSXJ8HGGYi0pyCJm1KUrvJrgdZ7EmcdX3fKcLLI8KvnD9VQg== X-Received: by 2002:a05:6830:3786:b0:726:ec20:7387 with SMTP id 46e09a7af769-726f1d327b5mr808688a34.27.1739314275431; Tue, 11 Feb 2025 14:51:15 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:14 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand Subject: [PULL 07/14] physmem: factor out RAM/ROMD check in memory_access_is_direct() Date: Tue, 11 Feb 2025 17:50:51 -0500 Message-ID: <20250211225059.182533-8-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: David Hildenbrand Let's factor more of the generic "is this directly accessible" check, independent of the "write" condition out. Note that the "!mr->rom_device" check in the write case essentially disallows the memory_region_is_romd() condition again. Further note that RAM DEVICE regions are also RAM regions, so we can check for RAM+ROMD first. This is a preparation for further changes. Reviewed-by: Peter Xu Signed-off-by: David Hildenbrand Link: https://lore.kernel.org/r/20250210084648.33798-3-david@redhat.com Signed-off-by: Peter Xu --- include/exec/memory.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 5cd7574c60..cb35c38402 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -2997,6 +2997,10 @@ bool prepare_mmio_access(MemoryRegion *mr); static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) { + /* ROM DEVICE regions only allow direct access if in ROMD mode. */ + if (!memory_region_is_ram(mr) && !memory_region_is_romd(mr)) { + return false; + } /* * RAM DEVICE regions can be accessed directly using memcpy, but it might * be MMIO and access using mempy can be wrong (e.g., using instructions not @@ -3006,11 +3010,9 @@ static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) return false; } if (is_write) { - return memory_region_is_ram(mr) && !mr->readonly && - !mr->rom_device; - } else { - return memory_region_is_ram(mr) || memory_region_is_romd(mr); + return !mr->readonly && !mr->rom_device; } + return true; } /** From patchwork Tue Feb 11 22:50:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970811 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 04CAFC0219B for ; Tue, 11 Feb 2025 22:53:09 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz6J-0005hx-HI; Tue, 11 Feb 2025 17:51:31 -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 1thz6C-0005f2-Ij for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz6A-000556-Br for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314281; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=h+h7PqMkjUYUL06lu6YiwfavWSql50PnNNQ3ntizUZY=; b=YkkxwqplHqNkSrxENlgHLYQf0AijefZapm1cUG10be6CqlaN9PwjuiPhRFpNu8/Jtx+M2A mh0Y+UChHOG4DUkvgscjS+LOcj4NQ182v1MK6lwhgEe8MNBZ8Sfu+RSxYB6gyvn1X7Pl3H FRLSBNVaC8hK9G79nidffTcUI4A7MpY= Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-64-4Em0lLJhM5GEe8RiejS8eg-1; Tue, 11 Feb 2025 17:51:19 -0500 X-MC-Unique: 4Em0lLJhM5GEe8RiejS8eg-1 X-Mimecast-MFC-AGG-ID: 4Em0lLJhM5GEe8RiejS8eg_1739314278 Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-724d17262dfso712939a34.1 for ; Tue, 11 Feb 2025 14:51:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314277; x=1739919077; 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=h+h7PqMkjUYUL06lu6YiwfavWSql50PnNNQ3ntizUZY=; b=TLgDtkzdcACbkns08fJpN+l3trr/oGhGV2jmFLh4xO1w9U8XyljcDn/AYmiUn1e8Tm PaKaBJb/Vg3h6axlY/UJPBaozJrN08c811GTY9Aqcg/9CW/+gV/KBn5HYMOZqRRCSagB n14C+qwr7QoTfQOiU3isuGRmDdXO+c8rS+g49uVIFYFbKJwA0Sn4XXF2BA5otuSscnI7 0GITNjq+OHU7pPU6o0cTiPlOLA7U0XhVdfTMJhKVllaXiOOsTmbRmlN8kk6DkukoEc2y jaqhIvNauhDlp1sA7Hs9EV00dZ4SMU+2DrElzYxGl1UP86+rgLV03dy4/wxvQIogB2Py 3N7Q== X-Gm-Message-State: AOJu0Yyc+lRwfAOJoyQcexQyWYYiAwFeHcZ5IZlJiXfdSw2EtPKSy8C2 5y9SU18rcWMWDMBab2/IjOzMgGzMrGKIzj/z6zILOO/W9mCsLzGq22ShLlaX3Zterdrvs++GlAr D8tVePeL+k0xXgGDisa/JTkWl04Kd7UCGpTi8s5dPTiUL5NDJwUT01KIsqVBT4+G+NonCX8dFee G/keHYz8qUw6H4s2VCSMIjDGk21/8pBfaeZg== X-Gm-Gg: ASbGncsN4tha5wD5qeOirInd7wKZxRlrRiogSRSC8PXjxnIdYwRVauwyGAXSb658Q3t A8rQI2+SDfouCr3QP10sAvvZJoRyrK1tG/gPqPTaeB2Bg5Zmm9KLtsFzSw67wYyoKAxsZ6G4naV lZmxpSAFHpNOeaMslTX+M0KGBlBXovW7fejXMNvP5zWAbrzDULGrhbgqXQhvPS/1I0IjL59zyK9 5dswRpdEwgqFDcEA+a/NBIBCJjkG3axCmpQpsOjhdRK+P814ig6hk7X X-Received: by 2002:a05:6830:6dc3:b0:71d:5336:df80 with SMTP id 46e09a7af769-726f1d158eamr924602a34.20.1739314277762; Tue, 11 Feb 2025 14:51:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IGV+XBIrrNnTI9PUV3PLUrTZUR8P5pfM1YUE3TLx9Sr7ATKcIDOMXugWNd3Xo3fMfXqzMyt7g== X-Received: by 2002:a05:6830:6dc3:b0:71d:5336:df80 with SMTP id 46e09a7af769-726f1d158eamr924582a34.20.1739314277308; Tue, 11 Feb 2025 14:51:17 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:16 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand Subject: [PULL 08/14] physmem: factor out direct access check into memory_region_supports_direct_access() Date: Tue, 11 Feb 2025 17:50:52 -0500 Message-ID: <20250211225059.182533-9-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: David Hildenbrand Let's factor the complete "directly accessible" check independent of the "write" condition out so we can reuse it next. We can now split up the checks RAM and ROMD check, so we really only check for RAM DEVICE in case of RAM -- ROM DEVICE is neither RAM not RAM DEVICE. Reviewed-by: Peter Xu Signed-off-by: David Hildenbrand Link: https://lore.kernel.org/r/20250210084648.33798-4-david@redhat.com Signed-off-by: Peter Xu --- include/exec/memory.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index cb35c38402..4e2cf95ab6 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -2995,10 +2995,13 @@ MemTxResult address_space_write_cached_slow(MemoryRegionCache *cache, int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr); bool prepare_mmio_access(MemoryRegion *mr); -static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) +static inline bool memory_region_supports_direct_access(MemoryRegion *mr) { /* ROM DEVICE regions only allow direct access if in ROMD mode. */ - if (!memory_region_is_ram(mr) && !memory_region_is_romd(mr)) { + if (memory_region_is_romd(mr)) { + return true; + } + if (!memory_region_is_ram(mr)) { return false; } /* @@ -3006,7 +3009,12 @@ static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) * be MMIO and access using mempy can be wrong (e.g., using instructions not * intended for MMIO access). So we treat this as IO. */ - if (memory_region_is_ram_device(mr)) { + return !memory_region_is_ram_device(mr); +} + +static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) +{ + if (!memory_region_supports_direct_access(mr)) { return false; } if (is_write) { From patchwork Tue Feb 11 22:50:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970805 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5675DC0219B for ; Tue, 11 Feb 2025 22:52:03 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz6H-0005gy-N0; Tue, 11 Feb 2025 17:51:30 -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 1thz6C-0005f3-Io for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz6A-000558-ES for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314281; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eeNEejx0gSgxbc7SXhQ12rr1X3D1jH5EIOgc3BWNRhY=; b=UdwRJUWJ8YQCl55pLJDspTxZGT5J1XXDS2zY/dZlzHWpOPtjqWWAXLW0d3smqFx1GnF1z2 pm26QRs8AUJfMZMOUIqcziy2YoMBOLcZlvaqbSPk3lDWNnkKI3QHbY/cWXJxn+Qnl5H0Ia VpWHGUWwZ4QK0/vCk8YvJSHxxEEiVBQ= Received: from mail-oo1-f71.google.com (mail-oo1-f71.google.com [209.85.161.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-302-SkavUxZcMn6hX0m4uuANiA-1; Tue, 11 Feb 2025 17:51:20 -0500 X-MC-Unique: SkavUxZcMn6hX0m4uuANiA-1 X-Mimecast-MFC-AGG-ID: SkavUxZcMn6hX0m4uuANiA Received: by mail-oo1-f71.google.com with SMTP id 006d021491bc7-5f8d5e499a5so4254096eaf.2 for ; Tue, 11 Feb 2025 14:51:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314279; x=1739919079; 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=eeNEejx0gSgxbc7SXhQ12rr1X3D1jH5EIOgc3BWNRhY=; b=PsbH3wgtslWbrdQvo8PfvkAWj/Xij/BTzfq53ZPCCOqlQ3BH5WHBsKnW81o2FI/DJa 7md0zwXLEOxZwTmHHuYXhNadqNZGzJyauDeZIk141Ccs62z+ezlTnlxFH9f26DNK+18m zFMKV6TtDTSKXgT+njXwKgYCa3tdWW8T2mm/w5RWej9i0kKlmIHopZDpciDlDl/s3Gzy 1fy1deRCCtuALkWOcNS2kxvwYA/xmhEmFUnHxpiITS3X3YZuOTZ90gwPZ7CQ4NdITFZC dQmZEJG1R49ksczbjLnB+Vs8UJB63VhtmrnXvsdhrW1sF2IjD+kroB2ZMxTAJTuaASuQ lKCQ== X-Gm-Message-State: AOJu0YyJSTDnJy9YskNIcUBC+VVRYKbWKmjK3KON+GL/cH3J8VCmYwRm MalPSsC7yXDfNHZiAxtUPeCfoNacJk8CjfMZOIhszdLo1LSakPb9LtBuiopZGGN/xE/CyLZ3XjG 46kMUbZTWGI6QAGE6J/vbuncRcjnIhoo0Vko3TKDVJSIjQhuScekyHFK2WxCj99QIplyHvlhX75 I88yHtvykBGcRdWTtsJBgQEZUMpVxheiX8wA== X-Gm-Gg: ASbGncv2tRyoLqL2qotbQ9EXk3jaFpSuTmJO+kI8oKmAs+G1O7ukm9420zLBm56UQBu ntXEC+BoxhtGT8XKuRbfS/T16l7hH90i9Irh2woJRFqvvZYlkVMu3PeCrdIGzhLghgt6+kXevNf lC/7fNGq86Z1nzw4aQh6DTDYfRELDqjT4jWbTPDWR0hQwAGv3bgRLWEJnjhml2QA5+8Eh0y3vsL c4XxhHoofLtQRFkbpiCshf++J9XO0IhLBAGWye4tMwyNgWOaYw0KcLa X-Received: by 2002:a05:6820:2904:b0:5f6:4ce2:fa4f with SMTP id 006d021491bc7-5fca186c700mr930336eaf.4.1739314279544; Tue, 11 Feb 2025 14:51:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IHi8QtzMQUmUreGQIUGxqIu0XEAI6SUojRpLlYv4wbLPbRgKM2Ie6dc8c8OIUfn2sWWNeLxuQ== X-Received: by 2002:a05:6820:2904:b0:5f6:4ce2:fa4f with SMTP id 006d021491bc7-5fca186c700mr930325eaf.4.1739314279170; Tue, 11 Feb 2025 14:51:19 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:18 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand , Alex Williamson Subject: [PULL 09/14] physmem: disallow direct access to RAM DEVICE in address_space_write_rom() Date: Tue, 11 Feb 2025 17:50:53 -0500 Message-ID: <20250211225059.182533-10-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: David Hildenbrand As documented in commit 4a2e242bbb306 ("memory: Don't use memcpy for ram_device regions"), we disallow direct access to RAM DEVICE regions. This change implies that address_space_write_rom() and cpu_memory_rw_debug() won't be able to write to RAM DEVICE regions. It will also affect cpu_flush_icache_range(), but it's only used by hw/core/loader.c after writing to ROM, so it is expected to not apply here with RAM DEVICE. This fixes direct access to these regions where we don't want direct access. We'll extend cpu_memory_rw_debug() next to also be able to write to these (and IO) regions. This is a preparation for further changes. Cc: Alex Williamson Reviewed-by: Peter Xu Signed-off-by: David Hildenbrand Link: https://lore.kernel.org/r/20250210084648.33798-5-david@redhat.com Signed-off-by: Peter Xu --- system/physmem.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/system/physmem.c b/system/physmem.c index 235015f3ea..cff15ca1df 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -3137,8 +3137,7 @@ static inline MemTxResult address_space_write_rom_internal(AddressSpace *as, l = len; mr = address_space_translate(as, addr, &addr1, &l, true, attrs); - if (!(memory_region_is_ram(mr) || - memory_region_is_romd(mr))) { + if (!memory_region_supports_direct_access(mr)) { l = memory_access_size(mr, l, addr1); } else { /* ROM/RAM case */ From patchwork Tue Feb 11 22:50:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970810 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1B912C021A0 for ; Tue, 11 Feb 2025 22:53:04 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz6I-0005hN-NQ; Tue, 11 Feb 2025 17:51:30 -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 1thz6G-0005gF-1D for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:28 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz6D-000564-SE for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314285; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PLWlmmzb89GolrhpSAzIBMC01IwngDNc8Jt0Hcx2LH0=; b=ZNtZzQtL8SLKZ9MPgJrn6pmJg9TUhMIb5sByq/vFXZXn/dGdDCiIL7ah5DD1nkk3f21z4Q GkgfeoE4+D37Vz0uh4qYioc4hWRzSGBUQFH4tbtF+83DlXOnTJ0+kJA94gJWzAjeriXtHM bBYg4AnlCfUwZwKh+kv8pT/8lH8+UdM= Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-235-ybGuAkzpMxmC8X1iDtB1gg-1; Tue, 11 Feb 2025 17:51:22 -0500 X-MC-Unique: ybGuAkzpMxmC8X1iDtB1gg-1 X-Mimecast-MFC-AGG-ID: ybGuAkzpMxmC8X1iDtB1gg Received: by mail-oi1-f198.google.com with SMTP id 5614622812f47-3f3b93f4810so2086657b6e.3 for ; Tue, 11 Feb 2025 14:51:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314281; x=1739919081; 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=PLWlmmzb89GolrhpSAzIBMC01IwngDNc8Jt0Hcx2LH0=; b=r+CK27H4PWS3WOWTkvGi69g7Ava/9r0ZsiiyLds733gxNbVGClEfk7c7DQ22s/QTYg AMO2La8hA9onOSbxm7PgOf1By6LbSjqjaPs8Ty/W6oeop7o6qcdOKK7DTSN2BYnBrsbW 7CCGzsvgfC28nhM0NiFfBOGIMaNvwe2kjfQRPVHZbClsRH7ED+Ps8P3XV+yGMN6gMh3b whxtwz+/lFa3dUPq9Sw3HuL2zPN+Q0XtoaeD8iw2EQxG1klwO2By1XEB+Wkk47EsrH01 yRi9FZCIzd7nLXDkm3MRMiy/waQiG3nPqsXgBA+Me60dj4zrd6PPGbuiyDyo9/6enq6F 5ZqA== X-Gm-Message-State: AOJu0Yxr+f/RhltWZQpDB6NALyTb7mP++ZwTVdBUmpVRyguH3x+eQwN/ qhDxmcr2z94/mccQzyJuz7EAJCxnpWA2ejaeWaXffVGOIct9HQ7SQ7Fa8aoX41x5SpWjIKQsbh7 R3pA7sJP9AYtyGhjKs4GtPgI1yhPhEQkYJ/eICzNghwIFAQLfBACsz8GuN+ir3aRvTLTwW7jvCx yB87QG0m45/in5arxDOqZTNC3uYmSbTBqSRg== X-Gm-Gg: ASbGncuq46mDFt9r8zttMrbbu/YretDeLcWqyUySKkiSUrLKSDnPy10JKD3niOeSUDm w9GcJziFDT13x6S30xlGDcvffzZ1JFhhMMSgPF8Kdm2F63VC1A5dcMpO9WDWmvFib3tU75+94r2 njgAMTTGF1HRe4jutJac3alafByuvpsUC1vxYYzT+/Hvqh6MLRC0KF/Jio8wAc3/wmMSqtSqp8l zknDz+eRaAD3tq7tLIrKknE73yAjeTZOqK1RM7JTFCIqm+O6njEpM0u X-Received: by 2002:a05:6808:17a9:b0:3f3:cb68:a90a with SMTP id 5614622812f47-3f3cd3f1451mr1225966b6e.0.1739314281240; Tue, 11 Feb 2025 14:51:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IH0nKLedXfFlHw6W3fdTt3YLXfhItv4b6cfCx1+zV4lK4nTiJINHUfzaf++Ytw+V9jpPB9lNQ== X-Received: by 2002:a05:6808:17a9:b0:3f3:cb68:a90a with SMTP id 5614622812f47-3f3cd3f1451mr1225955b6e.0.1739314280951; Tue, 11 Feb 2025 14:51:20 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:20 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PULL 10/14] memory: pass MemTxAttrs to memory_access_is_direct() Date: Tue, 11 Feb 2025 17:50:54 -0500 Message-ID: <20250211225059.182533-11-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: David Hildenbrand We want to pass another flag that will be stored in MemTxAttrs. So pass MemTxAttrs directly. Reviewed-by: Peter Xu Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: David Hildenbrand Link: https://lore.kernel.org/r/20250210084648.33798-6-david@redhat.com Signed-off-by: Peter Xu --- include/exec/memory.h | 5 +++-- hw/core/loader.c | 2 +- hw/remote/vfio-user-obj.c | 2 +- system/physmem.c | 12 ++++++------ system/memory_ldst.c.inc | 18 +++++++++--------- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 4e2cf95ab6..b18ecf933e 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -3012,7 +3012,8 @@ static inline bool memory_region_supports_direct_access(MemoryRegion *mr) return !memory_region_is_ram_device(mr); } -static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) +static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write, + MemTxAttrs attrs) { if (!memory_region_supports_direct_access(mr)) { return false; @@ -3053,7 +3054,7 @@ MemTxResult address_space_read(AddressSpace *as, hwaddr addr, fv = address_space_to_flatview(as); l = len; mr = flatview_translate(fv, addr, &addr1, &l, false, attrs); - if (len == l && memory_access_is_direct(mr, false)) { + if (len == l && memory_access_is_direct(mr, false, attrs)) { ptr = qemu_map_ram_ptr(mr->ram_block, addr1); memcpy(buf, ptr, len); } else { diff --git a/hw/core/loader.c b/hw/core/loader.c index fd25c5e01b..332b879a0b 100644 --- a/hw/core/loader.c +++ b/hw/core/loader.c @@ -144,7 +144,7 @@ ssize_t load_image_mr(const char *filename, MemoryRegion *mr) { ssize_t size; - if (!memory_access_is_direct(mr, false)) { + if (!memory_access_is_direct(mr, false, MEMTXATTRS_UNSPECIFIED)) { /* Can only load an image into RAM or ROM */ return -1; } diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c index 9e5ff6d87a..6e51a92856 100644 --- a/hw/remote/vfio-user-obj.c +++ b/hw/remote/vfio-user-obj.c @@ -358,7 +358,7 @@ static int vfu_object_mr_rw(MemoryRegion *mr, uint8_t *buf, hwaddr offset, int access_size; uint64_t val; - if (memory_access_is_direct(mr, is_write)) { + if (memory_access_is_direct(mr, is_write, MEMTXATTRS_UNSPECIFIED)) { /** * Some devices expose a PCI expansion ROM, which could be buffer * based as compared to other regions which are primarily based on diff --git a/system/physmem.c b/system/physmem.c index cff15ca1df..8745c10c9d 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -573,7 +573,7 @@ MemoryRegion *flatview_translate(FlatView *fv, hwaddr addr, hwaddr *xlat, is_write, true, &as, attrs); mr = section.mr; - if (xen_enabled() && memory_access_is_direct(mr, is_write)) { + if (xen_enabled() && memory_access_is_direct(mr, is_write, attrs)) { hwaddr page = ((addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE) - addr; *plen = MIN(page, *plen); } @@ -2869,7 +2869,7 @@ static MemTxResult flatview_write_continue_step(MemTxAttrs attrs, return MEMTX_ACCESS_ERROR; } - if (!memory_access_is_direct(mr, true)) { + if (!memory_access_is_direct(mr, true, attrs)) { uint64_t val; MemTxResult result; bool release_lock = prepare_mmio_access(mr); @@ -2965,7 +2965,7 @@ static MemTxResult flatview_read_continue_step(MemTxAttrs attrs, uint8_t *buf, return MEMTX_ACCESS_ERROR; } - if (!memory_access_is_direct(mr, false)) { + if (!memory_access_is_direct(mr, false, attrs)) { /* I/O case */ uint64_t val; MemTxResult result; @@ -3274,7 +3274,7 @@ static bool flatview_access_valid(FlatView *fv, hwaddr addr, hwaddr len, while (len > 0) { l = len; mr = flatview_translate(fv, addr, &xlat, &l, is_write, attrs); - if (!memory_access_is_direct(mr, is_write)) { + if (!memory_access_is_direct(mr, is_write, attrs)) { l = memory_access_size(mr, l, addr); if (!memory_region_access_valid(mr, xlat, l, is_write, attrs)) { return false; @@ -3354,7 +3354,7 @@ void *address_space_map(AddressSpace *as, fv = address_space_to_flatview(as); mr = flatview_translate(fv, addr, &xlat, &l, is_write, attrs); - if (!memory_access_is_direct(mr, is_write)) { + if (!memory_access_is_direct(mr, is_write, attrs)) { size_t used = qatomic_read(&as->bounce_buffer_size); for (;;) { hwaddr alloc = MIN(as->max_bounce_buffer_size - used, l); @@ -3487,7 +3487,7 @@ int64_t address_space_cache_init(MemoryRegionCache *cache, mr = cache->mrs.mr; memory_region_ref(mr); - if (memory_access_is_direct(mr, is_write)) { + if (memory_access_is_direct(mr, is_write, MEMTXATTRS_UNSPECIFIED)) { /* We don't care about the memory attributes here as we're only * doing this if we found actual RAM, which behaves the same * regardless of attributes; so UNSPECIFIED is fine. diff --git a/system/memory_ldst.c.inc b/system/memory_ldst.c.inc index 0e6f3940a9..7f32d3d9ff 100644 --- a/system/memory_ldst.c.inc +++ b/system/memory_ldst.c.inc @@ -34,7 +34,7 @@ static inline uint32_t glue(address_space_ldl_internal, SUFFIX)(ARG1_DECL, RCU_READ_LOCK(); mr = TRANSLATE(addr, &addr1, &l, false, attrs); - if (l < 4 || !memory_access_is_direct(mr, false)) { + if (l < 4 || !memory_access_is_direct(mr, false, attrs)) { release_lock |= prepare_mmio_access(mr); /* I/O case */ @@ -103,7 +103,7 @@ static inline uint64_t glue(address_space_ldq_internal, SUFFIX)(ARG1_DECL, RCU_READ_LOCK(); mr = TRANSLATE(addr, &addr1, &l, false, attrs); - if (l < 8 || !memory_access_is_direct(mr, false)) { + if (l < 8 || !memory_access_is_direct(mr, false, attrs)) { release_lock |= prepare_mmio_access(mr); /* I/O case */ @@ -170,7 +170,7 @@ uint8_t glue(address_space_ldub, SUFFIX)(ARG1_DECL, RCU_READ_LOCK(); mr = TRANSLATE(addr, &addr1, &l, false, attrs); - if (!memory_access_is_direct(mr, false)) { + if (!memory_access_is_direct(mr, false, attrs)) { release_lock |= prepare_mmio_access(mr); /* I/O case */ @@ -207,7 +207,7 @@ static inline uint16_t glue(address_space_lduw_internal, SUFFIX)(ARG1_DECL, RCU_READ_LOCK(); mr = TRANSLATE(addr, &addr1, &l, false, attrs); - if (l < 2 || !memory_access_is_direct(mr, false)) { + if (l < 2 || !memory_access_is_direct(mr, false, attrs)) { release_lock |= prepare_mmio_access(mr); /* I/O case */ @@ -277,7 +277,7 @@ void glue(address_space_stl_notdirty, SUFFIX)(ARG1_DECL, RCU_READ_LOCK(); mr = TRANSLATE(addr, &addr1, &l, true, attrs); - if (l < 4 || !memory_access_is_direct(mr, true)) { + if (l < 4 || !memory_access_is_direct(mr, true, attrs)) { release_lock |= prepare_mmio_access(mr); r = memory_region_dispatch_write(mr, addr1, val, MO_32, attrs); @@ -314,7 +314,7 @@ static inline void glue(address_space_stl_internal, SUFFIX)(ARG1_DECL, RCU_READ_LOCK(); mr = TRANSLATE(addr, &addr1, &l, true, attrs); - if (l < 4 || !memory_access_is_direct(mr, true)) { + if (l < 4 || !memory_access_is_direct(mr, true, attrs)) { release_lock |= prepare_mmio_access(mr); r = memory_region_dispatch_write(mr, addr1, val, MO_32 | devend_memop(endian), attrs); @@ -377,7 +377,7 @@ void glue(address_space_stb, SUFFIX)(ARG1_DECL, RCU_READ_LOCK(); mr = TRANSLATE(addr, &addr1, &l, true, attrs); - if (!memory_access_is_direct(mr, true)) { + if (!memory_access_is_direct(mr, true, attrs)) { release_lock |= prepare_mmio_access(mr); r = memory_region_dispatch_write(mr, addr1, val, MO_8, attrs); } else { @@ -410,7 +410,7 @@ static inline void glue(address_space_stw_internal, SUFFIX)(ARG1_DECL, RCU_READ_LOCK(); mr = TRANSLATE(addr, &addr1, &l, true, attrs); - if (l < 2 || !memory_access_is_direct(mr, true)) { + if (l < 2 || !memory_access_is_direct(mr, true, attrs)) { release_lock |= prepare_mmio_access(mr); r = memory_region_dispatch_write(mr, addr1, val, MO_16 | devend_memop(endian), attrs); @@ -474,7 +474,7 @@ static void glue(address_space_stq_internal, SUFFIX)(ARG1_DECL, RCU_READ_LOCK(); mr = TRANSLATE(addr, &addr1, &l, true, attrs); - if (l < 8 || !memory_access_is_direct(mr, true)) { + if (l < 8 || !memory_access_is_direct(mr, true, attrs)) { release_lock |= prepare_mmio_access(mr); r = memory_region_dispatch_write(mr, addr1, val, MO_64 | devend_memop(endian), attrs); From patchwork Tue Feb 11 22:50:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970815 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 12CF0C021A0 for ; Tue, 11 Feb 2025 22:53:38 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz6K-0005iq-OA; Tue, 11 Feb 2025 17:51:32 -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 1thz6G-0005gH-Eu for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:28 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz6E-000566-2E for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314285; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ML7ApDFrpE6LdiaKUIk8tOpXVHTwr8mMl/oUpLjQuwc=; b=h31ymj82qf/0iLuLg4fcmwavHFZpVt7+ag9eezqLFh1AugolB3X9UkxffWj0wt6NrZtbAn WvBMJBrXnOXPB+DrDyxIr3on+qVM0kBR9mlRQRLSYg6k42ouy+RuaZFFnwqQ9l2wf8RkLR 2AiO1eertjSTMfILt37l6O3VmGDvY5M= Received: from mail-ot1-f72.google.com (mail-ot1-f72.google.com [209.85.210.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-591-S5ASt57XOhOVO6RL0Hv4tg-1; Tue, 11 Feb 2025 17:51:24 -0500 X-MC-Unique: S5ASt57XOhOVO6RL0Hv4tg-1 X-Mimecast-MFC-AGG-ID: S5ASt57XOhOVO6RL0Hv4tg Received: by mail-ot1-f72.google.com with SMTP id 46e09a7af769-726d163f945so3620398a34.2 for ; Tue, 11 Feb 2025 14:51:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314283; x=1739919083; 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=ML7ApDFrpE6LdiaKUIk8tOpXVHTwr8mMl/oUpLjQuwc=; b=sH8DO4umCHptzmo7ECq2c0TEL7AMr7vmirS7+gckGlYz3Znh7/DeXeGIJy36CBeDZW jDqrBafwrwz/CYqCD6qR46M1sEWQB7Dt0Mr8ZLJqc9swb1nYe/vy1D793dwdb+MAmY9j 9EpyV8YSVrjSLeqXEPoXa8Nclpotdrir3MhTvZjPjYEgMGvvKylT/0RpjCiRi8sC4GZD lrqCWa+oMEbIIcpg+o3R4Yd6PW1CTJ8KF4j96+v03s4DUOWFuziZVNv/4C5cP1h9dzVv Ua3nCdcajMDQpguXib6x66zZNr6vdyRDGeh9xuDlPKYGq7ODjiX2l0uy2db1ms8KQQHa uZ0A== X-Gm-Message-State: AOJu0Yw3/drTI2cfX1PkwHc1kt3gokF280Bi+n0XChy9m0DnpJhq6gE+ UKhb1P77cKInbtLmYjGL6U4QH/tMhT3scQoy6Dozu00K9BwnQ/memdPwQRg+GFd0pwQUnr7Lc8v 1IEGkKrmaNlJHgpfKVPEXDsRcZQl9yBxaz6RBV42QqUK2d3LzeL5nchudYAE6mkW1gs8e/hBBnr keukne0AIsMOwtGUcM52uqNmrOFNdvGQUr5A== X-Gm-Gg: ASbGncvt1EmP3gYjBsFArY/68s1mwMQ1DlTh20U7BH03rcKNBhAjHN9OUdoajufIHLI lTPYFXP+OhF80SzwyFt+pAvcjLg7cox0E9pKjExbfXLPcIHXMU+4qyiRZxcnIYgALWGSCtRHcn4 9VRIIQh0pX+PLGA6ZkUFF4b5bPHQ5u0QyfRVIzB5eTdNLHgQRiHQX8nvqzErIFpsJUmbCpA/suF hy3gjPztKBaxPRYcTq55EJoe7In7/VZ3XBT8MnphCsgTbB/mAqeioHE X-Received: by 2002:a05:6830:6483:b0:71e:7e4:c54e with SMTP id 46e09a7af769-726f1c33b3fmr818457a34.3.1739314283086; Tue, 11 Feb 2025 14:51:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6fL7Pyjhd7ZDtPaoHijVGa0J4q94S5Nxr8WauUHU94TAZlVZS6xxzABtBGgymiCaNBnVDEw== X-Received: by 2002:a05:6830:6483:b0:71e:7e4:c54e with SMTP id 46e09a7af769-726f1c33b3fmr818437a34.3.1739314282746; Tue, 11 Feb 2025 14:51:22 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:21 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PULL 11/14] hmp: use cpu_get_phys_page_debug() in hmp_gva2gpa() Date: Tue, 11 Feb 2025 17:50:55 -0500 Message-ID: <20250211225059.182533-12-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: David Hildenbrand We don't need the MemTxAttrs, so let's simply use the simpler function variant. Reviewed-by: Peter Xu Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: David Hildenbrand Link: https://lore.kernel.org/r/20250210084648.33798-7-david@redhat.com Signed-off-by: Peter Xu --- monitor/hmp-cmds-target.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/monitor/hmp-cmds-target.c b/monitor/hmp-cmds-target.c index 27ffe61818..239c2a61a4 100644 --- a/monitor/hmp-cmds-target.c +++ b/monitor/hmp-cmds-target.c @@ -301,7 +301,6 @@ void hmp_gpa2hva(Monitor *mon, const QDict *qdict) void hmp_gva2gpa(Monitor *mon, const QDict *qdict) { target_ulong addr = qdict_get_int(qdict, "addr"); - MemTxAttrs attrs; CPUState *cs = mon_get_cpu(mon); hwaddr gpa; @@ -310,7 +309,7 @@ void hmp_gva2gpa(Monitor *mon, const QDict *qdict) return; } - gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs); + gpa = cpu_get_phys_page_debug(cs, addr & TARGET_PAGE_MASK); if (gpa == -1) { monitor_printf(mon, "Unmapped\n"); } else { From patchwork Tue Feb 11 22:50:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970803 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B6479C0219B for ; Tue, 11 Feb 2025 22:51:58 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz6K-0005ir-PP; Tue, 11 Feb 2025 17:51:32 -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 1thz6J-0005hh-5t for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:31 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz6H-00056f-E1 for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314288; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3GVYGVKTNNJ14jZdJ2J7SbDQH89zHrOWXpmWYxPzaaI=; b=AJzSIkPsabAG1WfeVq1o/unPEi0jwPnp/KPMIvp8HGPYkUnON7EzTHtOb0ps0BTSpj99Xq aoUaUpje7QJ/TqTiArdpn9nLLT0i/qfo0Xpt1IAhJnRbWPDuAQkkJ7gqy+/8dsnphNwtYW D2bI6iUXyIKLHg05S+Gb56jzNs9tY0Y= Received: from mail-oo1-f69.google.com (mail-oo1-f69.google.com [209.85.161.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-148-2WXLarOkMPmMRIfMfwz6oQ-1; Tue, 11 Feb 2025 17:51:26 -0500 X-MC-Unique: 2WXLarOkMPmMRIfMfwz6oQ-1 X-Mimecast-MFC-AGG-ID: 2WXLarOkMPmMRIfMfwz6oQ_1739314286 Received: by mail-oo1-f69.google.com with SMTP id 006d021491bc7-5fc834b0d5bso810697eaf.0 for ; Tue, 11 Feb 2025 14:51:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314285; x=1739919085; 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=3GVYGVKTNNJ14jZdJ2J7SbDQH89zHrOWXpmWYxPzaaI=; b=GPieFwhwgg4HWrwXAhz9OxzcU5ZvzAxaZJn+3XkrshpvxLbq0JoTIfLAR0y+PlEIA3 xKkseZvQISOz4MgzIc7ergkS1n0PoU1oinGrRUTHnhd7mrypUEd8X11sxu7ppCi5rO+7 uyByRqxF1/ahULA9M9SUKMNuq2JpmBOOgwlMRB3cHsG1FiSDLjRyLcAj/va9YCTPZKwl DFMOah0EXdl8xrKVaQYZM6VsdTZzJhC8Sta1ua3qFlZHZkP7Et7t67wRjsA5j9Un5wH5 pPNNaDixbpoTaLAOVhKrdSy+KeY20ePSlGG57aeCyQCIV9TkOSRkUdcGkYO57rWN8TI4 cKJw== X-Gm-Message-State: AOJu0YzNSgAezeKdHnAHP8/p7wtN4seLD44a79IsAyuAumZ381Abrdyq jj/64/vChEjgegv7UQ6h/TWawyoJq2dy7HC5c41+aKol55Tel4IbzOlN3m2wGK+uOuXo3q6pG/f GUDE1SqRQdyJjw5LWpSj0fw8DStKm3JVdIkyDxDUFJ+qp8G7PAh/Fb3piwiYcCNBTSlOnt72VsF lbrKAr1yL7R4k+dnnGlctI5uART9SuDMOcTg== X-Gm-Gg: ASbGnct7jinu7If8qeEZZaXFatMIScPCcD6vCoDGfRC9Rup1ClmXXTvqvAtlCZAbEpS Jg+CoXUiPXhYrHP7GavsRCa9Axc4Udss4i0AC+C/giRxb4V3K66L2OkWDXPQOZg+eg0TJpoJY6Y XRxAgZVnuRBOOCL+mpk4yFkjR6BTpeKR79cRuLcz18nNwdDKIEwC9+4hBLN4lZj5WhmyTK8QarC imWvewaa6XK+8QlUjKVRBF/OfxinXNKQutgFp/myChSbOye3AJ57XlJ X-Received: by 2002:a05:6820:16ab:b0:5fc:9d2a:4921 with SMTP id 006d021491bc7-5fca187cdb2mr689695eaf.5.1739314285247; Tue, 11 Feb 2025 14:51:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IFf9VdW7Gi+M7z0XGNMi3OY6myZ4ZfgzX+GF32HekQ60VQroCYfWQ4/Z5DdAti1XShnh/UqIQ== X-Received: by 2002:a05:6820:16ab:b0:5fc:9d2a:4921 with SMTP id 006d021491bc7-5fca187cdb2mr689671eaf.5.1739314284490; Tue, 11 Feb 2025 14:51:24 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:23 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand Subject: [PULL 12/14] physmem: teach cpu_memory_rw_debug() to write to more memory regions Date: Tue, 11 Feb 2025 17:50:56 -0500 Message-ID: <20250211225059.182533-13-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: David Hildenbrand Right now, we only allow for writing to memory regions that allow direct access using memcpy etc; all other writes are simply ignored. This implies that debugging guests will not work as expected when writing to MMIO device regions. Let's extend cpu_memory_rw_debug() to write to more memory regions, including MMIO device regions. Reshuffle the condition in memory_access_is_direct() to make it easier to read and add a comment. While this change implies that debug access can now also write to MMIO devices, we now are also permit ELF image loads and similar users of cpu_memory_rw_debug() to write to MMIO devices; currently we ignore these writes. Peter assumes [1] that there's probably a class of guest images, which will start writing junk (likely zeroes) into device model registers; we previously would silently ignore any such bogus ELF sections. Likely these images are of questionable correctness and this can be ignored. If ever a problem, we could make these cases use address_space_write_rom() instead, which is left unchanged for now. This patch is based on previous work by Stefan Zabka. [1] https://lore.kernel.org/all/CAFEAcA_2CEJKFyjvbwmpt=on=GgMVamQ5hiiVt+zUr6AY3X=Xg@mail.gmail.com/ Resolves: https://gitlab.com/qemu-project/qemu/-/issues/213 Reviewed-by: Peter Xu Signed-off-by: David Hildenbrand Link: https://lore.kernel.org/r/20250210084648.33798-8-david@redhat.com Signed-off-by: Peter Xu --- include/exec/memattrs.h | 5 ++++- include/exec/memory.h | 3 ++- hw/core/cpu-system.c | 13 +++++++++---- system/physmem.c | 9 ++------- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/exec/memattrs.h b/include/exec/memattrs.h index 060b7e7131..8db1d30464 100644 --- a/include/exec/memattrs.h +++ b/include/exec/memattrs.h @@ -44,6 +44,8 @@ typedef struct MemTxAttrs { * (see MEMTX_ACCESS_ERROR). */ unsigned int memory:1; + /* Debug access that can even write to ROM. */ + unsigned int debug:1; /* Requester ID (for MSI for example) */ unsigned int requester_id:16; @@ -56,7 +58,8 @@ typedef struct MemTxAttrs { * Bus masters which don't specify any attributes will get this * (via the MEMTXATTRS_UNSPECIFIED constant), so that we can * distinguish "all attributes deliberately clear" from - * "didn't specify" if necessary. + * "didn't specify" if necessary. "debug" can be set alongside + * "unspecified". */ bool unspecified; diff --git a/include/exec/memory.h b/include/exec/memory.h index b18ecf933e..78c4e0aec8 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -3018,7 +3018,8 @@ static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write, if (!memory_region_supports_direct_access(mr)) { return false; } - if (is_write) { + /* Debug access can write to ROM. */ + if (is_write && !attrs.debug) { return !mr->readonly && !mr->rom_device; } return true; diff --git a/hw/core/cpu-system.c b/hw/core/cpu-system.c index 6aae28a349..6e307c8959 100644 --- a/hw/core/cpu-system.c +++ b/hw/core/cpu-system.c @@ -51,13 +51,18 @@ hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr, MemTxAttrs *attrs) { CPUClass *cc = CPU_GET_CLASS(cpu); + hwaddr paddr; if (cc->sysemu_ops->get_phys_page_attrs_debug) { - return cc->sysemu_ops->get_phys_page_attrs_debug(cpu, addr, attrs); + paddr = cc->sysemu_ops->get_phys_page_attrs_debug(cpu, addr, attrs); + } else { + /* Fallback for CPUs which don't implement the _attrs_ hook */ + *attrs = MEMTXATTRS_UNSPECIFIED; + paddr = cc->sysemu_ops->get_phys_page_debug(cpu, addr); } - /* Fallback for CPUs which don't implement the _attrs_ hook */ - *attrs = MEMTXATTRS_UNSPECIFIED; - return cc->sysemu_ops->get_phys_page_debug(cpu, addr); + /* Indicate that this is a debug access. */ + attrs->debug = 1; + return paddr; } hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr) diff --git a/system/physmem.c b/system/physmem.c index 8745c10c9d..d3efdf13d3 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -3680,13 +3680,8 @@ int cpu_memory_rw_debug(CPUState *cpu, vaddr addr, if (l > len) l = len; phys_addr += (addr & ~TARGET_PAGE_MASK); - if (is_write) { - res = address_space_write_rom(cpu->cpu_ases[asidx].as, phys_addr, - attrs, buf, l); - } else { - res = address_space_read(cpu->cpu_ases[asidx].as, phys_addr, - attrs, buf, l); - } + res = address_space_rw(cpu->cpu_ases[asidx].as, phys_addr, attrs, buf, + l, is_write); if (res != MEMTX_OK) { return -1; } From patchwork Tue Feb 11 22:50:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970813 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7E468C0219B for ; Tue, 11 Feb 2025 22:53:24 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz6M-0005k0-3v; Tue, 11 Feb 2025 17:51:34 -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 1thz6K-0005iy-PI for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:32 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz6I-00056z-Qo for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:32 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314290; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dgza/Y0wiw4u+TyuTMHe0Q7D53LiwmAz3XUt+5camiA=; b=KBtg5UphyY2TR7Gs/rYc/TsWt3yMIh4jzu6omDuVoX8IUvVrZSwSNoTvP9zwCIlaVTVfRk Ii86kyz8SNdNrKOt9ERCpsoogBoaIywiaRioui7+iAhZrOF/okHTE3uaSU/kqARkTWMu1Q QOldWLRdVaePkFG5pkMaOx/6Odj2Vc8= Received: from mail-ot1-f69.google.com (mail-ot1-f69.google.com [209.85.210.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-208-UUkp4txgM7uj2fEdB8Rc2w-1; Tue, 11 Feb 2025 17:51:28 -0500 X-MC-Unique: UUkp4txgM7uj2fEdB8Rc2w-1 X-Mimecast-MFC-AGG-ID: UUkp4txgM7uj2fEdB8Rc2w Received: by mail-ot1-f69.google.com with SMTP id 46e09a7af769-726e862d3b9so1715263a34.0 for ; Tue, 11 Feb 2025 14:51:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314287; x=1739919087; 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=dgza/Y0wiw4u+TyuTMHe0Q7D53LiwmAz3XUt+5camiA=; b=T7NW+cAOSwReYS1Az+qLebkAXfLbbkINc3iO5TH9L58+0QClFoMMTfHNhavXOfApmz GLF1uQx4Bnb0d+VB0plhY5rGEy9KLXu5OzKtDCgzfBOGV3pLLX34zwjqDd2eYvstcx79 cZYC4IjUYgMcKUn854i+7KuwKroi16153qlIOpRP1CIr7a3U/yuGKuYpp7YneiVHFNIt //i2x4VApwKLVQavNrZdLxDEcz9NMtMrJ5um8XQVZhDPUVczpRBqG4P4e897EJHs+b/t UZTVAYBsqvesSqWRk3NtfD3wdim3U1iHECZ1JC7NBDoQbScMmknLHhZFr0hAFtk/DqP5 Fysw== X-Gm-Message-State: AOJu0YxeWdykk0BDXQtGzrNpJ0W32+iA/C3xo3BpjTFrBKUtQkS2MPt2 eJE9xVAMZOQXpZT6lc+eNFehTtwqEOYI1yKoqxWCELQ+VaYgRPVxe9b1ZAHi4BznL34MCCr6iEq C0tUNqMgLixBx9DYMf7tCIm5pV2iOhb3ObhXdi8kcUHn2cGhiPEOI3IE30gC3H0v/J5cYbWtM2U jH+p+VVBnK3wLgDBNPcMLC3NBpZFf7Q/rD9A== X-Gm-Gg: ASbGncunJBI3U6q0iTEFwIv97h9ZycXt2nkCT1Yakpw/LfFHvDlU/40h5ZdjaaAQj5g UbPxIWohkviH3JmLbVwhrbhTyIV4t7QbY5vxtoNTk5geo8IOxXb+iFiF8vuCiG/XAQiEPe29Ub8 uTcOA3TA9BWQ78Cu+p3zCkjxuKyH2SUHsczwCt+swq5F1jV7oEK07luT7221wTMOtRn8R1VlnE1 xflR93SprYqNdkIvfns5V7Ikh0lgd0Wy74r9OgC5ghSMMgtiC6czFQe X-Received: by 2002:a05:6830:65c1:b0:71d:5c89:e505 with SMTP id 46e09a7af769-726f1c65248mr869333a34.9.1739314286956; Tue, 11 Feb 2025 14:51:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IF7OsuzvjtstwJBJV5Gac4F2rxsCRoVwXw4UPUEbm8iAol2SkW/oPpc3U2o4S2ieJM+AjRGtw== X-Received: by 2002:a05:6830:65c1:b0:71d:5c89:e505 with SMTP id 46e09a7af769-726f1c65248mr869318a34.9.1739314286576; Tue, 11 Feb 2025 14:51:26 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:25 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand , William Roche Subject: [PULL 13/14] system/physmem: handle hugetlb correctly in qemu_ram_remap() Date: Tue, 11 Feb 2025 17:50:57 -0500 Message-ID: <20250211225059.182533-14-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: William Roche The list of hwpoison pages used to remap the memory on reset is based on the backend real page size. To correctly handle hugetlb, we must mmap(MAP_FIXED) a complete hugetlb page; hugetlb pages cannot be partially mapped. Signed-off-by: William Roche Co-developed-by: David Hildenbrand Acked-by: David Hildenbrand Reviewed-by: Peter Xu Link: https://lore.kernel.org/r/20250211212707.302391-2-william.roche@oracle.com Signed-off-by: Peter Xu --- include/exec/cpu-common.h | 2 +- accel/kvm/kvm-all.c | 2 +- system/physmem.c | 38 +++++++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h index b1d76d6985..3771b2130c 100644 --- a/include/exec/cpu-common.h +++ b/include/exec/cpu-common.h @@ -67,7 +67,7 @@ typedef uintptr_t ram_addr_t; /* memory API */ -void qemu_ram_remap(ram_addr_t addr, ram_addr_t length); +void qemu_ram_remap(ram_addr_t addr); /* This should not be used by devices. */ ram_addr_t qemu_ram_addr_from_host(void *ptr); ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr); diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index c65b790433..f89568bfa3 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -1288,7 +1288,7 @@ static void kvm_unpoison_all(void *param) QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) { QLIST_REMOVE(page, list); - qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE); + qemu_ram_remap(page->ram_addr); g_free(page); } } diff --git a/system/physmem.c b/system/physmem.c index d3efdf13d3..af1175a57c 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -2275,17 +2275,35 @@ void qemu_ram_free(RAMBlock *block) } #ifndef _WIN32 -void qemu_ram_remap(ram_addr_t addr, ram_addr_t length) +/* + * qemu_ram_remap - remap a single RAM page + * + * @addr: address in ram_addr_t address space. + * + * This function will try remapping a single page of guest RAM identified by + * @addr, essentially discarding memory to recover from previously poisoned + * memory (MCE). The page size depends on the RAMBlock (i.e., hugetlb). @addr + * does not have to point at the start of the page. + * + * This function is only to be used during system resets; it will kill the + * VM if remapping failed. + */ +void qemu_ram_remap(ram_addr_t addr) { RAMBlock *block; - ram_addr_t offset; + uint64_t offset; int flags; void *area, *vaddr; int prot; + size_t page_size; RAMBLOCK_FOREACH(block) { offset = addr - block->offset; if (offset < block->max_length) { + /* Respect the pagesize of our RAMBlock */ + page_size = qemu_ram_pagesize(block); + offset = QEMU_ALIGN_DOWN(offset, page_size); + vaddr = ramblock_ptr(block, offset); if (block->flags & RAM_PREALLOC) { ; @@ -2299,21 +2317,23 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t length) prot = PROT_READ; prot |= block->flags & RAM_READONLY ? 0 : PROT_WRITE; if (block->fd >= 0) { - area = mmap(vaddr, length, prot, flags, block->fd, + area = mmap(vaddr, page_size, prot, flags, block->fd, offset + block->fd_offset); } else { flags |= MAP_ANONYMOUS; - area = mmap(vaddr, length, prot, flags, -1, 0); + area = mmap(vaddr, page_size, prot, flags, -1, 0); } if (area != vaddr) { - error_report("Could not remap addr: " - RAM_ADDR_FMT "@" RAM_ADDR_FMT "", - length, addr); + error_report("Could not remap RAM %s:%" PRIx64 "+%" PRIx64 + " +%zx", block->idstr, offset, + block->fd_offset, page_size); exit(1); } - memory_try_enable_merging(vaddr, length); - qemu_ram_setup_dump(vaddr, length); + memory_try_enable_merging(vaddr, page_size); + qemu_ram_setup_dump(vaddr, page_size); } + + break; } } } From patchwork Tue Feb 11 22:50:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13970804 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 14A72C021A1 for ; Tue, 11 Feb 2025 22:52:01 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz6P-0005kr-Rs; Tue, 11 Feb 2025 17:51:37 -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 1thz6N-0005k8-FE for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:35 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz6L-00057S-Pl for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314293; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y5xSVhRiQUHuJeHocslUI3HV6Wv1hLUrZwIyEA1RiQE=; b=VYrZd3c+hS+uywmhGxYixJ+5woeNlOSSRixipAQj7hf9amoDqqMHrucLvy5OiuapYbN5t8 NFD+sfUrAR5QFK1oBpvcriYHztoHWPcOH8qVnrnZJNTypA1C/wzAJysKG301IJpeepJeB5 WNWSBnCR03RXLOveiHFq2+R4eNwW2KE= Received: from mail-oo1-f69.google.com (mail-oo1-f69.google.com [209.85.161.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-367-uBO6tYYcNi-xu47RL5k14g-1; Tue, 11 Feb 2025 17:51:30 -0500 X-MC-Unique: uBO6tYYcNi-xu47RL5k14g-1 X-Mimecast-MFC-AGG-ID: uBO6tYYcNi-xu47RL5k14g Received: by mail-oo1-f69.google.com with SMTP id 006d021491bc7-5fc7f76cae8so1864735eaf.2 for ; Tue, 11 Feb 2025 14:51:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314289; x=1739919089; 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=Y5xSVhRiQUHuJeHocslUI3HV6Wv1hLUrZwIyEA1RiQE=; b=l655qWhAWybPccb0V79k1vEjGdEnw6XyUw/h+Lc6vMhkTs7p8B2NIT0hTDjV0miHrX Xoj/irexZIyvOhEyIkee2k2rufPVuRoE7M4kUiOB029Kwr3S7NVjhJKunZ8KjDSo0W+Y HJWgEWlK8EtNtXuBlMwZfYkSsCQB6NZk5WjPmRUEGcczcrJ/7Wb/v6V0fWk4GqVaf9w2 ZpIrN74dLu6G7zWeqTuLxXWXCRJBkc8txB6Sj+HCT1u7x8Fm9CSSsmFWFmOjiBFHMVRA u4QfSP0YdylNVXQIUqOhkGHpLuPTZHRBrNFJjbVgglpqIQkNxnsNyrL1y6/HR6vG/rMi cVRg== X-Gm-Message-State: AOJu0YzP7NEdNQthQyPdDfAtbYIhtN8zpTKZ3yTYdINf7VhdF5bjV6b6 1We1fnqT52gFwngNG/v8Iv47W3e7+HmRHxiwbk3GMHFJ5tH6p4f57eVhYIG6ij907LuRN6YRI8C xax1hrXvAGPpXMQ7MQnsKzlpN5yoaY6E9028CMHmqilT7ZYCohvrKMTDNw6E1HA1VaRnIpQRfXB flInMY7WEqaJG+lmlgXlRlfQt8rIniCPrKAQ== X-Gm-Gg: ASbGncu1WVR7TGXH4xccXpV3s3uW3T2P2rJ6TNsjBCVhOinkJI1CclzRAtigNOe26tU yEq/iyawim06yZ64eRDJ8GjJ7f5qerAtFbBRavi/65sg/Yk9uxq8gonN2dqIGlL5lmIK1sBRLNg ol8Rwm84TwDlc6E2P6DAW7AAxRgPojfEB4JSj+e+Posl78KOFXryzYOH0EPpeO1ErLbHKwb4Wnc lu7uHvKyMx1gqBokcBBm5WSx8nHxcc48CXTGII64OIHU/yaxwP7uglv X-Received: by 2002:a05:6820:1f07:b0:5fa:3bf1:f9df with SMTP id 006d021491bc7-5fca301f2b6mr321843eaf.8.1739314288836; Tue, 11 Feb 2025 14:51:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IEa6jrKb3Gv58ndFAt0lsM7U7Kaesv3JiRWt8J6hqSNIqM0hfeScNvVAW30jnDEesyup1soiQ== X-Received: by 2002:a05:6820:1f07:b0:5fa:3bf1:f9df with SMTP id 006d021491bc7-5fca301f2b6mr321834eaf.8.1739314288496; Tue, 11 Feb 2025 14:51:28 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:27 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand , William Roche Subject: [PULL 14/14] system/physmem: poisoned memory discard on reboot Date: Tue, 11 Feb 2025 17:50:58 -0500 Message-ID: <20250211225059.182533-15-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: William Roche Repair poisoned memory location(s), calling ram_block_discard_range(): punching a hole in the backend file when necessary and regenerating a usable memory. If the kernel doesn't support the madvise calls used by this function and we are dealing with anonymous memory, fall back to remapping the location(s). Signed-off-by: William Roche Acked-by: David Hildenbrand Link: https://lore.kernel.org/r/20250211212707.302391-3-william.roche@oracle.com Signed-off-by: Peter Xu --- system/physmem.c | 57 ++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/system/physmem.c b/system/physmem.c index af1175a57c..67bdf631e6 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -2275,6 +2275,23 @@ void qemu_ram_free(RAMBlock *block) } #ifndef _WIN32 +/* Simply remap the given VM memory location from start to start+length */ +static int qemu_ram_remap_mmap(RAMBlock *block, uint64_t start, size_t length) +{ + int flags, prot; + void *area; + void *host_startaddr = block->host + start; + + assert(block->fd < 0); + flags = MAP_FIXED | MAP_ANONYMOUS; + flags |= block->flags & RAM_SHARED ? MAP_SHARED : MAP_PRIVATE; + flags |= block->flags & RAM_NORESERVE ? MAP_NORESERVE : 0; + prot = PROT_READ; + prot |= block->flags & RAM_READONLY ? 0 : PROT_WRITE; + area = mmap(host_startaddr, length, prot, flags, -1, 0); + return area != host_startaddr ? -errno : 0; +} + /* * qemu_ram_remap - remap a single RAM page * @@ -2292,9 +2309,7 @@ void qemu_ram_remap(ram_addr_t addr) { RAMBlock *block; uint64_t offset; - int flags; - void *area, *vaddr; - int prot; + void *vaddr; size_t page_size; RAMBLOCK_FOREACH(block) { @@ -2310,24 +2325,24 @@ void qemu_ram_remap(ram_addr_t addr) } else if (xen_enabled()) { abort(); } else { - flags = MAP_FIXED; - flags |= block->flags & RAM_SHARED ? - MAP_SHARED : MAP_PRIVATE; - flags |= block->flags & RAM_NORESERVE ? MAP_NORESERVE : 0; - prot = PROT_READ; - prot |= block->flags & RAM_READONLY ? 0 : PROT_WRITE; - if (block->fd >= 0) { - area = mmap(vaddr, page_size, prot, flags, block->fd, - offset + block->fd_offset); - } else { - flags |= MAP_ANONYMOUS; - area = mmap(vaddr, page_size, prot, flags, -1, 0); - } - if (area != vaddr) { - error_report("Could not remap RAM %s:%" PRIx64 "+%" PRIx64 - " +%zx", block->idstr, offset, - block->fd_offset, page_size); - exit(1); + if (ram_block_discard_range(block, offset, page_size) != 0) { + /* + * Fall back to using mmap() only for anonymous mapping, + * as if a backing file is associated we may not be able + * to recover the memory in all cases. + * So don't take the risk of using only mmap and fail now. + */ + if (block->fd >= 0) { + error_report("Could not remap RAM %s:%" PRIx64 "+%" + PRIx64 " +%zx", block->idstr, offset, + block->fd_offset, page_size); + exit(1); + } + if (qemu_ram_remap_mmap(block, offset, page_size) != 0) { + error_report("Could not remap RAM %s:%" PRIx64 " +%zx", + block->idstr, offset, page_size); + exit(1); + } } memory_try_enable_merging(vaddr, page_size); qemu_ram_setup_dump(vaddr, page_size);