11eaf0ac3Slogwang /*-
2*d4a07e70Sfengbojiang * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d4a07e70Sfengbojiang *
41eaf0ac3Slogwang * Copyright (c) 2014 Gleb Smirnoff <[email protected]>
51eaf0ac3Slogwang * Copyright (c) 2003-2004 Alan L. Cox <[email protected]>
61eaf0ac3Slogwang * All rights reserved.
71eaf0ac3Slogwang *
81eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without
91eaf0ac3Slogwang * modification, are permitted provided that the following conditions
101eaf0ac3Slogwang * are met:
111eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright
121eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer.
131eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright
141eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the
151eaf0ac3Slogwang * documentation and/or other materials provided with the distribution.
161eaf0ac3Slogwang *
171eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
181eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
211eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271eaf0ac3Slogwang * SUCH DAMAGE.
281eaf0ac3Slogwang *
291eaf0ac3Slogwang * $FreeBSD$
301eaf0ac3Slogwang */
311eaf0ac3Slogwang
321eaf0ac3Slogwang #ifndef _SYS_SF_BUF_H_
331eaf0ac3Slogwang #define _SYS_SF_BUF_H_
341eaf0ac3Slogwang
351eaf0ac3Slogwang struct sfstat { /* sendfile statistics */
361eaf0ac3Slogwang uint64_t sf_syscalls; /* times sendfile was called */
371eaf0ac3Slogwang uint64_t sf_noiocnt; /* times sendfile didn't require I/O */
381eaf0ac3Slogwang uint64_t sf_iocnt; /* times sendfile had to do disk I/O */
391eaf0ac3Slogwang uint64_t sf_pages_read; /* pages read as part of a request */
401eaf0ac3Slogwang uint64_t sf_pages_valid; /* pages were valid for a request */
411eaf0ac3Slogwang uint64_t sf_rhpages_requested; /* readahead pages requested */
421eaf0ac3Slogwang uint64_t sf_rhpages_read; /* readahead pages read */
431eaf0ac3Slogwang uint64_t sf_busy; /* times aborted on a busy page */
441eaf0ac3Slogwang uint64_t sf_allocfail; /* times sfbuf allocation failed */
451eaf0ac3Slogwang uint64_t sf_allocwait; /* times sfbuf allocation had to wait */
46*d4a07e70Sfengbojiang uint64_t sf_pages_bogus; /* times bogus page was used */
471eaf0ac3Slogwang };
481eaf0ac3Slogwang
491eaf0ac3Slogwang #ifdef _KERNEL
501eaf0ac3Slogwang #include <sys/types.h>
511eaf0ac3Slogwang #include <sys/systm.h>
521eaf0ac3Slogwang #include <sys/counter.h>
531eaf0ac3Slogwang #include <vm/vm.h>
541eaf0ac3Slogwang #include <vm/vm_param.h>
551eaf0ac3Slogwang #include <vm/vm_page.h>
561eaf0ac3Slogwang
571eaf0ac3Slogwang /*
581eaf0ac3Slogwang * Sf_bufs, or sendfile(2) buffers provide a vm_page that is mapped
591eaf0ac3Slogwang * into kernel address space. Note, that they aren't used only
601eaf0ac3Slogwang * by sendfile(2)!
611eaf0ac3Slogwang *
621eaf0ac3Slogwang * Sf_bufs could be implemented as a feature of vm_page_t, but that
631eaf0ac3Slogwang * would require growth of the structure. That's why they are implemented
641eaf0ac3Slogwang * as a separate hash indexed by vm_page address. Implementation lives in
651eaf0ac3Slogwang * kern/subr_sfbuf.c. Meanwhile, most 64-bit machines have a physical map,
661eaf0ac3Slogwang * so they don't require this hash at all, thus ignore subr_sfbuf.c.
671eaf0ac3Slogwang *
681eaf0ac3Slogwang * Different 32-bit architectures demand different requirements on sf_buf
691eaf0ac3Slogwang * hash and functions. They request features in machine/vmparam.h, which
701eaf0ac3Slogwang * enable parts of this file. They can also optionally provide helpers in
711eaf0ac3Slogwang * machine/sf_buf.h
721eaf0ac3Slogwang *
731eaf0ac3Slogwang * Defines are:
741eaf0ac3Slogwang * SFBUF This machine requires sf_buf hash.
751eaf0ac3Slogwang * subr_sfbuf.c should be compiled.
761eaf0ac3Slogwang * SFBUF_CPUSET This machine can perform SFB_CPUPRIVATE mappings,
771eaf0ac3Slogwang * that do no invalidate cache on the rest of CPUs.
781eaf0ac3Slogwang * SFBUF_NOMD This machine doesn't have machine/sf_buf.h
791eaf0ac3Slogwang *
801eaf0ac3Slogwang * SFBUF_MAP This machine provides its own sf_buf_map() and
811eaf0ac3Slogwang * sf_buf_unmap().
821eaf0ac3Slogwang * SFBUF_PROCESS_PAGE This machine provides sf_buf_process_page()
831eaf0ac3Slogwang * function.
841eaf0ac3Slogwang */
851eaf0ac3Slogwang
861eaf0ac3Slogwang #ifdef SFBUF
871eaf0ac3Slogwang #if defined(SMP) && defined(SFBUF_CPUSET)
881eaf0ac3Slogwang #include <sys/_cpuset.h>
891eaf0ac3Slogwang #endif
901eaf0ac3Slogwang #include <sys/queue.h>
911eaf0ac3Slogwang
921eaf0ac3Slogwang struct sf_buf {
931eaf0ac3Slogwang LIST_ENTRY(sf_buf) list_entry; /* list of buffers */
941eaf0ac3Slogwang TAILQ_ENTRY(sf_buf) free_entry; /* list of buffers */
951eaf0ac3Slogwang vm_page_t m; /* currently mapped page */
961eaf0ac3Slogwang vm_offset_t kva; /* va of mapping */
971eaf0ac3Slogwang int ref_count; /* usage of this mapping */
981eaf0ac3Slogwang #if defined(SMP) && defined(SFBUF_CPUSET)
991eaf0ac3Slogwang cpuset_t cpumask; /* where mapping is valid */
1001eaf0ac3Slogwang #endif
1011eaf0ac3Slogwang };
1021eaf0ac3Slogwang #else /* ! SFBUF */
1031eaf0ac3Slogwang struct sf_buf;
1041eaf0ac3Slogwang #endif /* SFBUF */
1051eaf0ac3Slogwang
1061eaf0ac3Slogwang #ifndef SFBUF_NOMD
1071eaf0ac3Slogwang #include <machine/sf_buf.h>
1081eaf0ac3Slogwang #endif
1091eaf0ac3Slogwang
1101eaf0ac3Slogwang #ifdef SFBUF
1111eaf0ac3Slogwang struct sf_buf *sf_buf_alloc(struct vm_page *, int);
1121eaf0ac3Slogwang void sf_buf_free(struct sf_buf *);
1131eaf0ac3Slogwang void sf_buf_ref(struct sf_buf *);
1141eaf0ac3Slogwang
1151eaf0ac3Slogwang static inline vm_offset_t
sf_buf_kva(struct sf_buf * sf)1161eaf0ac3Slogwang sf_buf_kva(struct sf_buf *sf)
1171eaf0ac3Slogwang {
118*d4a07e70Sfengbojiang if (PMAP_HAS_DMAP)
119*d4a07e70Sfengbojiang return (PHYS_TO_DMAP(VM_PAGE_TO_PHYS((vm_page_t)sf)));
1201eaf0ac3Slogwang
1211eaf0ac3Slogwang return (sf->kva);
1221eaf0ac3Slogwang }
1231eaf0ac3Slogwang
1241eaf0ac3Slogwang static inline vm_page_t
sf_buf_page(struct sf_buf * sf)1251eaf0ac3Slogwang sf_buf_page(struct sf_buf *sf)
1261eaf0ac3Slogwang {
127*d4a07e70Sfengbojiang if (PMAP_HAS_DMAP)
1281eaf0ac3Slogwang return ((vm_page_t)sf);
1291eaf0ac3Slogwang
1301eaf0ac3Slogwang return (sf->m);
1311eaf0ac3Slogwang }
1321eaf0ac3Slogwang
1331eaf0ac3Slogwang #ifndef SFBUF_MAP
1341eaf0ac3Slogwang #include <vm/pmap.h>
1351eaf0ac3Slogwang
1361eaf0ac3Slogwang static inline void
sf_buf_map(struct sf_buf * sf,int flags)1371eaf0ac3Slogwang sf_buf_map(struct sf_buf *sf, int flags)
1381eaf0ac3Slogwang {
1391eaf0ac3Slogwang
1401eaf0ac3Slogwang pmap_qenter(sf->kva, &sf->m, 1);
1411eaf0ac3Slogwang }
1421eaf0ac3Slogwang
1431eaf0ac3Slogwang static inline int
sf_buf_unmap(struct sf_buf * sf)1441eaf0ac3Slogwang sf_buf_unmap(struct sf_buf *sf)
1451eaf0ac3Slogwang {
1461eaf0ac3Slogwang
1471eaf0ac3Slogwang return (0);
1481eaf0ac3Slogwang }
1491eaf0ac3Slogwang #endif /* SFBUF_MAP */
1501eaf0ac3Slogwang
1511eaf0ac3Slogwang #if defined(SMP) && defined(SFBUF_CPUSET)
1521eaf0ac3Slogwang void sf_buf_shootdown(struct sf_buf *, int);
1531eaf0ac3Slogwang #endif
1541eaf0ac3Slogwang
1551eaf0ac3Slogwang #ifdef SFBUF_PROCESS_PAGE
1561eaf0ac3Slogwang boolean_t sf_buf_process_page(vm_page_t, void (*)(struct sf_buf *));
1571eaf0ac3Slogwang #endif
1581eaf0ac3Slogwang
1591eaf0ac3Slogwang #else /* ! SFBUF */
1601eaf0ac3Slogwang
1611eaf0ac3Slogwang static inline struct sf_buf *
sf_buf_alloc(struct vm_page * m,int pri)1621eaf0ac3Slogwang sf_buf_alloc(struct vm_page *m, int pri)
1631eaf0ac3Slogwang {
1641eaf0ac3Slogwang
1651eaf0ac3Slogwang return ((struct sf_buf *)m);
1661eaf0ac3Slogwang }
1671eaf0ac3Slogwang
1681eaf0ac3Slogwang static inline void
sf_buf_free(struct sf_buf * sf)1691eaf0ac3Slogwang sf_buf_free(struct sf_buf *sf)
1701eaf0ac3Slogwang {
1711eaf0ac3Slogwang }
1721eaf0ac3Slogwang
1731eaf0ac3Slogwang static inline void
sf_buf_ref(struct sf_buf * sf)1741eaf0ac3Slogwang sf_buf_ref(struct sf_buf *sf)
1751eaf0ac3Slogwang {
1761eaf0ac3Slogwang }
1771eaf0ac3Slogwang #endif /* SFBUF */
1781eaf0ac3Slogwang
1791eaf0ac3Slogwang /*
1801eaf0ac3Slogwang * Options to sf_buf_alloc() are specified through its flags argument. This
1811eaf0ac3Slogwang * argument's value should be the result of a bitwise or'ing of one or more
1821eaf0ac3Slogwang * of the following values.
1831eaf0ac3Slogwang */
1841eaf0ac3Slogwang #define SFB_CATCH 1 /* Check signals if the allocation
1851eaf0ac3Slogwang sleeps. */
1861eaf0ac3Slogwang #define SFB_CPUPRIVATE 2 /* Create a CPU private mapping. */
1871eaf0ac3Slogwang #define SFB_DEFAULT 0
1881eaf0ac3Slogwang #define SFB_NOWAIT 4 /* Return NULL if all bufs are used. */
1891eaf0ac3Slogwang
1901eaf0ac3Slogwang extern counter_u64_t sfstat[sizeof(struct sfstat) / sizeof(uint64_t)];
1911eaf0ac3Slogwang #define SFSTAT_ADD(name, val) \
1921eaf0ac3Slogwang counter_u64_add(sfstat[offsetof(struct sfstat, name) / sizeof(uint64_t)],\
1931eaf0ac3Slogwang (val))
1941eaf0ac3Slogwang #define SFSTAT_INC(name) SFSTAT_ADD(name, 1)
1951eaf0ac3Slogwang #endif /* _KERNEL */
1961eaf0ac3Slogwang #endif /* !_SYS_SF_BUF_H_ */
197