Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp4945997imm; Tue, 19 Jun 2018 02:27:22 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLmCBeqetltyU7pecFG2t9iMOW6Fvw3DnJk8rJ/EpNbiQxK+9uHdQ1EC9XWwf8myUC91j64 X-Received: by 2002:a17:902:b483:: with SMTP id y3-v6mr18214578plr.66.1529400442945; Tue, 19 Jun 2018 02:27:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529400442; cv=none; d=google.com; s=arc-20160816; b=Wh5t1XsjN1lvJbhIoBaugYxGO6NeNE+93CgjYpvTf5F8XK9HZl8y4O7MijIXO5h+eM ROJIl6p3T8GrpyLa9vOPNdIfwHt9iSECugUzmYfdAlMxM+1U/OGMDvDbwS+kLQAS1IHy xB0Y3/KyId61Oa0Uy+ax1/oS9vXbqbSGxkXq6On18lbSM3fg/Un6BV5QT1DUU70M7loi 3XQzj9eDYOGnUUqq2Z7S2BdL6bGLsf/q9D2tf8+ELI07c5lRhvub4FU70DEpq4gLvNo6 1eKwsENqcLfT7UMUj4bnDk+9M13Zy3zNC6fZdhuR+n25IZDXf8ipJnVbfvxm8V2RbTZ6 exJw== 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:arc-authentication-results; bh=QS+xCKoIvL2Rqc04qEuDccQx26EsRfpiNnCMNC0f/hU=; b=yey/79hi6pABBtdvVLJmrN5LiNCURc4U2UetZBjpjZ5v0947oXtoVmvtLNaGX7mDFb QFyjjYedcxWjKrlBg7EDD4ejVPsUAYqCZ99o1Vksqd8o9y8NFZDsvfSKZYkIhJXo+YQB fYvfscFo9+4YWX6GSXi7X9xIOu+pfHrc3WhkfiBG5v7myBKbwXW3N0YqY+oVI3ERD7RI P1bETWGyJ1pDicVTfVUH3hULLwrgcz9mlDnnI5EU2mvNW0IWbuxkwzxtguuJ3uVBL8QP iWL0f9rNOIP7+/sBEBkQCV4aD4aaK79RNp+eTZTt+/JUmazT91czDOvjjvGFy+prHxOD z25w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=FY7UV3fR; 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 d34-v6si797697pld.252.2018.06.19.02.27.08; Tue, 19 Jun 2018 02:27:22 -0700 (PDT) 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=FY7UV3fR; 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 S1756814AbeFSJ0U (ORCPT + 99 others); Tue, 19 Jun 2018 05:26:20 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:45396 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756270AbeFSJ0S (ORCPT ); Tue, 19 Jun 2018 05:26:18 -0400 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=QS+xCKoIvL2Rqc04qEuDccQx26EsRfpiNnCMNC0f/hU=; b=FY7UV3fRzFSlQGNQTNAWul/Uo 0ePAB1NicqKpvmyN9n4btPZeDrDpEVXAg5NxCTiNrqUtPOsdNvPSOzD/nTlCth1Hu3eLbao8m8BMR NNyDWO0+jvlZ9qtz7lnmrvQXQsFyePlbExh9fhYDjbEm4gTmvP3oWddJfGLpSe4uj0cvhSe5CU/qd 57A35t+2tOh5U+ZUYHHkntTAZGqEYE/BMrpllShtj0kX7dOScl2ytE9Kvw9XspIznQuvabJv2iV71 kizvyGHN0uJXUCVt11JjlPxOrCVv1Xd5qfPQll2owhxyAIR33wkhE4B5HNYv6X+Q3QPf/IiZTh73J 7O6aUT0XQ==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1fVCu0-0002CC-KY; Tue, 19 Jun 2018 09:26:16 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 528452011CEBD; Tue, 19 Jun 2018 11:26:14 +0200 (CEST) Date: Tue, 19 Jun 2018 11:26:14 +0200 From: Peter Zijlstra To: Alexey Brodkin Cc: linux-snps-arc@lists.infradead.org, linux-kernel@vger.kernel.org, Vineet Gupta , Max Filippov , linux-arch@vger.kernel.org Subject: Re: [PATCH] ARC: Improve cmpxchng syscall implementation Message-ID: <20180619092614.GD2494@hirez.programming.kicks-ass.net> References: <20180319110002.27419-1-abrodkin@synopsys.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180319110002.27419-1-abrodkin@synopsys.com> User-Agent: Mutt/1.10.0 (2018-05-17) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 19, 2018 at 02:00:02PM +0300, Alexey Brodkin wrote: > diff --git a/arch/arc/kernel/process.c b/arch/arc/kernel/process.c > index 5ac3b547453f..d7d3e16133d6 100644 > --- a/arch/arc/kernel/process.c > +++ b/arch/arc/kernel/process.c > @@ -47,7 +47,9 @@ SYSCALL_DEFINE0(arc_gettls) > SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) > { > struct pt_regs *regs = current_pt_regs(); > + struct page *page; > + u32 val; > + int ret; > > /* > * This is only for old cores lacking LLOCK/SCOND, which by defintion > @@ -60,23 +62,48 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) > /* Z indicates to userspace if operation succeded */ > regs->status32 &= ~STATUS_Z_MASK; > > + ret = access_ok(VERIFY_WRITE, uaddr, sizeof(*uaddr)); > + if (!ret) > + goto fail; > > +again: > preempt_disable(); > > + ret = __get_user(val, uaddr); > + if (ret == -EFAULT) { > + preempt_enable(); > + ret = get_user_pages_fast((unsigned long)uaddr, 1, 1, &page); > + if (ret < 0) > + goto fail; > + > + put_page(page); > + goto again; > + } else if (ret) > + goto fail; goto fail with preempt disabled. > + > + if (val == expected) { > + ret = __put_user(new, uaddr); > + if (!ret) > regs->status32 |= STATUS_Z_MASK; > } > > -done: > preempt_enable(); > > - return uval; > + if (ret == -EFAULT) { > + ret = get_user_pages_fast((unsigned long)uaddr, 1, 1, &page); > + if (ret < 0) > + goto fail; > + > + put_page(page); > + goto again; > + } else if (ret) > + goto fail; > + > + return val; > + > +fail: > + force_sig(SIGSEGV, current); > + return ret; > } Sorry for the delay, but I would write it like: --- a/arch/arc/kernel/process.c +++ b/arch/arc/kernel/process.c @@ -47,7 +47,9 @@ SYSCALL_DEFINE0(arc_gettls) SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new) { struct pt_regs *regs = current_pt_regs(); - int uval = -EFAULT; + struct page *page; + u32 val; + int ret; /* * This is only for old cores lacking LLOCK/SCOND, which by defintion @@ -60,23 +62,46 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, /* Z indicates to userspace if operation succeded */ regs->status32 &= ~STATUS_Z_MASK; - if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int))) - return -EFAULT; + ret = access_ok(VERIFY_WRITE, uaddr, sizeof(*uaddr)); + if (!ret) + goto fail; +again: preempt_disable(); - if (__get_user(uval, uaddr)) - goto done; + ret = __get_user(val, uaddr); + if (ret) + goto fault; - if (uval == expected) { - if (!__put_user(new, uaddr)) - regs->status32 |= STATUS_Z_MASK; - } + if (val != expected) + goto out; -done: + ret = __put_user(new, uaddr); + if (ret) + goto fault; + + regs->status32 |= STATUS_Z_MASK; + +out: preempt_enable(); + return val; + +fault: + preempt_enable(); + + if (unlikely(ret != -EFAULT)) + goto fail; - return uval; + down_read(¤t->mm->mmap_sem); + ret = fixup_user_fault(current, current->mm, uaddr, FAULT_FLAG_WRITE, NULL); + up_read(¤t->mm->mmap_sem); + + if (likely(!ret)) + goto again; + +fail: + force_sig(SIGSEGV, current); + return ret; } #ifdef CONFIG_ISA_ARCV2