Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp348255lqh; Tue, 7 May 2024 00:23:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWE/j/enwz8pfaFJdGv4HjWA5tgCiNM8xLgV7xiwuXWVRUzrnQAMwqf4qt3mAC6Xz9qMUpBXe4SnGEsddeTa1OLunhlfYeIw2ZbRNg//Q== X-Google-Smtp-Source: AGHT+IEGWqYvb0hJ+IkLpHthhSI1wNXMG0QIb46D6Q8P29hHTfLCwpHQf69IjdNzeSmB1SJ6uUZr X-Received: by 2002:a05:6a21:8cc6:b0:1a9:a820:c604 with SMTP id ta6-20020a056a218cc600b001a9a820c604mr16717525pzb.48.1715066616788; Tue, 07 May 2024 00:23:36 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715066616; cv=pass; d=google.com; s=arc-20160816; b=fS1xX76DDqsqarsHp7O6Nf97++2hFYNDd32om7dxaZsN5R+JB0UEJeqcpf8GFEzZ4s P5Z2ful4pfgXSRNTF0qbNjuceSZlq210jFxHVflxK+EqinMGbvQys82IoO2AmWHVqy9P ZvZ+QcKilqZ6g/uANi18r+8SzNbLAtq/1L8Gjs02rDAm6AwvOD+APqGpnh3kH4DQFWqg VUtkcLTme4npDf/cB/LbvYvAiwmMj+xMM4boNlWqfST/BbV4amWcbVhYiCDXBFDfx+jQ gH/zAtShsAnehvuYlIgHUpK5RJXV5V2aRNmG+nVCMVxm0KEKzUsibB39QYMYwgV42RNU jGzg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=mHTNlabQ/no1MBx7O6fZNv/TDWph6e1m6ymph9+zOqU=; fh=/4CqNbSKFfNHfwFQThAAb9zVxVjo4k/FSUUjpV33RSg=; b=clmcn+7qJQUkC4BO5FSpe4U7hyDEzJtyAoKBEA+RVRwPWdFidSl3xRDCjzkXPfl2Rf l4Xvc6UHxNP/AtLi+O3FmB1vhCE4jqznOu/uTCbKzkrc6v1sZQFVsiLc9Wm2NmIyZClm yF9ISZdFz5vmQLK7MvoGB+TAEAfbbajIHtmFf4mYRoSLOBsOXAq3hoi+qeND/oVjy6+/ ihvBArXzq/ebGh6UnxJxW3cTIWD3H9H0OsokImVDB1xLRD7c17pFUOExIx8jkDwsvRtb 3a8J62CKoGf6xZsPS+TeniBM/svf/vO8T/d6dvjQdOthTP6JQTLgehayYYHvdtvxaqxe 9EDQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=unisoc.com); spf=pass (google.com: domain of linux-kernel+bounces-170811-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170811-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id mt18-20020a17090b231200b002af04f1a939si9617854pjb.21.2024.05.07.00.23.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 00:23:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-170811-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=unisoc.com); spf=pass (google.com: domain of linux-kernel+bounces-170811-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170811-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 46C5C285910 for ; Tue, 7 May 2024 07:23:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B1EE413BC1A; Tue, 7 May 2024 07:23:30 +0000 (UTC) Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C94C3D0BD for ; Tue, 7 May 2024 07:23:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715066610; cv=none; b=DsBWifekNuhbrL80kxramdZ+5xG1pIpvL+o3lngFj6r3gARnUCRSuKdvRA6QacUrbUXmvHasfzw5x7FuJItuIJ2x0HXQWhyjAFutTnlOHVtTS08ou/dLELyYDKZEnE5x6t7mA67mY4VM/QeVTN29js6Vg3GUXQ4I6Gg6x3BMB+U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715066610; c=relaxed/simple; bh=ar1FpdDgqeQQb1JwIhAccz/THq72DBOdMDqQLqZOQcY=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=E6qmqDGF2wTjYi2kPK7XQPL3tnGWz27OJDxMw4TugbMmaE8SdKYVTEz+Rp0pg/rWAiKsC0PsmZXsecL1157HW73xcYqVOxla3QLqTMzoqRfIWaaaanikITg3Ow69aOOf062BQkl83XchuS7jakSy0tiP2XXT68D0lTppUrqMFnY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 4477N64F089743; Tue, 7 May 2024 15:23:06 +0800 (+08) (envelope-from Xuewen.Yan@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4VYV6B6hwgz2K6x8R; Tue, 7 May 2024 15:20:02 +0800 (CST) Received: from BJ10918NBW01.spreadtrum.com (10.0.73.73) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Tue, 7 May 2024 15:23:03 +0800 From: Xuewen Yan To: , , , , CC: , , , , , , , , Subject: [RFC PATCH] sched: Clear user_cpus_ptr only when no intersection with the new mask Date: Tue, 7 May 2024 15:22:42 +0800 Message-ID: <20240507072242.585-1-xuewen.yan@unisoc.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL:SHSQR01.spreadtrum.com 4477N64F089743 The commit 851a723e45d1c("sched: Always clear user_cpus_ptr in do_set_cpus_allowed()") would cause that online/offline cpu will produce different results for the !top-cpuset task. For example: If the task was running, then offline task's cpus, would lead to clear its user-mask. unisoc:/ # while true; do sleep 600; done& [1] 6786 unisoc:/ # echo 6786 > /dev/cpuset/top-app/tasks unisoc:/ # cat /dev/cpuset/top-app/cpus 0-7 unisoc:/ # cat /proc/6786/status | grep Cpus Cpus_allowed: ff Cpus_allowed_list: 0-7 unisoc:/ # taskset -p c0 6786 pid 6786's current affinity mask: ff pid 6786's new affinity mask: c0 unisoc:/ # cat /proc/6786/status | grep Cpus Cpus_allowed: c0 Cpus_allowed_list: 6-7 After offline the cpu6 and cpu7, the user-mask would be cleared: unisoc:/ # echo 0 > /sys/devices/system/cpu/cpu7/online unisoc:/ # cat /proc/6786/status | grep Cpus Cpus_allowed: 40 Cpus_allowed_list: 6 ums9621_1h10:/ # echo 0 > /sys/devices/system/cpu/cpu6/online ums9621_1h10:/ # cat /proc/6786/status | grep Cpus Cpus_allowed: 3f Cpus_allowed_list: 0-5 When online the cpu6/7, the user-mask can not bring back: unisoc:/ # echo 1 > /sys/devices/system/cpu/cpu6/online unisoc:/ # echo 1 > /sys/devices/system/cpu/cpu7/online unisoc:/ # cat /proc/6786/status | grep Cpus Cpus_allowed: ff Cpus_allowed_list: 0-6 However, if we offline the cpu when the task is sleeping, at this time, because would not call the fallback_cpu(), its user-mask will not be cleared. unisoc:/ # while true; do sleep 600; done& [1] 5990 unisoc:/ # echo 5990 > /dev/cpuset/top-app/tasks unisoc:/ # cat /proc/5990/status | grep Cpus Cpus_allowed: ff Cpus_allowed_list: 0-7 unisoc:/ # taskset -p c0 5990 pid 5990's current affinity mask: ff pid 5990's new affinity mask: c0 unisoc:/ # cat /proc/5990/status | grep Cpus Cpus_allowed: c0 Cpus_allowed_list: 6-7 unisoc:/ # echo 0 > /sys/devices/system/cpu/cpu6/online unisoc:/ # cat /proc/5990/status | grep Cpus Cpus_allowed: 80 Cpus_allowed_list: 7 unisoc:/ # echo 0 > /sys/devices/system/cpu/cpu7/online unisoc:/ # cat /proc/5990/status | grep Cpus Cpus_allowed: 3f Cpus_allowed_list: 0-5 After 10 minutes, it was waked up, it can also keep its user-mask: ums9621_1h10:/ # cat /proc/5990/status | grep Cpus Cpus_allowed: 3f Cpus_allowed_list: 0-5 And when online the cpu6/7,the user-mask could bring back. unisoc:/ # echo 1 > /sys/devices/system/cpu/cpu6/online unisoc:/ # echo 1 > /sys/devices/system/cpu/cpu7/online unisoc:/ # cat /proc/6786/status | grep Cpus Cpus_allowed: c0 Cpus_allowed_list: 6-7 Indeed, there is no need to clear the user_cpus_ptr if there is an intersection between user_cpus_ptr and new_mask. So add the judgement of whether there is an intersection between them. Clear user_cpus_ptr only when no intersection with the new mask. In this way, the above problems can also be solved. Suggested-by: Waiman Long Signed-off-by: Xuewen Yan --- previous discussion: https://lore.kernel.org/all/e402d623-1875-47a2-9db3-8299a54502ef@redhat.com/ --- kernel/sched/core.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 7019a40457a6..bbb8e88949f4 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2796,21 +2796,24 @@ __do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx) } /* - * Used for kthread_bind() and select_fallback_rq(), in both cases the user - * affinity (if any) should be destroyed too. + * Used for kthread_bind() and select_fallback_rq(). + * Destroy user affinity if no intersection with the new_mask. */ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) { struct affinity_context ac = { .new_mask = new_mask, .user_mask = NULL, - .flags = SCA_USER, /* clear the user requested mask */ + .flags = 0, }; union cpumask_rcuhead { cpumask_t cpumask; struct rcu_head rcu; }; + if (p->user_cpus_ptr && !cpumask_intersects(p->user_cpus_ptr, new_mask)) + ac.flags = SCA_USER; /* clear the user requested mask */ + __do_set_cpus_allowed(p, &ac); /* -- 2.25.1