Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp2393130lqo; Mon, 13 May 2024 18:09:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUn+LvbGDQaIZvaFzk4VhLE8MX0+PTExzJpSfzFGeRegv9kg1yMn2B0Os99yqRL3CLnXTC/G7vSTx0TSTI9dAD1usTbI/NH7GOgHl5A6g== X-Google-Smtp-Source: AGHT+IEstObzHKlzU6AFQKhzkAiqWVMR8KTW56jvEKuiof+PPNP29rjEz4Y2WUFex+vVPo1zWI/l X-Received: by 2002:a05:622a:1653:b0:43e:f5b:7a78 with SMTP id d75a77b69052e-43e0f5b7e44mr96714441cf.43.1715648965900; Mon, 13 May 2024 18:09:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715648965; cv=pass; d=google.com; s=arc-20160816; b=TEd/uRBLNl0/nLIgVr2vTyeJVFQ6rOnY8Ewfqx00o573AhusvpvjZCiODgWCTpE7Vk 2Itxwsz1F0sBx4lZOzNu/IOnAdzWcAp5D41KECpyr5CLfdFzeUSv0YvD3LN7Js/sn1Tt rTbNeNfV4SuGF/VlQgXy8D9AlR1Qr5Hx44boWYiLC+LieUG+h6pZI9FMJoiMj0ozpv2X nvF1qvG/s0TsNYxbA+4cp/wLqkZgvHEzeF/I/8kFIV2S1jJ6qr2ru/J3NO4/z1oXCbeV Fq66IvsFOfL4ykEo+SCrrfyHDHTGC2tKx0zLwgVBUM4pwSso0cvBayYTUp2aB9kZ+5vo YwUQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :message-id:date:references:in-reply-to:subject:cc:to:from :dkim-signature; bh=cJZZq6BaqUzfoKYWBZ/95aiewNAVNaflLJy9ncQn21Q=; fh=LVIHv+mOP/NO3io6SWGQD+eTo2mu+LRDYjphOr7DCLY=; b=rnL2X6CsJsPdpBG+CU1a2aKWEq+cvt4mf/p08M6UKpbyzNjHrIrKDvUz45q2/M33lP v+nA2NJ7nWnby39ae+ccG2WjqY72/6UgW8XyvYMM7h5vN/UaFPupukMa2R/sYtfE/Zg+ fGdhKKMapzIQD+IPDdgId5SOhUzJhPyB0kSJcXTDaiFNZtd3KpYDLJRYqsaiwtfHLRuQ 3AZDpJd3Af0kvDbhNNyUEKx5E1arkG3pXSVa3YIWBeV8Td8znBPfKoTBr2zowEK4kHFD JGTmZGeRNt1rP6WixpcjzrfmTM3TbqYBM3cURSM9psQsSOg4DFMu+NxnhahdWv5jmCGy vOMg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ellerman.id.au header.s=201909 header.b=nhWdX3An; arc=pass (i=1 spf=pass spfdomain=ellerman.id.au dkim=pass dkdomain=ellerman.id.au); spf=pass (google.com: domain of linux-kernel+bounces-178231-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178231-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-43df56d7157si109803191cf.709.2024.05.13.18.09.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 18:09:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-178231-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ellerman.id.au header.s=201909 header.b=nhWdX3An; arc=pass (i=1 spf=pass spfdomain=ellerman.id.au dkim=pass dkdomain=ellerman.id.au); spf=pass (google.com: domain of linux-kernel+bounces-178231-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178231-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 8FB401C22C5A for ; Tue, 14 May 2024 01:09:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3809B1878; Tue, 14 May 2024 01:09:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ellerman.id.au header.i=@ellerman.id.au header.b="nhWdX3An" Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B69B5EC5; Tue, 14 May 2024 01:09:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=150.107.74.76 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715648956; cv=none; b=gioD83WxgL8NhBuSjnYvrIIRD/Z5XvKVldZA51hOWdWgk3zJ+W7QeyVtrgMdIB5CRTvFcMNGWO3XwAvhySu30eVCv+OVBQKXHaun6ajDY8NUWx04BjMVVoLQrtvv2MQko4rY7Ed8BYBzbnKRuk1TEJy6nLhPvcmzYf9Vm1VSiNQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715648956; c=relaxed/simple; bh=Z0a9zo/oD7Fzoeh2HMvsQbMyP/a4DTsMNsBtVleFWZA=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=VF6viBxU4cViOEtn5FxgnGBMmU11NcaBkS5YrfUlv3Plm0GNPNSKISCbPaGTI4olYSY0W2gMOxjoQoQgguxbikKO7TnaltQMzzRFbn/uDt4tQkX9G+xdya/doJoneM+kJAVX7AS4IWlLLH1MtJIuPKdDGdrxpGa1VYSFmSEkUXQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ellerman.id.au; spf=pass smtp.mailfrom=ellerman.id.au; dkim=pass (2048-bit key) header.d=ellerman.id.au header.i=@ellerman.id.au header.b=nhWdX3An; arc=none smtp.client-ip=150.107.74.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ellerman.id.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ellerman.id.au DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ellerman.id.au; s=201909; t=1715648950; bh=cJZZq6BaqUzfoKYWBZ/95aiewNAVNaflLJy9ncQn21Q=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=nhWdX3AnX/lfpTM/3msxKX1vr3ahC0e0aj5/uYWopsMwiOJhDV5Nj/9I89GXiJ3Q/ 6aWx/cOv3OZ5Xo/4WZgD4+v03yGXP9Y07XFHKiUgCf6W10BmHEh3ar0Z6HIhINcKMB Nvk9huz4bBVvkHluHiCMpZ+lJHDL8VkSbohw0wrxlC3wLUTp0h+L99ji3VJ1t4ZXCl Irb8bpFJpVczlOs7DFojFAjJN7zOYxbbCkp2/BPs86cHpna5tdzFe7teI5Zjk/pgWb lkRs4aODolq4b60AuGvlOMP8nfOQuE2sioi7ZEaPvpXugmAHNmKLhYBf4LxJH1xpS8 esjU/uxMK+iMQ== Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.ozlabs.org (Postfix) with ESMTPSA id 4VddY15HGZz4wc3; Tue, 14 May 2024 11:09:09 +1000 (AEST) From: Michael Ellerman To: Puranjay Mohan , Naveen N Rao Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , Hari Bathini , bpf@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, paulmck@kernel.org Subject: Re: [PATCH bpf v3] powerpc/bpf: enforce full ordering for ATOMIC operations with BPF_FETCH In-Reply-To: References: <20240513100248.110535-1-puranjay@kernel.org> Date: Tue, 14 May 2024 11:09:07 +1000 Message-ID: <87h6f1w66k.fsf@mail.lhotse> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Puranjay Mohan writes: > Naveen N Rao writes: >> On Mon, May 13, 2024 at 10:02:48AM GMT, Puranjay Mohan wrote: >>> The Linux Kernel Memory Model [1][2] requires RMW operations that have a >>> return value to be fully ordered. >>> >>> BPF atomic operations with BPF_FETCH (including BPF_XCHG and >>> BPF_CMPXCHG) return a value back so they need to be JITed to fully >>> ordered operations. POWERPC currently emits relaxed operations for >>> these. >>> >>> We can show this by running the following litmus-test: >>> >>> PPC SB+atomic_add+fetch >>> >>> { >>> 0:r0=x; (* dst reg assuming offset is 0 *) >>> 0:r1=2; (* src reg *) >>> 0:r2=1; >>> 0:r4=y; (* P0 writes to this, P1 reads this *) >>> 0:r5=z; (* P1 writes to this, P0 reads this *) >>> 0:r6=0; >>> >>> 1:r2=1; >>> 1:r4=y; >>> 1:r5=z; >>> } >>> >>> P0 | P1 ; >>> stw r2, 0(r4) | stw r2,0(r5) ; >>> | ; >>> loop:lwarx r3, r6, r0 | ; >>> mr r8, r3 | ; >>> add r3, r3, r1 | sync ; >>> stwcx. r3, r6, r0 | ; >>> bne loop | ; >>> mr r1, r8 | ; >>> | ; >>> lwa r7, 0(r5) | lwa r7,0(r4) ; >>> >>> ~exists(0:r7=0 /\ 1:r7=0) >>> >>> Witnesses >>> Positive: 9 Negative: 3 >>> Condition ~exists (0:r7=0 /\ 1:r7=0) >>> Observation SB+atomic_add+fetch Sometimes 3 9 >>> >>> This test shows that the older store in P0 is reordered with a newer >>> load to a different address. Although there is a RMW operation with >>> fetch between them. Adding a sync before and after RMW fixes the issue: >>> >>> Witnesses >>> Positive: 9 Negative: 0 >>> Condition ~exists (0:r7=0 /\ 1:r7=0) >>> Observation SB+atomic_add+fetch Never 0 9 >>> >>> [1] https://www.kernel.org/doc/Documentation/memory-barriers.txt >>> [2] https://www.kernel.org/doc/Documentation/atomic_t.txt >>> >>> Fixes: 65112709115f ("powerpc/bpf/64: add support for BPF_ATOMIC bitwise operations") >> >> As I noted in v2, I think that is the wrong commit. This fixes the below > > Sorry for missing this. Would this need another version or your message > below will make it work with the stable process? No need for another version. b4 should pick up those tags, or if not I'll add them by hand. cheers