From patchwork Wed Feb 23 15:36:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 12757091 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 EF48DC433EF for ; Wed, 23 Feb 2022 15:36:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84E258D001D; Wed, 23 Feb 2022 10:36:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7FD618D0011; Wed, 23 Feb 2022 10:36:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C6A38D001D; Wed, 23 Feb 2022 10:36:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0131.hostedemail.com [216.40.44.131]) by kanga.kvack.org (Postfix) with ESMTP id 486D78D0011 for ; Wed, 23 Feb 2022 10:36:45 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id E4F5D96F1C for ; Wed, 23 Feb 2022 15:36:44 +0000 (UTC) X-FDA: 79174447128.14.2ED6309 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf23.hostedemail.com (Postfix) with ESMTP id 63A93140004 for ; Wed, 23 Feb 2022 15:36:21 +0000 (UTC) Received: by mail-yb1-f202.google.com with SMTP id o5-20020a25d705000000b0062499d760easo9323859ybg.7 for ; Wed, 23 Feb 2022 07:36:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=VTm7fP795OcGxTyvbahW12BdyN0osMpgZF+oc5TFR48=; b=IRWJmzYqcQ3FOFrXy/WU1zsLFBzMCkcEVBFDmvJo0x44BOSca5zhysVzPxz+4FPm1/ OiSFXHCdeqG/LaaoyQ0VTPkUwgIhTrm77sOWyc3sWxmZcwNbUk2nwCuNdbxiCy428MHz MG609HxJ9QLQ3zBWtV9vLCvyi2jU5vcOCTLb5FHepKP/L3xcYhgTM2XLFabE9iT6Utuq iaaa0dXYRB9deRLvG64p5RHZg7AQtf1+JvSwPIfQ1Sz7tSf865E6QU2vZoDEgkEa4bYD 1O0GrjoEmBa1lc7H1NU7LF/HODYw4KmV9KnSGp90MJUpcWrmazrmoV/4sWKVoAa0Edlb lckg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=VTm7fP795OcGxTyvbahW12BdyN0osMpgZF+oc5TFR48=; b=wjc1EXViCiYpZHwD6izS044ox4wsP0nCIf9j4k7/8JKUEeMSl4GnLBqRTGYvy3sOV5 NiK+oS4YAJlRJTM4fExvvi3jMyZ3yisMHy47ZL/jxcT5z3tHS3XWVpt8AVJu4Zzr0ing 3sIHqms1IZnXz3H77eH+h3hKI3KwfYD/L7XsusYA0XUZd97DrGoHydtjG5nyGlsfCP8E F0cHzcwFGOY4p4rsDRza8f4e60CsN8F1LcNbhtc6EoetrDiSzg+PYw3A2Fy4IAOPyI+I iollNyL4vZgmzjhMC62Cla0oNQjtKSSqyrOKq5Srqam3+/P1XlGim3/RHzhJ0cMN+Bb/ wQSw== X-Gm-Message-State: AOAM5313Ftqr1FMy/tYRemo5kSO+319hklMj0v5Ain9SY1CzLB6a3Bfd uA3LfOIfJbdes8fB22937DodghnTUus= X-Google-Smtp-Source: ABdhPJw+ehLGYr/coulof1rBJJzPvQAJiBIsiXatKMdNfWrhBrL6oVKrpydjbcVTigvdAofGAgHfZlN35m0= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:5093:9fb5:d0ba:a5f]) (user=surenb job=sendgmr) by 2002:a25:180b:0:b0:61a:a60:e7b0 with SMTP id 11-20020a25180b000000b0061a0a60e7b0mr274980yby.454.1645630580430; Wed, 23 Feb 2022 07:36:20 -0800 (PST) Date: Wed, 23 Feb 2022 07:36:12 -0800 In-Reply-To: <20220223153613.835563-1-surenb@google.com> Message-Id: <20220223153613.835563-2-surenb@google.com> Mime-Version: 1.0 References: <20220223153613.835563-1-surenb@google.com> X-Mailer: git-send-email 2.35.1.473.g83b2b277ed-goog Subject: [PATCH v2 2/3] mm: prevent vm_area_struct::anon_name refcount saturation From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: ccross@google.com, sumit.semwal@linaro.org, mhocko@suse.com, dave.hansen@intel.com, keescook@chromium.org, willy@infradead.org, kirill.shutemov@linux.intel.com, vbabka@suse.cz, hannes@cmpxchg.org, ebiederm@xmission.com, brauner@kernel.org, legion@kernel.org, ran.xiaokai@zte.com.cn, sashal@kernel.org, chris.hyser@oracle.com, dave@stgolabs.net, pcc@google.com, caoxiaofeng@yulong.com, david@redhat.com, gorcunov@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Rspamd-Queue-Id: 63A93140004 X-Stat-Signature: 1z54usqkzcgez7mjbmedpr5bz7azh9fa Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=IRWJmzYq; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3dFQWYgYKCI09B8v4sx55x2v.t532z4BE-331Crt1.58x@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3dFQWYgYKCI09B8v4sx55x2v.t532z4BE-331Crt1.58x@flex--surenb.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1645630581-690202 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: A deep process chain with many vmas could grow really high. With default sysctl_max_map_count (64k) and default pid_max (32k) the max number of vmas in the system is 2147450880 and the refcounter has headroom of 1073774592 before it reaches REFCOUNT_SATURATED (3221225472). Therefore it's unlikely that an anonymous name refcounter will overflow with these defaults. Currently the max for pid_max is PID_MAX_LIMIT (4194304) and for sysctl_max_map_count it's INT_MAX (2147483647). In this configuration anon_vma_name refcount overflow becomes theoretically possible (that still require heavy sharing of that anon_vma_name between processes). kref refcounting interface used in anon_vma_name structure will detect a counter overflow when it reaches REFCOUNT_SATURATED value but will only generate a warning about broken refcounter. To ensure anon_vma_name refcount does not overflow, stop anon_vma_name sharing when the refcount reaches REFCOUNT_MAX (2147483647), which still leaves INT_MAX/2 (1073741823) values before the counter reaches REFCOUNT_SATURATED. This should provide enough headroom for raising the refcounts temporarily. Suggested-by: Michal Hocko Signed-off-by: Suren Baghdasaryan Acked-by: Michal Hocko --- changes in v2: - Updated description to include calculation details, per Michal Hocko include/linux/mm_inline.h | 18 ++++++++++++++---- mm/madvise.c | 3 +-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 4bad32507570..f82085ff8a6b 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -161,15 +161,25 @@ static inline void anon_vma_name_put(struct anon_vma_name *anon_name) kref_put(&anon_name->kref, anon_vma_name_free); } +static inline +struct anon_vma_name *anon_vma_name_reuse(struct anon_vma_name *anon_name) +{ + /* Prevent anon_name refcount saturation early on */ + if (kref_read(&anon_name->kref) < REFCOUNT_MAX) { + anon_vma_name_get(anon_name); + return anon_name; + + } + return anon_vma_name_alloc(anon_name->name); +} + static inline void dup_anon_vma_name(struct vm_area_struct *orig_vma, struct vm_area_struct *new_vma) { struct anon_vma_name *anon_name = anon_vma_name(orig_vma); - if (anon_name) { - anon_vma_name_get(anon_name); - new_vma->anon_name = anon_name; - } + if (anon_name) + new_vma->anon_name = anon_vma_name_reuse(anon_name); } static inline void free_anon_vma_name(struct vm_area_struct *vma) diff --git a/mm/madvise.c b/mm/madvise.c index 081b1cded21e..1f2693dccf7b 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -113,8 +113,7 @@ static int replace_anon_vma_name(struct vm_area_struct *vma, if (anon_vma_name_eq(orig_name, anon_name)) return 0; - anon_vma_name_get(anon_name); - vma->anon_name = anon_name; + vma->anon_name = anon_vma_name_reuse(anon_name); anon_vma_name_put(orig_name); return 0;