From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mxout1-he-de.apache.org (mxout1-he-de.apache.org [95.216.194.37]) by sourceware.org (Postfix) with ESMTPS id 43B463858C42 for ; Wed, 13 Mar 2024 01:48:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 43B463858C42 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=apache.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=apache.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 43B463858C42 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=95.216.194.37 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710294519; cv=none; b=s4UoCv3+IB2waflp/vckU3/20UcAdZT8wulGcp8H9SR7uPP0Zl1g3UoCsmdbV4FOCRKQubZOzV9Yiu9opH6LEiczUkW5UwdtsVKdA97folXZi60JQofBz9nOeBHK9g/UC4KyesIEeR7oUXmPIh+shf/s7IyrE3wuvMVetrbTPZg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710294519; c=relaxed/simple; bh=b9sw1lscPhWzDY/NZoaFtEEjahgbhZ8cZx7pkfyvdg4=; h=Message-ID:Date:MIME-Version:Subject:To:From; b=BM2qGwh8g2j2LG7FvNaSGnE9h06Yi96FYGFqXP1syB2R5ouO0utxCA5sYcyvJHRjPfFEuGWBzotedMKZDHEZNiVMn0ap/D9+tRfoImaefkMTcA8uVveBTGCCfTfJHBqox3kkguz0/MtKgQavCdjN5McQtmCQP+TFXoUe9B+8nn0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from mail.apache.org (mailgw-he-de.apache.org [IPv6:2a01:4f8:c2c:d4aa::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mxout1-he-de.apache.org (ASF Mail Server at mxout1-he-de.apache.org) with ESMTPS id AE44162955 for ; Wed, 13 Mar 2024 01:48:36 +0000 (UTC) Received: (qmail 2972881 invoked by uid 116); 13 Mar 2024 01:48:36 -0000 Received: from ec2-52-204-25-47.compute-1.amazonaws.com (HELO mailrelay1-ec2-va.apache.org) (52.204.25.47) by apache.org (qpsmtpd/0.94) with ESMTP; Wed, 13 Mar 2024 01:48:36 +0000 Authentication-Results: apache.org; auth=none Received: from [192.168.28.122] (unknown [125.119.221.110]) by mailrelay1-ec2-va.apache.org (ASF Mail Server at mailrelay1-ec2-va.apache.org) with ESMTPSA id 24D6F3EAA2; Wed, 13 Mar 2024 01:48:34 +0000 (UTC) Message-ID: Date: Wed, 13 Mar 2024 09:48:32 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Priority Inversion and Unlimited Spin of pthread_rwlock_t To: Florian Weimer Cc: libc-alpha@sourceware.org References: <0e210b7b-5349-41fe-a476-87958990e03e@apache.org> <87jzm7tsz4.fsf@oldenburg.str.redhat.com> Content-Language: en-US From: Peng Zheng In-Reply-To: <87jzm7tsz4.fsf@oldenburg.str.redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-9.0 required=5.0 tests=BAYES_00,ENV_AND_HDR_SPF_MATCH,KAM_DMARC_STATUS,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 2024/3/12 22:39, Florian Weimer wrote: > * Peng Zheng: > >> And the SCHED_OTHER writer was just about to enable the `__wrphase_futex` in >> `__pthread_rwlock_wrlock_full` (just one ARM instruction away) >> but never able to do that (the two readers ate nearly all available CPUs): >> >> while ((r & PTHREAD_RWLOCK_WRPHASE) == 0 >> && (r >> PTHREAD_RWLOCK_READER_SHIFT) == 0) >> { >> if (atomic_compare_exchange_weak_acquire (&rwlock->__data.__readers, >> &r, r | PTHREAD_RWLOCK_WRPHASE)) >> { >> atomic_store_relaxed (&rwlock->__data.__wrphase_futex, 1); /* writer was stuck HERE! */ >> >> goto done; >> } >> /* TODO Back-off. */ >> } > > Is this about filling in the TODO? No, I forgot to remove inline comments from the original source. It is about a low priority (SCHED_OTHER) writer, which was about to acquire its lock, preempted by high priority readers, and thus was not able to set `__wrphase_futex` to 1 (see IT IS PREEMPTED HERE comment). while ((r & PTHREAD_RWLOCK_WRPHASE) == 0 && (r >> PTHREAD_RWLOCK_READER_SHIFT) == 0) { if (atomic_compare_exchange_weak_acquire (&rwlock->__data.__readers, &r, r | PTHREAD_RWLOCK_WRPHASE)) { /* IT IS PREEMPTED HERE */ atomic_store_relaxed (&rwlock->__data.__wrphase_futex, 1); goto done; } } And these two priority readers were stuck in a loop near the end of `__pthread_rwlock_rdlock_full` eating all available CPU. for (;;) { while (((wpf = atomic_load_relaxed (&rwlock->__data.__wrphase_futex)) | PTHREAD_RWLOCK_FUTEX_USED) == (1 | PTHREAD_RWLOCK_FUTEX_USED)) {/*omitted*/} if (ready) break; if ((atomic_load_acquire (&rwlock->__data.__readers) & PTHREAD_RWLOCK_WRPHASE) == 0) ready = true; } return 0; Note that PTHREAD_RWLOCK_WRPHASE was already set by the preempted writer. That means `ready` is always false. Note also that `__wrphase_futex` was not yet enabled by the preempted writer. That means these readers can not wait on futex to stop spinning. This illustrates one of the several unlimited spin possibilities and I encounter two/three of them. If you are interested, I could provide corresponding postmortem debug sessions. -- Peng Zheng