Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp2720986ybc; Wed, 13 Nov 2019 20:35:02 -0800 (PST) X-Google-Smtp-Source: APXvYqwBfWlWw9G2N9yIn94GrHSmXuu+sspmOunurIORTbyfYp7z7bDii/XrNj06lmNo9Hn0zxzs X-Received: by 2002:a17:906:6a43:: with SMTP id n3mr187938ejs.31.1573706102850; Wed, 13 Nov 2019 20:35:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573706102; cv=none; d=google.com; s=arc-20160816; b=bnktQfIwCAXpO+nRgswMxlhechiwcUyZfEXZZOUdt4QlBAgR7ljTeXrmfKuYYL5N9u NLYP9HRFCcOKAWlHFoO/NfyqSirJcs//DRpmq0t5SRF7mtibWSfY+NuPnRL1fr9cyLRP 5YIgFP02QL7Jd5j39SGorMhMht6QdSgbbKj9ovNxA8LCD3J3VGLjbd/eVOQXlkh4Xrnt ms6VuPdoS7OYe1uh+9Ijw0xIDe4GzfZFWdax5KnU5blc4ChfWH8OrYWuxLd0ukBmfaAo cn8SN7+bbZ1SfXqHIbmcfMyM59dr1vbNGzZZO4m5kTmUakTm08tRIpqxGDtxIhyCl0Se 3EGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:mime-version:user-agent:date:message-id:subject :from:cc:to:dkim-signature; bh=ATcCOvPgaDxDdfb/2fmiaxeweoZIFDGlDDkWkjQsyvY=; b=ExeVQkPaMR35qyqHz/3vpRHYKU2Uy0e5C1L3FVC2Cez3+96s+HxGMyTZXLKXJ6k/VM GZCyPz49WBwIKg7omgNvKd1zSo23RGZAs+O9QbkOjOYUFPKpac+Mz5V5RaYzYSPa3mXW Khx+I7Vk0BPzo9uaHEr+W7WTwyF3q6k5z9MOQPNxTYRpnOa7melPD44BHUAq4pgwEXC8 YoXeBFqmZfqIg5wv3k85snItNyvEv+NwystqxJkTXCHKiGyvv+ZvNBLTBFJl2ltp9cNc Nwe0Jd0zqADhMFM8+6W1vUOmaEdLOqkGVKAJaqztTYiFIwFoZIXg9FwpJdvd5SGF/HA9 4IJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b=RLZ3Ddrz; 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 c6si2649105ejm.157.2019.11.13.20.34.38; Wed, 13 Nov 2019 20:35:02 -0800 (PST) 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; dkim=pass header.i=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b=RLZ3Ddrz; 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 S1726755AbfKNEbM (ORCPT + 99 others); Wed, 13 Nov 2019 23:31:12 -0500 Received: from mail-pg1-f194.google.com ([209.85.215.194]:36069 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726489AbfKNEbM (ORCPT ); Wed, 13 Nov 2019 23:31:12 -0500 Received: by mail-pg1-f194.google.com with SMTP id k13so2868938pgh.3 for ; Wed, 13 Nov 2019 20:31:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=to:cc:from:subject:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=ATcCOvPgaDxDdfb/2fmiaxeweoZIFDGlDDkWkjQsyvY=; b=RLZ3DdrzvnlpKn78eg7PLlkgOiwaq/1jGHecsmEGH08sCwiSEyiJIHWQVQosVurRAz 8drBb1+jSOJHM3kkew5ancydBlYeV0wM/r6eafY/ZQn5vBKAG/x4tQ2Gj908mvMXFLCf mqzgpWxUdzpinHuYY/3+Dbkgoq+W2pl/z0E0WHMEdNfu97C2DsJHsJd+sHJMOS4iv8as vzmiveQRoT2t0QgKXq8k3V831DxPctrdOrxZ21uIp+f2pliDtnlLtzuF7EWiHncJZ4sU yzfaBEcmbGsQhQlTV/9RqtpYBQedd6PHhKse6r5k1r60QM9ZnpPJcBdve/Yx+THGQ3SN ZWaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:cc:from:subject:message-id:date:user-agent :mime-version:content-language:content-transfer-encoding; bh=ATcCOvPgaDxDdfb/2fmiaxeweoZIFDGlDDkWkjQsyvY=; b=RPXjWlOXxkxOpG/Em8Wdy2zZmU6HeUJcvD06W+yTzRMly+6QjXH02NEskRtdTQVLfe oZV43mB25rVw7q6t16SgtUrhw/c51ccXrZHa9aofcIMCoFfFgFNNPUnu00sTkCEi30hp HfYc1FkcffcuDSVxQ0QYsjRrGWs2H/ch+BXYlcVbUTW7Fd3vA80eMXeu19nBaHlngu9E behmZHF6lj59f8ghO7Q1dEqrVbFCVhPAgedPheHvzXCdrmrceU0tNe91uuFKdnb4XjLD ZS6raeRS2LjlnM5MbzogEUDkufV6raWV5AetJLZl3iJJr44N/IsGdo3JfPrCpjLrs9+G Vu7g== X-Gm-Message-State: APjAAAUGc0WN4BX9hY6lc6DDGUQTD4OL60MwFmZXvDCrlbREAZ43iHBF e/jnWC3N883iEbhasMJ75kpXYg== X-Received: by 2002:a63:f441:: with SMTP id p1mr7746477pgk.362.1573705869452; Wed, 13 Nov 2019 20:31:09 -0800 (PST) Received: from [192.168.1.188] ([66.219.217.79]) by smtp.gmail.com with ESMTPSA id 129sm3179960pfd.174.2019.11.13.20.31.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Nov 2019 20:31:08 -0800 (PST) To: io-uring@vger.kernel.org, "linux-kernel@vger.kernel.org" Cc: Alexander Viro , Andrew Morton , linux-fsdevel , Christoph Hellwig From: Jens Axboe Subject: [PATCH RFC] io_uring: make signalfd work with io_uring (and aio) POLL Message-ID: <58059c9c-adf9-1683-99f5-7e45280aea87@kernel.dk> Date: Wed, 13 Nov 2019 21:31:05 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a case of "I don't really know what I'm doing, but this works for me". Caveat emptor, but I'd love some input on this. I got a bug report that using the poll command with signalfd doesn't work for io_uring. The reporter also noted that it doesn't work with the aio poll implementation either. So I took a look at it. What happens is that the original task issues the poll request, we call ->poll() (which ends up with signalfd for this fd), and find that nothing is pending. Then we wait, and the poll is passed to async context. When the requested signal comes in, that worker is woken up, and proceeds to call ->poll() again, and signalfd unsurprisingly finds no signals pending, since it's the async worker calling it. That's obviously no good. The below allows you to pass in the task in the poll_table, and it does the right thing for me, signal is delivered and the correct mask is checked in signalfd_poll(). Similar patch for aio would be trivial, of course. Not-really-signed-off-by: Jens Axboe --- diff --git a/fs/io_uring.c b/fs/io_uring.c index d8ea9b4f83a7..d9a4c9aac958 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -299,6 +299,7 @@ struct io_poll_iocb { bool done; bool canceled; struct wait_queue_entry wait; + struct task_struct *task; }; struct io_timeout { @@ -2021,7 +2022,10 @@ static void io_poll_complete_work(struct io_wq_work **workptr) struct io_wq_work *work = *workptr; struct io_kiocb *req = container_of(work, struct io_kiocb, work); struct io_poll_iocb *poll = &req->poll; - struct poll_table_struct pt = { ._key = poll->events }; + struct poll_table_struct pt = { + ._key = poll->events, + .task = poll->task + }; struct io_ring_ctx *ctx = req->ctx; struct io_kiocb *nxt = NULL; __poll_t mask = 0; @@ -2139,9 +2143,11 @@ static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe, poll->head = NULL; poll->done = false; poll->canceled = false; + poll->task = current; ipt.pt._qproc = io_poll_queue_proc; ipt.pt._key = poll->events; + ipt.pt.task = poll->task; ipt.req = req; ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ diff --git a/fs/signalfd.c b/fs/signalfd.c index 44b6845b071c..a7f31758db1a 100644 --- a/fs/signalfd.c +++ b/fs/signalfd.c @@ -61,16 +61,17 @@ static int signalfd_release(struct inode *inode, struct file *file) static __poll_t signalfd_poll(struct file *file, poll_table *wait) { struct signalfd_ctx *ctx = file->private_data; + struct task_struct *tsk = wait->task ?: current; __poll_t events = 0; - poll_wait(file, ¤t->sighand->signalfd_wqh, wait); + poll_wait(file, &tsk->sighand->signalfd_wqh, wait); - spin_lock_irq(¤t->sighand->siglock); - if (next_signal(¤t->pending, &ctx->sigmask) || - next_signal(¤t->signal->shared_pending, + spin_lock_irq(&tsk->sighand->siglock); + if (next_signal(&tsk->pending, &ctx->sigmask) || + next_signal(&tsk->signal->shared_pending, &ctx->sigmask)) events |= EPOLLIN; - spin_unlock_irq(¤t->sighand->siglock); + spin_unlock_irq(&tsk->sighand->siglock); return events; } diff --git a/include/linux/poll.h b/include/linux/poll.h index 1cdc32b1f1b0..6d2b6d923b2b 100644 --- a/include/linux/poll.h +++ b/include/linux/poll.h @@ -43,6 +43,7 @@ typedef void (*poll_queue_proc)(struct file *, wait_queue_head_t *, struct poll_ typedef struct poll_table_struct { poll_queue_proc _qproc; __poll_t _key; + struct task_struct *task; } poll_table; static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p) @@ -76,6 +77,7 @@ static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc) { pt->_qproc = qproc; pt->_key = ~(__poll_t)0; /* all events enabled */ + pt->task = NULL; } static inline bool file_can_poll(struct file *file) -- Jens Axboe