Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp642881pxb; Thu, 19 Nov 2020 10:03:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJw7A1CRYFBgwiW+C4aNTidv/azrGZgSKbKN7/uI3HLMlIZEzdz/9ranqLsE7+/wzLawUCiI X-Received: by 2002:a50:bc02:: with SMTP id j2mr33419649edh.317.1605809017890; Thu, 19 Nov 2020 10:03:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605809017; cv=none; d=google.com; s=arc-20160816; b=jTMu9ux+wO2jYBxjeVjncO/pFwhoQppJLy8bSP9X8muEDqGlsTHvsu4cqhS6qLZ6Zz dOdS9IPmAsHWaNihCAvujUksH0vtemvk83XroWKwU0DeLkkJYq+tDx4dRABbcd5hbJey KVtTIbgS06plzEmRidMTjKrgmd+YmYBM9ky9UMdDgWxCJVcXPVqElU1jJE8D3+clegfW CluxI4gyh/vYf8WGeJY3jCnwbXYISCFWuYwxpNglNKU3iuex2QuJ21fBMXR4/ZRIQchs pnqVNTz5yooLkH+lvGloP5CyYNK7OSbFx3XGGAoriq4maYm+c9EWqcsOAfsJ6jVsGIM3 lUtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:date:cc:to:subject:from:message-id; bh=BESaRB62vpt4rp9jsBfocSy/OGFqFh+2WuRPO1hFQJg=; b=hs3C7vfIA5E02gGZv7izH8ta3T4katY0Zqc9UYYIiWfdiGQmbZpBNYfHzhUjIiosHT r2REAXSxH2EBap9c+4wYVmLt7wmraAZtTeaoWt8SgO+/+WHjOVK847hvKc/E32sJG/kT 7yeWl6HcVAbwQ02wOSmQXI4nSu5te4IiP+ha1JZoAJKr09OYk1h/+OagJz8u4NYI0wNf BhxPpYnrDuVpk23vCNqX3F0pAV9QZF4YCKGLTVca2eUGMcTLQmi7tpU/ONcWY263LjBt rqeHATalX6VN63Vz7sR7pLwLWu+ZwovfBJQsXzN3r8riTJodFC/hJXHbdp3bxwkD6PhM cY+g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e16si270092eds.500.2020.11.19.10.03.10; Thu, 19 Nov 2020 10:03:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729103AbgKSSAW (ORCPT + 99 others); Thu, 19 Nov 2020 13:00:22 -0500 Received: from mx2.suse.de ([195.135.220.15]:44800 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727016AbgKSSAV (ORCPT ); Thu, 19 Nov 2020 13:00:21 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id D4904AC2D; Thu, 19 Nov 2020 18:00:19 +0000 (UTC) Received: by lion.mk-sys.cz (Postfix, from userid 1000) id 99F9D603F9; Thu, 19 Nov 2020 19:00:19 +0100 (CET) Message-Id: From: Michal Kubecek Subject: [PATCH v2] eventfd: convert to ->write_iter() To: Alexander Viro Cc: linux-fsdevel@vger.kernel.org, Jens Axboe , Christoph Hellwig , linux-kernel@vger.kernel.org Date: Thu, 19 Nov 2020 19:00:19 +0100 (CET) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org While eventfd ->read() callback was replaced by ->read_iter() recently by commit 12aceb89b0bc ("eventfd: convert to f_op->read_iter()"), ->write() was not replaced. Convert also ->write() to ->write_iter() to make the interface more consistent and allow non-blocking writes from e.g. io_uring. Also reorganize the code and return value handling in a similar way as it was done in eventfd_read(). v2: different reasoning in commit message (no code change) Signed-off-by: Michal Kubecek --- fs/eventfd.c | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/fs/eventfd.c b/fs/eventfd.c index df466ef81ddd..35973d216847 100644 --- a/fs/eventfd.c +++ b/fs/eventfd.c @@ -261,35 +261,36 @@ static ssize_t eventfd_read(struct kiocb *iocb, struct iov_iter *to) return sizeof(ucnt); } -static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count, - loff_t *ppos) +static ssize_t eventfd_write(struct kiocb *iocb, struct iov_iter *from) { + struct file *file = iocb->ki_filp; struct eventfd_ctx *ctx = file->private_data; - ssize_t res; __u64 ucnt; DECLARE_WAITQUEUE(wait, current); - if (count < sizeof(ucnt)) + if (iov_iter_count(from) < sizeof(ucnt)) return -EINVAL; - if (copy_from_user(&ucnt, buf, sizeof(ucnt))) + if (unlikely(!copy_from_iter_full(&ucnt, sizeof(ucnt), from))) return -EFAULT; if (ucnt == ULLONG_MAX) return -EINVAL; spin_lock_irq(&ctx->wqh.lock); - res = -EAGAIN; - if (ULLONG_MAX - ctx->count > ucnt) - res = sizeof(ucnt); - else if (!(file->f_flags & O_NONBLOCK)) { + if (ULLONG_MAX - ctx->count <= ucnt) { + if ((file->f_flags & O_NONBLOCK) || + (iocb->ki_flags & IOCB_NOWAIT)) { + spin_unlock_irq(&ctx->wqh.lock); + return -EAGAIN; + } __add_wait_queue(&ctx->wqh, &wait); - for (res = 0;;) { + for (;;) { set_current_state(TASK_INTERRUPTIBLE); - if (ULLONG_MAX - ctx->count > ucnt) { - res = sizeof(ucnt); + if (ULLONG_MAX - ctx->count > ucnt) break; - } if (signal_pending(current)) { - res = -ERESTARTSYS; - break; + __remove_wait_queue(&ctx->wqh, &wait); + __set_current_state(TASK_RUNNING); + spin_unlock_irq(&ctx->wqh.lock); + return -ERESTARTSYS; } spin_unlock_irq(&ctx->wqh.lock); schedule(); @@ -298,14 +299,12 @@ static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c __remove_wait_queue(&ctx->wqh, &wait); __set_current_state(TASK_RUNNING); } - if (likely(res > 0)) { - ctx->count += ucnt; - if (waitqueue_active(&ctx->wqh)) - wake_up_locked_poll(&ctx->wqh, EPOLLIN); - } + ctx->count += ucnt; + if (waitqueue_active(&ctx->wqh)) + wake_up_locked_poll(&ctx->wqh, EPOLLIN); spin_unlock_irq(&ctx->wqh.lock); - return res; + return sizeof(ucnt); } #ifdef CONFIG_PROC_FS @@ -328,7 +327,7 @@ static const struct file_operations eventfd_fops = { .release = eventfd_release, .poll = eventfd_poll, .read_iter = eventfd_read, - .write = eventfd_write, + .write_iter = eventfd_write, .llseek = noop_llseek, }; -- 2.29.2