lkml.org 
[lkml]   [2022]   [Aug]   [16]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    From
    Subject[PATCH v5 2/3] sched: Provide copy_user_cpus_mask() to copy out user mask
    Date
    Since accessing the content of the user_cpus_ptr requires lock protection
    to ensure its validity, provide a helper function copy_user_cpus_mask()
    to facilitate its reading.

    Suggested-by: Peter Zijlstra <peterz@infradead.org>
    Signed-off-by: Waiman Long <longman@redhat.com>
    ---
    include/linux/sched.h | 1 +
    kernel/sched/core.c | 19 +++++++++++++++++++
    2 files changed, 20 insertions(+)

    diff --git a/include/linux/sched.h b/include/linux/sched.h
    index e7b2f8a5c711..f2b0340c094e 100644
    --- a/include/linux/sched.h
    +++ b/include/linux/sched.h
    @@ -1830,6 +1830,7 @@ extern int task_can_attach(struct task_struct *p, const struct cpumask *cs_effec
    extern void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask);
    extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask);
    extern int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, int node);
    +extern struct cpumask *copy_user_cpus_mask(struct task_struct *p, struct cpumask *user_mask);
    extern void release_user_cpus_ptr(struct task_struct *p);
    extern int dl_task_check_affinity(struct task_struct *p, const struct cpumask *mask);
    extern void force_compatible_cpus_allowed_ptr(struct task_struct *p);
    diff --git a/kernel/sched/core.c b/kernel/sched/core.c
    index 03053eebb22e..a0987784913e 100644
    --- a/kernel/sched/core.c
    +++ b/kernel/sched/core.c
    @@ -2618,6 +2618,25 @@ void release_user_cpus_ptr(struct task_struct *p)
    kfree(clear_user_cpus_ptr(p));
    }

    +/*
    + * Return the copied mask pointer or NULL if user mask not available.
    + * Acquiring pi_lock for read access protection.
    + */
    +struct cpumask *copy_user_cpus_mask(struct task_struct *p,
    + struct cpumask *user_mask)
    +{
    + struct cpumask *mask = NULL;
    + unsigned long flags;
    +
    + raw_spin_lock_irqsave(&p->pi_lock, flags);
    + if (p->user_cpus_ptr) {
    + cpumask_copy(user_mask, p->user_cpus_ptr);
    + mask = user_mask;
    + }
    + raw_spin_unlock_irqrestore(&p->pi_lock, flags);
    + return mask;
    +}
    +
    /*
    * This function is wildly self concurrent; here be dragons.
    *
    --
    2.31.1
    \
     
     \ /
      Last update: 2022-08-16 21:29    [W:2.105 / U:0.036 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site