33#include "opt_kern_tls.h"
37#include <sys/capsicum.h>
38#include <sys/kernel.h>
42#include <sys/malloc.h>
47#include <sys/protosw.h>
48#include <sys/rwlock.h>
49#include <sys/sf_buf.h>
50#include <sys/socket.h>
51#include <sys/socketvar.h>
52#include <sys/syscallsubr.h>
53#include <sys/sysctl.h>
54#include <sys/sysproto.h>
58#include <netinet/in.h>
59#include <netinet/tcp.h>
61#include <security/audit/audit.h>
62#include <security/mac/mac_framework.h>
65#include <vm/vm_object.h>
66#include <vm/vm_pager.h>
70#define EXT_FLAG_SYNC EXT_FLAG_VENDOR1
71#define EXT_FLAG_NOCACHE EXT_FLAG_VENDOR2
72#define EXT_FLAG_CACHE_LAST EXT_FLAG_VENDOR3
97 struct ktls_session *tls;
115 KASSERT(sfs->
count == 0, (
"sendfile sync %p still busy", sfs));
118 mtx_destroy(&sfs->
mtx);
119 free(sfs, M_SENDFILE);
126 KASSERT(sfs->
count > 0, (
"sendfile sync %p not busy", sfs));
127 if (--sfs->
count == 0) {
136 mtx_unlock(&sfs->
mtx);
145 COUNTER_ARRAY_ALLOC(
sfstat,
sizeof(
struct sfstat) /
sizeof(uint64_t),
155 COUNTER_ARRAY_COPY(
sfstat, &s,
sizeof(s) /
sizeof(uint64_t));
157 COUNTER_ARRAY_ZERO(
sfstat,
sizeof(s) /
sizeof(uint64_t));
158 return (SYSCTL_OUT(req, &s,
sizeof(s)));
161 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
163 "sendfile statistics");
172 KASSERT(m->m_flags & M_EXT && m->m_ext.ext_type == EXT_SFBUF,
173 (
"%s: m %p !M_EXT or !EXT_SFBUF", __func__, m));
175 sf = m->m_ext.ext_arg1;
176 pg = sf_buf_page(sf);
180 vm_page_release(pg,
flags);
200 for (i = 0; i < m->m_epg_npgs; i++) {
201 if (cache_last && i == m->m_epg_npgs - 1)
203 pg = PHYS_TO_VM_PAGE(m->m_epg_pa[i]);
204 vm_page_release(pg,
flags);
218xfsize(
int i,
int n, off_t off, off_t len)
222 return (omin(PAGE_SIZE - (off & PAGE_MASK), len));
224 if (i == n - 1 && ((off + len) & PAGE_MASK) > 0)
225 return ((off + len) & PAGE_MASK);
233static inline vm_ooffset_t
238 return ((vm_ooffset_t)off);
240 return (trunc_page(off + i * PAGE_SIZE));
252 KASSERT(old >
new, (
"%s: old %d new %d", __func__, old,
new));
255 *space -=
xfsize(old - 1, old, off, *space);
264 *space -=
xfsize(0, old, off, *space);
269 *space -= (old -
new) * PAGE_SIZE;
271 KASSERT(*space >= 0, (
"%s: space went backwards", __func__));
281 while (atomic_load_int(&sfio->
nios) != 1)
291 struct sf_io *sfio = arg;
308 MPASS(
count == 0 || pa[0] != bogus_page);
309 for (i = 0; i <
count; i++) {
310 if (pa[i] == bogus_page) {
311 sfio->
pa[(pa[0]->pindex - sfio->
pindex0) + i] =
312 pa[i] = vm_page_relookup(sfio->
obj,
314 KASSERT(pa[i] != NULL,
315 (
"%s: page %p[%d] disappeared",
318 vm_page_xunbusy_unchecked(pa[i]);
322 if (!refcount_release(&sfio->
nios))
326 for (i = 1; i < sfio->
npages; i++) {
327 if (sfio->
pa[i] == NULL)
329 KASSERT(vm_page_wired(sfio->
pa[i]),
330 (
"sfio %p page %d %p not wired", sfio, i, sfio->
pa[i]));
333 KASSERT(sfio->
pa[0]->object == sfio->
pa[i]->object,
334 (
"sfio %p page %d %p wrong owner %p %p", sfio, i,
335 sfio->
pa[i], sfio->
pa[0]->object, sfio->
pa[i]->object));
336 KASSERT(sfio->
pa[0]->pindex + i == sfio->
pa[i]->pindex,
337 (
"sfio %p page %d %p wrong index %jx %jx", sfio, i,
338 sfio->
pa[i], (uintmax_t)sfio->
pa[0]->pindex,
339 (uintmax_t)sfio->
pa[i]->pindex));
343 vm_object_pip_wakeup(sfio->
obj);
345 if (sfio->
m == NULL) {
353 MPASS((curthread->td_pflags & TDP_KTHREAD) == 0);
354 free(sfio, M_SENDFILE);
358#if defined(KERN_TLS) && defined(INVARIANTS)
359 if ((sfio->
m->m_flags & M_EXTPG) != 0)
360 KASSERT(sfio->tls == sfio->
m->m_epg_tls,
361 (
"TLS session mismatch"));
363 KASSERT(sfio->tls == NULL,
364 (
"non-ext_pgs mbuf with TLS session"));
367 CURVNET_SET(so->so_vnet);
368 if (__predict_false(sfio->
error)) {
380 so->so_proto->pr_usrreqs->pru_abort(so);
385 }
else if (sfio->tls != NULL && sfio->tls->mode == TCP_TLS_MODE_SW) {
399 (
void)(so->so_proto->pr_usrreqs->pru_ready)(so, sfio->
m,
407 free(sfio, M_SENDFILE);
415 off_t len,
int rhpages,
int flags)
418 int a,
count, count1, grabbed, i, j, npages, rv;
423 flags = (
flags & SF_NODISKIO) ? VM_ALLOC_NOWAIT : 0;
424 sfio->
pindex0 = OFF_TO_IDX(off);
430 grabbed = vm_page_grab_pages_unlocked(obj, OFF_TO_IDX(off),
431 VM_ALLOC_NORMAL | VM_ALLOC_WIRED |
flags, pa, npages);
432 if (grabbed < npages) {
433 for (
int i = grabbed; i < npages; i++)
439 for (i = 0; i < npages;) {
441 if (vm_page_is_valid(pa[i],
vmoff(i, off) & PAGE_MASK,
442 xfsize(i, npages, off, len))) {
443 vm_page_xunbusy(pa[i]);
444 SFSTAT_INC(sf_pages_valid);
459 VM_OBJECT_RLOCK(obj);
460 if (!vm_pager_has_page(obj, OFF_TO_IDX(
vmoff(i, off)), NULL,
462 VM_OBJECT_RUNLOCK(obj);
463 pmap_zero_page(pa[i]);
464 vm_page_valid(pa[i]);
465 MPASS(pa[i]->dirty == 0);
466 vm_page_xunbusy(pa[i]);
470 VM_OBJECT_RUNLOCK(obj);
476 count = min(a + 1, npages - i);
495 for (j = i +
count - 1; j > i; j--) {
496 if (vm_page_is_valid(pa[j],
vmoff(j, off) & PAGE_MASK,
497 xfsize(j, npages, off, len))) {
498 vm_page_xunbusy(pa[j]);
499 SFSTAT_INC(sf_pages_valid);
512 MPASS(pa[i +
count - 1]->valid != VM_PAGE_BITS_ALL);
513 for (j = i + 1; j < i +
count - 1; j++) {
514 if (vm_page_is_valid(pa[j],
vmoff(j, off) & PAGE_MASK,
515 xfsize(j, npages, off, len))) {
516 vm_page_xunbusy(pa[j]);
517 SFSTAT_INC(sf_pages_valid);
518 SFSTAT_INC(sf_pages_bogus);
523 refcount_acquire(&sfio->
nios);
524 rv = vm_pager_get_pages_async(obj, pa + i,
count, NULL,
525 i +
count == npages ? &rhpages : NULL,
527 if (__predict_false(rv != VM_PAGER_OK)) {
535 for (j = 0; j < npages; j++) {
537 vm_page_xunbusy(pa[j]);
538 KASSERT(pa[j] != NULL && pa[j] != bogus_page,
539 (
"%s: page %p[%d] I/O recovery failure",
541 vm_page_unwire(pa[j], PQ_INACTIVE);
547 SFSTAT_INC(sf_iocnt);
548 SFSTAT_ADD(sf_pages_read,
count);
549 if (i +
count == npages)
550 SFSTAT_ADD(sf_rhpages_read, rhpages);
556 if (*nios == 0 && npages != 0)
557 SFSTAT_INC(sf_noiocnt);
564 struct vnode **vp_res,
struct shmfd **shmfd_res, off_t *obj_size,
576 shmfd = *shmfd_res = NULL;
583 if (fp->f_type == DTYPE_VNODE) {
585 vn_lock(vp, LK_SHARED | LK_RETRY);
586 if (vp->v_type != VREG) {
590 *bsize = vp->v_mount->mnt_stat.f_iosize;
602 if (obj->type == OBJT_VNODE) {
603 VM_OBJECT_RLOCK(obj);
604 *obj_size = obj->un_pager.vnp.vnp_size;
606 error = VOP_GETATTR(vp, &va, td->td_ucred);
609 *obj_size = va.va_size;
610 VM_OBJECT_RLOCK(obj);
612 }
else if (fp->f_type == DTYPE_SHM) {
614 obj = shmfd->shm_object;
615 VM_OBJECT_RLOCK(obj);
616 *obj_size = shmfd->shm_size;
622 if ((obj->flags & OBJ_DEAD) != 0) {
623 VM_OBJECT_RUNLOCK(obj);
633 vm_object_reference_locked(obj);
634 VM_OBJECT_RUNLOCK(obj);
658 sock_fp, NULL, NULL);
661 *so = (*sock_fp)->f_data;
662 if ((*so)->so_type != SOCK_STREAM)
668 if ((*so)->so_proto->pr_protocol == IPPROTO_SCTP)
675 struct uio *trl_uio, off_t offset,
size_t nbytes, off_t *sent,
int flags,
678 struct file *sock_fp;
680 struct vm_object *obj;
684 struct ktls_session *tls;
686 struct mbuf *m, *mh, *mhtail;
691 off_t off, sbytes, rem, obj_size, nobj_size;
692 int bsize, error, ext_pgs_idx, hdrlen, max_pgs, softerr;
718 error = mac_socket_check_send(td->td_ucred, so);
723 SFSTAT_INC(sf_syscalls);
724 SFSTAT_ADD(sf_rhpages_requested, SF_READAHEAD(
flags));
726 if (
flags & SF_SYNC) {
727 sfs =
malloc(
sizeof(*sfs), M_SENDFILE, M_WAITOK | M_ZERO);
728 mtx_init(&sfs->
mtx,
"sendfile", NULL, MTX_DEF);
733 rem =
nbytes ? omin(
nbytes, obj_size - offset) : obj_size - offset;
741 error = SOCK_IO_SEND_LOCK(so, SBL_WAIT | SBL_NOINTR);
745 tls = ktls_hold(so->so_snd.sb_tls_info);
759 for (off = offset; rem > 0; ) {
762 struct mbuf *m0, *mtail;
763 int nios, space, npages, rhpages;
782 SOCKBUF_LOCK(&so->so_snd);
783 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
784 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
786 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
788 SOCKBUF_UNLOCK(&so->so_snd);
790 }
else if (so->so_error) {
791 error = so->so_error;
793 SOCKBUF_UNLOCK(&so->so_snd);
796 if ((so->so_state & SS_ISCONNECTED) == 0) {
797 SOCKBUF_UNLOCK(&so->so_snd);
802 space = sbspace(&so->so_snd);
805 space < so->so_snd.sb_lowat)) {
806 if (so->so_state & SS_NBIO) {
807 SOCKBUF_UNLOCK(&so->so_snd);
817 error =
sbwait(&so->so_snd);
824 SOCKBUF_UNLOCK(&so->so_snd);
829 SOCKBUF_UNLOCK(&so->so_snd);
837 if (hdr_uio != NULL && hdr_uio->uio_resid > 0) {
838 hdr_uio->uio_td = td;
839 hdr_uio->uio_rw = UIO_WRITE;
843 tls->params.max_frame_len, M_EXTPG);
863 error = vn_lock(vp, LK_SHARED);
870 if (obj->type == OBJT_VNODE) {
871 VM_OBJECT_RLOCK(obj);
872 nobj_size = obj->un_pager.vnp.vnp_size;
873 VM_OBJECT_RUNLOCK(obj);
875 error = VOP_GETATTR(vp, &va, td->td_ucred);
880 nobj_size = va.va_size;
882 if (off >= nobj_size) {
886 if (nobj_size != obj_size) {
887 obj_size = nobj_size;
896 else if (space > PAGE_SIZE) {
902 space -= (PAGE_SIZE - (off & PAGE_MASK));
903 space = trunc_page(space);
905 space += (PAGE_SIZE - (off & PAGE_MASK));
908 npages = howmany(space + (off & PAGE_MASK), PAGE_SIZE);
919 if (
flags & SF_USER_READAHEAD) {
920 rhpages = SF_READAHEAD(
flags);
922 rhpages = howmany(rem + (off & PAGE_MASK), PAGE_SIZE) -
924 rhpages += SF_READAHEAD(
flags);
926 rhpages = min(howmany(
maxphys, PAGE_SIZE), rhpages);
927 rhpages = min(howmany(obj_size - trunc_page(off), PAGE_SIZE) -
931 npages *
sizeof(vm_page_t), M_SENDFILE, M_WAITOK);
932 refcount_init(&sfio->
nios, 1);
945 vm_object_pip_add(obj, 1);
970 so->so_proto->pr_protocol == IPPROTO_TCP)
978 max_pgs = num_pages(tls->params.max_frame_len);
981 max_pgs = MBUF_PEXT_MAX_PGS;
984 ext_pgs_idx = max_pgs - 1;
987 for (
int i = 0; i < npages; i++) {
1000 if (pga == bogus_page)
1001 pga = vm_page_relookup(obj, sfio->
pindex0 + i);
1007 if (ext_pgs_idx == max_pgs) {
1011 if (
flags & SF_NOCACHE) {
1012 m0->m_ext.ext_flags |=
1020 if ((npages - i <= max_pgs) &&
1021 ((off + space) & PAGE_MASK) &&
1022 (rem > space || rhpages > 0))
1023 m0->m_ext.ext_flags |=
1027 m0->m_ext.ext_flags |=
1029 m0->m_ext.ext_arg1 = sfs;
1030 mtx_lock(&sfs->
mtx);
1032 mtx_unlock(&sfs->
mtx);
1043 vmoff(i, off) & PAGE_MASK;
1046 mtail->m_flags |= M_NOTREADY;
1050 m0->m_epg_pa[ext_pgs_idx] = VM_PAGE_TO_PHYS(pga);
1052 xfs =
xfsize(i, npages, off, space);
1053 m0->m_epg_last_len = xfs;
1054 MBUF_EXT_PGS_ASSERT_SANITY(m0);
1055 mtail->m_len += xfs;
1056 mtail->m_ext.ext_size += PAGE_SIZE;
1070 m != NULL ? SFB_NOWAIT : SFB_CATCH);
1072 SFSTAT_INC(sf_allocfail);
1074 for (
int j = i; j < npages; j++) {
1075 vm_page_unwire(pa[j], PQ_INACTIVE);
1085 m0 = m_get(M_WAITOK, MT_DATA);
1086 m0->m_ext.ext_buf = (
char *)sf_buf_kva(sf);
1087 m0->m_ext.ext_size = PAGE_SIZE;
1088 m0->m_ext.ext_arg1 = sf;
1089 m0->m_ext.ext_type = EXT_SFBUF;
1090 m0->m_ext.ext_flags = EXT_FLAG_EMBREF;
1099 if ((
flags & SF_NOCACHE) &&
1101 !((off + space) & PAGE_MASK) ||
1102 !(rem > space || rhpages > 0)))
1106 m0->m_ext.ext_arg2 = sfs;
1107 mtx_lock(&sfs->
mtx);
1109 mtx_unlock(&sfs->
mtx);
1111 m0->m_ext.ext_count = 1;
1112 m0->m_flags |= (M_EXT | M_RDONLY);
1114 m0->m_flags |= M_NOTREADY;
1115 m0->m_data = (
char *)sf_buf_kva(sf) +
1116 (
vmoff(i, off) & PAGE_MASK);
1117 m0->m_len =
xfsize(i, npages, off, space);
1140 m0 = mhtail->m_next = m;
1147 KASSERT(softerr, (
"%s: m NULL, no error", __func__));
1154 KASSERT(
m_length(m, NULL) == space + hdrlen,
1155 (
"%s: mlen %u space %d hdrlen %d",
1156 __func__,
m_length(m, NULL), space, hdrlen));
1158 CURVNET_SET(so->so_vnet);
1161 ktls_frame(m, tls, &tls_enq_cnt, TLS_RLTYPE_APP);
1174 if (tls != NULL && tls->mode == TCP_TLS_MODE_SW) {
1175 error = (*so->so_proto->pr_usrreqs->pru_send)
1176 (so, PRUS_NOTREADY, m, NULL, NULL, td);
1185 error = (*so->so_proto->pr_usrreqs->pru_send)
1186 (so, 0, m, NULL, NULL, td);
1191 error = (*so->so_proto->pr_usrreqs->pru_send)
1192 (so, PRUS_NOTREADY, m, NULL, NULL, td);
1200 sbytes += space + hdrlen;
1212 if (trl_uio != NULL) {
1213 SOCK_IO_SEND_UNLOCK(so);
1216 sbytes += td->td_retval[0];
1221 SOCK_IO_SEND_UNLOCK(so);
1228 td->td_retval[0] = 0;
1234 vm_object_deallocate(obj);
1243 mtx_lock(&sfs->
mtx);
1244 if (sfs->
count != 0)
1245 error = cv_wait_sig(&sfs->
cv, &sfs->
mtx);
1246 if (sfs->
count == 0) {
1250 mtx_unlock(&sfs->
mtx);
1258 if (error == ERESTART)
1267 struct sf_hdtr hdtr;
1268 struct uio *hdr_uio, *trl_uio;
1277 if (uap->offset < 0)
1281 hdr_uio = trl_uio = NULL;
1283 if (uap->hdtr != NULL) {
1284 error = copyin(uap->hdtr, &hdtr,
sizeof(hdtr));
1287 if (hdtr.headers != NULL) {
1288 error =
copyinuio(hdtr.headers, hdtr.hdr_cnt,
1292#ifdef COMPAT_FREEBSD4
1299 if (uap->nbytes > hdr_uio->uio_resid)
1300 uap->nbytes -= hdr_uio->uio_resid;
1306 if (hdtr.trailers != NULL) {
1307 error =
copyinuio(hdtr.trailers, hdtr.trl_cnt,
1314 AUDIT_ARG_FD(uap->fd);
1323 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, uap->offset,
1324 uap->nbytes, &sbytes, uap->flags, td);
1327 if (uap->sbytes != NULL)
1328 copyout(&sbytes, uap->sbytes,
sizeof(off_t));
1331 free(hdr_uio, M_IOV);
1332 free(trl_uio, M_IOV);
1354#ifdef COMPAT_FREEBSD4
1356freebsd4_sendfile(
struct thread *td,
struct freebsd4_sendfile_args *uap)
1358 struct sendfile_args args;
1362 args.offset = uap->offset;
1363 args.nbytes = uap->nbytes;
1364 args.hdtr = uap->hdtr;
1365 args.sbytes = uap->sbytes;
1366 args.flags = uap->flags;
void cv_init(struct cv *cvp, const char *desc)
void cv_destroy(struct cv *cvp)
void cv_signal(struct cv *cvp)
int fget_read(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
void *() malloc(size_t size, struct malloc_type *mtp, int flags)
void free(void *addr, struct malloc_type *mtp)
void m_freem(struct mbuf *mb)
struct mbuf * mb_alloc_ext_pgs(int how, m_ext_free_t ext_free)
void mb_free_notready(struct mbuf *m, int count)
SYSCTL_PROC(_kern_ipc, OID_AUTO, sfstat, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_MPSAFE, NULL, 0, sfstat_sysctl, "I", "sendfile statistics")
static off_t xfsize(int i, int n, off_t off, off_t len)
static void sendfile_free_mext_pg(struct mbuf *m)
counter_u64_t sfstat[sizeof(struct sfstat)/sizeof(uint64_t)]
static vm_ooffset_t vmoff(int i, off_t off)
static void sendfile_iowait(struct sf_io *sfio, const char *wmesg)
static int sendfile(struct thread *td, struct sendfile_args *uap, int compat)
static int sendfile_swapin(vm_object_t obj, struct sf_io *sfio, int *nios, off_t off, off_t len, int rhpages, int flags)
SYSINIT(sfstat, SI_SUB_MBUF, SI_ORDER_FIRST, sfstat_init, NULL)
static int sendfile_getsock(struct thread *td, int s, struct file **sock_fp, struct socket **so)
static int sfstat_sysctl(SYSCTL_HANDLER_ARGS)
static void sendfile_sync_signal(struct sendfile_sync *sfs)
static MALLOC_DEFINE(M_SENDFILE, "sendfile", "sendfile dynamic memory")
int sys_sendfile(struct thread *td, struct sendfile_args *uap)
static void fixspace(int old, int new, off_t off, int *space)
#define EXT_FLAG_CACHE_LAST
static void sendfile_iodone(void *arg, vm_page_t *pa, int count, int error)
static void sfstat_init(const void *unused)
static void sendfile_sync_destroy(struct sendfile_sync *sfs)
static void sendfile_free_mext(struct mbuf *m)
int vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio, struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags, struct thread *td)
static int sendfile_getobj(struct thread *td, struct file *fp, vm_object_t *obj_res, struct vnode **vp_res, struct shmfd **shmfd_res, off_t *obj_size, int *bsize)
__read_mostly cap_rights_t cap_pread_rights
__read_mostly cap_rights_t cap_send_rights
struct sf_buf * sf_buf_alloc(struct vm_page *m, int flags)
void sf_buf_free(struct sf_buf *sf)
int copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
int kern_writev(struct thread *td, int fd, struct uio *auio)
void ktls_enqueue(struct mbuf *m, struct socket *so, int page_count)
void ktls_frame(struct mbuf *top, struct ktls_session *tls, int *enq_cnt, uint8_t record_type)
u_int m_length(struct mbuf *m0, struct mbuf **last)
struct mbuf * m_uiotombuf(struct uio *uio, int how, int len, int align, int flags)
int sbwait(struct sockbuf *sb)
int getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp, u_int *fflagp, struct filecaps *havecapsp)