svn commit: r317072 - in head/sys: amd64/amd64 i386/i386 net
Jung-uk Kim
jkim at FreeBSD.org
Mon Apr 17 22:02:11 UTC 2017
Author: jkim
Date: Mon Apr 17 22:02:09 2017
New Revision: 317072
URL: https://svnweb.freebsd.org/changeset/base/317072
Log:
Use kmem_malloc() instead of malloc(9) for the native amd64 filter.
r316767 broke the BPF JIT compiler for amd64 because malloc()'d space is no
longer executable.
Discussed with: kib, alc
Modified:
head/sys/amd64/amd64/bpf_jit_machdep.c
head/sys/i386/i386/bpf_jit_machdep.c
head/sys/net/bpf_jitter.c
head/sys/net/bpf_jitter.h
Modified: head/sys/amd64/amd64/bpf_jit_machdep.c
==============================================================================
--- head/sys/amd64/amd64/bpf_jit_machdep.c Mon Apr 17 21:57:23 2017 (r317071)
+++ head/sys/amd64/amd64/bpf_jit_machdep.c Mon Apr 17 22:02:09 2017 (r317072)
@@ -1,6 +1,6 @@
/*-
* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
- * Copyright (C) 2005-2016 Jung-uk Kim <jkim at FreeBSD.org>
+ * Copyright (C) 2005-2017 Jung-uk Kim <jkim at FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -37,10 +37,14 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
-#include <sys/socket.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
+#include <sys/socket.h>
+
#include <net/if.h>
+#include <vm/vm.h>
+#include <vm/vm_extern.h>
+#include <vm/vm_kern.h>
#else
#include <stdlib.h>
#include <string.h>
@@ -599,7 +603,11 @@ bpf_jit_compile(struct bpf_insn *prog, u
*size = stream.cur_ip;
#ifdef _KERNEL
- stream.ibuf = malloc(*size, M_BPFJIT, M_NOWAIT);
+ /*
+ * We cannot use malloc(9) because DMAP is mapped as NX.
+ */
+ stream.ibuf = (void *)kmem_malloc(kernel_arena, *size,
+ M_NOWAIT);
if (stream.ibuf == NULL)
break;
#else
@@ -648,3 +656,14 @@ bpf_jit_compile(struct bpf_insn *prog, u
return ((bpf_filter_func)(void *)stream.ibuf);
}
+
+void
+bpf_jit_free(void *func, size_t size)
+{
+
+#ifdef _KERNEL
+ kmem_free(kernel_arena, (vm_offset_t)func, size);
+#else
+ munmap(func, size);
+#endif
+}
Modified: head/sys/i386/i386/bpf_jit_machdep.c
==============================================================================
--- head/sys/i386/i386/bpf_jit_machdep.c Mon Apr 17 21:57:23 2017 (r317071)
+++ head/sys/i386/i386/bpf_jit_machdep.c Mon Apr 17 22:02:09 2017 (r317072)
@@ -1,6 +1,6 @@
/*-
* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
- * Copyright (C) 2005-2016 Jung-uk Kim <jkim at FreeBSD.org>
+ * Copyright (C) 2005-2017 Jung-uk Kim <jkim at FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -37,9 +37,10 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
-#include <sys/socket.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
+#include <sys/socket.h>
+
#include <net/if.h>
#else
#include <stdlib.h>
@@ -678,3 +679,14 @@ bpf_jit_compile(struct bpf_insn *prog, u
return ((bpf_filter_func)(void *)stream.ibuf);
}
+
+void
+bpf_jit_free(void *func, size_t size)
+{
+
+#ifdef _KERNEL
+ free(func, M_BPFJIT);
+#else
+ munmap(func, size);
+#endif
+}
Modified: head/sys/net/bpf_jitter.c
==============================================================================
--- head/sys/net/bpf_jitter.c Mon Apr 17 21:57:23 2017 (r317071)
+++ head/sys/net/bpf_jitter.c Mon Apr 17 22:02:09 2017 (r317072)
@@ -1,6 +1,6 @@
/*-
* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
- * Copyright (C) 2005-2009 Jung-uk Kim <jkim at FreeBSD.org>
+ * Copyright (C) 2005-2017 Jung-uk Kim <jkim at FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -99,13 +99,11 @@ void
bpf_destroy_jit_filter(bpf_jit_filter *filter)
{
-#ifdef _KERNEL
if (filter->func != bpf_jit_accept_all)
- free(filter->func, M_BPFJIT);
+ bpf_jit_free(filter->func, filter->size);
+#ifdef _KERNEL
free(filter, M_BPFJIT);
#else
- if (filter->func != bpf_jit_accept_all)
- munmap(filter->func, filter->size);
free(filter);
#endif
}
Modified: head/sys/net/bpf_jitter.h
==============================================================================
--- head/sys/net/bpf_jitter.h Mon Apr 17 21:57:23 2017 (r317071)
+++ head/sys/net/bpf_jitter.h Mon Apr 17 22:02:09 2017 (r317072)
@@ -86,5 +86,6 @@ void bpf_destroy_jit_filter(bpf_jit_fil
struct bpf_insn;
bpf_filter_func bpf_jit_compile(struct bpf_insn *, u_int, size_t *);
+void bpf_jit_free(void *, size_t);
#endif /* _NET_BPF_JITTER_H_ */
More information about the svn-src-all
mailing list