From patchwork Mon Dec 16 19:24:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13910240 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 676FCE7717F for ; Mon, 16 Dec 2024 19:24:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7CC5D6B00B4; Mon, 16 Dec 2024 14:24:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 72D886B00B8; Mon, 16 Dec 2024 14:24:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5CF096B00B9; Mon, 16 Dec 2024 14:24:39 -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 34B486B00B4 for ; Mon, 16 Dec 2024 14:24:39 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E0B66A0A2F for ; Mon, 16 Dec 2024 19:24:38 +0000 (UTC) X-FDA: 82901798196.26.C200486 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf01.hostedemail.com (Postfix) with ESMTP id 9275740011 for ; Mon, 16 Dec 2024 19:24:14 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=zLW224GS; spf=pass (imf01.hostedemail.com: domain of 3c35gZwYKCDQikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3c35gZwYKCDQikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@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=1734377046; 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=1gkuZWwQBkEGAXVOS6gJMPBIIwpS2AmmvzoNNuLoLbs=; b=CBYhxc/AGfMkqTtufGlwj2uECYnybgQL30pdrlgHeCP+hm0rDOS49yP8ScXDHq3RX7f63C ad4jY3DECSaB/fHzR3ZAHtXi6pqQm5f3rCYn2bMy3pfGn8QD50PS60ds6TIY1tolb7PMCJ aSBHQ8/dnW3xHDq85twQElxX7pZqie0= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=zLW224GS; spf=pass (imf01.hostedemail.com: domain of 3c35gZwYKCDQikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3c35gZwYKCDQikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734377046; a=rsa-sha256; cv=none; b=rHmXdix80PL5SZT1yYlO2ovFZJ38ZfO3nY9eMK6u/DkDUjqX3wjrBybrzrWO2W/cnofR1h Jfmz6vnwLCUJPXbBr0jR75ig4OlN5Kp1g2bs/ZdFF/f5ExST5CHO2gRt3SY4buiibsDboR 0upSyVSjrsUgr3SKlggLjvhzeTUAnW4= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2163dc0f5dbso37053375ad.2 for ; Mon, 16 Dec 2024 11:24:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734377076; x=1734981876; 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=1gkuZWwQBkEGAXVOS6gJMPBIIwpS2AmmvzoNNuLoLbs=; b=zLW224GS/hz7LVhQYCzdt8WaKQH8KZGInp9G9CWiZqaz/ha7tbgBvBtZV+80cRJmyJ HahHU8U3yK569DypN74+/cOlo/5/1hgnnDPavjIJkqV1fhdDHEPANDhInLiWKKSEbeS4 GB7pcGGoXsKo3jS1/sV+EaxtKmCxnT2lcC9Teh0wbe6C0Z/NrzdAYJZiW2+THJVRVRVh VkwsBQKRE3Ii6JA3fpjUD499rI3o2zGx2wK/04wjjJJheK1i9xvY+qNxMyIGpWls83OD U9Es/Q1rE2n30pOMx20nagY9Xt6lG1RI7OqWt+b6jfL7VT7hPIxNmR6nLFqFVzjYi93A HYJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734377076; x=1734981876; 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=1gkuZWwQBkEGAXVOS6gJMPBIIwpS2AmmvzoNNuLoLbs=; b=GewsbBNwcRYOTKhdAMlEexKQFrd0+dE3RNslekhAKNSXVbT9/FE5fD5woP9QcesU6l phU+NSb9O9pokRR4Eg17K5rvJtdbdNaVvy4V2OIeQQnFkfFax2CzFAfMrUE/HHQtJlAR UJBbmH8cXoXsuQNzTHXJ0TlBod1UrhYTdODJX4rIQGHT9HQ2hqLvYwpR6OODpWjrQ/H2 NLYcy6V7Z/eWd7L1wR9A7djE8RCxpLdn8NVu+EQwFN/y4nUULi+dZWLPkGt072FWpHS4 LWRTnsiar7YuG/FSNEyV/dg+tPlwawn6epxwuM+YODGMRuFIOLqQMCmO35V7p9e+lKVV IplQ== X-Forwarded-Encrypted: i=1; AJvYcCVNEkBh+m/dvyTpsiaweqsuVENP5JeUADuvLyKapKjZHuZXQjvMrgNZWHF0JRHqIQL7fA5hUoMQKQ==@kvack.org X-Gm-Message-State: AOJu0YyeW08r9ZmdAo5vEQpvvOMVa+OaXZhE8sTqRECaaahVp8LRgMFs NsHWL/S0CUUbIDQCel2WwidMXod0cqV7/JjSzzHplBv+hwx7ENlQ35SRg199uKcRXtwdEBVWbd7 vPw== X-Google-Smtp-Source: AGHT+IFRpgE3svFkQGxxP8t9jtfuJXCqP6DbanV+kjCkw5VL6vSIPipGx+mrpM9/g8/yEqYCZ44EecENvBs= X-Received: from pjbsp4.prod.google.com ([2002:a17:90b:52c4:b0:2ee:4679:4a6b]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2744:b0:2ef:33a4:ae6e with SMTP id 98e67ed59e1d1-2f2d7d9fd46mr1314965a91.12.1734377075865; Mon, 16 Dec 2024 11:24:35 -0800 (PST) Date: Mon, 16 Dec 2024 11:24:09 -0800 In-Reply-To: <20241216192419.2970941-1-surenb@google.com> Mime-Version: 1.0 References: <20241216192419.2970941-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241216192419.2970941-7-surenb@google.com> Subject: [PATCH v6 06/16] 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 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9275740011 X-Rspam-User: X-Stat-Signature: 3xaxo5kcoinfk9pktzryfwxajoqhdtcw X-HE-Tag: 1734377054-174457 X-HE-Meta: U2FsdGVkX19WSsC8ohjXOXC5U5lklMQkqUI8gVNY0pmBIk8L2LM5ZX5NCBvBFjxprJHFVrFNxzSIgoAXeAmETvWLRZJNxCPl8rZrtL1RvndKKS8i+65LYh9xY59cZo6mIGjdM8S3x1kAHvrMs3+hIo4es5DZUZH9VKM5vrz81wTTWvZoxYNwa5Sz/SOqR+mx27ro8QPPyivfuNSZypjeu33EwgZGdXLUv1IFwwfirpo1qHOjoTaqTb+jNPQ5AofPdIkz7WKfctAdpiEG9se4zeqh1kIj/KL/6iP+Ez1OAO91R9J8pJPKPYwZTJVBJ8lMGxYBkGymF0KegKWT8ja5k9OxSNfUw/S9V5kPkDyw10NXS8jAlH1RQ85Sdx7xrM+1Huf/HX3Z6UnzxGuw/EJFePp/phwBMcpROl3XUs4id9XpKjCxjw3bLLThlBpbbztf6QVFTVAEuNBfLnevZ9LcJ5FW61uYKbqLyuKyndYqcnq96Qq8/mBZZ0Wj4yHMY8ZUnL51KtL7sZRO+QiiLngrTLNREaYcmxFeZC/P85XU2iCXNrL9i8lscW5qs8CooZrPqaHnkZeJk1QawkHbLRFnkc+v1UMNLNBXZm8h1+ApY5HFnWETk6rYjoZbwNl14jVOqGYlZqIIKFmJRerAkvBwWMdIz5BpJ9O8WhHMTjRpDAvLBJyO6CKdAMn2ME9TnbwPmmwHHNoKeJGQUP7mczxfDp0CDEOquQyWi2HYRoOEdwNI0BFBerKPN/pYjsERgF8K26PnmhFrvnuleJnaO+pmL05wRenEN0UZdIvYlChn3d7lMpVREEjRCLL4n1KI33Uz6u8YSZhQq1VMnSLdmMPSscUTVpjClU+eQUMptkwptbm1riYqF/DhNFyUs6v/o2J594g/+RVT/335+gsao/WotAkVKbCGNbmrizE11ILB+luzYXqqxKgfL5VODxeUB0M4tLYAPR160CYDN8QgO9h I6hnp+Zc 02orRpSHRS+czrtrELnuFI6oRrTNjtxW/E8LwbgwvIye8gNI7/y0ECVZZFy7fbm5Gs2cmaErj5m8ccN5tlSOkfgVTwHzE1B2fMwHERDpohOoiy+Lh7nV7tGnLjIgBc3t0bZfDW/m1QHsisY9/l8NrL5mMYJM/SayXDnsLYwfXjiy02l976lepH7DOuv4mgQCplAcMplNmLboPoe13l9e5FufVxIwBNxIZnN1fFpfBilejmZGWf9VA9m+xI7e7fGsul67P62Ayac5055FOfPJ5s3W9kNzTdfIV3XJeoMuOSsEjll8P6RVBkTmqWCJkW7PFmfpPaJcaw2S9wChhF5+s3G0pROM6m5yWThfpTp21FXj0WCfJ8vB+aXwJvxnAJQIC+i5URBACNr8RSw71M0qXDnT+bNARiePKGc7PZVQkk9iACVuE7I1UetYT2GqYazP8srH+UjC2f1elesgF0AyNOfEvasiNezLzTzoyuHtMkTgFfeVR9lp1e/Eg4DLGB6/XNAAckIHOBPFSqnKSri9SNtVjU14SzZF9NnEGAxe+dIbuN8qgqbVN/NiBp9UFmwHe8PKXQqC7pCLU8GuMw7pDAFbhPw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.158876, 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 689f5a1e2181..0ecd321c50b7 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 bc9a66ec6a6e..79e8ae676f75 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;