From patchwork Wed Jun 19 00:05:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13703198 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 EE804C27C4F for ; Wed, 19 Jun 2024 00:05:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E12E56B02C1; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D40B46B02BE; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A0C056B02BD; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 747DF6B02B2 for ; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2064880AEF for ; Wed, 19 Jun 2024 00:05:31 +0000 (UTC) X-FDA: 82245694062.25.F74FE2D Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by imf21.hostedemail.com (Postfix) with ESMTP id E87BA1C0016 for ; Wed, 19 Jun 2024 00:05:28 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=licRGVV3; spf=pass (imf21.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718755523; 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:in-reply-to:references:references:dkim-signature; bh=/OyteV4yM0qUOEafhhbvbTmI9bXZz54sg4qnQFhyy5A=; b=oX0bx11+y3R5EZ0aSOC7vV7GLjpBhfmgaYC3/yBkK5NUUYpZu1mh1riLQoqtkmrh2yxzrX hAmiCMzsp0o7LI30/FySPFju34RPf6CA1YmSfiM51zDVglLL/Br5Ca+zxkf18ZWRfr84C7 8t+djzhcu+/wNEjsIIlQU/iZsfZuowk= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=licRGVV3; spf=pass (imf21.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718755523; a=rsa-sha256; cv=none; b=sufk9vymVWFCmYjoRs/eiT3Vvb4OC4zfneC+PIk1AKOFpinfeZxq1qdnkA8d2pqHlO2qIz JdQ+kgVOvuM6nDw97tHiuhB+hh98lpOir5jisrkJk09xfRRAhHowDwHhl7j2Lsa9XMZLq9 IrB/MbA3diHCscsv5psgSfMQ1sPqd7k= Received: from pps.filterd (m0279872.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45ILaNQ0001140; Wed, 19 Jun 2024 00:05:18 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= /OyteV4yM0qUOEafhhbvbTmI9bXZz54sg4qnQFhyy5A=; b=licRGVV3sGtCHq36 0d7qxPKp8nHzVyyyjoDtjlEIvyc2Yl9kIh+zg/1vUDXoiMeuH4qrR8zEBbzYvhCQ NUka56yx1u+Ai8QeAN5kLA66MICuH0QcNTBlA5cchSBCmxBCfR9SAatx+pkRTzTm U7JSMQHJjmxKZZ5qhWtr2sjgInmJwV+pwQWrv6eqnmFQWGNJI7kTZqe4CDsTYn+p rO7Fsy2I/oduWoMg66mamclM2wjvvYcWlIzaHJlxeGOKpUs/sDqA8E+tKKKQBQEw sznWTshLaFRzYo2tyLd5eaELSFmOvfjHrcBKRTRdLqyrO51KbVbmiZSfjF94kRxC y12OOQ== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3yuja287fu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 19 Jun 2024 00:05:18 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA03.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 45J05GcX029964 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 19 Jun 2024 00:05:16 GMT Received: from hu-eberman-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Tue, 18 Jun 2024 17:05:16 -0700 From: Elliot Berman Date: Tue, 18 Jun 2024 17:05:07 -0700 Subject: [PATCH RFC 1/5] mm/gup: Move GUP_PIN_COUNTING_BIAS to page_ref.h MIME-Version: 1.0 Message-ID: <20240618-exclusive-gup-v1-1-30472a19c5d1@quicinc.com> References: <20240618-exclusive-gup-v1-0-30472a19c5d1@quicinc.com> In-Reply-To: <20240618-exclusive-gup-v1-0-30472a19c5d1@quicinc.com> To: Andrew Morton , Shuah Khan , David Hildenbrand , Matthew Wilcox , CC: , , , , , , Elliot Berman , Fuad Tabba X-Mailer: b4 0.13.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: eoEoI_FFBsbRB2SSN34e9igotn1wcWa9 X-Proofpoint-ORIG-GUID: eoEoI_FFBsbRB2SSN34e9igotn1wcWa9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-18_06,2024-06-17_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 mlxlogscore=684 suspectscore=0 clxscore=1015 impostorscore=0 malwarescore=0 mlxscore=0 lowpriorityscore=0 bulkscore=0 spamscore=0 phishscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2406180176 X-Stat-Signature: oo1s5c1kr7edk83nz7axtz4gis3ctd5r X-Rspam-User: X-Rspamd-Queue-Id: E87BA1C0016 X-Rspamd-Server: rspam02 X-HE-Tag: 1718755528-246285 X-HE-Meta: U2FsdGVkX1/ThNVdAoMpn5j+JJ1gg0GHw+hHd3S8NFkAJWnzBcuRew+v5nFKLVixj9WUrkF3KighBeCLJUueYfKe2nFzu3kL4C4BpkoMNeKomIUMSt29tL3r0xJ2+nOKEUEqianMQtv4xpUGJazbsO8zK8yHpEJxbJP8vujmr9mBrBmT4sLJNw866219P2FZh+oeAV//YRKxSSuXGQ/GeYjJGznCSzHGj3LQG4F8iScBMNfQC/bH1HuUB5j10mdEI6qRHOTAld8MVQIG9PGPKXSCsiVodth71COr1KC1iCxKqZFQcQ3anWU9JY5Ph+SgvCusFMARq6LNNcqSN0yZROpBf+oKphc6XBPcD0NV33QlAp4lToNQjXHW1WbQKVk0DIokgm9lSdOZzYAvAvKoz6t3CyOYSqglBy4kIuTot8b5kG+7jQJG2k6NayDMIqxidTvHm437BwjATTojGxrCZfDmItkal0TgXGyBG8xAeNrwBOoaa9U5ujqQHEsxg5S8PPTGQihOIdmGOy72tv2KzokGNRQZF24mERuGDsoOUJP/H1rgpOR7v9w7WjrMst50fSHxrRo8JsqTrK/E9z8gMZJQsniL37VouZjkGP9rzvoGlXkVmTA2HOXTsZIDjLlW6/0BmovWpOKhAlM74r3lsiDOGsp6bcNBXzHUCr16huIynTxPHE44H2ggyicGa+wAh/JFjTLfKe5NhqkOLgGzrbfouoEJ7dq1BBMsT8ng1OqF/MNyB1Tp50c3zv4DLbosRWqFX6nfrMCJ5LQwbWtutqYdXLigaV2/zGWnUJzJZDf5YW/gBLeHBO0X4ABG47pCRnYrj+rpLh4rTFeVp0DgTrZNUchw7X9WiL0IfvWGikfoHUVo9Pbt1CLZ8WPtnmzD72ZwcwuC6tmOhC2VQONM6pJ/X1jskaJLJgNEwSfFuwPdmYm2mQppW5OxEtJ1G/7nYWMuH0PfDAsSRoyUAfZ YwE+ZGgD JPDyQdDLLoDgyCsO9Nidy0sT61DKPLKj2OjtwLusK2/Q5/x08ml0QboEsSNznqfsyp3KaYIaM3wRWDGJoxg+B4fHi9hGcs4gm2Mq80iJxXaxb5VG8hP0pOQbRiCo6UNts+gVkrQdJvzNsTx06RBjjtjta9KFxQJQXDvVFYvngGEBqZwz0eKH/Y9ZIMhE7MdOPah/T2JlM6hWvRVz2a9HVRC88jq6is3MfxpeW/AKSNyRkoSd5es0BGdxwg1AnTrhWCGs325yKOzVtJ5lRUjV6TDhe5a54ty4PziY0FiqrhpGprnrYE1s+CAg1LEJiegkX+XteDtbRviR1vR7Eq/7SmOJQVlw1VieqHW2WkT2JX5pAy1jNdxc55Avenw== 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: From: Fuad Tabba No functional change intended. Signed-off-by: Fuad Tabba Signed-off-by: Elliot Berman --- include/linux/mm.h | 32 -------------------------------- include/linux/page_ref.h | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 9849dfda44d43..fd0d10b08e7ac 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1580,38 +1580,6 @@ static inline void put_page(struct page *page) folio_put(folio); } -/* - * GUP_PIN_COUNTING_BIAS, and the associated functions that use it, overload - * the page's refcount so that two separate items are tracked: the original page - * reference count, and also a new count of how many pin_user_pages() calls were - * made against the page. ("gup-pinned" is another term for the latter). - * - * With this scheme, pin_user_pages() becomes special: such pages are marked as - * distinct from normal pages. As such, the unpin_user_page() call (and its - * variants) must be used in order to release gup-pinned pages. - * - * Choice of value: - * - * By making GUP_PIN_COUNTING_BIAS a power of two, debugging of page reference - * counts with respect to pin_user_pages() and unpin_user_page() becomes - * simpler, due to the fact that adding an even power of two to the page - * refcount has the effect of using only the upper N bits, for the code that - * counts up using the bias value. This means that the lower bits are left for - * the exclusive use of the original code that increments and decrements by one - * (or at least, by much smaller values than the bias value). - * - * Of course, once the lower bits overflow into the upper bits (and this is - * OK, because subtraction recovers the original values), then visual inspection - * no longer suffices to directly view the separate counts. However, for normal - * applications that don't have huge page reference counts, this won't be an - * issue. - * - * Locking: the lockless algorithm described in folio_try_get_rcu() - * provides safe operation for get_user_pages(), page_mkclean() and - * other calls that race to set up page table entries. - */ -#define GUP_PIN_COUNTING_BIAS (1U << 10) - void unpin_user_page(struct page *page); void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, bool make_dirty); diff --git a/include/linux/page_ref.h b/include/linux/page_ref.h index 1acf5bac7f503..e6aeaafb143ca 100644 --- a/include/linux/page_ref.h +++ b/include/linux/page_ref.h @@ -62,6 +62,38 @@ static inline void __page_ref_unfreeze(struct page *page, int v) #endif +/* + * GUP_PIN_COUNTING_BIAS, and the associated functions that use it, overload + * the page's refcount so that two separate items are tracked: the original page + * reference count, and also a new count of how many pin_user_pages() calls were + * made against the page. ("gup-pinned" is another term for the latter). + * + * With this scheme, pin_user_pages() becomes special: such pages are marked as + * distinct from normal pages. As such, the unpin_user_page() call (and its + * variants) must be used in order to release gup-pinned pages. + * + * Choice of value: + * + * By making GUP_PIN_COUNTING_BIAS a power of two, debugging of page reference + * counts with respect to pin_user_pages() and unpin_user_page() becomes + * simpler, due to the fact that adding an even power of two to the page + * refcount has the effect of using only the upper N bits, for the code that + * counts up using the bias value. This means that the lower bits are left for + * the exclusive use of the original code that increments and decrements by one + * (or at least, by much smaller values than the bias value). + * + * Of course, once the lower bits overflow into the upper bits (and this is + * OK, because subtraction recovers the original values), then visual inspection + * no longer suffices to directly view the separate counts. However, for normal + * applications that don't have huge page reference counts, this won't be an + * issue. + * + * Locking: the lockless algorithm described in folio_try_get_rcu() + * provides safe operation for get_user_pages(), page_mkclean() and + * other calls that race to set up page table entries. + */ +#define GUP_PIN_COUNTING_BIAS (1U << 10) + static inline int page_ref_count(const struct page *page) { return atomic_read(&page->_refcount); From patchwork Wed Jun 19 00:05:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13703201 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 8CEF0C27C4F for ; Wed, 19 Jun 2024 00:05:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 184006B02D1; Tue, 18 Jun 2024 20:05:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F3ED36B02D4; Tue, 18 Jun 2024 20:05:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D41936B02D0; Tue, 18 Jun 2024 20:05:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 8EB166B02C7 for ; Tue, 18 Jun 2024 20:05:32 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3F892160BBF for ; Wed, 19 Jun 2024 00:05:32 +0000 (UTC) X-FDA: 82245694104.28.89DE7F9 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by imf12.hostedemail.com (Postfix) with ESMTP id 1C97940006 for ; Wed, 19 Jun 2024 00:05:29 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=ox6jmTii; spf=pass (imf12.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718755526; a=rsa-sha256; cv=none; b=h5krRaJyEfp27RISwRMHCXne3bXD62cw3K/U0pXBbn2RFZVI+flkLVKIBNtL2U/Ta2Dns2 df62qwFRpefc+hLy6hMvAjWUV4JLumUzPljYcNDUoVLpSwWgtBkl4Uk45+08aSOM67grQ3 tQ6ZEf993WuPQj5f3QQzWCADC+P9imU= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=ox6jmTii; spf=pass (imf12.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718755526; 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:in-reply-to:references:references:dkim-signature; bh=Pdz9/HkhqVOEA9g7xL2yq+OHaJU30ffU6niiZoYVcdE=; b=SmEEvnGgrY0q5X93E92RqFxZs++ajo/CfPNu4gdxBqi7XXy3sbXzvXqYlro3EoyKBzSU4B 6JQCnEKjcJIS/HMnb2VvfC1AqSZwyENfolVDdh9haLh0/gBUOlbRTRwPRf7HSFQTbaaj13 Oc97dxJOqvsNB1kbTatNJzhYLkbw8dc= Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45ILcmhp008764; Wed, 19 Jun 2024 00:05:18 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= Pdz9/HkhqVOEA9g7xL2yq+OHaJU30ffU6niiZoYVcdE=; b=ox6jmTiiLNHGqjgh KTUecqGu4t9dr97sxEjvvPIIrWyk570X0NTtmxMw43BqntSgStcrqmf+GhulZ4sL BBsW9sIJXypcGMeWn08B7oQEsGPb/VJM81uXiwnsxc0u0eCw2y21c2ILpZ8AuCBv xcav7jkify34Sl75n+/hCEUj8SLHBK4Ibq3X7k5uQdXV1/FJt2/XY9PFb0wmtEil dPLsXNKSQ4pKBMbaIYNJHyV0uYRXI4tXEPlslPuyBWSYeS0pAo/jcOYx0etIvLIF 6NXPF4FZwuMX3PYj2zWhTdEh9vp79nZZpYGWs6W0Y16AeVuxXxeejU+VQc9bcShJ 6HKIZA== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3yuja787bg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 19 Jun 2024 00:05:18 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA05.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 45J05H2f006583 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 19 Jun 2024 00:05:17 GMT Received: from hu-eberman-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Tue, 18 Jun 2024 17:05:16 -0700 From: Elliot Berman Date: Tue, 18 Jun 2024 17:05:08 -0700 Subject: [PATCH RFC 2/5] mm/gup: Add an option for obtaining an exclusive pin MIME-Version: 1.0 Message-ID: <20240618-exclusive-gup-v1-2-30472a19c5d1@quicinc.com> References: <20240618-exclusive-gup-v1-0-30472a19c5d1@quicinc.com> In-Reply-To: <20240618-exclusive-gup-v1-0-30472a19c5d1@quicinc.com> To: Andrew Morton , Shuah Khan , David Hildenbrand , Matthew Wilcox , CC: , , , , , , Elliot Berman , Fuad Tabba X-Mailer: b4 0.13.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: vSrJ-1fL5TL-mlKFMXsXbosJI6IXdRVp X-Proofpoint-GUID: vSrJ-1fL5TL-mlKFMXsXbosJI6IXdRVp X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-18_06,2024-06-17_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 impostorscore=0 lowpriorityscore=0 adultscore=0 mlxlogscore=802 malwarescore=0 phishscore=0 bulkscore=0 suspectscore=0 clxscore=1015 spamscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2406180176 X-Stat-Signature: dmyt7qra4stw6wqf91ftrnizykh3acci X-Rspamd-Queue-Id: 1C97940006 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1718755529-480 X-HE-Meta: U2FsdGVkX1+1lBcqk5QBIOsCXx0f3bYMlgieOd2DCqusR2hOZyBrPGdwUcKiTvjHJLVEFQ0HEysoaSAZQjj2Yr+YKQ81E6dTFALUW5Hj1qS/T4oTJd64+/iHCoEGGfbJU/v0UhFMrt33t1CutxaOPSFyLsSvVCj9aIq1YkxTS1nT9XgzZsXGk2QXIoxLspFAZ1y8kBleyjFaZHxqM/MNeB05w/cuxAlkcf/hwAkQ4wd/oq2T/8o1vvGfwTDXDU8oFQREsBLeRtV0+9BgvmDEDIOq8gJJrP8i3S3YLwJ6WIZT7cah7WJUl0qFuQGAg4rTbnEexQ3DxDjjrV6XEXdq05KFimasiONA45bZcoOu3r0whDYPtT2qeGfn29Tz+Lxcmg7o7FMytYM3Abrhmfjlo6kj5GWLnlcEwJaS0WPupCIIQCS2W3XIPlidGyQCcUvuxvP75N2xCuh8MQ8/ZacJYIh9bW7txINNWD1h6GVra2HY8shx/UTDRLguz3VVRSBV4/m9VKvEF3SDw+irAdQ42PjXQNtZjWpWrIhAIoorCrSkilBxRhF8w/51TjsSeDMCLbgDoPvQpztDP7RXKaxNB5yWX4Tfs0l8sz+r27VYmUyYyaERDJ5iGBidJ3GRLc3WKIOLvox7IETYImNPdkNR7p+Ux6UIGsFsGeiIP3hvKxh6WWLXGTKhdWj4PkSWR+5yq4p4ntph3shIdO6giM5jby9qEKaSHQCu8hngsK8iAhgtmZ/BD2zt88j2Bg+rccgliDYvCNFY+BNZ7WXfCsemdNbPUH7fi9kt7W6wPB97oZHVOAa/sCECxqvGmec7x6S6hpsvnKC08UxPZOPpTcpAnMq3biAvR4u9QrtRj4sRQTlqcFGI++tARDPhAJULRFXpPdlFhmrbo/fLos5Ejg23g2SWVgorHLrmoswytueYFoJmV+93hBaBLDYvKoC2zeWuIjHbWistSpCPckHFIA8 zl6g4SQm J/pc2b4q+oqfyxediolX9xs8Oc5rnqWFWMl6XjXW5qcXN61PjdL2dq9/YzDkm2804paXGnROkNZ6DRGugeBhZOJ4WXXxMbqDzLf54OHaxtHcMmNv01PGXycbGDThH5ETdbTu/XJA6ay+w3m+j+nIlxE8ceGgkvoHpk2gEWhJTsSC0C3dGVETrsBTMOJbvnlUiEnp8WMDu7f20H8O7THr+ULxkiNIRPZk1/MhA5nVd0GV6dkBeVNNbyBwJKeOs54Hx9d3cqkjU2t1kju36y95PtiIrV8Lvi6KSuX9Nib01/zX/sLbRbq1qAIMqPYPBHTtio7ZKFKOzG9yLreL+wRIaYW0xN9c4BoL8CFb0lt/CoOLM9Bo= 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: From: Fuad Tabba Introduce the ability to obtain an exclusive long-term pin on a page. This exclusive pin can only be held if there are no other pins on the page, regular, or exclusive. Moreover, once this pin is held, no other pins can be grabbed until the exclusive pin is released. This pin is grabbed using the (new) FOLL_EXCLUSIVE flag, and is gated by the EXCLUSIVE_PIN configuration option. Similar to how the normal GUP pin is obtain, the exclusive PIN overloads the _refcount field for normal pages, or the _pincount field for large pages. It appropriates bit 30 of these two fields, which still allows the detection of overflows into bit 31. It does however, half the number of potential normals pins for a page. In order to avoid the possibility of COWing such a page, once an exclusive pin has been obtained, it's marked as AnonExclusive. Co-Developed-by: Elliot Berman Signed-off-by: Elliot Berman Signed-off-by: Fuad Tabba Signed-off-by: Elliot Berman --- include/linux/mm.h | 24 +++++ include/linux/mm_types.h | 2 + include/linux/page_ref.h | 36 +++++++ mm/Kconfig | 5 + mm/gup.c | 239 +++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 279 insertions(+), 27 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index fd0d10b08e7ac..d03d62bceba08 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1583,9 +1583,13 @@ static inline void put_page(struct page *page) void unpin_user_page(struct page *page); void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, bool make_dirty); +void unpin_exc_pages_dirty_lock(struct page **pages, unsigned long npages, + bool make_dirty); void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages, bool make_dirty); void unpin_user_pages(struct page **pages, unsigned long npages); +void unpin_exc_pages(struct page **pages, unsigned long npages); +void unexc_user_page(struct page *page); static inline bool is_cow_mapping(vm_flags_t flags) { @@ -1958,6 +1962,26 @@ static inline bool folio_needs_cow_for_dma(struct vm_area_struct *vma, return folio_maybe_dma_pinned(folio); } +static inline bool folio_maybe_exclusive_pinned(const struct folio *folio) +{ + unsigned int count; + + if (!IS_ENABLED(CONFIG_EXCLUSIVE_PIN)) + return false; + + if (folio_test_large(folio)) + count = atomic_read(&folio->_pincount); + else + count = folio_ref_count(folio); + + return count >= GUP_PIN_EXCLUSIVE_BIAS; +} + +static inline bool page_maybe_exclusive_pinned(const struct page *page) +{ + return folio_maybe_exclusive_pinned(page_folio(page)); +} + /** * is_zero_page - Query if a page is a zero page * @page: The page to query diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index af3a0256fa93b..dc397e3465c23 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1465,6 +1465,8 @@ enum { * hinting faults. */ FOLL_HONOR_NUMA_FAULT = 1 << 12, + /* exclusive PIN only if there aren't other pins (including this) */ + FOLL_EXCLUSIVE = 1 << 13, /* See also internal only FOLL flags in mm/internal.h */ }; diff --git a/include/linux/page_ref.h b/include/linux/page_ref.h index e6aeaafb143ca..9d16e1f4db094 100644 --- a/include/linux/page_ref.h +++ b/include/linux/page_ref.h @@ -94,6 +94,14 @@ static inline void __page_ref_unfreeze(struct page *page, int v) */ #define GUP_PIN_COUNTING_BIAS (1U << 10) +/* + * GUP_PIN_EXCLUSIVE_BIAS is used to grab an exclusive pin over a page. + * This exclusive pin can only be taken once, and only if no other GUP pins + * exist for the page. + * After it's taken, no other gup pins can be taken. + */ +#define GUP_PIN_EXCLUSIVE_BIAS (1U << 30) + static inline int page_ref_count(const struct page *page) { return atomic_read(&page->_refcount); @@ -147,6 +155,34 @@ static inline void init_page_count(struct page *page) set_page_count(page, 1); } +static __must_check inline bool page_ref_setexc(struct page *page, unsigned int refs) +{ + unsigned int old_count, new_count; + + if (WARN_ON_ONCE(refs >= GUP_PIN_EXCLUSIVE_BIAS)) + return false; + + do { + old_count = atomic_read(&page->_refcount); + + if (old_count >= GUP_PIN_COUNTING_BIAS) + return false; + + if (check_add_overflow(old_count, refs + GUP_PIN_EXCLUSIVE_BIAS, &new_count)) + return false; + } while (atomic_cmpxchg(&page->_refcount, old_count, new_count) != old_count); + + if (page_ref_tracepoint_active(page_ref_mod)) + __page_ref_mod(page, refs); + + return true; +} + +static __must_check inline bool folio_ref_setexc(struct folio *folio, unsigned int refs) +{ + return page_ref_setexc(&folio->page, refs); +} + static inline void page_ref_add(struct page *page, int nr) { atomic_add(nr, &page->_refcount); diff --git a/mm/Kconfig b/mm/Kconfig index b4cb45255a541..56f8c80b996f5 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1249,6 +1249,11 @@ config IOMMU_MM_DATA config EXECMEM bool +config EXCLUSIVE_PIN + def_bool y + help + Add support for exclusive pins of pages. + source "mm/damon/Kconfig" endmenu diff --git a/mm/gup.c b/mm/gup.c index ca0f5cedce9b2..7f20de33221da 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -97,6 +97,65 @@ static inline struct folio *try_get_folio(struct page *page, int refs) return folio; } +static bool large_folio_pin_setexc(struct folio *folio, unsigned int pins) +{ + unsigned int old_pincount, new_pincount; + + if (WARN_ON_ONCE(pins >= GUP_PIN_EXCLUSIVE_BIAS)) + return false; + + do { + old_pincount = atomic_read(&folio->_pincount); + + if (old_pincount > 0) + return false; + + if (check_add_overflow(old_pincount, pins + GUP_PIN_EXCLUSIVE_BIAS, &new_pincount)) + return false; + } while (atomic_cmpxchg(&folio->_pincount, old_pincount, pins) != old_pincount); + + return true; +} + +static bool __try_grab_folio_excl(struct folio *folio, int pincount, int refcount) +{ + if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_EXCLUSIVE_PIN))) + return false; + + if (folio_test_large(folio)) { + if (!large_folio_pin_setexc(folio, pincount)) + return false; + } else if (!folio_ref_setexc(folio, refcount)) { + return false; + } + + if (!PageAnonExclusive(&folio->page)) + SetPageAnonExclusive(&folio->page); + + return true; +} + +static bool try_grab_folio_excl(struct folio *folio, int refs) +{ + /* + * When pinning a large folio, use an exact count to track it. + * + * However, be sure to *also* increment the normal folio + * refcount field at least once, so that the folio really + * is pinned. That's why the refcount from the earlier + * try_get_folio() is left intact. + */ + return __try_grab_folio_excl(folio, refs, + refs * (GUP_PIN_COUNTING_BIAS - 1)); +} + +static bool try_grab_page_excl(struct page *page) +{ + struct folio *folio = page_folio(page); + + return __try_grab_folio_excl(folio, 1, GUP_PIN_COUNTING_BIAS); +} + /** * try_grab_folio() - Attempt to get or pin a folio. * @page: pointer to page to be grabbed @@ -161,19 +220,41 @@ struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags) return NULL; } - /* - * When pinning a large folio, use an exact count to track it. - * - * However, be sure to *also* increment the normal folio - * refcount field at least once, so that the folio really - * is pinned. That's why the refcount from the earlier - * try_get_folio() is left intact. - */ - if (folio_test_large(folio)) - atomic_add(refs, &folio->_pincount); - else - folio_ref_add(folio, - refs * (GUP_PIN_COUNTING_BIAS - 1)); + if (unlikely(folio_maybe_exclusive_pinned(folio))) { + if (!put_devmap_managed_folio_refs(folio, refs)) + folio_put_refs(folio, refs); + return NULL; + } + + if (unlikely(flags & FOLL_EXCLUSIVE)) { + if (!try_grab_folio_excl(folio, refs)) + return NULL; + } else { + /* + * When pinning a large folio, use an exact count to track it. + * + * However, be sure to *also* increment the normal folio + * refcount field at least once, so that the folio really + * is pinned. That's why the refcount from the earlier + * try_get_folio() is left intact. + */ + if (folio_test_large(folio)) + atomic_add(refs, &folio->_pincount); + else + folio_ref_add(folio, + refs * (GUP_PIN_COUNTING_BIAS - 1)); + + if (unlikely(folio_maybe_exclusive_pinned(folio))) { + if (folio_test_large(folio)) + atomic_sub(refs, &folio->_pincount); + else + folio_put_refs(folio, + refs * (GUP_PIN_COUNTING_BIAS - 1)); + + return NULL; + } + } + /* * Adjust the pincount before re-checking the PTE for changes. * This is essentially a smp_mb() and is paired with a memory @@ -198,6 +279,26 @@ static void gup_put_folio(struct folio *folio, int refs, unsigned int flags) refs *= GUP_PIN_COUNTING_BIAS; } + if (unlikely(flags & FOLL_EXCLUSIVE)) { + if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_EXCLUSIVE_PIN))) + goto out; + if (is_zero_folio(folio)) + return; + if (folio_test_large(folio)) { + if (WARN_ON_ONCE((atomic_read(&folio->_pincount) < GUP_PIN_EXCLUSIVE_BIAS))) + goto out; + atomic_sub(GUP_PIN_EXCLUSIVE_BIAS, &folio->_pincount); + } else { + if (WARN_ON_ONCE((unsigned int)refs >= GUP_PIN_EXCLUSIVE_BIAS)) + goto out; + if (WARN_ON_ONCE(folio_ref_count(folio) < GUP_PIN_EXCLUSIVE_BIAS)) + goto out; + + refs += GUP_PIN_EXCLUSIVE_BIAS; + } + } + +out: if (!put_devmap_managed_folio_refs(folio, refs)) folio_put_refs(folio, refs); } @@ -242,16 +343,35 @@ int __must_check try_grab_page(struct page *page, unsigned int flags) if (is_zero_page(page)) return 0; - /* - * Similar to try_grab_folio(): be sure to *also* - * increment the normal page refcount field at least once, - * so that the page really is pinned. - */ - if (folio_test_large(folio)) { - folio_ref_add(folio, 1); - atomic_add(1, &folio->_pincount); + if (unlikely(folio_maybe_exclusive_pinned(folio))) + return -EBUSY; + + if (unlikely(flags & FOLL_EXCLUSIVE)) { + if (!try_grab_page_excl(page)) + return -EBUSY; } else { - folio_ref_add(folio, GUP_PIN_COUNTING_BIAS); + /* + * Similar to try_grab_folio(): be sure to *also* + * increment the normal page refcount field at least once, + * so that the page really is pinned. + */ + if (folio_test_large(folio)) { + folio_ref_add(folio, 1); + atomic_add(1, &folio->_pincount); + } else { + folio_ref_add(folio, GUP_PIN_COUNTING_BIAS); + } + + if (unlikely(folio_maybe_exclusive_pinned(folio))) { + if (folio_test_large(folio)) { + folio_put_refs(folio, 1); + atomic_sub(1, &folio->_pincount); + } else { + folio_put_refs(folio, GUP_PIN_COUNTING_BIAS); + } + + return -EBUSY; + } } node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, 1); @@ -288,6 +408,9 @@ void folio_add_pin(struct folio *folio) if (is_zero_folio(folio)) return; + if (unlikely(folio_maybe_exclusive_pinned(folio))) + return; + /* * Similar to try_grab_folio(): be sure to *also* increment the normal * page refcount field at least once, so that the page really is @@ -301,6 +424,15 @@ void folio_add_pin(struct folio *folio) WARN_ON_ONCE(folio_ref_count(folio) < GUP_PIN_COUNTING_BIAS); folio_ref_add(folio, GUP_PIN_COUNTING_BIAS); } + + if (unlikely(folio_maybe_exclusive_pinned(folio))) { + if (folio_test_large(folio)) { + folio_put_refs(folio, 1); + atomic_sub(1, &folio->_pincount); + } else { + folio_put_refs(folio, GUP_PIN_COUNTING_BIAS); + } + } } static inline struct folio *gup_folio_range_next(struct page *start, @@ -355,8 +487,8 @@ static inline struct folio *gup_folio_next(struct page **list, * set_page_dirty_lock(), unpin_user_page(). * */ -void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, - bool make_dirty) +static void __unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, + bool make_dirty, unsigned int flags) { unsigned long i; struct folio *folio; @@ -395,11 +527,28 @@ void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, folio_mark_dirty(folio); folio_unlock(folio); } - gup_put_folio(folio, nr, FOLL_PIN); + gup_put_folio(folio, nr, flags); } } + +void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, + bool make_dirty) +{ + __unpin_user_pages_dirty_lock(pages, npages, make_dirty, FOLL_PIN); +} EXPORT_SYMBOL(unpin_user_pages_dirty_lock); +void unpin_exc_pages_dirty_lock(struct page **pages, unsigned long npages, + bool make_dirty) +{ + if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_EXCLUSIVE_PIN))) + return; + + __unpin_user_pages_dirty_lock(pages, npages, make_dirty, + FOLL_PIN | FOLL_EXCLUSIVE); +} +EXPORT_SYMBOL(unpin_exc_pages_dirty_lock); + /** * unpin_user_page_range_dirty_lock() - release and optionally dirty * gup-pinned page range @@ -466,7 +615,7 @@ static void gup_fast_unpin_user_pages(struct page **pages, unsigned long npages) * * Please see the unpin_user_page() documentation for details. */ -void unpin_user_pages(struct page **pages, unsigned long npages) +static void __unpin_user_pages(struct page **pages, unsigned long npages, unsigned int flags) { unsigned long i; struct folio *folio; @@ -483,11 +632,35 @@ void unpin_user_pages(struct page **pages, unsigned long npages) sanity_check_pinned_pages(pages, npages); for (i = 0; i < npages; i += nr) { folio = gup_folio_next(pages, npages, i, &nr); - gup_put_folio(folio, nr, FOLL_PIN); + gup_put_folio(folio, nr, flags); } } + +void unpin_user_pages(struct page **pages, unsigned long npages) +{ + __unpin_user_pages(pages, npages, FOLL_PIN); +} EXPORT_SYMBOL(unpin_user_pages); +void unpin_exc_pages(struct page **pages, unsigned long npages) +{ + if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_EXCLUSIVE_PIN))) + return; + + __unpin_user_pages(pages, npages, FOLL_PIN | FOLL_EXCLUSIVE); +} +EXPORT_SYMBOL(unpin_exc_pages); + +void unexc_user_page(struct page *page) +{ + if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_EXCLUSIVE_PIN))) + return; + + sanity_check_pinned_pages(&page, 1); + gup_put_folio(page_folio(page), 0, FOLL_EXCLUSIVE); +} +EXPORT_SYMBOL(unexc_user_page); + /* * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's * lifecycle. Avoid setting the bit unless necessary, or it might cause write @@ -2610,6 +2783,18 @@ static bool is_valid_gup_args(struct page **pages, int *locked, if (WARN_ON_ONCE(!(gup_flags & FOLL_PIN) && (gup_flags & FOLL_LONGTERM))) return false; + /* EXCLUSIVE can only be specified when config is enabled */ + if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_EXCLUSIVE_PIN) && (gup_flags & FOLL_EXCLUSIVE))) + return false; + + /* EXCLUSIVE can only be specified when pinning */ + if (WARN_ON_ONCE(!(gup_flags & FOLL_PIN) && (gup_flags & FOLL_EXCLUSIVE))) + return false; + + /* EXCLUSIVE can only be specified when LONGTERM */ + if (WARN_ON_ONCE(!(gup_flags & FOLL_LONGTERM) && (gup_flags & FOLL_EXCLUSIVE))) + return false; + /* Pages input must be given if using GET/PIN */ if (WARN_ON_ONCE((gup_flags & (FOLL_GET | FOLL_PIN)) && !pages)) return false; From patchwork Wed Jun 19 00:05:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13703199 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 698BBC2BA15 for ; Wed, 19 Jun 2024 00:05:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 556046B02C8; Tue, 18 Jun 2024 20:05:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43F016B02C7; Tue, 18 Jun 2024 20:05:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E108D6B02BC; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) 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 B70836B02B2 for ; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5BFDBC0BC8 for ; Wed, 19 Jun 2024 00:05:31 +0000 (UTC) X-FDA: 82245694062.04.1B63929 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by imf25.hostedemail.com (Postfix) with ESMTP id 1E37EA0003 for ; Wed, 19 Jun 2024 00:05:28 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=Sr6RKo8y; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf25.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718755521; 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:in-reply-to:references:references:dkim-signature; bh=iRWeq01fhpWDKacRAHXGRmQhpNUX3EjJdBREfuPnW/s=; b=buhBcD8qEExkjFx+BmWVJnjzufl+Tbo8dW7bihu4h7IhI4iW2rjvFd0WN1rykl+FbyHlw0 5C7YTBLR88Q9+NAM/Fy5nk7Ze/xNETVQZx0UUROAvolkH2/BPbHIU6nkNX2w7ECVx+DEEd KBeqbxgA8XXrIBQeVCexskoLifcyrlw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718755521; a=rsa-sha256; cv=none; b=hL5+Y9iO+0cxJ51WlWEWjHdR4SFShJSIN44nHZdEVawcmRbTmKE9vOpNsPRDSOxVLD0Iol aF3M14XMIwNJ5cUSm5JGSN44Ebb/IDWnW0hOhz3/DXlxxTXVbAzVeVGbQ1Gb9Xns2hFvpV 8ZPVpjYVeBj8TA0Vosl1rdkiHDWVOOo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=Sr6RKo8y; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf25.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45ILb6h3006655; Wed, 19 Jun 2024 00:05:18 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= iRWeq01fhpWDKacRAHXGRmQhpNUX3EjJdBREfuPnW/s=; b=Sr6RKo8yez2811fS GciE+gQYZgtPZlfph3QNk/bpj/XO6a12duNefTd9I+nKa7/qYlcWEdAKTjEGaUqB jlji6Iv8+SzzzK2Pg4akuqixk+WoC24HuL7aGIY3hiJ2SHvQUAvslQ4esZ33CNXJ 1ylsU3wftuGrYeDOGsi9QqNTTXMElSzU6I1sM5bbK4IvLrv9p9DWpv3XSRRB2L9Z Wg+xbwkDSHa/jySmFZEHmozR/1LKn7u87hHwXyzVHjYi9yiFhJ1v/LB5EYO2QS5B lHUZtwYHwz7HK+l/at3UOCA6VIbJCkt13ibVWbb2orf/Dfds9096vZsEwhQARYc5 AOFzBw== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3yujag07au-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 19 Jun 2024 00:05:18 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA05.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 45J05HIE006596 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 19 Jun 2024 00:05:17 GMT Received: from hu-eberman-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Tue, 18 Jun 2024 17:05:17 -0700 From: Elliot Berman Date: Tue, 18 Jun 2024 17:05:09 -0700 Subject: [PATCH RFC 3/5] mm/gup: Add support for re-pinning a normal pinned page as exclusive MIME-Version: 1.0 Message-ID: <20240618-exclusive-gup-v1-3-30472a19c5d1@quicinc.com> References: <20240618-exclusive-gup-v1-0-30472a19c5d1@quicinc.com> In-Reply-To: <20240618-exclusive-gup-v1-0-30472a19c5d1@quicinc.com> To: Andrew Morton , Shuah Khan , David Hildenbrand , Matthew Wilcox , CC: , , , , , , Elliot Berman , Fuad Tabba X-Mailer: b4 0.13.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: RvxghgNS65Z373X2P6YCrPAm_VgGE1I5 X-Proofpoint-ORIG-GUID: RvxghgNS65Z373X2P6YCrPAm_VgGE1I5 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-18_06,2024-06-17_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 phishscore=0 mlxscore=0 adultscore=0 mlxlogscore=999 impostorscore=0 spamscore=0 bulkscore=0 malwarescore=0 clxscore=1015 suspectscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2406180176 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 1E37EA0003 X-Stat-Signature: yicz4ocaagjyk8sfk3ne4iw4sdrms88t X-Rspam-User: X-HE-Tag: 1718755528-833453 X-HE-Meta: U2FsdGVkX1+u7Yog639e7atbb5hu35cB3CxmI+RD4ZCShF/eOzmJKweTpVFgHvcsBhn8zOqGfnTWgLvghTbc69i8hBRA4a3RhvNKjXXMip8pjLCzQ29CmtVUMfZTGX2Yr2IRx6DK5tk4fN/+xmBAVjYiPEm4e4aGTQPiFeoTggHX/NNKjkQhml6KO1f5dMIX7TpmHyoIFik1zplGcP+ISaibAHjzHUP3GMvohiAz5WmCx73ghgKQ2u7gUJ+Xxje78yciOTVxkbkX9bRRd5G5xUHHKBxccfCD7FMTaIPIHhUSs++FnzF/OWavS7g0U0RCWfv1+6VYGNbujRk5UycVygtTcvdzexaYmUx7QwIWIe68E//6UGUM2MloXhW7zqe5O57h8Iey4XZs62orS5PywRKklbXJPiKEI+Zn19WhJB+JZTZgGVPCmsWemVyRaX167JuqOPZZNFt54jq4QMSI883i/40hJEWGnFgrFTFDNNZzEfVHnSHNCpvgvDYZXFj4dOoPt6JNCkzF57hyFuJigGsJBGaExUl/yTWL+wPqO7jPgBlnmCdZ2h4QEhd3cdJSXarsy3g2aL5AtHsGlaIv9RtESkw6BLBO90+k4jTqYmLqAniaMroG9BEo6Jk4Kj1af99wKoe/Uoz2rouW/r/AAkJ4dIL6DZmurEgqIWR6XVRUM1AFI9fIvf5tJZ6tjpRBTgp77YaX2Z9jaqPz1U2PGIIjS28+rc9p+rPXvUVlGJqMsX/ihn4GkggKwN6Ov8TI9Hbbe+0/RUZ/Z50FlZavqMAPg3aefyUiu/Eat7dgsSVS28aXnghnryo9/vqwfKs3+YG8iMUxxNNRm+JmjxImg8+X7sQZJSnO3l1r4fiig27QpG9hy4DSpSUpW4PDSZ+BTQyiIj5oRS3DnZFS40L/RcN9RGz1Vmrhrdp792CsWT9jcEirnyWnNfqihwgy8kvtaBS03E3kYIAQwLh6e2V WFIbFENH VkQUUrLlyfSLS0lHR4/uFp2vHBWZ24zfmtLnbTU2KMjuGwfASEVsYoT52Ktk5ehgBbYHcluIM3dZSPXDw56/V6StfWS7f2pGVdxMQdbLSAlAoppaLq6843McPkWYntKn0UD/mHL5YPxitROUmhfmzV8ZrwaYze3jz/K/2fr1HP6QO+R8nW6sVi0JB2tNnIygnbBTgiRLz2gG7uGo58bebf5+F2LlSNzKTklCdgEgb7V2j9l6HMp6G4yNKEso8TNY0MD6kgXl2ii09vOYLQlrbH4gBLt3WMuqs9yR1joEI4JnZor+Zea5ZOUYAEPlsc7FrHnwrDL2cF4LsZtQ= 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: From: Fuad Tabba When a page is shared, the exclusive pin is dropped, but one normal pin is maintained. In order to be able to unshare a page, add the ability to reaquire the exclusive pin, but only if there is only one normal pin on the page, and only if the page is marked as AnonExclusive. Co-Developed-by: Elliot Berman Signed-off-by: Elliot Berman Signed-off-by: Fuad Tabba Signed-off-by: Elliot Berman --- include/linux/mm.h | 1 + include/linux/page_ref.h | 18 ++++++++++++------ mm/gup.c | 48 +++++++++++++++++++++++++++++++++++++----------- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index d03d62bceba0..628ab936dd2b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1590,6 +1590,7 @@ void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages, void unpin_user_pages(struct page **pages, unsigned long npages); void unpin_exc_pages(struct page **pages, unsigned long npages); void unexc_user_page(struct page *page); +int reexc_user_page(struct page *page); static inline bool is_cow_mapping(vm_flags_t flags) { diff --git a/include/linux/page_ref.h b/include/linux/page_ref.h index 9d16e1f4db09..e66130fe995d 100644 --- a/include/linux/page_ref.h +++ b/include/linux/page_ref.h @@ -92,7 +92,8 @@ static inline void __page_ref_unfreeze(struct page *page, int v) * provides safe operation for get_user_pages(), page_mkclean() and * other calls that race to set up page table entries. */ -#define GUP_PIN_COUNTING_BIAS (1U << 10) +#define GUP_PIN_COUNTING_SHIFT (10) +#define GUP_PIN_COUNTING_BIAS (1U << GUP_PIN_COUNTING_SHIFT) /* * GUP_PIN_EXCLUSIVE_BIAS is used to grab an exclusive pin over a page. @@ -100,7 +101,8 @@ static inline void __page_ref_unfreeze(struct page *page, int v) * exist for the page. * After it's taken, no other gup pins can be taken. */ -#define GUP_PIN_EXCLUSIVE_BIAS (1U << 30) +#define GUP_PIN_EXCLUSIVE_SHIFT (30) +#define GUP_PIN_EXCLUSIVE_BIAS (1U << GUP_PIN_EXCLUSIVE_SHIFT) static inline int page_ref_count(const struct page *page) { @@ -155,7 +157,9 @@ static inline void init_page_count(struct page *page) set_page_count(page, 1); } -static __must_check inline bool page_ref_setexc(struct page *page, unsigned int refs) +static __must_check inline bool page_ref_setexc(struct page *page, + unsigned int expected_pins, + unsigned int refs) { unsigned int old_count, new_count; @@ -165,7 +169,7 @@ static __must_check inline bool page_ref_setexc(struct page *page, unsigned int do { old_count = atomic_read(&page->_refcount); - if (old_count >= GUP_PIN_COUNTING_BIAS) + if ((old_count >> GUP_PIN_COUNTING_SHIFT) != expected_pins) return false; if (check_add_overflow(old_count, refs + GUP_PIN_EXCLUSIVE_BIAS, &new_count)) @@ -178,9 +182,11 @@ static __must_check inline bool page_ref_setexc(struct page *page, unsigned int return true; } -static __must_check inline bool folio_ref_setexc(struct folio *folio, unsigned int refs) +static __must_check inline bool folio_ref_setexc(struct folio *folio, + unsigned int expected_pins, + unsigned int refs) { - return page_ref_setexc(&folio->page, refs); + return page_ref_setexc(&folio->page, expected_pins, refs); } static inline void page_ref_add(struct page *page, int nr) diff --git a/mm/gup.c b/mm/gup.c index 7f20de33221d..663030d03d95 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -97,7 +97,9 @@ static inline struct folio *try_get_folio(struct page *page, int refs) return folio; } -static bool large_folio_pin_setexc(struct folio *folio, unsigned int pins) +static bool large_folio_pin_setexc(struct folio *folio, + unsigned int expected_pins, + unsigned int pins) { unsigned int old_pincount, new_pincount; @@ -107,7 +109,7 @@ static bool large_folio_pin_setexc(struct folio *folio, unsigned int pins) do { old_pincount = atomic_read(&folio->_pincount); - if (old_pincount > 0) + if (old_pincount != expected_pins) return false; if (check_add_overflow(old_pincount, pins + GUP_PIN_EXCLUSIVE_BIAS, &new_pincount)) @@ -117,15 +119,18 @@ static bool large_folio_pin_setexc(struct folio *folio, unsigned int pins) return true; } -static bool __try_grab_folio_excl(struct folio *folio, int pincount, int refcount) +static bool __try_grab_folio_excl(struct folio *folio, + unsigned int expected_pins, + int pincount, + int refcount) { if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_EXCLUSIVE_PIN))) return false; if (folio_test_large(folio)) { - if (!large_folio_pin_setexc(folio, pincount)) + if (!large_folio_pin_setexc(folio, expected_pins, pincount)) return false; - } else if (!folio_ref_setexc(folio, refcount)) { + } else if (!folio_ref_setexc(folio, expected_pins, refcount)) { return false; } @@ -135,7 +140,9 @@ static bool __try_grab_folio_excl(struct folio *folio, int pincount, int refcoun return true; } -static bool try_grab_folio_excl(struct folio *folio, int refs) +static bool try_grab_folio_excl(struct folio *folio, + unsigned int expected_pins, + int refs) { /* * When pinning a large folio, use an exact count to track it. @@ -145,15 +152,17 @@ static bool try_grab_folio_excl(struct folio *folio, int refs) * is pinned. That's why the refcount from the earlier * try_get_folio() is left intact. */ - return __try_grab_folio_excl(folio, refs, + return __try_grab_folio_excl(folio, expected_pins, refs, refs * (GUP_PIN_COUNTING_BIAS - 1)); } -static bool try_grab_page_excl(struct page *page) +static bool try_grab_page_excl(struct page *page, + unsigned int expected_pins) { struct folio *folio = page_folio(page); - return __try_grab_folio_excl(folio, 1, GUP_PIN_COUNTING_BIAS); + return __try_grab_folio_excl(folio, expected_pins, 1, + GUP_PIN_COUNTING_BIAS); } /** @@ -227,7 +236,7 @@ struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags) } if (unlikely(flags & FOLL_EXCLUSIVE)) { - if (!try_grab_folio_excl(folio, refs)) + if (!try_grab_folio_excl(folio, 0, refs)) return NULL; } else { /* @@ -347,7 +356,7 @@ int __must_check try_grab_page(struct page *page, unsigned int flags) return -EBUSY; if (unlikely(flags & FOLL_EXCLUSIVE)) { - if (!try_grab_page_excl(page)) + if (!try_grab_page_excl(page, 0)) return -EBUSY; } else { /* @@ -661,6 +670,23 @@ void unexc_user_page(struct page *page) } EXPORT_SYMBOL(unexc_user_page); +int reexc_user_page(struct page *page) +{ + if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_EXCLUSIVE_PIN))) + return -EINVAL; + + sanity_check_pinned_pages(&page, 1); + + if (!PageAnonExclusive(page)) + return -EINVAL; + + if (!try_grab_page_excl(page, 1)) + return -EBUSY; + + return 0; +} +EXPORT_SYMBOL(reexc_user_page); + /* * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's * lifecycle. Avoid setting the bit unless necessary, or it might cause write From patchwork Wed Jun 19 00:05:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13703196 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 C029AC27C4F for ; Wed, 19 Jun 2024 00:05:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0BDD36B02B1; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 06DA06B02B2; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E50566B02B3; Tue, 18 Jun 2024 20:05:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id C4ABB6B02B1 for ; Tue, 18 Jun 2024 20:05:30 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 61C4D40B8B for ; Wed, 19 Jun 2024 00:05:30 +0000 (UTC) X-FDA: 82245694020.12.4CCE91C Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by imf07.hostedemail.com (Postfix) with ESMTP id 2188D40010 for ; Wed, 19 Jun 2024 00:05:27 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b="Ua/TTQQY"; spf=pass (imf07.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718755524; a=rsa-sha256; cv=none; b=mvVno/Lu0IRAXc3qyNQvGcJ+V2nok3n5CRv+UiGC6cBrpxHHFxSC3Y7PEQqkZhPQ8sOHJ1 h28bXVjk00sNaJCuLH8V9/fmd3vt6M+sTESPD5T5bGFm9f/fMu2aqlK15IJRh6pTQU+CR6 bX4jsk7egmFU5dlgpyvZAevk6mBPDbQ= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b="Ua/TTQQY"; spf=pass (imf07.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718755524; 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:in-reply-to:references:references:dkim-signature; bh=fbO+Vx6FL2r30CUuolgRIrjrd/S25P9zWMEY/WMDOJw=; b=OZ42TFPvukqYgwy97TDqiR4eDykXmQGT0PPc5BT5qVZqeAhZ1oiEQ3VIQxs3fObUGUmRaS HyYQN/dZh4XROiNROZOna3DZ4bchIG42TbHw7AI3tu+ygJjR43wd6np/xj9vw956PTvyEf pW/501E6rrnTiF7/jpOFuldjIMTy8a8= Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45IMFwkH016036; Wed, 19 Jun 2024 00:05:19 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= fbO+Vx6FL2r30CUuolgRIrjrd/S25P9zWMEY/WMDOJw=; b=Ua/TTQQYT8glFHXW LchHycEnI/XJlu9OkuSKKkzIokPq+WYkDbm9QUpGy0LILerA0qctjtvnnsXBIG2K xS5vVGKhdEeLJVuU5ZjxobXgt6siWIs2lxCz8B2vD0HhXc1sZ3AhxaH5TxnBydem gKbCo5eOK3cx2W3bF/heGEI/tTuxELAxpjI3qAfX13yZZNW2DGDVxXOMCXWzYcYN 6jgN2CBe8mpIa1D6DVGWxlbUFFrdLUQltstDGWbuutlONHp11wj/29GjJtA6PjMK sdVzcMoJGKsdMO+apXi4QUGIDX9SqGqdQ2NTO5XvHvwvHO24ZveFByS61VBEdxxm LwXswg== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3yujah877q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 19 Jun 2024 00:05:19 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA05.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 45J05H2c006643 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 19 Jun 2024 00:05:17 GMT Received: from hu-eberman-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Tue, 18 Jun 2024 17:05:17 -0700 From: Elliot Berman Date: Tue, 18 Jun 2024 17:05:10 -0700 Subject: [PATCH RFC 4/5] mm/gup-test: Verify exclusive pinned MIME-Version: 1.0 Message-ID: <20240618-exclusive-gup-v1-4-30472a19c5d1@quicinc.com> References: <20240618-exclusive-gup-v1-0-30472a19c5d1@quicinc.com> In-Reply-To: <20240618-exclusive-gup-v1-0-30472a19c5d1@quicinc.com> To: Andrew Morton , Shuah Khan , David Hildenbrand , Matthew Wilcox , CC: , , , , , , Elliot Berman X-Mailer: b4 0.13.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: Ep5_bKbxZPmtupXQY1F98ZZxOd7BDf84 X-Proofpoint-GUID: Ep5_bKbxZPmtupXQY1F98ZZxOd7BDf84 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-18_06,2024-06-17_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 mlxlogscore=812 clxscore=1015 spamscore=0 lowpriorityscore=0 mlxscore=0 priorityscore=1501 phishscore=0 adultscore=0 suspectscore=0 impostorscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2406180176 X-Stat-Signature: b969s5684fntywob8hknizct6sgijebz X-Rspamd-Queue-Id: 2188D40010 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1718755527-39355 X-HE-Meta: U2FsdGVkX194RGSoQ1wlpcUo4B7we/x0grk1KYqNLgDSgVVNBXPMgn9dtWuZFfUahxGeBUduEvZqHpd9jwZwdJB7w9rIJ/r9hvAnE2cOpmypmLGRYTu/BNpwHJObEJ2RUxSDT5vkGht5NTgxKYMVLKEe/Ot4RHLTDE9AIx/aCqFpztSVcVTga85KQwCPHS2ormkCaODHReaHktm53pWSBtOno/2dHURacTQ1XXuxy2199Vdg571dEz9uhMjOH5ymnC1Xo5OEiMh1ksaB/yMcvCq7aiCMz7HUM0Rwjzwac4tF1rp8DwvXgmyzU72RoJiTpQDw35hNSTItNLNhF16nMW9n9RDNw37bohhMnaVmLiaDMS1XCoe6q4J9lsh+DJLHfWYnIxh3DZbpFQ7UPcwA2hFUQQRbhoahT0J4+NgsCl/k2cvAr/Tmh3i3jPoj49B84KWR8OeE62+iAoO519xf87QRNhoFpihAqBmYmdVjaI8PkXCJ047FBO3/xuJTQj8mQghDyIxFX42Wzragktm+Jn0hAfze/+8ffQiDLOMRej1noDIOS3KvZo2cQEEhfFmxFoa8l0dDiGapkraoWyU2P35UQMZq3yM2hjArCOudXLuhUV7qNI2IOCO7vjQ2/aoWY5aEBQlO//3wFV+wQP/dMTj5a2sWUZtw3tCn6B9/LyJSp93mPb/wz/Kdw7FBgfdPpxTHXELiq0q/KJ5SVfvT0OKsmwf+RR2DOjaHxH3TEkL7nSRg+J9Ssw5Rr9owWMa/ymKZ12YXsLqOCAYKUCB9tMHfcDeethivTcRRWjSiUYgJS622q+Ls/gOaGT+eJfOEOiUro/Ihw3BZ9ov+EGbdjcWvmYxvyzFufx8S2O9CpJ0ad9cJ/oz4mSeADL7xMZkO0FMxhmoN1XPV/42G2Q1o9jNXol1qjuUoufe2sKsNs+KpkheWskEpaGZrh1lGH3TF7ANPTrJM3LbFdcZ8nQO nkXr9lsn U7KCPVIPb0tt2Qy6HjzKVNlfHuQgbALxArPIdrnh+XwpDYtCDM+uKY228JPxnjv31XSQ6rzwCrc6bJ9GbqtjGo8oHsdfHpSSnDZRzbjaR+DoVFaJwK4Lsn8J88PGEF9PVS0Ft6mHQ0cmdUtBeSYGdhTtoHyIgo1YHsr5+eeR3GrsbLP6c7Md98dz1lHwwAOp+i9Eq1J5IhmAZAU+agDU/dsIZimeO1Hf5fsWA1hxlzTKLw8c6en6A39C98BojpWsV/pPifxSCn1XAzV7GnC+f1L/tdrAA8UhUOgzrrE5RWInVcnEWIJaq65VhT3fJ9+HrNpvoz3Z+PbfCm70UCOsK9ZINkA== 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: Add test that pages have the exclusive pin bias when providing FOLL_EXCLUSIVE. Signed-off-by: Elliot Berman --- mm/gup_test.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mm/gup_test.c b/mm/gup_test.c index eeb3f4d87c510..9c6b8c93e44a7 100644 --- a/mm/gup_test.c +++ b/mm/gup_test.c @@ -66,6 +66,26 @@ static void verify_dma_pinned(unsigned int cmd, struct page **pages, } } +static void verify_exclusive_pinned(unsigned int gup_flags, struct page **pages, + unsigned long nr_pages) +{ + unsigned long i; + const struct folio *folio; + + if (!(gup_flags & FOLL_EXCLUSIVE)) + return; + + for (i = 0; i < nr_pages; i++) { + folio = page_folio(pages[i]); + + if (WARN(!folio_maybe_exclusive_pinned(folio), + "pages[%lu] is not exclusive pinned\n", i)) { + dump_page(&folio->page, "gup_test failure"); + break; + } + } +} + static void dump_pages_test(struct gup_test *gup, struct page **pages, unsigned long nr_pages) { @@ -185,6 +205,8 @@ static int __gup_test_ioctl(unsigned int cmd, */ verify_dma_pinned(cmd, pages, nr_pages); + verify_exclusive_pinned(gup->gup_flags, pages, nr_pages); + if (cmd == DUMP_USER_PAGES_TEST) dump_pages_test(gup, pages, nr_pages); From patchwork Wed Jun 19 00:05:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13703197 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 B7DBBC2BA1A for ; Wed, 19 Jun 2024 00:05:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94CFE6B02B5; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F8D86B02BC; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 724416B02B8; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 552BE6B02B2 for ; Tue, 18 Jun 2024 20:05:31 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 13CB6120BC8 for ; Wed, 19 Jun 2024 00:05:31 +0000 (UTC) X-FDA: 82245694062.01.9320E3A Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by imf26.hostedemail.com (Postfix) with ESMTP id DAB18140007 for ; Wed, 19 Jun 2024 00:05:28 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=o2j3NCQE; spf=pass (imf26.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718755519; 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:in-reply-to:references:references:dkim-signature; bh=iMkszGdm9ae/n7pRpO/rrT8MarQ3LgEe54I6kubPvLI=; b=zAwfjgaTuJpNLimm2qs3dpMm/XhsdaNdYRdrOvcrO8HlGoEFzYy/8Lhv9J2lpoUo1dLDQ+ g0czIDKvhAg5o6TRrN57b1XW9DK+ask9MfsMECKXvd79W7HUt89dn7/vjW9lRtrUb4ilGS K6Jbd3JEGAr/5qk6wuSFzzKYJLjrtqs= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=o2j3NCQE; spf=pass (imf26.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718755519; a=rsa-sha256; cv=none; b=M2aOt7zoodVlNKjuJqmLHbsZS5hTR8MZ0C2Sq3YB7D9TUFv0LRN9stDnWIE/1fxF11S+HF qfsooFJ21H9Vwl+d8/z1ttNOR5gFbz3R2w83CuqQo8VC21wjh8irOLSItqa5GVmBWX7D4J E7utbhey0QJmXVYq/rSt8LdZuY6l8qM= Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45ILb6Xo000504; Wed, 19 Jun 2024 00:05:19 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= iMkszGdm9ae/n7pRpO/rrT8MarQ3LgEe54I6kubPvLI=; b=o2j3NCQEJTFU2hZA IcfJBRdS+w2hLNTSnVEQqnCk6AXpZRPDzzyjWU3587KuuQzZLw5I/0tOLUNVu6UP Rs6j0xFWBjkFJmhUD+PeP0g9pvohDo5Y8d2hN4+BOXp6oLp/9rFH5UWggdClLz25 rRhoaL7pj6CoQXuGDGoeGiDlQlUrTPKq5n4fqNiF4XWg7fYW2lZIs0xabEKlei/F fubw4xm7gCPQsE4bGTlJ+UpHrkc22hnsqi5ae6AcKEp8m+Dmg7hgq2ReUMYGqtoV N2eCzPir57qdth9XiRT9nDGlGmmenoIIk2FhV/utsVba60mRmieP4CfYuSeG0BnC WXrfVg== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3yujag07ba-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 19 Jun 2024 00:05:19 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA03.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 45J05IcW029972 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 19 Jun 2024 00:05:18 GMT Received: from hu-eberman-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Tue, 18 Jun 2024 17:05:18 -0700 From: Elliot Berman Date: Tue, 18 Jun 2024 17:05:11 -0700 Subject: [PATCH RFC 5/5] mm/gup_test: Verify GUP grabs same pages twice MIME-Version: 1.0 Message-ID: <20240618-exclusive-gup-v1-5-30472a19c5d1@quicinc.com> References: <20240618-exclusive-gup-v1-0-30472a19c5d1@quicinc.com> In-Reply-To: <20240618-exclusive-gup-v1-0-30472a19c5d1@quicinc.com> To: Andrew Morton , Shuah Khan , David Hildenbrand , Matthew Wilcox , CC: , , , , , , Elliot Berman X-Mailer: b4 0.13.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: unhDqPMZarSEVc31r4x6VcJKNGdRtYup X-Proofpoint-ORIG-GUID: unhDqPMZarSEVc31r4x6VcJKNGdRtYup X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-18_06,2024-06-17_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 mlxscore=0 clxscore=1015 impostorscore=0 suspectscore=0 adultscore=0 spamscore=0 malwarescore=0 phishscore=0 mlxlogscore=880 bulkscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2406180176 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: DAB18140007 X-Stat-Signature: bsrsxer9ddwpuwd38f6xoteqkfsb6yqg X-Rspam-User: X-HE-Tag: 1718755528-376017 X-HE-Meta: U2FsdGVkX19RiLdn9nSSsfVqIS9FEPPgcPO8igNkqxftfmXYrRMzWeAq96nRHNuZd7mcJ0lJcKz1kjeRiqYX8HBJeTYpH2saLODTks62HQEzopgmHHODo+3J4VmKTmKSkIg/o6ciJPVCFJFfvcl8IPBzPTJX5lcMuKHHJuWtG8Hy8P0UuZW67dG9WN9r013AZLfTq0Oggs22iUKqZGzoku6VdeyonKUcyKaK37WBI+1TK4NEdzUQHtVtaA0JcYbYN24teVDrkZ77VOWxh4oc1X+whyGPfI+kPH0b/2Vrat37LSIqDNSlzj5L4kzJkcZmtqKW2VjRlcd92WmeAAMKa2qX5cdbB7Pt9KhbxmswD/vAcbSCRhPSu9LKwZXT/eqKbBdYR6h0yvcT6u6NRgPmYJqrj33ShngJjyg2SGzzTzp/cUoQr5GXDZjsS2hu4ExdzCTX60v0v4xZnaxTu5KJYiD3F6RNDVqn7j2gtM1V3i5L4k+7IOx8XTQK1slpEa3d+ttt6pxIAk0MXKUg7+czOeiezDedym0yWw/X5o2llm5R7UIrCPxAYu3BeTg4tuyIW/NblTaSuHt8jOq+vEuw4dDzMgb6ZRGt9uj9HMvh3d/DNOSyyWPxVjB4ybf1Qy10i6afUAmQSCZf5nmthTa4z13AkPz/729o43H3QbG8+8/zSTu9rFqopgLntCai15tMFzc9dyEuTR2zo1afWgKzWe0oqiaY4hnyAIA/x48oqLn9BurzWGyXdmW7Pr/dUhbwn20Ss0OqoFFJoAtLohbLuXfmFVPikPgB524EPfqQFqNehEL/2iwPaPnvDwLw4HVlGcyHfUNkhFXZpgjvBSq33M0AfpA/v6zKWbu/ohNacN++y8W2rlEMeO67a6fmzQeFaEbodHdgXQ4ZVcKXB1F+eXXbxWL+6itrHhj8VR4NUzr058gg59r3BQKbl28wsztHAqbk/yIfI+WHuKwa4JL lYs+W/1T uoED1xJCaxSw/Qf9169YtsR9y4M0+uDdyRgXVCbq2vMFCTREDmjKhqM2UeHRjPjpmUzdCyNgp5QN2HxME45WBnXVL5NNwMdWCIZjTR4pniPtD7ffzJx/WAjtCJsbEMlWuoss8YZnGZofX4oV9EoeFkae2xAoTJ9nscesnKVYpvoL2xwC+kk/KQ1IBsL62Au24XLyj81BPRO4UEWHRhXgbEun8GB7dk21F6WKqxQCVw8wgBJsVC8eXYZRLwJSZgt3mRKEliylZYo/zVKKTTWNRKfF1oqF+mgTz7PRRDi6aIbnL+9YWn2mytcDu3JT20fcdg6u5e19bdLY8Evtta3l2MJdsdgOf96vDALB6K7akFYWiT59T8AlWwcwyhBIRma2CytRF 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: GUP'ing pages should get the same pages, test it. In case of FOLL_EXCLUSIVE, the second pin should fail to get any pages. Note: this change ought to be refactored to pull out the GUP'ing bits that's duplicated between the original and the second GUP. Signed-off-by: Elliot Berman --- mm/gup_test.c | 86 +++++++++++++++++++++++++++++++++++ mm/gup_test.h | 1 + tools/testing/selftests/mm/gup_test.c | 5 +- 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/mm/gup_test.c b/mm/gup_test.c index 9c6b8c93e44a7..28cc422b60b78 100644 --- a/mm/gup_test.c +++ b/mm/gup_test.c @@ -86,6 +86,89 @@ static void verify_exclusive_pinned(unsigned int gup_flags, struct page **pages, } } +static int verify_gup_twice(unsigned int cmd, struct gup_test *gup, + struct page **expected_pages, + unsigned long expected_nr_pages) +{ + unsigned long i, nr_pages, addr, next; + long nr; + struct page **pages __free(kfree) = NULL; + int ret = 0; + + nr_pages = gup->size / PAGE_SIZE; + pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL); + if (!pages) + return -ENOMEM; + + i = 0; + nr = gup->nr_pages_per_call; + for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) { + if (nr != gup->nr_pages_per_call) + break; + + next = addr + nr * PAGE_SIZE; + if (next > gup->addr + gup->size) { + next = gup->addr + gup->size; + nr = (next - addr) / PAGE_SIZE; + } + + switch (cmd) { + case GUP_FAST_BENCHMARK: + nr = get_user_pages_fast(addr, nr, gup->gup_flags, + pages + i); + break; + case GUP_BASIC_TEST: + nr = get_user_pages(addr, nr, gup->gup_flags, pages + i); + break; + case PIN_FAST_BENCHMARK: + nr = pin_user_pages_fast(addr, nr, gup->gup_flags, + pages + i); + break; + case PIN_BASIC_TEST: + nr = pin_user_pages(addr, nr, gup->gup_flags, pages + i); + break; + case PIN_LONGTERM_BENCHMARK: + nr = pin_user_pages(addr, nr, + gup->gup_flags | FOLL_LONGTERM, + pages + i); + break; + default: + pr_err("cmd %d not supported for %s\n", cmd, __func__); + return -EINVAL; + } + + if (nr <= 0) + break; + i += nr; + } + + nr_pages = i; + + if (gup->gup_flags & FOLL_EXCLUSIVE) { + if (WARN(nr_pages, + "Able to acquire exclusive pin twice for %ld of %ld pages", + nr_pages, expected_nr_pages)) { + dump_page(pages[0], + "gup_test: verify_gup_twice() test"); + ret = -EIO; + } + } else if (nr_pages != expected_nr_pages) { + pr_err("%s: Expected %ld pages, got %ld\n", __func__, + expected_nr_pages, nr_pages); + ret = -EIO; + } else { + for (i = 0; i < nr_pages; i++) { + if (WARN(pages[i] != expected_pages[i], + "pages[%lu] mismatch\n", i)) + break; + } + } + + put_back_pages(cmd, pages, nr_pages, gup->test_flags); + + return ret; +} + static void dump_pages_test(struct gup_test *gup, struct page **pages, unsigned long nr_pages) { @@ -210,6 +293,9 @@ static int __gup_test_ioctl(unsigned int cmd, if (cmd == DUMP_USER_PAGES_TEST) dump_pages_test(gup, pages, nr_pages); + if (gup->test_flags & GUP_TEST_FLAG_GUP_TWICE) + ret = verify_gup_twice(cmd, gup, pages, nr_pages); + start_time = ktime_get(); put_back_pages(cmd, pages, nr_pages, gup->test_flags); diff --git a/mm/gup_test.h b/mm/gup_test.h index 5b37b54e8bea6..fcd41919b0159 100644 --- a/mm/gup_test.h +++ b/mm/gup_test.h @@ -17,6 +17,7 @@ #define GUP_TEST_MAX_PAGES_TO_DUMP 8 #define GUP_TEST_FLAG_DUMP_PAGES_USE_PIN 0x1 +#define GUP_TEST_FLAG_GUP_TWICE 0x2 struct gup_test { __u64 get_delta_usec; diff --git a/tools/testing/selftests/mm/gup_test.c b/tools/testing/selftests/mm/gup_test.c index bdeaac67ff9aa..b4b10c8338f80 100644 --- a/tools/testing/selftests/mm/gup_test.c +++ b/tools/testing/selftests/mm/gup_test.c @@ -98,7 +98,7 @@ int main(int argc, char **argv) pthread_t *tid; char *p; - while ((opt = getopt(argc, argv, "m:r:n:F:f:abcj:tTLUuwWSHpz")) != -1) { + while ((opt = getopt(argc, argv, "m:r:n:F:f:abcj:dtTLUuwWSHpz")) != -1) { switch (opt) { case 'a': cmd = PIN_FAST_BENCHMARK; @@ -172,6 +172,9 @@ int main(int argc, char **argv) /* fault pages in gup, do not fault in userland */ touch = 1; break; + case 'd': + gup.test_flags |= GUP_TEST_FLAG_GUP_TWICE; + break; default: ksft_exit_fail_msg("Wrong argument\n"); }