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 D21DDC54EED for ; Mon, 30 Jan 2023 13:14:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 425146B0072; Mon, 30 Jan 2023 08:14:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3AE946B0073; Mon, 30 Jan 2023 08:14:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24E646B0074; Mon, 30 Jan 2023 08:14:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 1126E6B0072 for ; Mon, 30 Jan 2023 08:14:46 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id BA8B2160A3F for ; Mon, 30 Jan 2023 13:14:45 +0000 (UTC) X-FDA: 80411510130.12.9166B37 Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) by imf26.hostedemail.com (Postfix) with ESMTP id 837D914000D for ; Mon, 30 Jan 2023 13:14:43 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=EKmBfppr; spf=pass (imf26.hostedemail.com: domain of urezki@gmail.com designates 209.85.218.43 as permitted sender) smtp.mailfrom=urezki@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=1675084483; 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=U4ot21tosj26w8vKX0wsIMvfJKyxg9HzLw84nCxBKGg=; b=P6xV3tSL73n7bBsmnGcigv9/AJdiLWgRQhnSfWESOj2vlLbA23kbKRZIMTGpBiCoEOsO6K PCjpYAW46FKr3J1O20laxjdUttafVHCKZF3tkAekV5VHBZ7QCrP6eH6Kpk3csXck1kUksN pJ9WZNW8b1e3uWMlECxYdzpqTZ/cZXg= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=EKmBfppr; spf=pass (imf26.hostedemail.com: domain of urezki@gmail.com designates 209.85.218.43 as permitted sender) smtp.mailfrom=urezki@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675084483; a=rsa-sha256; cv=none; b=xp8YjW1SLEkj0iZxktufBXuxA71sAkF/4mwxwNSV8Ma9kFDC2VpWj8szcT3v4Li1TSqmcu GrP7eyutXbXaW472DPMrIvgIelSm/7sAFEFyFkbucrvQ4PV3DsSbLMT47xSbsbHYopL6YQ SaZThrB8CVMdYu06cJQ/Et7kOX20ypQ= Received: by mail-ej1-f43.google.com with SMTP id ud5so31603665ejc.4 for ; Mon, 30 Jan 2023 05:14:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:from:to:cc:subject:date:message-id:reply-to; bh=U4ot21tosj26w8vKX0wsIMvfJKyxg9HzLw84nCxBKGg=; b=EKmBfppr0esgDRuegyVD3n2yyRvfuYd5Nped5CfdnHnqqYSnKps3uax0Bot5kLLs5w gZChFUP2B3jyhtloZiwtXwwyxDITDbN2ai4gaRcIAVWghKsTQYuibmQQYAsQy+R9f/EM KE0PHzgVpgmdLCyQEJ9j0EJ+NEVc56/CTcMihGPaLni2qNAWMamgUpoKFTl4Wq1FwqlX Y4xGUr+6Dq9d/R7EQcrN6jAmjgxX68P+y9B2Xq62tJAMFyPv+bvOZ/eEDFA5AHY3rw9A MjUXv1e9laY19kcBUssY/KDjuPC1s+NwNWARHTS6SQqBccvCdUgLpwVKYayeyMt6Ea3P 0JiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=U4ot21tosj26w8vKX0wsIMvfJKyxg9HzLw84nCxBKGg=; b=3jgBNWBrKSuxNheF3Oee3kXONiEPsgsx5/qOcIzuVQvZOZqwWaL/H2R+pj+A2JNt12 mv6Lk8RcgQFRfEyMLL5+Ihxjh7m1Ts5VL/kbWrBa9QySnKBEYtQ9gGI1AArBz/Tdhehn inG41KNCJocAmZF2KSxnR2B22UFZmhdqAqjNdP42TFlyEdCMf3OzjAnExIgpKkmoWoOZ uty5cEnrevVRPhv2RA5hxO37dcq3HVNWSmcyHPCzMfpcWgzu22+a8WQHm5ZDEHg+Zoqy X2oG6mLyxorcyBcHR1losirUO2pxBx8LcxbEyEKyVphVW6LhLsJB2Q+jsEmyQNCvqh5b m4Ug== X-Gm-Message-State: AFqh2krSwp4U1lcnDwBMIci8FXppkGD4lnSJPC5nDw3qyvdHH3vBPru/ MRqR25zS4fjew0kIOr0tnAQ= X-Google-Smtp-Source: AMrXdXtn867dZ1QBqxXMOyN/Cfjr+lRRofBOeP0nBDbtTactpQJ4lkld7omxhBbX2ue8m/LACIr1vA== X-Received: by 2002:a17:907:a2cb:b0:871:dd2:4af0 with SMTP id re11-20020a170907a2cb00b008710dd24af0mr59836135ejc.26.1675084481825; Mon, 30 Jan 2023 05:14:41 -0800 (PST) Received: from pc636 (host-78-79-169-126.mobileonline.telia.com. [78.79.169.126]) by smtp.gmail.com with ESMTPSA id q5-20020a1709060f8500b00883ec4c63ddsm3552862ejj.146.2023.01.30.05.14.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Jan 2023 05:14:41 -0800 (PST) From: Uladzislau Rezki X-Google-Original-From: Uladzislau Rezki Date: Mon, 30 Jan 2023 14:14:37 +0100 To: Yafang Shao Cc: Alexei Starovoitov , Andrew Morton , urezki@gmail.com, Christoph Hellwig , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Vlastimil Babka , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Tejun Heo , dennis@kernel.org, Chris Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , linux-mm , bpf Subject: Re: [RFC PATCH bpf-next v2 00/11] mm, bpf: Add BPF into /proc/meminfo Message-ID: References: <20230112155326.26902-1-laoar.shao@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: x66gu6qfa9ze8iuo3iyz33mdmeg9se5r X-Rspamd-Queue-Id: 837D914000D X-HE-Tag: 1675084483-804620 X-HE-Meta: U2FsdGVkX1/Y9pVRQKmAaJbIrlguObZcGKo4f0Y+w5ZNgZGtt4TA7jQSSrMiBLGL/HayuO/vGNr9idpc6QgD7+tNJZjXn/tTQ/eP+/8XikBxlyl2F8GOyhMgEfqh8DfdwEhtxK6KGcp+1tbfqjW36rkCH1LUYtorfGy88My2aO/7qTuY62J+UTGDq/zSFjelhqH0mAG0oNSl08u65ohRq3IVztZQ3chVHf9VKw6QGdRfiGYWCe7JFHIODB999p5yaTlh4byGxIWUoJ4wsFbSORYfYTIkFAXn5LY+i2CR1xwzXDdQQygcGN7iYr8/cGhnczSZlfZNvHFjkKq1MB06v2L+HzUMCuUTimdN8xGaOlA0CtlDzKlsk8MdNvNeaI6vqwDa+rA0m7t0731Fu4ThBpBmAOUa9NOkJNd0+H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K7g76VvCLka6BekKPchPtiNcylRwam+e1GWFyL0SniJUQkXfBJdRMfChTRV7J 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 Sat, Jan 28, 2023 at 07:49:08PM +0800, Yafang Shao wrote: > On Thu, Jan 26, 2023 at 1:45 PM Alexei Starovoitov > wrote: > > > > On Tue, Jan 17, 2023 at 10:49 PM Yafang Shao wrote: > > > > > I just don't want to add many if-elses or switch-cases into > > > > > bpf_map_memory_footprint(), because I think it is a little ugly. > > > > > Introducing a new map ops could make it more clear. For example, > > > > > static unsigned long bpf_map_memory_footprint(const struct bpf_map *map) > > > > > { > > > > > unsigned long size; > > > > > > > > > > if (map->ops->map_mem_footprint) > > > > > return map->ops->map_mem_footprint(map); > > > > > > > > > > size = round_up(map->key_size + bpf_map_value_size(map), 8); > > > > > return round_up(map->max_entries * size, PAGE_SIZE); > > > > > } > > > > > > > > It is also ugly, because bpf_map_value_size() already has if-stmt. > > > > I prefer to keep all estimates in one place. > > > > There is no need to be 100% accurate. > > > > > > Per my investigation, it can be almost accurate with little effort. > > > Take the htab for example, > > > static unsigned long htab_mem_footprint(const struct bpf_map *map) > > > { > > > struct bpf_htab *htab = container_of(map, struct bpf_htab, map); > > > unsigned long size = 0; > > > > > > if (!htab_is_prealloc(htab)) { > > > size += htab_elements_size(htab); > > > } > > > size += kvsize(htab->elems); > > > size += percpu_size(htab->extra_elems); > > > size += kvsize(htab->buckets); > > > size += bpf_mem_alloc_size(&htab->pcpu_ma); > > > size += bpf_mem_alloc_size(&htab->ma); > > > if (htab->use_percpu_counter) > > > size += percpu_size(htab->pcount.counters); > > > size += percpu_size(htab->map_locked[i]) * HASHTAB_MAP_LOCK_COUNT; > > > size += kvsize(htab); > > > return size; > > > } > > > > Please don't. > > Above doesn't look maintainable. > > It is similar to htab_map_free(). These pointers are the pointers > which will be freed in map_free(). > We just need to keep map_mem_footprint() in sync with map_free(). It > won't be a problem for maintenance. > > > Look at kvsize(htab). Do you really care about hundred bytes? > > Just accept that there will be a small constant difference > > between what show_fdinfo reports and the real memory. > > The point is we don't have a clear idea what the margin is. > > > You cannot make it 100%. > > There is kfence that will allocate 4k though you asked kmalloc(8). > > > > We already have ksize()[1], which covers the kfence. > > [1]. https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/mm/slab_common.c#n1431 > > > > We just need to get the real memory size from the pointer instead of > > > calculating the size again. > > > For non-preallocated htab, it is a little trouble to get the element > > > size (not the unit_size), but it won't be a big deal. > > > > You'd have to convince mm folks that kvsize() is worth doing. > > I don't think it will be easy. > > > > As I mentioned above, we already have ksize(), so we only need to > introduce vsize(). Per my understanding, we can simply use > vm_struct->size to get the vmalloc size, see also the patch #5 in this > patchset[2]. > > Andrew, Uladzislau, Christoph, do you have any comments on this newly > introduced vsize()[2] ? > > [2]. https://lore.kernel.org/bpf/20230112155326.26902-6-laoar.shao@gmail.com/ > +/* Report full size of underlying allocation of a vmalloc'ed addr */ +static inline size_t vsize(const void *addr) +{ + struct vm_struct *area; + + if (!addr) + return 0; + + area = find_vm_area(addr); + if (unlikely(!area)) + return 0; + + return area->size; +} You can not access area after the lock is dropped. We do not have any ref counters for VA objects. Therefore it should be done like below: spin_lock(&vmap_area_lock); va = __find_vmap_area(addr, &vmap_area_root); if (va && va->vm) va_size = va->vm->size; spin_unlock(&vmap_area_lock); return va_size; -- Uladzislau Rezki