Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1853483pxk; Tue, 1 Sep 2020 09:16:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/R93Vl+hCPJLPQlA82oDw1TMa9vaexifbS42dWcnO93t1l5N1UHDevIJuMcwxNoA8KJYZ X-Received: by 2002:a17:906:3785:: with SMTP id n5mr2318833ejc.218.1598977002975; Tue, 01 Sep 2020 09:16:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598977002; cv=none; d=google.com; s=arc-20160816; b=06WLnOwd4JblOq2VRUyO9S7nwKsP8hbqUYF3/mBknyFqqHr97cJqMS9mPBp1uNbSVp PCSaxPy7RfX/WFCJiBciOrmUISzQLSiyb+N5WCVzBZDX0MVd3XR9LHHMYt1IstpigkkW b5/y8c8dIRGBTc4PKnlcu7UX2SpbTPodBZMv22XIglrN5rXkCcVJXudj2IktzMX5pYFT jdUy4kINKDKVWvcVtdXywN6TRAlw92urv663EbM+ftKbkVcBwKTMVKeqt/ArguZVXind oSQDQRle38AfKqa5Z9c+vhAwPEv8di0WQyiNM0HrEAsoks4+z42enpTalc0gpqI9DuFT RuPw== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=U1h0WFxrmlZti/5PdriJmCxO8e4BuJ9nFjihIY3qycs=; b=PwxMoU5CZ9+4UNPZauMEYBzDwdwTOZJTUcRawVjl7WRWMqgK/51kkyFq82hvE61P3l Njl+pCqSjGP64E3WvsdEwmPCgPWVuFIMRNcvFY0Xly8v72zzjHd372mY4oCZCRor63su feaWmmXjmz5Iq85ypM+OAJq6B0Q5SdeNDNtYqg7mufkmHSQnIMUOFdunl64x2a90lYTd 7rcYOeb+Z2fotrIxYK68x2uXSEqlGDUe5r3+v6xTK1l45UjldOvNUAhvIg1O4A6S6rKv MT0eKxQkcOsxLfUpzBr7PgmE3wOce587dqXUP8+u/w2pqGoZ8YiVo7hqd5LDW2e+Cebv vWvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=DMJZzRBu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bx2si826281edb.377.2020.09.01.09.16.18; Tue, 01 Sep 2020 09:16:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=DMJZzRBu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730619AbgIAQPq (ORCPT + 99 others); Tue, 1 Sep 2020 12:15:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731944AbgIAQPj (ORCPT ); Tue, 1 Sep 2020 12:15:39 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34931C061244 for ; Tue, 1 Sep 2020 09:15:39 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id u20so1079162pfn.0 for ; Tue, 01 Sep 2020 09:15:39 -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=DMJZzRBurdYUN4x8gReiESvjIjB6WMHksdeGaRDN41WunAsqZ5Yvt1bO5dntkRFy3t 8wwhIkp4rgfa2J7aKnYStujtMWjfHwnF6BkSHyc7XPqxl15S8Wx58seJIlrKeXoJmuGj MQaukCMelikiVQ/1Wm711W7exb+6DNt43lv72RbxdW6K74pL+q9Y56zYnklCSiMeeL35 pRebHGva3Zi2MmJ3dxDLMqa5QnCLLLYiGadJKrLiaLGeasmp+w0PpyFwxsv0zQw8Or3z 1eho9mmEcHG7AK6OcGSov1ce9KH+R9Js6Mtep8s4aW9yT+PGaaaWQiXvT8tY8YznovIj 7SWg== 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=S6wjgjAokvWbx4TlUtzCFI7yzkqey3uLWZNbN2ykwU/ac/5tN5PBADTzmIDFdmhnxB wYoPmVkFwa5dk9hDju80+cDH1BqsrSiBZ9MSRF0++pu9i0ZRjYuAvEIL7/xPsdobF1y5 NopOjpxqztVC01PCDNEIv/r3apMjfG4E58Sm2L2noQblZxZd60/kKeLwtImaWGt6hvnE ugMdPY0uFsTqms4KXNowpzFYCOVaCjLN1d24m103AvTom7q4z2wFgwFu+7wVPgA3McY8 2+GZLcgcGojuVNmUgbeoaviyQ0YsA/nwiTbz6wa/bCUw36HYbFLHruyqSTP/hGfhGvhv E1uQ== X-Gm-Message-State: AOAM531D9TWmtMwjb2jrutl39B4mhewWvjqqftCvAAWTxYPpMYrIc5Nx WHQzh/y0jIskvkNPZMT1TZov1Q== X-Received: by 2002:a63:1b42:: with SMTP id b2mr1968103pgm.397.1598976938667; Tue, 01 Sep 2020 09:15:38 -0700 (PDT) Received: from nagraj.lan ([175.100.146.50]) by smtp.gmail.com with ESMTPSA id d77sm2553169pfd.121.2020.09.01.09.15.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Sep 2020 09:15: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 , linux-fsdevel@vger.kernel.org, John Stultz , Sumit Semwal Subject: [PATCH v7 2/3] mm: memory: Add access_remote_vm_locked variant Date: Tue, 1 Sep 2020 21:44:58 +0530 Message-Id: <20200901161459.11772-3-sumit.semwal@linaro.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200901161459.11772-1-sumit.semwal@linaro.org> References: <20200901161459.11772-1-sumit.semwal@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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