Received: by 10.192.165.156 with SMTP id m28csp1542183imm; Wed, 18 Apr 2018 11:18:35 -0700 (PDT) X-Google-Smtp-Source: AIpwx49HoXaHLG/OUBU/SNl0Wf1kaBxVdEmtxmcMNJGkZ4BGjp22wU3d/BcVoGmLAV2Ve5bcDPRa X-Received: by 10.98.74.149 with SMTP id c21mr2901442pfj.23.1524075515653; Wed, 18 Apr 2018 11:18:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524075515; cv=none; d=google.com; s=arc-20160816; b=WMuOwjTzGQn2Rv5qpHxRIShmrnBQ2kY8kuNMJuSeoNgUHSzsMFzms2JO0R9TG6eB4a zN7VXVjnKC66E6c/BWVposDvrIM6z2QHQx+1XPOzHQKHM1Vygnov6sTJlFzwajmlJiKM L95cwvUsSPioLpivqtapGoUWlLVKABOVuOc7RWoginOsrkn8KqLjYwQwqglZRCNy/r7Q j8gDxJg2rX2cuNsMmbYLH7OLogZenrRdg7KHyKl2JjlZdkftg+bQ2CXtZyRDhmVi8Our oyyBVg+Mgd9QkcWsibeXom2IHUTvkfQLKzxkfZbPNkva9eYJxLaDm7GSSMmwjA8k153R 7U/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=o6n5zbb1ZTCPSblxZrNUOXfptCWdrzjesxxEjW102YQ=; b=jAX9076/cGfgdfNFy7bfuaOt+2DH1fKh++lvkvYIlwLrsJvMuaImkwcHC1KtOyi+rN GrQ6zSVlVTihNuM6IOkhcaQzJ/w/c8Kzxk8Vm/v6fP2rqamWggG0AeNk7pR+JD71hut5 5sLr9BxVs143VOBF+kpaSlqbv+mix7WNRONfYSCFj0ySFgM5Zd+PD9I7PA2x6LAq6tM/ tdIgyQmBiK8ENKeRyax3vVn1vDk55WDR6mAL7mqUVgpIsFx4HwXtsL6DF7jW+pT8TNsz YigsebqfUCdDKWJuC11on7FwsalETE8QLXxu0Xu2O3EIFBuXMVLzDftDCpFN4Zgh1mVT DYrA== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=synopsys.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v64si1341129pfb.78.2018.04.18.11.18.21; Wed, 18 Apr 2018 11:18:35 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=synopsys.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752617AbeDRSRA (ORCPT + 99 others); Wed, 18 Apr 2018 14:17:00 -0400 Received: from smtprelay4.synopsys.com ([198.182.47.9]:55996 "EHLO smtprelay.synopsys.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752337AbeDRSQ6 (ORCPT ); Wed, 18 Apr 2018 14:16:58 -0400 Received: from mailhost.synopsys.com (mailhost1.synopsys.com [10.12.238.239]) by smtprelay.synopsys.com (Postfix) with ESMTP id 069B124E07E7; Wed, 18 Apr 2018 11:16:58 -0700 (PDT) Received: from US01WEHTC2.internal.synopsys.com (us01wehtc2.internal.synopsys.com [10.12.239.237]) by mailhost.synopsys.com (Postfix) with ESMTP id 8CB0E54D1; Wed, 18 Apr 2018 11:16:57 -0700 (PDT) Received: from IN01WEHTCB.internal.synopsys.com (10.144.199.106) by US01WEHTC2.internal.synopsys.com (10.12.239.237) with Microsoft SMTP Server (TLS) id 14.3.361.1; Wed, 18 Apr 2018 11:16:57 -0700 Received: from IN01WEHTCA.internal.synopsys.com (10.144.199.103) by IN01WEHTCB.internal.synopsys.com (10.144.199.105) with Microsoft SMTP Server (TLS) id 14.3.361.1; Wed, 18 Apr 2018 23:46:54 +0530 Received: from [10.10.161.84] (10.10.161.84) by IN01WEHTCA.internal.synopsys.com (10.144.199.243) with Microsoft SMTP Server (TLS) id 14.3.361.1; Wed, 18 Apr 2018 23:46:54 +0530 Subject: Re: [PATCH] ARC: Improve cmpxchng syscall implementation To: Alexey Brodkin CC: "wbx@uclibc-ng.org" , "linux-kernel@vger.kernel.org" , "peterz@infradead.org" , "jcmvbkbc@gmail.com" , "linux-arch@vger.kernel.org" , "linux-snps-arc@lists.infradead.org" References: <20180319110002.27419-1-abrodkin@synopsys.com> <5bc39838-b1c5-ef65-f97d-8777ed33bda0@synopsys.com> <1521633274.9805.30.camel@synopsys.com> From: Vineet Gupta Message-ID: Date: Wed, 18 Apr 2018 11:16:47 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <1521633274.9805.30.camel@synopsys.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Originating-IP: [10.10.161.84] Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 03/21/2018 04:54 AM, Alexey Brodkin wrote: > /* >>> * 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; >>> >>> - 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; >>> - >>> - if (uval == expected) { >>> - if (!__put_user(new, uaddr)) >>> + ret = __get_user(val, uaddr); >>> + if (ret == -EFAULT) { >> >> Lets see if this warrants adding complexity ! This implies that TLB entry with >> Read permissions didn't exist for reading the var and page fault handler could not >> wire up even a zero page due to preempt_disable, meaning it was something not >> touched by userspace already - sort of uninitialized variable in user code. > Ok I completely missed the fact that fast path TLB miss handler is being > executed even if we have preemption disabled. So given the mapping exist > we do not need to retry with enabled preemption. > > Still maybe I'm a bit paranoid here but IMHO it's good to be ready for a corner-case > when the pointer is completely bogus and there's no mapping for him. > I understand that today we only expect this syscall to be used from libc's > internals but as long as syscall exists nobody stops anybody from using it > directly without libc. So maybe instead of doing get_user_pages_fast() just > send a SIGSEGV to the process? At least user will realize there's some problem > at earlier stage. if the pointer is bogus, we currently return -EFAULT, is that not enough ! I'm fine if u want to change that to segv. >> Otherwise it is extremely unlikely to start with a TLB entry with Read >> permissions, followed by syscall Trap only to find the entry missing, unless a >> global TLB flush came from other cores, right in the middle. But this syscall is >> not guaranteed to work with SMP anyways, so lets ignore any SMP misdoings here. > Well but that's exactly the situation I was debugging: we start from data from read-only > page and on attempt to write back modified value COW machinery gets involved. No exactly your situation. In your case the TLB entry *did* exist with Read permission. What I was pointing to is that case where it woudl vanish between user reading the backing page and making a syscall ! > >> Now in case it was *an* uninitialized var, do we have to guarantee any well >> defined semantics for the kernel emulation of cmpxchg ? IMO it should be fine to >> return 0 or -EFAULT etc. Infact -EFAULT is better as it will force a retry loop on >> user side, given the typical cmpxchg usage pattern. > The problem is libc only expects to get a value read from memory. > And in theory expected value might be -14 which is basically -EFAULT. > I'm not talking about 0 at all because in some cases that's exactly what > user-space expects. > > So if we read unexpected value then we'll just return it without even attempting > to write. > > If we read expected data but fail to write then we'll send a SIGSEGV and > return whatever... let it be -EFAULT - anyways the app will be killed on exit from > this syscall. I'm not sure what you mean here. I'm fine with adding segv kill semantics, but don't think complexity for get_user is worth it ! -Vineet