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 3CD7AC433F5 for ; Wed, 5 Jan 2022 17:08:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7272C6B0071; Wed, 5 Jan 2022 12:08:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D6BA6B0073; Wed, 5 Jan 2022 12:08:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 576A96B0074; Wed, 5 Jan 2022 12:08:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0131.hostedemail.com [216.40.44.131]) by kanga.kvack.org (Postfix) with ESMTP id 485D76B0071 for ; Wed, 5 Jan 2022 12:08:48 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id ECF2F1815BBC6 for ; Wed, 5 Jan 2022 17:08:47 +0000 (UTC) X-FDA: 78996867894.22.602B6ED Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by imf27.hostedemail.com (Postfix) with ESMTP id 1662440012 for ; Wed, 5 Jan 2022 17:08:45 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 1593721101; Wed, 5 Jan 2022 17:08:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1641402526; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=29fjnm569NOx8opF3VJURmxxXs5tPBd+njPEliFY6vg=; b=Yp5zyhN55xPlgtXUSDEWS8ZqkuvunbCQnFj6iBc4VZWle07lY7jMZ58LznZAhvTwa0XfAA eczLEe1zySZcSFbsqJ7xCX90XIO2UZ9CoSqooqQjqOhyg4dG6vZaMfnzkI5Z01G5SfaYAJ x13HG044++hLIyXJfgo1rntzMtWy1jQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1641402526; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=29fjnm569NOx8opF3VJURmxxXs5tPBd+njPEliFY6vg=; b=eDpp4vu0iWT2XSATxygwktscqoGN9pcOwnFgc3cM01p341RTh/Txx3p8CeRx9itQyCVz/G IAJopIDSMqg+6eCQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id D0F9913BF9; Wed, 5 Jan 2022 17:08:45 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id iGY8Mp3Q1WEdHQAAMHmgww (envelope-from ); Wed, 05 Jan 2022 17:08:45 +0000 Message-ID: <56c7a92b-f830-93dd-3fd0-4b6f1eb723ef@suse.cz> Date: Wed, 5 Jan 2022 18:08:45 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.1 Content-Language: en-US To: Roman Gushchin Cc: Matthew Wilcox , Christoph Lameter , David Rientjes , Joonsoo Kim , Pekka Enberg , linux-mm@kvack.org, Andrew Morton , Johannes Weiner , Hyeonggon Yoo <42.hyeyoo@gmail.com>, patches@lists.linux.dev, Michal Hocko , Vladimir Davydov , cgroups@vger.kernel.org References: <20220104001046.12263-1-vbabka@suse.cz> <20220104001046.12263-24-vbabka@suse.cz> From: Vlastimil Babka Subject: Re: [PATCH v4 23/32] mm/memcg: Convert slab objcgs from struct page to struct slab In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 1662440012 X-Stat-Signature: xsqba8m5gc9mq5kmq1w1jx8yeig6knc4 Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=Yp5zyhN5; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=eDpp4vu0; dmarc=none; spf=pass (imf27.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.28 as permitted sender) smtp.mailfrom=vbabka@suse.cz X-HE-Tag: 1641402525-916605 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 1/5/22 03:41, Roman Gushchin wrote: > On Tue, Jan 04, 2022 at 01:10:37AM +0100, Vlastimil Babka wrote: >> page->memcg_data is used with MEMCG_DATA_OBJCGS flag only for slab pages >> so convert all the related infrastructure to struct slab. Also use >> struct folio instead of struct page when resolving object pointers. >> >> This is not just mechanistic changing of types and names. Now in >> mem_cgroup_from_obj() we use folio_test_slab() to decide if we interpret >> the folio as a real slab instead of a large kmalloc, instead of relying >> on MEMCG_DATA_OBJCGS bit that used to be checked in page_objcgs_check(). >> Similarly in memcg_slab_free_hook() where we can encounter >> kmalloc_large() pages (here the folio slab flag check is implied by >> virt_to_slab()). As a result, page_objcgs_check() can be dropped instead >> of converted. >> >> To avoid include cycles, move the inline definition of slab_objcgs() >> from memcontrol.h to mm/slab.h. >> >> Signed-off-by: Vlastimil Babka >> Cc: Johannes Weiner >> Cc: Michal Hocko >> Cc: Vladimir Davydov >> Cc: >> /* >> * Slab objects are accounted individually, not per-page. >> * Memcg membership data for each individual object is saved in >> * the page->obj_cgroups. > ^^^^^^^^^^^^^^^^^ > slab->memcg_data Good catch, fixed. >> */ >> - if (page_objcgs_check(page)) { >> - struct obj_cgroup *objcg; >> + if (folio_test_slab(folio)) { >> + struct obj_cgroup **objcgs; >> + struct slab *slab; >> unsigned int off; >> >> - off = obj_to_index(page->slab_cache, page_slab(page), p); >> - objcg = page_objcgs(page)[off]; >> - if (objcg) >> - return obj_cgroup_memcg(objcg); >> + slab = folio_slab(folio); >> + objcgs = slab_objcgs(slab); >> + if (!objcgs) >> + return NULL; >> + >> + off = obj_to_index(slab->slab_cache, slab, p); >> + if (objcgs[off]) >> + return obj_cgroup_memcg(objcgs[off]); >> >> return NULL; >> } > > There is a comment below, which needs some changes: > /* > * page_memcg_check() is used here, because page_has_obj_cgroups() > * check above could fail because the object cgroups vector wasn't set > * at that moment, but it can be set concurrently. > * page_memcg_check(page) will guarantee that a proper memory > * cgroup pointer or NULL will be returned. > */ > > In reality the folio's slab flag can be cleared before releasing the objcgs \ > vector. It seems that there is no such possibility at setting the flag, > it's always set before allocating and assigning the objcg vector. You're right. I'm changing it to: * page_memcg_check() is used here, because in theory we can encounter * a folio where the slab flag has been cleared already, but * slab->memcg_data has not been freed yet * page_memcg_check(page) will guarantee that a proper memory * cgroup pointer or NULL will be returned. I wrote "in theory" because AFAICS it implies a race as we would have to be freeing a slab and at the same time query an object address. We probably could have used the non-check version, but at this point I don't want to make any functional changes besides these comment fixes. I assume your patch on top would cover it? >> @@ -2896,7 +2901,7 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p) >> * page_memcg_check(page) will guarantee that a proper memory >> * cgroup pointer or NULL will be returned. >> */ >> - return page_memcg_check(page); >> + return page_memcg_check(folio_page(folio, 0)); >> } >> >> __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) >> diff --git a/mm/slab.h b/mm/slab.h >> index bca9181e96d7..36e0022d8267 100644 >> --- a/mm/slab.h >> +++ b/mm/slab.h >> @@ -412,15 +412,36 @@ static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t fla >> } >> >> #ifdef CONFIG_MEMCG_KMEM >> -int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s, >> - gfp_t gfp, bool new_page); >> +/* >> + * slab_objcgs - get the object cgroups vector associated with a slab >> + * @slab: a pointer to the slab struct >> + * >> + * Returns a pointer to the object cgroups vector associated with the slab, >> + * or NULL. This function assumes that the slab is known to have an >> + * associated object cgroups vector. It's not safe to call this function >> + * against slabs with underlying pages, which might have an associated memory >> + * cgroup: e.g. kernel stack pages. > > Hm, is it still true? I don't think so. It must be safe to call it for any > slab now. Right, forgot to update after removing the _check variant. Changing to: * Returns a pointer to the object cgroups vector associated with the slab, * or NULL if no such vector has been associated yet. > The rest looks good to me, please feel free to add > Reviewed-by: Roman Gushchin > after fixing these comments. Thanks! > Thanks!