From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36A8DC28CC0 for ; Wed, 29 May 2019 18:50:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1C0FD240B7 for ; Wed, 29 May 2019 18:50:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726118AbfE2SuN (ORCPT ); Wed, 29 May 2019 14:50:13 -0400 Received: from dcvr.yhbt.net ([64.71.152.64]:59246 "EHLO dcvr.yhbt.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725914AbfE2SuN (ORCPT ); Wed, 29 May 2019 14:50:13 -0400 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 645D01F462; Wed, 29 May 2019 18:50:12 +0000 (UTC) Date: Wed, 29 May 2019 18:50:12 +0000 From: Eric Wong To: David Laight Cc: 'Oleg Nesterov' , Deepa Dinamani , Al Viro , "Eric W. Biederman" , Linus Torvalds , "linux-kernel@vger.kernel.org" , "akpm@linux-foundation.org" , "arnd@arndb.de" , "dbueso@suse.de" , "axboe@kernel.dk" , "dave@stgolabs.net" , "jbaron@akamai.com" , "linux-fsdevel@vger.kernel.org" , "linux-aio@kvack.org" , "omar.kilani@gmail.com" , "tglx@linutronix.de" , "stable@vger.kernel.org" Subject: Re: pselect/etc semantics (Was: [PATCH v2] signal: Adjust error codes according to restore_user_sigmask()) Message-ID: <20190529185012.qqeqq4fsolprknrz@dcvr> References: <20190522032144.10995-1-deepa.kernel@gmail.com> <20190529161157.GA27659@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org David Laight wrote: > From: Oleg Nesterov > > Sent: 29 May 2019 17:12 > > Al, Linus, Eric, please help. > > > > The previous discussion was very confusing, we simply can not understand each > > other. > > > > To me everything looks very simple and clear, but perhaps I missed something > > obvious? Please correct me. > > > > I think that the following code is correct > > > > int interrupted = 0; > > > > void sigint_handler(int sig) > > { > > interrupted = 1; > > } > > > > int main(void) > > { > > sigset_t sigint, empty; > > > > sigemptyset(&sigint); > > sigaddset(&sigint, SIGINT); > > sigprocmask(SIG_BLOCK, &sigint, NULL); > > > > signal(SIGINT, sigint_handler); > > > > sigemptyset(&empty); // so pselect() unblocks SIGINT > > > > ret = pselect(..., &empty); > ^^^^^ sigint > > > > if (ret >= 0) // sucess or timeout > > assert(!interrupted); > > > > if (interrupted) > > assert(ret == -EINTR); > > } > > > > IOW, if pselect(sigmask) temporary unblocks SIGINT according to sigmask, this > > signal should not be delivered if a ready fd was found or timeout. The signal > > handle should only run if ret == -EINTR. > > Personally I think that is wrong. > Given code like the above that has: > while (!interrupted) { > pselect(..., &sigint); > // process available data > } > > You want the signal handler to be executed even if one of the fds > always has available data. > Otherwise you can't interrupt a process that is always busy. Agreed... I believe cmogstored has always had a bug in the way it uses epoll_pwait because it failed to check interrupts if: a) an FD is ready + interrupt b) epoll_pwait returns 0 on interrupt The bug remains in userspace for a), which I will fix by adding an interrupt check when an FD is ready. The window is very small for a) and difficult to trigger, and also in a rare code path. The b) case is the kernel bug introduced in 854a6ed56839a40f ("signal: Add restore_user_sigmask()"). I don't think there's any disagreement that b) is a kernel bug. So the confusion is for a), and POSIX is not clear w.r.t. how pselect/poll works when there's both FD readiness and an interrupt. Thus I'm inclined to believe *select/*poll/epoll_*wait should follow POSIX read() semantics: If a read() is interrupted by a signal before it reads any data, it shall return −1 with errno set to [EINTR]. If a read() is interrupted by a signal after it has successfully read some data, it shall return the number of bytes read. > One option is to return -EINTR if a signal is pending when the mask > is updated - before even looking at anything else. > > Signals that happen later on (eg after a timeout) need not be reported > (until the next time around the loop). I'm not sure that's necessary and it would cause delays in signal handling.