From patchwork Tue Jan 23 23:10:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13528267 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B1EEC47DDC for ; Tue, 23 Jan 2024 23:10:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9AE7A6B0083; Tue, 23 Jan 2024 18:10:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 936EC6B0088; Tue, 23 Jan 2024 18:10:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 715D66B0089; Tue, 23 Jan 2024 18:10:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 5193B6B0083 for ; Tue, 23 Jan 2024 18:10:24 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2C6D3A1A09 for ; Tue, 23 Jan 2024 23:10:24 +0000 (UTC) X-FDA: 81712121568.15.31A2AC6 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf23.hostedemail.com (Postfix) with ESMTP id 669E3140009 for ; Tue, 23 Jan 2024 23:10:22 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=zE5qGg3A; spf=pass (imf23.hostedemail.com: domain of 3XUewZQYKCOAUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3XUewZQYKCOAUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706051422; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iOH4eD/kP+yC8NUPTXlai05Ia1nTXRgcymGMGu85eLQ=; b=wTJy6419seVTlvd0xsnvWzSeJoFDyVNUPaIZWHPLCGOemt/jPHgCVCisbbPcMDMev32eNZ lqFKXdLLT6EqO+xdx+Drtl7ISFX82kMW/nxY2fKSnVKnC/J+vVqT2z0wUvRhb911f7EyAH BlRhxekncPl9Cuu5h1clbyIPzrdlUzs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706051422; a=rsa-sha256; cv=none; b=Q8gNPxk9MMjseRxgdzD7Khbyr9O0qezWGOyLt3i73HCZWeeoX63L40A7JCY4JrRxLkVpAg 0x84+cVLMPoG+NTEKDhTuuUkCH2Y40hxC8mlwFfc+Z4AOjycWF1+EOOdo4alJ4TnC0piCe tPoMmJEJr3B3gWZ0NKldMbaETZFBmjA= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=zE5qGg3A; spf=pass (imf23.hostedemail.com: domain of 3XUewZQYKCOAUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3XUewZQYKCOAUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5f0fd486b9aso60978667b3.2 for ; Tue, 23 Jan 2024 15:10:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706051421; x=1706656221; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iOH4eD/kP+yC8NUPTXlai05Ia1nTXRgcymGMGu85eLQ=; b=zE5qGg3A52Y55Mc6pBFO5+CvTnZDcbuEU4QDz3LTFBFkY75L+M0XcCueblbK+s6msr OqnR7yGB8n2d1uMEcirzqyuCjOAwczNPE4JLGUf3Sye6WIZqQFWTWWE9Lcjn/L/mnGa/ HihH4OIoSKVLePaHAVuWlvnvScsbezSYmfslTakaxXg3tG2IY0c07MbyMZzp6ho6yA51 Tx0AAeT6Hghwf10roIkd3+mGw/IXIu1RLdEi2VqdwbWkhAte74ivwYJUqup0Opql6Lqr MCpyhrDsPIIrPKJlHQfmjAIADrECTodlkV+XsKAH0kxVjBi+y577qQizy1MVwleSfsit O1rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706051421; x=1706656221; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=iOH4eD/kP+yC8NUPTXlai05Ia1nTXRgcymGMGu85eLQ=; b=lfTvGCRYNVCsez8hFa2gyFgHYxk4WELNe+qY0w++VOjTUoSSvt8ME8XhDFk2YeJ31l Gaa9JEIfc7sGCwe9JF864nWO/3EIia5s1+07aH8QbDokuAKQZvLZ7BF4A/8dhEDCxwB5 gKWxrIEngiAVrqO/GX9nWteov1hF3zrbabpWVEd93yvOpuPhrGrOOoaN2ubcOOK2jUKF tC3DruTMcEI5ep5GjhR1NbKxeyR8LnKaXILOwakNBSngZsqqvfjXAb75cJThTE6RIN7g jmlVTZkbgpUkwGe89vuyodw/h0/vTBDoqC00evKVBsVBPJ269g7g4zEtkuCq0K+T941z 0lKg== X-Gm-Message-State: AOJu0YziP70zsh/edoJgLmL6HUetroOrZ3pAgWyBAOOlMlDU6L1GyLlq MTYrLtgxLnvioH9Tf3w4wo5oC4R82q7jAah47dFIuo1y0hvX1D/YNbpWF10n23B2dWxuyTH+dF1 GbQ== X-Google-Smtp-Source: AGHT+IFELlpA6dr+hh0ohU9jI75g0Qo+vt9bgKXouAI4UTf2K5w/MytBcB95Yhp71v6eqOEpu4Ziung4r/A= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:8fc3:c34f:d407:388]) (user=surenb job=sendgmr) by 2002:a25:ce11:0:b0:dbd:b909:f090 with SMTP id x17-20020a25ce11000000b00dbdb909f090mr373700ybe.11.1706051421519; Tue, 23 Jan 2024 15:10:21 -0800 (PST) Date: Tue, 23 Jan 2024 15:10:14 -0800 In-Reply-To: <20240123231014.3801041-1-surenb@google.com> Mime-Version: 1.0 References: <20240123231014.3801041-1-surenb@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123231014.3801041-3-surenb@google.com> Subject: [PATCH v2 3/3] mm/maps: read proc/pid/maps under RCU From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, dchinner@redhat.com, casey@schaufler-ca.com, ben.wolsieffer@hefring.com, paulmck@kernel.org, david@redhat.com, avagin@google.com, usama.anjum@collabora.com, peterx@redhat.com, hughd@google.com, ryan.roberts@arm.com, wangkefeng.wang@huawei.com, Liam.Howlett@Oracle.com, yuzhao@google.com, axelrasmussen@google.com, lstoakes@gmail.com, talumbau@google.com, willy@infradead.org, vbabka@suse.cz, mgorman@techsingularity.net, jhubbard@nvidia.com, vishal.moola@gmail.com, mathieu.desnoyers@efficios.com, dhowells@redhat.com, jgg@ziepe.ca, sidhartha.kumar@oracle.com, andriy.shevchenko@linux.intel.com, yangxingui@huawei.com, keescook@chromium.org, sj@kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, kernel-team@android.com, surenb@google.com X-Rspamd-Queue-Id: 669E3140009 X-Rspam-User: X-Rspamd-Server: rsp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Rl0NSDBeQP+DDkzJJdW10vWWkvKYsu/c/P07FCTKW3gosOUUeEeRe1PmTzBTGEV+0wCsBA99kJhKu6pF8xFu+6l+VXMkj+YFADvlhecIIoNAd5Rhj066bGQtYePH7ue2mFHcSZsdgfMKI1/+6w+hXrQ0EfJwEkZKbhKxxi0E1Zp6HWbH8xLqy4yeT6BuvNRteEF455ae23F8Uc7h7zpEmyQymIq+7Njqq0K8r7FYXU41kJjvBNW+N/G99JGmPmzqJQlcXrtjZSM82IWkHV/l7q0Ym7EcR1dmwWnncn7YUAGrwaN2Sy0FbTzDko7S8Bf5YFQmUSWbNOK8FPi1mlPHHaVdL2tCpn3dTV977GwD4w6/izHd+gNa/fSIVAFIY2VvnNSk+ueKBGGIxl50SK/VkF2aMWQIFdC+J2RY1x6bSsENU0Ti8B4jZ7d3i82U1/HNTQSU6xhMPwxevwFY4hFVc1UsUcgEjwbb7P2nYZi/HIkfZKU2gnMyRRClcnwKfsdhkn/O+/OXK7AHdbl1qskKLtA5pMPtD6gtWZ1M+TEI/XYx7u6fCfhuH3KCWH8hk/rJwKk02v+7AUUBaEq3elIZdrRcL95qWci0qWjYM3Dfjc6kIS5yFw+yNkY9U8WgbaNjE1+nDUuGgceWlrsA4doUhc4RfL9mpH+7wPHK5XPGDxNoVkJ7N4aRUPoqjgoAoR15XKM8IoolOwY/H5I7JgODZszSWrOr7o= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: With maple_tree supporting vma tree traversal under RCU and per-vma locks making vma access RCU-safe, /proc/pid/maps can be read under RCU and without the need to read-lock mmap_lock. However vma content can change from under us, therefore we make a copy of the vma and we pin pointer fields used when generating the output (currently only vm_file and anon_name). Afterwards we check for concurrent address space modifications, wait for them to end and retry. That last check is needed to avoid possibility of missing a vma during concurrent maple_tree node replacement, which might report a NULL when a vma is replaced with another one. While we take the mmap_lock for reading during such contention, we do that momentarily only to record new mm_wr_seq counter. This change is designed to reduce mmap_lock contention and prevent a process reading /proc/pid/maps files (often a low priority task, such as monitoring/data collection services) from blocking address space updates. Note that this change has a userspace visible disadvantage: it allows for sub-page data tearing as opposed to the previous mechanism where data tearing could happen only between pages of generated output data. Since current userspace considers data tearing between pages to be acceptable, we assume is will be able to handle sub-page data tearing as well. Signed-off-by: Suren Baghdasaryan --- Changes since v1 [1]: - Fixed CONFIG_ANON_VMA_NAME=n build by introducing anon_vma_name_{get|put}_if_valid, per SeongJae Park - Fixed misspelling of get_vma_snapshot() [1] https://lore.kernel.org/all/20240122071324.2099712-3-surenb@google.com/ fs/proc/internal.h | 2 + fs/proc/task_mmu.c | 113 +++++++++++++++++++++++++++++++++++--- include/linux/mm_inline.h | 18 ++++++ 3 files changed, 126 insertions(+), 7 deletions(-) diff --git a/fs/proc/internal.h b/fs/proc/internal.h index a71ac5379584..e0247225bb68 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -290,6 +290,8 @@ struct proc_maps_private { struct task_struct *task; struct mm_struct *mm; struct vma_iterator iter; + unsigned long mm_wr_seq; + struct vm_area_struct vma_copy; #ifdef CONFIG_NUMA struct mempolicy *task_mempolicy; #endif diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 3f78ebbb795f..0d5a515156ee 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -126,11 +126,95 @@ static void release_task_mempolicy(struct proc_maps_private *priv) } #endif -static struct vm_area_struct *proc_get_vma(struct proc_maps_private *priv, - loff_t *ppos) +#ifdef CONFIG_PER_VMA_LOCK + +static const struct seq_operations proc_pid_maps_op; + +/* + * Take VMA snapshot and pin vm_file and anon_name as they are used by + * show_map_vma. + */ +static int get_vma_snapshot(struct proc_maps_private *priv, struct vm_area_struct *vma) +{ + struct vm_area_struct *copy = &priv->vma_copy; + int ret = -EAGAIN; + + memcpy(copy, vma, sizeof(*vma)); + if (copy->vm_file && !get_file_rcu(©->vm_file)) + goto out; + + if (!anon_vma_name_get_if_valid(copy)) + goto put_file; + + if (priv->mm_wr_seq == mmap_write_seq_read(priv->mm)) + return 0; + + /* Address space got modified, vma might be stale. Wait and retry. */ + rcu_read_unlock(); + ret = mmap_read_lock_killable(priv->mm); + mmap_write_seq_record(priv->mm, &priv->mm_wr_seq); + mmap_read_unlock(priv->mm); + rcu_read_lock(); + + if (!ret) + ret = -EAGAIN; /* no other errors, ok to retry */ + + anon_vma_name_put_if_valid(copy); +put_file: + if (copy->vm_file) + fput(copy->vm_file); +out: + return ret; +} + +static void put_vma_snapshot(struct proc_maps_private *priv) +{ + struct vm_area_struct *vma = &priv->vma_copy; + + anon_vma_name_put_if_valid(vma); + if (vma->vm_file) + fput(vma->vm_file); +} + +static inline bool needs_mmap_lock(struct seq_file *m) +{ + /* + * smaps and numa_maps perform page table walk, therefore require + * mmap_lock but maps can be read under RCU. + */ + return m->op != &proc_pid_maps_op; +} + +#else /* CONFIG_PER_VMA_LOCK */ + +/* Without per-vma locks VMA access is not RCU-safe */ +static inline bool needs_mmap_lock(struct seq_file *m) { return true; } + +#endif /* CONFIG_PER_VMA_LOCK */ + +static struct vm_area_struct *proc_get_vma(struct seq_file *m, loff_t *ppos) { + struct proc_maps_private *priv = m->private; struct vm_area_struct *vma = vma_next(&priv->iter); +#ifdef CONFIG_PER_VMA_LOCK + if (vma && !needs_mmap_lock(m)) { + int ret; + + put_vma_snapshot(priv); + while ((ret = get_vma_snapshot(priv, vma)) == -EAGAIN) { + /* lookup the vma at the last position again */ + vma_iter_init(&priv->iter, priv->mm, *ppos); + vma = vma_next(&priv->iter); + } + + if (ret) { + put_vma_snapshot(priv); + return NULL; + } + vma = &priv->vma_copy; + } +#endif if (vma) { *ppos = vma->vm_start; } else { @@ -169,12 +253,20 @@ static void *m_start(struct seq_file *m, loff_t *ppos) return ERR_PTR(-EINTR); } + /* Drop mmap_lock if possible */ + if (!needs_mmap_lock(m)) { + mmap_write_seq_record(priv->mm, &priv->mm_wr_seq); + mmap_read_unlock(priv->mm); + rcu_read_lock(); + memset(&priv->vma_copy, 0, sizeof(priv->vma_copy)); + } + vma_iter_init(&priv->iter, mm, last_addr); hold_task_mempolicy(priv); if (last_addr == -2UL) return get_gate_vma(mm); - return proc_get_vma(priv, ppos); + return proc_get_vma(m, ppos); } static void *m_next(struct seq_file *m, void *v, loff_t *ppos) @@ -183,7 +275,7 @@ static void *m_next(struct seq_file *m, void *v, loff_t *ppos) *ppos = -1UL; return NULL; } - return proc_get_vma(m->private, ppos); + return proc_get_vma(m, ppos); } static void m_stop(struct seq_file *m, void *v) @@ -195,7 +287,10 @@ static void m_stop(struct seq_file *m, void *v) return; release_task_mempolicy(priv); - mmap_read_unlock(mm); + if (needs_mmap_lock(m)) + mmap_read_unlock(mm); + else + rcu_read_unlock(); mmput(mm); put_task_struct(priv->task); priv->task = NULL; @@ -283,8 +378,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma) start = vma->vm_start; end = vma->vm_end; show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino); - if (mm) - anon_name = anon_vma_name(vma); + if (mm) { + anon_name = needs_mmap_lock(m) ? anon_vma_name(vma) : + anon_vma_name_get_rcu(vma); + } /* * Print the dentry name for named mappings, and a @@ -338,6 +435,8 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma) seq_puts(m, name); } seq_putc(m, '\n'); + if (anon_name && !needs_mmap_lock(m)) + anon_vma_name_put(anon_name); } static int show_map(struct seq_file *m, void *v) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index bbdb0ca857f1..a4a644fe005e 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -413,6 +413,21 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1, struct anon_vma_name *anon_vma_name_get_rcu(struct vm_area_struct *vma); +/* + * Takes a reference if anon_vma is valid and stable (has references). + * Fails only if anon_vma is valid but we failed to get a reference. + */ +static inline bool anon_vma_name_get_if_valid(struct vm_area_struct *vma) +{ + return !vma->anon_name || anon_vma_name_get_rcu(vma); +} + +static inline void anon_vma_name_put_if_valid(struct vm_area_struct *vma) +{ + if (vma->anon_name) + anon_vma_name_put(vma->anon_name); +} + #else /* CONFIG_ANON_VMA_NAME */ static inline void anon_vma_name_get(struct anon_vma_name *anon_name) {} static inline void anon_vma_name_put(struct anon_vma_name *anon_name) {} @@ -432,6 +447,9 @@ struct anon_vma_name *anon_vma_name_get_rcu(struct vm_area_struct *vma) return NULL; } +static inline bool anon_vma_name_get_if_valid(struct vm_area_struct *vma) { return true; } +static inline void anon_vma_name_put_if_valid(struct vm_area_struct *vma) {} + #endif /* CONFIG_ANON_VMA_NAME */ static inline void init_tlb_flush_pending(struct mm_struct *mm)