From nobody Sat Apr 5 18:32:58 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=suse.de ARC-Seal: i=1; a=rsa-sha256; t=1738166650; cv=none; d=zohomail.com; s=zohoarc; b=n0uz+CKgelEyErpbNlBF0+juuxni97LtJ3kq7lE3QL2g44TM1LWM+nvpRIjfmQXQt5Ft05FRSNT8/H07VjZcpgn/CeqfLiosj5mCLUb8bLbu0NIkrA9nlk/F9ykhQ7cv5vL0bP4OUBNeOdHuVMtzgf4GWBPGfX+QQQgVsFLNVpE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1738166650; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=NBNZtSiI+Z0X4jH0jdTf9Edwx+1OSMQPOSIcGOrxR9A=; b=IP2nX7vKJ11cbP+DcD60DcosBiT6RAYy80ofqrrgCmupgOqGg7lbHqkA/VJkg7VUprt4CHWyxjzsyG9s/p8LSBUkcqMG3l9bzlmGBZhg47F87HN5bjV0Sb7wy3B7eicrY1jKy1c0ZKdwczZ6OQ2BzLU3ozRqUCSjpt8jDRmMpdw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1738166649020463.1923054593682; Wed, 29 Jan 2025 08:04:09 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tdAXZ-0006GH-LV; Wed, 29 Jan 2025 11:03:45 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tdAVX-0003HY-V9 for qemu-devel@nongnu.org; Wed, 29 Jan 2025 11:01:52 -0500 Received: from smtp-out2.suse.de ([2a07:de40:b251:101:10:150:64:2]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tdAVU-0005I0-QU for qemu-devel@nongnu.org; Wed, 29 Jan 2025 11:01:38 -0500 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 5703F1F365; Wed, 29 Jan 2025 16:01:35 +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 0E478137DB; Wed, 29 Jan 2025 16:01:33 +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 4BQIMN1QmmffPAAAD6G6ig (envelope-from ); Wed, 29 Jan 2025 16:01:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1738166495; 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=NBNZtSiI+Z0X4jH0jdTf9Edwx+1OSMQPOSIcGOrxR9A=; b=jfVycR95vFsR/QMhhtZVUTwCZ0UNZtUE7vE5YS2R/nEv/Cdeg4fl0eSDtc8p9GASfgX6Gd 8HeqgdWArGAOmaMEc9kRPmXKzcQgpcKhKiToMgipgIU7FUYaE8Q4Nbt4nK53cXVKyOGBBx mf7lFmmWVtDzzFPkHkZb04o05a3js3Y= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1738166495; 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=NBNZtSiI+Z0X4jH0jdTf9Edwx+1OSMQPOSIcGOrxR9A=; b=oqSVdwLgQtNTXoBhSgtnbTNxQKo6+gWcgC0kjGPaDb06hW7O2Cgx6izgFr1lU0XEDg8DoL E2TVGl9RaC3YCSBA== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=jfVycR95; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=oqSVdwLg DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1738166495; 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=NBNZtSiI+Z0X4jH0jdTf9Edwx+1OSMQPOSIcGOrxR9A=; b=jfVycR95vFsR/QMhhtZVUTwCZ0UNZtUE7vE5YS2R/nEv/Cdeg4fl0eSDtc8p9GASfgX6Gd 8HeqgdWArGAOmaMEc9kRPmXKzcQgpcKhKiToMgipgIU7FUYaE8Q4Nbt4nK53cXVKyOGBBx mf7lFmmWVtDzzFPkHkZb04o05a3js3Y= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1738166495; 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=NBNZtSiI+Z0X4jH0jdTf9Edwx+1OSMQPOSIcGOrxR9A=; b=oqSVdwLgQtNTXoBhSgtnbTNxQKo6+gWcgC0kjGPaDb06hW7O2Cgx6izgFr1lU0XEDg8DoL E2TVGl9RaC3YCSBA== From: Fabiano Rosas To: qemu-devel@nongnu.org Cc: Peter Xu , Steve Sistare Subject: [PULL 14/42] migration: SCM_RIGHTS for QEMUFile Date: Wed, 29 Jan 2025 13:00:31 -0300 Message-Id: <20250129160059.6987-15-farosas@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20250129160059.6987-1-farosas@suse.de> References: <20250129160059.6987-1-farosas@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [5.36 / 50.00]; BAYES_SPAM(5.07)[99.91%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; BAD_REP_POLICIES(0.10)[]; MX_GOOD(-0.01)[]; RCVD_COUNT_TWO(0.00)[2]; FUZZY_BLOCKED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; ARC_NA(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; R_DKIM_ALLOW(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; RCVD_VIA_SMTP_AUTH(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; FROM_EQ_ENVFROM(0.00)[]; DKIM_TRACE(0.00)[suse.de:+]; FROM_HAS_DN(0.00)[]; RCVD_TLS_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email, suse.de:dkim, suse.de:mid, oracle.com:email] X-Spam-Score: 5.36 X-Spamd-Bar: +++++ X-Rspamd-Queue-Id: 5703F1F365 X-Rspamd-Action: no action X-Rspamd-Server: rspamd2.dmz-prg2.suse.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a07:de40:b251:101:10:150:64:2; envelope-from=farosas@suse.de; helo=smtp-out2.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @suse.de) X-ZM-MESSAGEID: 1738166817461019100 Content-Type: text/plain; charset="utf-8" From: Steve Sistare Define functions to put/get file descriptors to/from a QEMUFile, for qio channels that support SCM_RIGHTS. Maintain ordering such that put(A), put(fd), put(B) followed by get(A), get(fd), get(B) always succeeds. Other get orderings may succeed but are not guaranteed. Signed-off-by: Steve Sistare Reviewed-by: Peter Xu Link: https://lore.kernel.org/r/1736967650-129648-14-git-send-email-steven.= sistare@oracle.com Signed-off-by: Fabiano Rosas --- migration/qemu-file.c | 84 ++++++++++++++++++++++++++++++++++++++++-- migration/qemu-file.h | 2 + migration/trace-events | 2 + 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/migration/qemu-file.c b/migration/qemu-file.c index b6d2f588bd..1303a5bf58 100644 --- a/migration/qemu-file.c +++ b/migration/qemu-file.c @@ -37,6 +37,11 @@ #define IO_BUF_SIZE 32768 #define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64) =20 +typedef struct FdEntry { + QTAILQ_ENTRY(FdEntry) entry; + int fd; +} FdEntry; + struct QEMUFile { QIOChannel *ioc; bool is_writable; @@ -51,6 +56,9 @@ struct QEMUFile { =20 int last_error; Error *last_error_obj; + + bool can_pass_fd; + QTAILQ_HEAD(, FdEntry) fds; }; =20 /* @@ -109,6 +117,8 @@ static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bo= ol is_writable) object_ref(ioc); f->ioc =3D ioc; f->is_writable =3D is_writable; + f->can_pass_fd =3D qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_FD= _PASS); + QTAILQ_INIT(&f->fds); =20 return f; } @@ -310,6 +320,10 @@ static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEM= UFile *f) int len; int pending; Error *local_error =3D NULL; + g_autofree int *fds =3D NULL; + size_t nfd =3D 0; + int **pfds =3D f->can_pass_fd ? &fds : NULL; + size_t *pnfd =3D f->can_pass_fd ? &nfd : NULL; =20 assert(!qemu_file_is_writable(f)); =20 @@ -325,10 +339,9 @@ static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEM= UFile *f) } =20 do { - len =3D qio_channel_read(f->ioc, - (char *)f->buf + pending, - IO_BUF_SIZE - pending, - &local_error); + struct iovec iov =3D { f->buf + pending, IO_BUF_SIZE - pending }; + len =3D qio_channel_readv_full(f->ioc, &iov, 1, pfds, pnfd, 0, + &local_error); if (len =3D=3D QIO_CHANNEL_ERR_BLOCK) { if (qemu_in_coroutine()) { qio_channel_yield(f->ioc, G_IO_IN); @@ -348,9 +361,66 @@ static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEM= UFile *f) qemu_file_set_error_obj(f, len, local_error); } =20 + for (int i =3D 0; i < nfd; i++) { + FdEntry *fde =3D g_new0(FdEntry, 1); + fde->fd =3D fds[i]; + QTAILQ_INSERT_TAIL(&f->fds, fde, entry); + } + return len; } =20 +int qemu_file_put_fd(QEMUFile *f, int fd) +{ + int ret =3D 0; + QIOChannel *ioc =3D qemu_file_get_ioc(f); + Error *err =3D NULL; + struct iovec iov =3D { (void *)" ", 1 }; + + /* + * Send a dummy byte so qemu_fill_buffer on the receiving side does not + * fail with a len=3D0 error. Flush first to maintain ordering wrt ot= her + * data. + */ + + qemu_fflush(f); + if (qio_channel_writev_full(ioc, &iov, 1, &fd, 1, 0, &err) < 1) { + error_report_err(error_copy(err)); + qemu_file_set_error_obj(f, -EIO, err); + ret =3D -1; + } + trace_qemu_file_put_fd(f->ioc->name, fd, ret); + return ret; +} + +int qemu_file_get_fd(QEMUFile *f) +{ + int fd =3D -1; + FdEntry *fde; + + if (!f->can_pass_fd) { + Error *err =3D NULL; + error_setg(&err, "%s does not support fd passing", f->ioc->name); + error_report_err(error_copy(err)); + qemu_file_set_error_obj(f, -EIO, err); + goto out; + } + + /* Force the dummy byte and its fd passenger to appear. */ + qemu_peek_byte(f, 0); + + fde =3D QTAILQ_FIRST(&f->fds); + if (fde) { + qemu_get_byte(f); /* Drop the dummy byte */ + fd =3D fde->fd; + QTAILQ_REMOVE(&f->fds, fde, entry); + g_free(fde); + } +out: + trace_qemu_file_get_fd(f->ioc->name, fd); + return fd; +} + /** Closes the file * * Returns negative error value if any error happened on previous operatio= ns or @@ -361,11 +431,17 @@ static ssize_t coroutine_mixed_fn qemu_fill_buffer(QE= MUFile *f) */ int qemu_fclose(QEMUFile *f) { + FdEntry *fde, *next; int ret =3D qemu_fflush(f); int ret2 =3D qio_channel_close(f->ioc, NULL); if (ret >=3D 0) { ret =3D ret2; } + QTAILQ_FOREACH_SAFE(fde, &f->fds, entry, next) { + warn_report("qemu_fclose: received fd %d was never claimed", fde->= fd); + close(fde->fd); + g_free(fde); + } g_clear_pointer(&f->ioc, object_unref); error_free(f->last_error_obj); g_free(f); diff --git a/migration/qemu-file.h b/migration/qemu-file.h index 11c2120edd..3e47a20621 100644 --- a/migration/qemu-file.h +++ b/migration/qemu-file.h @@ -79,5 +79,7 @@ size_t qemu_get_buffer_at(QEMUFile *f, const uint8_t *buf= , size_t buflen, off_t pos); =20 QIOChannel *qemu_file_get_ioc(QEMUFile *file); +int qemu_file_put_fd(QEMUFile *f, int fd); +int qemu_file_get_fd(QEMUFile *f); =20 #endif diff --git a/migration/trace-events b/migration/trace-events index 4e3061bc55..abd9cdf2a1 100644 --- a/migration/trace-events +++ b/migration/trace-events @@ -88,6 +88,8 @@ put_qlist_end(const char *field_name, const char *vmsd_na= me) "%s(%s)" =20 # qemu-file.c qemu_file_fclose(void) "" +qemu_file_put_fd(const char *name, int fd, int ret) "ioc %s, fd %d -> stat= us %d" +qemu_file_get_fd(const char *name, int fd) "ioc %s -> fd %d" =20 # ram.c get_queued_page(const char *block_name, uint64_t tmp_offset, unsigned long= page_abs) "%s/0x%" PRIx64 " page_abs=3D0x%lx" --=20 2.35.3