Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp4463813pxb; Sat, 6 Nov 2021 14:36:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyFHt4iop1s435xPDW5JSWLUISsTOCeSATj0c06lZxu0tzCayc7oyEL9TMp/COq+6oYuEp8 X-Received: by 2002:a17:906:2599:: with SMTP id m25mr84685918ejb.302.1636234561284; Sat, 06 Nov 2021 14:36:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1636234561; cv=none; d=google.com; s=arc-20160816; b=pr7RFnMYF1Hvp+dpBkjVfh19dKRIqn0kUHK4IEu+h9e8bKP2DyQPdOVksqFITaOr1e CMiSHYOPvU0q9yXGNeqCJbsJNzA0vYA3SfqoxGzGgXKWuiod43Uy4VjbgBSawzGveSt2 EIIJWxstErm1iiQ9p9/6YzFEvqjfohqLe5M6e62m7cz2jkBzvWGICOfmppRO9Vc5jIiz YXFMqUa8iKg7h4oadYvESSjgpNTLjPsA+X/tTWHoYRGi5IRnsE2luIo1+h/LXRex/bBN EE0GrhanPz3zgfPk86XAtwskW8PK296R1UK826GGNznARBl9LOXxYRxjAM9AMBdQ5fB+ IbPQ== 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:dkim-signature:date; bh=XMmhs9HYhNcGgQ65VphvZRVIvAdwsAifoT5Xiz9YzPo=; b=JFhYAnWEPNB9cvWUVJERuD4P1JVqGHKWcFQ/xS5H5jZ7hgS1tQE05hBoUV+91pB5nH 6+dC6MAdsE/8YKkwzAh1Z8YkaTlvbLP0tky2ry2mSntJkQxXRvxwPV+Zo4zf1cV08NXL ExP7uavsZViCuLxiZxea3sXxn5VQ0eYJ5/zsQ+6Bv0ODHeqpWcplreH5fnQ7KOVoOiJd F+OPH+7wzs1qGGI0R+2fyCP9V9NPLxALfdoXAs5MpPvfAgGVgvaSqQ3jaoFhU1yl5F5d m+HiNHIUWW2fr8shpm0LqhFtxjZXXCn6EaW7zJgVims4alXxGMH9qer3R1MyJ9oElTKt v/LA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=gcXkaPyb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cb8si16347688edb.352.2021.11.06.14.35.38; Sat, 06 Nov 2021 14:36:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=gcXkaPyb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233357AbhKFRWc (ORCPT + 99 others); Sat, 6 Nov 2021 13:22:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229551AbhKFRWb (ORCPT ); Sat, 6 Nov 2021 13:22:31 -0400 Received: from out1.migadu.com (out1.migadu.com [IPv6:2001:41d0:2:863f::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B72A5C061570; Sat, 6 Nov 2021 10:19:49 -0700 (PDT) Date: Sun, 7 Nov 2021 01:20:26 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1636219186; 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=XMmhs9HYhNcGgQ65VphvZRVIvAdwsAifoT5Xiz9YzPo=; b=gcXkaPyby+FXI8KETJ5qGraTykKZH6YXSiFsHLqtJ3Xv528zpUisCBfyGFglMTVL6FWOPT gUzrN+hHi/CBchqgxFh1QGzQ6ZiIfbkhhMPtbDz39EjUuS2KYELg2Rkv6mJVp32TYmW3uF ubdvRSbonJ8tGxOrXP4Niobp7BWOolM= 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 3/6] sched/umcg: implement UMCG syscalls Message-ID: References: <20211104195804.83240-1-posk@google.com> <20211104195804.83240-4-posk@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211104195804.83240-4-posk@google.com> X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: tao.zhou@linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Peter, On Thu, Nov 04, 2021 at 12:58:01PM -0700, Peter Oskolkov wrote: > +/** > + * umcg_update_state: atomically update umcg_task.state_ts, set new timestamp. > + * @state_ts - points to the state_ts member of struct umcg_task to update; > + * @expected - the expected value of state_ts, including the timestamp; > + * @desired - the desired value of state_ts, state part only; > + * @may_fault - whether to use normal or _nofault cmpxchg. > + * > + * The function is basically cmpxchg(state_ts, expected, desired), with extra > + * code to set the timestamp in @desired. > + */ > +static int umcg_update_state(u64 __user *state_ts, u64 *expected, u64 desired, > + bool may_fault) > +{ > + u64 curr_ts = (*expected) >> (64 - UMCG_STATE_TIMESTAMP_BITS); > + u64 next_ts = ktime_get_ns() >> UMCG_STATE_TIMESTAMP_GRANULARITY; > + > + /* Cut higher order bits. */ > + next_ts &= UMCG_TASK_STATE_MASK_FULL; next_ts &= (1 << UMCG_STATE_TIMESTAMP_BITS) - 1; or am I wrong. > + if (next_ts == curr_ts) > + ++next_ts; > + > + /* Remove an old timestamp, if any. */ > + desired &= UMCG_TASK_STATE_MASK_FULL; > + > + /* Set the new timestamp. */ > + desired |= (next_ts << (64 - UMCG_STATE_TIMESTAMP_BITS)); > + > + if (may_fault) > + return cmpxchg_user_64(state_ts, expected, desired); > + > + return cmpxchg_user_64_nofault(state_ts, expected, desired); > +} > + > +/** > + * sys_umcg_ctl: (un)register the current task as a UMCG task. > + * @flags: ORed values from enum umcg_ctl_flag; see below; > + * @self: a pointer to struct umcg_task that describes this > + * task and governs the behavior of sys_umcg_wait if > + * registering; must be NULL if unregistering. > + * > + * @flags & UMCG_CTL_REGISTER: register a UMCG task: > + * UMCG workers: > + * - @flags & UMCG_CTL_WORKER > + * - self->state must be UMCG_TASK_BLOCKED > + * UMCG servers: > + * - !(@flags & UMCG_CTL_WORKER) > + * - self->state must be UMCG_TASK_RUNNING > + * > + * All tasks: > + * - self->next_tid must be zero > + * > + * If the conditions above are met, sys_umcg_ctl() immediately returns > + * if the registered task is a server; a worker will be added to > + * idle_workers_ptr, and the worker put to sleep; an idle server > + * from idle_server_tid_ptr will be woken, if present. > + * > + * @flags == UMCG_CTL_UNREGISTER: unregister a UMCG task. If the current task > + * is a UMCG worker, the userspace is responsible for waking its > + * server (before or after calling sys_umcg_ctl). > + * > + * Return: > + * 0 - success > + * -EFAULT - failed to read @self > + * -EINVAL - some other error occurred > + */ > +SYSCALL_DEFINE2(umcg_ctl, u32, flags, struct umcg_task __user *, self) > +{ > + struct umcg_task ut; > + > + if (flags == UMCG_CTL_UNREGISTER) { > + if (self || !current->umcg_task) > + return -EINVAL; > + > + if (current->flags & PF_UMCG_WORKER) > + umcg_handle_exiting_worker(); > + else > + umcg_clear_task(current); > + > + return 0; > + } > + > + if (!(flags & UMCG_CTL_REGISTER)) > + return -EINVAL; > + > + flags &= ~UMCG_CTL_REGISTER; > + if (flags && flags != UMCG_CTL_WORKER) > + return -EINVAL; > + > + if (current->umcg_task || !self) > + return -EINVAL; > + > + if (copy_from_user(&ut, self, sizeof(ut))) > + return -EFAULT; > + > + if (ut.next_tid) > + return -EINVAL; > + > + if (flags == UMCG_CTL_WORKER) { > + if ((ut.state_ts & UMCG_TASK_STATE_MASK_FULL) != UMCG_TASK_BLOCKED) Or use UMCG_TASK_STATE_MASK that is enough. > + return -EINVAL; > + > + WRITE_ONCE(current->umcg_task, self); > + current->flags |= PF_UMCG_WORKER; > + > + /* Trigger umcg_handle_resuming_worker() */ > + set_tsk_thread_flag(current, TIF_NOTIFY_RESUME); > + } else { > + if ((ut.state_ts & UMCG_TASK_STATE_MASK_FULL) != UMCG_TASK_RUNNING) The same here. > + return -EINVAL; > + > + WRITE_ONCE(current->umcg_task, self); > + } > + > + return 0; > +} > + Thanks, Tao