-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat(kmod): reuse mempool #451
Closed
+190
−34
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
#pragma once | ||
#include <kunit/test.h> | ||
|
||
#define TEST_CASES_NEW_SHM \ | ||
KUNIT_CASE(test_case_new_shm_normal), KUNIT_CASE(test_case_new_shm_many), \ | ||
KUNIT_CASE(test_case_new_shm_not_aligned), KUNIT_CASE(test_case_new_shm_twice) | ||
#define TEST_CASES_NEW_SHM \ | ||
KUNIT_CASE(test_case_new_shm_normal), KUNIT_CASE(test_case_new_shm_too_big), \ | ||
KUNIT_CASE(test_case_new_shm_too_many), KUNIT_CASE(test_case_new_shm_not_aligned), \ | ||
KUNIT_CASE(test_case_new_shm_twice) | ||
|
||
void test_case_new_shm_normal(struct kunit * test); | ||
void test_case_new_shm_many(struct kunit * test); | ||
void test_case_new_shm_too_big(struct kunit * test); | ||
void test_case_new_shm_too_many(struct kunit * test); | ||
void test_case_new_shm_not_aligned(struct kunit * test); | ||
void test_case_new_shm_twice(struct kunit * test); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#include "memory_allocator.h" | ||
|
||
#include <linux/module.h> | ||
|
||
MODULE_LICENSE("Dual BSD/GPL"); | ||
|
||
#define MEMPOOL_128MB_NUM 1000 | ||
#define MEMPOOL_1GB_NUM 100 | ||
#define MEMPOOL_8GB_NUM 10 | ||
|
||
static uint64_t mempool_size_128mb = 134217728; // 128 * 1024 * 1024 | ||
static uint64_t mempool_size_1gb = 1073741824; // 1 * 1024 * 1024 * 1024 | ||
static uint64_t mempool_size_8gb = 8589934592; // 8 * 1024 * 1024 * 1024 | ||
|
||
static pid_t mempool_128mb_pid[MEMPOOL_128MB_NUM]; | ||
static pid_t mempool_1gb_pid[MEMPOOL_1GB_NUM]; | ||
static pid_t mempool_8gb_pid[MEMPOOL_8GB_NUM]; | ||
|
||
static uint64_t mempool_128mb_addr[MEMPOOL_128MB_NUM]; | ||
static uint64_t mempool_1gb_addr[MEMPOOL_1GB_NUM]; | ||
static uint64_t mempool_8gb_addr[MEMPOOL_8GB_NUM]; | ||
|
||
void agnocast_init_memory_allocator(void) | ||
{ | ||
// TODO(Ryuta Kambe): currently, we assume that the address from 0x40000000000 to 0x8B000000000 is | ||
// available | ||
uint64_t addr = 0x40000000000; | ||
|
||
for (int i = 0; i < MEMPOOL_128MB_NUM; i++) { | ||
mempool_128mb_pid[i] = 0; | ||
mempool_128mb_addr[i] = addr; | ||
addr += mempool_size_128mb; | ||
} | ||
|
||
for (int i = 0; i < MEMPOOL_1GB_NUM; i++) { | ||
mempool_1gb_pid[i] = 0; | ||
mempool_1gb_addr[i] = addr; | ||
addr += mempool_size_1gb; | ||
} | ||
|
||
for (int i = 0; i < MEMPOOL_8GB_NUM; i++) { | ||
mempool_8gb_pid[i] = 0; | ||
mempool_8gb_addr[i] = addr; | ||
addr += mempool_size_8gb; | ||
} | ||
} | ||
|
||
uint64_t agnocast_assign_memory(const pid_t pid, const uint64_t size) | ||
{ | ||
if (size <= mempool_size_128mb) { | ||
for (int i = 0; i < MEMPOOL_128MB_NUM; i++) { | ||
if (mempool_128mb_pid[i] == 0) { | ||
mempool_128mb_pid[i] = pid; | ||
return mempool_128mb_addr[i]; | ||
} | ||
} | ||
} | ||
|
||
if (size <= mempool_size_1gb) { | ||
for (int i = 0; i < MEMPOOL_1GB_NUM; i++) { | ||
if (mempool_1gb_pid[i] == 0) { | ||
mempool_1gb_pid[i] = pid; | ||
return mempool_1gb_addr[i]; | ||
} | ||
} | ||
} | ||
|
||
if (size <= mempool_size_8gb) { | ||
for (int i = 0; i < MEMPOOL_8GB_NUM; i++) { | ||
if (mempool_8gb_pid[i] == 0) { | ||
mempool_8gb_pid[i] = pid; | ||
return mempool_8gb_addr[i]; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int agnocast_free_memory(const pid_t pid) | ||
{ | ||
for (int i = 0; i < MEMPOOL_128MB_NUM; i++) { | ||
if (mempool_128mb_pid[i] == pid) { | ||
mempool_128mb_pid[i] = 0; | ||
return 0; | ||
} | ||
} | ||
|
||
for (int i = 0; i < MEMPOOL_1GB_NUM; i++) { | ||
if (mempool_1gb_pid[i] == pid) { | ||
mempool_1gb_pid[i] = 0; | ||
return 0; | ||
} | ||
} | ||
|
||
for (int i = 0; i < MEMPOOL_8GB_NUM; i++) { | ||
if (mempool_8gb_pid[i] == pid) { | ||
mempool_8gb_pid[i] = 0; | ||
return 0; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
void agnocast_exit_memory_allocator(void) | ||
{ | ||
for (int i = 0; i < MEMPOOL_128MB_NUM; i++) { | ||
mempool_128mb_pid[i] = 0; | ||
} | ||
|
||
for (int i = 0; i < MEMPOOL_1GB_NUM; i++) { | ||
mempool_1gb_pid[i] = 0; | ||
} | ||
|
||
for (int i = 0; i < MEMPOOL_8GB_NUM; i++) { | ||
mempool_8gb_pid[i] = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
#include <linux/types.h> | ||
|
||
void agnocast_init_memory_allocator(void); | ||
uint64_t agnocast_assign_memory(const pid_t pid, const uint64_t size); | ||
int agnocast_free_memory(const pid_t pid); | ||
void agnocast_exit_memory_allocator(void); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
現状のAutowareだとありえないケースだと思いますが、publisherがexitした後にsubscriberが生き残り続けた場合subscriberはまだ該当の仮想アドレス範囲を共有メモリにmapした状態で参照を続けています。その上で、新たに参加したpublisherがたまたまその仮想アドレス範囲をassignされてしまい、しかもsubscriberがそのpublisherのpublishするtopicをsubscribeしようとすると仮想アドレス範囲がかぶってmmapでエラーになってしまいます。
これを回避するにはfree_memoryを行うのを「該当プロセスがexitした」かつ「すべてのmapped_pids上のsubscriberプロセスがexitした」ことを確認の上で行う必要がありそうです。幸いこのmapped_pidsという配列がprocess_infoのstruct上に今はあるのでやろうと思えばできそうなんで、一応やっとくというのはどうでしょう?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
たしかにそうですね、やります。