Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp11024807rwr; Fri, 12 May 2023 17:36:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6yI8xtUkGI9TvNioHWTbBpKkR/zwlKysfANqDLzyNAERrpsdXSOdx9A/6EPAw+CXVAZ99O X-Received: by 2002:a17:902:7482:b0:1aa:f43f:e60 with SMTP id h2-20020a170902748200b001aaf43f0e60mr28246383pll.36.1683938203916; Fri, 12 May 2023 17:36:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683938203; cv=none; d=google.com; s=arc-20160816; b=WecvuSe6BBxIoFFlIItYFcKszB8VJTRu6+TXdQC74f052864l8uDuksUaG2VvyeR0S qnjiWZKrrHJZ55c+A+oZsCRmOVORpSDi9yIbBbCvG13ZMkLgJpvtN90n6m4D/4XFEchz EEnasZ79x6MfrWDLjlCmH1iNrb3lablMbY6lbPMBiQH/Delnh8Uhb+85xtpDRrCJxyco bSVg/Zu983WgGMVYIopeKmo5nduqPcOgEEEDeRAYcZFQ7sYIRZRoSRK95FI8Bx6cAOai RODIAOZgqovHlNUfDJkedI8vNSJi+byKEehUeXk4cYT7/rvh/GctG0HENn+LS5tYeoH4 hA3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=ECIWNGuPLBcMK3MFxNjEXhgU3KJQ68fxx8B8tOmAlPY=; b=L9NsSD1mvGV8yIKH7BtX3//fsJAJz72JMYnUClHrzedHO8W1b3suLEW/CGvWwwQ8T3 sPm2ULe8YnqVDJJyknOVtlldoIV4p0p/J3tmbkagTuwg2yCAzLvy8AUDPOcp9Im+VoS5 MI1jhWxjREmSb6jXWxvt0Zyf1aY7KJ4B7GzgOjYWFvEDst18wZLTqiJWfagLaqKZpE29 /UQcKiA8xKfk04AJNdT+hdN+7pPcVJPzndFT/+7enVHTIeyIjrGOc7VQXzEf8+HbHyGj RjTEsM2aKVUx+8TvJooKyOp/pdXWAkpMFgTDWIpLui1O1h4PRwqLXZ3b3yj+w6UfX0xC 3g2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=Pb0v1wYd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z3-20020a170903018300b001a24efe1f22si11380473plg.64.2023.05.12.17.36.30; Fri, 12 May 2023 17:36:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=Pb0v1wYd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240784AbjEMA0W (ORCPT + 99 others); Fri, 12 May 2023 20:26:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240657AbjEMA0V (ORCPT ); Fri, 12 May 2023 20:26:21 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6651B5583 for ; Fri, 12 May 2023 17:26:20 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id F1D1A6506F for ; Sat, 13 May 2023 00:26:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C0BCC433EF; Sat, 13 May 2023 00:26:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1683937579; bh=GO3UO5KJn9t64GdS1QfysnlIesFzZertc6nkP94v5WA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Pb0v1wYdxfS63fntMjF2mCAybc9rQe6uwe1U+5ImJmcN/gPUljpz/TtjyJsH8dLcn HedrJfpRj7YG87SfuwGJxSuGzWsib1aaOBSNRUbVroAbA8FT7vfqSwMkOMKH1E50fz 4dYEv2fKWj2UQrXAne/ciH4LlfwrfbIf+mIZzjc4= Date: Fri, 12 May 2023 17:26:18 -0700 From: Andrew Morton To: Haifeng Xu Cc: luto@kernel.org, bigeasy@linutronix.de, tglx@linutronix.de, bristot@kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] fork: optimize memcg_charge_kernel_stack() a bit Message-Id: <20230512172618.1120ab4c1e6d3026081c20d9@linux-foundation.org> In-Reply-To: <20230508064458.32855-1-haifeng.xu@shopee.com> References: <20230508064458.32855-1-haifeng.xu@shopee.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-9.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 8 May 2023 06:44:58 +0000 Haifeng Xu wrote: > Since commit f1c1a9ee00e4 ("fork: Move memcg_charge_kernel_stack() > into CONFIG_VMAP_STACK"), memcg_charge_kernel_stack() has been moved > into CONFIG_VMAP_STACK block, so the CONFIG_VMAP_STACK check can be > removed. > > Furthermore, memcg_charge_kernel_stack() is only invoked by > alloc_thread_stack_node() instead of dup_task_struct(). If > memcg_kmem_charge_page() fails, the uncharge process is handled in > memcg_charge_kernel_stack() itself instead of free_thread_stack(), > so remove the incorrect comments. > > If memcg_charge_kernel_stack() fails to charge pages used by kernel > stack, only charged pages need to be uncharged. It's unnecessary to > uncharge those pages which memory cgroup pointer is NULL. > > ... > > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -250,23 +250,20 @@ static int memcg_charge_kernel_stack(struct vm_struct *vm) > { > int i; > int ret; > + int nr_charged = 0; > > - BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0); > + BUILD_BUG_ON(PAGE_SIZE % 1024 != 0); This check now deserves some sort of award. I'll remove it. > BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE); > > for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { > ret = memcg_kmem_charge_page(vm->pages[i], GFP_KERNEL, 0); > if (ret) > goto err; > + nr_charged++; > } > return 0; > err: > - /* > - * If memcg_kmem_charge_page() fails, page's memory cgroup pointer is > - * NULL, and memcg_kmem_uncharge_page() in free_thread_stack() will > - * ignore this page. > - */ > - for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) > + for (i = 0; i < nr_charged; i++) > memcg_kmem_uncharge_page(vm->pages[i], 0); > return ret; We don't really need nr_charged - we could just do while (--i >= 0) but nr_charged is straightforward, which is always a good choice.