From patchwork Thu Sep 22 18:46:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zach O'Keefe X-Patchwork-Id: 12985713 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 40ED9C54EE9 for ; Thu, 22 Sep 2022 18:48:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BA030940007; Thu, 22 Sep 2022 14:48:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B4FF26B0072; Thu, 22 Sep 2022 14:48:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F04E940007; Thu, 22 Sep 2022 14:48:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8CFB36B0071 for ; Thu, 22 Sep 2022 14:48:14 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5E915A0563 for ; Thu, 22 Sep 2022 18:48:14 +0000 (UTC) X-FDA: 79940606508.23.C86D9A6 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf29.hostedemail.com (Postfix) with ESMTP id 14CFD12000F for ; Thu, 22 Sep 2022 18:48:13 +0000 (UTC) Received: by mail-pf1-f202.google.com with SMTP id y17-20020a056a00181100b0053e8868496bso5896898pfa.21 for ; Thu, 22 Sep 2022 11:48:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date; bh=My2OSBDrSoUjJIoGNL9cK5MktxjMPKLSnc7Ndu4M8C8=; b=pL9/QLPftQLl1VnFDGbBX8xp+1fUbqCRUcEwPP1ZJBKa2cp+ApRbAlg0pl1hWvMQWO viaGOO8p1WNX05P2R+cOdErnmpp2vAVb/FLvqStnedL93biCzfXkZ1mY1LT8EeAgBXuY FzRpNLGGEYFEwh7bIOHZVqqs3ExnsbovqsMFn45IfQDgnvWI//k1wYm7WupZkFgJiVA0 wKXnIKzX1mrB28HCLPu4ySMHpC5TVfbg3bP0mQNYO+nBenUJblE/QsJDRmuWAxsLM1lb zkpAzVR1g150l0lThDwKpFI0Dbh7iy0ska18FrDaAeVOScp8jjtkLwGh8xPZDi+sQ9k2 39wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date; bh=My2OSBDrSoUjJIoGNL9cK5MktxjMPKLSnc7Ndu4M8C8=; b=VkcNmc/X6dCSXaEBtKhhTp6KIqxqAvEhcMOzBjVYzUYZrjp5OUgtS4oJqolC4Rby5t JAC0NthAI9DV5jl5/BOoWI2Agx7ll04Zj0VqkZGxFotXVL/z/ae+XqdLkeeK8DmKrL5C 6UEWPRc2IvxtY7NKz85KsPQkUOYTWjIWHROMDODv+zhWq5ZPYELpC79PIsVLj+lXHNqS yAhtaRlX97Uv+eHVAvqGuaW9xa+Uyef2f49rz93WCgD6hFb2o1Ah9O33JzgFBq5dCCbw +zV/IYYZDCTODYZm/Z9I3/XlH9OdVh4oRcgVzTANnE+fmBFZQlPjEp6Mb61jsfh1BEe5 2ZOA== X-Gm-Message-State: ACrzQf0mvJI8dyTbILfVfO+omiyi8SBdR0O3qY6QTm8cw+uBF1xmqqmx rvKwWvLQCGFumRmPyErjI3Ns8PiGtqDoNaB8i8C2+i7CrmpEd4gpKQVA6hhjPhOuFte9JnL2572 MpKBxcbbUatQzXUVyLJMXnQyJc8CBQDXn6iR8zagMqmlFBx7x8AM9Hzrzddw= X-Google-Smtp-Source: AMsMyM5AmahX9p9E0pDTPtVpWETdLlsDi/O9ghFRepGFBczyb6vBZ2kWJafQHK/JtvGpl6LwweUfGp68px8Z X-Received: from zokeefe3.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1b6]) (user=zokeefe job=sendgmr) by 2002:a05:6a00:1a47:b0:52e:6a8c:5430 with SMTP id h7-20020a056a001a4700b0052e6a8c5430mr5036755pfv.48.1663872492688; Thu, 22 Sep 2022 11:48:12 -0700 (PDT) Date: Thu, 22 Sep 2022 11:46:50 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.37.3.998.g577e59143f-goog Message-ID: <20220922184651.1016461-1-zokeefe@google.com> Subject: [PATCH 1/2] mm/madvise: MADV_COLLAPSE return EAGAIN when page cannot be isolated From: "Zach O'Keefe" To: linux-mm@kvack.org Cc: Andrew Morton , Yang Shi , "Zach O'Keefe" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663872494; a=rsa-sha256; cv=none; b=s8UCBNF8uxJkCR3gUU7O29t+0bLdGxUqgtgiEpuc++JdGOpC3wOXN2+ZnrJ/jxk8HZWf3P 1aevFBDJt0JI+hsou1h5UtEBZEL8g+GkviWr5CRoXtigomB+TUtgrCAYBND1ZvUIZsjnZY C2/NzfTT9J5rV+Fltbzk3Z8ehCQMq9Y= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="pL9/QLPf"; spf=pass (imf29.hostedemail.com: domain of 37K0sYwcKCBsQFB55657FF7C5.3FDC9ELO-DDBM13B.FI7@flex--zokeefe.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=37K0sYwcKCBsQFB55657FF7C5.3FDC9ELO-DDBM13B.FI7@flex--zokeefe.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=1663872494; 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: references:dkim-signature; bh=My2OSBDrSoUjJIoGNL9cK5MktxjMPKLSnc7Ndu4M8C8=; b=wsUaxhrphntOrqINdBgBZJswkYq/yyN1Cn58NqvellUgE3bC4uoICO9/zG03PEzJO8htHO w2h8igfbi1kTFZ+0M3zjLGEpxybVcXAeVBtkhJ7aJiu/3PamUzgKGQ9wwS/0gx59k96saN r4+8cFSq4+BOeGAankIvBHrQ8edhL+I= Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="pL9/QLPf"; spf=pass (imf29.hostedemail.com: domain of 37K0sYwcKCBsQFB55657FF7C5.3FDC9ELO-DDBM13B.FI7@flex--zokeefe.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=37K0sYwcKCBsQFB55657FF7C5.3FDC9ELO-DDBM13B.FI7@flex--zokeefe.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Stat-Signature: zfsu3z4unx3xrgg59wjniikew343unhg X-Rspamd-Queue-Id: 14CFD12000F X-Rspamd-Server: rspam02 X-HE-Tag: 1663872493-240734 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: MADV_COLLAPSE is a best-effort request that attempts to set an actionable errno value if the request cannot be fulfilled at the time. EAGAIN should be used to communicate that a resource was temporarily unavailable, but that the user may try again immediately. SCAN_DEL_PAGE_LRU is an internal result code used when a page cannot be isolated from it's LRU list. Since this, like SCAN_PAGE_LRU, is likely a transitory state, make MADV_COLLAPSE return EAGAIN so that users know they may reattempt the operation. Another important scenario to consider is race with khugepaged. khugepaged might isolate a page while MADV_COLLAPSE is interested in it. Even though racing with khugepaged might mean that the memory has already been collapsed, signalling an errno that is non-intrinsic to that memory or arguments provided to madvise(2) lets the user know that future attempts might (and in this case likely would) succeed, and avoids false-negative assumptions by the user. Fixes: 7d8faaf15545 ("mm/madvise: introduce MADV_COLLAPSE sync hugepage collapse") Signed-off-by: Zach O'Keefe --- mm/khugepaged.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index beaba3952dd1..5dcb2c34651e 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2374,6 +2374,7 @@ static int madvise_collapse_errno(enum scan_result r) /* Resource temporary unavailable - trying again might succeed */ case SCAN_PAGE_LOCK: case SCAN_PAGE_LRU: + case SCAN_DEL_PAGE_LRU: return -EAGAIN; /* * Other: Trying again likely not to succeed / error intrinsic to @@ -2456,6 +2457,7 @@ int madvise_collapse(struct vm_area_struct *vma, struct vm_area_struct **prev, case SCAN_PAGE_LOCK: case SCAN_PAGE_COMPOUND: case SCAN_PAGE_LRU: + case SCAN_DEL_PAGE_LRU: last_fail = result; break; default: From patchwork Thu Sep 22 18:46:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zach O'Keefe X-Patchwork-Id: 12985714 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 E3860ECAAD8 for ; Thu, 22 Sep 2022 18:48:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 784126B0071; Thu, 22 Sep 2022 14:48:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 732EC940008; Thu, 22 Sep 2022 14:48:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 583DE6B0073; Thu, 22 Sep 2022 14:48:16 -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 4C6F86B0071 for ; Thu, 22 Sep 2022 14:48:16 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 0BA511C693F for ; Thu, 22 Sep 2022 18:48:16 +0000 (UTC) X-FDA: 79940606592.23.A416064 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf10.hostedemail.com (Postfix) with ESMTP id BF89DC005C for ; Thu, 22 Sep 2022 18:48:15 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-348c4a1e12dso85592927b3.11 for ; Thu, 22 Sep 2022 11:48:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=yTcRpbsZ/noY/+CgwnbOYUPErza2wu5HU8vTdyW5RaM=; b=cD1jM/a7v0VpDe69xpPEKS/NAcRu5mqmlTrWs/JNPiKzo5/tOAE/5XRjsF3gdgXOFA gUqNfe50vmSJlngUNDbpWk7FzSKswr3aHhyTHyAgQO3KaghiEGNcjhjnQPYyE0xH601J h/b7xb+72FLwjwmErQmti34WmSBmxSqYRxqFlEssjDb8eGnqYvrAkPMDl2ePP176VqJV OSU0tBOXXFfJxLiG5VvDd2on2q2u1Iovrg1Q6znwoQpacexmoU3iFDF79dQB1Fy+0DmF zR7AtrOH9pOcsW74ZBf9GgocogI2+WqCqyeU6BeOGVFi1eG0jwoKGWRQroAp1q4/k3VU fOvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=yTcRpbsZ/noY/+CgwnbOYUPErza2wu5HU8vTdyW5RaM=; b=Jsw6jE+JlIwkdLR7VWLaWdjdOcZflvA4z21ON2zeZrhWqby402zw+kbpTwHpmHkGFp JXqKgE0ILa6xz9hrruFz5EhMh5TllWIl9Zk2YWL3bvVAhry5PXCCtp3Pagz9FTPuvliE Fo8ZrxeG4IySZJW717ByyTgIqAWBmiwkewnOcEjXGeWjirfTVZCS1ndsNyz/v+x2Tkep LU/T80tIZ2wDO4bjVGE1bASCsyOB3VqQeFd3/el94e2avibc5vbLguP1Z929rkT9XXgX /gtBvL0wMUS+lLDikiKBiR7dN0j5924m8NpqalvzYtYkOfc7fOXASIj8DtghmqYTX/p1 1y6A== X-Gm-Message-State: ACrzQf23A1pirXimbOfIM2Cd2okrYvm3a/EGQlFihvcYQq2d6rEU/wAF Bub4h0/iGy6iA5ZArb8XZ1Rzr/SOwUd+QJdWUtE8OcfgTpPajGcrR5MzfDx6pNJ3HuwRSH846UR 6v3E+8SWttQaPBcpES/ydKNqc9hej3HjTqqtMsDis9IZQy3rIdvuZXqL92K0= X-Google-Smtp-Source: AMsMyM4nSpSPWpL3mKo3hp3HxCmWg58bRMlTNSfP2tjiZbJu4r5qzS4bXRXetMQM0dmQ5ElgJpXX1HchTZb0 X-Received: from zokeefe3.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1b6]) (user=zokeefe job=sendgmr) by 2002:a25:4283:0:b0:6b0:1db9:104d with SMTP id p125-20020a254283000000b006b01db9104dmr5385006yba.162.1663872494997; Thu, 22 Sep 2022 11:48:14 -0700 (PDT) Date: Thu, 22 Sep 2022 11:46:51 -0700 In-Reply-To: <20220922184651.1016461-1-zokeefe@google.com> Mime-Version: 1.0 References: <20220922184651.1016461-1-zokeefe@google.com> X-Mailer: git-send-email 2.37.3.998.g577e59143f-goog Message-ID: <20220922184651.1016461-2-zokeefe@google.com> Subject: [PATCH 2/2] selftests/vm: retry on EAGAIN for MADV_COLLAPSE selftest From: "Zach O'Keefe" To: linux-mm@kvack.org Cc: Andrew Morton , Yang Shi , "Zach O'Keefe" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663872495; a=rsa-sha256; cv=none; b=t3ZZi6xWc4cyhwmv+79ZLPe6uRfuJbn7f6GILQ5U5H2zTJf9p+fm7qK4T7v+gVyxjlOkTl Ay9eqmVb0d+0ntyNYlqHULYEtW4lTAa+WEAkFRpuVBUYLAyb92uSkZskUgaURTjUlr0jl/ aKn+12lJmBcAws5xuxf7pL0kQCb8OGU= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="cD1jM/a7"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 37q0sYwcKCB0SHD77879HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--zokeefe.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=37q0sYwcKCB0SHD77879HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--zokeefe.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663872495; 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=yTcRpbsZ/noY/+CgwnbOYUPErza2wu5HU8vTdyW5RaM=; b=f6eY320NqSDXyhywK7G26q/bI9LQ+803vjEFNJK4qJOU5ACSfuu8euLHCA15eNuOzTnUiL pZ6e7/m+vW/3IpEt0aJbV2F0pTae0/fOLTOn1VrEtpXzXXSfla5dqc+zhNKbb5Yd5yeaLe eri+i1wKAd7pnHNQ2qR8BU1e0sSWAoc= X-Stat-Signature: 9mzmbgkojtwnf9tb4mbaqr7e59phsj8o X-Rspamd-Queue-Id: BF89DC005C Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="cD1jM/a7"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 37q0sYwcKCB0SHD77879HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--zokeefe.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=37q0sYwcKCB0SHD77879HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--zokeefe.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1663872495-462035 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: MADV_COLLAPSE is a best-effort request that will set errno to an actionable value if the request cannot be performed. For example, if pages are not found on the LRU, or if they are currently locked by something else, MADV_COLLAPSE will fail and set errno to EAGAIN to inform callers that they may try again. Since the khugepaged selftest is the first public use of MADV_COLLAPSE, set a best practice of checking errno and retrying on EAGAIN. Fixes: 9330694de59f ("selftests/vm: add MADV_COLLAPSE collapse context to selftests") Signed-off-by: Zach O'Keefe --- tools/testing/selftests/vm/khugepaged.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/vm/khugepaged.c b/tools/testing/selftests/vm/khugepaged.c index b77b1e28cdb3..b55dc331af13 100644 --- a/tools/testing/selftests/vm/khugepaged.c +++ b/tools/testing/selftests/vm/khugepaged.c @@ -1,4 +1,5 @@ #define _GNU_SOURCE +#include #include #include #include @@ -477,6 +478,26 @@ static void fill_memory(int *p, unsigned long start, unsigned long end) p[i * page_size / sizeof(*p)] = i + 0xdead0000; } +/* + * MADV_COLLAPSE is a best-effort request and may fail if an internal + * resource is temporarily unavailable, in which case it will set errno to + * EAGAIN. In such a case, immediately reattempt the operation one more + * time. + */ +static int madvise_collapse_retry(void *p, unsigned long size) +{ + bool retry = true; + int ret; + +retry: + ret = madvise(p, size, MADV_COLLAPSE); + if (ret && errno == EAGAIN && retry) { + retry = false; + goto retry; + } + return ret; +} + /* * Returns pmd-mapped hugepage in VMA marked VM_HUGEPAGE, filled with * validate_memory()'able contents. @@ -531,7 +552,7 @@ static void madvise_collapse(const char *msg, char *p, int nr_hpages, /* Clear VM_NOHUGEPAGE */ madvise(p, nr_hpages * hpage_pmd_size, MADV_HUGEPAGE); - ret = madvise(p, nr_hpages * hpage_pmd_size, MADV_COLLAPSE); + ret = madvise_collapse_retry(p, nr_hpages * hpage_pmd_size); if (((bool)ret) == expect) fail("Fail: Bad return value"); else if (check_huge(p, nr_hpages) != expect)