From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC76EC433E7 for ; Tue, 1 Sep 2020 09:19:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8084920866 for ; Tue, 1 Sep 2020 09:19:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="ZZxynk0Q" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726386AbgIAJTm (ORCPT ); Tue, 1 Sep 2020 05:19:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726310AbgIAJTi (ORCPT ); Tue, 1 Sep 2020 05:19:38 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0F34C061245 for ; Tue, 1 Sep 2020 02:19:38 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id u20so465211pfn.0 for ; Tue, 01 Sep 2020 02:19:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=U1h0WFxrmlZti/5PdriJmCxO8e4BuJ9nFjihIY3qycs=; b=ZZxynk0QlZfIRPJ4QGFzdKtFDLdfBU4R03PfY90TJ1NvYSWDbI0J9xD1LWHS8sJrEO Z550Jx5HfEQ3+mYsR3tdgjx8JVldlKUh+xa1Zg8d9Y5nEWWBXg/9FVMbCOTxmDUnHXOX xgnmx/rEhFv2BDg7w6UcV1niuf49OQV4lZ4m1syZHamEPTa7VryLxjRel9O+T9y5nAiW yDcJ0Yw+5H9QCVwGKCB/puFq4Og4mu4AVJlAQbOm1rwhOW/abH2hARkKzZVw/zWA21Gm xkCSsB18jzELz/vi7ZS/hUH6O7zwPtpAWJcGTENAJl05+M+UiM7mvi8bv4vtAZlhTnQz Jegw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=U1h0WFxrmlZti/5PdriJmCxO8e4BuJ9nFjihIY3qycs=; b=QXVoY7DXQ+9jD8CERnciTaxBf8GTUWKWtrrBvLfmwwECm5tdb5EgjnIkaNhOaYXAoF UZ2RY/7k43Q2ZjjLZVjiEDRv0YijT66jfc33T8GqHY+KwNa1XahDvz78LghWI8OeQYcs 2gJcgq7lIhEqeSXNXab8pYYYdLwMP44chMGt0fHByQKWZ40LdCYwfhkjBaKtFFWplScy Yv64NsWe+3yh+6AYESWVUg/RkGgCens4RGoOHriiVlGgXryY8IkB6Q9wFnzwgp2JV4T5 FPOWItViWFEQRaPqFk/V5pka2jdNyjmeJ6sgJPGsqZDf5RtvMHgyxCfAwJsenFjONqIj BPWg== X-Gm-Message-State: AOAM532Gk4olx0g07uxMOGI6udwrmhrlZ4K/+yEo4C2c1UPhJ5HU5/Nl tqDLR7MVp18GuDUqBEI9LsSS9A== X-Google-Smtp-Source: ABdhPJzVLT26wXuJg9u2T4boVuRqfvUnGNG0RnLFl6IKknv4osKlaL5cs8MXwfoVPfY2s2rSIUQIPA== X-Received: by 2002:a63:3fc4:: with SMTP id m187mr737501pga.322.1598951978300; Tue, 01 Sep 2020 02:19:38 -0700 (PDT) Received: from nagraj.local ([49.206.21.239]) by smtp.gmail.com with ESMTPSA id u191sm1337707pgu.56.2020.09.01.02.19.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Sep 2020 02:19:37 -0700 (PDT) From: Sumit Semwal To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Alexey Dobriyan , Jonathan Corbet Cc: Mauro Carvalho Chehab , Kees Cook , Michal Hocko , Colin Cross , Alexey Gladkov , Matthew Wilcox , Jason Gunthorpe , "Kirill A . Shutemov" , Michel Lespinasse , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Song Liu , Huang Ying , Vlastimil Babka , Yang Shi , chenqiwu , Mathieu Desnoyers , John Hubbard , Mike Christie , Bart Van Assche , Amit Pundir , Thomas Gleixner , Christian Brauner , Daniel Jordan , Adrian Reber , Nicolas Viennot , Al Viro , Thomas Cedeno , linux-fsdevel@vger.kernel.org, John Stultz , Sumit Semwal Subject: [PATCH v6 2/3] mm: memory: Add access_remote_vm_locked variant Date: Tue, 1 Sep 2020 14:49:00 +0530 Message-Id: <20200901091901.19779-3-sumit.semwal@linaro.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200901091901.19779-1-sumit.semwal@linaro.org> References: <20200901091901.19779-1-sumit.semwal@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org This allows accessing a remote vm while the mmap_lock is already held by the caller. While adding support for anonymous vma naming, show_map_vma() needs to access the remote vm to get the name of the anonymous vma. Since show_map_vma() already holds the mmap_lock, so this _locked variant was required. Signed-off-by: Sumit Semwal --- include/linux/mm.h | 2 ++ mm/memory.c | 49 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index ca6e6a81576b..e9212c0bb5ac 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1708,6 +1708,8 @@ extern int access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, int len, unsigned int gup_flags); extern int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, unsigned long addr, void *buf, int len, unsigned int gup_flags); +extern int access_remote_vm_locked(struct mm_struct *mm, unsigned long addr, + void *buf, int len, unsigned int gup_flags); long get_user_pages_remote(struct mm_struct *mm, unsigned long start, unsigned long nr_pages, diff --git a/mm/memory.c b/mm/memory.c index 602f4283122f..207be99390e9 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4726,17 +4726,17 @@ EXPORT_SYMBOL_GPL(generic_access_phys); /* * Access another process' address space as given in mm. If non-NULL, use the * given task for page fault accounting. + * This variant assumes that the mmap_lock is already held by the caller, so + * doesn't take the mmap_lock. */ -int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, - unsigned long addr, void *buf, int len, unsigned int gup_flags) +int __access_remote_vm_locked(struct task_struct *tsk, struct mm_struct *mm, + unsigned long addr, void *buf, int len, + unsigned int gup_flags) { struct vm_area_struct *vma; void *old_buf = buf; int write = gup_flags & FOLL_WRITE; - if (mmap_read_lock_killable(mm)) - return 0; - /* ignore errors, just check how much was successfully transferred */ while (len) { int bytes, ret, offset; @@ -4785,9 +4785,46 @@ int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, buf += bytes; addr += bytes; } + return buf - old_buf; +} + +/* + * Access another process' address space as given in mm. If non-NULL, use the + * given task for page fault accounting. + */ +int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, + unsigned long addr, void *buf, int len, unsigned int gup_flags) +{ + int ret; + + if (mmap_read_lock_killable(mm)) + return 0; + + ret = __access_remote_vm_locked(tsk, mm, addr, buf, len, gup_flags); mmap_read_unlock(mm); - return buf - old_buf; + return ret; +} + +/** + * access_remote_vm_locked - access another process' address space, without + * taking the mmap_lock. This allows nested calls from callers that already have + * taken the lock. + * + * @mm: the mm_struct of the target address space + * @addr: start address to access + * @buf: source or destination buffer + * @len: number of bytes to transfer + * @gup_flags: flags modifying lookup behaviour + * + * The caller must hold a reference on @mm, as well as hold the mmap_lock + * + * Return: number of bytes copied from source to destination. + */ +int access_remote_vm_locked(struct mm_struct *mm, unsigned long addr, void *buf, + int len, unsigned int gup_flags) +{ + return __access_remote_vm_locked(NULL, mm, addr, buf, len, gup_flags); } /** -- 2.28.0