Received: by 10.223.185.116 with SMTP id b49csp4088481wrg; Tue, 6 Mar 2018 09:35:39 -0800 (PST) X-Google-Smtp-Source: AG47ELv4Ral6DISKJiM7IcH9IiNeUIR+4jXUa2+zbIiqVeApYVH6X874+xem62HfuivP9dMZoc3V X-Received: by 10.98.205.140 with SMTP id o134mr19717270pfg.34.1520357739516; Tue, 06 Mar 2018 09:35:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520357739; cv=none; d=google.com; s=arc-20160816; b=Jb+3/sDwwN0llmFZoD2kgHrjl0Idv2qkDB/1Oap/T8rErorAcXxc+weNL0WKkZQqDw axbeK5KoyAcjPp/UaUXQxpDg52qF4N6CbRrEPhGl7lwg1UyG8uPA4wK9w2DpNsQwYbbT Lq7xE0iBNGMLxoz4o+DtNZJbUhEO74oqo0abex2cJaYwFJZs64RKiOpdgkc2AeUHn/al QGmXUfiRiP4KThXkOeFvLewF+Cjpj+x05Q7kK3f+76pn/gvX/wJvZSGIinfBPSDweHYn Ihbg+PHpR+LBn2ANLSfwKcksxdLtKSxdInSCcfkiyGRwogBEflXnhu/+kpv/QP++rBc/ wwWg== 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:dkim-signature :arc-authentication-results; bh=2sK0x8Jc+jGtj94hk6mffXo/isYJpY3lLDBdXPKdRqw=; b=sQKfNg/E5xI9b/T3i3GEW5dMIbJPNyyN66pkfFrl1TAxlNDk7kAmPplEbbTqHAH1Ri ArkZkrG4bRKOTUGgjw7vgQf77UyVNazwSOkdjz+E8gCtcGSoDWwT4TXOvxZJN0w3/4mA pD26nOdG1Hi886dUUo8rYTIxOoTPTDtzyDtB6QwqpPNDO4XE9h+PAglh3qA7xLB2zkqL vSwH2iP+bH6y8OuwkY/EH8t5pY21E+uvOVOllKuKyIPi3oi5Ewi0oJp0PKIli7LhwWgy TIUxVp5LDjgimrPCeW1e6XylLUOwrBkGrU/btN/XzHjvFzrMeq3vrtkL0NeAFzKbqIw3 dH/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RP0BLKvp; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 26si12208124pfl.261.2018.03.06.09.35.25; Tue, 06 Mar 2018 09:35:39 -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=@gmail.com header.s=20161025 header.b=RP0BLKvp; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753507AbeCFRdv (ORCPT + 99 others); Tue, 6 Mar 2018 12:33:51 -0500 Received: from mail-pl0-f65.google.com ([209.85.160.65]:35923 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750782AbeCFRdt (ORCPT ); Tue, 6 Mar 2018 12:33:49 -0500 Received: by mail-pl0-f65.google.com with SMTP id 61-v6so12245846plf.3; Tue, 06 Mar 2018 09:33:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=2sK0x8Jc+jGtj94hk6mffXo/isYJpY3lLDBdXPKdRqw=; b=RP0BLKvpibyCMMarVMOmt5hoCuDxmTbaYMT7jlrBJ7g+GD8p5GALgDROiJYEi1rFSX MlwDP3VjXLAWDCI4pw1Oe8eJMF8LVMcOwsl+MYiQu7Ck9VLASgHAPkXuh6ykQL9Tqcdo BDNbxmsFz685WOgq/AckR7suxujqvQ/bBpSuscen/9z4vt1Maxpx9ON3s4MnojLkupxh nXjL8YbGGEGP+y5JI1r6Fc5kxj60CqxVvO4XPWe9kIpLperJg8/hzdntnq4UmsDrF4ke l199IZEkEAKx/5IxKQ44p/34C5NIKg5M7AKLbng7patTCKY+crh7cCTlkkq6r145isRT Pyzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=2sK0x8Jc+jGtj94hk6mffXo/isYJpY3lLDBdXPKdRqw=; b=kJnu4Ht/7idN1cl+j0ReGOvtFO7WIhCoSdUr2ouMS/33PiTB31X/EkOxfkvyurCyqm OECgLR0nE2emv82aJduEjQ98D3zfmrNU6abAYjWcl+nX6Ri5NXmFrivLNzY5sQePOOPS 8iA6pgEUxi299S8EiVPRpNvlfEau2msDRc/bdSKk8NcLxn1AkU+xm9ziRkoR83b7wZ56 qG9/6QwR4fchp2YnZ/cS7HCHd4z06OiSVfgATKNF1byLvS9YqHDfgtZ/EF1VuKlwdG2I qZ3R/6L2I/lObTWiKgPJkoHL5Ll79ZaSmwvhE5YwUTAzRqjk3oqf7+iYXzhWlKwb4cGa oyzg== X-Gm-Message-State: APf1xPDu03/RdT6v01uPg1Mble69caMp4Y254yDqrdpcyKlLftL3dvkh /HYV3soNOq+lSmWh07NEa9A= X-Received: by 2002:a17:902:ab89:: with SMTP id f9-v6mr17210194plr.369.1520357628986; Tue, 06 Mar 2018 09:33:48 -0800 (PST) Received: from JF-EN-C02V905BHTDF.tld ([12.111.169.54]) by smtp.gmail.com with ESMTPSA id y124sm26335569pgb.29.2018.03.06.09.33.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Mar 2018 09:33:48 -0800 (PST) Subject: Re: [PATCH 7/7] Documentation for Pmalloc To: Igor Stoppa , david@fromorbit.com, willy@infradead.org, keescook@chromium.org, mhocko@kernel.org Cc: labbott@redhat.com, linux-security-module@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com References: <20180228200620.30026-1-igor.stoppa@huawei.com> <20180228200620.30026-8-igor.stoppa@huawei.com> From: J Freyensee Message-ID: <226151f3-bec6-e6f7-d7b7-7ad0c391865b@gmail.com> Date: Tue, 6 Mar 2018 09:33:44 -0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <20180228200620.30026-8-igor.stoppa@huawei.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Minus the comment-fixes Mike Rapoport mentioned, looks good: Reviewed-by: Jay Freyensee On 2/28/18 12:06 PM, Igor Stoppa wrote: > Detailed documentation about the protectable memory allocator. > > Signed-off-by: Igor Stoppa > --- > Documentation/core-api/index.rst | 1 + > Documentation/core-api/pmalloc.rst | 111 +++++++++++++++++++++++++++++++++++++ > 2 files changed, 112 insertions(+) > create mode 100644 Documentation/core-api/pmalloc.rst > > diff --git a/Documentation/core-api/index.rst b/Documentation/core-api/index.rst > index c670a8031786..8f5de42d6571 100644 > --- a/Documentation/core-api/index.rst > +++ b/Documentation/core-api/index.rst > @@ -25,6 +25,7 @@ Core utilities > genalloc > errseq > printk-formats > + pmalloc > > Interfaces for kernel debugging > =============================== > diff --git a/Documentation/core-api/pmalloc.rst b/Documentation/core-api/pmalloc.rst > new file mode 100644 > index 000000000000..8fb9c9d3171b > --- /dev/null > +++ b/Documentation/core-api/pmalloc.rst > @@ -0,0 +1,111 @@ > +.. SPDX-License-Identifier: GPL-2.0 > + > +Protectable memory allocator > +============================ > + > +Purpose > +------- > + > +The pmalloc library is meant to provide R/O status to data that, for some > +reason, could neither be declared as constant, nor could it take advantage > +of the qualifier __ro_after_init, but is write-once and read-only in spirit. > +It protects data from both accidental and malicious overwrites. > + > +Example: A policy that is loaded from userspace. > + > + > +Concept > +------- > + > +pmalloc builds on top of genalloc, using the same concept of memory pools. > + > +The value added by pmalloc is that now the memory contained in a pool can > +become R/O, for the rest of the life of the pool. > + > +Different kernel drivers and threads can use different pools, for finer > +control of what becomes R/O and when. And for improved lockless concurrency. > + > + > +Caveats > +------- > + > +- Memory freed while a pool is not yet protected will be reused. > + > +- Once a pool is protected, it's not possible to allocate any more memory > + from it. > + > +- Memory "freed" from a protected pool indicates that such memory is not > + in use anymore by the requester; however, it will not become available > + for further use, until the pool is destroyed. > + > +- pmalloc does not provide locking support with respect to allocating vs > + protecting an individual pool, for performance reasons. > + It is recommended not to share the same pool between unrelated functions. > + Should sharing be a necessity, the user of the shared pool is expected > + to implement locking for that pool. > + > +- pmalloc uses genalloc to optimize the use of the space it allocates > + through vmalloc. Some more TLB entries will be used, however less than > + in the case of using vmalloc directly. The exact number depends on the > + size of each allocation request and possible slack. > + > +- Considering that not much data is supposed to be dynamically allocated > + and then marked as read-only, it shouldn't be an issue that the address > + range for pmalloc is limited, on 32-bit systems. > + > +- Regarding SMP systems, the allocations are expected to happen mostly > + during an initial transient, after which there should be no more need to > + perform cross-processor synchronizations of page tables. > + > +- To facilitate the conversion of existing code to pmalloc pools, several > + helper functions are provided, mirroring their kmalloc counterparts. > + > + > +Use > +--- > + > +The typical sequence, when using pmalloc, is: > + > +1. create a pool > + > +.. kernel-doc:: include/linux/pmalloc.h > + :functions: pmalloc_create_pool > + > +2. [optional] pre-allocate some memory in the pool > + > +.. kernel-doc:: include/linux/pmalloc.h > + :functions: pmalloc_prealloc > + > +3. issue one or more allocation requests to the pool with locking as needed > + > +.. kernel-doc:: include/linux/pmalloc.h > + :functions: pmalloc > + > +.. kernel-doc:: include/linux/pmalloc.h > + :functions: pzalloc > + > +4. initialize the memory obtained with desired values > + > +5. [optional] iterate over points 3 & 4 as needed > + > +6. write-protect the pool > + > +.. kernel-doc:: include/linux/pmalloc.h > + :functions: pmalloc_protect_pool > + > +7. use in read-only mode the handles obtained through the allocations > + > +8. [optional] release all the memory allocated > + > +.. kernel-doc:: include/linux/pmalloc.h > + :functions: pfree > + > +9. [optional, but depends on point 8] destroy the pool > + > +.. kernel-doc:: include/linux/pmalloc.h > + :functions: pmalloc_destroy_pool > + > +API > +--- > + > +.. kernel-doc:: include/linux/pmalloc.h