initial commit

This commit is contained in:
Kaiwan N Billimoria
2023-04-12 10:30:21 +05:30
parent 71b074801d
commit 89d2d675dd
2 changed files with 110 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
# Makefile
# For 'Linux Kernel Programming' 2E, Kaiwan N Billimoria, Packt
# ch9/oom_killer_try
# userspace app.
ALL := oom_killer_try oom_killer_try_dbg
CC := ${CROSS_COMPILE}gcc
all: ${ALL}
oom_killer_try: oom_killer_try.c
${CC} -O2 oom_killer_try.c -o oom_killer_try -Wall
oom_killer_try_dbg: oom_killer_try.c
${CC} -O0 -g -ggdb -DDEBUG oom_killer_try.c -o oom_killer_try_dbg -Wall -Wextra
clean:
rm -v -f ${ALL}

View File

@@ -0,0 +1,96 @@
/*
* ch9/oom_killer_try/oom_killer_try.c
***************************************************************
* This program is part of the source code released for the book
* "Linux Kernel Programming" 2E
* (c) Author: Kaiwan N Billimoria
* Publisher: Packt
* GitHub repository:
* https://github.com/PacktPublishing/Linux-Kernel-Programming_2E
*
* From: Ch 9: Kernel Memory Allocation for Module Authors Part 2
****************************************************************
* Brief Description:
*
* A user mode demo app to try and invoke the Linux kernel OOM killer!
* How? simple: by having this process call malloc() repeatedly without
* freeing back memory to the system. Ultimately, the kernel will kill it via
* OOM. For a more "sure" kill, set the "force-page-fault" flag.
*
** WARNING **
* Be warned that running this intensively can/will cause heavy swapping on
* your system and might even necessitate a reboot; to be safe, only run this
* on a test VM.
*
* For details, please refer the book, Ch 9.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BLK (getpagesize()*2)
static int force_page_fault = 0;
int main(int argc, char **argv)
{
char *p;
int i = 0, j = 1, stepval = 5000, verbose = 0;
if (argc < 3) {
fprintf(stderr,
"Usage: %s alloc-loop-count force-page-fault[0|1] [verbose_flag[0|1]]\n",
argv[0]);
exit(EXIT_FAILURE);
}
if (atoi(argv[2]) == 1)
force_page_fault = 1;
if (argc >= 4) {
if (atoi(argv[3]) == 1)
verbose = 1;
}
printf("%s: PID %d (verbose mode: %s)\n",
argv[0], getpid(), (verbose==1?"on":"off"));
do {
p = (char *)malloc(BLK);
if (!p) {
fprintf(stderr, "%s: loop #%d: malloc failure.\n",
argv[0], i);
break;
}
/* *IMPORTANT* Demand Paging :
* Force the MMU to raise the page fault exception by writing into the
* just allocated pages; writing a single byte, any byte, into eahc page
* will do the trick! This is as the virtual addresses referenced will
* have no PTE entry, causing the MMU to raise the page fault!
* The fault handler, being intelligent, figures out it's a "good fault"
* (a minor fault) and allocates a page frame (per page) via the page
* allocator! Only now do we have physical memory!
*/
if (force_page_fault) {
p[2048] &= 0xde; // write something into a byte of the 1st page
p[6143] |= 0xad; // write something into a byte of the 2nd page
}
if (!(i % stepval)) { // every 'stepval' iterations..
if (!verbose) {
if (!(j%5))
printf(". ");
else
printf(".");
fflush(stdout);
j++;
} else {
printf("%06d\taddr p = %p break = %p\n",
i, (void *)p, (void *)sbrk(0));
}
}
i++;
} while (p && (i < atoi(argv[1])));
exit(EXIT_SUCCESS);
}