From patchwork Tue Oct 23 21:35:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10653807 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BEC7613A9 for ; Tue, 23 Oct 2018 21:39:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ABA642A426 for ; Tue, 23 Oct 2018 21:39:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9FBCC2A4FD; Tue, 23 Oct 2018 21:39:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id BC2F82A426 for ; Tue, 23 Oct 2018 21:39:46 +0000 (UTC) Received: (qmail 16009 invoked by uid 550); 23 Oct 2018 21:36:32 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 15863 invoked from network); 23 Oct 2018 21:36:32 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to; bh=8LbEKOKWe/FiMlUHhmCc7voGh1W3EYnicEfE4r2R4yA=; b=N8qUCIMyIvLATq4CE5wE0uPw31UaGBnKbzEM90Hb/Wj5TI1WQujrJdzbDP0qxRobgM ZCoMs/0ZzA4kG0cqvysWLv8m1q0Btspo1GbzYx1YZcfwsBvBLB347lafqVcLjNpuD7uh xNIhdaCGy21A02rCV1tCtVf8Qt9OVwhnyCylb8Rw+Qqs4nwLso7hSHPYpkPb2fncfVfs LOb4TQscLGzNtufTiVOunV6qW8ZGAQJeeXOSVZhjwVyf+ji2DRIiXWzgsXSlLdIPe+bb VhYGtK5SvxmDKoY1xlMKrg9FwOYZUbMyGD6luSZZPCqmzYSTidxTHU/+LpYhnSh1CdwT PDtQ== 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:in-reply-to :references:reply-to; bh=8LbEKOKWe/FiMlUHhmCc7voGh1W3EYnicEfE4r2R4yA=; b=tovlIyL3mGv2OOER4YVrX6RLF9iLqVhCAwCajdCpERWWOEMlKZYDlugCZGOKfFqBp7 Jpoi3s/1T3U1whOqTeVxfynWhjWdzeRGSwF2pPLlCKdOhu/At0QKTbYBMt4RuUBPiIZD J2BdqOEN8nnd2XPklqQEblOY0DfLfNTT7mmjd6OYb3OAYKlq4BUC75eTVmQvPW/lOiOv tZ4KX4829nPmePWAl2uIBv51GkTc/rxcq3HaXeIjnJO3tW3k+aOkAaUS3fFW2TbEScX7 4vGPQoNdoIQIL93Fkkxx52DSGqXL3RoWPT/9vYRyPBehe1VOQJKDzZrvQOizMPLsDANM FXhg== X-Gm-Message-State: ABuFfojCAnsqbcehOJsqC+7sC7VmNTp1Y3P6UXp1lNmyhZnDXG8SAh00 oVxXi8WO4eo9HNBcek7OHVc= X-Google-Smtp-Source: ACcGV60VSV41bEasnudRwceqLHq3cQn21dkmbWSOYeDgxo5mc3i8TMNkXsnfHZ82wYQ9h6vmAtB3yg== X-Received: by 2002:a2e:80d2:: with SMTP id r18-v6mr37260197ljg.100.1540330580535; Tue, 23 Oct 2018 14:36:20 -0700 (PDT) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Mimi Zohar , Kees Cook , Matthew Wilcox , Dave Chinner , James Morris , Michal Hocko , kernel-hardening@lists.openwall.com, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org Cc: igor.stoppa@huawei.com, Dave Hansen , Jonathan Corbet , Laura Abbott , Will Deacon , Peter Zijlstra , Boqun Feng , Arnd Bergmann , linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 16/17] prmem: pratomic-long Date: Wed, 24 Oct 2018 00:35:03 +0300 Message-Id: <20181023213504.28905-17-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181023213504.28905-1-igor.stoppa@huawei.com> References: <20181023213504.28905-1-igor.stoppa@huawei.com> X-Virus-Scanned: ClamAV using ClamSMTP Minimalistic functionality for having the write rare version of atomic_long_t data. Signed-off-by: Igor Stoppa CC: Will Deacon CC: Peter Zijlstra CC: Boqun Feng CC: Arnd Bergmann CC: linux-arch@vger.kernel.org CC: linux-kernel@vger.kernel.org --- MAINTAINERS | 1 + include/linux/pratomic-long.h | 73 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 include/linux/pratomic-long.h diff --git a/MAINTAINERS b/MAINTAINERS index e7f7cb1682a6..9d72688d00a3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -9466,6 +9466,7 @@ F: mm/test_pmalloc.c F: Documentation/core-api/prmem.rst F: include/linux/prlist.h F: lib/test_prlist.c +F: include/linux/pratomic-long.h MEMORY MANAGEMENT L: linux-mm@kvack.org diff --git a/include/linux/pratomic-long.h b/include/linux/pratomic-long.h new file mode 100644 index 000000000000..8f1408593733 --- /dev/null +++ b/include/linux/pratomic-long.h @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Atomic operations for write rare memory */ +#ifndef _LINUX_PRATOMIC_LONG_H +#define _LINUX_PRATOMIC_LONG_H +#include +#include +#include + +struct pratomic_long_t { + atomic_long_t l __aligned(sizeof(atomic_long_t)); +} __aligned(sizeof(atomic_long_t)); + +#define PRATOMIC_LONG_INIT(i) { \ + .l = ATOMIC_LONG_INIT((i)), \ +} + +static __always_inline +bool __pratomic_long_op(bool inc, struct pratomic_long_t *l) +{ + struct page *page; + uintptr_t base; + uintptr_t offset; + unsigned long flags; + size_t size = sizeof(*l); + bool is_virt = __is_wr_after_init(l, size); + + if (WARN(!(is_virt || likely(__is_wr_pool(l, size))), + WR_ERR_RANGE_MSG)) + return false; + local_irq_save(flags); + if (is_virt) + page = virt_to_page(l); + else + vmalloc_to_page(l); + offset = (~PAGE_MASK) & (uintptr_t)l; + base = (uintptr_t)vmap(&page, 1, VM_MAP, PAGE_KERNEL); + if (WARN(!base, WR_ERR_PAGE_MSG)) { + local_irq_restore(flags); + return false; + } + if (inc) + atomic_long_inc((atomic_long_t *)(base + offset)); + else + atomic_long_dec((atomic_long_t *)(base + offset)); + vunmap((void *)base); + local_irq_restore(flags); + return true; + +} + +/** + * pratomic_long_inc - atomic increment of rare write long + * @l: address of the variable of type struct pratomic_long_t + * + * Return: true on success, false otherwise + */ +static __always_inline bool pratomic_long_inc(struct pratomic_long_t *l) +{ + return __pratomic_long_op(true, l); +} + +/** + * pratomic_long_inc - atomic decrement of rare write long + * @l: address of the variable of type struct pratomic_long_t + * + * Return: true on success, false otherwise + */ +static __always_inline bool pratomic_long_dec(struct pratomic_long_t *l) +{ + return __pratomic_long_op(false, l); +} + +#endif