Skip to content

Commit 4af7b32

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Alexei Starovoitov says: ==================== pull-request: bpf 2020-08-21 The following pull-request contains BPF updates for your *net* tree. We've added 11 non-merge commits during the last 5 day(s) which contain a total of 12 files changed, 78 insertions(+), 24 deletions(-). The main changes are: 1) three fixes in BPF task iterator logic, from Yonghong. 2) fix for compressed dwarf sections in vmlinux, from Jiri. 3) fix xdp attach regression, from Andrii. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 774d977 + b16fc09 commit 4af7b32

File tree

12 files changed

+78
-24
lines changed

12 files changed

+78
-24
lines changed

include/uapi/linux/bpf.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ union bpf_attr {
767767
*
768768
* Also, note that **bpf_trace_printk**\ () is slow, and should
769769
* only be used for debugging purposes. For this reason, a notice
770-
* bloc (spanning several lines) is printed to kernel logs and
770+
* block (spanning several lines) is printed to kernel logs and
771771
* states that the helper should not be used "for production use"
772772
* the first time this helper is used (or more precisely, when
773773
* **trace_printk**\ () buffers are allocated). For passing values
@@ -1033,14 +1033,14 @@ union bpf_attr {
10331033
*
10341034
* int ret;
10351035
* struct bpf_tunnel_key key = {};
1036-
*
1036+
*
10371037
* ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
10381038
* if (ret < 0)
10391039
* return TC_ACT_SHOT; // drop packet
1040-
*
1040+
*
10411041
* if (key.remote_ipv4 != 0x0a000001)
10421042
* return TC_ACT_SHOT; // drop packet
1043-
*
1043+
*
10441044
* return TC_ACT_OK; // accept packet
10451045
*
10461046
* This interface can also be used with all encapsulation devices
@@ -1147,7 +1147,7 @@ union bpf_attr {
11471147
* Description
11481148
* Retrieve the realm or the route, that is to say the
11491149
* **tclassid** field of the destination for the *skb*. The
1150-
* indentifier retrieved is a user-provided tag, similar to the
1150+
* identifier retrieved is a user-provided tag, similar to the
11511151
* one used with the net_cls cgroup (see description for
11521152
* **bpf_get_cgroup_classid**\ () helper), but here this tag is
11531153
* held by a route (a destination entry), not by a task.

kernel/bpf/bpf_iter.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ static void bpf_iter_done_stop(struct seq_file *seq)
6767
iter_priv->done_stop = true;
6868
}
6969

70+
/* maximum visited objects before bailing out */
71+
#define MAX_ITER_OBJECTS 1000000
72+
7073
/* bpf_seq_read, a customized and simpler version for bpf iterator.
7174
* no_llseek is assumed for this file.
7275
* The following are differences from seq_read():
@@ -79,7 +82,7 @@ static ssize_t bpf_seq_read(struct file *file, char __user *buf, size_t size,
7982
{
8083
struct seq_file *seq = file->private_data;
8184
size_t n, offs, copied = 0;
82-
int err = 0;
85+
int err = 0, num_objs = 0;
8386
void *p;
8487

8588
mutex_lock(&seq->lock);
@@ -135,6 +138,7 @@ static ssize_t bpf_seq_read(struct file *file, char __user *buf, size_t size,
135138
while (1) {
136139
loff_t pos = seq->index;
137140

141+
num_objs++;
138142
offs = seq->count;
139143
p = seq->op->next(seq, p, &seq->index);
140144
if (pos == seq->index) {
@@ -153,6 +157,15 @@ static ssize_t bpf_seq_read(struct file *file, char __user *buf, size_t size,
153157
if (seq->count >= size)
154158
break;
155159

160+
if (num_objs >= MAX_ITER_OBJECTS) {
161+
if (offs == 0) {
162+
err = -EAGAIN;
163+
seq->op->stop(seq, p);
164+
goto done;
165+
}
166+
break;
167+
}
168+
156169
err = seq->op->show(seq, p);
157170
if (err > 0) {
158171
bpf_iter_dec_seq_num(seq);

kernel/bpf/task_iter.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
2929

3030
rcu_read_lock();
3131
retry:
32-
pid = idr_get_next(&ns->idr, tid);
32+
pid = find_ge_pid(*tid, ns);
3333
if (pid) {
34+
*tid = pid_nr_ns(pid, ns);
3435
task = get_pid_task(pid, PIDTYPE_PID);
3536
if (!task) {
3637
++*tid;
@@ -178,10 +179,11 @@ task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info,
178179
f = fcheck_files(curr_files, curr_fd);
179180
if (!f)
180181
continue;
182+
if (!get_file_rcu(f))
183+
continue;
181184

182185
/* set info->fd */
183186
info->fd = curr_fd;
184-
get_file(f);
185187
rcu_read_unlock();
186188
return f;
187189
}

net/core/dev.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8742,13 +8742,15 @@ struct bpf_xdp_link {
87428742
int flags;
87438743
};
87448744

8745-
static enum bpf_xdp_mode dev_xdp_mode(u32 flags)
8745+
static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags)
87468746
{
87478747
if (flags & XDP_FLAGS_HW_MODE)
87488748
return XDP_MODE_HW;
87498749
if (flags & XDP_FLAGS_DRV_MODE)
87508750
return XDP_MODE_DRV;
8751-
return XDP_MODE_SKB;
8751+
if (flags & XDP_FLAGS_SKB_MODE)
8752+
return XDP_MODE_SKB;
8753+
return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;
87528754
}
87538755

