Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp19814576rwd; Wed, 28 Jun 2023 14:57:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ53yDoPCu2CB36JywLWzH8AK3MQyfvcr1Q4rn8xg6b8D90iQg0Fmu+9E8M4Ni6v22UQmHaO X-Received: by 2002:a05:6512:3414:b0:4f9:cd02:4af1 with SMTP id i20-20020a056512341400b004f9cd024af1mr9877228lfr.34.1687989432964; Wed, 28 Jun 2023 14:57:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687989432; cv=none; d=google.com; s=arc-20160816; b=ALtloWmTKvvLlMgLMjZEO1yEuyEGTgBMWF8DcdDvic5JeS04CgxOq1pFndgRcgwacE qsavVIvbPhV7+hibT5rDqZP824fZGVCrb75+WkBn8OpBu67GTY5eaZ2BTaJ7jRS5gt9p lgOSD+D9U0zm6kUjhdFC5riOaE97rtf0Iz73Pvm7dlJVZxaVbpkZiRSMOe30TKTYoe4Z 0kwts7Q4Z8oameRj/6iOKSOW77OGSmTV/TLy/qAgiMCagJWM8E4BS4xNq8gdf5wOR0a7 3xhzICIiEvSAz0/Dguw7zvy0hv6Vzv2q0QrAz3aotNYVyj0O3I11ya6mCdolOB3OUW/w L/bQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=3eqHPH2jtk9ZGDW+IjmQhCJcfivZfs/kcGLVBmAENnY=; fh=PEiVI+lysiY+o7H6cHjpjKJD+x92WreQo/XVbmxN6iQ=; b=XglqU4eyrfkH7USrESuuwW/QY7boLS0k5kjlrRe7c0ziAhFSx3dpa9Xl7YJR9x2gGc MA8JUB4Pw41QWpk7TzuTlqL7+VRMRPni4sDBZxF3elHXUIJPg9NolxDmjcJucLf56put Udatv3pRREQO3C+W5+zfHiMQ7C+25tniDiXWZ549pCSp/31WmNPy6gqegWSgkgbauyqp Gf2qJYIFSsmNzwA5hf0vkEXc1J+hFmhO1AlR0raiLtMt2a1VHh7G3qV8H13G4/LMJ9fA 4UxOVm/y0VA0ulOP2ZwM6YvgmsmfkeqlM6xO1NEPD0p83q/Q/7A1MPBaw3bv3pJbzxPG CNtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Np/d+38t"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n5-20020a056402060500b0051bec9a96fasi5751215edv.35.2023.06.28.14.56.48; Wed, 28 Jun 2023 14:57:12 -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=@redhat.com header.s=mimecast20190719 header.b="Np/d+38t"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231756AbjF1VXM (ORCPT + 99 others); Wed, 28 Jun 2023 17:23:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231916AbjF1VWi (ORCPT ); Wed, 28 Jun 2023 17:22:38 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1708358D for ; Wed, 28 Jun 2023 14:17:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687987022; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=3eqHPH2jtk9ZGDW+IjmQhCJcfivZfs/kcGLVBmAENnY=; b=Np/d+38taulB1M0SKI9+Wjy7bMjtXKC2HTsbjZ8x5QMRVy8FyTGc8nfzfvjJx4zA6aYmun v6cY9SQHiOPThLJf3UdUDF+NUl0vMXNi82zNixgNRTWDfEUenMbsD2T3GZ5hHTXsNQDuQA kWj7HkA2b/xcUCai6R7nxmEcmQqPRQk= 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-90-yLOE8C9UN72SkTNYt8H-zQ-1; Wed, 28 Jun 2023 17:16:58 -0400 X-MC-Unique: yLOE8C9UN72SkTNYt8H-zQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 117F01C06ED1; Wed, 28 Jun 2023 21:16:58 +0000 (UTC) Received: from llong.com (unknown [10.22.34.177]) by smtp.corp.redhat.com (Postfix) with ESMTP id 36DF8F5CD4; Wed, 28 Jun 2023 21:16:57 +0000 (UTC) From: Waiman Long To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider Cc: linux-kernel@vger.kernel.org, Phil Auld , Brent Rowsell , Peter Hunt , Waiman Long Subject: [PATCH] sched/core: Use empty mask to reset cpumasks in sched_setaffinity() Date: Wed, 28 Jun 2023 17:16:37 -0400 Message-Id: <20230628211637.1679348-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,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 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. 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