From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A499C433F5 for ; Thu, 21 Apr 2022 17:09:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 664F46B0072; Thu, 21 Apr 2022 13:09:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6157C6B0073; Thu, 21 Apr 2022 13:09:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B59B6B0074; Thu, 21 Apr 2022 13:09:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id 3CC3B6B0072 for ; Thu, 21 Apr 2022 13:09:49 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E8F0E23384 for ; Thu, 21 Apr 2022 17:09:48 +0000 (UTC) X-FDA: 79381523256.17.B4175C8 Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) by imf19.hostedemail.com (Postfix) with ESMTP id 485CF1A002F for ; Thu, 21 Apr 2022 17:09:46 +0000 (UTC) Received: by mail-lj1-f173.google.com with SMTP id q22so6534205ljh.10 for ; Thu, 21 Apr 2022 10:09:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0IqQ6+qBUviTbm525jpNP9rFFjEP17/Uq4yu2pgmklg=; b=h0AVqXEZDnoKpki6s6mvGXaq/qVhk9PLSMD+KC2kB/6zAm9WZ0/TOIR8jL431bIerZ 7RJOezuZFJkSKXrVWiCiNh2VtITqKNiaXmqD1p7DnmDoWTRufGVc5rcdwItl1OU/NUUm /obosCnwvUIpl5bIMmP+YPXBXCAjBYScWaF/U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0IqQ6+qBUviTbm525jpNP9rFFjEP17/Uq4yu2pgmklg=; b=OoqOg8hYrcmCRXcPqbOrMUlo+awHhdhqL8q5Th9mYVPzqu9FC6Obk7Evzl0u5nXCZc SoMcCcSG3vx2e6abIYwiYTfsn7F7NEV47gGXQL4tiB7MN00O28+LX4L/b269IfhtOKKz FQObz70k4qldUXcpnIf9KtQ9It1s6uvfKfSJHE7PRBkdh1vS67AqpMuoxyOCoDGcrvIp WeANBbqcaExfdOdRku1wzdQgwDnZtWsXObu7jkpUCvqSI2qULcNy+T7o1FJjtkEip4OW xIOJGq5AhQIz+7X5dT05eVkq4brZmmLJYpVEMqmRqjDQK+Qz8sesbqYtmI3VWIkD8ts0 FORw== X-Gm-Message-State: AOAM531JpfPRQz33GX3Z2PlpJsdx+IvLi9eKPsJ+6mBzPhvPdKR912ya 6Rvo/POru+N0hqmg96wLsAuT4pbjIuXVlGjC1Ck= X-Google-Smtp-Source: ABdhPJxWvAzWEfMkIpFCbYE84Le+vIUGPa02k7UYGhX7lRAU4htK7GBs96L5v8pZzCKdOOaOWJ9wqg== X-Received: by 2002:a2e:880b:0:b0:24d:bb45:9b5b with SMTP id x11-20020a2e880b000000b0024dbb459b5bmr394408ljh.436.1650560986516; Thu, 21 Apr 2022 10:09:46 -0700 (PDT) Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com. [209.85.208.182]) by smtp.gmail.com with ESMTPSA id d11-20020a19384b000000b0046bbd144dfesm2231352lfj.125.2022.04.21.10.09.44 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Apr 2022 10:09:44 -0700 (PDT) Received: by mail-lj1-f182.google.com with SMTP id q14so6540069ljc.12 for ; Thu, 21 Apr 2022 10:09:43 -0700 (PDT) X-Received: by 2002:a2e:b8d2:0:b0:24e:e19c:5375 with SMTP id s18-20020a2eb8d2000000b0024ee19c5375mr413269ljp.176.1650560983746; Thu, 21 Apr 2022 10:09:43 -0700 (PDT) MIME-Version: 1.0 References: <20220421072212.608884-1-song@kernel.org> In-Reply-To: <20220421072212.608884-1-song@kernel.org> From: Linus Torvalds Date: Thu, 21 Apr 2022 10:09:27 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH bpf] bpf: invalidate unused part of bpf_prog_pack To: Song Liu Cc: bpf , Linux-MM , Linux Kernel Mailing List , Alexei Starovoitov , Daniel Borkmann , Kernel Team , Andrew Morton , "Edgecombe, Rick P" , Christoph Hellwig , Andrii Nakryiko Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 485CF1A002F X-Stat-Signature: gfrgpqbj39o47ieppygg1nfkwboq4bpa Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=google header.b=h0AVqXEZ; spf=pass (imf19.hostedemail.com: domain of torvalds@linuxfoundation.org designates 209.85.208.173 as permitted sender) smtp.mailfrom=torvalds@linuxfoundation.org; dmarc=none X-HE-Tag: 1650560986-817512 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Apr 21, 2022 at 12:27 AM Song Liu wrote: > > --- a/arch/x86/net/bpf_jit_comp.c > +++ b/arch/x86/net/bpf_jit_comp.c > @@ -228,6 +228,28 @@ static void jit_fill_hole(void *area, unsigned int size) > memset(area, 0xcc, size); > } > > +#define INVALID_BUF_SIZE PAGE_SIZE > +static char invalid_insn_buf[INVALID_BUF_SIZE]; > + > +static int __init bpf_init_invalid_insn_buf(void) > +{ > + jit_fill_hole(invalid_insn_buf, INVALID_BUF_SIZE); > + return 0; > +} > +pure_initcall(bpf_init_invalid_insn_buf); > + > +void bpf_arch_invalidate_text(void *dst, size_t len) > +{ > + size_t i = 0; > + > + while (i < len) { > + size_t s = min_t(size_t, len - i, INVALID_BUF_SIZE); > + > + bpf_arch_text_copy(dst + i, invalid_insn_buf, s); > + i += s; > + } > +} Why do we need this new infrastructure? Why bpf_arch_invalidate_text()? Why not jit_fill_hole() unconditionally? It seems a bit pointless to have page buffer for containing this data, when we already have a (trivial) function to fill an area with invalid instructions. On x86, it's literally just "memset(0xcc)" (ie all 'int3' instructions). And on most RISC architectures, it would be some variation of "memset32(TRAP_INSN)". And all bpf targets should already have that nicely as that jit_fill_hole() function, no? The pack-allocator bpf code already *does* that, and is already passed that function. But it's just that it does it too late. Instead of doing it when allocating a new pack, it does it in the sub-allocator. Afaik the code in bpf/core.c already has all the information it needs, and already has that jit_fill_hole() function pointer, but is applying it at the wrong point. So I think the fix should be to just pass in that 'bpf_fill_ill_insns' function pointer all the way to alloc_new_pack(), instead of using it in bpf_jit_binary_alloc(). NOTE! Once again, I'm not actually all that familiar with the code. Maybe I'm missing something. Linus