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 304D442538; Thu, 7 Sep 2023 18:10:47 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 0C02A402B0; Thu, 7 Sep 2023 18:10:47 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id 4C3B6400EF for ; Thu, 7 Sep 2023 18:10:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694103044; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EecJxGRJUdYPSN85L2Z1x9DAs7W/pY/3Kwur5dofxCg=; b=ZjxnsS0w4a1tCm7M1+vgkNfcAFMhScIVanZDoIDc8ZGnMD2GThUwhzzMRm4df4LsLjhWCz GsUdHlXz2+DxY/EUP8ItqCwoNbcd7j79nMJYzz2iWKJ04muYe5PcppLsb0Qs3Yd1hfXNU2 +2o3Pjn7dMiYiRHNBYdYaCMXZt8Zmhc= Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-571-_VJ689TGO52lZrVFA3DR9Q-1; Thu, 07 Sep 2023 12:10:43 -0400 X-MC-Unique: _VJ689TGO52lZrVFA3DR9Q-1 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-5007ac1c653so1221071e87.2 for ; Thu, 07 Sep 2023 09:10:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1694103042; x=1694707842; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EecJxGRJUdYPSN85L2Z1x9DAs7W/pY/3Kwur5dofxCg=; b=hRICwvh0viWbJcpTV2+XXLQdzHFp0XINbKKW3eJoxY46VdG8r2zfMs1WwJAE1J28fd nOaWUfoUxTcGYuVOsYx0wnUA+e68Osdl6+hcgsQuqBFvGHnVW/9sCyt9k0jGifueHGet seyyFlWoNLse3BS/tumHhDEHzxyqT1mlmI8e0B4zVWnxSqPOTuPnkCQiXH2AfPo4hJOj huV2xW/8kX7ZRZgGH9zMN1rTd2l8FdviW7sK4AVj8g0tvows+Y+OYwADSQ7+XT/BSw2v WWU/Hu7kPEE0FbHos6IwFN0O0X6kOvVAV7J+/orLrxdkPZuv/Frvw38J89WMLwrN9Fdh Xv2A== X-Gm-Message-State: AOJu0Ywwncj6i7hk/ox4BBI5H1RXr8M8zjqF52NKeL6qaC7qmb+Kuv5c mvYEpOvuKFHgl+TKcm5nBTQkbAyYdpJ/xMmaqVTaqxUd9I7V+kC0ntJCxvCeodA8pzzq0pHrcnn bGojI7Cy/lSKGZwhtXJI= X-Received: by 2002:a05:6512:750:b0:4fe:193c:de74 with SMTP id c16-20020a056512075000b004fe193cde74mr4732738lfs.18.1694103041819; Thu, 07 Sep 2023 09:10:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7vtGCBUUeFr2YmMV+OITab/AjbuNDUHSzPpraxbcvOdUtRjB/MIaY61A7BDhuQ3tLxktNAM/JyrFDt1Vbz8I= X-Received: by 2002:a05:6512:750:b0:4fe:193c:de74 with SMTP id c16-20020a056512075000b004fe193cde74mr4732718lfs.18.1694103041466; Thu, 07 Sep 2023 09:10:41 -0700 (PDT) MIME-Version: 1.0 References: <20230906155302.82749-1-stephen@networkplumber.org> <20230907152456.20570-1-stephen@networkplumber.org> <20230907152456.20570-3-stephen@networkplumber.org> <20230907084743.6e43e885@hermes.local> In-Reply-To: <20230907084743.6e43e885@hermes.local> From: David Marchand Date: Thu, 7 Sep 2023 18:10:30 +0200 Message-ID: Subject: Re: [PATCH v2 2/2] random: make rte_rand() thread safe for non-EAL threads To: Stephen Hemminger Cc: dev@dpdk.org, =?UTF-8?Q?Mattias_R=C3=B6nnblom?= , Bruce Richardson X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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 Thu, Sep 7, 2023 at 5:48=E2=80=AFPM Stephen Hemminger wrote: > > On Thu, 7 Sep 2023 08:24:56 -0700 > Stephen Hemminger wrote: > > > > > +static __rte_always_inline > > +void __rte_rand_put_state(struct rte_rand_state *state) > > +{ > > + if (state =3D=3D &rand_states[RTE_MAX_LCORE]) > > + rte_spinlock_unlock(&rte_rand_lock); > > +} > > Conditional locking like this make clang lock analyzer unhappy though. Ugly, but some macro can do the job... diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c index 02b6b6b97b..3f2a4830fd 100644 --- a/lib/eal/common/rte_random.c +++ b/lib/eal/common/rte_random.c @@ -128,20 +128,22 @@ struct rte_rand_state *__rte_rand_get_state(void) idx =3D rte_lcore_id(); /* last instance reserved for unregistered non-EAL threads */ - if (unlikely(idx =3D=3D LCORE_ID_ANY)) { + if (unlikely(idx =3D=3D LCORE_ID_ANY)) idx =3D RTE_MAX_LCORE; - rte_spinlock_lock(&rte_rand_lock); - } return &rand_states[idx]; } -static __rte_always_inline -void __rte_rand_put_state(struct rte_rand_state *state) -{ - if (state =3D=3D &rand_states[RTE_MAX_LCORE]) - rte_spinlock_unlock(&rte_rand_lock); -} +#define PROTECT_NON_EAL_THREADS(...) do { \ + unsigned int idx =3D rte_lcore_id(); \ + if (idx =3D=3D LCORE_ID_ANY) { \ + rte_spinlock_lock(&rte_rand_lock); \ + __VA_ARGS__ \ + rte_spinlock_unlock(&rte_rand_lock); \ + } else { \ + __VA_ARGS__ \ + } \ +} while (0) uint64_t rte_rand(void) @@ -149,9 +151,10 @@ rte_rand(void) struct rte_rand_state *state; uint64_t res; + PROTECT_NON_EAL_THREADS( state =3D __rte_rand_get_state(); res =3D __rte_rand_lfsr258(state); - __rte_rand_put_state(state); + ); return res; } @@ -168,6 +171,7 @@ rte_rand_max(uint64_t upper_bound) if (unlikely(upper_bound < 2)) return 0; + PROTECT_NON_EAL_THREADS( state =3D __rte_rand_get_state(); ones =3D rte_popcount64(upper_bound); @@ -192,7 +196,7 @@ rte_rand_max(uint64_t upper_bound) res =3D __rte_rand_lfsr258(state) & mask; } while (unlikely(res >=3D upper_bound)); } - __rte_rand_put_state(state); + ); return res; } --=20 David Marchand