Sat Jul 29 23:51:29 2023 UTC ()
sys_memfd: Fix logic errors for offset in the previous.


(rin)
diff -r1.6 -r1.7 src/sys/kern/sys_memfd.c

cvs diff -r1.6 -r1.7 src/sys/kern/sys_memfd.c (expand / switch to unified diff)

--- src/sys/kern/sys_memfd.c 2023/07/29 17:54:54 1.6
+++ src/sys/kern/sys_memfd.c 2023/07/29 23:51:29 1.7
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: sys_memfd.c,v 1.6 2023/07/29 17:54:54 christos Exp $ */ 1/* $NetBSD: sys_memfd.c,v 1.7 2023/07/29 23:51:29 rin Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2023 The NetBSD Foundation, Inc. 4 * Copyright (c) 2023 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Theodore Preduta. 8 * by Theodore Preduta.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -20,27 +20,27 @@ @@ -20,27 +20,27 @@
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: sys_memfd.c,v 1.6 2023/07/29 17:54:54 christos Exp $"); 33__KERNEL_RCSID(0, "$NetBSD: sys_memfd.c,v 1.7 2023/07/29 23:51:29 rin Exp $");
34 34
35#include <sys/param.h> 35#include <sys/param.h>
36#include <sys/types.h> 36#include <sys/types.h>
37 37
38#include <sys/fcntl.h> 38#include <sys/fcntl.h>
39#include <sys/file.h> 39#include <sys/file.h>
40#include <sys/filedesc.h> 40#include <sys/filedesc.h>
41#include <sys/memfd.h> 41#include <sys/memfd.h>
42#include <sys/mman.h> 42#include <sys/mman.h>
43#include <sys/syscallargs.h> 43#include <sys/syscallargs.h>
44 44
45#include <uvm/uvm_extern.h> 45#include <uvm/uvm_extern.h>
46#include <uvm/uvm_object.h> 46#include <uvm/uvm_object.h>
@@ -150,32 +150,32 @@ memfd_read(file_t *fp, off_t *offp, stru @@ -150,32 +150,32 @@ memfd_read(file_t *fp, off_t *offp, stru
150 mutex_enter(&fp->f_lock); 150 mutex_enter(&fp->f_lock);
151 151
152 if (*offp < 0) { 152 if (*offp < 0) {
153 error = EINVAL; 153 error = EINVAL;
154 goto leave; 154 goto leave;
155 } 155 }
156 156
157 /* Trying to read past the end does nothing. */ 157 /* Trying to read past the end does nothing. */
158 if (*offp >= mfd->mfd_size) { 158 if (*offp >= mfd->mfd_size) {
159 error = 0; 159 error = 0;
160 goto leave; 160 goto leave;
161 } 161 }
162 162
163 if (flags & FOF_UPDATE_OFFSET) 163 uio->uio_offset = *offp;
164 *offp = uio->uio_offset; 
165 todo = MIN(uio->uio_resid, mfd->mfd_size - *offp); 164 todo = MIN(uio->uio_resid, mfd->mfd_size - *offp);
166 error = ubc_uiomove(mfd->mfd_uobj, uio, todo, UVM_ADV_SEQUENTIAL, 165 error = ubc_uiomove(mfd->mfd_uobj, uio, todo, UVM_ADV_SEQUENTIAL,
167 UBC_READ|UBC_PARTIALOK); 166 UBC_READ|UBC_PARTIALOK);
168 *offp = uio->uio_offset; 167 if (flags & FOF_UPDATE_OFFSET)
 168 *offp = uio->uio_offset;
169 169
170leave: 170leave:
171 getnanotime(&mfd->mfd_atime); 171 getnanotime(&mfd->mfd_atime);
172 172
173 173
174 mutex_exit(&fp->f_lock); 174 mutex_exit(&fp->f_lock);
175 175
176 return error; 176 return error;
177} 177}
178 178
179static int 179static int
180memfd_write(file_t *fp, off_t *offp, struct uio *uio, kauth_cred_t cred, 180memfd_write(file_t *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
181 int flags) 181 int flags)