add task 13

This commit is contained in:
rick 2024-02-04 23:07:41 +01:00
parent d595cdf771
commit 1136d7dd57
Signed by: Rick
GPG key ID: 5CBE8779CD27BCBA

113
README.md
View file

@ -302,6 +302,119 @@ plus que ça :
* [Chapitre 11, Linux Device Drivers, 3rd Edition](https://lwn.net/Kernel/LDD3/)
### Tache 13
Il faut mettre un constructeur de manière exceptionnel. Cela permet de l'afficher séparemment dans le fichier `/proc/slabinfo`, sinon Linux va optimiser ça en le regroupant avec un autre cache.
Voici le patch :
```diff
From 4b55e26775647e71c86164aed41d82beeb17febe Mon Sep 17 00:00:00 2001
From: rick <rick@gnous.eu>
Date: Sun, 4 Feb 2024 23:02:32 +0100
Subject: [PATCH] add task 13
---
task_12/list.c | 28 ++++++++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/task_12/list.c b/task_12/list.c
index b5fa8fa..a7989be 100644
--- a/task_12/list.c
+++ b/task_12/list.c
@@ -4,6 +4,7 @@
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
+#include <linux/slab.h>
#include <linux/string.h>
#define NAME_SIZE 20
@@ -16,11 +17,13 @@ struct identity {
};
static struct list_head identity_list;
+static struct kmem_cache *cache;
static int identity_create(char *name, int id)
{
int ret = 0;
- struct identity *new = kmalloc(sizeof(*new), GFP_KERNEL);
+ struct identity *new = (struct identity *)kmem_cache_alloc(cache,
+ GFP_KERNEL);
if (!new) {
ret = -ENOMEM;
@@ -63,15 +66,31 @@ static void identity_destroy(int id)
if (tmp) {
list_del(&tmp->list);
- kfree(tmp);
+ kmem_cache_free(cache, tmp);
}
}
+/*
+ * juste pour pouvoir être dans le slabinfo, sinon Linux le fusionne avec un
+ * autre qui a des propriétés similaires.
+ */
+static void my_constructor(void *ptr)
+{
+ memset(ptr, 0, sizeof(struct identity));
+}
+
static int __init my_init(void)
{
struct identity *temp;
pr_info("Coucou le gens !!!!\n");
+ cache =
+ kmem_cache_create("eudyptula_identity", sizeof(struct identity), 0,
+ SLAB_HWCACHE_ALIGN, my_constructor);
+
+ if (!cache)
+ return -ENOMEM;
+
INIT_LIST_HEAD(&identity_list);
if (identity_create("Alice", 1))
@@ -86,8 +105,8 @@ static int __init my_init(void)
temp = identity_find(3);
if (!temp)
pr_info("id 3 not found\n");
- else
- pr_info("id 3 = %s\n", temp->name);
+ else
+ pr_info("id 3 = %s\n", temp->name);
temp = identity_find(42);
if (!temp)
@@ -103,6 +122,7 @@ static int __init my_init(void)
static void __exit my_exit(void)
{
+ kmem_cache_destroy(cache);
pr_info("Tschuss !!!\n");
}
--
2.43.0
```
Et la sortie :
```bash
insmod list.ko
grep eud /proc/slabinfo
# eudyptula_identity 64 64 64 64 1 : tunables 0 0 0 : slabdata 1 1 0
dmesg
# [1246412.478052] Coucou le gens !!!!
# [1246412.478108] id 3 = Dave
# [1246412.478112] id 42 not found
# [1246450.284783] Tschuss !!!
```
* [Chapitre 8, Linux Device Drivers, 3rd Edition](https://lwn.net/Kernel/LDD3/)
## Informations diverses
Je liste dans cette section quelques informations que j'ai pu découvrir en