From patchwork Wed Dec 15 22:29:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Raphael Norwitz X-Patchwork-Id: 12679647 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5488CC433F5 for ; Wed, 15 Dec 2021 22:32:47 +0000 (UTC) Received: from localhost ([::1]:57996 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mxcpK-0006C1-ES for qemu-devel@archiver.kernel.org; Wed, 15 Dec 2021 17:32:46 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57320) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mxcmp-0003M2-Cw for qemu-devel@nongnu.org; Wed, 15 Dec 2021 17:30:11 -0500 Received: from mx0b-002c1b01.pphosted.com ([148.163.155.12]:11932) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mxcmc-0006Ie-JU for qemu-devel@nongnu.org; Wed, 15 Dec 2021 17:30:03 -0500 Received: from pps.filterd (m0127843.ppops.net [127.0.0.1]) by mx0b-002c1b01.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 1BFGW9bX007586; Wed, 15 Dec 2021 14:29:57 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=proofpoint20171006; bh=T65VD3UfjuYQXE+Opa0TFYmDscjGWwVbqXvUdg822Fc=; b=d6W2ylYCHMUkaBW77Ur/9CPmsjI4sKKm2Zw2uKj1R1K7v88lGBK6Ba+o5iYr2GTXwWet vlgx0L1IA0yRD9r5iDAhmjX78mpJdJiW+o1DNrNkDDOOJqy2Ae08ub9ic/xUW36cLIQU E64Wzg1ewZ7f203wf0lnwHHmInBJQDxasZbslxWOyhAr4wO7Il8Jfd+Nbyo1IhVU42Dc x7v3dXwdXJhiPdHAUL613SATOJDn6rtjNpysFNMaxLWaa+TUoO+tb1IAoEAzbdc03x/2 lauBxWd7b1W0AOSJrfwmAUqoJt6XhuOE7rXpry3RJJymBS6Atf8NZS7++7v9ChPw80Q1 wA== Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2169.outbound.protection.outlook.com [104.47.59.169]) by mx0b-002c1b01.pphosted.com (PPS) with ESMTPS id 3cxxfekbwk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 15 Dec 2021 14:29:57 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kanRburXAy7jbcXWpzNyp36Q7EUk0iz2b5QQTDnTwZrBk82RwJXKb+hGtTYwGgARD4zlyoIYI0h5VC9Kt+feUJbTwjdYNZ3FRS4NwvDkVMKoubPMEIvUNf6lGMJXiRl55RKRyXRgVDm41epn3HGtfRLijPF0XplDjO+C9xj1gl5B9vZrRPY9DdnrDVNHHQ1upMrNlI8DI6btTilwTcg0mnKYpgbB/voX/PrWlZTJw2NH0HmrP+WOGTmPlw1RHZUUYpKeKsOPsWEiw9QUP/6s5SixPx2wpD7JA8/FbbpcpyI7iSqYoDb0HQxqDqtU+/Xh1kHmw6Ar3vH1kJo+JSl0wQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=T65VD3UfjuYQXE+Opa0TFYmDscjGWwVbqXvUdg822Fc=; b=RdVv56PFiEWOAUucjKjMiPHHLQRlHfxQFrP8MhH21dBkcweznJVIyixBZGBGOX8CCJY4ZqJ7nR423NdeBl7f1ogCTlEqMgkft7ita927K+nlBsMiItYde42CT5ef5WNWwt28sqWsaOOivq3KGDZY1sE2vdHfJApYNAnDfciIE8Mmu400s5c9Ew9b8WENCUJQoajkY689+37JhIuNK/DCo0c9UIgLOLeqrYSf5ElAdk1N/DXyDmKSoz7hGkEhKaIYwSNnJ9q9dhI8ty0TO8luageK2z2lbRx0D4mKsMHn1gs1utkOekFMXrF8udjMdFzmRYgBd2EEtuq5RJvBX8pj8w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nutanix.com; dmarc=pass action=none header.from=nutanix.com; dkim=pass header.d=nutanix.com; arc=none Received: from BL3PR02MB7938.namprd02.prod.outlook.com (2603:10b6:208:355::20) by BL0PR02MB5457.namprd02.prod.outlook.com (2603:10b6:208:87::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.12; Wed, 15 Dec 2021 22:29:56 +0000 Received: from BL3PR02MB7938.namprd02.prod.outlook.com ([fe80::25f1:c1cf:5f90:35af]) by BL3PR02MB7938.namprd02.prod.outlook.com ([fe80::25f1:c1cf:5f90:35af%8]) with mapi id 15.20.4778.016; Wed, 15 Dec 2021 22:29:56 +0000 From: Raphael Norwitz To: "stefanha@redhat.com" , "marcandre.lureau@redhat.com" , "mst@redhat.com" , "david@redhat.com" Subject: [RFC 5/5] libvhost-user: handle removal of identical regions Thread-Topic: [RFC 5/5] libvhost-user: handle removal of identical regions Thread-Index: AQHX8gNJEQPpKFdBa0aQg0lnFIf3ow== Date: Wed, 15 Dec 2021 22:29:55 +0000 Message-ID: <20211215222939.24738-6-raphael.norwitz@nutanix.com> References: <20211215222939.24738-1-raphael.norwitz@nutanix.com> In-Reply-To: <20211215222939.24738-1-raphael.norwitz@nutanix.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.20.1 x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 6b0d60b8-66e3-40b0-f75b-08d9c01a6be3 x-ms-traffictypediagnostic: BL0PR02MB5457:EE_ x-microsoft-antispam-prvs: x-proofpoint-crosstenant: true x-ms-oob-tlc-oobclassifiers: OLM:7691; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: aXJ2hdDtST1rR1kjvWtR7Jo27L7TvubUrMn72YWlJ9fTY1nk9upCrS7MGfB06yF2su/THbmv9UZC92uC4CIeSdBFTKP/TreTk9DeVkTKS9SD9H/UuxVvwTItNw66MQ5YqYGg5N7sOY5a8S179cUPf+qV9ABIoMPyfkxW/CfpvYIFY5Vz7uKJ/Ej1HVUufUpC64Ittk16JeOJip0BMfe/h8grK+V77mBRJxWCTHh2vZWSxrHuRQl6fat/U8k9nLz3ERibKYciDujHJKGvHseI8tJ4ERo0jMIoUxOYjHpegPafklL1WcYJsZKsNVJfDydTuZOPgaXAM3yEGfCojnoxE5m1fJ7X9YkD0fAkKH95InGe/XiLB5NYNGsPMbhkbzHFsnlIUE6zE6z6psPswhtNJmheXa1yVWQtnbjSiJ5k+iID8aYFUqkZHIOFErL3dubwE5g1FWRVGJuR9IdlywkTf+qVQxu8VvmVvPFkpaPvYvXIH+Auh0D6F6Yf3f8Sso8IvKqekAjEy+VUtkNhSopQrr/lStxqvvK7Ymmy4AiQ+H5f4q0PbSBn2ReAbmXw8AvJebdzD5/6ykx513UMWUFHLWgIW9wMc58M88DK64mJ7fMF4oLjzWDA16p1xGWPfowZFqa+KtjcfMsvzg4H+WquToNy3kW4cmP6EYci+xEnxrITWK6/gZnvodjsNJw/85muYSyym1pF+JXpuhUBJnovtg== x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BL3PR02MB7938.namprd02.prod.outlook.com; PTR:; CAT:NONE; SFS:(366004)(83380400001)(2906002)(8936002)(6506007)(8676002)(26005)(1076003)(107886003)(2616005)(316002)(110136005)(122000001)(54906003)(5660300002)(38100700002)(86362001)(38070700005)(66946007)(186003)(44832011)(36756003)(66446008)(4326008)(64756008)(66476007)(76116006)(91956017)(6512007)(66556008)(71200400001)(6486002)(508600001); DIR:OUT; SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?NHvAiFxP1O7r6a7i7B3FooN?= =?iso-8859-1?q?PMwSWA21GFLEn2dMggx7TBjpyYajqC1LK/hu6tDGL04Mb1bWTn9N0m+4KAEs?= =?iso-8859-1?q?PpY1X+uJINy3uKiYJ5EPtloOAAXAppABU3MTWfU+tFK4Wnu11p9stbQimL3u?= =?iso-8859-1?q?1AowEwEvSzWIAsTe2jjn+BOjnMYZdX9OLyMLL8YcmN2VDQUH+F/nX0pAyjDL?= =?iso-8859-1?q?rfMTCdkQcxSPbga9kUpZOfEOi8AOv1dJCYK0cG/9xmYkBK2LtdGVjWcu1+yy?= =?iso-8859-1?q?+AN0Vg0/NHd8wM3bEGz9K9+HP97BISf9Rhm4w5U91iOO6hQ91vG7QBrqlz7N?= =?iso-8859-1?q?hJPb/XyBXSmfRDFZLDwVRMNWtgeiOeS6Q0CYb7oEbSf6yi7X8AEePpAmkLKs?= =?iso-8859-1?q?sFKwgQLg4WKCzqXGFeeuy6JSMWmxC+Znf0IEO8PCFvgC19F6vyPtniX3OAxA?= =?iso-8859-1?q?BH4muy3o6n3R+7wx3TvsL8SyYqGA6voM2Mc9ZHf69tW/9hvY9pXhiwRWouTC?= =?iso-8859-1?q?FGllNukXaqBaTuENKHBAr1o8hq5Q1vJnq392+C7KpmcWC3c2BToqOQPXqS5c?= =?i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Je24kkXedWwnd8IpBXLTEC3qs55cZ5DodSwIBIN?= =?iso-8859-1?q?M66AFhSfA0kXvUBUjaK3n0xTU7SXVJ/0GEAoq1F+lilLQiax2g3V2VLp3TXp?= =?iso-8859-1?q?5EthYDvT6G0VwlPByFZDIMDfJQycyU03BScrCScCdoQMqUrdtWzc6kP0Pk1V?= =?iso-8859-1?q?xoL63j7DMD5F7Q+baFEJkk9RLhFw1nw8uGCWIddAkBWk7Oy+BLJt6+8iFazn?= =?iso-8859-1?q?SijM5+VabO0nGOMRfd9LXJ4fqYWlMrkK8dW/RhrFuPplnOdUzWfQTgDmStqt?= =?iso-8859-1?q?PuBE=3D?= MIME-Version: 1.0 X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: BL3PR02MB7938.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6b0d60b8-66e3-40b0-f75b-08d9c01a6be3 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Dec 2021 22:29:56.0285 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: bb047546-786f-4de1-bd75-24e5b6f79043 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: dx4u/BS+qt03vOJSNuYtEtHjdj8KTFgj24aRDm0bvFusmzDBfNRmU0DUB6Jp3lO9Y55Gyi4/sWj7Sniq19H+Z6SqO10pYf3OvAo4kmKYrpA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR02MB5457 X-Proofpoint-GUID: IKZK1-cG_8KgWlEWiSi0WZ2eA-_48-g7 X-Proofpoint-ORIG-GUID: IKZK1-cG_8KgWlEWiSi0WZ2eA-_48-g7 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.790,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2021-12-15_13,2021-12-14_01,2021-12-02_01 X-Proofpoint-Spam-Reason: safe Received-SPF: pass client-ip=148.163.155.12; envelope-from=raphael.norwitz@nutanix.com; helo=mx0b-002c1b01.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.719, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "raphael.s.norwitz@gmail.com" , "qemu-devel@nongnu.org" , Raphael Norwitz Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Today if QEMU (or any other VMM) has sent multiple copies of the same region to a libvhost-user based backend and then attempts to remove the region, only one instance of the region will be removed, leaving stale copies of the region in dev->regions[]. This change resolves this by having vu_rem_mem_reg() iterate through all regions in dev->regions[] and delete all matching regions. Suggested-by: Stefan Hajnoczi Signed-off-by: Raphael Norwitz --- subprojects/libvhost-user/libvhost-user.c | 27 ++++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvhost-user/libvhost-user.c index 74a9980194..2f465a4f0e 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -809,6 +809,7 @@ static bool vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m; int i; + bool found = false; if (vmsg->fd_num != 1 || vmsg->size != sizeof(vmsg->payload.memreg)) { @@ -831,25 +832,25 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { VuDevRegion *r = &dev->regions[i]; void *m = (void *) (uintptr_t) r->mmap_addr; - if (m) { + if (m && !found) { munmap(m, r->size + r->mmap_offset); } - break; + /* + * Shift all affected entries by 1 to close the hole at index i and + * zero out the last entry. + */ + memmove(dev->regions + i, dev->regions + i + 1, + sizeof(VuDevRegion) * (dev->nregions - i - 1)); + memset(dev->regions + dev->nregions - 1, 0, sizeof(VuDevRegion)); + DPRINT("Successfully removed a region\n"); + dev->nregions--; + + found = true; } } - if (i < dev->nregions) { - /* - * Shift all affected entries by 1 to close the hole at index i and - * zero out the last entry. - */ - memmove(dev->regions + i, dev->regions + i + 1, - sizeof(VuDevRegion) * (dev->nregions - i - 1)); - memset(dev->regions + dev->nregions - 1, 0, - sizeof(VuDevRegion)); - DPRINT("Successfully removed a region\n"); - dev->nregions--; + if (found) { vmsg_set_reply_u64(vmsg, 0); } else { vu_panic(dev, "Specified region not found\n");