Skip to content

Commit ff0fadf

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Daniel Borkmann says: ==================== pull-request: bpf 2018-08-24 The following pull-request contains BPF updates for your *net* tree. The main changes are: 1) Fix BPF sockmap and tls where we get a hang in do_tcp_sendpages() when sndbuf is full due to missing calls into underlying socket's sk_write_space(), from John. 2) Two BPF sockmap fixes to reject invalid parameters on map creation and to fix a map element miscount on allocation failure. Another fix for BPF hash tables to use per hash table salt for jhash(), from Daniel. 3) Fix for bpftool's command line parsing in order to terminate on bad arguments instead of keeping looping in some border cases, from Quentin. 4) Fix error value of xdp_umem_assign_dev() in order to comply with expected bind ops error codes, from Prashant. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents c08eeba + 785e76d commit ff0fadf

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

kernel/bpf/hashtab.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/jhash.h>
1616
#include <linux/filter.h>
1717
#include <linux/rculist_nulls.h>
18+
#include <linux/random.h>
1819
#include <uapi/linux/btf.h>
1920
#include "percpu_freelist.h"
2021
#include "bpf_lru_list.h"
@@ -41,6 +42,7 @@ struct bpf_htab {
4142
atomic_t count; /* number of elements in this hashtable */
4243
u32 n_buckets; /* number of hash buckets */
4344
u32 elem_size; /* size of each element in bytes */
45+
u32 hashrnd;
4446
};
4547

4648
/* each htab element is struct htab_elem + key + value */
@@ -371,6 +373,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
371373
if (!htab->buckets)
372374
goto free_htab;
373375

376+
htab->hashrnd = get_random_int();
374377
for (i = 0; i < htab->n_buckets; i++) {
375378
INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
376379
raw_spin_lock_init(&htab->buckets[i].lock);
@@ -402,9 +405,9 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
402405
return ERR_PTR(err);
403406
}
404407

405-
static inline u32 htab_map_hash(const void *key, u32 key_len)
408+
static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
406409
{
407-
return jhash(key, key_len, 0);
410+
return jhash(key, key_len, hashrnd);
408411
}
409412

410413
static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
@@ -470,7 +473,7 @@ static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
470473

471474
key_size = map->key_size;
472475

473-
hash = htab_map_hash(key, key_size);
476+
hash = htab_map_hash(key, key_size, htab->hashrnd);
474477

475478
head = select_bucket(htab, hash);
476479

@@ -597,7 +600,7 @@ static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
597600
if (!key)
598601
goto find_first_elem;
599602

600-
hash = htab_map_hash(key, key_size);
603+
hash = htab_map_hash(key, key_size, htab->hashrnd);
601604

602605
head = select_bucket(htab, hash);
603606

@@ -824,7 +827,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
824827

825828
key_size = map->key_size;
826829

827-
hash = htab_map_hash(key, key_size);
830+
hash = htab_map_hash(key, key_size, htab->hashrnd);
828831

829832
b = __select_bucket(htab, hash);
830833
head = &b->head;
@@ -880,7 +883,7 @@ static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
880883

881884
key_size = map->key_size;
882885

883-
hash = htab_map_hash(key, key_size);
886+
hash = htab_map_hash(key, key_size, htab->hashrnd);
884887

885888
b = __select_bucket(htab, hash);
886889
head = &b->head;
@@ -945,7 +948,7 @@ static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
945948

946949
key_size = map->key_size;
947950

948-
hash = htab_map_hash(key, key_size);
951+
hash = htab_map_hash(key, key_size, htab->hashrnd);
949952

950953
b = __select_bucket(htab, hash);
951954
head = &b->head;
@@ -998,7 +1001,7 @@ static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
9981001

9991002
key_size = map->key_size;
10001003

1001-
hash = htab_map_hash(key, key_size);
1004+
hash = htab_map_hash(key, key_size, htab->hashrnd);
10021005

10031006
b = __select_bucket(htab, hash);
10041007
head = &b->head;
@@ -1071,7 +1074,7 @@ static int htab_map_delete_elem(struct bpf_map *map, void *key)
10711074

10721075
key_size = map->key_size;
10731076

1074-
hash = htab_map_hash(key, key_size);
1077+
hash = htab_map_hash(key, key_size, htab->hashrnd);
10751078
b = __select_bucket(htab, hash);
10761079
head = &b->head;
10771080

