Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp278152lqo; Tue, 7 May 2024 22:06:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUQ1YpLZ1fBfaGsG+Bn8f9tDVfQc8mfchWO1w8425k5YZTUy8EMA4YUXTZin6jqCnDHd+V3YTFDOtUADqBNd82tvjjWCT/KaZj8pC29hQ== X-Google-Smtp-Source: AGHT+IHW75gH1lz4InabiE24d4JRA2AZHcSGmJSIvQzdbmsgoMJ8Ez+66jSkqFD6Jp6Bhnbs/+P1 X-Received: by 2002:a05:6a21:4982:b0:1af:af86:ce44 with SMTP id adf61e73a8af0-1afc8db5bdfmr1670487637.40.1715144789610; Tue, 07 May 2024 22:06:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715144789; cv=pass; d=google.com; s=arc-20160816; b=grZjl0j6EpT4qEG9JBuCcSeIgTrqT9fr4PCtdLMEMVKdMVWHXc5yNuMBgMDYylnUtq NN6UT8MiO2+Cgg4GbhAWeqj+c5IV6dgwj0N61nbdYKaKZpUQ7HwEqdcQL+qZjqfomPPk RO2ahT/0bHNUH3due1qQ9BuNJNJPQLbPt7CQvplCdmW3LTY82snJ0sWfnvwD6YOb2RJk UiNU+rl7Y2mSNsF7noJliXXO45R5nKoSFL+TK0EiOjRI1nrAVdKdR/0xREhxEvQk92js JSw41E+wd8X/NuOhqvryL79I8CP3n/Sb6v/XjSsIgZ2Gagpz0j6rTlxMzaQ+S9UDaXAL P7jg== 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=qDxi+XXbzr8mIuwlB1aliy4UlEscitEJu0tfYntXSqk=; fh=x6D+icnxtEFZm9BTI/2Jxd8tgPVN4sqSuNXU4WnBB1M=; b=MptlLScVuFMyBtPIJTVATDp/2wl81HKsq0QOwmeoYzcpcaQCrstbStqOFX4LmuSRhK Az9sH+vX3NoPQZ2J+xISyV6C7S9r/zVdDmmrnDQPS9KUHi3aturTC9YmRsA6LTpM4qCB +asUMR+P15laUaoYSJ7m7UGDjgcz0M324C3s9YazcZqNOSANrOkQTt+2O2o0SXHGvyor i385sKw9shXp+IQRmGAzjHbscalqY4LQlQ6YDvM3AL6Nn86QO5FmSMmmow/1JH6HRHFS 9hr2T4y+Bz5ID9jUUZOyfRa4eBFM0UHPozprVHSsTbj4bvlXCKqzx1LfIybycr+XBufo l19Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ellerman.id.au header.s=201909 header.b=afORrE3T; 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-172690-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172690-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id j16-20020a056a00131000b006eab1d8a23asi11023401pfu.172.2024.05.07.22.06.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 22:06:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-172690-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@ellerman.id.au header.s=201909 header.b=afORrE3T; 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-172690-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172690-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id D4EC6B20F6F for ; Wed, 8 May 2024 05:06:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2C68318028; Wed, 8 May 2024 05:05:47 +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="afORrE3T" 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 CB8CE17559; Wed, 8 May 2024 05:05:43 +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=1715144746; cv=none; b=FvDztn8FQC1qHpkMobaKGFkgNHzpaSn2B4lpQDg2iTfmsKh+e56jQsAq4wzXibjs+W9cPglf/Jvmhi2eAVmdxIXjU+7yONMkslGcL11iLVktOM6vXdtq/hDNrlaNqO13q5fpFkShYpuQruq+cvm+dq3Eoo2qLdMpYAb4wnHdI34= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715144746; c=relaxed/simple; bh=8tlkHKRM2YMJamihFiXet8yEil9oqOy2oa3BI13SVkk=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=bpqkM9OlvE6mxn0yVsvTTDmy0lMzYgny10oThQF/swO6FwkmEoBeW5MCgUxUJMY1Fn27yL+dNUA94TF2NmpVo78TrPqvuIkFgOXhabl5fRPJNDLGVaAxcJGa65Zl+toLvhRPv9gVORqPPjwygblhoSsDCtaDeHk/iRnD0YIIb2k= 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=afORrE3T; 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=1715144741; bh=qDxi+XXbzr8mIuwlB1aliy4UlEscitEJu0tfYntXSqk=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=afORrE3T6fm9owzOay6Q4EjIov5tp69f4yV6zWLQdPsWDile3ZCCvtHhzO2PqLNhY m+q7v/FoA8SLQDfn596Zo1eZM+hR6/YdWkcLKevLwwEApws3ePuCppayzHHPscXyJF TvBQUdEABHJNrqArtC95+goi1c4tXk3VHRtSchfW2Fvwvkx7nbQaM69bjdX40GStHi sswR2je3OJ6aclZFJUTBI6AB57hHjLBZkIb72MLcLpFHfZFymMKkfdhI1OB7cMBOgs X2mfW+XeE/gaofip2NTMzaeOv5sbyT2gLUkqkWyl5VgYm4tFgR3AvFayfeeXD22ujL DGyduQoKPLUuQ== 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 4VZ34h4KM9z4wc1; Wed, 8 May 2024 15:05:40 +1000 (AEST) From: Michael Ellerman To: Puranjay Mohan , 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 , "Naveen N. Rao" , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , Hari Bathini , bpf@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Cc: puranjay12@gmail.com Subject: Re: [PATCH bpf] powerpc/bpf: enforce full ordering for ATOMIC operations with BPF_FETCH In-Reply-To: <20240507175439.119467-1-puranjay@kernel.org> References: <20240507175439.119467-1-puranjay@kernel.org> Date: Wed, 08 May 2024 15:05:40 +1000 Message-ID: <87o79gopuj.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: > 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. Thanks for catching this. > diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c > index 2f39c50ca729..b635e5344e8a 100644 > --- a/arch/powerpc/net/bpf_jit_comp32.c > +++ b/arch/powerpc/net/bpf_jit_comp32.c > @@ -853,6 +853,15 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code > /* Get offset into TMP_REG */ > EMIT(PPC_RAW_LI(tmp_reg, off)); > tmp_idx = ctx->idx * 4; > + /* > + * Enforce full ordering for operations with BPF_FETCH by emitting a 'sync' > + * before and after the operation. > + * > + * This is a requirement in the Linux Kernel Memory Model. > + * See __cmpxchg_u64() in asm/cmpxchg.h as an example. > + */ > + if (imm & BPF_FETCH) > + EMIT(PPC_RAW_SYNC()); > /* load value from memory into r0 */ > EMIT(PPC_RAW_LWARX(_R0, tmp_reg, dst_reg, 0)); > > @@ -905,6 +914,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code > > /* For the BPF_FETCH variant, get old data into src_reg */ > if (imm & BPF_FETCH) { > + /* Emit 'sync' to enforce full ordering */ > + EMIT(PPC_RAW_SYNC()); > EMIT(PPC_RAW_MR(ret_reg, ax_reg)); > if (!fp->aux->verifier_zext) > EMIT(PPC_RAW_LI(ret_reg - 1, 0)); /* higher 32-bit */ On 32-bit there are non-SMP systems where those syncs will probably be expensive. I think just adding an IS_ENABLED(CONFIG_SMP) around the syncs is probably sufficient. Christophe? cheers