History log of /freebsd-head/sys/sys/malloc.h
Revision Date Author Comments
e34fac850c2be28222aba49143f0bd746e90611c 24-Sep-2020 imp <imp@FreeBSD.org> Create a standalone version of sys/malloc.h

The ZSTD support for the boot loader will need to include files that
use the kernel's malloc interface. Create a standalone stub version
that's functional enough to allow this to work. There's some
limitations in this interface, and it's not quite a perfect
match. Specifically, M_WAITOK allocations can fail because there's
nothing that can be done we no memory is available.
1ba6953720e56ecab6fb4047b9b0f827203bbadd 29-Aug-2020 wulf <wulf@FreeBSD.org> LinuxKPI: Implement ksize() function.

In Linux, ksize() gets the actual amount of memory allocated for a given
object. This commit adds malloc_usable_size() to FreeBSD KPI which does
the same. It also maps LinuxKPI ksize() to newly created function.

ksize() function is used by drm-kmod.

Reviewed by: hselasky, kib
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D26215
2215e2cd8f9121f1b63d67dfc649d766c3c06405 04-Aug-2020 markj <markj@FreeBSD.org> Remove free_domain() and uma_zfree_domain().

These functions were introduced before UMA started ensuring that freed
memory gets placed in domain-local caches. They no longer serve any
purpose since UMA now provides their functionality by default. Remove
them to simplyify the kernel memory allocator interfaces a bit.

Reviewed by: cem, kib
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D25937
7bee496aa0053ca57526774b6da7e064952ea449 16-Feb-2020 mmacy <mmacy@FreeBSD.org> Add zfree to zero allocation before free

Key and cookie management typically wants to
avoid information leaks by explicitly zeroing
before free. This routine simplifies that by
permitting consumers to do so without carrying
the size around.

Reviewed by: jeff@, jhb@
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC (Netgate)
Differential Revision: https://reviews.freebsd.org/D22790
81515d0cb6081ddd370ecb8efd7d9f2a4d2f5e57 28-May-2019 andrew <andrew@FreeBSD.org> The alignment is passed into contigmalloc_domainset in the 7th argument.

KUBSAN was complaining the pointer contigmalloc_domainset returned was
misaligned. Fix this by using the correct argument to find the alignment
in the function signature.

Reported by: KUBSAN
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
7d39a491bfd3fb599c6391bdcae4d82fb494f0d4 18-May-2019 markj <markj@FreeBSD.org> Implement the M_NEXTFIT allocation strategy for vmem(9).

This is described in the vmem paper: "directs vmem to use the next free
segment after the one previously allocated." The implementation adds a
new boundary tag type, M_CURSOR, which is linked into the segment list
and precedes the segment following the previous M_NEXTFIT allocation.
The cursor is used to locate the next free segment satisfying the
allocation constraints.

This implementation isn't O(1) since busy tags aren't coalesced, and we
may potentially scan the entire segment list during an M_NEXTFIT
allocation.

Reviewed by: alc
MFC after: 1 month
Differential Revision: https://reviews.freebsd.org/D17226
f931b753dd4c23f434c34bdfe082b7f31a3cd085 30-Oct-2018 markj <markj@FreeBSD.org> Add malloc_domainset(9) and _domainset variants to other allocator KPIs.

Remove malloc_domain(9) and most other _domain KPIs added in r327900.
The new functions allow the caller to specify a general NUMA domain
selection policy, rather than specifically requesting an allocation from
a specific domain. The latter policy tends to interact poorly with
M_WAITOK, resulting in situations where a caller is blocked indefinitely
because the specified domain is depleted. Most existing consumers of
the _domain KPIs are converted to instead use a DOMAINSET_PREF() policy,
in which we fall back to other domains to satisfy the allocation
request.

This change also defines a set of DOMAINSET_FIXED() policies, which
only permit allocations from the specified domain.

Discussed with: gallatin, jeff
Reported and tested by: pho (previous version)
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D17418
e3c2d3a906b1063421584e83d3d3968849b04690 23-Sep-2018 mjg <mjg@FreeBSD.org> Eliminate false sharing in malloc due to statistic collection

Currently stats are collected in a MAXCPU-sized array which is not
aligned and suffers enormous false-sharing. Fix the problem by
utilizing per-cpu allocation.

The counter(9) API is not used here as it is too incomplete and does
not provide a win over per-cpu zone sized for malloc stats struct. In
particular stats are being reported for each cpu separately by just
copying what is supposed to be an array element for given cpu.

This eliminates significant false-sharing during malloc-heavy tests
e.g. on Skylake. See the review for details.

Reviewed by: markj
Approved by: re (kib)
Differential Revision: https://reviews.freebsd.org/D17289
482f290c9a630d12ff83e136dd350122df99b34a 23-Aug-2018 markj <markj@FreeBSD.org> Remove a redundant #ifdef _KERNEL.

Submitted by: Sebastian Huber <sebastian.huber@embedded-brains.de>
MFC after: 3 days
c7b15ce78190cb29f4b4349cc1bcb63b037f9d5b 02-Jul-2018 mmacy <mmacy@FreeBSD.org> inpcb: don't gratuitously defer frees

Don't defer frees in sysctl handlers. It isn't necessary
and it just confuses things.
revert: r333911, r334104, and r334125

Requested by: jtl
8222f5cb7cdf1ad98c5f281437e35ec2d5da0b96 13-Jun-2018 jtl <jtl@FreeBSD.org> Make UMA and malloc(9) return non-executable memory in most cases.

Most kernel memory that is allocated after boot does not need to be
executable. There are a few exceptions. For example, kernel modules
do need executable memory, but they don't use UMA or malloc(9). The
BPF JIT compiler also needs executable memory and did use malloc(9)
until r317072.

(Note that a side effect of r316767 was that the "small allocation"
path in UMA on amd64 already returned non-executable memory. This
meant that some calls to malloc(9) or the UMA zone(9) allocator could
return executable memory, while others could return non-executable
memory. This change makes the behavior consistent.)

This change makes malloc(9) return non-executable memory unless the new
M_EXEC flag is specified. After this change, the UMA zone(9) allocator
will always return non-executable memory, and a KASSERT will catch
attempts to use the M_EXEC flag to allocate executable memory using
uma_zalloc() or its variants.

Allocations that do need executable memory have various choices. They
may use the M_EXEC flag to malloc(9), or they may use a different VM
interfact to obtain executable pages.

Now that malloc(9) again allows executable allocations, this change also
reverts most of r317072.

PR: 228927
Reviewed by: alc, kib, markj, jhb (previous version)
Sponsored by: Netflix
Differential Revision: https://reviews.freebsd.org/D15691
2defad5711231308ab742434b206d2897307669c 06-Jun-2018 mjg <mjg@FreeBSD.org> malloc: elaborate on r334545 due to frequent questions

While here annotate the NULL check as probably true.
515692db3e212a3c4dffb544fbdaa1ae7528c88e 05-Jun-2018 vangyzen <vangyzen@FreeBSD.org> Make Coverity more happy with r334545

Coverity complains about:

if (((flags) & M_WAITOK) || _malloc_item != NULL)

saying:

The expression
1 /* (2 | 0x100) & 2 */ || _malloc_item != NULL
is suspicious because it performs a Boolean operation
on a constant other than 0 or 1.

Although the code is correct, add "!= 0" to make it slightly
more legible and to silence hundreds(?) of Coverity warnings.

Reported by: Coverity
Discussed with: mjg
Sponsored by: Dell EMC
fe4195ffb477fb81f38e6bc969cbd3f5d0e09c7c 02-Jun-2018 mjg <mjg@FreeBSD.org> malloc: try to use builtins for zeroing at the callsite

Plenty of allocation sites pass M_ZERO and sizes which are small and known
at compilation time. Handling them internally in malloc loses this information
and results in avoidable calls to memset.

Instead, let the compiler take the advantage of it whenever possible.

