Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 17 Apr 2017 22:02:09 +0000 (UTC)
From:      Jung-uk Kim <jkim@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r317072 - in head/sys: amd64/amd64 i386/i386 net
Message-ID:  <201704172202.v3HM29oU034516@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
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@FreeBSD.org>
+ * Copyright (C) 2005-2017 Jung-uk Kim <jkim@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@FreeBSD.org>
+ * Copyright (C) 2005-2017 Jung-uk Kim <jkim@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@FreeBSD.org>
+ * Copyright (C) 2005-2017 Jung-uk Kim <jkim@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_ */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201704172202.v3HM29oU034516>