From: "Daniel P. Berrangé" <berrange@redhat.com>
To: "Maciej S. Szmigiero" <mail@maciej.szmigiero.name>
Cc: "Filip Hejsek" <filip.hejsek@gmail.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
qemu-devel@nongnu.org, "Paolo Bonzini" <pbonzini@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Michael Tokarev" <mjt@tls.msk.ru>,
"Laurent Vivier" <laurent@vivier.eu>,
"Eric Blake" <eblake@redhat.com>,
"Vladimir Sementsov-Ogievskiy" <vsementsov@yandex-team.ru>,
qemu-block@nongnu.org, "Peter Xu" <peterx@redhat.com>,
"Fabiano Rosas" <farosas@suse.de>,
"Mark Kanda" <mark.kanda@oracle.com>,
"Ben Chaney" <bchaney@akamai.com>,
"Igor Mammedov" <imammedo@redhat.com>,
"Ani Sinha" <anisinha@redhat.com>
Subject: Re: [PATCH v4] treewide: use qemu_dup() instead of dup()
Date: Tue, 24 Nov 2026 08:28:36 +0000 [thread overview]
Message-ID: <awVKtNi4M_1BsZvt@redhat.com> (raw)
In-Reply-To: <52cc9199-8043-4068-83a3-f5462a236e68@maciej.szmigiero.name>
On Mon, Nov 23, 2026 at 07:15:58PM +0100, Maciej S. Szmigiero wrote:
> On 12.11.2026 17:17, Filip Hejsek wrote:
> > Move qemu_dup() out of an #ifndef so it can be called from code that
> > runs on Windows. Then use it instead of dup() to ensure the cloexec
> > flag is set on dup'd fds.
> >
> > The only direct users of dup() are now guest syscalls in the user
> > emulators and the implementation of qemu_dup() itself.
> >
> > Signed-off-by: Filip Hejsek <filip.hejsek@gmail.com>
> > ---
> > I fixed the Windows build now, checked with docker-test-mingw. I had
> > to remove ENABLE_RUST=1 in fedora-win64-cross.docker, because Rust
> > cross-compilation was broken.
> >
> > Changes in v4:
> > - Fix Windows build
> > - Link to v3: https://lore.kernel.org/qemu-devel/20261015-use-qemu-dup-v3-1-ca02b5927725@gmail.com
> >
> > Changes in v3:
> > - Also replace the use in migration/cpr-exec.c and the tests
> > - Link to v2: https://lore.kernel.org/qemu-devel/20261013-use-qemu-dup-v2-1-12ecd94263b7@gmail.com
> >
> > Changes in v2:
> > - Replace the use in qemu-nbd.c too
> > - Changed commit message
> > - Link to v1: https://lore.kernel.org/qemu-devel/20261012-use-qemu-dup-v1-1-03d94852a6c8@gmail.com
> > ---
> > hw/virtio/virtio.c | 2 +-
> > include/qemu/osdep.h | 2 +-
> > io/channel-file.c | 2 +-
> > migration/cpr-exec.c | 2 +-
> > qemu-nbd.c | 2 +-
> > system/vl.c | 9 +--------
> > tests/qtest/bios-tables-test.c | 2 +-
> > tests/unit/test-util-sockets.c | 6 +++---
> > tests/unit/test-vmstate.c | 2 +-
> > ui/dmabuf.c | 2 +-
> > util/osdep.c | 28 ++++++++++++++--------------
> > 11 files changed, 26 insertions(+), 33 deletions(-)
> >
> > diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
> > index c9f9e5b511..70779fbd56 100644
> > --- a/hw/virtio/virtio.c
> > +++ b/hw/virtio/virtio.c
> > @@ -3204,7 +3204,7 @@ VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new(uint8_t shmid,
> > return NULL;
> > }
> > - fd = dup(fd);
> > + fd = qemu_dup(fd);
> > if (fd < 0) {
> > error_report("Failed to duplicate fd: %s", strerror(errno));
> > return NULL;
> > diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> > index b27d159d5b..b5dbfc8a8a 100644
> > --- a/include/qemu/osdep.h
> > +++ b/include/qemu/osdep.h
> > @@ -653,9 +653,9 @@ int qemu_open(const char *name, int flags, Error **errp);
> > int qemu_create(const char *name, int flags, mode_t mode, Error **errp);
> > int qemu_close(int fd);
> > int qemu_unlink(const char *name);
> > +int qemu_dup(int fd);
> > #ifndef _WIN32
> > int qemu_dup_flags(int fd, int flags);
> > -int qemu_dup(int fd);
> > int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);
> > int qemu_unlock_fd(int fd, int64_t start, int64_t len);
> > int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive);
> > diff --git a/io/channel-file.c b/io/channel-file.c
> > index 4c9da6619b..548a633be8 100644
> > --- a/io/channel-file.c
> > +++ b/io/channel-file.c
> > @@ -54,7 +54,7 @@ qio_channel_file_new_fd(int fd)
> > QIOChannelFile *
> > qio_channel_file_new_dupfd(int fd, Error **errp)
> > {
> > - int newfd = dup(fd);
> > + int newfd = qemu_dup(fd);
> > if (newfd < 0) {
> > error_setg_errno(errp, errno, "Could not dup FD %d", fd);
> > diff --git a/migration/cpr-exec.c b/migration/cpr-exec.c
> > index daa50916d2..2f38db30bb 100644
> > --- a/migration/cpr-exec.c
> > +++ b/migration/cpr-exec.c
> > @@ -44,7 +44,7 @@ bool cpr_exec_persist_state(QEMUFile *f, Error **errp)
> > {
> > QIOChannelFile *fioc = QIO_CHANNEL_FILE(qemu_file_get_ioc(f));
> > /* coverity[leaked_storage] - mfd intentionally kept open across exec() */
> > - int mfd = dup(fioc->fd);
> > + int mfd = qemu_dup(fioc->fd);
> > char val[16];
> > /* Remember mfd in environment for post-exec load */
> The code below immediately clears that CLOEXEC flag on this new fd
> so I'm not sure if there's any value in switching to qemu_dup() here.
The benefit is that we eliminate all use of plain 'dup', such that
we know all FDs start with CLOEXEC set. Any code to then explicitly
clear CLOEXEC then stands out as a deliberate design choice, as
opposed to dup() usage which is mostly likely to be an accident.
With regards,
Daniel
--
|: https://berrange.com ~~ https://hachyderm.io/@berrange :|
|: https://libvirt.org ~~ https://entangle-photo.org :|
|: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
prev parent reply other threads:[~2026-11-24 8:29 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 16:17 [PATCH v4] treewide: use qemu_dup() instead of dup() Filip Hejsek
2026-11-12 19:18 ` Eric Blake
2026-11-23 18:15 ` Maciej S. Szmigiero
2026-11-24 8:28 ` Daniel P. Berrangé [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=awVKtNi4M_1BsZvt@redhat.com \
--to=berrange@redhat.com \
--cc=anisinha@redhat.com \
--cc=bchaney@akamai.com \
--cc=eblake@redhat.com \
--cc=farosas@suse.de \
--cc=filip.hejsek@gmail.com \
--cc=imammedo@redhat.com \
--cc=laurent@vivier.eu \
--cc=mail@maciej.szmigiero.name \
--cc=marcandre.lureau@redhat.com \
--cc=mark.kanda@oracle.com \
--cc=mjt@tls.msk.ru \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=vsementsov@yandex-team.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.