From patchwork Wed Jan 22 16:42:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11346069 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8E887921 for ; Wed, 22 Jan 2020 16:42:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6B8BF2465A for ; Wed, 22 Jan 2020 16:42:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20150623.gappssmtp.com header.i=@kernel-dk.20150623.gappssmtp.com header.b="Vtu+Oq5g" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726004AbgAVQmt (ORCPT ); Wed, 22 Jan 2020 11:42:49 -0500 Received: from mail-io1-f65.google.com ([209.85.166.65]:37975 "EHLO mail-io1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725836AbgAVQms (ORCPT ); Wed, 22 Jan 2020 11:42:48 -0500 Received: by mail-io1-f65.google.com with SMTP id i7so7284219ioo.5 for ; Wed, 22 Jan 2020 08:42:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RfiRzl729pSQkcVcyHTXNJXLD8uT/Zh0KX5h0rH8OOE=; b=Vtu+Oq5gEAO9RZ0XdxLepwTDRsT+4l7hnqDjoUlOnBJhyJQeZDk9ysQkmuahDdl7V9 DTWL5O4OWPfwccVKZhdwTLs96FprFEYcUuaBoYVGr/K0M/zwRgn8b37r3VSK2vGHRpvm x66UPSrgUxYd+vJAkvBrV0YKJ68s/W2TPBbkKV2i/ti2N2WKqTFt4B7YQkBrkMCRtl+C U8Qp4SapIKKw2FqIQjDqLt0c5ufvTqAll+btDUpAyBhR0zhf6ctW5mUoAjhnMyhJP0n3 pIJw1uAFPf6Ff6hQ2bbhBCYWmwUml2Ofdtz8QugF4ToaqKGFSyHfsEHI4RfuFdgcUJl4 RtJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RfiRzl729pSQkcVcyHTXNJXLD8uT/Zh0KX5h0rH8OOE=; b=IUgoy5bhnbQlAWTbzTu8T6FRp4T1a5v5TV5mp05JkDZMkCgvC/PQLLkwNCRAO1ctm3 NuLis5BYSt717RvCN1eDzA2hUMXZjz+1qnlXL7dQcG+b0ITB6gkbLfap1Vu174YV10PA 9V3KsbJa0Qe6cI03MSuKzjMY1MHK2LWhTKN2GXz8F4Ij49rm8FxiKFPjX3xuY+XU2xh5 F53ObNdOjnhsfY3JfMTFWWaHDZVxXRlO8Fjbssndne6oipYtlI4taTv2A9hvQPTItqqO ixpmbw6eD7Rn7hcMfPL2NJWenK7e0eVbYDW3ufG9Sel6RhOMUAQAYJnwx6GoYXO0bVJG 9LtQ== X-Gm-Message-State: APjAAAUHQjmt0189yehbU9oYnOz5rzSxKMlK3zxfsELYn4ad+vwpLYI6 hCc1qh9SZvHpB4vze2JFQRZaHKO0ajI= X-Google-Smtp-Source: APXvYqwyFd9pcByXHyotx2H96fZrn/peNcW0PRxTpuSU/TvG3mekjSSzF3Qnbe3qbIo7g6Xv2ADQ+A== X-Received: by 2002:a5d:9805:: with SMTP id a5mr3425782iol.80.1579711367872; Wed, 22 Jan 2020 08:42:47 -0800 (PST) Received: from x1.localdomain ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id o6sm14599681ilc.76.2020.01.22.08.42.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jan 2020 08:42:47 -0800 (PST) From: Jens Axboe To: io-uring@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, jannh@google.com, Jens Axboe Subject: [PATCH 1/3] eventpoll: abstract out epoll_ctl() handler Date: Wed, 22 Jan 2020 09:42:42 -0700 Message-Id: <20200122164244.27799-2-axboe@kernel.dk> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200122164244.27799-1-axboe@kernel.dk> References: <20200122164244.27799-1-axboe@kernel.dk> MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org No functional changes in this patch. Signed-off-by: Jens Axboe --- fs/eventpoll.c | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index 67a395039268..cd848e8d08e2 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -2074,27 +2074,15 @@ SYSCALL_DEFINE1(epoll_create, int, size) return do_epoll_create(0); } -/* - * The following function implements the controller interface for - * the eventpoll file that enables the insertion/removal/change of - * file descriptors inside the interest set. - */ -SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, - struct epoll_event __user *, event) +static int do_epoll_ctl(int epfd, int op, int fd, struct epoll_event *epds) { int error; int full_check = 0; struct fd f, tf; struct eventpoll *ep; struct epitem *epi; - struct epoll_event epds; struct eventpoll *tep = NULL; - error = -EFAULT; - if (ep_op_has_event(op) && - copy_from_user(&epds, event, sizeof(struct epoll_event))) - goto error_return; - error = -EBADF; f = fdget(epfd); if (!f.file) @@ -2112,7 +2100,7 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, /* Check if EPOLLWAKEUP is allowed */ if (ep_op_has_event(op)) - ep_take_care_of_epollwakeup(&epds); + ep_take_care_of_epollwakeup(epds); /* * We have to check that the file structure underneath the file descriptor @@ -2128,11 +2116,11 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, * so EPOLLEXCLUSIVE is not allowed for a EPOLL_CTL_MOD operation. * Also, we do not currently supported nested exclusive wakeups. */ - if (ep_op_has_event(op) && (epds.events & EPOLLEXCLUSIVE)) { + if (ep_op_has_event(op) && (epds->events & EPOLLEXCLUSIVE)) { if (op == EPOLL_CTL_MOD) goto error_tgt_fput; if (op == EPOLL_CTL_ADD && (is_file_epoll(tf.file) || - (epds.events & ~EPOLLEXCLUSIVE_OK_BITS))) + (epds->events & ~EPOLLEXCLUSIVE_OK_BITS))) goto error_tgt_fput; } @@ -2192,8 +2180,8 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, switch (op) { case EPOLL_CTL_ADD: if (!epi) { - epds.events |= EPOLLERR | EPOLLHUP; - error = ep_insert(ep, &epds, tf.file, fd, full_check); + epds->events |= EPOLLERR | EPOLLHUP; + error = ep_insert(ep, epds, tf.file, fd, full_check); } else error = -EEXIST; if (full_check) @@ -2208,8 +2196,8 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, case EPOLL_CTL_MOD: if (epi) { if (!(epi->event.events & EPOLLEXCLUSIVE)) { - epds.events |= EPOLLERR | EPOLLHUP; - error = ep_modify(ep, epi, &epds); + epds->events |= EPOLLERR | EPOLLHUP; + error = ep_modify(ep, epi, epds); } } else error = -ENOENT; @@ -2231,6 +2219,23 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, return error; } +/* + * The following function implements the controller interface for + * the eventpoll file that enables the insertion/removal/change of + * file descriptors inside the interest set. + */ +SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, + struct epoll_event __user *, event) +{ + struct epoll_event epds; + + if (ep_op_has_event(op) && + copy_from_user(&epds, event, sizeof(struct epoll_event))) + return -EFAULT; + + return do_epoll_ctl(epfd, op, fd, &epds); +} + /* * Implement the event wait interface for the eventpoll file. It is the kernel * part of the user space epoll_wait(2). From patchwork Wed Jan 22 16:42:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11346071 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E1F5A921 for ; Wed, 22 Jan 2020 16:42:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BF80324656 for ; Wed, 22 Jan 2020 16:42:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20150623.gappssmtp.com header.i=@kernel-dk.20150623.gappssmtp.com header.b="Io0n3sYN" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726194AbgAVQmu (ORCPT ); Wed, 22 Jan 2020 11:42:50 -0500 Received: from mail-io1-f67.google.com ([209.85.166.67]:33486 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725970AbgAVQmu (ORCPT ); Wed, 22 Jan 2020 11:42:50 -0500 Received: by mail-io1-f67.google.com with SMTP id z8so7305711ioh.0 for ; Wed, 22 Jan 2020 08:42:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BAFfS5OIp3jxEUeDeCIVOeYSwdTU3d0hKCDzqFsPnqo=; b=Io0n3sYNpwQM8CdCxWnopIOeOP5krysVQuURUiwa4PYvS+MRY9UCoyemshpeqG8KMn vs9QU5zcdwO/jeOblKk1AI0Fjw+m4RRBgp/A2x3AbG5QUDP9YrmoKycJyCO6Ar7q7K0A /uyBjTqChU0AKeEXfrZJJgg4HweZspeytK+aYzSR2F+5oI9Za7Cunmg73/0xAvs25ofk DV7O+eGbqrDlDjahQugG1jE23PST6ARM9Hs3r6qcvVIFklMX39AtdkecH3H0YzsY5gr2 J8GtNB6Xr4WLPUipdJiRWH/N3mS2ebfKN5L6ycGOWHsm+u7ghka4j+XKXh+xA+BaUusa WSsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BAFfS5OIp3jxEUeDeCIVOeYSwdTU3d0hKCDzqFsPnqo=; b=JCsF9Mntm+nxyR62yhYrV8MhStlyMZLa9ZTkxtOwO/Sx91PP6Gph0ljq1gPKF3Yl/f TEgQpeIFC3aTFYJnZECdsw/8paITonHtv7kdBHV7KAij3D/tC1Sdl1HwaJjJIHF8n6da yek4pBBxk2QDuixFSkIQ5RzdUPBbpvSEinaEXplVpLy/JuW/SPUMUz1O3N+GP3ZJol4P XuVcUQm0vf/2/xKqo/2erVNFzvRNsjdkxMySOu9spT3mKPRoiG7y5nC5h3DkNLW/g+A7 kmuSbrO9wu4VqYuXzrwvgyel16pcD0FSsR2mdzxzfO8Qr9WcD2spI4WLeKb2HKo4b6ip B8lw== X-Gm-Message-State: APjAAAWzBanxXtBzLkivo4YeLWSUOGkrutaPyFADFOCQ88gAJDw7C4LP U0XbLnfV9QS7H+c9cN2zjHpQMQ== X-Google-Smtp-Source: APXvYqxu2DqtaxjVNoRDx51OWRR3Ym3l6DHf/aadfHj8lW0h1xOcIGJ1jzKSRUIa4u6hglBVJ/qxIQ== X-Received: by 2002:a6b:ca43:: with SMTP id a64mr7366905iog.217.1579711368999; Wed, 22 Jan 2020 08:42:48 -0800 (PST) Received: from x1.localdomain ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id o6sm14599681ilc.76.2020.01.22.08.42.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jan 2020 08:42:48 -0800 (PST) From: Jens Axboe To: io-uring@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, jannh@google.com, Jens Axboe Subject: [PATCH 2/3] eventpoll: support non-blocking do_epoll_ctl() calls Date: Wed, 22 Jan 2020 09:42:43 -0700 Message-Id: <20200122164244.27799-3-axboe@kernel.dk> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200122164244.27799-1-axboe@kernel.dk> References: <20200122164244.27799-1-axboe@kernel.dk> MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Also make it available outside of epoll, along with the helper that decides if we need to copy the passed in epoll_event. Signed-off-by: Jens Axboe --- fs/eventpoll.c | 46 ++++++++++++++++++++++++++++----------- include/linux/eventpoll.h | 9 ++++++++ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index cd848e8d08e2..b041b66002db 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -354,12 +354,6 @@ static inline struct epitem *ep_item_from_epqueue(poll_table *p) return container_of(p, struct ep_pqueue, pt)->epi; } -/* Tells if the epoll_ctl(2) operation needs an event copy from userspace */ -static inline int ep_op_has_event(int op) -{ - return op != EPOLL_CTL_DEL; -} - /* Initialize the poll safe wake up structure */ static void ep_nested_calls_init(struct nested_calls *ncalls) { @@ -2074,7 +2068,20 @@ SYSCALL_DEFINE1(epoll_create, int, size) return do_epoll_create(0); } -static int do_epoll_ctl(int epfd, int op, int fd, struct epoll_event *epds) +static inline int epoll_mutex_lock(struct mutex *mutex, int depth, + bool nonblock) +{ + if (!nonblock) { + mutex_lock_nested(mutex, depth); + return 0; + } + if (mutex_trylock(mutex)) + return 0; + return -EAGAIN; +} + +int do_epoll_ctl(int epfd, int op, int fd, struct epoll_event *epds, + bool nonblock) { int error; int full_check = 0; @@ -2145,13 +2152,17 @@ static int do_epoll_ctl(int epfd, int op, int fd, struct epoll_event *epds) * deep wakeup paths from forming in parallel through multiple * EPOLL_CTL_ADD operations. */ - mutex_lock_nested(&ep->mtx, 0); + error = epoll_mutex_lock(&ep->mtx, 0, nonblock); + if (error) + goto error_tgt_fput; if (op == EPOLL_CTL_ADD) { if (!list_empty(&f.file->f_ep_links) || is_file_epoll(tf.file)) { - full_check = 1; mutex_unlock(&ep->mtx); - mutex_lock(&epmutex); + error = epoll_mutex_lock(&epmutex, 0, nonblock); + if (error) + goto error_tgt_fput; + full_check = 1; if (is_file_epoll(tf.file)) { error = -ELOOP; if (ep_loop_check(ep, tf.file) != 0) { @@ -2161,10 +2172,19 @@ static int do_epoll_ctl(int epfd, int op, int fd, struct epoll_event *epds) } else list_add(&tf.file->f_tfile_llink, &tfile_check_list); - mutex_lock_nested(&ep->mtx, 0); + error = epoll_mutex_lock(&ep->mtx, 0, nonblock); + if (error) { +out_del: + list_del(&tf.file->f_tfile_llink); + goto error_tgt_fput; + } if (is_file_epoll(tf.file)) { tep = tf.file->private_data; - mutex_lock_nested(&tep->mtx, 1); + error = epoll_mutex_lock(&tep->mtx, 1, nonblock); + if (error) { + mutex_unlock(&ep->mtx); + goto out_del; + } } } } @@ -2233,7 +2253,7 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, copy_from_user(&epds, event, sizeof(struct epoll_event))) return -EFAULT; - return do_epoll_ctl(epfd, op, fd, &epds); + return do_epoll_ctl(epfd, op, fd, &epds, false); } /* diff --git a/include/linux/eventpoll.h b/include/linux/eventpoll.h index bc6d79b00c4e..8f000fada5a4 100644 --- a/include/linux/eventpoll.h +++ b/include/linux/eventpoll.h @@ -61,6 +61,15 @@ static inline void eventpoll_release(struct file *file) eventpoll_release_file(file); } +int do_epoll_ctl(int epfd, int op, int fd, struct epoll_event *epds, + bool nonblock); + +/* Tells if the epoll_ctl(2) operation needs an event copy from userspace */ +static inline int ep_op_has_event(int op) +{ + return op != EPOLL_CTL_DEL; +} + #else static inline void eventpoll_init_file(struct file *file) {} From patchwork Wed Jan 22 16:42:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11346073 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 455451820 for ; Wed, 22 Jan 2020 16:42:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 240482465A for ; Wed, 22 Jan 2020 16:42:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20150623.gappssmtp.com header.i=@kernel-dk.20150623.gappssmtp.com header.b="1x3paWK2" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726621AbgAVQmv (ORCPT ); Wed, 22 Jan 2020 11:42:51 -0500 Received: from mail-io1-f66.google.com ([209.85.166.66]:41239 "EHLO mail-io1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725970AbgAVQmu (ORCPT ); Wed, 22 Jan 2020 11:42:50 -0500 Received: by mail-io1-f66.google.com with SMTP id m25so7259217ioo.8 for ; Wed, 22 Jan 2020 08:42:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Lw+J2xi976wnKQAQkpo+sULjl/GCtrZo1kXXLVHWSQs=; b=1x3paWK2Cfi6ijmuaglijsnNVNAqxSIjpplllPrVPkY3Scrwee1dttVJHBtV2sYQd8 CWevNHr2V4S+WaXcqVW8TpXi0GbnDKiDYDLBTpsItcvCOQnU7ZvG9GbPKOpZtqCQRNMU +y3K6I7cF22TLpxW4BUldwFH0M19IJQVCywGb/vUWC3MAM2JgeMEwPwxIvpuBKtstzjB yK/sTkmc/8xMYBqMfOcBvKZp6AZGHNzFwQYvUj9hNCnrdXeCvKFwAaJbkbfKc5JZwZV7 GwVsCHIcwrWeW1j5gLoD6TriWf48q0WR5KIozQ68H7IPk4arSZmAbG6dz9YwMDWW7q4i gbjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Lw+J2xi976wnKQAQkpo+sULjl/GCtrZo1kXXLVHWSQs=; b=Jh3KrRLkylG/HVAWkDl5+8H6zQA8OLtsdOvpe/kr3FjlbRR5ufj6KFlCtGKs9IenLL 85VTeKzHnYE/uLm7/jvxIsBj8Yw5hYx/jRU12xsD6RFBubk1QSpx/biyaImiWtbbU6Fy 4ZIJkRus3Oy1fZE+YaSkm8GHNz3bbYw0X2V6KQcY5tev85+y4zCZ62mfLFfaASr/Clrr LEoxcU2wvsJU+Byej02KHOxAogADqkMA4MZSAFsvImT1E9kiyh3e17FnI4E5aEjlSkMp J1AY/qyf5FkFJ/Ek9l30soKRcSoiJaXga76TradZ8yvOnhNU5oujlPjTUStQYNR3ARnI oqYQ== X-Gm-Message-State: APjAAAXT3hVCTC/eEian7lN8h86FS+Ss6ohjkm5PDIpLh4J3hojMLTmm 6nEqhcqVY2XBabLOiaRg3DKU4g== X-Google-Smtp-Source: APXvYqwUIUrCuVXxIF1ib3EmjDf3/PUroHs1scSnugdzhBGw7PikBwAl7cB7Rkw0JRqEV44Pbwv1YA== X-Received: by 2002:a05:6638:24f:: with SMTP id w15mr8125624jaq.130.1579711369919; Wed, 22 Jan 2020 08:42:49 -0800 (PST) Received: from x1.localdomain ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id o6sm14599681ilc.76.2020.01.22.08.42.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jan 2020 08:42:49 -0800 (PST) From: Jens Axboe To: io-uring@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, jannh@google.com, Jens Axboe Subject: [PATCH 3/3] io_uring: add support for epoll_ctl(2) Date: Wed, 22 Jan 2020 09:42:44 -0700 Message-Id: <20200122164244.27799-4-axboe@kernel.dk> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200122164244.27799-1-axboe@kernel.dk> References: <20200122164244.27799-1-axboe@kernel.dk> MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org This adds IORING_OP_EPOLL_CTL, which can perform the same work as the epoll_ctl(2) system call. Signed-off-by: Jens Axboe --- fs/io_uring.c | 72 +++++++++++++++++++++++++++++++++++ include/uapi/linux/io_uring.h | 1 + 2 files changed, 73 insertions(+) diff --git a/fs/io_uring.c b/fs/io_uring.c index 09503d1e9e45..64dc9e5df6d4 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -74,6 +74,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -421,6 +422,14 @@ struct io_madvise { u32 advice; }; +struct io_epoll { + struct file *file; + int epfd; + int op; + int fd; + struct epoll_event event; +}; + struct io_async_connect { struct sockaddr_storage address; }; @@ -534,6 +543,7 @@ struct io_kiocb { struct io_files_update files_update; struct io_fadvise fadvise; struct io_madvise madvise; + struct io_epoll epoll; }; struct io_async_ctx *io; @@ -719,6 +729,9 @@ static const struct io_op_def io_op_defs[] = { .needs_file = 1, .fd_non_neg = 1, }, + [IORING_OP_EPOLL_CTL] = { + .unbound_nonreg_file = 1, + }, }; static void io_wq_submit_work(struct io_wq_work **workptr); @@ -2578,6 +2591,54 @@ static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, return io_openat2(req, nxt, force_nonblock); } +static int io_epoll_ctl_prep(struct io_kiocb *req, + const struct io_uring_sqe *sqe) +{ +#if defined(CONFIG_EPOLL) + if (sqe->ioprio || sqe->buf_index) + return -EINVAL; + + req->epoll.epfd = READ_ONCE(sqe->fd); + req->epoll.op = READ_ONCE(sqe->len); + req->epoll.fd = READ_ONCE(sqe->off); + + if (ep_op_has_event(req->epoll.op)) { + struct epoll_event __user *ev; + + ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); + if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) + return -EFAULT; + } + + return 0; +#else + return -EOPNOTSUPP; +#endif +} + +static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt, + bool force_nonblock) +{ +#if defined(CONFIG_EPOLL) + struct io_epoll *ie = &req->epoll; + int ret; + + ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); + if (force_nonblock && ret == -EAGAIN) { + req->work.flags |= IO_WQ_WORK_NEEDS_FILES; + return -EAGAIN; + } + + if (ret < 0) + req_set_fail_links(req); + io_cqring_add_event(req, ret); + io_put_req_find_next(req, nxt); + return 0; +#else + return -EOPNOTSUPP; +#endif +} + static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) { #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) @@ -4039,6 +4100,9 @@ static int io_req_defer_prep(struct io_kiocb *req, case IORING_OP_OPENAT2: ret = io_openat2_prep(req, sqe); break; + case IORING_OP_EPOLL_CTL: + ret = io_epoll_ctl_prep(req, sqe); + break; default: printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", req->opcode); @@ -4267,6 +4331,14 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, } ret = io_openat2(req, nxt, force_nonblock); break; + case IORING_OP_EPOLL_CTL: + if (sqe) { + ret = io_epoll_ctl_prep(req, sqe); + if (ret) + break; + } + ret = io_epoll_ctl(req, nxt, force_nonblock); + break; default: ret = -EINVAL; break; diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index 57d05cc5e271..cffa6fd33827 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -106,6 +106,7 @@ enum { IORING_OP_SEND, IORING_OP_RECV, IORING_OP_OPENAT2, + IORING_OP_EPOLL_CTL, /* this goes last, obviously */ IORING_OP_LAST,