리눅스/커널2012. 7. 20. 21:25
cache allocator 의 핵심:
1) 메모리를 각각 다른 사이즈의 오브젝트 들끼리 모아놓은 pool 들로 나누는 것
2) 메모리 할당에 대한 request가 오면 1)에서 맞는 사이즈를 가진 pool을 리턴

이런 pool들을 cache하고 하며 kmem_cache_t 의 타입으로 선언되어 있다. kmem_cache_t에는 cache에 대한 여러가지 정보를 담을 수 있는 변수들이 준비되어 있다.
kmem_cache_create()를 통하여 cache를 생성할 수 있다.
kmem_cache_create()는 caller에서 넘겨중 사이즈만한 cache를 생성하며 cache_cache라는 생성된 cache들의 global list에 넣는다.

cache_cache는 static kmem_cache_t 타입이며 시스템 전반적으로 cache들을 가지고 있다. 따라서 pool이 속해있는 object들의 사이즈는 sizeof(kmem_cache_t) 이다. 시스템 전반에 걸친 cache들에 대한 정보는 /proc/slabinfo에서 볼 수 있다. cache_cache외에도 cache들의 array 형태로 되어있는 일반적인 cache들이 있다. array마다 2배수로 커지는 사이즈 형태이며 각 사이즈별로 2개의 cache(일반 cache, DMA용 cache)가 있다. kmalloc()은 이 일반적인 cache들에서 맞는 사이즈의 cache를 찾고 __kmem_cache_alloc()으로 cache 오브젝트을 caller에게 넘겨주는 일을 한다.
유사하게 kfree() 은 __kmem_cache_free()으로 오브젝트를 cache에 돌려준다.

iPhone 에서 작성된 글입니다.

'리눅스 > 커널' 카테고리의 다른 글

boot memory allocator 의 필요성  (0) 2012.09.04
defunct 프로세스 죽이기  (0) 2012.08.16
리눅스커널 timestamp 찍는 법  (0) 2012.07.18
__user  (0) 2012.07.01
__initcall(), module_init()  (0) 2012.07.01
Posted by code cat
리눅스/커널2012. 7. 18. 07:42
linux kenel timestamp
--------------------------------------
include
include

char tbuf[50], *tp;
unsigned tlen;
unsigned long long t;
unsigned long nanosec_rem;
int this_cpu = smp_processor_id();

t = cpu_clock(this_cpu);
nanosec_rem = do_div(t, 1000000000);
tlen = sprintf(tbuf, "[%5lu.%06lu] ",
(unsigned long) t,
nanosec_rem / 1000);

iPhone 에서 작성된 글입니다.

'리눅스 > 커널' 카테고리의 다른 글

defunct 프로세스 죽이기  (0) 2012.08.16
kmalloc, cache allocator  (0) 2012.07.20
__user  (0) 2012.07.01
__initcall(), module_init()  (0) 2012.07.01
리눅스 커널 분석 시 아키텍쳐별 tag 및 cscope생성  (0) 2012.04.16
Posted by code cat
프로그래밍/C2012. 7. 3. 17:19

error: expected identifier before numeric constant


위와 같은 에러가 나는 이유

1. include로 헤더파일을 추가했는데 어디선가 같은 이름의 매크로가 꼬였을 때,

해결책 --> (에러 나는 라인의 매크로로 grep을 잡아보자!)


'프로그래밍 > C' 카테고리의 다른 글

include guard에 대해서  (0) 2012.09.24
구조체 초기화 방법  (0) 2012.08.21
정적 라이브러리, 동적 라이브러리 만들기  (0) 2012.04.04
sprintf 사용법  (0) 2012.04.03
sscanf 사용법  (0) 2012.03.28
Posted by code cat

vim에서 검색한 내용 copy & paste 하기


vim에서 search 한 내용만 골라서 copy를 하고 싶을 경우 다음과 같이 하면 된다.


qaq

:g/검색어/y A

