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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E3994C433F5 for ; Sun, 7 Nov 2021 16:33:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5F0EF60FDA for ; Sun, 7 Nov 2021 16:33:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 5F0EF60FDA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 80A466B0078; Sun, 7 Nov 2021 11:33:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B99D6B007B; Sun, 7 Nov 2021 11:33:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6CFA76B00C7; Sun, 7 Nov 2021 11:33:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0186.hostedemail.com [216.40.44.186]) by kanga.kvack.org (Postfix) with ESMTP id 5A0476B0078 for ; Sun, 7 Nov 2021 11:33:22 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 0617B77A5A for ; Sun, 7 Nov 2021 16:33:22 +0000 (UTC) X-FDA: 78782679318.19.0FC01B5 Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf31.hostedemail.com (Postfix) with ESMTP id 34594104DE88 for ; Sun, 7 Nov 2021 16:33:11 +0000 (UTC) Date: Mon, 8 Nov 2021 00:34:00 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1636302799; 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: in-reply-to:in-reply-to:references:references; bh=RS/Nz/UG56GlGiRm7O/muf8wz5uSzD19AWx+yQ3NlEo=; b=G8+4iXdmP/uX9z5jnC2n5GBIzfBb+e/KpFD9HAleCwzk6ibUcXgsIj2a6HgqhU0YTDoi9U BYRRYTKTJBzN6G+gZ/ZwHnDh1MHVb5cX144Hvoa/14Pv1Bog9CWmp8FdYGnQoduqeg1EgN aW1XXWa/2iIafq4PyJ4LLPHtX1NKKG4= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Tao Zhou To: Peter Oskolkov Cc: Peter Zijlstra , Ingo Molnar , Thomas Gleixner , Andrew Morton , Dave Hansen , Andy Lutomirski , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Paul Turner , Ben Segall , Peter Oskolkov , Andrei Vagin , Jann Horn , Thierry Delisle , Tao Zhou Subject: Re: [PATCH v0.8 4/6] sched/umcg, lib/umcg: implement libumcg Message-ID: References: <20211104195804.83240-1-posk@google.com> <20211104195804.83240-5-posk@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211104195804.83240-5-posk@google.com> X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: tao.zhou@linux.dev X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 34594104DE88 X-Stat-Signature: k71dggconaonwp6bfk6c3jggdtpsstc3 Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=G8+4iXdm; spf=pass (imf31.hostedemail.com: domain of tao.zhou@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=tao.zhou@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1636302791-410031 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Nov 04, 2021 at 12:58:02PM -0700, Peter Oskolkov wrote: > +/* Update the state variable, set new timestamp. */ > +static bool umcg_update_state(uint64_t *state, uint64_t *prev, uint64_t next) > +{ > + uint64_t prev_ts = (*prev) >> (64 - UMCG_STATE_TIMESTAMP_BITS); > + struct timespec now; > + uint64_t next_ts; > + int res; > + > + /* > + * clock_gettime(CLOCK_MONOTONIC, ...) takes less than 20ns on a > + * typical Intel processor on average, even when run concurrently, > + * so the overhead is low enough for most applications. > + * > + * If this is still too high, `next_ts = prev_ts + 1` should work > + * as well. The only real requirement is that the "timestamps" are > + * uniqueue per thread within a reasonable time frame. > + */ > + res = clock_gettime(CLOCK_MONOTONIC, &now); > + assert(!res); > + next_ts = (now.tv_sec * NSEC_PER_SEC + now.tv_nsec) >> > + UMCG_STATE_TIMESTAMP_GRANULARITY; > + > + /* Cut higher order bits. */ > + next_ts &= ((1ULL << UMCG_STATE_TIMESTAMP_BITS) - 1); This is the right cut.. The same to the kernel side. > + > + if (next_ts == prev_ts) > + ++next_ts; > + > +#ifndef NDEBUG > + if (prev_ts > next_ts) { > + fprintf(stderr, "%s: time goes back: prev_ts: %lu " > + "next_ts: %lu diff: %lu\n", __func__, > + prev_ts, next_ts, prev_ts - next_ts); > + } > +#endif > + > + /* Remove old timestamp, if any. */ > + next &= ((1ULL << (64 - UMCG_STATE_TIMESTAMP_BITS)) - 1); > + > + /* Set the new timestamp. */ > + next |= (next_ts << (64 - UMCG_STATE_TIMESTAMP_BITS)); > + > + /* > + * TODO: review whether memory order below can be weakened to > + * memory_order_acq_rel for success and memory_order_acquire for > + * failure. > + */ > + return atomic_compare_exchange_strong_explicit(state, prev, next, > + memory_order_seq_cst, memory_order_seq_cst); > +} > + > +static void task_unlock(struct umcg_task_tls *task, uint64_t expected_state, > + uint64_t new_state) > +{ > + bool ok; > + uint64_t next; > + uint64_t prev = atomic_load_explicit(&task->umcg_task.state_ts, > + memory_order_acquire); > + > + next = ((prev & ~UMCG_TASK_STATE_MASK_FULL) | new_state) & ~UMCG_TF_LOCKED; Use UMCG_TASK_STATE_MASK instead and the other state flag can be checked. All others places that use UMCG_TASK_STATE_MASK_FULL to mask to check the task state may seems reasonable if the state flag not allowed to be set when we check that task state, otherwise use UMCG_TASK_STATE_MASK will be enough. Not sure. Thanks, Tao > + assert(next != prev); > + assert((prev & UMCG_TASK_STATE_MASK_FULL & ~UMCG_TF_LOCKED) == expected_state); > + > + ok = umcg_update_state(&task->umcg_task.state_ts, &prev, next); > + assert(ok); > +}