From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Song Liu <song@kernel.org>, Alexei Starovoitov <ast@kernel.org>,
Sasha Levin <sashal@kernel.org>,
davem@davemloft.net, yoshfuji@linux-ipv6.org, dsahern@kernel.org,
daniel@iogearbox.net, andrii@kernel.org, tglx@linutronix.de,
mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com,
x86@kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org
Subject: [PATCH AUTOSEL 5.19 66/73] bpf: use bpf_prog_pack for bpf_dispatcher
Date: Sun, 9 Oct 2022 18:14:44 -0400 [thread overview]
Message-ID: <20221009221453.1216158-66-sashal@kernel.org> (raw)
In-Reply-To: <20221009221453.1216158-1-sashal@kernel.org>
From: Song Liu <song@kernel.org>
[ Upstream commit 19c02415da2345d0dda2b5c4495bc17cc14b18b5 ]
Allocate bpf_dispatcher with bpf_prog_pack_alloc so that bpf_dispatcher
can share pages with bpf programs.
arch_prepare_bpf_dispatcher() is updated to provide a RW buffer as working
area for arch code to write to.
This also fixes CPA W^X warnning like:
CPA refuse W^X violation: 8000000000000163 -> 0000000000000163 range: ...
Signed-off-by: Song Liu <song@kernel.org>
Link: https://lore.kernel.org/r/20220926184739.3512547-2-song@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
arch/x86/net/bpf_jit_comp.c | 16 ++++++++--------
include/linux/bpf.h | 3 ++-
include/linux/filter.h | 5 +++++
kernel/bpf/core.c | 9 +++++++--
kernel/bpf/dispatcher.c | 27 +++++++++++++++++++++------
5 files changed, 43 insertions(+), 17 deletions(-)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 41d170653e8d..fc4d899f10f6 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -2216,7 +2216,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
return ret;
}
-static int emit_bpf_dispatcher(u8 **pprog, int a, int b, s64 *progs)
+static int emit_bpf_dispatcher(u8 **pprog, int a, int b, s64 *progs, u8 *image, u8 *buf)
{
u8 *jg_reloc, *prog = *pprog;
int pivot, err, jg_bytes = 1;
@@ -2232,12 +2232,12 @@ static int emit_bpf_dispatcher(u8 **pprog, int a, int b, s64 *progs)
EMIT2_off32(0x81, add_1reg(0xF8, BPF_REG_3),
progs[a]);
err = emit_cond_near_jump(&prog, /* je func */
- (void *)progs[a], prog,
+ (void *)progs[a], image + (prog - buf),
X86_JE);
if (err)
return err;
- emit_indirect_jump(&prog, 2 /* rdx */, prog);
+ emit_indirect_jump(&prog, 2 /* rdx */, image + (prog - buf));
*pprog = prog;
return 0;
@@ -2262,7 +2262,7 @@ static int emit_bpf_dispatcher(u8 **pprog, int a, int b, s64 *progs)
jg_reloc = prog;
err = emit_bpf_dispatcher(&prog, a, a + pivot, /* emit lower_part */
- progs);
+ progs, image, buf);
if (err)
return err;
@@ -2276,7 +2276,7 @@ static int emit_bpf_dispatcher(u8 **pprog, int a, int b, s64 *progs)
emit_code(jg_reloc - jg_bytes, jg_offset, jg_bytes);
err = emit_bpf_dispatcher(&prog, a + pivot + 1, /* emit upper_part */
- b, progs);
+ b, progs, image, buf);
if (err)
return err;
@@ -2296,12 +2296,12 @@ static int cmp_ips(const void *a, const void *b)
return 0;
}
-int arch_prepare_bpf_dispatcher(void *image, s64 *funcs, int num_funcs)
+int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs)
{
- u8 *prog = image;
+ u8 *prog = buf;
sort(funcs, num_funcs, sizeof(funcs[0]), cmp_ips, NULL);
- return emit_bpf_dispatcher(&prog, 0, num_funcs - 1, funcs);
+ return emit_bpf_dispatcher(&prog, 0, num_funcs - 1, funcs, image, buf);
}
struct x64_jit_data {
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index ed352c00330c..df9cd469947a 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -869,6 +869,7 @@ struct bpf_dispatcher {
struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
int num_progs;
void *image;
+ void *rw_image;
u32 image_off;
struct bpf_ksym ksym;
};
@@ -888,7 +889,7 @@ int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampolin
struct bpf_trampoline *bpf_trampoline_get(u64 key,
struct bpf_attach_target_info *tgt_info);
void bpf_trampoline_put(struct bpf_trampoline *tr);
-int arch_prepare_bpf_dispatcher(void *image, s64 *funcs, int num_funcs);
+int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs);
#define BPF_DISPATCHER_INIT(_name) { \
.mutex = __MUTEX_INITIALIZER(_name.mutex), \
.func = &_name##_func, \
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 8fd2e2f58eeb..e11335c70982 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1052,6 +1052,8 @@ extern long bpf_jit_limit_max;
typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
+void bpf_jit_fill_hole_with_zero(void *area, unsigned int size);
+
struct bpf_binary_header *
bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
unsigned int alignment,
@@ -1064,6 +1066,9 @@ void bpf_jit_free(struct bpf_prog *fp);
struct bpf_binary_header *
bpf_jit_binary_pack_hdr(const struct bpf_prog *fp);
+void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns);
+void bpf_prog_pack_free(struct bpf_binary_header *hdr);
+
static inline bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
{
return list_empty(&fp->aux->ksym.lnode) ||
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index cf44ff50b1f2..be736aa97927 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -822,6 +822,11 @@ struct bpf_prog_pack {
unsigned long bitmap[];
};
+void bpf_jit_fill_hole_with_zero(void *area, unsigned int size)
+{
+ memset(area, 0, size);
+}
+
#define BPF_PROG_SIZE_TO_NBITS(size) (round_up(size, BPF_PROG_CHUNK_SIZE) / BPF_PROG_CHUNK_SIZE)
static size_t bpf_prog_pack_size = -1;
@@ -892,7 +897,7 @@ static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_ins
return pack;
}
-static void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns)
+void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns)
{
unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size);
struct bpf_prog_pack *pack;
@@ -936,7 +941,7 @@ static void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insn
return ptr;
}
-static void bpf_prog_pack_free(struct bpf_binary_header *hdr)
+void bpf_prog_pack_free(struct bpf_binary_header *hdr)
{
struct bpf_prog_pack *pack = NULL, *tmp;
unsigned int nbits;
diff --git a/kernel/bpf/dispatcher.c b/kernel/bpf/dispatcher.c
index 2444bd15cc2d..fa64b80b8bca 100644
--- a/kernel/bpf/dispatcher.c
+++ b/kernel/bpf/dispatcher.c
@@ -85,12 +85,12 @@ static bool bpf_dispatcher_remove_prog(struct bpf_dispatcher *d,
return false;
}
-int __weak arch_prepare_bpf_dispatcher(void *image, s64 *funcs, int num_funcs)
+int __weak arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs)
{
return -ENOTSUPP;
}
-static int bpf_dispatcher_prepare(struct bpf_dispatcher *d, void *image)
+static int bpf_dispatcher_prepare(struct bpf_dispatcher *d, void *image, void *buf)
{
s64 ips[BPF_DISPATCHER_MAX] = {}, *ipsp = &ips[0];
int i;
@@ -99,12 +99,12 @@ static int bpf_dispatcher_prepare(struct bpf_dispatcher *d, void *image)
if (d->progs[i].prog)
*ipsp++ = (s64)(uintptr_t)d->progs[i].prog->bpf_func;
}
- return arch_prepare_bpf_dispatcher(image, &ips[0], d->num_progs);
+ return arch_prepare_bpf_dispatcher(image, buf, &ips[0], d->num_progs);
}
static void bpf_dispatcher_update(struct bpf_dispatcher *d, int prev_num_progs)
{
- void *old, *new;
+ void *old, *new, *tmp;
u32 noff;
int err;
@@ -117,8 +117,14 @@ static void bpf_dispatcher_update(struct bpf_dispatcher *d, int prev_num_progs)
}
new = d->num_progs ? d->image + noff : NULL;
+ tmp = d->num_progs ? d->rw_image + noff : NULL;
if (new) {
- if (bpf_dispatcher_prepare(d, new))
+ /* Prepare the dispatcher in d->rw_image. Then use
+ * bpf_arch_text_copy to update d->image, which is RO+X.
+ */
+ if (bpf_dispatcher_prepare(d, new, tmp))
+ return;
+ if (IS_ERR(bpf_arch_text_copy(new, tmp, PAGE_SIZE / 2)))
return;
}
@@ -140,9 +146,18 @@ void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
mutex_lock(&d->mutex);
if (!d->image) {
- d->image = bpf_jit_alloc_exec_page();
+ d->image = bpf_prog_pack_alloc(PAGE_SIZE, bpf_jit_fill_hole_with_zero);
if (!d->image)
goto out;
+ d->rw_image = bpf_jit_alloc_exec(PAGE_SIZE);
+ if (!d->rw_image) {
+ u32 size = PAGE_SIZE;
+
+ bpf_arch_text_copy(d->image, &size, sizeof(size));
+ bpf_prog_pack_free((struct bpf_binary_header *)d->image);
+ d->image = NULL;
+ goto out;
+ }
bpf_image_ksym_add(d->image, &d->ksym);
}
--
2.35.1
next prev parent reply other threads:[~2022-10-09 22:30 UTC|newest]
Thread overview: 66+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20221009221453.1216158-1-sashal@kernel.org>
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 02/73] wifi: rtw88: phy: fix warning of possible buffer overflow Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 03/73] wifi: ath10k: Set tx credit to one for WCN3990 snoc based devices Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 04/73] wifi: brcmfmac: fix invalid address access when enabling SCAN log level Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 06/73] ice: set tx_tstamps when creating new Tx rings via ethtool Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 07/73] net: ethernet: ti: davinci_mdio: Add workaround for errata i2329 Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 08/73] openvswitch: Fix double reporting of drops in dropwatch Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 09/73] openvswitch: Fix overreporting " Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 10/73] net: dsa: all DSA masters must be down when changing the tagging protocol Sasha Levin
2022-10-10 11:53 ` Vladimir Oltean
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 11/73] net: mscc: ocelot: adjust forwarding domain for CPU ports in a LAG Sasha Levin
2022-10-10 11:58 ` Vladimir Oltean
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 12/73] tcp: annotate data-race around tcp_md5sig_pool_populated Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 13/73] micrel: ksz8851: fixes struct pointer issue Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 14/73] genetlink: hold read cb_lock during iteration of genl_fam_idr in genl_bind() Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 15/73] net: dsa: mv88e6xxx: Allow external SMI if serial Sasha Levin
2022-10-10 11:53 ` Vladimir Oltean
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 17/73] wifi: ath9k: avoid uninit memory read in ath9k_htc_rx_msg() Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 20/73] net: axienet: Switch to 64-bit RX/TX statistics Sasha Levin
2022-10-09 22:13 ` [PATCH AUTOSEL 5.19 21/73] net-next: Fix IP_UNICAST_IF option behavior for connected sockets Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 22/73] xfrm: Update ipcomp_scratches with NULL when freed Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 23/73] wifi: ath11k: Register shutdown handler for WCN6750 Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 24/73] rtw89: ser: leave lps with mutex Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 25/73] net: broadcom: Fix return type for implementation of Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 26/73] net: xscale: Fix return type for implementation of ndo_start_xmit Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 27/73] net: sunplus: " Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 28/73] net: lantiq_etop: " Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 29/73] netlink: Bounds-check struct nlmsgerr creation Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 30/73] net: ftmac100: fix endianness-related issues from 'sparse' Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 31/73] iavf: Fix race between iavf_close and iavf_reset_task Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 32/73] wifi: brcmfmac: fix use-after-free bug in brcmf_netdev_start_xmit() Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 33/73] net: sparx5: fix function return type to match actual type Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 34/73] net: mscc: ocelot: report FIFO drop counters through stats->rx_dropped Sasha Levin
2022-10-10 11:58 ` Vladimir Oltean
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 37/73] wifi: ath11k: mhi: fix potential memory leak in ath11k_mhi_register() Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 38/73] wifi: mt76: mt7921: reset msta->airtime_ac while clearing up hw value Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 39/73] wifi: rtw89: free unused skb to prevent memory leak Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 40/73] wifi: rtw89: fix rx filter after scan Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 41/73] Bluetooth: L2CAP: initialize delayed works at l2cap_chan_create() Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 42/73] net: ax88796c: Fix return type of ax88796c_start_xmit Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 43/73] net: davicom: Fix return type of dm9000_start_xmit Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 44/73] net: ethernet: ti: davinci_emac: Fix return type of emac_dev_xmit Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 45/73] net: ethernet: litex: Fix return type of liteeth_start_xmit Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 46/73] net: korina: Fix return type of korina_send_packet Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 47/73] net: wwan: iosm: Fix return type of ipc_wwan_link_transmit Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 48/73] net: wwan: t7xx: Fix return type of t7xx_ccmni_start_xmit Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 49/73] net: sfp: re-implement soft state polling setup Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 50/73] net: sfp: move quirk handling into sfp.c Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 51/73] net: sfp: move Alcatel Lucent 3FE46541AA fixup Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 52/73] net/sched: taprio: taprio_dump and taprio_change are protected by rtnl_mutex Sasha Levin
2022-10-10 11:58 ` Vladimir Oltean
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 53/73] Bluetooth: hci_sysfs: Fix attempting to call device_add multiple times Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 54/73] wifi: ath10k: reset pointer after memory free to avoid potential use-after-free Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 55/73] bnxt_en: replace reset with config timestamps Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 57/73] can: bcm: check the result of can_send() in bcm_can_tx() Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 58/73] wifi: rt2x00: don't run Rt5592 IQ calibration on MT7620 Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 59/73] wifi: rt2x00: set correct TX_SW_CFG1 MAC register for MT7620 Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 60/73] wifi: rt2x00: set VGC gain for both chains of MT7620 Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 61/73] wifi: rt2x00: set SoC wmac clock register Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 62/73] wifi: rt2x00: correctly set BBP register 86 for MT7620 Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 64/73] net: If sock is dead don't access sock's sk_wq in sk_stream_wait_memory Sasha Levin
2022-10-09 22:14 ` Sasha Levin [this message]
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 67/73] Bluetooth: L2CAP: Fix user-after-free Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 68/73] net: sched: cls_u32: Avoid memcpy() false-positive warning Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 71/73] net: sparx5: Fix return type of sparx5_port_xmit_impl Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 72/73] net: lan966x: Fix return type of lan966x_port_xmit Sasha Levin
2022-10-09 22:14 ` [PATCH AUTOSEL 5.19 73/73] r8152: Rate limit overflow messages Sasha Levin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221009221453.1216158-66-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bp@alien8.de \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dave.hansen@linux.intel.com \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=song@kernel.org \
--cc=stable@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
--cc=yoshfuji@linux-ipv6.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).