From patchwork Fri May 12 00:31:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13238584 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 97350C77B7F for ; Fri, 12 May 2023 00:31:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C66426B0071; Thu, 11 May 2023 20:31:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C15B76B0074; Thu, 11 May 2023 20:31:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A8F2E6B0075; Thu, 11 May 2023 20:31:11 -0400 (EDT) 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 9ACC76B0071 for ; Thu, 11 May 2023 20:31:11 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6D08B40DC9 for ; Fri, 12 May 2023 00:31:11 +0000 (UTC) X-FDA: 80779723542.11.F4006FA Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf23.hostedemail.com (Postfix) with ESMTP id 53893140013 for ; Fri, 12 May 2023 00:31:09 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OLIIuJML; spf=pass (imf23.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1683851469; 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:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=GadWHwp50KH9hxb9lEESltKioaCu8cw3tBG7j+NI1MU=; b=d3n7NL0q8X763WwZ14FyGGBXoyGn80U2fuAEABI66PlY6O1/VqRGoBDUOpot44L+GYoBQo 3VQEtkJphAb08bJ54RWDWFJ1f34Zhs4sxyNJAliyXRhc9LTUVoVcY21iw5yssXL4kP3AZv iRoq2wAKJsavgo3DIXK7JHRKeBtftFk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1683851469; a=rsa-sha256; cv=none; b=olZeCS2tSIw/PMphfvSYy2leMJ9gRiPW8FmrWPaWCo1EZd8fjI5HKef0I4U+lVVDdedQIH +5GWpMLtJo7VaaX1uqxZRgbHNdhugmH1ScRxZlDBmtJbxLvwR/EwiaFVJ3u+qqHHzf+xwp j5F1USJi0BLYYU0uSDC8CJsrdEyawyQ= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OLIIuJML; spf=pass (imf23.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683851468; h=from:from: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:content-transfer-encoding; bh=GadWHwp50KH9hxb9lEESltKioaCu8cw3tBG7j+NI1MU=; b=OLIIuJMLFAsazIZTQyVO5t3ypDLA/k/E+TuqPScI14X39MygdjisyU3pPsAkJjNsE/BCOl XRffeE9VYWNWqbLcAr74B1qSRK0BgcziYT295e+OqFDICmBMFCAE/6s0AKUKl1BDKGsVWy UAXbLzSs7lRW7KOk++BAezSCwV5QJkA= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-214-kbEiS11dNyy__FlzwDCU0A-1; Thu, 11 May 2023 20:31:07 -0400 X-MC-Unique: kbEiS11dNyy__FlzwDCU0A-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-74e23e33f80so96672085a.0 for ; Thu, 11 May 2023 17:31:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683851465; x=1686443465; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GadWHwp50KH9hxb9lEESltKioaCu8cw3tBG7j+NI1MU=; b=inaNMb40QobmC1qQDw2bl/ZxLNrsBcrYdJIKEby1SpnlTDtjh/9ug+oIRBjTjN5r+k ZV+lKLEPU3Wc5wINMA3XGqTfRqCkolFtcppzzF/iPgNZq+EhwIn7kEh54f9oy6fOmnmY ww2XkNo7XmVAA+8JYva7MBHFDJESq7m6EDbyLr7Modcop5npxfo8hqidvZD+m3vz7/00 +cDxuA8/Spy/XBEnMHyBNIUUQObvQSs6zjijv90PHIFkZDhDO+tVnk4Wcc3xBYGWvPBm jLqL+P9Sq1DxWsWd3CWFk8gVREVdP6kwm0alvIRnrApJgbcVhRrFWS4wT7gDcxcx6sAx xZYg== X-Gm-Message-State: AC+VfDy84p+r6Yq0yqDTTa+QOfRYYLOB1upIIUTUshkW+ChtXhwxXIzL 8STLK71zOPL74rgKnEU0bMiUqcDUuDsmy+vf1TKriM1Zj6UoVXO+9fSxsk8pRblqiM3Trs1yCzc z9ZJTlyPVHuIBU6VFrn310MOfUcdTgT7EAXPV2TDm7yoqmvUXxK6N64qbbmXyXCSFVd11 X-Received: by 2002:ac8:5c91:0:b0:3f2:38d:5c84 with SMTP id r17-20020ac85c91000000b003f2038d5c84mr38416156qta.4.1683851465102; Thu, 11 May 2023 17:31:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4GZ+IEWUFzTh92wkKNXuxaRW1/gPh6xOqwEBKdblJQ8DEN9s4K+2av9EFrKk0/m7CHKrQMlQ== X-Received: by 2002:ac8:5c91:0:b0:3f2:38d:5c84 with SMTP id r17-20020ac85c91000000b003f2038d5c84mr38416117qta.4.1683851464664; Thu, 11 May 2023 17:31:04 -0700 (PDT) Received: from x1n.. (bras-base-aurron9127w-grc-62-70-24-86-62.dsl.bell.ca. [70.24.86.62]) by smtp.gmail.com with ESMTPSA id v21-20020a05622a189500b003ef38277445sm1498590qtc.16.2023.05.11.17.31.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 May 2023 17:31:04 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: John Hubbard , Jason Gunthorpe , Andrew Morton , "Kirill A . Shutemov" , Lorenzo Stoakes , peterx@redhat.com, David Hildenbrand Subject: [PATCH] mm/gup: Fixes FOLL_UNLOCKABLE against FOLL_NOWAIT Date: Thu, 11 May 2023 20:31:02 -0400 Message-Id: <20230512003102.3149737-1-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Stat-Signature: 1ofqso5iozru3wgcmxp8z4aum4c5uups X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 53893140013 X-Rspam-User: X-HE-Tag: 1683851469-696966 X-HE-Meta: U2FsdGVkX18uVAgJbcOd48fBv2LfwXfEq36LCINZS6nvnx1fhYI7TAttzYz9WIUzFa8729/+tyVoUS6Xt7IgHt/AbsAAjPmqnhm8EZMgsDdLZtInZR9JIdpdTkY0TkIgA2bTzoJeJTjGXj6kYwGyMTxyjm/DqBno4w2q9iAUlmE98r3mhmbPrP2mpoBTO001bFkJi4pEStGjAv5+U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5V8a7h88CenRpr7ggrUtjeK+8yXILdSO5ouVIV0S0KR1dNKvFC+2niCC3ULRgLYjJv8ArYvofDndCLPqWnmZoBQOlC5nkusbtUHEpLfuULWN3N7ZyPnARGg8lYqVJ61xfjms1cafC5aH1tXxao5/Oa/M/pJzxcT4wZ6g= 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: This is a follow up on f04740f54594 ("mm/gup: add FOLL_UNLOCKABLE"). FOLL_NOWAIT is the gup alias of FAULT_FLAG_RETRY_NOWAIT, which means when FOLL_NOWAIT is set we definitely don't want to release the mmap read lock when faulting. It's against the meaning of the newly introduced flag FOLL_UNLOCKABLE. E.g., with current code we could at last have FAULT_FLAG_RETRY_NOWAIT set even if with a FOLL_UNLOCKABLE gup which doesn't make a lot of sense. Code-wise, it _seems_ all still fine, because when NOWAIT+UNLOCKABLE both set it'll be the same as old NOWAIT plus FAULT_FLAG_KILLABLE (since luckily both of them leverage ALLOW_RETRY OTOH), which I don't see a major issue so far. So not copying stable or attaching fixes, as there's no immediate issue found. Still better clarify the use. Since at it, the same commit added unconditional FOLL_UNLOCKABLE in faultin_vma_page_range(), which is code-wise correct becuase the helper only has one user right now and it always has "locked" set. However it can be abused if someone reuse faultin_vma_page_range() in other call sites in the future. Add a sanity check for that, also add the missing comment for UNLOCKABLE. Cc: Jason Gunthorpe Signed-off-by: Peter Xu --- This is something I found when I was reading the code alongside only. I hope I didn't miss something. --- mm/gup.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 90d9b65ff35c..202097627667 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1621,6 +1621,9 @@ long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start, VM_BUG_ON_VMA(end > vma->vm_end, vma); mmap_assert_locked(mm); + /* We'll do unconditional FOLL_UNLOCKABLE */ + VM_WARN_ON_ONCE(!locked); + /* * FOLL_TOUCH: Mark page accessed and thereby young; will also mark * the page dirty with FOLL_WRITE -- which doesn't make a @@ -1629,6 +1632,7 @@ long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start, * FOLL_HWPOISON: Return -EHWPOISON instead of -EFAULT when we hit * a poisoned page. * !FOLL_FORCE: Require proper access permissions. + * FOLL_UNLOCKABLE: Allow the fault to unlock mmap read lock */ gup_flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_UNLOCKABLE; if (write) @@ -2334,10 +2338,13 @@ EXPORT_SYMBOL(get_user_pages); long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags) { + unsigned int extra = FOLL_TOUCH; int locked = 0; - if (!is_valid_gup_args(pages, NULL, NULL, &gup_flags, - FOLL_TOUCH | FOLL_UNLOCKABLE)) + if (!(gup_flags & FOLL_NOWAIT)) + extra |= FOLL_UNLOCKABLE; + + if (!is_valid_gup_args(pages, NULL, NULL, &gup_flags, extra)) return -EINVAL; return __get_user_pages_locked(current->mm, start, nr_pages, pages,