From mboxrd@z Thu Jan 1 00:00:00 1970 From: Peter Zijlstra Subject: Re: [PATCH v5 1/3] sched: Use user_cpus_ptr for saving user provided cpumask in sched_setaffinity() Date: Wed, 17 Aug 2022 10:46:28 +0200 Message-ID: References: <20220816192734.67115-1-longman@redhat.com> <20220816192734.67115-2-longman@redhat.com> Mime-Version: 1.0 Return-path: DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=CKeCiigKMtOdgr4YLuQq0s6tHiN60WT8O1gsyf1py6Y=; b=OYd1O3mEkB+wAABrc+5g7lt0dm S+0AdbaoEm8UtVjwn+dC5BXqtsgv3oynpOR8By0PJCSCNJvzHZ5h940eGMSrNDeKiEukda2HZhArL ze3zylI40u7KNiWAI161EbKzjOYT0kuCvfTd2aaVvxaqn91cFB5B9L4+bW7iNVxkAZ6pBlDDJG7I0 WV2+66RpxVyfHz/+acnMp4WoS0kV8qnsyhZ9vmZ4GQW4Wr4OL+jvPu8pWPu7/h/67pKyamyymVBSq zvqfZCQIW6HxCFZQ3/Ma9XqACtBoXlNb43JuULRrCn6X3bXUU+6OTceIBiTR5FJQSz1Wl77gPBJi2 vtQYWo9Q==; Content-Disposition: inline In-Reply-To: <20220816192734.67115-2-longman-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> List-ID: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Waiman Long 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-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, Linus Torvalds On Tue, Aug 16, 2022 at 03:27:32PM -0400, Waiman Long wrote: > @@ -8079,10 +8056,11 @@ int dl_task_check_affinity(struct task_struct *p, const struct cpumask *mask) > #endif > > 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) > { > int retval; > cpumask_var_t cpus_allowed, new_mask; > + struct cpumask *user_mask = NULL; > > if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) > return -ENOMEM; Please move that retval down so the variable declarations are properly ordered again. > @@ -8098,8 +8076,33 @@ __sched_setaffinity(struct task_struct *p, const struct cpumask *mask) > retval = dl_task_check_affinity(p, new_mask); > if (retval) > goto out_free_new_mask; > + > + /* > + * Save the user requested mask into user_cpus_ptr if save_mask set. > + * pi_lock is used for protecting user_cpus_ptr. > + */ > + if (save_mask && !p->user_cpus_ptr) { > + user_mask = kmalloc(cpumask_size(), GFP_KERNEL); > + > + if (!user_mask) { > + retval = -ENOMEM; > + goto out_free_new_mask; > + } > + } > + if (save_mask) { > + unsigned long flags; > + > + raw_spin_lock_irqsave(&p->pi_lock, flags); > + if (!p->user_cpus_ptr) { > + p->user_cpus_ptr = user_mask; > + user_mask = NULL; > + } > + > + cpumask_copy(p->user_cpus_ptr, mask); > + raw_spin_unlock_irqrestore(&p->pi_lock, flags); > + } How about: if (save_mask) { if (!p->user_cpus_ptr) { ... } ... } ? 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 D9F9BC25B08 for ; Wed, 17 Aug 2022 08:46:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233296AbiHQIqn (ORCPT ); Wed, 17 Aug 2022 04:46:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229509AbiHQIql (ORCPT ); Wed, 17 Aug 2022 04:46:41 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F14DF74E0D; Wed, 17 Aug 2022 01:46:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=CKeCiigKMtOdgr4YLuQq0s6tHiN60WT8O1gsyf1py6Y=; b=OYd1O3mEkB+wAABrc+5g7lt0dm S+0AdbaoEm8UtVjwn+dC5BXqtsgv3oynpOR8By0PJCSCNJvzHZ5h940eGMSrNDeKiEukda2HZhArL ze3zylI40u7KNiWAI161EbKzjOYT0kuCvfTd2aaVvxaqn91cFB5B9L4+bW7iNVxkAZ6pBlDDJG7I0 WV2+66RpxVyfHz/+acnMp4WoS0kV8qnsyhZ9vmZ4GQW4Wr4OL+jvPu8pWPu7/h/67pKyamyymVBSq zvqfZCQIW6HxCFZQ3/Ma9XqACtBoXlNb43JuULRrCn6X3bXUU+6OTceIBiTR5FJQSz1Wl77gPBJi2 vtQYWo9Q==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=worktop.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1oOEh3-007wZq-28; Wed, 17 Aug 2022 08:46:29 +0000 Received: by worktop.programming.kicks-ass.net (Postfix, from userid 1000) id 25743980089; Wed, 17 Aug 2022 10:46:28 +0200 (CEST) Date: Wed, 17 Aug 2022 10:46:28 +0200 From: Peter Zijlstra To: Waiman Long 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 Subject: Re: [PATCH v5 1/3] sched: Use user_cpus_ptr for saving user provided cpumask in sched_setaffinity() Message-ID: References: <20220816192734.67115-1-longman@redhat.com> <20220816192734.67115-2-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220816192734.67115-2-longman@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 16, 2022 at 03:27:32PM -0400, Waiman Long wrote: > @@ -8079,10 +8056,11 @@ int dl_task_check_affinity(struct task_struct *p, const struct cpumask *mask) > #endif > > 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) > { > int retval; > cpumask_var_t cpus_allowed, new_mask; > + struct cpumask *user_mask = NULL; > > if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) > return -ENOMEM; Please move that retval down so the variable declarations are properly ordered again. > @@ -8098,8 +8076,33 @@ __sched_setaffinity(struct task_struct *p, const struct cpumask *mask) > retval = dl_task_check_affinity(p, new_mask); > if (retval) > goto out_free_new_mask; > + > + /* > + * Save the user requested mask into user_cpus_ptr if save_mask set. > + * pi_lock is used for protecting user_cpus_ptr. > + */ > + if (save_mask && !p->user_cpus_ptr) { > + user_mask = kmalloc(cpumask_size(), GFP_KERNEL); > + > + if (!user_mask) { > + retval = -ENOMEM; > + goto out_free_new_mask; > + } > + } > + if (save_mask) { > + unsigned long flags; > + > + raw_spin_lock_irqsave(&p->pi_lock, flags); > + if (!p->user_cpus_ptr) { > + p->user_cpus_ptr = user_mask; > + user_mask = NULL; > + } > + > + cpumask_copy(p->user_cpus_ptr, mask); > + raw_spin_unlock_irqrestore(&p->pi_lock, flags); > + } How about: if (save_mask) { if (!p->user_cpus_ptr) { ... } ... } ?