Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp22798rwn; Wed, 7 Sep 2022 19:07:41 -0700 (PDT) X-Google-Smtp-Source: AA6agR6+3WA6xf2/bs4eZn5qS3nMvBZYsh4T+BumYYf3isCcLyF50WzuJOzT6bUd9ohxMYbGGEuS X-Received: by 2002:a17:90b:4a08:b0:200:e221:e98a with SMTP id kk8-20020a17090b4a0800b00200e221e98amr1587923pjb.55.1662602860909; Wed, 07 Sep 2022 19:07:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662602860; cv=none; d=google.com; s=arc-20160816; b=RjHFBU2XJeCzN41hVQfHe5FYmTEPMavps9qh/8gXj4zzuyx9StBljaKv+m8bko60Ue 3Fkh/hLuvXEtdIaeGLKxdXwMXoQibVrEZ1I1trxO9x0pm0fgLX86wkEZXCjP436NeYlq 3HSE1B6PIqsWMAC0uQx+LFTUEz33BQgpkgkEJVbWkO24Kdi9rp2zX/lGj4hEvES2ecMX 4NNwR4z97rxi45mWtutQXZPl8vFc6UZo3gGT3ENEZjzVmwOgoksJxuu6fraKlY3wBrOj UJuIKSxcENonszC7Tmd3lbMxquVNOdhdXDAUHiqzi3fOtANvAaPHSPUoRoc4HH9KJq3i rHuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:references :in-reply-to:mime-version:dkim-signature; bh=f3FVyDagMbu4/t709XgA6UIISAASxFuNGu2n5xDzxVc=; b=ih15pngAEpk2f4OsxXOP+JvrIStDuYsbH4qooRrhaf4C/3kf5SFSoPvB0uKmjLYROr kvNchmhI4BzWEakqwlvpd4KjhGyyR6nuo95OXf0kDdGHn5YWNvLAZZ5OTmO8ypvvRECb duI2SfM4YNw25Dk/XEUooIxqgWRCp1tcBOngJWRVk0T1RkU/kuxyWBJtnu1DoGE+/dVG rnH6/zqX485G1s6WM9jW9Bktql84EhUzqFUJpePHgMkDo2XTjGq5LorXYe71JnlKysgg HhjUXxPJbv0mrLhGpF5OYyInN/ixCgd0YooePZ4tQAmMQCr7VBPnPJ5DEtEu/5tjS3uR vWJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="C7Lh/pvP"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s71-20020a632c4a000000b004340afa4acasi16045847pgs.388.2022.09.07.19.07.26; Wed, 07 Sep 2022 19:07:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="C7Lh/pvP"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229990AbiIHBzE (ORCPT + 99 others); Wed, 7 Sep 2022 21:55:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229498AbiIHBzC (ORCPT ); Wed, 7 Sep 2022 21:55:02 -0400 Received: from mail-yb1-xb2e.google.com (mail-yb1-xb2e.google.com [IPv6:2607:f8b0:4864:20::b2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2D5025299 for ; Wed, 7 Sep 2022 18:55:01 -0700 (PDT) Received: by mail-yb1-xb2e.google.com with SMTP id 130so18646101ybz.9 for ; Wed, 07 Sep 2022 18:55:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date; bh=f3FVyDagMbu4/t709XgA6UIISAASxFuNGu2n5xDzxVc=; b=C7Lh/pvPURleQ+IEP1X5+h1MdXx5ODZXRKvti11CImKo/LZ9D9zNCWuhd726NwuQcc 6GS0cZJmzMTa26O2fjbtpv6mQYIfYLNqQ9QfkETPYnwjA++gS7mowbki4VT+xMGRX/Sn qtrb6d+/m4+qaHxby+pEmiMgwC14yjlStUp2LDq795aBU5lpLAPJuJkk5lsnnZQkdYmH iHxmZjpaYiV/VwowFOz0dj6BxD3335I+lXfGccGnQI8PzxNqEug1iI3oxxh+ZEN2HJr3 HeGIhdtgSvVRFbanM54m22wEKbKca7zTvu1B9K9DSr7ZJbIcwGAlSgPNYFeOMWDgUQDn Ds2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=f3FVyDagMbu4/t709XgA6UIISAASxFuNGu2n5xDzxVc=; b=2DephFkga2ZpU9scIrB3rqxwdhvYtx43y820Ndeo6pHEkK4+DiZfRyHhRVglFfijaR kHlCNbZBVeHln0CxSI0POrF6wCKP47ob/2BFenN1k8l8wX+PP7UdKH3aMmcJiajUYzY3 23k5aFe4EAVPikoLz8XhweLuJS5Dn55ZUBXjhQi8W4XFBKiD7MXkdsZsZyewvKp5ZTBb mhotY4nnEzwT8axI0bq67vdu1IObBXHIfgUInoRcxdpYZEi2imxtNR1Pe+VS4KfwVt/J 0FI71zUhv93BLX8DlkXMSrwgWVzFvd0ZmZS4bSMvpwz1s+YjuQbJiC0CbTeXc3Uu/LHv 4C4A== X-Gm-Message-State: ACgBeo3qU0XGoFrD+5sW+W4bSc3xOKGFBT7kBJpVwzDcSOYVymHI3pl0 HguRq3RzNFSy4qMPM/6ZoBGlzmHIFitBfDaDDUY= X-Received: by 2002:a05:6902:154d:b0:6a9:7aa7:e17b with SMTP id r13-20020a056902154d00b006a97aa7e17bmr1032680ybu.170.1662602100799; Wed, 07 Sep 2022 18:55:00 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a05:7108:80ab:0:0:0:0 with HTTP; Wed, 7 Sep 2022 18:54:59 -0700 (PDT) In-Reply-To: References: From: Jason Vas Dias Date: Thu, 8 Sep 2022 02:54:59 +0100 Message-ID: Subject: Re: SIGIO with si_code==POLL_HUP on read pipe FD with no writers? To: Jason Vas Dias Cc: David Howells , linux-kernel@vger.kernel.org Content-Type: multipart/mixed; boundary="0000000000009f48d605e820b734" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --0000000000009f48d605e820b734 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable OK, I raised bug https://bugzilla.kernel.org/show_bug.cgi?id=3D216458 about this and submitted this patch, which I have tested by incorporating into the Fedora 36 kernel.spec file build, which I updated to v5.19.7. Now, my test program runs fine and the whole Fedora subsystem & GUI comes up OK under the patched kernel - so it appears I have not broken anything so far - I will test more thoroughly with any kernel / POSIX / I/O test suites I can find tomorrow - can anyone recommend any ? The patch certainly does not break any POSIX rules AFAICS , (rather it tries to honor the ones concerning SIGIO & POLL_HUP, as described in the latest linux & POSIX manual pages, better ). I can post the RPMs to my google drive account & share them if anyone is interested . Please review kernel bug 216458 & consider including this patch in a future kernel release - it works well, causes no harm, and makes I/O programming with SIGIO & pipes much more robust and straightforward. Thanks, Best Regards, Jason Vas Dias On 05/09/2022, Jason Vas Dias wrote: > > Good day - > > To the last committer & maintainers of 'linux/fs/pipe.c' : > > Why isn't a SIGIO signal with POLL_HUP in 'si_code' > raised on an O_ASYNC, F_SETOWN{,_EX} pid F_SETSIG > signal owning pipe read file descriptor ? > > All that happens when the write end of a pipe is > closed is that a SIGIO gets raised with the > (struct siginfo* parameter)->si_code set > to 1 ( POLL_IN ) , and then > ioctl( fd, FIONREAD, &sz) > then returns with sz=3D=3D0 for that fd ; > a read() on that fd would then return 0. > > Looking at pipe.c, the situation of no pipe writers > is detected and revents is set to contain EPOLLHUP > ONLY in pipe_poll(), not pipe_read() . > > pipe_read() (in version 5.19) DOES detect the > no writers situation : > > fs/pipe.c, @line 255: > for (;;) { > /* Read ->head with a barrier vs post_one_notification() */ > ... > @line 341: > if (!pipe->writers) > break; > ... > > It would be quite easy to add after the pipe_read() loop quits a clau= se > as in > pipe_poll() , @ line 677: > mask =3D 0; > if (filp->f_mode & FMODE_READ) { > if (!pipe_empty(head, tail)) > mask |=3D EPOLLIN | EPOLLRDNORM; > if (!pipe->writers && filp->f_version !=3D pipe->w_counter) > mask |=3D EPOLLHUP; > } > > which does something like : > > if ( !pipe->writers ) > kill_fasync(&pipe->fasync_readers, SIGIO, POLL_HUP); > > > It is not nice to have to GUESS that just because > ioctl(fd, FIONREAD, &sz) > returns with sz=3D=3D0 immediately after a POLL_IN event, > that the pipe in fact has no writers, because the > signal could be blocked when the ioctl call happens. > > And if one happens not to try to read 0 bytes from the pipe, > then one would never know that no writers exist on it, and > could pause() infinitely waiting for a signal. > Or why should I have to put the FD into O_NONBLOCK mode > (which mine was not in) and attempt a read to return > 0 bytes, when I know 0 bytes are available to read ? > > OR, maybe in pipe_write(), @ line 595 where it does : > > kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); > > (which is probably where the FINAL POLL_IN signal originates) > it could instead do: > kill_fasync(&pipe->fasync_readers, SIGIO, > ((ret=3D=3D0) && (pipe->fasync_writers <=3D 1)) > ? POLL_HUP > : POLL_IN > ); > > It seems there are several easy ways to fix this and > I believe that it would make processes wanting to > read pipes using SIGIO much more robust & simple to code. > > Processes would still be able to rely on read()s returning > 0 in this case, but please, why can't SIGIO using processes > also get a definitive SIGIO with si_code=3D=3DPOLL_HUP, not POLL_IN = ? > > There appears to be similar logic that does send > a final POLL_HUP SIGIO when the remote write end of > a readable socket closes - why not for pipes ? > > And the sigaction manual page states: > " > The following values can be placed in si_code for a SIGIO/SIGPOLL > sig=E2=80=90 > nal: > > POLL_IN > Data input available. > > POLL_OUT > Output buffers available. > > POLL_MSG > Input message available. > > POLL_ERR > I/O error. > > POLL_PRI > High priority input available. > > POLL_HUP > Device disconnected. > " > which suggests that these events should be raised for all devices - > it does not mention any special cases for pipe file descriptors, > so readers would reasonably expect a POLL_HUP event to be sent > on a read end of a pipe with no writers. > > Please do something about this - > or would a patch from me that fixes this ever be > likely to be considered ? > > Thanks for any responses & Best Regards, > Jason Vas Dias > > > > > > > --0000000000009f48d605e820b734 Content-Type: text/x-patch; charset="US-ASCII"; name="fs_pipe_c_pipe_fd_sigio_poll_hup.patch" Content-Disposition: attachment; filename="fs_pipe_c_pipe_fd_sigio_poll_hup.patch" Content-Transfer-Encoding: base64 X-Attachment-Id: file0 ZGlmZiAtdXAgbGludXgtNS4xOS43LTIwMC5mYzM2Lng4Nl82NC9mcy9waXBlLmMucGlwZV9mZF9z aWdpb19wb2xsX2h1cCBsaW51eC01LjE5LjctMjAwLmZjMzYueDg2XzY0L2ZzL3BpcGUuYwotLS0g bGludXgtNS4xOS43LTIwMC5mYzM2Lng4Nl82NC9mcy9waXBlLmMucGlwZV9mZF9zaWdpb19wb2xs X2h1cAkyMDIyLTA5LTA1IDA5OjMxOjM2LjAwMDAwMDAwMCArMDEwMAorKysgbGludXgtNS4xOS43 LTIwMC5mYzM2Lng4Nl82NC9mcy9waXBlLmMJMjAyMi0wOS0wNyAyMTowOToxN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