From nobody Wed Sep 10 14:27:34 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 4cMNLv0BqYz62jxl; Wed, 10 Sep 2025 14:27:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cMNLt6bB4z431G; Wed, 10 Sep 2025 14:27:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1757514455; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=HSbeelb/va30XJmAupzVC7JHrx/vIc74loWyc2rntAU=; b=DBz1oeTYoidWkQpqlvZD82Na4sRcsVJHpwOhDlw9vY4ViQfBRV68JDV3JvW6vJcL2QQYLr 5kukchqpWgpwstqzLX5rlEpVrhu0KRyM6ZNTZj20al+weSKMf6N9nRhDiKlMxZdDPQ6Uis qkC+hSC3HC2uUMplGYakvuUz8ZT6f0yyYBT0DUAgcs6nGbBg+SHZXwIGC+z01zLtkIRvIE T3Qn4StYBbj2q/nyVNGtvZwuBSlcg38R/CWDd5XUi+ZDkMLQwS/gBVDThp+ayzn+Y4YejX f4XjYeA74FyqdbIhnR4CMhrkOt3qtlI1+ZkyZyygi/6tlL6Gaz/GDeCLgPo+Ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1757514455; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=HSbeelb/va30XJmAupzVC7JHrx/vIc74loWyc2rntAU=; b=FXIHQMbhlp5phRXxqIYq9QdbTEtUijCphSzuh4PjgfwAab6XH9DVyaEWKgzFXwbjGt6wJq s/rT0kLC3me4J0TYpMrS9gOb1ACGFB6ztYg8EiyMCLq5NJrol9n29wlJTCC742CYZzvZVZ pioSfgGkbNxUtghhvrXBdNe+nagCnPfFAwCg2uqDAXP9/HChq9szc+ARLQLxtVa6o5jr0+ JPcKCLMsCF81dVyFV8NdfiK/fGNqFEVCHHW0v0n8oqXfc2dotGxFwT7vPRPxkjGNbzpF1g 7sRUeYQEDoHemkzhDZvbCoic+zHqFb3l7GqQhKGPL/i9k0Jl+K73kGsGJfmFLw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1757514455; a=rsa-sha256; cv=none; b=E30GDZ489ZEHPYtXrt+In1ZeQ1l5luCgwix7u7+yxVBMVhG2cd6vUx+1r0KMhWudiy9PNE SSPcLNh0sNZuJeQgnW/7Az5XkOen/bZRoAWgJmQLxhGCIxRiJlLPMKIvwqLeUMTkZeAwIa 3qTJgtoQDaUM+e4NUWdZ33VLyaMnehABZCVUX8vwIWS0sl5mpNRVPtJj4V1qNYw6qq9MGE UB1RK45LY8uADcvL2gHyBH3ZTTVahmV0sTGarcsywUrlIeQtPfmULm6YG0DHYH7DLjfvkS 7nvR9JktxP4WhDfdYGxO8s0VYnILUImwEgpFJ/e7AY32qGx9gwWkpEW77+mR1A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4cMNLt5lw0z9H6; Wed, 10 Sep 2025 14:27:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 58AERYud078798; Wed, 10 Sep 2025 14:27:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 58AERYXT078795; Wed, 10 Sep 2025 14:27:34 GMT (envelope-from git) Date: Wed, 10 Sep 2025 14:27:34 GMT Message-Id: <202509101427.58AERYXT078795@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: 3c152a3de42a - main - fcntl(F_SETFL): Don't unconditionally invoke FIONBIO and FIOASYNC 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=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 3c152a3de42a7d077e8d19159b679c3fb7572820 Auto-Submitted: auto-generated The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=3c152a3de42a7d077e8d19159b679c3fb7572820 commit 3c152a3de42a7d077e8d19159b679c3fb7572820 Author: John Baldwin AuthorDate: 2025-09-10 14:22:19 +0000 Commit: John Baldwin CommitDate: 2025-09-10 14:22:19 +0000 fcntl(F_SETFL): Don't unconditionally invoke FIONBIO and FIOASYNC Currently, F_SETFL always invokes FIONBIO and FIOASYNC ioctls on the file descriptor even if the state of the associated flag has not changed. This means that a character device driver that implements non-blocking I/O but not async I/O needs a handler for FIOASYNC that permits setting the value to 0. This also means that fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)) can fail for a character device driver that does not handle both FIONBIO and FIOASYNC. These requirements are not obvious nor well documented. Instead, only invoke FIONBIO and FIOASYNC if the relevant flag changes state. This only requires a device driver to implement support for FIONBIO or FIOASYNC if it supports the corresponding flag. While here, if a request aims to toggle both F_NOBLOCK and F_ASYNC and FIOASYNC fails, pass the previous state of F_NONBLOCK to FIONBIO instead of always disabling non-blocking I/O and then possibly reverting the flag back to on in f_flags. Reviewed by: mckusick, imp, kib, emaste Differential Revision: https://reviews.freebsd.org/D52403 --- sys/kern/kern_descrip.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 057235574eb5..2a833d2eafbe 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -665,20 +665,26 @@ kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg) } while (atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0); got_set = tmp & ~flg; got_cleared = flg & ~tmp; - tmp = fp->f_flag & FNONBLOCK; - error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); - if (error != 0) - goto revert_f_setfl; - tmp = fp->f_flag & FASYNC; - error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td); - if (error == 0) { - fdrop(fp, td); - break; + if (((got_set | got_cleared) & FNONBLOCK) != 0) { + tmp = fp->f_flag & FNONBLOCK; + error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); + if (error != 0) + goto revert_flags; + } + if (((got_set | got_cleared) & FASYNC) != 0) { + tmp = fp->f_flag & FASYNC; + error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td); + if (error != 0) + goto revert_nonblock; + } + fdrop(fp, td); + break; +revert_nonblock: + if (((got_set | got_cleared) & FNONBLOCK) != 0) { + tmp = ~fp->f_flag & FNONBLOCK; + (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); } - atomic_clear_int(&fp->f_flag, FNONBLOCK); - tmp = 0; - (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); -revert_f_setfl: +revert_flags: do { tmp = flg = fp->f_flag; tmp &= ~FCNTLFLAGS;