Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3406934imu; Mon, 14 Jan 2019 02:24:43 -0800 (PST) X-Google-Smtp-Source: ALg8bN6qOnln5L35htN/LMBFDbfGinPVbGRof9LeAGnLfmx/Po5JGNIxWgPW2e3Y2hJgsos7bOfk X-Received: by 2002:a63:d846:: with SMTP id k6mr22611389pgj.251.1547461483370; Mon, 14 Jan 2019 02:24:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547461483; cv=none; d=google.com; s=arc-20160816; b=k90jt7cB7yYfKA5jT0BHyMdle9siH7VRgtYT4qVO0Vokr4Bu1Au3Nbxp/KKbmLLOzx B24Mb0dj/y5zmttE5eADdgzgPcGGQlZehAE5oo3nMDSfIN4jbCM6sj15Tahw8nLLULyj vboC7sTPvSSzM/1ZLgpgYtCAoIcyhfN4UO8VbFyzaXsJOCvPu74ky/5ugrwrlpszmsIj +cSfB5nzD1BCYFBuw27gcjw5EwYDo1rMtrVNw8r3O6b3QlKEy0aIH3ELCwFP3UzifAL8 tBx8W7wq7WWDoI4LN5lOaDMf2xbn+Q061H79XjShGNWqgGyC4MXfsDnV2m6ElCwqAZn4 kCGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=bxkTnJx2ceAX8K5kersQk7t4n/UQEPJI29wphVIz808=; b=nLuqjkr9kPk2V99tbYbz9yrylU6+VK2g63HuDJL2y6qLNzVIcwXSuAKowX6B8rlRjD huydzd8LxeBuHXcNGHFUcgSIFHrcej65zXoczzm4acomw8kEIJ9h5e2TEO0BY7DKwQP1 hIW1B6/e0d7vNolVhY/cYtZ2FaYJ98mImAVSlCmz9mO8lMWPKgX5ui2ntsZu5eC71s6F 5KzefIfq4/1/x9duX5JGHoW+81yxM0iNzRICO7TTyu0uifzSZhh1gSoQsVJ4xfBWKz9v 4zWj57lkKgfkNZqfy4ullyfDXZ0nLKEt/aZHWuJZGdF2Z8eRinL+HqfUEed91mvZYwin nPfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=iTx0Df8c; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y10si10196pgp.348.2019.01.14.02.24.27; Mon, 14 Jan 2019 02:24:43 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=iTx0Df8c; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726559AbfANKWg (ORCPT + 99 others); Mon, 14 Jan 2019 05:22:36 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:39318 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726306AbfANKWf (ORCPT ); Mon, 14 Jan 2019 05:22:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=bxkTnJx2ceAX8K5kersQk7t4n/UQEPJI29wphVIz808=; b=iTx0Df8cQ26qLeQXkOAlcgl6R S2nzh31CdDuB970VvEDUWiwBW5xA1/4XMZRIYQfWaBXIPctv1Oh8o6eORzQ2NCcZ2aiakvHxPtKov QDaWujMi7FJzuy52oerT/t1DRQ3sWQg99IplBuXO9NjqenPNZWhIS8q4lyyHrAly3odk8jk71RfYM VNuYVniepN5psaUaTbiQbXavkl44rAV3gREv6vZpgTgTluXk+FJfFBDrfHuj5yAjuy+J9JvqpiwGk hj3HI3zuIMkCDWk4AbOLKye8cp/bmldHJz5YUVqVAM7C5JvklIYE6Lft5Djf6BJIaCXfyjqaqYuD0 JiVLSdVbQ==; Received: from [89.200.46.118] (helo=worktop.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1gizNy-0000ZP-4j; Mon, 14 Jan 2019 10:22:26 +0000 Received: by worktop.programming.kicks-ass.net (Postfix, from userid 1000) id 7E89C981766; Mon, 14 Jan 2019 11:21:37 +0100 (CET) Date: Mon, 14 Jan 2019 11:21:37 +0100 From: Peter Zijlstra To: Suren Baghdasaryan Cc: gregkh@linuxfoundation.org, tj@kernel.org, lizefan@huawei.com, hannes@cmpxchg.org, axboe@kernel.dk, dennis@kernel.org, dennisszhou@gmail.com, mingo@redhat.com, akpm@linux-foundation.org, corbet@lwn.net, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH v2 5/5] psi: introduce psi monitor Message-ID: <20190114102137.GB14054@worktop.programming.kicks-ass.net> References: <20190110220718.261134-1-surenb@google.com> <20190110220718.261134-6-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190110220718.261134-6-surenb@google.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jan 10, 2019 at 02:07:18PM -0800, Suren Baghdasaryan wrote: > +/* > + * psi_update_work represents slowpath accounting part while > + * psi_group_change represents hotpath part. > + * There are two potential races between these path: > + * 1. Changes to group->polling when slowpath checks for new stall, then > + * hotpath records new stall and then slowpath resets group->polling > + * flag. This leads to the exit from the polling mode while monitored > + * states are still changing. > + * 2. Slowpath overwriting an immediate update scheduled from the hotpath > + * with a regular update further in the future and missing the > + * immediate update. > + * Both races are handled with a retry cycle in the slowpath: > + * > + * HOTPATH: | SLOWPATH: > + * | > + * A) times[cpu] += delta | E) delta = times[*] > + * B) start_poll = (delta[poll_mask] &&| if delta[poll_mask]: > + * cmpxchg(g->polling, 0, 1) == 0)| F) polling_until = now + > + * | grace_period > + * | if now > polling_until: > + * if start_poll: | if g->polling: > + * C) mod_delayed_work(1) | G) g->polling = polling = 0 > + * else if !delayed_work_pending(): | H) goto SLOWPATH > + * D) schedule_delayed_work(PSI_FREQ)| else: > + * | if !g->polling: > + * | I) g->polling = polling = 1 > + * | J) if delta && first_pass: > + * | next_avg = calculate_averages() > + * | if polling: > + * | next_poll = poll_triggers() > + * | if (delta && first_pass) || polling: > + * | K) mod_delayed_work( > + * | min(next_avg, next_poll)) > + * | if !polling: > + * | first_pass = false > + * | L) goto SLOWPATH > + * > + * Race #1 is represented by (EABGD) sequence in which case slowpath > + * deactivates polling mode because it misses new monitored stall and hotpath > + * doesn't activate it because at (B) g->polling is not yet reset by slowpath > + * in (G). This race is handled by the (H) retry, which in the race described > + * above results in the new sequence of (EABGDHEIK) that reactivates polling > + * mode. > + * > + * Race #2 is represented by polling==false && (JABCK) sequence which > + * overwrites immediate update scheduled at (C) with a later (next_avg) update > + * scheduled at (K). This race is handled by the (L) retry which results in the > + * new sequence of polling==false && (JABCKLEIK) that reactivates polling mode > + * and reschedules next polling update (next_poll). > + * > + * Note that retries can't result in an infinite loop because retry #1 happens > + * only during polling reactivation and retry #2 happens only on the first > + * pass. Constant reactivations are impossible because polling will stay active > + * for at least grace_period. Worst case scenario involves two retries (HEJKLE) > + */ I'm having a fairly hard time with this. There's a distinct lack of memory ordering, and a suspicious mixing of atomic ops (cmpxchg) and regular loads and stores (without READ_ONCE/WRITE_ONCE even). Please clarify. (also, you look to have a whole bunch of line-breaks that are really not needed; concattenated the line would not be over 80 chars).