Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp4290415imm; Mon, 25 Jun 2018 13:05:22 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLgm8jV4cQy8Z0LZ2Fr3+5bz95ABfnycSvCqYmpMnHrrgCGERWfsuOQdbvsRTz9/8HI92kz X-Received: by 2002:a17:902:262:: with SMTP id 89-v6mr13844048plc.252.1529957122086; Mon, 25 Jun 2018 13:05:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529957122; cv=none; d=google.com; s=arc-20160816; b=f2zBdro44nSzgu9Zg4NpEKIqlibVpkEzZTTGbAurYmPiFZFv/38RW4rtIELZYbqy8I WvHyfWQ4B3lJeqarbwsk8nJ445c8Fwe5ekBfGmM2I2vLq5XYUpyMzDdGnO7TFs32Eu2T EzKZ28NFFt9Izj/5uGezrfvQrIbBEIyPEcSlYg3qJp3nsfSoPhy8N3JXWm4QNGnzkwlf Oq5kn0S3o0Y4ynR7zyuTjndlAyTM5UKI0M6WIXhtbxLmF1koDf/U0vpQP2lRS67VUlJx XjG82xV3m+5yTVMpBNuRTXhxySY4cOqbZYQA6Gsfd8JwA4Z1bGih0drff1Zs89cD9kPT RD9w== 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:autocrypt:openpgp:from:references:cc:to:subject :dkim-signature:arc-authentication-results; bh=fOJ7t7gDoCJyFD87Xat3WBy5atdOTOoCS/s4ihFzQwQ=; b=UldeLBfmbOGyGmA4/3UuyZXgAPGfkWJdLouyPzZuGc+flL2CCB5qJelA8v/8r18aUV qFABdpArLFtB/LlrVEa3SsupFeFUQz9x7jnmJhIc+lFe77iMYTCMRiKSclEDUH9T4ZEK kw5OKyxgm/Zm3INJ+8Q3A2L3qUmgpVqW6V0wEw62agDvaCfBpp8wUxuR9sPd/O7M64BT kzXxzoo5kllIJByzcMBxFi+cxhMjmJjBKNh5V5eLru0XZ2/YhdcvvlkIbnIw9PMBu88P g9Sqk7mPwh/IGL26swccjMIL6e8xyNMjx8WkvMGZj5hqblvW2HLMO/7knfoX+3NK44Gw E+qw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@synopsys.com header.s=mail header.b=MPNdxLGu; 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=pass (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 74-v6si14985741pfp.161.2018.06.25.13.05.07; Mon, 25 Jun 2018 13:05: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=pass header.i=@synopsys.com header.s=mail header.b=MPNdxLGu; 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=pass (p=NONE sp=NONE dis=NONE) header.from=synopsys.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965111AbeFYUEJ (ORCPT + 99 others); Mon, 25 Jun 2018 16:04:09 -0400 Received: from smtprelay.synopsys.com ([198.182.60.111]:57968 "EHLO smtprelay.synopsys.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965002AbeFYUEF (ORCPT ); Mon, 25 Jun 2018 16:04:05 -0400 Received: from mailhost.synopsys.com (mailhost3.synopsys.com [10.12.238.238]) by smtprelay.synopsys.com (Postfix) with ESMTP id CA4DE10C05A9; Mon, 25 Jun 2018 13:04:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=synopsys.com; s=mail; t=1529957044; bh=dg8ydnLOmdAGZadZYl/ZH4BA9RAN63c+rFfj1xJ5BzE=; h=Subject:To:CC:References:From:Date:In-Reply-To:From; b=MPNdxLGuWYf5wIr/ySiOPHeBoLJdbRoEs99g4UQKvbJxgbOM4/JIa2iR/6VyL5Cg5 psFlLgW0PfI6Sdn+tB/aPNqCTYcqe7V4EWPY4kiZmWIHQPeIAOd+sKY0qrQnaNPe8U Us4DNHYFWEQTh34i8TrOI3zj6GU6X8CfInh8m66eW7Q850xZOkxw4Rw1MkWuhIauwi 0hhqAWbpEG7eg0oIxN0riKt8mHCQPsjvy04vj+u+Pd5sIW0Xe6ayBGsLNbTkz8dw5g PTaYyWVyNO2uC3MvtwxV18qjRfJ2XE7HfrkTjE/PjPSJiJ9bKWnmbtF79ukcsGc2pq YrgwCoA9dFTEA== Received: from us01wehtc1.internal.synopsys.com (us01wehtc1-vip.internal.synopsys.com [10.12.239.236]) by mailhost.synopsys.com (Postfix) with ESMTP id B24F63FE3; Mon, 25 Jun 2018 13:04:04 -0700 (PDT) Received: from IN01WEHTCB.internal.synopsys.com (10.144.199.106) by us01wehtc1.internal.synopsys.com (10.12.239.235) with Microsoft SMTP Server (TLS) id 14.3.361.1; Mon, 25 Jun 2018 13:04:04 -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; Tue, 26 Jun 2018 01:34:01 +0530 Received: from [10.10.161.98] (10.10.161.98) by IN01WEHTCA.internal.synopsys.com (10.144.199.243) with Microsoft SMTP Server (TLS) id 14.3.361.1; Tue, 26 Jun 2018 01:34:01 +0530 Subject: Re: [PATCH v2] ARC: Improve cmpxchg syscall implementation To: Alexey Brodkin , "linux-snps-arc@lists.infradead.org" CC: "linux-kernel@vger.kernel.org" , "Peter Zijlstra" , Max Filippov , "linux-arch@vger.kernel.org" References: <20180619142205.20493-1-abrodkin@synopsys.com> From: Vineet Gupta Openpgp: preference=signencrypt Autocrypt: addr=vgupta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q85bw13t2ttNLw5eHufLIpo EyAhTCLNQ58eT91YGVGvFs39IuH0b8ovVvdkKGInCT59Vr0MtfgcsqpDxWQXJXYZYTFHd3/R swARAQABwsFlBBgBAgAPAhsMBQJbBYpwBQkLx0HdAAoJEGnX8d3iisJewe8P/36pkZrVTfO+ U+Gl1OQh4m6weozuI8Y98/DHLMxEujKAmRzy+zMHYlIl3WgSih1UMOZ7U84yVZQwXQkLItcw XoihChKD5D2BKnZYEOLM+7f9DuJuWhXpee80aNPzEaubBYQ7dYt8rcmB7SdRz/yZq3lALOrF /zb6SRleBh0DiBLP/jKUV74UAYV3OYEDHN9blvhWUEFFE0Z+j96M4/kuRdxvbDmp04Nfx79A mJEnfv1Vvc9CFiWVbBrNPKomIN+JV7a7m2lhbfhlLpUk0zGFDTWcWejl4qz/pCYSoIUU4r/V BsCVZrOun4vd4cSi/yYJRY4kaAJGCL5k7qhflL2tgldUs+wERH8ZCzimWVDBzHTBojz0Ff3w 2+gY6FUbAJBrBZANkymPpdAB/lTsl8D2ZRWyy90f4VVc8LB/QIWY/GiS2towRXQBjHOfkUB1 JiEXYH/i93k71mCaKfzKGXTVxObU2I441w7r4vtNlu0sADRHCMUqHmkpkjV1YbnYPvBPFrDB S1V9OfD9SutXeDjJYe3N+WaLRp3T3x7fYVnkfjQIjDSOdyPWlTzqQv0I3YlUk7KjFrh1rxtr poYSIQKf5HuMowUNtjyiK2VhA5V2XDqd+ZUT3RqfAPf3Y5HjkhKJRqoIDggUKMUKmXaxCkPG i91ThhqBJlyU6MVUa6vZNv8E Message-ID: Date: Mon, 25 Jun 2018 13:03:56 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <20180619142205.20493-1-abrodkin@synopsys.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Content-Language: en-US X-Originating-IP: [10.10.161.98] Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 06/19/2018 07:22 AM, Alexey Brodkin wrote: > From: Peter Zijlstra > > arc_usr_cmpxchg syscall is supposed to be used on platforms > that lack support of Load-Locked/Store-Conditional instructions > in hardware. And in that case we mimic missing hardware features > with help of kernel's sycall that "atomically" checks current > value in memory and then if it matches caller expectation new > value is written to that same location. > > What's important in the description above: > - Check-and-exchange must be "atomical" which means > preemption must be disabled during entire "transaction" > - Data accessed is from user-space, i.e. we're dealing > with virtual addresses > > And in current implementation we have a couple of problems: > > 1. We do disable preemprion around __get_user() & __put_user() > but that in its turn disables page fault handler. > That means if a pointer to user's data has no mapping in > the TLB we won't be able to access required data. > Instead software "exception handling" code from __get_user_fn() > will return -EFAULT. > > 2. What's worse if we're dealing with data from not yet allocated > page (think of pre-copy-on-write state) we'll successfully > read data but on write we'll silently return to user-space > with correct result (which we really read just before). That leads > to very strange problems in user-space app further down the line > because new value was never written to the destination. > > 3. Regardless of what went wrong we'll return from syscall > and user-space application will continue to execute. > Even if user's pointer was completely bogus. > In case of hardware LL/SC that app would have been killed > by the kernel. > > With that change we attempt to imrove on all 3 items above: > > 1. We still disable preemption around write of user's data but > if we happen to fail with write we're enabling preemption > and try to fix-up page fault so that we have a correct permission > for writing user's data. Then re-try again in "atomic" context. > > 2. If real page fault fails or even access_ok() returns false > we send SIGSEGV to the user-space process so if something goes > seriously wrong we'll know about it much earlier. > > Signed-off-by: Peter Zijlstra > Signed-off-by: Alexey Brodkin > Cc: Vineet Gupta > Cc: Max Filippov > Cc: linux-arch@vger.kernel.org > --- > > Changes v1 -> v2: > > * Peter's almost clean-room reimplmentation with less paranoid checks > and direct invocation of fixup_user_fault() for in-place update of > write permissions. > I don't like the changelog - it is way too verbose and doesn't say the exact problem we are trying to solve. How about something like below ? ----->     ARC: Improve cmpxchg syscall implementation         This is used in configs lacking hardware atomics to emulate atomic r-m-w     for user space, implemented by disabling preemption in kernel.         However there are issues in current implementation:         1. Process not terminated if invalid user pointer passed:        i.e. __get_user() failed.         2. The reason for this patch was __put_user() failure not being handled,        for COW break scenario. The zero page is initially wired up and        read by __get_user() succeeds. However a write by __put_user()        doesn't complete the page fault handling due to the page fault        disabling from preempt disable. And what's worse is we silently return        the stale zero value from __get_user() to user space. So the fix        handles the specific case by re-enabling preemption and explicitly        fixing up the fault and retrying the whole sequence over. OK ? -Vineet