From patchwork Wed Nov 27 14:21:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Wool X-Patchwork-Id: 11264171 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 604976C1 for ; Wed, 27 Nov 2019 14:21:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2C4E8207DD for ; Wed, 27 Nov 2019 14:21:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ftBPOest" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2C4E8207DD Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 516226B03C1; Wed, 27 Nov 2019 09:21:22 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4ED176B0495; Wed, 27 Nov 2019 09:21:22 -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 403B16B0496; Wed, 27 Nov 2019 09:21:22 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0050.hostedemail.com [216.40.44.50]) by kanga.kvack.org (Postfix) with ESMTP id 2A7786B03C1 for ; Wed, 27 Nov 2019 09:21:22 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with SMTP id E1A0A3ABF for ; Wed, 27 Nov 2019 14:21:21 +0000 (UTC) X-FDA: 76202269962.28.table92_46118f0700c31 X-Spam-Summary: 2,0,0,4fd12a4d7f73c4ea,d41d8cd98f00b204,vitalywool@gmail.com,::linux-kernel@vger.kernel.org:akpm@linux-foundation.org,RULES_HIT:41:355:379:541:800:960:965:966:973:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:3138:3139:3140:3141:3142:3352:3865:3867:3868:3871:3874:4250:4321:4385:4390:4395:5007:6261:6653:7558:8540:9413:10004:10400:11026:11658:11914:12043:12297:12438:12517:12519:12555:12760:12895:12986:13069:13161:13229:13311:13357:13439:14181:14394:14659:14687:14721:21080:21444:21451:21627:21666:30054,0,RBL:209.85.167.67:@gmail.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: table92_46118f0700c31 X-Filterd-Recvd-Size: 4497 Received: from mail-lf1-f67.google.com (mail-lf1-f67.google.com [209.85.167.67]) by imf03.hostedemail.com (Postfix) with ESMTP for ; Wed, 27 Nov 2019 14:21:21 +0000 (UTC) Received: by mail-lf1-f67.google.com with SMTP id m30so15460956lfp.8 for ; Wed, 27 Nov 2019 06:21:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=khh+69lU2KetWdK6CIdAngBwxv5Htih9yoOaJIYwrW8=; b=ftBPOestA1Q2O3T/wJCQP91u9a+2dv+PtGe5TXzBmISBuZ5b8P2VlrypUP01v211oP nl3iaJhqv6FvwSmNQPqJB7Txw2QNsCmzmV35nFVmwl5MDicV0eROeLjyUVEaXkOsR+h2 f45wdasQw+eaFsW9GSQ61qqDV3WDk9B8XPgf2KN95w/QiBxI2yNHKug4xluvp2YsajaP TFJj/kYr1CZhukiFlbAhwZ6vINcB/o/2cUPDUIaiDQzU7o1rMcgtLOwC3O6aPqsbWtz2 28X6v0bwEgr4umyZPKKYStMmM8HGY9mcGC71AtaTu2tKWTi6M5COaYz0MXZbOWmryi7P aU+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=khh+69lU2KetWdK6CIdAngBwxv5Htih9yoOaJIYwrW8=; b=R4gQNOrTVLjF7H+3efAey5saSLS+DhEdZo0jZAzjxRvTAn3WFY8Eb/pK44fWOmTuK3 YNoCMGgPPY7qXGBUbzUCJ8zmU3TmJxd9HUJZVjfdgv5FXLYFzjGP0oL+j64fDN4SDMxY 8MmjCj9dZkxCbEg6CnqZwQIkXnZ7sfcbcQeMFS9rVACj0bmRZ07W7puMakHow0Hs9o+q c99EXWo4WFYZ2llBA918l2f66wqaXvas/RAMUBjqeC/wqSUURTN5K2Ib3Dw+ANSqZUGh Rm5CQoJQxIhhc13EXFNWst9MFCDgqQQV7L4kpOjg4KD2YAL/xbOxKiNeDU3EHFF/vJRH s6IQ== X-Gm-Message-State: APjAAAVRlaQ9ditkEG2LlM9JXmMReAK3I/chKGhLe2AlAO5nLmRAXGSp XjgfZagluSZv73MIgebvIkDlJpJ1 X-Google-Smtp-Source: APXvYqz42pCtuhbTJGlQld5BLHIqvNKuF+iMyZlJdqMynm4sPrUp72H7xXc79jeihCejanakTg3SIQ== X-Received: by 2002:a19:c10f:: with SMTP id r15mr20815139lff.172.1574864479752; Wed, 27 Nov 2019 06:21:19 -0800 (PST) Received: from seldlx21914.corpusers.net ([37.139.156.40]) by smtp.gmail.com with ESMTPSA id c20sm1879200ljj.55.2019.11.27.06.21.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Nov 2019 06:21:18 -0800 (PST) Date: Wed, 27 Nov 2019 15:21:18 +0100 From: Vitaly Wool To: , linux-kernel@vger.kernel.org Cc: Andrew Morton Subject: [PATCH 1/3] z3fold: avoid subtle race when freeing slots Message-Id: <20191127152118.6314b99074b0626d4c5a8835@gmail.com> In-Reply-To: <20191127152012.17a4b35f9e7f6e50f9aaca9c@gmail.com> References: <20191127152012.17a4b35f9e7f6e50f9aaca9c@gmail.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; x86_64-unknown-linux-gnu) Mime-Version: 1.0 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: There is a subtle race between freeing slots and setting the last slot to zero since the OPRPHANED flag was set after the rwlock had been released. Fix that to avoid rare memory leaks caused by this race. Signed-off-by: Vitaly Wool --- mm/z3fold.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/z3fold.c b/mm/z3fold.c index d48d0ec3bcdd..36bd2612f609 100644 --- a/mm/z3fold.c +++ b/mm/z3fold.c @@ -327,6 +327,10 @@ static inline void free_handle(unsigned long handle) zhdr->foreign_handles--; is_free = true; read_lock(&slots->lock); + if (!test_bit(HANDLES_ORPHANED, &slots->pool)) { + read_unlock(&slots->lock); + return; + } for (i = 0; i <= BUDDY_MASK; i++) { if (slots->slot[i]) { is_free = false; @@ -335,7 +339,7 @@ static inline void free_handle(unsigned long handle) } read_unlock(&slots->lock); - if (is_free && test_and_clear_bit(HANDLES_ORPHANED, &slots->pool)) { + if (is_free) { struct z3fold_pool *pool = slots_to_pool(slots); kmem_cache_free(pool->c_handle, slots); @@ -531,12 +535,12 @@ static void __release_z3fold_page(struct z3fold_header *zhdr, bool locked) break; } } + if (!is_free) + set_bit(HANDLES_ORPHANED, &zhdr->slots->pool); read_unlock(&zhdr->slots->lock); if (is_free) kmem_cache_free(pool->c_handle, zhdr->slots); - else - set_bit(HANDLES_ORPHANED, &zhdr->slots->pool); if (locked) z3fold_page_unlock(zhdr); From patchwork Wed Nov 27 14:22:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Wool X-Patchwork-Id: 11264175 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 D88C91390 for ; Wed, 27 Nov 2019 14:22:21 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9823B20674 for ; Wed, 27 Nov 2019 14:22:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LiD7wVli" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9823B20674 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DC7DC6B0497; Wed, 27 Nov 2019 09:22:20 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D78026B0498; Wed, 27 Nov 2019 09:22:20 -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 C8D4A6B0499; Wed, 27 Nov 2019 09:22:20 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0021.hostedemail.com [216.40.44.21]) by kanga.kvack.org (Postfix) with ESMTP id B353C6B0497 for ; Wed, 27 Nov 2019 09:22:20 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with SMTP id 5F2CE181AEF10 for ; Wed, 27 Nov 2019 14:22:20 +0000 (UTC) X-FDA: 76202272440.28.love24_4e8e4958d3219 X-Spam-Summary: 2,0,0,552405f359067152,d41d8cd98f00b204,vitalywool@gmail.com,::linux-kernel@vger.kernel.org:akpm@linux-foundation.org,RULES_HIT:41:355:379:541:800:960:966:973:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1535:1543:1593:1594:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2693:2897:3138:3139:3140:3141:3142:3354:3865:3867:3868:3870:3871:3872:3874:4117:4321:4385:5007:6261:6653:7558:7903:8603:8957:9413:10004:10400:10450:10455:11026:11473:11658:11914:12043:12296:12297:12438:12517:12519:12555:12760:12895:12986:13161:13229:13439:14096:14097:14181:14394:14659:14687:14721:19904:19999:21080:21444:21451:21627:21666:30054,0,RBL:209.85.167.65:@gmail.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: love24_4e8e4958d3219 X-Filterd-Recvd-Size: 6117 Received: from mail-lf1-f65.google.com (mail-lf1-f65.google.com [209.85.167.65]) by imf27.hostedemail.com (Postfix) with ESMTP for ; Wed, 27 Nov 2019 14:22:19 +0000 (UTC) Received: by mail-lf1-f65.google.com with SMTP id 203so17283909lfa.12 for ; Wed, 27 Nov 2019 06:22:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1ueKGNrZq9k5CAhuDKF0UemrCbj1DCqBbGnBf2zTeN8=; b=LiD7wVliol4JBaFNF5iQsLBWwl2fPcg4c0y+Sanhx3VYe3K1c9XiMQGBdJhwRicVkC drsVbAmx3ccSsK+EMri63sqmqTtDq1BzSMbB37X0vg4cGT4AA2UMN9y3iEdNdCauseoe W9ZcP3hdjy1DLrQzXLCitdqOu+3KPX38dU3zkK/IFCXjEVEd3kg8Lhehs+zGgvdbuZZI F1tohcfdEkBCWYN74A0rjMH5daupj2rAeavcgKrpUcJqH62BqpXIm/KRiTpDyBFDAwjo 1+ITbTDY4LQYwd/fZbZJwrneofyYAbo9OAueDrPgIcdlH5iyYmhV4cpoAKcebOLKELfk d+Ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1ueKGNrZq9k5CAhuDKF0UemrCbj1DCqBbGnBf2zTeN8=; b=Vk5Q3beE4tWlfDiV1JUZjM9ysli6NprKMP+9mwvC0w2+8w3lr7MZuZH14G9wcSSO9/ 2iWPqeVj08wMII9s6z/fF1ERglFeU3HpD/78s18oqroBbjD/JrlD8R3m8WqbF9550jwr c2wTFX2rDQY8GD6xnN0j55r7r7k4dGtUOY+jkPspEVND4nR3eRerxL+gTqv+RPvpv3kg jVAYwtXPrx3ONXQq6KizlY+t2XCaCaMWrCJEqJCqxXRajRvd2GoPJi9YMK4fOM3H5rAB fWEIoeccfhaTl7W8eVhJFC2pI3n82nFExwXTC+OsKSoZNhkuW0qYwdmEqsR832bsGtZA FJLw== X-Gm-Message-State: APjAAAUXSq5ZcdKh4gaDRMrd6zLhMvfplOb2lI/4M+dSpWzgr0RehkQ/ /waKi0W/YIk+v75H3CTe3t7g7AsD X-Google-Smtp-Source: APXvYqxoDGM2pTjkXU7EFq31NdlZMDDL+QXhB5NtEwqztyorARwYt7dKhqs0xkXQ+f7uB2oxBnZ6hQ== X-Received: by 2002:ac2:51b5:: with SMTP id f21mr28773344lfk.159.1574864538094; Wed, 27 Nov 2019 06:22:18 -0800 (PST) Received: from seldlx21914.corpusers.net ([37.139.156.40]) by smtp.gmail.com with ESMTPSA id x9sm7032158lfn.21.2019.11.27.06.22.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Nov 2019 06:22:17 -0800 (PST) Date: Wed, 27 Nov 2019 15:22:16 +0100 From: Vitaly Wool To: , linux-kernel@vger.kernel.org Cc: Andrew Morton Subject: [PATCH 2/3] z3fold: compact objects more accurately Message-Id: <20191127152216.6ad33745a21ba71c53606acb@gmail.com> In-Reply-To: <20191127152012.17a4b35f9e7f6e50f9aaca9c@gmail.com> References: <20191127152012.17a4b35f9e7f6e50f9aaca9c@gmail.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; x86_64-unknown-linux-gnu) Mime-Version: 1.0 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: There are several small things to be considered regarding the new inter-page compaction mechanism. First, we better set the relevant size in chunks to 0 in the old z3fold header for the object that has been moved to another z3fold page. Then, we shouldn't do inter-page compaction if an object is mapped. Lastly, free_handle should happen before release_z3fold_page (but not in case the page is under reclaim, it will the handle will be freed by reclaim then). This patch addresses all three issues. Signed-off-by: Vitaly Wool --- mm/z3fold.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/z3fold.c b/mm/z3fold.c index 36bd2612f609..f2a75418e248 100644 --- a/mm/z3fold.c +++ b/mm/z3fold.c @@ -670,6 +670,7 @@ static struct z3fold_header *compact_single_buddy(struct z3fold_header *zhdr) int first_idx = __idx(zhdr, FIRST); int middle_idx = __idx(zhdr, MIDDLE); int last_idx = __idx(zhdr, LAST); + unsigned short *moved_chunks = NULL; /* * No need to protect slots here -- all the slots are "local" and @@ -679,14 +680,17 @@ static struct z3fold_header *compact_single_buddy(struct z3fold_header *zhdr) p += ZHDR_SIZE_ALIGNED; sz = zhdr->first_chunks << CHUNK_SHIFT; old_handle = (unsigned long)&zhdr->slots->slot[first_idx]; + moved_chunks = &zhdr->first_chunks; } else if (zhdr->middle_chunks && zhdr->slots->slot[middle_idx]) { p += zhdr->start_middle << CHUNK_SHIFT; sz = zhdr->middle_chunks << CHUNK_SHIFT; old_handle = (unsigned long)&zhdr->slots->slot[middle_idx]; + moved_chunks = &zhdr->middle_chunks; } else if (zhdr->last_chunks && zhdr->slots->slot[last_idx]) { p += PAGE_SIZE - (zhdr->last_chunks << CHUNK_SHIFT); sz = zhdr->last_chunks << CHUNK_SHIFT; old_handle = (unsigned long)&zhdr->slots->slot[last_idx]; + moved_chunks = &zhdr->last_chunks; } if (sz > 0) { @@ -743,6 +747,8 @@ static struct z3fold_header *compact_single_buddy(struct z3fold_header *zhdr) write_unlock(&zhdr->slots->lock); add_to_unbuddied(pool, new_zhdr); z3fold_page_unlock(new_zhdr); + + *moved_chunks = 0; } return new_zhdr; @@ -840,7 +846,7 @@ static void do_compact_page(struct z3fold_header *zhdr, bool locked) } if (!zhdr->foreign_handles && buddy_single(zhdr) && - compact_single_buddy(zhdr)) { + zhdr->mapped_count == 0 && compact_single_buddy(zhdr)) { if (kref_put(&zhdr->refcount, release_z3fold_page_locked)) atomic64_dec(&pool->pages_nr); else @@ -1254,6 +1260,8 @@ static void z3fold_free(struct z3fold_pool *pool, unsigned long handle) return; } + if (!page_claimed) + free_handle(handle); if (kref_put(&zhdr->refcount, release_z3fold_page_locked_list)) { atomic64_dec(&pool->pages_nr); return; @@ -1263,7 +1271,6 @@ static void z3fold_free(struct z3fold_pool *pool, unsigned long handle) z3fold_page_unlock(zhdr); return; } - free_handle(handle); if (unlikely(PageIsolated(page)) || test_and_set_bit(NEEDS_COMPACTING, &page->private)) { put_z3fold_header(zhdr); From patchwork Wed Nov 27 14:23:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Wool X-Patchwork-Id: 11264193 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 9271F6C1 for ; Wed, 27 Nov 2019 14:23:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 549572064B for ; Wed, 27 Nov 2019 14:23:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NVsRjIUc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 549572064B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 75D4E6B04A4; Wed, 27 Nov 2019 09:23:49 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 70DB06B04A5; Wed, 27 Nov 2019 09:23:49 -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 64C446B04A6; Wed, 27 Nov 2019 09:23:49 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0124.hostedemail.com [216.40.44.124]) by kanga.kvack.org (Postfix) with ESMTP id 4EB576B04A4 for ; Wed, 27 Nov 2019 09:23:49 -0500 (EST) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with SMTP id C9C712497 for ; Wed, 27 Nov 2019 14:23:48 +0000 (UTC) X-FDA: 76202276136.17.toes68_5b6dbea8e3e5a X-Spam-Summary: 2,0,0,cff3549f95ae7d46,d41d8cd98f00b204,vitalywool@gmail.com,::linux-kernel@vger.kernel.org:akpm@linux-foundation.org,RULES_HIT:41:355:379:541:800:960:966:973:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:3138:3139:3140:3141:3142:3352:3867:3874:4250:4321:4385:5007:6117:6261:6653:7558:7875:7903:9036:9413:10004:10400:11026:11658:11914:12297:12517:12519:12555:12760:12895:12986:13069:13311:13357:13439:14181:14394:14659:14687:14721:21080:21094:21323:21444:21451:21627:21666:30054,0,RBL:209.85.167.67:@gmail.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:2,LUA_SUMMARY:none X-HE-Tag: toes68_5b6dbea8e3e5a X-Filterd-Recvd-Size: 4428 Received: from mail-lf1-f67.google.com (mail-lf1-f67.google.com [209.85.167.67]) by imf41.hostedemail.com (Postfix) with ESMTP for ; Wed, 27 Nov 2019 14:23:48 +0000 (UTC) Received: by mail-lf1-f67.google.com with SMTP id 203so17288375lfa.12 for ; Wed, 27 Nov 2019 06:23:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NQAc09iOSs+OUg5bzPj2H1rBE2GvXaGtHzNxY2vVFm8=; b=NVsRjIUcwILPd1tnDv3S17qPH2Rc8pdEr06OlwognC5L1wYvuuWE2VmFAVum6Qb1sC lmVgIpOLWlVMCndd202EatqCqX9bWwKuQpuknmnYAvNBZuRM0nfXZWN+zzgPxEDxXtNP jo3HcmJ7bYOf3vgkcdZcmnDKCfYRUit71JHPUh0YjkuskgVPvkpJtC7gGesDIl5k/ASd TnVlq3uYhRek3JP6dVNkz96AweylBNW4uXW04xnsmUiSETC69B8fHHkaoIdGKO35hm8Y 2kalCjn+HqYa1IEXmMSEhDzdLjntmK6J3oLzeaoyaS1e+VSazkpj5LuLvCDr9RKRbaqp qWhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NQAc09iOSs+OUg5bzPj2H1rBE2GvXaGtHzNxY2vVFm8=; b=QkeHHJILpbG0okA2AIykGB8dL5ogOY9KVyotiI4xT0deheAKn1HhzbXJK99I7J2XwB Vynqc/P6QAeQwnGw+irFSCr34U2CWTpgfDtKt8yw6ozsGga7i2hhL51ygMEJOEoi4Wz5 ZcgghUNL55KkFhWnpPqHhNVxIeY99Ab2nBeYeX4n4fJTkL6hMbKH8HdGGJhP7tVBrijU Kv3+BRbjREvdo308tlDbu9G4dPdHZ3xEBLLkgnr2gHCU22yvg9SuLZ1D3BFOKX2p1RnG lHjYbns9u6tj3QO5S5VIcx4BSdTX+Hz1YhnAA6c7ax37yTqkuNpMeGP9ivJXMhNP6zg0 6QbA== X-Gm-Message-State: APjAAAWmeFXmLBvLK+hisKSgT7wV08r5bmGwF9oiIDntRiKg0sjR+Iip CQruFKqOcVbbIfxUx7vNHXpGMMQp X-Google-Smtp-Source: APXvYqzhLx4HT6/iHXYY/lRbgzvswtQ8BpAwyny4yrxeVKhWIvSHhwsbr3b6fJNLMHfbnSj4C4QlJQ== X-Received: by 2002:ac2:4c82:: with SMTP id d2mr15617326lfl.62.1574864626598; Wed, 27 Nov 2019 06:23:46 -0800 (PST) Received: from seldlx21914.corpusers.net ([37.139.156.40]) by smtp.gmail.com with ESMTPSA id t8sm6975946lfl.51.2019.11.27.06.23.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Nov 2019 06:23:46 -0800 (PST) Date: Wed, 27 Nov 2019 15:23:45 +0100 From: Vitaly Wool To: , linux-kernel@vger.kernel.org Cc: Andrew Morton Subject: [PATCH 3/3] z3fold: protect handle reads Message-Id: <20191127152345.8059852f60947686674d726d@gmail.com> In-Reply-To: <20191127152012.17a4b35f9e7f6e50f9aaca9c@gmail.com> References: <20191127152012.17a4b35f9e7f6e50f9aaca9c@gmail.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; x86_64-unknown-linux-gnu) Mime-Version: 1.0 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: We need to make sure slots are protected on reading a handle. Since handles are modified by free_handle() which only takes slot rwlock, that lock should be used when handles are read. Signed-off-by: Vitaly Wool --- mm/z3fold.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/z3fold.c b/mm/z3fold.c index f2a75418e248..43754d8ebce8 100644 --- a/mm/z3fold.c +++ b/mm/z3fold.c @@ -486,8 +486,12 @@ static unsigned long encode_handle(struct z3fold_header *zhdr, enum buddy bud) /* only for LAST bud, returns zero otherwise */ static unsigned short handle_to_chunks(unsigned long handle) { - unsigned long addr = *(unsigned long *)handle; + struct z3fold_buddy_slots *slots = handle_to_slots(handle); + unsigned long addr; + read_lock(&slots->lock); + addr = *(unsigned long *)handle; + read_unlock(&slots->lock); return (addr & ~PAGE_MASK) >> BUDDY_SHIFT; } @@ -499,10 +503,13 @@ static unsigned short handle_to_chunks(unsigned long handle) static enum buddy handle_to_buddy(unsigned long handle) { struct z3fold_header *zhdr; + struct z3fold_buddy_slots *slots = handle_to_slots(handle); unsigned long addr; + read_lock(&slots->lock); WARN_ON(handle & (1 << PAGE_HEADLESS)); addr = *(unsigned long *)handle; + read_unlock(&slots->lock); zhdr = (struct z3fold_header *)(addr & PAGE_MASK); return (addr - zhdr->first_num) & BUDDY_MASK; }