From nobody Mon Feb 9 18:44:33 2026 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=1650467438; cv=none; d=zohomail.com; s=zohoarc; b=PZQsSEkqFm9/hsSSWy3f1ODNoR2V8rfpJwwN293Cw64tNRfgE+XSgeaSwdJxAxgIe6xsAE7/nPePq3VX3nOihFqYo5vPqBj/RhBkPsw/IMrSEDYhZf09XagYdmIWK02wAh5zuYP58qZT5ZIopn6WI50GUcVVvO4c5D2EiEws+a0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1650467438; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=YDGIDWKPRWQB7unVe+D0Q46rvuLr6BdasgzAbPg2LQ4=; b=GmTPe+yXMfMZHVZ+Fj4h3W5vSvZ0UXBf2f8R5LHUrqGArulZCJzVUXUDVSnJ7v3wE0yS2EpCXP4QnZPjCVWSdCvCEwfhN2L5nCr2hbPmf6dC42V6eVsAjU83202H0EIr9++uo4bqT/WZjP3Eun4m6f+rjRtun+d/FQ8XHByj+4c= 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) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1650467437995563.9317364639079; Wed, 20 Apr 2022 08:10:37 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.309355.525556 (Exim 4.92) (envelope-from ) id 1nhBy2-0005L4-VS; Wed, 20 Apr 2022 15:10:06 +0000 Received: by outflank-mailman (output) from mailman id 309355.525556; Wed, 20 Apr 2022 15:10:06 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nhBy2-0005Hn-7b; Wed, 20 Apr 2022 15:10:06 +0000 Received: by outflank-mailman (input) for mailman id 309355; Wed, 20 Apr 2022 15:10:04 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nhBxz-0003l4-Me for xen-devel@lists.xenproject.org; Wed, 20 Apr 2022 15:10:03 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id f2b5a086-c0bb-11ec-a405-831a346695d4; Wed, 20 Apr 2022 17:10:00 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 6E98B215FD; Wed, 20 Apr 2022 15:10:00 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 45D1113AD5; Wed, 20 Apr 2022 15:10:00 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id kDnRD0giYGJILQAAMHmgww (envelope-from ); Wed, 20 Apr 2022 15:10:00 +0000 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: f2b5a086-c0bb-11ec-a405-831a346695d4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1650467400; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YDGIDWKPRWQB7unVe+D0Q46rvuLr6BdasgzAbPg2LQ4=; b=ldrF4kjq+B1ALbw+0FXFa11PZ6m6yv1eHafGyMjuw6sVEYhgTHTCPqG4eHHk3RNDnjFvFR uDngELLfvTSL23P1jfO6z9oZtu/Q96ANRFBwBt10W+2B3ZxB7zzO52ScagQgii6L1DZlt7 yUr1q+kDuVo+nLFzfmzqcDwMKjr82z4= From: Juergen Gross To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini Subject: [PATCH 09/18] xen/xenbus: add xenbus_setup_ring() service function Date: Wed, 20 Apr 2022 17:09:33 +0200 Message-Id: <20220420150942.31235-10-jgross@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220420150942.31235-1-jgross@suse.com> References: <20220420150942.31235-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1650467438848100026 Content-Type: text/plain; charset="utf-8" Most PV device frontends share very similar code for setting up shared ring buffers: - allocate page(s) - init the ring admin data - give the backend access to the ring via grants Tearing down the ring requires similar actions in all frontends again: - remove grants - free the page(s) Provide service functions xenbus_setup_ring() and xenbus_teardown_ring() for that purpose. Signed-off-by: Juergen Gross --- drivers/xen/xenbus/xenbus_client.c | 69 ++++++++++++++++++++++++++++++ include/xen/xenbus.h | 4 ++ 2 files changed, 73 insertions(+) diff --git a/drivers/xen/xenbus/xenbus_client.c b/drivers/xen/xenbus/xenbus= _client.c index df6890681231..1a2e0d94ccd1 100644 --- a/drivers/xen/xenbus/xenbus_client.c +++ b/drivers/xen/xenbus/xenbus_client.c @@ -407,6 +407,75 @@ int xenbus_grant_ring(struct xenbus_device *dev, void = *vaddr, } EXPORT_SYMBOL_GPL(xenbus_grant_ring); =20 +/* + * xenbus_setup_ring + * @dev: xenbus device + * @vaddr: pointer to starting virtual address of the ring + * @nr_pages: number of pages to be granted + * @grefs: grant reference array to be filled in + * + * Allocate physically contiguous pages for a shared ring buffer and grant= it + * to the peer of the given device. The ring buffer is initially filled wi= th + * zeroes. The virtual address of the ring is stored at @vaddr and the + * grant references are stored in the @grefs array. In case of error @vaddr + * will be set to NULL and @grefs will be filled with INVALID_GRANT_REF. + */ +int xenbus_setup_ring(struct xenbus_device *dev, gfp_t gfp, void **vaddr, + unsigned int nr_pages, grant_ref_t *grefs) +{ + unsigned long ring_size =3D nr_pages * XEN_PAGE_SIZE; + unsigned int i; + int ret; + + *vaddr =3D alloc_pages_exact(ring_size, gfp | __GFP_ZERO); + if (!*vaddr) { + ret =3D -ENOMEM; + goto err; + } + + ret =3D xenbus_grant_ring(dev, *vaddr, nr_pages, grefs); + if (ret) + goto err; + + return 0; + + err: + if (*vaddr) + free_pages_exact(*vaddr, ring_size); + for (i =3D 0; i < nr_pages; i++) + grefs[i] =3D INVALID_GRANT_REF; + *vaddr =3D NULL; + + return ret; +} +EXPORT_SYMBOL_GPL(xenbus_setup_ring); + +/* + * xenbus_teardown_ring + * @vaddr: starting virtual address of the ring + * @nr_pages: number of pages + * @grefs: grant reference array + * + * Remove grants for the shared ring buffer and free the associated memory. + * On return the grant reference array is filled with INVALID_GRANT_REF. + */ +void xenbus_teardown_ring(void **vaddr, unsigned int nr_pages, + grant_ref_t *grefs) +{ + unsigned int i; + + for (i =3D 0; i < nr_pages; i++) { + if (grefs[i] !=3D INVALID_GRANT_REF) { + gnttab_end_foreign_access(grefs[i], 0); + grefs[i] =3D INVALID_GRANT_REF; + } + } + + if (*vaddr) + free_pages_exact(*vaddr, nr_pages * XEN_PAGE_SIZE); + *vaddr =3D NULL; +} +EXPORT_SYMBOL_GPL(xenbus_teardown_ring); =20 /** * Allocate an event channel for the given xenbus_device, assigning the ne= wly diff --git a/include/xen/xenbus.h b/include/xen/xenbus.h index b13eb86395e0..b533b4adc835 100644 --- a/include/xen/xenbus.h +++ b/include/xen/xenbus.h @@ -226,6 +226,10 @@ int xenbus_watch_pathfmt(struct xenbus_device *dev, st= ruct xenbus_watch *watch, int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state new_s= tate); int xenbus_grant_ring(struct xenbus_device *dev, void *vaddr, unsigned int nr_pages, grant_ref_t *grefs); +int xenbus_setup_ring(struct xenbus_device *dev, gfp_t gfp, void **vaddr, + unsigned int nr_pages, grant_ref_t *grefs); +void xenbus_teardown_ring(void **vaddr, unsigned int nr_pages, + grant_ref_t *grefs); int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_ref= s, unsigned int nr_grefs, void **vaddr); =20 --=20 2.34.1