ฉันกำลังทำงานกับระบบ aarch64 ที่มี RAM 512 MB ใช้ 4.14 และฉันกำลังพยายามใช้กลไกการอัปเดตเฟิร์มแวร์สำหรับส่วนประกอบ USB แต่ขนาดการอัปเดตรูปภาพอยู่ที่ประมาณ 100MB ดังนั้น ฉันต้องสามารถบัฟเฟอร์เปอร์เซ็นต์ที่ดีของ RAM ทั้งหมดที่มีอยู่ในระบบ รวมทั้งสำรองเพิ่มอีกเล็กน้อยเพื่อทำการบีบอัด/ประมวลผล ฯลฯ
ปัญหาคือการจัดสรรหน่วยความจำนั้นแล้วเข้าถึง ดูเหมือนว่าจะทำให้ระบบหยุดทำงานหรือเคอร์เนลแพนิคบนอุปกรณ์ของฉัน
ฉันสามารถมองผ่าน /proc/meminfo ว่าฉันมีหน่วยความจำเพียงพอที่จะตอบสนองการจัดสรร (ประมาณ 400MB) ดังนั้น malloc สำหรับหน่วยความจำนั้นจึงสำเร็จจริง ๆ แต่เมื่อฉันพยายามเข้าถึงบัฟเฟอร์ที่จัดสรรใหม่ จะหยุดระบบของฉันและ บางครั้งสร้างเคอร์เนลแพนิคนี้:
[ 659.987365] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000004
[ 659.987365]
[ 659.996536] CPU: 0 PID: 1 Comm: procd Not tainted 4.14.241 #0
[ 660.002291] Hardware name: dev (DT)
[ 660.006130] Call trace:
[ 660.008589] dump_backtrace+0x0/0x168
[ 660.012259] show_stack+0x14/0x20
[ 660.015581] dump_stack+0xa4/0xc8
[ 660.018900] panic+0x13c/0x298
[ 660.021960] do_exit+0x1a8/0x8d8
[ 660.025193] SyS_exit_group+0x0/0x10
[ 660.028776] get_signal+0x4dc/0x570
[ 660.032270] do_signal+0x54c/0x5b8
[ 660.035676] do_notify_resume+0x80/0x248
[ 660.039604] work_pending+0x8/0x10
[ 660.043012] Kernel Offset: disabled
[ 660.046504] CPU features: 0x0002000
[ 660.049994] Memory Limit: none
[ 660.053054] Rebooting in 3 seconds..
ตัวอย่างโค้ดทั้งสองต่อไปนี้ทำให้เกิดปัญหาที่ฉันพบ:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
volatile bool done = false;
static void handle_exit_signal(int signal) {
(void)signal;
done = true;
}
#define BUF_SIZE 100000000
int main(int argc, char *argv[]) {
char *ptr = NULL;
signal(SIGINT, handle_exit_signal);
signal(SIGTERM, handle_exit_signal);
/* this will always cause problems */
ptr = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_LOCKED, -1, 0);
if (ptr == MAP_FAILED) {
printf("unable to map memory resources: %s\n", strerror(errno));
return -1;
}
memset(ptr, 0xFF, BUF_SIZE);
while(!done);
munmap(ptr, BUF_SIZE);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
volatile bool done = false;
static void handle_exit_signal(int signal) {
(void)signal;
done = true;
}
#define BUF_SIZE 100000000
int main(int argc, char *argv[]) {
char *ptr = NULL;
signal(SIGINT, handle_exit_signal);
signal(SIGTERM, handle_exit_signal);
ptr = malloc(BUF_SIZE);
if (!ptr) {
printf("unable to allocate memory\n");
return -1;
}
/* this is actually where the hang occurs when using malloc */
memset(ptr, 0xFF, BUF_SIZE);
while(!done);
free(ptr);
return 0;
}
ฉันยังพยายามปิดการใช้งานหน่วยความจำที่คอมมิตมากเกินไป (vm.overcommit_memory=2 & vm.overcommit_ratio=100) และดูเหมือนว่าจะไม่มีผลจริงๆ
มีบางอย่างที่ชัดเจนว่าฉันขาดหายไปเมื่อพยายามจัดสรรบัฟเฟอร์ขนาดใหญ่บนแพลตฟอร์มแบบฝังตัวหรือไม่ หรือปัญหาอาจเป็นเรื่องที่ละเอียดอ่อนกว่านี้?
ความช่วยเหลือใด ๆ ที่ชื่นชม