From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by sourceware.org (Postfix) with ESMTPS id 795003857C50 for ; Tue, 16 Feb 2021 09:05:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 795003857C50 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: sourceware.org; spf=none smtp.mailfrom=peterz@infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=ueDYc2cZ6bpbKw5Gc+nyHkg0hLkWmeN2VNHfX5Xo3ag=; b=K+/WiHc6o5yV4kz+dOxC4G4yUC 8uNKBREjJ4Jzye1NK4C+S1Uz/YP2nm/DFL77Q5Cmq3OB62po9MPSnmyUS+479Ndr/zQaVVG0f3SBN iKCQs+2BB74237hcpDgRoKRVaj4ZCWpOJfKjOVpxWqThLN/7Zez8qh1JvWOeKTRQnR2zl05mVGLk5 ZeIIeyGsPN7KwNUc2aFIYFACzoxRewV8R+Y0Wglm5Kwl/CdPyhVvRZ0/wL3tHKxEEOPdh/bFC2TU1 xCjT9j6TWdMWi8gYJIzbxqIlqHlFfLJIoKi1Vpz4wCKLS4WNqXrxs0O6UCSZhLeDQcjSegVQmb+g5 mm8Q5Kng==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lBwFc-00GfhV-8w; Tue, 16 Feb 2021 09:03:16 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id C67243003E5; Tue, 16 Feb 2021 10:02:28 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 2950D205A431C; Tue, 16 Feb 2021 10:02:28 +0100 (CET) Date: Tue, 16 Feb 2021 10:02:28 +0100 From: Peter Zijlstra To: =?iso-8859-1?Q?Andr=E9?= Almeida 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 Subject: Re: [RFC PATCH 01/13] futex2: Implement wait and wake functions Message-ID: References: <20210215152404.250281-1-andrealmeid@collabora.com> <20210215152404.250281-2-andrealmeid@collabora.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20210215152404.250281-2-andrealmeid@collabora.com> X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Feb 2021 09:05:14 -0000 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(). > + 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. > + > + /* > + * 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? > + * 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. > + > + 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 */ > + } > +}