Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42D22C433B4 for ; Thu, 29 Apr 2021 13:15:59 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C511761412 for ; Thu, 29 Apr 2021 13:15:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C511761412 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.119997.226884 (Exim 4.92) (envelope-from ) id 1lc6W9-0001Py-HP; Thu, 29 Apr 2021 13:15:45 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 119997.226884; Thu, 29 Apr 2021 13:15:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lc6W9-0001Pr-E2; Thu, 29 Apr 2021 13:15:45 +0000 Received: by outflank-mailman (input) for mailman id 119997; Thu, 29 Apr 2021 13:15:44 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lc6W8-0001Pm-AY for xen-devel@lists.xenproject.org; Thu, 29 Apr 2021 13:15:44 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lc6W6-0004qM-9y; Thu, 29 Apr 2021 13:15:42 +0000 Received: from 54-240-197-239.amazon.com ([54.240.197.239] helo=a483e7b01a66.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lc6W6-0000kC-0v; Thu, 29 Apr 2021 13:15:42 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type:In-Reply-To: MIME-Version:Date:Message-ID:From:References:Cc:To:Subject; bh=e0Q1Ff80FfEIWIQMfznCRyf4yKWW+MH4snAWf1VboQQ=; b=RmuL+ku1QfzoVKyxo1Liyj8zCy 1laW4g69Wj5cl/a9w7yLt97PNqq1bToulwJJlBqIULhxe8R+UhDFqwM0dErAwlHoKje6gShCjiy3b i75O5eKI1V1DOusNqPl6xGp5fyskHAiA71TpKV7nA1tVEtdm24vBkI0ZOdlF3FqcBM34=; Subject: Re: [PATCH v3] gnttab: defer allocation of status frame tracking array To: Jan Beulich , "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , George Dunlap , Ian Jackson , Stefano Stabellini , Wei Liu References: From: Julien Grall Message-ID: <581f843f-25de-bf8a-e8b9-7a407158bd4f@xen.org> Date: Thu, 29 Apr 2021 14:15:39 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Hi Jan, On 15/04/2021 10:41, Jan Beulich wrote: > 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(). Given the controversy of the change, I would suggest to summarize why this approach is considered to be ok in the commit message. > Signed-off-by: Jan Beulich > --- > 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(); > > + if ( gt->status == ZERO_BLOCK_PTR ) > + { > + gt->status = xzalloc_array(grant_status_t *, > + grant_to_status_frames(gt->max_grant_frames)); > + if ( !gt->status ) > + { > + gt->status = ZERO_BLOCK_PTR; > + return -ENOMEM; > + } > + } > + > for ( i = nr_status_frames(gt); i < req_status_frames; i++ ) > { > if ( (gt->status[i] = alloc_xenheap_page()) == NULL ) > @@ -1767,18 +1778,23 @@ status_alloc_failed: > free_xenheap_page(gt->status[i]); > gt->status[i] = NULL; > } NIT: can you add a newline here and... > + if ( !nr_status_frames(gt) ) > + { > + xfree(gt->status); > + gt->status = ZERO_BLOCK_PTR; > + } ... here for readability. > return -ENOMEM; > } > > static int > gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt) > { > - unsigned int i; > + unsigned int i, n = nr_status_frames(gt); > > /* Make sure, prior version checks are architectural visible */ > block_speculation(); > > - for ( i = 0; i < nr_status_frames(gt); i++ ) > + for ( i = 0; i < n; i++ ) > { > struct page_info *pg = virt_to_page(gt->status[i]); > gfn_t gfn = gnttab_get_frame_gfn(gt, true, i); > @@ -1833,12 +1849,11 @@ gnttab_unpopulate_status_frames(struct d > page_set_owner(pg, NULL); > } > > - for ( i = 0; i < nr_status_frames(gt); i++ ) > - { > - free_xenheap_page(gt->status[i]); > - gt->status[i] = NULL; > - } > gt->nr_status_frames = 0; > + for ( i = 0; i < n; i++ ) > + free_xenheap_page(gt->status[i]); > + xfree(gt->status); > + gt->status = ZERO_BLOCK_PTR; The new position of the for loop seems unrelated to the purpose of the patch. May I ask why this was done? > > return 0; > } > @@ -1969,11 +1984,11 @@ int grant_table_init(struct domain *d, i > if ( gt->shared_raw == NULL ) > goto out; > > - /* Status pages for grant table - for version 2 */ > - gt->status = xzalloc_array(grant_status_t *, > - grant_to_status_frames(gt->max_grant_frames)); > - if ( gt->status == NULL ) > - goto out; > + /* > + * Status page tracking array for v2 gets allocated on demand. But don't > + * leave a NULL pointer there. > + */ > + gt->status = ZERO_BLOCK_PTR; > > grant_write_lock(gt); > > @@ -4047,11 +4062,12 @@ int gnttab_acquire_resource( > if ( gt->gt_version != 2 ) > break; > > + rc = gnttab_get_status_frame_mfn(d, final_frame, &tmp); NIT: It wasn't obvious to me why gnttab_get_status_frame_mfn() is moved before gt->status. May I suggest to add a in-code comment abouve the ordering? > + > /* Check that void ** is a suitable representation for gt->status. */ > BUILD_BUG_ON(!__builtin_types_compatible_p( > typeof(gt->status), grant_status_t **)); > vaddrs = (void **)gt->status; > - rc = gnttab_get_status_frame_mfn(d, final_frame, &tmp); > break; > } > > Cheers, -- Julien Grall