Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp10054323imu; Wed, 5 Dec 2018 15:17:45 -0800 (PST) X-Google-Smtp-Source: AFSGD/X+hDw+bUWV49Zxin7bSWtOgOKe/lQovBt30sDndSxfOB9eY9CNvBsX4lp15CnwFlkCEido X-Received: by 2002:a62:184e:: with SMTP id 75mr25773347pfy.28.1544051865460; Wed, 05 Dec 2018 15:17:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544051865; cv=none; d=google.com; s=arc-20160816; b=eTqUvebIVozO1yVemtbRb/ouzED/QoQaxLfj9pO4cxUnLDzS39l3SEecARS5t41mxU vpMGmhSXmBOhs8dah/3F85A39ahI0gBhWzFeMUJRcyDl/UHj1Y0FOJ+Pbt/4E7E94lue hPlkCwY+88HEuLLFHhGQDz3C39AHd6YDpLh8yR3RQQZDGtXdB9ctuKYRIpmwN0J60OM2 oyfljOr4fHlbuOdDCTvMxgv2a1UsLN9KwZnfiiMVr4d7snAvGdr8mPEPrirUGI2Kx80D 9BlTI11qavnlxfGwQdosYdAlFMQhbCM6qzoa/E0WZkrsv/exLwCXg7HHN2VWBlzetJeR 3Eaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=VU2a9sDd9zR3n6O685+MiYWyTmt01gdxPJM/9UVGH7U=; b=Y7CP51GaVk5ONE2YoE4912y6C3lEYQg2ASGUbYfymyWnc0gEqmyjJOHs9E/5qdZN3H f5tV8PmqoENFIGT6qLsulIlb3YOXnJhpWBW3+5hDsQvr6QVZPbWiPuEF3Mi083YlZWCB Wc2R/5uhTyrKkKCUPIVOrTbUDqOPVkUVy7GTvyuVDo0TYdCuOkI7HSEO1tk1nGIWnXEZ YLNZdMLqFrZaPSOvVYXU9ks9atZ4g8H7BqAn7x1FPCbAGgw32/D/lhPeywGbYAJEBjna uYw6F9/rC6vESfMKFZCsv9bGU1HJ3oFW7EeI3rcCO/rTv4KlQ+pATv3cLRDmwQX0tB9Y SNew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=p8d8iJ1Y; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d4si21016455pls.348.2018.12.05.15.17.29; Wed, 05 Dec 2018 15:17:45 -0800 (PST) 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=@kernel.org header.s=default header.b=p8d8iJ1Y; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728695AbeLEXQs (ORCPT + 99 others); Wed, 5 Dec 2018 18:16:48 -0500 Received: from mail.kernel.org ([198.145.29.99]:49762 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727645AbeLEXQs (ORCPT ); Wed, 5 Dec 2018 18:16:48 -0500 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BD2E0214DA for ; Wed, 5 Dec 2018 23:16:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544051807; bh=LQNeZgdotgA5zUyaVs+F8s8TonzSnfRs9CCRmMkMy88=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=p8d8iJ1YA7VXQOHfehykZUHGh0R6N5l6dYBW0Q774J8ulVQLmHk/NezPhAuYY111x qAwrK31FF6+LmwXjY9rpcpX6aOACXcZR5sORDHrtfwkbUjydLW/3nEYXrke3QmVbk6 tw9nXoQGGrA5fzjkQ/Os5/EXwqFlHc+Fh9J8nypU= Received: by mail-wm1-f52.google.com with SMTP id y185so12832198wmd.1 for ; Wed, 05 Dec 2018 15:16:46 -0800 (PST) X-Gm-Message-State: AA+aEWZr7Czuf50RBylNt+0rWwDupTA6GrA4CvPRUSIe0u3KOOF3S0ER OZBZfVM2+zuln8lGYAx7UkLzxUqpPjAz8A7y0FChsw== X-Received: by 2002:a1c:aa0f:: with SMTP id t15mr6727166wme.108.1544051805174; Wed, 05 Dec 2018 15:16:45 -0800 (PST) MIME-Version: 1.0 References: <20181128000754.18056-1-rick.p.edgecombe@intel.com> <20181128000754.18056-2-rick.p.edgecombe@intel.com> <4883FED1-D0EC-41B0-A90F-1A697756D41D@gmail.com> <20181204160304.GB7195@arm.com> <51281e69a3722014f718a6840f43b2e6773eed90.camel@intel.com> <20181205114148.GA15160@arm.com> In-Reply-To: <20181205114148.GA15160@arm.com> From: Andy Lutomirski Date: Wed, 5 Dec 2018 15:16:33 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 1/2] vmalloc: New flag for flush before releasing pages To: Will Deacon Cc: Andrew Lutomirski , Rick Edgecombe , Nadav Amit , LKML , Daniel Borkmann , jeyu@kernel.org, Steven Rostedt , Alexei Starovoitov , Ard Biesheuvel , Linux-MM , Jann Horn , "Dock, Deneen T" , Peter Zijlstra , Kristen Carlson Accardi , Andrew Morton , Ingo Molnar , Anil S Keshavamurthy , Kernel Hardening , Masami Hiramatsu , "Naveen N . Rao" , "David S. Miller" , Network Development , Dave Hansen Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 5, 2018 at 3:41 AM Will Deacon wrote: > > On Tue, Dec 04, 2018 at 12:09:49PM -0800, Andy Lutomirski wrote: > > On Tue, Dec 4, 2018 at 12:02 PM Edgecombe, Rick P > > wrote: > > > > > > On Tue, 2018-12-04 at 16:03 +0000, Will Deacon wrote: > > > > On Mon, Dec 03, 2018 at 05:43:11PM -0800, Nadav Amit wrote: > > > > > > On Nov 27, 2018, at 4:07 PM, Rick Edgecombe > > > > > > wrote: > > > > > > > > > > > > Since vfree will lazily flush the TLB, but not lazily free the = underlying > > > > > > pages, > > > > > > it often leaves stale TLB entries to freed pages that could get= re-used. > > > > > > This is > > > > > > undesirable for cases where the memory being freed has special = permissions > > > > > > such > > > > > > as executable. > > > > > > > > > > So I am trying to finish my patch-set for preventing transient W+= X mappings > > > > > from taking space, by handling kprobes & ftrace that I missed (th= anks again > > > > > for > > > > > pointing it out). > > > > > > > > > > But all of the sudden, I don=E2=80=99t understand why we have the= problem that this > > > > > (your) patch-set deals with at all. We already change the mapping= s to make > > > > > the memory writable before freeing the memory, so why can=E2=80= =99t we make it > > > > > non-executable at the same time? Actually, why do we make the mod= ule memory, > > > > > including its data executable before freeing it??? > > > > > > > > Yeah, this is really confusing, but I have a suspicion it's a combi= nation > > > > of the various different configurations and hysterical raisins. We = can't > > > > rely on module_alloc() allocating from the vmalloc area (see nios2)= nor > > > > can we rely on disable_ro_nx() being available at build time. > > > > > > > > If we *could* rely on module allocations always using vmalloc(), th= en > > > > we could pass in Rick's new flag and drop disable_ro_nx() altogethe= r > > > > afaict -- who cares about the memory attributes of a mapping that's= about > > > > to disappear anyway? > > > > > > > > Is it just nios2 that does something different? > > > > > > > Yea it is really intertwined. I think for x86, set_memory_nx everywhe= re would > > > solve it as well, in fact that was what I first thought the solution = should be > > > until this was suggested. It's interesting that from the other thread= Masami > > > Hiramatsu referenced, set_memory_nx was suggested last year and would= have > > > inadvertently blocked this on x86. But, on the other architectures I = have since > > > learned it is a bit different. > > > > > > It looks like actually most arch's don't re-define set_memory_*, and = so all of > > > the frob_* functions are actually just noops. In which case allocatin= g RWX is > > > needed to make it work at all, because that is what the allocation is= going to > > > stay at. So in these archs, set_memory_nx won't solve it because it w= ill do > > > nothing. > > > > > > On x86 I think you cannot get rid of disable_ro_nx fully because ther= e is the > > > changing of the permissions on the directmap as well. You don't want = some other > > > caller getting a page that was left RO when freed and then trying to = write to > > > it, if I understand this. > > > > > > > Exactly. > > Of course, I forgot about the linear mapping. On arm64, we've just queued > support for reflecting changes to read-only permissions in the linear map > [1]. So, whilst the linear map is always non-executable, we will need to > make parts of it writable again when freeing the module. > > > After slightly more thought, I suggest renaming VM_IMMEDIATE_UNMAP to > > VM_MAY_ADJUST_PERMS or similar. It would have the semantics you want, > > but it would also call some arch hooks to put back the direct map > > permissions before the flush. Does that seem reasonable? It would > > need to be hooked up that implement set_memory_ro(), but that should > > be quite easy. If nothing else, it could fall back to set_memory_ro() > > in the absence of a better implementation. > > You mean set_memory_rw() here, right? Although, eliding the TLB invalidat= ion > would open up a window where the vmap mapping is executable and the linea= r > mapping is writable, which is a bit rubbish. > Right, and Rick pointed out the same issue. Instead, we should set the direct map not-present or its ARM equivalent, then do the flush, then make it RW. I assume this also works on arm and arm64, although I don't know for sure. On x86, the CPU won't cache not-present PTEs.