당장 Soft IRQ 서비스를 사용하고 싶다면 커널을 수정해서 Soft IRQ 서비스를 추가해야 할까?

아니다. 태스크릿을 이용하면 된다.

태스크릿이란?

태스크릿은 Soft IRQ 서비스 중 하나이다.

const char * const softirq_to_name[NR_SOFTIRQS] = {
	"HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "IRQ_POLL",
	"TASKLET", "SCHED", "HRTIMER", "RCU"
};

위의 코드에서 "TASKLET"가 있다.  HI, TIMER 와 같은 Soft IRQ 서비스이다.

 

태스크릿은 어떻게 실행될까?

태스크릿은 기본적으로 Soft IRQ 서비스이기 때문에 __do_softirq에서 서비스 핸들러가 호출되어 돌아간다.

태스크릿의 핸들러를 등록하는 걸 아래 softirq_init()에서 볼 수 있다.

 

void __init softirq_init(void)
{
...
	open_softirq(TASKLET_SOFTIRQ, tasklet_action);
}

tasklet_action이 태스크릿의 핸들러 되시겠다.

 

static __latent_entropy void tasklet_action(struct softirq_action *a)
{
	tasklet_action_common(a, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
}

tasklet_action은 별로 하는게 없다. tasklet_action_common을 호출해주는 것 밖에 없다.

tasklet_vec는 태스크릿 연결리스트인 tasklet_struct를 가지고 있다.

(tasklet_action_common은 HI Soft IRQ에서도 사용하는 함수다. 같은 방식으로 처리되는 듯)

 

태스크릿 등록

DECLARE_TASKLET()이나 DECLARE_TASKLET_DISABLED()함수로 태스크릿을 등록한다.

 

각 속성은 다음과 같다.

  • name : tasklet_struct 구조체
  • count : 0이면 활성화, 1이면 비활성화
  • callback : 태스크릿 핸들러 함수

이 부분은 책과 다른데, 버전업이 되어서 func, data를 구조체에 지정하는 방식은 쓰지 않는다.

다만 아직 지원은 하고 있다.

callback()은 자기 자신을 인자로 받는다.

 

이렇게 등록을 하면 __tasklet_schedule_common() 에서 tasklet_head 구조체에 저장한다.

static void __tasklet_schedule_common(struct tasklet_struct *t,
				      struct tasklet_head __percpu *headp,
				      unsigned int softirq_nr)
{
	struct tasklet_head *head;
	unsigned long flags;

	local_irq_save(flags);
	head = this_cpu_ptr(headp);
	t->next = NULL;
	*head->tail = t;
	head->tail = &(t->next);
	raise_softirq_irqoff(softirq_nr);
	local_irq_restore(flags);
}

 

tasklet_head는 아래와 같다. 

연결리스트를 썼다. (https://2jun0.tistory.com/44)

struct tasklet_head {
	struct tasklet_struct *head;
	struct tasklet_struct **tail;
};

태스크릿 실행 흐름

  1. __do_softirq() 에서 tasklet_action() 실행
  2. tasklet_action() 에서 tasklet_action_common() 실행
  3. tasklet_action_common() 에서 태스크릿 연결리스트(tasklet_struct)를 탐색하여 서비스 처리

태스크릿도 Soft IRQ이기 때문에 실행이 막히면 ksoftirqd가 처리해준다.

'디버깅을 통해 배우는 리눅스 커널의 구조' 카테고리의 다른 글

스터디 계획(4주차)  (0) 2022.07.02
리눅스 커널 스터디 결산 (3주차)  (0) 2022.07.02
ksoftirqd 스레드  (0) 2022.07.02
스터디 계획(3주차)  (0) 2022.06.25
Soft IRQ  (0) 2022.06.25
2jun0