From mboxrd@z Thu Jan 1 00:00:00 1970 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=-12.1 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, NICE_REPLY_A,SIGNED_OFF_BY,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 412D8C433E0 for ; Thu, 30 Jul 2020 10:57:12 +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 1014A207F5 for ; Thu, 30 Jul 2020 10:57:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=xen.org header.i=@xen.org header.b="gbUGbRJs" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1014A207F5 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 localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k16En-0004E8-92; Thu, 30 Jul 2020 10:56:37 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k16Em-0004E3-CP for xen-devel@lists.xenproject.org; Thu, 30 Jul 2020 10:56:36 +0000 X-Inumbo-ID: 55e7c41e-d253-11ea-8d2f-bc764e2007e4 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 55e7c41e-d253-11ea-8d2f-bc764e2007e4; Thu, 30 Jul 2020 10:56:35 +0000 (UTC) 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:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=6Ax80nf+EoQR3KIUG/ok9tKdr127h71Jeq4MlF2OY8g=; b=gbUGbRJsZlop4q1hUweM7gdU59 9V1jcENTrb2+tmLS2NkVK+Gkzuk7GrEsnyXzpNmvPtMwEHrJ2hLNVqrDTiSW9TJXxR09Yay7skfYa /odVYVg88EeRhZ72SqJ3NxpWbjNc2SPbsODEdezoBahAD4nDLWJAa4NulkmfZwHuBsPM=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k16Eg-0006OS-TM; Thu, 30 Jul 2020 10:56:30 +0000 Received: from 54-240-197-226.amazon.com ([54.240.197.226] 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 1k16Eg-0000ek-J9; Thu, 30 Jul 2020 10:56:30 +0000 Subject: Re: [PATCH 2/5] xen/gnttab: Rework resource acquisition To: Andrew Cooper , Xen-devel References: <20200728113712.22966-1-andrew.cooper3@citrix.com> <20200728113712.22966-3-andrew.cooper3@citrix.com> From: Julien Grall Message-ID: Date: Thu, 30 Jul 2020 11:56:28 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200728113712.22966-3-andrew.cooper3@citrix.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 8bit X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Hubert Jasudowicz , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Paul Durrant , Jan Beulich , =?UTF-8?Q?Micha=c5=82_Leszczy=c5=84ski?= , Ian Jackson Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Hi Andrew, On 28/07/2020 12:37, Andrew Cooper wrote: > The existing logic doesn't function in the general case for mapping a guests > grant table, due to arbitrary 32 frame limit, and the default grant table > limit being 64. > > In order to start addressing this, rework the existing grant table logic by > implementing a single gnttab_acquire_resource(). This is far more efficient > than the previous acquire_grant_table() in memory.c because it doesn't take > the grant table write lock, and attempt to grow the table, for every single > frame. > > The new gnttab_acquire_resource() function subsumes the previous two > gnttab_get_{shared,status}_frame() helpers. > > No functional change. > > Signed-off-by: Andrew Cooper > --- > CC: George Dunlap > CC: Ian Jackson > CC: Jan Beulich > CC: Konrad Rzeszutek Wilk > CC: Stefano Stabellini > CC: Wei Liu > CC: Julien Grall > CC: Paul Durrant > CC: Michał Leszczyński > CC: Hubert Jasudowicz > --- > xen/common/grant_table.c | 93 ++++++++++++++++++++++++++++++------------- > xen/common/memory.c | 42 +------------------ > xen/include/xen/grant_table.h | 19 ++++----- > 3 files changed, 75 insertions(+), 79 deletions(-) > > diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c > index 9f0cae52c0..122d1e7596 100644 > --- a/xen/common/grant_table.c > +++ b/xen/common/grant_table.c > @@ -4013,6 +4013,72 @@ static int gnttab_get_shared_frame_mfn(struct domain *d, > return 0; > } > > +int gnttab_acquire_resource( > + struct domain *d, unsigned int id, unsigned long frame, > + unsigned int nr_frames, xen_pfn_t mfn_list[]) Any reasons for changing the way we usually indent parameters? > +{ > + struct grant_table *gt = d->grant_table; > + unsigned int i = nr_frames, tot_frames; > + void **vaddrs; > + int rc = 0; AFAICT rc will always be initialized when used. So is it necessary? > + > + /* Input sanity. */ > + if ( !nr_frames ) > + return -EINVAL; > + > + /* Overflow checks */ > + if ( frame + nr_frames < frame ) > + return -EINVAL; > + > + tot_frames = frame + nr_frames; > + if ( tot_frames != frame + nr_frames ) > + return -EINVAL; > + > + /* Grow table if necessary. */ > + grant_write_lock(gt); > + switch ( id ) > + { > + mfn_t tmp; > + > + case XENMEM_resource_grant_table_id_shared: > + rc = gnttab_get_shared_frame_mfn(d, tot_frames - 1, &tmp); > + break; > + > + case XENMEM_resource_grant_table_id_status: > + if ( gt->gt_version != 2 ) > + { > + default: > + rc = -EINVAL; > + break; I am aware this is valid C code, but I find this construct confusing. Could you just duplicate the 2 lines and have a separate default case? > + } > + rc = gnttab_get_status_frame_mfn(d, tot_frames - 1, &tmp); > + break; > + } > + > + /* Any errors from growing the table? */ > + if ( rc ) > + goto out; > + > + switch ( id ) > + { > + case XENMEM_resource_grant_table_id_shared: > + vaddrs = gt->shared_raw; > + break; > + > + case XENMEM_resource_grant_table_id_status: > + vaddrs = (void **)gt->status; > + break; > + } > + NIT: Would it make sense to check vaddrs? This would avoid NULL dereference pointers if something went wrong. > + for ( i = 0; i < nr_frames; ++i ) > + mfn_list[i] = virt_to_mfn(vaddrs[frame + i]); > + > + out: > + grant_write_unlock(gt); > + > + return rc; > +} > + Cheers, -- Julien Grall