87548756
static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode)
@@ -8896,7 +8898,7 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
88968898
return -EINVAL;
88978899
}
88988900

8899-
mode = dev_xdp_mode(flags);
8901+
mode = dev_xdp_mode(dev, flags);
89008902
/* can't replace attached link */
89018903
if (dev_xdp_link(dev, mode)) {
89028904
NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");
@@ -8984,7 +8986,7 @@ static int dev_xdp_detach_link(struct net_device *dev,
89848986

89858987
ASSERT_RTNL();
89868988

8987-
mode = dev_xdp_mode(link->flags);
8989+
mode = dev_xdp_mode(dev, link->flags);
89888990
if (dev_xdp_link(dev, mode) != link)
89898991
return -EINVAL;
89908992

@@ -9080,7 +9082,7 @@ static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
90809082
goto out_unlock;
90819083
}
90829084

9083-
mode = dev_xdp_mode(xdp_link->flags);
9085+
mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
90849086
bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
90859087
err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
90869088
xdp_link->flags, new_prog);
@@ -9164,7 +9166,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
91649166
int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
91659167
int fd, int expected_fd, u32 flags)
91669168
{
9167-
enum bpf_xdp_mode mode = dev_xdp_mode(flags);
9169+
enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags);
91689170
struct bpf_prog *new_prog = NULL, *old_prog = NULL;
91699171
int err;
91709172

tools/bpf/bpftool/pids.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type)
134134
while (true) {
135135
ret = read(fd, buf, sizeof(buf));
136136
if (ret < 0) {
137+
if (errno == EAGAIN)
138+
continue;
137139
err = -errno;
138140
p_err("failed to read PID iterator output: %d", err);
139141
goto out;

tools/bpf/resolve_btfids/main.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,39 @@ static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
233233
return btf_id__add(root, id, false);
234234
}
235235

236+
/*
237+
* The data of compressed section should be aligned to 4
238+
* (for 32bit) or 8 (for 64 bit) bytes. The binutils ld
239+
* sets sh_addralign to 1, which makes libelf fail with
240+
* misaligned section error during the update:
241+
* FAILED elf_update(WRITE): invalid section alignment
242+
*
243+
* While waiting for ld fix, we fix the compressed sections
244+
* sh_addralign value manualy.
245+
*/
246+
static int compressed_section_fix(Elf *elf, Elf_Scn *scn, GElf_Shdr *sh)
247+
{
248+
int expected = gelf_getclass(elf) == ELFCLASS32 ? 4 : 8;
249+
250+
if (!(sh->sh_flags & SHF_COMPRESSED))
251+
return 0;
252+
253+
if (sh->sh_addralign == expected)
254+
return 0;
255+
256+
pr_debug2(" - fixing wrong alignment sh_addralign %u, expected %u\n",
257+
sh->sh_addralign, expected);
258+
259+
sh->sh_addralign = expected;
260+
261+
if (gelf_update_shdr(scn, sh) == 0) {
262+
printf("FAILED cannot update section header: %s\n",
263+
elf_errmsg(-1));
264+
return -1;
265+
}
266+
return 0;
267+
}
268+
236269
static int elf_collect(struct object *obj)
237270
{
238271
Elf_Scn *scn = NULL;
@@ -309,6 +342,9 @@ static int elf_collect(struct object *obj)
309342
obj->efile.idlist_shndx = idx;
310343
obj->efile.idlist_addr = sh.sh_addr;
311344
}
345+
346+
if (compressed_section_fix(elf, scn, &sh))
347+
return -1;
312348
}
313349

