cacheflush(2)

πŸ’– cacheflush(2) β€” bestie edition πŸ’–

okay so this is a page from the System Calls Manual, and it's all about cacheflush(2). if you're on a MIPS-core girlie's machine or honestly anywhere with a split instruction/data cache, this one's for you. strap in, we're flushing caches and we're doing it cutely 🧹✨


🌸 NAME 🌸

cacheflush β€” flush contents of instruction and/or data cache

πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc)


✨ SYNOPSIS ✨

#include <sys/cachectl.h>

int cacheflush(int nbytes;
               void addr[nbytes], int nbytes, int cache);
bestie heads up: on some architectures there is literally no glibc wrapper for this system call. go read VERSIONS before you start typing, i'm begging 😭

πŸ’… DESCRIPTION πŸ’…

cacheflush() flushes the contents of the indicated cache(s) for the user addresses in the range addr to (addr+nbytes-1). yes, inclusive, yes, that's the vibe.

and cache can be one of these three icons:

  • ICACHE β€” flush the instruction cache. slay, the CPU knows what to do next πŸ”₯
  • DCACHE β€” write back to memory and invalidate the affected valid cache lines. two things at once, main character energy, no notes πŸ’…
  • BCACHE β€” same as (ICACHE|DCACHE). it's both, it's a combo, it ate 🌈

🫢 RETURN VALUE 🫢

cacheflush() returns 0 on success. iconic. flawless. no cap.

on error it returns -1 and sets errno to indicate the error. not the vibe, but we move 😭


🚨 ERRORS 🚨

  • EFAULT β€” some or all of the address range addr to (addr+nbytes-1) is not accessible. like, you pointed at something that isn't yours. rude but fair πŸ’€
  • EINVAL β€” cache is not one of ICACHE, DCACHE, or BCACHE (but see BUGS πŸ‘€ the call is lying to you a little bit)

πŸ¦‹ VERSIONS πŸ¦‹

lowkey important: cacheflush() should not be used in programs intended to be portable. it's giving "works on my machine" and it means it.

on Linux, this call first appeared on the MIPS architecture. but nowadays Linux provides a cacheflush() system call on some other architectures too β€” except with different arguments. same name, different girl. πŸ™ˆ

πŸŽ€ Architecture-specific variants πŸŽ€

glibc provides a wrapper for this system call, with the prototype shown up in SYNOPSIS, for the following architectures:

ARC, CSKY, MIPS, and NIOS2. (yes, those exact four, no extras, write them down ✍️)

on some other architectures, Linux provides this system call with different arguments:

M68K:

int cacheflush(unsigned long addr, int scope, int cache,
               unsigned long size);

SH:

int cacheflush(unsigned long addr, unsigned long size, int op);

NDS32:

int cacheflush(unsigned int start, unsigned int end, int cache);

on the architectures above, glibc does not provide a wrapper for this system call β€” you have to call it yourself using syscall(2). yes, manually. it's giving DIY ✨

🌈 GCC alternative 🌈

unless you need the finer grained control that this system call provides, you probably want to use the GCC built-in function __builtin___clear_cache(), which provides a portable interface across platforms supported by GCC and compatible compilers:

void __builtin___clear_cache(void *begin, void *end);

on platforms that don't require instruction cache flushes, __builtin___clear_cache() has no effect. it's just out here existing, doing nothing, and honestly? same 🫢

note: on some GCC-compatible compilers, the prototype for this built-in function uses char * instead of void * for the parameters. tiny little difference, big little consequences πŸ’œ

πŸ‘‘ STANDARDS πŸ‘‘

historically, this system call was available on all MIPS UNIX variants β€” including RISC/os, IRIX, Ultrix, NetBSD, OpenBSD, and FreeBSD β€” and also on some non-UNIX MIPS operating systems. so the existence of this call in MIPS operating systems is a de-facto standard. she's been everywhere, she's a legend, periodt πŸ’…


πŸ’€ BUGS πŸ’€

okay this is the messy part, listen up:

  • Linux kernels older than Linux 2.6.11 ignore the addr and nbytes arguments, making this function fairly expensive. therefore, the whole cache is always flushed. not "sometimes", not "partially" β€” the whole thing. every time. 😭
  • this function always behaves as if BCACHE has been passed for the cache argument, and does not do any error checking on the cache argument. so that EINVAL above? yeah. see? told you. the flag isn't even being read bestie. it's fake. it's decorative. it's a vibe. πŸ™ˆ

and that's the whole page!! go flush something, love you, bye!! πŸŒΈπŸŽ€πŸ’–

this page is from Linux man-pages 6.20, dated 2026-02-08 β€” mwah, kisses to the maintainers, thank you for your service πŸ’œβœ¨

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: GPL-2.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms