Skip to content

Commit abffd13

Browse files
committed
Replace kzalloc with mempool to reduce memory fragmentation
Signed-off-by: Nick Huang <[email protected]>
1 parent d3de0a6 commit abffd13

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

http_server.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "http_parser.h"
88
#include "http_server.h"
99

10+
extern mempool_t *http_buf_pool;
11+
1012
#define CRLF "\r\n"
1113

1214
#define HTTP_RESPONSE_200_DUMMY \
@@ -30,7 +32,6 @@
3032
"Content-Type: text/plain" CRLF "Content-Length: 21" CRLF \
3133
"Connection: KeepAlive" CRLF CRLF "501 Not Implemented" CRLF
3234

33-
#define RECV_BUFFER_SIZE 4096
3435

3536
struct http_request {
3637
struct socket *socket;
@@ -165,7 +166,7 @@ static int http_server_worker(void *arg)
165166
allow_signal(SIGKILL);
166167
allow_signal(SIGTERM);
167168

168-
buf = kzalloc(RECV_BUFFER_SIZE, GFP_KERNEL);
169+
buf = mempool_alloc(http_buf_pool, GFP_KERNEL);
169170
if (!buf) {
170171
pr_err("can't allocate memory!\n");
171172
err = -ENOMEM;
@@ -190,7 +191,7 @@ static int http_server_worker(void *arg)
190191
memset(buf, 0, RECV_BUFFER_SIZE);
191192
}
192193
out_free_buf:
193-
kfree(buf);
194+
mempool_free(buf, http_buf_pool);
194195
out:
195196
kernel_sock_shutdown(socket, SHUT_RDWR);
196197
sock_release(socket);

http_server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <net/sock.h>
55

6+
#define RECV_BUFFER_SIZE 4096
7+
68
struct http_server_param {
79
struct socket *listen_socket;
810
};

main.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22

33
#include <linux/kthread.h>
4+
#include <linux/mempool.h>
45
#include <linux/sched/signal.h>
6+
#include <linux/slab.h>
57
#include <linux/tcp.h>
68
#include <linux/version.h>
79
#include <net/sock.h>
@@ -10,6 +12,9 @@
1012

1113
#define DEFAULT_PORT 8081
1214
#define DEFAULT_BACKLOG 100
15+
#define POOL_MIN_NR 4
16+
17+
mempool_t *http_buf_pool;
1318

1419
static ushort port = DEFAULT_PORT;
1520
module_param(port, ushort, S_IRUGO);
@@ -152,8 +157,26 @@ static void close_listen_socket(struct socket *socket)
152157
sock_release(socket);
153158
}
154159

160+
161+
static void *http_buf_alloc(gfp_t gfp_mask, void *pool_data)
162+
{
163+
return kzalloc(RECV_BUFFER_SIZE, gfp_mask);
164+
}
165+
166+
static void http_buf_free(void *element, void *pool_data)
167+
{
168+
kfree(element);
169+
}
170+
155171
static int __init khttpd_init(void)
156172
{
173+
if (!(http_buf_pool = mempool_create(POOL_MIN_NR, http_buf_alloc,
174+
http_buf_free, NULL))) {
175+
pr_err("failed to create mempool\n");
176+
return -ENOMEM;
177+
}
178+
179+
157180
int err = open_listen_socket(port, backlog, &listen_socket);
158181
if (err < 0) {
159182
pr_err("can't open listen socket\n");

0 commit comments

Comments
 (0)