From patchwork Wed Jul 6 23:59:30 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: 12908936 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 92161C43334 for ; Thu, 7 Jul 2022 00:06:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 37EDA8E000C; Wed, 6 Jul 2022 20:06:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 292478E0001; Wed, 6 Jul 2022 20:06:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E5288E000C; Wed, 6 Jul 2022 20:06:27 -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 E858F8E0001 for ; Wed, 6 Jul 2022 20:06:26 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A9F3A2066D for ; Thu, 7 Jul 2022 00:06:26 +0000 (UTC) X-FDA: 79658361972.18.9C1F8D0 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf17.hostedemail.com (Postfix) with ESMTP id 4C97F40029 for ; Thu, 7 Jul 2022 00:06:26 +0000 (UTC) Received: by mail-pf1-f202.google.com with SMTP id h5-20020a62b405000000b00528c76085e4so368453pfn.15 for ; Wed, 06 Jul 2022 17:06:26 -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=akPXti7Jps/1XC2wL/0eQ+7YVYnKnCRrc4HJoC/suJY=; b=qFDcV4vmHToswl+yHgVdKHHZGLyRhipVUmciws/xx7aFgRL4MOQYSk00tU8pJsL0Y2 3A9QJCnvzvXyO8QltTQOeqL/3ZqQdCfVW7CWwn7o/KU+/dyVb3FWKlB0fTyDQAQHMGgr PP1LRCK935TDvYokPeYC06e2Vu3mlHLUFqzxgyvg9q0COoqe9lxp4ZA4oNq17DF5S8bI yVDuj7VnAKhOzxMb2ABuFX3Rw73AQZ0gjP8qc4TtN08/OpbqmGndAi0S2YdShiJ18Tgn ogv0KXASLL63pBTmyA7a3eQQr3dUpHcsbN0qxq4HkQ31y+qK31FDw05/bZdwSdfzxXRt hzUg== 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=akPXti7Jps/1XC2wL/0eQ+7YVYnKnCRrc4HJoC/suJY=; b=prBoMUphOoif2pzL+Y+qLeJ6rXsKs1wuYKgt+ZStXJGQyLG+3iIq43cIGmujpWMCjW JlMRsOyQ7Rs6h4IzbUbOz4V8TxM1X6RPHHoqLWVMaORTQAPnt8GP70+nA7toz4wKClxS lsiB+Zhy12iZ1iXhcju9lrDzQMivN8t+9Zw0zTRDzrNJPCRHkdz1ZgAN3r77W0gX8JOS FRt7BK8P6L3ZrvOO2FimYhMCpEPp84BDnW6Wt2aDWjkCJMI0L/LPvoW7JewLGDbqw+Ei n1beXz59AavlUBUkATQOWce28tkfs6+DNfQa6S8YHETaiFXSJ/dfVpOpmtthpdrPN3jb ZFnw== X-Gm-Message-State: AJIora+cok0hqDJVweh5HDypO9ZfekC7cUezay9ZsgNzMFtdmZayl6Jj mOM9n4eQxRanK9gAQl5HbBhGQJN5SfqJ X-Google-Smtp-Source: AGRyM1uh9/DmUjm74fPzN5piQB57xhrlgvcvR3mOptD5R00k+v56VyxBC5rS+zsEDOza+LJDbO4VzXIxBZrC X-Received: from zokeefe3.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1b6]) (user=zokeefe job=sendgmr) by 2002:a17:902:d142:b0:16b:f014:9e6a with SMTP id t2-20020a170902d14200b0016bf0149e6amr12997063plt.64.1657152385810; Wed, 06 Jul 2022 17:06:25 -0700 (PDT) Date: Wed, 6 Jul 2022 16:59:30 -0700 In-Reply-To: <20220706235936.2197195-1-zokeefe@google.com> Message-Id: <20220706235936.2197195-13-zokeefe@google.com> Mime-Version: 1.0 References: <20220706235936.2197195-1-zokeefe@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [mm-unstable v7 12/18] 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" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657152386; a=rsa-sha256; cv=none; b=jbnbLFUswjpLI+xndRqddwBZ8aG3yG11QdAArrDqfGMYXsxT6dSFPccEqp9SnW2HmPfB/k ZnTjxbWpUhnWxqqQpfX+2ZI0b26xfdfZ77faGfs9qy+RGVBukj2Uu4Lzave4F8fNilfrsd jy/w1SQoOoQQUa8pw528z3ELBmkIyzg= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=qFDcV4vm; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3gSPGYgcKCPw3soiijiksskpi.gsqpmry1-qqozego.svk@flex--zokeefe.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3gSPGYgcKCPw3soiijiksskpi.gsqpmry1-qqozego.svk@flex--zokeefe.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657152386; 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=akPXti7Jps/1XC2wL/0eQ+7YVYnKnCRrc4HJoC/suJY=; b=P6p5zr8NI0KA1yMfR/LwPSWoPD+RYgpCnjsdKckOQohkI8MCE/SjZJ4i2gg4v3ds9ZylAx llMYQ611gKedOLAriG6AN+sIFO46PDS2Bj0pHfHMv65PBd3iOtwwDzCY8UGzEkikL4rbXU qqOjlsdLdxOZfmooCfGFT64Wfyh8xgw= X-Rspamd-Server: rspam04 X-Rspam-User: Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=qFDcV4vm; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3gSPGYgcKCPw3soiijiksskpi.gsqpmry1-qqozego.svk@flex--zokeefe.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3gSPGYgcKCPw3soiijiksskpi.gsqpmry1-qqozego.svk@flex--zokeefe.bounces.google.com X-Stat-Signature: oyrkbyan6ubime4tck75556q9knafybj X-Rspamd-Queue-Id: 4C97F40029 X-HE-Tag: 1657152386-272860 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 Acked-by: David Rientjes --- mm/madvise.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 9f08e958ea86..6fb6b7160bda 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; }