scull's Memory Usage

Memory area used by scull, also known as a device, variable length, the more you write, the more it grows;. To be trimmed by using a short document covering device.

 

scull drive core 2 is introduced functions to manage the Linux kernel memory. These functions are defined in

<Linux / slab.h>, is:

 

void *kmalloc(size_t size, int flags); void kfree(void *ptr);

 

Kmalloc the call to attempt to allocate size bytes of memory; the return value is a pointer to a pointer to the memory allocation fails or if the argument is NULL flags should be used to describe how the memory allocation; we see these signs in detail in Chapter 8 for now. we've been using GFP_KERNEL. kfree allocated memory should be used to release. you should never pass anything that is not obtained from kmalloc to kfree. However, passing a NULL pointer to kfree is legal.

 

Kmalloc method is not the most efficient allocation of large memory area (see Chapter 8), so the selection to achieve scull is not a particularly clever. A clever source implementation may be more difficult to read, and the goal of this section is to show read and write , not memory management. this is why the code just use kmalloc and kfree without resorting to full-page allocation, although this approach would be more effective.

 

In the flip side, we do not want to limit the size of the "Device" area, in theory, be applied to arbitrary restrictions always a bad idea in the data items managed reasons on the theory and practice. In practice, scull can be used to temporarily devoured your system memory to run tests under low memory conditions. run such a test may help you understand the internals of the system. you can use the command cp / dev / zero / dev / scull0 to eat with a scull All true RAM, and you can use the dd tool to choose how much data to shell scull device.

 

In The scull, each device is a pointer list, each pointing to a scull_dev structure. Each such structure, default, point up to 4 megabytes. 1000 issue code uses a pointer to an array of pointers through an intermediate an array of pointers each 4000-byte area. we call each region a quantum memory array (or a length) of the subset a. a scull device and its memory area.

 

 

Is selected number, write a single byte in the scull or 12,000 KB of memory consumed 8000: 4000 is the quantum, a quantum 4000 or 8000 is set (represented by a 32-bit or 64-bit pointer on the target platform in accordance with) the opposite If you write a lot of data, list the cost is not too bad. per 4 MB data list only one element, the maximum size of the device is limited by the size of the computer's memory.

 

Selecting the appropriate values ​​for the quantum and quantum set a policy issue, instead of the mechanism, and to optimize the value depends on how the equipment. Accordingly, scull driver should not be forced to the quantum and the subset of any particular value. In scull the user can control to change these values, there are several ways: by changing the compile time scull.h macros

SCULL_QUANTUM and SCULL_QSET in, and setting the integer value scull_quantum scull_qset at module load time, or change the current value and using the ioctl default values ​​at run time.

 

When you use an integer value and macro definitions to compile and load the configuration when, for how to select the number of main memories. We use this technology on the value of any related policy or arbitrary in the drive.

 

The only question remaining is if you choose the default values. In this particular case, the problem is to find the best balance by filling half the quantum and quantum set waste memory, if you assign the case of quantum and quantum set small release and pointer connection overhead caused by additional interior design kmalloc should be taken into account (and now we do not pursue this point, however; internal kmalloc exploration in Chapter 8). select the default value when there may be a lot of data written from hypothesis testing scull, despite the normal use of the device is most likely to transmit only a few KB of data.

 

We have seen our apparatus for representing an internal structure of a quantum structure scull_dev and qset represent quantum devices and the size of the subset of the actual data, however, is a different track structure, we called struct scull_qset..:

 

struct scull_qset { void **data;

struct scull_qset *next;

};

 

The next code fragment shows in practice struct scull_dev struct scull_qset and how it is used to hold data. Sucll_trim function is responsible for freeing the whole data area, called by scull_open when the file is opened for writing. It simply go through the list and release it any quantum and found the subset.

 

int scull_trim(struct scull_dev *dev)

{

struct scull_qset *next, *dptr;

int qset = dev->qset; /* "dev" is not-null */ int i;

for (dptr = dev->data; dptr; dptr = next)

{ /* all the list items */

if (dptr->data) {

for (i = 0; i < qset; i++)

kfree(dptr->data[i]); kfree(dptr->data);

dptr->data = NULL;

}

 

next = dptr->next; kfree(dptr);

}

 

 

 

 

dev->size = 0;

dev->quantum = scull_quantum; dev->qset = scull_qset;

dev->data = NULL; return 0;

}

 

scull_trim also used in cleanup function module to return to the system memory used by scull.

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11106335.html