Discussed with: jeff
4435b85535e276bc678bc1ddbd8e3834c1cbfa76 24-May-2018 mmacy <mmacy@FreeBSD.org> malloc: Add flag for conditionally zeroing for INVARIANTS builds
c060d198e33c0a7d63588e70d353ab146aab3f81 25-Jan-2018 cem <cem@FreeBSD.org> style: Remove remaining deprecated MALLOC/FREE macros

Mechanically replace uses of MALLOC/FREE with appropriate invocations of
malloc(9) / free(9) (a series of sed expressions). Something like:

* MALLOC(a, b, ... -> a = malloc(...
* FREE( -> free(
* free((caddr_t) -> free(

No functional change.

For now, punt on modifying contrib ipfilter code, leaving a definition of
the macro in its KMALLOC().

Reported by: jhb
Reviewed by: cy, imp, markj, rmacklem
Sponsored by: Dell EMC Isilon
Differential Revision: https://reviews.freebsd.org/D14035
2b1bc6707d6761a1083814f8c8d33f6f6f578b72 24-Jan-2018 cem <cem@FreeBSD.org> malloc(9): Change nominal size to size_t to match standard C

No functional change -- size_t matches unsigned long on all platforms.

Reported by: bde
Discussed with: jhb
Sponsored by: Dell EMC Isilon
35335c1009e63e79b2a20cd64a4a142aba047ca6 22-Jan-2018 pfg <pfg@FreeBSD.org> Use the __alloc_size2 attribute where relevant.

This follows the documented use in GCC. It is basically only relevant for
calloc(3), reallocarray(3) and mallocarray(9).

Suggested by: Mark Millard

Reference:
https://docs.freebsd.org/cgi/mid.cgi?9DE674C6-EAA3-4E8A-906F-446E74D82FC4
f375b4dd66bc709573ad4286b3a5ac6c705ba4a8 12-Jan-2018 jeff <jeff@FreeBSD.org> Implement NUMA support in uma(9) and malloc(9). Allocations from specific
domains can be done by the _domain() API variants. UMA also supports a
first-touch policy via the NUMA zone flag.

The slab layer is now segregated by VM domains and is precise. It handles
iteration for round-robin directly. The per-cpu cache layer remains
a mix of domains according to where memory is allocated and freed. Well
behaved clients can achieve perfect locality with no performance penalty.

The direct domain allocation functions have to visit the slab layer and
so require per-zone locks which come at some expense.

Reviewed by: Attilio (a slightly older version)
Tested by: pho
Sponsored by: Netflix, Dell/EMC Isilon
2c9ae2323b3f88e7e5573b2ead5a93f30fdecf8e 10-Jan-2018 cem <cem@FreeBSD.org> mallocarray(9): panic if the requested allocation would overflow

Additionally, move the overflow check logic out to WOULD_OVERFLOW() for
consumers to have a common means of testing for overflowing allocations.
WOULD_OVERFLOW() should be a secondary check -- on 64-bit platforms, just
because an allocation won't overflow size_t does not mean it is a sane size
to request. Callers should be imposing reasonable allocation limits far,
far, below overflow.

Discussed with: emaste, jhb, kp
Sponsored by: Dell EMC Isilon
7c399eacbad7b7b07d02c441c0ac678d9e25255a 08-Jan-2018 pfg <pfg@FreeBSD.org> Revert r327697:
malloc(9): drop the __result_use_check attribute for the kernel allocator.

My bad: __result_use_check just checks the for the general and we always
want to make sure allocated memory is used, not only checked for nullness.

Add it to reallocf since that was missing.
04376fff7c1c5020df90ff91f9efeffbef689a58 08-Jan-2018 pfg <pfg@FreeBSD.org> malloc(9): drop the __result_use_check attribute for the kernel allocator.

The __result_use_check attribute was brought to the kernel malloc in
r281203 for consistency with the userland malloc.

For the case of the M_WAITOK flag, the kernel malloc(), realloc(), and
reallocf() cannot return NULL so in that case the __result_use_check
attribute makes no sense.

We don't have any way of conditionalizing such attributes so just drop it.

MFC after: 3 days
69291826acf685ab944ab2e72da506480728cde8 07-Jan-2018 kp <kp@FreeBSD.org> Introduce mallocarray() in the kernel

Similar to calloc() the mallocarray() function checks for integer
overflows before allocating memory.
It does not zero memory, unless the M_ZERO flag is set.

Reviewed by: pfg, vangyzen (previous version), imp (previous version)
Obtained from: OpenBSD
Differential Revision: https://reviews.freebsd.org/D13766
4736ccfd9c3411d50371d7f21f9450a47c19047e 20-Nov-2017 pfg <pfg@FreeBSD.org> sys: further adoption of SPDX licensing ID tags.

Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
7e6cabd06e6caa6a02eeb86308dc0cb3f27e10da 28-Feb-2017 imp <imp@FreeBSD.org> Renumber copyright clause 4

Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by: Jan Schaumann <jschauma@stevens.edu>
Pull Request: https://github.com/freebsd/freebsd/pull/96
00d578928eca75be320b36d37543a7e2a4f9fbdb 27-May-2016 grehan <grehan@FreeBSD.org> Create branch for bhyve graphics import.
a09fd6097b5d99712404c298ff992620c28d9768 20-May-2016 markj <markj@FreeBSD.org> Move IPv6 malloc tag definitions into the IPv6 code.
db5663c1319f9643adb1c81b78267d40d6e6ca06 15-May-2015 pfg <pfg@FreeBSD.org> Make use of GCC alloc_align attribute

This lets the compiler know about the alignment of pointers returned
by aligned_alloc(3), posix_memalign(3). and contigmalloc(9)

Currently this is only supported in recent gcc but we are ready to
use it if clang implements it.

Relnotes: yes
ae235f94bd8e004d0501d67e87e1486b2df2c971 07-Apr-2015 pfg <pfg@FreeBSD.org> Make use of allocation attributes in system headers.

Start using 'alloc_size' attribute in the allocator functions.
This is useful as it helps the compiler generate warnings on suspicious
code and can also enable some small optimizations.

This is based on r281130, which brought similar enhnacements
to the standard libc headers.
35b126e324b8032aebea9ab6b4daf7c0bf8daed0 28-Jun-2014 hselasky <hselasky@FreeBSD.org> Pull in r267961 and r267973 again. Fix for issues reported will follow.
fc21f40567ac7485e9e987cf5a539bd0d11c7155 27-Jun-2014 gjb <gjb@FreeBSD.org> Revert r267961, r267973:

These changes prevent sysctl(8) from returning proper output,
such as:

1) no output from sysctl(8)
2) erroneously returning ENOMEM with tools like truss(1)
or uname(1)
truss: can not get etype: Cannot allocate memory
bd1ed65f0faa90d56aad3c8fc1b55d874d1548d9 27-Jun-2014 hselasky <hselasky@FreeBSD.org> Extend the meaning of the CTLFLAG_TUN flag to automatically check if
there is an environment variable which shall initialize the SYSCTL
during early boot. This works for all SYSCTL types both statically and
dynamically created ones, except for the SYSCTL NODE type and SYSCTLs
which belong to VNETs. A new flag, CTLFLAG_NOFETCH, has been added to
be used in the case a tunable sysctl has a custom initialisation
function allowing the sysctl to still be marked as a tunable. The
kernel SYSCTL API is mostly the same, with a few exceptions for some
special operations like iterating childrens of a static/extern SYSCTL
node. This operation should probably be made into a factored out
common macro, hence some device drivers use this. The reason for
changing the SYSCTL API was the need for a SYSCTL parent OID pointer
and not only the SYSCTL parent OID list pointer in order to quickly
generate the sysctl path. The motivation behind this patch is to avoid
parameter loading cludges inside the OFED driver subsystem. Instead of
adding special code to the OFED driver subsystem to post-load tunables
into dynamically created sysctls, we generalize this in the kernel.

Other changes:
- Corrected a possibly incorrect sysctl name from "hw.cbb.intr_mask"
to "hw.pcic.intr_mask".
- Removed redundant TUNABLE statements throughout the kernel.
- Some minor code rewrites in connection to removing not needed
TUNABLE statements.
- Added a missing SYSCTL_DECL().
- Wrapped two very long lines.
- Avoid malloc()/free() inside sysctl string handling, in case it is
called to initialize a sysctl from a tunable, hence malloc()/free() is
not ready when sysctls from the sysctl dataset are registered.
- Bumped FreeBSD version to indicate SYSCTL API change.

MFC after: 2 weeks
Sponsored by: Mellanox Technologies
eb1a5f8de9f7ea602c373a710f531abbf81141c4 21-Feb-2014 gjb <gjb@FreeBSD.org> Move ^/user/gjb/hacking/release-embedded up one directory, and remove
^/user/gjb/hacking since this is likely to be merged to head/ soon.

Sponsored by: The FreeBSD Foundation
6b01bbf146ab195243a8e7d43bb11f8835c76af8 27-Dec-2013 gjb <gjb@FreeBSD.org> Copy head@r259933 -> user/gjb/hacking/release-embedded for initial
inclusion of (at least) arm builds with the release.

Sponsored by: The FreeBSD Foundation
e725dd5c1ef985f6374f7a36ebdaaf10964b0131 28-Jun-2013 jeff <jeff@FreeBSD.org> - Add a general purpose resource allocator, vmem, from NetBSD. It was
originally inspired by the Solaris vmem detailed in the proceedings
of usenix 2001. The NetBSD version was heavily refactored for bugs
and simplicity.
- Use this resource allocator to allocate the buffer and transient maps.
Buffer cache defrags are reduced by 25% when used by filesystems with
mixed block sizes. Ultimately this may permit dynamic buffer cache
sizing on low KVA machines.

Discussed with: alc, kib, attilio
Tested by: pho
Sponsored by: EMC / Isilon Storage Division
5013ab31bdd0c853bd73031dd2246844cd9f9cfe 01-Mar-2012 jhb <jhb@FreeBSD.org> - Change contigmalloc() to use the vm_paddr_t type instead of an unsigned
long for specifying a boundary constraint.
- Change bus_dma tags to use bus_addr_t instead of bus_size_t for boundary
constraints.

These allow boundary constraints to be fully expressed for cases where
sizeof(bus_addr_t) != sizeof(bus_size_t). Specifically, it allows a
driver to properly specify a 4GB boundary in a PAE kernel.

Note that this cannot be safely MFC'd without a lot of compat shims due
to KBI changes, so I do not intend to merge it.

Reviewed by: scottl
84d434965a78b0d9e3e8d915e58cd0bc752d6a25 27-Jan-2012 kmacy <kmacy@FreeBSD.org> exclude kmem_alloc'ed ARC data buffers from kernel minidumps on amd64
excluding other allocations including UMA now entails the addition of
a single flag to kmem_alloc or uma zone create

Reviewed by: alc, avg
MFC after: 2 weeks
09f9c897d33c41618ada06fbbcf1a9b3812dee53 19-Oct-2010 jamie <jamie@FreeBSD.org> A new jail(8) with a configuration file, to replace the work currently done
by /etc/rc.d/jail.
6857471cf3ec44101821a34c3d7dc18913d0c22b 28-Jul-2010 mdf <mdf@FreeBSD.org> Add MALLOC_DEBUG_MAXZONES debug malloc(9) option to use multiple uma
zones for each malloc bucket size. The purpose is to isolate
different malloc types into hash classes, so that any buffer overruns
or use-after-free will usually only affect memory from malloc types in
that hash class. This is purely a debugging tool; by varying the hash
function and tracking which hash class was corrupted, the intersection
of the hash classes from each instance will point to a single malloc
type that is being misused. At this point inspection or memguard(9)
can be used to catch the offending code.

Add MALLOC_DEBUG_MAXZONES=8 to -current GENERIC configuration files.
The suggestion to have this on by default came from Kostik Belousov on
-arch.

This code is based on work by Ron Steinke at Isilon Systems.

Reviewed by: -arch (mostly silence)
Reviewed by: zml
Approved by: zml (mentor)
f1216d1f0ade038907195fc114b7e630623b402c 19-Mar-2010 delphij <delphij@FreeBSD.org> Create a custom branch where I will be able to do the merge.
85f0248bf02fd777b530dc95e93bc561e954b0ed 19-Apr-2009 rwatson <rwatson@FreeBSD.org> Garbage collect now-unused struct malloc_type fields, bump __FreeBSD_version
as kernel modules will need to be rebuilt. These fields existed to support
binary compatibility with kernel modules from before the introduction of
libmemstat(3) in FreeBSD 6.x, so they are no longer required.
61dc3e056cc2ad51f33f296f1cfda0ece440b85e 31-Jan-2009 das <das@FreeBSD.org> Add a function attribute called `__malloc_like', which informs gcc
that the annotated function returns a pointer that doesn't alias any
extant pointer. This results in a 50%+ speedup in microbenchmarks such
as the following:

char *cp = malloc(1), *buf = malloc(BUF);
for (i = 0; i < BUF; i++) buf[i] = *cp;

In real programs, your mileage will vary. Note that gcc already
performs this optimization automatically for any function called
`malloc', `calloc', `strdup', or `strndup' unless -fno-builtins is
used.
19b6af98ec71398e77874582eb84ec5310c7156f 22-Nov-2008 dfr <dfr@FreeBSD.org> Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
performance in Xen's HVM mode.
2668f9825b6a67e6cde6f82c2a5ec2b8ff9c4e56 23-Oct-2008 des <des@FreeBSD.org> Revert the removal of the MALLOC and FREE macros from the net80211 code.

Requested by: sam
66f807ed8b3634dc73d9f7526c484e43f094c0ee 23-Oct-2008 des <des@FreeBSD.org> Retire the MALLOC and FREE macros. They are an abomination unto style(9).

MFC after: 3 months
cf5320822f93810742e3d4a1ac8202db8482e633 19-Oct-2008 lulf <lulf@FreeBSD.org> - Import the HEAD csup code which is the basis for the cvsmode work.
165851adb536053ee0b5bb3ccd3e4492ce4a48a3 27-Aug-2008 jb <jb@FreeBSD.org> MFC

DTrace support.

Note that this defaults the 'make buildkernel' to build with CTF data so
that the release kernel and modules are DTrace-able.
02b323b9738dd2274a014447c7a77a907d91006a 06-Aug-2008 emaste <emaste@FreeBSD.org> MFC r153880:

Improve memguard a bit:
- Provide tunable vm.memguard.desc, so one can specify memory type
without changing the code and recompiling the kernel.
- Allow to use memguard for kernel modules by providing sysctl
vm.memguard.desc, which can be changed to short description of memory
type before module is loaded.
- Move as much memguard code as possible to memguard.c.
- Add sysctl node vm.memguard. and move memguard-specific sysctl there.
- Add malloc_desc2type() function for finding memory type based on its
short description (ks_shortdesc field).
- Memory type can be changed (via vm.memguard.desc sysctl) only if it
doesn't exist (will be loaded later) or when no memory is allocated
yet. If there is allocated memory for the given memory type, return
EBUSY.
- Implement two ways of memory types comparsion and make safer/slower the
default.
8c4eed9aad5a7911341733c7a8d074c9fd9e11f1 23-May-2008 jb <jb@FreeBSD.org> Add support for the DTrace malloc provider which can enable probes
on a per-malloc type basis.
0c72b47a0e54873ac805cdbc304303173d1a0573 10-Nov-2007 alc <alc@FreeBSD.org> Eliminate a stale comment concerning contigmalloc(9).
3cc29e6ebff10906cf1f84a6cc2d518dcecf219c 30-Dec-2005 pjd <pjd@FreeBSD.org> Improve memguard a bit:
- Provide tunable vm.memguard.desc, so one can specify memory type without
changing the code and recompiling the kernel.
- Allow to use memguard for kernel modules by providing sysctl
vm.memguard.desc, which can be changed to short description of memory
type before module is loaded.
- Move as much memguard code as possible to memguard.c.
- Add sysctl node vm.memguard. and move memguard-specific sysctl there.
- Add malloc_desc2type() function for finding memory type based on its
short description (ks_shortdesc field).
- Memory type can be changed (via vm.memguard.desc sysctl) only if it
doesn't exist (will be loaded later) or when no memory is allocated yet.
If there is allocated memory for the given memory type, return EBUSY.
- Implement two ways of memory types comparsion and make safer/slower the
default.
be7673c35bc07e7d53eb5fa1690f0700b72673fb 23-Aug-2005 phk <phk@FreeBSD.org> End the MALLOC_DEFINE macro without the semi-colon, the caller supplies
that.

Spotted by: Flexelint
db6e7961b9ed25287b4a38b2260aead9c767677d 23-Jul-2005 rwatson <rwatson@FreeBSD.org> Merge kern_malloc.c:1.143,1.144 malloc.h:1.81 from HEAD to RELENG_6:

Introduce a new sysctl, kern.malloc_stats, which exports kernel malloc
statistics via a binary structure stream:

- Add structure 'malloc_type_stream_header', which defines a stream
version, definition of MAXCPUS used in the stream, and a number of
malloc_type records in the stream.

- Add structure 'malloc_type_header', which defines the name of the
malloc type being reported on.

- When the sysctl is queried, return a stream header, followed by a
series of type descriptions, each consisting of a type header
followed by a series of MAXCPUS malloc_type_stats structures holding
per-CPU allocation information. Typical values of MAXCPUS will be 1
(UP compiled kernel) and 16 (SMP compiled kernel).

This query mechanism allows user space monitoring tools to extract
memory allocation statistics in a machine-readable form, and to do so
at a per-CPU granularity, allowing monitoring of allocation patterns
across CPUs in order to better understand the distribution of work and
memory flow over multiple CPUs.

While here:

- Bump statistics width to uint64_t, and hard code using fixed-width
type in order to be more sure about structure layout in the stream.
We allocate and free a lot of memory.

- Add kmemcount, a counter of the number of registered malloc types,
in order to avoid excessive manual counting of types. Export via a
new sysctl to allow user-space code to better size buffers.

- De-XXX comment on no longer maintaining the high watermark in old
sysctl monitoring code.

A follow-up commit of libmemstat(3), a library to monitor kernel memory
allocation, will occur in the next few days. Likewise, similar changes
to UMA.

kern_malloc.c:1.144:

Correct build on 64-bit: cast u_int64_t to (unsigned long long) before
printfing as (unsigned long long). 32-bit build on i386 didn't notice
this. Whoops.

Reported by: arved
Tested by: sledge

Approved by: re (kensmith)
7a35a61825dfa26f2d1c482870403373442d33d8 14-Jul-2005 rwatson <rwatson@FreeBSD.org> Introduce a new sysctl, kern.malloc_stats, which exports kernel malloc
statistics via a binary structure stream:

- Add structure 'malloc_type_stream_header', which defines a stream
version, definition of MAXCPUS used in the stream, and a number of
malloc_type records in the stream.

- Add structure 'malloc_type_header', which defines the name of the
malloc type being reported on.

- When the sysctl is queried, return a stream header, followed by a
series of type descriptions, each consisting of a type header
followed by a series of MAXCPUS malloc_type_stats structures holding
per-CPU allocation information. Typical values of MAXCPUS will be 1
(UP compiled kernel) and 16 (SMP compiled kernel).

This query mechanism allows user space monitoring tools to extract
memory allocation statistics in a machine-readable form, and to do so
at a per-CPU granularity, allowing monitoring of allocation patterns
across CPUs in order to better understand the distribution of work and
memory flow over multiple CPUs.

While here:

- Bump statistics width to uint64_t, and hard code using fixed-width
type in order to be more sure about structure layout in the stream.
We allocate and free a lot of memory.

- Add kmemcount, a counter of the number of registered malloc types,
in order to avoid excessive manual counting of types. Export via a
new sysctl to allow user-space code to better size buffers.

- De-XXX comment on no longer maintaining the high watermark in old
sysctl monitoring code.

A follow-up commit of libmemstat(3), a library to monitor kernel memory
allocation, will occur in the next few days. Likewise, similar changes
to UMA.
75c0ea028315e6414a6ada857afaef370f3f745c 29-May-2005 rwatson <rwatson@FreeBSD.org> Due to a last minute change in the #ifdefing in malloc.h before committing,
a nested include of param.h is required so that MAXCPU is visible to all
consumers of sys/malloc.h. In an earlier version of the patch, the
malloc_type_internal structure was only conditionally visible.

Pointed out by: delphij
7035f9f56aa6a07ac9af727380ff3092b79ee9f7 29-May-2005 rwatson <rwatson@FreeBSD.org> Kernel malloc layers malloc_type allocation over one of two underlying
allocators: a set of power-of-two UMA zones for small allocations, and the
VM page allocator for large allocations. In order to maintain unified
statistics for specific malloc types, kernel malloc maintains a separate
per-type statistics pool, which can be monitored using vmstat -m. Prior
to this commit, each pool of per-type statistics was protected using a
per-type mutex associated with the malloc type.

This change modifies kernel malloc to maintain per-CPU statistics pools
for each malloc type, and protects writing those statistics using critical
sections. It also moves to unsynchronized reads of per-CPU statistics
when generating coalesced statistics. To do this, several changes are
implemented:

- In the previous world order, the statistics memory was allocated by
the owner of the malloc type structure, allocated statically using
MALLOC_DEFINE(). This embedded the definition of the malloc_type
structure into all kernel modules. Move to a model in which a pointer
within struct malloc_type points at a UMA-allocated
malloc_type_internal data structure owned and maintained by
kern_malloc.c, and not part of the exported ABI/API to the rest of
the kernel. For the purposes of easing a possible MFC, re-use an
existing pointer in 'struct malloc_type', and maintain the current
malloc_type structure size, as well as layout with respect to the
fields reused outside of the malloc subsystem (such as ks_shortdesc).
There are several unused fields as a result of no longer requiring
the mutex in malloc_type.

- Struct malloc_type_internal contains an array of malloc_type_stats,
of size MAXCPU. The structure defined above avoids hard-coding a
kernel compile-time value of MAXCPU into kernel modules that interact
with malloc.

- When accessing per-cpu statistics for a malloc type, surround read -
modify - update requests with critical_enter()/critical_exit() in
order to avoid races during write. The per-CPU fields are written
only from the CPU that owns them.

- Per-CPU stats now maintained "allocated" and "freed" counters for
number of allocations/frees and bytes allocated/freed, since there is
no longer a coherent global notion of the totals. When coalescing
malloc stats, accept a slight race between reading stats across CPUs,
and avoid showing the user a negative allocation count for the type
in the event of a race. The global high watermark is no longer
maintained for a malloc type, as there is no global notion of the
number of allocations.

- While tearing up the sysctl() path, also switch to using sbufs. The
current "export as text" sysctl format is retained with the same
syntax. We may want to change this in the future to export more
per-CPU information, such as how allocations and frees are balanced
across CPUs.

This change results in a substantial speedup of kernel malloc and free
paths on SMP, as critical sections (where usable) out-perform mutexes
due to avoiding atomic/bus-locked operations. There is also a minor
improvement on UP due to the slightly lower cost of critical sections
there. The cost of the change to this approach is the loss of a
continuous notion of total allocations that can be exploited to track
per-type high watermarks, as well as increased complexity when
monitoring statistics.

Due to carefully avoiding changing the ABI, as well as hardening the ABI
against future changes, it is not necessary to recompile kernel modules
for this change. However, MFC'ing this change to RELENG_5 will require
also MFC'ing optimizations for soft critical sections, which may modify
exposed kernel ABIs. The internal malloc API is changed, and
modifications to vmstat in order to restore "vmstat -m" on core dumps will
follow shortly.

Several improvements from: bde
Statistics approach discussed with: ups
Tested by: scottl, others
f0bf889d0d2ea7d83fd3b67266a98c89cdf14853 07-Jan-2005 imp <imp@FreeBSD.org> /* -> /*- for license, minor formatting changes
c4b4a8f0485fc72cdb96016e2461ed2f31eece8e 19-Jul-2004 green <green@FreeBSD.org> Reimplement contigmalloc(9) with an algorithm which stands a greatly-
improved chance of working despite pressure from running programs.
Instead of trying to throw a bunch of pages out to swap and hope for
the best, only a range that can potentially fulfill contigmalloc(9)'s
request will have its contents paged out (potentially, not forcibly)
at a time.

The new contigmalloc operation still operates in three passes, but it
could potentially be tuned to more or less. The first pass only looks
at pages in the cache and free pages, so they would be thrown out
without having to block. If this is not enough, the subsequent passes
page out any unwired memory. To combat memory pressure refragmenting
the section of memory being laundered, each page is removed from the
systems' free memory queue once it has been freed so that blocking
later doesn't cause the memory laundered so far to get reallocated.

The page-out operations are now blocking, as it would make little sense
to try to push out a page, then get its status immediately afterward
to remove it from the available free pages queue, if it's unlikely to
have been freed. Another change is that if KVA allocation fails, the
allocated memory segment will be freed and not leaked.

There is a sysctl/tunable, defaulting to on, which causes the old
contigmalloc() algorithm to be used. Nonetheless, I have been using
vm.old_contigmalloc=0 for over a month. It is safe to switch at
run-time to see the difference it makes.

A new interface has been used which does not require mapping the
allocated pages into KVA: vm_page.h functions vm_page_alloc_contig()
and vm_page_release_contig(). These are what vm.old_contigmalloc=0
uses internally, so the sysctl/tunable does not affect their operation.

When using the contigmalloc(9) and contigfree(9) interfaces, memory
is now tracked with malloc(9) stats. Several functions have been
exported from kern_malloc.c to allow other subsystems to use these
statistics, as well. This invalidates the BUGS section of the
contigmalloc(9) manpage.
9e1925f5605eb84767aaedbc35ab643a8f9f90a5 07-Apr-2004 imp <imp@FreeBSD.org> Remove advertising clause from University of California Regent's license,
per letter dated July 22, 1999.

Approved by: core
923b177486d23c3a3252d45a3ac8a413bad664c3 19-Aug-2003 sam <sam@FreeBSD.org> correct typo in comment
783ae539c3e44d35afb500b405ff986b3033bc4a 25-Mar-2003 jake <jake@FreeBSD.org> - Add vm_paddr_t, a physical address type. This is required for systems
where physical addresses larger than virtual addresses, such as i386s
with PAE.
- Use this to represent physical addresses in the MI vm system and in the
i386 pmap code. This also changes the paddr parameter to d_mmap_t.
- Fix printf formats to handle physical addresses >4G in the i386 memory
detection code, and due to kvtop returning vm_paddr_t instead of u_long.

Note that this is a name change only; vm_paddr_t is still the same as
vm_offset_t on all currently supported platforms.

Sponsored by: DARPA, Network Associates Laboratories
Discussed with: re, phk (cdevsw change)
b3deffb7e1c58d9f408582b7ba87f3eecdad6d2a 10-Mar-2003 phk <phk@FreeBSD.org> Make malloc and mbuf allocation mode flags nonoverlapping.

Under INVARIANTS whine if we get incompatible flags.

Submitted by: imp
1ef4bb7dc7aa066423a39f04b07ddec225f31aab 26-Feb-2003 rwatson <rwatson@FreeBSD.org> GC M_STRING, no longer required as strdup() accepts a malloc type.

Requested by: phk
3bf9482fa0a7d20205f89c243007961dbb93f701 23-Feb-2003 rwatson <rwatson@FreeBSD.org> Add an implementation of strdup() to libkern. Allocated memory is of
type M_STRING, now defined in malloc.h. Useful when string parsing
must occur using the kernel strsep() and we want to avoid toasting
the source string.

Obtained from: TrustedBSD Project
Sponsored by: DARPA, Network Associates Laboratories
cf874b345d0f766fb64cf4737e1c85ccc78d2bee 19-Feb-2003 imp <imp@FreeBSD.org> Back out M_* changes, per decision of the TRB.

Approved by: trb
bf8e8a6e8f0bd9165109f0a258730dd242299815 21-Jan-2003 alfred <alfred@FreeBSD.org> Remove M_TRYWAIT/M_WAITOK/M_WAIT. Callers should use 0.
Merge M_NOWAIT/M_DONTWAIT into a single flag M_NOWAIT.
27173bd0dd93081098b8676a7b2156fd227c52d3 01-Nov-2002 phk <phk@FreeBSD.org> Introduce malloc_last_fail() which returns the number of seconds since
malloc(9) failed last time. This is intended to help code adjust
memory usage to the current circumstances.

A typical use could be:
if (malloc_last_fail() < 60)
reduce_cache_by_one();
66a17c64befa7b46733cddb36bb548fed0497276 15-Sep-2002 bde <bde@FreeBSD.org> Garbage-collected splmem.

Moved the declaration of malloc_mtx to be with the other extern declarations
and not exposed to userland.

Fixed some minor style bugs.
486bc9dab8aab95468a5910f1ad064ed33582577 15-Sep-2002 bde <bde@FreeBSD.org> Removed most namespace pollution in this header: don't include <vm/uma.h>;
include <sys/_mutex.h> and its prerequisites instead of <sys/mutex.h> and
its prerequisite.
4a91ef45115c512a4f045716754133a0ff842672 19-Jun-2002 billf <billf@FreeBSD.org> fix whitespace botch in previous commit.
030d3fdb720958d03b806333c701404e96a45088 17-Jun-2002 jeff <jeff@FreeBSD.org> - Introduce the new M_NOVM option which tells uma to only check the currently
allocated slabs and bucket caches for free items. It will not go ask the vm
for pages. This differs from M_NOWAIT in that it not only doesn't block, it
doesn't even ask.

- Add a new zcreate option ZONE_VM, that sets the BUCKETCACHE zflag. This
tells uma that it should only allocate buckets out of the bucket cache, and
not from the VM. It does this by using the M_NOVM option to zalloc when
getting a new bucket. This is so that the VM doesn't recursively enter
itself while trying to allocate buckets for vm_map_entry zones. If there
are already allocated buckets when we get here we'll still use them but
otherwise we'll skip it.

- Use the ZONE_VM flag on vm map entries and pv entries on x86.
f7f01600de16995a3b8291a1e6f854e750acf936 02-May-2002 jeff <jeff@FreeBSD.org> malloc/free(9) no longer require Giant. Use the malloc_mtx to protect the
mallochash. Mallochash is going to go away as soon as I introduce the
kfree/kmalloc api and partially overhaul the malloc wrapper. This can't happen
until all users of the malloc api that expect memory to be aligned on the size
of the allocation are fixed.
968fe15c4ddd29119f7160321c6530f8df1bd320 30-Apr-2002 jeff <jeff@FreeBSD.org> Convert longs to u_longs in stats. This will hold off wrap arounds for a
while longer.
578e65f142f837c72b6bfe0c3f7d5137d90ebd35 21-Apr-2002 markm <markm@FreeBSD.org> Parenthesise macro arguments to reduce lint warnings.
da6660250eba783093ce41d6ff66df8ba49d2346 15-Apr-2002 jeff <jeff@FreeBSD.org> Remove malloc_type's ks_limit.

Updated the kmemzones logic such that the ks_size bitmap can be used as an
index into it to report the size of the zone used.

Create the kern.malloc sysctl which replaces the kvm mechanism to report
similar data. This will provide an easy place for statistics aggregation if
malloc_type statistics become per cpu data.

Add some code ifdef'd under MALLOC_PROFILING to facilitate a tool for sizing
the malloc buckets.
caca591db32de62e9d285089a17accffe2db18f1 08-Apr-2002 asmodai <asmodai@FreeBSD.org> Change ks_calls from int64_t to uint64_t, since it cannot be negative.

PR: 32342
Submitted by: ryan beasley <ryanb@goddamnbastard.org>
Reviewed by: jeff, Tim J Robbins
d27d5e3b44ff619a0151aa86993697a211e61bc7 23-Mar-2002 bde <bde@FreeBSD.org> Fixed some style bugs in the removal of __P(()). The main ones were
not removing tabs before "__P((", and not outdenting continuation lines
to preserve non-KNF lining up of code with parentheses. Switch to KNF
formatting and/or rewrap the whole prototype in some cases.
3b2d03b60a11ce28e58a87212bcccedd306f2c81 19-Mar-2002 alfred <alfred@FreeBSD.org> Remove __P
2923687da3c046deea227e675d5af075b9fa52d4 19-Mar-2002 jeff <jeff@FreeBSD.org> This is the first part of the new kernel memory allocator. This replaces
malloc(9) and vm_zone with a slab like allocator.

Reviewed by: arch@
4ff830618672fdf2a2864c71864aea5adc8d03e8 13-Mar-2002 archie <archie@FreeBSD.org> Add realloc() and reallocf(), and make free(NULL, ...) acceptable.

Reviewed by: alfred
2ff1c253cdbfd553e53fca698ea48f0fedd1ed13 10-Aug-2001 jhb <jhb@FreeBSD.org> - Remove asleep(), await(), and M_ASLEEP.
- Callers of asleep() and await() have been converted to calling tsleep().
The only caller outside of M_ASLEEP was the ata driver, which called both
asleep() and await() with spl-raised, so there was no need for the
asleep() and await() pair. M_ASLEEP was unused.

Reviewed by: jasone, peter
076c2e70fda136ef48c7da93fc16bf7d4c7cb0cf 31-Jan-2001 bp <bp@FreeBSD.org> Let M_PANIC go back to the private tree as its intention isn't understood well
for now.
e88e663eb2e5baec6cc42b7e50c7598cb0576a17 29-Jan-2001 bp <bp@FreeBSD.org> Add M_PANIC flag to the list of available flags passed to malloc().
With this flag set malloc() will panic if memory allocation failed.
This usable only in critical places where failed allocation is fatal.

Reviewed by: peter
55e86a81b7d5c5c9b30129c8436b626fd7e5747f 20-Oct-2000 phk <phk@FreeBSD.org> Introduce the M_ZERO flag to malloc(9)

Instead of:

foo = malloc(sizeof(foo), M_WAIT);
bzero(foo, sizeof(foo));

You can now (and please do) use:

foo = malloc(sizeof(foo), M_WAIT | M_ZERO);

In the future this will enable us to do idle-time pre-zeroing of
malloc-space.
76169e6b33f628abbefe6799fd6a548c8747023b 14-Jun-2000 bde <bde@FreeBSD.org> sys/malloc.h:
Order the SYSINIT() for MALLOC_DEFINE() correctly so that malloc()
doesn't have to waste time initializing itself. The
(SI_SUB_KMEM, SI_ORDER_ANY) order was shared with syscons' SYSINIT()
for scmeminit(), and scmeminit() calls malloc(), so malloc()
initialization was not always complete on the first call to malloc().

kern/kern_malloc.c:
- Removed self-initialization in malloc().
- Removed half-baked sanity check in free(). Trust MALLOC_DEFINE().
cc22f14b179a55f7686e798febd938bfef76c982 14-Jun-2000 bde <bde@FreeBSD.org> Removed support for generating inline code for MALLOC() and FREE()
in the dysfunctional !KMEMSTATS case. This hasn't compiled since
rev.1.31 of kern_malloc.c quietly removed the core of the support
for the !KMEMSTATS case. I fixed it to see if it was worth saving
and found that (as usual) inlining just wasted space and increased
complexity without significantly affecting time, at least for the
lmbench2 micro-benchmark on a Celeron. The space bloat was
surprisingly large - the text size increased from 1700K to 1840K
for a version with the entire malloc() family inlined.

Removed even older garbage (kmemxtob() and btokmemx() macros).

Attempt to deprecate MALLOC() and FREE(). Given current compilers
(gcc-2.x or C99), they don't do anything that (safe) function-like
macros or inline functions named malloc() and free() couldn't do.

Fixed missing casts of macro args in MALLOC() and FREE().
b42951578188c5aab5c9f8cbcde4a743f8092cdc 02-Apr-2000 cvs2svn <cvs2svn@FreeBSD.org> This commit was manufactured by cvs2svn to create branch 'ALSA'.
15b9bcb121e1f3735a2c98a11afdb52a03301d7e 29-Dec-1999 peter <peter@FreeBSD.org> Change #ifdef KERNEL to #ifdef _KERNEL in the public headers. "KERNEL"
is an application space macro and the applications are supposed to be free
to use it as they please (but cannot). This is consistant with the other
BSD's who made this change quite some time ago. More commits to come.
7efc91cadcfeb421fc4d02ba94db784616f3714c 05-Nov-1999 shin <shin@FreeBSD.org> KAME related header files additions and merges.
(only those which don't affect c source files so much)

Reviewed by: cvs-committers
Obtained from: KAME project
a1cfc5e8ef9629a4c3eef5cd3267cad159c0e0c5 01-Oct-1999 peter <peter@FreeBSD.org> Force the "calls" count for malloc types to be 64 bit where it keeps track
of the number of times a particular type has been used. It's rather easy
to overflow. One site I'm looking at seems to do it in a matter of days.
On the Alpha this is a no-op since 'long' is 64 bit already. The sole
user of this interface seems to be vmstat -m and friends which will need
a recompile. The overheads of using a 64 bit int should be pretty light
as the kernel just does "calls++" type operations and that's it.
a229a63526767d194764b7401510c12dfc658bd7 11-Sep-1999 bde <bde@FreeBSD.org> Fixed some style bugs (mainly disorderd prototypes).
ec1ed330ea849528059831d091267265414df4b9 11-Sep-1999 bde <bde@FreeBSD.org> Get rid of MALLOC_INSTANTIATE and MALLOC_MAKE_TYPE(). Just handle the 3
malloc types declared in <sys/malloc.h> like other global malloc types.
3b842d34e82312a8004a7ecd65ccdb837ef72ac1 28-Aug-1999 peter <peter@FreeBSD.org> $Id$ -> $FreeBSD$
e657118d0bda448b3f9d34a3768770329ebaf847 10-Aug-1999 peter <peter@FreeBSD.org> Add a contigfree() as a corollary to contigmalloc() as it's not clear
which free routine to use and people are tempted to use free() (which
doesn't work)
a40e0249d4e2495009cf3fb552bc428d0a8a8676 27-Jan-1999 dillon <dillon@FreeBSD.org> Fix warnings in preparation for adding -Wall -Wcast-qual to the
kernel compile
505442286b6fcd81977413325a0b3b3327404203 21-Jan-1999 dillon <dillon@FreeBSD.org> Renamed M_KERNEL to a more appropriate M_USE_RESERVE.
df24433bbe29112b4b9c9f38e80ba6cfb6988cb0 21-Jan-1999 dillon <dillon@FreeBSD.org> This is a rather large commit that encompasses the new swapper,
changes to the VM system to support the new swapper, VM bug
fixes, several VM optimizations, and some additional revamping of the
VM code. The specific bug fixes will be documented with additional
forced commits. This commit is somewhat rough in regards to code
cleanup issues.

Reviewed by: "John S. Dyson" <root@dyson.iquest.net>, "David Greenman" <dg@root.com>
0f95240d58e95f72af7231f521da0616cf35fa6d 10-Nov-1998 peter <peter@FreeBSD.org> Have MALLOC_DECLARE() initialize malloc types explicitly, and have them
removed at module unload (if in a module of course).
However; this introduces a new dependency on <sys/kernel.h> for things
that use MALLOC_DECLARE(). Bruce told me it is better to add sys/kernel.h
to the handful of files that need it rather than add an extra include to
sys/malloc.h for kernel compiles. Updates to follow in subsequent commits.
10c5ccc30ae8155c4d8bd25aeffd9ed0e476c387 08-Mar-1998 julian <julian@FreeBSD.org> Reviewed by: dyson@freebsd.org (john Dyson), dg@root.com (david greenman)
Submitted by: Kirk McKusick (mcKusick@mckusick.com)
Obtained from: WHistle development tree
0506343883d62f6649f7bbaf1a436133cef6261d 11-Jan-1998 cvs2svn <cvs2svn@FreeBSD.org> This commit was manufactured by cvs2svn to create branch 'jb'.
7c6e96080c4fb49bf912942804477d202a53396c 10-Jan-1998 cvs2svn <cvs2svn@FreeBSD.org> This commit was manufactured by cvs2svn to create branch 'JB'.
7629c0321e705cb78c28a7c4ad020f2c2451c5d6 27-Dec-1997 bde <bde@FreeBSD.org> Unspammed nested include of <vm/vm_zone.h>.
40c3cb0b98b56afcbdc716f9398ac632dcdda05e 05-Dec-1997 bde <bde@FreeBSD.org> Removed one `const' from the declaration of `ks_shortdesc'. The pointer
isn't actually const in vmstat.

Fixed pedantic syntax errors caused by trailing semicolons in macro
definitions.

Fixed style bugs and typos in revisions 1.26-1.33.
8ee98ba6eecb5aaaba384e00fe11ee588e3299a4 05-Dec-1997 bde <bde@FreeBSD.org> Moved declaration of M_IOV to a less bogus place. It belongs in
<sys/uio.h>, but it doesn't work there because of header pollution
(<sys/uio.h> is prematurely included by <sys/param.h>).
0f54a3a55fc405b6373d96d2bf95d51d78be5e0c 05-Dec-1997 dyson <dyson@FreeBSD.org> Some fixes from John Hood:
1) Fix the initialization of malloc structure that changed
due to perf opt.
2) Remove unneeded include.
3) An initialization assert added to malloc.
Submitted by: John Hood <cgull@smoke.marlboro.vt.us>
2214184fec81375cfc3d2d8efc4b57ef98780cfb 04-Dec-1997 dg <dg@FreeBSD.org> shuffle structs for better cacheline behavior.
349f4dfe035d86ca42c2320bc91fc94acd12ce10 28-Oct-1997 phk <phk@FreeBSD.org> Remove the long description from the in-kernel datastructure.
Put a magic field in there instead, to help catch uninitialized
malloc types.
36e7a51ea1dedf0fc860ff3106aee1db1ab3b1f5 12-Oct-1997 phk <phk@FreeBSD.org> Last major round (Unless Bruce thinks of somthing :-) of malloc changes.

Distribute all but the most fundamental malloc types. This time I also
remembered the trick to making things static: Put "static" in front of
them.

A couple of finer points by: bde
645e7b2ab6676a2a3a05a59a053929d3b7732b4d 11-Oct-1997 phk <phk@FreeBSD.org> Distribute and statizice a lot of the malloc M_* types.

Substantial input from: bde
3db5ba5d3e3d4ce627458c576ac34438db01ed84 11-Oct-1997 phk <phk@FreeBSD.org> Remove all traces of M_VFSCONF, which were for all practical
purposes unused.
c574a182640848da6843ed8bd55c761f4d748bd4 10-Oct-1997 phk <phk@FreeBSD.org> Remove a bunch of unused malloc types.
A couple of potential bogons flagged.
Various prototypes changed.
7c874af4b84b221f2d71b950c876c07d5cb68c79 10-Oct-1997 phk <phk@FreeBSD.org> Make malloc more extensible. The malloc type is now a pointer to
the struct kmemstats that describes the type.

This allows subsystems to declare their malloc types locally
and <sys/malloc.h> doesn't need tweaked everytime somebody
gets an idea. You can even have a type local to a lkm...

I don't know if we really need the longdesc, comments welcome.

TODO: There is a single nit in ext2fs, that will be fixed later,
and I intend to remove all unused malloc types and distribute
the rest closer to their use.
e64b1984f97c6d987d7d36b61a3afe5028a08312 21-Sep-1997 dyson <dyson@FreeBSD.org> Change the M_NAMEI allocations to use the zone allocator. This change
plus the previous changes to use the zone allocator decrease the useage
of malloc by half. The Zone allocator will be upgradeable to be able
to use per CPU-pools, and has more intelligent usage of SPLs. Additionally,
it has reasonable stats gathering capabilities, while making most calls
inline.
64d0bec2a826107aa4006f6e9dc8ec97d812eded 16-Sep-1997 bde <bde@FreeBSD.org> Fixed staticization. buckets[] was staticized but was still declared
extern in <sys/malloc.h> and it should not have been staticized for
the !(KMEMSTATS || DIAGNOSTIC) case.

Fixed the !(KMEMSTATS || DIAGNOSTIC) case. The MALLOC() and FREE()
macros are evil, but code generally doesn't allow for this and some code
involving else clauses did not compile.

Finished staticization.
4542c1cf5d7077caf33d6d9468f5e647cd9d19e5 16-Aug-1997 wollman <wollman@FreeBSD.org> Fix all areas of the system (or at least all those in LINT) to avoid storing
socket addresses in mbufs. (Socket buffers are the one exception.) A number
of kernel APIs needed to get fixed in order to make this happen. Also,
fix three protocol families which kept PCBs in mbufs to not malloc them
instead. Delete some old compatibility cruft while we're at it, and add
some new routines in the in_cksum family.
8fa8ae3d0d6a785deced2be3e7fd647f31aaf23e 05-Aug-1997 dyson <dyson@FreeBSD.org> Get rid of the ad-hoc memory allocator for vm_map_entries, in lieu of
a simple, clean zone type allocator. This new allocator will also be
used for machine dependent pmap PV entries.
532dea9042abf44b0a21290e3e3e16fba10a32f7 06-Jul-1997 dyson <dyson@FreeBSD.org> This is an upgrade so that the kernel supports the AIO calls from
POSIX.4. Additionally, there is some initial code that supports LIO.
This code supports AIO/LIO for all types of file descriptors, with
few if any restrictions. There will be a followup very soon that
will support significantly more efficient operation for VCHR type
files (raw.) This code is also dependent on some kernel features
that don't work under SMP yet. After I commit the changes to the
kernel to support proper address space sharing on SMP, this code
will also work under SMP.
94b6d727947e1242356988da003ea702d41a97de 22-Feb-1997 peter <peter@FreeBSD.org> Back out part 1 of the MCFH that changed $Id$ to $FreeBSD$. We are not
ready for it yet.
10f666af84d48e89e4e2960415c9b616fce4077f 10-Feb-1997 dyson <dyson@FreeBSD.org> This is the kernel Lite/2 commit. There are some requisite userland
changes, so don't expect to be able to run the kernel as-is (very well)
without the appropriate Lite/2 userland changes.

The system boots and can mount UFS filesystems.

Untested: ext2fs, msdosfs, NFS
Known problems: Incorrect Berkeley ID strings in some files.
Mount_std mounts will not work until the getfsent
library routine is changed.

Reviewed by: various people
Submitted by: Jeffery Hsu <hsu@freebsd.org>
808a36ef658c1810327b5d329469bcf5dad24b28 14-Jan-1997 jkh <jkh@FreeBSD.org> Make the long-awaited change from $Id$ to $FreeBSD$

This will make a number of things easier in the future, as well as (finally!)
avoiding the Id-smashing problem which has plagued developers for so long.

Boy, I'm glad we're not using sup anymore. This update would have been
insane otherwise.
e1bb1cc95e64d0b01bb7f5e247843ada3c31c28f 29-Dec-1996 phk <phk@FreeBSD.org> Reserve 4 malloc types for generic geometry handling.
c61e4bcfdb1e5ae975e1d75cbeb8a7ed1ceebe97 20-Aug-1996 smpatel <smpatel@FreeBSD.org> Remove the kernel FD_SETSIZE limit for select().
Make select()'s first argument 'int' not 'u_int'.

Reviewed by: bde
ee88966824159672a760371172f32019eee12ae5 04-Aug-1996 phk <phk@FreeBSD.org> Add separate kmalloc classes for BIO buffers and Ktrace info.
845782b7e052a1136c206b3e7ae58942a6a725bd 14-Jun-1996 wollman <wollman@FreeBSD.org> This is the `netkey' kernel key-management service (the PF_KEY analogue
to PF_ROUTE) from NRL's IPv6 distribution, heavily modified by me for
better source layout, formatting, and textual conventions. I am told
that this code is no longer under active development, but it's a useful
hack for those interested in doing work on network security, key management,
etc. This code has only been tested twice, so it should be considered
highly experimental.

Obtained from: ftp.ripe.net
5a6fb3a7da69f11fbe5093f4a2cc7a4d86df5b36 02-May-1996 phk <phk@FreeBSD.org> removed:
CLBYTES PD_SHIFT PGSHIFT NBPG PGOFSET CLSIZELOG2 CLSIZE pdei()
ptei() kvtopte() ptetov() ispt() ptetoav() &c &c
new:
NPDEPG

Major macro cleanup.
73a498e93ef77f792f958b4a1ea0d9ad0490888a 11-Mar-1996 peter <peter@FreeBSD.org> Import 4.4BSD-Lite2 onto the vendor branch, note that in the kernel, all
files are off the vendor branch, so this should not change anything.

A "U" marker generally means that the file was not changed in between
the 4.4Lite and Lite-2 releases, and does not need a merge. "C" generally
means that there was a change.
[new sys/syscallargs.h file, to be "cvs rm"ed]
a95231aa2e4525b35dac98199326b2f492040263 11-Mar-1996 hsu <hsu@FreeBSD.org> Merge in Lite2: resolve manifest constant numbering in favor of least
required changes and also add parentheses around BUCKETINDX macro body.
Reviewed by: davidg & bde
0f99c673dca6e0c25f96b0853d856adb0dbd0b08 27-Jan-1996 bde <bde@FreeBSD.org> Added a `boundary' arg to vm_alloc_page_contig(). Previously the only
way to avoid crossing a 64K DMA boundary was to specify an alignment
greater than the size even when the alignment didn't matter, and for
sizes larger than a page, this reduced the chance of finding enough
contiguous pages. E.g., allocations of 8K not crossing a 64K boundary
previously had to be allocated on 8K boundaries; now they can be
allocated on any 4K boundary except (64 * n + 60)K.

Fixed bugs in vm_alloc_page_contig():
- the last page wasn't allocated for sizes smaller than a page.
- failures of kmem_alloc_pageable() weren't handled.

Mutated vm_page_alloc_contig() to create a more convenient interface
named contigmalloc(). This is the same as the one in 1.1.5 except
it has `low' and `high' args, and the `alignment' and `boundary'
args are multipliers instead of masks.
945b001c36df6c5fd91850c2b1a7369de4777555 02-Dec-1995 phk <phk@FreeBSD.org> malloc.h: add sysctl class.
sysctl.h add "AUTO" OID.

both of which will be need RSN.
86f1bc4514fdcfd255f37f3218fe234bdc3664fc 05-Nov-1995 cvs2svn <cvs2svn@FreeBSD.org> This commit was manufactured by cvs2svn to create branch 'LINUX'.
2602110a4c31711418aae41ef03f9876960d6dff 14-Sep-1995 wollman <wollman@FreeBSD.org> Remove bogus (caddr_t) casts from FREE macros; they can obscure serious
bugs that the type-checking would otherwise have found. (This has no
effect on anything that I noticed in the current system other than finding
the bug in some code that I am writing.)
266ce25fcbbdf2996fd32fc626b3cccf02ccff2f 20-Apr-1995 julian <julian@FreeBSD.org> Reviewed by: no-one yet, but non-intrusive
Submitted by: julian@tfs.com
Obtained from: written from scratch

slight changes to make space for devfs..
(also conditional test code in i386/isa/fd.c)

===================================================================
RCS file: /home/ncvs/src/sys/sys/malloc.h,v
retrieving revision 1.7
diff -r1.7 malloc.h
113a114,117
> #define M_DEVFSMNT 62 /* DEVFS mount structure */
> #define M_DEVFSBACK 63 /* DEVFS Back node */
> #define M_DEVFSFRONT 64 /* DEVFS Front node */
> #define M_DEVFSNODE 65 /* DEVFS node */
184c188,192
< NULL, NULL, NULL, NULL, NULL, \
---
> "DEVFS mount", /* 62 M_DEVFSMNT */ \
> "DEVFS back", /* 63 M_DEVFSBACK */ \
> "DEVFS front", /* 64 M_DEVFSFRONT */ \
> "DEVFS node", /* 65 M_DEVFSNODE */ \
> NULL, \
Index: sys/mount.h
===================================================================
RCS file: /home/ncvs/src/sys/sys/mount.h,v
retrieving revision 1.16
diff -r1.16 mount.h
100c100,101
< #define MOUNT_MAXTYPE 15
---
> #define MOUNT_DEVFS 16 /* existing device Filesystem */
> #define MOUNT_MAXTYPE 16
118a120
> "devfs", /* 15 MOUNT_DEVFS */ \
Index: sys/vnode.h
===================================================================
RCS file: /home/ncvs/src/sys/sys/vnode.h,v
retrieving revision 1.19
diff -r1.19 vnode.h
61c61
< VT_UNION, VT_MSDOSFS
---
> VT_UNION, VT_MSDOSFS, VT_DEVFS
2e14d9ebc3d3592c67bdf625af9ebe0dfc386653 14-Mar-1995 cvs2svn <cvs2svn@FreeBSD.org> This commit was manufactured by cvs2svn to create branch 'MATT_THOMAS'.
e05c371e44091a13a0f082f45b251966bcab33ce 12-Mar-1995 ugen <ugen@FreeBSD.org> Add M_DEVS for list of saved isa_device's
and M_IPFW for firewall chans memory on the same opportunity,
theese types of memory are unique enough to have own identifiers and
besides vmstat looks much more useful and right for those now
75b3143b74264b6b5477a8f3766ec1148873f8e3 02-Feb-1995 dg <dg@FreeBSD.org> Calling semantics for kmem_malloc() have been changed...and the third
argument is now more than just a single flag.

Submitted by: John Dyson
ddf9ae441960dcf931d0ab91aa12ff5fcb741433 04-Oct-1994 phk <phk@FreeBSD.org> Added M_GZIP for the imgact_gzip code. The gzip-code is likely to be used
for other weird things in the future (hint, hint!)
34cd81d75f398ee455e61969b118639dacbfd7a6 23-Sep-1994 cvs2svn <cvs2svn@FreeBSD.org> This commit was manufactured by cvs2svn to create branch 'MACKERRAS'.
7efbb964fdff1348e01bdcd7d4bb59551c389bef 19-Sep-1994 dfr <dfr@FreeBSD.org> Added msdosfs.

Obtained from: NetBSD
e16baf7a5fe7ac1453381d0017ed1dcdeefbc995 07-Aug-1994 cvs2svn <cvs2svn@FreeBSD.org> This commit was manufactured by cvs2svn to create branch 'SUNRPC'.
8d205697aac53476badf354623abd4e1c7bc5aff 02-Aug-1994 dg <dg@FreeBSD.org> Added $Id$
2469c867a164210ce96143517059f21db7f1fd17 25-May-1994 rgrimes <rgrimes@FreeBSD.org> The big 4.4BSD Lite to FreeBSD 2.0.0 (Development) patch.

Reviewed by: Rodney W. Grimes
Submitted by: John Dyson and David Greenman
8fb65ce818b3e3c6f165b583b910af24000768a5 24-May-1994 rgrimes <rgrimes@FreeBSD.org> BSD 4.4 Lite Kernel Sources