Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp1397827rdb; Wed, 24 Jan 2024 14:11:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IFaPDbGr1r9Z8uXWTtTI4iMINi/sp7ezi6JTvxof/s0mU9wmWc1iH9UTbBVKQNY4FkaaPQv X-Received: by 2002:a17:907:a683:b0:a2e:7eb0:cfe5 with SMTP id vv3-20020a170907a68300b00a2e7eb0cfe5mr4387ejc.146.1706134262343; Wed, 24 Jan 2024 14:11:02 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706134262; cv=pass; d=google.com; s=arc-20160816; b=sQyr82hlKkbtmlodlag21VXV88ClV+fxZ1Mnczsi6ta9xN8JpBiYrnXoYwKuQggrYR Gt5BCcaf1HeL47nIXnXV00FzzCo3Wbs6r/OuZ7QrmPI3J1HLjkt1OkBbSIrvpWYvE13c gq7EihNXZk4g9xTxH6FSSxBCBsojIZnNd+OuTlnJ6Lagn6tcGk7cB0woSHsBxoDAjM8K zC1OCv2c6w+/DnU8d3FJwAzLZ/+Cz1zcEiC4wTkrsVA7jruR7p8DoB/nMVJE9MOtr7l9 x6CzngbBeCY2yeMQaYVDTt3ukLOrFDN8k3WrA0fWpt3Tgk+T/AFfXWNn4seH8dXReu+0 +OBA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:message-id:date:references:in-reply-to:subject:cc:to :from:dkim-signature; bh=Of8y99/fZ6gvxjBPOuJPhzGycvWjT9wPG4BDRVEpUOo=; fh=jccGqPS4WUx8nXL+z6iwdP6UvW9fZfxqCDeZ5b7nuGg=; b=vjQxKOn8DvP/B96Hh9whKqBb1mQGNfU3kxXQ2qledi+jwuSyko1vXBjyI0eUOlvfyN kVfMw2pWS9wkbdfCcFBaoqq4CSA9hlYZWYMRU+wen+H0q6lJRj41J+f0hP6yoFV6PfxL HlER0j1QH/uVIbNym60b4FLc1uw28Fwl2R3M00u6vBGcus5FL4ISl3rs8SwXFx4l9y5c 3W4r0l10uU3HkbROgs9agIoxWIPTBWjb8GASGNj1sJbuUsCZExI+0h5TOGDeUcO5Ps+z mZ7ceHWcqYA+mpM/ati2DBH6rK022h/GGBxxfZFZKm4SwqPgqi/H+7FTe3AzmeG3t3Mt yhSw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=rBD5fHhj; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-37727-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37727-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id jo14-20020a170906f6ce00b00a2ae479d391si268008ejb.479.2024.01.24.14.11.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 14:11:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-37727-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=rBD5fHhj; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-37727-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37727-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 1BE0D1F23D95 for ; Wed, 24 Jan 2024 22:11:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B6601135A40; Wed, 24 Jan 2024 22:10:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="rBD5fHhj" Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57CEA131E26 for ; Wed, 24 Jan 2024 22:10:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706134252; cv=none; b=ij2ARQnyjSWgAJ84fBPHhL1rgDS2PUxBM0o5yNuhOeB3qdvwFL1oDb9dwkFj3EGj5ZLHHgstxi1gw7fUfcCsFCzplK+9lu7Mrs2iitfvOPr1OhJFn/H1prIMuQLDng2IZgQaRyGCRJFd3LgX3Pb3ktGKPWNV2NrTQOjMJGUH2HU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706134252; c=relaxed/simple; bh=qf1Bz4aoFrb6jzFeqw/Ih99tCsZ4qQQIkj71i8O1boA=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=tuUkrCX6WVRdAbicBqGhvAKQEhFOZ6doJa5KPVOuPHpIh1J81Y89jAHfpLEQ5MHAlOpZgwV32mO8eMYzRYBpGwoWjfC5V4ITU58Fv/zyUMm6+8fbA7lbBdcBEqz/NC78tFTFGBH3gQ64X/7S7jfcuAum6ltbEcA9JztCLD+OXeQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=rBD5fHhj; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1d72043fa06so9475ad.1 for ; Wed, 24 Jan 2024 14:10:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706134250; x=1706739050; darn=vger.kernel.org; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=Of8y99/fZ6gvxjBPOuJPhzGycvWjT9wPG4BDRVEpUOo=; b=rBD5fHhjCxDKMm1IaVbSPPi8b3xnXl8BSRNCu+TprOKToqAUmKufXEvLNS9q7qENqR wXGtkcnZxxMRpvEMKir2Otkiwf2BdupGY8hhWlnOuc+TcxoZJEB3JG3uMMAksJVTPmIs BSj0EYBeVrJfvHpcvdV10AXpjVTDygo0fHXHtQAwdb5ztRdSiLVxbJV0RWXos6k4yIAQ 4g73FdPoB8mKd+fp4IBSNq0jaXyRrwwSu/B8xPYEmqv3vwFSTIZxXl6WyPDNm4w5sTzz VxhgZVi4NI7mEQVaqQIE3nCWaLGHsb+X6FKdaNlsODfN5RgU7EUju72gpltOmB7stbSB t+vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706134250; x=1706739050; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Of8y99/fZ6gvxjBPOuJPhzGycvWjT9wPG4BDRVEpUOo=; b=oSo4zG0ed5dAte76SZw0I6ZkUZISsG3dijnCJkHtQVcJy454yjff34xCReSnjcj2J6 dRr51doQcZyTEEUADGJKCFGl/uBNzAOl+/ictTlLm6sbippkrorXPG1B1/2pHw0Uifk6 FCeymgLvLipOuSehQqnOYbO9zZSrZwCC8nKJtPaLBvm5GdBHJMDzn6edNfDlL80M+sME G+M/T9+s5sWp5kppvZrxkUBpKZ2N8UbZnVkph1bMSuSRmFTs6bAgUJq4QG1XIH2/LrXz sFV0xmqCoyIFlYAgpDAnTHXFa/eMIxQRPKwml3IpcfnUcfRV8KwZGNKp2onlJUOIQfQM DrIA== X-Gm-Message-State: AOJu0YxMWT+/GYqNchILd3bRthq2yPwPMAhISU9upt8YCMYIzM/x5I6r tog6RWjBtB/xDnujvHJkWBfE7ft5D9ezI8Eb6/E/PR1MNV8xYquQdGeOY0ziwtNRG1giBi2q44k Qyg== X-Received: by 2002:a17:903:32cf:b0:1d5:a556:7662 with SMTP id i15-20020a17090332cf00b001d5a5567662mr25291plr.9.1706134249956; Wed, 24 Jan 2024 14:10:49 -0800 (PST) Received: from bsegall-linux.svl.corp.google.com.localhost ([2620:15c:2a3:200:8bb1:6f4c:997b:e7c3]) by smtp.gmail.com with ESMTPSA id ku11-20020a170903288b00b001d72846e441sm8183171plb.72.2024.01.24.14.10.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 14:10:49 -0800 (PST) From: Benjamin Segall To: Hillf Danton Cc: Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng , linux-kernel@vger.kernel.org Subject: Re: [RFC PATCH] locking/percpu-rwsem: do not do lock handoff in percpu_up_write In-Reply-To: <20240123150541.1508-1-hdanton@sina.com> (Hillf Danton's message of "Tue, 23 Jan 2024 23:05:41 +0800") References: <20240123150541.1508-1-hdanton@sina.com> Date: Wed, 24 Jan 2024 14:10:43 -0800 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Hillf Danton writes: > On Mon, 22 Jan 2024 14:59:14 -0800 Benjamin Segall >> The waitq wakeup in percpu_up_write necessarily runs the wake function >> immediately in the current thread. With it calling >> __percpu_rwsem_trylock on behalf of the thread being woken, the lock is >> extremely fair and FIFO, with the window for unfairness merely being the >> time between the release of sem->block and the completion of a relevant >> trylock. >> >> However, the woken threads that now hold the lock may not be chosen to >> run for a long time, and it would be useful to have more of this window >> available for a currently running thread to unfairly take the lock >> immediately and use it. > > It makes no sense for lock acquirer to probe owner's activity except for > spining on owner. Nor for owner to guess if any acquirer comes soon. The code is not doing that; this text is just describing why we might choose a less fair heuristic for which thread gets the lock. > >> This can result in priority-inversion issues >> with high contention or things like CFS_BANDWIDTH quotas. > > Given mutex could not avoid PI (priority-inversion) and deadlock, why is > percpu-rwsem special wrt PI? I was going to say that mutex/rwsem have SPIN_ON_OWNER that dodge this somewhat (and percpu-rwsem cannot do that). Switching cgroup_threadgroup_rwsem to an actual rwsem and even disabling read-side RWSEM_FLAG_HANDOFF doesn't actually help noticeably for my artificial benchmark though, so the test may not be as representative as I hoped. The most obvious possibility is that with the real problem solving/not-causing the internal contention issues was sufficient, and that also attacking it from the percpu-rwsem angle was overkill. It wasn't sufficient for the artificial test, but cranking up the load to get a reliable test could easily have blown past the point where the other fix was sufficient. >> >> Signed-off-by: Ben Segall >> >> --- >> >> So the actual problem we saw was that one job had severe slowdowns >> during startup with certain other jobs on the machine, and the slowdowns >> turned out to be some cgroup moves it did during startup. The antagonist >> jobs were spawning huge numbers of threads and some other internal bugs >> were exacerbating their contention. The lock handoff meant that a batch >> of antagonist threads would receive the read lock of >> cgroup_threadgroup_rwsem and at least some of those threads would take a >> long time to be scheduled. > > If you want to avoid starved lock waiter, take a look at RWSEM_FLAG_HANDOFF > in rwsem_down_read_slowpath(). rwsem's HANDOFF flag is the exact opposite of what this patch is doing. Percpu-rwsem's current code has perfect handoff for read->write, and a very short window for write->read (or write->write) to be beaten by a new writer.