From nobody Tue Feb 10 02:58:45 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A30114373F for ; Thu, 26 Dec 2024 17:07:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735232851; cv=none; b=X1RrnIFBHBQN/HAYNeZ+sqUcQ52zmgQdAiDWR6wywn53WHB9PstznTg1nFT6DTpBE/g7GBVL7jNKa8bA4pOw3zcMF0cvUopkYyVwN976mncVpwOhspKGPUwxNB1wziPQoDq+OSi5ZeQ2eT+ZS1yZxbZaydhxm0PCwDm8BdLLXo4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735232851; c=relaxed/simple; bh=7e8BADgSS/eI3b4iWIZ0Vev97Rzzh61LrDXN/gVavY4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=I4AdGWBerqlhZCe7bqK1fzofmphhVfyGla4Ajc4k8sFqvIuxUc2f7yY0/ezSOotsxwlZvZnjL2Q4tbhbxiWfxJPCxrYFuPmi0OS9ZdnJi3zNFph2pzHz9xgeRN3BPRZupvrgF8wvBN3lS+T0VU2WW4m0iNXWF0EtUX5gSgMuO2Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QuaRa0qT; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QuaRa0qT" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21631cbf87dso73147115ad.3 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=vger.kernel.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=QuaRa0qT4etm+sFBA6ezx4cwSUGZpTQusYuRn66OSuWRWWvQQsUdZQN8zy3ArQ3UHw fc3YYrHntsQY5tMdKT9CTqTeUetaLetgrInjQgUad1aCW/tQS3EJrow2rS6Ef0cdUsYX dx86Yo8S9yHmzRwWuvbK3uOVKgsqNbPNn+Msh61aLSeFXRWgY/tP8ZFR9AzAmm7Zze14 J9/Ue0FGpf88YbwhprpBNo5EN/VR7XsLdDq4yvekA4PeGvBemQoMrDIxORMWTfHJuxzH 6D9e40mzJ8tgDplZx0zjqy6utvgS1QdeBOst/XorTBsiJ8HRnIdfFy3ocJaNMlkLMARA vLNw== 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=GK0JC7V7xx0hxct8D/MQQP0vimBhr+hTp+wtQy6iiuF1kKNF91WCg0hj38QZp2EL4+ u+Qbk+wox0BSvzn0lWm5G+FCzXUhWwfSsoGT4/0kdCq9CF4soOU6PmrjYLSAMsPrqAj2 zm2ysWz39s4oS5QRLeboyHXz/Sfi2hiysxVwDWnHx7h5C8GguOyzDOMoZICRvTKCcH+8 oMCU0DGxVgNK6FkSWdHS3Gj+p6W44V27dYUDaPV9PP8QH+nUg0TWXIzfpxMBbZt1nQfj yAnqqhWbEfbNcd+bahizvfbqFp0QdG22kRUhZFQWtAb5ni9NXBSEXCR57UjP0EEq7QUr HOtQ== X-Forwarded-Encrypted: i=1; AJvYcCUYVRfGu35OLrniZNX47WqyzO2EvykO+qG81iosWH4WlsMcmdDWQB419aGF/6QlWQDVYDJM5z8XwS+WIx0=@vger.kernel.org X-Gm-Message-State: AOJu0YwNHoDe8y90NiqwZwZYFYulT6XaQvxPv+v+Ahhl/jYv0Zq5vRqt EUVg3qymWCt6JZGL4rsXvlx7DJeb07MGXIvgySCnIljq8o/p6dW7ZBzfL4clBqsVQ2G/egtbWtm 6mQ== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Acked-by: Vlastimil Babka --- 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_stru= ct *vma) * not be used in such cases because it might fail due to mm_lock_seq over= flow. * This functionality is used to obtain vma read lock and drop the mmap re= ad 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; } =20 /* @@ -759,10 +760,11 @@ static inline void vma_start_read_locked_nested(struc= t vm_area_struct *vma, int * not be used in such cases because it might fail due to mm_lock_seq over= flow. * This functionality is used to obtain vma read lock and drop the mmap re= ad 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; } =20 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_str= uct *mm, mmap_read_lock(mm); vma =3D find_vma_and_prepare_anon(mm, address); if (!IS_ERR(vma)) - vma_start_read_locked(vma); + if (!vma_start_read_locked(vma)) + vma =3D ERR_PTR(-EAGAIN); =20 mmap_read_unlock(mm); return vma; @@ -1483,10 +1484,16 @@ static int uffd_move_lock(struct mm_struct *mm, mmap_read_lock(mm); err =3D find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap); if (!err) { - vma_start_read_locked(*dst_vmap); - if (*dst_vmap !=3D *src_vmap) - vma_start_read_locked_nested(*src_vmap, - SINGLE_DEPTH_NESTING); + if (vma_start_read_locked(*dst_vmap)) { + if (*dst_vmap !=3D *src_vmap) { + if (!vma_start_read_locked_nested(*src_vmap, + SINGLE_DEPTH_NESTING)) { + vma_end_read(*dst_vmap); + err =3D -EAGAIN; + } + } + } else + err =3D -EAGAIN; } mmap_read_unlock(mm); return err; --=20 2.47.1.613.gc27f4b7a9f-goog