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 E3F53ECAAD2 for ; Mon, 29 Aug 2022 23:13:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7017F940008; Mon, 29 Aug 2022 19:13:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B159940007; Mon, 29 Aug 2022 19:13:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57996940008; Mon, 29 Aug 2022 19:13:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 47FC9940007 for ; Mon, 29 Aug 2022 19:13:24 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1BDBBAAF33 for ; Mon, 29 Aug 2022 23:13:24 +0000 (UTC) X-FDA: 79854183528.02.2D1589D Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) by imf29.hostedemail.com (Postfix) with ESMTP id B6A0312003D for ; Mon, 29 Aug 2022 23:13:23 +0000 (UTC) Received: by mail-ed1-f54.google.com with SMTP id a36so8208289edf.5 for ; Mon, 29 Aug 2022 16:13:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=njl0hWSY2+DW45dmIHbGPxljycPGJABcIq0tfVBCbMI=; b=Gnzy/mJwzthlINXOBQKY8FxcmdfyK7QcqhL4O1eg5+EhbXGhJGkLI1Tw5eD7JU4ljj yc2fbolKCzjd4Ozhybkt27M0vkJmYOyLqZEZMEF2zgwpkjg1pBVmOhInnFiO8M18/AVq JUL0iI/MwooUDt92Gmx3ap2lfLmTbgOOI8QIhdRjEBAWCePGvSFeSE0f1WMCqFS3ECE3 M4wi/p9Hi9rNsZU3k9P6B0Gab0DQT25T1GJ28PWGxhiXYi/wrWjj7nu+ozlJw2Twi9jw h611M9uEasV8WD6gKoD3eunzhRU+4OWLyUh90D6AGo6FItLb0PWpldVPa97MF9vSNoCC JyzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=njl0hWSY2+DW45dmIHbGPxljycPGJABcIq0tfVBCbMI=; b=U359IGJ/0P5fJVwM8/Yd3P4LMzUQYqMqygiYLpZNbnfVS4PPm5VVIph0I+/cqlVKJt DvGBnHo5xU1tj2CBFMCd/W1aXuJPJNOGr/6RfIO2DknLJ5gE2EyeA0mUGw4Wztxs874l 8lBb9LHcfO6f+QUHmbedj4gJ8m7t4DycRzFb2eLkaoFxIWr9rSU4xz3ed/9WMrncKlPo psd4+dL0z+SqgVzS6wl+m9ajlUgEvWd14M6g9TVx0/IR5HrHcq/5AS0BXyfSqopc+8C4 DrkX3SRC/BaNpVMQK/LyPxGY1DsL6JlX/Nc9Wg23xHNhuM7U8xi4NFFEGfQ9LJMeQJ74 MF9A== X-Gm-Message-State: ACgBeo1dNHeAkuNOo+4fwZMNKx0CangMD7pOWp7vg5nLQmpAZbrCXoAn 11Z7FlkKwbapSUewfaWwOwaJ3ojellCBSKDjsqk= X-Google-Smtp-Source: AA6agR4ldAz9I8cYfaVXak/8efOQwhZm9GnOVtPe+ix16DZOieVh02KAn3zJquhtnJO/viWxLjG7ZPDivFnGuIbQRUw= X-Received: by 2002:a05:6402:28cb:b0:43b:c6d7:ef92 with SMTP id ef11-20020a05640228cb00b0043bc6d7ef92mr18364924edb.333.1661814802401; Mon, 29 Aug 2022 16:13:22 -0700 (PDT) MIME-Version: 1.0 References: <20220826024430.84565-1-alexei.starovoitov@gmail.com> <20220826024430.84565-2-alexei.starovoitov@gmail.com> <20220829223946.vfu4hi64ybitvt27@kafai-mbp.dhcp.thefacebook.com> In-Reply-To: From: Alexei Starovoitov Date: Mon, 29 Aug 2022 16:13:11 -0700 Message-ID: Subject: Re: [PATCH v4 bpf-next 01/15] bpf: Introduce any context BPF specific memory allocator. To: Kumar Kartikeya Dwivedi Cc: Martin KaFai Lau , "David S. Miller" , Daniel Borkmann , Andrii Nakryiko , Tejun Heo , Delyan Kratunov , linux-mm , bpf , Kernel Team Content-Type: text/plain; charset="UTF-8" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661814803; a=rsa-sha256; cv=none; b=dB+XQCBvYEGGV025fwFEGzS+MQ/R94tgYljud5uWZBjNdjgRkEU7WBRPbKK+H6ImVIskN3 K581Otytd6KoC8zV4WnW1JunsgCIJtRYte6/rkg/wG6ePVZ20tRM5B77y9ZyM3MLqNwHAF qwJG7A1kQRYhapKn36RPHc4ekPGC+oc= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="Gnzy/mJw"; spf=pass (imf29.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.208.54 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661814803; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=njl0hWSY2+DW45dmIHbGPxljycPGJABcIq0tfVBCbMI=; b=JV+rrW/21MDpXfffZWN8Y5qx7uMjjo0H/Nsstuytf9BYGVrhdLhq4zpK1mQsv7YcgBtdk7 rzWBMPW7pIcAeiDX+yLOARIYJwvrVLw3DUDtOQPDHV78c03V3QUzzg6YAYvxmOYjLZDYWi hehqFqy2IP+p0opKcqih0kNVnAmlTGo= Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="Gnzy/mJw"; spf=pass (imf29.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.208.54 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam05 X-Stat-Signature: yksoexx8z7frfh3iaas7f4yj1uzh888f X-Rspamd-Queue-Id: B6A0312003D X-Rspam-User: X-HE-Tag: 1661814803-460774 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 Mon, Aug 29, 2022 at 4:00 PM Kumar Kartikeya Dwivedi wrote: > > On Tue, 30 Aug 2022 at 00:43, Alexei Starovoitov > wrote: > > > > On Mon, Aug 29, 2022 at 3:39 PM Martin KaFai Lau wrote: > > > > > > On Thu, Aug 25, 2022 at 07:44:16PM -0700, Alexei Starovoitov wrote: > > > > +/* Mostly runs from irq_work except __init phase. */ > > > > +static void alloc_bulk(struct bpf_mem_cache *c, int cnt, int node) > > > > +{ > > > > + struct mem_cgroup *memcg = NULL, *old_memcg; > > > > + unsigned long flags; > > > > + void *obj; > > > > + int i; > > > > + > > > > + memcg = get_memcg(c); > > > > + old_memcg = set_active_memcg(memcg); > > > > + for (i = 0; i < cnt; i++) { > > > > + obj = __alloc(c, node); > > > > + if (!obj) > > > > + break; > > > > + if (IS_ENABLED(CONFIG_PREEMPT_RT)) > > > > + /* In RT irq_work runs in per-cpu kthread, so disable > > > > + * interrupts to avoid preemption and interrupts and > > > > + * reduce the chance of bpf prog executing on this cpu > > > > + * when active counter is busy. > > > > + */ > > > > + local_irq_save(flags); > > > > + if (local_inc_return(&c->active) == 1) { > > > Is it because it is always '== 1' here so that there is > > > no need to free the obj when it is '!= 1' ? > > > > Great catch. It's a bug indeed. > > Is it a bug? It seems it will always be true for alloc_bulk. IIUC it > is only needed to prevent NMI's unit_alloc, unit_free touching > free_llist, so that NMI llist_adds atomically to free_llist_nmi > instead. Since this runs from irq_work, we run exclusive to other > unit_free, otherwise the __llist_add wouldn't be safe either. > unit_free already does local_irq_save. Correct. It cannot happen in practice, but the code as written will look 'buggy' to any static analyzer. So we have to 'fix' it. I'm thinking to do: WARN_ON_ONCE(local_inc_return(&c->active) != 1); and the same in free_bulk.