Page MenuHomeFreeBSD

D62170.1796101918.diff
No OneTemporary

Authored By
Unknown
Size
23 KB
Referenced Files
None
Subscribers
None

D62170.1796101918.diff

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's 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);
diff --git a/tests/sys/fs/tmpfs/Makefile b/tests/sys/fs/tmpfs/Makefile
--- a/tests/sys/fs/tmpfs/Makefile
+++ b/tests/sys/fs/tmpfs/Makefile
@@ -6,6 +6,8 @@
TESTSRC= ${SRCTOP}/contrib/netbsd-tests/fs/${FILESYSTEM}
+ATF_TESTS_C+= mapped_io_test
+LIBADD.mapped_io_test+= util
ATF_TESTS_C+= setgid_test
LIBADD.setgid_test+= util
diff --git a/tests/sys/fs/tmpfs/mapped_io_test.c b/tests/sys/fs/tmpfs/mapped_io_test.c
new file mode 100644
--- /dev/null
+++ b/tests/sys/fs/tmpfs/mapped_io_test.c
@@ -0,0 +1,255 @@
+/*
+ * Copyright (c) 2026 The FreeBSD Foundation
+ *
+ * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * I/O between a tmpfs file and its own mapping, with the user buffer
+ * mapping the file page that is written or read. The file is mapped
+ * before its pages are populated, and the pages touched before the I/O
+ * are kept away from the tested pages, so that neither mmap(2) nor the
+ * fault-around prefaults the buffer, and the copy faults on it. The fault
+ * must not wait for the busy of the file page held by the I/O itself,
+ * otherwise the test hangs.
+ */
+
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+
+#include <atf-c.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <mntopts.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define NPAGES 16
+
+static void
+mount_fs(const char *fstype, const char *fspath, const char *target,
+ bool pgread)
+{
+ struct iovec *iov;
+ char errmsg[256];
+ int error, iovlen;
+
+ iov = NULL;
+ iovlen = 0;
+
+ ATF_REQUIRE(mkdir(fspath, 0755) == 0);
+ build_iovec(&iov, &iovlen, "fstype", __DECONST(char *, fstype),
+ (size_t)-1);
+ build_iovec(&iov, &iovlen, "fspath", __DECONST(char *, fspath),
+ (size_t)-1);
+ if (target != NULL) {
+ build_iovec(&iov, &iovlen, "target", __DECONST(char *, target),
+ (size_t)-1);
+ }
+ if (pgread)
+ build_iovec(&iov, &iovlen, "pgread", NULL, 0);
+ build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
+ errmsg[0] = '\0';
+ error = nmount(iov, iovlen, 0);
+ ATF_REQUIRE_MSG(error == 0, "nmount %s: %s", fstype,
+ errmsg[0] != '\0' ? errmsg : strerror(errno));
+ free_iovec(&iov, &iovlen);
+}
+
+static void
+unmount_fs(const char *fspath)
+{
+ if (unmount(fspath, MNT_FORCE) != 0)
+ warn("unmount %s", fspath);
+}
+
+/*
+ * Create the file of NPAGES pages, map it with mflags, and populate the
+ * pages listed in pidx with a pattern, which is also stored into
+ * *expectedp. For a private mapping, COW the last page to create the
+ * shadow object. A fault for read on another page is then resolved in the
+ * file object below the shadow, and busies the file page, while a busy
+ * page of the top object is mapped for read without busying it.
+ */
+static char *
+setup(const char *path, int mflags, const size_t *pidx, size_t npidx,
+ int *fdp, char **expectedp)
+{
+ char *expected, *p;
+ size_t i, len, off, ps;
+ int fd;
+
+ ps = getpagesize();
+ len = NPAGES * ps;
+ fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0644);
+ ATF_REQUIRE_MSG(fd >= 0, "open %s: %s", path, strerror(errno));
+ ATF_REQUIRE_MSG(ftruncate(fd, len) == 0, "ftruncate: %s",
+ strerror(errno));
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE, mflags, fd, 0);
+ ATF_REQUIRE_MSG(p != MAP_FAILED, "mmap: %s", strerror(errno));
+
+ expected = calloc(1, len);
+ ATF_REQUIRE(expected != NULL);
+ for (i = 0; i < npidx; i++) {
+ for (off = pidx[i] * ps; off < (pidx[i] + 1) * ps; off++)
+ expected[off] = (char)(off % 251);
+ ATF_REQUIRE_MSG(pwrite(fd, expected + pidx[i] * ps, ps,
+ pidx[i] * ps) == (ssize_t)ps, "pwrite: %s", strerror(errno));
+ }
+ if ((mflags & MAP_PRIVATE) != 0)
+ p[len - 1] = 1;
+
+ *fdp = fd;
+ *expectedp = expected;
+ return (p);
+}
+
+static void
+finish(int fd, char *p, char *expected)
+{
+ char *buf;
+ size_t len;
+
+ len = NPAGES * getpagesize();
+ buf = malloc(len);
+ ATF_REQUIRE(buf != NULL);
+ ATF_REQUIRE_MSG(pread(fd, buf, len, 0) == (ssize_t)len, "pread: %s",
+ strerror(errno));
+ ATF_CHECK_MSG(memcmp(buf, expected, len) == 0,
+ "unexpected file contents");
+ free(buf);
+ free(expected);
+ ATF_REQUIRE(munmap(p, len) == 0);
+ ATF_REQUIRE(close(fd) == 0);
+}
+
+static void
+do_write_from_private_mapping(const char *path)
+{
+ const size_t pidx[] = { 0, 8, 9 };
+ char *expected, *p;
+ size_t ps;
+ int fd;
+
+ ps = getpagesize();
+ p = setup(path, MAP_PRIVATE, pidx, nitems(pidx), &fd, &expected);
+
+ /* The source is in the destination page. */
+ ATF_REQUIRE_MSG(pwrite(fd, p + 100, 200, 1000) == 200, "pwrite: %s",
+ strerror(errno));
+ memcpy(expected + 1000, expected + 100, 200);
+
+ /* The source spans two pages, the second one is the destination. */
+ ATF_REQUIRE_MSG(pwrite(fd, p + 9 * ps - 100, 300, 9 * ps + 1000) ==
+ 300, "pwrite: %s", strerror(errno));
+ memcpy(expected + 9 * ps + 1000, expected + 9 * ps - 100, 300);
+
+ finish(fd, p, expected);
+}
+
+static void
+do_read_into_shared_mapping(const char *path)
+{
+ const size_t pidx[] = { 0, 8, 9 };
+ char *expected, *p;
+ size_t ps;
+ int fd;
+
+ ps = getpagesize();
+ p = setup(path, MAP_SHARED, pidx, nitems(pidx), &fd, &expected);
+
+ /* The destination is in the source page. */
+ ATF_REQUIRE_MSG(pread(fd, p + 1000, 200, 100) == 200, "pread: %s",
+ strerror(errno));
+ memcpy(expected + 1000, expected + 100, 200);
+
+ /* The destination spans two pages, the second one is the source. */
+ ATF_REQUIRE_MSG(pread(fd, p + 9 * ps - 100, 300, 9 * ps + 1000) ==
+ 300, "pread: %s", strerror(errno));
+ memcpy(expected + 9 * ps - 100, expected + 9 * ps + 1000, 300);
+
+ finish(fd, p, expected);
+}
+
+ATF_TC_WITH_CLEANUP(write_from_private_mapping);
+ATF_TC_HEAD(write_from_private_mapping, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(write_from_private_mapping, tc)
+{
+ mount_fs("tmpfs", "mnt", NULL, false);
+ do_write_from_private_mapping("mnt/file");
+}
+ATF_TC_CLEANUP(write_from_private_mapping, tc)
+{
+ unmount_fs("mnt");
+}
+
+/* tmpfs_read() */
+ATF_TC_WITH_CLEANUP(read_into_shared_mapping);
+ATF_TC_HEAD(read_into_shared_mapping, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(read_into_shared_mapping, tc)
+{
+ mount_fs("tmpfs", "mnt", NULL, false);
+ do_read_into_shared_mapping("mnt/file");
+}
+ATF_TC_CLEANUP(read_into_shared_mapping, tc)
+{
+ unmount_fs("mnt");
+}
+
+/* tmpfs_read_pgcache() */
+ATF_TC_WITH_CLEANUP(read_into_shared_mapping_pgread);
+ATF_TC_HEAD(read_into_shared_mapping_pgread, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(read_into_shared_mapping_pgread, tc)
+{
+ mount_fs("tmpfs", "mnt", NULL, true);
+ do_read_into_shared_mapping("mnt/file");
+}
+ATF_TC_CLEANUP(read_into_shared_mapping_pgread, tc)
+{
+ unmount_fs("mnt");
+}
+
+/* null_read_pgcache() over tmpfs_read_pgcache() */
+ATF_TC_WITH_CLEANUP(read_into_shared_mapping_nullfs);
+ATF_TC_HEAD(read_into_shared_mapping_nullfs, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(read_into_shared_mapping_nullfs, tc)
+{
+ mount_fs("tmpfs", "mnt", NULL, true);
+ mount_fs("nullfs", "mnt2", "mnt", false);
+ do_read_into_shared_mapping("mnt2/file");
+}
+ATF_TC_CLEANUP(read_into_shared_mapping_nullfs, tc)
+{
+ unmount_fs("mnt2");
+ unmount_fs("mnt");
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, write_from_private_mapping);
+ ATF_TP_ADD_TC(tp, read_into_shared_mapping);
+ ATF_TP_ADD_TC(tp, read_into_shared_mapping_pgread);
+ ATF_TP_ADD_TC(tp, read_into_shared_mapping_nullfs);
+ return (atf_no_error());
+}
diff --git a/tests/sys/posixshm/posixshm_test.c b/tests/sys/posixshm/posixshm_test.c
--- a/tests/sys/posixshm/posixshm_test.c
+++ b/tests/sys/posixshm/posixshm_test.c
@@ -37,6 +37,7 @@
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysctl.h>
+#include <sys/uio.h>
#include <sys/wait.h>
#ifdef __amd64__
@@ -1233,6 +1234,156 @@
"close failed; errno=%d", errno);
}
+/*
+ * The following tests do I/O between an object and its own mapping, with
+ * the user buffer mapping the object page that is written or read. The
+ * object is mapped before its pages are populated, and the pages touched
+ * before the I/O are kept away from the tested pages, so that neither
+ * mmap(2) nor the fault-around prefaults the buffer, and the copy faults
+ * on it. The fault must not wait for the busy of the object page held by
+ * the I/O itself, otherwise the test hangs.
+ */
+#define MAPPED_IO_NPAGES 16
+
+/*
+ * Create an object of MAPPED_IO_NPAGES pages, map it with mflags, and
+ * populate the pages listed in pidx with a pattern, which is also stored
+ * into *expectedp. For a private mapping, COW the last page to create the
+ * shadow object. A fault for read on another page is then resolved in the
+ * object below the shadow, and busies the object page, while a busy page
+ * of the top object is mapped for read without busying it.
+ */
+static char *
+mapped_io_setup(int mflags, const size_t *pidx, size_t npidx, int *fdp,
+ char **expectedp)
+{
+ char *expected, *p;
+ size_t i, len, off, ps;
+ int fd;
+
+ ps = getpagesize();
+ len = MAPPED_IO_NPAGES * ps;
+ fd = shm_open(SHM_ANON, O_RDWR, 0600);
+ ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
+ ATF_REQUIRE_MSG(ftruncate(fd, len) == 0,
+ "ftruncate failed; errno=%d", errno);
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE, mflags, fd, 0);
+ ATF_REQUIRE_MSG(p != MAP_FAILED, "mmap failed; errno=%d", errno);
+
+ expected = calloc(1, len);
+ ATF_REQUIRE(expected != NULL);
+ for (i = 0; i < npidx; i++) {
+ for (off = pidx[i] * ps; off < (pidx[i] + 1) * ps; off++)
+ expected[off] = (char)(off % 251);
+ ATF_REQUIRE_MSG(pwrite(fd, expected + pidx[i] * ps, ps,
+ pidx[i] * ps) == (ssize_t)ps,
+ "pwrite failed; errno=%d", errno);
+ }
+ if ((mflags & MAP_PRIVATE) != 0)
+ p[len - 1] = 1;
+
+ *fdp = fd;
+ *expectedp = expected;
+ return (p);
+}
+
+static void
+mapped_io_finish(int fd, char *p, char *expected)
+{
+ char *buf;
+ size_t len;
+
+ len = MAPPED_IO_NPAGES * getpagesize();
+ buf = malloc(len);
+ ATF_REQUIRE(buf != NULL);
+ ATF_REQUIRE_MSG(pread(fd, buf, len, 0) == (ssize_t)len,
+ "pread failed; errno=%d", errno);
+ ATF_CHECK_MSG(memcmp(buf, expected, len) == 0,
+ "unexpected object contents");
+ free(buf);
+ free(expected);
+ ATF_REQUIRE(munmap(p, len) == 0);
+ ATF_REQUIRE(close(fd) == 0);
+}
+
+ATF_TC_WITHOUT_HEAD(write_from_own_private_mapping);
+ATF_TC_BODY(write_from_own_private_mapping, tc)
+{
+ const size_t pidx[] = { 0, 8, 9 };
+ char *expected, *p;
+ size_t ps;
+ int fd;
+
+ ps = getpagesize();
+ p = mapped_io_setup(MAP_PRIVATE, pidx, nitems(pidx), &fd, &expected);
+
+ /* The source is in the destination page. */
+ ATF_REQUIRE_MSG(pwrite(fd, p + 100, 200, 1000) == 200,
+ "pwrite failed; errno=%d", errno);
+ memcpy(expected + 1000, expected + 100, 200);
+
+ /* The source spans two pages, the second one is the destination. */
+ ATF_REQUIRE_MSG(pwrite(fd, p + 9 * ps - 100, 300, 9 * ps + 1000) ==
+ 300, "pwrite failed; errno=%d", errno);
+ memcpy(expected + 9 * ps + 1000, expected + 9 * ps - 100, 300);
+
+ mapped_io_finish(fd, p, expected);
+}
+
+ATF_TC_WITHOUT_HEAD(writev_from_own_private_mapping);
+ATF_TC_BODY(writev_from_own_private_mapping, tc)
+{
+ const size_t pidx[] = { 0, 8 };
+ struct iovec iov[2];
+ char *expected, *p;
+ size_t ps;
+ int fd;
+
+ ps = getpagesize();
+ p = mapped_io_setup(MAP_PRIVATE, pidx, nitems(pidx), &fd, &expected);
+
+ /*
+ * Fault in the first source page, so that the copy from the first
+ * iovec succeeds, and the copy from the second iovec, which is in
+ * the destination page, faults after that progress.
+ */
+ (void)*(volatile char *)p;
+ iov[0].iov_base = p + 100;
+ iov[0].iov_len = 100;
+ iov[1].iov_base = p + 8 * ps + 200;
+ iov[1].iov_len = 200;
+ ATF_REQUIRE_MSG(pwritev(fd, iov, nitems(iov), 8 * ps + 1000) == 300,
+ "pwritev failed; errno=%d", errno);
+ memcpy(expected + 8 * ps + 1000, expected + 100, 100);
+ memcpy(expected + 8 * ps + 1100, expected + 8 * ps + 200, 200);
+
+ mapped_io_finish(fd, p, expected);
+}
+
+ATF_TC_WITHOUT_HEAD(read_into_own_shared_mapping);
+ATF_TC_BODY(read_into_own_shared_mapping, tc)
+{
+ const size_t pidx[] = { 0, 8, 9 };
+ char *expected, *p;
+ size_t ps;
+ int fd;
+
+ ps = getpagesize();
+ p = mapped_io_setup(MAP_SHARED, pidx, nitems(pidx), &fd, &expected);
+
+ /* The destination is in the source page. */
+ ATF_REQUIRE_MSG(pread(fd, p + 1000, 200, 100) == 200,
+ "pread failed; errno=%d", errno);
+ memcpy(expected + 1000, expected + 100, 200);
+
+ /* The destination spans two pages, the second one is the source. */
+ ATF_REQUIRE_MSG(pread(fd, p + 9 * ps - 100, 300, 9 * ps + 1000) ==
+ 300, "pread failed; errno=%d", errno);
+ memcpy(expected + 9 * ps - 100, expected + 9 * ps + 1000, 300);
+
+ mapped_io_finish(fd, p, expected);
+}
+
static int
shm_open_large(int psind, int policy, size_t sz)
{
@@ -2427,6 +2578,9 @@
ATF_TP_ADD_TC(tp, fspacectl);
ATF_TP_ADD_TC(tp, accounting);
ATF_TP_ADD_TC(tp, mmap_prot);
+ ATF_TP_ADD_TC(tp, write_from_own_private_mapping);
+ ATF_TP_ADD_TC(tp, writev_from_own_private_mapping);
+ ATF_TP_ADD_TC(tp, read_into_own_shared_mapping);
ATF_TP_ADD_TC(tp, largepage_basic);
ATF_TP_ADD_TC(tp, largepage_config);
ATF_TP_ADD_TC(tp, largepage_fspacectl);

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 1, 5:11 AM (15 h, 45 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42119651
Default Alt Text
D62170.1796101918.diff (23 KB)

Event Timeline