From nobody Thu Sep 18 23:12:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 915FEC43217 for ; Thu, 1 Dec 2022 18:12:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231182AbiLASMj (ORCPT ); Thu, 1 Dec 2022 13:12:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230456AbiLASMX (ORCPT ); Thu, 1 Dec 2022 13:12:23 -0500 Received: from mail-io1-xd2b.google.com (mail-io1-xd2b.google.com [IPv6:2607:f8b0:4864:20::d2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7364B8474 for ; Thu, 1 Dec 2022 10:12:11 -0800 (PST) Received: by mail-io1-xd2b.google.com with SMTP id h184so1440208iof.10 for ; Thu, 01 Dec 2022 10:12:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hAGk4MFjlfW7Mob6ujVIOlyczZbXb2RyPAqfUdaDlEo=; b=Gnqziq8xRmWSBCtI5LbkgYdL2sMBi4ijnnNDCjyb1I6NyYRb4wWQRfi1KeWDcV6woX 2hBG2JA6tPkR84DL4X+rhLHSfSacd65MbGAg32TAPk9YlGxw9kcZgSYXFGNGCC0L5YgY +AuvkWHv/JFbbonRZzYiDMVHxoX9MAkrOh3UCZuIcqxQAktZruc/xq+PU1feO+g8bBS7 DfS7IYpIU+ZQSkmZeoRLSQwzB/++gpslbtWACrtpE7SHmjRbQrhtK/+tRdg7UXMzqpQw Uh8ymHGPUCn+JbALqvUi8JNEliEEGmRAUlCbzIKJhVyPNtyMey7JusEhVCtcfubPIMbD /pfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hAGk4MFjlfW7Mob6ujVIOlyczZbXb2RyPAqfUdaDlEo=; b=zgj1wCh4aHoa5oIhpeom0aGcnIx9NEyemYRp/oxLBKwCcBqozrywT4XZai4zGkqA+j 3GH1XpmLX8lG0p6poZd19Say2qvNYe3MYJbjn08c+YeWd7oJKW4NLOWufbHPcHbXbvrv uQU2WulNXdV/qpqeB33bf2eFMQ1bxBhF2qOPrWStBkcq9daXZYifT0VWvrOYF0zdZS15 +dZztPjsG0oZujtEQUW8PrMcYqSdxTEi9nK1BWSDe+pl2EeTfm3tBF/Rier8UlhFN7ps w8koqCvoE6QXdO/Cn7o1WSDNOP9UOZnSveR+MU/roMmlP2TRJgwPKRjFJivWOknHkLMK eJ0w== X-Gm-Message-State: ANoB5pmllp+IFcMq2c+M9oIijzJrqUb9rlaOvyLIKZoQh8vBIBfngn9A oTIjxAz6cdQ3MsAgf0py+ozSn9jjbfjAQ6QP X-Google-Smtp-Source: AA0mqf43cOjadjWzOUmOk1OU4HLtoIJvOE0jX9fTTnSB5KjGk2DM0Qq0yRWwkjs/cVASjL2efMOJVA== X-Received: by 2002:a05:6638:4709:b0:389:e195:e8fb with SMTP id cs9-20020a056638470900b00389e195e8fbmr10218197jab.254.1669918330191; Thu, 01 Dec 2022 10:12:10 -0800 (PST) Received: from m1max.localdomain ([207.135.234.126]) by smtp.gmail.com with ESMTPSA id y21-20020a027315000000b00374fe4f0bc3sm1842028jab.158.2022.12.01.10.12.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Dec 2022 10:12:09 -0800 (PST) From: Jens Axboe To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: soheil@google.com, willemdebruijn.kernel@gmail.com, stefanha@redhat.com, Jens Axboe Subject: [PATCH 7/7] eventpoll: add method for configuring minimum wait on epoll context Date: Thu, 1 Dec 2022 11:11:56 -0700 Message-Id: <20221201181156.848373-8-axboe@kernel.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221201181156.848373-1-axboe@kernel.dk> References: <20221201181156.848373-1-axboe@kernel.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add support for EPOLL_CTL_MIN_WAIT, which can be used to define a minimum reap time for an epoll context. Basic test case: struct d { int p1, p2; }; static void *fn(void *data) { struct d *d =3D data; char b =3D 0x89; /* Generate 2 events 20 msec apart */ usleep(10000); write(d->p1, &b, sizeof(b)); usleep(10000); write(d->p2, &b, sizeof(b)); return NULL; } int main(int argc, char *argv[]) { struct epoll_event ev, events[2]; pthread_t thread; int p1[2], p2[2]; struct d d; int efd, ret; efd =3D epoll_create1(0); if (efd < 0) { perror("epoll_create"); return 1; } if (pipe(p1) < 0) { perror("pipe"); return 1; } if (pipe(p2) < 0) { perror("pipe"); return 1; } ev.events =3D EPOLLIN; ev.data.fd =3D p1[0]; if (epoll_ctl(efd, EPOLL_CTL_ADD, p1[0], &ev) < 0) { perror("epoll add"); return 1; } ev.events =3D EPOLLIN; ev.data.fd =3D p2[0]; if (epoll_ctl(efd, EPOLL_CTL_ADD, p2[0], &ev) < 0) { perror("epoll add"); return 1; } /* always wait 200 msec for events */ ev.data.u64 =3D 200000; if (epoll_ctl(efd, EPOLL_CTL_MIN_WAIT, -1, &ev) < 0) { perror("epoll add set timeout"); return 1; } d.p1 =3D p1[1]; d.p2 =3D p2[1]; pthread_create(&thread, NULL, fn, &d); /* expect to get 2 events here rather than just 1 */ ret =3D epoll_wait(efd, events, 2, -1); printf("epoll_wait=3D%d\n", ret); return 0; } If EPOLL_CTL_MIN_WAIT is used with a timeout of 0, it is a no-op, and acts the same as if it wasn't called to begin with. Only a non-zero usec delay value will result in a wait time being applied for reaping events. Signed-off-by: Jens Axboe --- fs/eventpoll.c | 13 ++++++++++++- include/linux/eventpoll.h | 2 +- include/uapi/linux/eventpoll.h | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index daa9885d9c2b..ec7ffce8265a 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -2183,6 +2183,17 @@ int do_epoll_ctl(int epfd, int op, int fd, struct ep= oll_event *epds, */ ep =3D f.file->private_data; =20 + /* + * Handle EPOLL_CTL_MIN_WAIT upfront as we don't need to care about + * the fd being passed in. + */ + if (op =3D=3D EPOLL_CTL_MIN_WAIT) { + /* return old value */ + error =3D ep->min_wait_ts; + ep->min_wait_ts =3D epds->data; + goto error_fput; + } + /* Get the "struct file *" for the target file */ tf =3D fdget(fd); if (!tf.file) @@ -2315,7 +2326,7 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, f= d, { struct epoll_event epds; =20 - if (ep_op_has_event(op) && + if ((ep_op_has_event(op) || op =3D=3D EPOLL_CTL_MIN_WAIT) && copy_from_user(&epds, event, sizeof(struct epoll_event))) return -EFAULT; =20 diff --git a/include/linux/eventpoll.h b/include/linux/eventpoll.h index 3337745d81bd..cbef635cb7e4 100644 --- a/include/linux/eventpoll.h +++ b/include/linux/eventpoll.h @@ -59,7 +59,7 @@ int do_epoll_ctl(int epfd, int op, int fd, struct epoll_e= vent *epds, /* Tells if the epoll_ctl(2) operation needs an event copy from userspace = */ static inline int ep_op_has_event(int op) { - return op !=3D EPOLL_CTL_DEL; + return op !=3D EPOLL_CTL_DEL && op !=3D EPOLL_CTL_MIN_WAIT; } =20 #else diff --git a/include/uapi/linux/eventpoll.h b/include/uapi/linux/eventpoll.h index 8a3432d0f0dc..81ecb1ca36e0 100644 --- a/include/uapi/linux/eventpoll.h +++ b/include/uapi/linux/eventpoll.h @@ -26,6 +26,7 @@ #define EPOLL_CTL_ADD 1 #define EPOLL_CTL_DEL 2 #define EPOLL_CTL_MOD 3 +#define EPOLL_CTL_MIN_WAIT 4 =20 /* Epoll event masks */ #define EPOLLIN (__force __poll_t)0x00000001 --=20 2.35.1