kernel에서 kmalloc으로 메모리 할당 할때 입니다.

tajja78의 이미지

안녕하세요...

커널쪽을 관심있게 보기 시작한 사람입니다.
간단한 모듈을 하나 만들어서 돌려 보는중에 메모리 할당 부분에서 계속 죽는문제가 생기는데요...
왜 죽는지 알길이 없어 고수님들의 도움을 요청 합니다.
아래는 문제가 발생하고 있는 코드 일부 입니다.

 
typedef struct A_t {
    unsigned int a;
    unsigned int aa;
    unsigned int aaa;
    unsigned int aaaa;
}A;
 
typedef struct B_t {
    A a_t;
    struct ethhdr ether;
    unsigned char data[1500];
}B;
 
typedef struct C_t {
    B b_t;
    struct list_head list;
}C;
 
void test()
{
   C *c_t = (C *)kmalloc(sizeof(C), GFP_KERNEL | GFP_DMA);
   if(NULL == c_t) return;
 
   ......
}
 
위 test 함수가 실행되면 디버그 메세지가 아래 처럼 나옵니다. 
 
=> BUG: unable to handle kernel paging request at ffff8801db8b53ff (주소값은 수시로 변함) 
=> IP: [<ffffffffa020b79f>] test+0x7f/0xd0 [testmodule]
=> PGD 1c06063 PUD 0
=> Oops: 0000 [#4] SMP
=> CPU 0
....
 
kernel에서 메모리 할당을 잘못한건지 알수가 없네요. 
익명 사용자의 이미지

커널은 일반적인 응용 프로그램과 달리 제약 조건이 많습니다.
위의 경우라면 아무래도 ISR 같은 곳에서 메모리 할당을 시도한 것으로 보입니다.
커널의 메모리 할당 함수는 paging이 불가능한 상황에서는 동작하지 않습니다.
굳이 동적할당이 필요하다면 kernel thread에서 작업을 진행해야 합니다.

tajja78의 이미지

일단 thread 돌려서 다시 해봐야 겠군요.. ㅎ

bushi의 이미지

paging 이라는 용어를 어떤 목적으로 쓰셨는지 아리송하지만...

GFP_KERNEL 은 가용 메모리가 모자랄 경우 alloc 을 대기시키고 메모리 정리를 시도합니다.
각종 cache(filesystem, blockdevice...) 가 날아갈 수도 있고, swap 이 발생할 수도 있습니다.
다시말해 sleep 이 가능한 상태에서만 이 플래그를 사용해야 합니다.

GFP_ATOMIC 은 가용 메모리가 모자랄 경우 곧바로 null 리턴합니다.

kernel config 에서 몇가지 검사 옵션을 켜면,
sleep 불가능한 상황에서 GFP_KERNEL 을 사용하는 경우에 친절하게 panic 을 내주면서 stack trace 를 보여줍니다.
추적해서 잠재적인 버그를 수정하라는 뜻이죠.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.