From patchwork Sat Jun 4 00:40:00 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: 12869468 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 5AF0DC43334 for ; Sat, 4 Jun 2022 00:40:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE29B8D000E; Fri, 3 Jun 2022 20:40:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D6A9C8D000C; Fri, 3 Jun 2022 20:40:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C35708D000E; Fri, 3 Jun 2022 20:40:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id AE07A8D000C for ; Fri, 3 Jun 2022 20:40:33 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8E0CF35141 for ; Sat, 4 Jun 2022 00:40:33 +0000 (UTC) X-FDA: 79538697546.04.3DBB290 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf01.hostedemail.com (Postfix) with ESMTP id 9F15840005 for ; Sat, 4 Jun 2022 00:40:24 +0000 (UTC) Received: by mail-pj1-f73.google.com with SMTP id mh12-20020a17090b4acc00b001e32eb45751so7999284pjb.9 for ; Fri, 03 Jun 2022 17:40:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=IEIcsbesdwgjyLSnjhy6rJjaQh02HrF984U0ropJC2o=; b=LYN/ksS+4YaGhN39iKQ36tutvy8UlGcAXYH/lMRBLudOX79Y7melvYm0cwj4wLEYGR vRB3TOLIjywBB14bfHdTT+f88T/hSY71wmLK1xdWi+QK2CpBgHK+yNJ3iHILvdsHLxjv YiyCJNvhIZWWeaEsdN8QN3rmbcDDRKDSkwSMXIOOyeSQrnZ1xZmT14pM7OVRgONOn7Sm bWP8JTlmLG8ffn5QXh2pqheTFlKDDK1v+QEcZsLN8NZ8Tj+nBJXsLomqN6rWswsnZfB1 Yf1b+QOqY7hvlUe/LAMjniyqljrbgH7m+vT2MULnVFAXGZLIV6mWrjLI+OhTff0tg/mz eqWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=IEIcsbesdwgjyLSnjhy6rJjaQh02HrF984U0ropJC2o=; b=6lGOBxz8Nm2TnjngHEZJ58ELQRzYPzUKILGCI1U/TQQCh6jOBA6YBSogVKKb9hJpxN DqPh9bF/RLyvqAyqAoP8alNMBXHBEvdmvf54tSGbY1b0mPPwD6p24QShKTU+LD5oEkps i64sznvwOLWVK/Zf1/QCN6O3tFAs5sw7IhtlTegtUCRGRb4pzIhLRPkFjY2VKheVTyKj p5GwNvexVKBv+4PjS/EUTzVk5JUOxVVNnV6ayzFeFZKKIhJr0jUtpNND4d1FrsJfAZ9o ssCRQuW97HjqkmmkWpTZszmcuT7r8bCHJJe9gDrPyiV4w3AfV5ey98LX3mzZv+nr2fJT HJ6Q== X-Gm-Message-State: AOAM531/vWu4Uv0tS+dV26JWZih9vciv9hid3NwfKBARRUMWFSeYYAlJ HEGpS8FvgwiPPNFCuiSEi97VGWPjBKpm X-Google-Smtp-Source: ABdhPJxYaAFfAc4d82J0Wwd0FnaP+CDLLMowH6hKLwkrA7fjGudj2bsg/b++dxaiApAADpMHGpD4UIVZQEhT X-Received: from zokeefe3.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1b6]) (user=zokeefe job=sendgmr) by 2002:a17:902:cec2:b0:163:e44a:c678 with SMTP id d2-20020a170902cec200b00163e44ac678mr12381407plg.137.1654303232178; Fri, 03 Jun 2022 17:40:32 -0700 (PDT) Date: Fri, 3 Jun 2022 17:40:00 -0700 In-Reply-To: <20220604004004.954674-1-zokeefe@google.com> Message-Id: <20220604004004.954674-12-zokeefe@google.com> Mime-Version: 1.0 References: <20220604004004.954674-1-zokeefe@google.com> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH v6 11/15] mm/madvise: add MADV_COLLAPSE to process_madvise() From: "Zach O'Keefe" To: Alex Shi , David Hildenbrand , David Rientjes , Matthew Wilcox , Michal Hocko , Pasha Tatashin , Peter Xu , Rongwei Wang , SeongJae Park , Song Liu , Vlastimil Babka , Yang Shi , Zi Yan , linux-mm@kvack.org Cc: Andrea Arcangeli , Andrew Morton , Arnd Bergmann , Axel Rasmussen , Chris Kennelly , Chris Zankel , Helge Deller , Hugh Dickins , Ivan Kokshaysky , "James E.J. Bottomley" , Jens Axboe , "Kirill A. Shutemov" , Matt Turner , Max Filippov , Miaohe Lin , Minchan Kim , Patrick Xia , Pavel Begunkov , Thomas Bogendoerfer , "Zach O'Keefe" X-Stat-Signature: hoogeaj9aztehzxbao9xbymhwfa1x8bu X-Rspam-User: Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="LYN/ksS+"; spf=pass (imf01.hostedemail.com: domain of 3AKqaYgcKCNkUJF99A9BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--zokeefe.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3AKqaYgcKCNkUJF99A9BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--zokeefe.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9F15840005 X-HE-Tag: 1654303224-286291 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: Allow MADV_COLLAPSE behavior for process_madvise(2) if caller has CAP_SYS_ADMIN or is requesting collapse of it's own memory. This is useful for the development of userspace agents that seek to optimize THP utilization system-wide by using userspace signals to prioritize what memory is most deserving of being THP-backed. Signed-off-by: Zach O'Keefe --- mm/madvise.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index eccac2620226..b19e2f4b924c 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1171,13 +1171,15 @@ madvise_behavior_valid(int behavior) } static bool -process_madvise_behavior_valid(int behavior) +process_madvise_behavior_valid(int behavior, struct task_struct *task) { switch (behavior) { case MADV_COLD: case MADV_PAGEOUT: case MADV_WILLNEED: return true; + case MADV_COLLAPSE: + return task == current || capable(CAP_SYS_ADMIN); default: return false; } @@ -1455,7 +1457,7 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec, goto free_iov; } - if (!process_madvise_behavior_valid(behavior)) { + if (!process_madvise_behavior_valid(behavior, task)) { ret = -EINVAL; goto release_task; }