[질문] 리눅스 커널안의 Memory Barrier 에 관해서

kernelnewbie의 이미지

안녕하세요 ? 리눅스 커널의 memory barrier 사용과 관련된 코드에 대한 질문입니다.

memory barrier 를 사용한 부분이 Kernel 2.6 의 timer.c 에 아래와 같이 있습니다.

그런데 좀 잘 이해가 되지 않습니다.

smp_rmb(); 구문 밑에 있는 parent = me->group_leader->real_parent; 다음으로 pid=parent->tgid;가

올수 있는 상황이 발생한다고 생각되는데 이러한 상황이 발생하는 경우가 가능한지도 잘 모르겠고

아무튼 좀 두서가 없는데 좀 명확히 아시는분 설명 부탁드립니다.

 /*
 * Accessing ->group_leader->real_parent is not SMP-safe, it could
 * change from under us. However, rather than getting any lock
 * we can use an optimistic algorithm: get the parent
 * pid, and go back and check that the parent is still
 * the same. If it has changed (which is extremely unlikely
 * indeed), we just try again..
 *
 * NOTE! This depends on the fact that even if we _do_
 * get an old value of "parent", we can happily dereference
 * the pointer (it was and remains a dereferencable kernel pointer
 * no matter what): we just can't necessarily trust the result
 * until we know that the parent pointer is valid.
 *
 * NOTE2: ->group_leader never changes from under us.
 */
asmlinkage long sys_getppid(void)
{
	int pid;
	struct task_struct *me = current;
	struct task_struct *parent;
 
	parent = me->group_leader->real_parent;
	for (;;) {
		pid = parent->tgid;
#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
{
		struct task_struct *old = parent;
 
		/*
		 * Make sure we read the pid before re-reading the
		 * parent pointer:
		 */
		smp_rmb();
		parent = me->group_leader->real_parent;
		if (old != parent)
			continue;
}
#endif
		break;
	}
	return pid;
}