:let @+ = @a


처음 라인에서는 레지스터 a를 비운다.  (저 명령어는 매크로를 기록할 때 쓰는 명령어이다.)

두번째 라인에서는 뭐.. 알다시피 검색어를 찾아서 A 레지스터에 넣는다.

세번째 라인에서는 레지스터 A를 클립보드에 카피한다.


vim에서 검색한 내용 지우기


이건 쉽다.


:g/검색어/d


따로 설명할 필요 없으리라 믿는다.



한가지 검색한 거를 제외하고 위의 작업을 하고 싶을 땐 어떻하면 좋을까???


inverse를 떠올리면 된다!


:v/검색어/d


알고나니 참 좋은 기능이다.

'리눅스 > 스크립트/유틸' 카테고리의 다른 글

Code Sourcery 설치하기  (0) 2012.07.31
screen에서 ctrl+s 잘못 눌러서 먹통 됐을 때...  (0) 2012.07.23
samba 로그인 정보 지우기/보기  (0) 2012.06.23
SVN in conflict  (0) 2012.06.21
cpio vs tar vs cp  (0) 2012.06.11
Posted by code cat
리눅스/커널2012. 7. 1. 11:25

__user 로 타입이 정의된 변수는 kernel space 밖에 있다는 걸 의미한다.


예를 들어,


char __user *buff


라고 하면, 이는 user space안에 char *buff라고 선언한 것이다.


이를 이용해 kernel과 user 공간 사이의 communication(예: read/write )을 할 때 편리하게 쓸 수 있다. 


이와 함께 쓰이는 함수가 대표적으로 copy_from_user 와 copy_to_user가 있겠다.



stackoverflow에 보니 아래와 같은 질문이 있었다.

http://stackoverflow.com/questions/4521551/what-are-the-implications-of-the-linux-user-macro


I was hoping someone could explain the nuances of the __user macro used in the linux kernel source.

First of all, the macro:
# define __user __attribute__((noderef, address_space(1)))

Now, after some googling I read that this macro allows one to designate a pointer as belonging to the user address space, and that it should not be dereferenced.

I may be missing some obvious facts, but could someone please explain the implications of such a macro? For instance, what is a good example of where this macro would be of use? Again, forgive me if I am missing something obvious.

To put this in some context, I came accross the macro while examining some USB code (linux/usbdevice_fs.h). I am only looking for a general understanding of this macros( or others like it) use within the kernel.

Thanks for looking!
c macros linux-kernel kernel link|edit|flag asked Dec 23 '10 at 18:46

Mr. Shickadance
1,155624

82% accept rate

2

See do_execve() source for good example. See how argv is used in count(). If you'd simply dereference (*argv[0]) or something, sparse(1) will warn about it. address_space says not all pointers are equal, requiring different (dereference) rules and should not be mixed. – adobriyan Dec 24 '10 at 17:12


2 Answers
active
oldest
votes
up vote
4
down vote
accepted
It allows tools like sparse to tell kernel developers that they're possibly using an untrusted pointer (or a pointer that may be invalid in the current virtual address mapping) improperly.link|edit|flag answered Dec 23 '10 at 18:59

Michael Burr
117k11123313




up vote
3
down vote
I think __user marks user space pointers and tells the developer/system not to trust it. If user gives you "invalid" pointer, then kernel tries to reference it (note that kernel can reference everywhere) and it can corrupt it's own space.

For example in "read"(in you usbdevice_fs.h) should provide you a (__user) buffer to write the result to. So you have to use copy_to_user, but not memcopy, strcpy or anything like this.

Note: This is not formal definition/description, but the only part I'm aware of.


__user로 선언된 변수는 dereference를 할 경우 어떤 동작을 일으킬지 모르는 것 같다.(untrusted pointer라는 문맥)  저렇게 함으로서 개발자(?)에게 __user로 선언된 변수를 deference하지 말라고 표시 하는 거 같다.

Posted by code cat