@@ -1103,7 +1106,7 @@ static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
11031106

11041107
key_size = map->key_size;
11051108

1106-
hash = htab_map_hash(key, key_size);
1109+
hash = htab_map_hash(key, key_size, htab->hashrnd);
11071110
b = __select_bucket(htab, hash);
11081111
head = &b->head;
11091112

kernel/bpf/sockmap.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,12 +1427,15 @@ static void smap_tx_work(struct work_struct *w)
14271427
static void smap_write_space(struct sock *sk)
14281428
{
14291429
struct smap_psock *psock;
1430+
void (*write_space)(struct sock *sk);
14301431

14311432
rcu_read_lock();
14321433
psock = smap_psock_sk(sk);
14331434
if (likely(psock && test_bit(SMAP_TX_RUNNING, &psock->state)))
14341435
schedule_work(&psock->tx_work);
1436+
write_space = psock->save_write_space;
14351437
rcu_read_unlock();
1438+
write_space(sk);
14361439
}
14371440

14381441
static void smap_stop_sock(struct smap_psock *psock, struct sock *sk)
@@ -2140,7 +2143,9 @@ static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
21402143
return ERR_PTR(-EPERM);
21412144

21422145
/* check sanity of attributes */
2143-
if (attr->max_entries == 0 || attr->value_size != 4 ||
2146+
if (attr->max_entries == 0 ||
2147+
attr->key_size == 0 ||
2148+
attr->value_size != 4 ||
21442149
attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
21452150
return ERR_PTR(-EINVAL);
21462151

@@ -2267,8 +2272,10 @@ static struct htab_elem *alloc_sock_hash_elem(struct bpf_htab *htab,
22672272
}
22682273
l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
22692274
htab->map.numa_node);
2270-
if (!l_new)
2275+
if (!l_new) {
2276+
atomic_dec(&htab->count);
22712277
return ERR_PTR(-ENOMEM);
2278+
}
22722279

22732280
memcpy(l_new->key, key, key_size);
22742281
l_new->sk = sk;

net/tls/tls_main.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,14 @@ static void tls_write_space(struct sock *sk)
213213
{
214214
struct tls_context *ctx = tls_get_ctx(sk);
215215

216-
/* We are already sending pages, ignore notification */
217-
if (ctx->in_tcp_sendpages)
216+
/* If in_tcp_sendpages call lower protocol write space handler
217+
* to ensure we wake up any waiting operations there. For example
218+
* if do_tcp_sendpages where to call sk_wait_event.
219+
*/
220+
if (ctx->in_tcp_sendpages) {
221+
ctx->sk_write_space(sk);
218222
return;
223+
}
219224

220225
if (!sk->sk_write_pending && tls_is_pending_closed_record(ctx)) {
221226
gfp_t sk_allocation = sk->sk_allocation;

net/xdp/xdp_umem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
7474
return 0;
7575

7676
if (!dev->netdev_ops->ndo_bpf || !dev->netdev_ops->ndo_xsk_async_xmit)
77-
return force_zc ? -ENOTSUPP : 0; /* fail or fallback */
77+
return force_zc ? -EOPNOTSUPP : 0; /* fail or fallback */
7878

7979
bpf.command = XDP_QUERY_XSK_UMEM;
8080

8181
rtnl_lock();
8282
err = xdp_umem_query(dev, queue_id);
8383
if (err) {
84-
err = err < 0 ? -ENOTSUPP : -EBUSY;
84+
err = err < 0 ? -EOPNOTSUPP : -EBUSY;
8585
goto err_rtnl_unlock;
8686
}
8787

tools/bpf/bpftool/map_perf_ring.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ int do_event_pipe(int argc, char **argv)
194194
}
195195

196196
while (argc) {
197-
if (argc < 2)
197+
if (argc < 2) {
198198
BAD_ARG();
199+
goto err_close_map;
200+
}
199201

200202
if (is_prefix(*argv, "cpu")) {
201203
char *endptr;
@@ -221,6 +223,7 @@ int do_event_pipe(int argc, char **argv)
221223
NEXT_ARG();
222224
} else {
223225
BAD_ARG();
226+
goto err_close_map;
224227
}
225228

226229
do_all = false;

0 commit comments

Comments
 (0)