Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2403898pxb; Tue, 13 Apr 2021 00:39:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysdPvHDg0C6Dz0guFUq8YP+Fjk9k+EkIYHuE/wdP5VX9nJPgiUqltZl5dyW5fnSSOJJXhU X-Received: by 2002:a17:907:7283:: with SMTP id dt3mr7837137ejc.47.1618299554747; Tue, 13 Apr 2021 00:39:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618299554; cv=none; d=google.com; s=arc-20160816; b=sL640801+nU1D7kf0JujBg4sN9OF7uIw/Yv+oZaur0UPBHUPmOWu78C0oMIdq8ddwU /RueSDeE4C08Y76IUR8oXwe0eYwUr6pHnmBnty15Zr2JnpTP55eyL/HnG+7WcDRaQAzi EUIYPKwC06f0CNil529wq6wS+lw9ZsrpkESdFmWNlsK7lhgCr6PxfqgpYgpKtEkgG0Fp zxko7R0FT7R+6JnBAfvyREOBk61CacwoBVO+HsI2qaNeaqxMvvbFqzGS10O96Gt3Rj8M 6Ew0WVUGvQTtspLYcO/TUVyKQWC2LrSCU/tBHuCrLpxspm/05hkCtUgUf7gvQOoQVoMZ cOFQ== 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=NsnOZhfDDmNX8NdBm3yb2Qi8vpH9UqjQP9RovkzcARY=; b=dPaMwCpWHa9GkGRTiRwCOaHJrp3llvfA0AeP5RZul1f6EtMR+FzdNgb7iZ4J061E89 xYVOJ52O2dJhQsq3ydYXv1pQsegFsaLglqQ5o45YDBUJ/BnTb0TV6L+76djKoxkfoWh/ gpmK7ekMK5aS7D1HHxFBAGVE+1E3+AwpkYWE332YxogtbdSXUjHgTlZhRVUxmUyFLXOj s0VcF4RsV2NDPP4i1z/8rjrstFHwl7q3B6Km2SbkfQ1Idd7JQTjsVmkbBaV5hEMlXbEU HeV3ydRboiRdXeB13fcHvNhiPTSdPj/j3vycX/RCknpR4qJdrWQEJfsZPI/271uPaUIg gcgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SNEQzAlA; 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 gb9si8390880ejc.673.2021.04.13.00.38.51; Tue, 13 Apr 2021 00:39:14 -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=SNEQzAlA; 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 S237386AbhDMBDY (ORCPT + 99 others); Mon, 12 Apr 2021 21:03:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:47811 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240180AbhDMBDX (ORCPT ); Mon, 12 Apr 2021 21:03:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618275784; 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=NsnOZhfDDmNX8NdBm3yb2Qi8vpH9UqjQP9RovkzcARY=; b=SNEQzAlAPT+aQ60YxDF9A1iBUUmr1sXHtAEsrfNdGhf2loB6+6ib5wrjICqvnBCidFZDFb 1tHCXoFy+IsBn4JlE4ACf4KLKCJpbDRxWHucGfM5+9GVc84Z/VvKKNT/Q/ZJ8klY10wpRz zRNow3guhdZmrLhoNxCPXKnPu0rH4MY= 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-226-tQw6gGfgObK00hAD1Rc99g-1; Mon, 12 Apr 2021 21:03:03 -0400 X-MC-Unique: tQw6gGfgObK00hAD1Rc99g-1 Received: by mail-qv1-f71.google.com with SMTP id i7so9140878qvj.1 for ; Mon, 12 Apr 2021 18:03:03 -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=NsnOZhfDDmNX8NdBm3yb2Qi8vpH9UqjQP9RovkzcARY=; b=i7bZtSI1DJPq5vglru0UebveVsJ3a/Y4WA+v68qGTbvCWZkjWRDo5zDIrx8kf0fFMj 9CB4mSnRJoXAO2+xmEEJXq35rFJIU4u35bEUtdsn2Dv+UAtr4qujTiYsJs3AhJ5HTasH Y8XebYrMB8nIWCPuSRsgdRFu2a0Wr4QrEPVhPdIfL6Fj7aFtioIBdCF6z+mCLbsroRud KuKMd7uLsS1g5jAy/nmIBIIR6VHSPvXo24WaiDBjiHAGkng350wbjnVNRaXgKV6rxJ0M 310Y6Zbuj6XFIH+cO7u1Cut2iS3kQljMyVTPNp5F5xYgcGVsPyTtN3kVHQmQ03embZSN 96gg== X-Gm-Message-State: AOAM531S+xm+GbkgJMStgDJSYLJFrOZ2pnQOLk3tf1xcyR5ma4v74Lp4 laP+v1ocK27FOsUPd0yrhX92vINHFUIRlhlCJoZK3PyQWnEhKCs2k/C8zfsigcYx4ML7tbSkfm/ rmdwkMNdHkPVS/rBBsJykyN8Y X-Received: by 2002:ac8:40d9:: with SMTP id f25mr27965005qtm.323.1618275782803; Mon, 12 Apr 2021 18:03:02 -0700 (PDT) X-Received: by 2002:ac8:40d9:: with SMTP id f25mr27964989qtm.323.1618275782525; Mon, 12 Apr 2021 18:03:02 -0700 (PDT) Received: from llong.remote.csb ([2601:191:8500:76c0::cdbc]) by smtp.gmail.com with ESMTPSA id f9sm9097847qkk.115.2021.04.12.18.03.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 12 Apr 2021 18:03:01 -0700 (PDT) From: Waiman Long X-Google-Original-From: Waiman Long Subject: Re: [PATCH v2 5/5] mm/memcg: Optimize user context object stock access To: Shakeel Butt Cc: Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Tejun Heo , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , LKML , Cgroups , Linux MM , Muchun Song , Alex Shi , Chris Down , Yafang Shao , Wei Yang , Masayoshi Mizuma , Xing Zhengjun References: <20210412225503.15119-1-longman@redhat.com> <20210412225503.15119-6-longman@redhat.com> Message-ID: <507e199e-963c-b7e2-958a-6cc61b55ca5d@redhat.com> Date: Mon, 12 Apr 2021 21:03:00 -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/12/21 7:10 PM, Shakeel Butt wrote: > On Mon, Apr 12, 2021 at 3:55 PM 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 >> --- >> mm/memcontrol.c | 73 +++++++++++++++++++++++++++++++++++++++---------- >> 1 file changed, 59 insertions(+), 14 deletions(-) >> >> diff --git a/mm/memcontrol.c b/mm/memcontrol.c >> index 69f728383efe..29f2df76644a 100644 >> --- a/mm/memcontrol.c >> +++ b/mm/memcontrol.c >> @@ -2229,7 +2229,8 @@ struct obj_stock { >> struct memcg_stock_pcp { >> struct mem_cgroup *cached; /* this never be root cgroup */ >> unsigned int nr_pages; >> - struct obj_stock obj; >> + struct obj_stock task_obj; >> + struct obj_stock irq_obj; >> >> struct work_struct work; >> unsigned long flags; >> @@ -2254,11 +2255,48 @@ static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, >> } >> #endif >> >> +/* >> + * Most kmem_cache_alloc() calls are from user context. 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 by disabling preemption only >> + * 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. >> + */ >> static inline struct obj_stock *current_obj_stock(void) >> { >> struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock); >> >> - return &stock->obj; >> + return in_task() ? &stock->task_obj : &stock->irq_obj; >> +} >> + >> +#define get_obj_stock(flags) \ >> +({ \ >> + struct memcg_stock_pcp *stock; \ >> + struct obj_stock *obj_stock; \ >> + \ >> + if (in_task()) { \ >> + preempt_disable(); \ >> + (flags) = -1L; \ >> + stock = this_cpu_ptr(&memcg_stock); \ > The above line was missing in the previous version. > >> + obj_stock = &stock->task_obj; \ >> + } else { \ >> + local_irq_save(flags); \ >> + stock = this_cpu_ptr(&memcg_stock); \ >> + obj_stock = &stock->irq_obj; \ >> + } \ >> + obj_stock; \ >> +}) >> + >> +static inline void put_obj_stock(unsigned long flags) >> +{ >> + if (flags == -1L) >> + preempt_enable(); >> + else >> + local_irq_restore(flags); >> } >> >> /** >> @@ -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(); >> } >> >> @@ -3193,7 +3233,7 @@ static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes) >> unsigned long flags; >> bool ret = false; >> >> - local_irq_save(flags); >> + stock = get_obj_stock(flags); >> >> stock = current_obj_stock(); > The above is redundant. Right. I should check the patch carefully. Will remove it. Thanks, Longman