Skip to content

Replace kzalloc with mempool to reduce memory fragmentation #19

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

Open
wants to merge 1 commit 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
7 changes: 4 additions & 3 deletions http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "http_parser.h"
#include "http_server.h"

extern mempool_t *http_buf_pool;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use extern here. Honor the header instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue has been fixed. Thanks, professor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue has been fixed.

No, you didn't.


#define CRLF "\r\n"

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

#define RECV_BUFFER_SIZE 4096

struct http_request {
struct socket *socket;
Expand Down Expand Up @@ -165,7 +166,7 @@ static int http_server_worker(void *arg)
allow_signal(SIGKILL);
allow_signal(SIGTERM);

buf = kzalloc(RECV_BUFFER_SIZE, GFP_KERNEL);
buf = mempool_alloc(http_buf_pool, GFP_KERNEL);
if (!buf) {
pr_err("can't allocate memory!\n");
err = -ENOMEM;
Expand All @@ -190,7 +191,7 @@ static int http_server_worker(void *arg)
memset(buf, 0, RECV_BUFFER_SIZE);
}
out_free_buf:
kfree(buf);
mempool_free(buf, http_buf_pool);
out:
kernel_sock_shutdown(socket, SHUT_RDWR);
sock_release(socket);
Expand Down
12 changes: 12 additions & 0 deletions http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@

#include <net/sock.h>

#define RECV_BUFFER_SIZE 4096
extern mempool_t *http_buf_pool;

struct http_server_param {
struct socket *listen_socket;
};

extern int http_server_daemon(void *arg);

static inline void *http_buf_alloc(gfp_t gfp_mask, void *pool_data)
{
return kzalloc(RECV_BUFFER_SIZE, gfp_mask);
}

static inline void http_buf_free(void *element, void *pool_data)
{
kfree(element);
}
#endif
10 changes: 10 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/kthread.h>
#include <linux/mempool.h>
#include <linux/sched/signal.h>
#include <linux/slab.h>
#include <linux/tcp.h>
#include <linux/version.h>
#include <net/sock.h>
Expand All @@ -10,6 +12,9 @@

#define DEFAULT_PORT 8081
#define DEFAULT_BACKLOG 100
#define POOL_MIN_NR 4

mempool_t *http_buf_pool;

static ushort port = DEFAULT_PORT;
module_param(port, ushort, S_IRUGO);
Expand Down Expand Up @@ -154,6 +159,11 @@ static void close_listen_socket(struct socket *socket)

static int __init khttpd_init(void)
{
if (!(http_buf_pool = mempool_create(POOL_MIN_NR, http_buf_alloc,
http_buf_free, NULL))) {
pr_err("failed to create mempool\n");
return -ENOMEM;
}
int err = open_listen_socket(port, backlog, &listen_socket);
if (err < 0) {
pr_err("can't open listen socket\n");
Expand Down