From patchwork Mon Jan 22 07:13:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13524877 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 1EF4DC47DAF for ; Mon, 22 Jan 2024 07:13:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 82B688D0005; Mon, 22 Jan 2024 02:13:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7DAB58D0001; Mon, 22 Jan 2024 02:13:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 67BC88D0005; Mon, 22 Jan 2024 02:13:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5331C8D0001 for ; Mon, 22 Jan 2024 02:13:31 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 20A27A1910 for ; Mon, 22 Jan 2024 07:13:31 +0000 (UTC) X-FDA: 81706081422.12.2A11914 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf13.hostedemail.com (Postfix) with ESMTP id 95C6520007 for ; Mon, 22 Jan 2024 07:13:29 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HUsdT70D; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3mBWuZQYKCK8hjgTcQVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3mBWuZQYKCK8hjgTcQVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705907609; 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: references:dkim-signature; bh=oXyU4pkvCRYAyA2TUvsUC8dh5sAPzBBH4qagn6tOpF0=; b=tfXN+j3/qnE7tTEs6UPwtVEEOFZ827a2hhNzHkxcmAIriTR1jerBTv5ujqTz6QoQEPIs+4 1uAKtnFf5EHjPZQsa7cTxhnO7kdALnTWZc51amTDg/aG0NDtz1C7YS2KvNFhQlGSRC7U0C 0tRfYPCtBcIMjlFRyaBJvTyNdhMIM4U= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HUsdT70D; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3mBWuZQYKCK8hjgTcQVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3mBWuZQYKCK8hjgTcQVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705907609; a=rsa-sha256; cv=none; b=EFI3cD04kJ9+sstYqqZ9v/nuyGxigx9LvvzJ/klB8eIGnJZIrTu0jegIlyKUKV/+3zoRLJ /IQvstPzPQ/TKygZDvx/kgt5oJ1Ngptk3PJiDOrgP+p3eHKOHISMmPLwegZF2zFzp1Lu5V msmVsUM8dDHlsbHuQQrL45SZIvgWpYc= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5f38d676cecso43397357b3.0 for ; Sun, 21 Jan 2024 23:13:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705907608; x=1706512408; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=oXyU4pkvCRYAyA2TUvsUC8dh5sAPzBBH4qagn6tOpF0=; b=HUsdT70Df6ln/jWGIhnnTHuUkIq3034Zxf+0tmEdXDglW/SLrFMRSnTmHGUUadLlvv THyQZmfEeP2nSOYXHMTclZluEjPa+2tnQxgdXtxz3UUvAHDzWD1N14cHcIM5ARTFUjUr Zf7uu8+eyTBs9PWAkioWji8KTv1JX9ZCboyWLocmbL0a9sxGU+B7x+hafxfhlhKD3lnA kU1YSOycDOCZfypPzjJ6EiIFLV2KThxNLNXalvY59WhQ7jJOVjHgfyOcQDPe3JqQ6gWK e1IDRRxPmhmkkexknLfRJ82lwKfWulnriIxBCSgJ01FTLeW1tshvRMeMAKzMIrhZbEMu kMyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705907608; x=1706512408; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=oXyU4pkvCRYAyA2TUvsUC8dh5sAPzBBH4qagn6tOpF0=; b=vVXxmtF4hhTmFjXyjHjcM7bw02EOJTsQclaTviS8/vLIhg5i0AtlN2DrduvSS2apk3 bvJRFRcqGdxxud/IVJ/RTwM/hBL1ekaKrrimeTonTQgOhZhEfGBWkjuoNJkJnh2uYzWc q0m8BnySFbWpcuplCR40Ijn6zL/AU9yVRF1XHx3PpkDGwh/dtchctvEuOrFcVyragvHp oZepTzUXKZHR92JCmEWaZ4x6dpdbUhZaM57SPcYFjK4Ll/jllBmYTtQCkxGiUu3gbGdK Ki/ZGzFmTjvsMZgBgSS5KdT/I+dN1v06KnJ6axbFNxLtItjxrEJx6Rwm9K+1/oTHgI36 axXQ== X-Gm-Message-State: AOJu0Yxi5pNgvPT4eozf+X6DCKlf0u/JMIgtrs7IWfKoREv+Vd2GmRkE msR4Wrj0g8BV6xQOAWi5DvxE3wI6AiDNhrindNZoaKSKDQEfuVSlE84CRy1zH+GfZUhuwra9SJ+ g/Q== X-Google-Smtp-Source: AGHT+IG8d1Gn9VWPvChN4p2/RHkaCghVVAROZ7VOgDGN0KGJpb0phlIzNaSwlCWzvJeInLF/vdqx3fCBkG0= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:4979:1d79:d572:5708]) (user=surenb job=sendgmr) by 2002:a25:e90c:0:b0:dbe:49ca:eb03 with SMTP id n12-20020a25e90c000000b00dbe49caeb03mr1889847ybd.5.1705907608657; Sun, 21 Jan 2024 23:13:28 -0800 (PST) Date: Sun, 21 Jan 2024 23:13:22 -0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240122071324.2099712-1-surenb@google.com> Subject: [PATCH 1/3] mm: make vm_area_struct anon_name field RCU-safe 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, 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: 95C6520007 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: gg9ii7bbk61q4yosm7zbb75z1yujojdb X-HE-T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7clY2Gp337ttTWeUvZvftsji+1b8z7cmELlm0Uxuh4VmOCRrhgevzKI8AHtczIAmDONXi31knmU29hE2T1vslHcITHVIO3LFyx8EHF5uA2KqwYdkQsPfovGZrYUIPwGPGTDrDiqrgsWRfWP+5OtF1hjoYVhYUANfPZGbHbEE3cy3ozt+/1xIviGUPmqDN3qTkoqgBz1hMr8zBcBmI+/R8Pz5anguv3fDVEbNGUOu8AfOuQDdp51fIfD8KqZ0VL9mALAldGGo4AxLqCSzxPEJ90l8QcMWWuWYFUiZhARt8IFD8gDuM42YnXOOhkHktwuthWRDB4eALH1mApwFjMugmo5Jw+Li+R9su1FhEELINp44wPhkFoREdK+epkzpGV6gg4tIBVb0KPgl/OSt3++wa6nZMQ== 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: For lockless /proc/pid/maps reading we have to ensure all the fields used when generating the output are RCU-safe. The only pointer fields in vm_area_struct which are used to generate that file's output are vm_file and anon_name. vm_file is RCU-safe but anon_name is not. Make anon_name RCU-safe as well. Signed-off-by: Suren Baghdasaryan --- include/linux/mm_inline.h | 10 +++++++++- include/linux/mm_types.h | 3 ++- mm/madvise.c | 30 ++++++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index f4fe593c1400..bbdb0ca857f1 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -389,7 +389,7 @@ static inline void dup_anon_vma_name(struct vm_area_struct *orig_vma, struct anon_vma_name *anon_name = anon_vma_name(orig_vma); if (anon_name) - new_vma->anon_name = anon_vma_name_reuse(anon_name); + rcu_assign_pointer(new_vma->anon_name, anon_vma_name_reuse(anon_name)); } static inline void free_anon_vma_name(struct vm_area_struct *vma) @@ -411,6 +411,8 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1, !strcmp(anon_name1->name, anon_name2->name); } +struct anon_vma_name *anon_vma_name_get_rcu(struct vm_area_struct *vma); + #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) {} @@ -424,6 +426,12 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1, return true; } +static inline +struct anon_vma_name *anon_vma_name_get_rcu(struct vm_area_struct *vma) +{ + return NULL; +} + #endif /* CONFIG_ANON_VMA_NAME */ static inline void init_tlb_flush_pending(struct mm_struct *mm) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 8b611e13153e..bbe1223cd992 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -545,6 +545,7 @@ struct vm_userfaultfd_ctx {}; struct anon_vma_name { struct kref kref; + struct rcu_head rcu; /* The name needs to be at the end because it is dynamically sized. */ char name[]; }; @@ -699,7 +700,7 @@ struct vm_area_struct { * terminated string containing the name given to the vma, or NULL if * unnamed. Serialized by mmap_lock. Use anon_vma_name to access. */ - struct anon_vma_name *anon_name; + struct anon_vma_name __rcu *anon_name; #endif #ifdef CONFIG_SWAP atomic_long_t swap_readahead_info; diff --git a/mm/madvise.c b/mm/madvise.c index 912155a94ed5..0f222d464254 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -88,14 +88,15 @@ void anon_vma_name_free(struct kref *kref) { struct anon_vma_name *anon_name = container_of(kref, struct anon_vma_name, kref); - kfree(anon_name); + kfree_rcu(anon_name, rcu); } struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma) { mmap_assert_locked(vma->vm_mm); - return vma->anon_name; + return rcu_dereference_protected(vma->anon_name, + rwsem_is_locked(&vma->vm_mm->mmap_lock)); } /* mmap_lock should be write-locked */ @@ -105,7 +106,7 @@ static int replace_anon_vma_name(struct vm_area_struct *vma, struct anon_vma_name *orig_name = anon_vma_name(vma); if (!anon_name) { - vma->anon_name = NULL; + rcu_assign_pointer(vma->anon_name, NULL); anon_vma_name_put(orig_name); return 0; } @@ -113,11 +114,32 @@ static int replace_anon_vma_name(struct vm_area_struct *vma, if (anon_vma_name_eq(orig_name, anon_name)) return 0; - vma->anon_name = anon_vma_name_reuse(anon_name); + rcu_assign_pointer(vma->anon_name, anon_vma_name_reuse(anon_name)); anon_vma_name_put(orig_name); return 0; } + +/* + * Returned anon_vma_name is stable due to elevated refcount but not guaranteed + * to be assigned to the original VMA after the call. + */ +struct anon_vma_name *anon_vma_name_get_rcu(struct vm_area_struct *vma) +{ + struct anon_vma_name __rcu *anon_name; + + WARN_ON_ONCE(!rcu_read_lock_held()); + + anon_name = rcu_dereference(vma->anon_name); + if (!anon_name) + return NULL; + + if (unlikely(!kref_get_unless_zero(&anon_name->kref))) + return NULL; + + return anon_name; +} + #else /* CONFIG_ANON_VMA_NAME */ static int replace_anon_vma_name(struct vm_area_struct *vma, struct anon_vma_name *anon_name)