Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp631620ybk; Fri, 15 May 2020 09:29:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzMxEQns1BwmweUy/2NO2xXzV05rI50YPjdXmuXfRbI5fOZ9d8BbKdLM0W3it/y1VBQI57i X-Received: by 2002:a50:ec0c:: with SMTP id g12mr3919877edr.140.1589560192447; Fri, 15 May 2020 09:29:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589560192; cv=none; d=google.com; s=arc-20160816; b=xAl3FLH/zzgPdQvt9ESV8adXQbMqBeXo0S9Lv1AjmLEPrvn3PK8110hfjEALsjncvC 6BWa3Fdwo3qC/GJ0kguJc1Ano4a5QbXmgIE0ULyg9unJhEhRwaOY0UdIzQCkOJdG/PWg 9WvjV3UY0hO901uSnil1R4UucgNOWJ4stabpLnmE/DM6w+nTmavllTZjNw8Lzab5qgGk RtvF0pvodo4SsdWXcAEMekD7NmOE6DiC/bYAXloT7TI7DKx5gpa1f+pxyjdFalPqfH4B hyPV3tD5SSEr+9X6gxnzZFn2myAHAanTdy368a3QSTfyQu+E/qkde4Bs+1o2/QJvT8K9 rSKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=aC9hyHleLo4vDUxT+Z5QeopjJNtSZOJdlvX7fJFebgc=; b=oKsI3/5YUv4/uSkyOHGlOWPhRdQ4VUaZKzKldL/hwxAnmpPNZRFdAXYeVZlxHgbrLb L/Gi3QZnh5Adh3meZa0ePw+iXTWsAhgOxZaKeMCx+wMEFVVAKnWzvBq0QDMnz3mmoViV t+zYP9JYkzEVTk1Q2EW0zFMKoGNUKSuP/agLiwau19QWkqmyqYfV0EbY7QXOodhrEUBA VGPAS1q0RJaF6DQ7p1hfHCUIbfebdSegEPrKTLIgojNzoiv6sUJBMsj+vJZGSJZkw7+V A7oMN+2fUc5qCY97B/F/8SFDwTyM6u8qGimzk/bWXDYSyX16uoO5tNVCYAQgHPkYWLi1 ymlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=SDisOMox; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d20si1649445edj.13.2020.05.15.09.29.29; Fri, 15 May 2020 09:29:52 -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=fail header.i=@infradead.org header.s=merlin.20170209 header.b=SDisOMox; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726236AbgEOQ1b (ORCPT + 99 others); Fri, 15 May 2020 12:27:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726023AbgEOQ1a (ORCPT ); Fri, 15 May 2020 12:27:30 -0400 Received: from merlin.infradead.org (unknown [IPv6:2001:8b0:10b:1231::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B00E9C061A0C; Fri, 15 May 2020 09:27:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.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; bh=aC9hyHleLo4vDUxT+Z5QeopjJNtSZOJdlvX7fJFebgc=; b=SDisOMoxxYtKbAHobnvGTQfe18 I/euPce16QW5/Fu9jH2GrcWgGP2Tsuj1+B8LmQI1QTJstm2MhikZVoiYzczeTXpNFFno4u5Z2Abzy Jw51BzW/byw1yl+BIMGwsNRrUcqasuMn0voH8dv8JUFzG4IPgbZTk1O5GdaGnCZnYAdYAMSXyN7IP ikGqCsWmykoL9di/7l9bcWU7dBtprsxn3DxE0oxOYk37908e6P41JMwhuiZ6OEVNQsa2TluMHyzUU BFmAH7Kii0egxM/0i3grgMOMXHV0N78i4olzuoW9Q2dfPivXstNsEPVxdLb8Nw1nloQlQku59jkHw XVfxdwqA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jZdAz-0006bN-58; Fri, 15 May 2020 16:27:09 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id BBF17301A42; Fri, 15 May 2020 18:27:07 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id A420020097509; Fri, 15 May 2020 18:27:07 +0200 (CEST) Date: Fri, 15 May 2020 18:27:07 +0200 From: Peter Zijlstra To: Konstantin Khlebnikov Cc: linux-kernel@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Darren Hart , Maxim Samoylov , Linus Torvalds , linux-api@vger.kernel.org Subject: Re: [PATCH] futex: send SIGBUS if argument is not aligned on a four-byte boundary Message-ID: <20200515162707.GI2978@hirez.programming.kicks-ass.net> References: <158955700764.647498.18025770126733698386.stgit@buzz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <158955700764.647498.18025770126733698386.stgit@buzz> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 15, 2020 at 06:36:47PM +0300, Konstantin Khlebnikov wrote: > Userspace implementations of mutexes (including glibc) in some cases > retries operation without checking error code from syscall futex. > This is good for performance because most errors are impossible when > locking code trusts itself. > > Some errors which could came from outer code are handled automatically, > for example invalid address triggers SIGSEGV on atomic fast path. > > But one case turns into nasty busy-loop: when address is unaligned. > futex(FUTEX_WAIT) returns EINVAL immediately and loop goes to retry. > > Example which loops inside second call rather than hung peacefully: > > #include > #include > > int main(int argc, char **argv) > { > char buf[sizeof(pthread_mutex_t) + 1]; > pthread_mutex_t *mutex = (pthread_mutex_t *)(buf + 1); > > pthread_mutex_init(mutex, NULL); > pthread_mutex_lock(mutex); > pthread_mutex_lock(mutex); > } > > It seems there is no practical usage for calling syscall futex for > unaligned address. This may be only bug in user space. Let's help > and handle this gracefully without adding extra code on fast path. > > This patch sends SIGBUS signal to slay task and break busy-loop. > > Signed-off-by: Konstantin Khlebnikov > Reported-by: Maxim Samoylov Seems like a sensible idea to me. Acked-by: Peter Zijlstra (Intel) > --- > kernel/futex.c | 13 ++++++++++++- > 1 file changed, 12 insertions(+), 1 deletion(-) > > diff --git a/kernel/futex.c b/kernel/futex.c > index b59532862bc0..8a6d35fa56bc 100644 > --- a/kernel/futex.c > +++ b/kernel/futex.c > @@ -508,10 +508,21 @@ get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, enum futex_a > > /* > * The futex address must be "naturally" aligned. > + * Also send signal to break busy-loop if user-space ignore error. > + * EFAULT case should trigger SIGSEGV at access from user-space. > */ > key->both.offset = address % PAGE_SIZE; > - if (unlikely((address % sizeof(u32)) != 0)) > + if (unlikely((address % sizeof(u32)) != 0)) { > + struct kernel_siginfo info; > + > + clear_siginfo(&info); > + info.si_signo = SIGBUS; > + info.si_code = BUS_ADRALN; > + info.si_addr = uaddr; > + force_sig_info(&info); > + > return -EINVAL; > + } > address -= key->both.offset; > > if (unlikely(!access_ok(uaddr, sizeof(u32)))) >