Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp1030372pxb; Thu, 15 Apr 2021 12:08:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwmVuq9wMsiR5ZdhTzJqB28tNEuiiynF/eOZxAliSgsvRHQECvv5s7pVFR54pv53DRlbog9 X-Received: by 2002:a05:6402:6c1:: with SMTP id n1mr6034414edy.158.1618513699284; Thu, 15 Apr 2021 12:08:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618513699; cv=none; d=google.com; s=arc-20160816; b=bx2hdGB7O/oKaOPaXoV0x46rBOVMMQk1eWNTbi+2XlE+kk5qPLQJHXH+bkW4yt4jn5 qp/B7JmfA1uQZd43egCeLO+7tzdW42Ys6m/0jyu9uSGGWr+TxwIDbxLoLpNOhFV4Tuej 69I4qbLGdquNkesvG/bJknATfIEwYUtWGSOFAvOwuO1P1kVYyvWOzmfTlvOu+ah/IuG3 MizywaEhSehIDdZJL56kmCEPdYatkYIy7E7XzHSuGo+/HOF16fmujn6Le7qCerIivkez UXKVCkKfyyLrpf8K4UZSUBqhjszTH/HAmtC9t4aoDoaYE+64hdXPaLLbO463g7s9RR6g CkBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:references:cc :to:subject:from:dkim-signature; bh=gEe76+1lfitRLX7ayQ138JdnojjAHhZEepE6oK4fMzM=; b=u3XeVFU2ZGKDL1IxZPPrVTy2gOkMm/Tw+/WmFFUlOO8KwVhPAD9/bDxSqdouS/zEW/ W0TnOg/n60Y0qc/TYntUpeNA0E/nRBssyMh/TBbYemFRjbpkkY9zbbkt2JlTemaqc5BT /HbvGTpezOKVIEopLlOGsuRhvVKcx8rNKHTsdwPmRSl6P5RsOsJE+N6bQIfSO3w1LV+R JPKaGPIHvxfbYbZ0mNN5ZNl5s3n+9SEvpGc6+DoWQ1HU41m5too4axnXEVdCs05aEo9M 2afthtOeKFDUCRgZUmZB8OtyfvuM76dq/3VI9fGH7zV5X0xmhuzI0lHB/FaaPO6olCqN MWsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=H67Muo1X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d10si2593911edm.362.2021.04.15.12.07.54; Thu, 15 Apr 2021 12:08:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=H67Muo1X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234701AbhDOTG6 (ORCPT + 99 others); Thu, 15 Apr 2021 15:06:58 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:56490 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234749AbhDOTG5 (ORCPT ); Thu, 15 Apr 2021 15:06:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618513593; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gEe76+1lfitRLX7ayQ138JdnojjAHhZEepE6oK4fMzM=; b=H67Muo1X73tlPmia4K7LglApEuG1s2zaApj3eA+e1bLPoY2vDLqovBatsNJ2gPs4FCBX5f 3zHO7uRxEjPLDuJcLrW/2LlPK8VBAj6W6ZI9u+NpaRZRogJd8AEGWXPpRgXNTM2/8VSMl4 W2KweDrwHJGG5ICPwclJkX+acPy/w5c= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-451-NH4EKSrLO42WnnhuEehOYA-1; Thu, 15 Apr 2021 15:06:32 -0400 X-MC-Unique: NH4EKSrLO42WnnhuEehOYA-1 Received: by mail-qv1-f71.google.com with SMTP id z17-20020a0cf0110000b029019a84330150so2457412qvk.13 for ; Thu, 15 Apr 2021 12:06:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=gEe76+1lfitRLX7ayQ138JdnojjAHhZEepE6oK4fMzM=; b=sjtRjhXpO+mhO/GEV3jGlZkWKAI/4labYSxKcCYIec85uMuFXzGMJ0WI/isiEfxL96 RfhtliTrt1U7SkKSMUJ/Gy5PGhYCHev2QXCv8P9lof9GuxpjHQNWa1dXxDYef8qNVDKf C92yG8Sohl8BPX/l9tjokVjAZq9wYsR6VjBL1zr1oFOi74o5Eq8AVN9T7KOjPDDu61hF 9oTb9Em85Y3sxrTI46Jy9u8mtP20h8rsNMwBMFYJnR1jzUmT+wpLTxVolV7fM+gW+Kmb N+3Fek2uzOdcvMAyzslYoCTnZ8FmNBGOhXPvXJopVJnRZ5xOaL71o4NcANsoW+kK6LaH HwOw== X-Gm-Message-State: AOAM532w0qBIDs53/WsbxXPcXkMr0/WFuFLmEZFCIroOkVBEaxArlm91 wVdWlZaxWIyhJSAf6luupzUA32C+Yk/JUEKq42HOvK7GmnmiY91WyqV1XhveiAk7k+E4Qj9+Hre 1jCEElzaD8tbZ5ty4zR7ssw2G X-Received: by 2002:ad4:50d0:: with SMTP id e16mr4732751qvq.6.1618513591646; Thu, 15 Apr 2021 12:06:31 -0700 (PDT) X-Received: by 2002:ad4:50d0:: with SMTP id e16mr4732719qvq.6.1618513591332; Thu, 15 Apr 2021 12:06:31 -0700 (PDT) Received: from llong.remote.csb ([2601:191:8500:76c0::cdbc]) by smtp.gmail.com with ESMTPSA id o189sm2659335qkd.60.2021.04.15.12.06.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 15 Apr 2021 12:06:29 -0700 (PDT) From: Waiman Long X-Google-Original-From: Waiman Long Subject: Re: [PATCH v3 5/5] mm/memcg: Optimize user context object stock access To: Johannes Weiner , Waiman Long Cc: Michal Hocko , Vladimir Davydov , Andrew Morton , Tejun Heo , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Shakeel Butt , Muchun Song , Alex Shi , Chris Down , Yafang Shao , Wei Yang , Masayoshi Mizuma , Xing Zhengjun References: <20210414012027.5352-1-longman@redhat.com> <20210414012027.5352-6-longman@redhat.com> <8dbd3505-9c51-362f-82d8-5efa5773e020@redhat.com> Message-ID: Date: Thu, 15 Apr 2021 15:06:28 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/15/21 2:53 PM, Johannes Weiner wrote: > On Thu, Apr 15, 2021 at 02:16:17PM -0400, Waiman Long wrote: >> On 4/15/21 1:53 PM, Johannes Weiner wrote: >>> On Tue, Apr 13, 2021 at 09:20:27PM -0400, Waiman Long wrote: >>>> Most kmem_cache_alloc() calls are from user context. With instrumentation >>>> enabled, the measured amount of kmem_cache_alloc() calls from non-task >>>> context was about 0.01% of the total. >>>> >>>> The irq disable/enable sequence used in this case to access content >>>> from object stock is slow. To optimize for user context access, there >>>> are now two object stocks for task context and interrupt context access >>>> respectively. >>>> >>>> The task context object stock can be accessed after disabling preemption >>>> which is cheap in non-preempt kernel. The interrupt context object stock >>>> can only be accessed after disabling interrupt. User context code can >>>> access interrupt object stock, but not vice versa. >>>> >>>> The mod_objcg_state() function is also modified to make sure that memcg >>>> and lruvec stat updates are done with interrupted disabled. >>>> >>>> The downside of this change is that there are more data stored in local >>>> object stocks and not reflected in the charge counter and the vmstat >>>> arrays. However, this is a small price to pay for better performance. >>>> >>>> Signed-off-by: Waiman Long >>>> Acked-by: Roman Gushchin >>>> Reviewed-by: Shakeel Butt >>> This makes sense, and also explains the previous patch a bit >>> better. But please merge those two. >> The reason I broke it into two is so that the patches are individually >> easier to review. I prefer to update the commit log of patch 4 to explain >> why the obj_stock structure is introduced instead of merging the two. > Well I did not find them easier to review separately. > >>>> @@ -2327,7 +2365,9 @@ static void drain_local_stock(struct work_struct *dummy) >>>> local_irq_save(flags); >>>> stock = this_cpu_ptr(&memcg_stock); >>>> - drain_obj_stock(&stock->obj); >>>> + drain_obj_stock(&stock->irq_obj); >>>> + if (in_task()) >>>> + drain_obj_stock(&stock->task_obj); >>>> drain_stock(stock); >>>> clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); >>>> @@ -3183,7 +3223,7 @@ static inline void mod_objcg_state(struct obj_cgroup *objcg, >>>> memcg = obj_cgroup_memcg(objcg); >>>> if (pgdat) >>>> lruvec = mem_cgroup_lruvec(memcg, pgdat); >>>> - __mod_memcg_lruvec_state(memcg, lruvec, idx, nr); >>>> + mod_memcg_lruvec_state(memcg, lruvec, idx, nr); >>>> rcu_read_unlock(); >>> This is actually a bug introduced in the earlier patch, isn't it? >>> Calling __mod_memcg_lruvec_state() without irqs disabled... >>> >> Not really, in patch 3, mod_objcg_state() is called only in the stock update >> context where interrupt had already been disabled. But now, that is no >> longer the case, that is why i need to update mod_objcg_state() to make sure >> irq is disabled before updating vmstat data array. > Oh, I see it now. Man, that's subtle. We've had several very hard to > track down preemption bugs in those stats, because they manifest as > counter imbalances and you have no idea if there is a leak somewhere. > > The convention for these functions is that the __ prefix indicates > that preemption has been suitably disabled. Please always follow this > convention, even if the semantic change is temporary. I see. I will fix that in the next version. > > Btw, is there a reason why the stock caching isn't just part of > mod_objcg_state()? Why does the user need to choose if they want the > caching or not? It's not like we ask for this when charging, either. > Yes, I can revert it to make mod_objcg_state() to call mod_obj_stock_state() internally instead of the other way around. Cheers, Longman