Kernel Source Reading (V) Process ID

In the next section, we continue to learn process ID.
In the previous section, we mentioned that node is a hash element, which has not been explained much. Here we give a more detailed description.

This hash table is designed to find PID structure instances of PID arrays corresponding to specified PID values in a given namespace.
static struct hlist_head *pid_hash;
The hlist_head above is a standard data structure for the kernel, which is used to create a two-way hash table.
pid_hash is an array of hlist_head, global pid hash table, the number of buckets is between 16 and 4096, which is determined by the available memory of the system. pidhash_init() is used to calculate and match the appropriate memory.

If we have assigned a new pid instance and set the ID type, we can associate it with the process using the following functions.

int fastcall attach_pid(struct task_struct *task, enum pid_type type,
        struct pid *pid)
{
    struct pid_link *link;

    /* Establishing the relationship between task_struct and pid */
    link = &task->pids[type];
    link->pid = pid;
    /* Establishing the relationship between pid and task_struct */
    hlist_add_head_rcu(&link->node, &pid->tasks[type]);

    return 0;
}

Next we will focus on how to obtain local ids, such as task_pid, task_tgid and so on, through the data structure of the previous section, and the conversion process between the local number ID of the namespace and task_struct.

struct task_struct *find_task_by_pid_type_ns(int type, int nr,
        struct pid_namespace *ns)
{
    return pid_task(find_pid_ns(nr, ns), type);
}

EXPORT_SYMBOL(find_task_by_pid_type_ns);

/**
 * Finding tasks through global pid
 */
struct task_struct *find_task_by_pid(pid_t nr)
{
    return find_task_by_pid_type_ns(PIDTYPE_PID, nr, &init_pid_ns);
}
EXPORT_SYMBOL(find_task_by_pid);

/**
 * In the namespace of the current process, a process that finds a specific process number
 */
struct task_struct *find_task_by_vpid(pid_t vnr)
{
    return find_task_by_pid_type_ns(PIDTYPE_PID, vnr,
            current->nsproxy->pid_ns);
}
EXPORT_SYMBOL(find_task_by_vpid);

/**
 * Find processes in namespaces based on id
 */
struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
{
    return find_task_by_pid_type_ns(PIDTYPE_PID, nr, ns);
}
EXPORT_SYMBOL(find_task_by_pid_ns);

struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
{
    struct pid *pid;
    rcu_read_lock();
    pid = get_pid(task->pids[type].pid);
    rcu_read_unlock();
    return pid;
}
pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
{
    return pid_nr_ns(task_pid(tsk), ns);
}
EXPORT_SYMBOL(task_pid_nr_ns);

pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
{
    return pid_nr_ns(task_tgid(tsk), ns);
}
EXPORT_SYMBOL(task_tgid_nr_ns);

pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
{
    return pid_nr_ns(task_pgrp(tsk), ns);
}
EXPORT_SYMBOL(task_pgrp_nr_ns);

pid_t task_session_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
{
    return pid_nr_ns(task_session(tsk), ns);
}
EXPORT_SYMBOL(task_session_nr_ns);
struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
{
    struct pid *pid;
    rcu_read_lock();
    pid = get_pid(task->pids[type].pid);
    rcu_read_unlock();
    return pid;
}

struct task_struct *fastcall get_pid_task(struct pid *pid, enum pid_type type)
{
    struct task_struct *result;
    rcu_read_lock();
    result = pid_task(pid, type);
    if (result)
        get_task_struct(result);
    rcu_read_unlock();
    return result;
}

How to generate a unique PID is described below.
The kernel uses a large bitmap to manage and track the PID. Each PID is marked by a bit, idle 0, and vice versa.

It is important to note that when PID allocation is used to establish a new process, local PID must be generated because the process may be visible in the dogmatic space. This needs to be processed in alloc_pid(), and then alloc_pidmap() can be called to allocate the pid. The same is true when released.

struct pid *alloc_pid(struct pid_namespace *ns)
{
    struct pid *pid;
    enum pid_type type;
    int i, nr;
    struct pid_namespace *tmp;
    struct upid *upid;

    pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
    if (!pid)
        goto out;

