Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp6664885ybi; Wed, 29 May 2019 11:07:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqwF23h+CcLo3Bzin8YKgRMp/YeG2TAis8Y18dp74flu0jR19F2Doe42XQTzVh/t7tAqiQk0 X-Received: by 2002:a17:902:bf05:: with SMTP id bi5mr4092686plb.341.1559153244833; Wed, 29 May 2019 11:07:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559153244; cv=none; d=google.com; s=arc-20160816; b=L/F2iUqyZsZ3CpsabS13FCrrfMdJo5U01Rbt5f+ggW/AuhwudqBEYdI4Agl2ciHZfA qgSMclsv7Gqo4B3AA24vM7+RTrW+J04VpEAMI2mPQnuQM+9W+v906504fQ06OVSAlpOf ExQQEl1Ok882SBAnc0GA1OuL8Q7hwYHeylNxW1fL0CWpjk5oB4Tqp4y+XNLk27D8rdAp NZYJP4Ka3ZWheUuDsJX1jP8BNBcT7CpCFE4T7ECnqdFAX/PlNV91Wf4hw5zsY3+Bj7tl EQDk6Ve4U+nwwKEkO12kiOn28f0LLqRF6+Jv4nbyXSzww1+7T80cJ3xkKgVleJbL1cxl BjJQ== 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=6pwKEIjHhhdYh2aV+QDVDoOKT+9lEeGbCVsj5xLoLt8=; b=CFlsMYUTt8EH4JvmFmNoX9DA4rpwZqT7kELsHvuURIoQi/XkAtxrB7vob3d4+uLlZ7 wGIaf8uYL1Q5Lkv4XLgBCL37GnxcKH06nY2vjb5g36MZkqXU2Yq8T2SUhj3HnmBLZg5E 2l3Z75IUpi29Ls7/0mSAEWSBiCsuG+xk1lVG9zCgot9mJnvMm3Z2Ida37jKIYHnfmibv 2Zgf7dB8FbIJkPU/JR8P+uXdVeVknZN47X/C6AwlkbkiaLnoX5HWdfdHcSJq3GKzUEbr yKGwjnC1xrI8jacxl+ebWDlypL2F1E79sLdgjG4hbTU+gZ+ncBaIjvdkG4acP7i+Tyzz rfrQ== 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=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o6si43191pjp.76.2019.05.29.11.07.06; Wed, 29 May 2019 11:07:24 -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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727275AbfE2SEt (ORCPT + 99 others); Wed, 29 May 2019 14:04:49 -0400 Received: from mga18.intel.com ([134.134.136.126]:64466 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725990AbfE2SEs (ORCPT ); Wed, 29 May 2019 14:04:48 -0400 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 May 2019 11:04:47 -0700 X-ExtLoop1: 1 Received: from iweiny-desk2.sc.intel.com ([10.3.52.157]) by orsmga002.jf.intel.com with ESMTP; 29 May 2019 11:04:46 -0700 Date: Wed, 29 May 2019 11:05:48 -0700 From: Ira Weiny To: Daniel Jordan Cc: akpm@linux-foundation.org, Alexey Kardashevskiy , Alan Tull , Alex Williamson , Benjamin Herrenschmidt , Christoph Lameter , Christophe Leroy , Davidlohr Bueso , Jason Gunthorpe , Mark Rutland , Michael Ellerman , Moritz Fischer , Paul Mackerras , Steve Sistare , Wu Hao , linux-mm@kvack.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-fpga@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] mm: add account_locked_vm utility function Message-ID: <20190529180547.GA16182@iweiny-DESK2.sc.intel.com> References: <20190524175045.26897-1-daniel.m.jordan@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190524175045.26897-1-daniel.m.jordan@oracle.com> User-Agent: Mutt/1.11.1 (2018-12-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 24, 2019 at 01:50:45PM -0400, Daniel Jordan wrote: [snip] > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 0e8834ac32b7..72c1034d2ec7 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -1564,6 +1564,25 @@ long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, > int get_user_pages_fast(unsigned long start, int nr_pages, > unsigned int gup_flags, struct page **pages); > > +int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, > + struct task_struct *task, bool bypass_rlim); > + > +static inline int account_locked_vm(struct mm_struct *mm, unsigned long pages, > + bool inc) > +{ > + int ret; > + > + if (pages == 0 || !mm) > + return 0; > + > + down_write(&mm->mmap_sem); > + ret = __account_locked_vm(mm, pages, inc, current, > + capable(CAP_IPC_LOCK)); > + up_write(&mm->mmap_sem); > + > + return ret; > +} > + > /* Container for pinned pfns / pages */ > struct frame_vector { > unsigned int nr_allocated; /* Number of frames we have space for */ > diff --git a/mm/util.c b/mm/util.c > index e2e4f8c3fa12..bd3bdf16a084 100644 > --- a/mm/util.c > +++ b/mm/util.c > @@ -6,6 +6,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -346,6 +347,51 @@ int __weak get_user_pages_fast(unsigned long start, > } > EXPORT_SYMBOL_GPL(get_user_pages_fast); > > +/** > + * __account_locked_vm - account locked pages to an mm's locked_vm > + * @mm: mm to account against, may be NULL This kernel doc is wrong. You dereference mm straight away... > + * @pages: number of pages to account > + * @inc: %true if @pages should be considered positive, %false if not > + * @task: task used to check RLIMIT_MEMLOCK > + * @bypass_rlim: %true if checking RLIMIT_MEMLOCK should be skipped > + * > + * Assumes @task and @mm are valid (i.e. at least one reference on each), and > + * that mmap_sem is held as writer. > + * > + * Return: > + * * 0 on success > + * * 0 if @mm is NULL (can happen for example if the task is exiting) > + * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. > + */ > +int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, > + struct task_struct *task, bool bypass_rlim) > +{ > + unsigned long locked_vm, limit; > + int ret = 0; > + > + locked_vm = mm->locked_vm; here... Perhaps the comment was meant to document account_locked_vm()? Or should the parameter checks be moved here? Ira > + if (inc) { > + if (!bypass_rlim) { > + limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; > + if (locked_vm + pages > limit) > + ret = -ENOMEM; > + } > + if (!ret) > + mm->locked_vm = locked_vm + pages; > + } else { > + WARN_ON_ONCE(pages > locked_vm); > + mm->locked_vm = locked_vm - pages; > + } > + > + pr_debug("%s: [%d] caller %ps %c%lu %lu/%lu%s\n", __func__, task->pid, > + (void *)_RET_IP_, (inc) ? '+' : '-', pages << PAGE_SHIFT, > + locked_vm << PAGE_SHIFT, task_rlimit(task, RLIMIT_MEMLOCK), > + ret ? " - exceeded" : ""); > + > + return ret; > +} > +EXPORT_SYMBOL_GPL(__account_locked_vm); > > + > unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, > unsigned long len, unsigned long prot, > unsigned long flag, unsigned long pgoff) > > base-commit: a188339ca5a396acc588e5851ed7e19f66b0ebd9 > -- > 2.21.0 >