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 7812CEB64D7 for ; Fri, 23 Jun 2023 16:07:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C9BD68D0002; Fri, 23 Jun 2023 12:07:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C4B8D8D0001; Fri, 23 Jun 2023 12:07:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B13DA8D0002; Fri, 23 Jun 2023 12:07:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id A06918D0001 for ; Fri, 23 Jun 2023 12:07:54 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 62C1EC096F for ; Fri, 23 Jun 2023 16:07:54 +0000 (UTC) X-FDA: 80934493668.10.D96A264 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by imf19.hostedemail.com (Postfix) with ESMTP id CD1621A02F4 for ; Fri, 23 Jun 2023 16:04:34 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=T2OuTBZs; dkim=pass header.d=linutronix.de header.s=2020e header.b=GeP62kQJ; dmarc=pass (policy=none) header.from=linutronix.de; spf=pass (imf19.hostedemail.com: domain of bigeasy@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=bigeasy@linutronix.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687536275; 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=8985B142eFvXnVJAwi+cpAyaFpyJwYdpP0zmNyjAekc=; b=p53u5nrNbWKq2F+J1W2MnuBefi6wfJNpxL4EUjE9EtnuVw71U4MFOOdJr/8p2ApDnt3iI7 OQTKfWvf+OnaNj1QE3b+Z5LY3HRijvHGvmf7fTguR65Z4QbUhGPB7vPxgd6kuMnUjyJQIL Q5/HsYz7u5tlUxz76LXEA2veraDXXqY= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=T2OuTBZs; dkim=pass header.d=linutronix.de header.s=2020e header.b=GeP62kQJ; dmarc=pass (policy=none) header.from=linutronix.de; spf=pass (imf19.hostedemail.com: domain of bigeasy@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=bigeasy@linutronix.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687536275; a=rsa-sha256; cv=none; b=MwecH9R/pNLHkOTFAuPxrwIeTzV8uScfDLuFjs36N457/kdXMI+zs/sV7HqBxPZRb2UhDp 2uHNuJQ3q0HVTukI+2KHZOBpUdwaaOIcb81YPdHDlp8A0hTsl9MGYaYi80wQQ5vIAajbKy S9Vtm17pZYqSy4stNEUnBp1uQYqjlAo= Date: Fri, 23 Jun 2023 18:04:29 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1687536272; h=from:from:reply-to:subject:subject: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=8985B142eFvXnVJAwi+cpAyaFpyJwYdpP0zmNyjAekc=; b=T2OuTBZsiPoXHSstCTTJyxW9Tj6fAOVjC/OwJgnFKVWPiOHrDmRxkBshgtTcVkLTTxouRm wwpEk25x8TtHXEWxPgdLVNHhOmmvEglSGKBdqQocaxs3ckGVBM7odYOd5WpzjpP4Q9BbDI ScIyGmsZ5G/0/hj/ufTACo6fuYB6UYtCiGlwOYJFfmRsHdoS/XTIrkBRkXp4rWt4YZOMpx aYQuDNecFKb/1KaOse8epjpGNGcvxzDo5MLA9/7LK+i+XhdGxmOSpz3XdZPOPdT+HN9eLV qM5l/8ULd2RnedsNH23DnXZoaSVzB3aTC09FnI50wUF4iVCHNOfChnJ0QNMcgw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1687536272; h=from:from:reply-to:subject:subject: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=8985B142eFvXnVJAwi+cpAyaFpyJwYdpP0zmNyjAekc=; b=GeP62kQJ1keUefinO/DUr7iPani+KHjNgKyKLcCD7LQvVwQAbzqxQN3JqRlQVik/fzBViN btd2J4VaT4dBSLBg== From: Sebastian Andrzej Siewior To: Petr Mladek 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: <20230623160429.0bUesCCL@linutronix.de> References: <20230623094544.RcLNdg6H@linutronix.de> <20230623101111.7tuAg5p5@linutronix.de> <20230623133127.fJ8Hf29t@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: CD1621A02F4 X-Stat-Signature: 819b9i9z5na96hic7t17y753fw3hgt3f X-HE-Tag: 1687536274-327060 X-HE-Meta: U2FsdGVkX19j6rq+WPV6RpxcMpXKt+zbFBvoeRMbg+kZIXzJyYsogxHWdVg9JCMN2Mj1/BrW+lXtEy/s8CYLJGh7tCo7nXI2U7ubQL01pMO63uO20LbKkabDHvxL5eL5c8m4/7PXsna394lUqopE0er3zGAJBPm9drdZpHDBPUQLAXi5LVCuilG4Ebxx9c6D4kwyYrtbOCcrMSYje9iyA/nokljS7Vu4b7+wnnwz628pSg/gbaZX783bo8OiFgHlv5+aPbakfOLmhCpZwUbiL6lbiU6ZyUAXBfpGkMcF/34+sRiseOOa8Sp8V9GM1Zzb73Wtllw3HEnWZOWRhBAXttU3bWuUXLZjSRLzE8n7MULt36W0u/5I4OibxlJ5utDsu2bfv//CbGnf4FtED9EmUCB2d3xWzhfFnYnlx1v7AUpZ8cYjYzZi9c6NcifmhOsBweOaplbaWTCeWWWBXie6kYAVpCNfITBMSVDuwiXCFowSdk3IR4ShJJrQYIIwk0i41/afycFcdb/lbTzOcBPWwbykB9hmbG8VrGuA3HAr/BNW2KUSUIjslOdJDpcaHM/Xw2TSgo6k8SAGWTYAtI8f62MWP8poX4hjxg5tugkf39pk+wo2WQYoGKRvZ+E2M5X4KQhtzl/6mNtjp1PieSKvmH5H/4Q1La7+4RdKE+0WpFBs2QKGIX/RPM7cJNYM7q3I0zFx5PUUqEF4T8Aj4KyRbxEE3zQGBt7GWAMq6Oga8QIoohnzs5f5AfvJkh/tUuy9M7n3IbmknhYY77R3td+optHWOh+b6X3CVcnCV8jfvsScwDv86sSR8lYTVfFQseNIdAsIu9zPMihoSPu2oqjmmtnNd7Vf2L5XeLQut9IbwQnXPYzprqO6AArpJ8ls5avdsHulct2pIC1rnT5BeBbCyeU2hIuyliJhKW56oKXBKYFNLgSrQb7gBTkDgt3Jhy2Pr9rBFZi0oSxl59MWahv a7Zciiyc oEiB75E+9YILyiIvGR5bvysn2UvY14hkhNcPbO70rzneGOSYVz4W0+fIcw+5AC74PZQOkzYUzjFQ2rjN2/YKHlWFY53dl+7ah5OSUHBVz0dEXUXC0ChxNBYwWAfXR5u4TytGUVBUPNUX2xBA= 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 2023-06-23 17:38:45 [+0200], Petr Mladek wrote: > > 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. You do realise that __read_seqcount_begin() is | while ((__seq = seqprop_sequence(s)) & 1) \ | cpu_relax(); \ so the code you try to modify is within seqprop_sequence() so you _do_ loop until the resulting __seq is even. And only then you continue towards do___read_seqcount_retry(). > But it might be odd if you read "start" outsize of the lock and do > not check if it is odd. no, see above. > 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". It is a counter and not locked variable and it is designed to be read outside of the lock. The usage of the lock is not for "protecting" as locking reasons but for making progress. > 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? Yes. See above. If after the unlock the read sequence is odd again we do notice it. Doing it from within the locked section, we don't. > Best Regards, > Petr Sebastian