314350
return 0;

tools/include/uapi/linux/bpf.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ union bpf_attr {
767767
*
768768
* Also, note that **bpf_trace_printk**\ () is slow, and should
769769
* only be used for debugging purposes. For this reason, a notice
770-
* bloc (spanning several lines) is printed to kernel logs and
770+
* block (spanning several lines) is printed to kernel logs and
771771
* states that the helper should not be used "for production use"
772772
* the first time this helper is used (or more precisely, when
773773
* **trace_printk**\ () buffers are allocated). For passing values
@@ -1033,14 +1033,14 @@ union bpf_attr {
10331033
*
10341034
* int ret;
10351035
* struct bpf_tunnel_key key = {};
1036-
*
1036+
*
10371037
* ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
10381038
* if (ret < 0)
10391039
* return TC_ACT_SHOT; // drop packet
1040-
*
1040+
*
10411041
* if (key.remote_ipv4 != 0x0a000001)
10421042
* return TC_ACT_SHOT; // drop packet
1043-
*
1043+
*
10441044
* return TC_ACT_OK; // accept packet
10451045
*
10461046
* This interface can also be used with all encapsulation devices
@@ -1147,7 +1147,7 @@ union bpf_attr {
11471147
* Description
11481148
* Retrieve the realm or the route, that is to say the
11491149
* **tclassid** field of the destination for the *skb*. The
1150-
* indentifier retrieved is a user-provided tag, similar to the
1150+
* identifier retrieved is a user-provided tag, similar to the
11511151
* one used with the net_cls cgroup (see description for
11521152
* **bpf_get_cgroup_classid**\ () helper), but here this tag is
11531153
* held by a route (a destination entry), not by a task.

tools/lib/bpf/btf_dump.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ static void btf_dump_emit_struct_def(struct btf_dump *d,
879879
btf_dump_printf(d, ": %d", m_sz);
880880
off = m_off + m_sz;
881881
} else {
882-
m_sz = max(0LL, btf__resolve_size(d->btf, m->type));
882+
m_sz = max((__s64)0, btf__resolve_size(d->btf, m->type));
883883
off = m_off + m_sz * 8;
884884
}
885885
btf_dump_printf(d, ";");

tools/lib/bpf/libbpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2264,7 +2264,7 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
22642264
data = elf_getdata(scn, NULL);
22652265
if (!scn || !data) {
22662266
pr_warn("failed to get Elf_Data from map section %d (%s)\n",
2267-
obj->efile.maps_shndx, MAPS_ELF_SEC);
2267+
obj->efile.btf_maps_shndx, MAPS_ELF_SEC);
22682268
return -EINVAL;
22692269
}
22702270

tools/testing/selftests/bpf/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ test_lpm_map
66
test_tag
77
FEATURE-DUMP.libbpf
88
fixdep
9-
test_align
109
test_dev_cgroup
1110
/test_progs*
1211
test_tcpbpf_user

0 commit comments

Comments
 (0)