Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of work SO2, lab02 #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/labs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/skels/
#/skels/
vmlinux
zImage
serial.pts
Expand Down
9 changes: 9 additions & 0 deletions tools/labs/skels/Kbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# autogenerated, do not edit
ccflags-y += -Wno-unused-function -Wno-unused-label -Wno-unused-variable
obj-m += ./kernel_api/5-list-full/
obj-m += ./kernel_api/4-list/
obj-m += ./kernel_api/1-mem/
obj-m += ./kernel_api/2-sched-spin/
obj-m += ./kernel_api/7-list-test/
obj-m += ./kernel_api/3-memory/
obj-m += ./kernel_api/6-list-sync/
3 changes: 3 additions & 0 deletions tools/labs/skels/kernel_api/1-mem/Kbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ccflags-y = -Wno-unused-function -Wno-unused-label -Wno-unused-variable

obj-m = mem.o
46 changes: 46 additions & 0 deletions tools/labs/skels/kernel_api/1-mem/mem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Kernel API lab
*
* mem.c - Memory allocation in Linux
*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/ctype.h>

MODULE_DESCRIPTION("Print memory");
MODULE_AUTHOR("SO2");
MODULE_LICENSE("GPL");

static char *mem;

static int mem_init(void)
{
size_t i;

mem = kmalloc(4096 * sizeof(*mem), GFP_KERNEL);
if (mem == NULL)
goto err_mem;

pr_info("chars: ");
for (i = 0; i < 4096; i++) {
if (isalpha(mem[i]))
printk("%c ", mem[i]);
}
pr_info("\n");

return 0;

err_mem:
return -1;
}

static void mem_exit(void)
{
kfree(mem);
}

module_init(mem_init);
module_exit(mem_exit);
3 changes: 3 additions & 0 deletions tools/labs/skels/kernel_api/2-sched-spin/Kbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ccflags-y = -Wno-unused-function -Wno-unused-label -Wno-unused-variable

obj-m = sched-spin.o
40 changes: 40 additions & 0 deletions tools/labs/skels/kernel_api/2-sched-spin/sched-spin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Kernel API lab
*
* sched-spin.c: Sleeping in atomic context
*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h>

MODULE_DESCRIPTION("Sleep while atomic");
MODULE_AUTHOR("SO2");
MODULE_LICENSE("GPL");

static int sched_spin_init(void)
{
spinlock_t lock;

spin_lock_init(&lock);

/* TODO 0: Use spin_lock to aquire the lock */
spin_lock(&lock);

set_current_state(TASK_INTERRUPTIBLE);
/* Try to sleep for 5 seconds. */
schedule_timeout(5 * HZ);

/* TODO 0: Use spin_unlock to release the lock */
spin_unlock(&lock);

return 0;
}

static void sched_spin_exit(void)
{
}

module_init(sched_spin_init);
module_exit(sched_spin_exit);
3 changes: 3 additions & 0 deletions tools/labs/skels/kernel_api/3-memory/Kbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ccflags-y = -Wno-unused-function -Wno-unused-label -Wno-unused-variable

obj-m = memory.o
77 changes: 77 additions & 0 deletions tools/labs/skels/kernel_api/3-memory/memory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* SO2 lab3 - task 3
*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>

MODULE_DESCRIPTION("Memory processing");
MODULE_AUTHOR("SO2");
MODULE_LICENSE("GPL");

struct task_info {
pid_t pid;
unsigned long timestamp;
};

static struct task_info *ti1, *ti2, *ti3, *ti4;

static struct task_info *task_info_alloc(int pid)
{
struct task_info *ti;

/* TODO 1: allocated and initialize a task_info struct */
ti = kmalloc(sizeof(*ti), GFP_KERNEL);
if (ti == NULL)
return NULL;

ti->pid = pid;
ti->timestamp = jiffies;

return ti;
}

static int memory_init(void)
{
struct task_struct *p;
p = get_current();

/* TODO 2: call task_info_alloc for current pid */
ti1 = task_info_alloc(p->pid);

/* TODO 2: call task_info_alloc for parent PID */
p = next_task(p);
ti2 = task_info_alloc(p->pid);

/* TODO 2: call task_info alloc for next process PID */
p = next_task(p);
ti3 = task_info_alloc(p->pid);

/* TODO 2: call task_info_alloc for next process of the next process */
p = next_task(p);
ti4 = task_info_alloc(p->pid);

return 0;
}

static void memory_exit(void)
{
/* TODO 3: print ti* field values */
printk("pid: %d, timestamp: %ld\n", ti1->pid, ti1->timestamp);
printk("pid: %d, timestamp: %ld\n", ti2->pid, ti2->timestamp);
printk("pid: %d, timestamp: %ld\n", ti3->pid, ti3->timestamp);
printk("pid: %d, timestamp: %ld\n", ti4->pid, ti4->timestamp);

/* TODO 4: free ti* structures */
kfree(ti1);
kfree(ti2);
kfree(ti3);
kfree(ti4);
}

module_init(memory_init);
module_exit(memory_exit);
3 changes: 3 additions & 0 deletions tools/labs/skels/kernel_api/4-list/Kbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ccflags-y = -Wno-unused-function -Wno-unused-label -Wno-unused-variable

obj-m = list.o
101 changes: 101 additions & 0 deletions tools/labs/skels/kernel_api/4-list/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Kernel API lab
*
* list.c: Working with lists
*
*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/sched/signal.h>

MODULE_DESCRIPTION("Use list to process task info");
MODULE_AUTHOR("SO2");
MODULE_LICENSE("GPL");

struct task_info {
pid_t pid;
unsigned long timestamp;
struct list_head list;
};

static struct list_head head;

static struct task_info *task_info_alloc(int pid)
{
struct task_info *ti;

ti = kmalloc(sizeof(*ti), GFP_KERNEL);
if (ti == NULL)
return NULL;
ti->pid = pid;
ti->timestamp = jiffies;

return ti;
}

static void task_info_add_to_list(int pid)
{
struct task_info *ti;

/* TODO 1: Allocate task_info and add it to list */
ti = task_info_alloc(pid);
list_add(&ti->list, &head);
}

static void task_info_add_for_current(void)
{
/* Add current, parent, next and next of next to the list */
task_info_add_to_list(current->pid);
task_info_add_to_list(current->parent->pid);
task_info_add_to_list(next_task(current)->pid);
task_info_add_to_list(next_task(next_task(current))->pid);
}

static void task_info_print_list(const char *msg)
{
struct list_head *p;
struct task_info *ti;

pr_info("%s: [ ", msg);
list_for_each(p, &head) {
ti = list_entry(p, struct task_info, list);
pr_info("(%d, %lu) ", ti->pid, ti->timestamp);
}
pr_info("]\n");
}

static void task_info_purge_list(void)
{
struct list_head *p, *q;
struct task_info *ti;

/* TODO 2: Iterate over the list and delete all elements */
list_for_each_safe(p, q, &head)
{
ti = list_entry(p, struct task_info, list);
list_del(p);
kfree(ti);
}
}

static int list_init(void)
{
INIT_LIST_HEAD(&head);

task_info_add_for_current();

return 0;
}

static void list_exit(void)
{
task_info_print_list("before exiting");
task_info_purge_list();
}

module_init(list_init);
module_exit(list_exit);
3 changes: 3 additions & 0 deletions tools/labs/skels/kernel_api/5-list-full/Kbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ccflags-y = -Wno-unused-function -Wno-unused-label -Wno-unused-variable

obj-m = list-full.o
Loading