Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp25977710rwd; Mon, 3 Jul 2023 03:54:02 -0700 (PDT) X-Google-Smtp-Source: APBJJlGP9TNrsmSqVQS1gzpYMFmXHAM/jMe9YlWPHbVKjDdWG/HFzGc9HiXgbd1TIlcQWwOM6jke X-Received: by 2002:a05:6a20:8e0c:b0:12e:b362:a3fa with SMTP id y12-20020a056a208e0c00b0012eb362a3famr1178007pzj.21.1688381642037; Mon, 03 Jul 2023 03:54:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688381642; cv=none; d=google.com; s=arc-20160816; b=bIcD5dneST8GP6/J1OQNTXjug0WTFrdcFpF1XqOhblqwwcGAgx/g/g+p52FKFkxy+s SUeDZPQAM0CrvwSTVzGeVOlXeMcgEHeRvxSYmHGVAET+EHP81RjmjbcyXxF6AF+gudmO CdJMf4CGKil2GT5LDEGYtx4wA7DSqXFwRXh1FWpI3lTUQFRprZWDjFBJEtGMDJmWoczb QTY58KQI0YqmJBVr1neEm4dCobRmRfZdVb0Tzrnz/PdxEja+Lre4WaY8g+oUCDU0Bd9i volhvQ8rZY65HrePKSU/O7JAS8y/ZLJVhthxrc1w+Hqjg8RmkmjNsZh1fd3yM1p3UkLc uSxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=iTxxOeNUsimMPmX/cHsXm4j47p8TRYnXS2BDD+byQE8=; fh=V0EURbwsq7Qih4wMvIjmsEfh6adx2pujvE2D4CqkJSY=; b=wdtHEBCRWa/juotQX1k0V0nNKKPzf5zNGmKmOJuCXIpfiAdqiz+iQhwyX4HgrYm00O hBD6m6vGKCQGgMKYaIYAr5vfUc6/2xw1bB1IWXzjQnDvRYyCkfSNyRHW2GLYkkXxwWTo 6b8E5AMbIxZd8w1jCReEoWOERphPSCfDlmoyE7ZpqoLvOIS5xyPAZHAqQIZ34nQf01fj 2fHRo76hP9d+5koCUnAT6ce+qLV9uMfkXzWvtOcZDyr0PnVOrUnWy7lSqCfoW5lObUl2 +cK3q/1QWERRrXc5ahh5Ltyo8nccwIoHBZCfUsV1QSUPvS4dUi1qyLjq1l7gLsqTwgJr ntdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=v59Gm5is; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kg14-20020a170903060e00b001b89bfd0c34si1118844plb.18.2023.07.03.03.53.46; Mon, 03 Jul 2023 03:54:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=v59Gm5is; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231151AbjGCK0Z (ORCPT + 99 others); Mon, 3 Jul 2023 06:26:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229534AbjGCK0Y (ORCPT ); Mon, 3 Jul 2023 06:26:24 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0E05B8 for ; Mon, 3 Jul 2023 03:26:22 -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=iTxxOeNUsimMPmX/cHsXm4j47p8TRYnXS2BDD+byQE8=; b=v59Gm5isube2eamYKYso7L6fNh FidiNM4uSwoD4sQQJ6ST5MRXEVF5IXwsc0bydXWw2wts/di/JsB85kMiTNBLFy1EEsc9E7kOz7wMw eJZMvCafqlOemjSOc3VXciZTEKh/HoAuMGiTeZtQzoPgvV/z51ZTI5tngFygUHR0f/0IsiPcikurF xRHCllW16tVO44Sjr4h1LCnbRhitm91zP+auhoHrjXt10uXsLzsGv66pDw31xuGY4J7jCl4GOMHnB 7noFds20pIm+dpCit/lLDyxDTljCEJz7YqTdQwDccl9yuPLGREqaT4mqXWeUn9i7tpjgSQgH/lTEn UbvMdAoQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1qGGkw-008927-Tr; Mon, 03 Jul 2023 10:26:06 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 32A2730023F; Mon, 3 Jul 2023 12:26:05 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 0AA5F202A2A17; Mon, 3 Jul 2023 12:26:05 +0200 (CEST) Date: Mon, 3 Jul 2023 12:26:04 +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 , linux-kernel@vger.kernel.org, Phil Auld , Brent Rowsell , Peter Hunt Subject: Re: [PATCH] sched/core: Use empty mask to reset cpumasks in sched_setaffinity() Message-ID: <20230703102604.GC4253@hirez.programming.kicks-ass.net> References: <20230628211637.1679348-1-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230628211637.1679348-1-longman@redhat.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 28, 2023 at 05:16:37PM -0400, Waiman Long wrote: > Since commit 8f9ea86fdf99 ("sched: Always preserve the user requested > cpumask"), user provided CPU affinity via sched_setaffinity(2) is > perserved even if the task is being moved to a different cpuset. However, > that affinity is also being inherited by any subsequently created child > processes which may not want or be aware of that affinity. > > One way to solve this problem is to provide a way to back off from > that user provided CPU affinity. This patch implements such a scheme > by using an empty cpumask to signal a reset of the cpumasks to the > default as allowed by the current cpuset. > > Before this patch, passing in an empty cpumask to sched_setaffinity(2) > will return an EINVAL error. With this patch, an error will no longer > be returned. Instead, the user_cpus_ptr that stores the user provided > affinity, if set, will be cleared and the task's CPU affinity will be > reset to that of the current cpuset. This reverts the cpumask change > done by all the previous sched_setaffinity(2) calls. > This is a user visible ABI change -- but with very limited motivation. Why do we want this? Who will use this? > Signed-off-by: Waiman Long > --- > kernel/sched/core.c | 26 +++++++++++++++++++++----- > 1 file changed, 21 insertions(+), 5 deletions(-) > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index c52c2eba7c73..f4806d969fc9 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -8317,7 +8317,12 @@ __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx) > } > > cpuset_cpus_allowed(p, cpus_allowed); > - cpumask_and(new_mask, ctx->new_mask, cpus_allowed); > + > + /* Default to cpus_allowed with NULL new_mask */ > + if (ctx->new_mask) > + cpumask_and(new_mask, ctx->new_mask, cpus_allowed); > + else > + cpumask_copy(new_mask, cpus_allowed); > > ctx->new_mask = new_mask; > ctx->flags |= SCA_CHECK; > @@ -8366,6 +8371,7 @@ __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx) > > long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > { > + bool reset_cpumasks = cpumask_empty(in_mask); > struct affinity_context ac; > struct cpumask *user_mask; > struct task_struct *p; > @@ -8403,13 +8409,23 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > goto out_put_task; > > /* > - * With non-SMP configs, user_cpus_ptr/user_mask isn't used and > - * alloc_user_cpus_ptr() returns NULL. > + * If an empty cpumask is passed in, clear user_cpus_ptr, if set, > + * and reset the current cpu affinity to the default for the > + * current cpuset. > */ > - user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE); > + if (reset_cpumasks) { > + in_mask = NULL; /* To be updated in __sched_setaffinity */ > + user_mask = NULL; > + } else { > + /* > + * With non-SMP configs, user_cpus_ptr/user_mask isn't used > + * and alloc_user_cpus_ptr() returns NULL. > + */ > + user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE); > + } > if (user_mask) { > cpumask_copy(user_mask, in_mask); > - } else if (IS_ENABLED(CONFIG_SMP)) { > + } else if (!reset_cpumasks && IS_ENABLED(CONFIG_SMP)) { > retval = -ENOMEM; > goto out_put_task; > } > -- > 2.31.1 >