Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178136676
D62170.1796077175.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None
D62170.1796077175.diff
View Options
diff --git a/sys/dev/ksyms/ksyms.c b/sys/dev/ksyms/ksyms.c
--- a/sys/dev/ksyms/ksyms.c
+++ b/sys/dev/ksyms/ksyms.c
@@ -171,7 +171,7 @@
uio.uio_rw = UIO_WRITE;
uio.uio_td = curthread;
- return (uiomove_object(sc->sc_obj, sc->sc_objsz, &uio));
+ return (uiomove_object(sc->sc_obj, sc->sc_objsz, &uio, 0));
}
#define SYMBLKSZ (256 * sizeof(Elf_Sym))
@@ -467,7 +467,8 @@
error = devfs_get_cdevpriv((void **)&sc);
if (error != 0)
return (error);
- return (uiomove_object(sc->sc_obj, sc->sc_objsz, uio));
+ return (uiomove_object(sc->sc_obj, sc->sc_objsz, uio,
+ UIOMOVE_OBJ_NOBUSY));
}
static int
diff --git a/sys/fs/nullfs/null_vnops.c b/sys/fs/nullfs/null_vnops.c
--- a/sys/fs/nullfs/null_vnops.c
+++ b/sys/fs/nullfs/null_vnops.c
@@ -1121,6 +1121,8 @@
{
struct vnode *lvp, *vp;
struct null_node *xp;
+ struct uio *uio;
+ void *cookie;
int error;
vp = ap->a_vp;
@@ -1133,7 +1135,11 @@
lvp = xp->null_lowervp;
vref(lvp);
VI_UNLOCK(vp);
- error = VOP_READ_PGCACHE(lvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
+ uio = ap->a_uio;
+ cookie = vn_rangelock_rlock(lvp, uio->uio_offset,
+ uio->uio_offset + uio->uio_resid);
+ error = VOP_READ_PGCACHE(lvp, uio, ap->a_ioflag, ap->a_cred);
+ vn_rangelock_unlock(lvp, cookie);
vrele(lvp);
return (error);
}
diff --git a/sys/fs/tmpfs/tmpfs_vnops.c b/sys/fs/tmpfs/tmpfs_vnops.c
--- a/sys/fs/tmpfs/tmpfs_vnops.c
+++ b/sys/fs/tmpfs/tmpfs_vnops.c
@@ -608,7 +608,8 @@
return (EINVAL);
node = VP_TO_TMPFS_NODE(vp);
tmpfs_set_accessed(VFS_TO_TMPFS(vp->v_mount), node);
- return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
+ return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio,
+ UIOMOVE_OBJ_NOBUSY));
}
static int
@@ -646,7 +647,12 @@
size = node->tn_size;
tmpfs_set_accessed(node->tn_reg.tn_tmp, node);
vfs_smr_exit();
- error = uiomove_object(object, size, v->a_uio);
+ /*
+ * Busy the pages: only the rangelock is held, and not
+ * all writers take it, e.g. nfsd and md(4), or writes
+ * through nullfs, which lock the nullfs vnode's range.
+ */
+ error = uiomove_object(object, size, v->a_uio, 0);
return (error);
}
out_smr:
@@ -701,7 +707,8 @@
goto out;
}
- error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
+ error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio,
+ 0);
node->tn_status |= TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED;
node->tn_accessed = true;
if (node->tn_mode & (S_ISUID | S_ISGID)) {
diff --git a/sys/kern/uipc_shm.c b/sys/kern/uipc_shm.c
--- a/sys/kern/uipc_shm.c
+++ b/sys/kern/uipc_shm.c
@@ -75,6 +75,7 @@
#include <sys/resourcevar.h>
#include <sys/rwlock.h>
#include <sys/sbuf.h>
+#include <sys/sf_buf.h>
#include <sys/stat.h>
#include <sys/syscallsubr.h>
#include <sys/sysctl.h>
@@ -193,21 +194,73 @@
#define shm_rangelock_wlock(shmfd, start, end) \
rangelock_wlock(&(shmfd)->shm_rl, (start), (end))
+/* A user buffer chunk of up to PAGE_SIZE bytes spans at most two pages. */
+#define UIOMOVE_OBJECT_MAXHOLD 2
+
+/*
+ * Hold the pages of the user buffer for the next transfer, clipping
+ * *tlenp to the current iovec. Called without the object page busied,
+ * so faulting the buffer in cannot wait for our own busy.
+ */
static int
-uiomove_object_page(vm_object_t obj, size_t len, struct uio *uio)
+uiomove_object_hold(struct uio *uio, size_t *tlenp, vm_page_t *ma,
+ int *countp)
+{
+ struct iovec *iov;
+ vm_prot_t prot;
+ int count;
+
+ while (uio->uio_iov->iov_len == 0) {
+ uio->uio_iov++;
+ uio->uio_iovcnt--;
+ }
+ iov = uio->uio_iov;
+ *tlenp = MIN(*tlenp, iov->iov_len);
+ prot = uio->uio_rw == UIO_WRITE ? VM_PROT_READ : VM_PROT_WRITE;
+ count = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
+ (vm_offset_t)iov->iov_base, *tlenp, prot, ma,
+ UIOMOVE_OBJECT_MAXHOLD);
+ if (count == -1)
+ return (EFAULT);
+ *countp = count;
+ return (0);
+}
+
+/*
+ * Optimization: if safe_nobusy is true, the object page is wired
+ * instead of busied for the copy, which must be a read. The caller
+ * guarantees that the page is neither modified, other than through
+ * mappings, nor removed from the object meanwhile.
+ */
+static int
+uiomove_object_page(vm_object_t obj, size_t len, struct uio *uio,
+ bool safe_nobusy)
{
struct pctrie_iter pages;
- vm_page_t m;
+ struct sf_buf *sf;
+ vm_page_t m, ma[UIOMOVE_OBJECT_MAXHOLD];
+ vm_offset_t uoffset;
vm_pindex_t idx;
+ ssize_t resid;
size_t tlen;
- int error, offset, rv;
+ int error, flags, held, offset, rv;
+ bool write;
idx = OFF_TO_IDX(uio->uio_offset);
offset = uio->uio_offset & PAGE_MASK;
tlen = MIN(PAGE_SIZE - offset, len);
+ write = uio->uio_rw == UIO_WRITE;
+ if (safe_nobusy)
+ flags = VM_ALLOC_NOBUSY | VM_ALLOC_IGN_SBUSY | VM_ALLOC_WIRED;
+ else if (!write)
+ flags = VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY;
+ else
+ flags = 0;
+ held = 0;
- rv = vm_page_grab_valid_unlocked(&m, obj, idx,
- VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY | VM_ALLOC_NOCREAT);
+again:
+ rv = vm_page_grab_valid_unlocked(&m, obj, idx, flags |
+ VM_ALLOC_NOCREAT);
if (rv == VM_PAGER_OK)
goto found;
@@ -219,54 +272,104 @@
vm_page_iter_init(&pages, obj);
VM_OBJECT_WLOCK(obj);
m = vm_radix_iter_lookup(&pages, idx);
- if (uio->uio_rw == UIO_READ && m == NULL &&
- !vm_pager_has_page(obj, idx, NULL, NULL)) {
+ if (!write && m == NULL && !vm_pager_has_page(obj, idx, NULL, NULL)) {
VM_OBJECT_WUNLOCK(obj);
- return (uiomove(__DECONST(void *, zero_region), tlen, uio));
+ error = uiomove(__DECONST(void *, zero_region), tlen, uio);
+ goto out;
}
/*
- * Although the tmpfs vnode lock is held here, it is
+ * Although a tmpfs vnode lock may be held here, it is
* nonetheless safe to sleep waiting for a free page. The
* pageout daemon does not need to acquire the tmpfs vnode
* lock to page out tobj's pages because tobj is a OBJT_SWAP
* type object.
*/
- rv = vm_page_grab_valid_iter(&m, obj, idx,
- VM_ALLOC_NORMAL | VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY, &pages);
+ rv = vm_page_grab_valid_iter(&m, obj, idx, VM_ALLOC_NORMAL | flags,
+ &pages);
if (rv != VM_PAGER_OK) {
VM_OBJECT_WUNLOCK(obj);
if (bootverbose) {
printf("uiomove_object: vm_obj %p idx %jd "
"pager error %d\n", obj, idx, rv);
}
- return (rv == VM_PAGER_AGAIN ? ENOSPC : EIO);
+ error = rv == VM_PAGER_AGAIN ? ENOSPC : EIO;
+ goto out;
}
VM_OBJECT_WUNLOCK(obj);
found:
- error = uiomove_fromphys(&m, offset, tlen, uio);
- if (uio->uio_rw == UIO_WRITE) {
+ resid = uio->uio_resid;
+ if (held != 0) {
+ uoffset = (vm_offset_t)uio->uio_iov->iov_base & PAGE_MASK;
+ if (write)
+ pmap_copy_pages(ma, uoffset, &m, offset, tlen);
+ else
+ pmap_copy_pages(&m, offset, ma, uoffset, tlen);
+ uioadvance(uio, tlen);
+ error = 0;
+ } else if (uio->uio_segflg == UIO_USERSPACE && !safe_nobusy) {
/*
- * Even a failed copy may have changed the page's contents,
- * because uiomove_fromphys() can copy part of the data before
- * it fails.
+ * The page is busy, and a fault on the user buffer might
+ * need that same busy if the buffer maps the page. Do
+ * not fault, and fall back to holding the buffer below.
*/
- vm_page_set_dirty(m);
+ sf = sf_buf_alloc(m, 0);
+ error = uiomove_nofault((char *)sf_buf_kva(sf) + offset, tlen,
+ uio);
+ sf_buf_free(sf);
+ } else {
+ error = uiomove_fromphys(&m, offset, tlen, uio);
}
- vm_page_activate(m);
- vm_page_sunbusy(m);
+ /*
+ * Even a failed copy may have changed the page's contents,
+ * because the copy can transfer part of the data before
+ * it fails.
+ */
+ if (write)
+ vm_page_set_dirty(m);
+
+ if (safe_nobusy) {
+ vm_page_unwire(m, PQ_ACTIVE);
+ } else {
+ vm_page_activate(m);
+ if (write)
+ vm_page_xunbusy(m);
+ else
+ vm_page_sunbusy(m);
+ }
+
+ if (error == EFAULT && held == 0 && uio->uio_segflg == UIO_USERSPACE &&
+ !safe_nobusy) {
+ if (uio->uio_resid != resid) {
+ /* Let the caller come back for the rest. */
+ error = 0;
+ } else {
+ /*
+ * Fault the buffer in and hold it, with the page
+ * unbusied, then copy between the physical pages.
+ */
+ error = uiomove_object_hold(uio, &tlen, ma, &held);
+ if (error == 0)
+ goto again;
+ }
+ }
+out:
+ if (held != 0)
+ vm_page_unhold_pages(ma, held);
return (error);
}
int
-uiomove_object(vm_object_t obj, off_t obj_size, struct uio *uio)
+uiomove_object(vm_object_t obj, off_t obj_size, struct uio *uio,
+ int flags)
{
ssize_t resid;
size_t len;
int error;
+ MPASS((flags & UIOMOVE_OBJ_NOBUSY) == 0 || uio->uio_rw == UIO_READ);
error = 0;
while ((resid = uio->uio_resid) > 0) {
if (obj_size <= uio->uio_offset)
@@ -274,7 +377,8 @@
len = MIN(obj_size - uio->uio_offset, resid);
if (len == 0)
break;
- error = uiomove_object_page(obj, len, uio);
+ error = uiomove_object_page(obj, len, uio,
+ (flags & UIOMOVE_OBJ_NOBUSY) != 0);
if (error != 0 || resid == uio->uio_resid)
break;
}
@@ -494,7 +598,8 @@
foffset_lock_uio(fp, uio, flags);
rl_cookie = shm_rangelock_rlock(shmfd, uio->uio_offset,
uio->uio_offset + uio->uio_resid);
- error = uiomove_object(shmfd->shm_object, shmfd->shm_size, uio);
+ error = uiomove_object(shmfd->shm_object, shmfd->shm_size, uio,
+ UIOMOVE_OBJ_NOBUSY);
shm_rangelock_unlock(shmfd, rl_cookie);
foffset_unlock_uio(fp, uio, flags);
return (error);
@@ -552,7 +657,7 @@
}
if (error == 0)
error = uiomove_object(shmfd->shm_object,
- shmfd->shm_size, uio);
+ shmfd->shm_size, uio, 0);
}
shm_rangelock_unlock(shmfd, rl_cookie);
foffset_unlock_uio(fp, uio, flags);
diff --git a/sys/sys/uio.h b/sys/sys/uio.h
--- a/sys/sys/uio.h
+++ b/sys/sys/uio.h
@@ -76,6 +76,9 @@
struct vm_page;
struct bus_dma_segment;
+#define UIOMOVE_OBJ_NOBUSY 0x0001 /* safe to not busy the object' page
+ for io */
+
struct uio *allocuio(u_int iovcnt);
void freeuio(struct uio *uio);
struct uio *cloneuio(struct uio *uiop);
@@ -97,7 +100,8 @@
int uiomove_fromphys(struct vm_page *ma[], vm_offset_t offset, int n,
struct uio *uio);
int uiomove_nofault(void *cp, int n, struct uio *uio);
-int uiomove_object(struct vm_object *obj, off_t obj_size, struct uio *uio);
+int uiomove_object(struct vm_object *obj, off_t obj_size, struct uio *uio,
+ int flags);
int uiomove_step(void *cp, void *base, size_t cnt, struct uio *uio);
int updateiov(const struct uio *uiop, void *iovp);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 30, 10:19 PM (8 h, 53 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42114027
Default Alt Text
D62170.1796077175.diff (10 KB)
Attached To
Mode
D62170: Handle races and deadlocks in uiomove_object_page()
Attached
Detach File
Event Timeline