Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2212936rdb; Tue, 3 Oct 2023 13:58:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGj4rgbuqWGjveN8iWSN30YA5kQErT/rKnHRiysE+GrDONbjBa5qpKEUBU112winpV/TIhK X-Received: by 2002:a17:90b:4c03:b0:26c:f6d2:2694 with SMTP id na3-20020a17090b4c0300b0026cf6d22694mr486905pjb.12.1696366734850; Tue, 03 Oct 2023 13:58:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696366734; cv=none; d=google.com; s=arc-20160816; b=yrvp0FBgv/8R1/Y7PB8Mf4gRCbnBS0MzE/rl4pflMJy7gi6+cv2xM7hJ7VPR8Ub8cm HLObipL8gRdl1ixn5IaUA0212Abl1Jvv5jNVWW7k+fRMbpXmIo7f2TIwhWSczBWYTZ9P 7CXB6IlTnm+KAtqrZom3xY64SoNh4RyJuCTMWvrOx6td3AKw3H9WP9Qjufd4suiCotQ9 N/xXuJk7KVM6vC5b0k9qipEMpOe+ZxWXZUa8y+L7VZg3aJRFtBICuj+N5NHY2LSaP3Ke hRUjgunA+3CBukpT/ddtYYXdnkYi31+c4B+qGIy8T2mFkvmXNeu1EfArahw1Z0uZQqNx d+yw== 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=R0J7KehNMsr0jjrLYehZsVAtwRuvkbKBMf/ZqDdCVXY=; fh=PEiVI+lysiY+o7H6cHjpjKJD+x92WreQo/XVbmxN6iQ=; b=lWJ7qmcsAgP2FxxlvIg5C221rxfu7B1VOVRZVF+Lr6LkqbIuS6ZXZjSthSA2sHI9WF UqR7sIQhmVcpFTB0pz1afGsNKV/kSgO9bMqWJmYG+vj5OL6SBEqJGFpoT+85E35IrUbI a5pKq/wHK+rZBVmJ0hTjO7QOkSsTWEHiNExd9ocWy2CgluBBiLFZ79gZmnwbJPCmJzrX is/3iK0GUqLi5RevsbxfPhaHwC44Lil/yzohFjcx3kOCPxsMIQ0/kENyErU4nV99ri0n pXCROffkQob1abWBGlkt1z0wcUdMRbOsVMhxFvhFgQt+oaEAPByzMZcGst5Iy8r3SMt9 f/8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Y7Hk8Nk4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id so3-20020a17090b1f8300b0026b52571571si1110917pjb.1.2023.10.03.13.58.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 13:58:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Y7Hk8Nk4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 7E12382DAD23; Tue, 3 Oct 2023 13:58:53 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232285AbjJCU6v (ORCPT + 99 others); Tue, 3 Oct 2023 16:58:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232079AbjJCU6u (ORCPT ); Tue, 3 Oct 2023 16:58:50 -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 51BFEAB for ; Tue, 3 Oct 2023 13:58:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696366681; 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=R0J7KehNMsr0jjrLYehZsVAtwRuvkbKBMf/ZqDdCVXY=; b=Y7Hk8Nk4dB5Ylx63syWvmPcxLaZmot4iE0e7yR5zuKtVrmZAZPdfxpI4MbMOuCG5gz3OFv gcoj8pp/xvBmStKw7C+zKAJI/U7C19UsRYUV0mR/j72Vxf59F3qYoVu/dbaw7XyIHzydho LOqeoIi9S8XR0GT4aE5ZNFPx23wndlI= Received: from mimecast-mx02.redhat.com (mx-ext.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-691-_IGW9c5dOaOKfgZ-fURqaw-1; Tue, 03 Oct 2023 16:57:46 -0400 X-MC-Unique: _IGW9c5dOaOKfgZ-fURqaw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8536F3806706; Tue, 3 Oct 2023 20:57:45 +0000 (UTC) Received: from llong.com (unknown [10.22.10.176]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8D3CA40C6EA8; Tue, 3 Oct 2023 20:57:44 +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 v4] sched/core: Use zero length to reset cpumasks in sched_setaffinity() Date: Tue, 3 Oct 2023 16:57:35 -0400 Message-Id: <20231003205735.2921964-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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_BLOCKED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 03 Oct 2023 13:58:53 -0700 (PDT) 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 input cpumask length of 0 to signal a reset of the cpumasks to the default as allowed by the current cpuset. A non-NULL cpumask should still be provided to avoid problem with older kernel. If sched_setaffinity(2) has been called previously to set a user supplied cpumask, a value of 0 will be returned to indicate success. Otherwise, an error value of -EINVAL will be returned. We may have to update the sched_setaffinity(2) manpage to document this new side effect of passing in an input length of 0. Signed-off-by: Waiman Long --- kernel/sched/core.c | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 802551e0009b..a10d507a05df 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8315,7 +8315,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; @@ -8401,15 +8406,29 @@ 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 a NULL cpumask is passed in and user_cpus_ptr is set, + * clear user_cpus_ptr and reset the current cpu affinity to the + * default for the current cpuset. If user_cpus_ptr isn't set, + * -EINVAL will be returned. */ - user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE); - if (user_mask) { - cpumask_copy(user_mask, in_mask); - } else if (IS_ENABLED(CONFIG_SMP)) { - retval = -ENOMEM; - goto out_put_task; + if (!in_mask) { + if (!p->user_cpus_ptr) { + retval = -EINVAL; + goto out_put_task; + } + 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)) { + retval = -ENOMEM; + goto out_put_task; + } } ac = (struct affinity_context){ @@ -8451,6 +8470,12 @@ SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len, cpumask_var_t new_mask; int retval; + /* + * A len of 0 will reset a previously set user cpumask. + */ + if (!len) + return sched_setaffinity(pid, NULL); + if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) return -ENOMEM; -- 2.39.3