From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id BC417A0513; Sun, 8 May 2022 18:10:40 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 690984068F; Sun, 8 May 2022 18:10:40 +0200 (CEST) Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by mails.dpdk.org (Postfix) with ESMTP id 655B440395 for ; Sun, 8 May 2022 18:10:39 +0200 (CEST) Received: by mail-pj1-f52.google.com with SMTP id p6so11149107pjm.1 for ; Sun, 08 May 2022 09:10:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UxhkEp2EwmepUG2qLE5lM4ywIpgyujjP6NeRYfYpaTs=; b=B2OD16WlbM3aCYMQrKewjEKDgAl/iajjbx1Yc/kKbXi9gx5rU0r4FAPyamA66GiBF/ G2oh1JUqM1GOsDr137140pdfs8h+Uh1yj7TMg5c+aCWOW+u2Iu6/mYX/5RZrGBt+AwwS MngEztCvAbb10M8lVa+C5zgYIU7kf4TelUlwWAX1vWrvC00Js7Fc78v4ejqPuokyaVmJ DVC++3MKDo/WkNUeZuc1VFEYiPDsZXiACKYRWc27KM+GoKbR0Y4cPGfzFFFHnSmWrJye OV821UHd2NGyUVBcqEzb9LkFPzqPtRLAsroJ2pxzkEwU4KMPmSubCOsLteMhjcqwOKb2 mBEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UxhkEp2EwmepUG2qLE5lM4ywIpgyujjP6NeRYfYpaTs=; b=aUEwHHB+iD0Ifhsw7E7VBktSaUzfHC1i+u4ft/+NY78sxTIcDME/MYom9G3YJomUr8 PTa68FX5+VwLb+EXzGRMAKGRJjqYED+yDCg+4KtXx8kqlaotH4kkqqb4Tb5CTK/l4sw1 GGI31DkQZue1/z4ycwQBdlCsVPHlTZVQ+UPJx3SLfCplul1ybiD8HdtyufmGl8R13jhH w57fZRHvQGCkDRDklLHZo7DFt1qOCw0tXTs1FZjFnycey4QM2QHJYd+m/nIK7TbMx0Uq Rx5kbvNui6SJlLr4Fu+Q356yRgAeLgKRFr3cvC6LJxjCjiAJzPXUI2568N8SAEhqU/gm pwtw== X-Gm-Message-State: AOAM530mjvC8BI5vSODIL0TC6zZi0p7pLyeOhpTQVpno40gLC9c/yjGd AXAd+enKeXJ77xDjbjbwAu6gAQ== X-Google-Smtp-Source: ABdhPJxixhuhOb1JUSZDYIJMW1uL9QHjZBfoefhGgWKOUxHrKipIqNGoeV9zCczx8c4zj/ZGeFA32w== X-Received: by 2002:a17:902:ef46:b0:153:81f7:7fc2 with SMTP id e6-20020a170902ef4600b0015381f77fc2mr12539865plx.26.1652026238319; Sun, 08 May 2022 09:10:38 -0700 (PDT) Received: from hermes.local (204-195-112-199.wavecable.com. [204.195.112.199]) by smtp.gmail.com with ESMTPSA id l11-20020a170903120b00b0015e8d4eb21esm5369668plh.104.2022.05.08.09.10.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 09:10:37 -0700 (PDT) Date: Sun, 8 May 2022 09:10:34 -0700 From: Stephen Hemminger To: Mattias =?UTF-8?B?UsO2bm5ibG9t?= Cc: Thomas Monjalon , David Marchand , , , , , , , , Chengwen Feng , Ola Liljedahl Subject: Re: [PATCH v6] eal: add seqlock Message-ID: <20220508091034.53b23b3e@hermes.local> In-Reply-To: <20220508121242.290008-1-mattias.ronnblom@ericsson.com> References: <20220508121242.290008-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org On Sun, 8 May 2022 14:12:42 +0200 Mattias R=C3=B6nnblom wrote: > A sequence lock (seqlock) is a synchronization primitive which allows > for data-race free, low-overhead, high-frequency reads, suitable for > data structures shared across many cores and which are updated > relatively infrequently. >=20 > A seqlock permits multiple parallel readers. The variant of seqlock > implemented in this patch supports multiple writers as well. A > spinlock is used for writer-writer serialization. >=20 > To avoid resource reclamation and other issues, the data protected by > a seqlock is best off being self-contained (i.e., no pointers [except > to constant data]). >=20 > One way to think about seqlocks is that they provide means to perform > atomic operations on data objects larger than what the native atomic > machine instructions allow for. >=20 > DPDK seqlocks are not preemption safe on the writer side. A thread > preemption affects performance, not correctness. >=20 > A seqlock contains a sequence number, which can be thought of as the > generation of the data it protects. >=20 > A reader will > 1. Load the sequence number (sn). > 2. Load, in arbitrary order, the seqlock-protected data. > 3. Load the sn again. > 4. Check if the first and second sn are equal, and even numbered. > If they are not, discard the loaded data, and restart from 1. >=20 > The first three steps need to be ordered using suitable memory fences. >=20 > A writer will > 1. Take the spinlock, to serialize writer access. > 2. Load the sn. > 3. Store the original sn + 1 as the new sn. > 4. Perform load and stores to the seqlock-protected data. > 5. Store the original sn + 2 as the new sn. > 6. Release the spinlock. >=20 > Proper memory fencing is required to make sure the first sn store, the > data stores, and the second sn store appear to the reader in the > mentioned order. >=20 > The sn loads and stores must be atomic, but the data loads and stores > need not be. >=20 > The original seqlock design and implementation was done by Stephen > Hemminger. This is an independent implementation, using C11 atomics. >=20 > For more information on seqlocks, see > https://en.wikipedia.org/wiki/Seqlock I think would be good to have the sequence count (read side only) like the kernel and sequence lock (sequence count + spinlock) as separate things. That way the application could use sequence count + ticket lock if it needed to scale to more writers. > diff --git a/lib/eal/common/rte_seqlock.c b/lib/eal/common/rte_seqlock.c > new file mode 100644 > index 0000000000..d4fe648799 > --- /dev/null > +++ b/lib/eal/common/rte_seqlock.c > @@ -0,0 +1,12 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright(c) 2022 Ericsson AB > + */ > + > +#include > + > +void > +rte_seqlock_init(rte_seqlock_t *seqlock) > +{ > + seqlock->sn =3D 0; > + rte_spinlock_init(&seqlock->lock); > +} So small, worth just making inline?