Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp5066325imd; Tue, 30 Oct 2018 11:25:31 -0700 (PDT) X-Google-Smtp-Source: AJdET5cfHOdAjwNBWYGap2QAN6FxUZOnxVARduu7crqDijt0VW5eSiwQ1o23J7cfQTVu8mYlBr1/ X-Received: by 2002:a17:902:7786:: with SMTP id o6-v6mr9698298pll.95.1540923931390; Tue, 30 Oct 2018 11:25:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540923931; cv=none; d=google.com; s=arc-20160816; b=exT7ZIynFeXwfyASLb7Gfcr9ZTWK5eR+KsleFuoNkymD6eIfVNwboL1AihfrfFk9Ek dPcRAoD662BC+tbtCLUgpSlGnDCn03vqOs6bhpej9zw6JwrJX/tcvM01//61qD7uuPx3 Zg2S4iw8+2BU+Xt5bUAT4NHzpEjStcOAKFfNDYW2UVwmHNFg8MwamwSrQYQJmcePP60f 59JbhBnSqj5zWCv7NzaiRlCn5yuJFyiKTChAnegxivABDhdh7VJSvIRtE7l6BPbVsgbF WeiwmNv4LBbG/bClaNs0//HVTWwiHbeIs0GXgn+Y4jQJ/c0/gj+5pxRZzRgc28G8wKMZ ZY/w== 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; bh=r8R1QXQRlecg0DK1QXd6jOwJThcwOwvUNHmSjvFCJqg=; b=OjwAV4AT1uDYmo6FxrxYby3gW52yJiHesV3VyYzEIH+lMWx6hf1/aQrA7qa8RTstfU xhwMa6c27DgQxlhOtGfsmQgrhQE2/bxJp80cpcly3lb/w5wjLVty4xGuFZmjbHlKHhm4 brmQNnBbHlBMl3kFL/54isZq+FTF1azgUrAN8G2y/tzCuLtbdpRWIHRaZ1tGr2/YzzuO GWCqnPQYIKWiDwfhDh0DebrCbMSIwFnLDR4Av9wjelFvLvHs/NQsGz+EC2TWCcE7G1+F /6l7URbvnCWxPUmzOOkSsJozfj75j1KQ5NUwCPafHiZy0NwRJxeok4MhBxh0m4efDPTZ SWIQ== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f8-v6si24223516pgg.60.2018.10.30.11.25.15; Tue, 30 Oct 2018 11:25:31 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727821AbeJaBWT (ORCPT + 99 others); Tue, 30 Oct 2018 21:22:19 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:56938 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727008AbeJaBWT (ORCPT ); Tue, 30 Oct 2018 21:22:19 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 7552180D; Tue, 30 Oct 2018 09:28:09 -0700 (PDT) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 445323F557; Tue, 30 Oct 2018 09:28:09 -0700 (PDT) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id B20FB1AE0CFD; Tue, 30 Oct 2018 16:28:16 +0000 (GMT) Date: Tue, 30 Oct 2018 16:28:16 +0000 From: Will Deacon To: Peter Zijlstra Cc: Igor Stoppa , Mimi Zohar , Kees Cook , Matthew Wilcox , Dave Chinner , James Morris , Michal Hocko , kernel-hardening@lists.openwall.com, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, igor.stoppa@huawei.com, Dave Hansen , Jonathan Corbet , Laura Abbott , Boqun Feng , Arnd Bergmann , linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 16/17] prmem: pratomic-long Message-ID: <20181030162815.GB20827@arm.com> References: <20181023213504.28905-1-igor.stoppa@huawei.com> <20181023213504.28905-17-igor.stoppa@huawei.com> <20181025001312.GA3159@worktop.c.hoisthospitality.com> <806bfff0-8d62-9918-480d-ec791b93841e@gmail.com> <20181030155841.GF8177@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181030155841.GF8177@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 30, 2018 at 04:58:41PM +0100, Peter Zijlstra wrote: > On Mon, Oct 29, 2018 at 11:17:14PM +0200, Igor Stoppa wrote: > > > > > > On 25/10/2018 01:13, Peter Zijlstra wrote: > > > On Wed, Oct 24, 2018 at 12:35:03AM +0300, Igor Stoppa wrote: > > > > +static __always_inline > > > > +bool __pratomic_long_op(bool inc, struct pratomic_long_t *l) > > > > +{ > > > > + struct page *page; > > > > + uintptr_t base; > > > > + uintptr_t offset; > > > > + unsigned long flags; > > > > + size_t size = sizeof(*l); > > > > + bool is_virt = __is_wr_after_init(l, size); > > > > + > > > > + if (WARN(!(is_virt || likely(__is_wr_pool(l, size))), > > > > + WR_ERR_RANGE_MSG)) > > > > + return false; > > > > + local_irq_save(flags); > > > > + if (is_virt) > > > > + page = virt_to_page(l); > > > > + else > > > > + vmalloc_to_page(l); > > > > + offset = (~PAGE_MASK) & (uintptr_t)l; > > > > + base = (uintptr_t)vmap(&page, 1, VM_MAP, PAGE_KERNEL); > > > > + if (WARN(!base, WR_ERR_PAGE_MSG)) { > > > > + local_irq_restore(flags); > > > > + return false; > > > > + } > > > > + if (inc) > > > > + atomic_long_inc((atomic_long_t *)(base + offset)); > > > > + else > > > > + atomic_long_dec((atomic_long_t *)(base + offset)); > > > > + vunmap((void *)base); > > > > + local_irq_restore(flags); > > > > + return true; > > > > + > > > > +} > > > > > > That's just hideously nasty.. and horribly broken. > > > > > > We're not going to duplicate all these kernel interfaces wrapped in gunk > > > like that. > > > > one possibility would be to have macros which use typeof() on the parameter > > being passed, to decide what implementation to use: regular or write-rare > > > > This means that type punning would still be needed, to select the > > implementation. > > > > Would this be enough? Is there some better way? > > Like mentioned elsewhere; if you do write_enable() + write_disable() > thingies, it all becomes: > > write_enable(); > atomic_foo(&bar); > write_disable(); > > No magic gunk infested duplication at all. Of course, ideally you'd then > teach objtool about this (or a GCC plugin I suppose) to ensure any > enable reached a disable. Isn't the issue here that we don't want to change the page tables for the mapping of &bar, but instead want to create a temporary writable alias at a random virtual address? So you'd want: wbar = write_enable(&bar); atomic_foo(wbar); write_disable(wbar); which is probably better expressed as a map/unmap API. I suspect this would also be the only way to do things for cmpxchg() loops, where you want to create the mapping outside of the loop to minimise your time in the critical section. Will