Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp237287pxx; Wed, 28 Oct 2020 03:44:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7sEI+QmK18atObRAMCp2nCOPq1QPs+tzY2/Lg+lnvtPMONZCEKsEppTh3Fc8uHaQOO8wm X-Received: by 2002:a50:d555:: with SMTP id f21mr7122274edj.334.1603881850345; Wed, 28 Oct 2020 03:44:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603881850; cv=none; d=google.com; s=arc-20160816; b=JxVQIQxhBrvMmcXUDBtGw163aPnK2ktT3TYfktHlAEbt55j3Bf0lVW2MPscHlC29fR m+f3O11U83Fp1FHrvAnR+pAFTL8T5v7ih02QGpZqZZ20kL5egivfe4A2xNx8FosVSbA9 ebUdLnO5AYluG1R76UuO2ZcSRQPpuiLpBOxhDR7NPCDQicvI8TQKZEpKzveFLH+9lvQh bV2mbd44pQVeH7DWXQyEKInU7MHDRx5K4/zbkXu0E4YBORNyatz5ze9+KvGiW01FDWoH e2Ux6WACuTfKzEN7lRbEXoJ+YZ8CAvp5qcT2optu/eetfBwXE+FwrQPks2QnR/8uioKq akPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Ky5XOnoA5FazKREDNTCt0WMFUeVRIccm+j5v/d3Eg4A=; b=UGoYnVufgy3TXBygpe2XYtnPlwZzDth41hbDM+iz9D2rSbyqdkRXPzA1BNOLTqxHd3 Jrbp5BelEPLEppglRBcsYWSuvNBFJ/g55gvz6tqxIPwYft/ritWL/3fmTiXtC1TFnlCV Py3a5yASU09S3eQxrPSwZ3gn2Z4S1aP2wMNateapE6ZL8/40jfNjOLKw55vCTe7vFycL v2p877gK657950/fGTND9DwV9igyUrpkdjSKL9xel3e+rPr2kVo2wIMk6uMMwpIfYxTu dw+w5+Qo7ZA8lGem7fjVh1rvH3XpqrR4b8jAPz1dtVMCKtdwod9fbRSia5WhDbkPqYuS Rnpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=N9QzzNWp; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s18si2795558ejd.147.2020.10.28.03.43.47; Wed, 28 Oct 2020 03:44:10 -0700 (PDT) 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=@kernel.org header.s=default header.b=N9QzzNWp; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1764036AbgJ0OqC (ORCPT + 99 others); Tue, 27 Oct 2020 10:46:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:45870 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2899938AbgJ0Op5 (ORCPT ); Tue, 27 Oct 2020 10:45:57 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E558F22258; Tue, 27 Oct 2020 14:45:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603809955; bh=XvMgVztAh3JO68OK0QUk3KY/LnZc26xvhCUuPLNnCpA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N9QzzNWp0kvd+C27eoXUjyBJWDoqTcGFAr2Wa5mnrYi1kTAbE8RnB5FyE9RUku0h3 N9oU3tCH7oSRmVjtcDInAcyJCYGtnlLG3NWFqoQgbj2uR0fQBia6SbsNjt5V9cNP3G s/WxWHigsrqctojxX/S+WBXnV1C9uIa9aD1wQHPQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrii Nakryiko , Yonghong Song , Alexei Starovoitov , Sasha Levin Subject: [PATCH 5.4 377/408] selftests/bpf: Fix test_sysctl_loop{1, 2} failure due to clang change Date: Tue, 27 Oct 2020 14:55:15 +0100 Message-Id: <20201027135512.493880274@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135455.027547757@linuxfoundation.org> References: <20201027135455.027547757@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yonghong Song [ Upstream commit 7fb5eefd76394cfefb380724a87ca40b47d44405 ] Andrii reported that with latest clang, when building selftests, we have error likes: error: progs/test_sysctl_loop1.c:23:16: in function sysctl_tcp_mem i32 (%struct.bpf_sysctl*): Looks like the BPF stack limit of 512 bytes is exceeded. Please move large on stack variables into BPF per-cpu array map. The error is triggered by the following LLVM patch: https://reviews.llvm.org/D87134 For example, the following code is from test_sysctl_loop1.c: static __always_inline int is_tcp_mem(struct bpf_sysctl *ctx) { volatile char tcp_mem_name[] = "net/ipv4/tcp_mem/very_very_very_very_long_pointless_string"; ... } Without the above LLVM patch, the compiler did optimization to load the string (59 bytes long) with 7 64bit loads, 1 8bit load and 1 16bit load, occupying 64 byte stack size. With the above LLVM patch, the compiler only uses 8bit loads, but subregister is 32bit. So stack requirements become 4 * 59 = 236 bytes. Together with other stuff on the stack, total stack size exceeds 512 bytes, hence compiler complains and quits. To fix the issue, removing "volatile" key word or changing "volatile" to "const"/"static const" does not work, the string is put in .rodata.str1.1 section, which libbpf did not process it and errors out with libbpf: elf: skipping unrecognized data section(6) .rodata.str1.1 libbpf: prog 'sysctl_tcp_mem': bad map relo against '.L__const.is_tcp_mem.tcp_mem_name' in section '.rodata.str1.1' Defining the string const as global variable can fix the issue as it puts the string constant in '.rodata' section which is recognized by libbpf. In the future, when libbpf can process '.rodata.str*.*' properly, the global definition can be changed back to local definition. Defining tcp_mem_name as a global, however, triggered a verifier failure. ./test_progs -n 7/21 libbpf: load bpf program failed: Permission denied libbpf: -- BEGIN DUMP LOG --- libbpf: invalid stack off=0 size=1 verification time 6975 usec stack depth 160+64 processed 889 insns (limit 1000000) max_states_per_insn 4 total_states 14 peak_states 14 mark_read 10 libbpf: -- END LOG -- libbpf: failed to load program 'sysctl_tcp_mem' libbpf: failed to load object 'test_sysctl_loop2.o' test_bpf_verif_scale:FAIL:114 #7/21 test_sysctl_loop2.o:FAIL This actually exposed a bpf program bug. In test_sysctl_loop{1,2}, we have code like const char tcp_mem_name[] = "<...long string...>"; ... char name[64]; ... for (i = 0; i < sizeof(tcp_mem_name); ++i) if (name[i] != tcp_mem_name[i]) return 0; In the above code, if sizeof(tcp_mem_name) > 64, name[i] access may be out of bound. The sizeof(tcp_mem_name) is 59 for test_sysctl_loop1.c and 79 for test_sysctl_loop2.c. Without promotion-to-global change, old compiler generates code where the overflowed stack access is actually filled with valid value, so hiding the bpf program bug. With promotion-to-global change, the code is different, more specifically, the previous loading constants to stack is gone, and "name" occupies stack[-64:0] and overflow access triggers a verifier error. To fix the issue, adjust "name" buffer size properly. Reported-by: Andrii Nakryiko Signed-off-by: Yonghong Song Signed-off-by: Alexei Starovoitov Acked-by: Andrii Nakryiko Link: https://lore.kernel.org/bpf/20200909171542.3673449-1-yhs@fb.com Signed-off-by: Sasha Levin --- tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 4 ++-- tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/bpf/progs/test_sysctl_loop1.c b/tools/testing/selftests/bpf/progs/test_sysctl_loop1.c index d22e438198cf7..9af8822ece477 100644 --- a/tools/testing/selftests/bpf/progs/test_sysctl_loop1.c +++ b/tools/testing/selftests/bpf/progs/test_sysctl_loop1.c @@ -18,11 +18,11 @@ #define MAX_ULONG_STR_LEN 7 #define MAX_VALUE_STR_LEN (TCP_MEM_LOOPS * MAX_ULONG_STR_LEN) +const char tcp_mem_name[] = "net/ipv4/tcp_mem/very_very_very_very_long_pointless_string"; static __always_inline int is_tcp_mem(struct bpf_sysctl *ctx) { - volatile char tcp_mem_name[] = "net/ipv4/tcp_mem/very_very_very_very_long_pointless_string"; unsigned char i; - char name[64]; + char name[sizeof(tcp_mem_name)]; int ret; memset(name, 0, sizeof(name)); diff --git a/tools/testing/selftests/bpf/progs/test_sysctl_loop2.c b/tools/testing/selftests/bpf/progs/test_sysctl_loop2.c index cb201cbe11e77..55251046c9b73 100644 --- a/tools/testing/selftests/bpf/progs/test_sysctl_loop2.c +++ b/tools/testing/selftests/bpf/progs/test_sysctl_loop2.c @@ -18,11 +18,11 @@ #define MAX_ULONG_STR_LEN 7 #define MAX_VALUE_STR_LEN (TCP_MEM_LOOPS * MAX_ULONG_STR_LEN) +const char tcp_mem_name[] = "net/ipv4/tcp_mem/very_very_very_very_long_pointless_string_to_stress_byte_loop"; static __attribute__((noinline)) int is_tcp_mem(struct bpf_sysctl *ctx) { - volatile char tcp_mem_name[] = "net/ipv4/tcp_mem/very_very_very_very_long_pointless_string_to_stress_byte_loop"; unsigned char i; - char name[64]; + char name[sizeof(tcp_mem_name)]; int ret; memset(name, 0, sizeof(name)); -- 2.25.1