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 678C2C2BD09 for ; Fri, 28 Jun 2024 01:06:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0028A6B00A2; Thu, 27 Jun 2024 21:06:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECBC86B00A3; Thu, 27 Jun 2024 21:06:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1EDE6B00A5; Thu, 27 Jun 2024 21:06:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B03766B00A2 for ; Thu, 27 Jun 2024 21:06:39 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3155016130A for ; Fri, 28 Jun 2024 01:06:39 +0000 (UTC) X-FDA: 82278507318.05.5C85C6C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 48F5B4000F for ; Fri, 28 Jun 2024 01:06:37 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OqpNC25W; spf=pass (imf17.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719536779; 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=njkkozG9EFlh7coxFPh179Qtw83r0T/2ltt/6G5V93o=; b=43fGcvr1wG3OfqFI9D1j06icqh4RF1Ea/El7BN9oudI4HuAGSZctUogQlTIj1Np/N9iDST t71XS3YxPh6nRMKv1bJRQxc/OE/p0Q3tzBIAxd8Bt2kWBDF1xHMvEu3Q/p/xqWi96zHgAr 8mhG0KxPTMVAz2AvKP+IFBtRHv2Vw9s= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719536779; a=rsa-sha256; cv=none; b=qnN2N0TYbyo5HD+vTHmAlDI5mt0YHgKCRM762IJDYQiWfaQw1lAOTseKK+Rf4JIjxmeA36 /hunf5tMwd/Po0N92/Ky/mflmYScjq5a/T68QKlABw6yxgWro+UHX/BR8MSkTB4pGhhzML EkqXkTPsUv/QQ5bMFRXPhRcwI8GR64Y= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OqpNC25W; spf=pass (imf17.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719536796; 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=njkkozG9EFlh7coxFPh179Qtw83r0T/2ltt/6G5V93o=; b=OqpNC25WaNh2cpmkzqneOpKAi4+F5c8sGIW20FJSMVhmMSzfYxBT5OqOkFgbmMbXux3NY3 U4WHCJ8S1FUOG1QTnd7DXRV9eiY6l47c/xVh1OjqhN+rdAW5/zbi0UIr7m/wQHAPSSV6M6 L3fRhcTmN9yPXkgmiLk+pgQ7jgIKoIQ= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-361-DsioewEANdKRqGGg7Q38Eg-1; Thu, 27 Jun 2024 21:06:32 -0400 X-MC-Unique: DsioewEANdKRqGGg7Q38Eg-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 499A31955F03; Fri, 28 Jun 2024 01:06:30 +0000 (UTC) Received: from [10.22.32.240] (unknown [10.22.32.240]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id BEB0F300021A; Fri, 28 Jun 2024 01:06:27 +0000 (UTC) Message-ID: <4b86e9ec-f94d-4838-ad24-e1a3c89c9939@redhat.com> Date: Thu, 27 Jun 2024 21:06:26 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH V4 1/2] cgroup/rstat: Helper functions for locking expose trylock From: Waiman Long To: Jesper Dangaard Brouer , tj@kernel.org, cgroups@vger.kernel.org, yosryahmed@google.com, shakeel.butt@linux.dev Cc: hannes@cmpxchg.org, lizefan.x@bytedance.com, kernel-team@cloudflare.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <171952310959.1810550.17003659816794335660.stgit@firesoul> <08062501-f3fb-4e4d-b72c-f1b0f964640f@redhat.com> Content-Language: en-US In-Reply-To: <08062501-f3fb-4e4d-b72c-f1b0f964640f@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Stat-Signature: opcagbyoysw4ebqhq6b3yy3cr9bogmnn X-Rspamd-Queue-Id: 48F5B4000F X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1719536797-713285 X-HE-Meta: U2FsdGVkX18OeBQqat0gXWseWz6NLe/aNR+Hi52al4ATsu7CZ9XTHerU83JLJ9crFbue9tA/cmYYisjBPRhKhWkqLu5CaA0ei4S1tiUDfG+f8Pd6uCa/zG5OVGhTCaREf8sW3WPio3PxzCO5HTL4sl1LrjEhebUKMRnvCQ6btgAvxEDDMv0cOPJrecPvE5bhgmddnk7PKKuIEdmpdSe8QjNx64n8VX/PBhtO8tw2pI03ARLDc5s34D3DFj6zsA60/k8lwhr+505U8Bcatm89QyR+F6wmwg9ujmH2h3oQ8BO2AIIA1j42GA7Q5/AnkiEahv7CpmnCCgk9TszJ5wA8cJnSoIM7trttg+zg47jtV3Fc+FxjsRHnlSYLa+Kmzm9szdTPYtVHTrnIGmzObZMa4oNRfOsUjKXwTL/a4P1hi7FTZNu9erePUuG1ZgDDRqLXkLlN47HpyrLxSw0F0mQ0PC5E046+zv9iAFLzplU7++kEIddpFov0qtvTH+Q2VWt2iJ8PaBIlpVZHbBOOoZigNt9/G5xbUV6czNLohhJ7XruT6x3uWexIBzuD9r/gz65YL9wn9uIXBidafUbm6aA67F6O7yxHtnjENE5BB1Pacbg1QYWj7U1Cxvpl5XCvOfl9hf/9a4Grn7tNw59P8gmHMPSE/4X3yzjd7R907LB8SyACENJmWMTuOgL38k+1ylBxVHl2+CJYz2eetx1AV9rF+XOlOWpRHQ8Ye5ZZL8tXruBxngN3t1aRfK14472OkLlsp2ViuHmONBJptb8LM0LgtCuWsZbY0gOPq91xI6tWd24j7T5lm/bjYWniiiznW9hBqPvKqAm4/fzsftBjlzTfh617cNNx5X9aj205YXuMr+9iK0MRL97u02wpFYy2TkZuTZJoAHZB6KEFPidv4TcGf0U5k1hvxYsblwk2jDdCXVZ9UvztMLqs7TdBe0duqpqXxXDjksiMYSnledNM65d oq+u0759 /6uIYr4ryALYJfDShgvSmd9iEEBUgOTfDpR5YWo5CJ12UJ1TyeT8BqbGq2uDzcVJNX192d9ucTGQ+Ezsr8aVDuwpTCYwqqrl2/8S53OVyeBxgKLfN1Z74hTlPVAX4Qj+S3zy8HLAsggMOIatHmuyiqekKuw697sylueacYSWtIEb1bZ5dNw+EURU6sDgkNuBNbVdQLtzXwh3SDdavusFZwZfaPjuQ5V088v7dqPQr/7yf2vX/ki5i5I9ZuznJBP25G0k9iDprAutNFUgrPXr2Gno9IZKA9s8SVceMWIjuxAn+iNMZAN3BHPcS65TdzvPTqP5LbW01aTjE9Z74vO/nClGYz0Aejt9xbX5JSeXkJVm4b6IPhP2ecBopRgUb/0lgufgwqfsqxIsDsZa/6C+pbMko+dMXGJEw84+A X-Bogosity: Ham, tests=bogofilter, spamicity=0.000045, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 6/27/24 18:22, Waiman Long wrote: > > On 6/27/24 17:18, Jesper Dangaard Brouer wrote: >> Signed-off-by: Jesper Dangaard Brouer >> --- >>   kernel/cgroup/rstat.c |   40 ++++++++++++++++++++++++++++++---------- >>   1 file changed, 30 insertions(+), 10 deletions(-) >> >> diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c >> index fb8b49437573..2a42be3a9bb3 100644 >> --- a/kernel/cgroup/rstat.c >> +++ b/kernel/cgroup/rstat.c >> @@ -279,17 +279,30 @@ __bpf_hook_end(); >>    * value -1 is used when obtaining the main lock else this is the CPU >>    * number processed last. >>    */ >> -static inline void __cgroup_rstat_lock(struct cgroup *cgrp, int >> cpu_in_loop) >> +static inline bool __cgroup_rstat_trylock(struct cgroup *cgrp, int >> cpu_in_loop) >> +{ >> +    bool locked; >> + >> +    locked = spin_trylock_irq(&cgroup_rstat_lock); >> +    if (!locked) >> +        trace_cgroup_rstat_lock_contended(cgrp, cpu_in_loop, !locked); >> + >> +    return locked; >> +} >> + >> +static inline void __cgroup_rstat_lock(struct cgroup *cgrp, int >> cpu_in_loop, >> +                       bool check_contention) >>       __acquires(&cgroup_rstat_lock) >>   { >> -    bool contended; >> +    bool locked = false; >>   -    contended = !spin_trylock_irq(&cgroup_rstat_lock); >> -    if (contended) { >> -        trace_cgroup_rstat_lock_contended(cgrp, cpu_in_loop, >> contended); >> +    if (check_contention) >> +        locked = __cgroup_rstat_trylock(cgrp, cpu_in_loop); >> + >> +    if (!locked) >>           spin_lock_irq(&cgroup_rstat_lock); >> -    } >> -    trace_cgroup_rstat_locked(cgrp, cpu_in_loop, contended); >> + >> +    trace_cgroup_rstat_locked(cgrp, cpu_in_loop, !locked); >>   } >>     static inline void __cgroup_rstat_unlock(struct cgroup *cgrp, int >> cpu_in_loop) >> @@ -328,7 +341,7 @@ static void cgroup_rstat_flush_locked(struct >> cgroup *cgrp) >>               __cgroup_rstat_unlock(cgrp, cpu); >>               if (!cond_resched()) >>                   cpu_relax(); >> -            __cgroup_rstat_lock(cgrp, cpu); >> +            __cgroup_rstat_lock(cgrp, cpu, true); >>           } >>       } >>   } >> @@ -348,9 +361,16 @@ static void cgroup_rstat_flush_locked(struct >> cgroup *cgrp) >>    */ >>   __bpf_kfunc void cgroup_rstat_flush(struct cgroup *cgrp) >>   { >> +    bool locked; >> + >>       might_sleep(); >>   -    __cgroup_rstat_lock(cgrp, -1); >> +    locked = __cgroup_rstat_trylock(cgrp, -1); >> +    if (!locked) { >> +        /* Opportunity to ongoing flush detection */ >> +        __cgroup_rstat_lock(cgrp, -1, false); >> +    } >> + >>       cgroup_rstat_flush_locked(cgrp); >>       __cgroup_rstat_unlock(cgrp, -1); >>   } >> @@ -368,7 +388,7 @@ void cgroup_rstat_flush_hold(struct cgroup *cgrp) >>       __acquires(&cgroup_rstat_lock) >>   { >>       might_sleep(); >> -    __cgroup_rstat_lock(cgrp, -1); >> +    __cgroup_rstat_lock(cgrp, -1, true); >>       cgroup_rstat_flush_locked(cgrp); >>   } >> >> > Will it be cleaner to add a "bool *flushed" output parameter to > __cgroup_rstat_lock() so that the caller can respond differently > whether the flushed flag is set or not? In that way, you don't need to > expose a separate trylock() API. Also your commit log is empty. Looking at the use case in patch 2, I would suggest the following APIs. - bool cgroup_rstat_lock(struct cgroup *cgrp) - bool cgroup_rstat_lock_or_flushed(struct cgroup *cgrp) Both will return a bool indicating a flush has already been done if true. The 2nd function will not take the lock if a flush is ongoing. Both will wait if a flush is ongoing. Cheers, Longman