Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp797696lqm; Wed, 1 May 2024 16:45:25 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXvFL4bDRckmJXEk6zzIv9d86eWuDfCuiDNVF0doefWcGidxeMZLsqA1IsamDElfkiWuMrnOjpsQmX9gi0wlwxBYA99ya0GTK4WUaVg+w== X-Google-Smtp-Source: AGHT+IGbPU1a0J5AK2lG15Xu49RMZfrFm3gLdmFiZk9rS5tQQzD5zo6qWyu+GZUXqkp3H/0csDlD X-Received: by 2002:adf:ce0f:0:b0:34c:6933:280f with SMTP id p15-20020adfce0f000000b0034c6933280fmr3004893wrn.23.1714607124849; Wed, 01 May 2024 16:45:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714607124; cv=pass; d=google.com; s=arc-20160816; b=owBQI61hoNN5uI4t6CORabcLNVelQh77FN/9snAfsawNyX5eOTbe6gBDF/N8r8Q3lc aTxuzwwjDjOjrNaFVR6510lPe39CG5Zei9oI3KJ8lorj3YtNFRtHst7r4piogS3TY7rX EByhy5xgIuje3CItlwjScC7u7irPwsuv+a8wLrT0Uht94eqtFo/BEACwjtSwVf567AYu MHsOW9U33A2SB57dj6bxXsECMrb7Kcw/eG5o6MLOEnMTKNTbLX2DKsR7VQYVrGjmnGGA d48wDvN3tz74WfGO5+Aq/UrMc89uZi1GAm1Ub1e8URw56yQf2laCXjpRBuzYnzXRd4qZ KWuw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=Ut6b0SDeFkL5LMt8SlT7mycy5WHbPm1GhBrY2OAen2o=; fh=/mdFTQBoGK1WioVLcVXJKrQ9lO45Mcc4x5GyBrCVjlU=; b=HOqe1aBryfSg19qSIY5U1t8zckS6ogHIjwyAeBwsNsf6kvVQgwCKWukLTS1TW6lT4F Mr5Mdi3XEHA8R7M6Xy3PZiQJLafD8FMQOYlPzjM3gwDVkgzblkiLJ7yUBrNeIlQW6cqf a88TQO2uRC0F7IjpIMbDMH5ipiGEAkBFkhfGdCs+vsTYTZF64Xg+apul9TbJ1K8tap7i Y5meZHT6nmhIiUKWvK8Uu+Qmn1gHM9gs/7SHO0wa7AU3uNt4cvOXBs7aTAIWlk19XnQH rjdNWmE82XkFfqd2gm3+G+z1o9wk96UFbkr8iAbIIu/StqvCTRz1MqbaH6gcC75FjeY5 uvIw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@igalia.com header.s=20170329 header.b=CjKthMws; arc=pass (i=1 spf=pass spfdomain=igalia.com dkim=pass dkdomain=igalia.com); spf=pass (google.com: domain of linux-kernel+bounces-165858-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165858-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id hg6-20020a1709072cc600b00a58f9cecfa5si4978170ejc.407.2024.05.01.16.45.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 16:45:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165858-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=fail header.i=@igalia.com header.s=20170329 header.b=CjKthMws; arc=pass (i=1 spf=pass spfdomain=igalia.com dkim=pass dkdomain=igalia.com); spf=pass (google.com: domain of linux-kernel+bounces-165858-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165858-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 8AA981F222D1 for ; Wed, 1 May 2024 23:45:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A2D4816ABC2; Wed, 1 May 2024 23:45:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b="CjKthMws" Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) (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 0DFE742A96; Wed, 1 May 2024 23:45:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.60.130.6 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714607117; cv=none; b=SXGHDzsKhtY785H1j5tDaMuuHOUxJtB/jKyRaQ7W6moeckR/APWVLsQlPRfGCn3bzPfU0PtVSr7/dEAC9kddv8k49puc6z+VOPy3Z7oqu3pUztsX7Q/OW3aIDFNtuJwSxW1I5qrtiLaWC87MD6M8tu0sekWc/O7+OBj+uuos3J4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714607117; c=relaxed/simple; bh=G4pqX1RMSl4RCZYG0IsdfgPoTzDclcLMoFT8Uh1hu1Q=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=LBU0NQc69g3pTNCDVPBDCReUIh+2e3Ud52ZaO6LhNDZ+aNN1MMRKk4gYTRBOEsMPX7TFm+7yAPqDHM8u1Tuhilu54xG/qXmo6BswN17bkdRIEuL/8zKjWzSHZnds5cHoZ9zFrCLkk5Pm0LnkGDWf5CecVepUoeUS9mSvyPdZQNk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b=CjKthMws; arc=none smtp.client-ip=178.60.130.6 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:In-Reply-To:From: References:Cc:To:Subject:MIME-Version:Date:Message-ID:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Ut6b0SDeFkL5LMt8SlT7mycy5WHbPm1GhBrY2OAen2o=; b=CjKthMwsLJXjsFlsgkKRHaOJGD 2WpPy8u5p46Z1R6MfHGCNl48S1JRNPwHK/cJmsjI7Rzn0UeF2TCRXmsf7g74zGEVBNe1oOxUqXgDg 5VOqxkfsWnmKV6GH/3m/mosxHIqKm2Ic7z3aZpeM2PdYxUHYkF7yzJ5epiVQ90DSDtuFXoO6pxXhH XYwnaK+5yaH1F7DC/ejrh2YDh4d/8G3C0iUl33yXtZpBWtVy7qi5W8RvQLPLKTOwHTaTCAUt8CoHP 0mPbb51pBnan9zydhbUMlyF5qpLdVBnAfBhuLPhWtN4Ue0wIDcD6bikaB0Byd+YfAkwn3Nqg9LePM o/emag1w==; Received: from 201-42-129-95.dsl.telesp.net.br ([201.42.129.95] helo=[192.168.1.111]) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_128_GCM:128) (Exim) id 1s2Jd0-002mpu-CW; Thu, 02 May 2024 01:44:46 +0200 Message-ID: Date: Wed, 1 May 2024 20:44:36 -0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH 0/1] Add FUTEX_SPIN operation To: Christian Brauner Cc: Mathieu Desnoyers , Peter Zijlstra , Thomas Gleixner , linux-kernel@vger.kernel.org, "Paul E . McKenney" , Boqun Feng , "H . Peter Anvin" , Paul Turner , linux-api@vger.kernel.org, Florian Weimer , David.Laight@aculab.com, carlos@redhat.com, Peter Oskolkov , Alexander Mikhalitsyn , Chris Kennelly , Ingo Molnar , Darren Hart , Davidlohr Bueso , libc-alpha@sourceware.org, Steven Rostedt , Jonathan Corbet , Noah Goldstein , Daniel Colascione , longman@redhat.com, kernel-dev@igalia.com References: <20240425204332.221162-1-andrealmeid@igalia.com> <20240426-gaumen-zweibeinig-3490b06e86c2@brauner> Content-Language: en-US From: =?UTF-8?Q?Andr=C3=A9_Almeida?= In-Reply-To: <20240426-gaumen-zweibeinig-3490b06e86c2@brauner> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi Christian, Em 26/04/2024 07:26, Christian Brauner escreveu: > On Thu, Apr 25, 2024 at 05:43:31PM -0300, André Almeida wrote: >> Hi, >> >> In the last LPC, Mathieu Desnoyers and I presented[0] a proposal to extend the >> rseq interface to be able to implement spin locks in userspace correctly. Thomas >> Gleixner agreed that this is something that Linux could improve, but asked for >> an alternative proposal first: a futex operation that allows to spin a user >> lock inside the kernel. This patchset implements a prototype of this idea for >> further discussion. >> >> With FUTEX2_SPIN flag set during a futex_wait(), the futex value is expected to >> be the PID of the lock owner. Then, the kernel gets the task_struct of the >> corresponding PID, and checks if it's running. It spins until the futex >> is awaken, the task is scheduled out or if a timeout happens. If the lock owner >> is scheduled out at any time, then the syscall follows the normal path of >> sleeping as usual. >> >> If the futex is awaken and we are spinning, we can return to userspace quickly, >> avoid the scheduling out and in again to wake from a futex_wait(), thus >> speeding up the wait operation. >> >> I didn't manage to find a good mechanism to prevent race conditions between >> setting *futex = PID in userspace and doing find_get_task_by_vpid(PID) in kernel >> space, giving that there's enough room for the original PID owner exit and such >> PID to be relocated to another unrelated task in the system. I didn't performed > > One option would be to also allow pidfds. Starting with v6.9 they can be > used to reference individual threads. > > So for the really fast case where you have multiple threads and you > somehow may really do care about the impact of the atomic_long_inc() on > pidfd_file->f_count during fdget() (for the single-threaded case the > increment is elided), callers can pass the TID. But in cases where the > inc and put aren't a performance sensitive, you can use pidfds. > Thank you very much for making the effort here, much appreciated :) While I agree that pidfds would fix the PID race conditions, I will move this interface to support TIDs instead, as noted by Florian and Peter. With TID the race conditions are diminished I reckon?