Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp567811imm; Wed, 15 Aug 2018 02:06:17 -0700 (PDT) X-Google-Smtp-Source: AA+uWPwzxuBxSNbJmI78ZftBuVgzztW2GhaVWr9LXtaXSCOrFPVJQKsp9f/RxmKIZLHk9z9U240j X-Received: by 2002:a17:902:9883:: with SMTP id s3-v6mr23636272plp.194.1534323976955; Wed, 15 Aug 2018 02:06:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534323976; cv=none; d=google.com; s=arc-20160816; b=kbzJNpcaJrOqSUNRg58Zk0QN3W52gL87NTVUjo6VqJILnNQ2w7Dh5GpVoxBu9J+O2+ Pnrt2NynTumW09npy59xusHjewJV94FtaV0iZFw6vYRqlJp1zE11clZEtXqfp7dKnT/I uSbIfZOqdumt0239Fw3K7AIe6hCIDOjzUCV267/+SozvUVAVFksdmyov7O4G91bYMM6N xxOyH3IXpFcgIDDbUaJol+M+w9nzGOYoEgiOU1cQjErXCrrY0PklEjuVIV08nNz1SWn3 RDvPxHhKKfowYm3fPkebxQHHHsXIBr2id+cAqS8IpeKg5CKu4LqYgwOVvBEAul+qTTqi Q04g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:user-agent:in-reply-to :content-disposition:mime-version:references:subject:cc:to:from:date :arc-authentication-results; bh=lNuFncVxAli6/c7e55tWrq6zLKbtVChhyR3JCpBRDMY=; b=ZuK7JvTbwnaOyDpKaxSvfD7FRKdjH7YsJp/XsUR503Oazfrzuu6GeKu5QImhO+Zgk0 Jfvbd5SqqbzFJkFGSI8cGlj3WcefqeSUoe8CsUcBnKbywQOylTm02OxE7CyU78JKqxgH QJV8dNj5jkoa7PesvIXRmqWSWi0VCzAM1nJJblnngubQRnSiKQqJl8r7LaO6aeLOym1L eUiw8RjEglF9RV9H+PlbTdbEFwY2uMdf8vfCNWVimTPUnv7nNncVQx5l4NcOHLGe91Rd jlQA8XDRh94kWt+uBH80iMVvrp4GzZDCs22WPxnwAuLHjrtbpS20k0maJBBsPiQwXwR1 x3Tg== 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=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1-v6si6526461plt.148.2018.08.15.02.06.00; Wed, 15 Aug 2018 02:06:16 -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=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728914AbeHOLz6 (ORCPT + 99 others); Wed, 15 Aug 2018 07:55:58 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:60928 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728576AbeHOLz6 (ORCPT ); Wed, 15 Aug 2018 07:55:58 -0400 Received: from pps.filterd (m0098417.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w7F94AnZ086224 for ; Wed, 15 Aug 2018 05:04:38 -0400 Received: from e06smtp01.uk.ibm.com (e06smtp01.uk.ibm.com [195.75.94.97]) by mx0a-001b2d01.pphosted.com with ESMTP id 2kvfxrk4b1-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 15 Aug 2018 05:04:37 -0400 Received: from localhost by e06smtp01.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 15 Aug 2018 10:04:36 +0100 Received: from b06cxnps4076.portsmouth.uk.ibm.com (9.149.109.198) by e06smtp01.uk.ibm.com (192.168.101.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Wed, 15 Aug 2018 10:04:32 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w7F94VWu41812154 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 15 Aug 2018 09:04:31 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 288EC11C05B; Wed, 15 Aug 2018 12:04:35 +0100 (BST) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 88F4F11C04C; Wed, 15 Aug 2018 12:04:34 +0100 (BST) Received: from rapoport-lnx (unknown [9.148.8.197]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Wed, 15 Aug 2018 12:04:34 +0100 (BST) Date: Wed, 15 Aug 2018 12:04:29 +0300 From: Mike Rapoport To: Michal Hocko Cc: linux-mm@kvack.org, Jonathan Corbet , Matthew Wilcox , Vlastimil Babka , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [RFC PATCH] docs/core-api: add memory allocation guide References: <1534314887-9202-1-git-send-email-rppt@linux.vnet.ibm.com> <20180815063649.GB24091@rapoport-lnx> <20180815081539.GN32645@dhcp22.suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180815081539.GN32645@dhcp22.suse.cz> User-Agent: Mutt/1.5.24 (2015-08-30) X-TM-AS-GCONF: 00 x-cbid: 18081509-4275-0000-0000-000002AA14A5 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18081509-4276-0000-0000-000037B3351F Message-Id: <20180815090428.GD24091@rapoport-lnx> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-08-15_04:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1808150099 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 15, 2018 at 10:15:39AM +0200, Michal Hocko wrote: > On Wed 15-08-18 09:36:49, Mike Rapoport wrote: > > (this time with the subject, sorry for the noise) > > > > On Wed, Aug 15, 2018 at 09:34:47AM +0300, Mike Rapoport wrote: > > > As Vlastimil mentioned at [1], it would be nice to have some guide about > > > memory allocation. I've drafted an initial version that tries to summarize > > > "best practices" for allocation functions and GFP usage. > > > > > > [1] https://www.spinics.net/lists/netfilter-devel/msg55542.html > > > > > > From 8027c0d4b750b8dbd687234feda63305d0d5a057 Mon Sep 17 00:00:00 2001 > > > From: Mike Rapoport > > > Date: Wed, 15 Aug 2018 09:10:06 +0300 > > > Subject: [RFC PATCH] docs/core-api: add memory allocation guide > > > > > > Signed-off-by: Mike Rapoport > > > --- > > > Documentation/core-api/gfp_mask-from-fs-io.rst | 2 + > > > Documentation/core-api/index.rst | 1 + > > > Documentation/core-api/memory-allocation.rst | 117 +++++++++++++++++++++++++ > > > Documentation/core-api/mm-api.rst | 2 + > > > 4 files changed, 122 insertions(+) > > > create mode 100644 Documentation/core-api/memory-allocation.rst > > > > > > diff --git a/Documentation/core-api/gfp_mask-from-fs-io.rst b/Documentation/core-api/gfp_mask-from-fs-io.rst > > > index e0df8f4..e7c32a8 100644 > > > --- a/Documentation/core-api/gfp_mask-from-fs-io.rst > > > +++ b/Documentation/core-api/gfp_mask-from-fs-io.rst > > > @@ -1,3 +1,5 @@ > > > +.. _gfp_mask_from_fs_io: > > > + > > > ================================= > > > GFP masks used from FS/IO context > > > ================================= > > > diff --git a/Documentation/core-api/index.rst b/Documentation/core-api/index.rst > > > index cdc2020..8afc0da 100644 > > > --- a/Documentation/core-api/index.rst > > > +++ b/Documentation/core-api/index.rst > > > @@ -27,6 +27,7 @@ Core utilities > > > errseq > > > printk-formats > > > circular-buffers > > > + memory-allocation > > > mm-api > > > gfp_mask-from-fs-io > > > timekeeping > > > diff --git a/Documentation/core-api/memory-allocation.rst b/Documentation/core-api/memory-allocation.rst > > > new file mode 100644 > > > index 0000000..b1f2ad5 > > > --- /dev/null > > > +++ b/Documentation/core-api/memory-allocation.rst > > > @@ -0,0 +1,117 @@ > > > +======================= > > > +Memory Allocation Guide > > > +======================= > > > + > > > +Linux supplies variety of APIs for memory allocation. You can allocate > > > +small chunks using `kmalloc` or `kmem_cache_alloc` families, large > > > +virtually contiguous areas using `vmalloc` and it's derivatives, or > > > +you can directly request pages from the page allocator with > > > +`__get_free_pages`. It is also possible to use more specialized > > I would rather not mention __get_free_pages. alloc_pages is a more > generic API and less subtle one. If you want to mention __get_free_pages > then please make sure to mention the subtlety (namely that is can > allocate only lowmem memory). > > > > +allocators, for instance `cma_alloc` or `zs_malloc`. > > > + > > > +Most of the memory allocations APIs use GFP flags to express how that > > > +memory should be allocated. The GFP acronym stands for "get free > > > +pages", the underlying memory allocation function. > > > + > > > +Diversity of the allocation APIs combined with the numerous GFP flags > > > +makes the question "How should I allocate memory?" not that easy to > > > +answer, although very likely you should use > > > + > > > +:: > > > + > > > + kzalloc(, GFP_KERNEL); > > > + > > > +Of course there are cases when other allocation APIs and different GFP > > > +flags must be used. > > > + > > > +Get Free Page flags > > > +=================== > > > + > > > +The GFP flags control the allocators behavior. They tell what memory > > > +zones can be used, how hard the allocator should try to find a free > > > +memory, whether the memory can be accessed by the userspace etc. The > > > +:ref:`Documentation/core-api/mm-api.rst ` provides > > > +reference documentation for the GFP flags and their combinations and > > > +here we briefly outline their recommended usage: > > > + > > > + * Most of the times ``GFP_KERNEL`` is what you need. Memory for the > > > + kernel data structures, DMAable memory, inode cache, all these and > > > + many other allocations types can use ``GFP_KERNEL``. Note, that > > > + using ``GFP_KERNEL`` implies ``GFP_RECLAIM``, which means that > > > + direct reclaim may be triggered under memory pressure; the calling > > > + context must be allowed to sleep. > > > + * If the allocation is performed from an atomic context, e.g > > > + interrupt handler, use ``GFP_ATOMIC``. > > GFP_NOWAIT please. GFP_ATOMIC should be only used if accessing memory > reserves is justified. E.g. fallback allocation would be too costly. It > should be also noted that these allocation are quite likely to fail > especially under memory pressure. How about: * If the allocation is performed from an atomic context, e.g interrupt handler, use ``GFP_NOWARN``. This flag prevents direct reclaim and IO or filesystem operations. Consequently, under memory pressure ``GFP_NOWARN`` allocation is likely to fail. * If you think that accessing memory reserves is justified and the kernel will be stressed unless allocation succeeds, you may use ``GFP_ATOMIC``. > > > + * Untrusted allocations triggered from userspace should be a subject > > > + of kmem accounting and must have ``__GFP_ACCOUNT`` bit set. There > > > + is handy ``GFP_KERNEL_ACCOUNT`` shortcut for ``GFP_KERNEL`` > > > + allocations that should be accounted. > > > + * Userspace allocations should use either of the ``GFP_USER``, > > > + ``GFP_HIGHUSER`` and ``GFP_HIGHUSER_MOVABLE`` flags. The longer > > > + the flag name the less restrictive it is. > > > + > > > + The ``GFP_HIGHUSER_MOVABLE`` does not require that allocated > > > + memory will be directly accessible by the kernel or the hardware > > > + and implies that the data may move. > > @may move@is movable@ Ok > > > + The ``GFP_HIGHUSER`` means that the allocated memory is not > > > + movable, but it is not required to be directly accessible by the > > > + kernel or the hardware. An example may be a hardware allocation > > > + that maps data directly into userspace but has no addressing > > > + limitations. > > > + > > > + The ``GFP_USER`` means that the allocated memory is not movable > > > + and it must be directly accessible by the kernel or the > > > + hardware. It is typically used by hardware for buffers that are > > > + mapped to userspace (e.g. graphics) that hardware still must DMA > > > + to. > > > + > > > +You may notice that quite a few allocations in the existing code > > > +specify ``GFP_NOIO`` and ``GFP_NOFS``. Historically, they were used to > > > +prevent recursion deadlocks caused by direct memory reclaim calling > > > +back into the FS or IO paths and blocking on already held > > > +resources. Since 4.12 the preferred way to address this issue is to > > > +use new scope APIs described in > > > +:ref:`Documentation/core-api/gfp_mask-from-fs-io.rst `. > > > + > > > +Another legacy GFP flags are ``GFP_DMA`` and ``GFP_DMA32``. They are > > > +used to ensure that the allocated memory is accessible by hardware > > > +with limited addressing capabilities. So unless you are writing a > > > +driver for a device with such restrictions, avoid using these flags. > > And even with HW with restrictions it is preferable to use dma_alloc* > APIs Will add. > Looks nice otherwise. Thanks! With the above changes feel free to add > Acked-by: Michal Hocko Thanks! > -- > Michal Hocko > SUSE Labs > -- Sincerely yours, Mike.