Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C37EC433E0 for ; Thu, 4 Mar 2021 15:51:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5559264F02 for ; Thu, 4 Mar 2021 15:51:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232750AbhCDPuo (ORCPT ); Thu, 4 Mar 2021 10:50:44 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:4714 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234226AbhCDPul (ORCPT ); Thu, 4 Mar 2021 10:50:41 -0500 Received: from pps.filterd (m0098394.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 124FeihT061291; Thu, 4 Mar 2021 10:49:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=message-id : subject : from : to : cc : date : in-reply-to : references : content-type : mime-version : content-transfer-encoding; s=pp1; bh=SmZCnX2RcZ9lQoDkHRDr96FCBBnIXW5vp27uFvk+jDY=; b=YU98A7XCeuqymMhXTPniqllFXELiOzuRuYCvnFstYrzrPu7Zfcky/1I2Jzpelam80a55 TTfhbZVW/V8nt+tULydc/IcmFmxgOljhe5Zu8/JNXE/1E39LhDeWXuGf1I8GhgEuqw3Y zrOeqJkuN+8j+1/bYGBJyt+C+j/jSBHUd6wiqsJSunNyTlrEsZrtBJku1vSht7h9m3qU Q6GgvVSF15xDAPpStoLOZyHxI62pPF/y46kB/H5oKkjFNaze70gGum/dVoXl0xSPqIDV qn++syzi+PIqQcqn3B1yy1g50LEgqih61Rs2IHIXr3fN4ERPb6gKPxX4HGPwSFs88Dpj oQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3732d80hj1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 04 Mar 2021 10:49:48 -0500 Received: from m0098394.ppops.net (m0098394.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 124FefTr060859; Thu, 4 Mar 2021 10:49:47 -0500 Received: from ppma04fra.de.ibm.com (6a.4a.5195.ip4.static.sl-reverse.com [149.81.74.106]) by mx0a-001b2d01.pphosted.com with ESMTP id 3732d80hbx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 04 Mar 2021 10:49:47 -0500 Received: from pps.filterd (ppma04fra.de.ibm.com [127.0.0.1]) by ppma04fra.de.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 124FXThE012816; Thu, 4 Mar 2021 15:49:44 GMT Received: from b06avi18626390.portsmouth.uk.ibm.com (b06avi18626390.portsmouth.uk.ibm.com [9.149.26.192]) by ppma04fra.de.ibm.com with ESMTP id 37150csfj6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 04 Mar 2021 15:49:44 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06avi18626390.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 124FnRD531129918 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 4 Mar 2021 15:49:27 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C925311C04A; Thu, 4 Mar 2021 15:49:41 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5675111C05B; Thu, 4 Mar 2021 15:49:41 +0000 (GMT) Received: from sig-9-145-31-74.uk.ibm.com (unknown [9.145.31.74]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Thu, 4 Mar 2021 15:49:41 +0000 (GMT) Message-ID: Subject: Re: [PATCH v6 bpf-next] bpf: Explicitly zero-extend R0 after 32-bit cmpxchg From: Ilya Leoshkevich To: Brendan Jackman , bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , KP Singh , Florent Revest , Martin KaFai Lau Date: Thu, 04 Mar 2021 16:49:41 +0100 In-Reply-To: <20210303110402.3965626-1-jackmanb@google.com> References: <20210303110402.3965626-1-jackmanb@google.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.38.4 (3.38.4-1.fc33) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.761 definitions=2021-03-04_05:2021-03-03,2021-03-04 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 mlxlogscore=999 clxscore=1015 bulkscore=0 mlxscore=0 spamscore=0 phishscore=0 priorityscore=1501 malwarescore=0 suspectscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2103040075 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org On Wed, 2021-03-03 at 11:04 +0000, Brendan Jackman wrote: > As pointed out by Ilya and explained in the new comment, there's a > discrepancy between x86 and BPF CMPXCHG semantics: BPF always loads > the value from memory into r0, while x86 only does so when r0 and the > value in memory are different. The same issue affects s390. > > At first this might sound like pure semantics, but it makes a real > difference when the comparison is 32-bit, since the load will > zero-extend r0/rax. > > The fix is to explicitly zero-extend rax after doing such a > CMPXCHG. Since this problem affects multiple archs, this is done in > the verifier by patching in a BPF_ZEXT_REG instruction after every > 32-bit cmpxchg. Any archs that don't need such manual zero-extension > can do a look-ahead with insn_is_zext to skip the unnecessary mov. > > Reported-by: Ilya Leoshkevich > Fixes: 5ffa25502b5a ("bpf: Add instructions for atomic_[cmp]xchg") > Signed-off-by: Brendan Jackman [...] Acked-by: Ilya Leoshkevich # on s390, bpf-next + 83a2881903f3 + this commit Tested-by: Ilya Leoshkevich