Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3743700pxu; Mon, 30 Nov 2020 09:22:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJyx9sM3zleCLCcs8gExVhiHZLYInzgfH99qFVPjPJ9g4h2B7XvgsE8WSHcjIsZY5/Uptn/v X-Received: by 2002:a19:ca1c:: with SMTP id a28mr9531934lfg.61.1606756971733; Mon, 30 Nov 2020 09:22:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1606756971; cv=pass; d=google.com; s=arc-20160816; b=d/CDUSONHH3VCX7/oEiLPsO9C6oYyQGDwjASyPGVHlp2yt5dXZD9KQUsiJbPigO6i9 VNHt/Uq5NWrxj4/qBIKOMzGiiYOW7Y2o5TQijH9iD1Y98APmp0acC1slInKV1z05fode MsB56qn5gp7x4ao74NamqodCKr1AKsT6+1dtueRez9GOvKkhlF5XFsN1nUBBvD2w97z+ HGa0tkZ/fkZdCmeuILA8y78YzTaByGhw3Y4QTaySR3LQ9YFvrEQ4GpEl2RJ2aiHe7BJm ZA12+m7OHA0l+P32muWqUDe4GImxkhUaZ8c2wk1CMcSFfAWCrm1u6KsTRq6u6f2kmGsH b5aQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-language:in-reply-to:user-agent:date:message-id:from :references:cc:to:subject:dkim-signature:dkim-signature; bh=3iYmPYBHlImkJTSyBBsABRdDjv1fbL5wtifAQFxx8CI=; b=Jha6116yun3xsJVVEwbDuZF/nRG8RPYqQ+tuJPBnDDkIgXtt2FUkYaQ/E+4Ff+mIQ4 cJDZhYFHop+gUhEjkiXzccL6TSzNMsnLN72FQlcjTPNsCQQH9bPRXulR01vs2uBW72bI gGq/IdUWKtSI/psVffAObYC9pnEmD7++Eqcr2kbn0RQRoxjO0eOcle1xO6hSZ76hwVzr wl6EODr5sb7snUuxFVDCqqlCFwiZU7Z0JgID4ePHkjzNt39xdqYIC9N/F0S6UCSmGl6y Ug2gjyaT7vVdlyqEI6MQlis0LnDV5AKZCUirRGzXThf6N7pmvtIb5tXE6bhyd37SIVue JJ9g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=CLAXo3aB; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector2-fb-onmicrosoft-com header.b=Q8JRmX4Q; arc=pass (i=1 spf=pass spfdomain=fb.com dkim=pass dkdomain=fb.com dmarc=pass fromdomain=fb.com); 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=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r2si11685641edx.114.2020.11.30.09.22.28; Mon, 30 Nov 2020 09:22:51 -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=@fb.com header.s=facebook header.b=CLAXo3aB; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector2-fb-onmicrosoft-com header.b=Q8JRmX4Q; arc=pass (i=1 spf=pass spfdomain=fb.com dkim=pass dkdomain=fb.com dmarc=pass fromdomain=fb.com); 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=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387434AbgK3RTN (ORCPT + 99 others); Mon, 30 Nov 2020 12:19:13 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:41648 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2387420AbgK3RTM (ORCPT ); Mon, 30 Nov 2020 12:19:12 -0500 Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.16.0.42/8.16.0.42) with SMTP id 0AUHD9ud012484; Mon, 30 Nov 2020 09:18:14 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=subject : to : cc : references : from : message-id : date : in-reply-to : content-type : content-transfer-encoding : mime-version; s=facebook; bh=3iYmPYBHlImkJTSyBBsABRdDjv1fbL5wtifAQFxx8CI=; b=CLAXo3aBUgKCR1+u7SiX3IZP+vULqM7TcpNcz52eXv2IjYYqzJEFp7CDwmRYO3jxPfU/ BHMOTVd5x6nRuFRjzgd6gew7VMWndnm4vXZIv/0yRhZdKklA+N8nLopWmcoZvK1BG+Ia CdJhPgUOlhIH8JZr5Jg5wJEXKRcs3H46XeY= Received: from maileast.thefacebook.com ([163.114.130.16]) by m0001303.ppops.net with ESMTP id 354g9ucjpe-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Mon, 30 Nov 2020 09:18:14 -0800 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (100.104.31.183) by o365-in.thefacebook.com (100.104.35.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Mon, 30 Nov 2020 09:18:12 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bKFatuWJaoOmurIt1SGUlLhG+rGtsGmtPuOKcWyz11tJtKu2MVk/L8+eMl9FAWJdmbfiwqunIO8UhnoZ3kvxenMktBmT4JVMbvZVZorAz88fInhyuR725JTediTXqI62LovlIfpfHmK2YlYGanq8SUNyLnNa+KAgXwJcxWjeBJGV8oph5/DPe0llOT7c8EtLjra5J+h84mAu9GSRHdMbapW/mITKydmYx76SqeaiGUfP36LKnCChM8J/oK8xWnL2EflFnAVvdgnW4INN/kJ+vGoKaU5NuyefKKEotIRjz/9O5PKt1rbNJ23hA9vQmH7NQCHH4L2b0Q4pzUbp26wSGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3iYmPYBHlImkJTSyBBsABRdDjv1fbL5wtifAQFxx8CI=; b=GH5quthtAHlId2FKfSHWGskyFzqTlOta0zfS2pZmw2eBLWCuRrg3DW1sgWvvNfYQJvmbtrlrPXPRgGKGtPvSPOZDI48P+zfOdJ4ewcQ5VBq5ll/pLRcAKQOtkFHWuIR9Xrb9/mrCJlytmZ+vDgMJwaywmpjyJ1R1I1KUcKdr5U7k5TcAI6dqFIQcqenxHlkBsbGpmCfroHtzzcm61PefCkFnbZAa4zzwuNW4szfhHkslEVWl7Np0vOnwKUz5E9iwIXMgmcvoyVT8jnCrjUa0XZFm0f5+J6OEXTkTxkiE9LmreDkuS0TPT8lJnIFSNxlHWrCMisFcOvsVTHo+gfJBKQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass header.d=fb.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com; s=selector2-fb-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3iYmPYBHlImkJTSyBBsABRdDjv1fbL5wtifAQFxx8CI=; b=Q8JRmX4QekCQAqi5imdubyGnlXTd0PPdKpA3fO5h/ZUWjh65iy33/RN/vur3D9X+aSnTweVtvWuH0IJYaqvtslNFB7q83/gnW1tPymJieK5M66o8zQlkjgYWW8lcXbMyFt40ukMAk3biKwXb2+hzyZbLBHRzZuibudOWrMpMbnI= Received: from BYAPR15MB4088.namprd15.prod.outlook.com (2603:10b6:a02:c3::18) by BYAPR15MB2565.namprd15.prod.outlook.com (2603:10b6:a03:14f::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3611.24; Mon, 30 Nov 2020 17:18:12 +0000 Received: from BYAPR15MB4088.namprd15.prod.outlook.com ([fe80::9ae:1628:daf9:4b03]) by BYAPR15MB4088.namprd15.prod.outlook.com ([fe80::9ae:1628:daf9:4b03%7]) with mapi id 15.20.3611.025; Mon, 30 Nov 2020 17:18:12 +0000 Subject: Re: [PATCH v2 bpf-next 10/13] bpf: Add instructions for atomic[64]_[fetch_]sub To: Alexei Starovoitov CC: Brendan Jackman , , Alexei Starovoitov , Daniel Borkmann , KP Singh , Florent Revest , , Jann Horn References: <20201127175738.1085417-1-jackmanb@google.com> <20201127175738.1085417-11-jackmanb@google.com> <0fd52966-24b2-c50c-4f23-93428d8993c4@fb.com> <20201129013420.yi7ehnseawm5hsb7@ast-mbp> From: Yonghong Song Message-ID: <1dfd2e5e-f8d2-eac2-d6b2-7428ceb00c36@fb.com> Date: Mon, 30 Nov 2020 09:18:09 -0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 In-Reply-To: <20201129013420.yi7ehnseawm5hsb7@ast-mbp> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [2620:10d:c090:400::5:5f96] X-ClientProxiedBy: MWHPR18CA0042.namprd18.prod.outlook.com (2603:10b6:320:31::28) To BYAPR15MB4088.namprd15.prod.outlook.com (2603:10b6:a02:c3::18) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [IPv6:2620:10d:c085:21cf::124e] (2620:10d:c090:400::5:5f96) by MWHPR18CA0042.namprd18.prod.outlook.com (2603:10b6:320:31::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3611.20 via Frontend Transport; Mon, 30 Nov 2020 17:18:11 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1673778f-cee3-4557-f8b7-08d89553ea4e X-MS-TrafficTypeDiagnostic: BYAPR15MB2565: X-Microsoft-Antispam-PRVS: X-FB-Source: Internal X-MS-Oob-TLC-OOBClassifiers: OLM:3968; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fhWWCnRZh2Y2NFRUoY6wHv1Z5D2Sdpzq6phOog9ABzefLTdL2Aq8sK0mCabdUkgNfVdSongZD9UIkXMLsqL7/LHFdbvE6GgPIk9bhErGg2c1wxthyV7huVyeJ2yeAGZG2yGLUEbNTea5Q/KZ5/rRjjEAcMVlifUBUjq0NaQGr8BAJJfahpkRtOHVRJhh60Ye6LBgX135sHRfPsyA/tvcnIerPlboDAZkIK9y+k2hmwG8IQDXv2UK0l7LjOa3MEHYzhZfc5RhlTcuHmJv5TtK14p3OO8OW3MGsHEmnrjwnqO6TzMf8SNGteynrj92S/uHsYibbOMgg5M7HL2TPOKYxpnJpt0vZkNMLI/MOMZlr9xsXhhWDfjgi7ipwKhcV8QN X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR15MB4088.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFS:(39860400002)(136003)(346002)(376002)(396003)(366004)(83380400001)(316002)(36756003)(54906003)(31686004)(2906002)(8936002)(8676002)(16526019)(53546011)(66946007)(52116002)(6486002)(186003)(2616005)(66556008)(66476007)(86362001)(5660300002)(6916009)(31696002)(4326008)(478600001)(43740500002);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData: =?utf-8?B?dFRUZ2JHQlMyaVBZbElrMk5tTGkzR0lBaU50anNIdFJtNFJLQURva1UzYmhQ?= =?utf-8?B?bit1ZzA4ZmFLTHJRNmQyY29hQkZoeXFaeGl3ZytYOVdTMHN4R3g4cER3QThS?= =?utf-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HJCZEd0Nmdv?= =?utf-8?B?cWJMZlkzbHhEamJ1Z3c3WFFXWjRFNjF4V2hxOTM1Q01OVEZNR1lsbStXVU9y?= =?utf-8?B?dGdjUmtuLzJHVDhSZ1B1UUcvLzdacGlrR1lveEFJeDZDMHA4U2ZHV0w1M3NN?= =?utf-8?B?Y2lxRFlaU1M3ci9WTVhPekRTSE41cUVlL29qdFhDQytjd25IZ296SnhraTFT?= =?utf-8?B?VStiZVFBSE50UmhPRGt5SkprMHprbTNrMlVoblU2MWlySTI1bEVRcjIxb09n?= =?utf-8?B?Z1NYUU81SE0zM1JILyswKzhOUnZ5OGJhbHBNQUN3ODFiblRZbjZmZmlBL1di?= =?utf-8?B?dFpDVWU1djhsZnRFa0pPWUk0bFBFaG9HMTR2c2FXdUNCbXZKRnhvUkkrVG51?= =?utf-8?B?cmNaeEd4ZjdkNVE9PQ==?= X-MS-Exchange-CrossTenant-Network-Message-Id: 1673778f-cee3-4557-f8b7-08d89553ea4e X-MS-Exchange-CrossTenant-AuthSource: BYAPR15MB4088.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Nov 2020 17:18:12.0432 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: W9XiH9iiHmhqNl4A1JiaQfaLlvybAi1cFrOrbDjhGwwS5AoBJpd/2lMe7GgstWDY X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR15MB2565 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312,18.0.737 definitions=2020-11-30_06:2020-11-30,2020-11-30 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 lowpriorityscore=0 malwarescore=0 suspectscore=0 clxscore=1015 impostorscore=0 mlxlogscore=999 bulkscore=0 phishscore=0 spamscore=0 adultscore=0 mlxscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011300112 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/28/20 5:34 PM, Alexei Starovoitov wrote: > On Fri, Nov 27, 2020 at 09:35:07PM -0800, Yonghong Song wrote: >> >> >> On 11/27/20 9:57 AM, Brendan Jackman wrote: >>> Including only interpreter and x86 JIT support. >>> >>> x86 doesn't provide an atomic exchange-and-subtract instruction that >>> could be used for BPF_SUB | BPF_FETCH, however we can just emit a NEG >>> followed by an XADD to get the same effect. >>> >>> Signed-off-by: Brendan Jackman >>> --- >>> arch/x86/net/bpf_jit_comp.c | 16 ++++++++++++++-- >>> include/linux/filter.h | 20 ++++++++++++++++++++ >>> kernel/bpf/core.c | 1 + >>> kernel/bpf/disasm.c | 16 ++++++++++++---- >>> kernel/bpf/verifier.c | 2 ++ >>> tools/include/linux/filter.h | 20 ++++++++++++++++++++ >>> 6 files changed, 69 insertions(+), 6 deletions(-) >>> >>> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c >>> index 7431b2937157..a8a9fab13fcf 100644 >>> --- a/arch/x86/net/bpf_jit_comp.c >>> +++ b/arch/x86/net/bpf_jit_comp.c >>> @@ -823,6 +823,7 @@ static int emit_atomic(u8 **pprog, u8 atomic_op, >>> /* emit opcode */ >>> switch (atomic_op) { >>> + case BPF_SUB: >>> case BPF_ADD: >>> /* lock *(u32/u64*)(dst_reg + off) = src_reg */ >>> EMIT1(simple_alu_opcodes[atomic_op]); >>> @@ -1306,8 +1307,19 @@ st: if (is_imm8(insn->off)) >>> case BPF_STX | BPF_ATOMIC | BPF_W: >>> case BPF_STX | BPF_ATOMIC | BPF_DW: >>> - err = emit_atomic(&prog, insn->imm, dst_reg, src_reg, >>> - insn->off, BPF_SIZE(insn->code)); >>> + if (insn->imm == (BPF_SUB | BPF_FETCH)) { >>> + /* >>> + * x86 doesn't have an XSUB insn, so we negate >>> + * and XADD instead. >>> + */ >>> + emit_neg(&prog, src_reg, BPF_SIZE(insn->code) == BPF_DW); >>> + err = emit_atomic(&prog, BPF_ADD | BPF_FETCH, >>> + dst_reg, src_reg, insn->off, >>> + BPF_SIZE(insn->code)); >>> + } else { >>> + err = emit_atomic(&prog, insn->imm, dst_reg, src_reg, >>> + insn->off, BPF_SIZE(insn->code)); >>> + } >>> if (err) >>> return err; >>> break; >>> diff --git a/include/linux/filter.h b/include/linux/filter.h >>> index 6186280715ed..a20a3a536bf5 100644 >>> --- a/include/linux/filter.h >>> +++ b/include/linux/filter.h >>> @@ -280,6 +280,26 @@ static inline bool insn_is_zext(const struct bpf_insn *insn) >>> .off = OFF, \ >>> .imm = BPF_ADD | BPF_FETCH }) >>> +/* Atomic memory sub, *(uint *)(dst_reg + off16) -= src_reg */ >>> + >>> +#define BPF_ATOMIC_SUB(SIZE, DST, SRC, OFF) \ >>> + ((struct bpf_insn) { \ >>> + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_ATOMIC, \ >>> + .dst_reg = DST, \ >>> + .src_reg = SRC, \ >>> + .off = OFF, \ >>> + .imm = BPF_SUB }) >> >> Currently, llvm does not support XSUB, should we support it in llvm? >> At source code, as implemented in JIT, user can just do a negate >> followed by xadd. > > I forgot we have BPF_NEG insn :) > Indeed it's probably easier to handle atomic_fetch_sub() builtin > completely on llvm side. It can generate bpf_neg followed by atomic_fetch_add. Just tried. llvm selectiondag won't be able to automatically convert atomic_fetch_sub to neg + atomic_fetch_add. So there will be a need in BPFInstrInfo.td to match atomic_fetch_sub IR pattern. I will experiment this together with xsub. > No need to burden verifier, interpreter and JITs with it. >