From patchwork Sat Jan 11 04:25:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13935773 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 6C09BE7719A for ; Sat, 11 Jan 2025 04:26:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84F4D6B009A; Fri, 10 Jan 2025 23:26:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 763796B009B; Fri, 10 Jan 2025 23:26:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 590526B009C; Fri, 10 Jan 2025 23:26:26 -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 2752F6B009A for ; Fri, 10 Jan 2025 23:26:26 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DABD514108F for ; Sat, 11 Jan 2025 04:26:25 +0000 (UTC) X-FDA: 82993884330.27.2070D2C Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf13.hostedemail.com (Postfix) with ESMTP id 2415720003 for ; Sat, 11 Jan 2025 04:26:23 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="QHZa3/aG"; spf=pass (imf13.hostedemail.com: domain of 37vKBZwYKCB8NPM9I6BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--surenb.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=37vKBZwYKCB8NPM9I6BJJBG9.7JHGDIPS-HHFQ57F.JMB@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=1736569584; 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=7fOHsmOPwrKtQ1EzbWANyGDHIimQCM8l7vZM7q2M3lw=; b=2xeG8J/qEbMcR1yq4j4yiVevah4/MsHcM/QlwMx3h2W2e27fMZ5BT6BaIsnI8TFgOMuLwh 1NYzXVA5TPLVTM1Z40iXX6QCxIBIpFhhp+M7gFVCjWVr0/PuQbnyAIs2ZrblLfk2aWQ+bV TbCF73B2EEi1PjbIef8RBbPWFrzMYRQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="QHZa3/aG"; spf=pass (imf13.hostedemail.com: domain of 37vKBZwYKCB8NPM9I6BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--surenb.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=37vKBZwYKCB8NPM9I6BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736569584; a=rsa-sha256; cv=none; b=LT9DGts+1mdImJX9NaqZdJTyKOvugNTNs8pTWTzhmrBJ2WW9MAnMrVktv7lN8kx0hOkp2Q DjrxEK9rnLQJXd/qrTZohRjqMeiln+xRCRfXFhr2WXDl5awd0g1Ua1SeA2Tq0fN3yfZHEQ 3x6kmx2gC4gHpo5YGTfPC3IetCUggUE= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ee5668e09bso4895766a91.3 for ; Fri, 10 Jan 2025 20:26:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736569583; x=1737174383; 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=7fOHsmOPwrKtQ1EzbWANyGDHIimQCM8l7vZM7q2M3lw=; b=QHZa3/aG0n5VWPlzWLVi0ul+PJP0/y7tl7bi+p+gHCgqLpVstKxUU1dlMcsuH25V7y ly0vj8bjLnylKWbSqoGWRDnBGnv840cxP2ABUqvf6tq18of+1LkquGx4pXe2yLesjGsp kQDtJkdPfDB0mCJhmR9F3eAySQdTuZAxh3nTAAHE2x3BpmdbXPe/15X+tRFxhyH3SUDv q2YthShq1PR0Wjg8a+iddEV5y/Dy6FMkMsUJ8HHxTNQcXL7Ho57PdeN9GbQxqby7CLH8 3Shw8t9L1r9c98rJfSVWRi9RNgPhu0LBujtOsrGOFXcON/Fwqxok2tEPIbwPW/Hq2I7j +xUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736569583; x=1737174383; 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=7fOHsmOPwrKtQ1EzbWANyGDHIimQCM8l7vZM7q2M3lw=; b=q96lMXos6B1vRaN8tb/hzPnFo0GRpuXceY8S60ag443jsM8ekqFRSY1SToK78xkay9 kGKtAlB9TkQc9hto4NtBxdx+CsOUwQeSZR0BcNsMlcfZqcvW9cksY64AFfRhmGgGpmIt vhRVx7XdNcnG2u94VMNRlpJ1eYb+6/21FXA33dqMPqnN3k6A8MaXCdrq5j5sfABDXnxS EXgvr1VzvbBbhAdfO6R9SgvFIYKDkFK07Tgksyj0zUuGZKWaixs274A/lqK67Lz2xvjl eYbhQ7vzhN9Gg1UYQY1rzpqF/vfgxMeU+foBeigvA2drBsAPMrO99C4YsbGCZXMqZBxF 4+Iw== X-Forwarded-Encrypted: i=1; AJvYcCW2SDQhSDSA4SvANI+raWtd/xka4yNYwo8w85kxBZSF2dTAx3HzfcGwjrqJ2QmjGrSRjYxp9zjDyg==@kvack.org X-Gm-Message-State: AOJu0YzdLKAfx25sy2yXBm5uKdH4AjRyL9K++Pbu52miRbxFan/vLgsX neAd00l0MlVJlvCaWHW3+nZlAqx+hHhxKdIeOAHX1N9jUe8NF7GID0YgXR+TEvkvAPIY4QjYDAz 4QA== X-Google-Smtp-Source: AGHT+IEri6oWvbwaR9NKB8OaH7pKRVw+NxMfmWZU4bRXuSK8tL89q2iubRxHeJuXJzsmqdnV3Y+AwYimngo= X-Received: from pjbsl14.prod.google.com ([2002:a17:90b:2e0e:b0:2ef:8f54:4254]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:da88:b0:2ee:5111:a54b with SMTP id 98e67ed59e1d1-2f548f424c7mr16967174a91.31.1736569582930; Fri, 10 Jan 2025 20:26:22 -0800 (PST) Date: Fri, 10 Jan 2025 20:25:54 -0800 In-Reply-To: <20250111042604.3230628-1-surenb@google.com> Mime-Version: 1.0 References: <20250111042604.3230628-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111042604.3230628-8-surenb@google.com> Subject: [PATCH v9 07/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, david.laight.linux@gmail.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: rspam06 X-Rspamd-Queue-Id: 2415720003 X-Rspam-User: X-Stat-Signature: xt6bynqs7gat8ezdmhwfeo7gtwje8uni X-HE-Tag: 1736569583-43481 X-HE-Meta: U2FsdGVkX18rP5TS2F0wD+0Q/kQ4iyEJAdv8vdZETTCgWz3d2ttuuBp4tQzEGSsPj4CtLZfeF+Bn/M/5RZcuT2VbSMgaaOfgxghmGGnRSVN0uRurALySlaWQyWUCcKw0LpwZwQfcuDJcIqCdcozBjNHXzhW/SqKYLDH68U4Suafs6MiJXaUHZZ5oeErX7AVsgCsthNDs5S0jyQ1S8xP+WjKZSeBTwgOPxvKjCYgUNWbh7QeYeg2qOgHBLBrbMeBnZ9vzaMXo9a+peoEzJYfGjqvTW9N9Ponwo/9/HmH2N5J/B2dUNb2sR6Gd75Sftuk3pEJK36lNH2oWl/cDSvxejOBu6pqnslZCWwZpqB0lDIyrSyQL/dcfX4ge+lNbBQHOtZq1/pnXG6sjVHd3gHIXOXUxcVJLhn29tvilZOz8Eb6732YBmd4aqayqI/WGgrA0LOVgOOxB8+DNbqSKhwNCPFry2WPEJ182I5/kwNh4wKqBuXclK8W3unwLwj+qhfty1TifPMSmTnwK8FaDs7i6467vywRY4BxnbZX4SFNxtp5K+MvXBxd8IyfTd0B6trBFfBBkbFdk/g2oyw+swAv5oLFmX4sOYoGTD62ymMKD3cpBKtZB4XQFl6OPJR+5h+eCP9A7OS0PI6imL/7WbIU2hrBq7Jgx7PJzGJprHa3dcUi/iokdgcZ5sUrJzTnKEedQAJfWEgFjv+FLxM4Z3XnQTvmbfrAcKSzpaIyxwd/K0kYQCv9oqVbU28zZnCl4t9CpV9e1/ulH6GFeFhPJ3Ramn5ZYcMftLsitfCf9mhjtwJd+msBSKg5k00J8dlNYvx9M9l+byNtEgXyQuQuXSq8oer9HLRLMfDqr5yWinYzEQvxydnRg9omWZc81dtTtRNjyAUdGkIUi7RNB9/BkB4pzciaqmm3jpPMvPH8+wlM0T6dWBqq6PZiG3MSRXLZMp0X+zp+KBJ9STp1PC9Ztkeq SCMC+Z08 Vc+pNQLs5Dn2cdZTe5YFsnlK9mwZbOjU7eO3l1oectM4ozTnDzmqbtrCXFJ2f5xJsQh/OxV6V1DFPEo8uPh9jdatcO8b+v78wed6MKJmm4fVjEm0fv/ufy9n/JZXe2jm68vz4ofU7ujzYbqemwiXuEPGcxT1rrf+V6g8U04SlaxYQ3C3ay8wWW/cwq6Hbq6jYaM8qFn9/Tms3RNZCmexMDLBbpCdWep7WMl5gDajm7+TJIe2sDV/VOFWEOIcmlt1cMuXmdZIVlJV3+6E7JOF6Gvm/tsM8jlhuFP9wslDGW50MYBD1aeO8HLJVwnuq8nUnRjLZhqyKhY3Qe+x7e74bsqgLlnQscYmCSMD2m/QaoVDwOtkNNSoWQcJci0NY3xqajDMo0H6IZf9FcRV9aMps3+7R6XRoZjgs8x46GegvQqFVd+d00pYeVKtK40JwWUhFEpUWBTNh3UPEjy+VLLhlEAiwu0qXIp6Lj7XBayXc7j+wpg3v1I/TLa37nTeocWsCQfe6jPTEd6fUCn9YTiEbBqpKN9NeFnka0FtPAh87TGuXC6Y3hl0BryAQ1g7gadzvG+V81whIhigtuPmmFWzrzFi5ndyYOplA6QHKC/I5COtZScEVDVVWOKc5s2lwyZz/Iz+Jq71QHaCCbBwt0erTfkODcBJuH02eY2/kFOli0XB3RbphTb790OoZ/I8jatTYzB2xqKXUPAQyDOkWz4Ht8TtmEPZ3Whh2BP8KozHcxg6zAnw= 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 2f805f1a0176..cbb4e3dbbaed 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..411a663932c4 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,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;