Skip to content

Commit 8a873b9

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

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33

44
#include <net/sock.h>
55

6+
#define RECV_BUFFER_SIZE 4096
7+
extern mempool_t *http_buf_pool;
8+
69
struct http_server_param {
710
struct socket *listen_socket;
811
};
912

1013
extern int http_server_daemon(void *arg);
1114

15+
static inline void *http_buf_alloc(gfp_t gfp_mask, void *pool_data)
16+
{
17+
return kzalloc(RECV_BUFFER_SIZE, gfp_mask);
18+
}
19+
20+
static inline void http_buf_free(void *element, void *pool_data)
21+
{
22+
kfree(element);
23+
}
1224
#endif

main.c

Lines changed: 10 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);
@@ -154,6 +159,11 @@ static void close_listen_socket(struct socket *socket)
154159

155160
static int __init khttpd_init(void)
156161
{
162+
if (!(http_buf_pool = mempool_create(POOL_MIN_NR, http_buf_alloc,
163+
http_buf_free, NULL))) {
164+
pr_err("failed to create mempool\n");
165+
return -ENOMEM;
166+
}
157167
int err = open_listen_socket(port, backlog, &listen_socket);
158168
if (err < 0) {
159169
pr_err("can't open listen socket\n");

0 commit comments

Comments
 (0)