Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp560973ybg; Wed, 3 Jun 2020 07:52:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxTziyU+YklXTbjTuxYKooT5KNUGRxkCPAgRfO479pnFnoz62f90r/kkl2mBbkNucadFBu1 X-Received: by 2002:a50:ccc5:: with SMTP id b5mr30260730edj.340.1591195975312; Wed, 03 Jun 2020 07:52:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591195975; cv=none; d=google.com; s=arc-20160816; b=mbJE4iSrc/Wp3N82LVSfcCTUaadTEqGPL6Kl6YePIQ+uIJBvVoVpjEvcjS4W2vt0zU L6Ur0HX6lpIb6O/oIPJFIRtdhNJbuZ99VCaqWforQEiaYYThWgN5+LBGpziXLtU7GKsk ukPDhx8MkMYrX3UrKoTDZvcsOYe4+B9FVHVNpjThTLSPt5mAvD/FFKyBuN/hhLVenDnb AGR9VxXK8/+VLmDWiEzIP4HY1hXV96tzzaohXn348+51v6TURKRr8fPGmKxEwpLkvTai yyqHVJKuAVBFzhINFdnhaMzyCdvl0KmZp0rSm2usTy9hAIPMBwJ1l6c90lSl8SSQBu8d 7Wjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=zwUTf78rbupa3eCkRgBQtMk3hMgT7VdY0EaXF5DfJvI=; b=e/gT1jzpaIVn4FYPfcd6VDNahOZkzGr071L4sWCojD58YW3PFIHljQXGqM5Zq3qu4w 9fLc/WSMWLuspihp8i+eIC3L2UGloQBCmR3kxgw933FpZzcuYoK1PiZJk2K0QvmXQVr9 opyMqIYntgEi+FQV6Exl3TVPTN/k8QE0Rr/QADw2Bb7ZB9rOY6FVYkDP5cdQ53pm2dX8 0I8+fOYpFPdx47Fu3aosv83y2BY0ndiRSnlYX0IfBF/Twb+2Gk2Z8uiCnzzP7iLbNVHO NoWyjdbJOVQYpm+66KTv6HPQq+0Whae3EESbhY1Me6Gilp14JFvLg2sRV+1NeHx18M+f hViA== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lx2si1159568ejb.284.2020.06.03.07.52.31; Wed, 03 Jun 2020 07:52:55 -0700 (PDT) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726446AbgFCOuW (ORCPT + 99 others); Wed, 3 Jun 2020 10:50:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726397AbgFCOuT (ORCPT ); Wed, 3 Jun 2020 10:50:19 -0400 Received: from Galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E962C08C5C2; Wed, 3 Jun 2020 07:50:17 -0700 (PDT) Received: from [5.158.153.53] (helo=debian-buster-darwi.lab.linutronix.de.) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1jgUiT-0001xi-Uy; Wed, 03 Jun 2020 16:50:06 +0200 From: "Ahmed S. Darwish" To: Peter Zijlstra , Ingo Molnar , Will Deacon Cc: Thomas Gleixner , "Paul E. McKenney" , "Sebastian A. Siewior" , Steven Rostedt , LKML , "Ahmed S. Darwish" , Eric Dumazet , "David S. Miller" , Jakub Kicinski , netdev@vger.kernel.org Subject: [PATCH v2 3/6] u64_stats: Document writer non-preemptibility requirement Date: Wed, 3 Jun 2020 16:49:46 +0200 Message-Id: <20200603144949.1122421-4-a.darwish@linutronix.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200603144949.1122421-1-a.darwish@linutronix.de> References: <20200603144949.1122421-1-a.darwish@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The u64_stats mechanism uses sequence counters to protect against 64-bit values tearing on 32-bit architectures. Updating such statistics is a sequence counter write side critical section. Preemption must be disabled before entering this seqcount write critical section. Failing to do so, the seqcount read side can preempt the write side section and spin for the entire scheduler tick. If that reader belongs to a real-time scheduling class, it can spin forever and the kernel will livelock. Document this statistics update side non-preemptibility requirement. Reword the introductory paragraph to highlight u64_stats raison d'ĂȘtre: 64-bit values tearing protection on 32-bit architectures. Divide documentation on a basis of internal design vs. usage constraints. Reword the u64_stats header file top comment to always mention "Reader" or "Writer" at the start of each bullet point, making it easier to follow which side each point is actually for. Clarify the statement "whole thing is a NOOP on 64bit arches or UP kernels". For 32-bit UP kernels, preemption is always disabled for the statistics read side section. Signed-off-by: Ahmed S. Darwish Reviewed-by: Sebastian Andrzej Siewior --- include/linux/u64_stats_sync.h | 43 ++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/include/linux/u64_stats_sync.h b/include/linux/u64_stats_sync.h index 9de5c10293f5..c6abb79501b3 100644 --- a/include/linux/u64_stats_sync.h +++ b/include/linux/u64_stats_sync.h @@ -3,33 +3,36 @@ #define _LINUX_U64_STATS_SYNC_H /* - * To properly implement 64bits network statistics on 32bit and 64bit hosts, - * we provide a synchronization point, that is a noop on 64bit or UP kernels. + * Protect against 64-bit values tearing on 32-bit architectures. This is + * typically used for statistics read/update in different subsystems. * * Key points : - * 1) Use a seqcount on SMP 32bits, with low overhead. - * 2) Whole thing is a noop on 64bit arches or UP kernels. - * 3) Write side must ensure mutual exclusion or one seqcount update could + * + * - Use a seqcount on 32-bit SMP, only disable preemption for 32-bit UP. + * - The whole thing is a no-op on 64-bit architectures. + * + * Usage constraints: + * + * 1) Write side must ensure mutual exclusion, or one seqcount update could * be lost, thus blocking readers forever. - * If this synchronization point is not a mutex, but a spinlock or - * spinlock_bh() or disable_bh() : - * 3.1) Write side should not sleep. - * 3.2) Write side should not allow preemption. - * 3.3) If applicable, interrupts should be disabled. + * + * 2) Write side must disable preemption, or a seqcount reader can preempt the + * writer and also spin forever. + * + * 3) Write side must use the _irqsave() variant if other writers, or a reader, + * can be invoked from an IRQ context. * * 4) If reader fetches several counters, there is no guarantee the whole values - * are consistent (remember point 1) : this is a noop on 64bit arches anyway) + * are consistent w.r.t. each other (remember point #2: seqcounts are not + * used for 64bit architectures). * - * 5) readers are allowed to sleep or be preempted/interrupted : They perform - * pure reads. But if they have to fetch many values, it's better to not allow - * preemptions/interruptions to avoid many retries. + * 5) Readers are allowed to sleep or be preempted/interrupted: they perform + * pure reads. * - * 6) If counter might be written by an interrupt, readers should block interrupts. - * (On UP, there is no seqcount_t protection, a reader allowing interrupts could - * read partial values) - * - * 7) For irq and softirq uses, readers can use u64_stats_fetch_begin_irq() and - * u64_stats_fetch_retry_irq() helpers + * 6) Readers must use both u64_stats_fetch_{begin,retry}_irq() if the stats + * might be updated from a hardirq or softirq context (remember point #1: + * seqcounts are not used for UP kernels). 32-bit UP stat readers could read + * corrupted 64-bit values otherwise. * * Usage : * -- 2.20.1