From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582798434; cv=none; d=zohomail.com; s=zohoarc; b=XpPLuuaKIgHAu5qljFJQ7dXk7dnsqfyNC/aUhnXDimuAJJPqDK4rGMNxhdgl2ho1OQmRny6GhB4P8D5g8EsM7dscAIwQrsZy3IEdOAGOsAYyCdPcl0T8ubWON8IZOWNjpmKj/FZ+XQHNLVGH/vUNgXVAthFetCF2f/RePObRSTs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582798434; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=zwsZQQ5MN8MY5ICY0EQq4aovYGXTGBvqIvi5JLhszK0=; b=CdhOg8XXhlRzlzEYce2/bEL/R2f6DP3V8gLEWDQKyvaAnIMBbh4RkmeMGeT9fRqD6V27qGBkIf6Am3qFejrLc7X0N2B4d/NOHCo5ta2VC4+YrPnJBOllPruEaXsd1DDoRcv1I5/WRYhzEAnW3EV0zdk9kwvmPU67nCG91BadmwE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582798434050591.8615706557982; Thu, 27 Feb 2020 02:13:54 -0800 (PST) Received: from localhost ([::1]:56550 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GAy-0002dV-BS for importer@patchew.org; Thu, 27 Feb 2020 05:13:52 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39292) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GA2-0001ak-Hk for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:12:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GA1-0003Cs-4r for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:12:54 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:55453 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GA1-0003CW-1N for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:12:53 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-43-AmvJFN6nO82AeiyMb6tKRw-1; Thu, 27 Feb 2020 05:12:51 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9B1D1801E6C; Thu, 27 Feb 2020 10:12:49 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4C59E6E3EE; Thu, 27 Feb 2020 10:12:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798372; 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=zwsZQQ5MN8MY5ICY0EQq4aovYGXTGBvqIvi5JLhszK0=; b=hAP5NQjVQae3NT6q1YOUkw62LtcM5jJg5t6chl/adsdhvX4nedkAyrhdibpAGT7N79Hkdl Dn8B9AacE7ns7wgJlgBSfOR6kKzwFfrtVS67cwMKOF0G+rKxarcRX688jungEKwP+3pLlw VJ3cDoGcm4KB3PpfjNX95wG4Wm8L3LQ= X-MC-Unique: AmvJFN6nO82AeiyMb6tKRw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 01/15] util: vfio-helpers: Fix qemu_vfio_close() Date: Thu, 27 Feb 2020 11:11:51 +0100 Message-Id: <20200227101205.5616-2-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Alex Williamson , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Stefan Hajnoczi , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" qemu_vfio_undo_mapping() will decrement the number of mappings and reshuffle the array elements to fit into the reduced size. Iterating over all elements like this does not work as expected, let's make sure to remove all mappings properly. Reviewed-by: Peter Xu Cc: Richard Henderson Cc: Paolo Bonzini Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Alex Williamson Cc: Stefan Hajnoczi Signed-off-by: David Hildenbrand --- util/vfio-helpers.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/util/vfio-helpers.c b/util/vfio-helpers.c index 9ec01bfe26..f31aa77ffe 100644 --- a/util/vfio-helpers.c +++ b/util/vfio-helpers.c @@ -695,13 +695,11 @@ static void qemu_vfio_reset(QEMUVFIOState *s) /* Close and free the VFIO resources. */ void qemu_vfio_close(QEMUVFIOState *s) { - int i; - if (!s) { return; } - for (i =3D 0; i < s->nr_mappings; ++i) { - qemu_vfio_undo_mapping(s, &s->mappings[i], NULL); + while (s->nr_mappings) { + qemu_vfio_undo_mapping(s, &s->mappings[s->nr_mappings - 1], NULL); } ram_block_notifier_remove(&s->ram_notifier); qemu_vfio_reset(s); --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582798586; cv=none; d=zohomail.com; s=zohoarc; b=g9JMOIn/Vf23AwiDIfRPhWZQeI3FgtbPQtwz4HZPRBhXw8mTBLTUCbA+hlrKjF4qtcQC0QrK4MVNwU4f/smYvQTOHMC/IdFTCxDvrQkceP/LDXmpt6LP3TA53GjCd7AWhClxtFZwcnxqBlnYVnQYZWZw3RApPE1k06Vr08DoTeg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582798586; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=tSdqeDwxoY+yI2Z7vVys/AxXflRczSoFv2iz+5GIaqc=; b=Y652TLH5npG4oal8fgBJtCTSQgn83BsEhyr92muxfq3hfTAlchvZXPiD01/93os9kBJwSWERt10gXtqNs0rNcBHqKC2iTAP+40w2qq/6z7o9Nnwtgiupxwj0UG10eEkvaDoY5v3PvqYZWGun5LLHVUPlJsxGuiOGrEI8j+sTvw8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 158279858671353.492913967390564; Thu, 27 Feb 2020 02:16:26 -0800 (PST) Received: from localhost ([::1]:56610 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GDR-0005Rl-Gd for importer@patchew.org; Thu, 27 Feb 2020 05:16:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39549) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GAM-00024I-LK for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GAL-0003xf-GT for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:14 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:49686 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GAL-0003vU-76 for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:13 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-194-it8P8USRNSCTpYOJ-fQ8vw-1; Thu, 27 Feb 2020 05:13:11 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 361CB18A5505; Thu, 27 Feb 2020 10:13:10 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8B90E909FA; Thu, 27 Feb 2020 10:12:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798392; 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=tSdqeDwxoY+yI2Z7vVys/AxXflRczSoFv2iz+5GIaqc=; b=DW1RrB8cCOpfcGmWpc25qpqJIQHLY+wzKH0FlN+83QKSLwzqkiA1nMLNEjd42PbW1FRVEv +epR8KwmJ4TJMdwcyTcgijZ+48CRYyzs+dZrze153ECZmWEJqw+qX/tacvUyL1HFyeEjs4 Z2MUWxNine8LAavnw5KlLm+jR8aOopE= X-MC-Unique: it8P8USRNSCTpYOJ-fQ8vw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 02/15] util: vfio-helpers: Remove Error parameter from qemu_vfio_undo_mapping() Date: Thu, 27 Feb 2020 11:11:52 +0100 Message-Id: <20200227101205.5616-3-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Alex Williamson , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Stefan Hajnoczi , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Everybody discards the error. Let's error_report() instead so this error doesn't get lost. This is now the same error handling as in qemu_vfio_do_mapping(). However, we don't report any errors via the return value to the caller. This seems to be one of these "will never happen, but let's better print an error because the caller can't handle it either way" cases. Cc: Richard Henderson Cc: Paolo Bonzini Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Alex Williamson Cc: Stefan Hajnoczi Signed-off-by: David Hildenbrand --- util/vfio-helpers.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/util/vfio-helpers.c b/util/vfio-helpers.c index f31aa77ffe..7085ca702c 100644 --- a/util/vfio-helpers.c +++ b/util/vfio-helpers.c @@ -541,8 +541,7 @@ static int qemu_vfio_do_mapping(QEMUVFIOState *s, void = *host, size_t size, /** * Undo the DMA mapping from @s with VFIO, and remove from mapping list. */ -static void qemu_vfio_undo_mapping(QEMUVFIOState *s, IOVAMapping *mapping, - Error **errp) +static void qemu_vfio_undo_mapping(QEMUVFIOState *s, IOVAMapping *mapping) { int index; struct vfio_iommu_type1_dma_unmap unmap =3D { @@ -557,7 +556,7 @@ static void qemu_vfio_undo_mapping(QEMUVFIOState *s, IO= VAMapping *mapping, assert(QEMU_IS_ALIGNED(mapping->size, qemu_real_host_page_size)); assert(index >=3D 0 && index < s->nr_mappings); if (ioctl(s->container, VFIO_IOMMU_UNMAP_DMA, &unmap)) { - error_setg_errno(errp, errno, "VFIO_UNMAP_DMA failed"); + error_report("VFIO_UNMAP_DMA failed: %s", strerror(errno)); } memmove(mapping, &s->mappings[index + 1], sizeof(s->mappings[0]) * (s->nr_mappings - index - 1)); @@ -622,7 +621,7 @@ int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, siz= e_t size, assert(qemu_vfio_verify_mappings(s)); ret =3D qemu_vfio_do_mapping(s, host, size, iova0); if (ret) { - qemu_vfio_undo_mapping(s, mapping, NULL); + qemu_vfio_undo_mapping(s, mapping); goto out; } s->low_water_mark +=3D size; @@ -682,7 +681,7 @@ void qemu_vfio_dma_unmap(QEMUVFIOState *s, void *host) if (!m) { goto out; } - qemu_vfio_undo_mapping(s, m, NULL); + qemu_vfio_undo_mapping(s, m); out: qemu_mutex_unlock(&s->lock); } @@ -699,7 +698,7 @@ void qemu_vfio_close(QEMUVFIOState *s) return; } while (s->nr_mappings) { - qemu_vfio_undo_mapping(s, &s->mappings[s->nr_mappings - 1], NULL); + qemu_vfio_undo_mapping(s, &s->mappings[s->nr_mappings - 1]); } ram_block_notifier_remove(&s->ram_notifier); qemu_vfio_reset(s); --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582798580; cv=none; d=zohomail.com; s=zohoarc; b=g8SfnUkAgNSmTmDyxdWzmuzHfo9vaY9QBDtxpca0zdHyxAQLPfv3eytT23S1BtyScEg5/nE1J8VDxoQbGLxQfuglesRFi6ZbZmtwGNXRJ30XjJRd0z9chWRKe6DmyGyGyDrfRqlpyfIHtSUukClr2sroRV8kBc53cDSueFA6CQo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582798580; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0oUugQEab4bqH2Kasf8WtgB6oPJtcM2uppzFCmdwZI8=; b=kZ9TCVM6+7YXNNjBSdH4baYBbQQNrUqAq6wBmJgNoKvwMsotxCfQA70s/iN8SGqSGCmG9R6ETIUg2Tv8DBMT99aj4DEXReIo/8uTRy9mvjV0LQVCkgN60/NHNfb3n+dZoUFOJ9tKzEFN+sU/VaN+dcXIhGHcmVgdQQLARhkS/u0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582798580182392.99747233580706; Thu, 27 Feb 2020 02:16:20 -0800 (PST) Received: from localhost ([::1]:56608 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GDK-0005Fg-V2 for importer@patchew.org; Thu, 27 Feb 2020 05:16:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39725) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GAd-0002aZ-ER for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GAc-0004vE-6A for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:31 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:22256 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GAc-0004tR-2Q for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:30 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-184-lBkRLhNaPR-4ljnd_s9wpg-1; Thu, 27 Feb 2020 05:13:27 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E43E6DBAD; Thu, 27 Feb 2020 10:13:25 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1822A6E3EE; Thu, 27 Feb 2020 10:13:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798409; 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=0oUugQEab4bqH2Kasf8WtgB6oPJtcM2uppzFCmdwZI8=; b=T4Jt+cjdzqr0fDnzdKSbrbzfmmls+PzSs+S3wNO+s5kf2TItRbn1ltceJXUmnGqCmhBrqj 7ID3/kQea6eOHAr2akmxxc5K1qd3FgdIIO0+lL2IlliJJL2HS2xorRSlMnsEZfvYL5bd3m GyIrfUhG7tVqm6xize5aQz5/BUQe7ts= X-MC-Unique: lBkRLhNaPR-4ljnd_s9wpg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 03/15] util: vfio-helpers: Factor out removal from qemu_vfio_undo_mapping() Date: Thu, 27 Feb 2020 11:11:53 +0100 Message-Id: <20200227101205.5616-4-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Alex Williamson , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Stefan Hajnoczi , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Factor it out and properly use it where applicable. Make qemu_vfio_undo_mapping() look like qemu_vfio_do_mapping(), passing the size and iova, not the mapping. Reviewed-by: Peter Xu Cc: Richard Henderson Cc: Paolo Bonzini Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Alex Williamson Cc: Stefan Hajnoczi Signed-off-by: David Hildenbrand --- util/vfio-helpers.c | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/util/vfio-helpers.c b/util/vfio-helpers.c index 7085ca702c..f0c77f0d69 100644 --- a/util/vfio-helpers.c +++ b/util/vfio-helpers.c @@ -518,6 +518,20 @@ static IOVAMapping *qemu_vfio_add_mapping(QEMUVFIOStat= e *s, return insert; } =20 +/** + * Remove the mapping from @s and free it. + */ +static void qemu_vfio_remove_mapping(QEMUVFIOState *s, IOVAMapping *mappin= g) +{ + const int index =3D mapping - s->mappings; + + assert(index >=3D 0 && index < s->nr_mappings); + memmove(mapping, &s->mappings[index + 1], + sizeof(s->mappings[0]) * (s->nr_mappings - index - 1)); + s->nr_mappings--; + s->mappings =3D g_renew(IOVAMapping, s->mappings, s->nr_mappings); +} + /* Do the DMA mapping with VFIO. */ static int qemu_vfio_do_mapping(QEMUVFIOState *s, void *host, size_t size, uint64_t iova) @@ -539,29 +553,22 @@ static int qemu_vfio_do_mapping(QEMUVFIOState *s, voi= d *host, size_t size, } =20 /** - * Undo the DMA mapping from @s with VFIO, and remove from mapping list. + * Undo the DMA mapping from @s with VFIO. */ -static void qemu_vfio_undo_mapping(QEMUVFIOState *s, IOVAMapping *mapping) +static void qemu_vfio_undo_mapping(QEMUVFIOState *s, size_t size, uint64_t= iova) { - int index; struct vfio_iommu_type1_dma_unmap unmap =3D { .argsz =3D sizeof(unmap), .flags =3D 0, - .iova =3D mapping->iova, - .size =3D mapping->size, + .iova =3D iova, + .size =3D size, }; =20 - index =3D mapping - s->mappings; - assert(mapping->size > 0); - assert(QEMU_IS_ALIGNED(mapping->size, qemu_real_host_page_size)); - assert(index >=3D 0 && index < s->nr_mappings); + assert(size > 0); + assert(QEMU_IS_ALIGNED(size, qemu_real_host_page_size)); if (ioctl(s->container, VFIO_IOMMU_UNMAP_DMA, &unmap)) { error_report("VFIO_UNMAP_DMA failed: %s", strerror(errno)); } - memmove(mapping, &s->mappings[index + 1], - sizeof(s->mappings[0]) * (s->nr_mappings - index - 1)); - s->nr_mappings--; - s->mappings =3D g_renew(IOVAMapping, s->mappings, s->nr_mappings); } =20 /* Check if the mapping list is (ascending) ordered. */ @@ -621,7 +628,7 @@ int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, siz= e_t size, assert(qemu_vfio_verify_mappings(s)); ret =3D qemu_vfio_do_mapping(s, host, size, iova0); if (ret) { - qemu_vfio_undo_mapping(s, mapping); + qemu_vfio_remove_mapping(s, mapping); goto out; } s->low_water_mark +=3D size; @@ -681,7 +688,8 @@ void qemu_vfio_dma_unmap(QEMUVFIOState *s, void *host) if (!m) { goto out; } - qemu_vfio_undo_mapping(s, m); + qemu_vfio_undo_mapping(s, m->size, m->iova); + qemu_vfio_remove_mapping(s, m); out: qemu_mutex_unlock(&s->lock); } @@ -698,7 +706,10 @@ void qemu_vfio_close(QEMUVFIOState *s) return; } while (s->nr_mappings) { - qemu_vfio_undo_mapping(s, &s->mappings[s->nr_mappings - 1]); + IOVAMapping *m =3D &s->mappings[s->nr_mappings - 1]; + + qemu_vfio_undo_mapping(s, m->size, m->iova); + qemu_vfio_remove_mapping(s, m); } ram_block_notifier_remove(&s->ram_notifier); qemu_vfio_reset(s); --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582798516; cv=none; d=zohomail.com; s=zohoarc; b=YnoKScIifYxsnPBVu5FL30DR2sHUoZIE7nSK29mhhMo4HZoRfSZAZKObknCX8XiPRfuKYrAs5ns1mmsoeO6ZhIxuNxlnxhjZiAx8fCm21EnbkJZ98nJjFk/sxbLFaitEF9EvK6KLLGsyhhDiHJTPxJh/OZhQA1ohyoU82E2rVVM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582798516; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=+sluZhPYprErX++KiZmMUawrspIKQ57T8Kb+R83J2lg=; b=Evoz5aCNtPDowM9MSALrLsttZ4ZcFKurA8hDQ51xfXYVNxqj3bIVbf0zOeUipElBi/zRgtd0wiAk9hAZ+O7NoCPRU8TFsijzdFCzNwG2xaX0Aw3aFSODpTyu8PYpjOxgmYpUZYDqj3kij173f6RFmAXjS9kXQ7LYsMJVZslFG/8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582798516796306.2173947589338; Thu, 27 Feb 2020 02:15:16 -0800 (PST) Received: from localhost ([::1]:56580 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GCJ-00041l-Ds for importer@patchew.org; Thu, 27 Feb 2020 05:15:15 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39769) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GAg-0002fT-8I for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GAf-0005BX-25 for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:34 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:34589 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GAe-00059G-Tt for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:32 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-310-PoV_jcnWNP-QILhG0TiCvg-1; Thu, 27 Feb 2020 05:13:30 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A60041005512; Thu, 27 Feb 2020 10:13:29 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8F88A9079D; Thu, 27 Feb 2020 10:13:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798412; 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=+sluZhPYprErX++KiZmMUawrspIKQ57T8Kb+R83J2lg=; b=BmeKSSlgkuA0ygxQIzOGQ8WeKoOUG6F6SYot6W3YLnAaKg+rmXJKK4XKPWbhcEd6Z3Iq2b 0LURWXlzWtOmzMwvXAXeuAuk4jX6MaNVTQzJNZQtA2+dzTP4ioamWVQeey0qF4rFSHXIgU vYGKrvR2C7uMgWYlFPvmK325aOk/PDs= X-MC-Unique: PoV_jcnWNP-QILhG0TiCvg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 04/15] exec: Factor out setting ram settings (madvise ...) into qemu_ram_apply_settings() Date: Thu, 27 Feb 2020 11:11:54 +0100 Message-Id: <20200227101205.5616-5-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Richard Henderson , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Factor all settings out into qemu_ram_apply_settings(). For memory_try_enable_merging(), the important bit is that it won't be called with XEN - which is now still the case as new_block->host will remain NULL. Reviewed-by: Richard Henderson Reviewed-by: Peter Xu Cc: Richard Henderson Cc: Paolo Bonzini Cc: Igor Mammedov Signed-off-by: David Hildenbrand --- exec.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/exec.c b/exec.c index 9d351a7492..a0a8d93fee 100644 --- a/exec.c +++ b/exec.c @@ -2069,6 +2069,21 @@ static int memory_try_enable_merging(void *addr, siz= e_t len) return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE); } =20 +static void qemu_ram_apply_settings(void *host, size_t length) +{ + memory_try_enable_merging(host, length); + qemu_ram_setup_dump(host, length); + qemu_madvise(host, length, QEMU_MADV_HUGEPAGE); + /* + * MADV_DONTFORK is also needed by KVM in absence of synchronous MMU + * Configure it unless the machine is a qtest server, in which case KV= M is + * not used and it may be forked (eg for fuzzing purposes). + */ + if (!qtest_enabled()) { + qemu_madvise(host, length, QEMU_MADV_DONTFORK); + } +} + /* * Resizing RAM while migrating can result in the migration being canceled. * Care has to be taken if the guest might have already detected the memor= y. @@ -2227,7 +2242,6 @@ static void ram_block_add(RAMBlock *new_block, Error = **errp, bool shared) qemu_mutex_unlock_ramlist(); return; } - memory_try_enable_merging(new_block->host, new_block->max_leng= th); } } =20 @@ -2265,17 +2279,7 @@ static void ram_block_add(RAMBlock *new_block, Error= **errp, bool shared) DIRTY_CLIENTS_ALL); =20 if (new_block->host) { - qemu_ram_setup_dump(new_block->host, new_block->max_length); - qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_HUG= EPAGE); - /* - * MADV_DONTFORK is also needed by KVM in absence of synchronous M= MU - * Configure it unless the machine is a qtest server, in which case - * KVM is not used and it may be forked (eg for fuzzing purposes). - */ - if (!qtest_enabled()) { - qemu_madvise(new_block->host, new_block->max_length, - QEMU_MADV_DONTFORK); - } + qemu_ram_apply_settings(new_block->host, new_block->max_length); ram_block_notify_add(new_block->host, new_block->used_length, new_block->max_length); } --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582798735; cv=none; d=zohomail.com; s=zohoarc; b=hcl0hkZroDS1RADLevaBiPvpFQTbt0efC5HfhR3ola6CyQDYos7s05dqRizQ5gBYxA/3W2HFe2i7rSocNyDosDVzAnkUU9iOQmV/yNe9HAFDPQsVs6eOiucuK1mEWzrPxR9jK5saLBpeRU6AXWI5NT/PKq/FYeJ6dLEDETS6KRk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582798735; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=LMudDymxhn5SYN8zB8osoTwCL+VNgZTY4vrznHYmidc=; b=dI2Izsa0WArhKo0hRGAuXpRijoYS0muQMbVtZRfmNkd5xOi4E5e5mPQ1oYQxnOckeNqgagsD3yHR1Tw8dGQfx+MiaSZm+WEUgUyXvaID/nGBGaPfdQEwGi34vcWl7ekC646PRSGj0ebLGj5ucHYfxgNZFgBQzMrPLQPXRExanDM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582798735390750.3252777320932; Thu, 27 Feb 2020 02:18:55 -0800 (PST) Received: from localhost ([::1]:56626 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GFp-0007qX-US for importer@patchew.org; Thu, 27 Feb 2020 05:18:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39865) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GAt-00032j-6b for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GAs-0006Iw-5n for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:47 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:55255 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GAs-0006EZ-1P for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:46 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-318-VH5RIL7ANKGMIbL5DCQQvQ-1; Thu, 27 Feb 2020 05:13:44 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DD9EC107B7DB; Thu, 27 Feb 2020 10:13:42 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2C8F96E3EE; Thu, 27 Feb 2020 10:13:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798425; 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=LMudDymxhn5SYN8zB8osoTwCL+VNgZTY4vrznHYmidc=; b=eQvAd3fOyO4PRAUpPDITfhLKEQdvlL20O6nz1ESV4nbNAp7nvN5CufJFFcbpCzSzvOGB48 EXBPFqK0IgQ/iuxMMBvPNsCxacDhCZ2aUFStRcTVnSOt8ruSbudlqXjvKKFEPk8fCinU+4 WZL6ckPITKr14abnE6wNBvVZUgNkaZQ= X-MC-Unique: VH5RIL7ANKGMIbL5DCQQvQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 05/15] exec: Reuse qemu_ram_apply_settings() in qemu_ram_remap() Date: Thu, 27 Feb 2020 11:11:55 +0100 Message-Id: <20200227101205.5616-6-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Richard Henderson , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" I don't see why we shouldn't apply all settings to make it look like the surrounding RAM (and enable proper VMA merging). Note: memory backend settings might have overridden these settings. We would need a callback to let the memory backend fix that up. Reviewed-by: Richard Henderson Reviewed-by: Peter Xu Cc: Richard Henderson Cc: Paolo Bonzini Cc: Igor Mammedov Signed-off-by: David Hildenbrand --- exec.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/exec.c b/exec.c index a0a8d93fee..1fc1fa237e 100644 --- a/exec.c +++ b/exec.c @@ -2516,8 +2516,7 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t lengt= h) length, addr); exit(1); } - memory_try_enable_merging(vaddr, length); - qemu_ram_setup_dump(vaddr, length); + qemu_ram_apply_settings(vaddr, length); } } } --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582798677; cv=none; d=zohomail.com; s=zohoarc; b=bgbscOGwrv26S+DsitwoEbsHSQduSZIkRdfSNsat9j1NljZQOGY4brSFICSB2W/Ek4bpJvyHIgY2OCnqCLqIzv9HbZOOw1zCBopHAqnS+CaReeWk7r967XGNn6bmvqDv1uou43GWsDDH4GmokwxRkYgpCt5v21G9H99C/TTwVAM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582798677; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=u46KdImnOG4gKtgSS+ImhxFoNGYibfq/noWFfJOPceI=; b=Vf5dB6Ploa4aEOd95YJWtU1BpzJqMLweLLg4exb2dhI3QktLhntPwI/UOgijYsZP87IE+rJZE7VFqBD1XLJOcdrYvdcKX41DZfWrc7oOrWlB/IHDzUiUPBpWedWoZVDLFmTVbriDmuF4IWCJjJ+GTVK60t8LiLeBB5D3+MnSY7k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 158279867779454.1865296631413; Thu, 27 Feb 2020 02:17:57 -0800 (PST) Received: from localhost ([::1]:56624 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GEs-0006w7-RI for importer@patchew.org; Thu, 27 Feb 2020 05:17:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39913) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GB0-0003BH-Iv for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GAy-0006sV-Te for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:54 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:52679 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GAy-0006mi-Lk for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:52 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-183-wqQjZaXpPRCkwSNjRp-8rw-1; Thu, 27 Feb 2020 05:13:47 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 51068800D48; Thu, 27 Feb 2020 10:13:46 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 380E59079D; Thu, 27 Feb 2020 10:13:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798432; 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=u46KdImnOG4gKtgSS+ImhxFoNGYibfq/noWFfJOPceI=; b=PyjKUCAIcLOZX7DyNppnuH1Kjg4rH+kna2RkfPSCPIoWTQK/ApX81e9mQma8u5zwEMD0yb skDZ2O+W2m+S9zoRav7W3elDyYK6N3PwOyzR9bogQMv8GGCriDuC09dxOL3sd6VWuO5Lzz hGCU17E7XEScojpjUeACvh+0VW8nerQ= X-MC-Unique: wqQjZaXpPRCkwSNjRp-8rw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 06/15] exec: Drop "shared" parameter from ram_block_add() Date: Thu, 27 Feb 2020 11:11:56 +0100 Message-Id: <20200227101205.5616-7-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Richard Henderson , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Properly store it in the flags of the ram block instead (and the flag even already exists and is used). E.g., qemu_ram_is_shared() now properly succeeds on all ram blocks that are actually shared. Reviewed-by: Igor Kotrasinski Reviewed-by: Richard Henderson Reviewed-by: Peter Xu Cc: Richard Henderson Cc: Paolo Bonzini Cc: Igor Mammedov Signed-off-by: David Hildenbrand --- exec.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/exec.c b/exec.c index 1fc1fa237e..cdf962a936 100644 --- a/exec.c +++ b/exec.c @@ -2211,7 +2211,7 @@ static void dirty_memory_extend(ram_addr_t old_ram_si= ze, } } =20 -static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared) +static void ram_block_add(RAMBlock *new_block, Error **errp) { RAMBlock *block; RAMBlock *last_block =3D NULL; @@ -2234,7 +2234,8 @@ static void ram_block_add(RAMBlock *new_block, Error = **errp, bool shared) } } else { new_block->host =3D phys_mem_alloc(new_block->max_length, - &new_block->mr->align, shared= ); + &new_block->mr->align, + qemu_ram_is_shared(new_block)= ); if (!new_block->host) { error_setg_errno(errp, errno, "cannot set up guest memory '%s'", @@ -2339,7 +2340,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, Mem= oryRegion *mr, return NULL; } =20 - ram_block_add(new_block, &local_err, ram_flags & RAM_SHARED); + ram_block_add(new_block, &local_err); if (local_err) { g_free(new_block); error_propagate(errp, local_err); @@ -2401,10 +2402,13 @@ RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, = ram_addr_t max_size, if (host) { new_block->flags |=3D RAM_PREALLOC; } + if (share) { + new_block->flags |=3D RAM_SHARED; + } if (resizeable) { new_block->flags |=3D RAM_RESIZEABLE; } - ram_block_add(new_block, &local_err, share); + ram_block_add(new_block, &local_err); if (local_err) { g_free(new_block); error_propagate(errp, local_err); --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582798746; cv=none; d=zohomail.com; s=zohoarc; b=a0Tpr0xbLrDdPH5FzEZbdyVdTnRZrrH13aHmUZ+LSf5RI29yAukXviOK2lWBQmOhNdOt865ppZ6e9MzyhaKUuraWDT7K9KLAvSdcJijEaOPZErm6q6OCv3SS5AKZhPVIA+z8hQTw8jnwijQIJMR+tlJ10L8hvcsQnKYMOh/YVf8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582798746; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=C9/Crd0sxE5CGHffwV6wijir6jtZAmFeRZbzyOdoPiE=; b=CN5bJLi37lYrbDCXKgR5PyTw2l+3yeSsUckDVGF2yfyjG9tor9oi2foh+l7sq36SVbW2gm73lxSBcmwFcmbyikH7nqkHBDntPzk792cyiDQsbPzix9JpUPgRIkhd8k4jQXOfC+YnHcV8zBzX14j1g4SiEhZIKTcsKxF7/JUX1/w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582798746806950.1194249645108; Thu, 27 Feb 2020 02:19:06 -0800 (PST) Received: from localhost ([::1]:56628 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GG1-00082h-GC for importer@patchew.org; Thu, 27 Feb 2020 05:19:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39914) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GB0-0003BI-JW for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GAz-0006tE-0Y for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:54 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:24778 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GAy-0006og-T3 for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:13:52 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-307-2Xa-M6DDMpu9wafMM75uVQ-1; Thu, 27 Feb 2020 05:13:50 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 34B2D1005514; Thu, 27 Feb 2020 10:13:49 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1C016E3EE; Thu, 27 Feb 2020 10:13:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798432; 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=C9/Crd0sxE5CGHffwV6wijir6jtZAmFeRZbzyOdoPiE=; b=cHlfD8fQgo6bB46WYAdN/8svSMd1LdnGP2LtvXmXBw7W8wi9jW2YUR01UU5EYcs/pC3h2P 7KoJjt0Vx//gNVCDVMNB8tS3W+OcnVoi9UDzfPKldj/uhrlkgpR8Wbel0uSaDCH/qruJBJ Gd77kcUGfBtuhRRRiv+vzj/Ik5i9TKA= X-MC-Unique: 2Xa-M6DDMpu9wafMM75uVQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 07/15] util/mmap-alloc: Factor out calculation of the pagesize for the guard page Date: Thu, 27 Feb 2020 11:11:57 +0100 Message-Id: <20200227101205.5616-8-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Greg Kurz , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Let's factor out calculating the size of the guard page and rename the variable to make it clearer that this pagesize only applies to the guard page. Cc: "Michael S. Tsirkin" Cc: Murilo Opsfelder Araujo Cc: Greg Kurz Cc: Eduardo Habkost Cc: "Dr. David Alan Gilbert" Cc: Igor Mammedov Signed-off-by: David Hildenbrand Reviewed-by: Peter Xu --- util/mmap-alloc.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 27dcccd8ec..f0277f9fad 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -82,17 +82,27 @@ size_t qemu_mempath_getpagesize(const char *mem_path) return qemu_real_host_page_size; } =20 +static inline size_t mmap_guard_pagesize(int fd) +{ +#if defined(__powerpc64__) && defined(__linux__) + /* Mappings in the same segment must share the same page size */ + return qemu_fd_getpagesize(fd); +#else + return qemu_real_host_page_size; +#endif +} + void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared, bool is_pmem) { + const size_t guard_pagesize =3D mmap_guard_pagesize(fd); int flags; int map_sync_flags =3D 0; int guardfd; size_t offset; - size_t pagesize; size_t total; void *guardptr; void *ptr; @@ -113,8 +123,7 @@ void *qemu_ram_mmap(int fd, * anonymous memory is OK. */ flags =3D MAP_PRIVATE; - pagesize =3D qemu_fd_getpagesize(fd); - if (fd =3D=3D -1 || pagesize =3D=3D qemu_real_host_page_size) { + if (fd =3D=3D -1 || guard_pagesize =3D=3D qemu_real_host_page_size) { guardfd =3D -1; flags |=3D MAP_ANONYMOUS; } else { @@ -123,7 +132,6 @@ void *qemu_ram_mmap(int fd, } #else guardfd =3D -1; - pagesize =3D qemu_real_host_page_size; flags =3D MAP_PRIVATE | MAP_ANONYMOUS; #endif =20 @@ -135,7 +143,7 @@ void *qemu_ram_mmap(int fd, =20 assert(is_power_of_2(align)); /* Always align to host page size */ - assert(align >=3D pagesize); + assert(align >=3D guard_pagesize); =20 flags =3D MAP_FIXED; flags |=3D fd =3D=3D -1 ? MAP_ANONYMOUS : 0; @@ -189,8 +197,8 @@ void *qemu_ram_mmap(int fd, * a guard page guarding against potential buffer overflows. */ total -=3D offset; - if (total > size + pagesize) { - munmap(ptr + size + pagesize, total - size - pagesize); + if (total > size + guard_pagesize) { + munmap(ptr + size + guard_pagesize, total - size - guard_pagesize); } =20 return ptr; @@ -198,15 +206,8 @@ void *qemu_ram_mmap(int fd, =20 void qemu_ram_munmap(int fd, void *ptr, size_t size) { - size_t pagesize; - if (ptr) { /* Unmap both the RAM block and the guard page */ -#if defined(__powerpc64__) && defined(__linux__) - pagesize =3D qemu_fd_getpagesize(fd); -#else - pagesize =3D qemu_real_host_page_size; -#endif - munmap(ptr, size + pagesize); + munmap(ptr, size + mmap_guard_pagesize(fd)); } } --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582798907; cv=none; d=zohomail.com; s=zohoarc; b=NnG6rqylju3XNeCla/15q3XPKp0263XVSCOmPauz4DcekSZfeB8FQ1yE/oi3szXhxD+9rV59FdKgIV9dBg/2VJtNJb262Da4+E2wm5wJhdtHF5U9e/duSVcngCoHTdAx9vNTw6wD7FWOtWMXVxVtzAV5w1uF+tvNHo8L2KdTl7U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582798907; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0M+moTdl0qV7+Qhg/JRf45CFzQnG475adDlEdMRPHAI=; b=jYB/Wo0x7L5Qq1CT9faNWztz4iqheLsxvBxirtH8IJdQ+7yfL+mTEIwpqH1dR+KkhofThJFA0Cfhnw6wHOpctHMUt+/CNb0Y+kJq8vUizMkdat7bYPlTaHLEeRCCE5s7xKLdDW462Jeecck5a8Bh5ZaBDid9ir8mO2iqUDCTaRY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582798907573107.9548376254653; Thu, 27 Feb 2020 02:21:47 -0800 (PST) Received: from localhost ([::1]:56658 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GIc-0002Jo-BB for importer@patchew.org; Thu, 27 Feb 2020 05:21:46 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40010) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GBF-0003VA-ML for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GBE-0007wn-Gs for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:09 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:26460 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GBE-0007p0-CS for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:08 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-404-atXLREa4MFGqPwq_dOlFQQ-1; Thu, 27 Feb 2020 05:14:02 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3F27D800D53; Thu, 27 Feb 2020 10:14:01 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82A6E6E3EE; Thu, 27 Feb 2020 10:13:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798446; 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=0M+moTdl0qV7+Qhg/JRf45CFzQnG475adDlEdMRPHAI=; b=YZnzfpOgTLLGcaJdyHBdepLDuoC9YVD6rayMsIh+FoU5H9KQNQeCIU1CmcI0iiPUTVxqC2 XIT8Y6oU80TLIU0Cbq9HObYHH1ecR8zXPFjmZf95T85IhJi1wKHQ4TNf2B9pfuNW0+RNGU nAPaqsuvA87imACWnt9bfUNvkfSIaGE= X-MC-Unique: atXLREa4MFGqPwq_dOlFQQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 08/15] util/mmap-alloc: Factor out reserving of a memory region to mmap_reserve() Date: Thu, 27 Feb 2020 11:11:58 +0100 Message-Id: <20200227101205.5616-9-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Richard Henderson , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Greg Kurz , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We want to reserve a memory region without actually populating memory. Let's factor that out. Reviewed-by: Igor Kotrasinski Acked-by: Murilo Opsfelder Araujo Reviewed-by: Richard Henderson Reviewed-by: Peter Xu Cc: "Michael S. Tsirkin" Cc: Greg Kurz Cc: Murilo Opsfelder Araujo Cc: Eduardo Habkost Cc: "Dr. David Alan Gilbert" Cc: Igor Mammedov Signed-off-by: David Hildenbrand --- util/mmap-alloc.c | 58 +++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index f0277f9fad..9e9534a07e 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -82,6 +82,38 @@ size_t qemu_mempath_getpagesize(const char *mem_path) return qemu_real_host_page_size; } =20 +/* + * Reserve a new memory region of the requested size to be used for mapping + * from the given fd (if any). + */ +static void *mmap_reserve(size_t size, int fd) +{ + int flags =3D MAP_PRIVATE; + +#if defined(__powerpc64__) && defined(__linux__) + /* + * On ppc64 mappings in the same segment (aka slice) must share the sa= me + * page size. Since we will be re-allocating part of this segment + * from the supplied fd, we should make sure to use the same page size= , to + * this end we mmap the supplied fd. In this case, set MAP_NORESERVE = to + * avoid allocating backing store memory. + * We do this unless we are using the system page size, in which case + * anonymous memory is OK. + */ + if (fd =3D=3D -1 || qemu_fd_getpagesize(fd) =3D=3D qemu_real_host_page= _size) { + fd =3D -1; + flags |=3D MAP_ANONYMOUS; + } else { + flags |=3D MAP_NORESERVE; + } +#else + fd =3D -1; + flags |=3D MAP_ANONYMOUS; +#endif + + return mmap(0, size, PROT_NONE, flags, fd, 0); +} + static inline size_t mmap_guard_pagesize(int fd) { #if defined(__powerpc64__) && defined(__linux__) @@ -101,7 +133,6 @@ void *qemu_ram_mmap(int fd, const size_t guard_pagesize =3D mmap_guard_pagesize(fd); int flags; int map_sync_flags =3D 0; - int guardfd; size_t offset; size_t total; void *guardptr; @@ -113,30 +144,7 @@ void *qemu_ram_mmap(int fd, */ total =3D size + align; =20 -#if defined(__powerpc64__) && defined(__linux__) - /* On ppc64 mappings in the same segment (aka slice) must share the sa= me - * page size. Since we will be re-allocating part of this segment - * from the supplied fd, we should make sure to use the same page size= , to - * this end we mmap the supplied fd. In this case, set MAP_NORESERVE = to - * avoid allocating backing store memory. - * We do this unless we are using the system page size, in which case - * anonymous memory is OK. - */ - flags =3D MAP_PRIVATE; - if (fd =3D=3D -1 || guard_pagesize =3D=3D qemu_real_host_page_size) { - guardfd =3D -1; - flags |=3D MAP_ANONYMOUS; - } else { - guardfd =3D fd; - flags |=3D MAP_NORESERVE; - } -#else - guardfd =3D -1; - flags =3D MAP_PRIVATE | MAP_ANONYMOUS; -#endif - - guardptr =3D mmap(0, total, PROT_NONE, flags, guardfd, 0); - + guardptr =3D mmap_reserve(total, fd); if (guardptr =3D=3D MAP_FAILED) { return MAP_FAILED; } --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582798830; cv=none; d=zohomail.com; s=zohoarc; b=gIG4ATGZ5vlMpM20uveFctCmgx4954ihgv3irdnTrGBWf3B6EXfHgpdNLqPzK+xWF1z3WwzVuqN54mFg78PPv5TRHarIn9rEvhpoa8ZQkiMCZ5a8fGewdkHRQxKTF8DVPbNfizCqb/zKX06qy8ULD46IF3PlutMHp+swiLzI61U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582798830; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=m/8TwOF2Sia+Gjod6JfWjCPGtrFF4CPZ36Jyrg9pzx0=; b=TmpkIzfzzqlIfDQ10Go5AJd32B5rGQgkZFcXb1wLxJthPTFAoekwd6F9picewwPvLiQfjvjLSnF2bz/0/drzMiMc/UcBh2sxt1PgoFR86XM8bYnF/BJMr0mc68g7UPR29RNpceBfLgp78K+RSxWGhI5HEkV2Gb7RNPADYfeaKeU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582798830395558.8465482277601; Thu, 27 Feb 2020 02:20:30 -0800 (PST) Received: from localhost ([::1]:56642 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GHJ-00019D-Qh for importer@patchew.org; Thu, 27 Feb 2020 05:20:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40045) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GBI-0003YR-89 for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GBG-00088R-Tt for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:12 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:51420 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GBG-00086O-PS for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:10 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-143--T1rH1jlM2GgK7K_NigEhQ-1; Thu, 27 Feb 2020 05:14:08 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 61AA718A5507; Thu, 27 Feb 2020 10:14:07 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9239E6E3EE; Thu, 27 Feb 2020 10:14:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798450; 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=m/8TwOF2Sia+Gjod6JfWjCPGtrFF4CPZ36Jyrg9pzx0=; b=jQfpf1pEW1R/UVhPm2z5kf52Mi3fxvVnOYnLQ4ZICaolmzjDCDn0YsdcymkfpQYFXiSxMb hd3bSN4HyMG4Ae5mYjqyFJCZa2GaUJfCPl7/DKfyKF8E3Y2IF6gtvd8vWcaPogUXtbNQBI 1t9aupUu6nJrXvLGrCSD6erZZgwf10w= X-MC-Unique: -T1rH1jlM2GgK7K_NigEhQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 09/15] util/mmap-alloc: Factor out populating of memory to mmap_populate() Date: Thu, 27 Feb 2020 11:11:59 +0100 Message-Id: <20200227101205.5616-10-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Richard Henderson , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Greg Kurz , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We want to populate memory within a reserved memory region. Let's factor that out. Reviewed-by: Richard Henderson Acked-by: Murilo Opsfelder Araujo Reviewed-by: Peter Xu Cc: Igor Kotrasinski Cc: "Michael S. Tsirkin" Cc: Greg Kurz Cc: Murilo Opsfelder Araujo Cc: Eduardo Habkost Cc: "Dr. David Alan Gilbert" Cc: Igor Mammedov Signed-off-by: David Hildenbrand --- util/mmap-alloc.c | 89 +++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 9e9534a07e..73179f36d6 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -114,6 +114,50 @@ static void *mmap_reserve(size_t size, int fd) return mmap(0, size, PROT_NONE, flags, fd, 0); } =20 +/* + * Populate memory in a reserved region from the given fd (if any). + */ +static void *mmap_populate(void *ptr, size_t size, int fd, bool shared, + bool is_pmem) +{ + int map_sync_flags =3D 0; + int flags =3D MAP_FIXED; + void *populated_ptr; + + flags |=3D fd =3D=3D -1 ? MAP_ANONYMOUS : 0; + flags |=3D shared ? MAP_SHARED : MAP_PRIVATE; + if (shared && is_pmem) { + map_sync_flags =3D MAP_SYNC | MAP_SHARED_VALIDATE; + } + + populated_ptr =3D mmap(ptr, size, PROT_READ | PROT_WRITE, + flags | map_sync_flags, fd, 0); + if (populated_ptr =3D=3D MAP_FAILED && map_sync_flags) { + if (errno =3D=3D ENOTSUP) { + char *proc_link =3D g_strdup_printf("/proc/self/fd/%d", fd); + char *file_name =3D g_malloc0(PATH_MAX); + int len =3D readlink(proc_link, file_name, PATH_MAX - 1); + + if (len < 0) { + len =3D 0; + } + file_name[len] =3D '\0'; + fprintf(stderr, "Warning: requesting persistence across crashe= s " + "for backend file %s failed. Proceeding without " + "persistence, data might become corrupted in case of h= ost " + "crash.\n", file_name); + g_free(proc_link); + g_free(file_name); + } + /* + * If mmap failed with MAP_SHARED_VALIDATE | MAP_SYNC, we will try + * again without these flags to handle backwards compatibility. + */ + populated_ptr =3D mmap(ptr, size, PROT_READ | PROT_WRITE, flags, f= d, 0); + } + return populated_ptr; +} + static inline size_t mmap_guard_pagesize(int fd) { #if defined(__powerpc64__) && defined(__linux__) @@ -131,12 +175,8 @@ void *qemu_ram_mmap(int fd, bool is_pmem) { const size_t guard_pagesize =3D mmap_guard_pagesize(fd); - int flags; - int map_sync_flags =3D 0; - size_t offset; - size_t total; - void *guardptr; - void *ptr; + size_t offset, total; + void *ptr, *guardptr; =20 /* * Note: this always allocates at least one extra page of virtual addr= ess @@ -153,44 +193,9 @@ void *qemu_ram_mmap(int fd, /* Always align to host page size */ assert(align >=3D guard_pagesize); =20 - flags =3D MAP_FIXED; - flags |=3D fd =3D=3D -1 ? MAP_ANONYMOUS : 0; - flags |=3D shared ? MAP_SHARED : MAP_PRIVATE; - if (shared && is_pmem) { - map_sync_flags =3D MAP_SYNC | MAP_SHARED_VALIDATE; - } - offset =3D QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guar= dptr; =20 - ptr =3D mmap(guardptr + offset, size, PROT_READ | PROT_WRITE, - flags | map_sync_flags, fd, 0); - - if (ptr =3D=3D MAP_FAILED && map_sync_flags) { - if (errno =3D=3D ENOTSUP) { - char *proc_link, *file_name; - int len; - proc_link =3D g_strdup_printf("/proc/self/fd/%d", fd); - file_name =3D g_malloc0(PATH_MAX); - len =3D readlink(proc_link, file_name, PATH_MAX - 1); - if (len < 0) { - len =3D 0; - } - file_name[len] =3D '\0'; - fprintf(stderr, "Warning: requesting persistence across crashe= s " - "for backend file %s failed. Proceeding without " - "persistence, data might become corrupted in case of h= ost " - "crash.\n", file_name); - g_free(proc_link); - g_free(file_name); - } - /* - * if map failed with MAP_SHARED_VALIDATE | MAP_SYNC, - * we will remove these flags to handle compatibility. - */ - ptr =3D mmap(guardptr + offset, size, PROT_READ | PROT_WRITE, - flags, fd, 0); - } - + ptr =3D mmap_populate(guardptr + offset, size, fd, shared, is_pmem); if (ptr =3D=3D MAP_FAILED) { munmap(guardptr, total); return MAP_FAILED; --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582799024; cv=none; d=zohomail.com; s=zohoarc; b=QU3HdAcDVKoa1xdjLZyA/KN1ZMmH4B+QaUzQqji2WCG/lzjQZfjs5zqcCV/BtZLm6ME8iL2C+hDiew5w37enijb8/Sv4DABGWwN3lIXk9Qs4rAQOR94C8gatVyIXaEiQnmDy7LWBZiSnMlF5Zcr6eBFla6HjPy2rM54j6+vJG5I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582799024; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4mZwnvyZhERJne67+bQOssfXKczt9eVEF1mwFB9tgoM=; b=ESHbnzGJ2LCMG2IbVhFYZvGkyz4tc9xp6zvuYQBwOrgqS0nDJ1Ip+86ZMu9MX6lSA5QAE/sV0x5FOnRlcu+HJTKZfxWRO5qtBKv1KISCPRe+HUmkohlkpOt+XdE8+Y9tUBB7Wi8vWJZvdmQKwrQp1iGz20L5g831z93KHFvJfpw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 158279902469688.56028482781494; Thu, 27 Feb 2020 02:23:44 -0800 (PST) Received: from localhost ([::1]:56682 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GKV-0004FQ-5o for importer@patchew.org; Thu, 27 Feb 2020 05:23:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40176) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GBW-0003qG-Cf for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GBT-0000VR-TW for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:26 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:23104 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GBT-0000Su-P7 for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:23 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-244-pNJ7hmV9Mcae-qUqDXsgjQ-1; Thu, 27 Feb 2020 05:14:22 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AF9DD107B7D7; Thu, 27 Feb 2020 10:14:20 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 60B916E3EE; Thu, 27 Feb 2020 10:14:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798463; 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=4mZwnvyZhERJne67+bQOssfXKczt9eVEF1mwFB9tgoM=; b=gYOH2ziz+UPzecHZgJuepa/tcHp/QfIVmWn91CBn/L/u3bbbM75YaeAOMcEDUXEiJ4utHi 0nTPl09cQfngCig5VhzDKLXW4RsyB+F6V7CBNPu93BC1FA7wXK400/Bq5y/ES+b+cQkk5q cirJbgcmqmvUQCWFXDf6FQaUYIhfRlw= X-MC-Unique: pNJ7hmV9Mcae-qUqDXsgjQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 10/15] util/mmap-alloc: Prepare for resizeable mmaps Date: Thu, 27 Feb 2020 11:12:00 +0100 Message-Id: <20200227101205.5616-11-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Richard Henderson , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Greg Kurz , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When shrinking a mmap we want to re-reserve the already populated area. When growing a memory region, we want to populate starting with a given fd_offset. Prepare by allowing to pass these parameters. Also, let's make sure we always process full pages, to avoid unmapping/remapping pages that are already in use when growing/shrinking. Add some asserts. Reviewed-by: Richard Henderson Reviewed-by: Peter Xu Cc: Igor Kotrasinski Cc: Murilo Opsfelder Araujo Cc: "Michael S. Tsirkin" Cc: Greg Kurz Cc: Murilo Opsfelder Araujo Cc: Eduardo Habkost Cc: "Dr. David Alan Gilbert" Cc: Igor Mammedov Signed-off-by: David Hildenbrand --- util/mmap-alloc.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 73179f36d6..c6a6075215 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -83,12 +83,12 @@ size_t qemu_mempath_getpagesize(const char *mem_path) } =20 /* - * Reserve a new memory region of the requested size to be used for mapping - * from the given fd (if any). + * Reserve a new memory region of the requested size or re-reserve parts + * of an existing region to be used for mapping from the given fd (if any). */ -static void *mmap_reserve(size_t size, int fd) +static void *mmap_reserve(void *ptr, size_t size, int fd) { - int flags =3D MAP_PRIVATE; + int flags =3D MAP_PRIVATE | (ptr ? MAP_FIXED : 0); =20 #if defined(__powerpc64__) && defined(__linux__) /* @@ -111,19 +111,23 @@ static void *mmap_reserve(size_t size, int fd) flags |=3D MAP_ANONYMOUS; #endif =20 - return mmap(0, size, PROT_NONE, flags, fd, 0); + return mmap(ptr, size, PROT_NONE, flags, fd, 0); } =20 /* * Populate memory in a reserved region from the given fd (if any). */ -static void *mmap_populate(void *ptr, size_t size, int fd, bool shared, - bool is_pmem) +static void *mmap_populate(void *ptr, size_t size, int fd, size_t fd_offse= t, + bool shared, bool is_pmem) { int map_sync_flags =3D 0; int flags =3D MAP_FIXED; void *populated_ptr; =20 + if (fd =3D=3D -1) { + fd_offset =3D 0; + } + flags |=3D fd =3D=3D -1 ? MAP_ANONYMOUS : 0; flags |=3D shared ? MAP_SHARED : MAP_PRIVATE; if (shared && is_pmem) { @@ -131,7 +135,7 @@ static void *mmap_populate(void *ptr, size_t size, int = fd, bool shared, } =20 populated_ptr =3D mmap(ptr, size, PROT_READ | PROT_WRITE, - flags | map_sync_flags, fd, 0); + flags | map_sync_flags, fd, fd_offset); if (populated_ptr =3D=3D MAP_FAILED && map_sync_flags) { if (errno =3D=3D ENOTSUP) { char *proc_link =3D g_strdup_printf("/proc/self/fd/%d", fd); @@ -153,7 +157,8 @@ static void *mmap_populate(void *ptr, size_t size, int = fd, bool shared, * If mmap failed with MAP_SHARED_VALIDATE | MAP_SYNC, we will try * again without these flags to handle backwards compatibility. */ - populated_ptr =3D mmap(ptr, size, PROT_READ | PROT_WRITE, flags, f= d, 0); + populated_ptr =3D mmap(ptr, size, PROT_READ | PROT_WRITE, flags, f= d, + fd_offset); } return populated_ptr; } @@ -175,16 +180,19 @@ void *qemu_ram_mmap(int fd, bool is_pmem) { const size_t guard_pagesize =3D mmap_guard_pagesize(fd); + const size_t pagesize =3D qemu_fd_getpagesize(fd); size_t offset, total; void *ptr, *guardptr; =20 + g_assert(QEMU_IS_ALIGNED(size, pagesize)); + /* * Note: this always allocates at least one extra page of virtual addr= ess * space, even if size is already aligned. */ total =3D size + align; =20 - guardptr =3D mmap_reserve(total, fd); + guardptr =3D mmap_reserve(NULL, total, fd); if (guardptr =3D=3D MAP_FAILED) { return MAP_FAILED; } @@ -195,7 +203,7 @@ void *qemu_ram_mmap(int fd, =20 offset =3D QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guar= dptr; =20 - ptr =3D mmap_populate(guardptr + offset, size, fd, shared, is_pmem); + ptr =3D mmap_populate(guardptr + offset, size, fd, 0, shared, is_pmem); if (ptr =3D=3D MAP_FAILED) { munmap(guardptr, total); return MAP_FAILED; @@ -219,6 +227,10 @@ void *qemu_ram_mmap(int fd, =20 void qemu_ram_munmap(int fd, void *ptr, size_t size) { + const size_t pagesize =3D qemu_fd_getpagesize(fd); + + g_assert(QEMU_IS_ALIGNED(size, pagesize)); + if (ptr) { /* Unmap both the RAM block and the guard page */ munmap(ptr, size + mmap_guard_pagesize(fd)); --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582798904; cv=none; d=zohomail.com; s=zohoarc; b=j+PAN3ymYiyaxMV7zKUJlyv8iiCoO9vQ8zMO2cImUSFy2LvEroBYcWlnSk34DmitBbakNRdAMu9WSStLMJwn+oOeec4hU6j+BstMqjBYVS7JpRkRL5tN0IWYKov833hWuHQievFXLZH7EtaZXrVaAgc6exRaBgJGYumjHZTdaEo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582798904; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=40hhBbONGQBxpq10wEMGQ4Cqgszu2Aqt/JdYW098rXc=; b=An/WTVFM8YUSSWGZakgpStYCmJAP1bLZOtMMYpXYRqA0sJ7CpcfN7Gysn7tFkGIKThbHURZef1kY84gvn22ge4AExABGRX5xciEGvTZssmQUCjmj4quZZMZO+Nl+s3yTwO3UdanYtrQwfgOCbbPWlDbkcnXl02fMuXZmtTJwa2Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 158279890433571.32335086798992; Thu, 27 Feb 2020 02:21:44 -0800 (PST) Received: from localhost ([::1]:56656 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GIZ-0002FR-4c for importer@patchew.org; Thu, 27 Feb 2020 05:21:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40226) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GBZ-0003uo-JN for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GBY-0000h9-3V for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:29 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:27948 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GBX-0000e9-Tm for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:28 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-397-awGq2DrlMqeDQDbP8-q4Ag-1; Thu, 27 Feb 2020 05:14:25 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C03398017CC; Thu, 27 Feb 2020 10:14:23 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0AA9F90CCB; Thu, 27 Feb 2020 10:14:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798466; 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=40hhBbONGQBxpq10wEMGQ4Cqgszu2Aqt/JdYW098rXc=; b=WpulvFU+xgNbOoQYQ0UPlLf1vvCrXt1zAQ+8hlcw6hRFe3vh42dndUZMn4xnsi5ZIC61KU o6t543Q4zuXqBrTRLS5P3MziJ71HoGC8yHf1eNxaWGo9deLaUiPxFZGmIWGOq6wc6kJr0Y hf8og3cn5dqn1/SLxAfZapuGnjss/X8= X-MC-Unique: awGq2DrlMqeDQDbP8-q4Ag-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 11/15] util/mmap-alloc: Implement resizeable mmaps Date: Thu, 27 Feb 2020 11:12:01 +0100 Message-Id: <20200227101205.5616-12-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Richard Henderson , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Greg Kurz , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Implement resizeable mmaps. For now, the actual resizing is not wired up. Introduce qemu_ram_mmap_resizeable() and qemu_ram_mmap_resize(). Make qemu_ram_mmap() a wrapper of qemu_ram_mmap_resizeable(). Cc: Richard Henderson Cc: Igor Kotrasinski Cc: Murilo Opsfelder Araujo Cc: "Michael S. Tsirkin" Cc: Greg Kurz Cc: Eduardo Habkost Cc: "Dr. David Alan Gilbert" Cc: Igor Mammedov Signed-off-by: David Hildenbrand Reviewed-by: Peter Xu --- include/qemu/mmap-alloc.h | 21 +++++++++++-------- util/mmap-alloc.c | 43 ++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h index e786266b92..ca8f7edf70 100644 --- a/include/qemu/mmap-alloc.h +++ b/include/qemu/mmap-alloc.h @@ -7,11 +7,13 @@ size_t qemu_fd_getpagesize(int fd); size_t qemu_mempath_getpagesize(const char *mem_path); =20 /** - * qemu_ram_mmap: mmap the specified file or device. + * qemu_ram_mmap_resizeable: reserve a memory region of @max_size to mmap = the + * specified file or device and mmap @size of it. * * Parameters: * @fd: the file or the device to mmap * @size: the number of bytes to be mmaped + * @max_size: the number of bytes to be reserved * @align: if not zero, specify the alignment of the starting mapping add= ress; * otherwise, the alignment in use will be determined by QEMU. * @shared: map has RAM_SHARED flag. @@ -21,12 +23,15 @@ size_t qemu_mempath_getpagesize(const char *mem_path); * On success, return a pointer to the mapped area. * On failure, return MAP_FAILED. */ -void *qemu_ram_mmap(int fd, - size_t size, - size_t align, - bool shared, - bool is_pmem); - -void qemu_ram_munmap(int fd, void *ptr, size_t size); +void *qemu_ram_mmap_resizeable(int fd, size_t size, size_t max_size, + size_t align, bool shared, bool is_pmem); +bool qemu_ram_mmap_resize(void *ptr, int fd, size_t old_size, size_t new_s= ize, + bool shared, bool is_pmem); +static inline void *qemu_ram_mmap(int fd, size_t size, size_t align, + bool shared, bool is_pmem) +{ + return qemu_ram_mmap_resizeable(fd, size, size, align, shared, is_pmem= ); +} +void qemu_ram_munmap(int fd, void *ptr, size_t max_size); =20 #endif diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index c6a6075215..8f9c533f32 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -173,11 +173,8 @@ static inline size_t mmap_guard_pagesize(int fd) #endif } =20 -void *qemu_ram_mmap(int fd, - size_t size, - size_t align, - bool shared, - bool is_pmem) +void *qemu_ram_mmap_resizeable(int fd, size_t size, size_t max_size, + size_t align, bool shared, bool is_pmem) { const size_t guard_pagesize =3D mmap_guard_pagesize(fd); const size_t pagesize =3D qemu_fd_getpagesize(fd); @@ -185,12 +182,14 @@ void *qemu_ram_mmap(int fd, void *ptr, *guardptr; =20 g_assert(QEMU_IS_ALIGNED(size, pagesize)); + g_assert(QEMU_IS_ALIGNED(max_size, pagesize)); =20 /* * Note: this always allocates at least one extra page of virtual addr= ess - * space, even if size is already aligned. + * space, even if the size is already aligned. We will reserve an area= of + * at least max_size, but only populate the requested part of it. */ - total =3D size + align; + total =3D max_size + align; =20 guardptr =3D mmap_reserve(NULL, total, fd); if (guardptr =3D=3D MAP_FAILED) { @@ -218,21 +217,41 @@ void *qemu_ram_mmap(int fd, * a guard page guarding against potential buffer overflows. */ total -=3D offset; - if (total > size + guard_pagesize) { - munmap(ptr + size + guard_pagesize, total - size - guard_pagesize); + if (total > max_size + guard_pagesize) { + munmap(ptr + max_size + guard_pagesize, + total - max_size - guard_pagesize); } =20 return ptr; } =20 -void qemu_ram_munmap(int fd, void *ptr, size_t size) +bool qemu_ram_mmap_resize(void *ptr, int fd, size_t old_size, size_t new_s= ize, + bool shared, bool is_pmem) { const size_t pagesize =3D qemu_fd_getpagesize(fd); =20 - g_assert(QEMU_IS_ALIGNED(size, pagesize)); + g_assert(QEMU_IS_ALIGNED(old_size, pagesize)); + g_assert(QEMU_IS_ALIGNED(new_size, pagesize)); + + if (old_size < new_size) { + /* populate the missing piece into the reserved area */ + ptr =3D mmap_populate(ptr + old_size, new_size - old_size, fd, old= _size, + shared, is_pmem); + } else if (old_size > new_size) { + /* discard this piece, marking it reserved */ + ptr =3D mmap_reserve(ptr + new_size, old_size - new_size, fd); + } + return ptr !=3D MAP_FAILED; +} + +void qemu_ram_munmap(int fd, void *ptr, size_t max_size) +{ + const size_t pagesize =3D qemu_fd_getpagesize(fd); + + g_assert(QEMU_IS_ALIGNED(max_size, pagesize)); =20 if (ptr) { /* Unmap both the RAM block and the guard page */ - munmap(ptr, size + mmap_guard_pagesize(fd)); + munmap(ptr, max_size + mmap_guard_pagesize(fd)); } } --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582799160; cv=none; d=zohomail.com; s=zohoarc; b=R5KgUSYWbDRXoHFL384GONroU8JWvQcrXXkfjmy/ML8hZTKPM0K9Ygm7232eYlAr6IxYey5GJl2rwXj1wYa2YKtepMUVquXoK4NCC59NW5DzMl/C4j7K7wfqC7LcWgdiV+YifOU0Qh4xJCQQct+zl5DE1lNu58EpSA5WrKqdPXs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582799160; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=KMDvJ5mlpWKgYA3FKjQpwt+bcTDd3WLHazG7j+Anq/4=; b=VuvHSF2DkBb/OXjVXA0FY49YCuwhJIUYfvu5523utrexr1i6xdOgYS8HEUL9fELWDivYUD0jpnW5810MFNFUG2zCx3g/ToRk5A+knv7JaSSCxiaB8Z3/B5uQ0rF+QtgrisstgpEIMB6Y4NEhfDcid3tWmGzae58euUX2LKEsQZs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582799160174884.4752696820743; Thu, 27 Feb 2020 02:26:00 -0800 (PST) Received: from localhost ([::1]:56726 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GMe-0007ka-Tr for importer@patchew.org; Thu, 27 Feb 2020 05:25:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40323) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GBi-00041X-IE for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GBh-000103-20 for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:38 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:48061 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GBg-0000z7-Tj for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:36 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-133-pSBdSpkOMNKmI22A55bnZw-1; Thu, 27 Feb 2020 05:14:35 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9AAC7DBA3; Thu, 27 Feb 2020 10:14:33 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id AC8D987B08; Thu, 27 Feb 2020 10:14:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798476; 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=KMDvJ5mlpWKgYA3FKjQpwt+bcTDd3WLHazG7j+Anq/4=; b=ZbdLKDsgYoH+PVAA77/TGkGaolCIRCZGn44eJKrNyLs3/VTLHPDrkRcMD7LoSLh2Zk6rnq 2bKgBVIA4f8hR1/jpxlF6Ib83KbOhKfVMNoYjmOI1P2f12vmkIjWEucyqEQ/nIJoXDBrIA U+X4Q6SIrO4xT2fycfJMGRd4ieHTbkY= X-MC-Unique: pSBdSpkOMNKmI22A55bnZw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 12/15] util: vfio-helpers: Implement ram_block_resized() Date: Thu, 27 Feb 2020 11:12:02 +0100 Message-Id: <20200227101205.5616-13-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Alex Williamson , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Stefan Hajnoczi , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Let's implement ram_block_resized(), allowing resizeable mappings. For resizeable mappings, we reserve $max_size IOVA address space, but only map $size of it. When resizing, unmap the old part and remap the new part. We'll need a new ioctl to do this atomically (e.g., to resize while the guest is running - not allowed for now). Cc: Richard Henderson Cc: Paolo Bonzini Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Alex Williamson Cc: Stefan Hajnoczi Cc: "Dr. David Alan Gilbert" Signed-off-by: David Hildenbrand Reviewed-by: Peter Xu --- util/trace-events | 5 ++-- util/vfio-helpers.c | 70 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/util/trace-events b/util/trace-events index 83b6639018..88b7dbf4a5 100644 --- a/util/trace-events +++ b/util/trace-events @@ -74,8 +74,9 @@ qemu_mutex_unlock(void *mutex, const char *file, const in= t line) "released mutex =20 # vfio-helpers.c qemu_vfio_dma_reset_temporary(void *s) "s %p" -qemu_vfio_ram_block_added(void *s, void *p, size_t size) "s %p host %p siz= e 0x%zx" -qemu_vfio_ram_block_removed(void *s, void *p, size_t size) "s %p host %p s= ize 0x%zx" +qemu_vfio_ram_block_added(void *s, void *p, size_t size, size_t max_size) = "s %p host %p size 0x%zx max_size 0x%zx" +qemu_vfio_ram_block_removed(void *s, void *p, size_t size, size_t max_size= ) "s %p host %p size 0x%zx max_size 0x%zx" +qemu_vfio_ram_block_resized(void *s, void *p, size_t old_size, size_t new_= sizze) "s %p host %p old_size 0x%zx new_size 0x%zx" qemu_vfio_find_mapping(void *s, void *p) "s %p host %p" qemu_vfio_new_mapping(void *s, void *host, size_t size, int index, uint64_= t iova) "s %p host %p size %zu index %d iova 0x%"PRIx64 qemu_vfio_do_mapping(void *s, void *host, size_t size, uint64_t iova) "s %= p host %p size %zu iova 0x%"PRIx64 diff --git a/util/vfio-helpers.c b/util/vfio-helpers.c index f0c77f0d69..566eb4e1eb 100644 --- a/util/vfio-helpers.c +++ b/util/vfio-helpers.c @@ -372,14 +372,20 @@ fail_container: return ret; } =20 +static int qemu_vfio_dma_map_resizeable(QEMUVFIOState *s, void *host, + size_t size, size_t max_size, + bool temporary, uint64_t *iova); +static void qemu_vfio_dma_map_resize(QEMUVFIOState *s, void *host, + size_t old_size, size_t new_size); + static void qemu_vfio_ram_block_added(RAMBlockNotifier *n, void *host, size_t size, size_t max_size) { QEMUVFIOState *s =3D container_of(n, QEMUVFIOState, ram_notifier); int ret; =20 - trace_qemu_vfio_ram_block_added(s, host, max_size); - ret =3D qemu_vfio_dma_map(s, host, max_size, false, NULL); + trace_qemu_vfio_ram_block_added(s, host, size, max_size); + ret =3D qemu_vfio_dma_map_resizeable(s, host, size, max_size, false, N= ULL); if (ret) { error_report("qemu_vfio_dma_map(%p, %zu) failed: %s", host, max_si= ze, strerror(-ret)); @@ -391,16 +397,28 @@ static void qemu_vfio_ram_block_removed(RAMBlockNotif= ier *n, void *host, { QEMUVFIOState *s =3D container_of(n, QEMUVFIOState, ram_notifier); if (host) { - trace_qemu_vfio_ram_block_removed(s, host, max_size); + trace_qemu_vfio_ram_block_removed(s, host, size, max_size); qemu_vfio_dma_unmap(s, host); } } =20 +static void qemu_vfio_ram_block_resized(RAMBlockNotifier *n, void *host, + size_t old_size, size_t new_size) +{ + QEMUVFIOState *s =3D container_of(n, QEMUVFIOState, ram_notifier); + + if (host) { + trace_qemu_vfio_ram_block_resized(s, host, old_size, new_size); + qemu_vfio_dma_map_resize(s, host, old_size, new_size); + } +} + static void qemu_vfio_open_common(QEMUVFIOState *s) { qemu_mutex_init(&s->lock); s->ram_notifier.ram_block_added =3D qemu_vfio_ram_block_added; s->ram_notifier.ram_block_removed =3D qemu_vfio_ram_block_removed; + s->ram_notifier.ram_block_resized =3D qemu_vfio_ram_block_resized; s->low_water_mark =3D QEMU_VFIO_IOVA_MIN; s->high_water_mark =3D QEMU_VFIO_IOVA_MAX; ram_block_notifier_add(&s->ram_notifier); @@ -597,9 +615,14 @@ static bool qemu_vfio_verify_mappings(QEMUVFIOState *s) * the result in @iova if not NULL. The caller need to make sure the area = is * aligned to page size, and mustn't overlap with existing mapping areas (= split * mapping status within this area is not allowed). + * + * If size < max_size, a region of max_size in IOVA address is reserved, s= uch + * that the mapping can later be resized. Resizeable mappings are only all= owed + * for !temporary mappings. */ -int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, size_t size, - bool temporary, uint64_t *iova) +static int qemu_vfio_dma_map_resizeable(QEMUVFIOState *s, void *host, + size_t size, size_t max_size, + bool temporary, uint64_t *iova) { int ret =3D 0; int index; @@ -608,13 +631,17 @@ int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, s= ize_t size, =20 assert(QEMU_PTR_IS_ALIGNED(host, qemu_real_host_page_size)); assert(QEMU_IS_ALIGNED(size, qemu_real_host_page_size)); + assert(QEMU_IS_ALIGNED(max_size, qemu_real_host_page_size)); + assert(size =3D=3D max_size || !temporary); + assert(size <=3D max_size); + trace_qemu_vfio_dma_map(s, host, size, temporary, iova); qemu_mutex_lock(&s->lock); mapping =3D qemu_vfio_find_mapping(s, host, &index); if (mapping) { iova0 =3D mapping->iova + ((uint8_t *)host - (uint8_t *)mapping->h= ost); } else { - if (s->high_water_mark - s->low_water_mark + 1 < size) { + if (s->high_water_mark - s->low_water_mark + 1 < max_size) { ret =3D -ENOMEM; goto out; } @@ -631,7 +658,7 @@ int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, siz= e_t size, qemu_vfio_remove_mapping(s, mapping); goto out; } - s->low_water_mark +=3D size; + s->low_water_mark +=3D max_size; qemu_vfio_dump_mappings(s); } else { iova0 =3D s->high_water_mark - size; @@ -650,6 +677,12 @@ out: return ret; } =20 +int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, size_t size, + bool temporary, uint64_t *iova) +{ + return qemu_vfio_dma_map_resizeable(s, host, size, size, temporary, io= va); +} + /* Reset the high watermark and free all "temporary" mappings. */ int qemu_vfio_dma_reset_temporary(QEMUVFIOState *s) { @@ -694,6 +727,29 @@ out: qemu_mutex_unlock(&s->lock); } =20 +static void qemu_vfio_dma_map_resize(QEMUVFIOState *s, void *host, + size_t old_size, size_t new_size) +{ + IOVAMapping *m; + int index =3D 0; + + qemu_mutex_lock(&s->lock); + m =3D qemu_vfio_find_mapping(s, host, &index); + if (!m) { + return; + } + assert(m->size =3D=3D old_size); + + /* Note: Not atomic - we need a new ioctl for that. */ + qemu_vfio_undo_mapping(s, m->iova, m->size); + qemu_vfio_do_mapping(s, host, m->iova, new_size); + + m->size =3D new_size; + assert(qemu_vfio_verify_mappings(s)); + + qemu_mutex_unlock(&s->lock); +} + static void qemu_vfio_reset(QEMUVFIOState *s) { ioctl(s->device, VFIO_DEVICE_RESET); --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582799089; cv=none; d=zohomail.com; s=zohoarc; b=ZuEvhWkXvsArxhs1flrnjQt7MRZTa4czzwi5J1gNA8heFgTlZAaY287wcyWXuEMpayT4ZAw1bSAwpQTA82x9LHw6R1BMRYao/0b+bVGhZxn9nA6FrZg67AwJimGNClzMpdAP2LY0v0d9wWC3URpnUfDdypcePJwqp3qZmhg/lvA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582799089; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4RmxeN8/6nSqT/VuVrhD81d9IpRWztqLAUeB/YUff2U=; b=W2HNQARwjq/KIuoVnFcuQvPeqL2g3Rd6EcKHlnfhJSrc2DPGIOlIPEFnpyp7BRVlf8164Ve3ZtZs0dyHmlg7G9YnzK8LjgD0W7YdDyzN3Ngdqu0Ojqy8iz8tGFZYKn6swibAIfHZ+3TuJpYDIbxU96nq7zo0fYRzykHze9J+OH4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582799089382139.41971181028293; Thu, 27 Feb 2020 02:24:49 -0800 (PST) Received: from localhost ([::1]:56690 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GLX-0005qH-VL for importer@patchew.org; Thu, 27 Feb 2020 05:24:47 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40472) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GBz-0004Kv-MN for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GBw-0001RZ-Ck for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:54 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:20265 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GBw-0001Ni-8d for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:52 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-2-sots047lMpe-UBmCEvl1vw-1; Thu, 27 Feb 2020 05:14:48 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C8242800053; Thu, 27 Feb 2020 10:14:46 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id E873B6E3EE; Thu, 27 Feb 2020 10:14:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798489; 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=4RmxeN8/6nSqT/VuVrhD81d9IpRWztqLAUeB/YUff2U=; b=VsUuKc7snXbEAC9S7W9A8YhexbTR4jpgEp7fgZhfOsQ/i3rueU9glOH0etNM+eb+IeEh+6 0lLUD5RoGaWEqSfMFTUPS5YpC9yvhSS4QELgt64bTW4l6UloauPcCa6z8X3CQgv+ga2CB3 jlsPiy5c1f9bDIxxcdqCeMBYjzGo5H8= X-MC-Unique: sots047lMpe-UBmCEvl1vw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 13/15] util: oslib: Resizeable anonymous allocations under POSIX Date: Thu, 27 Feb 2020 11:12:03 +0100 Message-Id: <20200227101205.5616-14-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Stefan Weil , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Introduce qemu_anon_ram_alloc_resizeable() and qemu_anon_ram_resize(). Implement them under POSIX and make them return NULL under WIN32. Under POSIX, we make use of resizeable mmaps. An implementation under WIN32 is theoretically possible AFAIK and can be added later. In qemu_anon_ram_free(), rename the size parameter to max_size, to make it clearer that we have to use the maximum size when freeing resizeable anonymous allocations. Cc: Richard Henderson Cc: Paolo Bonzini Cc: "Dr. David Alan Gilbert" Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Stefan Weil Cc: Igor Mammedov Signed-off-by: David Hildenbrand Reviewed-by: Peter Xu --- include/qemu/osdep.h | 6 +++++- util/oslib-posix.c | 37 ++++++++++++++++++++++++++++++++++--- util/oslib-win32.c | 14 ++++++++++++++ util/trace-events | 4 +++- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 9bd3dcfd13..a1ea9e043d 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -311,8 +311,12 @@ int qemu_daemon(int nochdir, int noclose); void *qemu_try_memalign(size_t alignment, size_t size); void *qemu_memalign(size_t alignment, size_t size); void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared); +void *qemu_anon_ram_alloc_resizeable(size_t size, size_t max_size, + uint64_t *align, bool shared); +bool qemu_anon_ram_resize(void *ptr, size_t old_size, size_t new_size, + bool shared); void qemu_vfree(void *ptr); -void qemu_anon_ram_free(void *ptr, size_t size); +void qemu_anon_ram_free(void *ptr, size_t max_size); =20 #define QEMU_MADV_INVALID -1 =20 diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 897e8f3ba6..34b1ce74b7 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -223,16 +223,47 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *alig= nment, bool shared) return ptr; } =20 +void *qemu_anon_ram_alloc_resizeable(size_t size, size_t max_size, + uint64_t *alignment, bool shared) +{ + size_t align =3D QEMU_VMALLOC_ALIGN; + void *ptr =3D qemu_ram_mmap_resizeable(-1, size, max_size, align, shar= ed, + false); + + if (ptr =3D=3D MAP_FAILED) { + return NULL; + } + + if (alignment) { + *alignment =3D align; + } + + trace_qemu_anon_ram_alloc_resizeable(size, max_size, ptr); + return ptr; +} + +bool qemu_anon_ram_resize(void *ptr, size_t old_size, size_t new_size, + bool shared) +{ + bool resized =3D qemu_ram_mmap_resize(ptr, -1, old_size, new_size, sha= red, + false); + + if (resized) { + trace_qemu_anon_ram_resize(old_size, new_size, ptr); + } + return resized; +} + void qemu_vfree(void *ptr) { trace_qemu_vfree(ptr); free(ptr); } =20 -void qemu_anon_ram_free(void *ptr, size_t size) +void qemu_anon_ram_free(void *ptr, size_t max_size) { - trace_qemu_anon_ram_free(ptr, size); - qemu_ram_munmap(-1, ptr, size); + trace_qemu_anon_ram_free(ptr, max_size); + qemu_ram_munmap(-1, ptr, max_size); } =20 void qemu_set_block(int fd) diff --git a/util/oslib-win32.c b/util/oslib-win32.c index e9b14ab178..c034fdfe01 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -90,6 +90,20 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *align, = bool shared) return ptr; } =20 +void *qemu_anon_ram_alloc_resizeable(size_t size, size_t max_size, + uint64_t *align, bool shared) +{ + /* resizeable ram not implemented yet */ + return NULL; +} + +bool qemu_anon_ram_resize(void *ptr, size_t old_size, size_t new_size, + bool shared) +{ + /* resizeable ram not implemented yet */ + return false; +} + void qemu_vfree(void *ptr) { trace_qemu_vfree(ptr); diff --git a/util/trace-events b/util/trace-events index 88b7dbf4a5..e4af970312 100644 --- a/util/trace-events +++ b/util/trace-events @@ -46,8 +46,10 @@ qemu_co_mutex_unlock_return(void *mutex, void *self) "mu= tex %p self %p" # oslib-posix.c qemu_memalign(size_t alignment, size_t size, void *ptr) "alignment %zu siz= e %zu ptr %p" qemu_anon_ram_alloc(size_t size, void *ptr) "size %zu ptr %p" +qemu_anon_ram_alloc_resizeable(size_t size, size_t max_size, void *ptr) "s= ize %zu max_size %zu ptr %p" +qemu_anon_ram_resize(size_t old_size, size_t new_size, void *ptr) "old_siz= e %zu new_size %zu ptr %p" qemu_vfree(void *ptr) "ptr %p" -qemu_anon_ram_free(void *ptr, size_t size) "ptr %p size %zu" +qemu_anon_ram_free(void *ptr, size_t max_size) "ptr %p max_size %zu" =20 # hbitmap.c hbitmap_iter_skip_words(const void *hb, void *hbi, uint64_t pos, unsigned = long cur) "hb %p hbi %p pos %"PRId64" cur 0x%lx" --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582799089; cv=none; d=zohomail.com; s=zohoarc; b=OB6fHuM36MnmZ59cg6/vN07RBat68SwRUh/Z7Df+ZJwt1R7mwImj3Y9ljnlax6FXZiHTPxfhAsKjk5dxNrbmPZErEf6oF0tLmr0ftwDFYlpTvFS356Q1zBpL5tcpN1xMhZwExds2zosNNbB0lDlFxZNREKV4E507xcO7v7UZ79Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582799089; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7BJc03B0S3OKlAGbitvN5dpmwN+YdFl+CckVsdEMIJE=; b=cKMSItOTXkxc/bhh8lwOFIsdbp9l0UdSC+U9qUokxGQ2pwJ+AXu/K+bHZ8YIHuZGPyY77JPL0nJ4z9mZNBF92/JuLkXSgMhApIMCOp6BAbLflSOMRlOQnnEMSuu+mdVhxVnadSEHrPUazbgf1S/WUM0udhcBBKFtLcrGDxu3OuI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582799089910921.4823667154791; Thu, 27 Feb 2020 02:24:49 -0800 (PST) Received: from localhost ([::1]:56692 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GLY-0005qm-2w for importer@patchew.org; Thu, 27 Feb 2020 05:24:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40517) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GC4-0004OL-UP for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:15:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GC2-0001Yj-SD for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:15:00 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:46519 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GC1-0001UP-OQ for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:14:58 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-175-9Yix3FYUNNGWA9dC4mphqg-1; Thu, 27 Feb 2020 05:14:53 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 40B538017CC; Thu, 27 Feb 2020 10:14:52 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id C7EA06E3EE; Thu, 27 Feb 2020 10:14:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798495; 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=7BJc03B0S3OKlAGbitvN5dpmwN+YdFl+CckVsdEMIJE=; b=hXlM70B8tOAzpRMxZ+K6shhKr5Sxo92s1+0soFC3m0f2Xprd1Ewm00+fs5fjUpewe8qXl/ WgUO26UflAK1xv9RmUIrhfXTgWoWEBLNz2pjl1MhivYow/GX/TtZnsrSiLLUPj1XtwipLg rG23B9NSoTHgHp2VM4qufCuG36MU4cA= X-MC-Unique: 9Yix3FYUNNGWA9dC4mphqg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 14/15] numa: Introduce ram_block_notifiers_support_resize() Date: Thu, 27 Feb 2020 11:12:04 +0100 Message-Id: <20200227101205.5616-15-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We want to actually use resizeable allocations in resizeable ram blocks (IOW, make everything between used_length and max_length inaccessible) - however, not all ram block notifiers can support that. Introduce a way to detect if any registered notifier does not support resizes - ram_block_notifiers_support_resize() - which we can later use to fallback to legacy handling if a registered notifier (esp., SEV and HAX) does not support actual resizes. Cc: Richard Henderson Cc: Paolo Bonzini Cc: "Dr. David Alan Gilbert" Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: "Michael S. Tsirkin" Cc: Igor Mammedov Signed-off-by: David Hildenbrand Reviewed-by: Peter Xu --- hw/core/numa.c | 12 ++++++++++++ include/exec/ramlist.h | 1 + 2 files changed, 13 insertions(+) diff --git a/hw/core/numa.c b/hw/core/numa.c index 37ce175e13..1d5288c22c 100644 --- a/hw/core/numa.c +++ b/hw/core/numa.c @@ -914,3 +914,15 @@ void ram_block_notify_resize(void *host, size_t old_si= ze, size_t new_size) } } } + +bool ram_block_notifiers_support_resize(void) +{ + RAMBlockNotifier *notifier; + + QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { + if (!notifier->ram_block_resized) { + return false; + } + } + return true; +} diff --git a/include/exec/ramlist.h b/include/exec/ramlist.h index 293c0ddabe..ac5811be96 100644 --- a/include/exec/ramlist.h +++ b/include/exec/ramlist.h @@ -79,6 +79,7 @@ void ram_block_notifier_remove(RAMBlockNotifier *n); void ram_block_notify_add(void *host, size_t size, size_t max_size); void ram_block_notify_remove(void *host, size_t size, size_t max_size); void ram_block_notify_resize(void *host, size_t old_size, size_t new_size); +bool ram_block_notifiers_support_resize(void); =20 void ram_block_dump(Monitor *mon); =20 --=20 2.24.1 From nobody Thu Nov 13 20:27:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582799274; cv=none; d=zohomail.com; s=zohoarc; b=Vqg3Tz6C0fojj7mlCXrBQFBJTpIVdPFIimq14c1iEbhnn4YPcMszNRQ9SzBuN/JNMrvL/VS3pBgwUFbPejbmL6YO105LuqjTkm6QtQgghzkDN2k8cU0KjMOC9ncxfhas94P55ogdsjHkRON/c8ARqtvl6ACkhuqbTIOVXwrutvE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582799274; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=O8nHtc0k+Qjd2SKMYchY/IHyGuwzrWoBM7Pglrphr0s=; b=NZ1yR5l6EqAy70Bt4yPsK19zw2NfJZnHiHTFTSdBZnqXdS33NU94dnBez4DFmLR8e0aWDZvfwYiJUTHmSZeRbX/yoPwgwteQNcRe4n4PSzg+DXKLWAcQR2E1tmUgOE3JH+Q4ScG4jI6s0gh8KLF2zkPEHn2IzdArX30UypZsKWc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582799274079211.5312207166254; Thu, 27 Feb 2020 02:27:54 -0800 (PST) Received: from localhost ([::1]:56750 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GOW-0001JC-PO for importer@patchew.org; Thu, 27 Feb 2020 05:27:52 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40545) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7GC6-0004S5-H2 for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:15:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7GC4-0001cF-Nt for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:15:02 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:60681 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7GC4-0001aA-J6 for qemu-devel@nongnu.org; Thu, 27 Feb 2020 05:15:00 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-306-JywTa91CNO-HOW413aR3mQ-1; Thu, 27 Feb 2020 05:14:58 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 077481005513; Thu, 27 Feb 2020 10:14:57 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-36.ams2.redhat.com [10.36.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9394C6E3EE; Thu, 27 Feb 2020 10:14:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582798500; 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=O8nHtc0k+Qjd2SKMYchY/IHyGuwzrWoBM7Pglrphr0s=; b=DM9Spo8ojiqzUzhBVMIe6pqvciH5bR6rLudySsWgSMWjYUM3DIUwvahehZRAO2kgWsN9iI lz3+5jWTmxpE5NpX3A1uwGYQO4N6NCftugTEskIQ4P5dXSlQomewLbsujB1w36njLqSG7i QHRUUCi5purqxxsmAKwCjfcRChfvsYE= X-MC-Unique: JywTa91CNO-HOW413aR3mQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 15/15] exec: Ram blocks with resizeable anonymous allocations under POSIX Date: Thu, 27 Feb 2020 11:12:05 +0100 Message-Id: <20200227101205.5616-16-david@redhat.com> In-Reply-To: <20200227101205.5616-1-david@redhat.com> References: <20200227101205.5616-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , Stefan Weil , Igor Kotrasinski , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Shameerali Kolothum Thodi , Murilo Opsfelder Araujo , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We can now make use of resizeable anonymous allocations to implement actually resizeable ram blocks. Resizeable anonymous allocations are not implemented under WIN32 yet and are not available when using alternative allocators. Fall back to the existing handling. We also have to fallback to the existing handling in case any ram block notifier does not support resizing (esp., AMD SEV, HAX) yet. Remember in RAM_RESIZEABLE_ALLOC if we are using resizeable anonymous allocations. Try to grow early, as that can easily fail if out of memory. Shrink late and ignore errors (nothing will actually break). Warn only. The benefit of actually resizeable ram blocks is that e.g., under Linux, only the actual size will be reserved (even if "/proc/sys/vm/overcommit_memory" is set to "never"). Additional memory will be reserved when trying to resize, which allows to have ram blocks that start small but can theoretically grow very large. Note1: We are not able to create resizeable ram blocks with pre-allocated memory yet, so prealloc is not affected. Note2: mlock should work as it used to as os_mlock() does a mlockall(MCL_CURRENT | MCL_FUTURE), which includes future mappings. Note3: Nobody should access memory beyond used_length. Memory notifiers already properly take care of this, only ram block notifiers violate this constraint and, therefore, have to be special-cased. Especially, any ram block notifier that might dynamically register at runtime (e.g., vfio) has to support resizes. Add an assert for that. Both, HAX and SEV register early, so they are fine. Cc: Richard Henderson Cc: Paolo Bonzini Cc: "Dr. David Alan Gilbert" Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Stefan Weil Cc: Igor Mammedov Cc: Shameerali Kolothum Thodi Signed-off-by: David Hildenbrand Reviewed-by: Peter Xu --- exec.c | 64 ++++++++++++++++++++++++++++++++++++--- hw/core/numa.c | 7 +++++ include/exec/cpu-common.h | 2 ++ include/exec/memory.h | 8 +++++ 4 files changed, 76 insertions(+), 5 deletions(-) diff --git a/exec.c b/exec.c index cdf962a936..ba72f040e5 100644 --- a/exec.c +++ b/exec.c @@ -2001,6 +2001,16 @@ void qemu_ram_unset_migratable(RAMBlock *rb) rb->flags &=3D ~RAM_MIGRATABLE; } =20 +bool qemu_ram_is_resizeable(RAMBlock *rb) +{ + return rb->flags & RAM_RESIZEABLE; +} + +bool qemu_ram_is_resizeable_alloc(RAMBlock *rb) +{ + return rb->flags & RAM_RESIZEABLE_ALLOC; +} + /* Called with iothread lock held. */ void qemu_ram_set_idstr(RAMBlock *new_block, const char *name, DeviceState= *dev) { @@ -2094,6 +2104,7 @@ static void qemu_ram_apply_settings(void *host, size_= t length) */ int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp) { + const bool shared =3D block->flags & RAM_SHARED; const ram_addr_t oldsize =3D block->used_length; =20 assert(block); @@ -2104,7 +2115,7 @@ int qemu_ram_resize(RAMBlock *block, ram_addr_t newsi= ze, Error **errp) return 0; } =20 - if (!(block->flags & RAM_RESIZEABLE)) { + if (!qemu_ram_is_resizeable(block)) { error_setg_errno(errp, EINVAL, "Length mismatch: %s: 0x" RAM_ADDR_FMT " in !=3D 0x" RAM_ADDR_FMT, block->idstr, @@ -2120,6 +2131,15 @@ int qemu_ram_resize(RAMBlock *block, ram_addr_t news= ize, Error **errp) return -EINVAL; } =20 + if (oldsize < newsize && qemu_ram_is_resizeable_alloc(block)) { + if (!qemu_anon_ram_resize(block->host, oldsize, newsize, shared)) { + error_setg_errno(errp, -ENOMEM, "Cannot allocate enough memory= ."); + return -ENOMEM; + } + /* apply settings for the newly accessible memory */ + qemu_ram_apply_settings(block->host + oldsize, newsize - oldsize); + } + /* Notify before modifying the ram block and touching the bitmaps. */ if (block->host) { ram_block_notify_resize(block->host, oldsize, newsize); @@ -2133,6 +2153,16 @@ int qemu_ram_resize(RAMBlock *block, ram_addr_t news= ize, Error **errp) if (block->resized) { block->resized(block->idstr, newsize, block->host); } + + /* + * Shrinking will only fail in rare scenarios (e.g., maximum number of + * mappings reached), and can be ignored. Warn only. + */ + if (newsize < oldsize && qemu_ram_is_resizeable_alloc(block) && + !qemu_anon_ram_resize(block->host, oldsize, newsize, shared)) { + warn_report("Shrinking memory allocation failed."); + } + return 0; } =20 @@ -2211,6 +2241,28 @@ static void dirty_memory_extend(ram_addr_t old_ram_s= ize, } } =20 +static void ram_block_alloc_ram(RAMBlock *rb) +{ + const bool shared =3D qemu_ram_is_shared(rb); + + /* + * If we can, try to allocate actually resizeable ram. Will also fail + * if qemu_anon_ram_alloc_resizeable() is not implemented. + */ + if (phys_mem_alloc =3D=3D qemu_anon_ram_alloc && + qemu_ram_is_resizeable(rb) && + ram_block_notifiers_support_resize()) { + rb->host =3D qemu_anon_ram_alloc_resizeable(rb->used_length, + rb->max_length, + &rb->mr->align, shared); + if (rb->host) { + rb->flags |=3D RAM_RESIZEABLE_ALLOC; + return; + } + } + rb->host =3D phys_mem_alloc(rb->max_length, &rb->mr->align, shared); +} + static void ram_block_add(RAMBlock *new_block, Error **errp) { RAMBlock *block; @@ -2233,9 +2285,7 @@ static void ram_block_add(RAMBlock *new_block, Error = **errp) return; } } else { - new_block->host =3D phys_mem_alloc(new_block->max_length, - &new_block->mr->align, - qemu_ram_is_shared(new_block)= ); + ram_block_alloc_ram(new_block); if (!new_block->host) { error_setg_errno(errp, errno, "cannot set up guest memory '%s'", @@ -2280,7 +2330,11 @@ static void ram_block_add(RAMBlock *new_block, Error= **errp) DIRTY_CLIENTS_ALL); =20 if (new_block->host) { - qemu_ram_apply_settings(new_block->host, new_block->max_length); + if (qemu_ram_is_resizeable_alloc(new_block)) { + qemu_ram_apply_settings(new_block->host, new_block->used_lengt= h); + } else { + qemu_ram_apply_settings(new_block->host, new_block->max_length= ); + } ram_block_notify_add(new_block->host, new_block->used_length, new_block->max_length); } diff --git a/hw/core/numa.c b/hw/core/numa.c index 1d5288c22c..c547549e49 100644 --- a/hw/core/numa.c +++ b/hw/core/numa.c @@ -862,6 +862,13 @@ static int ram_block_notify_add_single(RAMBlock *rb, v= oid *opaque) RAMBlockNotifier *notifier =3D opaque; =20 if (host) { + /* + * Dynamically adding notifiers that don't support resizes is forb= idden + * when dealing with resizeable ram blocks that have actually resi= zeable + * allocations. + */ + g_assert(!qemu_ram_is_resizeable_alloc(rb) || + notifier->ram_block_resized); notifier->ram_block_added(notifier, host, size, max_size); } return 0; diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h index 09decb8d93..aacbf33b85 100644 --- a/include/exec/cpu-common.h +++ b/include/exec/cpu-common.h @@ -66,6 +66,8 @@ void qemu_ram_set_uf_zeroable(RAMBlock *rb); bool qemu_ram_is_migratable(RAMBlock *rb); void qemu_ram_set_migratable(RAMBlock *rb); void qemu_ram_unset_migratable(RAMBlock *rb); +bool qemu_ram_is_resizeable(RAMBlock *rb); +bool qemu_ram_is_resizeable_alloc(RAMBlock *rb); =20 size_t qemu_ram_pagesize(RAMBlock *block); size_t qemu_ram_pagesize_largest(void); diff --git a/include/exec/memory.h b/include/exec/memory.h index b9b9470a56..74805dd448 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -129,6 +129,14 @@ typedef struct IOMMUNotifier IOMMUNotifier; /* RAM is a persistent kind memory */ #define RAM_PMEM (1 << 5) =20 +/* + * Implies RAM_RESIZEABLE. Memory beyond the used_length is inaccessible + * (esp. initially and after resizing). For such memory blocks, only the + * used_length is reserved in the OS - resizing might fail. Will only be + * used with host OS support and if all ram block notifiers support resizi= ng. + */ +#define RAM_RESIZEABLE_ALLOC (1 << 6) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end, --=20 2.24.1