Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp1376750rwb; Thu, 19 Jan 2023 09:48:09 -0800 (PST) X-Google-Smtp-Source: AMrXdXs3YG+XNPFKDK69lQk/6jwM3XxhSyFnvSg4kPNeVLtWk3UdntzyqyBWn9tjxPN3b4cIHQvS X-Received: by 2002:a17:90a:7804:b0:229:912:1340 with SMTP id w4-20020a17090a780400b0022909121340mr11466166pjk.39.1674150488976; Thu, 19 Jan 2023 09:48:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674150488; cv=none; d=google.com; s=arc-20160816; b=hjfIAz+Y4y24Wy2A723nsNUojeRsyWvuPD1GGCUcLx7VeghRdhaN+H/GYq4wq+wsNc vSjtHHNF5U5S5qX8kNKNWJNhmkfJkGEw9+pz5PF4EvL9+UN2BWbFtXW9MDcrfGXUhrqx zeBdFtbE+t5HX2mKXfbyDO4gKEzO9RIWTCFPS8q88g+e+Vm35S/0ky2GnurE4BfjPlR7 HIkQqCSNNZesN1cjLicfTK/M0GQsrd4MyesZ5kKv2cR0pe2h0A9SPDb+9D3hfZPLAMr1 NU/aTS2EzMiNVGPf0NfCRrwmw1zhrB1zy3posye5F7OE/4PfIsBDxyeXTH5URI8d58JI chnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=lzcfiNQQ19ujGHIOiK8wrI7ssn6uzK8wgFdieWJfOj4=; b=RowzpIEClKkSabVT51gf9aNKqXATo3i7yg6nTqfUva5IBNOD7xvIDmD5rv+du0Z9P0 owlZxMeEACfHQwmWPaHl1QsM78+XyyrWRYcq0+61Pv/SaSygMi1/qZXE+hwZu8yfRQNH uPem/VjtWeoupPXB+rU325HacngeeerNevKu+qYAZibrPU8Cw8aR588sWw6BNSkIW0eg ZlHe/sCnz4TreA/ofqF7O7POUBfpXWvCMcCCCjP9CCT0DIFE6dDNnPh/eXR6ny9+DwuH zMAbx9naB3uRVwLVipkBlgDy4aD3ZVokYLj6HDAqSUEBoXHMXIhoR5I+HlivXzpMUl+y tiRw== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v5-20020a17090a0c8500b00229a8c10f51si5302368pja.163.2023.01.19.09.48.03; Thu, 19 Jan 2023 09:48:08 -0800 (PST) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229614AbjASRlj (ORCPT + 45 others); Thu, 19 Jan 2023 12:41:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230197AbjASRlX (ORCPT ); Thu, 19 Jan 2023 12:41:23 -0500 Received: from outbound-smtp57.blacknight.com (outbound-smtp57.blacknight.com [46.22.136.241]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C55EF8BAB7 for ; Thu, 19 Jan 2023 09:41:05 -0800 (PST) Received: from mail.blacknight.com (pemlinmail01.blacknight.ie [81.17.254.10]) by outbound-smtp57.blacknight.com (Postfix) with ESMTPS id C8CA1FABCD for ; Thu, 19 Jan 2023 17:41:03 +0000 (GMT) Received: (qmail 12993 invoked from network); 19 Jan 2023 17:41:03 -0000 Received: from unknown (HELO techsingularity.net) (mgorman@techsingularity.net@[84.203.198.246]) by 81.17.254.9 with ESMTPSA (AES256-SHA encrypted, authenticated); 19 Jan 2023 17:41:03 -0000 Date: Thu, 19 Jan 2023 17:41:01 +0000 From: Mel Gorman To: Sebastian Andrzej Siewior Cc: Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Davidlohr Bueso , Linux-RT , LKML Subject: Re: [PATCH v2] locking/rwbase: Prevent indefinite writer starvation Message-ID: <20230119174101.rddtxk5xlamlnquh@techsingularity.net> References: <20230117083817.togfwc5cy4g67e5r@techsingularity.net> <20230117165021.t5m7c2d6frbbfzig@techsingularity.net> <20230118173130.4n2b3cs4pxiqnqd3@techsingularity.net> <20230119110220.kphftcehehhi5l5u@techsingularity.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS 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 Thu, Jan 19, 2023 at 05:28:48PM +0100, Sebastian Andrzej Siewior wrote: > On 2023-01-19 11:02:20 [+0000], Mel Gorman wrote: > > > - Once the writer removes READER_BIAS, it forces the reader into the > > > slowpath. > > > > Removed in __rwbase_write_trylock IIUC > > And added back in case try trylock failed via __rwbase_write_unlock(). > The RTmutex is unlocked and the READER_BIAS is "returned". > Indeed. > > > At that time the writer does not own the wait_lock meaning > > > the reader _could_ check the timeout before writer had a chance to set > > > it. The worst thing is probably that if jiffies does not have the > > > highest bit set then it will always disable the reader bias here. > > > The easiest thing is probably to check timeout vs 0 and ensure on the > > > writer side that the lowest bit is always set (in the unlikely case it > > > will end up as zero). > > > > > > > I am missing something important. On the read side, we have > > > > Look at this side by side: > > writer reader > > | static int __sched rwbase_write_lock(struct rwbase_rt *rwb, > | unsigned int state) > | { > | /* Force readers into slow path */ > | atomic_sub(READER_BIAS, &rwb->readers); > > > | static int __sched __rwbase_read_lock(struct rwbase_rt *rwb, > | unsigned int state) > | { > | struct rt_mutex_base *rtm = &rwb->rtmutex; > | int ret; > | > | raw_spin_lock_irq(&rtm->wait_lock); > > Reader has the lock, writer will wait. > > | /* > | * Allow readers, as long as the writer has not completely > | * acquired the semaphore for write. > | */ > | if (atomic_read(&rwb->readers) != WRITER_BIAS) { > > here, the timeout value is not yet populated by the writer so the reader > compares vs 0. > > | atomic_inc(&rwb->readers); > | raw_spin_unlock_irq(&rtm->wait_lock); > | return 0; > | } > | > > | raw_spin_lock_irqsave(&rtm->wait_lock, flags); > | if (__rwbase_write_trylock(rwb)) > | goto out_unlock; > | > > Hope this makes it easier. > Yes, it makes your concern much clearer but I'm not sure it actually matters in terms of preventing write starvation or in terms of correctness. At worst, a writer is blocked that could have acquired the lock during a tiny race but that's a timing issue rather than a correctness issue. Lets say the race hits reader sees waiter_timeout == 0 writer acquires wait_lock __rwbase_write_trylock fails update waiter_timeout rwbase_schedule Each reader that hits the race goes ahead at a point in time but anything readers after that observe the timeout and eventually the writer goes ahead. If the waiter_timeout was updated before atomic_sub(READER_BIAS), it doesn't close the race as atomic_sub is unordered so barriers would also be needed and clearing of waiter_timeout moves to out_unlock in case __rwbase_write_trylock succeeds. That's possible but the need for barriers makes it more complicated than is necessary. The race could be closed by moving wait_lock acquisition before the atomic_sub in rwbase_write_lock() but it expands the scope of the wait_lock and I'm not sure that's necessary for either correctness or preventing writer starvation. It's a more straight-forward fix but expanding the scope of a lock unnecessarily has been unpopular in the past. I think we can close the race that concerns you but I'm not convinced we need to and changing the scope of wait_lock would need a big comment and probably deserves a separate patch. Sorry if I'm still missing something stupid and thanks for your patience reviewing this. -- Mel Gorman SUSE Labs