From nobody Mon Sep 22 21:05:45 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4cVwcw24hSz68R7S; Mon, 22 Sep 2025 21:05:52 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::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) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4cVwcv4xSgz3KDj; Mon, 22 Sep 2025 21:05:51 +0000 (UTC) (envelope-from kostikbel@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: from tom.home (kib@localhost [127.0.0.1] (may be forged)) by kib.kiev.ua (8.18.1/8.18.1) with ESMTP id 58ML5jMl059637; Tue, 23 Sep 2025 00:05:48 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 58ML5jMl059637 Received: (from kostik@localhost) by tom.home (8.18.1/8.18.1/Submit) id 58ML5jF4059636; Tue, 23 Sep 2025 00:05:45 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Tue, 23 Sep 2025 00:05:45 +0300 From: Konstantin Belousov To: John Baldwin Cc: src-committers@freebsd.org, dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org Subject: Re: git: 40a42785dbba - main - fcntl(F_SETFL): only allow one thread to perform F_SETFL Message-ID: References: <202509191419.58JEJsvj031867@gitrepo.freebsd.org> <92831372-745d-4612-b38f-aeb235dd8cca@FreeBSD.org> <02323a46-fb47-444b-812a-1ec199a654d1@FreeBSD.org> List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <02323a46-fb47-444b-812a-1ec199a654d1@FreeBSD.org> X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on tom.home X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Queue-Id: 4cVwcv4xSgz3KDj On Mon, Sep 22, 2025 at 09:50:09AM +0100, John Baldwin wrote: > On 9/22/25 04:41, John Baldwin wrote: > > On 9/19/25 10:19, Konstantin Belousov wrote: > > > The branch main has been updated by kib: > > > > > > URL: https://cgit.FreeBSD.org/src/commit/?id=40a42785dbba93cc5196178fc49d340c1a89cabe > > > > > > commit 40a42785dbba93cc5196178fc49d340c1a89cabe > > > Author: Konstantin Belousov > > > AuthorDate: 2025-09-11 10:05:04 +0000 > > > Commit: Konstantin Belousov > > > CommitDate: 2025-09-19 14:19:13 +0000 > > > > > > fcntl(F_SETFL): only allow one thread to perform F_SETFL > > > Use f_vflags file locking for this. > > > Allowing more than one thread handling F_SETFL might cause de-sync > > > between real driver state and flags. > > > Reviewed by: markj > > > Tested by: pho > > > Sponsored by: The FreeBSD Foundation > > > MFC after: 2 weeks > > > Differential revision: https://reviews.freebsd.org/D52487 > > > > Thanks for fixing this. I still slightly worry that "home-grown" locks > > aren't visible to WITNESS and it's checking. > > > > I was also expecting this to require more changes, but apparently if a > > process directly invokes FIONBIO on a file descriptor, f_flags isn't > > updated currently. I wonder if that is a bug. (Similarly for FIOASYNC.) > > > > Oh, we do handle that, but poorly. We don't revert on errors, and this > > should be updated to use fsetfl_lock now I think: > > > > kern_ioctl(...) > > { > > ... > > switch (com) { > > ... > > case FIONBIO: > > if ((tmp = *(int *)data)) > > atomic_set_int(&fp->f_flag, FNONBLOCK); > > else > > atomic_clear_int(&fp->f_flag, FNONBLOCK); > > data = (void *)&tmp; > > break; > > case FIOASYNC: > > if ((tmp = *(int *)data)) > > atomic_set_int(&fp->f_flag, FASYNC); > > else > > atomic_clear_int(&fp->f_flag, FASYNC); > > data = (void *)&tmp; > > break; > > } > > > > error = fo_ioctl(fp, com, data, td->td_ucred, td); > > out: > > > > I think instead we want something like: > > > > int f_flag; > > > > switch (com) { > > ... > > case FIONBIO: > > case FIOASYNC: > > fsetfl_lock(fp); > > tmp = *(int *)data; > > f_flag = com == FIONBIO ? FNONBLOCK : FASYNC; > > if ((fp->f_flag & f_flag) != 0) { > > This is wrong, should be: > > if (((fp->f_flag & f_flag) != 0) == (tmp != 0)) > > > fsetfl_unlock(fp); > > goto out; > > } > > data = (void *)&tmp; > > break; > > } > > > > error = fo_ioctl(fp, com, data, td->td_ucred, td); > > switch (com) { > > ... > > case FIONBIO: > > case FIOASYNC: > > if (error == 0) { > > if (tmp) > > Probably 'if (tmp != 0)' > > > atomic_set_int(&fp->f_flag, f_flag); > > else > > atomic_clear_int(&fp->f_flag, f_flag); > > } > > fsetfl_unlock(fp); > > break; > > } > > > > out: > > > > This only updates the flag if the underlying ioctl succeeds, and it also > > avoids invoking the underlying ioctl if the flag is already in the correct\ > > state. So will you handle this?