--- /dev/null
+From kamal@canonical.com Mon May 18 19:07:29 2020
+From: Kamal Mostafa <kamal@canonical.com>
+Date: Tue, 28 Apr 2020 10:21:21 -0700
+Subject: bpf: Test_progs, fix test_get_stack_rawtp_err.c build
+To: stable@vger.kernel.org
+Cc: Kamal Mostafa <kamal@canonical.com>
+Message-ID: <20200428172121.8635-1-kamal@canonical.com>
+
+From: Kamal Mostafa <kamal@canonical.com>
+
+The linux-5.4.y commit 8781011a302b ("bpf: Test_progs, add test to catch
+retval refine error handling") fails to build when libbpf headers are
+not installed, as it tries to include <bpf/bpf_helpers.h>:
+
+ progs/test_get_stack_rawtp_err.c:4:10:
+ fatal error: 'bpf/bpf_helpers.h' file not found
+
+For 5.4-stable (only) the new test prog needs to include "bpf_helpers.h"
+instead (like all the rest of progs/*.c do) because 5.4-stable does not
+carry commit e01a75c15969 ("libbpf: Move bpf_{helpers, helper_defs,
+endian, tracing}.h into libbpf").
+
+Signed-off-by: Kamal Mostafa <kamal@canonical.com>
+Fixes: 8781011a302b ("bpf: Test_progs, add test to catch retval refine error handling")
+Cc: <stable@vger.kernel.org> # v5.4
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ tools/testing/selftests/bpf/progs/test_get_stack_rawtp_err.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+--- a/tools/testing/selftests/bpf/progs/test_get_stack_rawtp_err.c
++++ b/tools/testing/selftests/bpf/progs/test_get_stack_rawtp_err.c
+@@ -1,7 +1,7 @@
+ // SPDX-License-Identifier: GPL-2.0
+
+ #include <linux/bpf.h>
+-#include <bpf/bpf_helpers.h>
++#include "bpf_helpers.h"
+
+ #define MAX_STACK_RAWTP 10
+
--- /dev/null
+From 6803ee25f0ead1e836808acb14396bb9a9849113 Mon Sep 17 00:00:00 2001
+From: Andrii Nakryiko <andriin@fb.com>
+Date: Wed, 11 Dec 2019 17:35:48 -0800
+Subject: libbpf: Extract and generalize CPU mask parsing logic
+
+From: Andrii Nakryiko <andriin@fb.com>
+
+commit 6803ee25f0ead1e836808acb14396bb9a9849113 upstream.
+
+This logic is re-used for parsing a set of online CPUs. Having it as an
+isolated piece of code working with input string makes it conveninent to test
+this logic as well. While refactoring, also improve the robustness of original
+implementation.
+
+Signed-off-by: Andrii Nakryiko <andriin@fb.com>
+Signed-off-by: Alexei Starovoitov <ast@kernel.org>
+Link: https://lore.kernel.org/bpf/20191212013548.1690564-1-andriin@fb.com
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+
+---
+ tools/lib/bpf/libbpf.c | 128 ++++++++++++++++++++++++++--------------
+ tools/lib/bpf/libbpf_internal.h | 2
+ 2 files changed, 87 insertions(+), 43 deletions(-)
+
+--- a/tools/lib/bpf/libbpf.c
++++ b/tools/lib/bpf/libbpf.c
+@@ -5905,62 +5905,104 @@ void bpf_program__bpil_offs_to_addr(stru
+ }
+ }
+
+-int libbpf_num_possible_cpus(void)
++int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
+ {
+- static const char *fcpu = "/sys/devices/system/cpu/possible";
+- int len = 0, n = 0, il = 0, ir = 0;
+- unsigned int start = 0, end = 0;
+- int tmp_cpus = 0;
+- static int cpus;
+- char buf[128];
+- int error = 0;
+- int fd = -1;
++ int err = 0, n, len, start, end = -1;
++ bool *tmp;
+
+- tmp_cpus = READ_ONCE(cpus);
+- if (tmp_cpus > 0)
+- return tmp_cpus;
++ *mask = NULL;
++ *mask_sz = 0;
++
++ /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
++ while (*s) {
++ if (*s == ',' || *s == '\n') {
++ s++;
++ continue;
++ }
++ n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len);
++ if (n <= 0 || n > 2) {
++ pr_warning("Failed to get CPU range %s: %d\n", s, n);
++ err = -EINVAL;
++ goto cleanup;
++ } else if (n == 1) {
++ end = start;
++ }
++ if (start < 0 || start > end) {
++ pr_warning("Invalid CPU range [%d,%d] in %s\n",
++ start, end, s);
++ err = -EINVAL;
++ goto cleanup;
++ }
++ tmp = realloc(*mask, end + 1);
++ if (!tmp) {
++ err = -ENOMEM;
++ goto cleanup;
++ }
++ *mask = tmp;
++ memset(tmp + *mask_sz, 0, start - *mask_sz);
++ memset(tmp + start, 1, end - start + 1);
++ *mask_sz = end + 1;
++ s += len;
++ }
++ if (!*mask_sz) {
++ pr_warning("Empty CPU range\n");
++ return -EINVAL;
++ }
++ return 0;
++cleanup:
++ free(*mask);
++ *mask = NULL;
++ return err;
++}
++
++int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
++{
++ int fd, err = 0, len;
++ char buf[128];
+
+ fd = open(fcpu, O_RDONLY);
+ if (fd < 0) {
+- error = errno;
+- pr_warning("Failed to open file %s: %s\n",
+- fcpu, strerror(error));
+- return -error;
++ err = -errno;
++ pr_warning("Failed to open cpu mask file %s: %d\n", fcpu, err);
++ return err;
+ }
+ len = read(fd, buf, sizeof(buf));
+ close(fd);
+ if (len <= 0) {
+- error = len ? errno : EINVAL;
+- pr_warning("Failed to read # of possible cpus from %s: %s\n",
+- fcpu, strerror(error));
+- return -error;
+- }
+- if (len == sizeof(buf)) {
+- pr_warning("File %s size overflow\n", fcpu);
+- return -EOVERFLOW;
++ err = len ? -errno : -EINVAL;
++ pr_warning("Failed to read cpu mask from %s: %d\n", fcpu, err);
++ return err;
++ }
++ if (len >= sizeof(buf)) {
++ pr_warning("CPU mask is too big in file %s\n", fcpu);
++ return -E2BIG;
+ }
+ buf[len] = '\0';
+
+- for (ir = 0, tmp_cpus = 0; ir <= len; ir++) {
+- /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
+- if (buf[ir] == ',' || buf[ir] == '\0') {
+- buf[ir] = '\0';
+- n = sscanf(&buf[il], "%u-%u", &start, &end);
+- if (n <= 0) {
+- pr_warning("Failed to get # CPUs from %s\n",
+- &buf[il]);
+- return -EINVAL;
+- } else if (n == 1) {
+- end = start;
+- }
+- tmp_cpus += end - start + 1;
+- il = ir + 1;
+- }
+- }
+- if (tmp_cpus <= 0) {
+- pr_warning("Invalid #CPUs %d from %s\n", tmp_cpus, fcpu);
+- return -EINVAL;
++ return parse_cpu_mask_str(buf, mask, mask_sz);
++}
++
++int libbpf_num_possible_cpus(void)
++{
++ static const char *fcpu = "/sys/devices/system/cpu/possible";
++ static int cpus;
++ int err, n, i, tmp_cpus;
++ bool *mask;
++
++ tmp_cpus = READ_ONCE(cpus);
++ if (tmp_cpus > 0)
++ return tmp_cpus;
++
++ err = parse_cpu_mask_file(fcpu, &mask, &n);
++ if (err)
++ return err;
++
++ tmp_cpus = 0;
++ for (i = 0; i < n; i++) {
++ if (mask[i])
++ tmp_cpus++;
+ }
++ free(mask);
+
+ WRITE_ONCE(cpus, tmp_cpus);
+ return tmp_cpus;
+--- a/tools/lib/bpf/libbpf_internal.h
++++ b/tools/lib/bpf/libbpf_internal.h
+@@ -63,6 +63,8 @@ do { \
+ #define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
+ #define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
+
++int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz);
++int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);
+ int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
+ const char *str_sec, size_t str_len);
+
--- /dev/null
+From andriin@fb.com Mon May 18 19:06:06 2020
+From: Andrii Nakryiko <andriin@fb.com>
+Date: Fri, 15 May 2020 17:40:17 -0700
+Subject: selftest/bpf: fix backported test_select_reuseport selftest changes
+To: <bpf@vger.kernel.org>, <netdev@vger.kernel.org>, <ast@fb.com>, <daniel@iogearbox.net>, <stable@vger.kernel.org>, <gregkh@linuxfoundation.org>
+Cc: <andrii.nakryiko@gmail.com>, <kernel-team@fb.com>, Andrii Nakryiko <andriin@fb.com>, Alexei Starovoitov <ast@kernel.org>
+Message-ID: <20200516004018.3500869-2-andriin@fb.com>
+
+From: Andrii Nakryiko <andriin@fb.com>
+
+Fix up RET_IF as CHECK macro to make selftests compile again.
+
+Fixes: b911c5e8686a ("selftests: bpf: Reset global state between reuseport test runs")
+Signed-off-by: Andrii Nakryiko <andriin@fb.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ tools/testing/selftests/bpf/test_select_reuseport.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+--- a/tools/testing/selftests/bpf/test_select_reuseport.c
++++ b/tools/testing/selftests/bpf/test_select_reuseport.c
+@@ -668,12 +668,12 @@ static void cleanup_per_test(void)
+
+ for (i = 0; i < NR_RESULTS; i++) {
+ err = bpf_map_update_elem(result_map, &i, &zero, BPF_ANY);
+- RET_IF(err, "reset elem in result_map",
++ CHECK(err, "reset elem in result_map",
+ "i:%u err:%d errno:%d\n", i, err, errno);
+ }
+
+ err = bpf_map_update_elem(linum_map, &zero, &zero, BPF_ANY);
+- RET_IF(err, "reset line number in linum_map", "err:%d errno:%d\n",
++ CHECK(err, "reset line number in linum_map", "err:%d errno:%d\n",
+ err, errno);
+
+ for (i = 0; i < REUSEPORT_ARRAY_SIZE; i++)