From patchwork Thu Jan 9 02:30:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13931814 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 D238BE77199 for ; Thu, 9 Jan 2025 02:30:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B056E6B009D; Wed, 8 Jan 2025 21:30:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A8ADB6B009E; Wed, 8 Jan 2025 21:30:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9047A6B009F; Wed, 8 Jan 2025 21:30:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 62B746B009D for ; Wed, 8 Jan 2025 21:30:47 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2E9CE1C81D5 for ; Thu, 9 Jan 2025 02:30:47 +0000 (UTC) X-FDA: 82986335334.20.5DD1643 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf19.hostedemail.com (Postfix) with ESMTP id 4F7E01A0020 for ; Thu, 9 Jan 2025 02:30:45 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uwZGWRx4; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 31DR_ZwYKCH0tvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=31DR_ZwYKCH0tvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736389845; a=rsa-sha256; cv=none; b=KNVIb9H22aUbV2+a4ddM9Yv4qy4yoEQm3XHTN1NFLJZoojLf6pXaTVjtKG2OmCh6HbPzAf D+Fu5DlQUzJpGPS3hkYlR8kBN2S+M+3WyAECWZkb4Z5Y0ez123ruGBA9U3preFI6x32kRv Elg0G2L7kMPf8QVD0gScjdQj2GiFQkg= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uwZGWRx4; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 31DR_ZwYKCH0tvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=31DR_ZwYKCH0tvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736389845; 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=jHfydvSHQ3WxL2+aQStFiDlG77OuqV7nJSSFFl9LU/E=; b=VlaFqHZyEQcAXd+r8HQQu8oMWAmejaRBXjjaI85/P/W/8NOM79fmclslIgNbRw9XnDIGfL 3INan6P2R6hMa5KsGURxLYcJBxz9xUeSnByjlZr6c7QvtTfi0FkJ1HAxn6KKu10nmmaJwa CtUyiknqbcwM9f3+EpfK5SQYfeJT3+Q= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef9e4c5343so1187989a91.0 for ; Wed, 08 Jan 2025 18:30:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736389844; x=1736994644; 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=jHfydvSHQ3WxL2+aQStFiDlG77OuqV7nJSSFFl9LU/E=; b=uwZGWRx4QWQIzOPxEtm5VqPjW2IS3wBug1l9iawxfYW91AGRRxGcHDWgNPYX9G1vF5 XO/zks3KNj2qMBRRM7KNWeWde0UcCx5h83isi4cWDqhOTbjFiYlErnSgyPmk31cY7qU/ O85cUXE426CZuGMjJcK64D1sjfYuX7MeFKf6ENs/AGypISQUaIZ9h+Cya/qTpj6SIBUc LN6xaF5ys4DjosSXnxgJmByIFCBqZXnjBR3Gc0ex80x54x9JZrIAzwA0Ed2Eg7tLhFPr OY1JmrTusG6bFKQLt6ybqD9O+c7UkJ8lytUPLMApzaDTH9bRadM0fou/dvsmbsfOJOGT mSzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736389844; x=1736994644; 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=jHfydvSHQ3WxL2+aQStFiDlG77OuqV7nJSSFFl9LU/E=; b=JHz/gcxM4t96q2PMEawszO4IhdBHUQcoopbBB6UZ+Ukshaz0sU4hd0CQ+oANTlvQQg Nfy98B2Thyi9GjDATfVJ0sbgI6IinlyFCKhLQqwrDO/dmm03hgsKp1wiVAsKenKcaGxk wJ/MBamGtMuBgv71kqyjNCoVwB69dANq0Qgiyug3Y+a+WoEWRVzYsny8xg+8cEAf3By2 0Ib9vCHsyyIYFeYGIs972jC6vPCcC/fKAlTMRNCzS3mt3akIFBNzvTdO0Xth3BCsvwpP fl3eq3FJw1QJlfNMzlZiHk9irjijV9RQZCtO/SQjkC4FTkvDYQLo/zh1b/xYW/ieEKku BmuA== X-Forwarded-Encrypted: i=1; AJvYcCXy9Yy+EX4yKQ7XG4JC71dhT+iHQj6LEg9hwM55Bsi0+u8nadn74qa7iUD5R/rlK/qAaO8hb1VzFw==@kvack.org X-Gm-Message-State: AOJu0YxVAMB2JJ7AqT6HoU6gVT98kGMk3PlFScrLKVFPyi41yLPmhd/h cDOt4mTPTRoArY3bZ/ehCZfudR0ShplUGRrY+A+NQM9Pyp+OTcvUDc4k1g4WGxHNyKGCN9MHcRN BJw== X-Google-Smtp-Source: AGHT+IFV1+9gbXGa72+TRMzfljX8nTas7v1/UQOwAWuQbBD3PGKS+ix9UpG8sQoR4NwvEysafdLPdictJdI= X-Received: from pjj15.prod.google.com ([2002:a17:90b:554f:b0:2ea:448a:8cd1]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5686:b0:2ee:df70:1ff3 with SMTP id 98e67ed59e1d1-2f548e4d0a7mr8382370a91.0.1736389844102; Wed, 08 Jan 2025 18:30:44 -0800 (PST) Date: Wed, 8 Jan 2025 18:30:16 -0800 In-Reply-To: <20250109023025.2242447-1-surenb@google.com> Mime-Version: 1.0 References: <20250109023025.2242447-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109023025.2242447-8-surenb@google.com> Subject: [PATCH v8 07/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, richard.weiyang@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: rspam04 X-Rspamd-Queue-Id: 4F7E01A0020 X-Stat-Signature: 3yk8ai3suo8f81xsuwtsb7jsckghuykn X-Rspam-User: X-HE-Tag: 1736389845-85711 X-HE-Meta: U2FsdGVkX18bCYsbD91aLjWYQR84fMG0LSN9ydadJrbI2+9uw5lONMUdOWRFSbJF30FQkPZHqpPbM6H/YriyOH1p2foWhPkKiC0TRLlZDu/Ek10hUwdzt0IUuS+ZGm9TJZ4gvXlJZkK+VEPlNP2zoyq3vQUCnaVSapkdzh41TWdh2etD4ewix3TtxpgAq+Gtl8AgYWEi0ia6iSYYngfD/c6o67vRhX7+Gl7tR37vJ+NmYikKp6AnYniSkCeIywNc6ikFF5UPEuwHfvns0Xeawt87cbWWT1XhYG1c0HXV/rA18VGmuPgVc3+dH9MkyNYivb6vh3pnC7iDyYQ1th1GWziCYizrJkbMRkNHd2xcsCvVvT7CevbO4iym5k0V/kbNqbu8IP9zicCB2G7bqteURP3jQoIvgU/zcIPAk7yO72kqrGaDaawh805L7kXf7BhadphDCm4YwoDNogkO2MR0raLc+S+7XgGX6vx41WL+FpHHTXAf2x0rNT5IsPGVGZkq/slYWRTk34pSNI7mYszlN6O7qsf+vbPs/62v6Z/+mPzis8bGCi+okijdqz+QGnw0FP6lawk4j7b84NqND9JZ6W9K/PqGrg88dpHdmGxBhSApixmBTEetFhOUS31bR7PaxgF4R6gLsfcKuH9rg299L/ASylOKH3Kk+qYUl69Fo6KsYH6nCBQ/KJPPU2lGZpJjV2OXH5oduUXv4p5SvX2yZQTHSEfi2Ia+deXFBzNdIUOLiwk5DAPA1Z+uYW40rOVtjCAn99UdGezDX5AWR8PaFB/c8L3x2ixIpPpwBO3GnAlIoS2CktNVwLhRI1M8Y00/fCICe2WkVQ8lnbDvjVG+x6AKkb+SaZhvQhZyye/uBDPo68RnmZe5rkEzod00dhjWo86yLhZf4i4zUYY/T+EhvTmnkwR5D9n60uZlemioSeOrFBmPh9IPvoze0p9fKDP0dnGqFN2ovk5Y0nvZ5/r Tlcyw4X8 FJvJ8lLZHq+DQ5xLN/XEHk2hN21RpzVz0eT26Bt1SmBS1cpHhX8RqZMfAHI4oOk4JTw/U3korVhKAfOtug+PEKohyzgPp2riMoSwRdyrdFEMn5z8V0LnekgNG/vgzgFROESulFN8xZAh7smAZ854+VU7oi/MEESR9L60u98uOMGeiLCCFxlxdQ3D676HqbaxnD63AHyDuLXJ86/aLsZF3iyXXAjdvfVmgjtEpI6MR3QmRICJfpQ/64AFLeB1/PlYIsBGRxK1MszKnvaZLaY19g8mnTqLmGBhfBkLKCMEvxdV7ZhVWnB5J2n8SC6quQobJ33QKESP3izGv9LW8TEfKPvcBCJu6SdjvpNYgvwIdPfRV5bJiKQ32VzBFuN2WG3LN+9CXj9LdjC0ASUbqXeXrSDqjjJQcrDmu9PQe/BG3YYIUB3khHuJUtjTL6+9PZ+aqvt0GNmEsVX/XQxEeGbcojh0WvUlTT58pdb700+iD3E7t0jlij/JUBGCroyQ/r25aHQP3jXI7swKvwhaWONHQPATpzhP25hiopJ7fOWPWM8zqZYAATRq1aTjmq4dP38pPrEZiCyfTnGekFjhNglvRdf+c8bnIgRwGxcqFjWQbUsWhoJcOolnyXit6KFYssVpiWZ41/XKa5TZqLI719BXHPwgUUIH9mB9efR26 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 Acked-by: Vlastimil Babka Cc: Lokesh Gidra --- include/linux/mm.h | 6 ++++-- mm/userfaultfd.c | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index e0d403c1ff63..6e6edfd4f3d9 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 a03c6f1ceb9e..eb2ca37b32ee 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; @@ -1482,10 +1483,17 @@ 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;