From nobody Fri Apr 26 03:53:16 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1620117772; cv=none; d=zohomail.com; s=zohoarc; b=cIHgvHC5WJIn/06G5hs7k/KToXHXwly/e7RL9V9l1knii9RfVwOsMAJDxgOT2dNnKmyONMoTpPTOKb0VGGEGXTnTn6hdie+29T5HsRbpPq4W9Ks+xrnKPlCBwUXc1LTjX4ilxUl3t7fwhnflfJ3RuP2PuB0Y0l0xwvlK6413r6Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620117772; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=t7pOKaQXzpRytMdA4ZFAwyRv6nAp2aFO8FSE0pQddp8=; b=E63NJa9RS7YyiquvcuNVHSezduZocyjiB7daBFW1HIQt+huDnzAOmIHlOD1BRTZzBUSt45JAJGxiekuFv5ORPXWefDl3mNZBvlAAktso3UBwDxieDsTDIhOX+jd8v/vNxjo2OCxw8MgAoiatdcabM9D4ZEvBunB4UEBCNYUpVos= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620117772967358.006727300717; Tue, 4 May 2021 01:42:52 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.122005.230103 (Exim 4.92) (envelope-from ) id 1ldqdb-0006Ze-Fj; Tue, 04 May 2021 08:42:39 +0000 Received: by outflank-mailman (output) from mailman id 122005.230103; Tue, 04 May 2021 08:42:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldqdb-0006ZV-B8; Tue, 04 May 2021 08:42:39 +0000 Received: by outflank-mailman (input) for mailman id 122005; Tue, 04 May 2021 08:42:38 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ldqda-0006ZH-HF for xen-devel@lists.xenproject.org; Tue, 04 May 2021 08:42:38 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id db5d6744-d172-4a28-bd53-2491b2c74f2c; Tue, 04 May 2021 08:42:37 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id B381FB158; Tue, 4 May 2021 08:42:36 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: db5d6744-d172-4a28-bd53-2491b2c74f2c X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1620117756; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=t7pOKaQXzpRytMdA4ZFAwyRv6nAp2aFO8FSE0pQddp8=; b=RkgIBAkp8+7NeSTaAzy1gImnP6TWxlhE6TRahF2gXrsiKBwF5JzZEEeYQb+n+Xew0ZPu2w uciv1rMdM36Ui1XimTetieS8r7+bcVxHtlflRhk7XepNWs/XNBO8O80nuYC2eWXEHle+Ry W1hCRbKcaiQ2c6SzNBD92WtPxE5nFns= From: Jan Beulich Subject: [PATCH v4] gnttab: defer allocation of status frame tracking array To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , George Dunlap , Ian Jackson , Julien Grall , Stefano Stabellini , Wei Liu Message-ID: <74048f89-fee7-06c2-ffd5-6e5a14bdf440@suse.com> Date: Tue, 4 May 2021 10:42:36 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" This array can be large when many grant frames are permitted; avoid allocating it when it's not going to be used anyway, by doing this only in gnttab_populate_status_frames(). While the delaying of the respective memory allocation adds possible reasons for failure of the respective enclosing operations, there are other memory allocations there already, so callers can't expect these operations to always succeed anyway. As to the re-ordering at the end of gnttab_unpopulate_status_frames(), this is merely to represent intended order of actions (shrink array bound, then free higher array entries). If there were racing accesses, suitable barriers would need adding in addition. Signed-off-by: Jan Beulich Reviewed-by: Julien Grall --- v4: Add a comment. Add a few blank lines. Extend description. v3: Drop smp_wmb(). Re-base. v2: Defer allocation to when a domain actually switches to the v2 grant API. --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -1747,6 +1747,17 @@ gnttab_populate_status_frames(struct dom /* Make sure, prior version checks are architectural visible */ block_speculation(); =20 + if ( gt->status =3D=3D ZERO_BLOCK_PTR ) + { + gt->status =3D xzalloc_array(grant_status_t *, + grant_to_status_frames(gt->max_grant_fr= ames)); + if ( !gt->status ) + { + gt->status =3D ZERO_BLOCK_PTR; + return -ENOMEM; + } + } + for ( i =3D nr_status_frames(gt); i < req_status_frames; i++ ) { if ( (gt->status[i] =3D alloc_xenheap_page()) =3D=3D NULL ) @@ -1767,18 +1778,25 @@ status_alloc_failed: free_xenheap_page(gt->status[i]); gt->status[i] =3D NULL; } + + if ( !nr_status_frames(gt) ) + { + xfree(gt->status); + gt->status =3D ZERO_BLOCK_PTR; + } + return -ENOMEM; } =20 static int gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt) { - unsigned int i; + unsigned int i, n =3D nr_status_frames(gt); =20 /* Make sure, prior version checks are architectural visible */ block_speculation(); =20 - for ( i =3D 0; i < nr_status_frames(gt); i++ ) + for ( i =3D 0; i < n; i++ ) { struct page_info *pg =3D virt_to_page(gt->status[i]); gfn_t gfn =3D gnttab_get_frame_gfn(gt, true, i); @@ -1833,12 +1849,11 @@ gnttab_unpopulate_status_frames(struct d page_set_owner(pg, NULL); } =20 - for ( i =3D 0; i < nr_status_frames(gt); i++ ) - { - free_xenheap_page(gt->status[i]); - gt->status[i] =3D NULL; - } gt->nr_status_frames =3D 0; + for ( i =3D 0; i < n; i++ ) + free_xenheap_page(gt->status[i]); + xfree(gt->status); + gt->status =3D ZERO_BLOCK_PTR; =20 return 0; } @@ -1969,11 +1984,11 @@ int grant_table_init(struct domain *d, i if ( gt->shared_raw =3D=3D NULL ) goto out; =20 - /* Status pages for grant table - for version 2 */ - gt->status =3D xzalloc_array(grant_status_t *, - grant_to_status_frames(gt->max_grant_frames= )); - if ( gt->status =3D=3D NULL ) - goto out; + /* + * Status page tracking array for v2 gets allocated on demand. But don= 't + * leave a NULL pointer there. + */ + gt->status =3D ZERO_BLOCK_PTR; =20 grant_write_lock(gt); =20 @@ -4047,11 +4062,13 @@ int gnttab_acquire_resource( if ( gt->gt_version !=3D 2 ) break; =20 + /* This may change gt->status, so has to happen before setting vad= drs. */=20 + rc =3D gnttab_get_status_frame_mfn(d, final_frame, &tmp); + /* Check that void ** is a suitable representation for gt->status.= */ BUILD_BUG_ON(!__builtin_types_compatible_p( typeof(gt->status), grant_status_t **)); vaddrs =3D (void **)gt->status; - rc =3D gnttab_get_status_frame_mfn(d, final_frame, &tmp); break; } =20