From nobody Thu Oct 30 22:41:36 2025 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=1753786931; cv=none; d=zohomail.com; s=zohoarc; b=kiFTZXdWpic1oenO56x82wzD7bqfrsbuM0sJijFp6gnaQ5LqiyPEB5asUPQaW/w15EwCLutSVYiIUXEU3n/ipr2BJq3NFRpgalOpfLitXzkm8FK+gWC3Qt4mkke/zhKa+6krK8pF0PfZDbml2QoSlHSUVh3yki6jOkwlcGvsrP4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753786931; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=vNHDZ/hMeBIZaL0gCeA1zWXL3NyMvP7GkyxquC6Es+I=; b=AwQRbGs7pOdxgERfOCukqQgFFkaX82bpW7Jay4lWCF5DTixv3sAO7GyBDO5xfBTryWk/kbObSTz/SpjOXR3dk/3QSc2YEcgfW/SWT8rnbxQeHw+C1RO4o4C7pcd3kToMA9ekA9Cti7K5sxbXEcDr+CvnIx9Vfae9ygE1w956V+A= 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 1753786931580478.47351338393196; Tue, 29 Jul 2025 04:02:11 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1062436.1428115 (Exim 4.92) (envelope-from ) id 1ugi5k-000280-Vj; Tue, 29 Jul 2025 11:01:56 +0000 Received: by outflank-mailman (output) from mailman id 1062436.1428115; Tue, 29 Jul 2025 11:01:56 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ugi5k-00027t-SQ; Tue, 29 Jul 2025 11:01:56 +0000 Received: by outflank-mailman (input) for mailman id 1062436; Tue, 29 Jul 2025 11:01:56 +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 1ugi5k-0001sN-5Z for xen-devel@lists.xenproject.org; Tue, 29 Jul 2025 11:01:56 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 708cb585-6c6b-11f0-a31e-13f23c93f187; Tue, 29 Jul 2025 13:01:55 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 1439F1F750; Tue, 29 Jul 2025 11:01:55 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id D718A13876; Tue, 29 Jul 2025 11:01:54 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id xvoZMyKqiGi/bQAAD6G6ig (envelope-from ); Tue, 29 Jul 2025 11:01:54 +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: 708cb585-6c6b-11f0-a31e-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1753786915; 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=vNHDZ/hMeBIZaL0gCeA1zWXL3NyMvP7GkyxquC6Es+I=; b=Upf0DpB6Yx8EyxvGsFXYXgSzQcvLIDsrvfVGHGjHUqKoyqMrbEMMvWL6Wf6FFQoTcwDD7e /m/PNl+z16DelBqsTzHtXhwih1TxGez2FcXVxK78I5iVii8T/RWBK6KYaYEd3xL7MWEEvw d5wqo2zMslKedeiXgxekBXnL1pK2ejM= Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=Upf0DpB6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1753786915; 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=vNHDZ/hMeBIZaL0gCeA1zWXL3NyMvP7GkyxquC6Es+I=; b=Upf0DpB6Yx8EyxvGsFXYXgSzQcvLIDsrvfVGHGjHUqKoyqMrbEMMvWL6Wf6FFQoTcwDD7e /m/PNl+z16DelBqsTzHtXhwih1TxGez2FcXVxK78I5iVii8T/RWBK6KYaYEd3xL7MWEEvw d5wqo2zMslKedeiXgxekBXnL1pK2ejM= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Julien Grall , Anthony PERARD , Jason Andryuk Subject: [PATCH v2 1/9] xenstored: use fread() instead of mmap() for reading live update state Date: Tue, 29 Jul 2025 13:01:37 +0200 Message-ID: <20250729110146.10580-2-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250729110146.10580-1-jgross@suse.com> References: <20250729110146.10580-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Level: X-Spam-Flag: NO X-Rspamd-Queue-Id: 1439F1F750 X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:rdns,imap1.dmz-prg2.suse.org:helo,suse.com:dkim,suse.com:mid,suse.com:email]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FUZZY_RATELIMITED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; RCPT_COUNT_FIVE(0.00)[5]; DKIM_TRACE(0.00)[suse.com:+] X-Spam-Score: -3.01 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1753786933471116600 Content-Type: text/plain; charset="utf-8" Mini-OS doesn't support using mmap() for accessing a file. In order to support reading the live update state from a 9pfs based file, use fread() instead of mmap(). Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- V2: - move to start of series --- tools/xenstored/lu.c | 95 +++++++++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 36 deletions(-) diff --git a/tools/xenstored/lu.c b/tools/xenstored/lu.c index 77e0d377c5..f2c8b92d07 100644 --- a/tools/xenstored/lu.c +++ b/tools/xenstored/lu.c @@ -27,9 +27,11 @@ struct live_update *lu_status; =20 struct lu_dump_state { void *buf; + unsigned int buf_size; unsigned int size; - int fd; + unsigned int offset; char *filename; + FILE *fp; }; =20 static int lu_destroy(void *data) @@ -80,9 +82,10 @@ bool lu_is_pending(void) return lu_status !=3D NULL; } =20 -static void lu_get_dump_state(struct lu_dump_state *state) +static void lu_get_dump_state(void *ctx, struct lu_dump_state *state) { struct stat statbuf; + int fd; =20 state->size =3D 0; =20 @@ -91,82 +94,107 @@ static void lu_get_dump_state(struct lu_dump_state *st= ate) if (!state->filename) barf("Allocation failure"); =20 - state->fd =3D open(state->filename, O_RDONLY); - if (state->fd < 0) - return; - if (fstat(state->fd, &statbuf) !=3D 0) - goto out_close; + fd =3D open(state->filename, O_RDONLY); + if (fd < 0) + barf("No state file found"); + if (fstat(fd, &statbuf) !=3D 0) + barf("Could not fstat state file"); state->size =3D statbuf.st_size; =20 - state->buf =3D mmap(NULL, state->size, PROT_READ, MAP_PRIVATE, - state->fd, 0); - if (state->buf =3D=3D MAP_FAILED) { - state->size =3D 0; - goto out_close; - } + /* Start with a 4k buffer. If needed we'll reallocate a larger one. */ + state->buf_size =3D 4096; + state->buf =3D talloc_size(ctx, state->buf_size); + if (!state->buf) + barf("Allocation failure"); =20 - return; + state->fp =3D fdopen(fd, "r"); +} =20 - out_close: - close(state->fd); +static void lu_dump_close(FILE *fp) +{ + fclose(fp); } =20 static void lu_close_dump_state(struct lu_dump_state *state) { assert(state->filename !=3D NULL); =20 - munmap(state->buf, state->size); - close(state->fd); + lu_dump_close(state->fp); =20 unlink(state->filename); talloc_free(state->filename); + talloc_free(state->buf); +} + +static void lu_read_data(void *ctx, struct lu_dump_state *state, + unsigned int size) +{ + if (state->offset + size > state->size) + barf("Inconsistent state data"); + + if (size > state->buf_size) { + state->buf =3D talloc_realloc_size(ctx, state->buf, size); + if (!state->buf) + barf("Allocation failure"); + state->buf_size =3D size; + } + + if (fread(state->buf, size, 1, state->fp) !=3D 1) + barf("State read error"); + + state->offset +=3D size; } =20 void lu_read_state(void) { struct lu_dump_state state =3D {}; - struct xs_state_record_header *head; + struct xs_state_record_header head; void *ctx =3D talloc_new(NULL); /* Work context for subfunctions. */ struct xs_state_preamble *pre; unsigned int version; =20 syslog(LOG_INFO, "live-update: read state\n"); - lu_get_dump_state(&state); + lu_get_dump_state(ctx, &state); if (state.size =3D=3D 0) barf_perror("No state found after live-update"); =20 + lu_read_data(ctx, &state, sizeof(*pre)); pre =3D state.buf; version =3D be32toh(pre->version); if (memcmp(pre->ident, XS_STATE_IDENT, sizeof(pre->ident)) || !version || version > XS_STATE_VERSION || pre->flags !=3D XS_STATE_FLAGS) barf("Unknown record identifier"); - for (head =3D state.buf + sizeof(*pre); - head->type !=3D XS_STATE_TYPE_END && - (void *)head - state.buf < state.size; - head =3D (void *)head + sizeof(*head) + head->length) { - switch (head->type) { + + for (;;) { + lu_read_data(ctx, &state, sizeof(head)); + head =3D *(struct xs_state_record_header *)(state.buf); + if (head.type =3D=3D XS_STATE_TYPE_END) + break; + lu_read_data(ctx, &state, head.length); + + switch (head.type) { case XS_STATE_TYPE_GLOBAL: - read_state_global(ctx, head + 1); + read_state_global(ctx, state.buf); break; case XS_STATE_TYPE_CONN: - read_state_connection(ctx, head + 1); + read_state_connection(ctx, state.buf); break; case XS_STATE_TYPE_WATCH: - read_state_watch(ctx, head + 1); + read_state_watch(ctx, state.buf); break; case XS_STATE_TYPE_TA: xprintf("live-update: ignore transaction record\n"); break; case XS_STATE_TYPE_NODE: - read_state_node(ctx, head + 1); + read_state_node(ctx, state.buf); break; case XS_STATE_TYPE_DOMAIN: - read_state_domain(ctx, head + 1, version); + read_state_domain(ctx, state.buf, version); break; default: xprintf("live-update: unknown state record %08x\n", - head->type); + head.type); break; } } @@ -271,11 +299,6 @@ static FILE *lu_dump_open(const void *ctx) return fdopen(fd, "w"); } =20 -static void lu_dump_close(FILE *fp) -{ - fclose(fp); -} - static const char *lu_dump_state(const void *ctx, struct connection *conn) { FILE *fp; --=20 2.43.0