Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp794602rdg; Wed, 11 Oct 2023 05:57:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEa8jC09NEI6rHv+9LXVMNlvDzZGeFCXcT+t6yUG2RdAfYt+Q1+9nhQwYXZLihCxbF52FJk X-Received: by 2002:a17:902:6bc1:b0:1c3:6e38:3943 with SMTP id m1-20020a1709026bc100b001c36e383943mr16218568plt.56.1697029050414; Wed, 11 Oct 2023 05:57:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697029050; cv=none; d=google.com; s=arc-20160816; b=nek56AdjjZp4olAV02cy07UkhMY9k5Aj0vBq7GlJW8/GIPD66jdcz8ZpC2PmN20Noo F24HE+EzXDn1XqrpGW5kohvOAJpvI707Ej5hi7PWSsCms/eIEQW4Hip3oYt6n5m9dJMF KrY2twVIYUOAnCDyYQi4AFCZbKztvFDMjEyIfBYWBnOkaPi/8JnJILNo1jEB0DcqH0Bo zb7rb2/aXRphW1usi4bsG77iJoYxy+SH2r27dxAO/PxuvCSDceBnr413dbLQzgCqgzOd pBJMPHc0lDZ5k1N10j0QdyauxDIjCsvAG/LnfCik/Tgt/d+h4ELPWIs5pErl8u++ZBLT +nWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=C3Q22l8FHOtSNLSPzzVZTuNcPC2ZofW6W5zArgJ64/4=; fh=T3AfTD3QJ7vOtO4TlNhrGKB/GcHweyYl6IPxog5WHro=; b=t0k3FjHcj1vo+jmXVVGnSNOXahr9sqEXdE/M2/jUBEe0wvt6mdmBR0aUDx4Bma3Xtx A5EPjK8ZYMYDfxg35AfgKFgXRc2SfsI7b5F1igALcUp+//EtyIGhEsJB1PJzQEDuRZa5 iGcDCkdqPJMFxBhzJ91hGl8BPBK+/XCQpaGwfW5TWeNORZRrSJfLm1ba2I04aAxu6Om+ nevGrdMQb3V+Jo93HnO/N4c0ljFD8yPhCwo1gLUEReGjfDxwbGYHKFPUCAxDdNnfo85d PGfEgxndfiyIxlL5MycjJTwAW7icME3UOdEBSCCQCZoyD4uk242glnQOS9gFfepgV2I7 xjLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gJ547nCr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id u9-20020a17090341c900b001c5ecff1bd4si1112622ple.603.2023.10.11.05.57.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 05:57:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gJ547nCr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 76263808E66C; Wed, 11 Oct 2023 05:56:37 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347003AbjJKM4I (ORCPT + 99 others); Wed, 11 Oct 2023 08:56:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234906AbjJKMzv (ORCPT ); Wed, 11 Oct 2023 08:55:51 -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 C7DE2AF for ; Wed, 11 Oct 2023 05:55:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697028900; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=C3Q22l8FHOtSNLSPzzVZTuNcPC2ZofW6W5zArgJ64/4=; b=gJ547nCrK4kxtHyzWhQaSArCi/O4fk6Ccw7aywlfqATGpR1fQS1Zl8rU05CerftLKNeod4 ZwIG5L6ihJTW8j9WvAbIj6xY9hmReKM94yyT875iIUssBYtU6uXgNa9vRwxpxd08dMBSel uOMSv/Gt8kv1Wi/WtmTIlD7Oq2VUFvQ= 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-298-eBsTBDv4PaiW8hiycp8iNg-1; Wed, 11 Oct 2023 08:54:56 -0400 X-MC-Unique: eBsTBDv4PaiW8hiycp8iNg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 2A8E33822E87; Wed, 11 Oct 2023 12:54:56 +0000 (UTC) Received: from [10.22.16.239] (unknown [10.22.16.239]) by smtp.corp.redhat.com (Postfix) with ESMTP id 886E3215670B; Wed, 11 Oct 2023 12:54:54 +0000 (UTC) Message-ID: <389a8abc-7f0f-7bcc-bc58-f70f045d00a5@redhat.com> Date: Wed, 11 Oct 2023 08:54:54 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: [PATCH] cgroup/cpuset: Change nr_deadline_tasks to an atomic_t value Content-Language: en-US To: Juri Lelli Cc: Tejun Heo , Zefan Li , Johannes Weiner , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Peter Zijlstra , Ingo Molnar , Qais Yousef , Hao Luo , Dietmar Eggemann , Steven Rostedt , Xia Fukun References: <20231009191515.3262292-1-longman@redhat.com> <6b769316-6434-5054-43f5-7933fc2bee01@redhat.com> <31e06652-1dbd-e32f-3123-d17e178c5c27@redhat.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED autolearn=unavailable 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 (snail.vger.email [0.0.0.0]); Wed, 11 Oct 2023 05:56:37 -0700 (PDT) On 10/11/23 04:14, Juri Lelli wrote: > On 10/10/23 16:03, Waiman Long wrote: >> On 10/10/23 15:44, Waiman Long wrote: >>> On 10/10/23 01:34, Juri Lelli wrote: >>>> Hi, >>>> >>>> On 09/10/23 15:15, Waiman Long wrote: >>>>> The nr_deadline_tasks field in cpuset structure was introduced by >>>>> commit 6c24849f5515 ("sched/cpuset: Keep track of SCHED_DEADLINE task >>>>> in cpusets"). Unlike nr_migrate_dl_tasks which is only modified under >>>>> cpuset_mutex, nr_deadline_tasks can be updated in various contexts >>>>> under different locks. As a result, data races may happen that cause >>>>> incorrect value to be stored in nr_deadline_tasks leading to incorrect >>>> Could you please make an example of such data races? >>> Since update to cs->nr_deadline_tasks is not protected by a single lock, >>> it is possible that multiple CPUs may try to modify it at the same >>> time.  It is possible that nr_deadline_tasks++ and nr_deadline_tasks-- >>> can be done in a single instruction like in x86 and hence atomic. >>> However, operation like "cs->nr_deadline_tasks += >>> cs->nr_migrate_dl_tasks" is likely a RMW operation and so is subjected >>> to racing. It is mostly theoretical, but probably not impossible. >> Sorry, even increment and decrement operators are not atomic. >> >> inc_dl_tasks_cs() is only called from switched_to_dl() in deadline.c which >> is protected by the rq_lock, but there are multiple rq's. dec_dl_tasks_cs() >> is called from switched_from_dl() in deadline.c and cgroup_exit() in >> cgroup.c. The later one is protected by css_set_lock. The other place where >> nr_deadline_tasks can be changed is in cpuset_attach() protected by >> cpuset_mutex. > So, let's see. :) > > switched_to_dl(), switched_from_dl() and cpuset_attach() should all be > protected (for DEADLINE tasks) by cpuset_mutex, see [1] for the former > two. Yes, I missed the cpuset_lock() call. > What leaves me perplexed is indeed cgroup_exit(), which seems to operate > under css_set_lock as you say. I however wonder why is that not racy > already wrt, say, cpuset_attach() which AFAIU uses css information w/o > holding css_set_lock? The css_set_lock protects changes made to css_set. Looking at cgroup_migrate_execute(), css_set_lock is taken when the tasks are actually moving from one css_set to another one. cpuset_attach() is called just to update the CPU and node affinity and cpuset_mutex is taken to ensure stability of the CPU and node masks. There is no change to css_set and so css_set_lock isn't needed. We can argue that there can be racing between cgroup_exit() and the iteration of tasks in cpuset_attach() or cpuset_can_attach(). An rcu_read_lock() is probably needed. I am stilling investigating that. Cheers, Longman > > Thanks, > Juri > > 1 - https://elixir.bootlin.com/linux/latest/source/kernel/sched/core.c#L7688 >