From patchwork Sat Jun 22 03:58:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leonardo Bras X-Patchwork-Id: 13708198 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 AB6B7C27C53 for ; Sat, 22 Jun 2024 03:59:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 234948D01B6; Fri, 21 Jun 2024 23:59:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 195A48D01AF; Fri, 21 Jun 2024 23:59:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F29DE8D01B6; Fri, 21 Jun 2024 23:59:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D309B8D01AF for ; Fri, 21 Jun 2024 23:59:01 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 953FE8133D for ; Sat, 22 Jun 2024 03:59:01 +0000 (UTC) X-FDA: 82257168882.03.B944AED Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 84BB52000E for ; Sat, 22 Jun 2024 03:58:59 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dNnBhZ3f; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of leobras@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=leobras@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719028731; a=rsa-sha256; cv=none; b=ayjWtvv47Uc4kMHlN7vDTraWL7fCNDiytMMimR0oqxRuqkTc6gizHQeUrrh7ViEBIpM/qx IdEFMla1UKiEq7+WXFMILa58SXFYb5xW4Yw5OVb13R33bb+mlVi56pbXIRVMgjcXn3TUE2 Q05vhZrea/Ncwzvv8jCviaFb9aZgvMA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dNnBhZ3f; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of leobras@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=leobras@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719028731; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=kqjqAcyBF16xwLE61W3REKh7p8uOAAMqSPa12WNm3GE=; b=U0SJ5EXzYLICFGq2rZ8ZGxoc70vTtMdErc4P56Ny1GsIKTUWIHMynvyF6Xy57SPJ63moCJ czrG+xlHaqtq8xAUQJJcmew6vwYmanhL6lrAGncwWfVnSuMRui8x+kdk2tKyy3n7TRcaa5 de1NjulPNErF7zPu1vMfL636KZn8X+0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719028738; 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=kqjqAcyBF16xwLE61W3REKh7p8uOAAMqSPa12WNm3GE=; b=dNnBhZ3fu37tK5ALUrxBoemtAGQoLee+hGA9NmvhWqTNvA/ENabHvL6Zayqdu4gqVI+S2v OLXhIN/q+42+0QT7JZGCgbVqyrAQvKEboFwZlMnVFVB5Umc7+XTG3CuOp//3aV5KzGKo9f adwW0aRLooNasXc5S+7Kdd4sic8g0dY= Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-635-98AKigi3Pg2VZYwYFCrJpw-1; Fri, 21 Jun 2024 23:58:57 -0400 X-MC-Unique: 98AKigi3Pg2VZYwYFCrJpw-1 Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-2c78c2b7f4bso2698480a91.0 for ; Fri, 21 Jun 2024 20:58:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719028736; x=1719633536; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kqjqAcyBF16xwLE61W3REKh7p8uOAAMqSPa12WNm3GE=; b=qEj3rj67PQv9PWfmTyJG9AU9KGxX/A7jcJzQQp2WOaLXKup34tC4uJeZAMwCj2zp2s 42j1JQb28atPc6MoteDCTO5uMA6IGXvlvoqaQjjwLNXiMKh185OA1lIR9pBXshDuQF3T /Y10JvapLROpzSx0XiJc3zR+onfjUqIYU3g11tjroiaw1IJk61P7JtdWA2VyaGbY+45i Mp6MSsIPVkDqtLiFtZ3tXwLVGDnl/fd/aRsgt9uqN+4uOrZZ0+G6vfER1kGOvn9+G+dL GxjkqLKHXvIkfclFaJL5gfHzv/zM82b/j8hcA6USWMhvvydk7pCEm41UTYMJKMpaQsKf Smtw== X-Forwarded-Encrypted: i=1; AJvYcCW0Chde0cV1lhMTfX+c1j3NTxAd7/Q5M2CQ49HQmF9rkICLbVfojB2UETEH0yczZSlSpQy5bEGOyfC+dFUd+JXkqz4= X-Gm-Message-State: AOJu0Yzyu3X3lj9pSEKRAFxdfymq1CV0C9IACizpJHFUX6ix8+GNi3sN uOIUc5TpEvYX64BBA21qq1/W9X4rNtFWe+pbaLEb3+XvILeQP83/K1YTAnIQVAdxZY9d/Q+EN/p O0CrxBN15By/5fe3opaPn0ztUUVxH5kTpLGDIz+Q8cHx/A23L X-Received: by 2002:a17:903:1cf:b0:1f6:92f1:b01c with SMTP id d9443c01a7336-1f9aa481248mr125471875ad.69.1719028736461; Fri, 21 Jun 2024 20:58:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGsa/g3e768QiFMvXJv3hzlQlYWrPA/q475LVIhwI75gKqm/BT5XtIgr5pD5KmuF/MDMe+IuQ== X-Received: by 2002:a17:903:1cf:b0:1f6:92f1:b01c with SMTP id d9443c01a7336-1f9aa481248mr125471745ad.69.1719028736115; Fri, 21 Jun 2024 20:58:56 -0700 (PDT) Received: from LeoBras.redhat.com ([2804:1b3:a801:c138:e21d:3579:5747:ad1]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb32b9edsm21832365ad.118.2024.06.21.20.58.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jun 2024 20:58:55 -0700 (PDT) From: Leonardo Bras To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Leonardo Bras , Thomas Gleixner , Marcelo Tosatti Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH v1 3/4] memcontrol: apply new queue_percpu_work_on() interface Date: Sat, 22 Jun 2024 00:58:11 -0300 Message-ID: <20240622035815.569665-4-leobras@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240622035815.569665-1-leobras@redhat.com> References: <20240622035815.569665-1-leobras@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: 84B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QdkA5F nYACY3vDTbrfAwyYGvArQBlNa+OfhX2vGFOnDNjHNIhCtqyT4SC7ukoB2i1q/tg2YYS6Jiqnksd8nsVt41WEodGYcpDg8I0QNLF9dq4cdc6F5AqOmDeWpZnPPZRyaVrP86bRms2BabXTP32JFBF8Layoj68lAewfmHIwhc2KexPKR5ZpGcAMqZDlsZyF6nifLdsqcGrqTvfWgMqfz9+Vx0/7ITSed82lWXXZwM+6dH89UUoWB9dQuV3M3AyzRYqO1oDg4lKckBUvd8xQ0xFLIJqHRunO/iy/JQJXoKIQRWOGdYWxnIHNWgbqOYuxhukpwW739x+Ie725X5gLnU28ykcH/5zKWkiVofVYenuEb3VZWyu3cMFdtpK54H1D3LRsyPC8ugMSeohyR8h0paFKUaX5II0Jprwok+4USfZpxPx9nXepta4hkwCQ9FcwmDm09HevTrbcu6dtR2aJ1Y8rgbVF17w== 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: List-Subscribe: List-Unsubscribe: Make use of the new qpw_{un,}lock*() and queue_percpu_work_on() interface to improve performance & latency on PREEMTP_RT kernels. For functions that may be scheduled in a different cpu, replace local_{un,}lock*() by qpw_{un,}lock*(), and replace schedule_work_on() by queue_percpu_work_on(). This change requires allocation of qpw_structs instead of a work_structs. This should bring no relevant performance impact on non-RT kernels: For functions that may be scheduled in a different cpu, the local_*lock's this_cpu_ptr() becomes a per_cpu_ptr(smp_processor_id()). Signed-off-by: Leonardo Bras --- mm/memcontrol.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 71fe2a95b8bd..18a987f8c998 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -59,20 +59,21 @@ #include #include #include #include #include #include #include #include #include #include +#include #include "internal.h" #include #include #include "slab.h" #include "swap.h" #include #include @@ -2415,21 +2416,21 @@ struct memcg_stock_pcp { unsigned int nr_pages; #ifdef CONFIG_MEMCG_KMEM struct obj_cgroup *cached_objcg; struct pglist_data *cached_pgdat; unsigned int nr_bytes; int nr_slab_reclaimable_b; int nr_slab_unreclaimable_b; #endif - struct work_struct work; + struct qpw_struct qpw; unsigned long flags; #define FLUSHING_CACHED_CHARGE 0 }; static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock) = { .stock_lock = INIT_LOCAL_LOCK(stock_lock), }; static DEFINE_MUTEX(percpu_charge_mutex); #ifdef CONFIG_MEMCG_KMEM static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock); @@ -2503,39 +2504,40 @@ static void drain_stock(struct memcg_stock_pcp *stock) if (do_memsw_account()) page_counter_uncharge(&old->memsw, stock_pages); WRITE_ONCE(stock->nr_pages, 0); } css_put(&old->css); WRITE_ONCE(stock->cached, NULL); } -static void drain_local_stock(struct work_struct *dummy) +static void drain_local_stock(struct work_struct *w) { struct memcg_stock_pcp *stock; struct obj_cgroup *old = NULL; unsigned long flags; + int cpu = qpw_get_cpu(w); /* * The only protection from cpu hotplug (memcg_hotplug_cpu_dead) vs. * drain_stock races is that we always operate on local CPU stock * here with IRQ disabled */ - local_lock_irqsave(&memcg_stock.stock_lock, flags); + qpw_lock_irqsave(&memcg_stock.stock_lock, flags, cpu); - stock = this_cpu_ptr(&memcg_stock); + stock = per_cpu_ptr(&memcg_stock, cpu); old = drain_obj_stock(stock); drain_stock(stock); clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); - local_unlock_irqrestore(&memcg_stock.stock_lock, flags); + qpw_unlock_irqrestore(&memcg_stock.stock_lock, flags, cpu); obj_cgroup_put(old); } /* * Cache charges(val) to local per_cpu area. * This will be consumed by consume_stock() function, later. */ static void __refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) { struct memcg_stock_pcp *stock; @@ -2592,23 +2594,23 @@ static void drain_all_stock(struct mem_cgroup *root_memcg) if (memcg && READ_ONCE(stock->nr_pages) && mem_cgroup_is_descendant(memcg, root_memcg)) flush = true; else if (obj_stock_flush_required(stock, root_memcg)) flush = true; rcu_read_unlock(); if (flush && !test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { if (cpu == curcpu) - drain_local_stock(&stock->work); + drain_local_stock(&stock->qpw.work); else if (!cpu_is_isolated(cpu)) - schedule_work_on(cpu, &stock->work); + queue_percpu_work_on(cpu, system_wq, &stock->qpw); } } migrate_enable(); mutex_unlock(&percpu_charge_mutex); } static int memcg_hotplug_cpu_dead(unsigned int cpu) { struct memcg_stock_pcp *stock; @@ -7956,22 +7958,22 @@ static int __init mem_cgroup_init(void) * used for per-memcg-per-cpu caching of per-node statistics. In order * to work fine, we should make sure that the overfill threshold can't * exceed S32_MAX / PAGE_SIZE. */ BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE); cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL, memcg_hotplug_cpu_dead); for_each_possible_cpu(cpu) - INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work, - drain_local_stock); + INIT_QPW(&per_cpu_ptr(&memcg_stock, cpu)->qpw, + drain_local_stock, cpu); for_each_node(node) { struct mem_cgroup_tree_per_node *rtpn; rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node); rtpn->rb_root = RB_ROOT; rtpn->rb_rightmost = NULL; spin_lock_init(&rtpn->lock); soft_limit_tree.rb_tree_per_node[node] = rtpn;