From patchwork Thu Sep 21 17:36:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 142922 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5146572vqi; Thu, 21 Sep 2023 14:22:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFtfoJ1TeJlxW31mTFONfZ0MB4qsWCngYiqmyAxNqNeVaUFk4pyR0AFViKjkXdMce0ng6hB X-Received: by 2002:a05:6a20:8f1c:b0:15d:5e1e:181f with SMTP id b28-20020a056a208f1c00b0015d5e1e181fmr2604743pzk.43.1695331327335; Thu, 21 Sep 2023 14:22:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695331327; cv=none; d=google.com; s=arc-20160816; b=j5zhvVE+LCvnA+HlD74xzWc2emYnShnXCyxGKoXpMTlDAP/SY69/s74dlM69ofZ9te svaxRbww710GLdsFdkMXGe/fsl00SZOxaHaZUSiwyCwK0FbQeJbHehuf64OKVOrB7gwt NNTg1Uz0pobCs9x4ngi6LQ6R7F2Jd2VpbmLTBC62cldy/vg3o83kuDTEYxuveWlTytcZ qQH0jFRfvFt3u22V9cVkrNReJkyGV+m7ZBzX6Bnxf5/+RSU7qWTv5eJBGx1AVkY3Zt/T A/fW+CXp/suRfVGqcZa7XWX+T7EGImCiOFXduO6nGF0ahtp1jr0NgDnTeCHdfkVTZfBV GOlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=BdlegKqSx1sFXYsl6OPUIPVsYZjue2Sou5wwaOjYFwE=; fh=51w4OSdjTJF7zPLz9BeYfqokcBXCUA1NOZqhQDttTbc=; b=XSdyC7wJSfqusZa8i4WeZDTZGUZYavmJp5CvRyF82TxI5jF7TL1IH4nOoMxD0JoZCh iaMai1x0BWk4gk1Y+JtPKq4gbLdak1LSaZ4FvDw03vtJBNv+VUQu55yc4OhkkqQPVOwf SVzExuHKyjy0U+Vzydt1RJEs2OuDM2FP/iwCs0TY+Fl8u1IBM+TJtmZxAbY5nRAy4TU/ l/mHy0HaBaXOI+9G0+2jOl8pieD+4X7YErc9aXkjng8mUqwQq19QCph8uLMw8D4rbirG JhH0O1fAk3LwZQOtNaj/t8Jkrj5y0f8ZXOZZFEB8o8rGpt0sZr5YLp9A0H/yuck+yr5Q L9AQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=N6Ne6cKw; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id x12-20020a170902ec8c00b001c43ff69d93si2301272plg.324.2023.09.21.14.22.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 14:22:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=N6Ne6cKw; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id DCDCE81BB0BF; Thu, 21 Sep 2023 13:37:44 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232199AbjIUUhi (ORCPT + 29 others); Thu, 21 Sep 2023 16:37:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231708AbjIUUhZ (ORCPT ); Thu, 21 Sep 2023 16:37:25 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3C1E81FFD; Thu, 21 Sep 2023 10:36:34 -0700 (PDT) Date: Thu, 21 Sep 2023 17:36:05 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1695317766; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BdlegKqSx1sFXYsl6OPUIPVsYZjue2Sou5wwaOjYFwE=; b=N6Ne6cKwyMO1FBjjffxoMh36OXm6HwtL+9ZZbTEj5vme1MTSni/wzqHsVtxk0e+On5iE3Y giAkdR93fYh9DDWjZiy/XIIfbhLPXobPq7cntEST+Vq1ZBJxUf3AId3L3pSmxdSIp4rDDU zvVnBXLZJ2KQ3eUpgke3fHhRWL82ND+FvkZk9EUChUhQYiC0/EZoW8+dX8IvUjWuyK6mXr lr5RUdWALBFZ+ySAx1E+Xj1L53cGSa60c1sRYdXLjSvn4TcspXgNzQn2EHDhsnwFLIN8bB VkDKkegayO6HmcZVA+CEWGlR6fvj8Xe34NyvnDqLIJ6+2i5ia3DkF5uOPkxlIQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1695317766; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BdlegKqSx1sFXYsl6OPUIPVsYZjue2Sou5wwaOjYFwE=; b=w/r0GLzYM8z+AgGBS/FJTA2A86kOH3teS/zTZ1WA4e27OORUGq5+LLGKS6v/UvILtIOgTy QrAd28T+KxMqrCBA== From: "tip-bot2 for peterz@infradead.org" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: locking/core] futex: Flag conversion Cc: "Peter Zijlstra (Intel)" , Thomas Gleixner , andrealmeid@igalia.com, x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230921105247.722140574@noisy.programming.kicks-ass.net> References: <20230921105247.722140574@noisy.programming.kicks-ass.net> MIME-Version: 1.0 Message-ID: <169531776573.27769.5454262358398654731.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Thu, 21 Sep 2023 13:37:44 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777683741864844127 X-GMAIL-MSGID: 1777683741864844127 The following commit has been merged into the locking/core branch of tip: Commit-ID: 5694289ce183bc3336407a78c8c722a0b9208f9b Gitweb: https://git.kernel.org/tip/5694289ce183bc3336407a78c8c722a0b9208f9b Author: peterz@infradead.org AuthorDate: Thu, 21 Sep 2023 12:45:08 +02:00 Committer: Peter Zijlstra CommitterDate: Thu, 21 Sep 2023 19:22:05 +02:00 futex: Flag conversion Futex has 3 sets of flags: - legacy futex op bits - futex2 flags - internal flags Add a few helpers to convert from the API flags into the internal flags. Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Thomas Gleixner Reviewed-by: André Almeida Link: https://lore.kernel.org/r/20230921105247.722140574@noisy.programming.kicks-ass.net --- kernel/futex/futex.h | 63 ++++++++++++++++++++++++++++++++++++++-- kernel/futex/syscalls.c | 24 +++++---------- kernel/futex/waitwake.c | 4 +-- 3 files changed, 71 insertions(+), 20 deletions(-) diff --git a/kernel/futex/futex.h b/kernel/futex/futex.h index b5379c0..68fc052 100644 --- a/kernel/futex/futex.h +++ b/kernel/futex/futex.h @@ -5,6 +5,7 @@ #include #include #include +#include #ifdef CONFIG_PREEMPT_RT #include @@ -16,8 +17,15 @@ * Futex flags used to encode options to functions and preserve them across * restarts. */ +#define FLAGS_SIZE_8 0x00 +#define FLAGS_SIZE_16 0x01 +#define FLAGS_SIZE_32 0x02 +#define FLAGS_SIZE_64 0x03 + +#define FLAGS_SIZE_MASK 0x03 + #ifdef CONFIG_MMU -# define FLAGS_SHARED 0x01 +# define FLAGS_SHARED 0x10 #else /* * NOMMU does not have per process address space. Let the compiler optimize @@ -25,8 +33,57 @@ */ # define FLAGS_SHARED 0x00 #endif -#define FLAGS_CLOCKRT 0x02 -#define FLAGS_HAS_TIMEOUT 0x04 +#define FLAGS_CLOCKRT 0x20 +#define FLAGS_HAS_TIMEOUT 0x40 +#define FLAGS_NUMA 0x80 + +/* FUTEX_ to FLAGS_ */ +static inline unsigned int futex_to_flags(unsigned int op) +{ + unsigned int flags = FLAGS_SIZE_32; + + if (!(op & FUTEX_PRIVATE_FLAG)) + flags |= FLAGS_SHARED; + + if (op & FUTEX_CLOCK_REALTIME) + flags |= FLAGS_CLOCKRT; + + return flags; +} + +/* FUTEX2_ to FLAGS_ */ +static inline unsigned int futex2_to_flags(unsigned int flags2) +{ + unsigned int flags = flags2 & FUTEX2_SIZE_MASK; + + if (!(flags2 & FUTEX2_PRIVATE)) + flags |= FLAGS_SHARED; + + if (flags2 & FUTEX2_NUMA) + flags |= FLAGS_NUMA; + + return flags; +} + +static inline unsigned int futex_size(unsigned int flags) +{ + return 1 << (flags & FLAGS_SIZE_MASK); +} + +static inline bool futex_flags_valid(unsigned int flags) +{ + /* Only 64bit futexes for 64bit code */ + if (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall()) { + if ((flags & FLAGS_SIZE_MASK) == FLAGS_SIZE_64) + return false; + } + + /* Only 32bit futexes are implemented -- for now */ + if ((flags & FLAGS_SIZE_MASK) != FLAGS_SIZE_32) + return false; + + return true; +} #ifdef CONFIG_FAIL_FUTEX extern bool should_fail_futex(bool fshared); diff --git a/kernel/futex/syscalls.c b/kernel/futex/syscalls.c index 953f0a4..948ac24 100644 --- a/kernel/futex/syscalls.c +++ b/kernel/futex/syscalls.c @@ -1,6 +1,5 @@ // SPDX-License-Identifier: GPL-2.0-or-later -#include #include #include @@ -85,15 +84,12 @@ err_unlock: long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout, u32 __user *uaddr2, u32 val2, u32 val3) { + unsigned int flags = futex_to_flags(op); int cmd = op & FUTEX_CMD_MASK; - unsigned int flags = 0; - if (!(op & FUTEX_PRIVATE_FLAG)) - flags |= FLAGS_SHARED; - - if (op & FUTEX_CLOCK_REALTIME) { - flags |= FLAGS_CLOCKRT; - if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI && + if (flags & FLAGS_CLOCKRT) { + if (cmd != FUTEX_WAIT_BITSET && + cmd != FUTEX_WAIT_REQUEUE_PI && cmd != FUTEX_LOCK_PI2) return -ENOSYS; } @@ -201,21 +197,19 @@ static int futex_parse_waitv(struct futex_vector *futexv, unsigned int i; for (i = 0; i < nr_futexes; i++) { + unsigned int flags; + if (copy_from_user(&aux, &uwaitv[i], sizeof(aux))) return -EFAULT; if ((aux.flags & ~FUTEX2_VALID_MASK) || aux.__reserved) return -EINVAL; - if (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall()) { - if ((aux.flags & FUTEX2_SIZE_MASK) == FUTEX2_SIZE_U64) - return -EINVAL; - } - - if ((aux.flags & FUTEX2_SIZE_MASK) != FUTEX2_SIZE_U32) + flags = futex2_to_flags(aux.flags); + if (!futex_flags_valid(flags)) return -EINVAL; - futexv[i].w.flags = aux.flags; + futexv[i].w.flags = flags; futexv[i].w.val = aux.val; futexv[i].w.uaddr = aux.uaddr; futexv[i].q = futex_q_init; diff --git a/kernel/futex/waitwake.c b/kernel/futex/waitwake.c index ba01b94..fa97577 100644 --- a/kernel/futex/waitwake.c +++ b/kernel/futex/waitwake.c @@ -419,11 +419,11 @@ static int futex_wait_multiple_setup(struct futex_vector *vs, int count, int *wo */ retry: for (i = 0; i < count; i++) { - if ((vs[i].w.flags & FUTEX_PRIVATE_FLAG) && retry) + if (!(vs[i].w.flags & FLAGS_SHARED) && retry) continue; ret = get_futex_key(u64_to_user_ptr(vs[i].w.uaddr), - !(vs[i].w.flags & FUTEX_PRIVATE_FLAG), + vs[i].w.flags & FLAGS_SHARED, &vs[i].q.key, FUTEX_READ); if (unlikely(ret))