Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp7519670pxb; Thu, 18 Feb 2021 12:15:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJx78ILTiy7yhuPgUHD6UZzlYLV68i5NWVP9BThKz1bUeyZJHLsDCNkQGISM54vL9da9YXV/ X-Received: by 2002:a05:6402:430c:: with SMTP id m12mr5948382edc.299.1613679318319; Thu, 18 Feb 2021 12:15:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613679318; cv=none; d=google.com; s=arc-20160816; b=AE5AQqLj1v1BnqRgMSZ2lpPJ8jaQ+wQdoYFeS6C77Wt+DVHEjcjrcILAFu8rKY76sz LSc3Hk4sehXQ+w/jNzPVzXo7ylLVuGnWZdx2uODfdH1zvj170QwnhTf8VU1dS9XUL7W3 tbniJYOoqkcATdzsieFHQqlri63bxrZgV6chySM1wf5Bs9977pVjDIoCoTH3Ny6vvjlP s4L48tzhZPLg42KMAAIJP07tMJQ5JLTgM0nt28x2kyyJp9ffUWSlMgK8cvANccsP3VZV HbfHultpCkj+yKDlL8FAjSxLRWNuntcOSqRBFnQ4e79MU1xaKModRLYCD4bxVSv8ugfv Fnsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=8KitgKFnD/Gm9kz0P0DZhPfz0gTRFpLGKUzEMaHaYs8=; b=v/DjiZNMk6MmgDJbc1UjWl1aBSBWTvR12/oztQV5PIV05O7BXNYOlBpH4uoNZFoqdm OIm51m7B2pO3/Z8w+CDKgen98uL0t9KMh8h0hFrJT1cbJ5ynJFbB31i3mUVtG1nalIpF mLwgNUkgVta8m5MonBW0ZPAszZyXE3ViypEBtN8MXkkbMHM8bjRJmz9Q7QVYc3i+lbTt 8VE+FDIPRLHuOPTOqylopKlEfNKifCaG6hR90AxsS/EDOvDeUg+56N1Z8bjeiLcSBFQx uenr/5zsJJx2ICXivqZDxhsPmczGMKBKmCBkicCG1ZgXej81dWpWjTanJqgjCy1D5EiX E2wg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c5si4250934edk.64.2021.02.18.12.14.53; Thu, 18 Feb 2021 12:15:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231482AbhBRUMJ (ORCPT + 99 others); Thu, 18 Feb 2021 15:12:09 -0500 Received: from bhuna.collabora.co.uk ([46.235.227.227]:43052 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231441AbhBRUJx (ORCPT ); Thu, 18 Feb 2021 15:09:53 -0500 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: tonyk) with ESMTPSA id 482F51F45D91 Subject: Re: [RFC PATCH 01/13] futex2: Implement wait and wake functions To: Peter Zijlstra Cc: Thomas Gleixner , Ingo Molnar , Darren Hart , linux-kernel@vger.kernel.org, Steven Rostedt , Sebastian Andrzej Siewior , kernel@collabora.com, krisman@collabora.com, pgriffais@valvesoftware.com, z.figura12@gmail.com, joel@joelfernandes.org, malteskarupke@fastmail.fm, linux-api@vger.kernel.org, fweimer@redhat.com, libc-alpha@sourceware.org, linux-kselftest@vger.kernel.org, shuah@kernel.org, acme@kernel.org, corbet@lwn.net References: <20210215152404.250281-1-andrealmeid@collabora.com> <20210215152404.250281-2-andrealmeid@collabora.com> From: =?UTF-8?Q?Andr=c3=a9_Almeida?= Message-ID: Date: Thu, 18 Feb 2021 17:09:02 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Peter, Às 06:02 de 16/02/21, Peter Zijlstra escreveu: > On Mon, Feb 15, 2021 at 12:23:52PM -0300, André Almeida wrote: >> +static int __futex_wait(struct futexv_head *futexv, unsigned int nr_futexes, >> + struct hrtimer_sleeper *timeout) >> +{ >> + int ret; >> + >> + while (1) { >> + int awakened = -1; >> + > > Might be easier to understand if the set_current_state() is here, > instead of squirreled away in futex_enqueue(). > I placed set_current_state() inside futex_enqueue() because we need to set RUNNING and then INTERRUPTIBLE again for the retry path. >> + ret = futex_enqueue(futexv, nr_futexes, &awakened); >> + >> + if (ret) { >> + if (awakened >= 0) >> + return awakened; >> + return ret; >> + } >> + >> + /* Before sleeping, check if someone was woken */ >> + if (!futexv->hint && (!timeout || timeout->task)) >> + freezable_schedule(); >> + >> + __set_current_state(TASK_RUNNING); > > This is typically after the loop. > Sorry, which loop? >> + >> + /* >> + * One of those things triggered this wake: >> + * >> + * * We have been removed from the bucket. futex_wake() woke >> + * us. We just need to dequeue and return 0 to userspace. >> + * >> + * However, if no futex was dequeued by a futex_wake(): >> + * >> + * * If the there's a timeout and it has expired, >> + * return -ETIMEDOUT. >> + * >> + * * If there is a signal pending, something wants to kill our >> + * thread, return -ERESTARTSYS. >> + * >> + * * If there's no signal pending, it was a spurious wake >> + * (scheduler gave us a change to do some work, even if we > > chance? Indeed, fixed. > >> + * don't want to). We need to remove ourselves from the >> + * bucket and add again, to prevent losing wakeups in the >> + * meantime. >> + */ > > Anyway, doing a dequeue and enqueue for spurious wakes is a bit of an > anti-pattern that can lead to starvation. I've not actually looked at > much detail yet as this is my first read-through, but did figure I'd > mention it. > So we could just leave everything enqueued for spurious wake? I was expecting that we would need to do all the work again (including rechecking *uaddr == val) to see if we didn't miss a futex_wake() between the kernel thread waking (spuriously) and going to sleep again. >> + >> + ret = futex_dequeue_multiple(futexv, nr_futexes); >> + >> + /* Normal wake */ >> + if (ret >= 0) >> + return ret; >> + >> + if (timeout && !timeout->task) >> + return -ETIMEDOUT; >> + >> + if (signal_pending(current)) >> + return -ERESTARTSYS; >> + >> + /* Spurious wake, do everything again */ >> + } >> +} Thanks, André