Received: by 2002:a19:f614:0:0:0:0:0 with SMTP id x20csp60889lfe; Fri, 15 Apr 2022 19:31:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxH/SRJlg/vp9MNKMk5lrja0fwEmFTUQtD7iW47aNZSz0xtyoEaLlexGOPT7RR5/kfRkxLR X-Received: by 2002:a17:903:2282:b0:158:b827:773a with SMTP id b2-20020a170903228200b00158b827773amr1556499plh.110.1650076302957; Fri, 15 Apr 2022 19:31:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650076302; cv=none; d=google.com; s=arc-20160816; b=kUhbLB0Zl9S8DA0VoEU8jutfk9xByFaXT1qckoE8FlED4YsRM3oqEnI34JuEGtVNaK NFxhMKEgqQrqfKcJAqdwy/b/RCeluGRiQK8b/Hix1Plo91krrf7ooUP42lKFjB/pC5RO UECZWE5QasYWxdBMZ/FbAW0UQLh5Kb5CBc/Yt0ZQr1iyaTL2x2B0zQc/zUceXrak9Onh bNdNnBAsjB7VMHg5OUWK9vz8p5+Xrs0VTunPIEbkYzZXek/jVakid7FYXEaSynvwfnfc 8HP6JJaALtrOJYssYwGwFnfWA+NBTN3wj2MUcPQpXm/Gjvy1m/mb4ERMzkLyLwwX3rji 4aKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=NUpVA5eYOb2M8t4G0qrVj0JSOzxiO0fCAejkOocIXao=; b=Olrz/B8WMuw7RsojFkm7IvpzZ1r0w4BsY2Fxn0sAOZpc48Wo7LNYYfk51oe3uspoMO YqFhzGM6p6V2nHiOHgR0OwUuJKDkcq9tP2VlucXqzihSPhUvIOMnHBKHr1+BMPY6uBMf YC+THByZnLUPX/VlpUBdttCOgjZiBt6p9vsAxjkVlhflc858NdGnnsMDfPuulRvvEUWh 8wGQmkep5moWHrQW5jbAMpJ7jECqbpmFrhjMrzt9rQo9iGjQONbjjWK+CLgj4SPXw4xn 5QvDaIOlFK0hPw8cO02oK5ULDMyfTkYERSLZdGLEWQs1S1Q0VvApYkxM4UClh2wajJbh VrRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Gpc5QjXr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id c12-20020a170902b68c00b00156ef408753si2484801pls.238.2022.04.15.19.31.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 19:31:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Gpc5QjXr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0447D125C80; Fri, 15 Apr 2022 18:41:44 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242346AbiDNREz (ORCPT + 99 others); Thu, 14 Apr 2022 13:04:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240302AbiDNRDn (ORCPT ); Thu, 14 Apr 2022 13:03:43 -0400 Received: from mail-vs1-xe2d.google.com (mail-vs1-xe2d.google.com [IPv6:2607:f8b0:4864:20::e2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A3ED972BD; Thu, 14 Apr 2022 09:40:03 -0700 (PDT) Received: by mail-vs1-xe2d.google.com with SMTP id k17so5054764vsq.0; Thu, 14 Apr 2022 09:40:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=NUpVA5eYOb2M8t4G0qrVj0JSOzxiO0fCAejkOocIXao=; b=Gpc5QjXr+gtvBrwu7vWEx+QgDTN+050AGC7nmF62hwXczPBWzSoA9w+knrsKgpTcgk 8oa3CY/ypOG50jZJ572CW7fZW/rm647lLzGc9pwMBpmScXddg/wDSXYF60ScPb6tOdo3 tiWaBYP/19QHNemVehQ+q3FbXzHyQBS7oSlDwg8C89KYWJo7Ct4lEu9vnzd/UDSCWV+j Gz8njvyp8lJTmOQyjvlOrfjbhWAb7KaN5ye2exn60ZWWIt3yk6MGxnlZ59N9BIPf7ETO ejvwjhrl7fQh62xrW7q24AZtsJc2pbNRYblgKWT1uiyH7/05ialXUCpzQBxn5XwHHnSw kNxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=NUpVA5eYOb2M8t4G0qrVj0JSOzxiO0fCAejkOocIXao=; b=w+vrgJWrrkK9lDSXoCY7RSXIBzKcIRzgEhW1HeObqfzv5kqnKTlUAdWruNY46231cC 3Aby9JXpr8gsI1iGWO5jfV9qlXlYvnC0SJUFyBrwDXqah8l2vckW1g8p7vGwFeH+vHTp lNlT8sE80InOAzcer/x5MNyWyRMW4siBuroVdXOGKgP7ZRSWS9/JksWv70pPO1h8joC7 qeY1duzxGHUuD26EHH1u/Z3BCDV19jV6UsrMuWQOOXRZpSe+lB6tiAAzRO9tluk6Ntr2 xKaVIj6h1RCInsxNs7XQgWZH1uaUWTXzakctMSl6d9ME8SNy7cDoVUtlMJY7u7s0tSAK 3SZA== X-Gm-Message-State: AOAM532SZHcEOvyTEkGBEGj9kx8CEhAHa/GzVVkgW4CRFPyTC8pNGLFi cAsGdCqScsxR6REXLUm6Ciq+j5tiv9iC9TH9Pq0FIw+bt8gRMQ== X-Received: by 2002:a67:a44d:0:b0:320:601b:2a08 with SMTP id p13-20020a67a44d000000b00320601b2a08mr1644691vsh.70.1649954402542; Thu, 14 Apr 2022 09:40:02 -0700 (PDT) MIME-Version: 1.0 References: <20220411135136.GG15609@suse.cz> <20220411155540.36853-1-schspa@gmail.com> <09c2a9ce-3b04-ed94-1d62-0e5a072b9dac@suse.com> In-Reply-To: From: Schspa Shi Date: Fri, 15 Apr 2022 00:39:49 +0800 Message-ID: Subject: Re: [PATCH v2] btrfs: zstd: use spin_lock in timer callback To: Nikolay Borisov Cc: dsterba@suse.cz, clm@fb.com, dsterba@suse.com, Josef Bacik , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, terrelln@fb.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Nikolay Borisov writes: > On 13.04.22 =D0=B3. 19:03 =D1=87., Schspa Shi wrote: >> Nikolay Borisov writes: >> >>> On 11.04.22 =D0=B3. 18:55 =D1=87., Schspa Shi wrote: >>>> This is an optimization for fix fee13fe96529 ("btrfs: >>>> correct zstd workspace manager lock to use spin_lock_bh()") >>>> The critical region for wsm.lock is only accessed by the process conte= xt and >>>> the softirq context. >>>> Because in the soft interrupt, the critical section will not be preemp= ted by >>>> the >>>> soft interrupt again, there is no need to call spin_lock_bh(&wsm.lock)= to turn >>>> off the soft interrupt, spin_lock(&wsm.lock) is enough for this situat= ion. >>>> Changelog: >>>> v1 -> v2: >>>> - Change the commit message to make it more readable. >>>> [1] https://lore.kernel.org/all/20220408181523.92322-1-schspa@gmail.co= m/ >>>> Signed-off-by: Schspa Shi >>> >>> Has there been any measurable impact by this change? While it's correct= it does mean that >>> someone looking at the code would see that in one call site we use pl= ain spinlock and in >>> another a _bh version and this is somewhat inconsistent. >>> >> Yes, it may seem a little confused. but it's allowed to save some >> little peace of CPU times. >> and "static inline void red_adaptative_timer(struct timer_list *t) in >> net/sched/sch_red.c" >> have similar usage. >> >>> What's more I believe this is a noop since when softirqs are executing = preemptible() would >>> be false due to preempt_count() being non-0 and in the bh-disabling cod= e >>> in the spinlock we have: >>> >>> /* First entry of a task into a BH disabled section? */ >>> 1 if (!current->softirq_disable_cnt) { >>> 167 if (preemptible()) { >>> 1 local_lock(&softirq_ctrl.lock); >>> 2 /* Required to meet the RCU bottomhalf r= equirements. */ >>> 3 rcu_read_lock(); >>> 4 } else { >>> 5 DEBUG_LOCKS_WARN_ON(this_cpu_read(softir= q_ctrl.cnt)); >>> 6 } >>> 7 } >>> >>> >>> In this case we'd hit the else branch. >> We won't hit the else branch. because current->softirq_disable_cnt >> won't be zero in the origin case. >> __do_softirq(void) >> softirq_handle_begin(void) >> __local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET); >> current->softirq_disable_cnt will be > 0 at thi= s time. > > That's only relevant on CONFIG_PREEMPT_RT though, on usual kernels > softirq_handle_being is empty. Furthermore, in case of the non-preempt > rt if preemptible() always returns false this means that even in the > __do_softirq path we'll never increment softirq_disable_cnt. So if > anything this change is only beneficial (theoretically at that in preempt= _rt > scenarios). > For either case, __local_bh_disable_ip will add preempt count or something = else. for CONFIG_PREEMPT_RT we have discussed, it will be OK and some beneficial. In the case of CONFIG_TRACE_IRQFLAGS: #ifdef CONFIG_TRACE_IRQFLAGS void __local_bh_disable_ip(unsigned long ip, unsigned int cnt) { unsigned long flags; WARN_ON_ONCE(in_hardirq()); raw_local_irq_save(flags); /* * The preempt tracer hooks into preempt_count_add and will break * lockdep because it calls back into lockdep after SOFTIRQ_OFFSET * is set and before current->softirq_enabled is cleared. * We must manually increment preempt_count here and manually * call the trace_preempt_off later. */ __preempt_count_add(cnt); /* * Were softirqs turned off above: */ if (softirq_count() =3D=3D (cnt & SOFTIRQ_MASK)) lockdep_softirqs_off(ip); raw_local_irq_restore(flags); if (preempt_count() =3D=3D cnt) { #ifdef CONFIG_DEBUG_PREEMPT current->preempt_disable_ip =3D get_lock_parent_ip(); #endif trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip()); } } EXPORT_SYMBOL(__local_bh_disable_ip); #endif /* CONFIG_TRACE_IRQFLAGS */ There is also __preempt_count_add(cnt), local IRQ disable. which reduces the system's corresponding speed. In another case (usual kernels): #if defined(CONFIG_PREEMPT_RT) || defined(CONFIG_TRACE_IRQFLAGS) extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt); #else static __always_inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt) { preempt_count_add(cnt); barrier(); } #endif There is preempt_count_add(cnt), and it's useless in the timer's callback. In summary: There is a benefit for all the cases to replace spin_lock_bh with spin_lock= in timer's callback. >> ...... >> zstd_reclaim_timer_fn(struct timer_list *timer) >> spin_lock_bh(&wsm.lock); >> __local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET)= ; >> if (!current->softirq_disable_cnt) { >> // this if branch won't= hit >> } >> softirq_handle_end(); >> In this case, the "__local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);" >> won't do anything useful it only >> increase softirq disable depth and decrease it in >> "__local_bh_enable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);". >> So it's safe to replace spin_lock_bh with spin_lock in a timer >> callback function. >> >> For the ksoftirqd, it's all the same. >>