From nobody Mon Feb 9 04:59:27 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=1606810930; cv=none; d=zohomail.com; s=zohoarc; b=iY/aBb3Kjcc4WO2R8cQdvwt7aRd6tpwkaMCH4cGUig4dy9j36XRNMwg8rOgGJRUd46vTJCxhzC5R43E5gKG3n9FVUUg56+IjYlVo3UMydL5P+B7ckc+ed/JYBN61uNtaEETHRYDd5pokK5IhSe8p8MjBw9VJYt/hoJebCDFyUoU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1606810930; 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=kUqinpHgs7lQk4VZmE9ndgJqbAEUMbPxpoEKorm7mzA=; b=J2xLu8hgK7eje/t6krsGdQRimNL0r3F6muhdIzWMGFZH6Nfb0J+qIcbecGI0p/ytROZB+vonRILTaKOXg1jpqqNfkdPw81JwVNpSTxXG1Yi3hJZpgKQfTM20KRRUj+p5MLPCZ8sPo2S3kulw6I8pBpMTrcErWPYNs1c4/Nmkq3s= 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 1606810930501659.8747923457175; Tue, 1 Dec 2020 00:22:10 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.41639.75006 (Exim 4.92) (envelope-from ) id 1kk0v4-00054R-9L; Tue, 01 Dec 2020 08:21:54 +0000 Received: by outflank-mailman (output) from mailman id 41639.75006; Tue, 01 Dec 2020 08:21:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kk0v4-00054E-4b; Tue, 01 Dec 2020 08:21:54 +0000 Received: by outflank-mailman (input) for mailman id 41639; Tue, 01 Dec 2020 08:21:52 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kk0v2-0004VK-5i for xen-devel@lists.xenproject.org; Tue, 01 Dec 2020 08:21:52 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 1c2e290f-45b7-4f0b-88e8-d03f1bef03bd; Tue, 01 Dec 2020 08:21:33 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id AB4D9AF0C; Tue, 1 Dec 2020 08:21:32 +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: 1c2e290f-45b7-4f0b-88e8-d03f1bef03bd 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=1606810892; 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=kUqinpHgs7lQk4VZmE9ndgJqbAEUMbPxpoEKorm7mzA=; b=aLg6UtsdqCn5u7PfntKCiyCpCF7kvj0R65v/6un82KaisYDyo2w/IllrDI1vPDMZ7C6asF fZ6A/M0p8FXu0LnT+JFWn3Lf5nqHF47aEk9dVs8F9qe2PTnaAHulrmq3lsNiTSHUIO2yno L8DmQOD0hJeA4od635U9bI5hsdEPp+w= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Andrew Cooper , George Dunlap , Ian Jackson , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu Subject: [PATCH v2 09/17] xen/hypfs: move per-node function pointers into a dedicated struct Date: Tue, 1 Dec 2020 09:21:20 +0100 Message-Id: <20201201082128.15239-10-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201201082128.15239-1-jgross@suse.com> References: <20201201082128.15239-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" Move the function pointers currently stored in each hypfs node into a dedicated structure in order to save some space for each node. This will save even more space with additional callbacks added in future. Provide some standard function vectors. Instead of testing the write pointer to be not NULL provide a dummy function just returning -EACCESS. ASSERT() all vector entries being populated when adding a node. This avoids any potential problem (e.g. pv domain privilege escalations) in case of calling a non populated vector entry. Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich --- V2: - make function vector const (Jan Beulich) - don't allow any NULL entry (Jan Beulich) - add callback comment --- xen/common/hypfs.c | 41 ++++++++++++++++++++---- xen/include/xen/hypfs.h | 71 ++++++++++++++++++++++++++++------------- xen/include/xen/param.h | 15 +++------ 3 files changed, 88 insertions(+), 39 deletions(-) diff --git a/xen/common/hypfs.c b/xen/common/hypfs.c index 8e932b5cf9..7befd144ba 100644 --- a/xen/common/hypfs.c +++ b/xen/common/hypfs.c @@ -24,6 +24,27 @@ CHECK_hypfs_dirlistentry; (DIRENTRY_NAME_OFF + \ ROUNDUP((name_len) + 1, alignof(struct xen_hypfs_direntry))) =20 +const struct hypfs_funcs hypfs_dir_funcs =3D { + .read =3D hypfs_read_dir, + .write =3D hypfs_write_deny, +}; +const struct hypfs_funcs hypfs_leaf_ro_funcs =3D { + .read =3D hypfs_read_leaf, + .write =3D hypfs_write_deny, +}; +const struct hypfs_funcs hypfs_leaf_wr_funcs =3D { + .read =3D hypfs_read_leaf, + .write =3D hypfs_write_leaf, +}; +const struct hypfs_funcs hypfs_bool_wr_funcs =3D { + .read =3D hypfs_read_leaf, + .write =3D hypfs_write_bool, +}; +const struct hypfs_funcs hypfs_custom_wr_funcs =3D { + .read =3D hypfs_read_leaf, + .write =3D hypfs_write_custom, +}; + static DEFINE_RWLOCK(hypfs_lock); enum hypfs_lock_state { hypfs_unlocked, @@ -74,6 +95,9 @@ static int add_entry(struct hypfs_entry_dir *parent, stru= ct hypfs_entry *new) int ret =3D -ENOENT; struct hypfs_entry *e; =20 + ASSERT(new->funcs->read); + ASSERT(new->funcs->write); + hypfs_write_lock(); =20 list_for_each_entry ( e, &parent->dirlist, list ) @@ -284,7 +308,7 @@ static int hypfs_read(const struct hypfs_entry *entry, =20 guest_handle_add_offset(uaddr, sizeof(e)); =20 - ret =3D entry->read(entry, uaddr); + ret =3D entry->funcs->read(entry, uaddr); =20 out: return ret; @@ -297,6 +321,7 @@ int hypfs_write_leaf(struct hypfs_entry_leaf *leaf, int ret; =20 ASSERT(this_cpu(hypfs_locked) =3D=3D hypfs_write_locked); + ASSERT(leaf->e.max_size); =20 if ( ulen > leaf->e.max_size ) return -ENOSPC; @@ -357,6 +382,7 @@ int hypfs_write_custom(struct hypfs_entry_leaf *leaf, int ret; =20 ASSERT(this_cpu(hypfs_locked) =3D=3D hypfs_write_locked); + ASSERT(leaf->e.max_size); =20 /* Avoid oversized buffer allocation. */ if ( ulen > MAX_PARAM_SIZE ) @@ -382,19 +408,20 @@ int hypfs_write_custom(struct hypfs_entry_leaf *leaf, return ret; } =20 +int hypfs_write_deny(struct hypfs_entry_leaf *leaf, + XEN_GUEST_HANDLE_PARAM(void) uaddr, unsigned int ulen) +{ + return -EACCES; +} + static int hypfs_write(struct hypfs_entry *entry, XEN_GUEST_HANDLE_PARAM(void) uaddr, unsigned long u= len) { struct hypfs_entry_leaf *l; =20 - if ( !entry->write ) - return -EACCES; - - ASSERT(entry->max_size); - l =3D container_of(entry, struct hypfs_entry_leaf, e); =20 - return entry->write(l, uaddr, ulen); + return entry->funcs->write(l, uaddr, ulen); } =20 long do_hypfs_op(unsigned int cmd, diff --git a/xen/include/xen/hypfs.h b/xen/include/xen/hypfs.h index 5ad99cb558..25fdf3ead7 100644 --- a/xen/include/xen/hypfs.h +++ b/xen/include/xen/hypfs.h @@ -7,6 +7,32 @@ #include =20 struct hypfs_entry_leaf; +struct hypfs_entry; + +/* + * Per-node callbacks: + * + * The callbacks are always called with the hypfs lock held. + * + * The read() callback is used to return the contents of a node (either + * directory or leaf). It is NOT used to get directory entries during trav= ersal + * of the tree. + * + * The write() callback is used to modify the contents of a node. Writing + * directories is not supported (this means all nodes are added at boot ti= me). + */ +struct hypfs_funcs { + int (*read)(const struct hypfs_entry *entry, + XEN_GUEST_HANDLE_PARAM(void) uaddr); + int (*write)(struct hypfs_entry_leaf *leaf, + XEN_GUEST_HANDLE_PARAM(void) uaddr, unsigned int ulen); +}; + +extern const struct hypfs_funcs hypfs_dir_funcs; +extern const struct hypfs_funcs hypfs_leaf_ro_funcs; +extern const struct hypfs_funcs hypfs_leaf_wr_funcs; +extern const struct hypfs_funcs hypfs_bool_wr_funcs; +extern const struct hypfs_funcs hypfs_custom_wr_funcs; =20 struct hypfs_entry { unsigned short type; @@ -15,10 +41,7 @@ struct hypfs_entry { unsigned int max_size; const char *name; struct list_head list; - int (*read)(const struct hypfs_entry *entry, - XEN_GUEST_HANDLE_PARAM(void) uaddr); - int (*write)(struct hypfs_entry_leaf *leaf, - XEN_GUEST_HANDLE_PARAM(void) uaddr, unsigned int ulen); + const struct hypfs_funcs *funcs; }; =20 struct hypfs_entry_leaf { @@ -42,7 +65,7 @@ struct hypfs_entry_dir { .e.size =3D 0, \ .e.max_size =3D 0, \ .e.list =3D LIST_HEAD_INIT(var.e.list), \ - .e.read =3D hypfs_read_dir, \ + .e.funcs =3D &hypfs_dir_funcs, \ .dirlist =3D LIST_HEAD_INIT(var.dirlist), \ } =20 @@ -52,7 +75,7 @@ struct hypfs_entry_dir { .e.encoding =3D XEN_HYPFS_ENC_PLAIN, \ .e.name =3D (nam), \ .e.max_size =3D (msz), \ - .e.read =3D hypfs_read_leaf, \ + .e.funcs =3D &hypfs_leaf_ro_funcs, \ } =20 /* Content and size need to be set via hypfs_string_set_reference(). */ @@ -72,35 +95,37 @@ static inline void hypfs_string_set_reference(struct hy= pfs_entry_leaf *leaf, leaf->e.size =3D strlen(str) + 1; } =20 -#define HYPFS_FIXEDSIZE_INIT(var, typ, nam, contvar, wr) \ - struct hypfs_entry_leaf __read_mostly var =3D { \ - .e.type =3D (typ), \ - .e.encoding =3D XEN_HYPFS_ENC_PLAIN, \ - .e.name =3D (nam), \ - .e.size =3D sizeof(contvar), \ - .e.max_size =3D (wr) ? sizeof(contvar) : 0, \ - .e.read =3D hypfs_read_leaf, \ - .e.write =3D (wr), \ - .u.content =3D &(contvar), \ +#define HYPFS_FIXEDSIZE_INIT(var, typ, nam, contvar, fn, wr) \ + struct hypfs_entry_leaf __read_mostly var =3D { \ + .e.type =3D (typ), \ + .e.encoding =3D XEN_HYPFS_ENC_PLAIN, \ + .e.name =3D (nam), \ + .e.size =3D sizeof(contvar), \ + .e.max_size =3D (wr) ? sizeof(contvar) : 0, \ + .e.funcs =3D (fn), \ + .u.content =3D &(contvar), \ } =20 #define HYPFS_UINT_INIT(var, nam, contvar) \ - HYPFS_FIXEDSIZE_INIT(var, XEN_HYPFS_TYPE_UINT, nam, contvar, NULL) + HYPFS_FIXEDSIZE_INIT(var, XEN_HYPFS_TYPE_UINT, nam, contvar, \ + &hypfs_leaf_ro_funcs, 0) #define HYPFS_UINT_INIT_WRITABLE(var, nam, contvar) \ HYPFS_FIXEDSIZE_INIT(var, XEN_HYPFS_TYPE_UINT, nam, contvar, \ - hypfs_write_leaf) + &hypfs_leaf_wr_funcs, 1) =20 #define HYPFS_INT_INIT(var, nam, contvar) \ - HYPFS_FIXEDSIZE_INIT(var, XEN_HYPFS_TYPE_INT, nam, contvar, NULL) + HYPFS_FIXEDSIZE_INIT(var, XEN_HYPFS_TYPE_INT, nam, contvar, \ + &hypfs_leaf_ro_funcs, 0) #define HYPFS_INT_INIT_WRITABLE(var, nam, contvar) \ HYPFS_FIXEDSIZE_INIT(var, XEN_HYPFS_TYPE_INT, nam, contvar, \ - hypfs_write_leaf) + &hypfs_leaf_wr_funcs, 1) =20 #define HYPFS_BOOL_INIT(var, nam, contvar) \ - HYPFS_FIXEDSIZE_INIT(var, XEN_HYPFS_TYPE_BOOL, nam, contvar, NULL) + HYPFS_FIXEDSIZE_INIT(var, XEN_HYPFS_TYPE_BOOL, nam, contvar, \ + &hypfs_leaf_ro_funcs, 0) #define HYPFS_BOOL_INIT_WRITABLE(var, nam, contvar) \ HYPFS_FIXEDSIZE_INIT(var, XEN_HYPFS_TYPE_BOOL, nam, contvar, \ - hypfs_write_bool) + &hypfs_bool_wr_funcs, 1) =20 extern struct hypfs_entry_dir hypfs_root; =20 @@ -112,6 +137,8 @@ int hypfs_read_dir(const struct hypfs_entry *entry, XEN_GUEST_HANDLE_PARAM(void) uaddr); int hypfs_read_leaf(const struct hypfs_entry *entry, XEN_GUEST_HANDLE_PARAM(void) uaddr); +int hypfs_write_deny(struct hypfs_entry_leaf *leaf, + XEN_GUEST_HANDLE_PARAM(void) uaddr, unsigned int ulen= ); int hypfs_write_leaf(struct hypfs_entry_leaf *leaf, XEN_GUEST_HANDLE_PARAM(void) uaddr, unsigned int ulen= ); int hypfs_write_bool(struct hypfs_entry_leaf *leaf, diff --git a/xen/include/xen/param.h b/xen/include/xen/param.h index d0409d3a0e..1b2c7db954 100644 --- a/xen/include/xen/param.h +++ b/xen/include/xen/param.h @@ -116,8 +116,7 @@ extern struct param_hypfs __paramhypfs_start[], __param= hypfs_end[]; { .hypfs.e.type =3D XEN_HYPFS_TYPE_STRING, \ .hypfs.e.encoding =3D XEN_HYPFS_ENC_PLAIN, \ .hypfs.e.name =3D (nam), \ - .hypfs.e.read =3D hypfs_read_leaf, \ - .hypfs.e.write =3D hypfs_write_custom, \ + .hypfs.e.funcs =3D &hypfs_custom_wr_funcs, \ .init_leaf =3D (initfunc), \ .func =3D (variable) } #define boolean_runtime_only_param(nam, variable) \ @@ -127,8 +126,7 @@ extern struct param_hypfs __paramhypfs_start[], __param= hypfs_end[]; .hypfs.e.name =3D (nam), \ .hypfs.e.size =3D sizeof(variable), \ .hypfs.e.max_size =3D sizeof(variable), \ - .hypfs.e.read =3D hypfs_read_leaf, \ - .hypfs.e.write =3D hypfs_write_bool, \ + .hypfs.e.funcs =3D &hypfs_bool_wr_funcs, \ .hypfs.u.content =3D &(variable) } #define integer_runtime_only_param(nam, variable) \ __paramfs __parfs_##variable =3D \ @@ -137,8 +135,7 @@ extern struct param_hypfs __paramhypfs_start[], __param= hypfs_end[]; .hypfs.e.name =3D (nam), \ .hypfs.e.size =3D sizeof(variable), \ .hypfs.e.max_size =3D sizeof(variable), \ - .hypfs.e.read =3D hypfs_read_leaf, \ - .hypfs.e.write =3D hypfs_write_leaf, \ + .hypfs.e.funcs =3D &hypfs_leaf_wr_funcs, \ .hypfs.u.content =3D &(variable) } #define size_runtime_only_param(nam, variable) \ __paramfs __parfs_##variable =3D \ @@ -147,8 +144,7 @@ extern struct param_hypfs __paramhypfs_start[], __param= hypfs_end[]; .hypfs.e.name =3D (nam), \ .hypfs.e.size =3D sizeof(variable), \ .hypfs.e.max_size =3D sizeof(variable), \ - .hypfs.e.read =3D hypfs_read_leaf, \ - .hypfs.e.write =3D hypfs_write_leaf, \ + .hypfs.e.funcs =3D &hypfs_leaf_wr_funcs, \ .hypfs.u.content =3D &(variable) } #define string_runtime_only_param(nam, variable) \ __paramfs __parfs_##variable =3D \ @@ -157,8 +153,7 @@ extern struct param_hypfs __paramhypfs_start[], __param= hypfs_end[]; .hypfs.e.name =3D (nam), \ .hypfs.e.size =3D 0, \ .hypfs.e.max_size =3D sizeof(variable), \ - .hypfs.e.read =3D hypfs_read_leaf, \ - .hypfs.e.write =3D hypfs_write_leaf, \ + .hypfs.e.funcs =3D &hypfs_leaf_wr_funcs, \ .hypfs.u.content =3D &(variable) } =20 #else --=20 2.26.2