From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF382C0015E for ; Fri, 23 Jun 2023 15:38:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E2638D0002; Fri, 23 Jun 2023 11:38:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 292708D0001; Fri, 23 Jun 2023 11:38:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 15AA98D0002; Fri, 23 Jun 2023 11:38:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 032228D0001 for ; Fri, 23 Jun 2023 11:38:51 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D42F81C9001 for ; Fri, 23 Jun 2023 15:38:49 +0000 (UTC) X-FDA: 80934420378.21.38A38EB Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by imf27.hostedemail.com (Postfix) with ESMTP id DBE5940005 for ; Fri, 23 Jun 2023 15:38:47 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=p6td+HXe; spf=pass (imf27.hostedemail.com: domain of pmladek@suse.com designates 195.135.220.28 as permitted sender) smtp.mailfrom=pmladek@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687534728; a=rsa-sha256; cv=none; b=Q0jgj8Es69/EgS180U6NAtTO6051XnAwvQarV8HJBoOFgZZ2r4dVkL8mHS9eCi/plqh0E4 raBUEVnOjUP0yt2OA8mb2ky5IZFVg6LPcZOCdHFVgMBQO9+FwZoqtqXGZoPoPAwutkvu8N YV+1YNLWICDoHbgbsBM6zZhzgZhU4vM= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=p6td+HXe; spf=pass (imf27.hostedemail.com: domain of pmladek@suse.com designates 195.135.220.28 as permitted sender) smtp.mailfrom=pmladek@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687534728; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YaD6tcW+W2a3m29m28IbRYTT+8UkLfXZyK8IAE7sI2w=; b=f2UTN0mgLe/CoeJRQi5A6qG2NBbhgJMy+P8/30v+9TUbWMs+btksnFjZvGD95WClYXCMUI BKxe2GAKcck4shI8TFPg41ihk32wq0looQIb5O4bHwc5mBQuAnpN40VhAHjsOqCkMiL/dq gx9pssJsIMcZ+bqAZ99AWovG5b8oNs0= Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 398C821867; Fri, 23 Jun 2023 15:38:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1687534726; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=YaD6tcW+W2a3m29m28IbRYTT+8UkLfXZyK8IAE7sI2w=; b=p6td+HXeTGMO4ZiH14p+dZvJYGzP69mMlNuUE1yQ1caKYQT6Ym+ne4aIsfEc/uJD1RO0Ut vwxzfvE6PXaEaftjSMrsHkMJYAconhlpRFIXSYa5xDUp/kJavzqpgUG3rRejJDXJFrVqXa G/w4tZzT/N0HcteH4u/3P2SQa7dTnc8= Received: from suse.cz (unknown [10.100.201.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id A19392C141; Fri, 23 Jun 2023 15:38:45 +0000 (UTC) Date: Fri, 23 Jun 2023 17:38:45 +0200 From: Petr Mladek To: Sebastian Andrzej Siewior Cc: Tetsuo Handa , linux-mm@kvack.org, "Luis Claudio R. Goncalves" , Andrew Morton , Mel Gorman , Michal Hocko , Thomas Gleixner Subject: Re: [PATCH] mm/page_alloc: Use write_seqlock_irqsave() instead write_seqlock() + local_irq_save(). Message-ID: References: <8b6d3f39-c573-ca2b-957b-8c48c2fa68ad@I-love.SAKURA.ne.jp> <20230623094544.RcLNdg6H@linutronix.de> <20230623101111.7tuAg5p5@linutronix.de> <20230623133127.fJ8Hf29t@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230623133127.fJ8Hf29t@linutronix.de> X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: DBE5940005 X-Stat-Signature: fh35hjh15s7b1yim17b6ozm6szshxajs X-Rspam-User: X-HE-Tag: 1687534727-903444 X-HE-Meta: U2FsdGVkX19p6YHDawzstktwtJePEZnEzfShNBm6wixE8Zvh2aPvjQIh4sTRgGKXTnkUcamOM7MY3sQgXmFRHAEhrYLiKE1SJVs0ZP6Lgn5FkkpGSZNRLhu+nIRwKtKkIBHCmAbq69SaWn4YBXLI6xxXRK8oPItDej+zJ8PJf5d1YXZxDkQF9Vk+Hv39NSNCpj9IgsFjiKP5VfdRCmX4vwei9cQLGSFXn2oBx1WqlJV8P5TKi9rWlaTw571oGPIMPfTu3O2+MGOGsFJmjfX/+ekzC+15ghK/QKSM8ZKh5ktxM9UbACy3ztUBMfoQ1weDE5IlH+MWHK0uodNK+7OZ5dHMx8CunWWjuUtD5Bf+rvMv94CW8dtvL9OiIUCXwDYM59kR8fBAoXVQnvgVepsQTfoVEqAW14zG86pxP/Zvrln7Ahuz68i9SCLg/JSZBlBD5dJFuDZE2MUUaQtiUgUgVzp9IFHZlSL3RrrTON4hjOsj3L8zjzbEV5TmMdZgl47DcBq+InuumXHLHK9BgGVCki8LDdFpJBxvN9xw3VtTA0vZPb2fJLvGDQgpc/m0HWbuTpZvkQ6mXVB798I+RtmTSZ6VhnIA3vEQIeEB8rgc6vgknYYBIEfO2Sio7jane5yB+9jGBX7JZVFeHJ+w4KQp1h5pS3iTH/LqTG0j65aAmwxE7uOBzAchwsbgaIug0OhDvdW2NPvDO1xYSt95HdTp+Cv49S6LZZw9mz7PSYMMhtAgG75wZsDGVdfFJr9rOsRAjxj2RIS9mFLoiVOfDW7rhPhhEN2x2T54YXPagfu5+p3tkBA124RrKfUll6aWzJgOD35GLnNM4ssiiQe7CfIzSoxya6iv0bRkYlViZrx3migEF/K6E+S7vYmZN68/v1VEcG23nZlbPZ66u1P8UZEd8VYaHbrF1PQGq3hoXqchcN0JhifQ0b+8kiy2NGfPZqxbna6ZW9bZJxxi2Nqojd1 V12PNIsq sj4oGSI7vFojeNAWIqozM9eKFY8pb1x8LDKsgM4WRKpIk3Fw7EpTDIYiTDDc16qchtTR0qwOjuc5h42ARr2srhMj+8FBYn9mK0AoRNKGGVurcMZENPF0os1qsC7Bj7q3r7iCQIwiQY8tl7Dx+r0xJxdvHDdZvu74rKVjiccl3MidP5W2FSEsJCxYEuHbD9kxRX2dI X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri 2023-06-23 15:31:27, Sebastian Andrzej Siewior wrote: > On 2023-06-23 12:53:57 [+0200], Petr Mladek wrote: > > BTW: I see a possible race in the below code. > > > > > | unsigned __seqprop_spinlock_sequence(const seqcount_spinlock_t *s) > > > | { > > > | unsigned seq = READ_ONCE(s->seqcount.sequence); > > > | > > > | if (unlikely(seq & 1)) { > > > | spin_lock(s->lock); > > > | spin_unlock(s->lock); > > > > This guarantees the reader waits for the writer. But does this > > guarantee that another writer could not bump the sequence > > before we read it below? > > A second writer can bump the seq after the first is done, correct. > > > > | seq = READ_ONCE(s->seqcount.sequence); > > > > IMHO, it should be: > > > > if (unlikely(seq & 1)) { > > spin_lock(s->lock); > > seq = READ_ONCE(s->seqcount.sequence); > > spin_unlock(s->lock); > > > > IMHO, only this would guarantee that returned seq couldn't be odd. > > This doesn't buy us anything. Sure? > If the writer increments the counter, > after the reader unlocks, then the reader observers the odd seq number > and locks immediately (within read_seqbegin()) on the lock without > spending cycles within the read section (outside of read_seqbegin()) and > learning about the seq increment in read_seqretry(). I am afraid that it will not get caugh: static inline int do___read_seqcount_retry(const seqcount_t *s, unsigned start) { kcsan_atomic_next(0); return unlikely(READ_ONCE(s->sequence) != start); } It checks if the sequence is the same. It does not check if it is odd. But it might be odd if you read "start" outsize of the lock and do not check if it is odd. I do not feel to be expert but a code: spin_lock(s->lock); spin_unlock(s->lock); s = locked_variable; just triggers many bells in my head. It is like from a book "Classic locking mistakes". With the code: spin_lock(s->lock); seq s->seqcount.sequence; spin_unlock(s->lock); you will always get the right value without any extra cost. The code took the lock anyway. And the single assignment is negligible. And RT could schedule inside the spin_lock anyway. Or do I miss anything? Best Regards, Petr