From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DB06EC2BB41 for ; Thu, 18 Aug 2022 15:31:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343619AbiHRPbq (ORCPT ); Thu, 18 Aug 2022 11:31:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243702AbiHRPbo (ORCPT ); Thu, 18 Aug 2022 11:31:44 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8B3A9F0E1 for ; Thu, 18 Aug 2022 08:31:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660836702; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eTTQvzeO8NbT9iEosZSAoBf5nEuQJaIJbAqJp7WZFCk=; b=NpsWZm1Kmfu4yey6P3UXOxN19DlKzdvFKtVal361vZulW30i3P6nTbgj1xHHARzsljid2t gdLiCblqRrUIVk+SYAvNtBdQdSADpXpWAcohB+9MGtgoMdcirtvgDTElSRTMLRM35FW7id CXznb7195JFVJAa+X5QOHjD+nFHEHbs= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-237-wUlIOUvQN0C_769kI5zOOw-1; Thu, 18 Aug 2022 11:31:39 -0400 X-MC-Unique: wUlIOUvQN0C_769kI5zOOw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3B5B12932485; Thu, 18 Aug 2022 15:31:38 +0000 (UTC) Received: from [10.22.32.4] (unknown [10.22.32.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0FA922166B26; Thu, 18 Aug 2022 15:31:24 +0000 (UTC) Message-ID: <81f06470-ad5b-2b92-86d6-dc2ca5d21d53@redhat.com> Date: Thu, 18 Aug 2022 08:31:24 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH v5 1/3] sched: Use user_cpus_ptr for saving user provided cpumask in sched_setaffinity() Content-Language: en-US To: Peter Zijlstra Cc: Ingo Molnar , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Tejun Heo , Zefan Li , Johannes Weiner , Will Deacon , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Linus Torvalds References: <20220816192734.67115-1-longman@redhat.com> <20220816192734.67115-2-longman@redhat.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 8/17/22 04:41, Peter Zijlstra wrote: > On Tue, Aug 16, 2022 at 03:27:32PM -0400, Waiman Long wrote: >> @@ -2981,25 +2969,21 @@ static int restrict_cpus_allowed_ptr(struct task_struct *p, >> goto err_unlock; >> } >> >> - if (!cpumask_and(new_mask, &p->cpus_mask, subset_mask)) { >> + >> + if (p->user_cpus_ptr) >> + not_empty = cpumask_and(new_mask, p->user_cpus_ptr, subset_mask); >> + else >> + not_empty = cpumask_and(new_mask, cpu_online_mask, subset_mask); >> + >> + if (!not_empty) { >> err = -EINVAL; >> goto err_unlock; >> } >> >> - /* >> - * We're about to butcher the task affinity, so keep track of what >> - * the user asked for in case we're able to restore it later on. >> - */ >> - if (user_mask) { >> - cpumask_copy(user_mask, p->cpus_ptr); >> - p->user_cpus_ptr = user_mask; >> - } >> - >> return __set_cpus_allowed_ptr_locked(p, new_mask, 0, rq, &rf); >> >> err_unlock: >> task_rq_unlock(rq, p, &rf); >> - kfree(user_mask); >> return err; >> } >> >> @@ -3049,34 +3033,27 @@ void force_compatible_cpus_allowed_ptr(struct task_struct *p) >> } >> >> static int >> -__sched_setaffinity(struct task_struct *p, const struct cpumask *mask); >> +__sched_setaffinity(struct task_struct *p, const struct cpumask *mask, bool save_mask); >> >> /* >> * Restore the affinity of a task @p which was previously restricted by a >> - * call to force_compatible_cpus_allowed_ptr(). This will clear (and free) >> - * @p->user_cpus_ptr. >> + * call to force_compatible_cpus_allowed_ptr(). >> * >> * It is the caller's responsibility to serialise this with any calls to >> * force_compatible_cpus_allowed_ptr(@p). >> */ >> void relax_compatible_cpus_allowed_ptr(struct task_struct *p) >> { >> - struct cpumask *user_mask = p->user_cpus_ptr; >> - unsigned long flags; >> + const struct cpumask *user_mask = p->user_cpus_ptr; >> + >> + if (!user_mask) >> + user_mask = cpu_online_mask; >> >> /* >> - * Try to restore the old affinity mask. If this fails, then >> - * we free the mask explicitly to avoid it being inherited across >> - * a subsequent fork(). >> + * Try to restore the old affinity mask with __sched_setaffinity(). >> + * Cpuset masking will be done there too. >> */ >> - if (!user_mask || !__sched_setaffinity(p, user_mask)) >> - return; >> - >> - raw_spin_lock_irqsave(&p->pi_lock, flags); >> - user_mask = clear_user_cpus_ptr(p); >> - raw_spin_unlock_irqrestore(&p->pi_lock, flags); >> - >> - kfree(user_mask); >> + __sched_setaffinity(p, user_mask, false); >> } >> >> void set_task_cpu(struct task_struct *p, unsigned int new_cpu) > > Would it not be simpler to write it something like so? > > --- > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 03053eebb22e..cdae4d50a588 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -2955,7 +2955,6 @@ static int restrict_cpus_allowed_ptr(struct task_struct *p, > struct rq_flags rf; > struct rq *rq; > int err; > - bool not_empty; > > rq = task_rq_lock(p, &rf); > > @@ -2969,13 +2968,7 @@ static int restrict_cpus_allowed_ptr(struct task_struct *p, > goto err_unlock; > } > > - > - if (p->user_cpus_ptr) > - not_empty = cpumask_and(new_mask, p->user_cpus_ptr, subset_mask); > - else > - not_empty = cpumask_and(new_mask, cpu_online_mask, subset_mask); > - > - if (!not_empty) { > + if (!cpumask_and(new_mask, task_user_cpus(p), subset_mask)) { > err = -EINVAL; > goto err_unlock; > } > @@ -3044,16 +3037,11 @@ __sched_setaffinity(struct task_struct *p, const struct cpumask *mask, bool save > */ > void relax_compatible_cpus_allowed_ptr(struct task_struct *p) > { > - const struct cpumask *user_mask = p->user_cpus_ptr; > - > - if (!user_mask) > - user_mask = cpu_online_mask; > - > /* > * Try to restore the old affinity mask with __sched_setaffinity(). > * Cpuset masking will be done there too. > */ > - __sched_setaffinity(p, user_mask, false); > + __sched_setaffinity(p, task_user_cpus(p), false); > } > > void set_task_cpu(struct task_struct *p, unsigned int new_cpu) > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h > index 15eefcd65faa..426e9b64b587 100644 > --- a/kernel/sched/sched.h > +++ b/kernel/sched/sched.h > @@ -1881,6 +1881,13 @@ static inline void dirty_sched_domain_sysctl(int cpu) > #endif > > extern int sched_update_scaling(void); > + > +static inline const struct cpumask *task_user_cpus(struct task_struct *p) > +{ > + if (!p->user_cpus_ptr) > + return cpus_possible_mask; /* &init_task.cpus_mask */ > + return p->user_cpus_ptr; > +} > #endif /* CONFIG_SMP */ > > #include "stats.h" > Thanks for the good suggestions, will make the changes. Cheers, Longman