From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4BBC6C61DD8 for ; Fri, 23 Oct 2020 07:58:34 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 771112192A for ; Fri, 23 Oct 2020 07:58:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oss-volkswagen-com.20150623.gappssmtp.com header.i=@oss-volkswagen-com.20150623.gappssmtp.com header.b="D4A6k5u0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 771112192A Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=oss.volkswagen.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3FB656B005D; Fri, 23 Oct 2020 03:58:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3ACAF6B0062; Fri, 23 Oct 2020 03:58:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 274BD6B0068; Fri, 23 Oct 2020 03:58:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0223.hostedemail.com [216.40.44.223]) by kanga.kvack.org (Postfix) with ESMTP id EF2146B005D for ; Fri, 23 Oct 2020 03:58:31 -0400 (EDT) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 871551EE6 for ; Fri, 23 Oct 2020 07:58:31 +0000 (UTC) X-FDA: 77402438022.08.cream79_090390927257 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin08.hostedemail.com (Postfix) with ESMTP id 651F91819E76B for ; Fri, 23 Oct 2020 07:58:31 +0000 (UTC) X-HE-Tag: cream79_090390927257 X-Filterd-Recvd-Size: 6355 Received: from mail-ed1-f66.google.com (mail-ed1-f66.google.com [209.85.208.66]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Fri, 23 Oct 2020 07:58:30 +0000 (UTC) Received: by mail-ed1-f66.google.com with SMTP id w25so668579edx.2 for ; Fri, 23 Oct 2020 00:58:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss-volkswagen-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=ozaKSQjVGkvoHvy8QV9FaUSvsUWVEbgo2/OW92z4puE=; b=D4A6k5u0N0xMQsH/eDkLFbSFv4YdWTOK5L5YQ/TAtpupAax91+1bzeinauPEd7ePmY a/ek3lixeS5XMIGcJ0r2tkq+9epvYpYN14WDMItqEp/oc0wVJO0jYhmL7n2E+BxPzewg QZPSp204OpQHrk+NDSU4p6GpJHCTLBUzwAwtfNwlhFzJLMqnmvKvb7Ngwfwa/TIPxad8 MMz8bwmVGv4Sx8QfkrFXnGUk41Hcy7qqhFRyKZ6TZXtQr+944Znp+EUYC7MAjDt0v9Ny UP57jdWtWec35EefmEMzzITpnizHo0tAGiQK5SIY37KObKB6e+o09B5G6eTBc4D6oSTT RQ1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ozaKSQjVGkvoHvy8QV9FaUSvsUWVEbgo2/OW92z4puE=; b=rs3P8iDeK9OZ4h00WhsrUPdTkn8wdwTE1dq5QrlnEiEn1F5uvVfwUugqGdz1omIuXW j18Ckuzn8MHxSYitKAZ6xUX9ERHAcZNyUgvj550K6A3j8HYFtM+7Nh1Yq7iqNf37tAet uSuenKGcoitVtP7tFjSBK7FIx855c3serqr/amq4kTyClUij1PGqzzyvNQyTDwWq1FKq TMGz+QZ7l3LdpBFtXZnTygHwBBwLBnURGp1NarTNC4N4SGR4yXVkWRt7I/inHdoYyWRb Sv775pOAhNN/RhZibUhMCJh3nrWnBMjmgWCeFjH2vZj5GMcICfgjqUMF8xi/DK7nSTWE u0Fg== X-Gm-Message-State: AOAM532pSIdmWFt3l+GnWcVOLuUsuLMO2VZbh02AB3/2HOq6VWpa2eY9 BA16qbgaFL3JfOPawcsnrqX20Q== X-Google-Smtp-Source: ABdhPJzGEG2TbPbdDyN11xNgCxXU1gEW6IIucRL2/YW5h2+eWRdD6XpecxLCCAFsi97PdnZUgqE2Kg== X-Received: by 2002:a05:6402:1684:: with SMTP id a4mr1037690edv.319.1603439909437; Fri, 23 Oct 2020 00:58:29 -0700 (PDT) Received: from fixcw2y-20469.fritz.box (200116b846b51600a9187f632e308bc5.dip.versatel-1u1.de. [2001:16b8:46b5:1600:a918:7f63:2e30:8bc5]) by smtp.gmail.com with ESMTPSA id f7sm363861ejz.23.2020.10.23.00.58.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Oct 2020 00:58:28 -0700 (PDT) From: Laurent Cremmer To: Mike Kravetz Cc: Andrew Morton , Mina Almasry , David Rientjes , linux-mm@kvack.org, Shuah Khan , Laurent Cremmer Subject: [PATCH] hugetlb: fix locking in region_add,region_cgh,allocate_file_region_entries Date: Fri, 23 Oct 2020 09:47:59 +0200 Message-Id: <20201023074759.46605-1-laurent@oss.volkswagen.com> X-Mailer: git-send-email 2.17.1 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: commit 0db9d74ed884 ("hugetlb: disable region_add file_region coalescing") introduced issues with regards to locking: - Missing spin_unlock in hugetlb.c:region_add and hugetlb.c:region_cgh when returning after an error. - Missing spin lock in hugetlb.c:allocate_file_region_entries when returning after an error. The first two errors were spotted using the coccinelle static code analysis tool while focusing on the mini_lock.cocci script, whose goal is to find missing unlocks. make coccicheck mode=REPORT m=mm/hugetlb.c mm/hugetlb.c:514:3-9: preceding lock on line 487 mm/hugetlb.c:564:2-8: preceding lock on line 554 The third instance spotted by manual examination. In static long region_add(...) and static long region_cgh(...) , releasing the acquired lock when exiting via their error path was removed. This will cause these functions to return with a lock held if they do not succeed. This patch reintroduces the original error path making sure the lock is properly released on all exits. A a new function allocate_file_region_entries was also introduced that must be called with a lock held and returned with the lock held. However the lock is temporarily released during processing but will not be reacquired on error. This patch ensures that the lock will be reacquired in the error path also. Fixes: 0db9d74ed884 ("hugetlb: disable region_add file_region coalescing") Link: https://lists.elisa.tech/g/development-process/message/289 Signed-off-by: Laurent Cremmer Reviewed-by: Oliver Hartkopp --- mm/hugetlb.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index fe76f8fd5a73..92bea6f77361 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -438,7 +438,7 @@ static int allocate_file_region_entries(struct resv_map *resv, for (i = 0; i < to_allocate; i++) { trg = kmalloc(sizeof(*trg), GFP_KERNEL); if (!trg) - goto out_of_memory; + goto out_of_memory_unlocked; list_add(&trg->link, &allocated_regions); } @@ -450,7 +450,8 @@ static int allocate_file_region_entries(struct resv_map *resv, return 0; -out_of_memory: +out_of_memory_unlocked: + spin_lock(&resv->lock); list_for_each_entry_safe(rg, trg, &allocated_regions, link) { list_del(&rg->link); kfree(rg); @@ -508,7 +509,8 @@ static long region_add(struct resv_map *resv, long f, long t, if (allocate_file_region_entries( resv, actual_regions_needed - in_regions_needed)) { - return -ENOMEM; + add = -ENOMEM; + goto out_locked; } goto retry; @@ -517,7 +519,7 @@ static long region_add(struct resv_map *resv, long f, long t, add = add_reservation_in_range(resv, f, t, h_cg, h, NULL); resv->adds_in_progress -= in_regions_needed; - +out_locked: spin_unlock(&resv->lock); VM_BUG_ON(add < 0); return add; @@ -557,11 +559,14 @@ static long region_chg(struct resv_map *resv, long f, long t, if (*out_regions_needed == 0) *out_regions_needed = 1; - if (allocate_file_region_entries(resv, *out_regions_needed)) - return -ENOMEM; + if (allocate_file_region_entries(resv, *out_regions_needed)) { + chg = -ENOMEM; + goto out_locked; + } resv->adds_in_progress += *out_regions_needed; +out_locked: spin_unlock(&resv->lock); return chg; } -- 2.17.1