Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp6708740ybi; Wed, 29 May 2019 11:51:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqzCBlJoeQsrIhADiKBMDlfb+CIwhU/2Y6sZNxD8D3rCbjRUsDsbt9IgDqcn1qdeayLhVPV1 X-Received: by 2002:a63:fd52:: with SMTP id m18mr141086133pgj.267.1559155901825; Wed, 29 May 2019 11:51:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559155901; cv=none; d=google.com; s=arc-20160816; b=T0YVSlewqLmydg3gH45TopEucJ3f4akwk78LM6CA9WhxPWMy5RJeANvW/hSkQNEeiY HSorBJAHTk1xNnWwN418GyGOhNORmNHG7MHMOOsTv4Dl093ukVfFAi8x7E+z3auAqx/7 E5Dcus+PL7tiK9S81WLC4gJztBSjt+lpn5VVkwFG3ZWI14e2vWfLD1IIdujoavyG+PIZ brHdENFRbZleTB3syfRdE81Ck6s10JWSo3x0yw8qexcZg2kCwCw2Ts5ZwO6aI0sRvqlt TJar/WgmKBulZUrGjIAH1MUmw6KU2NVHwrgTg0HQLaPsE5qmj33cgclYRht7I3ZnGnC9 28yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=jh9+6SO6ir2Idm0osY3/fhfQmkeJgMSS67oE3tw+Ujo=; b=HIWtvSTWzXgVwp4X7vlwghV4jZJDzoEeuEHBD4MoY7+HVc3X1UMkbKSiJq0DSgZGP5 fp2uNXt+Q4eIeDFXsqS1YccYg1gLbqBEE4IWysKw5WdtJvcr7y54W7trRwcO1mf3YkYs 8/yt7PTGRK2Y9kC/0oQfHwMKqSM5v5erbUx9aLHGBnpTUDwIJSTfzIf74OBGRWj+cuwF 4wkR9nwd1SikZ6Ftz9rEVKCvcsgQZwxJBAx0WS7d5uQFszwdsDSUiyFCPKDm4GZ4Dy/5 zRuRajPMHA4sEO3/qkTsFSwH3zhUN86fR3D9UCy20oDp/0csLlxKyJ+pvM6b29HLWUis gyIg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 97si557479ple.161.2019.05.29.11.51.24; Wed, 29 May 2019 11:51:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726311AbfE2SuN (ORCPT + 99 others); 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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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.