From patchwork Thu Dec 26 17:07:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13921389 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 C234BE77188 for ; Thu, 26 Dec 2024 17:07:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85E8D6B009B; Thu, 26 Dec 2024 12:07:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E6896B009C; Thu, 26 Dec 2024 12:07:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 684E06B009D; Thu, 26 Dec 2024 12:07:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 3D7526B009B for ; Thu, 26 Dec 2024 12:07:33 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EED4B1C9103 for ; Thu, 26 Dec 2024 17:07:32 +0000 (UTC) X-FDA: 82937740242.23.F73E43F Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf24.hostedemail.com (Postfix) with ESMTP id 8C743180013 for ; Thu, 26 Dec 2024 17:07:25 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=aXY5LKrQ; spf=pass (imf24.hostedemail.com: domain of 3UY1tZwYKCGQUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3UY1tZwYKCGQUWTGPDIQQING.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=1735232831; 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=Jqq50JvZ531gII06X2js4uIjNkeT7syCyMdKPeFhteI=; b=2D+OuTZkBiH5mQj7Fx1ke5WSu73tGI9w807zlbd/czXtmC2gnG2SRp7TcQ/yWRUk0JMeEm IWAie5XxBZ6MPUhrEB7Jlt/jtcpDIqn7BDbGpqmb3h++1sx7xM7IgHvUFJ2g3sEn6/WjYK tKM46OlMicjYvpBTUfb8DEn8MuLo/mY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735232831; a=rsa-sha256; cv=none; b=pG2W4EwExZsegJbn+jBpefwnAT48CoKXxxP6V85rhXW5kso6jkR4FrZ3zkfWecM2WEUlXe uVkVg11bBFL/+HxSbI3P3vG1iBu+Opq86AvSOskve2wjfykxQezXgTZZmdjkTJi81A8W0S ay757DQnldU1kkfhU1XyNx+SroZJNbc= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=aXY5LKrQ; spf=pass (imf24.hostedemail.com: domain of 3UY1tZwYKCGQUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3UY1tZwYKCGQUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef775ec883so8150957a91.1 for ; Thu, 26 Dec 2024 09:07:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735232850; x=1735837650; 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=Jqq50JvZ531gII06X2js4uIjNkeT7syCyMdKPeFhteI=; b=aXY5LKrQlUiUlVdvYgOR3iHnpn30zhE0q93c6clwoVigY/RUNsqgzCm9L0Po/GswRM EvnTD2p+ek0L/zwGbDQ4eYxy9GCY6Apps3iqRRcHyCgYuqbRSgrO20ErUYS+39vDD0ox piNUd2FMv6YHO9RvNoCtz0gS/OK+e2M442ydYz313OgTzzTiLTLNlk6EQ/WNqQyZuQIC 7en7kEa9FhpUbq8v+lIItCrOHmo5Cq0hBdR/Z4XmyyW/rr4zl/Y9jWIJWvscIGA8hVk4 PCnmZTNfcs9ftb5+cWlhLEmuE/0PGPMHVTey0ln1Tfk6mA9wpzIBK0OVnGwL7q0NjVAh z60Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735232850; x=1735837650; 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=Jqq50JvZ531gII06X2js4uIjNkeT7syCyMdKPeFhteI=; b=o6ZA4FxgE1MTX3CwcPAp/MIaSJYc9GxMnC1DjkQDdnIQ4XGa9yZswow2DzUEdzkypv jWMN9tnEyDRWWNmbF8cRqcb5L4zlW3fx2pM4vMecfrB2RHWD3Y5dNdzO2uceHZSJkIHp 0RKyT49eq5klIUoocT1xCD9FRjYT55zoCRXicEJQG6myqRo/oTOgjtrx3OdhTW79xLaD z7R6CnlWoWnQ0xo7kkna/T6qd9HTpIUMsVvIl0bTMITfTB9ysIf21hAxpWYhIq7zrKrR OscGDwuOGc7yizKzNHKAwQkPllK0tNs90JgZw2/yk7wvTdI1mfH8PXKv7a+6ADeMEMoX jO1Q== X-Forwarded-Encrypted: i=1; AJvYcCUS6Id6i5j2LrpJeFc9rvcL7bEdO5DrCZHGRMV9HVSzJVsbymnfCIn7GSzQgQPX327cYtttWfu78A==@kvack.org X-Gm-Message-State: AOJu0Yyxcmzmo8bz9ID48EhyOna5jJG8/HKLaIlLTUYzHh/foTJEUYRN EMHM7IGdq8//nyuJL1QqmSa2liSpyH5vVZGJYZNVmjCh69PgR3c4X7qvOoo39H3ibnYol8jgTZR 2Kg== X-Google-Smtp-Source: AGHT+IFQ/9ir9mO2gzhzka3p88zGCJjxZamQMl1FNnXg5zmv9rf3CH9hBVzxn6/eqqjJbIU25WLMpAjn/FY= X-Received: from pfbbe9.prod.google.com ([2002:a05:6a00:1f09:b0:725:936f:c305]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:6f0b:b0:725:b4f7:378e with SMTP id d2e1a72fcca58-72abdbe0cb5mr31015210b3a.0.1735232849837; Thu, 26 Dec 2024 09:07:29 -0800 (PST) Date: Thu, 26 Dec 2024 09:07:00 -0800 In-Reply-To: <20241226170710.1159679-1-surenb@google.com> Mime-Version: 1.0 References: <20241226170710.1159679-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241226170710.1159679-9-surenb@google.com> Subject: [PATCH v7 08/17] mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: peterz@infradead.org, willy@infradead.org, liam.howlett@oracle.com, lorenzo.stoakes@oracle.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mjguzik@gmail.com, oliver.sang@intel.com, mgorman@techsingularity.net, david@redhat.com, peterx@redhat.com, oleg@redhat.com, dave@stgolabs.net, paulmck@kernel.org, brauner@kernel.org, dhowells@redhat.com, hdanton@sina.com, hughd@google.com, lokeshgidra@google.com, minchan@google.com, jannh@google.com, shakeel.butt@linux.dev, souravpanda@google.com, pasha.tatashin@soleen.com, klarasmodin@gmail.com, corbet@lwn.net, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google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NepVrh5HeaHApaJqhJY8J/ SpZ8Cwd7 YoLz2z6B3knW81pRrh37qwex8+B1zsTejvpr4cwzl+CQQhaO0TqjSXSN5I4G7qaVNkT58ezO7/i/Xb0lH4gbUa69wiNvMsyvKGjXyOYPbtz57j46vPIYmVKglAZVfHUEEEUq+AJfKp7MUiLYLvwCoxiMmR1yNN6WbLNB0zGlMdq6zUgUJTSAFUl+Rh4+JoiO8xygik3alB4lulbOlPGoW8o5ZpOM3RPGU1+VSrwFHYELLUCPEX8zau4+ttZ3iA/WpAgwlwgU9auh7phi2ujlrRn1+zMZWWoi8sURF8T44GSmmAyiPqa+Cy0HI2phUaF+BJ6t0ze/HiIFBDWS2cgnL/vgAykSc94xnOqSmNwmHUsaVjh0cnWeYj2GG4XGPXJfspioCkuBFsWXhEBS/mDt6YqiBdtUB8YcE+mZ5FAIiVsZGVRW6tIp1wthZDXg6+WLyWUEgpAPzWQSttCYPjMJTaE3BE0xMWfP2TKxpcRGirOJBdXTqnH6OSMm81tlUZmEbzyfNzHePs7RBBSBXW82sSF2oNTQ9aN8bQhtH6WB6Ya8Ab2m3KrHCY0M+Tio926fszMQ8ImAamG3qtJ62NnE4s4Suig== 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 upcoming replacement of vm_lock with vm_refcnt, we need to handle a possibility of vma_start_read_locked/vma_start_read_locked_nested failing due to refcount overflow. Prepare for such possibility by changing these APIs and adjusting their users. Signed-off-by: Suren Baghdasaryan Cc: Lokesh Gidra --- include/linux/mm.h | 6 ++++-- mm/userfaultfd.c | 17 ++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index c50edfedd99d..ab27de9729d8 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -747,10 +747,11 @@ static inline bool vma_start_read(struct vm_area_struct *vma) * not be used in such cases because it might fail due to mm_lock_seq overflow. * This functionality is used to obtain vma read lock and drop the mmap read lock. */ -static inline void vma_start_read_locked_nested(struct vm_area_struct *vma, int subclass) +static inline bool vma_start_read_locked_nested(struct vm_area_struct *vma, int subclass) { mmap_assert_locked(vma->vm_mm); down_read_nested(&vma->vm_lock.lock, subclass); + return true; } /* @@ -759,10 +760,11 @@ static inline void vma_start_read_locked_nested(struct vm_area_struct *vma, int * not be used in such cases because it might fail due to mm_lock_seq overflow. * This functionality is used to obtain vma read lock and drop the mmap read lock. */ -static inline void vma_start_read_locked(struct vm_area_struct *vma) +static inline bool vma_start_read_locked(struct vm_area_struct *vma) { mmap_assert_locked(vma->vm_mm); down_read(&vma->vm_lock.lock); + return true; } static inline void vma_end_read(struct vm_area_struct *vma) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 4527c385935b..38207d8be205 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -85,7 +85,8 @@ static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm, mmap_read_lock(mm); vma = find_vma_and_prepare_anon(mm, address); if (!IS_ERR(vma)) - vma_start_read_locked(vma); + if (!vma_start_read_locked(vma)) + vma = ERR_PTR(-EAGAIN); mmap_read_unlock(mm); return vma; @@ -1483,10 +1484,16 @@ static int uffd_move_lock(struct mm_struct *mm, mmap_read_lock(mm); err = find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap); if (!err) { - vma_start_read_locked(*dst_vmap); - if (*dst_vmap != *src_vmap) - vma_start_read_locked_nested(*src_vmap, - SINGLE_DEPTH_NESTING); + if (vma_start_read_locked(*dst_vmap)) { + if (*dst_vmap != *src_vmap) { + if (!vma_start_read_locked_nested(*src_vmap, + SINGLE_DEPTH_NESTING)) { + vma_end_read(*dst_vmap); + err = -EAGAIN; + } + } + } else + err = -EAGAIN; } mmap_read_unlock(mm); return err;