Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp411151pxu; Tue, 1 Dec 2020 14:36:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJwxLurpwrwWgEtuv/3twJ3rvaHawvdNBw91//uDy1DTZolzKFlwYuZYcza6+368hKiAwPjg X-Received: by 2002:a50:b243:: with SMTP id o61mr5440860edd.57.1606862198440; Tue, 01 Dec 2020 14:36:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606862198; cv=none; d=google.com; s=arc-20160816; b=Mw/UVsDOChreFHJa5gZXCB3Q2qrGmL1woF5vqvS5uMS3GhlWnr/bRqhJoQU8gGrvYL TAsxgJm6/fsCDr04RrlkyCpJTiNEdc1RpMKlFXDcJCqVktnKSc/wZLY3HRXZdFcO3NXm FflbgvQxNf8Fwt9VQrfvGagQTf4+zhFd9iJkMwjSCwXeylmo4WhDzjvLm3MimdWcm7rr jF3Eb6aXd77KPkd+dm5Smb6mWWw9oPECwjqQMXRa6fBc4b/lWsMpTx4FhHU3Exw1TPtS Tz/Ho1HgUo1Q+ok/kaw3RqYZ9tX1czQ42+7bQDc54Kk+ZRDg6dpor1t3Yky4hX7zXF7Z RKdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=mTIT9saW4JZE48elAbVCzCLGem+oE0gSHJTG1Irc+L8=; b=J8VBdO1H3axtmEb6NnM4swJ6wfAhnFGFs4dI9h6NdTK0Pa343pHqLgizGyg/y/v+mq mnPWZLjBBxVdEY9wWypTN7jpDpco8Z6jCJt40/2U0xzVXLF7gScGZrcjzWrLvhMDcpP9 sTdKkgIk20vvwWjYpzhMnqmZwK1518F0UEc//5jFT0iH3JzCmjnt3fM42n7Nu2Mm8WAg n1oPJ292DhTKJ+0AjCtA7ysbtNS7kPFB7DXtLHO1uUNpG5qGockKFTt5oAWrf0Kndhfj W/X2ExYF2Cl99ktIoRtFhXuvGG4SGaBIIpEcKzKNLIxHC+D1oqZAn2LFTBRDJbV0D9Lh AdqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=d6ntMLNL; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p19si781534edu.169.2020.12.01.14.36.15; Tue, 01 Dec 2020 14:36:38 -0800 (PST) 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; dkim=pass header.i=@google.com header.s=20161025 header.b=d6ntMLNL; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728297AbgLAM2S (ORCPT + 99 others); Tue, 1 Dec 2020 07:28:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726304AbgLAM2R (ORCPT ); Tue, 1 Dec 2020 07:28:17 -0500 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30364C0613D4 for ; Tue, 1 Dec 2020 04:27:37 -0800 (PST) Received: by mail-wr1-x442.google.com with SMTP id 64so2330593wra.11 for ; Tue, 01 Dec 2020 04:27:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=mTIT9saW4JZE48elAbVCzCLGem+oE0gSHJTG1Irc+L8=; b=d6ntMLNLl1d9+VneIchH4PzLGhiXxI2qo0NKQWfgCD6PsNmCSq3GMuFVPqxCHsA8wS efiON3u3aoR+FWg1GjPGYT4hKV3wXygKXORxEMN4BWVD06yUzR8JMXGfi/9Zxa9334Oc EQEoN9dww0K8+LHSvSIew3YqbOaINFNG4FT6p5UCsrrIw9LnKOXUp+OEmElM3LYfX90D 8BjCK5dHpG3ROdJw0VaaUWT9mRi1KgyAIf0MVXoyQVY3c2Q8oZBrj5xd5y/TZE13oQNz KzLkJNKxws4V1BvlCZp7gayrcm0oKBnFCRjngZ37lP9LUgS7WWDCzn/xz2eJYWhAtkh0 w9Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=mTIT9saW4JZE48elAbVCzCLGem+oE0gSHJTG1Irc+L8=; b=gBwCko/a5SH8hSgWdvZGETnGaKGxh1Oqx49UpS7q2Iit3F2Qd2jzjh1KULYLCa6NP2 WjZm9y4Ck89LG5WWll4a7Nx4mY6w467H9tw8bi7jh4FW/KxiMmdOpNe/7vCh05KhhCXo 7cCmHlUbnlcGLZPYsp9jLz5aOxDdyuG2HoNJ+1kXBlw7nj0Z1eBP2Bc1zUY8ckqhA3am x/YY4zFB2df8g9yq5iJl5IMoEU0kZoGt7CeFau4XiyGTBLjVZ0V+xitcTOWPFAMdtxmD odjjHBckGxhAYlJ8q7yPZJR0Kt6fv2RVuuaAqf3+Ap1AwvUPk4J4YKgXcGCyS1F6xZpz HMGA== X-Gm-Message-State: AOAM531DxPbLXmKiNnSEgLdB+q2BktCPAxvPKS6MCAhzU3zEXI1zdccQ 4BrHyrQZo8kulGycoZI9ujygkw== X-Received: by 2002:adf:e44d:: with SMTP id t13mr3602310wrm.144.1606825655734; Tue, 01 Dec 2020 04:27:35 -0800 (PST) Received: from google.com (203.75.199.104.bc.googleusercontent.com. [104.199.75.203]) by smtp.gmail.com with ESMTPSA id 2sm3712858wrq.87.2020.12.01.04.27.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2020 04:27:34 -0800 (PST) Date: Tue, 1 Dec 2020 12:27:31 +0000 From: Brendan Jackman To: Yonghong Song Cc: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , KP Singh , Florent Revest , linux-kernel@vger.kernel.org, Jann Horn Subject: Re: [PATCH v2 bpf-next 08/13] bpf: Add instructions for atomic_[cmp]xchg Message-ID: <20201201122731.GE2114905@google.com> References: <20201127175738.1085417-1-jackmanb@google.com> <20201127175738.1085417-9-jackmanb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Nov 27, 2020 at 09:25:53PM -0800, Yonghong Song wrote: > > > On 11/27/20 9:57 AM, Brendan Jackman wrote: > > This adds two atomic opcodes, both of which include the BPF_FETCH > > flag. XCHG without the BPF_FETCh flag would naturally encode > > BPF_FETCH Ack, thanks > > atomic_set. This is not supported because it would be of limited > > value to userspace (it doesn't imply any barriers). CMPXCHG without > > BPF_FETCH woulud be an atomic compare-and-write. We don't have such > > an operation in the kernel so it isn't provided to BPF either. > > > > There are two significant design decisions made for the CMPXCHG > > instruction: > > > > - To solve the issue that this operation fundamentally has 3 > > operands, but we only have two register fields. Therefore the > > operand we compare against (the kernel's API calls it 'old') is > > hard-coded to be R0. x86 has similar design (and A64 doesn't > > have this problem). > > > > A potential alternative might be to encode the other operand's > > register number in the immediate field. > > > > - The kernel's atomic_cmpxchg returns the old value, while the C11 > > userspace APIs return a boolean indicating the comparison > > result. Which should BPF do? A64 returns the old value. x86 returns > > the old value in the hard-coded register (and also sets a > > flag). That means return-old-value is easier to JIT. > > > > Signed-off-by: Brendan Jackman > > --- > > arch/x86/net/bpf_jit_comp.c | 8 ++++++++ > > include/linux/filter.h | 20 ++++++++++++++++++++ > > include/uapi/linux/bpf.h | 4 +++- > > kernel/bpf/core.c | 20 ++++++++++++++++++++ > > kernel/bpf/disasm.c | 15 +++++++++++++++ > > kernel/bpf/verifier.c | 19 +++++++++++++++++-- > > tools/include/linux/filter.h | 20 ++++++++++++++++++++ > > tools/include/uapi/linux/bpf.h | 4 +++- > > 8 files changed, 106 insertions(+), 4 deletions(-) > > > [...] > > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > > index cd4c03b25573..c8311cc114ec 100644 > > --- a/kernel/bpf/verifier.c > > +++ b/kernel/bpf/verifier.c > > @@ -3601,10 +3601,13 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn > > static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) > > { > > int err; > > + int load_reg; > > switch (insn->imm) { > > case BPF_ADD: > > case BPF_ADD | BPF_FETCH: > > + case BPF_XCHG: > > + case BPF_CMPXCHG: > > break; > > default: > > verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); > > @@ -3626,6 +3629,13 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i > > if (err) > > return err; > > + if (insn->imm == BPF_CMPXCHG) { > > + /* check src3 operand */ > > better comment about what src3 means here? Ack, adding "Check comparison of R0 with memory location"