Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp3468390ioo; Wed, 25 May 2022 00:59:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyrTLUo4js3C9LtZTPQkbusnooxQv2ZMxAzo5QoNgVmc81qKW/CvcGo2YwgVZIen1vtzoBv X-Received: by 2002:a17:907:2d86:b0:6ff:14d1:b29d with SMTP id gt6-20020a1709072d8600b006ff14d1b29dmr1009027ejc.17.1653465546977; Wed, 25 May 2022 00:59:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653465546; cv=none; d=google.com; s=arc-20160816; b=js0bFEZL/o7VtnXTsdQQ6NiOHePiGQga8Lmf4UpuwvGxqqpqDeZ+7sHSKn839FZvcX S1ssUBel3TXyGCDs/frIWcu4GAac4KhnLcLScaQFyVkFH8lCAyjoDWT9uRa9twWatjLu IBNp3/Qj7ZOQHV5GPlNOC/nmZsMnRQpjkWiCBoOZnBC2aFwsmvEi9dxkzuv0S86Dg/ny vC2aDLn/KzqZfloxPrQ8xswFihew00Kec/Mws9ZV4jcddraEXahdf3tEbIoaRDm0VF7V Lg/FcxBYaYdPHRgO3iR3F86pmipLLL0F9WTWHYziX1BLS+hKVRKxwT7VrTAhr7aMDfIF X/vA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=p8BgjPR75xXU784y1E9IpwjBGFVwhDEOmbSXQMTZJNI=; b=mRQLCpjDQOOmlfzgxOPwC83Szq2YLgIn/zmJqfgqIdNJ+vQshpsc5ooZNxlp/Llfxv n2glKp3sqbNESsN1D4urK+pa1V0iSOjMXsa59+nXUIVf9ZzuZ129rZ0Vc3fqpDISo3lN sX22CWjYxnGBV9NIS1ZQkF9sELwdhfHpEs1zCu3mJw3iWnjpwHFJ15iWOjbITUmbRaab Rp5OUpzDMDBqroEHeGlfIvAQmBQXoLO+uHGLHBWR8yv50snjK3OncJoVWxh4EwpTeNkr v/Dfm1MIN3ZO33z+2MgK3x/8jvb7X1FgIrG6/mQkW54ghbKToyOf5HByGDiW8+QewCim d+KA== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e4-20020a17090658c400b006e8c412ccc4si16135662ejs.296.2022.05.25.00.58.40; Wed, 25 May 2022 00:59:06 -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; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237005AbiEXLsV (ORCPT + 99 others); Tue, 24 May 2022 07:48:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232178AbiEXLsU (ORCPT ); Tue, 24 May 2022 07:48:20 -0400 Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D68F722BCA; Tue, 24 May 2022 04:48:19 -0700 (PDT) Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-30007f11f88so47080867b3.7; Tue, 24 May 2022 04:48:19 -0700 (PDT) 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; bh=p8BgjPR75xXU784y1E9IpwjBGFVwhDEOmbSXQMTZJNI=; b=L5klvGbovapfMhJKruKmDk0BCWvGQ8WlFGPKz8bw2sGbvTl5IRGS1aEd2chI8EcF50 kXBua6Iul2VWw5tktT1o5iY0kZk7XKRQfzeQVY76Dt4wYpwmiieszPcEiOiQjtZ3B3nY DTPHNzUfcaCMCCJ9AQqqJtjk2PswDfxCGDhENW6+JwL+P4T7fOht14bYl7RyP9b0xptm DvwCl0TZpUlGtpX7Dzdk5kJWlh6vzxXsRCx8TfH9zglf08m0IUowDmmia56QWHigNH1y s+Qv5zskbkUqamneWOzDtZgid8aJSPFGJi0Eyilrq1z/0fFGIpg9m1SpMcgF0hb8H51p JmxQ== X-Gm-Message-State: AOAM533+GUHRWZIeEKh1RhjkUfMhahFoYnL3/nkdN+Z/2URs/qvisK7D gx5qLB0ijaAqXGBFqC/sBOgJXL6nRWpP9M9i0UxO602K X-Received: by 2002:a81:91d4:0:b0:2fe:e300:3581 with SMTP id i203-20020a8191d4000000b002fee3003581mr27973637ywg.7.1653392899108; Tue, 24 May 2022 04:48:19 -0700 (PDT) MIME-Version: 1.0 References: <20220511043515.fn2gz6q3kcpdai5p@vireshk-i7> <20220511122114.wccgyur6g3qs6fps@vireshk-i7> <20220512065623.q4aa6y52pst3zpxu@vireshk-i7> <20220513042705.nbnd6vccuiu6lb7a@vireshk-i7> <20220524111456.hw4qugsvt4bm7reh@vireshk-i7> <20220524112917.apcvvvblksg7jdu4@vireshk-i7> In-Reply-To: <20220524112917.apcvvvblksg7jdu4@vireshk-i7> From: "Rafael J. Wysocki" Date: Tue, 24 May 2022 13:48:08 +0200 Message-ID: Subject: Re: [PATCH v3] cpufreq: fix race on cpufreq online To: Viresh Kumar Cc: "Rafael J. Wysocki" , Schspa Shi , Linux Kernel Mailing List , Linux PM Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, 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 On Tue, May 24, 2022 at 1:29 PM Viresh Kumar wrote: > > On 24-05-22, 13:22, Rafael J. Wysocki wrote: > > On Tue, May 24, 2022 at 1:15 PM Viresh Kumar wrote: > > > > > > On 13-05-22, 09:57, Viresh Kumar wrote: > > > > On 12-05-22, 12:49, Rafael J. Wysocki wrote: > > > > > > > Moreover, I'm not sure why the locking dance in store() is necessary. > > > > > > > > > > > > commit fdd320da84c6 ("cpufreq: Lock CPU online/offline in cpufreq_register_driver()") > > > > > > > > > > I get that, but I'm wondering if locking CPU hotplug from store() is > > > > > needed at all. I mean, if we are in store(), we are holding an active > > > > > reference to the policy kobject, so the policy cannot go away until we > > > > > are done anyway. Thus it should be sufficient to use the policy rwsem > > > > > for synchronization. > > > > > > > > I think after the current patchset is applied and we have the inactive > > > > policy check in store(), we won't required the dance after all. > > > > > > I was writing a patch for this and then thought maybe look at mails > > > around this time, when you sent the patch, and found the reason why we > > > need the locking dance :) > > > > > > https://lore.kernel.org/lkml/20150729091136.GN7557@n2100.arm.linux.org.uk/ > > Actually no, this is for the lock in cpufreq_driver_register(). > > > Well, again, if we are in store(), we are holding a reference to the > > policy kobject, so this is not initialization time. > > This is the commit which made the change. > > commit 4f750c930822 ("cpufreq: Synchronize the cpufreq store_*() routines with CPU hotplug") So this was done before the entire CPU hotplug rework and it was useful at that time. The current code always runs cpufreq_set_policy() under policy->rwsem and governors are stopped under policy->rwsem, so this particular race cannot happen AFAICS. Locking CPU hotplug prevents CPUs from going away while store() is running, but in order to run store(), the caller must hold an active reference to the policy kobject. That prevents the policy from being freed and so policy->rwsem can be acquired. After policy->rwsem has been acquired, policy->cpus can be checked to determine whether or not there are any online CPUs for the given policy (there may be none), because policy->cpus is only manipulated under policy->rwsem. If a CPU that belongs to the given policy is going away, cpufreq_offline() has to remove it from policy->cpus under policy->rwsem, so either it has to wait for store() to release policy->rwsem, or store() will acquire policy->rwsem after it and will find that policy->cpus is empty.