    tmp = ns;
    for (i = ns->level; i >= 0; i--) {
        nr = alloc_pidmap(tmp);
        if (nr < 0)
            goto out_free;

        pid->numbers[i].nr = nr;
        pid->numbers[i].ns = tmp;
        tmp = tmp->parent;
    }

    get_pid_ns(ns);
    pid->level = ns->level;
    atomic_set(&pid->count, 1);
    for (type = 0; type < PIDTYPE_MAX; ++type)
        INIT_HLIST_HEAD(&pid->tasks[type]);

    spin_lock_irq(&pidmap_lock);
    for (i = ns->level; i >= 0; i--) {
        upid = &pid->numbers[i];
        hlist_add_head_rcu(&upid->pid_chain,
                &pid_hash[pid_hashfn(upid->nr, upid->ns)]);
    }
    spin_unlock_irq(&pidmap_lock);

out:
    return pid;

out_free:
    for (i++; i <= ns->level; i++)
        free_pidmap(pid->numbers[i].ns, pid->numbers[i].nr);

    kmem_cache_free(ns->pid_cachep, pid);
    pid = NULL;
    goto out;
}
/**
 * In a namespace, find and assign an available pid Number
 */
static int alloc_pidmap(struct pid_namespace *pid_ns)
{
    int i, offset, max_scan, pid, last = pid_ns->last_pid;
    struct pidmap *map;

    pid = last + 1;
    if (pid >= pid_max)
        pid = RESERVED_PIDS;
    offset = pid & BITS_PER_PAGE_MASK;
    map = &pid_ns->pidmap[pid/BITS_PER_PAGE];
    max_scan = (pid_max + BITS_PER_PAGE - 1)/BITS_PER_PAGE - !offset;
    for (i = 0; i <= max_scan; ++i) {
        if (unlikely(!map->page)) {
            void *page = kzalloc(PAGE_SIZE, GFP_KERNEL);
            /*
             * Free the page if someone raced with us
             * installing it:
             */
            spin_lock_irq(&pidmap_lock);
            if (map->page)
                kfree(page);
            else
                map->page = page;
            spin_unlock_irq(&pidmap_lock);
            if (unlikely(!map->page))
                break;
        }
        if (likely(atomic_read(&map->nr_free))) {
            do {
                if (!test_and_set_bit(offset, map->page)) {
                    atomic_dec(&map->nr_free);
                    pid_ns->last_pid = pid;
                    return pid;
                }
                offset = find_next_offset(map, offset);
                pid = mk_pid(pid_ns, map, offset);
            /*
             * find_next_offset() found a bit, the pid from it
             * is in-bounds, and if we fell back to the last
             * bitmap block and the final block was the same
             * as the starting point, pid is before last_pid.
             */
            } while (offset < BITS_PER_PAGE && pid < pid_max &&
                    (i != max_scan || pid < last ||
                        !((last+1) & BITS_PER_PAGE_MASK)));
        }
        if (map < &pid_ns->pidmap[(pid_max-1)/BITS_PER_PAGE]) {
            ++map;
            offset = 0;
        } else {
            map = &pid_ns->pidmap[0];
            offset = RESERVED_PIDS;
            if (unlikely(last == offset))
                break;
        }
        pid = mk_pid(pid_ns, map, offset);
    }
    return -1;
}
fastcall void free_pid(struct pid *pid)
{
    /* We can be called with write_lock_irq(&tasklist_lock) held */
    int i;
    unsigned long flags;

    spin_lock_irqsave(&pidmap_lock, flags);
    for (i = 0; i <= pid->level; i++)
        hlist_del_rcu(&pid->numbers[i].pid_chain);
    spin_unlock_irqrestore(&pidmap_lock, flags);

    for (i = 0; i <= pid->level; i++)
        free_pidmap(pid->numbers[i].ns, pid->numbers[i].nr);

    call_rcu(&pid->rcu, delayed_put_pid);
}
/**
 * Release an available pid number in the namespace
 */
static fastcall void free_pidmap(struct pid_namespace *pid_ns, int pid)
{
    struct pidmap *map = pid_ns->pidmap + pid / BITS_PER_PAGE;
    int offset = pid & BITS_PER_PAGE_MASK;

    clear_bit(offset, map->page);
    atomic_inc(&map->nr_free);
}

Posted by sasori on Mon, 03 Jun 2019 20:32:31 -0700