From patchwork Thu Jan 30 02:24:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gavin Shan X-Patchwork-Id: 11357341 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 64C791395 for ; Thu, 30 Jan 2020 02:24:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3161D206F0 for ; Thu, 30 Jan 2020 02:24:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="SbzWTHzD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3161D206F0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4D3736B02CB; Wed, 29 Jan 2020 21:24:55 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 484706B02CC; Wed, 29 Jan 2020 21:24:55 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 399B66B02CD; Wed, 29 Jan 2020 21:24:55 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0196.hostedemail.com [216.40.44.196]) by kanga.kvack.org (Postfix) with ESMTP id 23F126B02CB for ; Wed, 29 Jan 2020 21:24:55 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id B72A3181AEF0B for ; Thu, 30 Jan 2020 02:24:54 +0000 (UTC) X-FDA: 76432707708.18.name24_dbcf7e26e40d X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,gshan@redhat.com,::aarcange@redhat.com:akpm@linux-foundation.org:mhocko@suse.com,RULES_HIT:30012:30054,0,RBL:207.211.31.81:@redhat.com:.lbl8.mailshell.net-62.18.0.100 66.10.201.10,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: name24_dbcf7e26e40d X-Filterd-Recvd-Size: 9238 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Thu, 30 Jan 2020 02:24:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580351093; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hlume1Bkov9GcHxw+RCAA4JqBi7KM0TW+o4ETBlwdwc=; b=SbzWTHzDXNUc3+1JllY2LduwaHw2pYQjqm5Lrj0jCUb8oaCTTa+ZVlzgDWDWzNs/nEXmzw HPEtJ1qYERgaysK4mh1/CvSW8GAvHkLH46DV0HhOeb/OSzOM4l6a8s4pfL9yltq1ETPcE3 IiC7wMD4ELRp9IeQ/rKSxgq6p/LlPHk= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-161-Fk-tHHypO32GHQFQy55GBw-1; Wed, 29 Jan 2020 21:24:52 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6252D13E2; Thu, 30 Jan 2020 02:24:51 +0000 (UTC) Received: from localhost.localdomain.com (vpn2-54-25.bne.redhat.com [10.64.54.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 29B7710027BA; Thu, 30 Jan 2020 02:24:48 +0000 (UTC) From: Gavin Shan To: linux-mm@kvack.org Cc: aarcange@redhat.com, akpm@linux-foundation.org, mhocko@suse.com Subject: [PATCH 1/3] mm: Rework {enabled,defrag}_store() Date: Thu, 30 Jan 2020 13:24:39 +1100 Message-Id: <20200130022441.95488-2-gshan@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-MC-Unique: Fk-tHHypO32GHQFQy55GBw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: The suffix of the input buffer is truncated in {enabled,defrag}_store(). With that, the illegal settings can be accepted, as the following example shows. # cat /sys/kernel/mm/transparent_hugepage/enabled always [madvise] never # echo neveroff > /sys/kernel/mm/transparent_hugepage/enabled # cat /sys/kernel/mm/transparent_hugepage/enabled always madvise [never] This reworks {enabled,defrag}_store() to fix the issue and make the code a bit more extendible. With this applied, the illegal settings will be rejected: # echo neveroff > /sys/kernel/mm/transparent_hugepage/enabled sh: write error: Invalid argument Signed-off-by: Gavin Shan --- mm/huge_memory.c | 114 ++++++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 52 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index a88093213674..7bd573f1631d 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -160,6 +160,15 @@ static struct shrinker huge_zero_page_shrinker = { }; #ifdef CONFIG_SYSFS +static const char * const enabled_strings[] = { + "always", "madvise", "never" +}; +static unsigned int enabled_bits[] = { + TRANSPARENT_HUGEPAGE_FLAG, + TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, + UINT_MAX +}; + static ssize_t enabled_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -175,29 +184,29 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { - ssize_t ret = count; - - if (!memcmp("always", buf, - min(sizeof("always")-1, count))) { - clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); - } else if (!memcmp("madvise", buf, - min(sizeof("madvise")-1, count))) { - clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); - } else if (!memcmp("never", buf, - min(sizeof("never")-1, count))) { - clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); - } else - ret = -EINVAL; + int i, ret; - if (ret > 0) { - int err = start_stop_khugepaged(); - if (err) - ret = err; + for (i = 0; i < ARRAY_SIZE(enabled_strings); i++) { + if ((count - 1) == strlen(enabled_strings[i]) && + !memcmp(buf, enabled_strings[i], count - 1)) + break; } - return ret; + + if (i >= ARRAY_SIZE(enabled_strings)) + return -EINVAL; + + clear_bit(TRANSPARENT_HUGEPAGE_FLAG, + &transparent_hugepage_flags); + clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, + &transparent_hugepage_flags); + if (enabled_bits[i] != UINT_MAX) + set_bit(enabled_bits[i], &transparent_hugepage_flags); + + ret = start_stop_khugepaged(); + if (ret) + return ret; + + return count; } static struct kobj_attribute enabled_attr = __ATTR(enabled, 0644, enabled_show, enabled_store); @@ -232,6 +241,17 @@ ssize_t single_hugepage_flag_store(struct kobject *kobj, return count; } +static const char * const defrag_strings[] = { + "always", "defer+madvise", "defer", "madvise", "never" +}; +static unsigned int defrag_bits[] = { + TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, + TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, + TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, + TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, + UINT_MAX +}; + static ssize_t defrag_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -250,39 +270,29 @@ static ssize_t defrag_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { - if (!memcmp("always", buf, - min(sizeof("always")-1, count))) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - } else if (!memcmp("defer+madvise", buf, - min(sizeof("defer+madvise")-1, count))) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - } else if (!memcmp("defer", buf, - min(sizeof("defer")-1, count))) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - } else if (!memcmp("madvise", buf, - min(sizeof("madvise")-1, count))) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - } else if (!memcmp("never", buf, - min(sizeof("never")-1, count))) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - } else + int i; + + for (i = 0; i < ARRAY_SIZE(defrag_strings); i++) { + if ((count - 1) == strlen(defrag_strings[i]) && + !memcmp(buf, defrag_strings[i], count - 1)) + break; + } + + if (i >= ARRAY_SIZE(defrag_strings)) return -EINVAL; + clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, + &transparent_hugepage_flags); + clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, + &transparent_hugepage_flags); + clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, + &transparent_hugepage_flags); + clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, + &transparent_hugepage_flags); + + if (defrag_bits[i] != UINT_MAX) + set_bit(defrag_bits[i], &transparent_hugepage_flags); + return count; } static struct kobj_attribute defrag_attr =