From patchwork Wed Apr 3 14:02:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13616220 Received: from mail-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 01FC7148FF1 for ; Wed, 3 Apr 2024 14:04:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712153094; cv=none; b=ANTCyNknG5rY62Zb3uzkU+L7wZso7X5QR1bMj03IVmls7rjS5yHTIuYLdJo39cdU12elCq4hqS0tcOSAipfx6V+WxwW9VRZhCH9jpRC57oI9GItZQWm2FugOdxmqDTQe4fVFDYLYSBEtYCULaO5+qfIjhayxaJ1dWVlX3xN1eCI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712153094; c=relaxed/simple; bh=nKnUH95th5iYE+E4iFcTgJ/16TGJIT26Qlwp258+K5s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uOyytkQqxx4GWxFzDmQW+NQKHcDmGSsX828yM/iNzmPQhDnwco9ufiPiZJG/OSq5JtzuV1PGHOzJcbPvzABHVpHYfwPi3W0oezgaGQtLeZqn3ZiJ2gNZ4K6uMReSTKT4heKWxn/Iad6/Lg0xkpM6KyQc6z7hQP95VdjeEJMzvHY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=Su8LpXo+; arc=none smtp.client-ip=209.85.166.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="Su8LpXo+" Received: by mail-il1-f182.google.com with SMTP id e9e14a558f8ab-369f8526c85so323125ab.1 for ; Wed, 03 Apr 2024 07:04:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1712153092; x=1712757892; darn=vger.kernel.org; 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=0FZepTNIuSDeEcz8U+UNewgZo+OZhoLKPwuweNfu0j4=; b=Su8LpXo+iaQgc5KHnGAZQxWAVgnI4jhgA6qwGfcN86I2hFcui6Vlxo7YrKANwPqwzs jtNs6+aOAyx0bDE0xyYrgjIIeciXPCTyCemNH5ubb8PkKfHeI8SV0h8hHGJ4pEZwHYph ljhuV7CXvhZmoXxc8cHxaZOwfw5GPdedQjUgPXP1kgqwKDQe2bpArO4wKFmkTMxs4J5g K2bMXk0mBYCOjOMATX+2NDEvchOfkiqXIIyrvrnAl0j0/8QnlgEAs83t4XPXrz9P7jMf +XOCVv9nagwHM28LStvtmjnnqLPFDjbrwGxqxgY+GpVaMmCW3CRf/Dpwy4gvfTrzUxuC K45g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712153092; x=1712757892; 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=0FZepTNIuSDeEcz8U+UNewgZo+OZhoLKPwuweNfu0j4=; b=G4WRQw7+Z5WsaCHJ4eLeOw08cVFz2dRVEhDIUevbpN/TFeNuCWfbAm9cgKcEtW+pQq B3V5H6sx+An6brTrS9/ow7FmzMBtHrjOWeikY3mJxFvQ5NiOqU92J9DqFD9D1kND7kYy 60DXIO+cD3fnirgxpNuGvgcdob+K4GzvqbGSuUTXibcotvtl+MKrXxVBx3VvEyzNAlsY 2exzjYOuyIpdTxMbDuSNXDlSz8fh6u2NiLzTyzONgGo4nqH3sQe7ygKOhK/8nRNoolJb 672w2vIpJk085xFqD6wxi10rJ1IM+3a2B52iADqr7MjGzejn2Ew3KGEB4RUasAfXxHfI ZF9w== X-Gm-Message-State: AOJu0YzLaVeXjEv8/me/ErO23OCvQJl0/GsJVcD9lLRbTrBRALH05cEj AqzVJT7AaG2FGXFm/59bH/N6yGrVAdAZPMb1XuQNnOUwelbpqAKqGTCBuol5jWlvQizEiObQWfC e X-Google-Smtp-Source: AGHT+IH3lPKHfU1vOwHJ/4ZtYjlERZYHr6v/CLWGbUSbvDeAFa+fQqeqsFzVoIPeJCMXReEoqMi23Q== X-Received: by 2002:a05:6e02:2218:b0:365:2bd4:2f74 with SMTP id j24-20020a056e02221800b003652bd42f74mr17975319ilf.0.1712153091747; Wed, 03 Apr 2024 07:04:51 -0700 (PDT) Received: from localhost.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id a3-20020a056638164300b0047ef3ea2bdfsm2027098jat.78.2024.04.03.07.04.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 07:04:50 -0700 (PDT) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: brauner@kernel.org, linux-kernel@vger.kernel.org, Jens Axboe Subject: [PATCH 1/3] timerfd: convert to ->read_iter() Date: Wed, 3 Apr 2024 08:02:52 -0600 Message-ID: <20240403140446.1623931-2-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240403140446.1623931-1-axboe@kernel.dk> References: <20240403140446.1623931-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Switch timerfd to using fops->read_iter(), so it can support not just O_NONBLOCK but IOCB_NOWAIT as well. With the latter, users like io_uring interact with timerfds a lot better, as they can be driven purely by the poll trigger. Manually get and install the required fd, so that FMODE_NOWAIT can be set before the file is installed into the file table. No functional changes intended in this patch, it's purely a straight conversion to using the read iterator method. Signed-off-by: Jens Axboe --- fs/timerfd.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/fs/timerfd.c b/fs/timerfd.c index e9c96a0c79f1..b96690b46c1f 100644 --- a/fs/timerfd.c +++ b/fs/timerfd.c @@ -262,17 +262,18 @@ static __poll_t timerfd_poll(struct file *file, poll_table *wait) return events; } -static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count, - loff_t *ppos) +static ssize_t timerfd_read_iter(struct kiocb *iocb, struct iov_iter *to) { + struct file *file = iocb->ki_filp; struct timerfd_ctx *ctx = file->private_data; ssize_t res; u64 ticks = 0; - if (count < sizeof(ticks)) + if (iov_iter_count(to) < sizeof(ticks)) return -EINVAL; + spin_lock_irq(&ctx->wqh.lock); - if (file->f_flags & O_NONBLOCK) + if (file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT) res = -EAGAIN; else res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks); @@ -313,7 +314,7 @@ static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count, } spin_unlock_irq(&ctx->wqh.lock); if (ticks) - res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks); + res = copy_to_iter(&ticks, sizeof(ticks), to); return res; } @@ -384,7 +385,7 @@ static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg static const struct file_operations timerfd_fops = { .release = timerfd_release, .poll = timerfd_poll, - .read = timerfd_read, + .read_iter = timerfd_read_iter, .llseek = noop_llseek, .show_fdinfo = timerfd_show, .unlocked_ioctl = timerfd_ioctl, @@ -407,6 +408,7 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) { int ufd; struct timerfd_ctx *ctx; + struct file *file; /* Check the TFD_* constants for consistency. */ BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC); @@ -443,11 +445,22 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) ctx->moffs = ktime_mono_to_real(0); - ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx, - O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS)); - if (ufd < 0) + ufd = get_unused_fd_flags(O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS)); + if (ufd < 0) { kfree(ctx); + return ufd; + } + + file = anon_inode_getfile("[timerfd]", &timerfd_fops, ctx, + O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS)); + if (IS_ERR(file)) { + put_unused_fd(ufd); + kfree(ctx); + return PTR_ERR(file); + } + file->f_mode |= FMODE_NOWAIT; + fd_install(ufd, file); return ufd; } From patchwork Wed Apr 3 14:02:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13616221 Received: from mail-io1-f42.google.com (mail-io1-f42.google.com [209.85.166.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54C661494C8 for ; Wed, 3 Apr 2024 14:04:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712153096; cv=none; b=qNMx2qjXoZuzK+xRuUMVseeZzcNZMsO6QOy8nMy3cfVamp0noobfU8p+zsIdBSPg/ko571xZvYhNFG3xW7QT1ZDfGJZgzl1598Km/aSpA0mGcuAHP4d57w+mHlupnLJq5LFca/bUEZuUuBGELOeV/YOuCOzhduY0oi0yGpxOuIw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712153096; c=relaxed/simple; bh=6STkbNF9IykgRTEcdff9wMG+OREzNqGuxRB0b8pdocg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GUGYVzUusoVbOoUvc955OKkRx/ANYBzqN8fTS16TtVJjq1EHRtNEKtfYRHnPuxhzbJmRAPlV1un/lgh6TOljyqBWq+Zr4Tr45qlI81MJnZeAGiFMhayl8CXsuwaboHh1asp7FFtXjWwhrV85Mp77Ezs/eRIMmXkWw5qdjljbNvE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=rzr89TDQ; arc=none smtp.client-ip=209.85.166.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="rzr89TDQ" Received: by mail-io1-f42.google.com with SMTP id ca18e2360f4ac-7d341631262so21025439f.0 for ; Wed, 03 Apr 2024 07:04:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1712153094; x=1712757894; darn=vger.kernel.org; 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=Z/02XYy/LfSK56jjNNQ4gWduGouBBNFPrgasM4Sr4mA=; b=rzr89TDQcUtgS8BElm8txGhRp+UOLHEQKfUqPBYzs56deAzKpMYvNVyQa0U6uoWj6z jv3VZN5xALjQ0aYWrSbNdvJ4PmH7cvmo63ONR2aOkcYdXbOmhvwhHGzaZC8rj4L0UxZc 0H8Vud6Puzl6WfM6fRcw0+DueMPbuZBohnUYcov/9I9rvPGc4Kp+85DrfHbM+A6OnYjj JcpCKovllsnbuiwkOjUDiDNCDop1mbVtK4I5yKLhFhBUSC1kB8eiDnlktT6dRHelRlC1 p9crDhHczuiyHa6mCMgsU6ZbOGsoLVmBkBXzBKNj0QBCJCHXczHUmYN7d0YyGAXagR7W RIrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712153094; x=1712757894; 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=Z/02XYy/LfSK56jjNNQ4gWduGouBBNFPrgasM4Sr4mA=; b=Ux0xyml0mgcBzNapkXPaDkMcNvkhRWro5K7vz6s4CmBDLyELlgD86f598xMNxq49op y8uGZWzBvz87b3CoxTpsavZ56q0UGb5N9hQ3aJ6coDg5ycItqvNSsyPst3UXt2kKMH8w u7+p4pP2WOGpvztGrEfQ1evmW1wi7MXNca0ElYPxVxSA9tY6E7aiw0gNvlsi9TgyXS5a Z+PcSdhx1Fxb4Gn7WmVJyRb2uj/1dEYtKyfz0CBWNIfjqTbvEwFt7rIcOtwZVu0NU8Il DpZmMv9YsP3q8kC65n270NeOLmdg2v9JPE7m/lmNX9YLQcKavx3ouNgcAivQaea9FQXv hCCQ== X-Gm-Message-State: AOJu0YyQe10+uaN+Aew+5euH4hIcpOwjaVwWl2+mo0B1K48oDFqnlZyN JEtZtNMPIS/ISyy/Wo6p+4mXPEQYiLKRVHp/Q4ddI5KwfYTcaW0eeVp+plA87XRJrAqA2Q23Vkt 2 X-Google-Smtp-Source: AGHT+IFGqYAojbvaoIpX7A9sUe4fZS+QXUdR7LHOFgZEIG7dI5Xy+Zv7TlHS4XMezmx0QW3VMe40AQ== X-Received: by 2002:a5d:9b1a:0:b0:7d0:8461:7819 with SMTP id y26-20020a5d9b1a000000b007d084617819mr15118977ion.1.1712153093784; Wed, 03 Apr 2024 07:04:53 -0700 (PDT) Received: from localhost.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id a3-20020a056638164300b0047ef3ea2bdfsm2027098jat.78.2024.04.03.07.04.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 07:04:52 -0700 (PDT) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: brauner@kernel.org, linux-kernel@vger.kernel.org, Jens Axboe Subject: [PATCH 2/3] userfaultfd: convert to ->read_iter() Date: Wed, 3 Apr 2024 08:02:53 -0600 Message-ID: <20240403140446.1623931-3-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240403140446.1623931-1-axboe@kernel.dk> References: <20240403140446.1623931-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Rather than use the older style ->read() hook, use ->read_iter() so that userfaultfd can support both O_NONBLOCK and IOCB_NOWAIT for non-blocking read attempts. Split the fd setup into two parts, so that userfaultfd can mark the file mode with FMODE_NOWAIT before installing it into the process table. With that, we can also defer grabbing the mm until we know the rest will succeed, as the fd isn't visible before then. Signed-off-by: Jens Axboe --- fs/userfaultfd.c | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 60dcfafdc11a..d2f5409d60b6 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -31,6 +31,7 @@ #include #include #include +#include static int sysctl_unprivileged_userfaultfd __read_mostly; @@ -282,7 +283,7 @@ static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx, /* * Verify the pagetables are still not ok after having reigstered into * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any - * userfault that has already been resolved, if userfaultfd_read and + * userfault that has already been resolved, if userfaultfd_read_iter and * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different * threads. */ @@ -1177,34 +1178,34 @@ static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait, return ret; } -static ssize_t userfaultfd_read(struct file *file, char __user *buf, - size_t count, loff_t *ppos) +static ssize_t userfaultfd_read_iter(struct kiocb *iocb, struct iov_iter *to) { + struct file *file = iocb->ki_filp; struct userfaultfd_ctx *ctx = file->private_data; ssize_t _ret, ret = 0; struct uffd_msg msg; - int no_wait = file->f_flags & O_NONBLOCK; struct inode *inode = file_inode(file); + bool no_wait; if (!userfaultfd_is_initialized(ctx)) return -EINVAL; + no_wait = file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT; for (;;) { - if (count < sizeof(msg)) + if (iov_iter_count(to) < sizeof(msg)) return ret ? ret : -EINVAL; _ret = userfaultfd_ctx_read(ctx, no_wait, &msg, inode); if (_ret < 0) return ret ? ret : _ret; - if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg))) + _ret = copy_to_iter(&msg, sizeof(msg), to); + if (_ret < 0) return ret ? ret : -EFAULT; ret += sizeof(msg); - buf += sizeof(msg); - count -= sizeof(msg); /* * Allow to read more than one fault at time but only * block if waiting for the very first one. */ - no_wait = O_NONBLOCK; + no_wait = true; } } @@ -2172,7 +2173,7 @@ static const struct file_operations userfaultfd_fops = { #endif .release = userfaultfd_release, .poll = userfaultfd_poll, - .read = userfaultfd_read, + .read_iter = userfaultfd_read_iter, .unlocked_ioctl = userfaultfd_ioctl, .compat_ioctl = compat_ptr_ioctl, .llseek = noop_llseek, @@ -2192,6 +2193,7 @@ static void init_once_userfaultfd_ctx(void *mem) static int new_userfaultfd(int flags) { struct userfaultfd_ctx *ctx; + struct file *file; int fd; BUG_ON(!current->mm); @@ -2215,16 +2217,26 @@ static int new_userfaultfd(int flags) init_rwsem(&ctx->map_changing_lock); atomic_set(&ctx->mmap_changing, 0); ctx->mm = current->mm; - /* prevent the mm struct to be freed */ - mmgrab(ctx->mm); + + fd = get_unused_fd_flags(O_RDONLY | (flags & UFFD_SHARED_FCNTL_FLAGS)); + if (fd < 0) + goto err_out; /* Create a new inode so that the LSM can block the creation. */ - fd = anon_inode_create_getfd("[userfaultfd]", &userfaultfd_fops, ctx, + file = anon_inode_create_getfile("[userfaultfd]", &userfaultfd_fops, ctx, O_RDONLY | (flags & UFFD_SHARED_FCNTL_FLAGS), NULL); - if (fd < 0) { - mmdrop(ctx->mm); - kmem_cache_free(userfaultfd_ctx_cachep, ctx); + if (IS_ERR(file)) { + put_unused_fd(fd); + fd = PTR_ERR(file); + goto err_out; } + /* prevent the mm struct to be freed */ + mmgrab(ctx->mm); + file->f_mode |= FMODE_NOWAIT; + fd_install(fd, file); + return fd; +err_out: + kmem_cache_free(userfaultfd_ctx_cachep, ctx); return fd; } From patchwork Wed Apr 3 14:02:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13616222 Received: from mail-il1-f181.google.com (mail-il1-f181.google.com [209.85.166.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2B8F149C47 for ; Wed, 3 Apr 2024 14:04:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712153098; cv=none; b=XLMO/SP7mi0YtGm2f4URSGMh2qUClJnYhFBDwFXft3ghcHozZs8sp6xNNu3tf2I8x0KbvzRDfjS4yvIw3BD+SZOryVg7CLCb6IXsso8XI9gySAy/k2pgSEXwEqSy8OxwuWO03AtBs9fsvA31j+pC6v2jGOUI2PqiSHMGtxDHp94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712153098; c=relaxed/simple; bh=RYxshW62rNDqV/XwwfM71cdZpZnhqfyASR91PbgeFwI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dc+GXDYH/06mA3SGaJRCpQ/x6Bawdc6RbwsuBEvKYYSU7aOtqret/h4YJ/m6FS+RuAXoUVfpMdnfN6+qF79Vql0Hn/gJISO0PlIaMn8UNrre19DxwMb6NqHbzQZDd+oUjBGbBAcqlIsdFUMYX4C7vpjexPmEFvRhNf7M5Q7UZqc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=o7xE+H1r; arc=none smtp.client-ip=209.85.166.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="o7xE+H1r" Received: by mail-il1-f181.google.com with SMTP id e9e14a558f8ab-369f8526c85so323305ab.1 for ; Wed, 03 Apr 2024 07:04:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1712153096; x=1712757896; darn=vger.kernel.org; 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=7phpRQzKHUb5cKw5ojjkDKoDkjW2SeY3RKIXhDXMeoI=; b=o7xE+H1rwltHEdaNPiOdFytvVMA+f5fVW0FwhAv57WBI4XaC3HQK/NFMkgCBAGMzc8 1tSQRmE3JlGfMTbC7+KYX8MW8tUKP0QGCWTiqpCcLHlt+oj8+IAd5VacVRXZKrN8z+Hd PLSfvF3QpakKaefxy/MQN/5KUvGXB1wl4e6sIdYJoH5PXya/tRPANJ+HWtLDWDsn9ic3 NSsqrZTy83FQ6jT0ZuypCm5dYAw4GlyxADV9OgIn7qUdigSqJoNxmoXDSFjqahcxUZEz uE9STY8qpDskqs0KgsekQjBq06Eh1sKivD/oYnbWGAJ9oUFk1hXq+QeeicE/9mKld6x5 2skA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712153096; x=1712757896; 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=7phpRQzKHUb5cKw5ojjkDKoDkjW2SeY3RKIXhDXMeoI=; b=SLerX1UxonJkxZYvUuLQAHVWujIA8i17Eb/iibwstPeI4nHrjkReGi+zMKLSWCcnsG wXiTGG4zfOat7axAUQInTBR4h2Ep16Fatvhi6tp+U3Fvt9uPaHpCsTA/XqITwYBvf+UD pSiZPwtZbS0ZaixhsaKwgeMsAtnn1IrP511fWeRXxeQJOWpTI/l3kXHuJ+ARz8fabfcN Yvg/xC7hlVjthRnh96cVEOpSg0urBxLRAHfy3tARsI7lgqBH6wMG3uPQXCBV9rI+3y5z EXPDAmNbB2795blT+AvqFVbBdTZ2ZS4am+tG+HyRSD6lzT2FJCDH3am/sBan0edSjUOS N5xw== X-Gm-Message-State: AOJu0YyglrMvnL8C2qZpHxxx13PDd6Xr7uyOvaqzrt3pw9qac1hyqDP7 F68nxQjH5QyZf/P0l5YIHOBXHy4MEf3Gk1YotZDfVxOX7WxwKb9bS6mxVFQx4G6FZajf+pG8Zt7 q X-Google-Smtp-Source: AGHT+IEBppmO1/3FbazzRqFUqPwmzgcKQdhokjBVL/E2xk7pqmtXNeh2uBIDR19/CXN3YT0AOJ1H0w== X-Received: by 2002:a05:6e02:2218:b0:365:2bd4:2f74 with SMTP id j24-20020a056e02221800b003652bd42f74mr17975633ilf.0.1712153095826; Wed, 03 Apr 2024 07:04:55 -0700 (PDT) Received: from localhost.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id a3-20020a056638164300b0047ef3ea2bdfsm2027098jat.78.2024.04.03.07.04.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 07:04:54 -0700 (PDT) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: brauner@kernel.org, linux-kernel@vger.kernel.org, Jens Axboe Subject: [PATCH 3/3] signalfd: convert to ->read_iter() Date: Wed, 3 Apr 2024 08:02:54 -0600 Message-ID: <20240403140446.1623931-4-axboe@kernel.dk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240403140446.1623931-1-axboe@kernel.dk> References: <20240403140446.1623931-1-axboe@kernel.dk> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Rather than use the older style ->read() hook, use ->read_iter() so that signalfd can support both O_NONBLOCK and IOCB_NOWAIT for non-blocking read attempts. Split the fd setup into two parts, so that signalfd can mark the file mode with FMODE_NOWAIT before installing it into the process table. Signed-off-by: Jens Axboe --- fs/signalfd.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/fs/signalfd.c b/fs/signalfd.c index e20d1484c663..72b6796a9a40 100644 --- a/fs/signalfd.c +++ b/fs/signalfd.c @@ -68,8 +68,7 @@ static __poll_t signalfd_poll(struct file *file, poll_table *wait) /* * Copied from copy_siginfo_to_user() in kernel/signal.c */ -static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo, - kernel_siginfo_t const *kinfo) +static int signalfd_copyinfo(struct iov_iter *to, kernel_siginfo_t const *kinfo) { struct signalfd_siginfo new; @@ -146,10 +145,7 @@ static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo, break; } - if (copy_to_user(uinfo, &new, sizeof(struct signalfd_siginfo))) - return -EFAULT; - - return sizeof(*uinfo); + return copy_to_iter(&new, sizeof(struct signalfd_siginfo), to); } static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info, @@ -199,28 +195,27 @@ static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info * error code. The "count" parameter must be at least the size of a * "struct signalfd_siginfo". */ -static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count, - loff_t *ppos) +static ssize_t signalfd_read_iter(struct kiocb *iocb, struct iov_iter *to) { + struct file *file = iocb->ki_filp; struct signalfd_ctx *ctx = file->private_data; - struct signalfd_siginfo __user *siginfo; - int nonblock = file->f_flags & O_NONBLOCK; + size_t count = iov_iter_count(to); ssize_t ret, total = 0; kernel_siginfo_t info; + bool nonblock; count /= sizeof(struct signalfd_siginfo); if (!count) return -EINVAL; - siginfo = (struct signalfd_siginfo __user *) buf; + nonblock = file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT; do { ret = signalfd_dequeue(ctx, &info, nonblock); if (unlikely(ret <= 0)) break; - ret = signalfd_copyinfo(siginfo, &info); + ret = signalfd_copyinfo(to, &info); if (ret < 0) break; - siginfo++; total += ret; nonblock = 1; } while (--count); @@ -246,7 +241,7 @@ static const struct file_operations signalfd_fops = { #endif .release = signalfd_release, .poll = signalfd_poll, - .read = signalfd_read, + .read_iter = signalfd_read_iter, .llseek = noop_llseek, }; @@ -265,20 +260,35 @@ static int do_signalfd4(int ufd, sigset_t *mask, int flags) signotset(mask); if (ufd == -1) { + struct file *file; + ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); if (!ctx) return -ENOMEM; ctx->sigmask = *mask; + ufd = get_unused_fd_flags(O_RDWR | + (flags & (O_CLOEXEC | O_NONBLOCK))); + if (ufd < 0) { + kfree(ctx); + return ufd; + } + + file = anon_inode_getfile("[signalfd]", &signalfd_fops, ctx, + O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK))); + if (IS_ERR(file)) { + put_unused_fd(ufd); + kfree(ctx); + return ufd; + } + file->f_mode |= FMODE_NOWAIT; + /* * When we call this, the initialization must be complete, since * anon_inode_getfd() will install the fd. */ - ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx, - O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK))); - if (ufd < 0) - kfree(ctx); + fd_install(ufd, file); } else { struct fd f = fdget(ufd); if (!f.file)